| 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 $ |
| 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; |
| 87 |
> |
std::string outputFileName; |
| 88 |
|
Lattice *simpleLat; |
| 89 |
|
int* numMol; |
| 90 |
|
RealType latticeConstant; |
| 92 |
|
RealType mass; |
| 93 |
|
const RealType rhoConvertConst = 1.661; |
| 94 |
|
RealType density; |
| 95 |
< |
int nx, |
| 97 |
< |
ny, |
| 98 |
< |
nz; |
| 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); |
| 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 |
|
(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 */ |
| 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; |
| 227 |
< |
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 |
| 230 |
< |
createMdFile(inputFileName, outMdFileName, nComponents, numMol); |
| 252 |
> |
createMdFile(inputFileName, outputFileName, nComponents, numMol); |
| 253 |
|
|
| 254 |
< |
//fill Hmat |
| 255 |
< |
hmat(0, 0)= nx * latticeConstant; |
| 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 |
> |
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 |
| 246 |
< |
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 |
| 253 |
< |
//for the same type of molecule in same lattice, it will not change |
| 254 |
< |
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 |
|
|
| 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 |
– |
|
| 291 |
|
Molecule* mol; |
| 275 |
– |
SimInfo::MoleculeIterator mi; |
| 276 |
– |
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 |
< |
//create dumpwriter and write out the coordinates |
| 284 |
< |
newInfo->setFinalConfigFileName(outMdFileName); |
| 285 |
< |
writer = new DumpWriter(newInfo); |
| 305 |
> |
writer = new DumpWriter(newInfo, outputFileName); |
| 306 |
|
|
| 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->writeEor(); |
| 314 |
< |
std::cout << "new OOPSE MD file: " << outMdFileName |
| 315 |
< |
<< " has been generated." << std::endl; |
| 313 |
> |
writer->writeDump(); |
| 314 |
> |
|
| 315 |
> |
// deleting the writer will put the closing at the end of the dump file. |
| 316 |
> |
|
| 317 |
> |
delete writer; |
| 318 |
> |
|
| 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 |
|
} |