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 2574 by chuckv, Thu Jan 19 22:54:23 2006 UTC vs.
Revision 3046 by gezelter, Sat Oct 14 20:21:26 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 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 <  int numMol;
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 <  
95 <  
92 >  double particleRadius;
93  
94    Mat3x3d hmat;
98  MoLocator *locator;
99  sphericalNanoparticle *nanoparticle;
95    std::vector<Vector3d> latticePos;
96    std::vector<Vector3d> latticeOrt;
97    int numMolPerCell;
98    int nShells; /* Number of shells in nanoparticle*/
99 <  int numSites;
105 <  
99 >
100    DumpWriter *writer;
101    
102    // Parse Command Line Arguments
103    if (cmdline_parser(argc, argv, &args_info) != 0)
104      exit(1);
105 <  
112 <        
113 <        
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    
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  
124    latticeConstant = args_info.latticeCnst_arg;
125    particleRadius = args_info.radius_arg;
126 <  particleDiameter = 2.0 * particleRadius;
126 >  Globals* simParams = oldInfo->getSimParams();
127    
128 <  /* set lattice constant */
129 <  lc.push_back(latticeConstant);
130 <  simpleLat->setLatticeConstant(lc);
128 >  /* Create nanoparticle */
129 >  shapedLatticeSpherical nanoParticle(latticeConstant, latticeType,
130 >                                      particleRadius);
131    
132 <  
133 <  /*determine the output file names*/
134 <  if (args_info.output_given){
135 <    outInitFileName = args_info.output_arg;
136 <  }else{
137 <    outInitFileName = getPrefix(inputFileName.c_str()) + ".in";
132 >  /* Build a lattice and get lattice points for this lattice constant */
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 >  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 > RealType totalFraction = 0.0;
170 >
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 >    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 >    totalFraction += molFractions.at(i);
187    }
188 <  
189 <        
190 <  
191 <  
192 <  
181 <  /* create Molocators */
182 <  locator = new MoLocator(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
183 <  
184 <  /* create a new spherical nanoparticle */
185 <  nanoparticle = new sphericalNanoparticle(particleRadius,latticeConstant);
186 <  /* Build a nanoparticle to see how many sites are there */
187 <  numSites = new int[nComponents]
188 <  int numSites = nanoparticle.getNMol(simpleLat);
189 <  
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 <    std::cout << "Creating a random nanoparticle" << std::endl;
228 <    /* Check to see if we have enough components */
195 <    if (nComponents != args_info.molFraction_given + 1){
196 <      std::cerr << "Number of components does not equal molFraction occurances." << std::endl;
197 <      exit 1;
198 <    }
199 <    int totComponents = 0;
200 <    for (int i = 0;i<nComponents-2;i++){ /* Figure out Percent for each component */
201 <      numMol[i] = int((double)numSites * args_info.molFraction_arg[i]);
202 <      totComponents += numMol[i];
203 <    }
204 <    numMol[nComponents-1] = numSites - totComponents;
205 <
227 >    /* do the iPod thing, Shuffle da vector */
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;
234 <    }
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      
213    
214    
238    }
239  
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();
240    
241    
242    
243 <  // needed for writing out new md file.
243 >  outputFileName = args_info.output_arg;
244 >
245    
246 <    outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType;
247 <    outMdFileName = outPrefix + ".md";
246 >   //creat new .md file on fly which corrects the number of molecule    
247 >  createMdFile(inputFileName, outputFileName, nMol);
248    
228    //creat new .md file on fly which corrects the number of molecule    
229    createMdFile(inputFileName, outMdFileName, numcomponents,numMol);
230  
249    if (oldInfo != NULL)
250      delete oldInfo;
251    
# Line 235 | Line 253 | int main(int argc, char *argv []) {
253    // We need to read in new siminfo object.    
254    //parse md file and set up the system
255    //SimCreator NewCreator;
256 +  SimCreator newCreator;
257 +  SimInfo* NewInfo = newCreator.createSim(outputFileName, false);
258    
239  SimInfo* NewInfo = oldCreator.createSim(outMdFileName, false);
259    
260 <  // This was so much fun the first time, lets do it again.
242 <  
260 >  // Place molecules
261    Molecule* mol;
262    SimInfo::MoleculeIterator mi;
263    mol = NewInfo->beginMolecule(mi);
264 +  int l = 0;
265  
266 <
267 <  for(int i = -nx; i < nx; i++) {
268 <     for(int j = -ny; j < ny; j++) {
269 <        for(int k = -nz; k < nz; k++) {
270 <          
271 <           //get the position of the cell sites
272 <           simpleLat->getLatticePointsPos(latticePos, i, j, k);
273 <          
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 <     }
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    
276  
277    
278    //fill Hmat
279 <  hmat(0, 0)= nx * 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) = ny * 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) = nz * 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);
294 <  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 <  
312 <  if (writer != NULL)
313 <    delete writer;
314 <  delete simpleLat;    
315 <  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 328 | Line 327 | void createMdFile(const std::string&oldMdFileName, con
327    newMdFile.open(newMdFileName.c_str());
328    
329    oldMdFile.getline(buffer, MAXLEN);
330 <  
330 >
331 >  int i = 0;
332    while (!oldMdFile.eof()) {
333      
334      //correct molecule number
335      if (strstr(buffer, "nMol") != NULL) {
336 <      sprintf(buffer, "\tnMol = %i;", numMol);                          
337 <      newMdFile << buffer << std::endl;
336 >      if(i<nMol.size()){
337 >        sprintf(buffer, "\tnMol = %i;", nMol.at(i));                            
338 >        newMdFile << buffer << std::endl;
339 >        i++;
340 >      }
341      } else
342        newMdFile << buffer << std::endl;
343      

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines