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

Comparing trunk/OOPSE-4/src/applications/simpleBuilder/simpleBuilder.cpp (file contents):
Revision 1501 by tim, Tue Sep 28 23:24:25 2004 UTC vs.
Revision 2184 by tim, Tue Apr 12 22:33:50 2005 UTC

# Line 1 | Line 1
1 + /*
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>
# Line 7 | Line 48
48   #include <map>
49   #include <fstream>
50  
10 #include "io/Globals.hpp"
11 #include "brains/SimInfo.hpp"
12 #include "brains/SimSetup.hpp"
51   #include "applications/simpleBuilder/simpleBuilderCmd.h"
52 + #include "lattice/LatticeFactory.hpp"
53 + #include "utils/MoLocator.hpp"
54 + #include "lattice/Lattice.hpp"
55 + #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"
15 #include "applications/simpleBuilder/LatticeFactory.hpp"
16 #include "applications/simpleBuilder/Vector3d.hpp"
17 #include "applications/simpleBuilder/MoLocator.hpp"
18 #include "applications/simpleBuilder/Lattice.hpp"
62  
63   using namespace std;
64 + using namespace oopse;
65 + void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
66 +                  int numMol);
67  
68 < void createMdFile(const string& oldMdFileName, const string& newMdFileName, int numMol);
23 < double getMolMass(MoleculeStamp* molStamp, ForceFields* myFF);
68 > int main(int argc, char *argv []) {
69  
70 < int main( int argc, char* argv[]){
70 >    //register force fields
71 >    registerForceFields();
72 >    registerLattice();
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 >    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;
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  
98 <  gengetopt_args_info args_info;
99 <  string latticeType;
100 <  string inputFileName;
30 <  string outPrefix;
31 <  string outMdFileName;
32 <  string outInitFileName;
33 <  SimInfo* oldInfo;
34 <  SimSetup* oldSimSetup;
35 <  BaseLattice* simpleLat;
36 <  int numMol;
37 <  double latticeConstant;
38 <  vector<double> lc;
39 <  double mass;
40 <  const double rhoConvertConst = 1.661;
41 <  double density;
42 <  int nx, ny, nz;
43 <  double Hmat[3][3];
44 <  MoLocator *locator;
45 <  vector<Vector3d> latticePos;
46 <  vector<Vector3d> latticeOrt;
47 <  int numMolPerCell;
48 <  int curMolIndex;
49 <  DumpWriter* writer;
50 <  
51 <  // parse command line arguments
52 <  if (cmdline_parser (argc, argv, &args_info) != 0)
53 <    exit(1) ;
54 <  
55 <  density = args_info.density_arg;
98 >    // parse command line arguments
99 >    if (cmdline_parser(argc, argv, &args_info) != 0)
100 >        exit(1);
101  
102 <  //get lattice type
58 <  latticeType = UpperCase(args_info.latticetype_arg);
59 <  if(!LatticeFactory::getInstance()->hasLatticeCreator(latticeType)){
60 <    cerr << latticeType << " is an invalid lattice type" << endl;
61 <    cerr << LatticeFactory::getInstance()->toString() << endl;
62 <    exit(1);
63 <  }
102 >    density = args_info.density_arg;
103  
104 <  //get the number of unit cell
105 <  nx = args_info.nx_arg;
67 <  if(nx <= 0){
68 <    cerr << "The number of unit cell in h direction must be greater than 0" << endl;
69 <    exit(1);
70 <  }
104 >    //get lattice type
105 >    latticeType = UpperCase(args_info.latticetype_arg);
106  
107 <  ny = args_info.ny_arg;
108 <  if(ny <= 0){
109 <    cerr << "The number of unit cell in l direction must be greater than 0" << endl;
110 <    exit(1);
111 <  }
107 >    simpleLat = LatticeFactory::getInstance()->createLattice(latticeType);
108 >    
109 >    if (simpleLat == NULL) {
110 >      sprintf(painCave.errMsg, "Lattice Factory can not create %s lattice\n",
111 >              latticeType.c_str());
112 >      painCave.isFatal = 1;
113 >      simError();
114 >    }
115  
116 <  nz = args_info.nz_arg;
117 <  if(nz <= 0){
80 <    cerr << "The number of unit cell in k direction must be greater than 0" << endl;
81 <    exit(1);
82 <  }
83 <        
84 <  //get input file name
85 <  if (args_info.inputs_num)
86 <    inputFileName = args_info.inputs[0];
87 <  else {                
88 <    cerr <<"You must specify a input file name.\n" << endl;
89 <    cmdline_parser_print_help();
90 <    exit(1);
91 <  }
116 >    //get the number of unit cell
117 >    nx = args_info.nx_arg;
118  
119 <  
120 <  //parse md file and set up the system
121 <  oldInfo = new SimInfo;
122 <  if(oldInfo == NULL){
97 <     cerr << "error in creating SimInfo" << endl;
98 <     exit(1);
99 <  }
119 >    if (nx <= 0) {
120 >        std::cerr << "The number of unit cell in h direction must be greater than 0" << std::endl;
121 >        exit(1);
122 >    }
123  
124 <  oldSimSetup = new SimSetup();  
102 <  if(oldSimSetup == NULL){
103 <     cerr << "error in creating SimSetup" << endl;
104 <     exit(1);
105 <  }
124 >    ny = args_info.ny_arg;
125  
126 <  oldSimSetup->suspendInit();
127 <  oldSimSetup->setSimInfo(oldInfo );
128 <  oldSimSetup->parseFile(&inputFileName[0] );
129 <  oldSimSetup->createSim();
111 <  
112 <  if(oldInfo->nComponents >=2){
113 <      cerr << "can not build the system with more than two components" << endl;
114 <      exit(1);
115 <  }
116 <  
117 <  //get mass of molecule.
118 <  //Due to the design of OOPSE, given atom type, we have to query forcefiled to get the mass
119 <  mass = getMolMass(oldInfo->compStamps[0], oldSimSetup->getForceField());
120 <  
121 <  //creat lattice
122 <        simpleLat = LatticeFactory::getInstance()->createLattice(latticeType);
123 <        if(simpleLat == NULL){
124 <                cerr << "Error in creating lattice" << endl;
125 <                exit(1);
126 <        }
126 >    if (ny <= 0) {
127 >        std::cerr << "The number of unit cell in l direction must be greater than 0" << std::endl;
128 >        exit(1);
129 >    }
130  
131 <  numMolPerCell = simpleLat->getNumSitesPerCell();
129 <  
130 <  //calculate lattice constant (in Angstrom)
131 <  latticeConstant = pow(rhoConvertConst * numMolPerCell * mass /density, 1.0/3.0);
132 <  
133 <  //set lattice constant
134 <  lc.push_back(latticeConstant);
135 <  simpleLat->setLatticeConstant(lc);
136 <  
137 <  //calculate the total number of molecules
138 <  numMol = nx * ny * nz * numMolPerCell;
131 >    nz = args_info.nz_arg;
132  
133 <  if (oldInfo->n_mol != numMol){
133 >    if (nz <= 0) {
134 >        std::cerr << "The number of unit cell in k direction must be greater than 0" << std::endl;
135 >        exit(1);
136 >    }
137  
138 <    outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType;
139 <    outMdFileName = outPrefix + ".md";
138 >    //get input file name
139 >    if (args_info.inputs_num)
140 >        inputFileName = args_info.inputs[0];
141 >    else {
142 >        std::cerr << "You must specify a input file name.\n" << std::endl;
143 >        cmdline_parser_print_help();
144 >        exit(1);
145 >    }
146  
147 <    //creat new .md file on fly which corrects the number of molecule    
148 <    createMdFile(inputFileName, outMdFileName, numMol);
149 <    cerr << "SimpleBuilder Error: the number of molecule and the density are not matched" <<endl;
148 <    cerr << "A new .md file: " << outMdFileName << " is generated, use it to rerun the simpleBuilder" << endl;
149 <    exit(1);
150 <  }
151 <  
152 <  //determine the output file names  
153 <  if (args_info.output_given)
154 <    outInitFileName = args_info.output_arg;
155 <  else
156 <    outInitFileName = getPrefix(inputFileName.c_str())  + ".in";
157 <  
158 <  
159 <  //allocat memory for storing pos, vel and etc
160 <  oldInfo->getConfiguration()->createArrays(oldInfo->n_atoms);
161 <  for (int i = 0; i < oldInfo->n_atoms; i++)
162 <    oldInfo->atoms[i]->setCoords();  
163 <
164 <  //creat Molocator
165 <  locator = new MoLocator(oldInfo->compStamps[0], oldSimSetup->getForceField());
147 >    //parse md file and set up the system
148 >    SimCreator oldCreator;
149 >    SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
150  
151 <  //fill Hmat
152 <  Hmat[0][0] = nx * latticeConstant;
153 <  Hmat[0][1] = 0.0;
154 <  Hmat[0][2] = 0.0;
151 >    if (oldInfo->getNMoleculeStamp()>= 2) {
152 >        std::cerr << "can not build the system with more than two components"
153 >            << std::endl;
154 >        exit(1);
155 >    }
156  
157 <  Hmat[1][0] = 0.0;
173 <  Hmat[1][1] = ny * latticeConstant;
174 <  Hmat[1][2] = 0.0;
157 >    //get mass of molecule.
158  
159 <  Hmat[2][0] = 0.0;
177 <  Hmat[2][1] = 0.0;
178 <  Hmat[2][2] = nz * latticeConstant ;
159 >    mass = getMolMass(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
160  
161 <  //set Hmat
162 <  oldInfo->setBoxM(Hmat);
182 <  
183 <  //place the molecules
161 >    //creat lattice
162 >    simpleLat = LatticeFactory::getInstance()->createLattice(latticeType);
163  
164 <  curMolIndex = 0;
164 >    if (simpleLat == NULL) {
165 >        std::cerr << "Error in creating lattice" << std::endl;
166 >        exit(1);
167 >    }
168  
169 <  //get the orientation of the cell sites
188 <  //for the same type of molecule in same lattice, it will not change
189 <  latticeOrt = simpleLat->getLatticePointsOrt();
169 >    numMolPerCell = simpleLat->getNumSitesPerCell();
170  
171 <  for(int i =0; i < nx; i++){
172 <    for(int j=0; j < ny; j++){
173 <       for(int k = 0; k < nz; k++){
171 >    //calculate lattice constant (in Angstrom)
172 >    latticeConstant = pow(rhoConvertConst * numMolPerCell * mass / density,
173 >                          1.0 / 3.0);
174  
175 <          //get the position of the cell sites
176 <          simpleLat->getLatticePointsPos(latticePos, i, j, k);
175 >    //set lattice constant
176 >    lc.push_back(latticeConstant);
177 >    simpleLat->setLatticeConstant(lc);
178  
179 <          for(int l = 0; l < numMolPerCell; l++)
180 <            locator->placeMol(latticePos[l], latticeOrt[l], &(oldInfo->molecules[curMolIndex++]));
181 <       }
179 >    //calculate the total number of molecules
180 >    numMol = nx * ny * nz * numMolPerCell;
181 >
182 >    if (oldInfo->getNGlobalMolecules() != numMol) {
183 >        outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType;
184 >        outMdFileName = outPrefix + ".md";
185 >
186 >        //creat new .md file on fly which corrects the number of molecule    
187 >        createMdFile(inputFileName, outMdFileName, numMol);
188 >        std::cerr
189 >            << "SimpleBuilder Error: the number of molecule and the density are not matched"
190 >            << std::endl;
191 >        std::cerr << "A new .md file: " << outMdFileName
192 >            << " is generated, use it to rerun the simpleBuilder" << std::endl;
193 >        exit(1);
194      }
202  }
195  
196 <  //create dumpwriter and write out the coordinates
197 <  oldInfo->finalName = outInitFileName;
198 <  writer = new DumpWriter( oldInfo );
199 <  if(writer == NULL){
200 <    cerr << "error in creating DumpWriter" << endl;
201 <    exit(1);    
202 <  }
203 <  writer->writeFinal(0);
212 <  cout << "new initial configuration file: " << outInitFileName <<" is generated." << endl;
213 <  //delete objects
196 >    //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());
204  
205 <  //delete oldInfo and oldSimSetup
206 <  if(oldInfo != NULL)
207 <     delete oldInfo;
208 <  
219 <  if(oldSimSetup != NULL)
220 <     delete oldSimSetup;
221 <  
222 <  if (writer != NULL)
223 <    delete writer;
224 <  return 0;
225 < }
205 >    //fill Hmat
206 >    hmat(0, 0)= nx * latticeConstant;
207 >    hmat(0, 1) = 0.0;
208 >    hmat(0, 2) = 0.0;
209  
210 < void createMdFile(const string& oldMdFileName, const string& newMdFileName, int numMol){
211 <  ifstream oldMdFile;
212 <  ofstream newMdFile;
230 <  const int MAXLEN = 65535;
231 <  char buffer[MAXLEN];
210 >    hmat(1, 0) = 0.0;
211 >    hmat(1, 1) = ny * latticeConstant;
212 >    hmat(1, 2) = 0.0;
213  
214 <  //create new .md file based on old .md file
215 <  oldMdFile.open(oldMdFileName.c_str());
216 <  newMdFile.open(newMdFileName.c_str());
214 >    hmat(2, 0) = 0.0;
215 >    hmat(2, 1) = 0.0;
216 >    hmat(2, 2) = nz * latticeConstant;
217  
218 <  oldMdFile.getline(buffer, MAXLEN);
219 <  while(!oldMdFile.eof()){
218 >    //set Hmat
219 >    oldInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
220  
221 <    //correct molecule number
222 <    if(strstr(buffer, "nMol") !=NULL){      
223 <      sprintf(buffer, "\t\tnMol = %d;", numMol);
224 <      newMdFile << buffer << endl;
221 >    //place the molecules
222 >
223 >    curMolIndex = 0;
224 >
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 >
229 >    Molecule* mol;
230 >    SimInfo::MoleculeIterator mi;
231 >    mol = oldInfo->beginMolecule(mi);
232 >    for(int i = 0; i < nx; i++) {
233 >        for(int j = 0; j < ny; j++) {
234 >            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 >        }
249      }
245    else
246      newMdFile << buffer << endl;
250  
251 <    oldMdFile.getline(buffer, MAXLEN);
252 <  }
251 >    //create dumpwriter and write out the coordinates
252 >    oldInfo->setFinalConfigFileName(outInitFileName);
253 >    writer = new DumpWriter(oldInfo);
254  
255 <  oldMdFile.close();
256 <  newMdFile.close();
255 >    if (writer == NULL) {
256 >        std::cerr << "error in creating DumpWriter" << std::endl;
257 >        exit(1);
258 >    }
259  
260 +    writer->writeDump();
261 +    std::cout << "new initial configuration file: " << outInitFileName
262 +        << " is generated." << std::endl;
263 +
264 +    //delete objects
265 +
266 +    //delete oldInfo and oldSimSetup
267 +    if (oldInfo != NULL)
268 +        delete oldInfo;
269 +
270 +    if (writer != NULL)
271 +        delete writer;
272 +
273 +    return 0;
274   }
275  
276 < double getMolMass(MoleculeStamp* molStamp, ForceFields* myFF){
277 <  int nAtoms;
278 <  AtomStamp* currAtomStamp;
279 <  double totMass;
280 <  
281 <  totMass = 0;
262 <  nAtoms = molStamp->getNAtoms();
276 > void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
277 >                  int numMol) {
278 >    ifstream oldMdFile;
279 >    ofstream newMdFile;
280 >    const int MAXLEN = 65535;
281 >    char buffer[MAXLEN];
282  
283 <  for(size_t i=0; i<nAtoms; i++){
284 <    currAtomStamp = molStamp->getAtom(i);
285 <    totMass += myFF->getAtomTypeMass(currAtomStamp->getType());
267 <  }
283 >    //create new .md file based on old .md file
284 >    oldMdFile.open(oldMdFileName.c_str());
285 >    newMdFile.open(newMdFileName.c_str());
286  
287 <  return totMass;
287 >    oldMdFile.getline(buffer, MAXLEN);
288 >
289 >    while (!oldMdFile.eof()) {
290 >
291 >        //correct molecule number
292 >        if (strstr(buffer, "nMol") != NULL) {
293 >            sprintf(buffer, "\t\tnMol = %d;", numMol);
294 >            newMdFile << buffer << std::endl;
295 >        } else
296 >            newMdFile << buffer << std::endl;
297 >
298 >        oldMdFile.getline(buffer, MAXLEN);
299 >    }
300 >
301 >    oldMdFile.close();
302 >    newMdFile.close();
303   }
304 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines