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