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

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