ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/applications/nanoRodBuilder/nanorodBuilder.cpp
Revision: 2181
Committed: Tue Apr 12 21:58:09 2005 UTC (19 years, 3 months ago) by tim
File size: 10685 byte(s)
Log Message:
refactory LatticeFactory and LatticeCreator

File Contents

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