| 1 | 
/* Copyright (c) 2006 The University of Notre Dame. All Rights Reserved. | 
| 2 | 
 * | 
| 3 | 
 * The University of Notre Dame grants you ("Licensee") a | 
| 4 | 
 * non-exclusive, royalty free, license to use, modify and | 
| 5 | 
 * redistribute this software in source and binary code form, provided | 
| 6 | 
 * that the following conditions are met: | 
| 7 | 
 * | 
| 8 | 
 * 1. Acknowledgement of the program authors must be made in any | 
| 9 | 
 *    publication of scientific results based in part on use of the | 
| 10 | 
 *    program.  An acceptable form of acknowledgement is citation of | 
| 11 | 
 *    the article in which the program was described (Matthew | 
| 12 | 
 *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher | 
| 13 | 
 *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented | 
| 14 | 
 *    Parallel Simulation Engine for Molecular Dynamics," | 
| 15 | 
 *    J. Comput. Chem. 26, pp. 252-271 (2005)) | 
| 16 | 
 * | 
| 17 | 
 * 2. Redistributions of source code must retain the above copyright | 
| 18 | 
 *    notice, this list of conditions and the following disclaimer. | 
| 19 | 
 * | 
| 20 | 
 * 3. Redistributions in binary form must reproduce the above copyright | 
| 21 | 
 *    notice, this list of conditions and the following disclaimer in the | 
| 22 | 
 *    documentation and/or other materials provided with the | 
| 23 | 
 *    distribution. | 
| 24 | 
 * | 
| 25 | 
 * This software is provided "AS IS," without a warranty of any | 
| 26 | 
 * kind. All express or implied conditions, representations and | 
| 27 | 
 * warranties, including any implied warranty of merchantability, | 
| 28 | 
 * fitness for a particular purpose or non-infringement, are hereby | 
| 29 | 
 * excluded.  The University of Notre Dame and its licensors shall not | 
| 30 | 
 * be liable for any damages suffered by licensee as a result of | 
| 31 | 
 * using, modifying or distributing the software or its | 
| 32 | 
 * derivatives. In no event will the University of Notre Dame or its | 
| 33 | 
 * licensors be liable for any lost revenue, profit or data, or for | 
| 34 | 
 * direct, indirect, special, consequential, incidental or punitive | 
| 35 | 
 * damages, however caused and regardless of the theory of liability, | 
| 36 | 
 * arising out of the use of or inability to use software, even if the | 
| 37 | 
 * University of Notre Dame has been advised of the possibility of | 
| 38 | 
 * such damages. | 
| 39 | 
 * | 
| 40 | 
 * | 
| 41 | 
 *  randomBuilder.cpp | 
| 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.1 2006-04-25 22:59:27 chuckv Exp $ | 
| 46 | 
 * | 
| 47 | 
 */ | 
| 48 | 
 | 
| 49 | 
 | 
| 50 | 
#include <cstdlib> | 
| 51 | 
#include <cstdio> | 
| 52 | 
#include <cstring> | 
| 53 | 
#include <cmath> | 
| 54 | 
#include <iostream> | 
| 55 | 
#include <string> | 
| 56 | 
#include <map> | 
| 57 | 
#include <fstream> | 
| 58 | 
 | 
| 59 | 
#include "applications/randomBuilder/randomBuilderCmd.h" | 
| 60 | 
#include "lattice/LatticeFactory.hpp" | 
| 61 | 
#include "utils/MoLocator.hpp" | 
| 62 | 
#include "lattice/Lattice.hpp" | 
| 63 | 
#include "brains/Register.hpp" | 
| 64 | 
#include "brains/SimInfo.hpp" | 
| 65 | 
#include "brains/SimCreator.hpp" | 
| 66 | 
#include "io/DumpWriter.hpp" | 
| 67 | 
#include "math/Vector3.hpp" | 
| 68 | 
#include "math/SquareMatrix3.hpp" | 
| 69 | 
#include "utils/StringUtils.hpp" | 
| 70 | 
 | 
| 71 | 
using namespace std; | 
| 72 | 
using namespace oopse; | 
| 73 | 
 | 
| 74 | 
void createMdFile(const std::string&oldMdFileName,  | 
| 75 | 
                  const std::string&newMdFileName, | 
| 76 | 
                  int components,int* numMol); | 
| 77 | 
 | 
| 78 | 
int main(int argc, char *argv []) { | 
| 79 | 
 | 
| 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; | 
| 89 | 
  std::string outInitFileName; | 
| 90 | 
  Lattice *simpleLat; | 
| 91 | 
  int* numMol; | 
| 92 | 
  double latticeConstant; | 
| 93 | 
  std::vector<double> lc; | 
| 94 | 
  double mass; | 
| 95 | 
  const double rhoConvertConst = 1.661; | 
| 96 | 
  double density; | 
| 97 | 
  int nx, | 
| 98 | 
    ny, | 
| 99 | 
    nz; | 
| 100 | 
  Mat3x3d hmat; | 
| 101 | 
  MoLocator *locator; | 
| 102 | 
  std::vector<Vector3d> latticePos; | 
| 103 | 
  std::vector<Vector3d> latticeOrt; | 
| 104 | 
  int numMolPerCell; | 
| 105 | 
  int curMolIndex; | 
| 106 | 
  DumpWriter *writer; | 
| 107 | 
 | 
| 108 | 
  // parse command line arguments | 
| 109 | 
  if (cmdline_parser(argc, argv, &args_info) != 0) | 
| 110 | 
    exit(1); | 
| 111 | 
 | 
| 112 | 
  density = args_info.density_arg; | 
| 113 | 
 | 
| 114 | 
  //get lattice type | 
| 115 | 
  latticeType = UpperCase(args_info.latticetype_arg); | 
| 116 | 
 | 
| 117 | 
  simpleLat = LatticeFactory::getInstance()->createLattice(latticeType); | 
| 118 | 
     | 
| 119 | 
  if (simpleLat == NULL) { | 
| 120 | 
    sprintf(painCave.errMsg, "Lattice Factory can not create %s lattice\n", | 
| 121 | 
            latticeType.c_str()); | 
| 122 | 
    painCave.isFatal = 1; | 
| 123 | 
    simError(); | 
| 124 | 
  } | 
| 125 | 
 | 
| 126 | 
  //get the number of unit cell | 
| 127 | 
  nx = args_info.nx_arg; | 
| 128 | 
 | 
| 129 | 
  if (nx <= 0) { | 
| 130 | 
    std::cerr << "The number of unit cell in h direction must be greater than 0" << std::endl; | 
| 131 | 
    exit(1); | 
| 132 | 
  } | 
| 133 | 
 | 
| 134 | 
  ny = args_info.ny_arg; | 
| 135 | 
 | 
| 136 | 
  if (ny <= 0) { | 
| 137 | 
    std::cerr << "The number of unit cell in l direction must be greater than 0" << std::endl; | 
| 138 | 
    exit(1); | 
| 139 | 
  } | 
| 140 | 
 | 
| 141 | 
  nz = args_info.nz_arg; | 
| 142 | 
 | 
| 143 | 
  if (nz <= 0) { | 
| 144 | 
    std::cerr << "The number of unit cell in k direction must be greater than 0" << std::endl; | 
| 145 | 
    exit(1); | 
| 146 | 
  } | 
| 147 | 
 | 
| 148 | 
  //get input file name | 
| 149 | 
  if (args_info.inputs_num) | 
| 150 | 
    inputFileName = args_info.inputs[0]; | 
| 151 | 
  else { | 
| 152 | 
    std::cerr << "You must specify a input file name.\n" << std::endl; | 
| 153 | 
    cmdline_parser_print_help(); | 
| 154 | 
    exit(1); | 
| 155 | 
  } | 
| 156 | 
 | 
| 157 | 
  //parse md file and set up the system | 
| 158 | 
  SimCreator oldCreator; | 
| 159 | 
  SimInfo* oldInfo = oldCreator.createSim(inputFileName, false); | 
| 160 | 
  Globals* simParams = oldInfo->getSimParams(); | 
| 161 | 
 | 
| 162 | 
  int nComponents =simParams->getNComponents(); | 
| 163 | 
  if (oldInfo->getNMoleculeStamp()>= 2) { | 
| 164 | 
    std::cerr << "can not build the system with more than two components" | 
| 165 | 
              << std::endl; | 
| 166 | 
    exit(1); | 
| 167 | 
  } | 
| 168 | 
 | 
| 169 | 
  //get mass of molecule.  | 
| 170 | 
 | 
| 171 | 
  mass = getMolMass(oldInfo->getMoleculeStamp(0), oldInfo->getForceField()); | 
| 172 | 
 | 
| 173 | 
  //creat lattice | 
| 174 | 
  simpleLat = LatticeFactory::getInstance()->createLattice(latticeType); | 
| 175 | 
 | 
| 176 | 
  if (simpleLat == NULL) { | 
| 177 | 
    std::cerr << "Error in creating lattice" << std::endl; | 
| 178 | 
    exit(1); | 
| 179 | 
  } | 
| 180 | 
 | 
| 181 | 
  numMolPerCell = simpleLat->getNumSitesPerCell(); | 
| 182 | 
 | 
| 183 | 
  //calculate lattice constant (in Angstrom) | 
| 184 | 
  latticeConstant = pow(rhoConvertConst * numMolPerCell * mass / density, | 
| 185 | 
                        1.0 / 3.0); | 
| 186 | 
 | 
| 187 | 
  //set lattice constant | 
| 188 | 
  lc.push_back(latticeConstant); | 
| 189 | 
  simpleLat->setLatticeConstant(lc); | 
| 190 | 
 | 
| 191 | 
  //calculate the total number of molecules | 
| 192 | 
  int totMol = nx * ny * nz * numMolPerCell; | 
| 193 | 
 | 
| 194 | 
  // Calculate the lattice sites and fill lattice vector. | 
| 195 | 
  vector<Vector3d> latticeSites; | 
| 196 | 
   | 
| 197 | 
  for(int i = 0; i < nx; i++) { | 
| 198 | 
    for(int j = 0; j < ny; j++) { | 
| 199 | 
      for(int k = 0; k < nz; k++) { | 
| 200 | 
 | 
| 201 | 
        //get the position of the cell sites | 
| 202 | 
        simpleLat->getLatticePointsPos(latticePos, i, j, k); | 
| 203 | 
 | 
| 204 | 
        for(int l = 0; l < numMolPerCell; l++) { | 
| 205 | 
          latticeSites.push_back(latticePos[l]); | 
| 206 | 
        } | 
| 207 | 
      } | 
| 208 | 
    } | 
| 209 | 
  } | 
| 210 | 
 | 
| 211 | 
  int numSites = latticeSites.size(); | 
| 212 | 
 | 
| 213 | 
  numMol = new int[nComponents]; | 
| 214 | 
  if (nComponents != args_info.molFraction_given && nComponents != 1){ | 
| 215 | 
    std::cerr << "Number of components does not equal molFraction occurances." << std::endl; | 
| 216 | 
    exit(1); | 
| 217 | 
  } | 
| 218 | 
  int totComponents = 0; | 
| 219 | 
  for (int i = 0;i<nComponents-1;i++){ /* Figure out Percent for each component */ | 
| 220 | 
    numMol[i] = int((double)numSites * args_info.molFraction_arg[i]); | 
| 221 | 
    std::cout<<numMol[i]<<std::endl; | 
| 222 | 
    totComponents += numMol[i]; | 
| 223 | 
  } | 
| 224 | 
  numMol[nComponents-1] = numSites - totComponents; | 
| 225 | 
   | 
| 226 | 
 | 
| 227 | 
  outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType; | 
| 228 | 
  outMdFileName = outPrefix + ".md"; | 
| 229 | 
 | 
| 230 | 
  //creat new .md file on fly which corrects the number of molecule      | 
| 231 | 
  createMdFile(inputFileName, outMdFileName, nComponents,numMol); | 
| 232 | 
 | 
| 233 | 
 | 
| 234 | 
 | 
| 235 | 
  //determine the output file names   | 
| 236 | 
  if (args_info.output_given){ | 
| 237 | 
    outInitFileName = args_info.output_arg; | 
| 238 | 
  }else{ | 
| 239 | 
    outInitFileName = getPrefix(inputFileName.c_str()) + ".in"; | 
| 240 | 
  } | 
| 241 | 
 | 
| 242 | 
  //fill Hmat | 
| 243 | 
  hmat(0, 0)= nx * latticeConstant; | 
| 244 | 
  hmat(0, 1) = 0.0; | 
| 245 | 
  hmat(0, 2) = 0.0; | 
| 246 | 
 | 
| 247 | 
  hmat(1, 0) = 0.0; | 
| 248 | 
  hmat(1, 1) = ny * latticeConstant; | 
| 249 | 
  hmat(1, 2) = 0.0; | 
| 250 | 
 | 
| 251 | 
  hmat(2, 0) = 0.0; | 
| 252 | 
  hmat(2, 1) = 0.0; | 
| 253 | 
  hmat(2, 2) = nz * latticeConstant; | 
| 254 | 
 | 
| 255 | 
  //set Hmat | 
| 256 | 
  oldInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat); | 
| 257 | 
 | 
| 258 | 
  //place the molecules | 
| 259 | 
 | 
| 260 | 
  curMolIndex = 0; | 
| 261 | 
 | 
| 262 | 
  //get the orientation of the cell sites | 
| 263 | 
  //for the same type of molecule in same lattice, it will not change | 
| 264 | 
  latticeOrt = simpleLat->getLatticePointsOrt(); | 
| 265 | 
 | 
| 266 | 
  | 
| 267 | 
  /* Randomize position vector */ | 
| 268 | 
  std::random_shuffle(latticeSites.begin(), latticeSites.end()); | 
| 269 | 
 | 
| 270 | 
 | 
| 271 | 
  if (oldInfo != NULL) | 
| 272 | 
    delete oldInfo; | 
| 273 | 
   | 
| 274 | 
   | 
| 275 | 
  // We need to read in new siminfo object.      | 
| 276 | 
  //parse md file and set up the system | 
| 277 | 
  //SimCreator NewCreator; | 
| 278 | 
  SimCreator newCreator; | 
| 279 | 
  SimInfo* NewInfo = newCreator.createSim(outMdFileName, false); | 
| 280 | 
   | 
| 281 | 
  /* create Molocators */ | 
| 282 | 
  locator = new MoLocator(NewInfo->getMoleculeStamp(0), NewInfo->getForceField()); | 
| 283 | 
   | 
| 284 | 
   | 
| 285 | 
   | 
| 286 | 
  Molecule* mol; | 
| 287 | 
  SimInfo::MoleculeIterator mi; | 
| 288 | 
  mol = NewInfo->beginMolecule(mi); | 
| 289 | 
  int l = 0; | 
| 290 | 
  for (mol = NewInfo->beginMolecule(mi); mol != NULL; mol = NewInfo->nextMolecule(mi)) { | 
| 291 | 
    locator->placeMol(latticeSites[l], latticeOrt[l], mol); | 
| 292 | 
    l++; | 
| 293 | 
  } | 
| 294 | 
 | 
| 295 | 
 | 
| 296 | 
 | 
| 297 | 
  //create dumpwriter and write out the coordinates | 
| 298 | 
  oldInfo->setFinalConfigFileName(outInitFileName); | 
| 299 | 
  writer = new DumpWriter(oldInfo); | 
| 300 | 
 | 
| 301 | 
  if (writer == NULL) { | 
| 302 | 
    std::cerr << "error in creating DumpWriter" << std::endl; | 
| 303 | 
    exit(1); | 
| 304 | 
  } | 
| 305 | 
 | 
| 306 | 
  writer->writeDump(); | 
| 307 | 
  std::cout << "new initial configuration file: " << outInitFileName | 
| 308 | 
            << " is generated." << std::endl; | 
| 309 | 
 | 
| 310 | 
  //delete objects | 
| 311 | 
 | 
| 312 | 
  //delete oldInfo and oldSimSetup | 
| 313 | 
  if (oldInfo != NULL) | 
| 314 | 
    delete oldInfo; | 
| 315 | 
 | 
| 316 | 
  if (writer != NULL) | 
| 317 | 
    delete writer; | 
| 318 | 
     | 
| 319 | 
  delete simpleLat; | 
| 320 | 
 | 
| 321 | 
  return 0; | 
| 322 | 
} | 
| 323 | 
 | 
| 324 | 
void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName, | 
| 325 | 
                  int components,int* numMol) { | 
| 326 | 
  ifstream oldMdFile; | 
| 327 | 
  ofstream newMdFile; | 
| 328 | 
  const int MAXLEN = 65535; | 
| 329 | 
  char buffer[MAXLEN]; | 
| 330 | 
   | 
| 331 | 
  //create new .md file based on old .md file | 
| 332 | 
  oldMdFile.open(oldMdFileName.c_str()); | 
| 333 | 
  newMdFile.open(newMdFileName.c_str()); | 
| 334 | 
   | 
| 335 | 
  oldMdFile.getline(buffer, MAXLEN); | 
| 336 | 
  | 
| 337 | 
  int i = 0; | 
| 338 | 
  while (!oldMdFile.eof()) { | 
| 339 | 
     | 
| 340 | 
    //correct molecule number | 
| 341 | 
    if (strstr(buffer, "nMol") != NULL) { | 
| 342 | 
      if(i<components){ | 
| 343 | 
        sprintf(buffer, "\tnMol = %i;", numMol[i]);                              | 
| 344 | 
        newMdFile << buffer << std::endl; | 
| 345 | 
        i++; | 
| 346 | 
      } | 
| 347 | 
    } else | 
| 348 | 
      newMdFile << buffer << std::endl; | 
| 349 | 
     | 
| 350 | 
    oldMdFile.getline(buffer, MAXLEN); | 
| 351 | 
  } | 
| 352 | 
   | 
| 353 | 
  oldMdFile.close(); | 
| 354 | 
  newMdFile.close(); | 
| 355 | 
} | 
| 356 | 
 |