NAMD
ComputeRestraints.C
Go to the documentation of this file.
1 
7 #include "ComputeRestraints.h"
8 #include "Node.h"
9 #include "Molecule.h"
10 #include "SimParameters.h"
11 #include "Patch.h"
12 #include "NamdOneTools.h"
13 
14 /************************************************************************/
15 /* */
16 /* FUNCTION ComputeRestraints */
17 /* */
18 /************************************************************************/
19 
21  : ComputeHomePatch(c,pid)
22 {
24 
26 
27  // Get parameters from the SimParameters object
28  consExp = simParams->constraintExp;
29 
30  //****** BEGIN selective restraints (X,Y,Z) changes
31  consSelectOn = simParams->selectConstraintsOn;
32  if (consSelectOn) {
33  consSelectX = simParams->constrXOn;
34  consSelectY = simParams->constrYOn;
35  consSelectZ = simParams->constrZOn;
36  }
37  //****** END selective restraints (X,Y,Z) changes
38 
39  //****** BEGIN moving constraints changes
40  consMoveOn = simParams->movingConstraintsOn;
41  if (consMoveOn) {
42  moveVel = simParams->movingConsVel;
43  }
44  //****** END moving constraints changes
45  //****** BEGIN rotating constraints changes
46  consRotOn = simParams->rotConstraintsOn;
47  if (consRotOn) {
48  rotVel = simParams->rotConsVel;
49  rotAxis = simParams->rotConsAxis;
50  rotPivot = simParams->rotConsPivot;
51  }
52  //****** END rotating constraints changes
53 
54 }
55 /* END OF FUNCTION ComputeRestraints */
56 
57 /************************************************************************/
58 /* */
59 /* FUNCTION ~ComputeRestraints */
60 /* */
61 /* This is the destructor for the ComputeRestraints force object. */
62 /* */
63 /************************************************************************/
64 
66 
67 {
68  delete reduction;
69 }
70 /* END OF FUNCTION ~ComputeRestraints */
71 
72 /************************************************************************/
73 /* */
74 /* FUNCTION force */
75 /* */
76 /************************************************************************/
78 {
79  Molecule *molecule = Node::Object()->molecule;
80  Real k; // Force constant
82  BigReal scaling = simParams->constraintScaling;
83  Vector refPos; // Reference position
84  BigReal r, r2; // r=distance between atom position and the
85  // reference position, r2 = r^2
86  Vector Rij; // vector between current position and reference pos
87  BigReal value; // Current calculated value
88 
89  // aliases to work with old code
90  Force *f = res->f[Results::normal];
91  BigReal energy = 0;
92  BigReal m[9];
93  Tensor virial;
94  Vector netForce = 0;
95 
96  // BEGIN moving and rotating constraint changes ******
97 
98  // This version only allows one atom to be moved
99  // and only ALL ref positions to be rotated
100 
101  int currentTime = patch->flags.step;
102  if (consRotOn) {
103  vec_rotation_matrix(rotVel * currentTime, rotAxis, m);
104  }
105 
106  // END moving and rotating constraint changes ******
107 
108 
109  if (scaling != 0.) for (int localID=0; localID<numAtoms; ++localID)
110  {
111  if (molecule->is_atom_constrained(p[localID].id))
112  {
113 #ifndef MEM_OPT_VERSION
114  molecule->get_cons_params(k, refPos, p[localID].id);
115 #else
116  k = 1.0;
117  refPos = p[localID].fixedPosition;
118 #endif
119 
120  k *= scaling;
121 
122  // BEGIN moving and rotating constraint changes ******
123 
124  if (consMoveOn) {
125  refPos += currentTime * moveVel;
126  }
127  else if(consRotOn) {
128  refPos = mat_multiply_vec(refPos - rotPivot, m) + rotPivot;
129  }
130 
131  // END moving and rotating constraint changes *******
132 
133  if (simParams->sphericalConstraintsOn) {
134  BigReal refRad = (refPos - simParams->sphericalConstrCenter).length();
135  Vector relPos = patch->lattice.delta(p[localID].position, simParams->sphericalConstrCenter);
136  refPos = simParams->sphericalConstrCenter + relPos * (refRad/relPos.length());
137  }
138 
139  Rij = patch->lattice.delta(refPos,p[localID].position);
140  Vector vpos = refPos - Rij;
141 
142  //****** BEGIN selective restraints (X,Y,Z) changes
143  if (consSelectOn) { // check which components we want to restrain:
144  if (!consSelectX) {Rij.x=0.0;} // by setting the appropriate
145  if (!consSelectY) {Rij.y=0.0;} // Cartesian component to zero
146  if (!consSelectZ) {Rij.z=0.0;} // we will avoid a restoring force
147  } // along that component, and the
148  // energy will also be correct
149  //****** END selective restraints (X,Y,Z) changes
150 
151 
152  // Calculate the distance and the distance squared
153  r2 = Rij.length2();
154  r = sqrt(r2);
155 
156 
157  // Only calculate the energy and the force if the distance is
158  // non-zero. Otherwise, you could end up dividing by 0, which
159  // is bad
160  if (r>0.0)
161  {
162  value=k;
163 
164  // Loop through and multiple k by r consExp times.
165  // i.e. calculate kr^e
166  // I know, I could use pow(), but I don't trust it.
167  for (int k=0; k<consExp; ++k)
168  {
169  value *= r;
170  }
171 
172  // Add to the energy total
173  energy += value;
174 
175 
176 
177  // Now calculate the force, which is ekr^(e-1). Also, divide
178  // by another factor of r to normalize the vector before we
179  // multiple it by the magnitude
180  value *= consExp;
181  value /= r2;
182 
183  Rij *= value;
184  //iout << iINFO << "restraining force" << Rij << "\n";
185 
186  f[localID] += Rij;
187  netForce += Rij;
188  virial += outer(Rij,vpos);
189  }
190  }
191 
192  }
193 
194  reduction->item(REDUCTION_BC_ENERGY) += energy;
195  ADD_TENSOR_OBJECT(reduction,REDUCTION_VIRIAL_NORMAL,virial);
196  ADD_VECTOR_OBJECT(reduction,REDUCTION_EXT_FORCE_NORMAL,netForce);
197  reduction->submit();
198 
199 }
200 /* END OF FUNCTION force */
201 
static Node * Object()
Definition: Node.h:86
int32 ComputeID
Definition: NamdTypes.h:278
Position fixedPosition
Definition: NamdTypes.h:202
Lattice & lattice
Definition: Patch.h:127
NAMD_HOST_DEVICE Tensor outer(const Vector &v1, const Vector &v2)
Definition: Tensor.h:241
Definition: Vector.h:72
#define ADD_TENSOR_OBJECT(R, RL, D)
Definition: ReductionMgr.h:44
SimParameters * simParameters
Definition: Node.h:181
float Real
Definition: common.h:118
BigReal & item(int i)
Definition: ReductionMgr.h:313
void vec_rotation_matrix(BigReal angle, Vector v, BigReal m[])
Definition: NamdOneTools.C:132
SubmitReduction * willSubmit(int setID, int size=-1)
Definition: ReductionMgr.C:366
static ReductionMgr * Object(void)
Definition: ReductionMgr.h:279
Molecule stores the structural information for the system.
Definition: Molecule.h:175
Flags flags
Definition: Patch.h:128
uint32 id
Definition: NamdTypes.h:156
SubmitReduction * reduction
virtual void doForce(FullAtom *p, Results *r)
NAMD_HOST_DEVICE BigReal length(void) const
Definition: Vector.h:202
Vector mat_multiply_vec(const Vector &v, BigReal m[])
Definition: NamdOneTools.C:232
ComputeRestraints(ComputeID c, PatchID pid)
Force * f[maxNumForces]
Definition: PatchTypes.h:146
void get_cons_params(Real &k, Vector &refPos, int atomnum) const
Definition: Molecule.h:1349
virtual ~ComputeRestraints()
BigReal x
Definition: Vector.h:74
#define simParams
Definition: Output.C:129
Definition: Tensor.h:15
#define ADD_VECTOR_OBJECT(R, RL, D)
Definition: ReductionMgr.h:28
void submit(void)
Definition: ReductionMgr.h:324
Bool is_atom_constrained(int atomnum) const
Definition: Molecule.h:1265
int32 PatchID
Definition: NamdTypes.h:277
Molecule * molecule
Definition: Node.h:179
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