ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/applications/nanoRodBuilder/nanorodBuilder.cpp
Revision: 2204
Committed: Fri Apr 15 22:04:00 2005 UTC (19 years, 2 months ago) by gezelter
File size: 10897 byte(s)
Log Message:
xemacs has been drafted to perform our indentation services

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