NAMD
ComputeDihedrals.C
Go to the documentation of this file.
1 
7 #include "InfoStream.h"
8 #include "ComputeDihedrals.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 
18 // static initialization
23 
25  (Molecule* mol, int* count, int32*** byatom, Dihedral** structarray)
26 {
27 #ifdef MEM_OPT_VERSION
28  NAMD_die("Should not be called in DihedralElem::getMoleculePointers in memory optimized version!");
29 #else
30  *count = mol->numDihedrals;
31  *byatom = mol->dihedralsByAtom;
32  *structarray = mol->dihedrals;
33 #endif
34 }
35 
37  *v = p->dihedral_array;
38 }
39 
40 void DihedralElem::computeForce(DihedralElem *tuples, int ntuple, BigReal *reduction,
41  BigReal *pressureProfileData)
42 {
43  const Lattice & lattice = tuples[0].p[0]->p->lattice;
44 
45  //fepb BKR
47  const int step = tuples[0].p[0]->p->flags.step;
48  const BigReal alchLambda = simParams->getCurrentLambda(step);
49  const BigReal alchLambda2 = simParams->getCurrentLambda2(step);
50  const BigReal bond_lambda_1 = simParams->getBondLambda(alchLambda);
51  const BigReal bond_lambda_2 = simParams->getBondLambda(1-alchLambda);
52  const BigReal bond_lambda_12 = simParams->getBondLambda(alchLambda2);
53  const BigReal bond_lambda_22 = simParams->getBondLambda(1-alchLambda2);
54  Molecule *const mol = Node::Object()->molecule;
55  //fepe
56 
57  for ( int ituple=0; ituple<ntuple; ++ituple ) {
58  const DihedralElem &tup = tuples[ituple];
59  enum { size = 4 };
60  const AtomID (&atomID)[size](tup.atomID);
61  const int (&localIndex)[size](tup.localIndex);
62  TuplePatchElem * const(&p)[size](tup.p);
63  const Real (&scale)(tup.scale);
64  const DihedralValue * const(&value)(tup.value);
65 
66 #if defined(DEBUG_PROTOCELL)
67  if ((PRMIN <= atomID[0] && atomID[0] <= PRMAX) &&
68  (PRMIN <= atomID[1] && atomID[1] <= PRMAX) &&
69  (PRMIN <= atomID[2] && atomID[2] <= PRMAX) &&
70  (PRMIN <= atomID[3] && atomID[3] <= PRMAX)) {
71  int i = atomID[0];
72  int j = atomID[1];
73  int k = atomID[2];
74  int l = atomID[3];
75  if (atomID[3] < atomID[0]) {
76  i = atomID[3];
77  j = atomID[2];
78  k = atomID[1];
79  l = atomID[0];
80  }
81  int mult = value->multiplicity;
82  CkPrintf("%11d %11d %11d %11d mult=%d", i, j, k, l, mult);
83  for (int m=0; m < mult; m++) {
84  double kdelta = value->values[m].k;
85  double delta = value->values[m].delta;
86  int n = value->values[m].n;
87  CkPrintf(" k=%g delta=%g n=%d\n", kdelta, delta, n);
88  }
89  }
90 #endif
91 
92  DebugM(3, "::computeForce() localIndex = " << localIndex[0] << " "
93  << localIndex[1] << " " << localIndex[2] << std::endl);
94 
95  // Calculate the vectors between atoms
96  const Position & pos0 = p[0]->x[localIndex[0]].position;
97  const Position & pos1 = p[1]->x[localIndex[1]].position;
98  const Vector r12 = lattice.delta(pos0,pos1);
99  const Position & pos2 = p[2]->x[localIndex[2]].position;
100  const Vector r23 = lattice.delta(pos1,pos2);
101  const Position & pos3 = p[3]->x[localIndex[3]].position;
102  const Vector r34 = lattice.delta(pos2,pos3);
103 
104  // Calculate the cross products and distances
105  Vector A = cross(r12,r23);
106  register BigReal rAinv = A.rlength();
107  Vector B = cross(r23,r34);
108  register BigReal rBinv = B.rlength();
109  Vector C = cross(r23,A);
110  register BigReal rCinv = C.rlength();
111 
112  // Calculate the sin and cos
113  BigReal cos_phi = (A*B)*(rAinv*rBinv);
114  BigReal sin_phi = (C*B)*(rCinv*rBinv);
115 
116  BigReal phi= -atan2(sin_phi,cos_phi);
117 
118  BigReal K=0; // energy
119  BigReal K1=0; // force
120 
121  // get the dihedral information
122  int multiplicity = value->multiplicity;
123 
124  // Loop through the multiple parameter sets for this
125  // bond. We will only loop more than once if this
126  // has multiple parameter sets from Charmm22
127  for (int mult_num=0; mult_num<multiplicity; mult_num++)
128  {
129  /* get angle information */
130  Real k = value->values[mult_num].k * scale;
131  Real delta = value->values[mult_num].delta;
132  int n = value->values[mult_num].n;
133 
134  // Calculate the energy
135  if (n)
136  {
137  // Periodicity is greater than 0, so use cos form
138  K += k*(1+cos(n*phi - delta));
139  K1 += -n*k*sin(n*phi - delta);
140  }
141  else
142  {
143  // Periodicity is 0, so just use the harmonic form
144  BigReal diff = phi-delta;
145  if (diff < -PI) diff += TWOPI;
146  else if (diff > PI) diff -= TWOPI;
147 
148  K += k*diff*diff;
149  K1 += 2.0*k*diff;
150  }
151  } /* for multiplicity */
152 
153  //fepb - BKR scaling of alchemical bonded terms
154  // NB: TI derivative is the _unscaled_ energy.
155  if ( simParams->alchOn && !simParams->singleTopology) {
156  switch ( mol->get_fep_bonded_type(atomID, 4) ) {
157  case 1:
158  reduction[dihedralEnergyIndex_ti_1] += K;
159  reduction[dihedralEnergyIndex_f] += (bond_lambda_12 - bond_lambda_1)*K;
160  K *= bond_lambda_1;
161  K1 *= bond_lambda_1;
162  break;
163  case 2:
164  reduction[dihedralEnergyIndex_ti_2] += K;
165  reduction[dihedralEnergyIndex_f] += (bond_lambda_22 - bond_lambda_2)*K;
166  K *= bond_lambda_2;
167  K1 *= bond_lambda_2;
168  break;
169  }
170  }
171  //fepe
172 
173  Force f1,f2,f3;
174 
175  // Normalize B
176  //rB = 1.0/rB;
177  B *= rBinv;
178 
179  // Next, we want to calculate the forces. In order
180  // to do that, we first need to figure out whether the
181  // sin or cos form will be more stable. For this,
182  // just look at the value of phi
183  if (fabs(sin_phi) > 0.1)
184  {
185  // use the sin version to avoid 1/cos terms
186 
187  // Normalize A
188  A *= rAinv;
189  Vector dcosdA;
190  Vector dcosdB;
191 
192  dcosdA.x = rAinv*(cos_phi*A.x-B.x);
193  dcosdA.y = rAinv*(cos_phi*A.y-B.y);
194  dcosdA.z = rAinv*(cos_phi*A.z-B.z);
195 
196  dcosdB.x = rBinv*(cos_phi*B.x-A.x);
197  dcosdB.y = rBinv*(cos_phi*B.y-A.y);
198  dcosdB.z = rBinv*(cos_phi*B.z-A.z);
199 
200  K1 = K1/sin_phi;
201 
202  f1.x = K1*(r23.y*dcosdA.z - r23.z*dcosdA.y);
203  f1.y = K1*(r23.z*dcosdA.x - r23.x*dcosdA.z);
204  f1.z = K1*(r23.x*dcosdA.y - r23.y*dcosdA.x);
205 
206  f3.x = K1*(r23.z*dcosdB.y - r23.y*dcosdB.z);
207  f3.y = K1*(r23.x*dcosdB.z - r23.z*dcosdB.x);
208  f3.z = K1*(r23.y*dcosdB.x - r23.x*dcosdB.y);
209 
210  f2.x = K1*(r12.z*dcosdA.y - r12.y*dcosdA.z
211  + r34.y*dcosdB.z - r34.z*dcosdB.y);
212  f2.y = K1*(r12.x*dcosdA.z - r12.z*dcosdA.x
213  + r34.z*dcosdB.x - r34.x*dcosdB.z);
214  f2.z = K1*(r12.y*dcosdA.x - r12.x*dcosdA.y
215  + r34.x*dcosdB.y - r34.y*dcosdB.x);
216  }
217  else
218  {
219  // This angle is closer to 0 or 180 than it is to
220  // 90, so use the cos version to avoid 1/sin terms
221 
222  // Normalize C
223  // rC = 1.0/rC;
224  C *= rCinv;
225 
226  Vector dsindC;
227  Vector dsindB;
228 
229  dsindC.x = rCinv*(sin_phi*C.x-B.x);
230  dsindC.y = rCinv*(sin_phi*C.y-B.y);
231  dsindC.z = rCinv*(sin_phi*C.z-B.z);
232 
233  dsindB.x = rBinv*(sin_phi*B.x-C.x);
234  dsindB.y = rBinv*(sin_phi*B.y-C.y);
235  dsindB.z = rBinv*(sin_phi*B.z-C.z);
236 
237  K1 = -K1/cos_phi;
238 
239  f1.x = K1*((r23.y*r23.y + r23.z*r23.z)*dsindC.x
240  - r23.x*r23.y*dsindC.y
241  - r23.x*r23.z*dsindC.z);
242  f1.y = K1*((r23.z*r23.z + r23.x*r23.x)*dsindC.y
243  - r23.y*r23.z*dsindC.z
244  - r23.y*r23.x*dsindC.x);
245  f1.z = K1*((r23.x*r23.x + r23.y*r23.y)*dsindC.z
246  - r23.z*r23.x*dsindC.x
247  - r23.z*r23.y*dsindC.y);
248 
249  f3 = cross(K1,dsindB,r23);
250 
251  f2.x = K1*(-(r23.y*r12.y + r23.z*r12.z)*dsindC.x
252  +(2.0*r23.x*r12.y - r12.x*r23.y)*dsindC.y
253  +(2.0*r23.x*r12.z - r12.x*r23.z)*dsindC.z
254  +dsindB.z*r34.y - dsindB.y*r34.z);
255  f2.y = K1*(-(r23.z*r12.z + r23.x*r12.x)*dsindC.y
256  +(2.0*r23.y*r12.z - r12.y*r23.z)*dsindC.z
257  +(2.0*r23.y*r12.x - r12.y*r23.x)*dsindC.x
258  +dsindB.x*r34.z - dsindB.z*r34.x);
259  f2.z = K1*(-(r23.x*r12.x + r23.y*r12.y)*dsindC.z
260  +(2.0*r23.z*r12.x - r12.z*r23.x)*dsindC.x
261  +(2.0*r23.z*r12.y - r12.z*r23.y)*dsindC.y
262  +dsindB.y*r34.x - dsindB.x*r34.y);
263  }
264 
265  /* store the forces */
266  // p[0]->f[localIndex[0]] += f1;
267  // p[1]->f[localIndex[1]] += f2 - f1;
268  // p[2]->f[localIndex[2]] += f3 - f2;
269  // p[3]->f[localIndex[3]] += -f3;
270 
271  p[0]->f[localIndex[0]].x += f1.x;
272  p[0]->f[localIndex[0]].y += f1.y;
273  p[0]->f[localIndex[0]].z += f1.z;
274 
275  p[1]->f[localIndex[1]].x += f2.x - f1.x;
276  p[1]->f[localIndex[1]].y += f2.y - f1.y;
277  p[1]->f[localIndex[1]].z += f2.z - f1.z;
278 
279  p[2]->f[localIndex[2]].x += f3.x - f2.x;
280  p[2]->f[localIndex[2]].y += f3.y - f2.y;
281  p[2]->f[localIndex[2]].z += f3.z - f2.z;
282 
283  p[3]->f[localIndex[3]].x += -f3.x;
284  p[3]->f[localIndex[3]].y += -f3.y;
285  p[3]->f[localIndex[3]].z += -f3.z;
286 
287  /* store the force for dihedral-only accelMD */
288  if ( p[0]->af ) {
289  p[0]->af[localIndex[0]].x += f1.x;
290  p[0]->af[localIndex[0]].y += f1.y;
291  p[0]->af[localIndex[0]].z += f1.z;
292 
293  p[1]->af[localIndex[1]].x += f2.x - f1.x;
294  p[1]->af[localIndex[1]].y += f2.y - f1.y;
295  p[1]->af[localIndex[1]].z += f2.z - f1.z;
296 
297  p[2]->af[localIndex[2]].x += f3.x - f2.x;
298  p[2]->af[localIndex[2]].y += f3.y - f2.y;
299  p[2]->af[localIndex[2]].z += f3.z - f2.z;
300 
301  p[3]->af[localIndex[3]].x += -f3.x;
302  p[3]->af[localIndex[3]].y += -f3.y;
303  p[3]->af[localIndex[3]].z += -f3.z;
304  }
305 
306  DebugM(3, "::computeForce() -- ending with delta energy " << K << std::endl);
307  reduction[dihedralEnergyIndex] += K;
308  reduction[virialIndex_XX] += ( f1.x * r12.x + f2.x * r23.x + f3.x * r34.x );
309  reduction[virialIndex_XY] += ( f1.x * r12.y + f2.x * r23.y + f3.x * r34.y );
310  reduction[virialIndex_XZ] += ( f1.x * r12.z + f2.x * r23.z + f3.x * r34.z );
311  reduction[virialIndex_YX] += ( f1.y * r12.x + f2.y * r23.x + f3.y * r34.x );
312  reduction[virialIndex_YY] += ( f1.y * r12.y + f2.y * r23.y + f3.y * r34.y );
313  reduction[virialIndex_YZ] += ( f1.y * r12.z + f2.y * r23.z + f3.y * r34.z );
314  reduction[virialIndex_ZX] += ( f1.z * r12.x + f2.z * r23.x + f3.z * r34.x );
315  reduction[virialIndex_ZY] += ( f1.z * r12.y + f2.z * r23.y + f3.z * r34.y );
316  reduction[virialIndex_ZZ] += ( f1.z * r12.z + f2.z * r23.z + f3.z * r34.z );
317 
318  if (pressureProfileData) {
319  BigReal z1 = p[0]->x[localIndex[0]].position.z;
320  BigReal z2 = p[1]->x[localIndex[1]].position.z;
321  BigReal z3 = p[2]->x[localIndex[2]].position.z;
322  BigReal z4 = p[3]->x[localIndex[3]].position.z;
323  int n1 = (int)floor((z1-pressureProfileMin)/pressureProfileThickness);
324  int n2 = (int)floor((z2-pressureProfileMin)/pressureProfileThickness);
325  int n3 = (int)floor((z3-pressureProfileMin)/pressureProfileThickness);
326  int n4 = (int)floor((z4-pressureProfileMin)/pressureProfileThickness);
331  int p1 = p[0]->x[localIndex[0]].partition;
332  int p2 = p[1]->x[localIndex[1]].partition;
333  int p3 = p[2]->x[localIndex[2]].partition;
334  int p4 = p[3]->x[localIndex[3]].partition;
335  int pn = pressureProfileAtomTypes;
337  p1, p2, pn,
338  f1.x * r12.x, f1.y * r12.y, f1.z * r12.z,
339  pressureProfileData);
341  p2, p3, pn,
342  f2.x * r23.x, f2.y * r23.y, f2.z * r23.z,
343  pressureProfileData);
345  p3, p4, pn,
346  f3.x * r34.x, f3.y * r34.y, f3.z * r34.z,
347  pressureProfileData);
348  }
349 
350  }
351 }
352 
353 
355 {
360  ADD_TENSOR(reduction,REDUCTION_VIRIAL_NORMAL,data,virialIndex);
361  ADD_TENSOR(reduction,REDUCTION_VIRIAL_AMD_DIHE,data,virialIndex);
362 }
363 
static Node * Object()
Definition: Node.h:86
Lattice & lattice
Definition: Patch.h:127
#define ADD_TENSOR(R, RL, D, DL)
Definition: ReductionMgr.h:33
void pp_reduction(int nslabs, int n1, int n2, int atype1, int atype2, int numtypes, BigReal vxx, BigReal vyy, BigReal vzz, BigReal *reduction)
Definition: Vector.h:72
SimParameters * simParameters
Definition: Node.h:181
float Real
Definition: common.h:118
int32_t int32
Definition: common.h:38
BigReal & item(int i)
Definition: ReductionMgr.h:313
#define DebugM(x, y)
Definition: Debug.h:75
BigReal z
Definition: Vector.h:74
Position position
Definition: NamdTypes.h:77
static void computeForce(DihedralElem *, int, BigReal *, BigReal *)
static BigReal pressureProfileMin
int localIndex[size]
Molecule stores the structural information for the system.
Definition: Molecule.h:175
Flags flags
Definition: Patch.h:128
FourBodyConsts values[MAX_MULTIPLICITY]
Definition: Parameters.h:128
static int pressureProfileAtomTypes
#define PI
Definition: common.h:92
void pp_clamp(int &n, int nslabs)
static BigReal pressureProfileThickness
const DihedralValue * value
AtomID atomID[size]
uint8 partition
Definition: NamdTypes.h:80
BigReal x
Definition: Vector.h:74
int get_fep_bonded_type(const int *atomID, unsigned int order) const
Definition: Molecule.h:1477
void NAMD_die(const char *err_msg)
Definition: common.C:147
int numDihedrals
Definition: Molecule.h:590
#define TWOPI
Definition: common.h:96
#define simParams
Definition: Output.C:129
int32 AtomID
Definition: NamdTypes.h:35
BigReal y
Definition: Vector.h:74
static void submitReductionData(BigReal *, SubmitReduction *)
static int pressureProfileSlabs
NAMD_HOST_DEVICE BigReal rlength(void) const
Definition: Vector.h:210
TuplePatchElem * p[size]
Molecule * molecule
Definition: Node.h:179
static void getParameterPointers(Parameters *, const DihedralValue **)
static void getMoleculePointers(Molecule *, int *, int32 ***, Dihedral **)
double BigReal
Definition: common.h:123
NAMD_HOST_DEVICE Vector delta(const Position &pos1, const Position &pos2) const
Definition: Lattice.h:149
int step
Definition: PatchTypes.h:16