ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/nanoparticleBuilder/nanorodBuilder.cpp
Revision: 1701
Committed: Thu Apr 5 19:37:58 2012 UTC (13 years, 1 month ago) by kstocke1
File size: 16025 byte(s)
Log Message:
Added files for fcc and pentagonal nanorod builders. Updated runMe in samples/builders to include nanorod builders.

File Contents

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