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

Comparing trunk/OOPSE-4/src/applications/nanoparticleBuilder/nanoparticleBuilder.cpp (file contents):
Revision 2737 by chuckv, Tue Apr 25 22:54:55 2006 UTC vs.
Revision 3046 by gezelter, Sat Oct 14 20:21:26 2006 UTC

# Line 67 | Line 67 | void createMdFile(const std::string&oldMdFileName,
67   using namespace oopse;
68   void createMdFile(const std::string&oldMdFileName,
69                    const std::string&newMdFileName,
70 <                  int components,int* numMol);
70 >                  std::vector<int> numMol);
71  
72   int main(int argc, char *argv []) {
73    
# Line 78 | Line 78 | int main(int argc, char *argv []) {
78    gengetopt_args_info args_info;
79    std::string latticeType;
80    std::string inputFileName;
81 <  std::string outPrefix;
82 <  std::string outMdFileName;
83 <  std::string outInitFileName;
81 >  std::string outputFileName;
82  
85  
86  
83    Lattice *simpleLat;
84    MoLocator* locator;
85    int* numMol;
86    int nComponents;
87    double latticeConstant;
88    std::vector<double> lc;
89 <  double mass;                                                                                      
89 >  double mass;                                                                          
90    const double rhoConvertConst = 1.661;
91    double density;
92    double particleRadius;
97  
98  
93  
94    Mat3x3d hmat;
95    std::vector<Vector3d> latticePos;
96    std::vector<Vector3d> latticeOrt;
97    int numMolPerCell;
98    int nShells; /* Number of shells in nanoparticle*/
99 <  int numSites;
106 <  
99 >
100    DumpWriter *writer;
101    
102    // Parse Command Line Arguments
103    if (cmdline_parser(argc, argv, &args_info) != 0)
104      exit(1);
105 <  
113 <        
114 <        
105 >        
106    /* get lattice type */
107 <  latticeType = UpperCase(args_info.latticetype_arg);
108 <    
107 >  latticeType = "FCC";
108 >
109    /* get input file name */
110    if (args_info.inputs_num)
111      inputFileName = args_info.inputs[0];
112    else {
113 <    std::cerr << "You must specify a input file name.\n" << std::endl;
113 >      sprintf(painCave.errMsg, "No input .md file name was specified"
114 >              "on the command line");
115 >    painCave.isFatal = 1;
116      cmdline_parser_print_help();
117 <    exit(1);
117 >    simError();
118    }
119    
120    /* parse md file and set up the system */
121    SimCreator oldCreator;
122    SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
123    
131  
132  /*calculate lattice constant (in Angstrom)
133    latticeConstant = pow(rhoConvertConst * numMolPerCell * mass / density,
134    1.0 / 3.0);*/
135  
124    latticeConstant = args_info.latticeCnst_arg;
125    particleRadius = args_info.radius_arg;
126    Globals* simParams = oldInfo->getSimParams();
127    
140  /* Find out how many different components in this simualtion */
141  nComponents =simParams->getNComponents();
142  
143  /*determine the output file names*/
144  if (args_info.output_given){
145    outInitFileName = args_info.output_arg;
146  }else{
147    outInitFileName = getPrefix(inputFileName.c_str()) + ".in";
148  }
149  
150  std::cout <<"Before build shaped lattice. "<<std::endl;
151  
152  /* create Molocators */
153  locator = new MoLocator(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
154  
128    /* Create nanoparticle */
129 <  shapedLatticeSpherical nanoParticle(latticeConstant,latticeType,particleRadius);
129 >  shapedLatticeSpherical nanoParticle(latticeConstant, latticeType,
130 >                                      particleRadius);
131    
158  std::cout <<"Before build getPoints. "<<std::endl;
132    /* Build a lattice and get lattice points for this lattice constant */
133 <  vector<Vector3d> nanoParticleSites = nanoParticle.getPoints();
134 <  
133 >  vector<Vector3d> sites = nanoParticle.getSites();
134 >  vector<Vector3d> orientations = nanoParticle.getOrientations();
135 >
136 >  std::cout <<"nSites: " << sites.size() << std::endl;
137 >
138    /* Get number of lattice sites */
139 <  numSites = nanoParticleSites.size();
139 >  int nSites = sites.size();
140 >
141 >  std::vector<Component*> components = simParams->getComponents();
142 >  std::vector<RealType> molFractions;
143 >  std::vector<RealType> molecularMasses;
144 >  std::vector<int> nMol;
145 >  nComponents = components.size();
146 >
147 > if (nComponents == 1) {
148 >    molFractions.push_back(1.0);    
149 >  } else {
150 >   if (args_info.molFraction_given == nComponents) {
151 >     for (int i = 0; i < nComponents; i++) {
152 >       molFractions.push_back(args_info.molFraction_arg[i]);
153 >     }
154 >    } else if (args_info.molFraction_given == nComponents-1) {
155 >     RealType remainingFraction = 1.0;
156 >     for (int i = 0; i < nComponents-1; i++) {
157 >       molFractions.push_back(args_info.molFraction_arg[i]);
158 >       remainingFraction -= molFractions[i];
159 >     }
160 >     molFractions.push_back(remainingFraction);
161 >   } else {    
162 >     sprintf(painCave.errMsg, "nanoparticleBuilder can't figure out molFractions "
163 >             "for all of the components in the <MetaData> block.");
164 >     painCave.isFatal = 1;
165 >     simError();
166 >   }
167 > }
168  
169 <  //std::cout <<"numSites are %d "<<numSites<<std::endl;
166 <  // std::cout <<"nComponents are %d "<<nComponents<<std::endl;
167 <  numMol = new int[nComponents];
169 > RealType totalFraction = 0.0;
170  
171 <  
172 <  /* Random particle is the default case*/
173 <  if (!args_info.ShellRadius_given){
174 <    std::cout << "Creating a random nanoparticle" << std::endl;
175 <    /* Check to see if we have enough components */
176 <    if (nComponents != args_info.molFraction_given && nComponents != 1){
177 <      std::cerr << "Number of components does not equal molFraction occurances." << std::endl;
178 <      exit(1);
171 > /* Do some simple sanity checking*/
172 >
173 >  for (int i = 0; i < nComponents; i++) {
174 >    if (molFractions.at(i) < 0.0) {
175 >      sprintf(painCave.errMsg, "One of the requested molFractions was"
176 >              " less than zero!");
177 >      painCave.isFatal = 1;
178 >      simError();
179      }
180 <    /* Build the mole fractions and number of molecules of each type */  
181 <    int totComponents = 0;
182 <    for (int i = 0;i<nComponents-1;i++){ /* Figure out Percent for each component */
183 <      numMol[i] = int((double)numSites * args_info.molFraction_arg[i]);
184 <      std::cout<<numMol[i]<<std::endl;
183 <      totComponents += numMol[i];
180 >    if (molFractions.at(i) > 1.0) {
181 >      sprintf(painCave.errMsg, "One of the requested molFractions was"
182 >              " greater than one!");
183 >      painCave.isFatal = 1;
184 >      simError();
185      }
186 <    numMol[nComponents-1] = numSites - totComponents;
187 <  
186 >    totalFraction += molFractions.at(i);
187 >  }
188 >  if (abs(totalFraction - 1.0) > 1e-6) {
189 >    sprintf(painCave.errMsg, "The sum of molFractions was not close enough to 1.0");
190 >    painCave.isFatal = 1;
191 >    simError();
192 >  }
193 >
194 >  int remaining = nSites;
195 >  for (int i=0; i < nComponents-1; i++) {    
196 >    nMol.push_back(int((RealType)nSites * molFractions.at(i)));
197 >    remaining -= nMol.at(i);
198 >  }
199 >  nMol.push_back(remaining);
200 >
201 >
202 >
203 > // recompute actual mol fractions and perform final sanity check:
204 >
205 >  int totalMolecules = 0;
206 >  RealType totalMass = 0.0;
207 >  for (int i=0; i < nComponents; i++) {
208 >    molFractions[i] = (RealType)(nMol.at(i))/(RealType)nSites;
209 >    totalMolecules += nMol.at(i);
210 >    molecularMasses.push_back(getMolMass(oldInfo->getMoleculeStamp(i),
211 >                                         oldInfo->getForceField()));
212 >    totalMass += (RealType)(nMol.at(i)) * molecularMasses.at(i);
213 >  }
214 >  RealType avgMass = totalMass / (RealType) totalMolecules;
215 >
216 >  if (totalMolecules != nSites) {
217 >    sprintf(painCave.errMsg, "Computed total number of molecules is not equal "
218 >            "to the number of lattice sites!");
219 >    painCave.isFatal = 1;
220 >    simError();
221 >  }
222 >    
223 >  vector<int> ids;
224 >  for (int i = 0; i < sites.size(); i++) ids.push_back(i);
225 >  /* Random particle is the default case*/
226 >  if (!args_info.ShellRadius_given){
227      /* do the iPod thing, Shuffle da vector */
228 <    std::random_shuffle(nanoParticleSites.begin(), nanoParticleSites.end());
228 >    std::random_shuffle(ids.begin(), ids.end());
229    } else{ /*Handle core-shell with multiple components.*/
230      std::cout << "Creating a core-shell nanoparticle." << std::endl;
231      if (nComponents != args_info.ShellRadius_given + 1){
232 <      std::cerr << "Number of components does not equal ShellRadius occurances." << std::endl;
233 <      exit(1);
232 >      sprintf(painCave.errMsg, "Number of .md components "
233 >              "does not match the number of shell radius specifications");
234 >      painCave.isFatal = 1;
235 >      simError();
236      }  
237      
238    }
239  
240    
199  //get the orientation of the cell sites
200  //for the same type of molecule in same lattice, it will not change
201  latticeOrt = nanoParticle.getPointsOrt();
202  std::cout<<"Orientational vector Size: "<< std::endl;
203  std::cout<<latticeOrt.size()<< std::endl;
241    
242    
243 +  outputFileName = args_info.output_arg;
244 +
245    
246 <  // needed for writing out new md file.
246 >   //creat new .md file on fly which corrects the number of molecule    
247 >  createMdFile(inputFileName, outputFileName, nMol);
248    
209  outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType;
210  outMdFileName = outPrefix + ".md";
211  
212  //creat new .md file on fly which corrects the number of molecule    
213  createMdFile(inputFileName, outMdFileName, nComponents,numMol);
214  
249    if (oldInfo != NULL)
250      delete oldInfo;
251    
# Line 220 | Line 254 | int main(int argc, char *argv []) {
254    //parse md file and set up the system
255    //SimCreator NewCreator;
256    SimCreator newCreator;
257 <  SimInfo* NewInfo = newCreator.createSim(outMdFileName, false);
257 >  SimInfo* NewInfo = newCreator.createSim(outputFileName, false);
258    
259    
260    // Place molecules
# Line 228 | Line 262 | int main(int argc, char *argv []) {
262    SimInfo::MoleculeIterator mi;
263    mol = NewInfo->beginMolecule(mi);
264    int l = 0;
265 <  for (mol = NewInfo->beginMolecule(mi); mol != NULL; mol = NewInfo->nextMolecule(mi)) {
266 <    locator->placeMol(nanoParticleSites[l], latticeOrt[l], mol);
267 <    l++;
265 >
266 >  for (int i = 0; i < nComponents; i++){
267 >    locator = new MoLocator(NewInfo->getMoleculeStamp(i),
268 >                            NewInfo->getForceField());
269 >    for (int n = 0; n < nMol.at(i); n++) {
270 >      mol = NewInfo->getMoleculeByGlobalIndex(l);
271 >      locator->placeMol(sites[ids[l]], orientations[ids[l]], mol);
272 >      l++;
273 >    }
274    }
275 <
275 >  
276  
237
277    
278    //fill Hmat
279 <  hmat(0, 0)=  latticeConstant;
279 >  hmat(0, 0)=  2.0*particleRadius;
280    hmat(0, 1) = 0.0;
281    hmat(0, 2) = 0.0;
282    
283    hmat(1, 0) = 0.0;
284 <  hmat(1, 1) =  latticeConstant;
284 >  hmat(1, 1) =  2.0*particleRadius;
285    hmat(1, 2) = 0.0;
286    
287    hmat(2, 0) = 0.0;
288    hmat(2, 1) = 0.0;
289 <  hmat(2, 2) =  latticeConstant;
289 >  hmat(2, 2) =  2.0*particleRadius;
290    
291    //set Hmat
292    NewInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
293    
294    
295    //create dumpwriter and write out the coordinates
296 <  NewInfo->setFinalConfigFileName(outInitFileName);
258 <  writer = new DumpWriter(NewInfo);
296 >  writer = new DumpWriter(NewInfo, outputFileName);
297    
298    if (writer == NULL) {
299 <    std::cerr << "error in creating DumpWriter" << std::endl;
300 <    exit(1);
299 >    sprintf(painCave.errMsg, "Error in creating dumpwrite object ");
300 >      painCave.isFatal = 1;
301 >      simError();
302    }
303    
304    writer->writeDump();
305 <  std::cout << "new initial configuration file: " << outInitFileName
306 <            << " is generated." << std::endl;
307 <  
308 <  //delete objects
309 <  
310 <  //delete oldInfo and oldSimSetup
311 <  
312 <  if (NewInfo != NULL)
313 <    delete NewInfo;
314 <  
276 <  if (writer != NULL)
277 <    delete writer;      
278 <  cmdline_parser_free(&args_info);
305 >
306 >  // deleting the writer will put the closing at the end of the dump file
307 >
308 >  delete writer;
309 >
310 >  // cleanup a by calling sim error.....
311 >  sprintf(painCave.errMsg, "A new OOPSE MD file called \"%s\" has been "
312 >          "generated.\n", outputFileName.c_str());
313 >  painCave.isFatal = 0;
314 >  simError();
315    return 0;
316   }
317  
318   void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
319 <                  int components,int* numMol) {
319 >                  std::vector<int> nMol) {
320    ifstream oldMdFile;
321    ofstream newMdFile;
322    const int MAXLEN = 65535;
# Line 297 | Line 333 | void createMdFile(const std::string&oldMdFileName, con
333      
334      //correct molecule number
335      if (strstr(buffer, "nMol") != NULL) {
336 <      if(i<components){
337 <        sprintf(buffer, "\tnMol = %i;", numMol[i]);                            
336 >      if(i<nMol.size()){
337 >        sprintf(buffer, "\tnMol = %i;", nMol.at(i));                            
338          newMdFile << buffer << std::endl;
339          i++;
340        }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines