1 |
#include <cstdlib> |
2 |
#include <cstring> |
3 |
#include <cmath> |
4 |
|
5 |
#include <iostream> |
6 |
using namespace std; |
7 |
|
8 |
#include "SimInfo.hpp" |
9 |
#define __C |
10 |
#include "fSimulation.h" |
11 |
#include "simError.h" |
12 |
|
13 |
#include "fortranWrappers.hpp" |
14 |
|
15 |
#ifdef IS_MPI |
16 |
#include "mpiSimulation.hpp" |
17 |
#endif |
18 |
|
19 |
inline double roundMe( double x ){ |
20 |
return ( x >= 0 ) ? floor( x + 0.5 ) : ceil( x - 0.5 ); |
21 |
} |
22 |
|
23 |
|
24 |
SimInfo* currentInfo; |
25 |
|
26 |
SimInfo::SimInfo(){ |
27 |
excludes = NULL; |
28 |
n_constraints = 0; |
29 |
n_oriented = 0; |
30 |
n_dipoles = 0; |
31 |
ndf = 0; |
32 |
ndfRaw = 0; |
33 |
the_integrator = NULL; |
34 |
setTemp = 0; |
35 |
thermalTime = 0.0; |
36 |
rCut = 0.0; |
37 |
|
38 |
usePBC = 0; |
39 |
useLJ = 0; |
40 |
useSticky = 0; |
41 |
useDipole = 0; |
42 |
useReactionField = 0; |
43 |
useGB = 0; |
44 |
useEAM = 0; |
45 |
|
46 |
wrapMeSimInfo( this ); |
47 |
} |
48 |
|
49 |
void SimInfo::setBox(double newBox[3]) { |
50 |
|
51 |
int i, j; |
52 |
double tempMat[3][3]; |
53 |
|
54 |
for(i=0; i<3; i++) |
55 |
for (j=0; j<3; j++) tempMat[i][j] = 0.0;; |
56 |
|
57 |
tempMat[0][0] = newBox[0]; |
58 |
tempMat[1][1] = newBox[1]; |
59 |
tempMat[2][2] = newBox[2]; |
60 |
|
61 |
setBoxM( tempMat ); |
62 |
|
63 |
} |
64 |
|
65 |
void SimInfo::setBoxM( double theBox[3][3] ){ |
66 |
|
67 |
int i, j, status; |
68 |
double smallestBoxL, maxCutoff; |
69 |
double FortranHmat[9]; // to preserve compatibility with Fortran the |
70 |
// ordering in the array is as follows: |
71 |
// [ 0 3 6 ] |
72 |
// [ 1 4 7 ] |
73 |
// [ 2 5 8 ] |
74 |
double FortranHmatInv[9]; // the inverted Hmat (for Fortran); |
75 |
|
76 |
|
77 |
for(i=0; i < 3; i++) |
78 |
for (j=0; j < 3; j++) Hmat[i][j] = theBox[i][j]; |
79 |
|
80 |
cerr |
81 |
<< "setting Hmat ->\n" |
82 |
<< "[ " << Hmat[0][0] << ", " << Hmat[0][1] << ", " << Hmat[0][2] << " ]\n" |
83 |
<< "[ " << Hmat[1][0] << ", " << Hmat[1][1] << ", " << Hmat[1][2] << " ]\n" |
84 |
<< "[ " << Hmat[2][0] << ", " << Hmat[2][1] << ", " << Hmat[2][2] << " ]\n"; |
85 |
|
86 |
calcBoxL(); |
87 |
calcHmatInv(); |
88 |
|
89 |
for(i=0; i < 3; i++) { |
90 |
for (j=0; j < 3; j++) { |
91 |
FortranHmat[3*j + i] = Hmat[i][j]; |
92 |
FortranHmatInv[3*j + i] = HmatInv[i][j]; |
93 |
} |
94 |
} |
95 |
|
96 |
setFortranBoxSize(FortranHmat, FortranHmatInv, &orthoRhombic); |
97 |
|
98 |
smallestBoxL = boxLx; |
99 |
if (boxLy < smallestBoxL) smallestBoxL = boxLy; |
100 |
if (boxLz < smallestBoxL) smallestBoxL = boxLz; |
101 |
|
102 |
maxCutoff = smallestBoxL / 2.0; |
103 |
|
104 |
if (rList > maxCutoff) { |
105 |
sprintf( painCave.errMsg, |
106 |
"New Box size is forcing neighborlist radius down to %lf\n", |
107 |
maxCutoff ); |
108 |
painCave.isFatal = 0; |
109 |
simError(); |
110 |
|
111 |
rList = maxCutoff; |
112 |
|
113 |
sprintf( painCave.errMsg, |
114 |
"New Box size is forcing cutoff radius down to %lf\n", |
115 |
maxCutoff - 1.0 ); |
116 |
painCave.isFatal = 0; |
117 |
simError(); |
118 |
|
119 |
rCut = rList - 1.0; |
120 |
|
121 |
// list radius changed so we have to refresh the simulation structure. |
122 |
refreshSim(); |
123 |
} |
124 |
|
125 |
if (rCut > maxCutoff) { |
126 |
sprintf( painCave.errMsg, |
127 |
"New Box size is forcing cutoff radius down to %lf\n", |
128 |
maxCutoff ); |
129 |
painCave.isFatal = 0; |
130 |
simError(); |
131 |
|
132 |
status = 0; |
133 |
LJ_new_rcut(&rCut, &status); |
134 |
if (status != 0) { |
135 |
sprintf( painCave.errMsg, |
136 |
"Error in recomputing LJ shifts based on new rcut\n"); |
137 |
painCave.isFatal = 1; |
138 |
simError(); |
139 |
} |
140 |
} |
141 |
} |
142 |
|
143 |
|
144 |
void SimInfo::getBoxM (double theBox[3][3]) { |
145 |
|
146 |
int i, j; |
147 |
for(i=0; i<3; i++) |
148 |
for (j=0; j<3; j++) theBox[i][j] = Hmat[i][j]; |
149 |
} |
150 |
|
151 |
|
152 |
void SimInfo::scaleBox(double scale) { |
153 |
double theBox[3][3]; |
154 |
int i, j; |
155 |
|
156 |
cerr << "Scaling box by " << scale << "\n"; |
157 |
|
158 |
for(i=0; i<3; i++) |
159 |
for (j=0; j<3; j++) theBox[i][j] = Hmat[i][j]*scale; |
160 |
|
161 |
setBoxM(theBox); |
162 |
|
163 |
} |
164 |
|
165 |
void SimInfo::calcHmatInv( void ) { |
166 |
|
167 |
int i,j; |
168 |
double smallDiag; |
169 |
double tol; |
170 |
double sanity[3][3]; |
171 |
|
172 |
invertMat3( Hmat, HmatInv ); |
173 |
|
174 |
// Check the inverse to make sure it is sane: |
175 |
|
176 |
matMul3( Hmat, HmatInv, sanity ); |
177 |
|
178 |
// check to see if Hmat is orthorhombic |
179 |
|
180 |
smallDiag = Hmat[0][0]; |
181 |
if(smallDiag > Hmat[1][1]) smallDiag = Hmat[1][1]; |
182 |
if(smallDiag > Hmat[2][2]) smallDiag = Hmat[2][2]; |
183 |
tol = smallDiag * 1E-6; |
184 |
|
185 |
orthoRhombic = 1; |
186 |
|
187 |
for (i = 0; i < 3; i++ ) { |
188 |
for (j = 0 ; j < 3; j++) { |
189 |
if (i != j) { |
190 |
if (orthoRhombic) { |
191 |
if (Hmat[i][j] >= tol) orthoRhombic = 0; |
192 |
} |
193 |
} |
194 |
} |
195 |
} |
196 |
} |
197 |
|
198 |
double SimInfo::matDet3(double a[3][3]) { |
199 |
int i, j, k; |
200 |
double determinant; |
201 |
|
202 |
determinant = 0.0; |
203 |
|
204 |
for(i = 0; i < 3; i++) { |
205 |
j = (i+1)%3; |
206 |
k = (i+2)%3; |
207 |
|
208 |
determinant += a[0][i] * (a[1][j]*a[2][k] - a[1][k]*a[2][j]); |
209 |
} |
210 |
|
211 |
return determinant; |
212 |
} |
213 |
|
214 |
void SimInfo::invertMat3(double a[3][3], double b[3][3]) { |
215 |
|
216 |
int i, j, k, l, m, n; |
217 |
double determinant; |
218 |
|
219 |
determinant = matDet3( a ); |
220 |
|
221 |
if (determinant == 0.0) { |
222 |
sprintf( painCave.errMsg, |
223 |
"Can't invert a matrix with a zero determinant!\n"); |
224 |
painCave.isFatal = 1; |
225 |
simError(); |
226 |
} |
227 |
|
228 |
for (i=0; i < 3; i++) { |
229 |
j = (i+1)%3; |
230 |
k = (i+2)%3; |
231 |
for(l = 0; l < 3; l++) { |
232 |
m = (l+1)%3; |
233 |
n = (l+2)%3; |
234 |
|
235 |
b[l][i] = (a[j][m]*a[k][n] - a[j][n]*a[k][m]) / determinant; |
236 |
} |
237 |
} |
238 |
} |
239 |
|
240 |
void SimInfo::matMul3(double a[3][3], double b[3][3], double c[3][3]) { |
241 |
double r00, r01, r02, r10, r11, r12, r20, r21, r22; |
242 |
|
243 |
r00 = a[0][0]*b[0][0] + a[0][1]*b[1][0] + a[0][2]*b[2][0]; |
244 |
r01 = a[0][0]*b[0][1] + a[0][1]*b[1][1] + a[0][2]*b[2][1]; |
245 |
r02 = a[0][0]*b[0][2] + a[0][1]*b[1][2] + a[0][2]*b[2][2]; |
246 |
|
247 |
r10 = a[1][0]*b[0][0] + a[1][1]*b[1][0] + a[1][2]*b[2][0]; |
248 |
r11 = a[1][0]*b[0][1] + a[1][1]*b[1][1] + a[1][2]*b[2][1]; |
249 |
r12 = a[1][0]*b[0][2] + a[1][1]*b[1][2] + a[1][2]*b[2][2]; |
250 |
|
251 |
r20 = a[2][0]*b[0][0] + a[2][1]*b[1][0] + a[2][2]*b[2][0]; |
252 |
r21 = a[2][0]*b[0][1] + a[2][1]*b[1][1] + a[2][2]*b[2][1]; |
253 |
r22 = a[2][0]*b[0][2] + a[2][1]*b[1][2] + a[2][2]*b[2][2]; |
254 |
|
255 |
c[0][0] = r00; c[0][1] = r01; c[0][2] = r02; |
256 |
c[1][0] = r10; c[1][1] = r11; c[1][2] = r12; |
257 |
c[2][0] = r20; c[2][1] = r21; c[2][2] = r22; |
258 |
} |
259 |
|
260 |
void SimInfo::matVecMul3(double m[3][3], double inVec[3], double outVec[3]) { |
261 |
double a0, a1, a2; |
262 |
|
263 |
a0 = inVec[0]; a1 = inVec[1]; a2 = inVec[2]; |
264 |
|
265 |
outVec[0] = m[0][0]*a0 + m[0][1]*a1 + m[0][2]*a2; |
266 |
outVec[1] = m[1][0]*a0 + m[1][1]*a1 + m[1][2]*a2; |
267 |
outVec[2] = m[2][0]*a0 + m[2][1]*a1 + m[2][2]*a2; |
268 |
} |
269 |
|
270 |
void SimInfo::calcBoxL( void ){ |
271 |
|
272 |
double dx, dy, dz, dsq; |
273 |
int i; |
274 |
|
275 |
// boxVol = Determinant of Hmat |
276 |
|
277 |
boxVol = matDet3( Hmat ); |
278 |
|
279 |
// boxLx |
280 |
|
281 |
dx = Hmat[0][0]; dy = Hmat[1][0]; dz = Hmat[2][0]; |
282 |
dsq = dx*dx + dy*dy + dz*dz; |
283 |
boxLx = sqrt( dsq ); |
284 |
|
285 |
// boxLy |
286 |
|
287 |
dx = Hmat[0][1]; dy = Hmat[1][1]; dz = Hmat[2][1]; |
288 |
dsq = dx*dx + dy*dy + dz*dz; |
289 |
boxLy = sqrt( dsq ); |
290 |
|
291 |
// boxLz |
292 |
|
293 |
dx = Hmat[0][2]; dy = Hmat[1][2]; dz = Hmat[2][2]; |
294 |
dsq = dx*dx + dy*dy + dz*dz; |
295 |
boxLz = sqrt( dsq ); |
296 |
|
297 |
} |
298 |
|
299 |
|
300 |
void SimInfo::wrapVector( double thePos[3] ){ |
301 |
|
302 |
int i, j, k; |
303 |
double scaled[3]; |
304 |
|
305 |
if( !orthoRhombic ){ |
306 |
// calc the scaled coordinates. |
307 |
|
308 |
|
309 |
matVecMul3(HmatInv, thePos, scaled); |
310 |
|
311 |
for(i=0; i<3; i++) |
312 |
scaled[i] -= roundMe(scaled[i]); |
313 |
|
314 |
// calc the wrapped real coordinates from the wrapped scaled coordinates |
315 |
|
316 |
matVecMul3(Hmat, scaled, thePos); |
317 |
|
318 |
} |
319 |
else{ |
320 |
// calc the scaled coordinates. |
321 |
|
322 |
for(i=0; i<3; i++) |
323 |
scaled[i] = thePos[i]*HmatInv[i][i]; |
324 |
|
325 |
// wrap the scaled coordinates |
326 |
|
327 |
for(i=0; i<3; i++) |
328 |
scaled[i] -= roundMe(scaled[i]); |
329 |
|
330 |
// calc the wrapped real coordinates from the wrapped scaled coordinates |
331 |
|
332 |
for(i=0; i<3; i++) |
333 |
thePos[i] = scaled[i]*Hmat[i][i]; |
334 |
} |
335 |
|
336 |
} |
337 |
|
338 |
|
339 |
int SimInfo::getNDF(){ |
340 |
int ndf_local, ndf; |
341 |
|
342 |
ndf_local = 3 * n_atoms + 3 * n_oriented - n_constraints; |
343 |
|
344 |
#ifdef IS_MPI |
345 |
MPI_Allreduce(&ndf_local,&ndf,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD); |
346 |
#else |
347 |
ndf = ndf_local; |
348 |
#endif |
349 |
|
350 |
ndf = ndf - 3; |
351 |
|
352 |
return ndf; |
353 |
} |
354 |
|
355 |
int SimInfo::getNDFraw() { |
356 |
int ndfRaw_local, ndfRaw; |
357 |
|
358 |
// Raw degrees of freedom that we have to set |
359 |
ndfRaw_local = 3 * n_atoms + 3 * n_oriented; |
360 |
|
361 |
#ifdef IS_MPI |
362 |
MPI_Allreduce(&ndfRaw_local,&ndfRaw,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD); |
363 |
#else |
364 |
ndfRaw = ndfRaw_local; |
365 |
#endif |
366 |
|
367 |
return ndfRaw; |
368 |
} |
369 |
|
370 |
void SimInfo::refreshSim(){ |
371 |
|
372 |
simtype fInfo; |
373 |
int isError; |
374 |
int n_global; |
375 |
int* excl; |
376 |
|
377 |
fInfo.rrf = 0.0; |
378 |
fInfo.rt = 0.0; |
379 |
fInfo.dielect = 0.0; |
380 |
|
381 |
fInfo.rlist = rList; |
382 |
fInfo.rcut = rCut; |
383 |
|
384 |
if( useDipole ){ |
385 |
fInfo.rrf = ecr; |
386 |
fInfo.rt = ecr - est; |
387 |
if( useReactionField )fInfo.dielect = dielectric; |
388 |
} |
389 |
|
390 |
fInfo.SIM_uses_PBC = usePBC; |
391 |
//fInfo.SIM_uses_LJ = 0; |
392 |
fInfo.SIM_uses_LJ = useLJ; |
393 |
fInfo.SIM_uses_sticky = useSticky; |
394 |
//fInfo.SIM_uses_sticky = 0; |
395 |
fInfo.SIM_uses_dipoles = useDipole; |
396 |
//fInfo.SIM_uses_dipoles = 0; |
397 |
//fInfo.SIM_uses_RF = useReactionField; |
398 |
fInfo.SIM_uses_RF = 0; |
399 |
fInfo.SIM_uses_GB = useGB; |
400 |
fInfo.SIM_uses_EAM = useEAM; |
401 |
|
402 |
excl = Exclude::getArray(); |
403 |
|
404 |
#ifdef IS_MPI |
405 |
n_global = mpiSim->getTotAtoms(); |
406 |
#else |
407 |
n_global = n_atoms; |
408 |
#endif |
409 |
|
410 |
isError = 0; |
411 |
|
412 |
setFsimulation( &fInfo, &n_global, &n_atoms, identArray, &n_exclude, excl, |
413 |
&nGlobalExcludes, globalExcludes, molMembershipArray, |
414 |
&isError ); |
415 |
|
416 |
if( isError ){ |
417 |
|
418 |
sprintf( painCave.errMsg, |
419 |
"There was an error setting the simulation information in fortran.\n" ); |
420 |
painCave.isFatal = 1; |
421 |
simError(); |
422 |
} |
423 |
|
424 |
#ifdef IS_MPI |
425 |
sprintf( checkPointMsg, |
426 |
"succesfully sent the simulation information to fortran.\n"); |
427 |
MPIcheckPoint(); |
428 |
#endif // is_mpi |
429 |
|
430 |
this->ndf = this->getNDF(); |
431 |
this->ndfRaw = this->getNDFraw(); |
432 |
|
433 |
} |
434 |
|