OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
MoLocator.cpp
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 following paper when you publish your work:
33 *
34 * [1] Drisko et al., J. Open Source Softw. 9, 7004 (2024).
35 *
36 * Good starting points for code and simulation methodology are:
37 *
38 * [2] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
39 * [3] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
40 * [4] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
41 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
42 * [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
43 * [7] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
44 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
45 * [9] Drisko & Gezelter, J. Chem. Theory Comput. 20, 4986-4997 (2024).
46 */
47
48#include "utils/MoLocator.hpp"
49
50#include <cmath>
51#include <cstdlib>
52#include <iostream>
53
54#include "types/AtomType.hpp"
55#include "utils/simError.h"
56
57namespace OpenMD {
58 MoLocator::MoLocator(MoleculeStamp* theStamp, ForceField* theFF) {
59 myStamp = theStamp;
60 myFF = theFF;
61 nIntegrableObjects = myStamp->getNIntegrable();
62 calcRef();
63 }
64
65 void MoLocator::placeMol(const Vector3d& offset, const Vector3d& ort,
66 Molecule* mol) {
67 Vector3d newCoor;
68 Vector3d curRefCoor;
69 RotMat3x3d rotMat = latVec2RotMat(ort);
70
71 if (mol->getNIntegrableObjects() != nIntegrableObjects) {
72 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
73 "MoLocator::placeMol error.\n"
74 "\tThe number of integrable objects of MoleculeStamp is not\n"
75 "\tthe same as that of Molecule\n");
76 painCave.isFatal = 1;
77 simError();
78 }
79
80 Molecule::IntegrableObjectIterator ii;
81 StuntDouble* sd;
82 int i;
83 for (sd = mol->beginIntegrableObject(ii), i = 0; sd != NULL;
84 sd = mol->nextIntegrableObject(ii), ++i) {
85 newCoor = rotMat * refCoords[i];
86 newCoor += offset;
87
88 sd->setPos(newCoor);
89 sd->setVel(V3Zero);
90
91 if (sd->isDirectional()) {
92 sd->setA(rotMat * sd->getA());
93 sd->setJ(V3Zero);
94 }
95 }
96 }
97
98 void MoLocator::calcRef(void) {
99 AtomStamp* currAtomStamp;
100 RigidBodyStamp* rbStamp;
101 std::vector<RealType> mass;
102 Vector3d coor;
103 Vector3d refMolCom;
104 RealType totMassInRb;
105 RealType currAtomMass;
106 RealType molMass;
107
108 std::size_t nAtoms = myStamp->getNAtoms();
109 std::size_t nRigidBodies = myStamp->getNRigidBodies();
110
111 for (std::size_t i = 0; i < nAtoms; i++) {
112 currAtomStamp = myStamp->getAtomStamp(i);
113
114 if (!currAtomStamp->havePosition()) {
115 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
116 "MoLocator::calcRef error.\n"
117 "\tComponent %s, atom %s does not have a position specified.\n"
118 "\tThis means MoLocator cannot initalize it's position.\n",
119 myStamp->getName().c_str(), currAtomStamp->getType().c_str());
120
121 painCave.isFatal = 1;
122 simError();
123 }
124
125 // if atom belongs to rigidbody, just skip it
126 if (myStamp->isAtomInRigidBody(i)) continue;
127 // get mass and the reference coordinate
128 else {
129 currAtomMass = getAtomMass(currAtomStamp->getType(), myFF);
130 mass.push_back(currAtomMass);
131 coor.x() = currAtomStamp->getPosX();
132 coor.y() = currAtomStamp->getPosY();
133 coor.z() = currAtomStamp->getPosZ();
134 refCoords.push_back(coor);
135 }
136 }
137
138 for (std::size_t i = 0; i < nRigidBodies; i++) {
139 rbStamp = myStamp->getRigidBodyStamp(i);
140 std::size_t nAtomsInRb = rbStamp->getNMembers();
141
142 coor.x() = 0.0;
143 coor.y() = 0.0;
144 coor.z() = 0.0;
145 totMassInRb = 0.0;
146
147 for (std::size_t j = 0; j < nAtomsInRb; j++) {
148 currAtomStamp = myStamp->getAtomStamp(rbStamp->getMemberAt(j));
149 currAtomMass = getAtomMass(currAtomStamp->getType(), myFF);
150 totMassInRb += currAtomMass;
151
152 coor.x() += currAtomStamp->getPosX() * currAtomMass;
153 coor.y() += currAtomStamp->getPosY() * currAtomMass;
154 coor.z() += currAtomStamp->getPosZ() * currAtomMass;
155 }
156
157 mass.push_back(totMassInRb);
158 coor /= totMassInRb;
159 refCoords.push_back(coor);
160 }
161
162 // calculate the reference center of mass
163 molMass = 0;
164 refMolCom.x() = 0;
165 refMolCom.y() = 0;
166 refMolCom.z() = 0;
167
168 for (std::size_t i = 0; i < nIntegrableObjects; i++) {
169 refMolCom += refCoords[i] * mass[i];
170 molMass += mass[i];
171 }
172
173 refMolCom /= molMass;
174
175 // move the reference center of mass to (0,0,0) and adjust the
176 // reference coordinate of the integrabel objects
177 for (std::size_t i = 0; i < nIntegrableObjects; i++)
178 refCoords[i] -= refMolCom;
179 }
180
181 RealType MoLocator::getAtomMass(const std::string& at, ForceField* myFF) {
182 RealType mass;
183 AtomType* atomType = myFF->getAtomType(at);
184 if (atomType != NULL) {
185 mass = atomType->getMass();
186 } else {
187 mass = 0.0;
188 std::cerr << "Can not find AtomType: " << at << std::endl;
189 }
190 return mass;
191 }
192
193 RealType MoLocator::getMolMass(MoleculeStamp* molStamp, ForceField* myFF) {
194 unsigned int nAtoms;
195 RealType totMass = 0;
196 nAtoms = molStamp->getNAtoms();
197
198 for (std::size_t i = 0; i < nAtoms; i++) {
199 AtomStamp* currAtomStamp = molStamp->getAtomStamp(i);
200 totMass += getAtomMass(currAtomStamp->getType(), myFF);
201 }
202 return totMass;
203 }
204
205 RotMat3x3d MoLocator::latVec2RotMat(const Vector3d& lv) {
206 RealType theta = acos(lv[2]);
207 RealType phi = atan2(lv[1], lv[0]);
208 RealType psi = 0;
209
210 return RotMat3x3d(phi, theta, psi);
211 }
212} // namespace OpenMD
Real & x()
Returns reference of the first element of Vector3.
Definition Vector3.hpp:99
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.