ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/simpleBuilder/simpleBuilder.cpp
Revision: 1665
Committed: Tue Nov 22 20:38:56 2011 UTC (13 years, 5 months ago) by gezelter
File size: 8347 byte(s)
Log Message:
updated copyright notices

File Contents

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

Properties

Name Value
svn:keywords Author Id Revision Date