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