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.1 2006-04-25 22:59:27 chuckv Exp $ |
45 |
> |
* @version $Id: randomBuilder.cpp,v 1.4 2006-10-10 02:44:13 gezelter Exp $ |
46 |
|
* |
47 |
|
*/ |
48 |
|
|
73 |
|
|
74 |
|
void createMdFile(const std::string&oldMdFileName, |
75 |
|
const std::string&newMdFileName, |
76 |
< |
int components,int* numMol); |
76 |
> |
int components, int* numMol); |
77 |
|
|
78 |
|
int main(int argc, char *argv []) { |
79 |
|
|
80 |
< |
//register force fields |
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 |
< |
std::string outInitFileName; |
87 |
> |
std::string outputFileName; |
88 |
|
Lattice *simpleLat; |
89 |
|
int* numMol; |
90 |
< |
double latticeConstant; |
91 |
< |
std::vector<double> lc; |
92 |
< |
double mass; |
93 |
< |
const double rhoConvertConst = 1.661; |
94 |
< |
double density; |
95 |
< |
int nx, |
98 |
< |
ny, |
99 |
< |
nz; |
90 |
> |
RealType latticeConstant; |
91 |
> |
std::vector<RealType> lc; |
92 |
> |
RealType mass; |
93 |
> |
const RealType rhoConvertConst = 1.661; |
94 |
> |
RealType density; |
95 |
> |
int nx, ny, nz; |
96 |
|
Mat3x3d hmat; |
97 |
|
MoLocator *locator; |
98 |
|
std::vector<Vector3d> latticePos; |
119 |
|
simError(); |
120 |
|
} |
121 |
|
|
122 |
< |
//get the number of unit cell |
122 |
> |
//get the number of unit cells in each direction: |
123 |
> |
|
124 |
|
nx = args_info.nx_arg; |
125 |
|
|
126 |
|
if (nx <= 0) { |
127 |
< |
std::cerr << "The number of unit cell in h direction must be greater than 0" << std::endl; |
128 |
< |
exit(1); |
127 |
> |
sprintf(painCave.errMsg, "The number of unit cells in the x direction " |
128 |
> |
"must be greater than 0."); |
129 |
> |
painCave.isFatal = 1; |
130 |
> |
simError(); |
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); |
136 |
> |
sprintf(painCave.errMsg, "The number of unit cells in the y direction " |
137 |
> |
"must be greater than 0."); |
138 |
> |
painCave.isFatal = 1; |
139 |
> |
simError(); |
140 |
|
} |
141 |
|
|
142 |
|
nz = args_info.nz_arg; |
143 |
|
|
144 |
|
if (nz <= 0) { |
145 |
< |
std::cerr << "The number of unit cell in k direction must be greater than 0" << std::endl; |
146 |
< |
exit(1); |
145 |
> |
sprintf(painCave.errMsg, "The number of unit cells in the z direction " |
146 |
> |
"must be greater than 0."); |
147 |
> |
painCave.isFatal = 1; |
148 |
> |
simError(); |
149 |
|
} |
150 |
|
|
151 |
|
//get input file name |
152 |
|
if (args_info.inputs_num) |
153 |
|
inputFileName = args_info.inputs[0]; |
154 |
|
else { |
155 |
< |
std::cerr << "You must specify a input file name.\n" << std::endl; |
156 |
< |
cmdline_parser_print_help(); |
157 |
< |
exit(1); |
155 |
> |
sprintf(painCave.errMsg, "No input .md file name was specified " |
156 |
> |
"on the command line"); |
157 |
> |
painCave.isFatal = 1; |
158 |
> |
simError(); |
159 |
|
} |
160 |
|
|
161 |
|
//parse md file and set up the system |
162 |
+ |
|
163 |
|
SimCreator oldCreator; |
164 |
|
SimInfo* oldInfo = oldCreator.createSim(inputFileName, false); |
165 |
|
Globals* simParams = oldInfo->getSimParams(); |
166 |
|
|
167 |
|
int nComponents =simParams->getNComponents(); |
168 |
< |
if (oldInfo->getNMoleculeStamp()>= 2) { |
169 |
< |
std::cerr << "can not build the system with more than two components" |
170 |
< |
<< std::endl; |
171 |
< |
exit(1); |
168 |
> |
if (oldInfo->getNMoleculeStamp() > 2) { |
169 |
> |
sprintf(painCave.errMsg, "randomBuilder can't yet build a system with " |
170 |
> |
"more than two components."); |
171 |
> |
painCave.isFatal = 1; |
172 |
> |
simError(); |
173 |
|
} |
174 |
|
|
175 |
|
//get mass of molecule. |
176 |
|
|
177 |
|
mass = getMolMass(oldInfo->getMoleculeStamp(0), oldInfo->getForceField()); |
178 |
|
|
179 |
< |
//creat lattice |
179 |
> |
// Create the lattice |
180 |
> |
|
181 |
|
simpleLat = LatticeFactory::getInstance()->createLattice(latticeType); |
182 |
|
|
183 |
|
if (simpleLat == NULL) { |
184 |
< |
std::cerr << "Error in creating lattice" << std::endl; |
185 |
< |
exit(1); |
184 |
> |
sprintf(painCave.errMsg, "Error in creating lattice."); |
185 |
> |
painCave.isFatal = 1; |
186 |
> |
simError(); |
187 |
|
} |
188 |
|
|
189 |
|
numMolPerCell = simpleLat->getNumSitesPerCell(); |
190 |
|
|
191 |
< |
//calculate lattice constant (in Angstrom) |
191 |
> |
// Calculate lattice constant (in Angstroms) |
192 |
> |
|
193 |
|
latticeConstant = pow(rhoConvertConst * numMolPerCell * mass / density, |
194 |
< |
1.0 / 3.0); |
194 |
> |
(RealType)(1.0 / 3.0)); |
195 |
|
|
196 |
< |
//set lattice constant |
196 |
> |
// Set the lattice constant |
197 |
> |
|
198 |
|
lc.push_back(latticeConstant); |
199 |
|
simpleLat->setLatticeConstant(lc); |
200 |
|
|
201 |
< |
//calculate the total number of molecules |
201 |
> |
// Calculate the total number of molecules |
202 |
> |
|
203 |
|
int totMol = nx * ny * nz * numMolPerCell; |
204 |
|
|
205 |
< |
// Calculate the lattice sites and fill lattice vector. |
206 |
< |
vector<Vector3d> latticeSites; |
205 |
> |
// Calculate the lattice sites and fill the lattice vector. |
206 |
> |
|
207 |
> |
// Get the standard orientations of the cell sites |
208 |
> |
|
209 |
> |
latticeOrt = simpleLat->getLatticePointsOrt(); |
210 |
> |
|
211 |
> |
vector<Vector3d> sites; |
212 |
> |
vector<Vector3d> orientations; |
213 |
|
|
214 |
|
for(int i = 0; i < nx; i++) { |
215 |
|
for(int j = 0; j < ny; j++) { |
216 |
|
for(int k = 0; k < nz; k++) { |
217 |
|
|
218 |
< |
//get the position of the cell sites |
218 |
> |
// Get the position of the cell sites |
219 |
> |
|
220 |
|
simpleLat->getLatticePointsPos(latticePos, i, j, k); |
221 |
|
|
222 |
|
for(int l = 0; l < numMolPerCell; l++) { |
223 |
< |
latticeSites.push_back(latticePos[l]); |
223 |
> |
sites.push_back(latticePos[l]); |
224 |
> |
orientations.push_back(latticeOrt[l]); |
225 |
|
} |
226 |
|
} |
227 |
|
} |
228 |
|
} |
229 |
|
|
230 |
< |
int numSites = latticeSites.size(); |
230 |
> |
int numSites = sites.size(); |
231 |
|
|
232 |
|
numMol = new int[nComponents]; |
233 |
|
if (nComponents != args_info.molFraction_given && nComponents != 1){ |
234 |
< |
std::cerr << "Number of components does not equal molFraction occurances." << std::endl; |
235 |
< |
exit(1); |
234 |
> |
sprintf(painCave.errMsg, "There needs to be the same number of " |
235 |
> |
"molFraction arguments as there are components in the " |
236 |
> |
"<MetaData> block."); |
237 |
> |
painCave.isFatal = 1; |
238 |
> |
simError(); |
239 |
|
} |
240 |
|
int totComponents = 0; |
241 |
< |
for (int i = 0;i<nComponents-1;i++){ /* Figure out Percent for each component */ |
242 |
< |
numMol[i] = int((double)numSites * args_info.molFraction_arg[i]); |
241 |
> |
for (int i = 0;i<nComponents-1;i++){ |
242 |
> |
numMol[i] = int((RealType)numSites * args_info.molFraction_arg[i]); |
243 |
|
std::cout<<numMol[i]<<std::endl; |
244 |
|
totComponents += numMol[i]; |
245 |
|
} |
246 |
|
numMol[nComponents-1] = numSites - totComponents; |
247 |
|
|
248 |
+ |
outputFileName = args_info.output_arg; |
249 |
|
|
250 |
< |
outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType; |
228 |
< |
outMdFileName = outPrefix + ".md"; |
250 |
> |
// create a new .md file on the fly which corrects the number of molecules |
251 |
|
|
252 |
< |
//creat new .md file on fly which corrects the number of molecule |
253 |
< |
createMdFile(inputFileName, outMdFileName, nComponents,numMol); |
252 |
> |
createMdFile(inputFileName, outputFileName, nComponents, numMol); |
253 |
> |
|
254 |
> |
if (oldInfo != NULL) |
255 |
> |
delete oldInfo; |
256 |
> |
|
257 |
> |
// We need to read in the new SimInfo object, then Parse the |
258 |
> |
// md file and set up the system |
259 |
|
|
260 |
+ |
SimCreator newCreator; |
261 |
+ |
SimInfo* newInfo = newCreator.createSim(outputFileName, false); |
262 |
|
|
263 |
+ |
// fill Hmat |
264 |
|
|
265 |
< |
//determine the output file names |
236 |
< |
if (args_info.output_given){ |
237 |
< |
outInitFileName = args_info.output_arg; |
238 |
< |
}else{ |
239 |
< |
outInitFileName = getPrefix(inputFileName.c_str()) + ".in"; |
240 |
< |
} |
241 |
< |
|
242 |
< |
//fill Hmat |
243 |
< |
hmat(0, 0)= nx * latticeConstant; |
265 |
> |
hmat(0, 0) = nx * latticeConstant; |
266 |
|
hmat(0, 1) = 0.0; |
267 |
|
hmat(0, 2) = 0.0; |
268 |
|
|
274 |
|
hmat(2, 1) = 0.0; |
275 |
|
hmat(2, 2) = nz * latticeConstant; |
276 |
|
|
277 |
< |
//set Hmat |
256 |
< |
oldInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat); |
277 |
> |
// Set Hmat |
278 |
|
|
279 |
< |
//place the molecules |
279 |
> |
newInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat); |
280 |
|
|
281 |
+ |
// place the molecules |
282 |
+ |
|
283 |
|
curMolIndex = 0; |
284 |
|
|
285 |
< |
//get the orientation of the cell sites |
263 |
< |
//for the same type of molecule in same lattice, it will not change |
264 |
< |
latticeOrt = simpleLat->getLatticePointsOrt(); |
285 |
> |
// Randomize a vector of ints: |
286 |
|
|
287 |
< |
|
288 |
< |
/* Randomize position vector */ |
289 |
< |
std::random_shuffle(latticeSites.begin(), latticeSites.end()); |
287 |
> |
vector<int> ids; |
288 |
> |
for (int i = 0; i < sites.size(); i++) ids.push_back(i); |
289 |
> |
std::random_shuffle(ids.begin(), ids.end()); |
290 |
|
|
270 |
– |
|
271 |
– |
if (oldInfo != NULL) |
272 |
– |
delete oldInfo; |
273 |
– |
|
274 |
– |
|
275 |
– |
// We need to read in new siminfo object. |
276 |
– |
//parse md file and set up the system |
277 |
– |
//SimCreator NewCreator; |
278 |
– |
SimCreator newCreator; |
279 |
– |
SimInfo* NewInfo = newCreator.createSim(outMdFileName, false); |
280 |
– |
|
281 |
– |
/* create Molocators */ |
282 |
– |
locator = new MoLocator(NewInfo->getMoleculeStamp(0), NewInfo->getForceField()); |
283 |
– |
|
284 |
– |
|
285 |
– |
|
291 |
|
Molecule* mol; |
287 |
– |
SimInfo::MoleculeIterator mi; |
288 |
– |
mol = NewInfo->beginMolecule(mi); |
292 |
|
int l = 0; |
293 |
< |
for (mol = NewInfo->beginMolecule(mi); mol != NULL; mol = NewInfo->nextMolecule(mi)) { |
294 |
< |
locator->placeMol(latticeSites[l], latticeOrt[l], mol); |
295 |
< |
l++; |
293 |
> |
for (int i = 0; i < nComponents; i++){ |
294 |
> |
locator = new MoLocator(newInfo->getMoleculeStamp(i), |
295 |
> |
newInfo->getForceField()); |
296 |
> |
for (int n = 0; n < numMol[i]; n++) { |
297 |
> |
mol = newInfo->getMoleculeByGlobalIndex(l); |
298 |
> |
locator->placeMol(sites[ids[l]], orientations[ids[l]], mol); |
299 |
> |
l++; |
300 |
> |
} |
301 |
|
} |
302 |
+ |
|
303 |
+ |
// Create DumpWriter and write out the coordinates |
304 |
|
|
305 |
+ |
writer = new DumpWriter(newInfo, outputFileName); |
306 |
|
|
296 |
– |
|
297 |
– |
//create dumpwriter and write out the coordinates |
298 |
– |
oldInfo->setFinalConfigFileName(outInitFileName); |
299 |
– |
writer = new DumpWriter(oldInfo); |
300 |
– |
|
307 |
|
if (writer == NULL) { |
308 |
< |
std::cerr << "error in creating DumpWriter" << std::endl; |
309 |
< |
exit(1); |
308 |
> |
sprintf(painCave.errMsg, "error in creating DumpWriter"); |
309 |
> |
painCave.isFatal = 1; |
310 |
> |
simError(); |
311 |
|
} |
312 |
|
|
313 |
|
writer->writeDump(); |
307 |
– |
std::cout << "new initial configuration file: " << outInitFileName |
308 |
– |
<< " is generated." << std::endl; |
314 |
|
|
315 |
< |
//delete objects |
315 |
> |
// deleting the writer will put the closing at the end of the dump file. |
316 |
|
|
317 |
< |
//delete oldInfo and oldSimSetup |
313 |
< |
if (oldInfo != NULL) |
314 |
< |
delete oldInfo; |
317 |
> |
delete writer; |
318 |
|
|
319 |
< |
if (writer != NULL) |
320 |
< |
delete writer; |
321 |
< |
|
322 |
< |
delete simpleLat; |
320 |
< |
|
319 |
> |
sprintf(painCave.errMsg, "A new OOPSE MD file called \"%s\" has been " |
320 |
> |
"generated.", outputFileName.c_str()); |
321 |
> |
painCave.isFatal = 0; |
322 |
> |
simError(); |
323 |
|
return 0; |
324 |
|
} |
325 |
|
|
326 |
< |
void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName, |
327 |
< |
int components,int* numMol) { |
326 |
> |
void createMdFile(const std::string&oldMdFileName, |
327 |
> |
const std::string&newMdFileName, |
328 |
> |
int components, int* numMol) { |
329 |
|
ifstream oldMdFile; |
330 |
|
ofstream newMdFile; |
331 |
|
const int MAXLEN = 65535; |
332 |
|
char buffer[MAXLEN]; |
333 |
|
|
334 |
|
//create new .md file based on old .md file |
335 |
+ |
|
336 |
|
oldMdFile.open(oldMdFileName.c_str()); |
337 |
|
newMdFile.open(newMdFileName.c_str()); |
338 |
|
|
344 |
|
//correct molecule number |
345 |
|
if (strstr(buffer, "nMol") != NULL) { |
346 |
|
if(i<components){ |
347 |
< |
sprintf(buffer, "\tnMol = %i;", numMol[i]); |
347 |
> |
sprintf(buffer, "\tnMol = %i;", numMol[i]); |
348 |
|
newMdFile << buffer << std::endl; |
349 |
|
i++; |
350 |
|
} |