ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/nanoparticleBuilder/nanoparticleBuilder.cpp
Revision: 1704
Committed: Tue Apr 24 20:40:04 2012 UTC (13 years ago) by gezelter
File size: 15236 byte(s)
Log Message:
qhull modifications for compilation 


File Contents

# User Rev Content
1 chuckv 653 /*
2     * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3     *
4     * The University of Notre Dame grants you ("Licensee") a
5     * non-exclusive, royalty free, license to use, modify and
6     * redistribute this software in source and binary code form, provided
7     * that the following conditions are met:
8     *
9 gezelter 1390 * 1. Redistributions of source code must retain the above copyright
10 chuckv 653 * notice, this list of conditions and the following disclaimer.
11     *
12 gezelter 1390 * 2. Redistributions in binary form must reproduce the above copyright
13 chuckv 653 * notice, this list of conditions and the following disclaimer in the
14     * documentation and/or other materials provided with the
15     * distribution.
16     *
17     * This software is provided "AS IS," without a warranty of any
18     * kind. All express or implied conditions, representations and
19     * warranties, including any implied warranty of merchantability,
20     * fitness for a particular purpose or non-infringement, are hereby
21     * excluded. The University of Notre Dame and its licensors shall not
22     * be liable for any damages suffered by licensee as a result of
23     * using, modifying or distributing the software or its
24     * derivatives. In no event will the University of Notre Dame or its
25     * licensors be liable for any lost revenue, profit or data, or for
26     * direct, indirect, special, consequential, incidental or punitive
27     * damages, however caused and regardless of the theory of liability,
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 gezelter 1390 *
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 gezelter 1665 * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
40     * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41 chuckv 653 */
42    
43     #include <cstdlib>
44     #include <cstdio>
45     #include <cstring>
46     #include <cmath>
47     #include <iostream>
48     #include <string>
49     #include <map>
50     #include <fstream>
51 chuckv 911 #include <algorithm>
52 chuckv 653
53     #include "config.h"
54 chuckv 911 #include "shapedLatticeSpherical.hpp"
55 chuckv 653 #include "nanoparticleBuilderCmd.h"
56     #include "lattice/LatticeFactory.hpp"
57     #include "utils/MoLocator.hpp"
58     #include "lattice/Lattice.hpp"
59     #include "brains/Register.hpp"
60     #include "brains/SimInfo.hpp"
61     #include "brains/SimCreator.hpp"
62     #include "io/DumpWriter.hpp"
63     #include "math/Vector3.hpp"
64     #include "math/SquareMatrix3.hpp"
65     #include "utils/StringUtils.hpp"
66    
67     using namespace std;
68 gezelter 1390 using namespace OpenMD;
69 chuckv 653 void createMdFile(const std::string&oldMdFileName,
70     const std::string&newMdFileName,
71 chuckv 1069 std::vector<int> numMol);
72 chuckv 653
73     int main(int argc, char *argv []) {
74    
75     //register force fields
76     registerForceFields();
77     registerLattice();
78    
79     gengetopt_args_info args_info;
80     std::string latticeType;
81     std::string inputFileName;
82 chuckv 1069 std::string outputFileName;
83 chuckv 653
84 chuckv 911 MoLocator* locator;
85     int nComponents;
86 chuckv 653 double latticeConstant;
87     std::vector<double> lc;
88 gezelter 1075
89 gezelter 1077 RealType particleRadius;
90 chuckv 653
91     Mat3x3d hmat;
92     std::vector<Vector3d> latticePos;
93     std::vector<Vector3d> latticeOrt;
94 chuckv 1069
95 chuckv 653 DumpWriter *writer;
96    
97 chuckv 875 // Parse Command Line Arguments
98 chuckv 653 if (cmdline_parser(argc, argv, &args_info) != 0)
99     exit(1);
100 gezelter 1070
101 chuckv 653 /* get lattice type */
102 chuckv 1069 latticeType = "FCC";
103    
104 chuckv 653 /* get input file name */
105     if (args_info.inputs_num)
106     inputFileName = args_info.inputs[0];
107     else {
108 gezelter 1077 sprintf(painCave.errMsg, "No input .md file name was specified "
109 gezelter 1075 "on the command line");
110 chuckv 1069 painCave.isFatal = 1;
111 chuckv 653 cmdline_parser_print_help();
112 chuckv 1069 simError();
113 chuckv 653 }
114    
115     /* parse md file and set up the system */
116     SimCreator oldCreator;
117     SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
118    
119 gezelter 1077 latticeConstant = args_info.latticeConstant_arg;
120 chuckv 653 particleRadius = args_info.radius_arg;
121 chuckv 911 Globals* simParams = oldInfo->getSimParams();
122 chuckv 653
123 chuckv 911 /* Create nanoparticle */
124 gezelter 1070 shapedLatticeSpherical nanoParticle(latticeConstant, latticeType,
125     particleRadius);
126 chuckv 918
127 chuckv 911 /* Build a lattice and get lattice points for this lattice constant */
128 gezelter 1070 vector<Vector3d> sites = nanoParticle.getSites();
129     vector<Vector3d> orientations = nanoParticle.getOrientations();
130 gezelter 1704
131    
132 gezelter 1077 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 chuckv 1069
141 gezelter 1077 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 gezelter 1390 "\tsites between %lf and %lf.", (int) vacancyTargets.size(),
177 gezelter 1077 vIR, vOR);
178     painCave.isFatal = 0;
179     simError();
180 chuckv 1069
181 gezelter 1077 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 chuckv 911 /* Get number of lattice sites */
200 gezelter 1077 int nSites = sites.size() - vacancyTargets.size();
201 chuckv 1069
202     std::vector<Component*> components = simParams->getComponents();
203     std::vector<RealType> molFractions;
204 gezelter 1075 std::vector<RealType> shellRadii;
205 chuckv 1069 std::vector<RealType> molecularMasses;
206     std::vector<int> nMol;
207 gezelter 1075 std::map<int, int> componentFromSite;
208 chuckv 1069 nComponents = components.size();
209    
210 gezelter 1077 if (args_info.molFraction_given && args_info.shellRadius_given) {
211     sprintf(painCave.errMsg, "Specify either molFraction or shellRadius "
212 gezelter 1075 "arguments, but not both!");
213     painCave.isFatal = 1;
214     simError();
215     }
216    
217     if (nComponents == 1) {
218 chuckv 1069 molFractions.push_back(1.0);
219 gezelter 1075 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 chuckv 1069 painCave.isFatal = 1;
236     simError();
237     }
238 gezelter 1077 } else if ((int)args_info.shellRadius_given) {
239     if ((int)args_info.shellRadius_given == nComponents) {
240 gezelter 1075 for (int i = 0; i < nComponents; i++) {
241 gezelter 1077 shellRadii.push_back(args_info.shellRadius_arg[i]);
242 gezelter 1075 }
243 gezelter 1077 } else if ((int)args_info.shellRadius_given == nComponents-1) {
244 gezelter 1075 for (int i = 0; i < nComponents-1; i++) {
245 gezelter 1077 shellRadii.push_back(args_info.shellRadius_arg[i]);
246 gezelter 1075 }
247     shellRadii.push_back(particleRadius);
248     } else {
249 gezelter 1077 sprintf(painCave.errMsg, "nanoparticleBuilder can't figure out the\n"
250     "\tshell radii for all of the components in the <MetaData> block.");
251 chuckv 1069 painCave.isFatal = 1;
252     simError();
253     }
254 gezelter 1075 } else {
255 gezelter 1077 sprintf(painCave.errMsg, "You have a multi-component <MetaData> block,\n"
256     "\tbut have not specified either molFraction or shellRadius arguments.");
257 chuckv 1069 painCave.isFatal = 1;
258     simError();
259     }
260 gezelter 1075
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 chuckv 1069
310 gezelter 1075 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 chuckv 1069 }
323 gezelter 1077
324     vector<int> ids;
325 gezelter 1075 if ((int)args_info.molFraction_given){
326     sprintf(painCave.errMsg, "Creating a randomized spherical nanoparticle.");
327     painCave.isFatal = 0;
328     simError();
329 gezelter 1077 /* 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 chuckv 1069 std::random_shuffle(ids.begin(), ids.end());
335 gezelter 1077
336 gezelter 1075 } else{
337     sprintf(painCave.errMsg, "Creating a core-shell spherical nanoparticle.");
338     painCave.isFatal = 0;
339     simError();
340 chuckv 653
341 gezelter 1075 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 gezelter 1077 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 gezelter 1075 }
358     }
359 gezelter 1077 componentFromSite[i] = myComponent;
360     nMol[myComponent]++;
361 gezelter 1075 }
362 gezelter 1077 }
363     }
364 chuckv 949
365 chuckv 1069 outputFileName = args_info.output_arg;
366 gezelter 1077
367 gezelter 1075 //creat new .md file on fly which corrects the number of molecule
368 chuckv 1069 createMdFile(inputFileName, outputFileName, nMol);
369 chuckv 653
370     if (oldInfo != NULL)
371     delete oldInfo;
372    
373 chuckv 949 SimCreator newCreator;
374 chuckv 1069 SimInfo* NewInfo = newCreator.createSim(outputFileName, false);
375 gezelter 1075
376 chuckv 911 // Place molecules
377 chuckv 653 Molecule* mol;
378     SimInfo::MoleculeIterator mi;
379     mol = NewInfo->beginMolecule(mi);
380 gezelter 1077
381 chuckv 949 int l = 0;
382 gezelter 1077 int whichSite = 0;
383 chuckv 1069
384     for (int i = 0; i < nComponents; i++){
385     locator = new MoLocator(NewInfo->getMoleculeStamp(i),
386     NewInfo->getForceField());
387 gezelter 1077
388     if (!args_info.molFraction_given) {
389 gezelter 1075 for (int n = 0; n < sites.size(); n++) {
390 gezelter 1077 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 gezelter 1075 }
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 chuckv 1069 }
405 gezelter 1077 }
406 chuckv 653
407     //fill Hmat
408 gezelter 1075 hmat(0, 0)= 10.0*particleRadius;
409 chuckv 653 hmat(0, 1) = 0.0;
410     hmat(0, 2) = 0.0;
411    
412     hmat(1, 0) = 0.0;
413 gezelter 1075 hmat(1, 1) = 10.0*particleRadius;
414 chuckv 653 hmat(1, 2) = 0.0;
415    
416     hmat(2, 0) = 0.0;
417     hmat(2, 1) = 0.0;
418 gezelter 1075 hmat(2, 2) = 10.0*particleRadius;
419 chuckv 653
420     //set Hmat
421     NewInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
422    
423    
424     //create dumpwriter and write out the coordinates
425 gezelter 1070 writer = new DumpWriter(NewInfo, outputFileName);
426 chuckv 653
427     if (writer == NULL) {
428 gezelter 1077 sprintf(painCave.errMsg, "Error in creating dumpwriter object ");
429 gezelter 1075 painCave.isFatal = 1;
430     simError();
431 chuckv 653 }
432    
433     writer->writeDump();
434 chuckv 1069
435     // deleting the writer will put the closing at the end of the dump file
436 gezelter 1070
437 chuckv 1069 delete writer;
438    
439     // cleanup a by calling sim error.....
440 gezelter 1390 sprintf(painCave.errMsg, "A new OpenMD file called \"%s\" has been "
441 chuckv 1069 "generated.\n", outputFileName.c_str());
442     painCave.isFatal = 0;
443     simError();
444 chuckv 653 return 0;
445     }
446    
447 gezelter 1075 void createMdFile(const std::string&oldMdFileName,
448     const std::string&newMdFileName,
449 chuckv 1069 std::vector<int> nMol) {
450 chuckv 653 ifstream oldMdFile;
451     ofstream newMdFile;
452     const int MAXLEN = 65535;
453     char buffer[MAXLEN];
454    
455     //create new .md file based on old .md file
456     oldMdFile.open(oldMdFileName.c_str());
457     newMdFile.open(newMdFileName.c_str());
458     oldMdFile.getline(buffer, MAXLEN);
459 gezelter 1077
460 chuckv 911 int i = 0;
461 chuckv 653 while (!oldMdFile.eof()) {
462 gezelter 1077
463 chuckv 653 //correct molecule number
464     if (strstr(buffer, "nMol") != NULL) {
465 chuckv 1069 if(i<nMol.size()){
466 gezelter 1077 sprintf(buffer, "\tnMol = %i;", nMol.at(i));
467 chuckv 949 newMdFile << buffer << std::endl;
468     i++;
469     }
470 chuckv 653 } else
471     newMdFile << buffer << std::endl;
472    
473     oldMdFile.getline(buffer, MAXLEN);
474     }
475    
476     oldMdFile.close();
477     newMdFile.close();
478 gezelter 1077
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 chuckv 653 }
488    

Properties

Name Value
svn:keywords Author Id Revision Date