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