ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/applications/nanoparticleBuilder/nanoparticleBuilder.cpp
Revision: 2672
Committed: Fri Mar 24 20:46:26 2006 UTC (18 years, 5 months ago) by chuckv
File size: 9499 byte(s)
Log Message:
Debugging changes to nanoparticle builder.

File Contents

# User Rev Content
1 chuckv 2352 /*
2     * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3     *
4     * The University of Notre Dame grants you ("Licensee") a
5     * non-exclusive, royalty free, license to use, modify and
6     * redistribute this software in source and binary code form, provided
7     * that the following conditions are met:
8     *
9     * 1. Acknowledgement of the program authors must be made in any
10     * publication of scientific results based in part on use of the
11     * program. An acceptable form of acknowledgement is citation of
12     * the article in which the program was described (Matthew
13     * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14     * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15     * Parallel Simulation Engine for Molecular Dynamics,"
16     * J. Comput. Chem. 26, pp. 252-271 (2005))
17     *
18     * 2. Redistributions of source code must retain the above copyright
19     * notice, this list of conditions and the following disclaimer.
20     *
21     * 3. Redistributions in binary form must reproduce the above copyright
22     * notice, this list of conditions and the following disclaimer in the
23     * documentation and/or other materials provided with the
24     * distribution.
25     *
26     * This software is provided "AS IS," without a warranty of any
27     * kind. All express or implied conditions, representations and
28     * warranties, including any implied warranty of merchantability,
29     * fitness for a particular purpose or non-infringement, are hereby
30     * excluded. The University of Notre Dame and its licensors shall not
31     * be liable for any damages suffered by licensee as a result of
32     * using, modifying or distributing the software or its
33     * derivatives. In no event will the University of Notre Dame or its
34     * licensors be liable for any lost revenue, profit or data, or for
35     * direct, indirect, special, consequential, incidental or punitive
36     * damages, however caused and regardless of the theory of liability,
37     * arising out of the use of or inability to use software, even if the
38     * University of Notre Dame has been advised of the possibility of
39     * such damages.
40     */
41    
42     #include <cstdlib>
43     #include <cstdio>
44     #include <cstring>
45     #include <cmath>
46     #include <iostream>
47     #include <string>
48     #include <map>
49     #include <fstream>
50 chuckv 2657 #include <algorithm>
51 chuckv 2352
52     #include "config.h"
53 chuckv 2657 #include "shapedLatticeSpherical.hpp"
54 chuckv 2352 #include "nanoparticleBuilderCmd.h"
55     #include "lattice/LatticeFactory.hpp"
56     #include "utils/MoLocator.hpp"
57     #include "lattice/Lattice.hpp"
58     #include "brains/Register.hpp"
59     #include "brains/SimInfo.hpp"
60     #include "brains/SimCreator.hpp"
61     #include "io/DumpWriter.hpp"
62     #include "math/Vector3.hpp"
63     #include "math/SquareMatrix3.hpp"
64     #include "utils/StringUtils.hpp"
65    
66     using namespace std;
67     using namespace oopse;
68     void createMdFile(const std::string&oldMdFileName,
69     const std::string&newMdFileName,
70 chuckv 2657 int components,int* numMol);
71 chuckv 2352
72     int main(int argc, char *argv []) {
73    
74     //register force fields
75     registerForceFields();
76     registerLattice();
77    
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;
84    
85    
86    
87     Lattice *simpleLat;
88 chuckv 2657 MoLocator* locator;
89     int* numMol;
90     int nComponents;
91 chuckv 2352 double latticeConstant;
92     std::vector<double> lc;
93     double mass;
94     const double rhoConvertConst = 1.661;
95     double density;
96 chuckv 2657 double particleRadius;
97 chuckv 2352
98    
99    
100     Mat3x3d hmat;
101     std::vector<Vector3d> latticePos;
102     std::vector<Vector3d> latticeOrt;
103     int numMolPerCell;
104     int nShells; /* Number of shells in nanoparticle*/
105     int numSites;
106    
107     DumpWriter *writer;
108    
109 chuckv 2574 // Parse Command Line Arguments
110 chuckv 2352 if (cmdline_parser(argc, argv, &args_info) != 0)
111     exit(1);
112    
113    
114    
115     /* get lattice type */
116     latticeType = UpperCase(args_info.latticetype_arg);
117    
118     /* get input file name */
119     if (args_info.inputs_num)
120     inputFileName = args_info.inputs[0];
121     else {
122     std::cerr << "You must specify a input file name.\n" << std::endl;
123     cmdline_parser_print_help();
124     exit(1);
125     }
126    
127     /* parse md file and set up the system */
128     SimCreator oldCreator;
129     SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
130    
131    
132     /*calculate lattice constant (in Angstrom)
133     latticeConstant = pow(rhoConvertConst * numMolPerCell * mass / density,
134     1.0 / 3.0);*/
135    
136     latticeConstant = args_info.latticeCnst_arg;
137     particleRadius = args_info.radius_arg;
138 chuckv 2657 Globals* simParams = oldInfo->getSimParams();
139 chuckv 2352
140 chuckv 2657 /* Find out how many different components in this simualtion */
141     nComponents =simParams->getNComponents();
142 chuckv 2352
143     /*determine the output file names*/
144 chuckv 2574 if (args_info.output_given){
145 chuckv 2352 outInitFileName = args_info.output_arg;
146 chuckv 2574 }else{
147 chuckv 2352 outInitFileName = getPrefix(inputFileName.c_str()) + ".in";
148 chuckv 2574 }
149 chuckv 2657
150 chuckv 2672 std::cout <<"Before build shaped lattice. "<<std::endl;
151 chuckv 2352
152     /* create Molocators */
153     locator = new MoLocator(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
154    
155 chuckv 2657 /* Create nanoparticle */
156     shapedLatticeSpherical nanoParticle(latticeConstant,latticeType,particleRadius);
157 chuckv 2672
158     std::cout <<"Before build getPoints. "<<std::endl;
159 chuckv 2657 /* Build a lattice and get lattice points for this lattice constant */
160     vector<Vector3d> nanoParticleSites = nanoParticle.getPoints();
161 chuckv 2672
162 chuckv 2657 /* Get number of lattice sites */
163     numSites = nanoParticleSites.size();
164    
165 chuckv 2672 //std::cout <<"numSites are %d "<<numSites<<std::endl;
166     // std::cout <<"nComponents are %d "<<nComponents<<std::endl;
167 chuckv 2657 numMol = new int[nComponents];
168    
169 chuckv 2352
170     /* Random particle is the default case*/
171     if (!args_info.ShellRadius_given){
172     std::cout << "Creating a random nanoparticle" << std::endl;
173     /* Check to see if we have enough components */
174     if (nComponents != args_info.molFraction_given + 1){
175     std::cerr << "Number of components does not equal molFraction occurances." << std::endl;
176 chuckv 2657 exit(1);
177 chuckv 2352 }
178 chuckv 2657 /* Build the mole fractions and number of molecules of each type */
179 chuckv 2352 int totComponents = 0;
180     for (int i = 0;i<nComponents-2;i++){ /* Figure out Percent for each component */
181     numMol[i] = int((double)numSites * args_info.molFraction_arg[i]);
182     totComponents += numMol[i];
183     }
184     numMol[nComponents-1] = numSites - totComponents;
185 chuckv 2657 /* do the iPod thing, Shuffle da vector */
186     std::random_shuffle(nanoParticleSites.begin(), nanoParticleSites.end());
187 chuckv 2352 } else{ /*Handle core-shell with multiple components.*/
188     std::cout << "Creating a core-shell nanoparticle." << std::endl;
189     if (nComponents != args_info.ShellRadius_given + 1){
190     std::cerr << "Number of components does not equal ShellRadius occurances." << std::endl;
191 chuckv 2657 exit(1);
192     }
193 chuckv 2352
194     }
195    
196 chuckv 2657
197 chuckv 2352 //get the orientation of the cell sites
198     //for the same type of molecule in same lattice, it will not change
199 chuckv 2657 latticeOrt = nanoParticle.getPointsOrt();
200 chuckv 2352
201    
202    
203     // needed for writing out new md file.
204    
205     outPrefix = getPrefix(inputFileName.c_str()) + "_" + latticeType;
206     outMdFileName = outPrefix + ".md";
207    
208     //creat new .md file on fly which corrects the number of molecule
209 chuckv 2657 createMdFile(inputFileName, outMdFileName, nComponents,numMol);
210 chuckv 2352
211     if (oldInfo != NULL)
212     delete oldInfo;
213    
214    
215     // We need to read in new siminfo object.
216     //parse md file and set up the system
217     //SimCreator NewCreator;
218    
219     SimInfo* NewInfo = oldCreator.createSim(outMdFileName, false);
220    
221    
222 chuckv 2672 std::cout << "Contents of nanoParticleSites to follow: " << std::endl;
223     for (int i=0; i< nanoParticleSites.size()
224     ; i++) {
225     cout<<nanoParticleSites.at(i)<<endl;
226     }
227 chuckv 2657 // Place molecules
228 chuckv 2352 Molecule* mol;
229     SimInfo::MoleculeIterator mi;
230     mol = NewInfo->beginMolecule(mi);
231 chuckv 2657 int l = 0;
232     for (mol = NewInfo->beginMolecule(mi); mol != NULL; mol = NewInfo->nextMolecule(mi)) {
233 chuckv 2672 locator->placeMol(nanoParticleSites[l], latticeOrt[l], mol);
234 chuckv 2657 l++;
235     }
236 chuckv 2672
237 chuckv 2352
238    
239    
240     //fill Hmat
241 chuckv 2657 hmat(0, 0)= latticeConstant;
242 chuckv 2352 hmat(0, 1) = 0.0;
243     hmat(0, 2) = 0.0;
244    
245     hmat(1, 0) = 0.0;
246 chuckv 2657 hmat(1, 1) = latticeConstant;
247 chuckv 2352 hmat(1, 2) = 0.0;
248    
249     hmat(2, 0) = 0.0;
250     hmat(2, 1) = 0.0;
251 chuckv 2657 hmat(2, 2) = latticeConstant;
252 chuckv 2352
253     //set Hmat
254     NewInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
255    
256    
257     //create dumpwriter and write out the coordinates
258     NewInfo->setFinalConfigFileName(outInitFileName);
259     writer = new DumpWriter(NewInfo);
260    
261     if (writer == NULL) {
262     std::cerr << "error in creating DumpWriter" << std::endl;
263     exit(1);
264     }
265    
266     writer->writeDump();
267     std::cout << "new initial configuration file: " << outInitFileName
268     << " is generated." << std::endl;
269    
270     //delete objects
271    
272     //delete oldInfo and oldSimSetup
273    
274     if (NewInfo != NULL)
275     delete NewInfo;
276    
277     if (writer != NULL)
278 chuckv 2657 delete writer;
279 chuckv 2352 cmdline_parser_free(&args_info);
280     return 0;
281     }
282    
283     void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
284 chuckv 2657 int components,int* numMol) {
285 chuckv 2352 ifstream oldMdFile;
286     ofstream newMdFile;
287     const int MAXLEN = 65535;
288     char buffer[MAXLEN];
289    
290     //create new .md file based on old .md file
291     oldMdFile.open(oldMdFileName.c_str());
292     newMdFile.open(newMdFileName.c_str());
293    
294     oldMdFile.getline(buffer, MAXLEN);
295 chuckv 2657
296     int i = 0;
297 chuckv 2352 while (!oldMdFile.eof()) {
298    
299     //correct molecule number
300     if (strstr(buffer, "nMol") != NULL) {
301 chuckv 2657 if(i<components){
302     sprintf(buffer, "\tnMol = %i;", numMol[i]);
303 chuckv 2352 newMdFile << buffer << std::endl;
304 chuckv 2657 i++;
305     }
306 chuckv 2352 } else
307     newMdFile << buffer << std::endl;
308    
309     oldMdFile.getline(buffer, MAXLEN);
310     }
311    
312     oldMdFile.close();
313     newMdFile.close();
314     }
315