ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/applications/simpleBuilder/simpleBuilder.cpp
Revision: 3041
Committed: Tue Oct 10 18:34:12 2006 UTC (17 years, 9 months ago) by gezelter
File size: 8241 byte(s)
Log Message:
fixing missing lattice arguments, adding a builder sample

File Contents

# User Rev Content
1 gezelter 2204 /*
2 gezelter 1930 * 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. Acknowledgement of the program authors must be made in any
10     * publication of scientific results based in part on use of the
11     * program. An acceptable form of acknowledgement is citation of
12     * the article in which the program was described (Matthew
13     * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14     * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15     * Parallel Simulation Engine for Molecular Dynamics,"
16     * J. Comput. Chem. 26, pp. 252-271 (2005))
17     *
18     * 2. Redistributions of source code must retain the above copyright
19     * notice, this list of conditions and the following disclaimer.
20     *
21     * 3. Redistributions in binary form must reproduce the above copyright
22     * notice, this list of conditions and the following disclaimer in the
23     * documentation and/or other materials provided with the
24     * distribution.
25     *
26     * This software is provided "AS IS," without a warranty of any
27     * kind. All express or implied conditions, representations and
28     * warranties, including any implied warranty of merchantability,
29     * fitness for a particular purpose or non-infringement, are hereby
30     * excluded. The University of Notre Dame and its licensors shall not
31     * be liable for any damages suffered by licensee as a result of
32     * using, modifying or distributing the software or its
33     * derivatives. In no event will the University of Notre Dame or its
34     * licensors be liable for any lost revenue, profit or data, or for
35     * direct, indirect, special, consequential, incidental or punitive
36     * damages, however caused and regardless of the theory of liability,
37     * arising out of the use of or inability to use software, even if the
38     * University of Notre Dame has been advised of the possibility of
39     * such damages.
40     */
41    
42 tim 1501 #include <cstdlib>
43     #include <cstdio>
44     #include <cstring>
45     #include <cmath>
46     #include <iostream>
47     #include <string>
48     #include <map>
49     #include <fstream>
50    
51     #include "applications/simpleBuilder/simpleBuilderCmd.h"
52 gezelter 2178 #include "lattice/LatticeFactory.hpp"
53     #include "utils/MoLocator.hpp"
54     #include "lattice/Lattice.hpp"
55 gezelter 1930 #include "brains/Register.hpp"
56     #include "brains/SimInfo.hpp"
57     #include "brains/SimCreator.hpp"
58     #include "io/DumpWriter.hpp"
59     #include "math/Vector3.hpp"
60     #include "math/SquareMatrix3.hpp"
61     #include "utils/StringUtils.hpp"
62 tim 1501
63     using namespace std;
64 gezelter 1930 using namespace oopse;
65 tim 1501
66 gezelter 3041 void createMdFile(const std::string&oldMdFileName,
67     const std::string&newMdFileName,
68     int nMol);
69    
70 gezelter 1930 int main(int argc, char *argv []) {
71 tim 1501
72 gezelter 3041 // register force fields
73 gezelter 2204 registerForceFields();
74     registerLattice();
75 gezelter 1930
76 gezelter 2204 gengetopt_args_info args_info;
77     std::string latticeType;
78     std::string inputFileName;
79 gezelter 3041 std::string outputFileName;
80 gezelter 2204 Lattice *simpleLat;
81 gezelter 3041 RealType latticeConstant;
82     std::vector<RealType> lc;
83     const RealType rhoConvertConst = 1.661;
84     RealType density;
85     int nx, ny, nz;
86 gezelter 2204 Mat3x3d hmat;
87     MoLocator *locator;
88     std::vector<Vector3d> latticePos;
89     std::vector<Vector3d> latticeOrt;
90 gezelter 3041 int nMolPerCell;
91 gezelter 2204 DumpWriter *writer;
92 tim 1501
93 gezelter 2204 // parse command line arguments
94     if (cmdline_parser(argc, argv, &args_info) != 0)
95     exit(1);
96 tim 1501
97 gezelter 2204 density = args_info.density_arg;
98 tim 1501
99 gezelter 2204 //get lattice type
100 gezelter 3041 latticeType = "FCC";
101 tim 1501
102 gezelter 2204 simpleLat = LatticeFactory::getInstance()->createLattice(latticeType);
103 tim 2184
104 gezelter 2204 if (simpleLat == NULL) {
105     sprintf(painCave.errMsg, "Lattice Factory can not create %s lattice\n",
106     latticeType.c_str());
107     painCave.isFatal = 1;
108     simError();
109     }
110 gezelter 3041 nMolPerCell = simpleLat->getNumSitesPerCell();
111 tim 1501
112 gezelter 3041 //get the number of unit cells in each direction:
113    
114 gezelter 2204 nx = args_info.nx_arg;
115 tim 1501
116 gezelter 2204 if (nx <= 0) {
117 gezelter 3041 sprintf(painCave.errMsg, "The number of unit cells in the x direction "
118     "must be greater than 0.");
119     painCave.isFatal = 1;
120     simError();
121 gezelter 2204 }
122 tim 1501
123 gezelter 2204 ny = args_info.ny_arg;
124 tim 1501
125 gezelter 2204 if (ny <= 0) {
126 gezelter 3041 sprintf(painCave.errMsg, "The number of unit cells in the y direction "
127     "must be greater than 0.");
128     painCave.isFatal = 1;
129     simError();
130 gezelter 2204 }
131 tim 1501
132 gezelter 2204 nz = args_info.nz_arg;
133 tim 1501
134 gezelter 2204 if (nz <= 0) {
135 gezelter 3041 sprintf(painCave.errMsg, "The number of unit cells in the z direction "
136     "must be greater than 0.");
137     painCave.isFatal = 1;
138     simError();
139 gezelter 2204 }
140 tim 1501
141 gezelter 3041 int nSites = nMolPerCell * nx * ny * nz;
142    
143 gezelter 2204 //get input file name
144     if (args_info.inputs_num)
145     inputFileName = args_info.inputs[0];
146     else {
147 gezelter 3041 sprintf(painCave.errMsg, "No input .md file name was specified "
148     "on the command line");
149     painCave.isFatal = 1;
150     simError();
151 gezelter 2204 }
152 tim 1501
153 gezelter 2204 //parse md file and set up the system
154 gezelter 3041
155 gezelter 2204 SimCreator oldCreator;
156     SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
157 gezelter 3041 Globals* simParams = oldInfo->getSimParams();
158 tim 1501
159 gezelter 3041 // Calculate lattice constant (in Angstroms)
160 tim 1501
161 gezelter 3041 RealType avgMass = getMolMass(oldInfo->getMoleculeStamp(0),
162     oldInfo->getForceField());
163 tim 1501
164 gezelter 3041 latticeConstant = pow(rhoConvertConst * nMolPerCell * avgMass / density,
165     (RealType)(1.0 / 3.0));
166    
167     // Set the lattice constant
168    
169     lc.push_back(latticeConstant);
170     simpleLat->setLatticeConstant(lc);
171 tim 1501
172 gezelter 3041 // Calculate the lattice sites and fill the lattice vector.
173 tim 1501
174 gezelter 3041 // Get the standard orientations of the cell sites
175 tim 1501
176 gezelter 3041 latticeOrt = simpleLat->getLatticePointsOrt();
177 tim 1501
178 gezelter 3041 vector<Vector3d> sites;
179     vector<Vector3d> orientations;
180    
181     for(int i = 0; i < nx; i++) {
182     for(int j = 0; j < ny; j++) {
183     for(int k = 0; k < nz; k++) {
184 tim 1501
185 gezelter 3041 // Get the position of the cell sites
186    
187     simpleLat->getLatticePointsPos(latticePos, i, j, k);
188    
189     for(int l = 0; l < nMolPerCell; l++) {
190     sites.push_back(latticePos[l]);
191     orientations.push_back(latticeOrt[l]);
192     }
193     }
194     }
195     }
196    
197     outputFileName = args_info.output_arg;
198    
199     // create a new .md file on the fly which corrects the number of molecules
200 tim 1501
201 gezelter 3041 createMdFile(inputFileName, outputFileName, nSites);
202 tim 1501
203 gezelter 3041 if (oldInfo != NULL)
204     delete oldInfo;
205 gezelter 1930
206 gezelter 3041 // We need to read in the new SimInfo object, then Parse the
207     // md file and set up the system
208 tim 1501
209 gezelter 3041 SimCreator newCreator;
210     SimInfo* newInfo = newCreator.createSim(outputFileName, false);
211 tim 1501
212 gezelter 3041 // fill Hmat
213    
214     hmat(0, 0) = nx * latticeConstant;
215 gezelter 2204 hmat(0, 1) = 0.0;
216     hmat(0, 2) = 0.0;
217 tim 1501
218 gezelter 2204 hmat(1, 0) = 0.0;
219     hmat(1, 1) = ny * latticeConstant;
220     hmat(1, 2) = 0.0;
221 tim 1501
222 gezelter 2204 hmat(2, 0) = 0.0;
223     hmat(2, 1) = 0.0;
224     hmat(2, 2) = nz * latticeConstant;
225 tim 1501
226 gezelter 3041 // Set Hmat
227 tim 1501
228 gezelter 3041 newInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
229 gezelter 1930
230 gezelter 3041 // place the molecules
231    
232 gezelter 2204 Molecule* mol;
233 gezelter 3041 locator = new MoLocator(newInfo->getMoleculeStamp(0),
234     newInfo->getForceField());
235     for (int n = 0; n < nSites; n++) {
236     mol = newInfo->getMoleculeByGlobalIndex(n);
237     locator->placeMol(sites[n], orientations[n], mol);
238 gezelter 2204 }
239 gezelter 3041
240     // Create DumpWriter and write out the coordinates
241 tim 1501
242 gezelter 3041 writer = new DumpWriter(newInfo, outputFileName);
243    
244 gezelter 2204 if (writer == NULL) {
245 gezelter 3041 sprintf(painCave.errMsg, "error in creating DumpWriter");
246     painCave.isFatal = 1;
247     simError();
248 gezelter 2204 }
249 tim 1501
250 gezelter 2204 writer->writeDump();
251 gezelter 1930
252 gezelter 3041 // deleting the writer will put the closing at the end of the dump file.
253 gezelter 1930
254 gezelter 3041 delete writer;
255 gezelter 1930
256 gezelter 3041 sprintf(painCave.errMsg, "A new OOPSE MD file called \"%s\" has been "
257     "generated.\n", outputFileName.c_str());
258     painCave.isFatal = 0;
259     simError();
260 gezelter 2204 return 0;
261 tim 1501 }
262    
263 gezelter 3041 void createMdFile(const std::string&oldMdFileName,
264     const std::string&newMdFileName,
265     int nMol) {
266 gezelter 2204 ifstream oldMdFile;
267     ofstream newMdFile;
268     const int MAXLEN = 65535;
269     char buffer[MAXLEN];
270 tim 1501
271 gezelter 2204 //create new .md file based on old .md file
272     oldMdFile.open(oldMdFileName.c_str());
273     newMdFile.open(newMdFileName.c_str());
274 tim 1501
275 gezelter 2204 oldMdFile.getline(buffer, MAXLEN);
276 gezelter 1930
277 gezelter 2204 while (!oldMdFile.eof()) {
278 gezelter 1930
279 gezelter 2204 //correct molecule number
280     if (strstr(buffer, "nMol") != NULL) {
281 gezelter 3041 sprintf(buffer, "\t\tnMol = %d;", nMol);
282 gezelter 2204 newMdFile << buffer << std::endl;
283     } else
284     newMdFile << buffer << std::endl;
285 gezelter 1930
286 gezelter 2204 oldMdFile.getline(buffer, MAXLEN);
287     }
288 gezelter 1930
289 gezelter 2204 oldMdFile.close();
290     newMdFile.close();
291 tim 1501 }
292 gezelter 1930