| 1 | 
gezelter | 
507 | 
/* | 
| 2 | 
gezelter | 
246 | 
 * 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 | 
gezelter | 
1390 | 
 * 1. Redistributions of source code must retain the above copyright | 
| 10 | 
gezelter | 
246 | 
 *    notice, this list of conditions and the following disclaimer. | 
| 11 | 
  | 
  | 
 * | 
| 12 | 
gezelter | 
1390 | 
 * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 | 
gezelter | 
246 | 
 *    notice, this list of conditions and the following disclaimer in the | 
| 14 | 
  | 
  | 
 *    documentation and/or other materials provided with the | 
| 15 | 
  | 
  | 
 *    distribution. | 
| 16 | 
  | 
  | 
 * | 
| 17 | 
  | 
  | 
 * This software is provided "AS IS," without a warranty of any | 
| 18 | 
  | 
  | 
 * kind. All express or implied conditions, representations and | 
| 19 | 
  | 
  | 
 * warranties, including any implied warranty of merchantability, | 
| 20 | 
  | 
  | 
 * fitness for a particular purpose or non-infringement, are hereby | 
| 21 | 
  | 
  | 
 * excluded.  The University of Notre Dame and its licensors shall not | 
| 22 | 
  | 
  | 
 * be liable for any damages suffered by licensee as a result of | 
| 23 | 
  | 
  | 
 * using, modifying or distributing the software or its | 
| 24 | 
  | 
  | 
 * derivatives. In no event will the University of Notre Dame or its | 
| 25 | 
  | 
  | 
 * licensors be liable for any lost revenue, profit or data, or for | 
| 26 | 
  | 
  | 
 * direct, indirect, special, consequential, incidental or punitive | 
| 27 | 
  | 
  | 
 * damages, however caused and regardless of the theory of liability, | 
| 28 | 
  | 
  | 
 * arising out of the use of or inability to use software, even if the | 
| 29 | 
  | 
  | 
 * University of Notre Dame has been advised of the possibility of | 
| 30 | 
  | 
  | 
 * such damages. | 
| 31 | 
gezelter | 
1390 | 
 * | 
| 32 | 
  | 
  | 
 * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your | 
| 33 | 
  | 
  | 
 * research, please cite the appropriate papers when you publish your | 
| 34 | 
  | 
  | 
 * work.  Good starting points are: | 
| 35 | 
  | 
  | 
 *                                                                       | 
| 36 | 
  | 
  | 
 * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).              | 
| 37 | 
  | 
  | 
 * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).           | 
| 38 | 
  | 
  | 
 * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008).           | 
| 39 | 
  | 
  | 
 * [4]  Vardeman & Gezelter, in progress (2009).                         | 
| 40 | 
gezelter | 
246 | 
 */ | 
| 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 | 
gezelter | 
481 | 
#include "lattice/LatticeFactory.hpp" | 
| 53 | 
  | 
  | 
#include "utils/MoLocator.hpp" | 
| 54 | 
  | 
  | 
#include "lattice/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 | 
1390 | 
using namespace OpenMD; | 
| 65 | 
tim | 
12 | 
 | 
| 66 | 
gezelter | 
1067 | 
void createMdFile(const std::string&oldMdFileName,  | 
| 67 | 
  | 
  | 
                  const std::string&newMdFileName, | 
| 68 | 
  | 
  | 
                  int nMol); | 
| 69 | 
  | 
  | 
 | 
| 70 | 
gezelter | 
246 | 
int main(int argc, char *argv []) { | 
| 71 | 
tim | 
12 | 
 | 
| 72 | 
gezelter | 
1067 | 
  // register force fields | 
| 73 | 
gezelter | 
507 | 
  registerForceFields(); | 
| 74 | 
  | 
  | 
  registerLattice(); | 
| 75 | 
gezelter | 
246 | 
     | 
| 76 | 
gezelter | 
507 | 
  gengetopt_args_info args_info; | 
| 77 | 
  | 
  | 
  std::string latticeType; | 
| 78 | 
  | 
  | 
  std::string inputFileName; | 
| 79 | 
gezelter | 
1067 | 
  std::string outputFileName; | 
| 80 | 
gezelter | 
507 | 
  Lattice *simpleLat; | 
| 81 | 
gezelter | 
1067 | 
  RealType latticeConstant; | 
| 82 | 
  | 
  | 
  std::vector<RealType> lc; | 
| 83 | 
  | 
  | 
  const RealType rhoConvertConst = 1.661; | 
| 84 | 
  | 
  | 
  RealType density; | 
| 85 | 
  | 
  | 
  int nx, ny, nz; | 
| 86 | 
gezelter | 
507 | 
  Mat3x3d hmat; | 
| 87 | 
  | 
  | 
  MoLocator *locator; | 
| 88 | 
  | 
  | 
  std::vector<Vector3d> latticePos; | 
| 89 | 
  | 
  | 
  std::vector<Vector3d> latticeOrt; | 
| 90 | 
gezelter | 
1067 | 
  int nMolPerCell; | 
| 91 | 
gezelter | 
507 | 
  DumpWriter *writer; | 
| 92 | 
tim | 
12 | 
 | 
| 93 | 
gezelter | 
507 | 
  // parse command line arguments | 
| 94 | 
  | 
  | 
  if (cmdline_parser(argc, argv, &args_info) != 0) | 
| 95 | 
  | 
  | 
    exit(1); | 
| 96 | 
tim | 
12 | 
 | 
| 97 | 
gezelter | 
507 | 
  density = args_info.density_arg; | 
| 98 | 
tim | 
12 | 
 | 
| 99 | 
gezelter | 
507 | 
  //get lattice type | 
| 100 | 
gezelter | 
1067 | 
  latticeType = "FCC"; | 
| 101 | 
tim | 
12 | 
 | 
| 102 | 
gezelter | 
507 | 
  simpleLat = LatticeFactory::getInstance()->createLattice(latticeType); | 
| 103 | 
tim | 
487 | 
     | 
| 104 | 
gezelter | 
507 | 
  if (simpleLat == NULL) { | 
| 105 | 
  | 
  | 
    sprintf(painCave.errMsg, "Lattice Factory can not create %s lattice\n", | 
| 106 | 
  | 
  | 
            latticeType.c_str()); | 
| 107 | 
  | 
  | 
    painCave.isFatal = 1; | 
| 108 | 
  | 
  | 
    simError(); | 
| 109 | 
  | 
  | 
  } | 
| 110 | 
gezelter | 
1067 | 
  nMolPerCell = simpleLat->getNumSitesPerCell(); | 
| 111 | 
tim | 
12 | 
 | 
| 112 | 
gezelter | 
1067 | 
  //get the number of unit cells in each direction: | 
| 113 | 
  | 
  | 
 | 
| 114 | 
gezelter | 
507 | 
  nx = args_info.nx_arg; | 
| 115 | 
tim | 
12 | 
 | 
| 116 | 
gezelter | 
507 | 
  if (nx <= 0) { | 
| 117 | 
gezelter | 
1067 | 
    sprintf(painCave.errMsg, "The number of unit cells in the x direction " | 
| 118 | 
  | 
  | 
            "must be greater than 0."); | 
| 119 | 
  | 
  | 
    painCave.isFatal = 1; | 
| 120 | 
  | 
  | 
    simError(); | 
| 121 | 
gezelter | 
507 | 
  } | 
| 122 | 
tim | 
12 | 
 | 
| 123 | 
gezelter | 
507 | 
  ny = args_info.ny_arg; | 
| 124 | 
tim | 
12 | 
 | 
| 125 | 
gezelter | 
507 | 
  if (ny <= 0) { | 
| 126 | 
gezelter | 
1067 | 
    sprintf(painCave.errMsg, "The number of unit cells in the y direction " | 
| 127 | 
  | 
  | 
            "must be greater than 0."); | 
| 128 | 
  | 
  | 
    painCave.isFatal = 1; | 
| 129 | 
  | 
  | 
    simError(); | 
| 130 | 
gezelter | 
507 | 
  } | 
| 131 | 
tim | 
12 | 
 | 
| 132 | 
gezelter | 
507 | 
  nz = args_info.nz_arg; | 
| 133 | 
tim | 
12 | 
 | 
| 134 | 
gezelter | 
507 | 
  if (nz <= 0) { | 
| 135 | 
gezelter | 
1067 | 
    sprintf(painCave.errMsg, "The number of unit cells in the z direction " | 
| 136 | 
  | 
  | 
            "must be greater than 0."); | 
| 137 | 
  | 
  | 
    painCave.isFatal = 1; | 
| 138 | 
  | 
  | 
    simError(); | 
| 139 | 
gezelter | 
507 | 
  } | 
| 140 | 
tim | 
12 | 
 | 
| 141 | 
gezelter | 
1067 | 
  int nSites = nMolPerCell * nx * ny * nz; | 
| 142 | 
  | 
  | 
 | 
| 143 | 
gezelter | 
507 | 
  //get input file name | 
| 144 | 
  | 
  | 
  if (args_info.inputs_num) | 
| 145 | 
  | 
  | 
    inputFileName = args_info.inputs[0]; | 
| 146 | 
  | 
  | 
  else { | 
| 147 | 
gezelter | 
1067 | 
    sprintf(painCave.errMsg, "No input .md file name was specified " | 
| 148 | 
  | 
  | 
            "on the command line"); | 
| 149 | 
  | 
  | 
    painCave.isFatal = 1; | 
| 150 | 
  | 
  | 
    simError(); | 
| 151 | 
gezelter | 
507 | 
  } | 
| 152 | 
tim | 
12 | 
 | 
| 153 | 
gezelter | 
507 | 
  //parse md file and set up the system | 
| 154 | 
gezelter | 
1067 | 
 | 
| 155 | 
gezelter | 
507 | 
  SimCreator oldCreator; | 
| 156 | 
  | 
  | 
  SimInfo* oldInfo = oldCreator.createSim(inputFileName, false); | 
| 157 | 
gezelter | 
1067 | 
  Globals* simParams = oldInfo->getSimParams(); | 
| 158 | 
tim | 
12 | 
 | 
| 159 | 
gezelter | 
1067 | 
  // Calculate lattice constant (in Angstroms) | 
| 160 | 
tim | 
12 | 
 | 
| 161 | 
gezelter | 
1067 | 
  RealType avgMass = getMolMass(oldInfo->getMoleculeStamp(0), | 
| 162 | 
  | 
  | 
                                  oldInfo->getForceField()); | 
| 163 | 
tim | 
12 | 
 | 
| 164 | 
gezelter | 
1067 | 
  latticeConstant = pow(rhoConvertConst * nMolPerCell * avgMass / density, | 
| 165 | 
  | 
  | 
                        (RealType)(1.0 / 3.0)); | 
| 166 | 
  | 
  | 
   | 
| 167 | 
  | 
  | 
  // Set the lattice constant | 
| 168 | 
  | 
  | 
   | 
| 169 | 
  | 
  | 
  lc.push_back(latticeConstant); | 
| 170 | 
  | 
  | 
  simpleLat->setLatticeConstant(lc); | 
| 171 | 
tim | 
12 | 
 | 
| 172 | 
gezelter | 
1067 | 
  // Calculate the lattice sites and fill the lattice vector. | 
| 173 | 
tim | 
12 | 
 | 
| 174 | 
gezelter | 
1067 | 
  // Get the standard orientations of the cell sites | 
| 175 | 
tim | 
12 | 
 | 
| 176 | 
gezelter | 
1067 | 
  latticeOrt = simpleLat->getLatticePointsOrt(); | 
| 177 | 
tim | 
12 | 
 | 
| 178 | 
gezelter | 
1067 | 
  vector<Vector3d> sites; | 
| 179 | 
  | 
  | 
  vector<Vector3d> orientations; | 
| 180 | 
  | 
  | 
   | 
| 181 | 
  | 
  | 
  for(int i = 0; i < nx; i++) { | 
| 182 | 
  | 
  | 
    for(int j = 0; j < ny; j++) { | 
| 183 | 
  | 
  | 
      for(int k = 0; k < nz; k++) { | 
| 184 | 
tim | 
12 | 
 | 
| 185 | 
gezelter | 
1067 | 
        // Get the position of the cell sites | 
| 186 | 
  | 
  | 
         | 
| 187 | 
  | 
  | 
        simpleLat->getLatticePointsPos(latticePos, i, j, k); | 
| 188 | 
  | 
  | 
         | 
| 189 | 
  | 
  | 
        for(int l = 0; l < nMolPerCell; l++) { | 
| 190 | 
  | 
  | 
          sites.push_back(latticePos[l]); | 
| 191 | 
  | 
  | 
          orientations.push_back(latticeOrt[l]); | 
| 192 | 
  | 
  | 
        } | 
| 193 | 
  | 
  | 
      } | 
| 194 | 
  | 
  | 
    } | 
| 195 | 
  | 
  | 
  } | 
| 196 | 
  | 
  | 
   | 
| 197 | 
  | 
  | 
  outputFileName = args_info.output_arg; | 
| 198 | 
  | 
  | 
   | 
| 199 | 
  | 
  | 
  // create a new .md file on the fly which corrects the number of molecules | 
| 200 | 
tim | 
12 | 
 | 
| 201 | 
gezelter | 
1067 | 
  createMdFile(inputFileName, outputFileName, nSites); | 
| 202 | 
tim | 
12 | 
 | 
| 203 | 
gezelter | 
1067 | 
  if (oldInfo != NULL) | 
| 204 | 
  | 
  | 
    delete oldInfo; | 
| 205 | 
gezelter | 
246 | 
 | 
| 206 | 
gezelter | 
1067 | 
  // We need to read in the new SimInfo object, then Parse the  | 
| 207 | 
  | 
  | 
  // md file and set up the system | 
| 208 | 
tim | 
12 | 
 | 
| 209 | 
gezelter | 
1067 | 
  SimCreator newCreator; | 
| 210 | 
  | 
  | 
  SimInfo* newInfo = newCreator.createSim(outputFileName, false); | 
| 211 | 
tim | 
12 | 
 | 
| 212 | 
gezelter | 
1067 | 
  // fill Hmat | 
| 213 | 
  | 
  | 
 | 
| 214 | 
  | 
  | 
  hmat(0, 0) = nx * latticeConstant; | 
| 215 | 
gezelter | 
507 | 
  hmat(0, 1) = 0.0; | 
| 216 | 
  | 
  | 
  hmat(0, 2) = 0.0; | 
| 217 | 
tim | 
12 | 
 | 
| 218 | 
gezelter | 
507 | 
  hmat(1, 0) = 0.0; | 
| 219 | 
  | 
  | 
  hmat(1, 1) = ny * latticeConstant; | 
| 220 | 
  | 
  | 
  hmat(1, 2) = 0.0; | 
| 221 | 
tim | 
12 | 
 | 
| 222 | 
gezelter | 
507 | 
  hmat(2, 0) = 0.0; | 
| 223 | 
  | 
  | 
  hmat(2, 1) = 0.0; | 
| 224 | 
  | 
  | 
  hmat(2, 2) = nz * latticeConstant; | 
| 225 | 
tim | 
12 | 
 | 
| 226 | 
gezelter | 
1067 | 
  // Set Hmat | 
| 227 | 
tim | 
12 | 
 | 
| 228 | 
gezelter | 
1067 | 
  newInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat); | 
| 229 | 
gezelter | 
246 | 
 | 
| 230 | 
gezelter | 
1067 | 
  // place the molecules | 
| 231 | 
  | 
  | 
   | 
| 232 | 
gezelter | 
507 | 
  Molecule* mol; | 
| 233 | 
gezelter | 
1067 | 
  locator = new MoLocator(newInfo->getMoleculeStamp(0),  | 
| 234 | 
  | 
  | 
                          newInfo->getForceField()); | 
| 235 | 
  | 
  | 
  for (int n = 0; n < nSites; n++) { | 
| 236 | 
  | 
  | 
    mol = newInfo->getMoleculeByGlobalIndex(n); | 
| 237 | 
  | 
  | 
    locator->placeMol(sites[n], orientations[n], mol); | 
| 238 | 
gezelter | 
507 | 
  } | 
| 239 | 
gezelter | 
1067 | 
    | 
| 240 | 
  | 
  | 
  // Create DumpWriter and write out the coordinates | 
| 241 | 
tim | 
12 | 
 | 
| 242 | 
gezelter | 
1067 | 
  writer = new DumpWriter(newInfo, outputFileName); | 
| 243 | 
  | 
  | 
   | 
| 244 | 
gezelter | 
507 | 
  if (writer == NULL) { | 
| 245 | 
gezelter | 
1067 | 
    sprintf(painCave.errMsg, "error in creating DumpWriter"); | 
| 246 | 
  | 
  | 
    painCave.isFatal = 1; | 
| 247 | 
  | 
  | 
    simError(); | 
| 248 | 
gezelter | 
507 | 
  } | 
| 249 | 
tim | 
12 | 
 | 
| 250 | 
gezelter | 
507 | 
  writer->writeDump(); | 
| 251 | 
gezelter | 
246 | 
 | 
| 252 | 
gezelter | 
1067 | 
  // deleting the writer will put the closing at the end of the dump file. | 
| 253 | 
gezelter | 
246 | 
 | 
| 254 | 
gezelter | 
1067 | 
  delete writer; | 
| 255 | 
gezelter | 
246 | 
 | 
| 256 | 
gezelter | 
1390 | 
  sprintf(painCave.errMsg, "A new OpenMD file called \"%s\" has been " | 
| 257 | 
gezelter | 
1067 | 
          "generated.\n", outputFileName.c_str()); | 
| 258 | 
  | 
  | 
  painCave.isFatal = 0; | 
| 259 | 
  | 
  | 
  simError(); | 
| 260 | 
gezelter | 
507 | 
  return 0; | 
| 261 | 
tim | 
12 | 
} | 
| 262 | 
  | 
  | 
 | 
| 263 | 
gezelter | 
1067 | 
void createMdFile(const std::string&oldMdFileName,  | 
| 264 | 
  | 
  | 
                  const std::string&newMdFileName, | 
| 265 | 
  | 
  | 
                  int nMol) { | 
| 266 | 
gezelter | 
507 | 
  ifstream oldMdFile; | 
| 267 | 
  | 
  | 
  ofstream newMdFile; | 
| 268 | 
  | 
  | 
  const int MAXLEN = 65535; | 
| 269 | 
  | 
  | 
  char buffer[MAXLEN]; | 
| 270 | 
tim | 
12 | 
 | 
| 271 | 
gezelter | 
507 | 
  //create new .md file based on old .md file | 
| 272 | 
  | 
  | 
  oldMdFile.open(oldMdFileName.c_str()); | 
| 273 | 
  | 
  | 
  newMdFile.open(newMdFileName.c_str()); | 
| 274 | 
tim | 
12 | 
 | 
| 275 | 
gezelter | 
507 | 
  oldMdFile.getline(buffer, MAXLEN); | 
| 276 | 
gezelter | 
246 | 
 | 
| 277 | 
gezelter | 
507 | 
  while (!oldMdFile.eof()) { | 
| 278 | 
gezelter | 
246 | 
 | 
| 279 | 
gezelter | 
507 | 
    //correct molecule number | 
| 280 | 
  | 
  | 
    if (strstr(buffer, "nMol") != NULL) { | 
| 281 | 
gezelter | 
1067 | 
      sprintf(buffer, "\t\tnMol = %d;", nMol); | 
| 282 | 
gezelter | 
507 | 
      newMdFile << buffer << std::endl; | 
| 283 | 
  | 
  | 
    } else | 
| 284 | 
  | 
  | 
      newMdFile << buffer << std::endl; | 
| 285 | 
gezelter | 
246 | 
 | 
| 286 | 
gezelter | 
507 | 
    oldMdFile.getline(buffer, MAXLEN); | 
| 287 | 
  | 
  | 
  } | 
| 288 | 
gezelter | 
246 | 
 | 
| 289 | 
gezelter | 
507 | 
  oldMdFile.close(); | 
| 290 | 
  | 
  | 
  newMdFile.close(); | 
| 291 | 
tim | 
12 | 
} | 
| 292 | 
gezelter | 
246 | 
 |