NAMD
ComputeCrossterms.C
Go to the documentation of this file.
1 
7 #include "InfoStream.h"
8 #include "ComputeCrossterms.h"
9 #include "Molecule.h"
10 #include "Parameters.h"
11 #include "Node.h"
12 #include "ReductionMgr.h"
13 #include "Lattice.h"
14 #include "PressureProfile.h"
15 #include "Debug.h"
16 
17 #define FASTER
18 
19 enum {
23  CMAP_PHI_0 = -180,
24  CMAP_PSI_0 = -180
25 };
26 
27 #define INDEX(ncols,i,j) ((i)*ncols + (j))
28 
29 
30 // static initialization
35 
36 
38  (Molecule* mol, int* count, int32*** byatom, Crossterm** structarray)
39 {
40 #ifdef MEM_OPT_VERSION
41  NAMD_die("Should not be called in CrosstermElem::getMoleculePointers in memory optimized version!");
42 #else
43  *count = mol->numCrossterms;
44  *byatom = mol->crosstermsByAtom;
45  *structarray = mol->crossterms;
46 #endif
47 }
48 
50  *v = p->crossterm_array;
51 }
52 
53 void CrosstermElem::computeForce(CrosstermElem *tuples, int ntuple, BigReal *reduction,
54  BigReal *pressureProfileData)
55 {
56  const Lattice & lattice = tuples[0].p[0]->p->lattice;
57  //fepb BKR
59  const int step = tuples[0].p[0]->p->flags.step;
60  const BigReal alchLambda = simParams->getCurrentLambda(step);
61  const BigReal alchLambda2 = simParams->getCurrentLambda2(step);
62  const BigReal bond_lambda_1 = simParams->getBondLambda(alchLambda);
63  const BigReal bond_lambda_2 = simParams->getBondLambda(1-alchLambda);
64  const BigReal bond_lambda_12 = simParams->getBondLambda(alchLambda2);
65  const BigReal bond_lambda_22 = simParams->getBondLambda(1-alchLambda2);
66  Molecule *const mol = Node::Object()->molecule;
67  //fepe
68 
69  for ( int ituple=0; ituple<ntuple; ++ituple ) {
70  const CrosstermElem &tup = tuples[ituple];
71  enum { size = 8 };
72  const AtomID (&atomID)[size](tup.atomID);
73  const int (&localIndex)[size](tup.localIndex);
74  TuplePatchElem * const(&p)[size](tup.p);
75  const Real (&scale)(tup.scale);
76  const CrosstermValue * const(&value)(tup.value);
77 
78  DebugM(3, "::computeForce() localIndex = " << localIndex[0] << " "
79  << localIndex[1] << " " << localIndex[2] << " " <<
80  localIndex[3] << std::endl);
81 
82  // Vector r12, r23, r34; // vector between atoms
83  Vector A,B,C,D,E,F; // cross products
84  BigReal rA,rB,rC,rD,rE,rF; // length of vectors
85  BigReal energy=0; // energy from the angle
86  BigReal phi,psi; // angle between the plans
87  double cos_phi,cos_psi; // cos(phi)
88  double sin_phi,sin_psi; // sin(phi)
89  Vector dcosdA,dcosdD; // Derivative d(cos(phi))/dA
90  Vector dcosdB,dcosdE; // Derivative d(cos(phi))/dB
91  Vector dsindC,dsindF; // Derivative d(sin(phi))/dC
92  Vector dsindB,dsindE; // Derivative d(sin(phi))/dB
93  BigReal U,U_phi,U_psi; // energy constants
94  BigReal diff; // for periodicity
95  Force f1(0,0,0),f2(0,0,0),f3(0,0,0); // force components
96  Force f4(0,0,0),f5(0,0,0),f6(0,0,0); // force components
97 
98  //DebugM(3, "::computeForce() -- starting with crossterm type " << crosstermType << std::endl);
99 
100  // Calculate the vectors between atoms
101  const Position & pos0 = p[0]->x[localIndex[0]].position;
102  const Position & pos1 = p[1]->x[localIndex[1]].position;
103  const Position & pos2 = p[2]->x[localIndex[2]].position;
104  const Position & pos3 = p[3]->x[localIndex[3]].position;
105  const Position & pos4 = p[4]->x[localIndex[4]].position;
106  const Position & pos5 = p[5]->x[localIndex[5]].position;
107  const Position & pos6 = p[6]->x[localIndex[6]].position;
108  const Position & pos7 = p[7]->x[localIndex[7]].position;
109  const Vector r12 = lattice.delta(pos0,pos1);
110  const Vector r23 = lattice.delta(pos1,pos2);
111  const Vector r34 = lattice.delta(pos2,pos3);
112  const Vector r56 = lattice.delta(pos4,pos5);
113  const Vector r67 = lattice.delta(pos5,pos6);
114  const Vector r78 = lattice.delta(pos6,pos7);
115 
116  // Calculate the cross products
117  A = cross(r12,r23);
118  B = cross(r23,r34);
119  C = cross(r23,A);
120  D = cross(r56,r67);
121  E = cross(r67,r78);
122  F = cross(r67,D);
123 
124  // Calculate the distances
125  rA = A.length();
126  rB = B.length();
127  rC = C.length();
128  rD = D.length();
129  rE = E.length();
130  rF = F.length();
131 
132  // Calculate the sin and cos
133  cos_phi = A*B/(rA*rB);
134  sin_phi = C*B/(rC*rB);
135  cos_psi = D*E/(rD*rE);
136  sin_psi = F*E/(rF*rE);
137 
138  // Normalize B
139  rB = 1.0/rB;
140  B *= rB;
141  rE = 1.0/rE;
142  E *= rE;
143 
144  phi= -atan2(sin_phi,cos_phi);
145  psi= -atan2(sin_psi,cos_psi);
146 
147  if (fabs(sin_phi) > 0.1) {
148  // Normalize A
149  rA = 1.0/rA;
150  A *= rA;
151  dcosdA = rA*(cos_phi*A-B);
152  dcosdB = rB*(cos_phi*B-A);
153  } else {
154  // Normalize C
155  rC = 1.0/rC;
156  C *= rC;
157  dsindC = rC*(sin_phi*C-B);
158  dsindB = rB*(sin_phi*B-C);
159  }
160 
161  if (fabs(sin_psi) > 0.1) {
162  // Normalize A
163  rD = 1.0/rD;
164  D *= rD;
165  dcosdD = rD*(cos_psi*D-E);
166  dcosdE = rE*(cos_psi*E-D);
167  } else {
168  // Normalize C
169  rF = 1.0/rF;
170  F *= rF;
171  dsindF = rF*(sin_psi*F-E);
172  dsindE = rE*(sin_psi*E-F);
173  }
174 
175  // Calculate the energy
176 {
177  const double h = CMAP_SPACING * PI / 180.0;
178  const double h_1 = 1.0 / h;
179 #ifdef FASTER
180  const double six_h = 6.0 * h_1;
181 #endif
182  const double phi_0 = CMAP_PHI_0 * PI / 180.0;
183  const double psi_0 = CMAP_PSI_0 * PI / 180.0;
184 
185  enum { D = CMAP_TABLE_DIM };
186  double xa[2], xb[2], dxa[2], dxb[2];
187  double ya[2], yb[2], dya[2], dyb[2];
188  double t, dx_h, dy_h;
189 #ifdef FASTER
190  double s1, s2, s3, s4, s5;
191 #endif
192  double f = 0, fx = 0, fy = 0;
193  const CrosstermData *table = &value->c[0][0];
194  int i, j, ilo, jlo, ij;
195 
196  /* distance measured in grid points between angle and smallest value */
197  dx_h = (phi - phi_0) * h_1;
198  dy_h = (psi - psi_0) * h_1;
199 
200  /* find smallest numbered grid point in stencil */
201  ilo = (int) floor(dx_h);
202  if ( ilo < 0 ) ilo = 0;
203  if ( ilo >= CMAP_SETUP_DIM ) ilo = CMAP_SETUP_DIM - 1;
204  jlo = (int) floor(dy_h);
205  if ( jlo < 0 ) jlo = 0;
206  if ( jlo >= CMAP_SETUP_DIM ) jlo = CMAP_SETUP_DIM - 1;
207 
208 #if !defined(FASTER)
209 
210  /* find t for x-dimension and compute xa, xb, dxa, dxb */
211  t = dx_h - (double) ilo;
212  xa[0] = (1 - t) * (1 - t) * (1 + 2*t);
213  xb[0] = h * t * (1 - t) * (1 - t);
214  dxa[0] = -6 * t * (1 - t) * h_1;
215  dxb[0] = (1 - t) * (1 - 3*t);
216  t--;
217  xa[1] = (1 + t) * (1 + t) * (1 - 2*t);
218  xb[1] = h * t * (1 + t) * (1 + t);
219  dxa[1] = -6 * t * (1 + t) * h_1;
220  dxb[1] = (1 + t) * (1 + 3*t);
221 
222  /* find t for y-dimension and compute ya, yb, dya, dyb */
223  t = dy_h - (double) jlo;
224  ya[0] = (1 - t) * (1 - t) * (1 + 2*t);
225  yb[0] = h * t * (1 - t) * (1 - t);
226  dya[0] = -6 * t * (1 - t) * h_1;
227  dyb[0] = (1 - t) * (1 - 3*t);
228  t--;
229  ya[1] = (1 + t) * (1 + t) * (1 - 2*t);
230  yb[1] = h * t * (1 + t) * (1 + t);
231  dya[1] = -6 * t * (1 + t) * h_1;
232  dyb[1] = (1 + t) * (1 + 3*t);
233 
234 #else
235 
236  /* find t for x-dimension and compute xa, xb, dxa, dxb */
237  t = dx_h - (double) ilo;
238  s1 = 1-t;
239  s2 = 2*t;
240  s3 = 3*t;
241  s4 = t*s1;
242  s5 = h*s4;
243  xa[0] = s1*s1*(1+s2);
244  xa[1] = t*t*(3-s2);
245  xb[0] = s5*s1;
246  xb[1] = -s5*t;
247  dxa[0] = -six_h*s4;
248  dxa[1] = -dxa[0];
249  dxb[0] = s1*(1-s3);
250  dxb[1] = t*(-2+s3);
251 
252  /* find t for y-dimension and compute ya, yb, dya, dyb */
253  t = dy_h - (double) jlo;
254  s1 = 1-t;
255  s2 = 2*t;
256  s3 = 3*t;
257  s4 = t*s1;
258  s5 = h*s4;
259  ya[0] = s1*s1*(1+s2);
260  ya[1] = t*t*(3-s2);
261  yb[0] = s5*s1;
262  yb[1] = -s5*t;
263  dya[0] = -six_h*s4;
264  dya[1] = -dya[0];
265  dyb[0] = s1*(1-s3);
266  dyb[1] = t*(-2+s3);
267 
268 #endif
269 
270  for (i = 0; i < 2; i++) {
271  for (j = 0; j < 2; j++) {
272  ij = INDEX(D,i+ilo,j+jlo);
273 
274 #if !defined(FASTER)
275 
276  f += xa[i] * ya[j] * table[ij].d00
277  + xb[i] * ya[j] * table[ij].d10
278  + xa[i] * yb[j] * table[ij].d01
279  + xb[i] * yb[j] * table[ij].d11;
280 
281  fx += dxa[i] * ya[j] * table[ij].d00
282  + dxb[i] * ya[j] * table[ij].d10
283  + dxa[i] * yb[j] * table[ij].d01
284  + dxb[i] * yb[j] * table[ij].d11;
285 
286  fy += xa[i] * dya[j] * table[ij].d00
287  + xb[i] * dya[j] * table[ij].d10
288  + xa[i] * dyb[j] * table[ij].d01
289  + xb[i] * dyb[j] * table[ij].d11;
290 
291 #else
292 
293  s1=ya[j]*table[ij].d00;
294  s2=yb[j]*table[ij].d01;
295  s3=ya[j]*table[ij].d10;
296  s4=yb[j]*table[ij].d11;
297 
298  f+=xa[i]*(s1+s2)+xb[i]*(s3+s4);
299  fx+=dxa[i]*(s1+s2)+dxb[i]*(s3+s4);
300  fy+=xa[i]*(dya[j]*table[ij].d00+dyb[j]*table[ij].d01)
301  +xb[i]*(dya[j]*table[ij].d10+dyb[j]*table[ij].d11);
302 
303 #endif
304  }
305  }
306 
307  /* return accumulated values */
308  U = f * scale;
309  U_phi = fx * scale;
310  U_psi = fy * scale;
311 
312 /*
313 CkPrintf("crossterm %d-%d-%d-%d %d-%d-%d-%d %lf %lf %d %d %lf %lf %lf\n",
314  atomID[0], atomID[1], atomID[2], atomID[3],
315  atomID[4], atomID[5], atomID[6], atomID[7],
316  phi, psi, ilo, jlo, U, U_phi, U_psi);
317 CkPrintf("%d %d-%d-%d-%d %d-%d-%d-%d\n", CkMyPe(),
318  p[0]->patchID, p[1]->patchID, p[2]->patchID, p[3]->patchID,
319  p[4]->patchID, p[5]->patchID, p[6]->patchID, p[7]->patchID);
320 */
321 
322 }
323 
324  // Add the energy from this crossterm to the total energy
325  energy += U;
326 
327  // Next, we want to calculate the forces. In order
328  // to do that, we first need to figure out whether the
329  // sin or cos form will be more stable. For this,
330  // just look at the value of phi
331  if (fabs(sin_phi) > 0.1)
332  {
333  // use the sin version to avoid 1/cos terms
334  U_phi = U_phi/sin_phi;
335 
336  f1.x += U_phi*(r23.y*dcosdA.z - r23.z*dcosdA.y);
337  f1.y += U_phi*(r23.z*dcosdA.x - r23.x*dcosdA.z);
338  f1.z += U_phi*(r23.x*dcosdA.y - r23.y*dcosdA.x);
339 
340  f3.x += U_phi*(r23.z*dcosdB.y - r23.y*dcosdB.z);
341  f3.y += U_phi*(r23.x*dcosdB.z - r23.z*dcosdB.x);
342  f3.z += U_phi*(r23.y*dcosdB.x - r23.x*dcosdB.y);
343 
344  f2.x += U_phi*(r12.z*dcosdA.y - r12.y*dcosdA.z
345  + r34.y*dcosdB.z - r34.z*dcosdB.y);
346  f2.y += U_phi*(r12.x*dcosdA.z - r12.z*dcosdA.x
347  + r34.z*dcosdB.x - r34.x*dcosdB.z);
348  f2.z += U_phi*(r12.y*dcosdA.x - r12.x*dcosdA.y
349  + r34.x*dcosdB.y - r34.y*dcosdB.x);
350  }
351  else
352  {
353  // This angle is closer to 0 or 180 than it is to
354  // 90, so use the cos version to avoid 1/sin terms
355  U_phi = -U_phi/cos_phi;
356 
357  f1.x += U_phi*((r23.y*r23.y + r23.z*r23.z)*dsindC.x
358  - r23.x*r23.y*dsindC.y
359  - r23.x*r23.z*dsindC.z);
360  f1.y += U_phi*((r23.z*r23.z + r23.x*r23.x)*dsindC.y
361  - r23.y*r23.z*dsindC.z
362  - r23.y*r23.x*dsindC.x);
363  f1.z += U_phi*((r23.x*r23.x + r23.y*r23.y)*dsindC.z
364  - r23.z*r23.x*dsindC.x
365  - r23.z*r23.y*dsindC.y);
366 
367  f3 += cross(U_phi,dsindB,r23);
368 
369  f2.x += U_phi*(-(r23.y*r12.y + r23.z*r12.z)*dsindC.x
370  +(2.0*r23.x*r12.y - r12.x*r23.y)*dsindC.y
371  +(2.0*r23.x*r12.z - r12.x*r23.z)*dsindC.z
372  +dsindB.z*r34.y - dsindB.y*r34.z);
373  f2.y += U_phi*(-(r23.z*r12.z + r23.x*r12.x)*dsindC.y
374  +(2.0*r23.y*r12.z - r12.y*r23.z)*dsindC.z
375  +(2.0*r23.y*r12.x - r12.y*r23.x)*dsindC.x
376  +dsindB.x*r34.z - dsindB.z*r34.x);
377  f2.z += U_phi*(-(r23.x*r12.x + r23.y*r12.y)*dsindC.z
378  +(2.0*r23.z*r12.x - r12.z*r23.x)*dsindC.x
379  +(2.0*r23.z*r12.y - r12.z*r23.y)*dsindC.y
380  +dsindB.y*r34.x - dsindB.x*r34.y);
381  }
382 
383  if (fabs(sin_psi) > 0.1)
384  {
385  // use the sin version to avoid 1/cos terms
386  U_psi = U_psi/sin_psi;
387 
388  f4.x += U_psi*(r67.y*dcosdD.z - r67.z*dcosdD.y);
389  f4.y += U_psi*(r67.z*dcosdD.x - r67.x*dcosdD.z);
390  f4.z += U_psi*(r67.x*dcosdD.y - r67.y*dcosdD.x);
391 
392  f6.x += U_psi*(r67.z*dcosdE.y - r67.y*dcosdE.z);
393  f6.y += U_psi*(r67.x*dcosdE.z - r67.z*dcosdE.x);
394  f6.z += U_psi*(r67.y*dcosdE.x - r67.x*dcosdE.y);
395 
396  f5.x += U_psi*(r56.z*dcosdD.y - r56.y*dcosdD.z
397  + r78.y*dcosdE.z - r78.z*dcosdE.y);
398  f5.y += U_psi*(r56.x*dcosdD.z - r56.z*dcosdD.x
399  + r78.z*dcosdE.x - r78.x*dcosdE.z);
400  f5.z += U_psi*(r56.y*dcosdD.x - r56.x*dcosdD.y
401  + r78.x*dcosdE.y - r78.y*dcosdE.x);
402  }
403  else
404  {
405  // This angle is closer to 0 or 180 than it is to
406  // 90, so use the cos version to avoid 1/sin terms
407  U_psi = -U_psi/cos_psi;
408 
409  f4.x += U_psi*((r67.y*r67.y + r67.z*r67.z)*dsindF.x
410  - r67.x*r67.y*dsindF.y
411  - r67.x*r67.z*dsindF.z);
412  f4.y += U_psi*((r67.z*r67.z + r67.x*r67.x)*dsindF.y
413  - r67.y*r67.z*dsindF.z
414  - r67.y*r67.x*dsindF.x);
415  f4.z += U_psi*((r67.x*r67.x + r67.y*r67.y)*dsindF.z
416  - r67.z*r67.x*dsindF.x
417  - r67.z*r67.y*dsindF.y);
418 
419  f6 += cross(U_psi,dsindE,r67);
420 
421  f5.x += U_psi*(-(r67.y*r56.y + r67.z*r56.z)*dsindF.x
422  +(2.0*r67.x*r56.y - r56.x*r67.y)*dsindF.y
423  +(2.0*r67.x*r56.z - r56.x*r67.z)*dsindF.z
424  +dsindE.z*r78.y - dsindE.y*r78.z);
425  f5.y += U_psi*(-(r67.z*r56.z + r67.x*r56.x)*dsindF.y
426  +(2.0*r67.y*r56.z - r56.y*r67.z)*dsindF.z
427  +(2.0*r67.y*r56.x - r56.y*r67.x)*dsindF.x
428  +dsindE.x*r78.z - dsindE.z*r78.x);
429  f5.z += U_psi*(-(r67.x*r56.x + r67.y*r56.y)*dsindF.z
430  +(2.0*r67.z*r56.x - r56.z*r67.x)*dsindF.x
431  +(2.0*r67.z*r56.y - r56.z*r67.y)*dsindF.y
432  +dsindE.y*r78.x - dsindE.x*r78.y);
433  }
434 
435  //fepb - BKR scaling of alchemical bonded terms
436  // NB: TI derivative is the _unscaled_ energy.
437  if ( simParams->alchOn ) {
438  int typeSum1, typeSum2;
439  typeSum1 = typeSum2 = 0;
440  for (int i=0; i < 4; ++i ) {
441  typeSum1 += (mol->get_fep_type(atomID[i]) == 2 ? -1 :\
442  mol->get_fep_type(atomID[i]));
443  typeSum2 += (mol->get_fep_type(atomID[i+4]) == 2 ? -1 :\
444  mol->get_fep_type(atomID[i+4]));
445  }
446  int order = (simParams->alchBondDecouple ? 5 : 4);
447  if ( (0 < typeSum1 && typeSum1 < order) ||
448  (0 < typeSum2 && typeSum2 < order) ) {
449  reduction[crosstermEnergyIndex_ti_1] += energy;
450  reduction[crosstermEnergyIndex_f] += (bond_lambda_12 - bond_lambda_1)*energy;
451  energy *= bond_lambda_1;
452  f1 *= bond_lambda_1;
453  f2 *= bond_lambda_1;
454  f3 *= bond_lambda_1;
455  f4 *= bond_lambda_1;
456  f5 *= bond_lambda_1;
457  f6 *= bond_lambda_1;
458  } else if ( (0 > typeSum1 && typeSum1 > -order) ||
459  (0 > typeSum2 && typeSum2 > -order) ) {
460  reduction[crosstermEnergyIndex_ti_2] += energy;
461  reduction[crosstermEnergyIndex_f] += (bond_lambda_22 - bond_lambda_2)*energy;
462  energy *= bond_lambda_2;
463  f1 *= bond_lambda_2;
464  f2 *= bond_lambda_2;
465  f3 *= bond_lambda_2;
466  f4 *= bond_lambda_2;
467  f5 *= bond_lambda_2;
468  f6 *= bond_lambda_2;
469  }
470  }
471  //fepe
472 
473  /* store the forces */
474  p[0]->f[localIndex[0]] += f1;
475  p[1]->f[localIndex[1]] += f2 - f1;
476  p[2]->f[localIndex[2]] += f3 - f2;
477  p[3]->f[localIndex[3]] += -f3;
478  p[4]->f[localIndex[4]] += f4;
479  p[5]->f[localIndex[5]] += f5 - f4;
480  p[6]->f[localIndex[6]] += f6 - f5;
481  p[7]->f[localIndex[7]] += -f6;
482 
483  if ( p[0]->af ) {
484  p[0]->af[localIndex[0]] += f1;
485  p[1]->af[localIndex[1]] += f2 - f1;
486  p[2]->af[localIndex[2]] += f3 - f2;
487  p[3]->af[localIndex[3]] += -f3;
488  p[4]->af[localIndex[4]] += f4;
489  p[5]->af[localIndex[5]] += f5 - f4;
490  p[6]->af[localIndex[6]] += f6 - f5;
491  p[7]->af[localIndex[7]] += -f6;
492  }
493 
494  DebugM(3, "::computeForce() -- ending with delta energy " << energy << std::endl);
495  reduction[crosstermEnergyIndex] += energy;
496  reduction[virialIndex_XX] += ( f1.x * r12.x + f2.x * r23.x + f3.x * r34.x );
497  reduction[virialIndex_XY] += ( f1.x * r12.y + f2.x * r23.y + f3.x * r34.y );
498  reduction[virialIndex_XZ] += ( f1.x * r12.z + f2.x * r23.z + f3.x * r34.z );
499  reduction[virialIndex_YX] += ( f1.y * r12.x + f2.y * r23.x + f3.y * r34.x );
500  reduction[virialIndex_YY] += ( f1.y * r12.y + f2.y * r23.y + f3.y * r34.y );
501  reduction[virialIndex_YZ] += ( f1.y * r12.z + f2.y * r23.z + f3.y * r34.z );
502  reduction[virialIndex_ZX] += ( f1.z * r12.x + f2.z * r23.x + f3.z * r34.x );
503  reduction[virialIndex_ZY] += ( f1.z * r12.y + f2.z * r23.y + f3.z * r34.y );
504  reduction[virialIndex_ZZ] += ( f1.z * r12.z + f2.z * r23.z + f3.z * r34.z );
505 
506  reduction[virialIndex_XX] += ( f4.x * r56.x + f5.x * r67.x + f6.x * r78.x );
507  reduction[virialIndex_XY] += ( f4.x * r56.y + f5.x * r67.y + f6.x * r78.y );
508  reduction[virialIndex_XZ] += ( f4.x * r56.z + f5.x * r67.z + f6.x * r78.z );
509  reduction[virialIndex_YX] += ( f4.y * r56.x + f5.y * r67.x + f6.y * r78.x );
510  reduction[virialIndex_YY] += ( f4.y * r56.y + f5.y * r67.y + f6.y * r78.y );
511  reduction[virialIndex_YZ] += ( f4.y * r56.z + f5.y * r67.z + f6.y * r78.z );
512  reduction[virialIndex_ZX] += ( f4.z * r56.x + f5.z * r67.x + f6.z * r78.x );
513  reduction[virialIndex_ZY] += ( f4.z * r56.y + f5.z * r67.y + f6.z * r78.y );
514  reduction[virialIndex_ZZ] += ( f4.z * r56.z + f5.z * r67.z + f6.z * r78.z );
515 
516  if (pressureProfileData) {
517  BigReal z1 = p[0]->x[localIndex[0]].position.z;
518  BigReal z2 = p[1]->x[localIndex[1]].position.z;
519  BigReal z3 = p[2]->x[localIndex[2]].position.z;
520  BigReal z4 = p[3]->x[localIndex[3]].position.z;
521  BigReal z5 = p[4]->x[localIndex[4]].position.z;
522  BigReal z6 = p[5]->x[localIndex[5]].position.z;
523  BigReal z7 = p[6]->x[localIndex[6]].position.z;
524  BigReal z8 = p[7]->x[localIndex[7]].position.z;
525  int n1 = (int)floor((z1-pressureProfileMin)/pressureProfileThickness);
526  int n2 = (int)floor((z2-pressureProfileMin)/pressureProfileThickness);
527  int n3 = (int)floor((z3-pressureProfileMin)/pressureProfileThickness);
528  int n4 = (int)floor((z4-pressureProfileMin)/pressureProfileThickness);
529  int n5 = (int)floor((z5-pressureProfileMin)/pressureProfileThickness);
530  int n6 = (int)floor((z6-pressureProfileMin)/pressureProfileThickness);
531  int n7 = (int)floor((z7-pressureProfileMin)/pressureProfileThickness);
532  int n8 = (int)floor((z8-pressureProfileMin)/pressureProfileThickness);
541  int p1 = p[0]->x[localIndex[0]].partition;
542  int p2 = p[1]->x[localIndex[1]].partition;
543  int p3 = p[2]->x[localIndex[2]].partition;
544  int p4 = p[3]->x[localIndex[3]].partition;
545  int p5 = p[4]->x[localIndex[4]].partition;
546  int p6 = p[5]->x[localIndex[5]].partition;
547  int p7 = p[6]->x[localIndex[6]].partition;
548  int p8 = p[7]->x[localIndex[7]].partition;
549  int pn = pressureProfileAtomTypes;
550  pp_reduction(pressureProfileSlabs, n1, n2, p1, p2, pn,
551  f1.x * r12.x, f1.y * r12.y, f1.z * r12.z,
552  pressureProfileData);
553  pp_reduction(pressureProfileSlabs, n2, n3, p2, p3, pn,
554  f2.x * r23.x, f2.y * r23.y, f2.z * r23.z,
555  pressureProfileData);
556  pp_reduction(pressureProfileSlabs, n3, n4, p3, p4, pn,
557  f3.x * r34.x, f3.y * r34.y, f3.z * r34.z,
558  pressureProfileData);
559  pp_reduction(pressureProfileSlabs, n5, n6, p5, p6, pn,
560  f4.x * r56.x, f4.y * r56.y, f4.z * r56.z,
561  pressureProfileData);
562  pp_reduction(pressureProfileSlabs, n6, n7, p6, p7, pn,
563  f5.x * r67.x, f5.y * r67.y, f5.z * r67.z,
564  pressureProfileData);
565  pp_reduction(pressureProfileSlabs, n7, n8, p7, p8, pn,
566  f6.x * r78.x, f6.y * r78.y, f6.z * r78.z,
567  pressureProfileData);
568  }
569 
570  }
571 }
572 
574 {
579  ADD_TENSOR(reduction,REDUCTION_VIRIAL_NORMAL,data,virialIndex);
580  ADD_TENSOR(reduction,REDUCTION_VIRIAL_AMD_DIHE,data,virialIndex);
581 }
582 
583 
584 /******************************************************************************/
585 
586 static void lu_decomp_nopivot(double *m, int n);
587 static void forward_back_sub(double *b, double *m, int n);
588 
590 {
591  enum { D = CMAP_TABLE_DIM };
592  enum { N = CMAP_SETUP_DIM };
593  const double h_1 = 1.0 / ( CMAP_SPACING * PI / 180.0) ;
594  const double h_2 = h_1 * h_1;
595  const double tr_h = 3.0 * h_1;
596  int i, j;
597  int ij;
598  int ijp1p1, ijm1m1, ijp1m1, ijm1p1;
599  int ijp2p2, ijm2m2, ijp2m2, ijm2p2;
600 
601  /* allocate spline coefficient matrix */
602  double* const m = new double[N*N];
603  memset(m,0,N*N*sizeof(double));
604 
605  /* initialize spline coefficient matrix */
606  m[0] = 4;
607  for (i = 1; i < N; i++) {
608  m[INDEX(N,i-1,i)] = 1;
609  m[INDEX(N,i,i-1)] = 1;
610  m[INDEX(N,i,i)] = 4;
611  }
612  /* periodic boundary conditions for spline */
613  m[INDEX(N,0,N-1)] = 1;
614  m[INDEX(N,N-1,0)] = 1;
615 
616  /* compute LU-decomposition for this matrix */
617  lu_decomp_nopivot(m, N);
618 
619  /* allocate vector for solving spline derivatives */
620  double* const v = new double[N];
621  memset(v,0,N*sizeof(double));
622 
623  /* march through rows of table */
624  for (i = 0; i < N; i++) {
625 
626  /* setup RHS vector for solving spline derivatives */
627  v[0] = tr_h * (table[INDEX(D,i,1)].d00 - table[INDEX(D,i,N-1)].d00);
628  for (j = 1; j < N; j++) {
629  v[j] = tr_h * (table[INDEX(D,i,j+1)].d00 - table[INDEX(D,i,j-1)].d00);
630  }
631 
632  /* solve system, returned into vector */
633  forward_back_sub(v, m, N);
634 
635  /* store values as derivatives wrt differenced table values */
636  for (j = 0; j < N; j++) {
637  table[INDEX(D,i,j)].d01 = v[j];
638  }
639  table[INDEX(D,i,N)].d01 = v[0];
640  }
641  for (j = 0; j <= N; j++) {
642  table[INDEX(D,N,j)].d01 = table[INDEX(D,0,j)].d01;
643  }
644 
645  /* march through columns of table */
646  for (j = 0; j < N; j++) {
647 
648  /* setup RHS vector for solving spline derivatives */
649  v[0] = tr_h * (table[INDEX(D,1,j)].d00 - table[INDEX(D,N-1,j)].d00);
650  for (i = 1; i < N; i++) {
651  v[i] = tr_h * (table[INDEX(D,i+1,j)].d00 - table[INDEX(D,i-1,j)].d00);
652  }
653 
654  /* solve system, returned into vector */
655  forward_back_sub(v, m, N);
656 
657  /* store values as derivatives wrt differenced table values */
658  for (i = 0; i < N; i++) {
659  table[INDEX(D,i,j)].d10 = v[i];
660  }
661  table[INDEX(D,N,j)].d10 = v[0];
662  }
663  for (i = 0; i <= N; i++) {
664  table[INDEX(D,i,N)].d10 = table[INDEX(D,i,0)].d10;
665  }
666 
667  /* march back through rows of table
668  *
669  * This is CHARMM's approach for calculating mixed partial derivatives,
670  * by splining the first derivative values.
671  *
672  * Here we spline the dx values along y to calculate dxdy derivatives.
673  *
674  * Test cases show error with CHARMM is within 1e-5 roundoff error.
675  */
676  for (i = 0; i < N; i++) {
677 
678  /* setup RHS vector for solving dxy derivatives from dx */
679  v[0] = tr_h * (table[INDEX(D,i,1)].d10 - table[INDEX(D,i,N-1)].d10);
680  for (j = 1; j < N; j++) {
681  v[j] = tr_h * (table[INDEX(D,i,j+1)].d10 - table[INDEX(D,i,j-1)].d10);
682  }
683 
684  /* solve system, returned into vector */
685  forward_back_sub(v, m, N);
686 
687  /* store values as dxy derivatives wrt differenced table values */
688  for (j = 0; j < N; j++) {
689  table[INDEX(D,i,j)].d11 = v[j];
690  }
691  table[INDEX(D,i,N)].d11 = v[0];
692  }
693  for (j = 0; j <= N; j++) {
694  table[INDEX(D,N,j)].d11 = table[INDEX(D,0,j)].d11;
695  }
696 
697  /* done with temp storage */
698  delete [] m;
699  delete [] v;
700 
701 }
702 
703 
704 void lu_decomp_nopivot(double *m, int n)
705 {
706  double l_ik;
707  int i, j, k;
708 
709  for (k = 0; k < n-1; k++) {
710  for (i = k+1; i < n; i++) {
711  l_ik = m[INDEX(n,i,k)] / m[INDEX(n,k,k)];
712  for (j = k; j < n; j++) {
713  m[INDEX(n,i,j)] -= l_ik * m[INDEX(n,k,j)];
714  }
715  m[INDEX(n,i,k)] = l_ik;
716  }
717  }
718 }
719 
720 
721 void forward_back_sub(double *b, double *m, int n)
722 {
723  int i, j;
724 
725  /* in place forward elimination (solves Ly=b) using lower triangle of m */
726  for (j = 0; j < n-1; j++) {
727  for (i = j+1; i < n; i++) {
728  b[i] -= m[INDEX(n,i,j)] * b[j];
729  }
730  }
731  /* in place back substitution (solves Ux=y) using upper triangle of m */
732  for (j = n-1; j >= 0; j--) {
733  b[j] /= m[INDEX(n,j,j)];
734  for (i = j-1; i >= 0; i--) {
735  b[i] -= m[INDEX(n,i,j)] * b[j];
736  }
737  }
738 }
739 
740 
static Node * Object()
Definition: Node.h:86
unsigned char partition
Definition: NamdTypes.h:56
const CrosstermValue * value
CrosstermData c[dim][dim]
Definition: Parameters.h:122
short int32
Definition: dumpdcd.c:24
int AtomID
Definition: NamdTypes.h:29
Lattice & lattice
Definition: Patch.h:126
#define ADD_TENSOR(R, RL, D, DL)
Definition: ReductionMgr.h:32
void pp_reduction(int nslabs, int n1, int n2, int atype1, int atype2, int numtypes, BigReal vxx, BigReal vyy, BigReal vzz, BigReal *reduction)
const BigReal A
Definition: Vector.h:64
static void forward_back_sub(double *b, double *m, int n)
SimParameters * simParameters
Definition: Node.h:178
AtomID atomID[size]
#define INDEX(ncols, i, j)
float Real
Definition: common.h:107
BigReal & item(int i)
Definition: ReductionMgr.h:312
#define DebugM(x, y)
Definition: Debug.h:59
__device__ __forceinline__ float3 cross(const float3 v1, const float3 v2)
BigReal z
Definition: Vector.h:66
Position position
Definition: NamdTypes.h:53
static int pressureProfileSlabs
static void submitReductionData(BigReal *, SubmitReduction *)
static void lu_decomp_nopivot(double *m, int n)
BigReal length(void) const
Definition: Vector.h:169
CrosstermValue * crossterm_array
Definition: Parameters.h:245
static int pressureProfileAtomTypes
Flags flags
Definition: Patch.h:127
BigReal d11
Definition: Parameters.h:117
static void getParameterPointers(Parameters *, const CrosstermValue **)
#define PI
Definition: common.h:81
TuplePatchElem * p[size]
void pp_clamp(int &n, int nslabs)
#define order
Definition: PmeRealSpace.C:235
static BigReal pressureProfileMin
BigReal getBondLambda(const BigReal)
BigReal getCurrentLambda2(const int)
Vector delta(const Position &pos1, const Position &pos2) const
Definition: Lattice.h:144
int localIndex[size]
BigReal d00
Definition: Parameters.h:117
BigReal x
Definition: Vector.h:66
int numCrossterms
Definition: Molecule.h:567
void NAMD_die(const char *err_msg)
Definition: common.C:83
static void getMoleculePointers(Molecule *, int *, int32 ***, Crossterm **)
unsigned char get_fep_type(int anum) const
Definition: Molecule.h:1345
static void computeForce(CrosstermElem *, int, BigReal *, BigReal *)
#define simParams
Definition: Output.C:127
BigReal d01
Definition: Parameters.h:117
void crossterm_setup(CrosstermData *table)
BigReal y
Definition: Vector.h:66
const BigReal B
BigReal getCurrentLambda(const int)
Molecule * molecule
Definition: Node.h:176
BigReal d10
Definition: Parameters.h:117
double BigReal
Definition: common.h:112
static BigReal pressureProfileThickness
int step
Definition: PatchTypes.h:16