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

# 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
151
152 /* create Molocators */
153 locator = new MoLocator(oldInfo->getMoleculeStamp(0), oldInfo->getForceField());
154
155 /* 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
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 exit(1);
172 }
173 /* Build the mole fractions and number of molecules of each type */
174 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 /* do the iPod thing, Shuffle da vector */
181 std::random_shuffle(nanoParticleSites.begin(), nanoParticleSites.end());
182 } 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 exit(1);
187 }
188
189 }
190
191
192 //get the orientation of the cell sites
193 //for the same type of molecule in same lattice, it will not change
194 latticeOrt = nanoParticle.getPointsOrt();
195
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 createMdFile(inputFileName, outMdFileName, nComponents,numMol);
205
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 // Place molecules
218 Molecule* mol;
219 SimInfo::MoleculeIterator mi;
220 mol = NewInfo->beginMolecule(mi);
221 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
227
228
229
230
231
232 //fill Hmat
233 hmat(0, 0)= latticeConstant;
234 hmat(0, 1) = 0.0;
235 hmat(0, 2) = 0.0;
236
237 hmat(1, 0) = 0.0;
238 hmat(1, 1) = latticeConstant;
239 hmat(1, 2) = 0.0;
240
241 hmat(2, 0) = 0.0;
242 hmat(2, 1) = 0.0;
243 hmat(2, 2) = latticeConstant;
244
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 delete writer;
271 cmdline_parser_free(&args_info);
272 return 0;
273 }
274
275 void createMdFile(const std::string&oldMdFileName, const std::string&newMdFileName,
276 int components,int* numMol) {
277 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
288 int i = 0;
289 while (!oldMdFile.eof()) {
290
291 //correct molecule number
292 if (strstr(buffer, "nMol") != NULL) {
293 if(i<components){
294 sprintf(buffer, "\tnMol = %i;", numMol[i]);
295 newMdFile << buffer << std::endl;
296 i++;
297 }
298 } else
299 newMdFile << buffer << std::endl;
300
301 oldMdFile.getline(buffer, MAXLEN);
302 }
303
304 oldMdFile.close();
305 newMdFile.close();
306 }
307