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

Comparing trunk/OOPSE-3.0/src/applications/nanoRodBuilder/nanorodBuilder.cpp (file contents):
Revision 2185 by tim, Tue Apr 12 22:42:13 2005 UTC vs.
Revision 2253 by chuckv, Tue May 31 17:34:18 2005 UTC

# Line 1 | Line 1
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
# Line 38 | Line 38
38   * University of Notre Dame has been advised of the possibility of
39   * such damages.
40   */
41 <
41 >
42   #include <cstdlib>
43   #include <cstdio>
44   #include <cstring>
# Line 48 | Line 48
48   #include <map>
49   #include <fstream>
50  
51 + #include "config.h"
52 +
53   #include "nanorodBuilderCmd.h"
54 < //#include "GeometryBuilder.hpp"
54 > #ifdef HAVE_CGAL
55 > #include "GeometryBuilder.hpp"
56 > #endif
57   #include "lattice/LatticeFactory.hpp"
58   #include "utils/MoLocator.hpp"
59   #include "lattice/Lattice.hpp"
# Line 63 | Line 67 | void createMdFile(const std::string&oldMdFileName, con
67  
68   using namespace std;
69   using namespace oopse;
70 < void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
70 > void createMdFile(const std::string&oldMdFileName,
71 >                  const std::string&newMdFileName,
72                    int numMol);
73  
74   int main(int argc, char *argv []) {
75 <
76 <    //register force fields
77 <    registerForceFields();
78 <    registerLattice();
75 >  
76 >  //register force fields
77 >  registerForceFields();
78 >  registerLattice();
79 >  
80 >  gengetopt_args_info args_info;
81 >  std::string latticeType;
82 >  std::string inputFileName;
83 >  std::string outPrefix;
84 >  std::string outMdFileName;
85 >  std::string outInitFileName;
86 >  std::string outGeomFileName;
87 >  
88 >  
89 >  Lattice *simpleLat;
90 >  int numMol;
91 >  double latticeConstant;
92 >  std::vector<double> lc;
93 >  double mass;
94 >  const double rhoConvertConst = 1.661;
95 >  double density;
96 >  double rodLength;
97 >  double rodDiameter;
98 >  
99 >  
100 >  int nx, ny, nz;
101 >  Mat3x3d hmat;
102 >  MoLocator *locator;
103 >  std::vector<Vector3d> latticePos;
104 >  std::vector<Vector3d> latticeOrt;
105 >  int numMolPerCell;
106 >  DumpWriter *writer;
107 >  
108 >  // parse command line arguments
109 >  if (cmdline_parser(argc, argv, &args_info) != 0)
110 >    exit(1);
111 >  
112 >  
113 >  // Check for lib CGAL, if we don't have it, we should exit....
114 >        
115 > #ifndef HAVE_CGAL
116 >  std::cerr << "nanoRodBuilder requires libCGAL to function, please rebuild OOPSE with libCGAL."
117 >            << std::endl;
118 >  exit(1);
119 > #endif
120 >        
121 >        
122 >        
123 >  //get lattice type
124 >  latticeType = UpperCase(args_info.latticetype_arg);
125      
126 <    gengetopt_args_info args_info;
127 <    std::string latticeType;
128 <    std::string inputFileName;
129 <    std::string outPrefix;
130 <    std::string outMdFileName;
131 <    std::string outInitFileName;
132 <                std::string outGeomFileName;
133 <                
134 <                
135 <    Lattice *simpleLat;
136 <    int numMol;
137 <    double latticeConstant;
138 <    std::vector<double> lc;
139 <    double mass;
140 <    const double rhoConvertConst = 1.661;
141 <    double density;
142 <                double rodLength;
143 <                double rodDiameter;
144 <                
145 <                
146 <    int nx,
147 <    ny,
148 <    nz;
149 <    Mat3x3d hmat;
150 <    MoLocator *locator;
151 <    std::vector<Vector3d> latticePos;
152 <    std::vector<Vector3d> latticeOrt;
153 <    int numMolPerCell;
154 <    int curMolIndex;
155 <    DumpWriter *writer;
126 >  //get input file name
127 >  if (args_info.inputs_num)
128 >    inputFileName = args_info.inputs[0];
129 >  else {
130 >    std::cerr << "You must specify a input file name.\n" << std::endl;
131 >    cmdline_parser_print_help();
132 >    exit(1);
133 >  }
134 >  
135 >  //parse md file and set up the system
136 >  SimCreator oldCreator;
137 >  SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
138 >  
139 >  if (oldInfo->getNMoleculeStamp()> 1) {
140 >    std::cerr << "can not build nanorod with more than one components"
141 >              << std::endl;
142 >    exit(1);
143 >  }
144 >  
145 >  //get mass of molecule.
146 >  
147 >  mass = getMolMass(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
148 >  
149 >  //creat lattice
150 >  simpleLat = LatticeFactory::getInstance()->createLattice(latticeType);
151 >  
152 >  if (simpleLat == NULL) {
153 >    std::cerr << "Error in creating lattice" << std::endl;
154 >    exit(1);
155 >  }
156 >  
157 >  numMolPerCell = simpleLat->getNumSitesPerCell();
158 >  
159 >  //calculate lattice constant (in Angstrom)
160 >  //latticeConstant = pow(rhoConvertConst * numMolPerCell * mass / density,
161 >  //                      1.0 / 3.0);
162 >  
163 >  latticeConstant = args_info.latticeCnst_arg;
164 >  rodLength = args_info.length_arg;
165 >  rodDiameter = args_info.width_arg;
166 >  
167 >  //set lattice constant
168 >  lc.push_back(latticeConstant);
169 >  simpleLat->setLatticeConstant(lc);
170 >  
171 >  
172 >  //determine the output file names  
173 >  if (args_info.output_given)
174 >    outInitFileName = args_info.output_arg;
175 >  else
176 >    outInitFileName = getPrefix(inputFileName.c_str()) + ".in";
177 >  
178 >  
179 >        
180 >  
181 >  
182 >  
183 >  //creat Molocator
184 >  locator = new MoLocator(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
185 >  
186 >  /*
187 >    Assume we are carving nanorod out of a cublic block of material and that
188 >    the shape the material will fit within that block....
189 >    The model in geometry builder assumes the long axis is in the y direction and the x-z plane is the
190 >    diameter of the particle.            
191 >  */
192 >  // Number of Unit Cells in Length first
193 >  ny = (int)(rodLength/latticeConstant);
194 >  // Number of unit cells in Width
195 >  nx = (int)(rodDiameter/latticeConstant);
196 >  nz = (int)(rodDiameter/latticeConstant);
197 >  
198 >  
199 >  
200 >  // Create geometry for nanocrystal
201 > #ifdef HAVE_CGAL
202 >  GeometryBuilder *myGeometry;
203 >  // GeometryBuilder myGeometry(rodLength,rodDiameter);
204 >  if (args_info.twinnedCrystal_flag){
205 >     myGeometry = new GeometryBuilder(rodLength,rodDiameter);
206 >  }
207 >  else{
208 >     myGeometry = new GeometryBuilder(rodLength,rodDiameter);
209 >  }
210 >  
211 >  if (args_info.genGeomview_given){
212 >     if (args_info.genGeomview_flag){
213 >        outGeomFileName = getPrefix(inputFileName.c_str()) + ".off";
214 >        myGeometry->dumpGeometry(outGeomFileName);
215 >     }
216 >  }
217 >  
218 > #endif
219 >  
220 >  /*
221 >    We have to build the system first to figure out how many molecules
222 >    there are then create a md file and then actually build the
223 >    system.
224 >  */
225 >  
226 >  //place the molecules
227 >  
228  
229 <    // parse command line arguments
230 <    if (cmdline_parser(argc, argv, &args_info) != 0)
231 <        exit(1);
229 >  
230 >  //get the orientation of the cell sites
231 >  //for the same type of molecule in same lattice, it will not change
232 >  latticeOrt = simpleLat->getLatticePointsOrt();
233 >  
234 >  
235 >  
236 >  numMol = 0;
237 >  for(int i = -nx; i < nx; i++) {    
238 >    for(int j = -ny; j < ny; j++) {      
239 >      for(int k = -nz; k < nz; k++) {
240 >        //if (oldInfo->getNGlobalMolecules() != numMol) {
241 >        
242 >        
243 >        
244 >        //get the position of the cell sites
245 >        simpleLat->getLatticePointsPos(latticePos, i, j, k);
246 >        
247 >        for(int l = 0; l < numMolPerCell; l++) {
248  
249 <    
250 <
251 <    //get lattice type
252 <    latticeType = UpperCase(args_info.latticetype_arg);
253 <
254 <
255 <    simpleLat = LatticeFactory::getInstance()->createLattice(latticeType);
256 <    if (simpleLat == NULL) {
257 <        sprintf(painCave.errMsg, "Lattice Factory can not create %s lattice\n",
119 <                latticeType.c_str());
120 <        painCave.isFatal = 1;
121 <        simError();
122 <    }      
123 <
124 <    //get input file name
125 <    if (args_info.inputs_num)
126 <        inputFileName = args_info.inputs[0];
127 <    else {
128 <        std::cerr << "You must specify a input file name.\n" << std::endl;
129 <        cmdline_parser_print_help();
130 <        exit(1);
249 > #ifdef HAVE_CGAL
250 >          if (myGeometry->isInsidePolyhedron(latticePos[l][0],latticePos[l][1],latticePos[l][2])){
251 >            numMol++;
252 >          }
253 > #else
254 >           numMol++;
255 > #endif
256 >        }
257 >      }
258      }
259 +  }
260  
261 <    //parse md file and set up the system
262 <    SimCreator oldCreator;
263 <    SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
264 <                
265 <    if (oldInfo->getNMoleculeStamp()> 1) {
266 <        std::cerr << "can not build nanorod with more than one components"
267 <            << std::endl;
268 <        exit(1);
269 <    }
261 >  
262 >  // needed for writing out new md file.
263 >  
264 >  outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType;
265 >  outMdFileName = outPrefix + ".md";
266 >  
267 >  //creat new .md file on fly which corrects the number of molecule    
268 >  createMdFile(inputFileName, outMdFileName, numMol);
269 >  
270 >  if (oldInfo != NULL)
271 >    delete oldInfo;
272 >  
273 >  
274 >  // We need to read in new siminfo object.    
275 >  //parse md file and set up the system
276 >  //SimCreator NewCreator;
277 >  
278 >  SimInfo* NewInfo = oldCreator.createSim(outMdFileName, false);
279 >  
280 >  // This was so much fun the first time, lets do it again.
281 >  
282 >  Molecule* mol;
283 >  SimInfo::MoleculeIterator mi;
284 >  mol = NewInfo->beginMolecule(mi);
285  
143    //get mass of molecule.
286  
287 <    mass = getMolMass(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
288 <
289 <    //creat lattice
290 <    simpleLat = LatticeFactory::getInstance()->createLattice(latticeType);
291 <
292 <    if (simpleLat == NULL) {
293 <        std::cerr << "Error in creating lattice" << std::endl;
294 <        exit(1);
295 <    }
296 <
297 <    numMolPerCell = simpleLat->getNumSitesPerCell();
298 <
299 <    //calculate lattice constant (in Angstrom)
300 <    //latticeConstant = pow(rhoConvertConst * numMolPerCell * mass / density,
301 <    //                      1.0 / 3.0);
287 >  for(int i = -nx; i < nx; i++) {
288 >     for(int j = -ny; j < ny; j++) {
289 >        for(int k = -nz; k < nz; k++) {
290 >          
291 >           //get the position of the cell sites
292 >           simpleLat->getLatticePointsPos(latticePos, i, j, k);
293 >          
294 >           for(int l = 0; l < numMolPerCell; l++) {
295 > #ifdef HAVE_CGAL              
296 >              if (myGeometry->isInsidePolyhedron(latticePos[l][0],latticePos[l][1],latticePos[l][2])){
297 > #endif                              
298 >                 if (mol != NULL) {
299 >                    locator->placeMol(latticePos[l], latticeOrt[l], mol);
300 >                 } else {
301 >                    std::cerr<<"Error in placing molecule " << std::endl;                    
302 >                 }
303 >                 mol = NewInfo->nextMolecule(mi);
304 > #ifdef HAVE_CGAL                
305 >              }
306 > #endif              
307 >           }
308 >        }
309 >     }
310 >  }
311 >  
312  
313 <                latticeConstant = args_info.latticeCnst_arg;
314 <                rodLength = args_info.length_arg;
315 <                rodDiameter = args_info.width_arg;
316 <                
317 <    //set lattice constant
318 <    lc.push_back(latticeConstant);
319 <    simpleLat->setLatticeConstant(lc);
320 <
321 <    
322 <    //determine the output file names  
323 <    if (args_info.output_given)
324 <        outInitFileName = args_info.output_arg;
325 <    else
326 <        outInitFileName = getPrefix(inputFileName.c_str()) + ".in";
327 <    
328 <    //creat Molocator
329 <    locator = new MoLocator(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
330 <
331 <                /*
332 <                Assume we are carving nanorod out of a cublic block of material and that
333 <                the shape the material will fit within that block....
334 <                The model in geometry builder assumes the long axis is in the y direction and the x-z plane is the
335 <                diameter of the particle.                
336 <                 */
337 <                // Number of Unit Cells in Length first
338 <                ny = (int)(rodLength/latticeConstant);
339 <                // Number of unit cells in Width
340 <                nx = (int)(rodDiameter/latticeConstant);
341 <                nz = (int)(rodDiameter/latticeConstant);
342 <                
343 <                
344 <                
345 <                // Create geometry for nanocrystal
346 <                //GeometryBuilder myGeometry(rodLength,rodDiameter);
347 <                
348 <                /*
349 <                 We have to build the system first to figure out how many molecules there are
350 <                 then create a md file and then actually build the system.
351 <                 */
352 <                
353 <                //place the molecules
354 <                
355 <    curMolIndex = 0;
204 <                
205 <    //get the orientation of the cell sites
206 <    //for the same type of molecule in same lattice, it will not change
207 <    latticeOrt = simpleLat->getLatticePointsOrt();
208 <        
209 <                
210 <                /*
211 <                 void BaseLattice::getLatticePointsPos(std::vector<Vector3d>& latticePos, int nx, int ny, int nz){
212 <                        
213 <                         latticePos.resize(nCellSites);
214 <                        
215 <                         for( int i=0;i < nCellSites;i++){
216 <                                
217 <                                 latticePos[i][0] = origin[0] + cellSitesPos[i][0] + cellLen[0] * (double(nx) - 0.5);
218 <                                 latticePos[i][1] = origin[1] + cellSitesPos[i][1] + cellLen[1] * (double(ny) - 0.5);
219 <                                 latticePos[i][2] = origin[2] + cellSitesPos[i][2] + cellLen[2] * (double(nz) - 0.5);    
220 <                         }
221 <                        
222 <                         */
223 <                
224 <                
225 <                
226 <                
227 <                numMol = 0;
228 <    for(int i = 0; i < nx; i++) {
229 <                        for(int j = 0; j < ny; j++) {
230 <                                for(int k = 0; k < nz; k++) {
231 <                                        //if (oldInfo->getNGlobalMolecules() != numMol) {
232 <                                        
233 <                                        
234 <                                        
235 <                                        //get the position of the cell sites
236 <                                        simpleLat->getLatticePointsPos(latticePos, i, j, k);
237 <                                        
238 <                                        for(int l = 0; l < numMolPerCell; l++) {
239 <                                                /*
240 <                                                if (myGeometry.isInsidePolyhedron(latticePos[l][0],latticePos[l][1],latticePos[l][2])){
241 <                                                        numMol++;
242 <                                                }
243 <                                                 */
244 <                                                numMol++;
245 <                                        }
246 <                                        }
247 <                                }
248 <                        }
249 <                
250 <                        
251 <                // needed for writing out new md file.
252 <                
253 <                outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType;
254 <                outMdFileName = outPrefix + ".md";
255 <                
256 <                //creat new .md file on fly which corrects the number of molecule    
257 <                createMdFile(inputFileName, outMdFileName, numMol);
258 <                
259 <                if (oldInfo != NULL)
260 <                        delete oldInfo;
261 <
262 <                        
263 <                        // We need to read in new siminfo object.      
264 <                        //parse md file and set up the system
265 <    //SimCreator NewCreator;
266 <                
267 <    SimInfo* NewInfo = oldCreator.createSim(outMdFileName, false);
268 <                
269 <        // This was so much fun the first time, lets do it again.
270 <                
271 <    Molecule* mol;
272 <    SimInfo::MoleculeIterator mi;
273 <    mol = NewInfo->beginMolecule(mi);
274 <                
275 <                for(int i = 0; i < nx; i++) {
276 <                        for(int j = 0; j < ny; j++) {
277 <                                for(int k = 0; k < nz; k++) {
278 <                                        
279 <                                        //get the position of the cell sites
280 <                                        simpleLat->getLatticePointsPos(latticePos, i, j, k);
281 <                                        
282 <                                        for(int l = 0; l < numMolPerCell; l++) {
283 <                                                if (mol != NULL) {
284 <                                                        /*
285 <                                                        if (myGeometry.isInsidePolyhedron(latticePos[l][0],latticePos[l][1],latticePos[l][2])){
286 <                                                                locator->placeMol(latticePos[l], latticeOrt[l], mol);
287 <                                                        }              
288 <                                                         */
289 <                                                        locator->placeMol(latticePos[l], latticeOrt[l], mol);
290 <                                                } else {
291 <                                                        std::cerr << std::endl;                    
292 <                                                }
293 <                                                mol = NewInfo->nextMolecule(mi);
294 <                                        }
295 <                                }
296 <                        }
297 <    }
298 <                
299 <                
300 <                
301 <    //fill Hmat
302 <    hmat(0, 0)= nx * latticeConstant;
303 <    hmat(0, 1) = 0.0;
304 <    hmat(0, 2) = 0.0;
305 <
306 <    hmat(1, 0) = 0.0;
307 <    hmat(1, 1) = ny * latticeConstant;
308 <    hmat(1, 2) = 0.0;
309 <
310 <    hmat(2, 0) = 0.0;
311 <    hmat(2, 1) = 0.0;
312 <    hmat(2, 2) = nz * latticeConstant;
313 <
314 <    //set Hmat
315 <    NewInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
316 <
317 <    
318 <    //create dumpwriter and write out the coordinates
319 <    NewInfo->setFinalConfigFileName(outInitFileName);
320 <    writer = new DumpWriter(NewInfo);
321 <
322 <    if (writer == NULL) {
323 <        std::cerr << "error in creating DumpWriter" << std::endl;
324 <        exit(1);
325 <    }
326 <
327 <    writer->writeDump();
328 <    std::cout << "new initial configuration file: " << outInitFileName
329 <        << " is generated." << std::endl;
330 <
331 <    //delete objects
332 <
333 <    //delete oldInfo and oldSimSetup
334 <                
335 <                                if (NewInfo != NULL)
336 <                        delete NewInfo;
337 <                
338 <    if (writer != NULL)
339 <                        delete writer;
340 <    delete simpleLat;  
341 <
342 <    return 0;
313 >  
314 >  //fill Hmat
315 >  hmat(0, 0)= nx * latticeConstant;
316 >  hmat(0, 1) = 0.0;
317 >  hmat(0, 2) = 0.0;
318 >  
319 >  hmat(1, 0) = 0.0;
320 >  hmat(1, 1) = ny * latticeConstant;
321 >  hmat(1, 2) = 0.0;
322 >  
323 >  hmat(2, 0) = 0.0;
324 >  hmat(2, 1) = 0.0;
325 >  hmat(2, 2) = nz * latticeConstant;
326 >  
327 >  //set Hmat
328 >  NewInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
329 >  
330 >  
331 >  //create dumpwriter and write out the coordinates
332 >  NewInfo->setFinalConfigFileName(outInitFileName);
333 >  writer = new DumpWriter(NewInfo);
334 >  
335 >  if (writer == NULL) {
336 >    std::cerr << "error in creating DumpWriter" << std::endl;
337 >    exit(1);
338 >  }
339 >  
340 >  writer->writeDump();
341 >  std::cout << "new initial configuration file: " << outInitFileName
342 >            << " is generated." << std::endl;
343 >  
344 >  //delete objects
345 >  
346 >  //delete oldInfo and oldSimSetup
347 >  
348 >  if (NewInfo != NULL)
349 >    delete NewInfo;
350 >  
351 >  if (writer != NULL)
352 >    delete writer;
353 >  delete simpleLat;    
354 >  cmdline_parser_free(&args_info);
355 >  return 0;
356   }
357  
358   void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
359                    int numMol) {
360 <    ifstream oldMdFile;
361 <    ofstream newMdFile;
362 <    const int MAXLEN = 65535;
363 <    char buffer[MAXLEN];
364 <
365 <    //create new .md file based on old .md file
366 <    oldMdFile.open(oldMdFileName.c_str());
367 <    newMdFile.open(newMdFileName.c_str());
368 <
360 >  ifstream oldMdFile;
361 >  ofstream newMdFile;
362 >  const int MAXLEN = 65535;
363 >  char buffer[MAXLEN];
364 >  
365 >  //create new .md file based on old .md file
366 >  oldMdFile.open(oldMdFileName.c_str());
367 >  newMdFile.open(newMdFileName.c_str());
368 >  
369 >  oldMdFile.getline(buffer, MAXLEN);
370 >  
371 >  while (!oldMdFile.eof()) {
372 >    
373 >    //correct molecule number
374 >    if (strstr(buffer, "nMol") != NULL) {
375 >      sprintf(buffer, "\tnMol = %i;", numMol);                          
376 >      newMdFile << buffer << std::endl;
377 >    } else
378 >      newMdFile << buffer << std::endl;
379 >    
380      oldMdFile.getline(buffer, MAXLEN);
381 <
382 <    while (!oldMdFile.eof()) {
383 <
384 <        //correct molecule number
361 <        if (strstr(buffer, "nMol") != NULL) {
362 <            sprintf(buffer, "\tnMol = %i;", numMol);                            
363 <            newMdFile << buffer << std::endl;
364 <        } else
365 <            newMdFile << buffer << std::endl;
366 <
367 <        oldMdFile.getline(buffer, MAXLEN);
368 <    }
369 <
370 <    oldMdFile.close();
371 <    newMdFile.close();
381 >  }
382 >  
383 >  oldMdFile.close();
384 >  newMdFile.close();
385   }
386  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines