OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
simpleBuilder.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 <cmath>
49#include <cstdio>
50#include <cstdlib>
51#include <cstring>
52#include <fstream>
53#include <iostream>
54#include <map>
55#include <string>
56
57#include "brains/Register.hpp"
58#include "brains/SimCreator.hpp"
59#include "brains/SimInfo.hpp"
60#include "io/DumpWriter.hpp"
61#include "lattice/Lattice.hpp"
64#include "math/Vector3.hpp"
65#include "simpleBuilderCmd.hpp"
66#include "utils/MoLocator.hpp"
67#include "utils/StringUtils.hpp"
68
69using namespace std;
70using namespace OpenMD;
71
72void createMdFile(const std::string& oldMdFileName,
73 const std::string& newMdFileName, int nMol);
74
75int main(int argc, char* argv[]) {
77
78 gengetopt_args_info args_info;
79 std::string latticeType;
80 std::string inputFileName;
81 std::string outputFileName;
82 Lattice* simpleLat;
83 RealType latticeConstant;
84 std::vector<RealType> lc;
85 const RealType rhoConvertConst = 1.66053886;
86 RealType density;
87 int nx, ny, nz;
88 Mat3x3d hmat;
89 MoLocator* locator;
90 std::vector<Vector3d> latticePos;
91 std::vector<Vector3d> latticeOrt;
92 int nMolPerCell;
93 DumpWriter* writer;
94
95 // parse command line arguments
96 if (cmdline_parser(argc, argv, &args_info) != 0) exit(1);
97
98 density = args_info.density_arg;
99
100 // get lattice type
101 latticeType = "FCC";
102 if (args_info.lattice_given) { latticeType = args_info.lattice_arg; }
103
104 simpleLat = LatticeFactory::getInstance().createLattice(latticeType);
105
106 if (simpleLat == NULL) {
107 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
108 "Lattice Factory can not create %s lattice\n",
109 latticeType.c_str());
110 painCave.isFatal = 1;
111 simError();
112 }
113 nMolPerCell = simpleLat->getNumSitesPerCell();
114
115 // get the number of unit cells in each direction:
116
117 nx = args_info.nx_arg;
118
119 if (nx <= 0) {
120 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
121 "The number of unit cells in the x direction "
122 "must be greater than 0.");
123 painCave.isFatal = 1;
124 simError();
125 }
126
127 ny = args_info.ny_arg;
128
129 if (ny <= 0) {
130 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
131 "The number of unit cells in the y direction "
132 "must be greater than 0.");
133 painCave.isFatal = 1;
134 simError();
135 }
136
137 nz = args_info.nz_arg;
138
139 if (nz <= 0) {
140 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
141 "The number of unit cells in the z direction "
142 "must be greater than 0.");
143 painCave.isFatal = 1;
144 simError();
145 }
146
147 int nSites = nMolPerCell * nx * ny * nz;
148
149 // get input file name
150 if (args_info.inputs_num)
151 inputFileName = args_info.inputs[0];
152 else {
153 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
154 "No input .omd file name was specified "
155 "on the command line");
156 painCave.isFatal = 1;
157 simError();
158 }
159
160 // parse md file and set up the system
161
162 SimCreator oldCreator;
163 SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
164
165 // Calculate lattice constant (in Angstroms)
166
167 RealType avgMass = MoLocator::getMolMass(oldInfo->getMoleculeStamp(0),
168 oldInfo->getForceField());
169
170 latticeConstant = pow(rhoConvertConst * nMolPerCell * avgMass / density,
171 (RealType)(1.0 / 3.0));
172
173 // Set the lattice constant
174
175 lc.push_back(latticeConstant);
176 simpleLat->setLatticeConstant(lc);
177
178 // Calculate the lattice sites and fill the lattice vector.
179
180 // Get the standard orientations of the cell sites
181
182 latticeOrt = simpleLat->getLatticePointsOrt();
183
184 vector<Vector3d> sites;
185 vector<Vector3d> orientations;
186
187 for (int i = 0; i < nx; i++) {
188 for (int j = 0; j < ny; j++) {
189 for (int k = 0; k < nz; k++) {
190 // Get the position of the cell sites
191
192 simpleLat->getLatticePointsPos(latticePos, i, j, k);
193
194 for (int l = 0; l < nMolPerCell; l++) {
195 sites.push_back(latticePos[l]);
196 orientations.push_back(latticeOrt[l]);
197 }
198 }
199 }
200 }
201
202 outputFileName = args_info.output_arg;
203
204 // create a new .omd file on the fly which corrects the number of molecules
205
206 createMdFile(inputFileName, outputFileName, nSites);
207
208 delete oldInfo;
209
210 // We need to read in the new SimInfo object, then Parse the
211 // md file and set up the system
212
213 SimCreator newCreator;
214 SimInfo* newInfo = newCreator.createSim(outputFileName, false);
215
216 // fill Hmat
217
218 hmat(0, 0) = nx * latticeConstant;
219 hmat(0, 1) = 0.0;
220 hmat(0, 2) = 0.0;
221
222 hmat(1, 0) = 0.0;
223 hmat(1, 1) = ny * latticeConstant;
224 hmat(1, 2) = 0.0;
225
226 hmat(2, 0) = 0.0;
227 hmat(2, 1) = 0.0;
228 hmat(2, 2) = nz * latticeConstant;
229
230 // Set Hmat
231
232 newInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
233
234 // place the molecules
235
236 Molecule* mol;
237 locator =
238 new MoLocator(newInfo->getMoleculeStamp(0), newInfo->getForceField());
239 for (int n = 0; n < nSites; n++) {
240 mol = newInfo->getMoleculeByGlobalIndex(n);
241 locator->placeMol(sites[n], orientations[n], mol);
242 }
243
244 // Create DumpWriter and write out the coordinates
245
246 writer = new DumpWriter(newInfo, outputFileName);
247
248 if (writer == NULL) {
249 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
250 "error in creating DumpWriter");
251 painCave.isFatal = 1;
252 simError();
253 }
254
255 writer->writeDump();
256
257 // deleting the writer will put the closing at the end of the dump file.
258
259 delete writer;
260
261 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
262 "A new OpenMD file called \"%s\" has been generated.\n",
263 outputFileName.c_str());
264 painCave.isFatal = 0;
265 painCave.severity = OPENMD_INFO;
266 simError();
267 return 0;
268}
269
270void createMdFile(const std::string& oldMdFileName,
271 const std::string& newMdFileName, int nMol) {
272 ifstream oldMdFile;
273 ofstream newMdFile;
274 const int MAXLEN = 65535;
275 char buffer[MAXLEN];
276
277 // create new .omd file based on old .omd file
278 oldMdFile.open(oldMdFileName.c_str());
279 newMdFile.open(newMdFileName.c_str());
280
281 oldMdFile.getline(buffer, MAXLEN);
282
283 while (!oldMdFile.eof()) {
284 // correct molecule number
285 if (strstr(buffer, "nMol") != NULL) {
286 snprintf(buffer, MAXLEN, "\t\tnMol = %d;", nMol);
287 newMdFile << buffer << std::endl;
288 } else
289 newMdFile << buffer << std::endl;
290
291 oldMdFile.getline(buffer, MAXLEN);
292 }
293
294 oldMdFile.close();
295 newMdFile.close();
296}
Lattice * createLattice(const std::string &id)
Looks up the type identifier in the internal map.
static LatticeFactory & getInstance()
Returns an instance of Lattice factory.
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
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.
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 inputs_num
unamed options number
unsigned int lattice_given
Whether lattice was given.
char * output_arg
output file name.
char ** inputs
unamed options (options without names)
int ny_arg
number of unit cells in y.
int nz_arg
number of unit cells in z.
double density_arg
density (g/cm^3).
int nx_arg
number of unit cells in x.
char * lattice_arg
Lattice Type.