ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/applications/nanoRodBuilder/nanorodBuilder.cpp
Revision: 2184
Committed: Tue Apr 12 22:33:50 2005 UTC (19 years, 2 months ago) by tim
File size: 10705 byte(s)
Log Message:
refactory lattice

File Contents

# User Rev Content
1 chuckv 2164 /*
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 gezelter 2178 #include "lattice/LatticeFactory.hpp"
54     #include "utils/MoLocator.hpp"
55     #include "lattice/Lattice.hpp"
56 chuckv 2164 #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 tim 2181 registerLattice();
74 chuckv 2164
75     gengetopt_args_info args_info;
76     std::string latticeType;
77     std::string inputFileName;
78     std::string outPrefix;
79     std::string outMdFileName;
80     std::string outInitFileName;
81     std::string outGeomFileName;
82    
83    
84 tim 2184 Lattice *simpleLat;
85 chuckv 2164 int numMol;
86     double latticeConstant;
87     std::vector<double> lc;
88     double mass;
89     const double rhoConvertConst = 1.661;
90     double density;
91     double rodLength;
92     double rodDiameter;
93    
94    
95     int nx,
96     ny,
97     nz;
98     Mat3x3d hmat;
99     MoLocator *locator;
100     std::vector<Vector3d> latticePos;
101     std::vector<Vector3d> latticeOrt;
102     int numMolPerCell;
103     int curMolIndex;
104     DumpWriter *writer;
105    
106     // parse command line arguments
107     if (cmdline_parser(argc, argv, &args_info) != 0)
108     exit(1);
109    
110    
111    
112     //get lattice type
113     latticeType = UpperCase(args_info.latticetype_arg);
114    
115    
116 tim 2184 simpleLat = LatticeFactory::getInstance()->createLattice(latticeType);
117     if (simpleLat == NULL) {
118     sprintf(painCave.errMsg, "Lattice Factory can not create %s lattice\n",
119     latticeType.c_str());
120     painCave.isFatal = 1;
121     simError();
122     }
123    
124 chuckv 2164 //get input file name
125     if (args_info.inputs_num)
126     inputFileName = args_info.inputs[0];
127     else {
128     std::cerr << "You must specify a input file name.\n" << std::endl;
129     cmdline_parser_print_help();
130     exit(1);
131     }
132    
133     //parse md file and set up the system
134     SimCreator oldCreator;
135     SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
136    
137     if (oldInfo->getNMoleculeStamp()> 1) {
138     std::cerr << "can not build nanorod with more than one components"
139     << std::endl;
140     exit(1);
141     }
142    
143     //get mass of molecule.
144    
145     mass = getMolMass(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
146    
147     //creat lattice
148     simpleLat = LatticeFactory::getInstance()->createLattice(latticeType);
149    
150     if (simpleLat == NULL) {
151     std::cerr << "Error in creating lattice" << std::endl;
152     exit(1);
153     }
154    
155     numMolPerCell = simpleLat->getNumSitesPerCell();
156    
157     //calculate lattice constant (in Angstrom)
158     //latticeConstant = pow(rhoConvertConst * numMolPerCell * mass / density,
159     // 1.0 / 3.0);
160    
161     latticeConstant = args_info.latticeCnst_arg;
162     rodLength = args_info.length_arg;
163     rodDiameter = args_info.width_arg;
164    
165     //set lattice constant
166     lc.push_back(latticeConstant);
167     simpleLat->setLatticeConstant(lc);
168    
169    
170     //determine the output file names
171     if (args_info.output_given)
172     outInitFileName = args_info.output_arg;
173     else
174     outInitFileName = getPrefix(inputFileName.c_str()) + ".in";
175    
176     //creat Molocator
177     locator = new MoLocator(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
178    
179     /*
180     Assume we are carving nanorod out of a cublic block of material and that
181     the shape the material will fit within that block....
182     The model in geometry builder assumes the long axis is in the y direction and the x-z plane is the
183     diameter of the particle.
184     */
185     // Number of Unit Cells in Length first
186     ny = (int)(rodLength/latticeConstant);
187     // Number of unit cells in Width
188     nx = (int)(rodDiameter/latticeConstant);
189     nz = (int)(rodDiameter/latticeConstant);
190    
191    
192    
193     // Create geometry for nanocrystal
194     //GeometryBuilder myGeometry(rodLength,rodDiameter);
195    
196     /*
197     We have to build the system first to figure out how many molecules there are
198     then create a md file and then actually build the system.
199     */
200    
201     //place the molecules
202    
203     curMolIndex = 0;
204    
205     //get the orientation of the cell sites
206     //for the same type of molecule in same lattice, it will not change
207     latticeOrt = simpleLat->getLatticePointsOrt();
208    
209    
210     /*
211     void BaseLattice::getLatticePointsPos(std::vector<Vector3d>& latticePos, int nx, int ny, int nz){
212    
213     latticePos.resize(nCellSites);
214    
215     for( int i=0;i < nCellSites;i++){
216    
217     latticePos[i][0] = origin[0] + cellSitesPos[i][0] + cellLen[0] * (double(nx) - 0.5);
218     latticePos[i][1] = origin[1] + cellSitesPos[i][1] + cellLen[1] * (double(ny) - 0.5);
219     latticePos[i][2] = origin[2] + cellSitesPos[i][2] + cellLen[2] * (double(nz) - 0.5);
220     }
221    
222     */
223    
224    
225    
226    
227     numMol = 0;
228     for(int i = 0; i < nx; i++) {
229     for(int j = 0; j < ny; j++) {
230     for(int k = 0; k < nz; k++) {
231     //if (oldInfo->getNGlobalMolecules() != numMol) {
232    
233    
234    
235     //get the position of the cell sites
236     simpleLat->getLatticePointsPos(latticePos, i, j, k);
237    
238     for(int l = 0; l < numMolPerCell; l++) {
239     /*
240     if (myGeometry.isInsidePolyhedron(latticePos[l][0],latticePos[l][1],latticePos[l][2])){
241     numMol++;
242     }
243     */
244     numMol++;
245     }
246     }
247     }
248     }
249    
250    
251     // needed for writing out new md file.
252    
253     outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType;
254     outMdFileName = outPrefix + ".md";
255    
256     //creat new .md file on fly which corrects the number of molecule
257     createMdFile(inputFileName, outMdFileName, numMol);
258    
259     if (oldInfo != NULL)
260     delete oldInfo;
261    
262    
263     // We need to read in new siminfo object.
264     //parse md file and set up the system
265     //SimCreator NewCreator;
266    
267     SimInfo* NewInfo = oldCreator.createSim(outMdFileName, false);
268    
269     // This was so much fun the first time, lets do it again.
270    
271     Molecule* mol;
272     SimInfo::MoleculeIterator mi;
273     mol = NewInfo->beginMolecule(mi);
274    
275     for(int i = 0; i < nx; i++) {
276     for(int j = 0; j < ny; j++) {
277     for(int k = 0; k < nz; k++) {
278    
279     //get the position of the cell sites
280     simpleLat->getLatticePointsPos(latticePos, i, j, k);
281    
282     for(int l = 0; l < numMolPerCell; l++) {
283     if (mol != NULL) {
284     /*
285     if (myGeometry.isInsidePolyhedron(latticePos[l][0],latticePos[l][1],latticePos[l][2])){
286     locator->placeMol(latticePos[l], latticeOrt[l], mol);
287     }
288     */
289     locator->placeMol(latticePos[l], latticeOrt[l], mol);
290     } else {
291     std::cerr << std::endl;
292     }
293     mol = NewInfo->nextMolecule(mi);
294     }
295     }
296     }
297     }
298    
299    
300    
301     //fill Hmat
302     hmat(0, 0)= nx * latticeConstant;
303     hmat(0, 1) = 0.0;
304     hmat(0, 2) = 0.0;
305    
306     hmat(1, 0) = 0.0;
307     hmat(1, 1) = ny * latticeConstant;
308     hmat(1, 2) = 0.0;
309    
310     hmat(2, 0) = 0.0;
311     hmat(2, 1) = 0.0;
312     hmat(2, 2) = nz * latticeConstant;
313    
314     //set Hmat
315     NewInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
316    
317    
318     //create dumpwriter and write out the coordinates
319     NewInfo->setFinalConfigFileName(outInitFileName);
320     writer = new DumpWriter(NewInfo);
321    
322     if (writer == NULL) {
323     std::cerr << "error in creating DumpWriter" << std::endl;
324     exit(1);
325     }
326    
327     writer->writeDump();
328     std::cout << "new initial configuration file: " << outInitFileName
329     << " is generated." << std::endl;
330    
331     //delete objects
332    
333     //delete oldInfo and oldSimSetup
334    
335     if (NewInfo != NULL)
336     delete NewInfo;
337    
338     if (writer != NULL)
339     delete writer;
340    
341     return 0;
342     }
343    
344     void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
345     int numMol) {
346     ifstream oldMdFile;
347     ofstream newMdFile;
348     const int MAXLEN = 65535;
349     char buffer[MAXLEN];
350    
351     //create new .md file based on old .md file
352     oldMdFile.open(oldMdFileName.c_str());
353     newMdFile.open(newMdFileName.c_str());
354    
355     oldMdFile.getline(buffer, MAXLEN);
356    
357     while (!oldMdFile.eof()) {
358    
359     //correct molecule number
360     if (strstr(buffer, "nMol") != NULL) {
361     sprintf(buffer, "\tnMol = %i;", numMol);
362     newMdFile << buffer << std::endl;
363     } else
364     newMdFile << buffer << std::endl;
365    
366     oldMdFile.getline(buffer, MAXLEN);
367     }
368    
369     oldMdFile.close();
370     newMdFile.close();
371     }
372