OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
nanorodBuilder.cpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the following paper when you publish your work:
33 *
34 * [1] Drisko et al., J. Open Source Softw. 9, 7004 (2024).
35 *
36 * Good starting points for code and simulation methodology are:
37 *
38 * [2] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
39 * [3] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
40 * [4] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
41 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
42 * [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
43 * [7] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
44 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
45 * [9] Drisko & Gezelter, J. Chem. Theory Comput. 20, 4986-4997 (2024).
46 */
47
48#include <config.h>
49
50#include <algorithm>
51#include <cmath>
52#include <cstdio>
53#include <cstdlib>
54#include <cstring>
55#include <fstream>
56#include <iostream>
57#include <map>
58#include <random>
59#include <string>
60
61#include "brains/Register.hpp"
62#include "brains/SimCreator.hpp"
63#include "brains/SimInfo.hpp"
64#include "io/DumpWriter.hpp"
65#include "lattice/Lattice.hpp"
68#include "math/Vector3.hpp"
69#include "nanorodBuilderCmd.hpp"
70#include "shapedLatticeEllipsoid.hpp"
71#include "shapedLatticeRod.hpp"
72#include "utils/MoLocator.hpp"
73#include "utils/StringUtils.hpp"
74
75using namespace std;
76using namespace OpenMD;
77void createMdFile(const std::string& oldMdFileName,
78 const std::string& newMdFileName, std::vector<int> numMol);
79
80int main(int argc, char* argv[]) {
82
83 gengetopt_args_info args_info;
84 std::string latticeType;
85 std::string inputFileName;
86 std::string outputFileName;
87 MoLocator* locator;
88 int nComponents;
89 double latticeConstant;
90 RealType rodRadius;
91 RealType rodLength;
92 Mat3x3d hmat;
93 DumpWriter* writer;
94
95 // Parse Command Line Arguments
96 if (cmdline_parser(argc, argv, &args_info) != 0) exit(1);
97
98 /* get lattice type */
99 latticeType = "FCC";
100
101 /* get input file name */
102 if (args_info.inputs_num)
103 inputFileName = args_info.inputs[0];
104 else {
105 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
106 "No input .omd file name was specified "
107 "on the command line");
108 painCave.isFatal = 1;
110 simError();
111 }
112
113 /* parse md file and set up the system */
114 SimCreator oldCreator;
115 SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
116
117 latticeConstant = args_info.latticeConstant_arg;
118 rodRadius = args_info.radius_arg;
119 rodLength = args_info.length_arg;
120 Globals* simParams = oldInfo->getSimParams();
121
122 vector<Vector3d> sites;
123 vector<Vector3d> orientations;
124
125 if (args_info.ellipsoid_flag) {
126 shapedLatticeEllipsoid nanoEllipsoid(latticeConstant, latticeType,
127 rodLength, rodRadius);
128 sites = nanoEllipsoid.getSites();
129 orientations = nanoEllipsoid.getOrientations();
130 } else {
131 /* Create nanorod */
132 shapedLatticeRod nanoRod(latticeConstant, latticeType, rodRadius,
133 rodLength);
134 /* Build a lattice and get lattice points for this lattice constant */
135 sites = nanoRod.getSites();
136 orientations = nanoRod.getOrientations();
137 }
138
139 /* Set up the random number generator engine */
140 std::random_device rd; // Non-deterministic, uniformly-distributed integer
141 // random number generator
142 std::mt19937 gen(rd()); // 32-bit Mersenne Twister random number engine
143
144 std::vector<std::size_t> vacancyTargets;
145 vector<bool> isVacancy;
146
147 Vector3d myLoc;
148 RealType myR;
149
150 for (std::size_t i = 0; i < sites.size(); i++)
151 isVacancy.push_back(false);
152
153 // cerr << "checking vacancyPercent" << "\n";
154 if (args_info.vacancyPercent_given) {
155 // cerr << "vacancyPercent given" << "\n";
156 if (args_info.vacancyPercent_arg < 0.0 ||
157 args_info.vacancyPercent_arg > 100.0) {
158 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
159 "vacancyPercent was set to a non-sensical value.");
160 painCave.isFatal = 1;
161 simError();
162 } else {
163 RealType vF = args_info.vacancyPercent_arg / 100.0;
164 // cerr << "vacancyPercent = " << vF << "\n";
165 RealType vIR;
166 RealType vOR;
167 if (args_info.vacancyInnerRadius_given) {
168 vIR = args_info.vacancyInnerRadius_arg;
169 } else {
170 vIR = 0.0;
171 }
172 if (args_info.vacancyOuterRadius_given) {
173 vOR = args_info.vacancyOuterRadius_arg;
174 } else {
175 vOR = rodRadius;
176 }
177 if (vIR >= 0.0 && vOR <= rodRadius && vOR >= vIR) {
178 for (std::size_t i = 0; i < sites.size(); i++) {
179 myLoc = sites[i];
180 myR = myLoc.length();
181 if (myR >= vIR && myR <= vOR) { vacancyTargets.push_back(i); }
182 }
183 std::shuffle(vacancyTargets.begin(), vacancyTargets.end(), gen);
184
185 std::size_t nTargets = vacancyTargets.size();
186 vacancyTargets.resize((int)(vF * nTargets));
187
188 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
189 "Removing %d atoms from randomly-selected\n"
190 "\tsites between %lf and %lf.",
191 (int)vacancyTargets.size(), vIR, vOR);
192 painCave.isFatal = 0;
193 painCave.severity = OPENMD_INFO;
194 simError();
195
196 isVacancy.clear();
197 for (std::size_t i = 0; i < sites.size(); i++) {
198 bool vac = false;
199 for (std::size_t j = 0; j < vacancyTargets.size(); j++) {
200 if (i == vacancyTargets[j]) vac = true;
201 }
202 isVacancy.push_back(vac);
203 }
204
205 } else {
206 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
207 "Something is strange about the vacancy\n"
208 "\tinner or outer radii. Check their values.");
209 painCave.isFatal = 1;
210 simError();
211 }
212 }
213 }
214
215 /* Get number of lattice sites */
216 std::size_t nSites = sites.size() - vacancyTargets.size();
217
218 // cerr << "sites.size() = " << sites.size() << "\n";
219 // cerr << "nSites = " << nSites << "\n";
220 // cerr << "vacancyTargets = " << vacancyTargets.size() << "\n";
221
222 std::vector<Component*> components = simParams->getComponents();
223 std::vector<RealType> molFractions;
224 std::vector<RealType> shellRadii;
225 std::vector<int> nMol;
226 std::map<int, int> componentFromSite;
227 nComponents = components.size();
228 // cerr << "nComponents = " << nComponents << "\n";
229
230 if (args_info.molFraction_given && args_info.shellRadius_given) {
231 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
232 "Specify either molFraction or shellRadius "
233 "arguments, but not both!");
234 painCave.isFatal = 1;
235 simError();
236 }
237
238 if (nComponents == 1) {
239 molFractions.push_back(1.0);
240 shellRadii.push_back(rodRadius);
241 } else if (args_info.molFraction_given) {
242 if ((int)args_info.molFraction_given == nComponents) {
243 for (int i = 0; i < nComponents; i++) {
244 molFractions.push_back(args_info.molFraction_arg[i]);
245 }
246 } else if ((int)args_info.molFraction_given == nComponents - 1) {
247 RealType remainingFraction = 1.0;
248 for (int i = 0; i < nComponents - 1; i++) {
249 molFractions.push_back(args_info.molFraction_arg[i]);
250 remainingFraction -= molFractions[i];
251 }
252 molFractions.push_back(remainingFraction);
253 } else {
254 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
255 "nanorodBuilder can't figure out molFractions "
256 "for all of the components in the <MetaData> block.");
257 painCave.isFatal = 1;
258 simError();
259 }
260 } else if ((int)args_info.shellRadius_given) {
261 if ((int)args_info.shellRadius_given == nComponents) {
262 for (int i = 0; i < nComponents; i++) {
263 shellRadii.push_back(args_info.shellRadius_arg[i]);
264 }
265 } else if ((int)args_info.shellRadius_given == nComponents - 1) {
266 for (int i = 0; i < nComponents - 1; i++) {
267 shellRadii.push_back(args_info.shellRadius_arg[i]);
268 }
269 shellRadii.push_back(rodRadius);
270 } else {
271 snprintf(
272 painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
273 "nanorodBuilder can't figure out the\n"
274 "\tshell radii for all of the components in the <MetaData> block.");
275 painCave.isFatal = 1;
276 simError();
277 }
278 } else {
279 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
280 "You have a multi-component <MetaData> block,\n"
281 "\tbut have not specified either molFraction or shellRadius "
282 "arguments.");
283 painCave.isFatal = 1;
284 simError();
285 }
286
287 if (args_info.molFraction_given) {
288 RealType totalFraction = 0.0;
289
290 /* Do some simple sanity checking*/
291
292 for (int i = 0; i < nComponents; i++) {
293 if (molFractions.at(i) < 0.0) {
294 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
295 "One of the requested molFractions was"
296 " less than zero!");
297 painCave.isFatal = 1;
298 simError();
299 }
300 if (molFractions.at(i) > 1.0) {
301 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
302 "One of the requested molFractions was"
303 " greater than one!");
304 painCave.isFatal = 1;
305 simError();
306 }
307 totalFraction += molFractions.at(i);
308 }
309 if (abs(totalFraction - 1.0) > 1e-6) {
310 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
311 "The sum of molFractions was not close enough to 1.0");
312 painCave.isFatal = 1;
313 simError();
314 }
315
316 int remaining = nSites;
317 for (int i = 0; i < nComponents - 1; i++) {
318 nMol.push_back(int((RealType)nSites * molFractions.at(i)));
319 remaining -= nMol.at(i);
320 }
321 nMol.push_back(remaining);
322
323 // recompute actual mol fractions and perform final sanity check:
324
325 std::size_t totalMolecules = 0;
326 for (int i = 0; i < nComponents; i++) {
327 molFractions[i] = (RealType)(nMol.at(i)) / (RealType)nSites;
328 totalMolecules += nMol.at(i);
329 }
330 if (totalMolecules != nSites) {
331 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
332 "Computed total number of molecules is not equal "
333 "to the number of lattice sites!");
334 painCave.isFatal = 1;
335 simError();
336 }
337 } else {
338 for (unsigned int i = 0; i < shellRadii.size(); i++) {
339 if (shellRadii.at(i) > rodRadius + 1e-6) {
340 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
341 "One of the shellRadius values exceeds the rod Radius.");
342 painCave.isFatal = 1;
343 simError();
344 }
345 if (shellRadii.at(i) <= 0.0) {
346 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
347 "One of the shellRadius values is smaller than zero!");
348 painCave.isFatal = 1;
349 simError();
350 }
351 }
352 }
353
354 vector<int> ids;
355 if ((int)args_info.molFraction_given) {
356 // cerr << "molFraction given 2" << "\n";
357 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
358 "Creating a randomized spherically-capped nanorod.");
359 painCave.isFatal = 0;
360 painCave.severity = OPENMD_INFO;
361 simError();
362 /* Random rod is the default case*/
363
364 for (std::size_t i = 0; i < sites.size(); i++)
365 if (!isVacancy[i]) ids.push_back(i);
366
367 std::shuffle(ids.begin(), ids.end(), gen);
368
369 } else {
370 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
371 "Creating an fcc nanorod.");
372 painCave.isFatal = 0;
373 painCave.severity = OPENMD_INFO;
374 simError();
375
376 // RealType smallestSoFar;
377 int myComponent = -1;
378 nMol.clear();
379 nMol.resize(nComponents);
380
381 // cerr << "shellRadii[0] " << shellRadii[0] << "\n";
382 // cerr << "rodRadius " << rodRadius << "\n";
383
384 for (unsigned int i = 0; i < sites.size(); i++) {
385 myLoc = sites[i];
386 myR = myLoc.length();
387 // smallestSoFar = rodRadius;
388 // cerr << "vac = " << isVacancy[i]<< "\n";
389
390 if (!isVacancy[i]) {
391 // for (int j = 0; j < nComponents; j++) {
392 // if (myR <= shellRadii[j]) {
393 // if (shellRadii[j] <= smallestSoFar) {
394 // smallestSoFar = shellRadii[j];
395 // myComponent = j;
396 // }
397 // }
398 // }
399 myComponent = 0;
400 componentFromSite[i] = myComponent;
401 nMol[myComponent]++;
402 // cerr << "nMol for myComp(" << myComponent<<") = " <<
403 // nMol[myComponent] <<
404 //"\n";
405 }
406 }
407 }
408 // cerr << "nMol = " << nMol.at(0) << "\n";
409
410 outputFileName = args_info.output_arg;
411
412 // creat new .omd file on fly which corrects the number of molecule
413
414 createMdFile(inputFileName, outputFileName, nMol);
415
416 delete oldInfo;
417
418 SimCreator newCreator;
419 SimInfo* NewInfo = newCreator.createSim(outputFileName, false);
420
421 // Place molecules
422 Molecule* mol;
423 SimInfo::MoleculeIterator mi;
424 mol = NewInfo->beginMolecule(mi);
425
426 int l = 0;
427
428 for (int i = 0; i < nComponents; i++) {
429 locator =
430 new MoLocator(NewInfo->getMoleculeStamp(i), NewInfo->getForceField());
431
432 // cerr << "nMol = " << nMol.at(i) << "\n";
433 if (!args_info.molFraction_given) {
434 for (unsigned int n = 0; n < sites.size(); n++) {
435 if (!isVacancy[n]) {
436 if (componentFromSite[n] == i) {
437 mol = NewInfo->getMoleculeByGlobalIndex(l);
438 locator->placeMol(sites[n], orientations[n], mol);
439 l++;
440 }
441 }
442 }
443 } else {
444 for (int n = 0; n < nMol.at(i); n++) {
445 mol = NewInfo->getMoleculeByGlobalIndex(l);
446 locator->placeMol(sites[ids[l]], orientations[ids[l]], mol);
447 l++;
448 }
449 }
450 }
451
452 // fill Hmat
453 hmat(0, 0) = 10.0 * rodRadius;
454 hmat(0, 1) = 0.0;
455 hmat(0, 2) = 0.0;
456
457 hmat(1, 0) = 0.0;
458 hmat(1, 1) = 10.0 * rodRadius;
459 hmat(1, 2) = 0.0;
460
461 hmat(2, 0) = 0.0;
462 hmat(2, 1) = 0.0;
463 hmat(2, 2) = 5.0 * rodLength + 2.0 * rodRadius;
464
465 // set Hmat
466 NewInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
467
468 // create dumpwriter and write out the coordinates
469 writer = new DumpWriter(NewInfo, outputFileName);
470
471 if (writer == NULL) {
472 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
473 "Error in creating dumpwriter object ");
474 painCave.isFatal = 1;
475 simError();
476 }
477
478 writer->writeDump();
479
480 // deleting the writer will put the closing at the end of the dump file
481
482 delete writer;
483
484 // cleanup a by calling sim error.....
485 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
486 "A new OpenMD file called \"%s\" has been "
487 "generated.\n",
488 outputFileName.c_str());
489 painCave.isFatal = 0;
490 painCave.severity = OPENMD_INFO;
491 simError();
492 return 0;
493}
494
495void createMdFile(const std::string& oldMdFileName,
496 const std::string& newMdFileName, std::vector<int> nMol) {
497 ifstream oldMdFile;
498 ofstream newMdFile;
499 const int MAXLEN = 65535;
500 char buffer[MAXLEN];
501
502 // create new .omd file based on old .omd file
503 oldMdFile.open(oldMdFileName.c_str());
504 newMdFile.open(newMdFileName.c_str());
505 oldMdFile.getline(buffer, MAXLEN);
506
507 unsigned int i = 0;
508 while (!oldMdFile.eof()) {
509 // correct molecule number
510 if (strstr(buffer, "nMol") != NULL) {
511 if (i < nMol.size()) {
512 snprintf(buffer, MAXLEN, "\tnMol = %i;", nMol.at(i));
513 newMdFile << buffer << std::endl;
514 i++;
515 }
516 } else
517 newMdFile << buffer << std::endl;
518
519 oldMdFile.getline(buffer, MAXLEN);
520 }
521
522 oldMdFile.close();
523 newMdFile.close();
524
525 if (i != nMol.size()) {
526 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
527 "Couldn't replace the correct number of nMol\n"
528 "\tstatements in component blocks. Make sure that all\n"
529 "\tcomponents in the template file have nMol=1");
530 painCave.isFatal = 1;
531 simError();
532 }
533}
void cmdline_parser_print_help(void)
Print the help.
The only responsibility of SimCreator is to parse the meta-data file and create a SimInfo instance ba...
SimInfo * createSim(const std::string &mdFileName, bool loadInitCoords=true)
Setup Simulation.
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:96
Molecule * getMoleculeByGlobalIndex(int index)
Finds a molecule with a specified global index.
Definition SimInfo.hpp:303
ForceField * getForceField()
Returns the force field.
Definition SimInfo.hpp:269
Molecule * beginMolecule(MoleculeIterator &i)
Returns the first molecule in this SimInfo and intialize the iterator.
Definition SimInfo.cpp:243
MoleculeStamp * getMoleculeStamp(int id)
Returns the molecule stamp.
Definition SimInfo.hpp:293
SnapshotManager * getSnapshotManager()
Returns the snapshot manager.
Definition SimInfo.hpp:251
void setHmat(const Mat3x3d &m)
Sets the H-Matrix.
Definition Snapshot.cpp:220
Snapshot * getCurrentSnapshot()
Returns the pointer of current snapshot.
Real length() const
Returns the length of this vector.
Definition Vector.hpp:397
Implements an ellipsoid-shaped lattice.
Implements a spherically-capped rod-shaped lattice.
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
void registerLattice()
Register all lattice.
Definition Register.cpp:134
The header file for the command line option parser generated by GNU Gengetopt version 2....
Where the command line options are stored.
unsigned int vacancyInnerRadius_given
Whether vacancyInnerRadius was given.
double vacancyPercent_arg
Percentage of atoms to remove from within vacancy range.
double vacancyOuterRadius_arg
Radius arround core-shell where vacancies should be located.
unsigned inputs_num
unamed options number
unsigned int molFraction_given
Whether molFraction was given.
int ellipsoid_flag
Build an Ellipsoid instead of a rod.
double length_arg
maximum length (default='100').
double * molFraction_arg
Builds a multi-component random alloy nanoparticle.
char * output_arg
output file name.
char ** inputs
unamed options (options without names)
double * shellRadius_arg
Radius containing within it only molecules of a specific component.
unsigned int shellRadius_given
Whether shellRadius was given.
double radius_arg
Nanoparticle radius in Angstroms.
double vacancyInnerRadius_arg
Radius arround core-shell where vacancies should be located.
unsigned int vacancyPercent_given
Whether vacancyPercent was given.
double latticeConstant_arg
Lattice spacing in Angstroms for cubic lattice.
unsigned int vacancyOuterRadius_given
Whether vacancyOuterRadius was given.