ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/nanorodBuilder/nanorodBuilder.cpp
Revision: 2149
Committed: Wed Apr 6 17:05:21 2005 UTC (19 years, 2 months ago) by chuckv
File size: 8736 byte(s)
Log Message:
Building nanorod builder.

File Contents

# User Rev Content
1 chuckv 2148 /*
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. Acknowledgement of the program authors must be made in any
10     * publication of scientific results based in part on use of the
11     * program. An acceptable form of acknowledgement is citation of
12     * the article in which the program was described (Matthew
13     * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14     * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15     * Parallel Simulation Engine for Molecular Dynamics,"
16     * J. Comput. Chem. 26, pp. 252-271 (2005))
17     *
18     * 2. Redistributions of source code must retain the above copyright
19     * notice, this list of conditions and the following disclaimer.
20     *
21     * 3. Redistributions in binary form must reproduce the above copyright
22     * notice, this list of conditions and the following disclaimer in the
23     * documentation and/or other materials provided with the
24     * distribution.
25     *
26     * This software is provided "AS IS," without a warranty of any
27     * kind. All express or implied conditions, representations and
28     * warranties, including any implied warranty of merchantability,
29     * fitness for a particular purpose or non-infringement, are hereby
30     * excluded. The University of Notre Dame and its licensors shall not
31     * be liable for any damages suffered by licensee as a result of
32     * using, modifying or distributing the software or its
33     * derivatives. In no event will the University of Notre Dame or its
34     * licensors be liable for any lost revenue, profit or data, or for
35     * direct, indirect, special, consequential, incidental or punitive
36     * damages, however caused and regardless of the theory of liability,
37     * arising out of the use of or inability to use software, even if the
38     * University of Notre Dame has been advised of the possibility of
39     * such damages.
40     */
41    
42     #include <cstdlib>
43     #include <cstdio>
44     #include <cstring>
45     #include <cmath>
46     #include <iostream>
47     #include <string>
48     #include <map>
49     #include <fstream>
50    
51     #include "nanorodBuilderCmd.h"
52     #include "GeometryBuilder.hpp"
53     #include "applications/simpleBuilder/LatticeFactory.hpp"
54     #include "applications/simpleBuilder/MoLocator.hpp"
55     #include "applications/simpleBuilder/Lattice.hpp"
56     #include "brains/Register.hpp"
57     #include "brains/SimInfo.hpp"
58     #include "brains/SimCreator.hpp"
59     #include "io/DumpWriter.hpp"
60     #include "math/Vector3.hpp"
61     #include "math/SquareMatrix3.hpp"
62     #include "utils/StringUtils.hpp"
63    
64     using namespace std;
65     using namespace oopse;
66     void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
67     int numMol);
68    
69     int main(int argc, char *argv []) {
70    
71     //register force fields
72     registerForceFields();
73    
74     gengetopt_args_info args_info;
75     std::string latticeType;
76     std::string inputFileName;
77     std::string outPrefix;
78     std::string outMdFileName;
79     std::string outInitFileName;
80     std::string outGeomFileName;
81    
82    
83     BaseLattice *simpleLat;
84     int numMol;
85     double latticeConstant;
86     std::vector<double> lc;
87     double mass;
88     const double rhoConvertConst = 1.661;
89     double density;
90     double rodLength;
91     double rodDiameter;
92    
93    
94     int nx,
95     ny,
96     nz;
97     Mat3x3d hmat;
98     MoLocator *locator;
99     std::vector<Vector3d> latticePos;
100     std::vector<Vector3d> latticeOrt;
101     int numMolPerCell;
102     int curMolIndex;
103     DumpWriter *writer;
104    
105     // parse command line arguments
106     if (cmdline_parser(argc, argv, &args_info) != 0)
107     exit(1);
108    
109    
110    
111     //get lattice type
112     latticeType = UpperCase(args_info.latticetype_arg);
113    
114     if (!LatticeFactory::getInstance()->hasLatticeCreator(latticeType)) {
115     std::cerr << latticeType << " is an invalid lattice type" << std::endl;
116     std::cerr << LatticeFactory::getInstance()->toString() << std::endl;
117     exit(1);
118     }
119    
120    
121     //get input file name
122     if (args_info.inputs_num)
123     inputFileName = args_info.inputs[0];
124     else {
125     std::cerr << "You must specify a input file name.\n" << std::endl;
126     cmdline_parser_print_help();
127     exit(1);
128     }
129    
130     //parse md file and set up the system
131     SimCreator oldCreator;
132     SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
133    
134     if (oldInfo->getNMoleculeStamp()>= 1) {
135     std::cerr << "can not build nanorod with more than one components"
136     << std::endl;
137     exit(1);
138     }
139    
140     //get mass of molecule.
141    
142     mass = getMolMass(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
143    
144     //creat lattice
145     simpleLat = LatticeFactory::getInstance()->createLattice(latticeType);
146    
147     if (simpleLat == NULL) {
148     std::cerr << "Error in creating lattice" << std::endl;
149     exit(1);
150     }
151    
152     numMolPerCell = simpleLat->getNumSitesPerCell();
153    
154     //calculate lattice constant (in Angstrom)
155     //latticeConstant = pow(rhoConvertConst * numMolPerCell * mass / density,
156     // 1.0 / 3.0);
157    
158     latticeConstant = args_info.latticeCnst_arg;
159     rodLength = args_info.length_arg;
160     rodDiameter = args_info.width_arg;
161    
162     //set lattice constant
163     lc.push_back(latticeConstant);
164     simpleLat->setLatticeConstant(lc);
165    
166    
167     //determine the output file names
168     if (args_info.output_given)
169     outInitFileName = args_info.output_arg;
170     else
171     outInitFileName = getPrefix(inputFileName.c_str()) + ".in";
172    
173     //creat Molocator
174     locator = new MoLocator(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
175    
176    
177     //Assume we are carving nanorod out of a cublic block of material and that
178 chuckv 2149 //the shape the material will fit within that block....
179     // The model in geometry builder assumes the long axis is in the y direction and the x-z plane is the
180     // diameter of the particle.
181     // Length first
182     ny = (int)(rodLength/latticeConstant);
183     // Width
184     nx = (int)(rodDiameter/latticeConstant);
185     nz = (int)(rodDiameter/latticeConstant);
186 chuckv 2148
187    
188    
189     // Create geometry for nanocrystal
190     GeometryBuilder myGeometry(rodLength,rodDiameter);
191    
192    
193    
194    
195     //place the molecules
196    
197     curMolIndex = 0;
198    
199     //get the orientation of the cell sites
200     //for the same type of molecule in same lattice, it will not change
201     latticeOrt = simpleLat->getLatticePointsOrt();
202    
203     Molecule* mol;
204     SimInfo::MoleculeIterator mi;
205     mol = oldInfo->beginMolecule(mi);
206     for(int i = 0; i < nx; i++) {
207     for(int j = 0; j < ny; j++) {
208     for(int k = 0; k < nz; k++) {
209    
210     //get the position of the cell sites
211     simpleLat->getLatticePointsPos(latticePos, i, j, k);
212    
213     for(int l = 0; l < numMolPerCell; l++) {
214     if (mol != NULL) {
215     locator->placeMol(latticePos[l], latticeOrt[l], mol);
216     } else {
217     std::cerr << std::endl;
218     }
219     mol = oldInfo->nextMolecule(mi);
220     }
221     }
222     }
223     }
224    
225    
226    
227    
228    
229    
230    
231     //fill Hmat
232     hmat(0, 0)= nx * latticeConstant;
233     hmat(0, 1) = 0.0;
234     hmat(0, 2) = 0.0;
235    
236     hmat(1, 0) = 0.0;
237     hmat(1, 1) = ny * latticeConstant;
238     hmat(1, 2) = 0.0;
239    
240     hmat(2, 0) = 0.0;
241     hmat(2, 1) = 0.0;
242     hmat(2, 2) = nz * latticeConstant;
243    
244     //set Hmat
245     oldInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
246    
247    
248     //create dumpwriter and write out the coordinates
249     oldInfo->setFinalConfigFileName(outInitFileName);
250     writer = new DumpWriter(oldInfo);
251    
252     if (writer == NULL) {
253     std::cerr << "error in creating DumpWriter" << std::endl;
254     exit(1);
255     }
256    
257     writer->writeDump();
258     std::cout << "new initial configuration file: " << outInitFileName
259     << " is generated." << std::endl;
260    
261     //delete objects
262    
263     //delete oldInfo and oldSimSetup
264     if (oldInfo != NULL)
265     delete oldInfo;
266    
267     if (writer != NULL)
268     delete writer;
269    
270     return 0;
271     }
272    
273     void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
274     int numMol) {
275     ifstream oldMdFile;
276     ofstream newMdFile;
277     const int MAXLEN = 65535;
278     char buffer[MAXLEN];
279    
280     //create new .md file based on old .md file
281     oldMdFile.open(oldMdFileName.c_str());
282     newMdFile.open(newMdFileName.c_str());
283    
284     oldMdFile.getline(buffer, MAXLEN);
285    
286     while (!oldMdFile.eof()) {
287    
288     //correct molecule number
289     if (strstr(buffer, "nMol") != NULL) {
290     sprintf(buffer, "\t\tnMol = %d;", numMol);
291     newMdFile << buffer << std::endl;
292     } else
293     newMdFile << buffer << std::endl;
294    
295     oldMdFile.getline(buffer, MAXLEN);
296     }
297    
298     oldMdFile.close();
299     newMdFile.close();
300     }
301