1 |
gezelter |
507 |
/* |
2 |
chuckv |
467 |
* 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 |
493 |
|
42 |
chuckv |
467 |
#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 |
493 |
#include "config.h" |
52 |
|
|
|
53 |
chuckv |
467 |
#include "nanorodBuilderCmd.h" |
54 |
gezelter |
493 |
#ifdef HAVE_CGAL |
55 |
|
|
#include "GeometryBuilder.hpp" |
56 |
|
|
#endif |
57 |
gezelter |
481 |
#include "lattice/LatticeFactory.hpp" |
58 |
|
|
#include "utils/MoLocator.hpp" |
59 |
|
|
#include "lattice/Lattice.hpp" |
60 |
chuckv |
467 |
#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 |
493 |
void createMdFile(const std::string&oldMdFileName, |
71 |
|
|
const std::string&newMdFileName, |
72 |
chuckv |
467 |
int numMol); |
73 |
|
|
|
74 |
|
|
int main(int argc, char *argv []) { |
75 |
gezelter |
493 |
|
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 |
507 |
// Check for lib CGAL, if we don't have it, we should exit.... |
115 |
chuckv |
499 |
|
116 |
|
|
#ifndef HAVE_CGAL |
117 |
gezelter |
507 |
std::cerr << "nanoRodBuilder requires libCGAL to function, please rebuild OOPSE with libCGAL." |
118 |
|
|
<< std::endl; |
119 |
|
|
exit(1); |
120 |
chuckv |
499 |
#endif |
121 |
|
|
|
122 |
|
|
|
123 |
|
|
|
124 |
gezelter |
493 |
//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 |
chuckv |
518 |
|
189 |
|
|
|
190 |
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
gezelter |
493 |
//creat Molocator |
194 |
|
|
locator = new MoLocator(oldInfo->getMoleculeStamp(0), oldInfo->getForceField()); |
195 |
|
|
|
196 |
|
|
/* |
197 |
|
|
Assume we are carving nanorod out of a cublic block of material and that |
198 |
|
|
the shape the material will fit within that block.... |
199 |
|
|
The model in geometry builder assumes the long axis is in the y direction and the x-z plane is the |
200 |
|
|
diameter of the particle. |
201 |
|
|
*/ |
202 |
|
|
// Number of Unit Cells in Length first |
203 |
|
|
ny = (int)(rodLength/latticeConstant); |
204 |
|
|
// Number of unit cells in Width |
205 |
|
|
nx = (int)(rodDiameter/latticeConstant); |
206 |
|
|
nz = (int)(rodDiameter/latticeConstant); |
207 |
|
|
|
208 |
|
|
|
209 |
|
|
|
210 |
|
|
// Create geometry for nanocrystal |
211 |
chuckv |
499 |
#ifdef HAVE_CGAL |
212 |
gezelter |
493 |
GeometryBuilder myGeometry(rodLength,rodDiameter); |
213 |
chuckv |
518 |
if (args_info.genGeomview_given){ |
214 |
|
|
if (args_info.genGeomview_flag){ |
215 |
|
|
outGeomFileName = getPrefix(inputFileName.c_str()) + ".off"; |
216 |
|
|
myGeometry.dumpGeometry(outGeomFileName); |
217 |
|
|
} |
218 |
|
|
} |
219 |
|
|
|
220 |
gezelter |
493 |
#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 |
|
|
curMolIndex = 0; |
231 |
|
|
|
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 |
|
|
void BaseLattice::getLatticePointsPos(std::vector<Vector3d>& |
239 |
|
|
latticePos, int nx, int ny, int nz){ |
240 |
chuckv |
467 |
|
241 |
gezelter |
493 |
latticePos.resize(nCellSites); |
242 |
|
|
|
243 |
|
|
for( int i=0;i < nCellSites;i++){ |
244 |
chuckv |
467 |
|
245 |
gezelter |
493 |
latticePos[i][0] = origin[0] + cellSitesPos[i][0] + cellLen[0] * (double(nx) - 0.5); |
246 |
|
|
latticePos[i][1] = origin[1] + cellSitesPos[i][1] + cellLen[1] * (double(ny) - 0.5); |
247 |
|
|
latticePos[i][2] = origin[2] + cellSitesPos[i][2] + cellLen[2] * (double(nz) - 0.5); |
248 |
chuckv |
467 |
} |
249 |
|
|
|
250 |
gezelter |
493 |
*/ |
251 |
|
|
|
252 |
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
|
|
numMol = 0; |
256 |
chuckv |
518 |
for(int i = -nx; i < nx; i++) { |
257 |
|
|
for(int j = -ny; j < ny; j++) { |
258 |
|
|
for(int k = -nz; k < nz; k++) { |
259 |
gezelter |
493 |
//if (oldInfo->getNGlobalMolecules() != numMol) { |
260 |
chuckv |
467 |
|
261 |
gezelter |
493 |
|
262 |
|
|
|
263 |
|
|
//get the position of the cell sites |
264 |
|
|
simpleLat->getLatticePointsPos(latticePos, i, j, k); |
265 |
|
|
|
266 |
|
|
for(int l = 0; l < numMolPerCell; l++) { |
267 |
chuckv |
467 |
|
268 |
chuckv |
499 |
#ifdef HAVE_CGAL |
269 |
gezelter |
493 |
if (myGeometry.isInsidePolyhedron(latticePos[l][0],latticePos[l][1],latticePos[l][2])){ |
270 |
|
|
numMol++; |
271 |
|
|
} |
272 |
|
|
#endif |
273 |
|
|
} |
274 |
|
|
} |
275 |
chuckv |
467 |
} |
276 |
gezelter |
493 |
} |
277 |
chuckv |
518 |
std::cerr << "numMol before is " << numMol << std::endl; |
278 |
gezelter |
493 |
|
279 |
|
|
// needed for writing out new md file. |
280 |
|
|
|
281 |
|
|
outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType; |
282 |
|
|
outMdFileName = outPrefix + ".md"; |
283 |
|
|
|
284 |
|
|
//creat new .md file on fly which corrects the number of molecule |
285 |
|
|
createMdFile(inputFileName, outMdFileName, numMol); |
286 |
|
|
|
287 |
|
|
if (oldInfo != NULL) |
288 |
|
|
delete oldInfo; |
289 |
|
|
|
290 |
|
|
|
291 |
|
|
// We need to read in new siminfo object. |
292 |
|
|
//parse md file and set up the system |
293 |
|
|
//SimCreator NewCreator; |
294 |
|
|
|
295 |
|
|
SimInfo* NewInfo = oldCreator.createSim(outMdFileName, false); |
296 |
|
|
|
297 |
|
|
// This was so much fun the first time, lets do it again. |
298 |
|
|
|
299 |
|
|
Molecule* mol; |
300 |
|
|
SimInfo::MoleculeIterator mi; |
301 |
|
|
mol = NewInfo->beginMolecule(mi); |
302 |
chuckv |
518 |
numMol = 0; |
303 |
|
|
int countMol = 0; |
304 |
|
|
for(int i = -nx; i < nx; i++) { |
305 |
|
|
for(int j = -ny; j < ny; j++) { |
306 |
|
|
for(int k = -nz; k < nz; k++) { |
307 |
|
|
|
308 |
|
|
//get the position of the cell sites |
309 |
|
|
simpleLat->getLatticePointsPos(latticePos, i, j, k); |
310 |
|
|
|
311 |
|
|
for(int l = 0; l < numMolPerCell; l++) { |
312 |
|
|
if (myGeometry.isInsidePolyhedron(latticePos[l][0],latticePos[l][1],latticePos[l][2])){ |
313 |
|
|
countMol++; |
314 |
|
|
if (mol != NULL) { |
315 |
|
|
locator->placeMol(latticePos[l], latticeOrt[l], mol); |
316 |
|
|
numMol++; |
317 |
|
|
} else { |
318 |
|
|
std::cerr<<"Error in placing molecule " << countMol << std::endl; |
319 |
|
|
} |
320 |
|
|
mol = NewInfo->nextMolecule(mi); |
321 |
|
|
} |
322 |
|
|
|
323 |
|
|
} |
324 |
gezelter |
493 |
} |
325 |
chuckv |
518 |
} |
326 |
gezelter |
493 |
} |
327 |
|
|
|
328 |
chuckv |
518 |
std::cerr << "numMol after is " << numMol << std::endl; |
329 |
gezelter |
493 |
|
330 |
|
|
//fill Hmat |
331 |
|
|
hmat(0, 0)= nx * latticeConstant; |
332 |
|
|
hmat(0, 1) = 0.0; |
333 |
|
|
hmat(0, 2) = 0.0; |
334 |
|
|
|
335 |
|
|
hmat(1, 0) = 0.0; |
336 |
|
|
hmat(1, 1) = ny * latticeConstant; |
337 |
|
|
hmat(1, 2) = 0.0; |
338 |
|
|
|
339 |
|
|
hmat(2, 0) = 0.0; |
340 |
|
|
hmat(2, 1) = 0.0; |
341 |
|
|
hmat(2, 2) = nz * latticeConstant; |
342 |
|
|
|
343 |
|
|
//set Hmat |
344 |
|
|
NewInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat); |
345 |
|
|
|
346 |
|
|
|
347 |
|
|
//create dumpwriter and write out the coordinates |
348 |
|
|
NewInfo->setFinalConfigFileName(outInitFileName); |
349 |
|
|
writer = new DumpWriter(NewInfo); |
350 |
|
|
|
351 |
|
|
if (writer == NULL) { |
352 |
|
|
std::cerr << "error in creating DumpWriter" << std::endl; |
353 |
|
|
exit(1); |
354 |
gezelter |
507 |
} |
355 |
gezelter |
493 |
|
356 |
|
|
writer->writeDump(); |
357 |
|
|
std::cout << "new initial configuration file: " << outInitFileName |
358 |
|
|
<< " is generated." << std::endl; |
359 |
|
|
|
360 |
|
|
//delete objects |
361 |
|
|
|
362 |
|
|
//delete oldInfo and oldSimSetup |
363 |
|
|
|
364 |
|
|
if (NewInfo != NULL) |
365 |
|
|
delete NewInfo; |
366 |
|
|
|
367 |
|
|
if (writer != NULL) |
368 |
|
|
delete writer; |
369 |
|
|
delete simpleLat; |
370 |
|
|
cmdline_parser_free(&args_info); |
371 |
|
|
return 0; |
372 |
chuckv |
467 |
} |
373 |
|
|
|
374 |
|
|
void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName, |
375 |
|
|
int numMol) { |
376 |
gezelter |
493 |
ifstream oldMdFile; |
377 |
|
|
ofstream newMdFile; |
378 |
|
|
const int MAXLEN = 65535; |
379 |
|
|
char buffer[MAXLEN]; |
380 |
|
|
|
381 |
|
|
//create new .md file based on old .md file |
382 |
|
|
oldMdFile.open(oldMdFileName.c_str()); |
383 |
|
|
newMdFile.open(newMdFileName.c_str()); |
384 |
|
|
|
385 |
|
|
oldMdFile.getline(buffer, MAXLEN); |
386 |
|
|
|
387 |
|
|
while (!oldMdFile.eof()) { |
388 |
|
|
|
389 |
|
|
//correct molecule number |
390 |
|
|
if (strstr(buffer, "nMol") != NULL) { |
391 |
|
|
sprintf(buffer, "\tnMol = %i;", numMol); |
392 |
|
|
newMdFile << buffer << std::endl; |
393 |
|
|
} else |
394 |
|
|
newMdFile << buffer << std::endl; |
395 |
|
|
|
396 |
chuckv |
467 |
oldMdFile.getline(buffer, MAXLEN); |
397 |
gezelter |
493 |
} |
398 |
|
|
|
399 |
|
|
oldMdFile.close(); |
400 |
|
|
newMdFile.close(); |
401 |
chuckv |
467 |
} |
402 |
|
|
|