| 1 |
gezelter |
246 |
/* |
| 2 |
|
|
* 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 |
12 |
#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 |
|
|
#include "applications/simpleBuilder/LatticeFactory.hpp" |
| 53 |
|
|
#include "applications/simpleBuilder/MoLocator.hpp" |
| 54 |
|
|
#include "applications/simpleBuilder/Lattice.hpp" |
| 55 |
gezelter |
246 |
#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 |
12 |
|
| 63 |
|
|
using namespace std; |
| 64 |
gezelter |
246 |
using namespace oopse; |
| 65 |
|
|
void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName, |
| 66 |
|
|
int numMol); |
| 67 |
tim |
12 |
|
| 68 |
gezelter |
246 |
int main(int argc, char *argv []) { |
| 69 |
tim |
12 |
|
| 70 |
gezelter |
246 |
//register force fields |
| 71 |
|
|
registerForceFields(); |
| 72 |
|
|
|
| 73 |
|
|
gengetopt_args_info args_info; |
| 74 |
|
|
std::string latticeType; |
| 75 |
|
|
std::string inputFileName; |
| 76 |
|
|
std::string outPrefix; |
| 77 |
|
|
std::string outMdFileName; |
| 78 |
|
|
std::string outInitFileName; |
| 79 |
|
|
BaseLattice *simpleLat; |
| 80 |
|
|
int numMol; |
| 81 |
|
|
double latticeConstant; |
| 82 |
|
|
std::vector<double> lc; |
| 83 |
|
|
double mass; |
| 84 |
|
|
const double rhoConvertConst = 1.661; |
| 85 |
|
|
double density; |
| 86 |
|
|
int nx, |
| 87 |
|
|
ny, |
| 88 |
|
|
nz; |
| 89 |
|
|
Mat3x3d hmat; |
| 90 |
|
|
MoLocator *locator; |
| 91 |
|
|
std::vector<Vector3d> latticePos; |
| 92 |
|
|
std::vector<Vector3d> latticeOrt; |
| 93 |
|
|
int numMolPerCell; |
| 94 |
|
|
int curMolIndex; |
| 95 |
|
|
DumpWriter *writer; |
| 96 |
tim |
12 |
|
| 97 |
gezelter |
246 |
// parse command line arguments |
| 98 |
|
|
if (cmdline_parser(argc, argv, &args_info) != 0) |
| 99 |
|
|
exit(1); |
| 100 |
tim |
12 |
|
| 101 |
gezelter |
246 |
density = args_info.density_arg; |
| 102 |
tim |
12 |
|
| 103 |
gezelter |
246 |
//get lattice type |
| 104 |
|
|
latticeType = UpperCase(args_info.latticetype_arg); |
| 105 |
tim |
12 |
|
| 106 |
gezelter |
246 |
if (!LatticeFactory::getInstance()->hasLatticeCreator(latticeType)) { |
| 107 |
|
|
std::cerr << latticeType << " is an invalid lattice type" << std::endl; |
| 108 |
|
|
std::cerr << LatticeFactory::getInstance()->toString() << std::endl; |
| 109 |
|
|
exit(1); |
| 110 |
|
|
} |
| 111 |
tim |
12 |
|
| 112 |
gezelter |
246 |
//get the number of unit cell |
| 113 |
|
|
nx = args_info.nx_arg; |
| 114 |
tim |
12 |
|
| 115 |
gezelter |
246 |
if (nx <= 0) { |
| 116 |
|
|
std::cerr << "The number of unit cell in h direction must be greater than 0" << std::endl; |
| 117 |
|
|
exit(1); |
| 118 |
|
|
} |
| 119 |
tim |
12 |
|
| 120 |
gezelter |
246 |
ny = args_info.ny_arg; |
| 121 |
tim |
12 |
|
| 122 |
gezelter |
246 |
if (ny <= 0) { |
| 123 |
|
|
std::cerr << "The number of unit cell in l direction must be greater than 0" << std::endl; |
| 124 |
|
|
exit(1); |
| 125 |
|
|
} |
| 126 |
tim |
12 |
|
| 127 |
gezelter |
246 |
nz = args_info.nz_arg; |
| 128 |
tim |
12 |
|
| 129 |
gezelter |
246 |
if (nz <= 0) { |
| 130 |
|
|
std::cerr << "The number of unit cell in k direction must be greater than 0" << std::endl; |
| 131 |
|
|
exit(1); |
| 132 |
|
|
} |
| 133 |
tim |
12 |
|
| 134 |
gezelter |
246 |
//get input file name |
| 135 |
|
|
if (args_info.inputs_num) |
| 136 |
|
|
inputFileName = args_info.inputs[0]; |
| 137 |
|
|
else { |
| 138 |
|
|
std::cerr << "You must specify a input file name.\n" << std::endl; |
| 139 |
|
|
cmdline_parser_print_help(); |
| 140 |
|
|
exit(1); |
| 141 |
|
|
} |
| 142 |
tim |
12 |
|
| 143 |
gezelter |
246 |
//parse md file and set up the system |
| 144 |
|
|
SimCreator oldCreator; |
| 145 |
|
|
SimInfo* oldInfo = oldCreator.createSim(inputFileName, false); |
| 146 |
tim |
12 |
|
| 147 |
gezelter |
246 |
if (oldInfo->getNMoleculeStamp()>= 2) { |
| 148 |
|
|
std::cerr << "can not build the system with more than two components" |
| 149 |
|
|
<< std::endl; |
| 150 |
|
|
exit(1); |
| 151 |
|
|
} |
| 152 |
tim |
12 |
|
| 153 |
gezelter |
246 |
//get mass of molecule. |
| 154 |
tim |
12 |
|
| 155 |
gezelter |
246 |
mass = getMolMass(oldInfo->getMoleculeStamp(0), oldInfo->getForceField()); |
| 156 |
tim |
12 |
|
| 157 |
gezelter |
246 |
//creat lattice |
| 158 |
|
|
simpleLat = LatticeFactory::getInstance()->createLattice(latticeType); |
| 159 |
tim |
12 |
|
| 160 |
gezelter |
246 |
if (simpleLat == NULL) { |
| 161 |
|
|
std::cerr << "Error in creating lattice" << std::endl; |
| 162 |
|
|
exit(1); |
| 163 |
|
|
} |
| 164 |
tim |
12 |
|
| 165 |
gezelter |
246 |
numMolPerCell = simpleLat->getNumSitesPerCell(); |
| 166 |
tim |
12 |
|
| 167 |
gezelter |
246 |
//calculate lattice constant (in Angstrom) |
| 168 |
|
|
latticeConstant = pow(rhoConvertConst * numMolPerCell * mass / density, |
| 169 |
|
|
1.0 / 3.0); |
| 170 |
tim |
12 |
|
| 171 |
gezelter |
246 |
//set lattice constant |
| 172 |
|
|
lc.push_back(latticeConstant); |
| 173 |
|
|
simpleLat->setLatticeConstant(lc); |
| 174 |
tim |
12 |
|
| 175 |
gezelter |
246 |
//calculate the total number of molecules |
| 176 |
|
|
numMol = nx * ny * nz * numMolPerCell; |
| 177 |
tim |
12 |
|
| 178 |
gezelter |
246 |
if (oldInfo->getNGlobalMolecules() != numMol) { |
| 179 |
|
|
outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType; |
| 180 |
|
|
outMdFileName = outPrefix + ".md"; |
| 181 |
|
|
|
| 182 |
|
|
//creat new .md file on fly which corrects the number of molecule |
| 183 |
|
|
createMdFile(inputFileName, outMdFileName, numMol); |
| 184 |
|
|
std::cerr |
| 185 |
|
|
<< "SimpleBuilder Error: the number of molecule and the density are not matched" |
| 186 |
|
|
<< std::endl; |
| 187 |
|
|
std::cerr << "A new .md file: " << outMdFileName |
| 188 |
|
|
<< " is generated, use it to rerun the simpleBuilder" << std::endl; |
| 189 |
|
|
exit(1); |
| 190 |
tim |
12 |
} |
| 191 |
|
|
|
| 192 |
gezelter |
246 |
//determine the output file names |
| 193 |
|
|
if (args_info.output_given) |
| 194 |
|
|
outInitFileName = args_info.output_arg; |
| 195 |
|
|
else |
| 196 |
|
|
outInitFileName = getPrefix(inputFileName.c_str()) + ".in"; |
| 197 |
|
|
|
| 198 |
|
|
//creat Molocator |
| 199 |
|
|
locator = new MoLocator(oldInfo->getMoleculeStamp(0), oldInfo->getForceField()); |
| 200 |
tim |
12 |
|
| 201 |
gezelter |
246 |
//fill Hmat |
| 202 |
|
|
hmat(0, 0)= nx * latticeConstant; |
| 203 |
|
|
hmat(0, 1) = 0.0; |
| 204 |
|
|
hmat(0, 2) = 0.0; |
| 205 |
tim |
12 |
|
| 206 |
gezelter |
246 |
hmat(1, 0) = 0.0; |
| 207 |
|
|
hmat(1, 1) = ny * latticeConstant; |
| 208 |
|
|
hmat(1, 2) = 0.0; |
| 209 |
tim |
12 |
|
| 210 |
gezelter |
246 |
hmat(2, 0) = 0.0; |
| 211 |
|
|
hmat(2, 1) = 0.0; |
| 212 |
|
|
hmat(2, 2) = nz * latticeConstant; |
| 213 |
tim |
12 |
|
| 214 |
gezelter |
246 |
//set Hmat |
| 215 |
|
|
oldInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat); |
| 216 |
tim |
12 |
|
| 217 |
gezelter |
246 |
//place the molecules |
| 218 |
|
|
|
| 219 |
|
|
curMolIndex = 0; |
| 220 |
|
|
|
| 221 |
|
|
//get the orientation of the cell sites |
| 222 |
|
|
//for the same type of molecule in same lattice, it will not change |
| 223 |
|
|
latticeOrt = simpleLat->getLatticePointsOrt(); |
| 224 |
|
|
|
| 225 |
|
|
Molecule* mol; |
| 226 |
|
|
SimInfo::MoleculeIterator mi; |
| 227 |
|
|
mol = oldInfo->beginMolecule(mi); |
| 228 |
|
|
for(int i = 0; i < nx; i++) { |
| 229 |
|
|
for(int j = 0; j < ny; j++) { |
| 230 |
|
|
for(int k = 0; k < nz; k++) { |
| 231 |
|
|
|
| 232 |
|
|
//get the position of the cell sites |
| 233 |
|
|
simpleLat->getLatticePointsPos(latticePos, i, j, k); |
| 234 |
|
|
|
| 235 |
|
|
for(int l = 0; l < numMolPerCell; l++) { |
| 236 |
|
|
if (mol != NULL) { |
| 237 |
|
|
locator->placeMol(latticePos[l], latticeOrt[l], mol); |
| 238 |
|
|
} else { |
| 239 |
|
|
std::cerr << std::endl; |
| 240 |
|
|
} |
| 241 |
|
|
mol = oldInfo->nextMolecule(mi); |
| 242 |
|
|
} |
| 243 |
|
|
} |
| 244 |
|
|
} |
| 245 |
tim |
12 |
} |
| 246 |
|
|
|
| 247 |
gezelter |
246 |
//create dumpwriter and write out the coordinates |
| 248 |
|
|
oldInfo->setFinalConfigFileName(outInitFileName); |
| 249 |
|
|
writer = new DumpWriter(oldInfo, oldInfo->getDumpFileName()); |
| 250 |
tim |
12 |
|
| 251 |
gezelter |
246 |
if (writer == NULL) { |
| 252 |
|
|
std::cerr << "error in creating DumpWriter" << std::endl; |
| 253 |
|
|
exit(1); |
| 254 |
|
|
} |
| 255 |
tim |
12 |
|
| 256 |
gezelter |
246 |
writer->writeDump(); |
| 257 |
|
|
std::cout << "new initial configuration file: " << outInitFileName |
| 258 |
|
|
<< " is generated." << std::endl; |
| 259 |
|
|
|
| 260 |
|
|
//delete objects |
| 261 |
|
|
|
| 262 |
|
|
//delete oldInfo and oldSimSetup |
| 263 |
|
|
if (oldInfo != NULL) |
| 264 |
|
|
delete oldInfo; |
| 265 |
|
|
|
| 266 |
|
|
if (writer != NULL) |
| 267 |
|
|
delete writer; |
| 268 |
|
|
|
| 269 |
|
|
return 0; |
| 270 |
tim |
12 |
} |
| 271 |
|
|
|
| 272 |
gezelter |
246 |
void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName, |
| 273 |
|
|
int numMol) { |
| 274 |
|
|
ifstream oldMdFile; |
| 275 |
|
|
ofstream newMdFile; |
| 276 |
|
|
const int MAXLEN = 65535; |
| 277 |
|
|
char buffer[MAXLEN]; |
| 278 |
tim |
12 |
|
| 279 |
gezelter |
246 |
//create new .md file based on old .md file |
| 280 |
|
|
oldMdFile.open(oldMdFileName.c_str()); |
| 281 |
|
|
newMdFile.open(newMdFileName.c_str()); |
| 282 |
tim |
12 |
|
| 283 |
gezelter |
246 |
oldMdFile.getline(buffer, MAXLEN); |
| 284 |
|
|
|
| 285 |
|
|
while (!oldMdFile.eof()) { |
| 286 |
|
|
|
| 287 |
|
|
//correct molecule number |
| 288 |
|
|
if (strstr(buffer, "nMol") != NULL) { |
| 289 |
|
|
sprintf(buffer, "\t\tnMol = %d;", numMol); |
| 290 |
|
|
newMdFile << buffer << std::endl; |
| 291 |
|
|
} else |
| 292 |
|
|
newMdFile << buffer << std::endl; |
| 293 |
|
|
|
| 294 |
|
|
oldMdFile.getline(buffer, MAXLEN); |
| 295 |
|
|
} |
| 296 |
|
|
|
| 297 |
|
|
oldMdFile.close(); |
| 298 |
|
|
newMdFile.close(); |
| 299 |
tim |
12 |
} |
| 300 |
gezelter |
246 |
|