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 2352 by chuckv, Tue Oct 11 21:57:22 2005 UTC vs.
Revision 3044 by chuckv, Fri Oct 13 20:16:59 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines