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 949 by chuckv, Tue Apr 25 22:54:55 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 <                  int components,int* numMol);
71 >                  std::vector<int> numMol);
72  
73   int main(int argc, char *argv []) {
74    
# Line 78 | Line 79 | int main(int argc, char *argv []) {
79    gengetopt_args_info args_info;
80    std::string latticeType;
81    std::string inputFileName;
82 <  std::string outPrefix;
82 <  std::string outMdFileName;
83 <  std::string outInitFileName;
82 >  std::string outputFileName;
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;                                                                                      
94  const double rhoConvertConst = 1.661;
95  double density;
96  double particleRadius;
97  
98  
88  
89 +  RealType particleRadius;
90 +
91    Mat3x3d hmat;
92    std::vector<Vector3d> latticePos;
93    std::vector<Vector3d> latticeOrt;
94 <  int numMolPerCell;
104 <  int nShells; /* Number of shells in nanoparticle*/
105 <  int numSites;
106 <  
94 >
95    DumpWriter *writer;
96    
97    // Parse Command Line Arguments
98    if (cmdline_parser(argc, argv, &args_info) != 0)
99      exit(1);
100 <  
113 <        
114 <        
100 >        
101    /* get lattice type */
102 <  latticeType = UpperCase(args_info.latticetype_arg);
103 <    
102 >  latticeType = "FCC";
103 >
104    /* get input file name */
105    if (args_info.inputs_num)
106      inputFileName = args_info.inputs[0];
107    else {
108 <    std::cerr << "You must specify a input file name.\n" << std::endl;
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 <    exit(1);
112 >    simError();
113    }
114    
115    /* parse md file and set up the system */
116    SimCreator oldCreator;
117    SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
118    
119 <  
132 <  /*calculate lattice constant (in Angstrom)
133 <    latticeConstant = pow(rhoConvertConst * numMolPerCell * mass / density,
134 <    1.0 / 3.0);*/
135 <  
136 <  latticeConstant = args_info.latticeCnst_arg;
119 >  latticeConstant = args_info.latticeConstant_arg;
120    particleRadius = args_info.radius_arg;
121    Globals* simParams = oldInfo->getSimParams();
122    
140  /* Find out how many different components in this simualtion */
141  nComponents =simParams->getNComponents();
142  
143  /*determine the output file names*/
144  if (args_info.output_given){
145    outInitFileName = args_info.output_arg;
146  }else{
147    outInitFileName = getPrefix(inputFileName.c_str()) + ".in";
148  }
149  
150  std::cout <<"Before build shaped lattice. "<<std::endl;
151  
152  /* create Molocators */
153  locator = new MoLocator(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
154  
123    /* Create nanoparticle */
124 <  shapedLatticeSpherical nanoParticle(latticeConstant,latticeType,particleRadius);
124 >  shapedLatticeSpherical nanoParticle(latticeConstant, latticeType,
125 >                                      particleRadius);
126    
158  std::cout <<"Before build getPoints. "<<std::endl;
127    /* Build a lattice and get lattice points for this lattice constant */
128 <  vector<Vector3d> nanoParticleSites = nanoParticle.getPoints();
128 >  vector<Vector3d> sites = nanoParticle.getSites();
129 >  vector<Vector3d> orientations = nanoParticle.getOrientations();
130 >
131 >
132 >  std::vector<int> vacancyTargets;
133 >  vector<bool> isVacancy;
134    
135 <  /* Get number of lattice sites */
136 <  numSites = nanoParticleSites.size();
135 >  Vector3d myLoc;
136 >  RealType myR;
137  
138 <  //std::cout <<"numSites are %d "<<numSites<<std::endl;
139 <  // std::cout <<"nComponents are %d "<<nComponents<<std::endl;
140 <  numMol = new int[nComponents];
141 <
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();
209 >
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 <  /* Random particle is the default case*/
218 <  if (!args_info.ShellRadius_given){
219 <    std::cout << "Creating a random nanoparticle" << std::endl;
220 <    /* Check to see if we have enough components */
221 <    if (nComponents != args_info.molFraction_given && nComponents != 1){
222 <      std::cerr << "Number of components does not equal molFraction occurances." << std::endl;
223 <      exit(1);
217 >  if (nComponents == 1) {
218 >    molFractions.push_back(1.0);    
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 ((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]);
229 >        remainingFraction -= molFractions[i];
230 >      }
231 >      molFractions.push_back(remainingFraction);
232 >    } else {    
233 >      sprintf(painCave.errMsg, "nanoparticleBuilder can't figure out molFractions "
234 >              "for all of the components in the <MetaData> block.");
235 >      painCave.isFatal = 1;
236 >      simError();
237      }
238 <    /* Build the mole fractions and number of molecules of each type */  
239 <    int totComponents = 0;
240 <    for (int i = 0;i<nComponents-1;i++){ /* Figure out Percent for each component */
241 <      numMol[i] = int((double)numSites * args_info.molFraction_arg[i]);
242 <      std::cout<<numMol[i]<<std::endl;
243 <      totComponents += numMol[i];
244 <    }
245 <    numMol[nComponents-1] = numSites - totComponents;
246 <  
247 <    /* do the iPod thing, Shuffle da vector */
248 <    std::random_shuffle(nanoParticleSites.begin(), nanoParticleSites.end());
249 <  } else{ /*Handle core-shell with multiple components.*/
250 <    std::cout << "Creating a core-shell nanoparticle." << std::endl;
251 <    if (nComponents != args_info.ShellRadius_given + 1){
252 <      std::cerr << "Number of components does not equal ShellRadius occurances." << std::endl;
253 <      exit(1);
254 <    }  
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 >  } 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 +  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 +    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    }
323  
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 +    /* Random particle is the default case*/
330 +
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());
335 +    
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    
365 <  //get the orientation of the cell sites
366 <  //for the same type of molecule in same lattice, it will not change
201 <  latticeOrt = nanoParticle.getPointsOrt();
202 <  std::cout<<"Orientational vector Size: "<< std::endl;
203 <  std::cout<<latticeOrt.size()<< std::endl;
204 <  
205 <  
206 <  
207 <  // needed for writing out new md file.
208 <  
209 <  outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType;
210 <  outMdFileName = outPrefix + ".md";
211 <  
365 >  outputFileName = args_info.output_arg;
366 >  
367    //creat new .md file on fly which corrects the number of molecule    
368 <  createMdFile(inputFileName, outMdFileName, nComponents,numMol);
368 >  createMdFile(inputFileName, outputFileName, nMol);
369    
370    if (oldInfo != NULL)
371      delete oldInfo;
372    
218  
219  // We need to read in new siminfo object.    
220  //parse md file and set up the system
221  //SimCreator NewCreator;
373    SimCreator newCreator;
374 <  SimInfo* NewInfo = newCreator.createSim(outMdFileName, false);
375 <  
225 <  
374 >  SimInfo* NewInfo = newCreator.createSim(outputFileName, false);
375 >    
376    // Place molecules
377    Molecule* mol;
378    SimInfo::MoleculeIterator mi;
379    mol = NewInfo->beginMolecule(mi);
380 +
381    int l = 0;
382 <  for (mol = NewInfo->beginMolecule(mi); mol != NULL; mol = NewInfo->nextMolecule(mi)) {
232 <    locator->placeMol(nanoParticleSites[l], latticeOrt[l], mol);
233 <    l++;
234 <  }
235 <
382 >  int whichSite = 0;
383  
384 <
384 >  for (int i = 0; i < nComponents; i++){
385 >    locator = new MoLocator(NewInfo->getMoleculeStamp(i),
386 >                            NewInfo->getForceField());
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    
407    //fill Hmat
408 <  hmat(0, 0)=  latticeConstant;
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) =  latticeConstant;
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) =  latticeConstant;
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 <  NewInfo->setFinalConfigFileName(outInitFileName);
258 <  writer = new DumpWriter(NewInfo);
425 >  writer = new DumpWriter(NewInfo, outputFileName);
426    
427    if (writer == NULL) {
428 <    std::cerr << "error in creating DumpWriter" << std::endl;
429 <    exit(1);
428 >    sprintf(painCave.errMsg, "Error in creating dumpwriter object ");
429 >    painCave.isFatal = 1;
430 >    simError();
431    }
432    
433    writer->writeDump();
434 <  std::cout << "new initial configuration file: " << outInitFileName
435 <            << " is generated." << std::endl;
436 <  
437 <  //delete objects
438 <  
439 <  //delete oldInfo and oldSimSetup
440 <  
441 <  if (NewInfo != NULL)
442 <    delete NewInfo;
443 <  
276 <  if (writer != NULL)
277 <    delete writer;      
278 <  cmdline_parser_free(&args_info);
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 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,
448 <                  int components,int* numMol) {
447 > void createMdFile(const std::string&oldMdFileName,
448 >                  const std::string&newMdFileName,
449 >                  std::vector<int> nMol) {
450    ifstream oldMdFile;
451    ofstream newMdFile;
452    const int MAXLEN = 65535;
# Line 289 | 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());
292  
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<components){
466 <        sprintf(buffer, "\tnMol = %i;", numMol[i]);                            
465 >      if(i<nMol.size()){
466 >        sprintf(buffer, "\tnMol = %i;", nMol.at(i));
467          newMdFile << buffer << std::endl;
468          i++;
469        }
# Line 310 | 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 949 by chuckv, Tue Apr 25 22:54:55 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