ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/applications/nanoparticleBuilder/nanoparticleBuilder.cpp
(Generate patch)

Comparing trunk/src/applications/nanoparticleBuilder/nanoparticleBuilder.cpp (file contents):
Revision 1070 by gezelter, Sat Oct 14 20:21:26 2006 UTC vs.
Revision 1879 by gezelter, Sun Jun 16 15:15:42 2013 UTC

# Line 6 | Line 6
6   * redistribute this software in source and binary code form, provided
7   * that the following conditions are met:
8   *
9 < * 1. Acknowledgement of the program authors must be made in any
10 < *    publication of scientific results based in part on use of the
11 < *    program.  An acceptable form of acknowledgement is citation of
12 < *    the article in which the program was described (Matthew
13 < *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14 < *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15 < *    Parallel Simulation Engine for Molecular Dynamics,"
16 < *    J. Comput. Chem. 26, pp. 252-271 (2005))
17 < *
18 < * 2. Redistributions of source code must retain the above copyright
9 > * 1. Redistributions of source code must retain the above copyright
10   *    notice, this list of conditions and the following disclaimer.
11   *
12 < * 3. Redistributions in binary form must reproduce the above copyright
12 > * 2. Redistributions in binary form must reproduce the above copyright
13   *    notice, this list of conditions and the following disclaimer in the
14   *    documentation and/or other materials provided with the
15   *    distribution.
# Line 37 | Line 28
28   * arising out of the use of or inability to use software, even if the
29   * University of Notre Dame has been advised of the possibility of
30   * such damages.
31 + *
32 + * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your
33 + * research, please cite the appropriate papers when you publish your
34 + * work.  Good starting points are:
35 + *                                                                      
36 + * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).            
37 + * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).          
38 + * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).          
39 + * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010).
40 + * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41   */
42  
43   #include <cstdlib>
# Line 64 | Line 65 | using namespace std;
65   #include "utils/StringUtils.hpp"
66  
67   using namespace std;
68 < using namespace oopse;
68 > using namespace OpenMD;
69   void createMdFile(const std::string&oldMdFileName,
70                    const std::string&newMdFileName,
71                    std::vector<int> numMol);
72  
73   int main(int argc, char *argv []) {
74    
74  //register force fields
75  registerForceFields();
75    registerLattice();
76    
77    gengetopt_args_info args_info;
78    std::string latticeType;
79    std::string inputFileName;
80    std::string outputFileName;
82
83  Lattice *simpleLat;
81    MoLocator* locator;
85  int* numMol;
82    int nComponents;
83    double latticeConstant;
84 <  std::vector<double> lc;
89 <  double mass;                                                                          
90 <  const double rhoConvertConst = 1.661;
91 <  double density;
92 <  double particleRadius;
93 <
84 >  RealType particleRadius;
85    Mat3x3d hmat;
95  std::vector<Vector3d> latticePos;
96  std::vector<Vector3d> latticeOrt;
97  int numMolPerCell;
98  int nShells; /* Number of shells in nanoparticle*/
99
86    DumpWriter *writer;
87    
88    // Parse Command Line Arguments
# Line 110 | Line 96 | int main(int argc, char *argv []) {
96    if (args_info.inputs_num)
97      inputFileName = args_info.inputs[0];
98    else {
99 <      sprintf(painCave.errMsg, "No input .md file name was specified"
100 <              "on the command line");
99 >    sprintf(painCave.errMsg, "No input .md file name was specified "
100 >            "on the command line");
101      painCave.isFatal = 1;
102      cmdline_parser_print_help();
103      simError();
# Line 121 | Line 107 | int main(int argc, char *argv []) {
107    SimCreator oldCreator;
108    SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
109    
110 <  latticeConstant = args_info.latticeCnst_arg;
110 >  latticeConstant = args_info.latticeConstant_arg;
111    particleRadius = args_info.radius_arg;
112    Globals* simParams = oldInfo->getSimParams();
113    
# Line 133 | Line 119 | int main(int argc, char *argv []) {
119    vector<Vector3d> sites = nanoParticle.getSites();
120    vector<Vector3d> orientations = nanoParticle.getOrientations();
121  
136  std::cout <<"nSites: " << sites.size() << std::endl;
122  
123 +  std::vector<int> vacancyTargets;
124 +  vector<bool> isVacancy;
125 +  
126 +  Vector3d myLoc;
127 +  RealType myR;
128 +
129 +  for (unsigned int i = 0; i < sites.size(); i++)
130 +    isVacancy.push_back(false);
131 +
132 +  if (args_info.vacancyPercent_given) {
133 +    if (args_info.vacancyPercent_arg < 0.0 || args_info.vacancyPercent_arg > 100.0) {
134 +      sprintf(painCave.errMsg, "vacancyPercent was set to a non-sensical value.");
135 +      painCave.isFatal = 1;
136 +      simError();
137 +    } else {
138 +      RealType vF = args_info.vacancyPercent_arg / 100.0;
139 +      RealType vIR;
140 +      RealType vOR;
141 +      if (args_info.vacancyInnerRadius_given) {
142 +        vIR = args_info.vacancyInnerRadius_arg;
143 +      } else {
144 +        vIR = 0.0;
145 +      }
146 +      if (args_info.vacancyOuterRadius_given) {
147 +        vOR = args_info.vacancyOuterRadius_arg;
148 +      } else {
149 +        vOR = particleRadius;
150 +      }
151 +      if (vIR >= 0.0 && vOR <= particleRadius && vOR >= vIR) {
152 +        
153 +        for (unsigned int i = 0; i < sites.size(); i++) {
154 +          myLoc = sites[i];
155 +          myR = myLoc.length();
156 +          if (myR >= vIR && myR <= vOR) {
157 +            vacancyTargets.push_back(i);
158 +          }          
159 +        }
160 +        std::random_shuffle(vacancyTargets.begin(), vacancyTargets.end());
161 +        
162 +        int nTargets = vacancyTargets.size();
163 +        vacancyTargets.resize((int)(vF * nTargets));
164 +        
165 +                  
166 +        sprintf(painCave.errMsg, "Removing %d atoms from randomly-selected\n"
167 +                "\tsites between %lf and %lf.", (int) vacancyTargets.size(),
168 +                vIR, vOR);
169 +        painCave.isFatal = 0;
170 +        simError();
171 +
172 +        isVacancy.clear();
173 +        for (unsigned int i = 0; i < sites.size(); i++) {
174 +          bool vac = false;
175 +          for (unsigned int j = 0; j < vacancyTargets.size(); j++) {
176 +            if (i == vacancyTargets[j]) vac = true;
177 +          }
178 +          isVacancy.push_back(vac);
179 +        }
180 +              
181 +      } else {
182 +        sprintf(painCave.errMsg, "Something is strange about the vacancy\n"
183 +                "\tinner or outer radii.  Check their values.");
184 +        painCave.isFatal = 1;
185 +        simError();
186 +      }
187 +    }
188 +  }
189 +
190    /* Get number of lattice sites */
191 <  int nSites = sites.size();
191 >  int nSites = sites.size() - vacancyTargets.size();
192  
193    std::vector<Component*> components = simParams->getComponents();
194    std::vector<RealType> molFractions;
195 <  std::vector<RealType> molecularMasses;
195 >  std::vector<RealType> shellRadii;
196    std::vector<int> nMol;
197 +  std::map<int, int> componentFromSite;
198    nComponents = components.size();
199  
200 < if (nComponents == 1) {
200 >  if (args_info.molFraction_given && args_info.shellRadius_given) {
201 >    sprintf(painCave.errMsg, "Specify either molFraction or shellRadius "
202 >            "arguments, but not both!");
203 >    painCave.isFatal = 1;
204 >    simError();
205 >  }
206 >  
207 >  if (nComponents == 1) {
208      molFractions.push_back(1.0);    
209 <  } else {
210 <   if (args_info.molFraction_given == nComponents) {
211 <     for (int i = 0; i < nComponents; i++) {
212 <       molFractions.push_back(args_info.molFraction_arg[i]);
213 <     }
214 <    } else if (args_info.molFraction_given == nComponents-1) {
215 <     RealType remainingFraction = 1.0;
216 <     for (int i = 0; i < nComponents-1; i++) {
217 <       molFractions.push_back(args_info.molFraction_arg[i]);
218 <       remainingFraction -= molFractions[i];
219 <     }
220 <     molFractions.push_back(remainingFraction);
221 <   } else {    
222 <     sprintf(painCave.errMsg, "nanoparticleBuilder can't figure out molFractions "
223 <             "for all of the components in the <MetaData> block.");
224 <     painCave.isFatal = 1;
165 <     simError();
166 <   }
167 < }
168 <
169 < RealType totalFraction = 0.0;
170 <
171 < /* Do some simple sanity checking*/
172 <
173 <  for (int i = 0; i < nComponents; i++) {
174 <    if (molFractions.at(i) < 0.0) {
175 <      sprintf(painCave.errMsg, "One of the requested molFractions was"
176 <              " less than zero!");
209 >    shellRadii.push_back(particleRadius);
210 >  } else if (args_info.molFraction_given) {
211 >    if ((int)args_info.molFraction_given == nComponents) {
212 >      for (int i = 0; i < nComponents; i++) {
213 >        molFractions.push_back(args_info.molFraction_arg[i]);
214 >      }
215 >    } else if ((int)args_info.molFraction_given == nComponents-1) {
216 >      RealType remainingFraction = 1.0;
217 >      for (int i = 0; i < nComponents-1; i++) {
218 >        molFractions.push_back(args_info.molFraction_arg[i]);
219 >        remainingFraction -= molFractions[i];
220 >      }
221 >      molFractions.push_back(remainingFraction);
222 >    } else {    
223 >      sprintf(painCave.errMsg, "nanoparticleBuilder can't figure out molFractions "
224 >              "for all of the components in the <MetaData> block.");
225        painCave.isFatal = 1;
226        simError();
227      }
228 <    if (molFractions.at(i) > 1.0) {
229 <      sprintf(painCave.errMsg, "One of the requested molFractions was"
230 <              " greater than one!");
228 >  } else if ((int)args_info.shellRadius_given) {
229 >    if ((int)args_info.shellRadius_given == nComponents) {
230 >      for (int i = 0; i < nComponents; i++) {
231 >        shellRadii.push_back(args_info.shellRadius_arg[i]);
232 >      }
233 >    } else if ((int)args_info.shellRadius_given == nComponents-1) {
234 >      for (int i = 0; i < nComponents-1; i++) {
235 >        shellRadii.push_back(args_info.shellRadius_arg[i]);
236 >      }
237 >      shellRadii.push_back(particleRadius);
238 >    } else {    
239 >      sprintf(painCave.errMsg, "nanoparticleBuilder can't figure out the\n"
240 >              "\tshell radii for all of the components in the <MetaData> block.");
241        painCave.isFatal = 1;
242        simError();
243      }
244 <    totalFraction += molFractions.at(i);
245 <  }
246 <  if (abs(totalFraction - 1.0) > 1e-6) {
189 <    sprintf(painCave.errMsg, "The sum of molFractions was not close enough to 1.0");
244 >  } else {
245 >    sprintf(painCave.errMsg, "You have a multi-component <MetaData> block,\n"
246 >            "\tbut have not specified either molFraction or shellRadius arguments.");
247      painCave.isFatal = 1;
248      simError();
249    }
250 <
251 <  int remaining = nSites;
252 <  for (int i=0; i < nComponents-1; i++) {    
253 <    nMol.push_back(int((RealType)nSites * molFractions.at(i)));
254 <    remaining -= nMol.at(i);
255 <  }
256 <  nMol.push_back(remaining);
250 >    
251 >  if (args_info.molFraction_given) {
252 >    RealType totalFraction = 0.0;
253 >    
254 >    /* Do some simple sanity checking*/
255 >    
256 >    for (int i = 0; i < nComponents; i++) {
257 >      if (molFractions.at(i) < 0.0) {
258 >        sprintf(painCave.errMsg, "One of the requested molFractions was"
259 >                " less than zero!");
260 >        painCave.isFatal = 1;
261 >        simError();
262 >      }
263 >      if (molFractions.at(i) > 1.0) {
264 >        sprintf(painCave.errMsg, "One of the requested molFractions was"
265 >                " greater than one!");
266 >        painCave.isFatal = 1;
267 >        simError();
268 >      }
269 >      totalFraction += molFractions.at(i);
270 >    }
271 >    if (abs(totalFraction - 1.0) > 1e-6) {
272 >      sprintf(painCave.errMsg, "The sum of molFractions was not close enough to 1.0");
273 >      painCave.isFatal = 1;
274 >      simError();
275 >    }
276 >    
277 >    int remaining = nSites;
278 >    for (int i=0; i < nComponents-1; i++) {    
279 >      nMol.push_back(int((RealType)nSites * molFractions.at(i)));
280 >      remaining -= nMol.at(i);
281 >    }
282 >    nMol.push_back(remaining);
283 >    
284 >    // recompute actual mol fractions and perform final sanity check:
285 >    
286 >    int totalMolecules = 0;
287 >    for (int i=0; i < nComponents; i++) {
288 >      molFractions[i] = (RealType)(nMol.at(i))/(RealType)nSites;
289 >      totalMolecules += nMol.at(i);
290 >    }
291 >    
292 >    if (totalMolecules != nSites) {
293 >      sprintf(painCave.errMsg, "Computed total number of molecules is not equal "
294 >              "to the number of lattice sites!");
295 >      painCave.isFatal = 1;
296 >      simError();
297 >    }
298 >  } else {
299  
300 <
301 <
302 < // recompute actual mol fractions and perform final sanity check:
303 <
304 <  int totalMolecules = 0;
305 <  RealType totalMass = 0.0;
306 <  for (int i=0; i < nComponents; i++) {
307 <    molFractions[i] = (RealType)(nMol.at(i))/(RealType)nSites;
308 <    totalMolecules += nMol.at(i);
309 <    molecularMasses.push_back(getMolMass(oldInfo->getMoleculeStamp(i),
310 <                                         oldInfo->getForceField()));
311 <    totalMass += (RealType)(nMol.at(i)) * molecularMasses.at(i);
300 >    for (unsigned int i = 0; i < shellRadii.size(); i++) {
301 >      if (shellRadii.at(i) > particleRadius + 1e-6 ) {
302 >        sprintf(painCave.errMsg, "One of the shellRadius values exceeds the particle Radius.");
303 >        painCave.isFatal = 1;
304 >        simError();
305 >      }
306 >      if (shellRadii.at(i) <= 0.0 ) {
307 >        sprintf(painCave.errMsg, "One of the shellRadius values is smaller than zero!");
308 >        painCave.isFatal = 1;
309 >        simError();
310 >      }
311 >    }
312    }
214  RealType avgMass = totalMass / (RealType) totalMolecules;
313  
314 <  if (totalMolecules != nSites) {
315 <    sprintf(painCave.errMsg, "Computed total number of molecules is not equal "
316 <            "to the number of lattice sites!");
317 <    painCave.isFatal = 1;
314 >  vector<int> ids;          
315 >  if ((int)args_info.molFraction_given){
316 >    sprintf(painCave.errMsg, "Creating a randomized spherical nanoparticle.");
317 >    painCave.isFatal = 0;
318      simError();
319 <  }
320 <    
321 <  vector<int> ids;
322 <  for (int i = 0; i < sites.size(); i++) ids.push_back(i);
323 <  /* Random particle is the default case*/
226 <  if (!args_info.ShellRadius_given){
227 <    /* do the iPod thing, Shuffle da vector */
319 >    /* Random particle is the default case*/
320 >
321 >    for (unsigned int i = 0; i < sites.size(); i++)
322 >      if (!isVacancy[i]) ids.push_back(i);
323 >    
324      std::random_shuffle(ids.begin(), ids.end());
229  } else{ /*Handle core-shell with multiple components.*/
230    std::cout << "Creating a core-shell nanoparticle." << std::endl;
231    if (nComponents != args_info.ShellRadius_given + 1){
232      sprintf(painCave.errMsg, "Number of .md components "
233              "does not match the number of shell radius specifications");
234      painCave.isFatal = 1;
235      simError();
236    }  
325      
326 <  }
326 >  } else{
327 >    sprintf(painCave.errMsg, "Creating a core-shell spherical nanoparticle.");
328 >    painCave.isFatal = 0;
329 >    simError();
330  
331 +    RealType smallestSoFar;
332 +    int myComponent = -1;
333 +    nMol.clear();
334 +    nMol.resize(nComponents);
335 +
336 +    for (unsigned int i = 0; i < sites.size(); i++) {
337 +      myLoc = sites[i];
338 +      myR = myLoc.length();
339 +      smallestSoFar = particleRadius;      
340 +      if (!isVacancy[i]) {
341 +        for (int j = 0; j < nComponents; j++) {
342 +          if (myR <= shellRadii[j]) {
343 +            if (shellRadii[j] <= smallestSoFar) {
344 +              smallestSoFar = shellRadii[j];
345 +              myComponent = j;
346 +            }
347 +          }
348 +        }
349 +        componentFromSite[i] = myComponent;
350 +        nMol[myComponent]++;
351 +      }
352 +    }      
353 +  }
354    
241  
242  
355    outputFileName = args_info.output_arg;
356 <
357 <  
246 <   //creat new .md file on fly which corrects the number of molecule    
356 >  
357 >  //creat new .md file on fly which corrects the number of molecule    
358    createMdFile(inputFileName, outputFileName, nMol);
359    
360 <  if (oldInfo != NULL)
250 <    delete oldInfo;
360 >  delete oldInfo;
361    
252  
253  // We need to read in new siminfo object.    
254  //parse md file and set up the system
255  //SimCreator NewCreator;
362    SimCreator newCreator;
363    SimInfo* NewInfo = newCreator.createSim(outputFileName, false);
364 <  
259 <  
364 >    
365    // Place molecules
366    Molecule* mol;
367    SimInfo::MoleculeIterator mi;
368    mol = NewInfo->beginMolecule(mi);
369 +
370    int l = 0;
371  
372    for (int i = 0; i < nComponents; i++){
373      locator = new MoLocator(NewInfo->getMoleculeStamp(i),
374                              NewInfo->getForceField());
375 <    for (int n = 0; n < nMol.at(i); n++) {
376 <      mol = NewInfo->getMoleculeByGlobalIndex(l);
377 <      locator->placeMol(sites[ids[l]], orientations[ids[l]], mol);
378 <      l++;
375 >    
376 >    if (!args_info.molFraction_given) {
377 >      for (unsigned int n = 0; n < sites.size(); n++) {
378 >        if (!isVacancy[n]) {
379 >          if (componentFromSite[n] == i) {
380 >            mol = NewInfo->getMoleculeByGlobalIndex(l);
381 >            locator->placeMol(sites[n], orientations[n], mol);
382 >            l++;
383 >          }
384 >        }
385 >      }
386 >    } else {
387 >      for (int n = 0; n < nMol.at(i); n++) {
388 >        mol = NewInfo->getMoleculeByGlobalIndex(l);
389 >        locator->placeMol(sites[ids[l]], orientations[ids[l]], mol);
390 >        l++;
391 >      }
392      }
393    }
394    
276
277  
395    //fill Hmat
396 <  hmat(0, 0)=  2.0*particleRadius;
396 >  hmat(0, 0)=  10.0*particleRadius;
397    hmat(0, 1) = 0.0;
398    hmat(0, 2) = 0.0;
399    
400    hmat(1, 0) = 0.0;
401 <  hmat(1, 1) =  2.0*particleRadius;
401 >  hmat(1, 1) =  10.0*particleRadius;
402    hmat(1, 2) = 0.0;
403    
404    hmat(2, 0) = 0.0;
405    hmat(2, 1) = 0.0;
406 <  hmat(2, 2) =  2.0*particleRadius;
406 >  hmat(2, 2) =  10.0*particleRadius;
407    
408    //set Hmat
409    NewInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
# Line 296 | Line 413 | int main(int argc, char *argv []) {
413    writer = new DumpWriter(NewInfo, outputFileName);
414    
415    if (writer == NULL) {
416 <    sprintf(painCave.errMsg, "Error in creating dumpwrite object ");
417 <      painCave.isFatal = 1;
418 <      simError();
416 >    sprintf(painCave.errMsg, "Error in creating dumpwriter object ");
417 >    painCave.isFatal = 1;
418 >    simError();
419    }
420    
421    writer->writeDump();
# Line 308 | Line 425 | int main(int argc, char *argv []) {
425    delete writer;
426  
427    // cleanup a by calling sim error.....
428 <  sprintf(painCave.errMsg, "A new OOPSE MD file called \"%s\" has been "
428 >  sprintf(painCave.errMsg, "A new OpenMD file called \"%s\" has been "
429            "generated.\n", outputFileName.c_str());
430    painCave.isFatal = 0;
431    simError();
432    return 0;
433   }
434  
435 < void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
435 > void createMdFile(const std::string&oldMdFileName,
436 >                  const std::string&newMdFileName,
437                    std::vector<int> nMol) {
438    ifstream oldMdFile;
439    ofstream newMdFile;
# Line 325 | Line 443 | void createMdFile(const std::string&oldMdFileName, con
443    //create new .md file based on old .md file
444    oldMdFile.open(oldMdFileName.c_str());
445    newMdFile.open(newMdFileName.c_str());
328  
446    oldMdFile.getline(buffer, MAXLEN);
447 <
448 <  int i = 0;
447 >
448 >  unsigned int i = 0;
449    while (!oldMdFile.eof()) {
450 <    
450 >
451      //correct molecule number
452      if (strstr(buffer, "nMol") != NULL) {
453        if(i<nMol.size()){
454 <        sprintf(buffer, "\tnMol = %i;", nMol.at(i));                            
454 >        sprintf(buffer, "\tnMol = %i;", nMol.at(i));
455          newMdFile << buffer << std::endl;
456          i++;
457        }
# Line 346 | Line 463 | void createMdFile(const std::string&oldMdFileName, con
463    
464    oldMdFile.close();
465    newMdFile.close();
466 +
467 +  if (i != nMol.size()) {
468 +    sprintf(painCave.errMsg, "Couldn't replace the correct number of nMol\n"
469 +            "\tstatements in component blocks.  Make sure that all\n"
470 +            "\tcomponents in the template file have nMol=1");
471 +    painCave.isFatal = 1;
472 +    simError();
473 +  }
474 +    
475   }
476  

Comparing trunk/src/applications/nanoparticleBuilder/nanoparticleBuilder.cpp (property svn:keywords):
Revision 1070 by gezelter, Sat Oct 14 20:21:26 2006 UTC vs.
Revision 1879 by gezelter, Sun Jun 16 15:15:42 2013 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines