ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/applications/simpleBuilder/simpleBuilder.cpp
(Generate patch)

Comparing trunk/src/applications/simpleBuilder/simpleBuilder.cpp (file contents):
Revision 507 by gezelter, Fri Apr 15 22:04:00 2005 UTC vs.
Revision 1390 by gezelter, Wed Nov 25 20:02:06 2009 UTC

# Line 6 | Line 6
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
9 > * 1. Redistributions of source code must retain the above copyright
10   *    notice, this list of conditions and the following disclaimer.
11   *
12 < * 3. Redistributions in binary form must reproduce the above copyright
12 > * 2. Redistributions in binary form must reproduce the above copyright
13   *    notice, this list of conditions and the following disclaimer in the
14   *    documentation and/or other materials provided with the
15   *    distribution.
# Line 37 | Line 28
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 + *
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   */
41  
42   #include <cstdlib>
# Line 61 | Line 61 | using namespace std;
61   #include "utils/StringUtils.hpp"
62  
63   using namespace std;
64 < using namespace oopse;
65 < void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
66 <                  int numMol);
64 > using namespace OpenMD;
65  
66 + void createMdFile(const std::string&oldMdFileName,
67 +                  const std::string&newMdFileName,
68 +                  int nMol);
69 +
70   int main(int argc, char *argv []) {
71  
72 <  //register force fields
72 >  // register force fields
73    registerForceFields();
74    registerLattice();
75      
76    gengetopt_args_info args_info;
77    std::string latticeType;
78    std::string inputFileName;
79 <  std::string outPrefix;
78 <  std::string outMdFileName;
79 <  std::string outInitFileName;
79 >  std::string outputFileName;
80    Lattice *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;
81 >  RealType latticeConstant;
82 >  std::vector<RealType> lc;
83 >  const RealType rhoConvertConst = 1.661;
84 >  RealType density;
85 >  int nx, ny, nz;
86    Mat3x3d hmat;
87    MoLocator *locator;
88    std::vector<Vector3d> latticePos;
89    std::vector<Vector3d> latticeOrt;
90 <  int numMolPerCell;
95 <  int curMolIndex;
90 >  int nMolPerCell;
91    DumpWriter *writer;
92  
93    // parse command line arguments
# Line 102 | Line 97 | int main(int argc, char *argv []) {
97    density = args_info.density_arg;
98  
99    //get lattice type
100 <  latticeType = UpperCase(args_info.latticetype_arg);
100 >  latticeType = "FCC";
101  
102    simpleLat = LatticeFactory::getInstance()->createLattice(latticeType);
103      
# Line 112 | Line 107 | int main(int argc, char *argv []) {
107      painCave.isFatal = 1;
108      simError();
109    }
110 +  nMolPerCell = simpleLat->getNumSitesPerCell();
111  
112 <  //get the number of unit cell
112 >  //get the number of unit cells in each direction:
113 >
114    nx = args_info.nx_arg;
115  
116    if (nx <= 0) {
117 <    std::cerr << "The number of unit cell in h direction must be greater than 0" << std::endl;
118 <    exit(1);
117 >    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    }
122  
123    ny = args_info.ny_arg;
124  
125    if (ny <= 0) {
126 <    std::cerr << "The number of unit cell in l direction must be greater than 0" << std::endl;
127 <    exit(1);
126 >    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    }
131  
132    nz = args_info.nz_arg;
133  
134    if (nz <= 0) {
135 <    std::cerr << "The number of unit cell in k direction must be greater than 0" << std::endl;
136 <    exit(1);
135 >    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    }
140  
141 +  int nSites = nMolPerCell * nx * ny * nz;
142 +
143    //get input file name
144    if (args_info.inputs_num)
145      inputFileName = args_info.inputs[0];
146    else {
147 <    std::cerr << "You must specify a input file name.\n" << std::endl;
148 <    cmdline_parser_print_help();
149 <    exit(1);
147 >    sprintf(painCave.errMsg, "No input .md file name was specified "
148 >            "on the command line");
149 >    painCave.isFatal = 1;
150 >    simError();
151    }
152  
153    //parse md file and set up the system
154 +
155    SimCreator oldCreator;
156    SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
157 +  Globals* simParams = oldInfo->getSimParams();
158  
159 <  if (oldInfo->getNMoleculeStamp()>= 2) {
152 <    std::cerr << "can not build the system with more than two components"
153 <              << std::endl;
154 <    exit(1);
155 <  }
159 >  // Calculate lattice constant (in Angstroms)
160  
161 <  //get mass of molecule.
161 >  RealType avgMass = getMolMass(oldInfo->getMoleculeStamp(0),
162 >                                  oldInfo->getForceField());
163  
164 <  mass = getMolMass(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
165 <
166 <  //creat lattice
167 <  simpleLat = LatticeFactory::getInstance()->createLattice(latticeType);
168 <
164 <  if (simpleLat == NULL) {
165 <    std::cerr << "Error in creating lattice" << std::endl;
166 <    exit(1);
167 <  }
168 <
169 <  numMolPerCell = simpleLat->getNumSitesPerCell();
170 <
171 <  //calculate lattice constant (in Angstrom)
172 <  latticeConstant = pow(rhoConvertConst * numMolPerCell * mass / density,
173 <                        1.0 / 3.0);
174 <
175 <  //set lattice constant
164 >  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  
172 <  //calculate the total number of molecules
180 <  numMol = nx * ny * nz * numMolPerCell;
172 >  // Calculate the lattice sites and fill the lattice vector.
173  
174 <  if (oldInfo->getNGlobalMolecules() != numMol) {
183 <    outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType;
184 <    outMdFileName = outPrefix + ".md";
174 >  // Get the standard orientations of the cell sites
175  
176 <    //creat new .md file on fly which corrects the number of molecule    
177 <    createMdFile(inputFileName, outMdFileName, numMol);
178 <    std::cerr
179 <      << "SimpleBuilder Error: the number of molecule and the density are not matched"
180 <      << std::endl;
181 <    std::cerr << "A new .md file: " << outMdFileName
182 <              << " is generated, use it to rerun the simpleBuilder" << std::endl;
183 <    exit(1);
176 >  latticeOrt = simpleLat->getLatticePointsOrt();
177 >
178 >  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 >
185 >        // 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  
201 <  //determine the output file names  
197 <  if (args_info.output_given)
198 <    outInitFileName = args_info.output_arg;
199 <  else
200 <    outInitFileName = getPrefix(inputFileName.c_str()) + ".in";
201 <    
202 <  //creat Molocator
203 <  locator = new MoLocator(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
201 >  createMdFile(inputFileName, outputFileName, nSites);
202  
203 <  //fill Hmat
204 <  hmat(0, 0)= nx * latticeConstant;
203 >  if (oldInfo != NULL)
204 >    delete oldInfo;
205 >
206 >  // We need to read in the new SimInfo object, then Parse the
207 >  // md file and set up the system
208 >
209 >  SimCreator newCreator;
210 >  SimInfo* newInfo = newCreator.createSim(outputFileName, false);
211 >
212 >  // fill Hmat
213 >
214 >  hmat(0, 0) = nx * latticeConstant;
215    hmat(0, 1) = 0.0;
216    hmat(0, 2) = 0.0;
217  
# Line 215 | Line 223 | int main(int argc, char *argv []) {
223    hmat(2, 1) = 0.0;
224    hmat(2, 2) = nz * latticeConstant;
225  
226 <  //set Hmat
219 <  oldInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
226 >  // Set Hmat
227  
228 <  //place the molecules
228 >  newInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
229  
230 <  curMolIndex = 0;
231 <
225 <  //get the orientation of the cell sites
226 <  //for the same type of molecule in same lattice, it will not change
227 <  latticeOrt = simpleLat->getLatticePointsOrt();
228 <
230 >  // place the molecules
231 >  
232    Molecule* mol;
233 <  SimInfo::MoleculeIterator mi;
234 <  mol = oldInfo->beginMolecule(mi);
235 <  for(int i = 0; i < nx; i++) {
236 <    for(int j = 0; j < ny; j++) {
237 <      for(int k = 0; k < nz; k++) {
235 <
236 <        //get the position of the cell sites
237 <        simpleLat->getLatticePointsPos(latticePos, i, j, k);
238 <
239 <        for(int l = 0; l < numMolPerCell; l++) {
240 <          if (mol != NULL) {
241 <            locator->placeMol(latticePos[l], latticeOrt[l], mol);
242 <          } else {
243 <            std::cerr << std::endl;                    
244 <          }
245 <          mol = oldInfo->nextMolecule(mi);
246 <        }
247 <      }
248 <    }
233 >  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    }
239 +  
240 +  // Create DumpWriter and write out the coordinates
241  
242 <  //create dumpwriter and write out the coordinates
243 <  oldInfo->setFinalConfigFileName(outInitFileName);
253 <  writer = new DumpWriter(oldInfo);
254 <
242 >  writer = new DumpWriter(newInfo, outputFileName);
243 >  
244    if (writer == NULL) {
245 <    std::cerr << "error in creating DumpWriter" << std::endl;
246 <    exit(1);
245 >    sprintf(painCave.errMsg, "error in creating DumpWriter");
246 >    painCave.isFatal = 1;
247 >    simError();
248    }
249  
250    writer->writeDump();
261  std::cout << "new initial configuration file: " << outInitFileName
262            << " is generated." << std::endl;
251  
252 <  //delete objects
252 >  // deleting the writer will put the closing at the end of the dump file.
253  
254 <  //delete oldInfo and oldSimSetup
267 <  if (oldInfo != NULL)
268 <    delete oldInfo;
254 >  delete writer;
255  
256 <  if (writer != NULL)
257 <    delete writer;
258 <    
259 <  delete simpleLat;
274 <
256 >  sprintf(painCave.errMsg, "A new OpenMD file called \"%s\" has been "
257 >          "generated.\n", outputFileName.c_str());
258 >  painCave.isFatal = 0;
259 >  simError();
260    return 0;
261   }
262  
263 < void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
264 <                  int numMol) {
263 > void createMdFile(const std::string&oldMdFileName,
264 >                  const std::string&newMdFileName,
265 >                  int nMol) {
266    ifstream oldMdFile;
267    ofstream newMdFile;
268    const int MAXLEN = 65535;
# Line 292 | Line 278 | void createMdFile(const std::string&oldMdFileName, con
278  
279      //correct molecule number
280      if (strstr(buffer, "nMol") != NULL) {
281 <      sprintf(buffer, "\t\tnMol = %d;", numMol);
281 >      sprintf(buffer, "\t\tnMol = %d;", nMol);
282        newMdFile << buffer << std::endl;
283      } else
284        newMdFile << buffer << std::endl;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines