ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/tags/start/nanorodBuilder/nanorodBuilder.cpp
Revision: 3484
Committed: Tue Jan 13 14:39:51 2009 UTC (15 years, 7 months ago)
File size: 8443 byte(s)
Log Message:
This commit was manufactured by cvs2svn to create tag 'start'.

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     //the shape the material will fit within that block....
179    
180    
181    
182     // Create geometry for nanocrystal
183     GeometryBuilder myGeometry(rodLength,rodDiameter);
184    
185    
186    
187    
188     //place the molecules
189    
190     curMolIndex = 0;
191    
192     //get the orientation of the cell sites
193     //for the same type of molecule in same lattice, it will not change
194     latticeOrt = simpleLat->getLatticePointsOrt();
195    
196     Molecule* mol;
197     SimInfo::MoleculeIterator mi;
198     mol = oldInfo->beginMolecule(mi);
199     for(int i = 0; i < nx; i++) {
200     for(int j = 0; j < ny; j++) {
201     for(int k = 0; k < nz; k++) {
202    
203     //get the position of the cell sites
204     simpleLat->getLatticePointsPos(latticePos, i, j, k);
205    
206     for(int l = 0; l < numMolPerCell; l++) {
207     if (mol != NULL) {
208     locator->placeMol(latticePos[l], latticeOrt[l], mol);
209     } else {
210     std::cerr << std::endl;
211     }
212     mol = oldInfo->nextMolecule(mi);
213     }
214     }
215     }
216     }
217    
218    
219    
220    
221    
222    
223    
224     //fill Hmat
225     hmat(0, 0)= nx * latticeConstant;
226     hmat(0, 1) = 0.0;
227     hmat(0, 2) = 0.0;
228    
229     hmat(1, 0) = 0.0;
230     hmat(1, 1) = ny * latticeConstant;
231     hmat(1, 2) = 0.0;
232    
233     hmat(2, 0) = 0.0;
234     hmat(2, 1) = 0.0;
235     hmat(2, 2) = nz * latticeConstant;
236    
237     //set Hmat
238     oldInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
239    
240    
241     //create dumpwriter and write out the coordinates
242     oldInfo->setFinalConfigFileName(outInitFileName);
243     writer = new DumpWriter(oldInfo);
244    
245     if (writer == NULL) {
246     std::cerr << "error in creating DumpWriter" << std::endl;
247     exit(1);
248     }
249    
250     writer->writeDump();
251     std::cout << "new initial configuration file: " << outInitFileName
252     << " is generated." << std::endl;
253    
254     //delete objects
255    
256     //delete oldInfo and oldSimSetup
257     if (oldInfo != NULL)
258     delete oldInfo;
259    
260     if (writer != NULL)
261     delete writer;
262    
263     return 0;
264     }
265    
266     void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
267     int numMol) {
268     ifstream oldMdFile;
269     ofstream newMdFile;
270     const int MAXLEN = 65535;
271     char buffer[MAXLEN];
272    
273     //create new .md file based on old .md file
274     oldMdFile.open(oldMdFileName.c_str());
275     newMdFile.open(newMdFileName.c_str());
276    
277     oldMdFile.getline(buffer, MAXLEN);
278    
279     while (!oldMdFile.eof()) {
280    
281     //correct molecule number
282     if (strstr(buffer, "nMol") != NULL) {
283     sprintf(buffer, "\t\tnMol = %d;", numMol);
284     newMdFile << buffer << std::endl;
285     } else
286     newMdFile << buffer << std::endl;
287    
288     oldMdFile.getline(buffer, MAXLEN);
289     }
290    
291     oldMdFile.close();
292     newMdFile.close();
293     }
294