ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/nanoparticleBuilder/nanorod_pentBuilder.cpp
Revision: 1875
Committed: Fri May 17 14:41:42 2013 UTC (12 years, 7 months ago) by gezelter
File size: 18268 byte(s)
Log Message:
Fixed a bunch of stylistic and performance issues discovered via cppcheck.

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