ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/applications/nanoparticleBuilder/nanorod_pentBuilder.cpp
Revision: 1880
Committed: Mon Jun 17 18:28:30 2013 UTC (11 years, 10 months ago) by gezelter
File size: 18284 byte(s)
Log Message:
Preparing for official 2.1 release (clean-up)

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. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the
15 * distribution.
16 *
17 * This software is provided "AS IS," without a warranty of any
18 * kind. All express or implied conditions, representations and
19 * warranties, including any implied warranty of merchantability,
20 * fitness for a particular purpose or non-infringement, are hereby
21 * excluded. The University of Notre Dame and its licensors shall not
22 * be liable for any damages suffered by licensee as a result of
23 * using, modifying or distributing the software or its
24 * derivatives. In no event will the University of Notre Dame or its
25 * licensors be liable for any lost revenue, profit or data, or for
26 * direct, indirect, special, consequential, incidental or punitive
27 * damages, however caused and regardless of the theory of liability,
28 * arising out of the use of or inability to use software, even if the
29 * University of Notre Dame has been advised of the possibility of
30 * such damages.
31 *
32 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
33 * research, please cite the appropriate papers when you publish your
34 * work. Good starting points are:
35 *
36 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
37 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
38 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
39 * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
40 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41 * Created by Kelsey M. Stocker on 4/5/12.
42 * @author Kelsey M. Stocker
43 *
44 */
45
46
47
48 #include <cstdlib>
49 #include <cstdio>
50 #include <cstring>
51 #include <cmath>
52 #include <iostream>
53 #include <string>
54 #include <map>
55 #include <fstream>
56 #include <algorithm>
57
58 #include "config.h"
59 #include "shapedLatticePentRod.hpp"
60 #include "nanorod_pentBuilderCmd.h"
61 #include "shapedLatticeRod.hpp"
62 #include "lattice/LatticeFactory.hpp"
63 #include "utils/MoLocator.hpp"
64 #include "lattice/Lattice.hpp"
65 #include "brains/Register.hpp"
66 #include "brains/SimInfo.hpp"
67 #include "brains/SimCreator.hpp"
68 #include "io/DumpWriter.hpp"
69 #include "math/SquareMatrix3.hpp"
70 #include "utils/StringUtils.hpp"
71
72 using namespace std;
73 using namespace OpenMD;
74 void createMdFile(const std::string&oldMdFileName,
75 const std::string&newMdFileName,
76 std::vector<int> numMol);
77
78 int main(int argc, char *argv []) {
79
80 registerLattice();
81
82 gengetopt_args_info args_info;
83 std::string latticeType;
84 std::string inputFileName;
85 std::string outputFileName;
86 MoLocator* locator;
87 int nComponents;
88 double latticeConstant;
89 RealType rodRadius;
90 RealType rodLength;
91 Mat3x3d hmat;
92 DumpWriter *writer;
93
94 // Parse Command Line Arguments
95 if (cmdline_parser(argc, argv, &args_info) != 0)
96 exit(1);
97
98 /* get lattice type */
99 latticeType = "FCC";
100
101 /* get input file name */
102 if (args_info.inputs_num)
103 inputFileName = args_info.inputs[0];
104 else {
105 sprintf(painCave.errMsg, "No input .md file name was specified "
106 "on the command line");
107 painCave.isFatal = 1;
108 cmdline_parser_print_help();
109 simError();
110 }
111
112 /* parse md file and set up the system */
113 SimCreator oldCreator;
114 SimInfo* oldInfo = oldCreator.createSim(inputFileName, false);
115
116 latticeConstant = args_info.latticeConstant_arg;
117 rodRadius = args_info.radius_arg;
118 rodLength = args_info.length_arg;
119 Globals* simParams = oldInfo->getSimParams();
120
121 /* Create nanorod */
122 shapedLatticePentRod nanoRod(latticeConstant, latticeType,
123 rodRadius, rodLength);
124
125 /* Build a lattice and get lattice points for this lattice constant */
126
127 //Rotation angles for lattice
128 RealType phi, theta, psi;
129
130 /*
131 RealType cphi, sphi, ctheta, stheta, cpsi, spsi;
132
133 cphi = cos(phi);
134 sphi = sin(phi);
135 ctheta = cos(theta);
136 stheta = sin(theta);
137 cpsi = cos(psi);
138 spsi = sin(psi);
139 */
140
141 //Rotates 45 degrees about z-axis
142 RotMat3x3d rotation45( 45.0 * M_PI / 180.0, 0.0, 0.0);
143
144 /*rotation45[0][0] = sqrt(2)/2;
145 rotation45[0][1] = -sqrt(2)/2;
146 rotation45[0][2] = 0;
147 rotation45[1][0] = sqrt(2)/2;
148 rotation45[1][1] = sqrt(2)/2;
149 rotation45[1][2] = 0;
150 rotation45[2][0] = 0;
151 rotation45[2][1] = 0;
152 rotation45[2][2] = 1;*/
153
154 phi = 0.0;
155 theta = 72 * M_PI / 180.0;
156 psi = 0.0;
157
158 //Rotates 72 degrees about y-axis
159 RotMat3x3d rotation72(phi, theta, psi);
160
161 /*rotation72[0][0] = sqrt(5)/4 - 0.25;
162 rotation72[0][1] = 0;
163 rotation72[0][2] = sqrt(2*(sqrt(5) + 5))/4;
164 rotation72[1][0] = 0;
165 rotation72[1][1] = 1;
166 rotation72[1][2] = 0;
167 rotation72[2][0] = -sqrt(2*(sqrt(5) + 5))/4;
168 rotation72[2][1] = 0;
169 rotation72[2][2] = sqrt(5)/4 - 0.25;*/
170
171 vector<Vector3d> getsites = nanoRod.getSites();
172 vector<Vector3d> getorientations = nanoRod.getOrientations();
173 vector<Vector3d> sites;
174 vector<Vector3d> orientations;
175
176 for (unsigned int index = 0; index < getsites.size(); index++) {
177 Vector3d mySite = getsites[index];
178 Vector3d myOrient = getorientations[index];
179 Vector3d mySite2 = rotation45 * mySite;
180 Vector3d o2 = rotation45 * myOrient;
181 sites.push_back( mySite2 );
182 orientations.push_back( o2 );
183
184 mySite2 = rotation72 * mySite2;
185 o2 = rotation72 * o2;
186 sites.push_back( mySite2 );
187 orientations.push_back( o2 );
188
189 mySite2 = rotation72 * mySite2;
190 o2 = rotation72 * o2;
191 sites.push_back( mySite2 );
192 orientations.push_back( o2 );
193
194 mySite2 = rotation72 * mySite2;
195 o2 = rotation72 * o2;
196 sites.push_back( mySite2 );
197 orientations.push_back( o2 );
198
199 mySite2 = rotation72 * mySite2;
200 o2 = rotation72 * o2;
201 sites.push_back( mySite2 );
202 orientations.push_back( o2 );
203 }
204
205 int nCenter = int( (rodLength + 1.154700538*rodRadius)/2.88 );
206
207 for (unsigned int index = 0; index <= 0.5*nCenter; index++) {
208 Vector3d myLoc_top(2.88*index, 0.0, 0.0);
209 sites.push_back(myLoc_top);
210 orientations.push_back(Vector3d(0.0));
211 }
212
213 for (unsigned int index = 1; index <= 0.5*nCenter; index++) {
214 Vector3d myLoc_bottom(-2.88*index, 0.0, 0.0);
215 sites.push_back(myLoc_bottom);
216 orientations.push_back(Vector3d(0.0));
217 }
218
219 std::vector<int> vacancyTargets;
220 vector<bool> isVacancy;
221
222 Vector3d myLoc;
223 RealType myR;
224
225 for (unsigned int i = 0; i < sites.size(); i++)
226 isVacancy.push_back(false);
227
228 // cerr << "checking vacancyPercent" << "\n";
229 if (args_info.vacancyPercent_given) {
230 // cerr << "vacancyPercent given" << "\n";
231 if (args_info.vacancyPercent_arg < 0.0 || args_info.vacancyPercent_arg > 100.0) {
232 sprintf(painCave.errMsg, "vacancyPercent was set to a non-sensical value.");
233 painCave.isFatal = 1;
234 simError();
235 } else {
236 RealType vF = args_info.vacancyPercent_arg / 100.0;
237 // cerr << "vacancyPercent = " << vF << "\n";
238 RealType vIR;
239 RealType vOR;
240 if (args_info.vacancyInnerRadius_given) {
241 vIR = args_info.vacancyInnerRadius_arg;
242 } else {
243 vIR = 0.0;
244 }
245 if (args_info.vacancyOuterRadius_given) {
246 vOR = args_info.vacancyOuterRadius_arg;
247 } else {
248 vOR = rodRadius;
249 }
250 if (vIR >= 0.0 && vOR <= rodRadius && vOR >= vIR) {
251
252 for (unsigned int i = 0; i < sites.size(); i++) {
253 myLoc = sites[i];
254 myR = myLoc.length();
255 if (myR >= vIR && myR <= vOR) {
256 vacancyTargets.push_back(i);
257 }
258 }
259 std::random_shuffle(vacancyTargets.begin(), vacancyTargets.end());
260
261 int nTargets = vacancyTargets.size();
262 vacancyTargets.resize((int)(vF * nTargets));
263
264
265 sprintf(painCave.errMsg, "Removing %d atoms from randomly-selected\n"
266 "\tsites between %lf and %lf.", (int) vacancyTargets.size(),
267 vIR, vOR);
268 painCave.isFatal = 0;
269 simError();
270
271 isVacancy.clear();
272 for (unsigned int i = 0; i < sites.size(); i++) {
273 bool vac = false;
274 for (unsigned int j = 0; j < vacancyTargets.size(); j++) {
275 if (i == vacancyTargets[j]) vac = true;
276 }
277 isVacancy.push_back(vac);
278 }
279
280 } else {
281 sprintf(painCave.errMsg, "Something is strange about the vacancy\n"
282 "\tinner or outer radii. Check their values.");
283 painCave.isFatal = 1;
284 simError();
285 }
286 }
287 }
288
289 /* Get number of lattice sites */
290 int nSites = sites.size() - vacancyTargets.size();
291
292 // cerr << "sites.size() = " << sites.size() << "\n";
293 // cerr << "nSites = " << nSites << "\n";
294 // cerr << "vacancyTargets = " << vacancyTargets.size() << "\n";
295
296 std::vector<Component*> components = simParams->getComponents();
297 std::vector<RealType> molFractions;
298 std::vector<RealType> shellRadii;
299 std::vector<int> nMol;
300 std::map<int, int> componentFromSite;
301 nComponents = components.size();
302 // cerr << "nComponents = " << nComponents << "\n";
303
304 if (args_info.molFraction_given && args_info.shellRadius_given) {
305 sprintf(painCave.errMsg, "Specify either molFraction or shellRadius "
306 "arguments, but not both!");
307 painCave.isFatal = 1;
308 simError();
309 }
310
311 if (nComponents == 1) {
312 molFractions.push_back(1.0);
313 shellRadii.push_back(rodRadius);
314 } else if (args_info.molFraction_given) {
315 if ((int)args_info.molFraction_given == nComponents) {
316 for (int i = 0; i < nComponents; i++) {
317 molFractions.push_back(args_info.molFraction_arg[i]);
318 }
319 } else if ((int)args_info.molFraction_given == nComponents-1) {
320 RealType remainingFraction = 1.0;
321 for (int i = 0; i < nComponents-1; i++) {
322 molFractions.push_back(args_info.molFraction_arg[i]);
323 remainingFraction -= molFractions[i];
324 }
325 molFractions.push_back(remainingFraction);
326 } else {
327 sprintf(painCave.errMsg, "nanorodBuilder can't figure out molFractions "
328 "for all of the components in the <MetaData> block.");
329 painCave.isFatal = 1;
330 simError();
331 }
332 } else if ((int)args_info.shellRadius_given) {
333 if ((int)args_info.shellRadius_given == nComponents) {
334 for (int i = 0; i < nComponents; i++) {
335 shellRadii.push_back(args_info.shellRadius_arg[i]);
336 }
337 } else if ((int)args_info.shellRadius_given == nComponents-1) {
338 for (int i = 0; i < nComponents-1; i++) {
339 shellRadii.push_back(args_info.shellRadius_arg[i]);
340 }
341 shellRadii.push_back(rodRadius);
342 } else {
343 sprintf(painCave.errMsg, "nanorodBuilder can't figure out the\n"
344 "\tshell radii for all of the components in the <MetaData> block.");
345 painCave.isFatal = 1;
346 simError();
347 }
348 } else {
349 sprintf(painCave.errMsg, "You have a multi-component <MetaData> block,\n"
350 "\tbut have not specified either molFraction or shellRadius arguments.");
351 painCave.isFatal = 1;
352 simError();
353 }
354
355 if (args_info.molFraction_given) {
356 RealType totalFraction = 0.0;
357
358 /* Do some simple sanity checking*/
359
360 for (int i = 0; i < nComponents; i++) {
361 if (molFractions.at(i) < 0.0) {
362 sprintf(painCave.errMsg, "One of the requested molFractions was"
363 " less than zero!");
364 painCave.isFatal = 1;
365 simError();
366 }
367 if (molFractions.at(i) > 1.0) {
368 sprintf(painCave.errMsg, "One of the requested molFractions was"
369 " greater than one!");
370 painCave.isFatal = 1;
371 simError();
372 }
373 totalFraction += molFractions.at(i);
374 }
375 if (abs(totalFraction - 1.0) > 1e-6) {
376 sprintf(painCave.errMsg, "The sum of molFractions was not close enough to 1.0");
377 painCave.isFatal = 1;
378 simError();
379 }
380
381 int remaining = nSites;
382 for (int i=0; i < nComponents-1; i++) {
383 nMol.push_back(int((RealType)nSites * molFractions.at(i)));
384 remaining -= nMol.at(i);
385 }
386 nMol.push_back(remaining);
387
388 // recompute actual mol fractions and perform final sanity check:
389
390 int totalMolecules = 0;
391 for (int i=0; i < nComponents; i++) {
392 molFractions[i] = (RealType)(nMol.at(i))/(RealType)nSites;
393 totalMolecules += nMol.at(i);
394 }
395 if (totalMolecules != nSites) {
396 sprintf(painCave.errMsg, "Computed total number of molecules is not equal "
397 "to the number of lattice sites!");
398 painCave.isFatal = 1;
399 simError();
400 }
401 } else {
402
403 for (unsigned int i = 0; i < shellRadii.size(); i++) {
404 if (shellRadii.at(i) > rodRadius + 1e-6 ) {
405 sprintf(painCave.errMsg, "One of the shellRadius values exceeds the rod Radius.");
406 painCave.isFatal = 1;
407 simError();
408 }
409 if (shellRadii.at(i) <= 0.0 ) {
410 sprintf(painCave.errMsg, "One of the shellRadius values is smaller than zero!");
411 painCave.isFatal = 1;
412 simError();
413 }
414 }
415 }
416
417 vector<int> ids;
418 if ((int)args_info.molFraction_given){
419 // cerr << "molFraction given 2" << "\n";
420 sprintf(painCave.errMsg, "Creating a randomized spherically-capped nanorod.");
421 painCave.isFatal = 0;
422 simError();
423 /* Random rod is the default case*/
424
425 for (unsigned int i = 0; i < sites.size(); i++)
426 if (!isVacancy[i]) ids.push_back(i);
427
428 std::random_shuffle(ids.begin(), ids.end());
429
430 } else{
431 sprintf(painCave.errMsg, "Creating an fcc nanorod.");
432 painCave.isFatal = 0;
433 simError();
434
435 // RealType smallestSoFar;
436 int myComponent = -1;
437 nMol.clear();
438 nMol.resize(nComponents);
439
440 // cerr << "shellRadii[0] " << shellRadii[0] << "\n";
441 // cerr << "rodRadius " << rodRadius << "\n";
442
443 for (unsigned int i = 0; i < sites.size(); i++) {
444 myLoc = sites[i];
445 myR = myLoc.length();
446 // smallestSoFar = rodRadius;
447 // cerr << "vac = " << isVacancy[i]<< "\n";
448
449 if (!isVacancy[i]) {
450
451
452 // for (int j = 0; j < nComponents; j++) {
453 // if (myR <= shellRadii[j]) {
454 // if (shellRadii[j] <= smallestSoFar) {
455 // smallestSoFar = shellRadii[j];
456 // myComponent = j;
457 // }
458 // }
459 // }
460 myComponent = 0;
461 componentFromSite[i] = myComponent;
462 nMol[myComponent]++;
463 // cerr << "nMol for myComp(" << myComponent<<") = " << nMol[myComponent] << "\n";
464 }
465 }
466 }
467 // cerr << "nMol = " << nMol.at(0) << "\n";
468
469 outputFileName = args_info.output_arg;
470
471 //creat new .md file on fly which corrects the number of molecule
472
473 createMdFile(inputFileName, outputFileName, nMol);
474
475 delete oldInfo;
476
477 SimCreator newCreator;
478 SimInfo* NewInfo = newCreator.createSim(outputFileName, false);
479
480 // Place molecules
481 Molecule* mol;
482 SimInfo::MoleculeIterator mi;
483 mol = NewInfo->beginMolecule(mi);
484
485 int l = 0;
486
487 for (int i = 0; i < nComponents; i++){
488 locator = new MoLocator(NewInfo->getMoleculeStamp(i),
489 NewInfo->getForceField());
490
491 // cerr << "nMol = " << nMol.at(i) << "\n";
492 if (!args_info.molFraction_given) {
493 for (unsigned int n = 0; n < sites.size(); n++) {
494 if (!isVacancy[n]) {
495 if (componentFromSite[n] == i) {
496 mol = NewInfo->getMoleculeByGlobalIndex(l);
497 locator->placeMol(sites[n], orientations[n], mol);
498 l++;
499 }
500 }
501 }
502 } else {
503 for (int n = 0; n < nMol.at(i); n++) {
504 mol = NewInfo->getMoleculeByGlobalIndex(l);
505 locator->placeMol(sites[ids[l]], orientations[ids[l]], mol);
506 l++;
507 }
508 }
509 }
510
511 //fill Hmat
512 hmat(0, 0)= 10.0*rodRadius;
513 hmat(0, 1) = 0.0;
514 hmat(0, 2) = 0.0;
515
516 hmat(1, 0) = 0.0;
517 hmat(1, 1) = 10.0*rodRadius;
518 hmat(1, 2) = 0.0;
519
520 hmat(2, 0) = 0.0;
521 hmat(2, 1) = 0.0;
522 hmat(2, 2) = 5.0*rodLength + 2.0*rodRadius;
523
524 //set Hmat
525 NewInfo->getSnapshotManager()->getCurrentSnapshot()->setHmat(hmat);
526
527
528 //create dumpwriter and write out the coordinates
529 writer = new DumpWriter(NewInfo, outputFileName);
530
531 if (writer == NULL) {
532 sprintf(painCave.errMsg, "Error in creating dumpwriter object ");
533 painCave.isFatal = 1;
534 simError();
535 }
536
537 writer->writeDump();
538
539 // deleting the writer will put the closing at the end of the dump file
540
541 delete writer;
542
543 // cleanup a by calling sim error.....
544 sprintf(painCave.errMsg, "A new OpenMD file called \"%s\" has been "
545 "generated.\n", outputFileName.c_str());
546 painCave.isFatal = 0;
547 simError();
548 return 0;
549 }
550
551 void createMdFile(const std::string&oldMdFileName,
552 const std::string&newMdFileName,
553 std::vector<int> nMol) {
554 ifstream oldMdFile;
555 ofstream newMdFile;
556 const int MAXLEN = 65535;
557 char buffer[MAXLEN];
558
559 //create new .md file based on old .md file
560 oldMdFile.open(oldMdFileName.c_str());
561 newMdFile.open(newMdFileName.c_str());
562 oldMdFile.getline(buffer, MAXLEN);
563
564 unsigned int i = 0;
565 while (!oldMdFile.eof()) {
566
567 //correct molecule number
568 if (strstr(buffer, "nMol") != NULL) {
569 if(i<nMol.size()){
570 sprintf(buffer, "\tnMol = %i;", nMol.at(i));
571 newMdFile << buffer << std::endl;
572 i++;
573 }
574 } else
575 newMdFile << buffer << std::endl;
576
577 oldMdFile.getline(buffer, MAXLEN);
578 }
579
580 oldMdFile.close();
581 newMdFile.close();
582
583 if (i != nMol.size()) {
584 sprintf(painCave.errMsg, "Couldn't replace the correct number of nMol\n"
585 "\tstatements in component blocks. Make sure that all\n"
586 "\tcomponents in the template file have nMol=1");
587 painCave.isFatal = 1;
588 simError();
589 }
590
591 }
592