OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
MoleculeCreator.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45/**
46 * @file MoleculeCreator.cpp
47 * @author tlin
48 * @date 11/04/2004
49 * @version 1.0
50 */
51
53
54#include <cassert>
55#include <set>
56#include <typeinfo>
57
59#include "primitives/GhostTorsion.hpp"
60#include "types/AtomType.hpp"
61#include "types/BendTypeParser.hpp"
62#include "types/BondTypeParser.hpp"
64#include "types/FixedChargeAdapter.hpp"
65#include "types/InversionTypeParser.hpp"
66#include "types/TorsionTypeParser.hpp"
67#include "utils/StringUtils.hpp"
68#include "utils/simError.h"
69
70namespace OpenMD {
71
72 Molecule* MoleculeCreator::createMolecule(ForceField* ff,
73 MoleculeStamp* molStamp,
74 int stampId, int globalIndex,
75 LocalIndexManager* localIndexMan) {
76 Molecule* mol = new Molecule(stampId, globalIndex, molStamp->getName(),
77 molStamp->getRegion());
78
79 // create atoms
80 Atom* atom;
81 AtomStamp* currentAtomStamp;
82 size_t nAtom = molStamp->getNAtoms();
83 for (size_t i = 0; i < nAtom; ++i) {
84 currentAtomStamp = molStamp->getAtomStamp(i);
85 atom = createAtom(ff, mol, currentAtomStamp, localIndexMan);
86 mol->addAtom(atom);
87 }
88
89 // create rigidbodies
90 RigidBody* rb;
91 RigidBodyStamp* currentRigidBodyStamp;
92 size_t nRigidbodies = molStamp->getNRigidBodies();
93
94 for (size_t i = 0; i < nRigidbodies; ++i) {
95 currentRigidBodyStamp = molStamp->getRigidBodyStamp(i);
96 rb = createRigidBody(molStamp, mol, currentRigidBodyStamp, localIndexMan);
97 mol->addRigidBody(rb);
98 }
99
100 // create bonds
101 Bond* bond;
102 BondStamp* currentBondStamp;
103 size_t nBonds = molStamp->getNBonds();
104
105 for (size_t i = 0; i < nBonds; ++i) {
106 currentBondStamp = molStamp->getBondStamp(i);
107 bond = createBond(ff, mol, currentBondStamp, localIndexMan);
108 mol->addBond(bond);
109 }
110
111 // create bends
112 Bend* bend;
113 BendStamp* currentBendStamp;
114 size_t nBends = molStamp->getNBends();
115 for (size_t i = 0; i < nBends; ++i) {
116 currentBendStamp = molStamp->getBendStamp(i);
117 bend = createBend(ff, mol, currentBendStamp, localIndexMan);
118 mol->addBend(bend);
119 }
120
121 // create torsions
122 Torsion* torsion;
123 TorsionStamp* currentTorsionStamp;
124 size_t nTorsions = molStamp->getNTorsions();
125 for (size_t i = 0; i < nTorsions; ++i) {
126 currentTorsionStamp = molStamp->getTorsionStamp(i);
127 torsion = createTorsion(ff, mol, currentTorsionStamp, localIndexMan);
128 mol->addTorsion(torsion);
129 }
130
131 // create inversions
132 Inversion* inversion;
133 InversionStamp* currentInversionStamp;
134 size_t nInversions = molStamp->getNInversions();
135 for (size_t i = 0; i < nInversions; ++i) {
136 currentInversionStamp = molStamp->getInversionStamp(i);
137 inversion =
138 createInversion(ff, mol, currentInversionStamp, localIndexMan);
139 if (inversion != NULL) { mol->addInversion(inversion); }
140 }
141
142 // create cutoffGroups
143 CutoffGroup* cutoffGroup;
144 CutoffGroupStamp* currentCutoffGroupStamp;
145 size_t nCutoffGroups = molStamp->getNCutoffGroups();
146 for (size_t i = 0; i < nCutoffGroups; ++i) {
147 currentCutoffGroupStamp = molStamp->getCutoffGroupStamp(i);
148 cutoffGroup =
149 createCutoffGroup(mol, currentCutoffGroupStamp, localIndexMan);
150 mol->addCutoffGroup(cutoffGroup);
151 }
152
153 // every free atom is a cutoff group
154 std::vector<Atom*> freeAtoms;
155 std::vector<Atom*>::iterator ai;
156 std::vector<Atom*>::iterator fai;
157
158 // add all atoms into allAtoms set
159 for (atom = mol->beginAtom(fai); atom != NULL; atom = mol->nextAtom(fai)) {
160 freeAtoms.push_back(atom);
161 }
162
163 Molecule::CutoffGroupIterator ci;
164 CutoffGroup* cg;
165
166 for (cg = mol->beginCutoffGroup(ci); cg != NULL;
167 cg = mol->nextCutoffGroup(ci)) {
168 for (atom = cg->beginAtom(ai); atom != NULL; atom = cg->nextAtom(ai)) {
169 // erase the atoms belong to cutoff groups from freeAtoms vector
170 freeAtoms.erase(std::remove(freeAtoms.begin(), freeAtoms.end(), atom),
171 freeAtoms.end());
172 }
173 }
174
175 // loop over the free atoms and then create one cutoff group for
176 // every single free atom
177
178 for (fai = freeAtoms.begin(); fai != freeAtoms.end(); ++fai) {
179 cutoffGroup = createCutoffGroup(mol, *fai, localIndexMan);
180 mol->addCutoffGroup(cutoffGroup);
181 }
182
183 // create bonded constraintPairs:
184 createConstraintPair(mol);
185
186 // create non-bonded constraintPairs
187 for (std::size_t i = 0; i < molStamp->getNConstraints(); ++i) {
188 ConstraintStamp* cStamp = molStamp->getConstraintStamp(i);
189 Atom* atomA;
190 Atom* atomB;
191
192 atomA = mol->getAtomAt(cStamp->getA());
193 atomB = mol->getAtomAt(cStamp->getB());
194 assert(atomA && atomB);
195
196 bool printConstraintForce = false;
197
198 if (cStamp->havePrintConstraintForce()) {
199 printConstraintForce = cStamp->getPrintConstraintForce();
200 }
201
202 if (!cStamp->haveConstrainedDistance()) {
203 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
204 "Constraint Error: A non-bond constraint was specified\n"
205 "\twithout providing a value for the constrainedDistance.\n");
206 painCave.isFatal = 1;
207 simError();
208 } else {
209 RealType distance = cStamp->getConstrainedDistance();
210 ConstraintElem* consElemA = new ConstraintElem(atomA);
211 ConstraintElem* consElemB = new ConstraintElem(atomB);
212 ConstraintPair* cPair = new ConstraintPair(
213 consElemA, consElemB, distance, printConstraintForce);
214 mol->addConstraintPair(cPair);
215 }
216 }
217
218 // now create the constraint elements:
219
220 createConstraintElem(mol);
221
222 // Does this molecule stamp define a total constrained charge value?
223 // If so, let the created molecule know about it.
224 if (molStamp->haveConstrainTotalCharge()) {
225 mol->setConstrainTotalCharge(molStamp->getConstrainTotalCharge());
226 }
227
228 // The construction of this molecule is finished:
229 mol->complete();
230
231 return mol;
232 }
233
234 Atom* MoleculeCreator::createAtom(ForceField* ff, Molecule*, AtomStamp* stamp,
235 LocalIndexManager* localIndexMan) {
236 AtomType* atomType;
237 Atom* atom;
238
239 atomType = ff->getAtomType(stamp->getType());
240 if (atomType == NULL) {
241 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
242 "Can not find Matching Atom Type for[%s]",
243 stamp->getType().c_str());
244
245 painCave.isFatal = 1;
246 simError();
247 }
248
249 if (stamp->hasOverride()) {
250 std::string baseType = atomType->getName();
251 RealType oc = stamp->getOverrideCharge();
252
253 // Create a new atom type name that builds in the override charge:
254 std::ostringstream ss;
255 ss << oc;
256 std::string atomTypeOverrideName = baseType + "_q=" + ss.str();
257
258 // Maybe we've seen this before?
259
260 AtomType* atB = ff->getAtomType(atomTypeOverrideName);
261
262 if (atB == NULL) {
263 // Nope, we've never seen it before, so make a new one:
264 AtomType* atomTypeOverride = new AtomType();
265 // Base points to the atomType we already found
266 atomTypeOverride->useBase(atomType);
267 int ident = ff->getNAtomType();
268 atomTypeOverride->setIdent(ident);
269 atomTypeOverride->setName(atomTypeOverrideName);
270 ff->addAtomType(atomTypeOverrideName, atomTypeOverride);
271 FixedChargeAdapter fca = FixedChargeAdapter(atomTypeOverride);
272 RealType charge =
273 ff->getForceFieldOptions().getChargeUnitScaling() * oc;
274 fca.makeFixedCharge(charge);
275 // officially use override type for this atom
276 atomType = atomTypeOverride;
277 } else {
278 // we've previously created the override type for this atom, so use that
279 // one:
280 atomType = atB;
281 }
282 }
283
284 // below code still have some kind of hard-coding smell
285 if (atomType->isDirectional()) {
286 DirectionalAtom* dAtom;
287 dAtom = new DirectionalAtom(atomType);
288 atom = dAtom;
289 } else {
290 atom = new Atom(atomType);
291 }
292
293 atom->setLocalIndex(localIndexMan->getNextAtomIndex());
294
295 return atom;
296 }
297
298 RigidBody* MoleculeCreator::createRigidBody(
299 MoleculeStamp* molStamp, Molecule* mol, RigidBodyStamp* rbStamp,
300 LocalIndexManager* localIndexMan) {
301 Atom* atom;
302 size_t nAtoms;
303 Vector3d refCoor;
304 AtomStamp* atomStamp;
305
306 RigidBody* rb = new RigidBody();
307 nAtoms = rbStamp->getNMembers();
308 for (std::size_t i = 0; i < nAtoms; ++i) {
309 // rbStamp->getMember(i) return the local index of current atom
310 // inside the molecule. It is not the same as local index of
311 // atom which is the index of atom at DataStorage class
312 atom = mol->getAtomAt(rbStamp->getMemberAt(i));
313 atomStamp = molStamp->getAtomStamp(rbStamp->getMemberAt(i));
314 rb->addAtom(atom, atomStamp);
315 }
316
317 // after all of the atoms are added, we need to calculate the
318 // reference coordinates
319 rb->calcRefCoords();
320
321 // set the local index of this rigid body, global index will be set later
322 rb->setLocalIndex(localIndexMan->getNextRigidBodyIndex());
323
324 // The rule for naming a rigidbody is: MoleculeName_RB_Integer
325 // The first part is the name of the molecule
326 // The second part is always fixed as "RB"
327 // The third part is the index of the rigidbody defined in meta-data file
328 // For example, Butane_RB_0 is a valid rigid body name of butane molecule
329
330 std::string s = OpenMD_itoa(mol->getNRigidBodies(), 10);
331 rb->setType(mol->getType() + "_RB_" + s.c_str());
332 return rb;
333 }
334
335 Bond* MoleculeCreator::createBond(ForceField* ff, Molecule* mol,
336 BondStamp* stamp,
337 LocalIndexManager* localIndexMan) {
338 BondTypeParser btParser;
339 BondType* bondType = NULL;
340 Atom* atomA;
341 Atom* atomB;
342
343 atomA = mol->getAtomAt(stamp->getA());
344 atomB = mol->getAtomAt(stamp->getB());
345
346 assert(atomA && atomB);
347
348 if (stamp->hasOverride()) {
349 try {
350 bondType = btParser.parseTypeAndPars(stamp->getOverrideType(),
351 stamp->getOverridePars());
352 } catch (OpenMDException& e) {
353 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
354 "MoleculeCreator Error: %s "
355 "for molecule %s\n",
356 e.what(), mol->getType().c_str());
357 painCave.isFatal = 1;
358 simError();
359 }
360
361 } else {
362 bondType = ff->getBondType(atomA->getType(), atomB->getType());
363
364 if (bondType == NULL) {
365 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
366 "Can not find Matching Bond Type for[%s, %s]",
367 atomA->getType().c_str(), atomB->getType().c_str());
368
369 painCave.isFatal = 1;
370 simError();
371 }
372 }
373
374 Bond* bond = new Bond(atomA, atomB, bondType);
375
376 // set the local index of this bond, the global index will be set later
377 bond->setLocalIndex(localIndexMan->getNextBondIndex());
378
379 // The rule for naming a bond is: MoleculeName_Bond_Integer
380 // The first part is the name of the molecule
381 // The second part is always fixed as "Bond"
382 // The third part is the index of the bond defined in meta-data file
383 // For example, Butane_bond_0 is a valid Bond name in a butane molecule
384
385 std::string s = OpenMD_itoa(mol->getNBonds(), 10);
386 bond->setName(mol->getType() + "_Bond_" + s.c_str());
387 return bond;
388 }
389
390 Bend* MoleculeCreator::createBend(ForceField* ff, Molecule* mol,
391 BendStamp* stamp,
392 LocalIndexManager* localIndexMan) {
393 BendTypeParser btParser;
394 BendType* bendType = NULL;
395 Bend* bend = NULL;
396
397 std::vector<int> bendAtoms = stamp->getMembers();
398 if (bendAtoms.size() == 3) {
399 Atom* atomA = mol->getAtomAt(bendAtoms[0]);
400 Atom* atomB = mol->getAtomAt(bendAtoms[1]);
401 Atom* atomC = mol->getAtomAt(bendAtoms[2]);
402
403 assert(atomA && atomB && atomC);
404
405 if (stamp->hasOverride()) {
406 try {
407 bendType = btParser.parseTypeAndPars(stamp->getOverrideType(),
408 stamp->getOverridePars());
409 } catch (OpenMDException& e) {
410 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
411 "MoleculeCreator Error: %s "
412 "for molecule %s\n",
413 e.what(), mol->getType().c_str());
414 painCave.isFatal = 1;
415 simError();
416 }
417 } else {
418 bendType =
419 ff->getBendType(atomA->getType().c_str(), atomB->getType().c_str(),
420 atomC->getType().c_str());
421
422 if (bendType == NULL) {
423 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
424 "Can not find Matching Bend Type for[%s, %s, %s]",
425 atomA->getType().c_str(), atomB->getType().c_str(),
426 atomC->getType().c_str());
427
428 painCave.isFatal = 1;
429 simError();
430 }
431 }
432
433 bend = new Bend(atomA, atomB, atomC, bendType);
434
435 } else if (bendAtoms.size() == 2 && stamp->haveGhostVectorSource()) {
436 int ghostIndex = stamp->getGhostVectorSource();
437 int normalIndex =
438 ghostIndex != bendAtoms[0] ? bendAtoms[0] : bendAtoms[1];
439 Atom* normalAtom = mol->getAtomAt(normalIndex);
440 DirectionalAtom* ghostAtom =
441 dynamic_cast<DirectionalAtom*>(mol->getAtomAt(ghostIndex));
442 if (ghostAtom == NULL) {
443 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
444 "Can not cast Atom to DirectionalAtom");
445 painCave.isFatal = 1;
446 simError();
447 }
448
449 if (stamp->hasOverride()) {
450 try {
451 bendType = btParser.parseTypeAndPars(stamp->getOverrideType(),
452 stamp->getOverridePars());
453 } catch (OpenMDException& e) {
454 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
455 "MoleculeCreator Error: %s "
456 "for molecule %s\n",
457 e.what(), mol->getType().c_str());
458 painCave.isFatal = 1;
459 simError();
460 }
461 } else {
462 bendType = ff->getBendType(normalAtom->getType(), ghostAtom->getType(),
463 "GHOST");
464
465 if (bendType == NULL) {
466 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
467 "Can not find Matching Bend Type for[%s, %s, %s]",
468 normalAtom->getType().c_str(), ghostAtom->getType().c_str(),
469 "GHOST");
470
471 painCave.isFatal = 1;
472 simError();
473 }
474 }
475
476 bend = new GhostBend(normalAtom, ghostAtom, bendType);
477 }
478
479 // set the local index of this bend, the global index will be set later
480 bend->setLocalIndex(localIndexMan->getNextBendIndex());
481
482 // The rule for naming a bend is: MoleculeName_Bend_Integer
483 // The first part is the name of the molecule
484 // The second part is always fixed as "Bend"
485 // The third part is the index of the bend defined in meta-data file
486 // For example, Butane_Bend_0 is a valid Bend name in a butane molecule
487
488 std::string s = OpenMD_itoa(mol->getNBends(), 10);
489 bend->setName(mol->getType() + "_Bend_" + s.c_str());
490 return bend;
491 }
492
493 Torsion* MoleculeCreator::createTorsion(ForceField* ff, Molecule* mol,
494 TorsionStamp* stamp,
495 LocalIndexManager* localIndexMan) {
496 TorsionTypeParser ttParser;
497 TorsionType* torsionType = NULL;
498 Torsion* torsion = NULL;
499
500 std::vector<int> torsionAtoms = stamp->getMembers();
501 if (torsionAtoms.size() < 3) { return torsion; }
502
503 Atom* atomA = mol->getAtomAt(torsionAtoms[0]);
504 Atom* atomB = mol->getAtomAt(torsionAtoms[1]);
505 Atom* atomC = mol->getAtomAt(torsionAtoms[2]);
506
507 if (torsionAtoms.size() == 4) {
508 Atom* atomD = mol->getAtomAt(torsionAtoms[3]);
509
510 assert(atomA && atomB && atomC && atomD);
511
512 if (stamp->hasOverride()) {
513 try {
514 torsionType = ttParser.parseTypeAndPars(stamp->getOverrideType(),
515 stamp->getOverridePars());
516 } catch (OpenMDException& e) {
517 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
518 "MoleculeCreator Error: %s "
519 "for molecule %s\n",
520 e.what(), mol->getType().c_str());
521 painCave.isFatal = 1;
522 simError();
523 }
524 } else {
525 torsionType = ff->getTorsionType(atomA->getType(), atomB->getType(),
526 atomC->getType(), atomD->getType());
527 if (torsionType == NULL) {
528 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
529 "Can not find Matching Torsion Type for[%s, %s, %s, %s]",
530 atomA->getType().c_str(), atomB->getType().c_str(),
531 atomC->getType().c_str(), atomD->getType().c_str());
532
533 painCave.isFatal = 1;
534 simError();
535 }
536 }
537
538 torsion = new Torsion(atomA, atomB, atomC, atomD, torsionType);
539 } else {
540 DirectionalAtom* dAtom = dynamic_cast<DirectionalAtom*>(
541 mol->getAtomAt(stamp->getGhostVectorSource()));
542 if (dAtom == NULL) {
543 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
544 "Can not cast Atom to DirectionalAtom");
545 painCave.isFatal = 1;
546 simError();
547 }
548
549 if (stamp->hasOverride()) {
550 try {
551 torsionType = ttParser.parseTypeAndPars(stamp->getOverrideType(),
552 stamp->getOverridePars());
553 } catch (OpenMDException& e) {
554 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
555 "MoleculeCreator Error: %s "
556 "for molecule %s\n",
557 e.what(), mol->getType().c_str());
558 painCave.isFatal = 1;
559 simError();
560 }
561 } else {
562 torsionType = ff->getTorsionType(atomA->getType(), atomB->getType(),
563 atomC->getType(), "GHOST");
564
565 if (torsionType == NULL) {
566 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
567 "Can not find Matching Torsion Type for[%s, %s, %s, %s]",
568 atomA->getType().c_str(), atomB->getType().c_str(),
569 atomC->getType().c_str(), "GHOST");
570
571 painCave.isFatal = 1;
572 simError();
573 }
574 }
575
576 torsion = new GhostTorsion(atomA, atomB, dAtom, torsionType);
577 }
578
579 // set the local index of this torsion, the global index will be set later
580 torsion->setLocalIndex(localIndexMan->getNextTorsionIndex());
581
582 // The rule for naming a torsion is: MoleculeName_Torsion_Integer
583 // The first part is the name of the molecule
584 // The second part is always fixed as "Torsion"
585 // The third part is the index of the torsion defined in meta-data file
586 // For example, Butane_Torsion_0 is a valid Torsion name in a
587 // butane molecule
588
589 std::string s = OpenMD_itoa(mol->getNTorsions(), 10);
590 torsion->setName(mol->getType() + "_Torsion_" + s.c_str());
591 return torsion;
592 }
593
594 Inversion* MoleculeCreator::createInversion(
595 ForceField* ff, Molecule* mol, InversionStamp* stamp,
596 LocalIndexManager* localIndexMan) {
597 InversionTypeParser itParser;
598 InversionType* inversionType = NULL;
599 Inversion* inversion = NULL;
600
601 int center = stamp->getCenter();
602 std::vector<int> satellites = stamp->getSatellites();
603 if (satellites.size() != 3) { return inversion; }
604
605 Atom* atomA = mol->getAtomAt(center);
606 Atom* atomB = mol->getAtomAt(satellites[0]);
607 Atom* atomC = mol->getAtomAt(satellites[1]);
608 Atom* atomD = mol->getAtomAt(satellites[2]);
609
610 assert(atomA && atomB && atomC && atomD);
611
612 if (stamp->hasOverride()) {
613 try {
614 inversionType = itParser.parseTypeAndPars(stamp->getOverrideType(),
615 stamp->getOverridePars());
616 } catch (OpenMDException& e) {
617 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
618 "MoleculeCreator Error: %s "
619 "for molecule %s\n",
620 e.what(), mol->getType().c_str());
621 painCave.isFatal = 1;
622 simError();
623 }
624 } else {
625 inversionType = ff->getInversionType(atomA->getType(), atomB->getType(),
626 atomC->getType(), atomD->getType());
627
628 if (inversionType == NULL) {
629 snprintf(
630 painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
631 "No Matching Inversion Type for[%s, %s, %s, %s]\n"
632 "\t(May not be a problem: not all inversions are parametrized)\n",
633 atomA->getType().c_str(), atomB->getType().c_str(),
634 atomC->getType().c_str(), atomD->getType().c_str());
635
636 painCave.isFatal = 0;
637 painCave.severity = OPENMD_INFO;
638 simError();
639 }
640 }
641 if (inversionType != NULL) {
642 inversion = new Inversion(atomA, atomB, atomC, atomD, inversionType);
643
644 // set the local index of this inversion, the global index will
645 // be set later
646 inversion->setLocalIndex(localIndexMan->getNextInversionIndex());
647
648 // The rule for naming an inversion is: MoleculeName_Inversion_Integer
649 // The first part is the name of the molecule
650 // The second part is always fixed as "Inversion"
651 // The third part is the index of the inversion defined in meta-data file
652 // For example, Benzene_Inversion_0 is a valid Inversion name in a
653 // Benzene molecule
654
655 std::string s = OpenMD_itoa(mol->getNInversions(), 10);
656 inversion->setName(mol->getType() + "_Inversion_" + s.c_str());
657 return inversion;
658 } else {
659 return NULL;
660 }
661 }
662
663 CutoffGroup* MoleculeCreator::createCutoffGroup(
664 Molecule* mol, CutoffGroupStamp* stamp,
665 LocalIndexManager* localIndexMan) {
666 size_t nAtoms;
667 CutoffGroup* cg;
668 Atom* atom;
669 cg = new CutoffGroup();
670
671 nAtoms = stamp->getNMembers();
672 for (size_t i = 0; i < nAtoms; ++i) {
673 atom = mol->getAtomAt(stamp->getMemberAt(i));
674 assert(atom);
675 cg->addAtom(atom);
676 }
677
678 // set the local index of this cutoffGroup, global index will be set later
679 cg->setLocalIndex(localIndexMan->getNextCutoffGroupIndex());
680
681 return cg;
682 }
683
684 CutoffGroup* MoleculeCreator::createCutoffGroup(
685 Molecule*, Atom* atom, LocalIndexManager* localIndexMan) {
686 CutoffGroup* cg;
687 cg = new CutoffGroup();
688 cg->addAtom(atom);
689
690 // set the local index of this cutoffGroup, global index will be set later
691 cg->setLocalIndex(localIndexMan->getNextCutoffGroupIndex());
692
693 return cg;
694 }
695
696 void MoleculeCreator::createConstraintPair(Molecule* mol) {
697 // add bond constraints
698 Molecule::BondIterator bi;
699 Bond* bond;
700 ConstraintPair* cPair;
701
702 for (bond = mol->beginBond(bi); bond != NULL; bond = mol->nextBond(bi)) {
703 BondType* bt = bond->getBondType();
704
705 if (typeid(FixedBondType) == typeid(*bt)) {
706 FixedBondType* fbt = dynamic_cast<FixedBondType*>(bt);
707
708 ConstraintElem* consElemA = new ConstraintElem(bond->getAtomA());
709 ConstraintElem* consElemB = new ConstraintElem(bond->getAtomB());
710 cPair = new ConstraintPair(consElemA, consElemB,
711 fbt->getEquilibriumBondLength(), false);
712 mol->addConstraintPair(cPair);
713 }
714 }
715
716 // rigidbody -- rigidbody constraint is not support yet
717 }
718
719 void MoleculeCreator::createConstraintElem(Molecule* mol) {
720 ConstraintPair* consPair;
721 Molecule::ConstraintPairIterator cpi;
722 std::set<StuntDouble*> sdSet;
723 for (consPair = mol->beginConstraintPair(cpi); consPair != NULL;
724 consPair = mol->nextConstraintPair(cpi)) {
725 StuntDouble* sdA = consPair->getConsElem1()->getStuntDouble();
726 if (sdSet.find(sdA) == sdSet.end()) {
727 sdSet.insert(sdA);
728 mol->addConstraintElem(new ConstraintElem(sdA));
729 }
730
731 StuntDouble* sdB = consPair->getConsElem2()->getStuntDouble();
732 if (sdSet.find(sdB) == sdSet.end()) {
733 sdSet.insert(sdB);
734 mol->addConstraintElem(new ConstraintElem(sdB));
735 }
736 }
737 }
738} // namespace OpenMD
AtomType * getAtomType()
Returns the AtomType of this Atom.
Definition A.hpp:93
virtual Atom * createAtom(ForceField *ff, Molecule *, AtomStamp *stamp, LocalIndexManager *localIndexMan)
Create an atom by its stamp.
Definition MC.cpp:240
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
Real distance(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Returns the distance between two DynamicVectors.