ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/nanoparticleBuilder/nanoparticleBuilder.cpp
Revision: 1874
Committed: Wed May 15 15:09:35 2013 UTC (11 years, 11 months ago) by gezelter
File size: 15221 byte(s)
Log Message:
Fixed a bunch of cppcheck warnings.

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

Properties

Name Value
svn:keywords Author Id Revision Date