LCOV - code coverage report
Current view: top level - colvar - Dimer.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 89 95 93.7 %
Date: 2026-11-12 10:48:31 Functions: 4 5 80.0 %

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

Generated by: LCOV version 1.16