ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-1.0/utils/sysbuilder/simpleBuilder.cpp
Revision: 1432
Committed: Thu Jul 29 03:31:50 2004 UTC (19 years, 11 months ago) by tim
File size: 7496 byte(s)
Log Message:
simpleBuilder is built but Makefile is broken

File Contents

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