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 outputFileName; |
85 |
std::string outGeomFileName; |
86 |
|
87 |
|
88 |
Lattice *simpleLat; |
89 |
int numMol; |
90 |
RealType latticeConstant; |
91 |
std::vector<RealType> lc; |
92 |
RealType mass; |
93 |
const RealType rhoConvertConst = 1.661; |
94 |
RealType density; |
95 |
RealType rodLength; |
96 |
RealType rodDiameter; |
97 |
|
98 |
|
99 |
int nx, ny, nz; |
100 |
Mat3x3d hmat; |
101 |
MoLocator *locator; |
102 |
std::vector<Vector3d> latticePos; |
103 |
std::vector<Vector3d> latticeOrt; |
104 |
int numMolPerCell; |
105 |
int nComponents; |
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 |
/* get input file name */ |
127 |
if (args_info.inputs_num) |
128 |
inputFileName = args_info.inputs[0]; |
129 |
else { |
130 |
sprintf(painCave.errMsg, "No input .md file name was specified " |
131 |
"on the command line"); |
132 |
painCave.isFatal = 1; |
133 |
cmdline_parser_print_help(); |
134 |
simError(); |
135 |
} |
136 |
|
137 |
//parse md file and set up the system |
138 |
SimCreator oldCreator; |
139 |
SimInfo* oldInfo = oldCreator.createSim(inputFileName, false); |
140 |
Globals* simParams = oldInfo->getSimParams(); |
141 |
|
142 |
nComponents = simParams->getNComponents(); |
143 |
if (nComponents> 1) { |
144 |
sprintf(painCave.errMsg, "Nanorods can only contain a single component "); |
145 |
painCave.isFatal = 1; |
146 |
simError(); |
147 |
} |
148 |
|
149 |
//get mass of molecule. |
150 |
|
151 |
mass = getMolMass(oldInfo->getMoleculeStamp(0), oldInfo->getForceField()); |
152 |
|
153 |
//creat lattice |
154 |
simpleLat = LatticeFactory::getInstance()->createLattice(latticeType); |
155 |
|
156 |
if (simpleLat == NULL) { |
157 |
sprintf(painCave.errMsg, "Error in creating lattice. "); |
158 |
painCave.isFatal = 1; |
159 |
simError(); |
160 |
} |
161 |
|
162 |
numMolPerCell = simpleLat->getNumSitesPerCell(); |
163 |
|
164 |
//calculate lattice constant (in Angstrom) |
165 |
//latticeConstant = pow(rhoConvertConst * numMolPerCell * mass / density, |
166 |
// 1.0 / 3.0); |
167 |
|
168 |
latticeConstant = args_info.latticeCnst_arg; |
169 |
rodLength = args_info.length_arg; |
170 |
rodDiameter = args_info.width_arg; |
171 |
|
172 |
//set lattice constant |
173 |
lc.push_back(latticeConstant); |
174 |
simpleLat->setLatticeConstant(lc); |
175 |
|
176 |
|
177 |
//determine the output file names |
178 |
if (args_info.output_given){ |
179 |
outputFileName = args_info.output_arg; |
180 |
}else{ |
181 |
sprintf(painCave.errMsg, "No output file name was specified " |
182 |
"on the command line"); |
183 |
painCave.isFatal = 1; |
184 |
cmdline_parser_print_help(); |
185 |
simError(); |
186 |
} |
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
//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 |
#ifdef HAVE_CGAL |
212 |
GeometryBuilder *myGeometry; |
213 |
// GeometryBuilder myGeometry(rodLength,rodDiameter); |
214 |
if (args_info.twinnedCrystal_flag){ |
215 |
myGeometry = new GeometryBuilder(rodLength,rodDiameter); |
216 |
} |
217 |
else{ |
218 |
myGeometry = new GeometryBuilder(rodLength,rodDiameter); |
219 |
} |
220 |
|
221 |
if (args_info.genGeomview_given){ |
222 |
if (args_info.genGeomview_flag){ |
223 |
outGeomFileName = getPrefix(inputFileName.c_str()) + ".off"; |
224 |
myGeometry->dumpGeometry(outGeomFileName); |
225 |
} |
226 |
} |
227 |
|
228 |
#endif |
229 |
|
230 |
/* |
231 |
We have to build the system first to figure out how many molecules |
232 |
there are then create a md file and then actually build the |
233 |
system. |
234 |
*/ |
235 |
|
236 |
//place the molecules |
237 |
|
238 |
|
239 |
|
240 |
//get the orientation of the cell sites |
241 |
//for the same type of molecule in same lattice, it will not change |
242 |
latticeOrt = simpleLat->getLatticePointsOrt(); |
243 |
|
244 |
|
245 |
|
246 |
numMol = 0; |
247 |
for(int i = -nx; i < nx; i++) { |
248 |
for(int j = -ny; j < ny; j++) { |
249 |
for(int k = -nz; k < nz; k++) { |
250 |
//if (oldInfo->getNGlobalMolecules() != numMol) { |
251 |
|
252 |
|
253 |
|
254 |
//get the position of the cell sites |
255 |
simpleLat->getLatticePointsPos(latticePos, i, j, k); |
256 |
|
257 |
for(int l = 0; l < numMolPerCell; l++) { |
258 |
|
259 |
#ifdef HAVE_CGAL |
260 |
if (myGeometry->isInsidePolyhedron(latticePos[l][0],latticePos[l][1],latticePos[l][2])){ |
261 |
numMol++; |
262 |
} |
263 |
#else |
264 |
numMol++; |
265 |
#endif |
266 |
} |
267 |
} |
268 |
} |
269 |
} |
270 |
|
271 |
|
272 |
// needed for writing out new md file. |
273 |
|
274 |
|
275 |
|
276 |
|
277 |
//creat new .md file on fly which corrects the number of molecule |
278 |
createMdFile(inputFileName, outputFileName, numMol); |
279 |
|
280 |
if (oldInfo != NULL) |
281 |
delete oldInfo; |
282 |
|
283 |
|
284 |
// We need to read in new siminfo object. |
285 |
//parse md file and set up the system |
286 |
SimCreator newCreator; |
287 |
SimInfo* newInfo = newCreator.createSim(outputFileName, 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 |
sprintf(painCave.errMsg, "Error in placing molecule onto lattice "); |
311 |
painCave.isFatal = 1; |
312 |
simError(); |
313 |
} |
314 |
mol = newInfo->nextMolecule(mi); |
315 |
#ifdef HAVE_CGAL |
316 |
} |
317 |
#endif |
318 |
} |
319 |
} |
320 |
} |
321 |
} |
322 |
|
323 |
|
324 |
|
325 |
//fill Hmat |
326 |
hmat(0, 0)= nx * latticeConstant; |
327 |
hmat(0, 1) = 0.0; |
328 |
hmat(0, 2) = 0.0; |
329 |
|
330 |
hmat(1, 0) = 0.0; |
331 |
hmat(1, 1) = ny * latticeConstant; |
332 |
hmat(1, 2) = 0.0; |
333 |
|
334 |
hmat(2, 0) = 0.0; |
335 |
hmat(2, 1) = 0.0; |
336 |
hmat(2, 2) = nz * latticeConstant; |
337 |
|
338 |
//set Hmat |
339 |
newInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat); |
340 |
|
341 |
|
342 |
//create dumpwriter and write out the coordinates |
343 |
newInfo->setFinalConfigFileName(outputFileName); |
344 |
writer = new DumpWriter(newInfo); |
345 |
|
346 |
if (writer == NULL) { |
347 |
sprintf(painCave.errMsg, "Error in creating DumpWrite object. "); |
348 |
painCave.isFatal = 1; |
349 |
simError(); |
350 |
} |
351 |
|
352 |
writer->writeEor(); |
353 |
std::cout << "new initial configuration file: " << outputFileName |
354 |
<< " is generated." << std::endl; |
355 |
|
356 |
//delete objects |
357 |
|
358 |
//delete oldInfo and oldSimSetup |
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 |
} |
396 |
|