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

Comparing:
trunk/src/applications/nanoparticleBuilder/nanoparticleBuilder.cpp (file contents), Revision 1069 by chuckv, Fri Oct 13 20:16:59 2006 UTC vs.
branches/development/src/applications/nanoparticleBuilder/nanoparticleBuilder.cpp (file contents), Revision 1704 by gezelter, Tue Apr 24 20:40:04 2012 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, 24107 (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);
# Line 78 | Line 79 | int main(int argc, char *argv []) {
79    gengetopt_args_info args_info;
80    std::string latticeType;
81    std::string inputFileName;
81  std::string outPrefix;
82    std::string outputFileName;
83  std::string outInitFileName;
83  
85  
86  
87  Lattice *simpleLat;
84    MoLocator* locator;
89  int* numMol;
85    int nComponents;
86    double latticeConstant;
87    std::vector<double> lc;
93  double mass;                                                                          
88  
89 <  const double rhoConvertConst = 1.661;
96 <  double density;
97 <  double particleRadius;
98 <  
99 <  
89 >  RealType particleRadius;
90  
91    Mat3x3d hmat;
92    std::vector<Vector3d> latticePos;
93    std::vector<Vector3d> latticeOrt;
104  int numMolPerCell;
105  int nShells; /* Number of shells in nanoparticle*/
94  
107  
95    DumpWriter *writer;
96    
97    // Parse Command Line Arguments
98    if (cmdline_parser(argc, argv, &args_info) != 0)
99      exit(1);
100 <  
114 <        
115 <        
100 >        
101    /* get lattice type */
102    latticeType = "FCC";
103  
# Line 120 | Line 105 | int main(int argc, char *argv []) {
105    if (args_info.inputs_num)
106      inputFileName = args_info.inputs[0];
107    else {
108 <      sprintf(painCave.errMsg, "No input .md file name was specified"
109 <              "on the command line");
108 >    sprintf(painCave.errMsg, "No input .md file name was specified "
109 >            "on the command line");
110      painCave.isFatal = 1;
111      cmdline_parser_print_help();
112      simError();
# Line 131 | Line 116 | int main(int argc, char *argv []) {
116    SimCreator oldCreator;
117    SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
118    
119 <  
135 <  latticeConstant = args_info.latticeCnst_arg;
119 >  latticeConstant = args_info.latticeConstant_arg;
120    particleRadius = args_info.radius_arg;
121    Globals* simParams = oldInfo->getSimParams();
122    
139      
140  /* create Molocators */
141  locator = new MoLocator(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
142  
123    /* Create nanoparticle */
124 <  shapedLatticeSpherical nanoParticle(latticeConstant,latticeType,particleRadius);
124 >  shapedLatticeSpherical nanoParticle(latticeConstant, latticeType,
125 >                                      particleRadius);
126    
127    /* Build a lattice and get lattice points for this lattice constant */
128 <  vector<Vector3d> sites = nanoParticle.getPoints();
129 <  vector<Vector3d> orientations = nanoParticle.getPointsOrt();
128 >  vector<Vector3d> sites = nanoParticle.getSites();
129 >  vector<Vector3d> orientations = nanoParticle.getOrientations();
130  
150  std::cout <<"nSites: " << sites.size() << std::endl;
131  
132 <  /* Get number of lattice sites */
133 <  int nSites = sites.size();
132 >  std::vector<int> vacancyTargets;
133 >  vector<bool> isVacancy;
134 >  
135 >  Vector3d myLoc;
136 >  RealType myR;
137  
138 +  for (int i = 0; i < sites.size(); i++)
139 +    isVacancy.push_back(false);
140  
141 +  if (args_info.vacancyPercent_given) {
142 +    if (args_info.vacancyPercent_arg < 0.0 || args_info.vacancyPercent_arg > 100.0) {
143 +      sprintf(painCave.errMsg, "vacancyPercent was set to a non-sensical value.");
144 +      painCave.isFatal = 1;
145 +      simError();
146 +    } else {
147 +      RealType vF = args_info.vacancyPercent_arg / 100.0;
148 +      RealType vIR;
149 +      RealType vOR;
150 +      if (args_info.vacancyInnerRadius_given) {
151 +        vIR = args_info.vacancyInnerRadius_arg;
152 +      } else {
153 +        vIR = 0.0;
154 +      }
155 +      if (args_info.vacancyOuterRadius_given) {
156 +        vOR = args_info.vacancyOuterRadius_arg;
157 +      } else {
158 +        vOR = particleRadius;
159 +      }
160 +      if (vIR >= 0.0 && vOR <= particleRadius && vOR >= vIR) {
161 +        
162 +        for (int i = 0; i < sites.size(); i++) {
163 +          myLoc = sites[i];
164 +          myR = myLoc.length();
165 +          if (myR >= vIR && myR <= vOR) {
166 +            vacancyTargets.push_back(i);
167 +          }          
168 +        }
169 +        std::random_shuffle(vacancyTargets.begin(), vacancyTargets.end());
170 +        
171 +        int nTargets = vacancyTargets.size();
172 +        vacancyTargets.resize((int)(vF * nTargets));
173 +        
174 +                  
175 +        sprintf(painCave.errMsg, "Removing %d atoms from randomly-selected\n"
176 +                "\tsites between %lf and %lf.", (int) vacancyTargets.size(),
177 +                vIR, vOR);
178 +        painCave.isFatal = 0;
179 +        simError();
180  
181 +        isVacancy.clear();
182 +        for (int i = 0; i < sites.size(); i++) {
183 +          bool vac = false;
184 +          for (int j = 0; j < vacancyTargets.size(); j++) {
185 +            if (i == vacancyTargets[j]) vac = true;
186 +          }
187 +          isVacancy.push_back(vac);
188 +        }
189 +              
190 +      } else {
191 +        sprintf(painCave.errMsg, "Something is strange about the vacancy\n"
192 +                "\tinner or outer radii.  Check their values.");
193 +        painCave.isFatal = 1;
194 +        simError();
195 +      }
196 +    }
197 +  }
198  
199 +  /* Get number of lattice sites */
200 +  int nSites = sites.size() - vacancyTargets.size();
201 +
202    std::vector<Component*> components = simParams->getComponents();
203    std::vector<RealType> molFractions;
204 +  std::vector<RealType> shellRadii;
205    std::vector<RealType> molecularMasses;
206    std::vector<int> nMol;
207 +  std::map<int, int> componentFromSite;
208    nComponents = components.size();
163  
209  
210 <
211 < if (nComponents == 1) {
210 >  if (args_info.molFraction_given && args_info.shellRadius_given) {
211 >    sprintf(painCave.errMsg, "Specify either molFraction or shellRadius "
212 >            "arguments, but not both!");
213 >    painCave.isFatal = 1;
214 >    simError();
215 >  }
216 >  
217 >  if (nComponents == 1) {
218      molFractions.push_back(1.0);    
219 <  } else {
220 <    if (args_info.molFraction_given == nComponents) {
219 >    shellRadii.push_back(particleRadius);
220 >  } else if (args_info.molFraction_given) {
221 >    if ((int)args_info.molFraction_given == nComponents) {
222        for (int i = 0; i < nComponents; i++) {
223          molFractions.push_back(args_info.molFraction_arg[i]);
224        }
225 <    } else if (args_info.molFraction_given == nComponents-1) {
225 >    } else if ((int)args_info.molFraction_given == nComponents-1) {
226        RealType remainingFraction = 1.0;
227        for (int i = 0; i < nComponents-1; i++) {
228          molFractions.push_back(args_info.molFraction_arg[i]);
# Line 183 | Line 235 | int main(int argc, char *argv []) {
235        painCave.isFatal = 1;
236        simError();
237      }
238 <  }
239 <
240 < RealType totalFraction = 0.0;
241 <
242 < /* Do some simple sanity checking*/
243 <
244 <
245 <
246 <  for (int i = 0; i < nComponents; i++) {
247 <    if (molFractions.at(i) < 0.0) {
248 <      sprintf(painCave.errMsg, "One of the requested molFractions was"
249 <              " less than zero!");
238 >  } else if ((int)args_info.shellRadius_given) {
239 >    if ((int)args_info.shellRadius_given == nComponents) {
240 >      for (int i = 0; i < nComponents; i++) {
241 >        shellRadii.push_back(args_info.shellRadius_arg[i]);
242 >      }
243 >    } else if ((int)args_info.shellRadius_given == nComponents-1) {
244 >      for (int i = 0; i < nComponents-1; i++) {
245 >        shellRadii.push_back(args_info.shellRadius_arg[i]);
246 >      }
247 >      shellRadii.push_back(particleRadius);
248 >    } else {    
249 >      sprintf(painCave.errMsg, "nanoparticleBuilder can't figure out the\n"
250 >              "\tshell radii for all of the components in the <MetaData> block.");
251        painCave.isFatal = 1;
252        simError();
253      }
254 <    if (molFractions.at(i) > 1.0) {
255 <      sprintf(painCave.errMsg, "One of the requested molFractions was"
256 <              " greater than one!");
204 <      painCave.isFatal = 1;
205 <      simError();
206 <    }
207 <    totalFraction += molFractions.at(i);
208 <  }
209 <  if (abs(totalFraction - 1.0) > 1e-6) {
210 <    sprintf(painCave.errMsg, "The sum of molFractions was not close enough to 1.0");
254 >  } else {
255 >    sprintf(painCave.errMsg, "You have a multi-component <MetaData> block,\n"
256 >            "\tbut have not specified either molFraction or shellRadius arguments.");
257      painCave.isFatal = 1;
258      simError();
259    }
260 <
261 <  int remaining = nSites;
262 <  for (int i=0; i < nComponents-1; i++) {    
263 <    nMol.push_back(int((RealType)nSites * molFractions.at(i)));
264 <    remaining -= nMol.at(i);
265 <  }
266 <  nMol.push_back(remaining);
260 >    
261 >  if (args_info.molFraction_given) {
262 >    RealType totalFraction = 0.0;
263 >    
264 >    /* Do some simple sanity checking*/
265 >    
266 >    for (int i = 0; i < nComponents; i++) {
267 >      if (molFractions.at(i) < 0.0) {
268 >        sprintf(painCave.errMsg, "One of the requested molFractions was"
269 >                " less than zero!");
270 >        painCave.isFatal = 1;
271 >        simError();
272 >      }
273 >      if (molFractions.at(i) > 1.0) {
274 >        sprintf(painCave.errMsg, "One of the requested molFractions was"
275 >                " greater than one!");
276 >        painCave.isFatal = 1;
277 >        simError();
278 >      }
279 >      totalFraction += molFractions.at(i);
280 >    }
281 >    if (abs(totalFraction - 1.0) > 1e-6) {
282 >      sprintf(painCave.errMsg, "The sum of molFractions was not close enough to 1.0");
283 >      painCave.isFatal = 1;
284 >      simError();
285 >    }
286 >    
287 >    int remaining = nSites;
288 >    for (int i=0; i < nComponents-1; i++) {    
289 >      nMol.push_back(int((RealType)nSites * molFractions.at(i)));
290 >      remaining -= nMol.at(i);
291 >    }
292 >    nMol.push_back(remaining);
293 >    
294 >    // recompute actual mol fractions and perform final sanity check:
295 >    
296 >    int totalMolecules = 0;
297 >    for (int i=0; i < nComponents; i++) {
298 >      molFractions[i] = (RealType)(nMol.at(i))/(RealType)nSites;
299 >      totalMolecules += nMol.at(i);
300 >    }
301 >    
302 >    if (totalMolecules != nSites) {
303 >      sprintf(painCave.errMsg, "Computed total number of molecules is not equal "
304 >              "to the number of lattice sites!");
305 >      painCave.isFatal = 1;
306 >      simError();
307 >    }
308 >  } else {
309  
310 <
311 <
312 < // recompute actual mol fractions and perform final sanity check:
313 <
314 <  int totalMolecules = 0;
315 <  RealType totalMass = 0.0;
316 <  for (int i=0; i < nComponents; i++) {
317 <    molFractions[i] = (RealType)(nMol.at(i))/(RealType)nSites;
318 <    totalMolecules += nMol.at(i);
319 <    molecularMasses.push_back(getMolMass(oldInfo->getMoleculeStamp(i),
320 <                                         oldInfo->getForceField()));
321 <    totalMass += (RealType)(nMol.at(i)) * molecularMasses.at(i);
310 >    for (int i = 0; i < shellRadii.size(); i++) {
311 >      if (shellRadii.at(i) > particleRadius + 1e-6 ) {
312 >        sprintf(painCave.errMsg, "One of the shellRadius values exceeds the particle Radius.");
313 >        painCave.isFatal = 1;
314 >        simError();
315 >      }
316 >      if (shellRadii.at(i) <= 0.0 ) {
317 >        sprintf(painCave.errMsg, "One of the shellRadius values is smaller than zero!");
318 >        painCave.isFatal = 1;
319 >        simError();
320 >      }
321 >    }
322    }
235  RealType avgMass = totalMass / (RealType) totalMolecules;
323  
324 <  if (totalMolecules != nSites) {
325 <    sprintf(painCave.errMsg, "Computed total number of molecules is not equal "
326 <            "to the number of lattice sites!");
327 <    painCave.isFatal = 1;
324 >  vector<int> ids;          
325 >  if ((int)args_info.molFraction_given){
326 >    sprintf(painCave.errMsg, "Creating a randomized spherical nanoparticle.");
327 >    painCave.isFatal = 0;
328      simError();
329 <  }
243 <    
329 >    /* Random particle is the default case*/
330  
331 <
332 <
333 <  vector<int> ids;
248 <  for (int i = 0; i < sites.size(); i++) ids.push_back(i);
249 <  /* Random particle is the default case*/
250 <  if (!args_info.ShellRadius_given){
251 <    /* do the iPod thing, Shuffle da vector */
331 >    for (int i = 0; i < sites.size(); i++)
332 >      if (!isVacancy[i]) ids.push_back(i);
333 >    
334      std::random_shuffle(ids.begin(), ids.end());
253  } else{ /*Handle core-shell with multiple components.*/
254    std::cout << "Creating a core-shell nanoparticle." << std::endl;
255    if (nComponents != args_info.ShellRadius_given + 1){
256      sprintf(painCave.errMsg, "Number of .md components "
257              "does not match the number of shell radius specifications");
258      painCave.isFatal = 1;
259      simError();
260    }  
335      
336 <  }
336 >  } else{
337 >    sprintf(painCave.errMsg, "Creating a core-shell spherical nanoparticle.");
338 >    painCave.isFatal = 0;
339 >    simError();
340  
341 +    RealType smallestSoFar;
342 +    int myComponent = -1;
343 +    nMol.clear();
344 +    nMol.resize(nComponents);
345 +
346 +    for (int i = 0; i < sites.size(); i++) {
347 +      myLoc = sites[i];
348 +      myR = myLoc.length();
349 +      smallestSoFar = particleRadius;      
350 +      if (!isVacancy[i]) {
351 +        for (int j = 0; j < nComponents; j++) {
352 +          if (myR <= shellRadii[j]) {
353 +            if (shellRadii[j] <= smallestSoFar) {
354 +              smallestSoFar = shellRadii[j];
355 +              myComponent = j;
356 +            }
357 +          }
358 +        }
359 +        componentFromSite[i] = myComponent;
360 +        nMol[myComponent]++;
361 +      }
362 +    }      
363 +  }
364    
265  
266  
365    outputFileName = args_info.output_arg;
366 <
367 <  
270 <   //creat new .md file on fly which corrects the number of molecule    
366 >  
367 >  //creat new .md file on fly which corrects the number of molecule    
368    createMdFile(inputFileName, outputFileName, nMol);
369    
370    if (oldInfo != NULL)
371      delete oldInfo;
372    
276  
277  // We need to read in new siminfo object.    
278  //parse md file and set up the system
279  //SimCreator NewCreator;
373    SimCreator newCreator;
374    SimInfo* NewInfo = newCreator.createSim(outputFileName, false);
375 <  
283 <  
375 >    
376    // Place molecules
377    Molecule* mol;
378    SimInfo::MoleculeIterator mi;
379    mol = NewInfo->beginMolecule(mi);
380 +
381    int l = 0;
382 +  int whichSite = 0;
383  
384    for (int i = 0; i < nComponents; i++){
385      locator = new MoLocator(NewInfo->getMoleculeStamp(i),
386                              NewInfo->getForceField());
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++;
387 >    
388 >    if (!args_info.molFraction_given) {
389 >      for (int n = 0; n < sites.size(); n++) {
390 >        if (!isVacancy[n]) {
391 >          if (componentFromSite[n] == i) {
392 >            mol = NewInfo->getMoleculeByGlobalIndex(l);
393 >            locator->placeMol(sites[n], orientations[n], mol);
394 >            l++;
395 >          }
396 >        }
397 >      }
398 >    } else {
399 >      for (int n = 0; n < nMol.at(i); n++) {
400 >        mol = NewInfo->getMoleculeByGlobalIndex(l);
401 >        locator->placeMol(sites[ids[l]], orientations[ids[l]], mol);
402 >        l++;
403 >      }
404      }
405    }
406    
300
301  
407    //fill Hmat
408 <  hmat(0, 0)=  2.0*particleRadius;
408 >  hmat(0, 0)=  10.0*particleRadius;
409    hmat(0, 1) = 0.0;
410    hmat(0, 2) = 0.0;
411    
412    hmat(1, 0) = 0.0;
413 <  hmat(1, 1) =  2.0*particleRadius;
413 >  hmat(1, 1) =  10.0*particleRadius;
414    hmat(1, 2) = 0.0;
415    
416    hmat(2, 0) = 0.0;
417    hmat(2, 1) = 0.0;
418 <  hmat(2, 2) =  2.0*particleRadius;
418 >  hmat(2, 2) =  10.0*particleRadius;
419    
420    //set Hmat
421    NewInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
422    
423    
424    //create dumpwriter and write out the coordinates
425 <  writer = new DumpWriter(NewInfo,outputFileName);
425 >  writer = new DumpWriter(NewInfo, outputFileName);
426    
427    if (writer == NULL) {
428 <    sprintf(painCave.errMsg, "Error in creating dumpwrite object ");
429 <      painCave.isFatal = 1;
430 <      simError();
428 >    sprintf(painCave.errMsg, "Error in creating dumpwriter object ");
429 >    painCave.isFatal = 1;
430 >    simError();
431    }
432    
433    writer->writeDump();
329  std::cout << "new initial configuration file: " << outInitFileName
330            << " is generated." << std::endl;
331
434  
435    // deleting the writer will put the closing at the end of the dump file
436 +
437    delete writer;
438  
439    // cleanup a by calling sim error.....
440 <  sprintf(painCave.errMsg, "A new OOPSE MD file called \"%s\" has been "
440 >  sprintf(painCave.errMsg, "A new OpenMD file called \"%s\" has been "
441            "generated.\n", outputFileName.c_str());
442    painCave.isFatal = 0;
443    simError();
444    return 0;
445   }
446  
447 < void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
447 > void createMdFile(const std::string&oldMdFileName,
448 >                  const std::string&newMdFileName,
449                    std::vector<int> nMol) {
450    ifstream oldMdFile;
451    ofstream newMdFile;
# Line 351 | Line 455 | void createMdFile(const std::string&oldMdFileName, con
455    //create new .md file based on old .md file
456    oldMdFile.open(oldMdFileName.c_str());
457    newMdFile.open(newMdFileName.c_str());
354  
458    oldMdFile.getline(buffer, MAXLEN);
459 <
459 >
460    int i = 0;
461    while (!oldMdFile.eof()) {
462 <    
462 >
463      //correct molecule number
464      if (strstr(buffer, "nMol") != NULL) {
465        if(i<nMol.size()){
466 <        sprintf(buffer, "\tnMol = %i;", nMol.at(i));                            
466 >        sprintf(buffer, "\tnMol = %i;", nMol.at(i));
467          newMdFile << buffer << std::endl;
468          i++;
469        }
# Line 372 | Line 475 | void createMdFile(const std::string&oldMdFileName, con
475    
476    oldMdFile.close();
477    newMdFile.close();
478 +
479 +  if (i != nMol.size()) {
480 +    sprintf(painCave.errMsg, "Couldn't replace the correct number of nMol\n"
481 +            "\tstatements in component blocks.  Make sure that all\n"
482 +            "\tcomponents in the template file have nMol=1");
483 +    painCave.isFatal = 1;
484 +    simError();
485 +  }
486 +    
487   }
488  

Comparing:
trunk/src/applications/nanoparticleBuilder/nanoparticleBuilder.cpp (property svn:keywords), Revision 1069 by chuckv, Fri Oct 13 20:16:59 2006 UTC vs.
branches/development/src/applications/nanoparticleBuilder/nanoparticleBuilder.cpp (property svn:keywords), Revision 1704 by gezelter, Tue Apr 24 20:40:04 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines