ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/applications/simpleBuilder/simpleBuilder.cpp
Revision: 2071
Committed: Sat Mar 7 21:41:51 2015 UTC (10 years, 1 month ago) by gezelter
File size: 8278 byte(s)
Log Message:
Reducing the number of warnings when using g++ to compile.

File Contents

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

Properties

Name Value
svn:keywords Author Id Revision Date