ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/applications/nanoparticleBuilder/nanoparticleBuilder.cpp
Revision: 2657
Committed: Wed Mar 22 20:57:09 2006 UTC (18 years, 5 months ago) by chuckv
File size: 9093 byte(s)
Log Message:
Added shaped lattice.

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