1 |
#include <iostream> |
2 |
|
3 |
#include <cstdlib> |
4 |
#include <cmath> |
5 |
|
6 |
#include "simError.h" |
7 |
#include "MoLocator.hpp" |
8 |
|
9 |
MoLocator::MoLocator( MoleculeStamp* theStamp, ForceFields* theFF){ |
10 |
|
11 |
myStamp = theStamp; |
12 |
myFF = theFF; |
13 |
nIntegrableObjects = myStamp->getNIntegrable(); |
14 |
calcRefCoords(); |
15 |
} |
16 |
|
17 |
void MoLocator::placeMol( const Vector3d& offset, const Vector3d& ort, Molecule* mol){ |
18 |
Vector3d newCoor; |
19 |
Vector3d velocity(0.0, 0.0, 0.0); |
20 |
Vector3d angMomentum(0.0, 0.0, 0.0); |
21 |
double quaternion[4]; |
22 |
vector<StuntDouble*> myIntegrableObjects; |
23 |
|
24 |
quaternion[0] = 1.0; |
25 |
quaternion[1] = 0.0; |
26 |
quaternion[2] = 0.0; |
27 |
quaternion[3] = 0.0; |
28 |
|
29 |
myIntegrableObjects = mol->getIntegrableObjects(); |
30 |
|
31 |
if(myIntegrableObjects.size() != nIntegrableObjects){ |
32 |
sprintf( painCave.errMsg, |
33 |
"MoLocator error.\n" |
34 |
" The number of integrable objects of MoleculeStamp is not the same as that of Molecule\n"); |
35 |
painCave.isFatal = 1; |
36 |
simError(); |
37 |
|
38 |
} |
39 |
|
40 |
for(int i=0; i<nIntegrableObjects; i++) { |
41 |
|
42 |
newCoor = refCoords[i] + offset; |
43 |
myIntegrableObjects[i]->setPos( newCoor.vec); |
44 |
myIntegrableObjects[i]->setVel(velocity.vec); |
45 |
|
46 |
if(myIntegrableObjects[i]->isDirectional()){ |
47 |
myIntegrableObjects[i]->setQ(quaternion); |
48 |
myIntegrableObjects[i]->setJ(angMomentum.vec); |
49 |
} |
50 |
} |
51 |
|
52 |
} |
53 |
|
54 |
void MoLocator::calcRefCoords( void ){ |
55 |
AtomStamp* currAtomStamp; |
56 |
int nAtoms; |
57 |
int nRigidBodies; |
58 |
vector<double> mass; |
59 |
Vector3d coor; |
60 |
Vector3d refMolCom; |
61 |
int nAtomsInRb; |
62 |
double totMassInRb; |
63 |
double currAtomMass; |
64 |
double molMass; |
65 |
|
66 |
mass.resize(nIntegrableObjects); |
67 |
|
68 |
nAtoms= myStamp->getNAtoms(); |
69 |
nRigidBodies = myStamp->getNRigidBodies(); |
70 |
|
71 |
for(size_t i=0; i<nAtoms; i++){ |
72 |
|
73 |
currAtomStamp = myStamp->getAtom(i); |
74 |
|
75 |
if( !currAtomStamp->havePosition() ){ |
76 |
sprintf( painCave.errMsg, |
77 |
"MoLocator error.\n" |
78 |
" Component %s, atom %s does not have a position specified.\n" |
79 |
" This means MoLocator cannot initalize it's position.\n", |
80 |
myStamp->getID(), |
81 |
currAtomStamp->getType() ); |
82 |
|
83 |
painCave.isFatal = 1; |
84 |
simError(); |
85 |
} |
86 |
|
87 |
//if atom belongs to rigidbody, just skip it |
88 |
if(myStamp->isAtomInRigidBody(i)) |
89 |
continue; |
90 |
//get mass and the reference coordinate |
91 |
else{ |
92 |
currAtomMass = myFF->getAtomTypeMass(currAtomStamp->getType()); |
93 |
mass.push_back(currAtomMass); |
94 |
coor.x = currAtomStamp->getPosX(); |
95 |
coor.y = currAtomStamp->getPosY(); |
96 |
coor.z = currAtomStamp->getPosZ(); |
97 |
refCoords.push_back(coor); |
98 |
|
99 |
} |
100 |
} |
101 |
|
102 |
for(int i = 0; i < nRigidBodies; i++){ |
103 |
coor.x = 0; |
104 |
coor.y = 0; |
105 |
coor.z = 0; |
106 |
totMassInRb = 0; |
107 |
|
108 |
for(int j = 0; j < nAtomsInRb; j++){ |
109 |
|
110 |
currAtomMass = myFF->getAtomTypeMass(currAtomStamp->getType()); |
111 |
totMassInRb += currAtomMass; |
112 |
|
113 |
coor.x += currAtomStamp->getPosX() * currAtomMass; |
114 |
coor.y += currAtomStamp->getPosY() * currAtomMass; |
115 |
coor.z += currAtomStamp->getPosZ() * currAtomMass; |
116 |
} |
117 |
|
118 |
mass.push_back(totMassInRb); |
119 |
coor /= totMassInRb; |
120 |
refCoords.push_back(coor); |
121 |
} |
122 |
|
123 |
|
124 |
//calculate the reference center of mass |
125 |
molMass = 0; |
126 |
refMolCom.x = 0; |
127 |
refMolCom.y = 0; |
128 |
refMolCom.z = 0; |
129 |
|
130 |
for(int i = 0; i < nIntegrableObjects; i++){ |
131 |
refMolCom += refCoords[i] * mass[i]; |
132 |
molMass += mass[i]; |
133 |
} |
134 |
|
135 |
refMolCom /= molMass; |
136 |
|
137 |
//move the reference center of mass to (0,0,0) and adjust the reference coordinate |
138 |
//of the integrabel objects |
139 |
for(int i = 0; i < nIntegrableObjects; i++) |
140 |
refCoords[i] -= refMolCom; |
141 |
} |
142 |
|