ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/applications/nanoparticleBuilder/nanoparticleBuilder.cpp
Revision: 1977
Committed: Wed Mar 12 21:35:23 2014 UTC (11 years, 1 month ago) by gezelter
File size: 15227 byte(s)
Log Message:
Removed some scary errors that are really supposed to be OPENMD_INFO messages

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

Properties

Name Value
svn:keywords Author Id Revision Date