1 |
#include <iostream> |
2 |
|
3 |
#include <cstdlib> |
4 |
#include <cmath> |
5 |
|
6 |
#include "utils/simError.h" |
7 |
#include "applications/simpleBuilder/MoLocator.hpp" |
8 |
#include "types/AtomType.hpp" |
9 |
namespace oopse { |
10 |
MoLocator::MoLocator( MoleculeStamp* theStamp, ForceField* theFF){ |
11 |
|
12 |
myStamp = theStamp; |
13 |
myFF = theFF; |
14 |
nIntegrableObjects = myStamp->getNIntegrable(); |
15 |
calcRef(); |
16 |
} |
17 |
|
18 |
void MoLocator::placeMol( const Vector3d& offset, const Vector3d& ort, Molecule* mol){ |
19 |
Vector3d newCoor; |
20 |
Vector3d curRefCoor; |
21 |
RotMat3x3d rotMat = latVec2RotMat(ort); |
22 |
|
23 |
if(mol->getNIntegrableObjects() != nIntegrableObjects){ |
24 |
sprintf( painCave.errMsg, |
25 |
"MoLocator error.\n" |
26 |
" The number of integrable objects of MoleculeStamp is not the same as that of Molecule\n"); |
27 |
painCave.isFatal = 1; |
28 |
simError(); |
29 |
} |
30 |
|
31 |
Molecule::IntegrableObjectIterator ii; |
32 |
StuntDouble* integrableObject; |
33 |
int i; |
34 |
for (integrableObject = mol->beginIntegrableObject(ii), i = 0; integrableObject != NULL; |
35 |
integrableObject = mol->nextIntegrableObject(ii), ++i) { |
36 |
|
37 |
newCoor = rotMat * refCoords[i]; |
38 |
newCoor += offset; |
39 |
|
40 |
integrableObject->setPos( newCoor); |
41 |
integrableObject->setVel(V3Zero); |
42 |
|
43 |
if(integrableObject->isDirectional()){ |
44 |
integrableObject->setA(rotMat * integrableObject->getA()); |
45 |
integrableObject->setJ(V3Zero); |
46 |
} |
47 |
} |
48 |
} |
49 |
|
50 |
void MoLocator::calcRef( void ){ |
51 |
AtomStamp* currAtomStamp; |
52 |
int nAtoms; |
53 |
int nRigidBodies; |
54 |
std::vector<double> mass; |
55 |
Vector3d coor; |
56 |
Vector3d refMolCom; |
57 |
int nAtomsInRb; |
58 |
double totMassInRb; |
59 |
double currAtomMass; |
60 |
double molMass; |
61 |
|
62 |
nAtoms= myStamp->getNAtoms(); |
63 |
nRigidBodies = myStamp->getNRigidBodies(); |
64 |
|
65 |
for(size_t i=0; i<nAtoms; i++){ |
66 |
|
67 |
currAtomStamp = myStamp->getAtom(i); |
68 |
|
69 |
if( !currAtomStamp->havePosition() ){ |
70 |
sprintf( painCave.errMsg, |
71 |
"MoLocator error.\n" |
72 |
" Component %s, atom %s does not have a position specified.\n" |
73 |
" This means MoLocator cannot initalize it's position.\n", |
74 |
myStamp->getID(), |
75 |
currAtomStamp->getType() ); |
76 |
|
77 |
painCave.isFatal = 1; |
78 |
simError(); |
79 |
} |
80 |
|
81 |
//if atom belongs to rigidbody, just skip it |
82 |
if(myStamp->isAtomInRigidBody(i)) |
83 |
continue; |
84 |
//get mass and the reference coordinate |
85 |
else{ |
86 |
|
87 |
mass.push_back(currAtomMass); |
88 |
coor.x() = currAtomStamp->getPosX(); |
89 |
coor.y() = currAtomStamp->getPosY(); |
90 |
coor.z() = currAtomStamp->getPosZ(); |
91 |
refCoords.push_back(coor); |
92 |
|
93 |
} |
94 |
} |
95 |
|
96 |
for(int i = 0; i < nRigidBodies; i++){ |
97 |
coor.x() = 0; |
98 |
coor.y() = 0; |
99 |
coor.z() = 0; |
100 |
totMassInRb = 0; |
101 |
|
102 |
for(int j = 0; j < nAtomsInRb; j++){ |
103 |
|
104 |
currAtomMass = getAtomMass(currAtomStamp->getType(), myFF); |
105 |
totMassInRb += currAtomMass; |
106 |
|
107 |
coor.x() += currAtomStamp->getPosX() * currAtomMass; |
108 |
coor.y() += currAtomStamp->getPosY() * currAtomMass; |
109 |
coor.z() += currAtomStamp->getPosZ() * currAtomMass; |
110 |
} |
111 |
|
112 |
mass.push_back(totMassInRb); |
113 |
coor /= totMassInRb; |
114 |
refCoords.push_back(coor); |
115 |
} |
116 |
|
117 |
|
118 |
//calculate the reference center of mass |
119 |
molMass = 0; |
120 |
refMolCom.x() = 0; |
121 |
refMolCom.y() = 0; |
122 |
refMolCom.z() = 0; |
123 |
|
124 |
for(int i = 0; i < nIntegrableObjects; i++){ |
125 |
refMolCom += refCoords[i] * mass[i]; |
126 |
molMass += mass[i]; |
127 |
} |
128 |
|
129 |
refMolCom /= molMass; |
130 |
|
131 |
//move the reference center of mass to (0,0,0) and adjust the reference coordinate |
132 |
//of the integrabel objects |
133 |
for(int i = 0; i < nIntegrableObjects; i++) |
134 |
refCoords[i] -= refMolCom; |
135 |
} |
136 |
|
137 |
|
138 |
|
139 |
double getAtomMass(const std::string& at, ForceField* myFF) { |
140 |
double mass; |
141 |
AtomType* atomType= myFF->getAtomType(at); |
142 |
if (atomType != NULL) { |
143 |
mass = atomType->getMass(); |
144 |
} else { |
145 |
mass = 0.0; |
146 |
std::cerr << "Can not find AtomType: " << at << std::endl; |
147 |
} |
148 |
return mass; |
149 |
} |
150 |
|
151 |
double getMolMass(MoleculeStamp *molStamp, ForceField *myFF) { |
152 |
int nAtoms; |
153 |
AtomStamp *currAtomStamp; |
154 |
double totMass; |
155 |
|
156 |
totMass = 0; |
157 |
nAtoms = molStamp->getNAtoms(); |
158 |
|
159 |
for(size_t i = 0; i < nAtoms; i++) { |
160 |
totMass += getAtomMass(currAtomStamp->getType(), myFF); |
161 |
|
162 |
} |
163 |
} |
164 |
RotMat3x3d latVec2RotMat(const Vector3d& lv){ |
165 |
|
166 |
double theta =acos(lv[2]); |
167 |
double phi = atan2(lv[1], lv[0]); |
168 |
double psi = 0; |
169 |
|
170 |
return RotMat3x3d(phi, theta, psi); |
171 |
|
172 |
} |
173 |
|
174 |
} |