| 1 | chuckv | 467 | /* | 
| 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 |  |  | #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 "nanorodBuilderCmd.h" | 
| 52 |  |  | //#include "GeometryBuilder.hpp" | 
| 53 |  |  | #include "applications/simpleBuilder/LatticeFactory.hpp" | 
| 54 |  |  | #include "applications/simpleBuilder/MoLocator.hpp" | 
| 55 |  |  | #include "applications/simpleBuilder/Lattice.hpp" | 
| 56 |  |  | #include "brains/Register.hpp" | 
| 57 |  |  | #include "brains/SimInfo.hpp" | 
| 58 |  |  | #include "brains/SimCreator.hpp" | 
| 59 |  |  | #include "io/DumpWriter.hpp" | 
| 60 |  |  | #include "math/Vector3.hpp" | 
| 61 |  |  | #include "math/SquareMatrix3.hpp" | 
| 62 |  |  | #include "utils/StringUtils.hpp" | 
| 63 |  |  |  | 
| 64 |  |  | using namespace std; | 
| 65 |  |  | using namespace oopse; | 
| 66 |  |  | void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName, | 
| 67 |  |  | int numMol); | 
| 68 |  |  |  | 
| 69 |  |  | int main(int argc, char *argv []) { | 
| 70 |  |  |  | 
| 71 |  |  | //register force fields | 
| 72 |  |  | registerForceFields(); | 
| 73 |  |  |  | 
| 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 |  |  | std::string outGeomFileName; | 
| 81 |  |  |  | 
| 82 |  |  |  | 
| 83 |  |  | BaseLattice *simpleLat; | 
| 84 |  |  | int numMol; | 
| 85 |  |  | double latticeConstant; | 
| 86 |  |  | std::vector<double> lc; | 
| 87 |  |  | double mass; | 
| 88 |  |  | const double rhoConvertConst = 1.661; | 
| 89 |  |  | double density; | 
| 90 |  |  | double rodLength; | 
| 91 |  |  | double rodDiameter; | 
| 92 |  |  |  | 
| 93 |  |  |  | 
| 94 |  |  | int nx, | 
| 95 |  |  | ny, | 
| 96 |  |  | nz; | 
| 97 |  |  | Mat3x3d hmat; | 
| 98 |  |  | MoLocator *locator; | 
| 99 |  |  | std::vector<Vector3d> latticePos; | 
| 100 |  |  | std::vector<Vector3d> latticeOrt; | 
| 101 |  |  | int numMolPerCell; | 
| 102 |  |  | int curMolIndex; | 
| 103 |  |  | DumpWriter *writer; | 
| 104 |  |  |  | 
| 105 |  |  | // parse command line arguments | 
| 106 |  |  | if (cmdline_parser(argc, argv, &args_info) != 0) | 
| 107 |  |  | exit(1); | 
| 108 |  |  |  | 
| 109 |  |  |  | 
| 110 |  |  |  | 
| 111 |  |  | //get lattice type | 
| 112 |  |  | latticeType = UpperCase(args_info.latticetype_arg); | 
| 113 |  |  |  | 
| 114 |  |  | if (!LatticeFactory::getInstance()->hasLatticeCreator(latticeType)) { | 
| 115 |  |  | std::cerr << latticeType << " is an invalid lattice type" << std::endl; | 
| 116 |  |  | std::cerr << LatticeFactory::getInstance()->toString() << std::endl; | 
| 117 |  |  | exit(1); | 
| 118 |  |  | } | 
| 119 |  |  |  | 
| 120 |  |  |  | 
| 121 |  |  | //get input file name | 
| 122 |  |  | if (args_info.inputs_num) | 
| 123 |  |  | inputFileName = args_info.inputs[0]; | 
| 124 |  |  | else { | 
| 125 |  |  | std::cerr << "You must specify a input file name.\n" << std::endl; | 
| 126 |  |  | cmdline_parser_print_help(); | 
| 127 |  |  | exit(1); | 
| 128 |  |  | } | 
| 129 |  |  |  | 
| 130 |  |  | //parse md file and set up the system | 
| 131 |  |  | SimCreator oldCreator; | 
| 132 |  |  | SimInfo* oldInfo = oldCreator.createSim(inputFileName, false); | 
| 133 |  |  |  | 
| 134 |  |  | if (oldInfo->getNMoleculeStamp()> 1) { | 
| 135 |  |  | std::cerr << "can not build nanorod with more than one components" | 
| 136 |  |  | << std::endl; | 
| 137 |  |  | exit(1); | 
| 138 |  |  | } | 
| 139 |  |  |  | 
| 140 |  |  | //get mass of molecule. | 
| 141 |  |  |  | 
| 142 |  |  | mass = getMolMass(oldInfo->getMoleculeStamp(0), oldInfo->getForceField()); | 
| 143 |  |  |  | 
| 144 |  |  | //creat lattice | 
| 145 |  |  | simpleLat = LatticeFactory::getInstance()->createLattice(latticeType); | 
| 146 |  |  |  | 
| 147 |  |  | if (simpleLat == NULL) { | 
| 148 |  |  | std::cerr << "Error in creating lattice" << std::endl; | 
| 149 |  |  | exit(1); | 
| 150 |  |  | } | 
| 151 |  |  |  | 
| 152 |  |  | numMolPerCell = simpleLat->getNumSitesPerCell(); | 
| 153 |  |  |  | 
| 154 |  |  | //calculate lattice constant (in Angstrom) | 
| 155 |  |  | //latticeConstant = pow(rhoConvertConst * numMolPerCell * mass / density, | 
| 156 |  |  | //                      1.0 / 3.0); | 
| 157 |  |  |  | 
| 158 |  |  | latticeConstant = args_info.latticeCnst_arg; | 
| 159 |  |  | rodLength = args_info.length_arg; | 
| 160 |  |  | rodDiameter = args_info.width_arg; | 
| 161 |  |  |  | 
| 162 |  |  | //set lattice constant | 
| 163 |  |  | lc.push_back(latticeConstant); | 
| 164 |  |  | simpleLat->setLatticeConstant(lc); | 
| 165 |  |  |  | 
| 166 |  |  |  | 
| 167 |  |  | //determine the output file names | 
| 168 |  |  | if (args_info.output_given) | 
| 169 |  |  | outInitFileName = args_info.output_arg; | 
| 170 |  |  | else | 
| 171 |  |  | outInitFileName = getPrefix(inputFileName.c_str()) + ".in"; | 
| 172 |  |  |  | 
| 173 |  |  | //creat Molocator | 
| 174 |  |  | locator = new MoLocator(oldInfo->getMoleculeStamp(0), oldInfo->getForceField()); | 
| 175 |  |  |  | 
| 176 |  |  | /* | 
| 177 |  |  | Assume we are carving nanorod out of a cublic block of material and that | 
| 178 |  |  | the shape the material will fit within that block.... | 
| 179 |  |  | The model in geometry builder assumes the long axis is in the y direction and the x-z plane is the | 
| 180 |  |  | diameter of the particle. | 
| 181 |  |  | */ | 
| 182 |  |  | // Number of Unit Cells in Length first | 
| 183 |  |  | ny = (int)(rodLength/latticeConstant); | 
| 184 |  |  | // Number of unit cells in Width | 
| 185 |  |  | nx = (int)(rodDiameter/latticeConstant); | 
| 186 |  |  | nz = (int)(rodDiameter/latticeConstant); | 
| 187 |  |  |  | 
| 188 |  |  |  | 
| 189 |  |  |  | 
| 190 |  |  | // Create geometry for nanocrystal | 
| 191 |  |  | //GeometryBuilder myGeometry(rodLength,rodDiameter); | 
| 192 |  |  |  | 
| 193 |  |  | /* | 
| 194 |  |  | We have to build the system first to figure out how many molecules there are | 
| 195 |  |  | then create a md file and then actually build the system. | 
| 196 |  |  | */ | 
| 197 |  |  |  | 
| 198 |  |  | //place the molecules | 
| 199 |  |  |  | 
| 200 |  |  | curMolIndex = 0; | 
| 201 |  |  |  | 
| 202 |  |  | //get the orientation of the cell sites | 
| 203 |  |  | //for the same type of molecule in same lattice, it will not change | 
| 204 |  |  | latticeOrt = simpleLat->getLatticePointsOrt(); | 
| 205 |  |  |  | 
| 206 |  |  |  | 
| 207 |  |  | /* | 
| 208 |  |  | void BaseLattice::getLatticePointsPos(std::vector<Vector3d>& latticePos, int nx, int ny, int nz){ | 
| 209 |  |  |  | 
| 210 |  |  | latticePos.resize(nCellSites); | 
| 211 |  |  |  | 
| 212 |  |  | for( int i=0;i < nCellSites;i++){ | 
| 213 |  |  |  | 
| 214 |  |  | latticePos[i][0] = origin[0] + cellSitesPos[i][0] + cellLen[0] * (double(nx) - 0.5); | 
| 215 |  |  | latticePos[i][1] = origin[1] + cellSitesPos[i][1] + cellLen[1] * (double(ny) - 0.5); | 
| 216 |  |  | latticePos[i][2] = origin[2] + cellSitesPos[i][2] + cellLen[2] * (double(nz) - 0.5); | 
| 217 |  |  | } | 
| 218 |  |  |  | 
| 219 |  |  | */ | 
| 220 |  |  |  | 
| 221 |  |  |  | 
| 222 |  |  |  | 
| 223 |  |  |  | 
| 224 |  |  | numMol = 0; | 
| 225 |  |  | for(int i = 0; i < nx; i++) { | 
| 226 |  |  | for(int j = 0; j < ny; j++) { | 
| 227 |  |  | for(int k = 0; k < nz; k++) { | 
| 228 |  |  | //if (oldInfo->getNGlobalMolecules() != numMol) { | 
| 229 |  |  |  | 
| 230 |  |  |  | 
| 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 |  |  | /* | 
| 237 |  |  | if (myGeometry.isInsidePolyhedron(latticePos[l][0],latticePos[l][1],latticePos[l][2])){ | 
| 238 |  |  | numMol++; | 
| 239 |  |  | } | 
| 240 |  |  | */ | 
| 241 |  |  | numMol++; | 
| 242 |  |  | } | 
| 243 |  |  | } | 
| 244 |  |  | } | 
| 245 |  |  | } | 
| 246 |  |  |  | 
| 247 |  |  |  | 
| 248 |  |  | // needed for writing out new md file. | 
| 249 |  |  |  | 
| 250 |  |  | outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType; | 
| 251 |  |  | outMdFileName = outPrefix + ".md"; | 
| 252 |  |  |  | 
| 253 |  |  | //creat new .md file on fly which corrects the number of molecule | 
| 254 |  |  | createMdFile(inputFileName, outMdFileName, numMol); | 
| 255 |  |  |  | 
| 256 |  |  | if (oldInfo != NULL) | 
| 257 |  |  | delete oldInfo; | 
| 258 |  |  |  | 
| 259 |  |  |  | 
| 260 |  |  | // We need to read in new siminfo object. | 
| 261 |  |  | //parse md file and set up the system | 
| 262 |  |  | //SimCreator NewCreator; | 
| 263 |  |  |  | 
| 264 |  |  | SimInfo* NewInfo = oldCreator.createSim(outMdFileName, false); | 
| 265 |  |  |  | 
| 266 |  |  | // This was so much fun the first time, lets do it again. | 
| 267 |  |  |  | 
| 268 |  |  | Molecule* mol; | 
| 269 |  |  | SimInfo::MoleculeIterator mi; | 
| 270 |  |  | mol = NewInfo->beginMolecule(mi); | 
| 271 |  |  |  | 
| 272 |  |  | for(int i = 0; i < nx; i++) { | 
| 273 |  |  | for(int j = 0; j < ny; j++) { | 
| 274 |  |  | for(int k = 0; k < nz; k++) { | 
| 275 |  |  |  | 
| 276 |  |  | //get the position of the cell sites | 
| 277 |  |  | simpleLat->getLatticePointsPos(latticePos, i, j, k); | 
| 278 |  |  |  | 
| 279 |  |  | for(int l = 0; l < numMolPerCell; l++) { | 
| 280 |  |  | if (mol != NULL) { | 
| 281 |  |  | /* | 
| 282 |  |  | if (myGeometry.isInsidePolyhedron(latticePos[l][0],latticePos[l][1],latticePos[l][2])){ | 
| 283 |  |  | locator->placeMol(latticePos[l], latticeOrt[l], mol); | 
| 284 |  |  | } | 
| 285 |  |  | */ | 
| 286 |  |  | locator->placeMol(latticePos[l], latticeOrt[l], mol); | 
| 287 |  |  | } else { | 
| 288 |  |  | std::cerr << std::endl; | 
| 289 |  |  | } | 
| 290 |  |  | mol = NewInfo->nextMolecule(mi); | 
| 291 |  |  | } | 
| 292 |  |  | } | 
| 293 |  |  | } | 
| 294 |  |  | } | 
| 295 |  |  |  | 
| 296 |  |  |  | 
| 297 |  |  |  | 
| 298 |  |  | //fill Hmat | 
| 299 |  |  | hmat(0, 0)= nx * latticeConstant; | 
| 300 |  |  | hmat(0, 1) = 0.0; | 
| 301 |  |  | hmat(0, 2) = 0.0; | 
| 302 |  |  |  | 
| 303 |  |  | hmat(1, 0) = 0.0; | 
| 304 |  |  | hmat(1, 1) = ny * latticeConstant; | 
| 305 |  |  | hmat(1, 2) = 0.0; | 
| 306 |  |  |  | 
| 307 |  |  | hmat(2, 0) = 0.0; | 
| 308 |  |  | hmat(2, 1) = 0.0; | 
| 309 |  |  | hmat(2, 2) = nz * latticeConstant; | 
| 310 |  |  |  | 
| 311 |  |  | //set Hmat | 
| 312 |  |  | NewInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat); | 
| 313 |  |  |  | 
| 314 |  |  |  | 
| 315 |  |  | //create dumpwriter and write out the coordinates | 
| 316 |  |  | NewInfo->setFinalConfigFileName(outInitFileName); | 
| 317 |  |  | writer = new DumpWriter(NewInfo); | 
| 318 |  |  |  | 
| 319 |  |  | if (writer == NULL) { | 
| 320 |  |  | std::cerr << "error in creating DumpWriter" << std::endl; | 
| 321 |  |  | exit(1); | 
| 322 |  |  | } | 
| 323 |  |  |  | 
| 324 |  |  | writer->writeDump(); | 
| 325 |  |  | std::cout << "new initial configuration file: " << outInitFileName | 
| 326 |  |  | << " is generated." << std::endl; | 
| 327 |  |  |  | 
| 328 |  |  | //delete objects | 
| 329 |  |  |  | 
| 330 |  |  | //delete oldInfo and oldSimSetup | 
| 331 |  |  |  | 
| 332 |  |  | if (NewInfo != NULL) | 
| 333 |  |  | delete NewInfo; | 
| 334 |  |  |  | 
| 335 |  |  | if (writer != NULL) | 
| 336 |  |  | delete writer; | 
| 337 |  |  |  | 
| 338 |  |  | return 0; | 
| 339 |  |  | } | 
| 340 |  |  |  | 
| 341 |  |  | void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName, | 
| 342 |  |  | int numMol) { | 
| 343 |  |  | ifstream oldMdFile; | 
| 344 |  |  | ofstream newMdFile; | 
| 345 |  |  | const int MAXLEN = 65535; | 
| 346 |  |  | char buffer[MAXLEN]; | 
| 347 |  |  |  | 
| 348 |  |  | //create new .md file based on old .md file | 
| 349 |  |  | oldMdFile.open(oldMdFileName.c_str()); | 
| 350 |  |  | newMdFile.open(newMdFileName.c_str()); | 
| 351 |  |  |  | 
| 352 |  |  | oldMdFile.getline(buffer, MAXLEN); | 
| 353 |  |  |  | 
| 354 |  |  | while (!oldMdFile.eof()) { | 
| 355 |  |  |  | 
| 356 |  |  | //correct molecule number | 
| 357 |  |  | if (strstr(buffer, "nMol") != NULL) { | 
| 358 |  |  | sprintf(buffer, "\tnMol = %i;", numMol); | 
| 359 |  |  | newMdFile << buffer << std::endl; | 
| 360 |  |  | } else | 
| 361 |  |  | newMdFile << buffer << std::endl; | 
| 362 |  |  |  | 
| 363 |  |  | oldMdFile.getline(buffer, MAXLEN); | 
| 364 |  |  | } | 
| 365 |  |  |  | 
| 366 |  |  | oldMdFile.close(); | 
| 367 |  |  | newMdFile.close(); | 
| 368 |  |  | } | 
| 369 |  |  |  |