ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/applications/nanoparticleBuilder/nanoparticleBuilder.cpp
Revision: 2737
Committed: Tue Apr 25 22:54:55 2006 UTC (18 years, 2 months ago) by chuckv
File size: 9501 byte(s)
Log Message:
Change in siminfo used in nanoparticleBuilder.

File Contents

# Content
1 /*
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 #include <algorithm>
51
52 #include "config.h"
53 #include "shapedLatticeSpherical.hpp"
54 #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 int components,int* numMol);
71
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 MoLocator* locator;
89 int* numMol;
90 int nComponents;
91 double latticeConstant;
92 std::vector<double> lc;
93 double mass;
94 const double rhoConvertConst = 1.661;
95 double density;
96 double particleRadius;
97
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 // Parse Command Line Arguments
110 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 Globals* simParams = oldInfo->getSimParams();
139
140 /* Find out how many different components in this simualtion */
141 nComponents =simParams->getNComponents();
142
143 /*determine the output file names*/
144 if (args_info.output_given){
145 outInitFileName = args_info.output_arg;
146 }else{
147 outInitFileName = getPrefix(inputFileName.c_str()) + ".in";
148 }
149
150 std::cout <<"Before build shaped lattice. "<<std::endl;
151
152 /* create Molocators */
153 locator = new MoLocator(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
154
155 /* Create nanoparticle */
156 shapedLatticeSpherical nanoParticle(latticeConstant,latticeType,particleRadius);
157
158 std::cout <<"Before build getPoints. "<<std::endl;
159 /* Build a lattice and get lattice points for this lattice constant */
160 vector<Vector3d> nanoParticleSites = nanoParticle.getPoints();
161
162 /* Get number of lattice sites */
163 numSites = nanoParticleSites.size();
164
165 //std::cout <<"numSites are %d "<<numSites<<std::endl;
166 // std::cout <<"nComponents are %d "<<nComponents<<std::endl;
167 numMol = new int[nComponents];
168
169
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 && nComponents != 1){
175 std::cerr << "Number of components does not equal molFraction occurances." << std::endl;
176 exit(1);
177 }
178 /* Build the mole fractions and number of molecules of each type */
179 int totComponents = 0;
180 for (int i = 0;i<nComponents-1;i++){ /* Figure out Percent for each component */
181 numMol[i] = int((double)numSites * args_info.molFraction_arg[i]);
182 std::cout<<numMol[i]<<std::endl;
183 totComponents += numMol[i];
184 }
185 numMol[nComponents-1] = numSites - totComponents;
186
187 /* do the iPod thing, Shuffle da vector */
188 std::random_shuffle(nanoParticleSites.begin(), nanoParticleSites.end());
189 } 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 exit(1);
194 }
195
196 }
197
198
199 //get the orientation of the cell sites
200 //for the same type of molecule in same lattice, it will not change
201 latticeOrt = nanoParticle.getPointsOrt();
202 std::cout<<"Orientational vector Size: "<< std::endl;
203 std::cout<<latticeOrt.size()<< std::endl;
204
205
206
207 // 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 createMdFile(inputFileName, outMdFileName, nComponents,numMol);
214
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 SimCreator newCreator;
223 SimInfo* NewInfo = newCreator.createSim(outMdFileName, false);
224
225
226 // Place molecules
227 Molecule* mol;
228 SimInfo::MoleculeIterator mi;
229 mol = NewInfo->beginMolecule(mi);
230 int l = 0;
231 for (mol = NewInfo->beginMolecule(mi); mol != NULL; mol = NewInfo->nextMolecule(mi)) {
232 locator->placeMol(nanoParticleSites[l], latticeOrt[l], mol);
233 l++;
234 }
235
236
237
238
239 //fill Hmat
240 hmat(0, 0)= latticeConstant;
241 hmat(0, 1) = 0.0;
242 hmat(0, 2) = 0.0;
243
244 hmat(1, 0) = 0.0;
245 hmat(1, 1) = latticeConstant;
246 hmat(1, 2) = 0.0;
247
248 hmat(2, 0) = 0.0;
249 hmat(2, 1) = 0.0;
250 hmat(2, 2) = latticeConstant;
251
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 delete writer;
278 cmdline_parser_free(&args_info);
279 return 0;
280 }
281
282 void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
283 int components,int* numMol) {
284 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
295 int i = 0;
296 while (!oldMdFile.eof()) {
297
298 //correct molecule number
299 if (strstr(buffer, "nMol") != NULL) {
300 if(i<components){
301 sprintf(buffer, "\tnMol = %i;", numMol[i]);
302 newMdFile << buffer << std::endl;
303 i++;
304 }
305 } else
306 newMdFile << buffer << std::endl;
307
308 oldMdFile.getline(buffer, MAXLEN);
309 }
310
311 oldMdFile.close();
312 newMdFile.close();
313 }
314