1 |
/* Copyright (c) 2006 The University of Notre Dame. All Rights Reserved. |
2 |
* |
3 |
* The University of Notre Dame grants you ("Licensee") a |
4 |
* non-exclusive, royalty free, license to use, modify and |
5 |
* redistribute this software in source and binary code form, provided |
6 |
* that the following conditions are met: |
7 |
* |
8 |
* 1. Acknowledgement of the program authors must be made in any |
9 |
* publication of scientific results based in part on use of the |
10 |
* program. An acceptable form of acknowledgement is citation of |
11 |
* the article in which the program was described (Matthew |
12 |
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
13 |
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
14 |
* Parallel Simulation Engine for Molecular Dynamics," |
15 |
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
16 |
* |
17 |
* 2. Redistributions of source code must retain the above copyright |
18 |
* notice, this list of conditions and the following disclaimer. |
19 |
* |
20 |
* 3. Redistributions in binary form must reproduce the above copyright |
21 |
* notice, this list of conditions and the following disclaimer in the |
22 |
* documentation and/or other materials provided with the |
23 |
* distribution. |
24 |
* |
25 |
* This software is provided "AS IS," without a warranty of any |
26 |
* kind. All express or implied conditions, representations and |
27 |
* warranties, including any implied warranty of merchantability, |
28 |
* fitness for a particular purpose or non-infringement, are hereby |
29 |
* excluded. The University of Notre Dame and its licensors shall not |
30 |
* be liable for any damages suffered by licensee as a result of |
31 |
* using, modifying or distributing the software or its |
32 |
* derivatives. In no event will the University of Notre Dame or its |
33 |
* licensors be liable for any lost revenue, profit or data, or for |
34 |
* direct, indirect, special, consequential, incidental or punitive |
35 |
* damages, however caused and regardless of the theory of liability, |
36 |
* arising out of the use of or inability to use software, even if the |
37 |
* University of Notre Dame has been advised of the possibility of |
38 |
* such damages. |
39 |
* |
40 |
* |
41 |
* randomBuilder.cpp |
42 |
* |
43 |
* Created by Charles F. Vardeman II on 10 Apr 2006. |
44 |
* @author Charles F. Vardeman II |
45 |
* @version $Id: randomBuilder.cpp,v 1.3 2006-10-09 22:16:44 gezelter Exp $ |
46 |
* |
47 |
*/ |
48 |
|
49 |
|
50 |
#include <cstdlib> |
51 |
#include <cstdio> |
52 |
#include <cstring> |
53 |
#include <cmath> |
54 |
#include <iostream> |
55 |
#include <string> |
56 |
#include <map> |
57 |
#include <fstream> |
58 |
|
59 |
#include "applications/randomBuilder/randomBuilderCmd.h" |
60 |
#include "lattice/LatticeFactory.hpp" |
61 |
#include "utils/MoLocator.hpp" |
62 |
#include "lattice/Lattice.hpp" |
63 |
#include "brains/Register.hpp" |
64 |
#include "brains/SimInfo.hpp" |
65 |
#include "brains/SimCreator.hpp" |
66 |
#include "io/DumpWriter.hpp" |
67 |
#include "math/Vector3.hpp" |
68 |
#include "math/SquareMatrix3.hpp" |
69 |
#include "utils/StringUtils.hpp" |
70 |
|
71 |
using namespace std; |
72 |
using namespace oopse; |
73 |
|
74 |
void createMdFile(const std::string&oldMdFileName, |
75 |
const std::string&newMdFileName, |
76 |
int components,int* numMol); |
77 |
|
78 |
int main(int argc, char *argv []) { |
79 |
|
80 |
//register force fields |
81 |
registerForceFields(); |
82 |
registerLattice(); |
83 |
|
84 |
gengetopt_args_info args_info; |
85 |
std::string latticeType; |
86 |
std::string inputFileName; |
87 |
std::string outPrefix; |
88 |
std::string outMdFileName; |
89 |
Lattice *simpleLat; |
90 |
int* numMol; |
91 |
RealType latticeConstant; |
92 |
std::vector<RealType> lc; |
93 |
RealType mass; |
94 |
const RealType rhoConvertConst = 1.661; |
95 |
RealType density; |
96 |
int nx, |
97 |
ny, |
98 |
nz; |
99 |
Mat3x3d hmat; |
100 |
MoLocator *locator; |
101 |
std::vector<Vector3d> latticePos; |
102 |
std::vector<Vector3d> latticeOrt; |
103 |
int numMolPerCell; |
104 |
int curMolIndex; |
105 |
DumpWriter *writer; |
106 |
|
107 |
// parse command line arguments |
108 |
if (cmdline_parser(argc, argv, &args_info) != 0) |
109 |
exit(1); |
110 |
|
111 |
density = args_info.density_arg; |
112 |
|
113 |
//get lattice type |
114 |
latticeType = UpperCase(args_info.latticetype_arg); |
115 |
|
116 |
simpleLat = LatticeFactory::getInstance()->createLattice(latticeType); |
117 |
|
118 |
if (simpleLat == NULL) { |
119 |
sprintf(painCave.errMsg, "Lattice Factory can not create %s lattice\n", |
120 |
latticeType.c_str()); |
121 |
painCave.isFatal = 1; |
122 |
simError(); |
123 |
} |
124 |
|
125 |
//get the number of unit cell |
126 |
nx = args_info.nx_arg; |
127 |
|
128 |
if (nx <= 0) { |
129 |
std::cerr << "The number of unit cell in h direction must be greater than 0" << std::endl; |
130 |
exit(1); |
131 |
} |
132 |
|
133 |
ny = args_info.ny_arg; |
134 |
|
135 |
if (ny <= 0) { |
136 |
std::cerr << "The number of unit cell in l direction must be greater than 0" << std::endl; |
137 |
exit(1); |
138 |
} |
139 |
|
140 |
nz = args_info.nz_arg; |
141 |
|
142 |
if (nz <= 0) { |
143 |
std::cerr << "The number of unit cell in k direction must be greater than 0" << std::endl; |
144 |
exit(1); |
145 |
} |
146 |
|
147 |
//get input file name |
148 |
if (args_info.inputs_num) |
149 |
inputFileName = args_info.inputs[0]; |
150 |
else { |
151 |
std::cerr << "You must specify a input file name.\n" << std::endl; |
152 |
cmdline_parser_print_help(); |
153 |
exit(1); |
154 |
} |
155 |
|
156 |
//parse md file and set up the system |
157 |
SimCreator oldCreator; |
158 |
SimInfo* oldInfo = oldCreator.createSim(inputFileName, false); |
159 |
Globals* simParams = oldInfo->getSimParams(); |
160 |
|
161 |
int nComponents =simParams->getNComponents(); |
162 |
if (oldInfo->getNMoleculeStamp() > 2) { |
163 |
std::cerr << "can not build the system with more than two components" |
164 |
<< std::endl; |
165 |
exit(1); |
166 |
} |
167 |
|
168 |
//get mass of molecule. |
169 |
|
170 |
mass = getMolMass(oldInfo->getMoleculeStamp(0), oldInfo->getForceField()); |
171 |
|
172 |
//creat lattice |
173 |
simpleLat = LatticeFactory::getInstance()->createLattice(latticeType); |
174 |
|
175 |
if (simpleLat == NULL) { |
176 |
std::cerr << "Error in creating lattice" << std::endl; |
177 |
exit(1); |
178 |
} |
179 |
|
180 |
numMolPerCell = simpleLat->getNumSitesPerCell(); |
181 |
|
182 |
//calculate lattice constant (in Angstrom) |
183 |
latticeConstant = pow(rhoConvertConst * numMolPerCell * mass / density, |
184 |
(RealType)(1.0 / 3.0)); |
185 |
|
186 |
//set lattice constant |
187 |
lc.push_back(latticeConstant); |
188 |
simpleLat->setLatticeConstant(lc); |
189 |
|
190 |
//calculate the total number of molecules |
191 |
int totMol = nx * ny * nz * numMolPerCell; |
192 |
|
193 |
// Calculate the lattice sites and fill lattice vector. |
194 |
vector<Vector3d> latticeSites; |
195 |
|
196 |
for(int i = 0; i < nx; i++) { |
197 |
for(int j = 0; j < ny; j++) { |
198 |
for(int k = 0; k < nz; k++) { |
199 |
|
200 |
//get the position of the cell sites |
201 |
simpleLat->getLatticePointsPos(latticePos, i, j, k); |
202 |
|
203 |
for(int l = 0; l < numMolPerCell; l++) { |
204 |
latticeSites.push_back(latticePos[l]); |
205 |
} |
206 |
} |
207 |
} |
208 |
} |
209 |
|
210 |
int numSites = latticeSites.size(); |
211 |
|
212 |
numMol = new int[nComponents]; |
213 |
if (nComponents != args_info.molFraction_given && nComponents != 1){ |
214 |
std::cerr << "Number of components does not equal molFraction occurances." << std::endl; |
215 |
exit(1); |
216 |
} |
217 |
int totComponents = 0; |
218 |
for (int i = 0;i<nComponents-1;i++){ /* Figure out Percent for each component */ |
219 |
numMol[i] = int((RealType)numSites * args_info.molFraction_arg[i]); |
220 |
std::cout<<numMol[i]<<std::endl; |
221 |
totComponents += numMol[i]; |
222 |
} |
223 |
numMol[nComponents-1] = numSites - totComponents; |
224 |
|
225 |
|
226 |
outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType; |
227 |
outMdFileName = outPrefix + ".md"; |
228 |
|
229 |
//creat new .md file on fly which corrects the number of molecule |
230 |
createMdFile(inputFileName, outMdFileName, nComponents, numMol); |
231 |
|
232 |
//fill Hmat |
233 |
hmat(0, 0)= nx * latticeConstant; |
234 |
hmat(0, 1) = 0.0; |
235 |
hmat(0, 2) = 0.0; |
236 |
|
237 |
hmat(1, 0) = 0.0; |
238 |
hmat(1, 1) = ny * latticeConstant; |
239 |
hmat(1, 2) = 0.0; |
240 |
|
241 |
hmat(2, 0) = 0.0; |
242 |
hmat(2, 1) = 0.0; |
243 |
hmat(2, 2) = nz * latticeConstant; |
244 |
|
245 |
//set Hmat |
246 |
oldInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat); |
247 |
|
248 |
//place the molecules |
249 |
|
250 |
curMolIndex = 0; |
251 |
|
252 |
//get the orientation of the cell sites |
253 |
//for the same type of molecule in same lattice, it will not change |
254 |
latticeOrt = simpleLat->getLatticePointsOrt(); |
255 |
|
256 |
|
257 |
/* Randomize position vector */ |
258 |
std::random_shuffle(latticeSites.begin(), latticeSites.end()); |
259 |
|
260 |
if (oldInfo != NULL) |
261 |
delete oldInfo; |
262 |
|
263 |
// We need to read in new siminfo object. |
264 |
// parse md file and set up the system |
265 |
|
266 |
SimCreator newCreator; |
267 |
SimInfo* newInfo = newCreator.createSim(outMdFileName, false); |
268 |
|
269 |
/* create Molocators */ |
270 |
locator = new MoLocator(newInfo->getMoleculeStamp(0), newInfo->getForceField()); |
271 |
|
272 |
|
273 |
|
274 |
Molecule* mol; |
275 |
SimInfo::MoleculeIterator mi; |
276 |
mol = newInfo->beginMolecule(mi); |
277 |
int l = 0; |
278 |
for (mol = newInfo->beginMolecule(mi); mol != NULL; mol = newInfo->nextMolecule(mi)) { |
279 |
locator->placeMol(latticeSites[l], latticeOrt[l], mol); |
280 |
l++; |
281 |
} |
282 |
|
283 |
//create dumpwriter and write out the coordinates |
284 |
newInfo->setFinalConfigFileName(outMdFileName); |
285 |
writer = new DumpWriter(newInfo); |
286 |
|
287 |
if (writer == NULL) { |
288 |
std::cerr << "error in creating DumpWriter" << std::endl; |
289 |
exit(1); |
290 |
} |
291 |
|
292 |
writer->writeEor(); |
293 |
std::cout << "new OOPSE MD file: " << outMdFileName |
294 |
<< " has been generated." << std::endl; |
295 |
return 0; |
296 |
} |
297 |
|
298 |
void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName, |
299 |
int components,int* numMol) { |
300 |
ifstream oldMdFile; |
301 |
ofstream newMdFile; |
302 |
const int MAXLEN = 65535; |
303 |
char buffer[MAXLEN]; |
304 |
|
305 |
//create new .md file based on old .md file |
306 |
oldMdFile.open(oldMdFileName.c_str()); |
307 |
newMdFile.open(newMdFileName.c_str()); |
308 |
|
309 |
oldMdFile.getline(buffer, MAXLEN); |
310 |
|
311 |
int i = 0; |
312 |
while (!oldMdFile.eof()) { |
313 |
|
314 |
//correct molecule number |
315 |
if (strstr(buffer, "nMol") != NULL) { |
316 |
if(i<components){ |
317 |
sprintf(buffer, "\tnMol = %i;", numMol[i]); |
318 |
newMdFile << buffer << std::endl; |
319 |
i++; |
320 |
} |
321 |
} else |
322 |
newMdFile << buffer << std::endl; |
323 |
|
324 |
oldMdFile.getline(buffer, MAXLEN); |
325 |
} |
326 |
|
327 |
oldMdFile.close(); |
328 |
newMdFile.close(); |
329 |
} |
330 |
|