LCOV - code coverage report
Current view: top level - colvar - Torsion.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 64 70 91.4 %
Date: 2020-11-18 11:20:57 Functions: 10 11 90.9 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2011-2019 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.org for more information.
       6             : 
       7             :    This file is part of plumed, version 2.
       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 "Colvar.h"
      23             : #include "ActionRegister.h"
      24             : #include "tools/Torsion.h"
      25             : 
      26             : #include <string>
      27             : #include <cmath>
      28             : 
      29             : using namespace std;
      30             : 
      31             : namespace PLMD {
      32             : namespace colvar {
      33             : 
      34             : //+PLUMEDOC COLVAR TORSION
      35             : /*
      36             : Calculate a torsional angle.
      37             : 
      38             : This command can be used to compute the torsion between four atoms or alternatively
      39             : to calculate the angle between two vectors projected on the plane
      40             : orthogonal to an axis.
      41             : 
      42             : \par Examples
      43             : 
      44             : This input tells plumed to print the torsional angle between atoms 1, 2, 3 and 4
      45             : on file COLVAR.
      46             : \plumedfile
      47             : t: TORSION ATOMS=1,2,3,4
      48             : # this is an alternative, equivalent, definition:
      49             : # t: TORSION VECTOR1=2,1 AXIS=2,3 VECTOR2=3,4
      50             : PRINT ARG=t FILE=COLVAR
      51             : \endplumedfile
      52             : 
      53             : If you are working with a protein you can specify the special named torsion angles \f$\phi\f$, \f$\psi\f$, \f$\omega\f$ and \f$\chi_1\f$
      54             : by using TORSION in combination with the \ref MOLINFO command.  This can be done by using the following
      55             : syntax.
      56             : 
      57             : \plumedfile
      58             : MOLINFO MOLTYPE=protein STRUCTURE=myprotein.pdb
      59             : t1: TORSION ATOMS=@phi-3
      60             : t2: TORSION ATOMS=@psi-4
      61             : PRINT ARG=t1,t2 FILE=colvar STRIDE=10
      62             : \endplumedfile
      63             : 
      64             : Here, \@phi-3 tells plumed that you would like to calculate the \f$\phi\f$ angle in the third residue of the protein.
      65             : Similarly \@psi-4 tells plumed that you want to calculate the \f$\psi\f$ angle of the 4th residue of the protein.
      66             : 
      67             : Both of the previous examples specify that the torsion angle should be calculated based on the position of four atoms.
      68             : For the first example in particular the assumption when the torsion is specified in this way is that there are chemical
      69             : bonds between atoms 1 and 2, atoms 2 and 3 and atoms 3 and 4. In general, however, a torsional angle measures the angle
      70             : between two planes, which have at least one vector in common.  As shown below, there is thus an alternate, more general, way
      71             : through which we can define a torsional angle:
      72             : 
      73             : \plumedfile
      74             : t1: TORSION VECTOR1=1,2 AXIS=3,4 VECTOR2=5,6
      75             : PRINT ARG=t1 FILE=colvar STRIDE=20
      76             : \endplumedfile
      77             : 
      78             : This input instructs PLUMED to calculate the angle between the plane containing the vector connecting atoms 1 and 2 and the vector
      79             : connecting atoms 3 and 4 and the plane containing this second vector and the vector connecting atoms 5 and 6.  We can even use
      80             : PLUMED to calculate the torsional angle between two bond vectors around the z-axis as shown below:
      81             : 
      82             : \plumedfile
      83             : a0: FIXEDATOM AT=0,0,0
      84             : az: FIXEDATOM AT=0,0,1
      85             : t1: TORSION VECTOR1=1,2 AXIS=a0,az VECTOR2=5,6
      86             : PRINT ARG=t1 FILE=colvar STRIDE=20
      87             : \endplumedfile
      88             : 
      89             : 
      90             : */
      91             : //+ENDPLUMEDOC
      92             : 
      93         880 : class Torsion : public Colvar {
      94             :   bool pbc;
      95             :   bool do_cosine;
      96             : 
      97             : public:
      98             :   explicit Torsion(const ActionOptions&);
      99             : // active methods:
     100             :   virtual void calculate();
     101             :   static void registerKeywords(Keywords& keys);
     102             : };
     103             : 
     104        6894 : PLUMED_REGISTER_ACTION(Torsion,"TORSION")
     105             : 
     106         443 : void Torsion::registerKeywords(Keywords& keys) {
     107         443 :   Colvar::registerKeywords( keys );
     108        1772 :   keys.add("atoms-1","ATOMS","the four atoms involved in the torsional angle");
     109        1772 :   keys.add("atoms-2","AXIS","two atoms that define an axis.  You can use this to find the angle in the plane perpendicular to the axis between the vectors specified using the VECTOR1 and VECTOR2 keywords.");
     110        1772 :   keys.add("atoms-2","VECTOR1","two atoms that define a vector.  You can use this in combination with VECTOR2 and AXIS");
     111        1772 :   keys.add("atoms-2","VECTOR2","two atoms that define a vector.  You can use this in combination with VECTOR1 and AXIS");
     112        1329 :   keys.addFlag("COSINE",false,"calculate cosine instead of dihedral");
     113         443 : }
     114             : 
     115         442 : Torsion::Torsion(const ActionOptions&ao):
     116             :   PLUMED_COLVAR_INIT(ao),
     117             :   pbc(true),
     118         444 :   do_cosine(false)
     119             : {
     120             :   vector<AtomNumber> atoms,v1,v2,axis;
     121         884 :   parseAtomList("ATOMS",atoms);
     122         884 :   parseAtomList("VECTOR1",v1);
     123         884 :   parseAtomList("VECTOR2",v2);
     124         884 :   parseAtomList("AXIS",axis);
     125             : 
     126         884 :   parseFlag("COSINE",do_cosine);
     127             : 
     128         442 :   bool nopbc=!pbc;
     129         884 :   parseFlag("NOPBC",nopbc);
     130         442 :   pbc=!nopbc;
     131         442 :   checkRead();
     132             : 
     133         442 :   if(atoms.size()==4) {
     134        1303 :     if(!(v1.empty() && v2.empty() && axis.empty()))
     135           2 :       error("ATOMS keyword is not compatible with VECTOR1, VECTOR2 and AXIS keywords");
     136         868 :     log.printf("  between atoms %d %d %d %d\n",atoms[0].serial(),atoms[1].serial(),atoms[2].serial(),atoms[3].serial());
     137         434 :     atoms.resize(6);
     138         434 :     atoms[5]=atoms[3];
     139         434 :     atoms[4]=atoms[2];
     140         434 :     atoms[3]=atoms[2];
     141         434 :     atoms[2]=atoms[1];
     142           7 :   } else if(atoms.empty()) {
     143          18 :     if(!(v1.size()==2 && v2.size()==2 && axis.size()==2))
     144           0 :       error("VECTOR1, VECTOR2 and AXIS should specify 2 atoms each");
     145          12 :     log.printf("  between lines %d-%d and %d-%d, projected on the plane orthogonal to line %d-%d\n",
     146             :                v1[0].serial(),v1[1].serial(),v2[0].serial(),v2[1].serial(),axis[0].serial(),axis[1].serial());
     147           6 :     atoms.resize(6);
     148           6 :     atoms[0]=v1[1];
     149           6 :     atoms[1]=v1[0];
     150           6 :     atoms[2]=axis[0];
     151           6 :     atoms[3]=axis[1];
     152           6 :     atoms[4]=v2[0];
     153           6 :     atoms[5]=v2[1];
     154           2 :   } else error("ATOMS should specify 4 atoms");
     155             : 
     156         440 :   if(pbc) log.printf("  using periodic boundary conditions\n");
     157         108 :   else    log.printf("  without periodic boundary conditions\n");
     158             : 
     159         440 :   if(do_cosine) log.printf("  calculating cosine instead of torsion\n");
     160             : 
     161         440 :   addValueWithDerivatives();
     162        1320 :   if(!do_cosine) setPeriodic("-pi","pi");
     163           0 :   else setNotPeriodic();
     164         440 :   requestAtoms(atoms);
     165         440 : }
     166             : 
     167             : // calculator
     168       24039 : void Torsion::calculate() {
     169             : 
     170       24039 :   Vector d0,d1,d2;
     171       24039 :   if(pbc) makeWhole();
     172       24039 :   d0=delta(getPosition(1),getPosition(0));
     173       24039 :   d1=delta(getPosition(3),getPosition(2));
     174       24039 :   d2=delta(getPosition(5),getPosition(4));
     175       24039 :   Vector dd0,dd1,dd2;
     176             :   PLMD::Torsion t;
     177       24039 :   double torsion=t.compute(d0,d1,d2,dd0,dd1,dd2);
     178       24039 :   if(do_cosine) {
     179           0 :     dd0 *= -sin(torsion);
     180           0 :     dd1 *= -sin(torsion);
     181           0 :     dd2 *= -sin(torsion);
     182           0 :     torsion = cos(torsion);
     183             :   }
     184       24039 :   setAtomsDerivatives(0,dd0);
     185       48078 :   setAtomsDerivatives(1,-dd0);
     186             :   setAtomsDerivatives(2,dd1);
     187       48078 :   setAtomsDerivatives(3,-dd1);
     188             :   setAtomsDerivatives(4,dd2);
     189       48078 :   setAtomsDerivatives(5,-dd2);
     190             : 
     191       24039 :   setValue           (torsion);
     192             :   setBoxDerivativesNoPbc();
     193       24039 : }
     194             : 
     195             : }
     196        4839 : }
     197             : 
     198             : 
     199             : 

Generated by: LCOV version 1.13