ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/applications/nanoparticleBuilder/nanoparticleBuilder.cpp
Revision: 2679
Committed: Thu Mar 30 19:37:48 2006 UTC (18 years, 3 months ago) by chuckv
File size: 9506 byte(s)
Log Message:
More testing of nanoparticleBuilder.

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 chuckv 2679 if (nComponents != args_info.molFraction_given && nComponents != 1){
175 chuckv 2352 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 chuckv 2679 for (int i = 0;i<nComponents-1;i++){ /* Figure out Percent for each component */
181 chuckv 2352 numMol[i] = int((double)numSites * args_info.molFraction_arg[i]);
182 chuckv 2679 std::cout<<numMol[i]<<std::endl;
183 chuckv 2352 totComponents += numMol[i];
184     }
185     numMol[nComponents-1] = numSites - totComponents;
186 chuckv 2679
187 chuckv 2657 /* do the iPod thing, Shuffle da vector */
188     std::random_shuffle(nanoParticleSites.begin(), nanoParticleSites.end());
189 chuckv 2352 } else{ /*Handle core-shell with multiple components.*/
190     std::cout << "Creating a core-shell nanoparticle." << std::endl;
191     if (nComponents != args_info.ShellRadius_given + 1){
192     std::cerr << "Number of components does not equal ShellRadius occurances." << std::endl;
193 chuckv 2657 exit(1);
194     }
195 chuckv 2352
196     }
197    
198 chuckv 2657
199 chuckv 2352 //get the orientation of the cell sites
200     //for the same type of molecule in same lattice, it will not change
201 chuckv 2657 latticeOrt = nanoParticle.getPointsOrt();
202 chuckv 2677 std::cout<<"Orientational vector Size: "<< std::endl;
203     std::cout<<latticeOrt.size()<< std::endl;
204    
205 chuckv 2352
206 chuckv 2677
207 chuckv 2352 // needed for writing out new md file.
208    
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 chuckv 2657 createMdFile(inputFileName, outMdFileName, nComponents,numMol);
214 chuckv 2352
215     if (oldInfo != NULL)
216     delete oldInfo;
217    
218    
219     // We need to read in new siminfo object.
220     //parse md file and set up the system
221     //SimCreator NewCreator;
222 chuckv 2679 SimCreator newCreator;
223     SimInfo* NewInfo = newCreator.createSim(outMdFileName, false);
224 chuckv 2352
225    
226 chuckv 2657 // Place molecules
227 chuckv 2352 Molecule* mol;
228     SimInfo::MoleculeIterator mi;
229     mol = NewInfo->beginMolecule(mi);
230 chuckv 2657 int l = 0;
231     for (mol = NewInfo->beginMolecule(mi); mol != NULL; mol = NewInfo->nextMolecule(mi)) {
232 chuckv 2672 locator->placeMol(nanoParticleSites[l], latticeOrt[l], mol);
233 chuckv 2657 l++;
234     }
235 chuckv 2672
236 chuckv 2352
237    
238    
239     //fill Hmat
240 chuckv 2657 hmat(0, 0)= latticeConstant;
241 chuckv 2352 hmat(0, 1) = 0.0;
242     hmat(0, 2) = 0.0;
243    
244     hmat(1, 0) = 0.0;
245 chuckv 2657 hmat(1, 1) = latticeConstant;
246 chuckv 2352 hmat(1, 2) = 0.0;
247    
248     hmat(2, 0) = 0.0;
249     hmat(2, 1) = 0.0;
250 chuckv 2657 hmat(2, 2) = latticeConstant;
251 chuckv 2352
252     //set Hmat
253     NewInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
254    
255    
256     //create dumpwriter and write out the coordinates
257     NewInfo->setFinalConfigFileName(outInitFileName);
258     writer = new DumpWriter(NewInfo);
259    
260     if (writer == NULL) {
261     std::cerr << "error in creating DumpWriter" << std::endl;
262     exit(1);
263     }
264    
265     writer->writeDump();
266     std::cout << "new initial configuration file: " << outInitFileName
267     << " is generated." << std::endl;
268    
269     //delete objects
270    
271     //delete oldInfo and oldSimSetup
272    
273     if (NewInfo != NULL)
274     delete NewInfo;
275    
276     if (writer != NULL)
277 chuckv 2657 delete writer;
278 chuckv 2352 cmdline_parser_free(&args_info);
279     return 0;
280     }
281    
282     void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
283 chuckv 2657 int components,int* numMol) {
284 chuckv 2352 ifstream oldMdFile;
285     ofstream newMdFile;
286     const int MAXLEN = 65535;
287     char buffer[MAXLEN];
288    
289     //create new .md file based on old .md file
290     oldMdFile.open(oldMdFileName.c_str());
291     newMdFile.open(newMdFileName.c_str());
292    
293     oldMdFile.getline(buffer, MAXLEN);
294 chuckv 2657
295     int i = 0;
296 chuckv 2352 while (!oldMdFile.eof()) {
297    
298     //correct molecule number
299     if (strstr(buffer, "nMol") != NULL) {
300 chuckv 2657 if(i<components){
301     sprintf(buffer, "\tnMol = %i;", numMol[i]);
302 chuckv 2352 newMdFile << buffer << std::endl;
303 chuckv 2657 i++;
304     }
305 chuckv 2352 } else
306     newMdFile << buffer << std::endl;
307    
308     oldMdFile.getline(buffer, MAXLEN);
309     }
310    
311     oldMdFile.close();
312     newMdFile.close();
313     }
314