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. Redistributions of source code must retain the above copyright |
10 |
* notice, this list of conditions and the following disclaimer. |
11 |
* |
12 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
* notice, this list of conditions and the following disclaimer in the |
14 |
* documentation and/or other materials provided with the |
15 |
* distribution. |
16 |
* |
17 |
* This software is provided "AS IS," without a warranty of any |
18 |
* kind. All express or implied conditions, representations and |
19 |
* warranties, including any implied warranty of merchantability, |
20 |
* fitness for a particular purpose or non-infringement, are hereby |
21 |
* excluded. The University of Notre Dame and its licensors shall not |
22 |
* be liable for any damages suffered by licensee as a result of |
23 |
* using, modifying or distributing the software or its |
24 |
* derivatives. In no event will the University of Notre Dame or its |
25 |
* licensors be liable for any lost revenue, profit or data, or for |
26 |
* direct, indirect, special, consequential, incidental or punitive |
27 |
* damages, however caused and regardless of the theory of liability, |
28 |
* arising out of the use of or inability to use software, even if the |
29 |
* University of Notre Dame has been advised of the possibility of |
30 |
* such damages. |
31 |
* |
32 |
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
* research, please cite the appropriate papers when you publish your |
34 |
* work. Good starting points are: |
35 |
* |
36 |
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
39 |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
*/ |
42 |
|
43 |
#include <cstdlib> |
44 |
#include <cstdio> |
45 |
#include <cstring> |
46 |
#include <cmath> |
47 |
#include <iostream> |
48 |
#include <string> |
49 |
#include <map> |
50 |
#include <fstream> |
51 |
#include <algorithm> |
52 |
|
53 |
#include "config.h" |
54 |
#include "shapedLatticeSpherical.hpp" |
55 |
#include "nanoparticleBuilderCmd.h" |
56 |
#include "lattice/LatticeFactory.hpp" |
57 |
#include "utils/MoLocator.hpp" |
58 |
#include "lattice/Lattice.hpp" |
59 |
#include "brains/Register.hpp" |
60 |
#include "brains/SimInfo.hpp" |
61 |
#include "brains/SimCreator.hpp" |
62 |
#include "io/DumpWriter.hpp" |
63 |
#include "math/Vector3.hpp" |
64 |
#include "math/SquareMatrix3.hpp" |
65 |
#include "utils/StringUtils.hpp" |
66 |
|
67 |
using namespace std; |
68 |
using namespace OpenMD; |
69 |
void createMdFile(const std::string&oldMdFileName, |
70 |
const std::string&newMdFileName, |
71 |
std::vector<int> numMol); |
72 |
|
73 |
int main(int argc, char *argv []) { |
74 |
|
75 |
//register force fields |
76 |
registerForceFields(); |
77 |
registerLattice(); |
78 |
|
79 |
gengetopt_args_info args_info; |
80 |
std::string latticeType; |
81 |
std::string inputFileName; |
82 |
std::string outputFileName; |
83 |
|
84 |
MoLocator* locator; |
85 |
int nComponents; |
86 |
double latticeConstant; |
87 |
std::vector<double> lc; |
88 |
|
89 |
RealType particleRadius; |
90 |
|
91 |
Mat3x3d hmat; |
92 |
std::vector<Vector3d> latticePos; |
93 |
std::vector<Vector3d> latticeOrt; |
94 |
|
95 |
DumpWriter *writer; |
96 |
|
97 |
// Parse Command Line Arguments |
98 |
if (cmdline_parser(argc, argv, &args_info) != 0) |
99 |
exit(1); |
100 |
|
101 |
/* get lattice type */ |
102 |
latticeType = "FCC"; |
103 |
|
104 |
/* get input file name */ |
105 |
if (args_info.inputs_num) |
106 |
inputFileName = args_info.inputs[0]; |
107 |
else { |
108 |
sprintf(painCave.errMsg, "No input .md file name was specified " |
109 |
"on the command line"); |
110 |
painCave.isFatal = 1; |
111 |
cmdline_parser_print_help(); |
112 |
simError(); |
113 |
} |
114 |
|
115 |
/* parse md file and set up the system */ |
116 |
SimCreator oldCreator; |
117 |
SimInfo* oldInfo = oldCreator.createSim(inputFileName, false); |
118 |
|
119 |
latticeConstant = args_info.latticeConstant_arg; |
120 |
particleRadius = args_info.radius_arg; |
121 |
Globals* simParams = oldInfo->getSimParams(); |
122 |
|
123 |
/* Create nanoparticle */ |
124 |
shapedLatticeSpherical nanoParticle(latticeConstant, latticeType, |
125 |
particleRadius); |
126 |
|
127 |
/* Build a lattice and get lattice points for this lattice constant */ |
128 |
vector<Vector3d> sites = nanoParticle.getSites(); |
129 |
vector<Vector3d> orientations = nanoParticle.getOrientations(); |
130 |
|
131 |
|
132 |
std::vector<int> vacancyTargets; |
133 |
vector<bool> isVacancy; |
134 |
|
135 |
Vector3d myLoc; |
136 |
RealType myR; |
137 |
|
138 |
for (int i = 0; i < sites.size(); i++) |
139 |
isVacancy.push_back(false); |
140 |
|
141 |
if (args_info.vacancyPercent_given) { |
142 |
if (args_info.vacancyPercent_arg < 0.0 || args_info.vacancyPercent_arg > 100.0) { |
143 |
sprintf(painCave.errMsg, "vacancyPercent was set to a non-sensical value."); |
144 |
painCave.isFatal = 1; |
145 |
simError(); |
146 |
} else { |
147 |
RealType vF = args_info.vacancyPercent_arg / 100.0; |
148 |
RealType vIR; |
149 |
RealType vOR; |
150 |
if (args_info.vacancyInnerRadius_given) { |
151 |
vIR = args_info.vacancyInnerRadius_arg; |
152 |
} else { |
153 |
vIR = 0.0; |
154 |
} |
155 |
if (args_info.vacancyOuterRadius_given) { |
156 |
vOR = args_info.vacancyOuterRadius_arg; |
157 |
} else { |
158 |
vOR = particleRadius; |
159 |
} |
160 |
if (vIR >= 0.0 && vOR <= particleRadius && vOR >= vIR) { |
161 |
|
162 |
for (int i = 0; i < sites.size(); i++) { |
163 |
myLoc = sites[i]; |
164 |
myR = myLoc.length(); |
165 |
if (myR >= vIR && myR <= vOR) { |
166 |
vacancyTargets.push_back(i); |
167 |
} |
168 |
} |
169 |
std::random_shuffle(vacancyTargets.begin(), vacancyTargets.end()); |
170 |
|
171 |
int nTargets = vacancyTargets.size(); |
172 |
vacancyTargets.resize((int)(vF * nTargets)); |
173 |
|
174 |
|
175 |
sprintf(painCave.errMsg, "Removing %d atoms from randomly-selected\n" |
176 |
"\tsites between %lf and %lf.", (int) vacancyTargets.size(), |
177 |
vIR, vOR); |
178 |
painCave.isFatal = 0; |
179 |
simError(); |
180 |
|
181 |
isVacancy.clear(); |
182 |
for (int i = 0; i < sites.size(); i++) { |
183 |
bool vac = false; |
184 |
for (int j = 0; j < vacancyTargets.size(); j++) { |
185 |
if (i == vacancyTargets[j]) vac = true; |
186 |
} |
187 |
isVacancy.push_back(vac); |
188 |
} |
189 |
|
190 |
} else { |
191 |
sprintf(painCave.errMsg, "Something is strange about the vacancy\n" |
192 |
"\tinner or outer radii. Check their values."); |
193 |
painCave.isFatal = 1; |
194 |
simError(); |
195 |
} |
196 |
} |
197 |
} |
198 |
|
199 |
/* Get number of lattice sites */ |
200 |
int nSites = sites.size() - vacancyTargets.size(); |
201 |
|
202 |
std::vector<Component*> components = simParams->getComponents(); |
203 |
std::vector<RealType> molFractions; |
204 |
std::vector<RealType> shellRadii; |
205 |
std::vector<RealType> molecularMasses; |
206 |
std::vector<int> nMol; |
207 |
std::map<int, int> componentFromSite; |
208 |
nComponents = components.size(); |
209 |
|
210 |
if (args_info.molFraction_given && args_info.shellRadius_given) { |
211 |
sprintf(painCave.errMsg, "Specify either molFraction or shellRadius " |
212 |
"arguments, but not both!"); |
213 |
painCave.isFatal = 1; |
214 |
simError(); |
215 |
} |
216 |
|
217 |
if (nComponents == 1) { |
218 |
molFractions.push_back(1.0); |
219 |
shellRadii.push_back(particleRadius); |
220 |
} else if (args_info.molFraction_given) { |
221 |
if ((int)args_info.molFraction_given == nComponents) { |
222 |
for (int i = 0; i < nComponents; i++) { |
223 |
molFractions.push_back(args_info.molFraction_arg[i]); |
224 |
} |
225 |
} else if ((int)args_info.molFraction_given == nComponents-1) { |
226 |
RealType remainingFraction = 1.0; |
227 |
for (int i = 0; i < nComponents-1; i++) { |
228 |
molFractions.push_back(args_info.molFraction_arg[i]); |
229 |
remainingFraction -= molFractions[i]; |
230 |
} |
231 |
molFractions.push_back(remainingFraction); |
232 |
} else { |
233 |
sprintf(painCave.errMsg, "nanoparticleBuilder can't figure out molFractions " |
234 |
"for all of the components in the <MetaData> block."); |
235 |
painCave.isFatal = 1; |
236 |
simError(); |
237 |
} |
238 |
} else if ((int)args_info.shellRadius_given) { |
239 |
if ((int)args_info.shellRadius_given == nComponents) { |
240 |
for (int i = 0; i < nComponents; i++) { |
241 |
shellRadii.push_back(args_info.shellRadius_arg[i]); |
242 |
} |
243 |
} else if ((int)args_info.shellRadius_given == nComponents-1) { |
244 |
for (int i = 0; i < nComponents-1; i++) { |
245 |
shellRadii.push_back(args_info.shellRadius_arg[i]); |
246 |
} |
247 |
shellRadii.push_back(particleRadius); |
248 |
} else { |
249 |
sprintf(painCave.errMsg, "nanoparticleBuilder can't figure out the\n" |
250 |
"\tshell radii for all of the components in the <MetaData> block."); |
251 |
painCave.isFatal = 1; |
252 |
simError(); |
253 |
} |
254 |
} else { |
255 |
sprintf(painCave.errMsg, "You have a multi-component <MetaData> block,\n" |
256 |
"\tbut have not specified either molFraction or shellRadius arguments."); |
257 |
painCave.isFatal = 1; |
258 |
simError(); |
259 |
} |
260 |
|
261 |
if (args_info.molFraction_given) { |
262 |
RealType totalFraction = 0.0; |
263 |
|
264 |
/* Do some simple sanity checking*/ |
265 |
|
266 |
for (int i = 0; i < nComponents; i++) { |
267 |
if (molFractions.at(i) < 0.0) { |
268 |
sprintf(painCave.errMsg, "One of the requested molFractions was" |
269 |
" less than zero!"); |
270 |
painCave.isFatal = 1; |
271 |
simError(); |
272 |
} |
273 |
if (molFractions.at(i) > 1.0) { |
274 |
sprintf(painCave.errMsg, "One of the requested molFractions was" |
275 |
" greater than one!"); |
276 |
painCave.isFatal = 1; |
277 |
simError(); |
278 |
} |
279 |
totalFraction += molFractions.at(i); |
280 |
} |
281 |
if (abs(totalFraction - 1.0) > 1e-6) { |
282 |
sprintf(painCave.errMsg, "The sum of molFractions was not close enough to 1.0"); |
283 |
painCave.isFatal = 1; |
284 |
simError(); |
285 |
} |
286 |
|
287 |
int remaining = nSites; |
288 |
for (int i=0; i < nComponents-1; i++) { |
289 |
nMol.push_back(int((RealType)nSites * molFractions.at(i))); |
290 |
remaining -= nMol.at(i); |
291 |
} |
292 |
nMol.push_back(remaining); |
293 |
|
294 |
// recompute actual mol fractions and perform final sanity check: |
295 |
|
296 |
int totalMolecules = 0; |
297 |
for (int i=0; i < nComponents; i++) { |
298 |
molFractions[i] = (RealType)(nMol.at(i))/(RealType)nSites; |
299 |
totalMolecules += nMol.at(i); |
300 |
} |
301 |
|
302 |
if (totalMolecules != nSites) { |
303 |
sprintf(painCave.errMsg, "Computed total number of molecules is not equal " |
304 |
"to the number of lattice sites!"); |
305 |
painCave.isFatal = 1; |
306 |
simError(); |
307 |
} |
308 |
} else { |
309 |
|
310 |
for (int i = 0; i < shellRadii.size(); i++) { |
311 |
if (shellRadii.at(i) > particleRadius + 1e-6 ) { |
312 |
sprintf(painCave.errMsg, "One of the shellRadius values exceeds the particle Radius."); |
313 |
painCave.isFatal = 1; |
314 |
simError(); |
315 |
} |
316 |
if (shellRadii.at(i) <= 0.0 ) { |
317 |
sprintf(painCave.errMsg, "One of the shellRadius values is smaller than zero!"); |
318 |
painCave.isFatal = 1; |
319 |
simError(); |
320 |
} |
321 |
} |
322 |
} |
323 |
|
324 |
vector<int> ids; |
325 |
if ((int)args_info.molFraction_given){ |
326 |
sprintf(painCave.errMsg, "Creating a randomized spherical nanoparticle."); |
327 |
painCave.isFatal = 0; |
328 |
simError(); |
329 |
/* Random particle is the default case*/ |
330 |
|
331 |
for (int i = 0; i < sites.size(); i++) |
332 |
if (!isVacancy[i]) ids.push_back(i); |
333 |
|
334 |
std::random_shuffle(ids.begin(), ids.end()); |
335 |
|
336 |
} else{ |
337 |
sprintf(painCave.errMsg, "Creating a core-shell spherical nanoparticle."); |
338 |
painCave.isFatal = 0; |
339 |
simError(); |
340 |
|
341 |
RealType smallestSoFar; |
342 |
int myComponent = -1; |
343 |
nMol.clear(); |
344 |
nMol.resize(nComponents); |
345 |
|
346 |
for (int i = 0; i < sites.size(); i++) { |
347 |
myLoc = sites[i]; |
348 |
myR = myLoc.length(); |
349 |
smallestSoFar = particleRadius; |
350 |
if (!isVacancy[i]) { |
351 |
for (int j = 0; j < nComponents; j++) { |
352 |
if (myR <= shellRadii[j]) { |
353 |
if (shellRadii[j] <= smallestSoFar) { |
354 |
smallestSoFar = shellRadii[j]; |
355 |
myComponent = j; |
356 |
} |
357 |
} |
358 |
} |
359 |
componentFromSite[i] = myComponent; |
360 |
nMol[myComponent]++; |
361 |
} |
362 |
} |
363 |
} |
364 |
|
365 |
outputFileName = args_info.output_arg; |
366 |
|
367 |
//creat new .md file on fly which corrects the number of molecule |
368 |
createMdFile(inputFileName, outputFileName, nMol); |
369 |
|
370 |
if (oldInfo != NULL) |
371 |
delete oldInfo; |
372 |
|
373 |
SimCreator newCreator; |
374 |
SimInfo* NewInfo = newCreator.createSim(outputFileName, false); |
375 |
|
376 |
// Place molecules |
377 |
Molecule* mol; |
378 |
SimInfo::MoleculeIterator mi; |
379 |
mol = NewInfo->beginMolecule(mi); |
380 |
|
381 |
int l = 0; |
382 |
int whichSite = 0; |
383 |
|
384 |
for (int i = 0; i < nComponents; i++){ |
385 |
locator = new MoLocator(NewInfo->getMoleculeStamp(i), |
386 |
NewInfo->getForceField()); |
387 |
|
388 |
if (!args_info.molFraction_given) { |
389 |
for (int n = 0; n < sites.size(); n++) { |
390 |
if (!isVacancy[n]) { |
391 |
if (componentFromSite[n] == i) { |
392 |
mol = NewInfo->getMoleculeByGlobalIndex(l); |
393 |
locator->placeMol(sites[n], orientations[n], mol); |
394 |
l++; |
395 |
} |
396 |
} |
397 |
} |
398 |
} else { |
399 |
for (int n = 0; n < nMol.at(i); n++) { |
400 |
mol = NewInfo->getMoleculeByGlobalIndex(l); |
401 |
locator->placeMol(sites[ids[l]], orientations[ids[l]], mol); |
402 |
l++; |
403 |
} |
404 |
} |
405 |
} |
406 |
|
407 |
//fill Hmat |
408 |
hmat(0, 0)= 10.0*particleRadius; |
409 |
hmat(0, 1) = 0.0; |
410 |
hmat(0, 2) = 0.0; |
411 |
|
412 |
hmat(1, 0) = 0.0; |
413 |
hmat(1, 1) = 10.0*particleRadius; |
414 |
hmat(1, 2) = 0.0; |
415 |
|
416 |
hmat(2, 0) = 0.0; |
417 |
hmat(2, 1) = 0.0; |
418 |
hmat(2, 2) = 10.0*particleRadius; |
419 |
|
420 |
//set Hmat |
421 |
NewInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat); |
422 |
|
423 |
|
424 |
//create dumpwriter and write out the coordinates |
425 |
writer = new DumpWriter(NewInfo, outputFileName); |
426 |
|
427 |
if (writer == NULL) { |
428 |
sprintf(painCave.errMsg, "Error in creating dumpwriter object "); |
429 |
painCave.isFatal = 1; |
430 |
simError(); |
431 |
} |
432 |
|
433 |
writer->writeDump(); |
434 |
|
435 |
// deleting the writer will put the closing at the end of the dump file |
436 |
|
437 |
delete writer; |
438 |
|
439 |
// cleanup a by calling sim error..... |
440 |
sprintf(painCave.errMsg, "A new OpenMD file called \"%s\" has been " |
441 |
"generated.\n", outputFileName.c_str()); |
442 |
painCave.isFatal = 0; |
443 |
simError(); |
444 |
return 0; |
445 |
} |
446 |
|
447 |
void createMdFile(const std::string&oldMdFileName, |
448 |
const std::string&newMdFileName, |
449 |
std::vector<int> nMol) { |
450 |
ifstream oldMdFile; |
451 |
ofstream newMdFile; |
452 |
const int MAXLEN = 65535; |
453 |
char buffer[MAXLEN]; |
454 |
|
455 |
//create new .md file based on old .md file |
456 |
oldMdFile.open(oldMdFileName.c_str()); |
457 |
newMdFile.open(newMdFileName.c_str()); |
458 |
oldMdFile.getline(buffer, MAXLEN); |
459 |
|
460 |
int i = 0; |
461 |
while (!oldMdFile.eof()) { |
462 |
|
463 |
//correct molecule number |
464 |
if (strstr(buffer, "nMol") != NULL) { |
465 |
if(i<nMol.size()){ |
466 |
sprintf(buffer, "\tnMol = %i;", nMol.at(i)); |
467 |
newMdFile << buffer << std::endl; |
468 |
i++; |
469 |
} |
470 |
} else |
471 |
newMdFile << buffer << std::endl; |
472 |
|
473 |
oldMdFile.getline(buffer, MAXLEN); |
474 |
} |
475 |
|
476 |
oldMdFile.close(); |
477 |
newMdFile.close(); |
478 |
|
479 |
if (i != nMol.size()) { |
480 |
sprintf(painCave.errMsg, "Couldn't replace the correct number of nMol\n" |
481 |
"\tstatements in component blocks. Make sure that all\n" |
482 |
"\tcomponents in the template file have nMol=1"); |
483 |
painCave.isFatal = 1; |
484 |
simError(); |
485 |
} |
486 |
|
487 |
} |
488 |
|