ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/nanoparticleBuilder/nanorodBuilder.cpp
Revision: 1876
Committed: Fri May 17 17:10:11 2013 UTC (11 years, 11 months ago) by gezelter
File size: 16266 byte(s)
Log Message:
Compilation and portability fixes

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 gezelter 1850 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
39 kstocke1 1701 * [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 gezelter 1864 #include "shapedLatticeEllipsoid.hpp"
59 kstocke1 1701 #include "nanorodBuilderCmd.h"
60     #include "lattice/LatticeFactory.hpp"
61     #include "utils/MoLocator.hpp"
62     #include "lattice/Lattice.hpp"
63     #include "brains/Register.hpp"
64     #include "brains/SimInfo.hpp"
65     #include "brains/SimCreator.hpp"
66     #include "io/DumpWriter.hpp"
67     #include "math/Vector3.hpp"
68     #include "math/SquareMatrix3.hpp"
69     #include "utils/StringUtils.hpp"
70    
71     using namespace std;
72     using namespace OpenMD;
73     void createMdFile(const std::string&oldMdFileName,
74     const std::string&newMdFileName,
75     std::vector<int> numMol);
76    
77     int main(int argc, char *argv []) {
78    
79     registerLattice();
80    
81     gengetopt_args_info args_info;
82     std::string latticeType;
83     std::string inputFileName;
84     std::string outputFileName;
85     MoLocator* locator;
86     int nComponents;
87     double latticeConstant;
88     RealType rodRadius;
89     RealType rodLength;
90     Mat3x3d hmat;
91     DumpWriter *writer;
92    
93     // Parse Command Line Arguments
94     if (cmdline_parser(argc, argv, &args_info) != 0)
95     exit(1);
96    
97     /* get lattice type */
98     latticeType = "FCC";
99    
100     /* get input file name */
101     if (args_info.inputs_num)
102     inputFileName = args_info.inputs[0];
103     else {
104     sprintf(painCave.errMsg, "No input .md file name was specified "
105     "on the command line");
106     painCave.isFatal = 1;
107     cmdline_parser_print_help();
108     simError();
109     }
110    
111     /* parse md file and set up the system */
112     SimCreator oldCreator;
113     SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
114    
115     latticeConstant = args_info.latticeConstant_arg;
116     rodRadius = args_info.radius_arg;
117     rodLength = args_info.length_arg;
118     Globals* simParams = oldInfo->getSimParams();
119    
120 gezelter 1864 vector<Vector3d> sites;
121     vector<Vector3d> orientations;
122    
123     if (args_info.ellipsoid_flag) {
124     shapedLatticeEllipsoid nanoEllipsoid(latticeConstant, latticeType,
125     rodLength, rodRadius);
126     sites = nanoEllipsoid.getSites();
127     orientations = nanoEllipsoid.getOrientations();
128     } else {
129    
130     /* Create nanorod */
131     shapedLatticeRod nanoRod(latticeConstant, latticeType,
132     rodRadius, rodLength);
133     /* Build a lattice and get lattice points for this lattice constant */
134     sites = nanoRod.getSites();
135     orientations = nanoRod.getOrientations();
136     }
137    
138 kstocke1 1701 std::vector<int> vacancyTargets;
139     vector<bool> isVacancy;
140    
141     Vector3d myLoc;
142     RealType myR;
143    
144 gezelter 1876 for (unsigned int i = 0; i < sites.size(); i++)
145 kstocke1 1701 isVacancy.push_back(false);
146 gezelter 1876
147 kstocke1 1701 // cerr << "checking vacancyPercent" << "\n";
148     if (args_info.vacancyPercent_given) {
149     // cerr << "vacancyPercent given" << "\n";
150     if (args_info.vacancyPercent_arg < 0.0 || args_info.vacancyPercent_arg > 100.0) {
151     sprintf(painCave.errMsg, "vacancyPercent was set to a non-sensical value.");
152     painCave.isFatal = 1;
153     simError();
154     } else {
155     RealType vF = args_info.vacancyPercent_arg / 100.0;
156     // cerr << "vacancyPercent = " << vF << "\n";
157     RealType vIR;
158     RealType vOR;
159     if (args_info.vacancyInnerRadius_given) {
160     vIR = args_info.vacancyInnerRadius_arg;
161     } else {
162     vIR = 0.0;
163     }
164     if (args_info.vacancyOuterRadius_given) {
165     vOR = args_info.vacancyOuterRadius_arg;
166     } else {
167     vOR = rodRadius;
168     }
169     if (vIR >= 0.0 && vOR <= rodRadius && vOR >= vIR) {
170    
171 gezelter 1876 for (unsigned int i = 0; i < sites.size(); i++) {
172 kstocke1 1701 myLoc = sites[i];
173     myR = myLoc.length();
174     if (myR >= vIR && myR <= vOR) {
175     vacancyTargets.push_back(i);
176     }
177     }
178     std::random_shuffle(vacancyTargets.begin(), vacancyTargets.end());
179    
180     int nTargets = vacancyTargets.size();
181     vacancyTargets.resize((int)(vF * nTargets));
182    
183    
184     sprintf(painCave.errMsg, "Removing %d atoms from randomly-selected\n"
185     "\tsites between %lf and %lf.", (int) vacancyTargets.size(),
186     vIR, vOR);
187     painCave.isFatal = 0;
188     simError();
189    
190     isVacancy.clear();
191 gezelter 1876 for (unsigned int i = 0; i < sites.size(); i++) {
192 kstocke1 1701 bool vac = false;
193 gezelter 1876 for (unsigned int j = 0; j < vacancyTargets.size(); j++) {
194 kstocke1 1701 if (i == vacancyTargets[j]) vac = true;
195     }
196     isVacancy.push_back(vac);
197     }
198    
199     } else {
200     sprintf(painCave.errMsg, "Something is strange about the vacancy\n"
201     "\tinner or outer radii. Check their values.");
202     painCave.isFatal = 1;
203     simError();
204     }
205     }
206     }
207    
208     /* Get number of lattice sites */
209     int nSites = sites.size() - vacancyTargets.size();
210    
211     // cerr << "sites.size() = " << sites.size() << "\n";
212     // cerr << "nSites = " << nSites << "\n";
213     // cerr << "vacancyTargets = " << vacancyTargets.size() << "\n";
214    
215     std::vector<Component*> components = simParams->getComponents();
216     std::vector<RealType> molFractions;
217     std::vector<RealType> shellRadii;
218     std::vector<int> nMol;
219     std::map<int, int> componentFromSite;
220     nComponents = components.size();
221     // cerr << "nComponents = " << nComponents << "\n";
222    
223     if (args_info.molFraction_given && args_info.shellRadius_given) {
224     sprintf(painCave.errMsg, "Specify either molFraction or shellRadius "
225     "arguments, but not both!");
226     painCave.isFatal = 1;
227     simError();
228     }
229    
230     if (nComponents == 1) {
231     molFractions.push_back(1.0);
232     shellRadii.push_back(rodRadius);
233     } else if (args_info.molFraction_given) {
234     if ((int)args_info.molFraction_given == nComponents) {
235     for (int i = 0; i < nComponents; i++) {
236     molFractions.push_back(args_info.molFraction_arg[i]);
237     }
238     } else if ((int)args_info.molFraction_given == nComponents-1) {
239     RealType remainingFraction = 1.0;
240     for (int i = 0; i < nComponents-1; i++) {
241     molFractions.push_back(args_info.molFraction_arg[i]);
242     remainingFraction -= molFractions[i];
243     }
244     molFractions.push_back(remainingFraction);
245     } else {
246     sprintf(painCave.errMsg, "nanorodBuilder can't figure out molFractions "
247     "for all of the components in the <MetaData> block.");
248     painCave.isFatal = 1;
249     simError();
250     }
251     } else if ((int)args_info.shellRadius_given) {
252     if ((int)args_info.shellRadius_given == nComponents) {
253     for (int i = 0; i < nComponents; i++) {
254     shellRadii.push_back(args_info.shellRadius_arg[i]);
255     }
256     } else if ((int)args_info.shellRadius_given == nComponents-1) {
257     for (int i = 0; i < nComponents-1; i++) {
258     shellRadii.push_back(args_info.shellRadius_arg[i]);
259     }
260     shellRadii.push_back(rodRadius);
261     } else {
262     sprintf(painCave.errMsg, "nanorodBuilder can't figure out the\n"
263     "\tshell radii for all of the components in the <MetaData> block.");
264     painCave.isFatal = 1;
265     simError();
266     }
267     } else {
268     sprintf(painCave.errMsg, "You have a multi-component <MetaData> block,\n"
269     "\tbut have not specified either molFraction or shellRadius arguments.");
270     painCave.isFatal = 1;
271     simError();
272     }
273    
274     if (args_info.molFraction_given) {
275     RealType totalFraction = 0.0;
276    
277     /* Do some simple sanity checking*/
278    
279     for (int i = 0; i < nComponents; i++) {
280     if (molFractions.at(i) < 0.0) {
281     sprintf(painCave.errMsg, "One of the requested molFractions was"
282     " less than zero!");
283     painCave.isFatal = 1;
284     simError();
285     }
286     if (molFractions.at(i) > 1.0) {
287     sprintf(painCave.errMsg, "One of the requested molFractions was"
288     " greater than one!");
289     painCave.isFatal = 1;
290     simError();
291     }
292     totalFraction += molFractions.at(i);
293     }
294     if (abs(totalFraction - 1.0) > 1e-6) {
295     sprintf(painCave.errMsg, "The sum of molFractions was not close enough to 1.0");
296     painCave.isFatal = 1;
297     simError();
298     }
299    
300     int remaining = nSites;
301     for (int i=0; i < nComponents-1; i++) {
302     nMol.push_back(int((RealType)nSites * molFractions.at(i)));
303     remaining -= nMol.at(i);
304     }
305     nMol.push_back(remaining);
306    
307     // recompute actual mol fractions and perform final sanity check:
308    
309     int totalMolecules = 0;
310     for (int i=0; i < nComponents; i++) {
311     molFractions[i] = (RealType)(nMol.at(i))/(RealType)nSites;
312     totalMolecules += nMol.at(i);
313     }
314     if (totalMolecules != nSites) {
315     sprintf(painCave.errMsg, "Computed total number of molecules is not equal "
316     "to the number of lattice sites!");
317     painCave.isFatal = 1;
318     simError();
319     }
320     } else {
321    
322 gezelter 1876 for (unsigned int i = 0; i < shellRadii.size(); i++) {
323 kstocke1 1701 if (shellRadii.at(i) > rodRadius + 1e-6 ) {
324     sprintf(painCave.errMsg, "One of the shellRadius values exceeds the rod Radius.");
325     painCave.isFatal = 1;
326     simError();
327     }
328     if (shellRadii.at(i) <= 0.0 ) {
329     sprintf(painCave.errMsg, "One of the shellRadius values is smaller than zero!");
330     painCave.isFatal = 1;
331     simError();
332     }
333     }
334     }
335    
336     vector<int> ids;
337     if ((int)args_info.molFraction_given){
338     // cerr << "molFraction given 2" << "\n";
339     sprintf(painCave.errMsg, "Creating a randomized spherically-capped nanorod.");
340     painCave.isFatal = 0;
341     simError();
342     /* Random rod is the default case*/
343    
344 gezelter 1876 for (unsigned int i = 0; i < sites.size(); i++)
345 kstocke1 1701 if (!isVacancy[i]) ids.push_back(i);
346    
347     std::random_shuffle(ids.begin(), ids.end());
348    
349     } else{
350     sprintf(painCave.errMsg, "Creating an fcc nanorod.");
351     painCave.isFatal = 0;
352     simError();
353    
354 gezelter 1828 // RealType smallestSoFar;
355 kstocke1 1701 int myComponent = -1;
356     nMol.clear();
357     nMol.resize(nComponents);
358    
359     // cerr << "shellRadii[0] " << shellRadii[0] << "\n";
360 gezelter 1828 // cerr << "rodRadius " << rodRadius << "\n";
361 kstocke1 1701
362 gezelter 1876 for (unsigned int i = 0; i < sites.size(); i++) {
363 kstocke1 1701 myLoc = sites[i];
364     myR = myLoc.length();
365 gezelter 1828 // smallestSoFar = rodRadius;
366     // cerr << "vac = " << isVacancy[i]<< "\n";
367 kstocke1 1701
368     if (!isVacancy[i]) {
369    
370    
371     // for (int j = 0; j < nComponents; j++) {
372     // if (myR <= shellRadii[j]) {
373     // if (shellRadii[j] <= smallestSoFar) {
374     // smallestSoFar = shellRadii[j];
375     // myComponent = j;
376     // }
377     // }
378     // }
379     myComponent = 0;
380     componentFromSite[i] = myComponent;
381     nMol[myComponent]++;
382     // cerr << "nMol for myComp(" << myComponent<<") = " << nMol[myComponent] << "\n";
383     }
384     }
385     }
386     // cerr << "nMol = " << nMol.at(0) << "\n";
387    
388     outputFileName = args_info.output_arg;
389    
390     //creat new .md file on fly which corrects the number of molecule
391    
392     createMdFile(inputFileName, outputFileName, nMol);
393    
394 gezelter 1874 delete oldInfo;
395 kstocke1 1701
396     SimCreator newCreator;
397     SimInfo* NewInfo = newCreator.createSim(outputFileName, false);
398    
399     // Place molecules
400     Molecule* mol;
401     SimInfo::MoleculeIterator mi;
402     mol = NewInfo->beginMolecule(mi);
403    
404     int l = 0;
405    
406 gezelter 1876 for (unsigned int i = 0; i < nComponents; i++){
407 kstocke1 1701 locator = new MoLocator(NewInfo->getMoleculeStamp(i),
408     NewInfo->getForceField());
409    
410     // cerr << "nMol = " << nMol.at(i) << "\n";
411     if (!args_info.molFraction_given) {
412 gezelter 1876 for (unsigned int n = 0; n < sites.size(); n++) {
413 kstocke1 1701 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 gezelter 1876 for (unsigned int n = 0; n < nMol.at(i); n++) {
423 kstocke1 1701 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 gezelter 1876 unsigned int i = 0;
484 kstocke1 1701 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