ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/applications/nanoRodBuilder/nanorodBuilder.cpp
Revision: 2252
Committed: Mon May 30 14:01:52 2005 UTC (19 years, 1 month ago) by chuckv
File size: 10773 byte(s)
Log Message:
Added method to remove system angular momentum to velocitizer and added method to calculate system angular momentum to siminfo.

File Contents

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