ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-3.0/src/primitives/Torsion.cpp
(Generate patch)

Comparing branches/new_design/OOPSE-3.0/src/primitives/Torsion.cpp (file contents):
Revision 1701 by tim, Wed Nov 3 16:08:43 2004 UTC vs.
Revision 1742 by tim, Tue Nov 16 20:36:18 2004 UTC

# Line 1 | Line 1
1 < #include "primitives/SRI.hpp"
2 < #include "primitives/Atom.hpp"
3 < #include <math.h>
4 < #include <iostream>
5 < #include <stdlib.h>
1 > #include "primitives/Torsion.hpp"
2  
3 < void Torsion::set_atoms( Atom &a, Atom &b, Atom &c, Atom &d){
4 <  c_p_a = &a;
5 <  c_p_b = &b;
6 <  c_p_c = &c;
7 <  c_p_d = &d;
3 > namespace oopse {
4 >
5 > Torsion::Torsion(Atom* atom1, Atom* atom2, Atom* atom3, Atom* atom4, TorsionType* tt)
6 >            : atom1_(atom1), atom2_(atom2), atom3_(atom3), atom4_(atom4) {
7 >
8   }
9  
10 + void Torsion::calcForce() {
11  
12 +    Vector3d pos1 = atom1_->getPos();
13 +    Vector3d pos2 = atom2_->getPos();
14 +    Vector3d pos3 = atom3_->getPos();
15 +    Vector3d pos4 = atom4_->getPos();
16 +
17 +    Vector3d r12 = pos1 - pos2;
18 +    Vector3d r23 = pos2 - pos3;
19 +    Vector3d r34 = pos3 - pos4;
20 +
21 +    //  Calculate the cross products and distances
22 +    Vector3d A = cross(r12,r23);
23 +    double rA = A.length();
24 +    Vector3d B = cross(r23,r34);
25 +    double rB = B.length();
26 +    Vector3d C = cross(r23,A);
27 +    double rC = C.length();
28 +
29 +    //  Calculate the sin and cos
30 +    double cos_phi = (A*B)/(rA*rB);
31 +    double sin_phi = (C*B)/(rC*rB);
32 +
33 +    double phi= -atan2(sin_phi,cos_phi);
34 +
35 +    double firstDerivative;
36 +    torsionType_->calcForce(phi, firstDerivative, potential_);
37 +
38 +
39 +    Vector3d f1,f2,f3;
40 +    
41 +    //  Normalize B
42 +    rB = 1.0/rB;
43 +    B *= rB;
44 +
45 +    //  Next, we want to calculate the forces.  In order
46 +    //  to do that, we first need to figure out whether the
47 +    //  sin or cos form will be more stable.  For this,
48 +    //  just look at the value of phi
49 +    if (fabs(sin_phi) > 0.1) {
50 +        //  use the sin version to avoid 1/cos terms
51 +
52 +        rA = 1.0/rA;
53 +        A *= rA;
54 +        Vector3d dcosdA = rA*(cos_phi*A-B);
55 +        Vector3d dcosdB = rB*(cos_phi*B-A);
56 +
57 +        K1 = K1/sin_phi;
58 +
59 +        //simple form
60 +        //f1 = K1 * cross(r23, dcosdA);
61 +        //f3 = K1 * cross(r23, dcosdB);
62 +        //f2 = K1 * ( cross(r34, dcosdB) - cross(r12, dcosdA));
63 +
64 +        f1.x = K1*(r23.y*dcosdA.z - r23.z*dcosdA.y);
65 +        f1.y = K1*(r23.z*dcosdA.x - r23.x*dcosdA.z);
66 +        f1.z = K1*(r23.x*dcosdA.y - r23.y*dcosdA.x);
67 +
68 +        f3.x = K1*(r23.z*dcosdB.y - r23.y*dcosdB.z);
69 +        f3.y = K1*(r23.x*dcosdB.z - r23.z*dcosdB.x);
70 +        f3.z = K1*(r23.y*dcosdB.x - r23.x*dcosdB.y);
71 +
72 +        f2.x = K1*(r12.z*dcosdA.y - r12.y*dcosdA.z + r34.y*dcosdB.z - r34.z*dcosdB.y);
73 +        f2.y = K1*(r12.x*dcosdA.z - r12.z*dcosdA.x + r34.z*dcosdB.x - r34.x*dcosdB.z);
74 +        f2.z = K1*(r12.y*dcosdA.x - r12.x*dcosdA.y + r34.x*dcosdB.y - r34.y*dcosdB.x);
75 +    } else {
76 +        //  This angle is closer to 0 or 180 than it is to
77 +        //  90, so use the cos version to avoid 1/sin terms
78 +
79 +        //  Normalize C
80 +        rC = 1.0/rC;
81 +        C *= rC;
82 +        Vector3d dsindC = rC*(sin_phi*C-B);
83 +        Vector3d dsindB = rB*(sin_phi*B-C);
84 +
85 +        K1 = -K1/cos_phi;
86 +
87 +        f1.x = K1*((r23.y*r23.y + r23.z*r23.z)*dsindC.x - r23.x*r23.y*dsindC.y - r23.x*r23.z*dsindC.z);
88 +        f1.y = K1*((r23.z*r23.z + r23.x*r23.x)*dsindC.y - r23.y*r23.z*dsindC.z - r23.y*r23.x*dsindC.x);
89 +        f1.z = K1*((r23.x*r23.x + r23.y*r23.y)*dsindC.z - r23.z*r23.x*dsindC.x - r23.z*r23.y*dsindC.y);
90 +
91 +        f3 = K1 *cross(dsindB,r23);
92 +
93 +        f2.x = K1*(-(r23.y*r12.y + r23.z*r12.z)*dsindC.x + (2.0*r23.x*r12.y - r12.x*r23.y)*dsindC.y
94 +                 + (2.0*r23.x*r12.z - r12.x*r23.z)*dsindC.z + dsindB.z*r34.y - dsindB.y*r34.z);
95 +        f2.y = K1*(-(r23.z*r12.z + r23.x*r12.x)*dsindC.y + (2.0*r23.y*r12.z - r12.y*r23.z)*dsindC.z
96 +                 + (2.0*r23.y*r12.x - r12.y*r23.x)*dsindC.x + dsindB.x*r34.z - dsindB.z*r34.x);
97 +        f2.z = K1*(-(r23.x*r12.x + r23.y*r12.y)*dsindC.z + (2.0*r23.z*r12.x - r12.z*r23.x)*dsindC.x
98 +                  +(2.0*r23.z*r12.y - r12.z*r23.y)*dsindC.y + dsindB.y*r34.x - dsindB.x*r34.y);
99 +    }
100 +
101 +    atom1_->addFrc(f1);
102 +    atom2_->addFrc(f2 - f1);
103 +    atom3_->addFrc(f3 - f2);
104 +    atom4_->addFrc(-f3);
105 +    
106 + }
107 +
108 +
109 +      double K=0;       // energy
110 +      double K1=0;      // force
111 +
112 +  // get the dihedral information
113 +  int multiplicity = value->multiplicity;
114 +
115 +  //  Loop through the multiple parameter sets for this
116 +  //  bond.  We will only loop more than once if this
117 +  //  has multiple parameter sets from Charmm22
118 +  for (int mult_num=0; mult_num<multiplicity; mult_num++)
119 +  {
120 +    /* get angle information */
121 +    double k = value->values[mult_num].k * scale;
122 +    double delta = value->values[mult_num].delta;
123 +    int n = value->values[mult_num].n;
124 +
125 +    //  Calculate the energy
126 +    if (n)
127 +    {
128 +      //  Periodicity is greater than 0, so use cos form
129 +      K += k*(1+cos(n*phi + delta));
130 +      K1 += -n*k*sin(n*phi + delta);
131 +    }
132 +    else
133 +    {
134 +      //  Periodicity is 0, so just use the harmonic form
135 +      double diff = phi-delta;
136 +      if (diff < -PI)           diff += TWOPI;
137 +      else if (diff > PI)       diff -= TWOPI;
138 +
139 +      K += k*diff*diff;
140 +      K1 += 2.0*k*diff;
141 +    }
142 +  } /* for multiplicity */
143 +
144 +
145   void Torsion::calc_forces(){
146    
147    /**********************************************************************
# Line 40 | Line 170 | void Torsion::calc_forces(){
170    double r_cr1_r_cr2; /* the length of r_cr1 * length of r_cr2 */
171    
172    Vector3d aR, bR, cR, dR;
173 <  Vector3d aF, bF, cF, dF;
173 >  Vector3d  aF, bF, cF, dF;
174  
175    aR = c_p_a->getPos();
176    bR = c_p_b->getPos();
# Line 82 | Line 212 | void Torsion::calc_forces(){
212  
213    r_cr1_r_cr2 = r_cr1.length * r_cr2.length;
214  
215 +    //Vector3d pos1 = atom1_->getPos();
216 +    //Vector3d pos2 = atom2_->getPos();
217 +    //Vector3d pos3 = atom3_->getPos();
218 +    //Vector3d pos4 = atom4_->getPos();
219 +  
220 +    //Vector3d r12 = pos2 - pos1;
221 +    //Vector3d r32 = pos2 - pos3;
222 +    //Vector3d r34 = pos4 - pos3;
223 +
224 +    //A = cross(r12, r32);
225 +    //B = cross(r32, r34);
226 +    
227 +    //rA = A.length();  
228 +    //rB = B.length();
229 +  
230    /**********************************************************************
231     *
232     * dot product and angle calculations
# Line 100 | Line 245 | void Torsion::calc_forces(){
245    if(cos_phi > 1.0) cos_phi = 1.0;
246    if(cos_phi < -1.0) cos_phi = -1.0;
247  
248 +    //cos_phi = dot (A, B) / (rA * rB);
249 +    //if (cos_phi > 1.0) {
250 +    //  cos_phi = 1.0;
251 +    //}
252 +    //if (cos_phi < -1.0) {
253 +    //  cos_phi = -1.0;
254 +    //}
255  
256 +
257 +
258    /********************************************************************
259     *
260     * This next section calculates derivatives needed for the force
# Line 126 | Line 280 | void Torsion::calc_forces(){
280    d_cos_dy_cr2 = r_cr1.y / r_cr1_r_cr2 - (cos_phi * r_cr2.y) / r_cr2_sqr;
281    d_cos_dz_cr2 = r_cr1.z / r_cr1_r_cr2 - (cos_phi * r_cr2.z) / r_cr2_sqr;
282  
283 +  //Vector3d dcosdA = B /(rA * rB) - cos_phi  /(rA * rA) * A;
284 +  //Vector3d dcosdA = 1.0 /rA * (B.normalize() - cos_phi * A.normalize());
285 +  //Vector3d dcosdB = 1.0 /rB * (A.normalize() - cos_phi * B.normalize());
286 +
287    /***********************************************************************
288     *
289     * Calculate the actual forces and place them in the atoms.
# Line 141 | Line 299 | void Torsion::calc_forces(){
299    aF[2] = force * (d_cos_dx_cr1 * r_cb.y - d_cos_dy_cr1 * r_cb.x);
300  
301    bF[0] = force * (  d_cos_dy_cr1 * (r_ab.z - r_cb.z)
302 <                   - d_cos_dy_cr2 *  r_cd.z      
303 <                   + d_cos_dz_cr1 * (r_cb.y - r_ab.y)
304 <                   + d_cos_dz_cr2 *  r_cd.y);
302 >           - d_cos_dy_cr2 *  r_cd.z  
303 >           + d_cos_dz_cr1 * (r_cb.y - r_ab.y)
304 >           + d_cos_dz_cr2 *  r_cd.y);
305    bF[1] = force * (  d_cos_dx_cr1 * (r_cb.z - r_ab.z)
306 <                   + d_cos_dx_cr2 *  r_cd.z      
307 <                   + d_cos_dz_cr1 * (r_ab.x - r_cb.x)
308 <                   - d_cos_dz_cr2 *  r_cd.x);
306 >           + d_cos_dx_cr2 *  r_cd.z  
307 >           + d_cos_dz_cr1 * (r_ab.x - r_cb.x)
308 >           - d_cos_dz_cr2 *  r_cd.x);
309    bF[2] = force * (  d_cos_dx_cr1 * (r_ab.y - r_cb.y)
310 <                   - d_cos_dx_cr2 *  r_cd.y      
311 <                   + d_cos_dy_cr1 * (r_cb.x - r_ab.x)
312 <                   + d_cos_dy_cr2 *  r_cd.x);
310 >           - d_cos_dx_cr2 *  r_cd.y  
311 >           + d_cos_dy_cr1 * (r_cb.x - r_ab.x)
312 >           + d_cos_dy_cr2 *  r_cd.x);
313  
314    cF[0] = force * (- d_cos_dy_cr1 *  r_ab.z
315 <                   - d_cos_dy_cr2 * (r_cb.z - r_cd.z)
316 <                   + d_cos_dz_cr1 *  r_ab.y
317 <                   - d_cos_dz_cr2 * (r_cd.y - r_cb.y));
315 >           - d_cos_dy_cr2 * (r_cb.z - r_cd.z)
316 >           + d_cos_dz_cr1 *  r_ab.y
317 >           - d_cos_dz_cr2 * (r_cd.y - r_cb.y));
318    cF[1] = force * (  d_cos_dx_cr1 *  r_ab.z
319 <                   - d_cos_dx_cr2 * (r_cd.z - r_cb.z)
320 <                   - d_cos_dz_cr1 *  r_ab.x
321 <                   - d_cos_dz_cr2 * (r_cb.x - r_cd.x));
319 >           - d_cos_dx_cr2 * (r_cd.z - r_cb.z)
320 >           - d_cos_dz_cr1 *  r_ab.x
321 >           - d_cos_dz_cr2 * (r_cb.x - r_cd.x));
322    cF[2] = force * (- d_cos_dx_cr1 *  r_ab.y
323 <                   - d_cos_dx_cr2 * (r_cb.y - r_cd.y)
324 <                   + d_cos_dy_cr1 *  r_ab.x
325 <                   - d_cos_dy_cr2 * (r_cd.x - r_cb.x));
323 >           - d_cos_dx_cr2 * (r_cb.y - r_cd.y)
324 >           + d_cos_dy_cr1 *  r_ab.x
325 >           - d_cos_dy_cr2 * (r_cd.x - r_cb.x));
326  
327    dF[0] = force * (d_cos_dy_cr2 * r_cb.z - d_cos_dz_cr2 * r_cb.y);
328    dF[1] = force * (d_cos_dz_cr2 * r_cb.x - d_cos_dx_cr2 * r_cb.z);
# Line 175 | Line 333 | void Torsion::calc_forces(){
333    c_p_b->addFrc(bF);
334    c_p_c->addFrc(cF);
335    c_p_d->addFrc(dF);
336 +
337 +    //double firstDerivative;
338 +    //bondType_->calcForce(cos_phi, firstDerivative, potential_);
339 +    //f1 = force * cross (dcosdA, r32);
340 +    //f2 =
341 +    //f3 =
342 +    //f4 = force * cross(dcosdB, r32);
343 +    //atom1_->addFrc(f1);
344 +    //atom2_->addFrc(f2);
345 +    //atom3_->addFrc(f3);
346 +    //atom4_->addFrc(f4);
347 +
348 +  
349   }
350 +
351 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines