LCOV - code coverage report
Current view: top level - colvar - Gyration.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 143 184 77.7 %
Date: 2024-10-18 13:59:31 Functions: 3 4 75.0 %

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

Generated by: LCOV version 1.16