1 |
#include <stdlib.h> |
2 |
|
3 |
|
4 |
#include "Molecule.hpp" |
5 |
#include "simError.h" |
6 |
|
7 |
|
8 |
|
9 |
Molecule::Molecule( void ){ |
10 |
|
11 |
myAtoms = NULL; |
12 |
myBonds = NULL; |
13 |
myBends = NULL; |
14 |
myTorsions = NULL; |
15 |
} |
16 |
|
17 |
Molecule::~Molecule( void ){ |
18 |
int i; |
19 |
CutoffGroup* cg; |
20 |
vector<CutoffGroup*>::iterator iter; |
21 |
|
22 |
if( myAtoms != NULL ){ |
23 |
for(i=0; i<nAtoms; i++) if(myAtoms[i] != NULL ) delete myAtoms[i]; |
24 |
delete[] myAtoms; |
25 |
} |
26 |
|
27 |
if( myBonds != NULL ){ |
28 |
for(i=0; i<nBonds; i++) if(myBonds[i] != NULL ) delete myBonds[i]; |
29 |
delete[] myBonds; |
30 |
} |
31 |
|
32 |
if( myBends != NULL ){ |
33 |
for(i=0; i<nBends; i++) if(myBends[i] != NULL ) delete myBends[i]; |
34 |
delete[] myBends; |
35 |
} |
36 |
|
37 |
if( myTorsions != NULL ){ |
38 |
for(i=0; i<nTorsions; i++) if(myTorsions[i] != NULL ) delete myTorsions[i]; |
39 |
delete[] myTorsions; |
40 |
} |
41 |
|
42 |
for(cg = beginCutoffGroup(iter); cg != NULL; cg = nextCutoffGroup(iter)) |
43 |
delete cg; |
44 |
myCutoffGroups.clear(); |
45 |
|
46 |
} |
47 |
|
48 |
|
49 |
void Molecule::initialize( molInit &theInit ){ |
50 |
|
51 |
CutoffGroup* curCutoffGroup; |
52 |
vector<CutoffGroup*>::iterator iterCutoff; |
53 |
Atom* cutoffAtom; |
54 |
vector<Atom*>::iterator iterAtom; |
55 |
int atomIndex; |
56 |
|
57 |
nAtoms = theInit.nAtoms; |
58 |
nMembers = nAtoms; |
59 |
nBonds = theInit.nBonds; |
60 |
nBends = theInit.nBends; |
61 |
nTorsions = theInit.nTorsions; |
62 |
nRigidBodies = theInit.nRigidBodies; |
63 |
nOriented = theInit.nOriented; |
64 |
|
65 |
myAtoms = theInit.myAtoms; |
66 |
myBonds = theInit.myBonds; |
67 |
myBends = theInit.myBends; |
68 |
myTorsions = theInit.myTorsions; |
69 |
myRigidBodies = theInit.myRigidBodies; |
70 |
|
71 |
myIntegrableObjects = theInit.myIntegrableObjects; |
72 |
|
73 |
for (int i = 0; i < myRigidBodies.size(); i++) |
74 |
myRigidBodies[i]->calcRefCoords(); |
75 |
|
76 |
myCutoffGroups = theInit.myCutoffGroups; |
77 |
nCutoffGroups = myCutoffGroups.size(); |
78 |
|
79 |
myConstraintPairs = theInit.myConstraintPairs; |
80 |
|
81 |
} |
82 |
|
83 |
void Molecule::calcForces( void ){ |
84 |
|
85 |
int i; |
86 |
double com[3]; |
87 |
|
88 |
for(i=0; i<myRigidBodies.size(); i++) { |
89 |
myRigidBodies[i]->updateAtoms(); |
90 |
} |
91 |
|
92 |
//calculate the center of mass of the molecule |
93 |
//getCOM(com); |
94 |
//for(int i = 0; i < nAtoms; i ++) |
95 |
// myAtoms[i]->setRc(com); |
96 |
|
97 |
|
98 |
for(i=0; i<nBonds; i++){ |
99 |
myBonds[i]->calc_forces(); |
100 |
} |
101 |
|
102 |
for(i=0; i<nBends; i++){ |
103 |
myBends[i]->calc_forces(); |
104 |
} |
105 |
|
106 |
for(i=0; i<nTorsions; i++){ |
107 |
myTorsions[i]->calc_forces(); |
108 |
} |
109 |
|
110 |
// Rigid Body forces and torques are done after the fortran force loop |
111 |
|
112 |
} |
113 |
|
114 |
|
115 |
double Molecule::getPotential( void ){ |
116 |
|
117 |
int i; |
118 |
double myPot = 0.0; |
119 |
|
120 |
for(i=0; i<myRigidBodies.size(); i++) { |
121 |
myRigidBodies[i]->updateAtoms(); |
122 |
} |
123 |
|
124 |
for(i=0; i<nBonds; i++){ |
125 |
myPot += myBonds[i]->get_potential(); |
126 |
} |
127 |
|
128 |
for(i=0; i<nBends; i++){ |
129 |
myPot += myBends[i]->get_potential(); |
130 |
} |
131 |
|
132 |
for(i=0; i<nTorsions; i++){ |
133 |
myPot += myTorsions[i]->get_potential(); |
134 |
} |
135 |
|
136 |
return myPot; |
137 |
} |
138 |
|
139 |
void Molecule::printMe( void ){ |
140 |
|
141 |
int i; |
142 |
|
143 |
for(i=0; i<nBonds; i++){ |
144 |
myBonds[i]->printMe(); |
145 |
} |
146 |
|
147 |
for(i=0; i<nBends; i++){ |
148 |
myBends[i]->printMe(); |
149 |
} |
150 |
|
151 |
for(i=0; i<nTorsions; i++){ |
152 |
myTorsions[i]->printMe(); |
153 |
} |
154 |
|
155 |
} |
156 |
|
157 |
void Molecule::moveCOM(double delta[3]){ |
158 |
double aPos[3]; |
159 |
int i, j; |
160 |
|
161 |
for(i=0; i<myIntegrableObjects.size(); i++) { |
162 |
if(myIntegrableObjects[i] != NULL ) { |
163 |
|
164 |
myIntegrableObjects[i]->getPos( aPos ); |
165 |
|
166 |
for (j=0; j< 3; j++) |
167 |
aPos[j] += delta[j]; |
168 |
|
169 |
myIntegrableObjects[i]->setPos( aPos ); |
170 |
} |
171 |
} |
172 |
|
173 |
for(i=0; i<myRigidBodies.size(); i++) { |
174 |
|
175 |
myRigidBodies[i]->getPos( aPos ); |
176 |
|
177 |
for (j=0; j< 3; j++) |
178 |
aPos[j] += delta[j]; |
179 |
|
180 |
myRigidBodies[i]->setPos( aPos ); |
181 |
} |
182 |
} |
183 |
|
184 |
void Molecule::atoms2rigidBodies( void ) { |
185 |
int i; |
186 |
for (i = 0; i < myRigidBodies.size(); i++) { |
187 |
myRigidBodies[i]->calcForcesAndTorques(); |
188 |
} |
189 |
} |
190 |
|
191 |
void Molecule::getCOM( double COM[3] ) { |
192 |
|
193 |
double mass, mtot; |
194 |
double aPos[3]; |
195 |
int i, j; |
196 |
|
197 |
for (j=0; j<3; j++) |
198 |
COM[j] = 0.0; |
199 |
|
200 |
mtot = 0.0; |
201 |
|
202 |
for (i=0; i < myIntegrableObjects.size(); i++) { |
203 |
if (myIntegrableObjects[i] != NULL) { |
204 |
|
205 |
mass = myIntegrableObjects[i]->getMass(); |
206 |
mtot += mass; |
207 |
|
208 |
myIntegrableObjects[i]->getPos( aPos ); |
209 |
|
210 |
for( j = 0; j < 3; j++) |
211 |
COM[j] += aPos[j] * mass; |
212 |
|
213 |
} |
214 |
} |
215 |
|
216 |
for (j = 0; j < 3; j++) |
217 |
COM[j] /= mtot; |
218 |
} |
219 |
|
220 |
double Molecule::getCOMvel( double COMvel[3] ) { |
221 |
|
222 |
double mass, mtot; |
223 |
double aVel[3]; |
224 |
int i, j; |
225 |
|
226 |
|
227 |
for (j=0; j<3; j++) |
228 |
COMvel[j] = 0.0; |
229 |
|
230 |
mtot = 0.0; |
231 |
|
232 |
for (i=0; i < myIntegrableObjects.size(); i++) { |
233 |
if (myIntegrableObjects[i] != NULL) { |
234 |
|
235 |
mass = myIntegrableObjects[i]->getMass(); |
236 |
mtot += mass; |
237 |
|
238 |
myIntegrableObjects[i]->getVel(aVel); |
239 |
|
240 |
for (j=0; j<3; j++) |
241 |
COMvel[j] += aVel[j]*mass; |
242 |
|
243 |
} |
244 |
} |
245 |
|
246 |
for (j=0; j<3; j++) |
247 |
COMvel[j] /= mtot; |
248 |
|
249 |
return mtot; |
250 |
|
251 |
} |
252 |
|
253 |
double Molecule::getTotalMass() |
254 |
{ |
255 |
|
256 |
double totalMass; |
257 |
|
258 |
totalMass = 0; |
259 |
for(int i =0; i < myIntegrableObjects.size(); i++){ |
260 |
totalMass += myIntegrableObjects[i]->getMass(); |
261 |
} |
262 |
|
263 |
return totalMass; |
264 |
} |