All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Torsion.cpp
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 The plumed team
3  (see the PEOPLE file at the root of the distribution for a list of names)
4 
5  See http://www.plumed-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
8 
9  plumed is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  plumed is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 #include "Torsion.h"
23 #include "Tensor.h"
24 
25 #include <cmath>
26 #include <iostream>
27 
28 namespace PLMD{
29 
30 double Torsion::compute(const Vector& v1,const Vector& v2,const Vector& v3)const{
31  const Vector nv2(v2*(1.0/v2.modulo()));
32  const Vector a(crossProduct(nv2,v1));
33  const Vector b(crossProduct(v3,nv2));
34  const double cosangle=dotProduct(a,b);
35  const double sinangle=dotProduct(crossProduct(a,b),nv2);
36  return std::atan2(-sinangle,cosangle);
37 }
38 
39 double Torsion::compute(const Vector& v1,const Vector& v2,const Vector& v3,Vector& d1,Vector& d2,Vector& d3)const{
40 
41  d1.zero();
42  d2.zero();
43  d3.zero();
44 
45  const double modv2(v2.modulo());
46  const Vector nv2(v2/modv2);
47  const Tensor dnv2_v2((Tensor::identity()-extProduct(nv2,nv2))/modv2);
48 
49  const Vector a(crossProduct(v2,v1));
50  const Tensor da_dv2(dcrossDv1(v2,v1));
51  const Tensor da_dv1(dcrossDv2(v2,v1));
52  const Vector b(crossProduct(v3,v2));
53  const Tensor db_dv3(dcrossDv1(v3,v2));
54  const Tensor db_dv2(dcrossDv2(v3,v2));
55  const double cosangle=dotProduct(a,b);
56  const Vector dcosangle_dv1=matmul(b,da_dv1);
57  const Vector dcosangle_dv2=matmul(b,da_dv2) + matmul(a,db_dv2);
58  const Vector dcosangle_dv3=matmul(a,db_dv3);
59 
60  const Vector cab(crossProduct(a,b));
61  const Tensor dcab_dv1(matmul(dcrossDv1(a,b),da_dv1));
62  const Tensor dcab_dv2(matmul(dcrossDv1(a,b),da_dv2) + matmul(dcrossDv2(a,b),db_dv2));
63  const Tensor dcab_dv3(matmul(dcrossDv2(a,b),db_dv3));
64 
65  const double sinangle=dotProduct(cab,nv2);
66  const Vector dsinangle_dv1=matmul(nv2,dcab_dv1);
67  const Vector dsinangle_dv2=matmul(nv2,dcab_dv2)+matmul(cab,dnv2_v2);
68  const Vector dsinangle_dv3=matmul(nv2,dcab_dv3);
69 
70  const double x=-sinangle/cosangle;
71  const Vector dx_dv1=-dsinangle_dv1/cosangle + sinangle/(cosangle*cosangle) * dcosangle_dv1;
72  const Vector dx_dv2=-dsinangle_dv2/cosangle + sinangle/(cosangle*cosangle) * dcosangle_dv2;
73  const Vector dx_dv3=-dsinangle_dv3/cosangle + sinangle/(cosangle*cosangle) * dcosangle_dv3;
74 
75  const double torsion=std::atan2(-sinangle,cosangle);
76  d1=1.0/(1.0+x*x) * dx_dv1;
77  d2=1.0/(1.0+x*x) * dx_dv2;
78  d3=1.0/(1.0+x*x) * dx_dv3;
79 
80  return torsion;
81 }
82 
83 }
84 
85 
86 
TensorGeneric< 3, 3 > dcrossDv2(const VectorGeneric< 3 > &v1, const VectorGeneric< 3 > &v2)
Definition: Tensor.h:410
double compute(const Vector &v1, const Vector &v2, const Vector &v3) const
Compute the angle between the projections of v1 and v3 on the plane orthogonal to v2...
Definition: Torsion.cpp:30
double modulo() const
Compute the modulo.
Definition: Vector.h:325
VectorGeneric< 3 > crossProduct(const VectorGeneric< 3 > &v1, const VectorGeneric< 3 > &v2)
Definition: Vector.h:317
Class implementing fixed size matrices of doubles.
Definition: Tensor.h:70
TensorGeneric< n, l > matmul(const TensorGeneric< n, m > &a, const TensorGeneric< m, l > &b)
Definition: Tensor.h:343
Class implementing fixed size vectors of doubles.
Definition: Vector.h:74
void zero()
set it to zero
Definition: Vector.h:173
static TensorGeneric< n, n > identity()
return an identity tensor
Definition: Tensor.h:318
TensorGeneric< 3, 3 > dcrossDv1(const VectorGeneric< 3 > &v1, const VectorGeneric< 3 > &v2)
Definition: Tensor.h:401
T dotProduct(const std::vector< T > &A, const std::vector< T > &B)
Calculate the dot product between two vectors.
Definition: Matrix.h:54
TensorGeneric< n, m > extProduct(const VectorGeneric< n > &v1, const VectorGeneric< m > &v2)
Definition: Tensor.h:396
void const char const char int double * a
Definition: Matrix.h:42