ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/applications/nanoRodBuilder/nanorodBuilder.cpp
Revision: 2218
Committed: Tue May 3 17:55:28 2005 UTC (19 years, 2 months ago) by chuckv
File size: 10982 byte(s)
Log Message:
More changes to nanoRodBuilder....

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