LCOV - code coverage report
Current view: top level - colvar - Gyration.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 153 196 78.1 %
Date: 2018-12-19 07:49:13 Functions: 10 11 90.9 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2012-2018 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 "core/PlumedMain.h"
      25             : #include "tools/Matrix.h"
      26             : 
      27             : #include <string>
      28             : #include <cmath>
      29             : 
      30             : using namespace std;
      31             : 
      32             : namespace PLMD {
      33             : namespace colvar {
      34             : 
      35             : //+PLUMEDOC COLVAR GYRATION
      36             : /*
      37             : Calculate the radius of gyration, or other properties related to it.
      38             : 
      39             : The different properties can be calculated and selected by the TYPE keyword:
      40             : the Radius of Gyration (RADIUS); the Trace of the Gyration Tensor (TRACE);
      41             : the Largest Principal Moment of the Gyration Tensor (GTPC_1); the middle Principal Moment of the Gyration Tensor (GTPC_2);
      42             : the Smallest Principal Moment of the Gyration Tensor (GTPC_3); the Asphericiry (ASPHERICITY); the Acylindricity (ACYLINDRICITY);
      43             : the Relative Shape Anisotropy (KAPPA2); the Smallest Principal Radius Of Gyration (GYRATION_3);
      44             : the Middle Principal Radius of Gyration (GYRATION_2); the Largest Principal Radius of Gyration (GYRATION_1).
      45             : A derivation of all these different variants can be found in \cite Vymetal:2011gv
      46             : 
      47             : The radius of gyration is calculated using:
      48             : 
      49             : \f[
      50             : s_{\rm Gyr}=\Big ( \frac{\sum_i^{n}
      51             :  m_i \vert {r}_i -{r}_{\rm COM} \vert ^2 }{\sum_i^{n} m_i} \Big)^{1/2}
      52             : \f]
      53             : 
      54             : with the position of the center of mass \f${r}_{\rm COM}\f$ given by:
      55             : 
      56             : \f[
      57             : {r}_{\rm COM}=\frac{\sum_i^{n} {r}_i\ m_i }{\sum_i^{n} m_i}
      58             : \f]
      59             : 
      60             : The radius of gyration usually makes sense when atoms used for the calculation
      61             : are all part of the same molecule.
      62             : When running with periodic boundary conditions, the atoms should be
      63             : in the proper periodic image. This is done automatically since PLUMED 2.2,
      64             : by considering the ordered list of atoms and rebuilding PBCs with a procedure
      65             : that is equivalent to that done in \ref WHOLEMOLECULES . Notice that
      66             : rebuilding is local to this action. This is different from \ref WHOLEMOLECULES
      67             : which actually modifies the coordinates stored in PLUMED.
      68             : 
      69             : In case you want to recover the old behavior you should use the NOPBC flag.
      70             : In that case you need to take care that atoms are in the correct
      71             : periodic image.
      72             : 
      73             : 
      74             : \par Examples
      75             : 
      76             : The following input tells plumed to print the radius of gyration of the
      77             : chain containing atoms 10 to 20.
      78             : \verbatim
      79             : GYRATION TYPE=RADIUS ATOMS=10-20 LABEL=rg
      80             : PRINT ARG=rg STRIDE=1 FILE=colvar
      81             : \endverbatim
      82             : (See also \ref PRINT)
      83             : 
      84             : */
      85             : //+ENDPLUMEDOC
      86             : 
      87          48 : class Gyration : public Colvar {
      88             : private:
      89             :   enum CV_TYPE {RADIUS, TRACE, GTPC_1, GTPC_2, GTPC_3, ASPHERICITY, ACYLINDRICITY, KAPPA2, GYRATION_3, GYRATION_2, GYRATION_1, TOT};
      90             :   int rg_type;
      91             :   bool use_masses;
      92             :   bool nopbc;
      93             : public:
      94             :   static void registerKeywords(Keywords& keys);
      95             :   explicit Gyration(const ActionOptions&);
      96             :   virtual void calculate();
      97             : };
      98             : 
      99        2547 : PLUMED_REGISTER_ACTION(Gyration,"GYRATION")
     100             : 
     101          25 : void Gyration::registerKeywords(Keywords& keys) {
     102          25 :   Colvar::registerKeywords(keys);
     103          25 :   keys.add("atoms","ATOMS","the group of atoms that you are calculating the Gyration Tensor for");
     104          25 :   keys.add("compulsory","TYPE","RADIUS","The type of calculation relative to the Gyration Tensor you want to perform");
     105          25 :   keys.addFlag("MASS_WEIGHTED",false,"set the masses of all the atoms equal to one");
     106          25 : }
     107             : 
     108          24 : Gyration::Gyration(const ActionOptions&ao):
     109             :   PLUMED_COLVAR_INIT(ao),
     110             :   use_masses(false),
     111          24 :   nopbc(false)
     112             : {
     113          24 :   std::vector<AtomNumber> atoms;
     114          24 :   parseAtomList("ATOMS",atoms);
     115          24 :   if(atoms.size()==0) error("no atoms specified");
     116          24 :   parseFlag("MASS_WEIGHTED",use_masses);
     117          48 :   std::string Type;
     118          24 :   parse("TYPE",Type);
     119          24 :   parseFlag("NOPBC",nopbc);
     120          24 :   checkRead();
     121             : 
     122          24 :   if(Type=="RADIUS") rg_type=RADIUS;
     123          20 :   else if(Type=="TRACE") rg_type=TRACE;
     124          18 :   else if(Type=="GTPC_1") rg_type=GTPC_1;
     125          16 :   else if(Type=="GTPC_2") rg_type=GTPC_2;
     126          14 :   else if(Type=="GTPC_3") rg_type=GTPC_3;
     127          12 :   else if(Type=="ASPHERICITY") rg_type=ASPHERICITY;
     128          10 :   else if(Type=="ACYLINDRICITY") rg_type=ACYLINDRICITY;
     129           8 :   else if(Type=="KAPPA2") rg_type=KAPPA2;
     130           6 :   else if(Type=="RGYR_3") rg_type=GYRATION_3;
     131           4 :   else if(Type=="RGYR_2") rg_type=GYRATION_2;
     132           2 :   else if(Type=="RGYR_1") rg_type=GYRATION_1;
     133           0 :   else error("Unknown GYRATION type");
     134             : 
     135          24 :   switch(rg_type)
     136             :   {
     137           4 :   case RADIUS:   log.printf("  GYRATION RADIUS (Rg);"); break;
     138           2 :   case TRACE:  log.printf("  TRACE OF THE GYRATION TENSOR;"); break;
     139           2 :   case GTPC_1: log.printf("  THE LARGEST PRINCIPAL MOMENT OF THE GYRATION TENSOR (S'_1);"); break;
     140           2 :   case GTPC_2: log.printf("  THE MIDDLE PRINCIPAL MOMENT OF THE GYRATION TENSOR (S'_2);");  break;
     141           2 :   case GTPC_3: log.printf("  THE SMALLEST PRINCIPAL MOMENT OF THE GYRATION TENSOR (S'_3);"); break;
     142           2 :   case ASPHERICITY: log.printf("  THE ASPHERICITY (b');"); break;
     143           2 :   case ACYLINDRICITY: log.printf("  THE ACYLINDRICITY (c');"); break;
     144           2 :   case KAPPA2: log.printf("  THE RELATIVE SHAPE ANISOTROPY (kappa^2);"); break;
     145           2 :   case GYRATION_3: log.printf("  THE SMALLEST PRINCIPAL RADIUS OF GYRATION (r_g3);"); break;
     146           2 :   case GYRATION_2: log.printf("  THE MIDDLE PRINCIPAL RADIUS OF GYRATION (r_g2);"); break;
     147           2 :   case GYRATION_1: log.printf("  THE LARGEST PRINCIPAL RADIUS OF GYRATION (r_g1);"); break;
     148             :   }
     149          24 :   if(rg_type>TRACE) log<<"  Bibliography "<<plumed.cite("Jirí Vymetal and Jirí Vondrasek, J. Phys. Chem. A 115, 11455 (2011)"); log<<"\n";
     150             : 
     151          24 :   log.printf("  atoms involved : ");
     152          24 :   for(unsigned i=0; i<atoms.size(); ++i) log.printf("%d ",atoms[i].serial());
     153          24 :   log.printf("\n");
     154             : 
     155          24 :   if(nopbc) {
     156           4 :     log<<"  PBC will be ignored\n";
     157             :   } else {
     158          20 :     log<<"  broken molecules will be rebuilt assuming atoms are in the proper order\n";
     159             :   }
     160             : 
     161          24 :   addValueWithDerivatives(); setNotPeriodic();
     162          48 :   requestAtoms(atoms);
     163          24 : }
     164             : 
     165        1188 : void Gyration::calculate() {
     166             : 
     167        1188 :   if(!nopbc) makeWhole();
     168             : 
     169        1188 :   Vector com;
     170        1188 :   double totmass = 0.;
     171        1188 :   if( use_masses ) {
     172           0 :     for(unsigned i=0; i<getNumberOfAtoms(); i++) {
     173           0 :       totmass+=getMass(i);
     174           0 :       com+=getMass(i)*getPosition(i);
     175             :     }
     176             :   } else {
     177        1188 :     totmass = static_cast<double>(getNumberOfAtoms());
     178       10296 :     for(unsigned i=0; i<getNumberOfAtoms(); i++) {
     179        9108 :       com+=getPosition(i);
     180             :     }
     181             :   }
     182        1188 :   com /= totmass;
     183             : 
     184        1188 :   double rgyr=0.;
     185        1188 :   vector<Vector> derivatives( getNumberOfAtoms() );
     186        1188 :   Tensor virial;
     187             : 
     188        1188 :   if(rg_type==RADIUS||rg_type==TRACE) {
     189         788 :     if( use_masses ) {
     190           0 :       for(unsigned i=0; i<getNumberOfAtoms(); i++) {
     191           0 :         const Vector diff = delta( com, getPosition(i) );
     192           0 :         rgyr          += getMass(i)*diff.modulo2();
     193           0 :         derivatives[i] = diff*getMass(i);
     194           0 :         virial        -= Tensor(getPosition(i),derivatives[i]);
     195             :       }
     196             :     } else {
     197        7896 :       for(unsigned i=0; i<getNumberOfAtoms(); i++) {
     198        7108 :         const Vector diff = delta( com, getPosition(i) );
     199        7108 :         rgyr          += diff.modulo2();
     200        7108 :         derivatives[i] = diff;
     201        7108 :         virial        -= Tensor(getPosition(i),derivatives[i]);
     202             :       }
     203             :     }
     204             :     double fact;
     205         788 :     if(rg_type==RADIUS) {
     206         658 :       rgyr = sqrt(rgyr/totmass);
     207         658 :       fact = 1./(rgyr*totmass);
     208             :     } else {
     209         130 :       rgyr = 2.*rgyr;
     210         130 :       fact = 4;
     211             :     }
     212         788 :     setValue(rgyr);
     213         788 :     for(unsigned i=0; i<getNumberOfAtoms(); i++) setAtomsDerivatives(i,fact*derivatives[i]);
     214         788 :     setBoxDerivatives(fact*virial);
     215        1976 :     return;
     216             :   }
     217             : 
     218             : 
     219         800 :   Matrix<double> gyr_tens(3,3);
     220         400 :   for(unsigned i=0; i<3; i++)  for(unsigned j=0; j<3; j++) gyr_tens(i,j)=0.;
     221             :   //calculate gyration tensor
     222         400 :   if( use_masses ) {
     223           0 :     for(unsigned i=0; i<getNumberOfAtoms(); i++) {
     224           0 :       const Vector diff=delta( com, getPosition(i) );
     225           0 :       gyr_tens[0][0]+=getMass(i)*diff[0]*diff[0];
     226           0 :       gyr_tens[1][1]+=getMass(i)*diff[1]*diff[1];
     227           0 :       gyr_tens[2][2]+=getMass(i)*diff[2]*diff[2];
     228           0 :       gyr_tens[0][1]+=getMass(i)*diff[0]*diff[1];
     229           0 :       gyr_tens[0][2]+=getMass(i)*diff[0]*diff[2];
     230           0 :       gyr_tens[1][2]+=getMass(i)*diff[1]*diff[2];
     231             :     }
     232             :   } else {
     233        2400 :     for(unsigned i=0; i<getNumberOfAtoms(); i++) {
     234        2000 :       const Vector diff=delta( com, getPosition(i) );
     235        2000 :       gyr_tens[0][0]+=diff[0]*diff[0];
     236        2000 :       gyr_tens[1][1]+=diff[1]*diff[1];
     237        2000 :       gyr_tens[2][2]+=diff[2]*diff[2];
     238        2000 :       gyr_tens[0][1]+=diff[0]*diff[1];
     239        2000 :       gyr_tens[0][2]+=diff[0]*diff[2];
     240        2000 :       gyr_tens[1][2]+=diff[1]*diff[2];
     241             :     }
     242             :   }
     243             : 
     244             :   // first make the matrix symmetric
     245         400 :   gyr_tens[1][0] = gyr_tens[0][1];
     246         400 :   gyr_tens[2][0] = gyr_tens[0][2];
     247         400 :   gyr_tens[2][1] = gyr_tens[1][2];
     248         800 :   Matrix<double> ttransf(3,3), transf(3,3);
     249         800 :   vector<double> princ_comp(3), prefactor(3);
     250         400 :   prefactor[0]=prefactor[1]=prefactor[2]=0.;
     251             :   //diagonalize gyration tensor
     252         400 :   diagMat(gyr_tens, princ_comp, ttransf);
     253         400 :   transpose(ttransf, transf);
     254             :   //sort eigenvalues and eigenvectors
     255         400 :   if (princ_comp[0]<princ_comp[1]) {
     256         400 :     double tmp=princ_comp[0]; princ_comp[0]=princ_comp[1]; princ_comp[1]=tmp;
     257         400 :     for (unsigned i=0; i<3; i++) {tmp=transf[i][0]; transf[i][0]=transf[i][1]; transf[i][1]=tmp;}
     258             :   }
     259         400 :   if (princ_comp[1]<princ_comp[2]) {
     260         400 :     double tmp=princ_comp[1]; princ_comp[1]=princ_comp[2]; princ_comp[2]=tmp;
     261         400 :     for (unsigned i=0; i<3; i++) {tmp=transf[i][1]; transf[i][1]=transf[i][2]; transf[i][2]=tmp;}
     262             :   }
     263         400 :   if (princ_comp[0]<princ_comp[1]) {
     264         400 :     double tmp=princ_comp[0]; princ_comp[0]=princ_comp[1]; princ_comp[1]=tmp;
     265         400 :     for (unsigned i=0; i<3; i++) {tmp=transf[i][0]; transf[i][0]=transf[i][1]; transf[i][1]=tmp;}
     266             :   }
     267             :   //calculate determinant of transformation matrix
     268        1200 :   double det = transf[0][0]*transf[1][1]*transf[2][2]+transf[0][1]*transf[1][2]*transf[2][0]+
     269        1600 :                transf[0][2]*transf[1][0]*transf[2][1]-transf[0][2]*transf[1][1]*transf[2][0]-
     270         800 :                transf[0][1]*transf[1][0]*transf[2][2]-transf[0][0]*transf[1][2]*transf[2][1];
     271             :   // trasformation matrix for rotation must have positive determinant, otherwise multiply one column by (-1)
     272         400 :   if(det<0) {
     273         400 :     for(unsigned j=0; j<3; j++) transf[j][2]=-transf[j][2];
     274         400 :     det = -det;
     275             :   }
     276         400 :   if(fabs(det-1.)>0.0001) error("Plumed Error: Cannot diagonalize gyration tensor\n");
     277         400 :   switch(rg_type) {
     278             :   case GTPC_1:
     279             :   case GTPC_2:
     280             :   case GTPC_3:
     281             :   {
     282         135 :     int pc_index = rg_type-2; //index of principal component
     283         135 :     rgyr=sqrt(princ_comp[pc_index]/totmass);
     284         135 :     double rm = rgyr*totmass;
     285         135 :     if(rm>1e-6) prefactor[pc_index]=1.0/rm; //some parts of derivate
     286         135 :     break;
     287             :   }
     288             :   case GYRATION_3:        //the smallest principal radius of gyration
     289             :   {
     290           0 :     rgyr=sqrt((princ_comp[1]+princ_comp[2])/totmass);
     291           0 :     double rm = rgyr*totmass;
     292           0 :     if (rm>1e-6) {
     293           0 :       prefactor[1]=1.0/rm;
     294           0 :       prefactor[2]=1.0/rm;
     295             :     }
     296           0 :     break;
     297             :   }
     298             :   case GYRATION_2:       //the midle principal radius of gyration
     299             :   {
     300         130 :     rgyr=sqrt((princ_comp[0]+princ_comp[2])/totmass);
     301         130 :     double rm = rgyr*totmass;
     302         130 :     if (rm>1e-6) {
     303         130 :       prefactor[0]=1.0/rm;
     304         130 :       prefactor[2]=1.0/rm;
     305             :     }
     306         130 :     break;
     307             :   }
     308             :   case GYRATION_1:      //the largest principal radius of gyration
     309             :   {
     310           0 :     rgyr=sqrt((princ_comp[0]+princ_comp[1])/totmass);
     311           0 :     double rm = rgyr*totmass;
     312           0 :     if (rm>1e-6) {
     313           0 :       prefactor[0]=1.0/rm;
     314           0 :       prefactor[1]=1.0/rm;
     315             :     }
     316           0 :     break;
     317             :   }
     318             :   case ASPHERICITY:
     319             :   {
     320           5 :     rgyr=sqrt((princ_comp[0]-0.5*(princ_comp[1]+princ_comp[2]))/totmass);
     321           5 :     double rm = rgyr*totmass;
     322           5 :     if (rm>1e-6) {
     323           5 :       prefactor[0]= 1.0/rm;
     324           5 :       prefactor[1]=-0.5/rm;
     325           5 :       prefactor[2]=-0.5/rm;
     326             :     }
     327           5 :     break;
     328             :   }
     329             :   case ACYLINDRICITY:
     330             :   {
     331           0 :     rgyr=sqrt((princ_comp[1]-princ_comp[2])/totmass);
     332           0 :     double rm = rgyr*totmass;
     333           0 :     if (rm>1e-6) {  //avoid division by zero
     334           0 :       prefactor[1]= 1.0/rm;
     335           0 :       prefactor[2]=-1.0/rm;
     336             :     }
     337           0 :     break;
     338             :   }
     339             :   case KAPPA2: // relative shape anisotropy
     340             :   {
     341         130 :     double trace = princ_comp[0]+princ_comp[1]+princ_comp[2];
     342         130 :     double tmp=princ_comp[0]*princ_comp[1]+ princ_comp[1]*princ_comp[2]+ princ_comp[0]*princ_comp[2];
     343         130 :     rgyr=1.0-3*(tmp/(trace*trace));
     344         130 :     if (rgyr>1e-6) {
     345         130 :       prefactor[0]= -3*((princ_comp[1]+princ_comp[2])-2*tmp/trace)/(trace*trace) *2;
     346         130 :       prefactor[1]= -3*((princ_comp[0]+princ_comp[2])-2*tmp/trace)/(trace*trace) *2;
     347         130 :       prefactor[2]= -3*((princ_comp[0]+princ_comp[1])-2*tmp/trace)/(trace*trace) *2;
     348             :     }
     349         130 :     break;
     350             :   }
     351             :   }
     352             : 
     353         400 :   if(use_masses) {
     354           0 :     for(unsigned i=0; i<getNumberOfAtoms(); i++) {
     355           0 :       Vector tX;
     356           0 :       const Vector diff=delta( com,getPosition(i) );
     357             :       //project atomic postional vectors to diagonalized frame
     358           0 :       for(unsigned j=0; j<3; j++) tX[j]=transf[0][j]*diff[0]+transf[1][j]*diff[1]+transf[2][j]*diff[2];
     359           0 :       for(unsigned j=0; j<3; j++) derivatives[i][j]=getMass(i)*(prefactor[0]*transf[j][0]*tX[0]+
     360           0 :             prefactor[1]*transf[j][1]*tX[1]+
     361           0 :             prefactor[2]*transf[j][2]*tX[2]);
     362           0 :       setAtomsDerivatives(i,derivatives[i]);
     363             :     }
     364             :   } else {
     365        2400 :     for(unsigned i=0; i<getNumberOfAtoms(); i++) {
     366        2000 :       Vector tX;
     367        2000 :       const Vector diff=delta( com,getPosition(i) );
     368             :       //project atomic postional vectors to diagonalized frame
     369        2000 :       for(unsigned j=0; j<3; j++) tX[j]=transf[0][j]*diff[0]+transf[1][j]*diff[1]+transf[2][j]*diff[2];
     370       14000 :       for(unsigned j=0; j<3; j++) derivatives[i][j]=prefactor[0]*transf[j][0]*tX[0]+
     371       18000 :             prefactor[1]*transf[j][1]*tX[1]+
     372       12000 :             prefactor[2]*transf[j][2]*tX[2];
     373        2000 :       setAtomsDerivatives(i,derivatives[i]);
     374             :     }
     375             :   }
     376             : 
     377         400 :   setValue(rgyr);
     378         800 :   setBoxDerivativesNoPbc();
     379             : }
     380             : 
     381             : }
     382        2523 : }

Generated by: LCOV version 1.13