LCOV - code coverage report
Current view: top level - colvar - Dimer.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 83 90 92.2 %
Date: 2024-10-11 08:09:47 Functions: 7 8 87.5 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2017-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 "ActionRegister.h"
      24             : #include "core/PlumedMain.h"
      25             : 
      26             : namespace PLMD {
      27             : namespace colvar {
      28             : 
      29             : //+PLUMEDOC COLVAR DIMER
      30             : /*
      31             : This CV computes the dimer interaction energy for a collection of dimers.
      32             : 
      33             : Each dimer represents an atom, as described in the dimer paper \cite dimer-metad.
      34             : A system of N atoms is thus represented with N dimers, each
      35             : Dimer being composed of two beads and eventually a virtual site representing its center of mass.
      36             : 
      37             : A typical configuration for a dimerized system has the following ordering of atoms:
      38             : 
      39             : 1    TAG1 X Y Z          N atoms representing the first bead of each Dimer
      40             : 
      41             : 2    TAG2 X Y Z
      42             : 
      43             : ...
      44             : 
      45             : N    TAGN X Y Z          N atoms representing the second bead of each Dimer
      46             : 
      47             : N+1  TAG1 X Y Z
      48             : 
      49             : N+2  TAG2 X Y Z
      50             : 
      51             : ...
      52             : 
      53             : 2N   TAGN X Y Z          Optional: N atoms representing the center of mass of each Dimer
      54             : 
      55             : 2N+1 TAG1 X Y Z
      56             : 
      57             : 2N+2 TAG2 X Y Z
      58             : 
      59             : ...
      60             : 
      61             : 3N   TAGN X Y Z          The configuration might go on with un-dimerized atoms (like a solvent)
      62             : 
      63             : 3N+1
      64             : 
      65             : 3N+2
      66             : 
      67             : ...
      68             : 
      69             : 
      70             : The Dimer interaction energy is defined between atoms x and N+x, for x=1,...,N and is
      71             : characterized by two parameters Q and DSIGMA. These are passed as mandatory arguments along with
      72             : the temperature of the system.
      73             : 
      74             : \par Examples
      75             : 
      76             : This line tells Plumed to compute the Dimer interaction energy for every dimer in the system.
      77             : 
      78             : \plumedfile
      79             : dim: DIMER TEMP=300 Q=0.5 ALLATOMS DSIGMA=0.002
      80             : \endplumedfile
      81             : 
      82             : If the simulation doesn't use virtual sites for the dimers centers of mass,
      83             : Plumed has to know in order to determine correctly the total number of dimers from
      84             : the total number of atoms:
      85             : \plumedfile
      86             : dim: DIMER TEMP=300 Q=0.5 ALLATOMS DSIGMA=0.002 NOVSITES
      87             : \endplumedfile
      88             : 
      89             : The NOVSITES flag is not required if one provides the atom serials of each Dimer. These are
      90             : defined through two lists of atoms provided __instead__ of the ALLATOMS keyword.
      91             : For example, the Dimer interaction energy of dimers specified by beads (1;23),(5;27),(7;29) is:
      92             : \plumedfile
      93             : dim: DIMER TEMP=300 Q=0.5 ATOMS1=1,5,7 ATOMS2=23,27,29 DSIGMA=0.002
      94             : \endplumedfile
      95             : 
      96             : Note that the ATOMS1,ATOMS2 keywords can support atom groups and
      97             : interval notation as defined in \ref GROUP.
      98             : 
      99             : 
     100             : In a Replica Exchange simulation the keyword DSIGMA can be used in two ways:
     101             : if a plumed.n.dat file is provided for each replica, then DSIGMA is passed as a single value,
     102             : like in the previous examples, and each replica will read its own DSIGMA value. If
     103             : a unique plumed.dat is given, DSIGMA has to be a list containing a value for each replica.
     104             : For 4 replicas:
     105             : \plumedfile
     106             : #SETTINGS NREPLICAS=4
     107             : dim: DIMER TEMP=300 Q=0.5 ATOMS1=1,5,7 ATOMS2=23,27,29 DSIGMA=0.002,0.002,0.004,0.01
     108             : \endplumedfile
     109             : 
     110             : 
     111             : \par Usage of the CV
     112             : 
     113             : The dimer interaction is not coded in the driver program and has to be inserted
     114             : in the Hamiltonian of the system as a linear RESTRAINT (see \ref RESTRAINT):
     115             : \plumedfile
     116             : dim: DIMER TEMP=300 Q=0.5 ALLATOMS DSIGMA=0.002
     117             : RESTRAINT ARG=dim AT=0 KAPPA=0 SLOPE=1 LABEL=dimforces
     118             : \endplumedfile
     119             : 
     120             : In a replica exchange, Metadynamics (see \ref METAD) can be used on the Dimer CV to reduce
     121             : the number of replicas. Just keep in mind that METAD SIGMA values should be tuned
     122             : in the standard way for each replica according to the value of DSIGMA.
     123             : */
     124             : //+ENDPLUMEDOC
     125             : 
     126             : class Dimer : public Colvar {
     127             : public:
     128             :   static void registerKeywords( Keywords& keys);
     129             :   explicit Dimer(const ActionOptions&);
     130             :   void calculate() override;
     131             : protected:
     132             :   bool trimer,useall;
     133             :   int myrank, nranks;
     134             :   double qexp,temperature,beta,dsigma;
     135             :   std::vector<double> dsigmas;
     136             : private:
     137             :   void consistencyCheck();
     138             :   std::vector<AtomNumber> usedatoms1;
     139             :   std::vector<AtomNumber> usedatoms2;
     140             : 
     141             : };
     142             : 
     143       10423 : PLUMED_REGISTER_ACTION(Dimer, "DIMER")
     144             : 
     145             : 
     146             : 
     147           3 : void Dimer::registerKeywords( Keywords& keys) {
     148           3 :   Colvar::registerKeywords(keys);
     149             : 
     150           6 :   keys.add("compulsory","DSIGMA","The interaction strength of the dimer bond.");
     151           6 :   keys.add("compulsory", "Q", "The exponent of the dimer potential.");
     152           6 :   keys.add("compulsory", "TEMP", "The temperature (in Kelvin) of the simulation.");
     153           6 :   keys.add("atoms", "ATOMS1", "The list of atoms representing the first bead of each Dimer being considered by this CV. Used if ALLATOMS flag is missing");
     154           6 :   keys.add("atoms", "ATOMS2", "The list of atoms representing the second bead of each Dimer being considered by this CV. Used if ALLATOMS flag is missing");
     155           6 :   keys.addFlag("ALLATOMS", false, "Use EVERY atom of the system. Overrides ATOMS keyword.");
     156           6 :   keys.addFlag("NOVSITES", false, "If present the configuration is without virtual sites at the centroid positions.");
     157             : 
     158           3 : }
     159             : 
     160             : 
     161             : 
     162           2 : Dimer::Dimer(const ActionOptions& ao):
     163           2 :   PLUMED_COLVAR_INIT(ao)
     164             : {
     165             : 
     166           4 :   log<<" Bibliography "<<plumed.cite("M Nava, F. Palazzesi, C. Perego and M. Parrinello, J. Chem. Theory Comput. 13, 425(2017)")<<"\n";
     167           2 :   parseVector("DSIGMA",dsigmas);
     168           2 :   parse("Q",qexp);
     169           4 :   parse("TEMP",temperature);
     170             : 
     171             : 
     172             :   std::vector<AtomNumber> atoms;
     173           2 :   parseFlag("ALLATOMS",useall);
     174           2 :   trimer=true;
     175             :   bool notrim;
     176           2 :   parseFlag("NOVSITES",notrim);
     177           2 :   trimer=!notrim;
     178             : 
     179           2 :   nranks=multi_sim_comm.Get_size();
     180           2 :   myrank=multi_sim_comm.Get_rank();
     181           2 :   if(dsigmas.size()==1)
     182           2 :     dsigma=dsigmas[0];
     183             :   else
     184           0 :     dsigma=dsigmas[myrank];
     185             : 
     186             : 
     187             : 
     188             : 
     189           2 :   if(useall)
     190             :   {
     191             :     // go with every atom in the system but not the virtuals...
     192             :     int natoms;
     193           1 :     if(trimer)
     194           1 :       natoms= 2*getTotAtoms()/3;
     195             :     else
     196           0 :       natoms=getTotAtoms()/2;
     197             : 
     198          45 :     for(unsigned int i=0; i<((unsigned int)natoms); i++)
     199             :     {
     200             :       AtomNumber ati;
     201             :       ati.setIndex(i);
     202          44 :       atoms.push_back(ati);
     203             :     }
     204             :   }
     205             :   else  // serials for the first beads of each dimer are given
     206             :   {
     207           1 :     parseAtomList("ATOMS1",usedatoms1);
     208           2 :     parseAtomList("ATOMS2",usedatoms2);
     209             : 
     210             :     int isz1 = usedatoms1.size();
     211             : 
     212           5 :     for(unsigned int i=0; i<isz1; i++)
     213             :     {
     214             :       AtomNumber ati;
     215           4 :       ati.setIndex(usedatoms1[i].index());
     216           4 :       atoms.push_back(ati);
     217             :     }
     218             : 
     219             :     int isz2 = usedatoms2.size();
     220           5 :     for(unsigned int i=0; i<isz2; i++)
     221             :     {
     222             :       AtomNumber atip2;
     223           4 :       atip2.setIndex(usedatoms2[i].index());
     224           4 :       atoms.push_back(atip2);
     225             :     }
     226             : 
     227             :   }
     228           2 :   consistencyCheck();
     229           2 :   checkRead();
     230           2 :   beta = 1./(kBoltzmann*temperature);
     231             : 
     232           2 :   addValueWithDerivatives();  // allocate
     233           2 :   requestAtoms(atoms);
     234           2 :   setNotPeriodic();
     235           2 : }
     236             : 
     237           4 : void Dimer::calculate()
     238             : {
     239           4 :   double cv_val=0;
     240           4 :   Tensor virial;
     241             :   std::vector<Vector> derivatives;
     242           4 :   std::vector<Vector> my_pos=getPositions();
     243           4 :   int atms = my_pos.size();
     244             :   std::vector<Vector> der_b2;
     245          38 :   for(int i=0; i<atms/2; i++)
     246             :   {
     247          34 :     Vector dist;
     248          34 :     dist = pbcDistance(my_pos[i],my_pos[i+atms/2]);
     249             :     double distquad=0;
     250         136 :     for(int j=0; j<3; j++)
     251         102 :       distquad += dist[j]*dist[j];
     252             : 
     253          34 :     double dsigquad = dsigma*dsigma;
     254          34 :     double fac1 = 1.0 + distquad/(2*qexp*dsigquad);
     255          34 :     double fac1qm1 = std::pow(fac1,qexp-1);
     256             : 
     257             : 
     258          34 :     cv_val += (fac1*fac1qm1-1.0)/beta;
     259          34 :     Vector der_val;
     260          34 :     Vector mder_val;
     261         136 :     for(int j=0; j<3; j++)
     262             :     {
     263         102 :       der_val[j] = -fac1qm1*dist[j]/(dsigquad*beta);
     264         102 :       mder_val[j]=-der_val[j];
     265             :     }
     266          34 :     derivatives.push_back(der_val);
     267          34 :     der_b2.push_back(mder_val);
     268             : 
     269             :     // virial part: each dimer contributes -x_{ij}*ds/dx_{ij}  (s is the CV)
     270          34 :     double dfunc = fac1qm1/(beta*dsigquad);
     271          34 :     Vector dd(dfunc*dist);
     272          34 :     Tensor vv(dd,dist);
     273          34 :     virial -= vv;
     274             : 
     275             :   }
     276             : 
     277           4 :   derivatives.insert(derivatives.end(), der_b2.begin(), der_b2.end());
     278             : 
     279          72 :   for(unsigned int i=0; i<derivatives.size(); i++)
     280          68 :     setAtomsDerivatives(i,derivatives[i]);
     281             : 
     282           4 :   setValue(cv_val);
     283           4 :   setBoxDerivatives(virial);
     284             : 
     285           4 : }
     286             : 
     287             : 
     288             : 
     289             : /*****************
     290             : There are some conditions that a valid input should satisfy.
     291             : These are checked here and PLUMED error handlers are (eventually) called.
     292             : ******************/
     293           2 : void Dimer::consistencyCheck()
     294             : {
     295           2 :   if(!useall && usedatoms1.size()!=usedatoms2.size())
     296           0 :     error("The provided atom lists are of different sizes.");
     297             : 
     298           2 :   if(qexp<0.5 || qexp>1)
     299           0 :     warning("Dimer CV is meant to be used with q-exponents between 0.5 and 1. We are not responsible for any black hole. :-)");
     300             : 
     301           2 :   if(dsigma<0)
     302           0 :     error("Please use positive sigma values for the Dimer strength constant");
     303             : 
     304           2 :   if(temperature<0)
     305           0 :     error("Please, use a positive value for the temperature...");
     306             : 
     307             :   // if dsigmas has only one element means that either
     308             :   // you are using different plumed.x.dat files or a plumed.dat with a single replica
     309           2 :   if(dsigmas.size()!=nranks && dsigmas.size()!=1)
     310           0 :     error("Mismatch between provided sigmas and number of replicas");
     311             : 
     312           2 : }
     313             : 
     314             : 
     315             : }
     316             : }
     317             : 

Generated by: LCOV version 1.15