LCOV - code coverage report
Current view: top level - function - Ensemble.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 82 130 63.1 %
Date: 2024-10-11 08:09:47 Functions: 6 7 85.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2014-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 "Function.h"
      23             : #include "ActionRegister.h"
      24             : #include "core/PlumedMain.h"
      25             : #include "core/Atoms.h"
      26             : 
      27             : namespace PLMD {
      28             : namespace function {
      29             : 
      30             : //+PLUMEDOC FUNCTION ENSEMBLE
      31             : /*
      32             : Calculates the replica averaging of a collective variable over multiple replicas.
      33             : 
      34             : Each collective variable is averaged separately and stored in a component labelled <em>label</em>.cvlabel.
      35             : 
      36             : \par Examples
      37             : 
      38             : The following input tells plumed to calculate the distance between atoms 3 and 5
      39             : and the average it over the available replicas.
      40             : \plumedfile
      41             : dist: DISTANCE ATOMS=3,5
      42             : ens: ENSEMBLE ARG=dist
      43             : PRINT ARG=dist,ens.dist
      44             : \endplumedfile
      45             : 
      46             : */
      47             : //+ENDPLUMEDOC
      48             : 
      49             : 
      50             : class Ensemble :
      51             :   public Function
      52             : {
      53             :   unsigned ens_dim;
      54             :   unsigned my_repl;
      55             :   unsigned narg;
      56             :   bool     master;
      57             :   bool     do_reweight;
      58             :   bool     do_moments;
      59             :   bool     do_central;
      60             :   bool     do_powers;
      61             :   double   kbt;
      62             :   double   moment;
      63             :   double   power;
      64             : public:
      65             :   explicit Ensemble(const ActionOptions&);
      66             :   void     calculate() override;
      67             :   static void registerKeywords(Keywords& keys);
      68             : };
      69             : 
      70             : 
      71       10473 : PLUMED_REGISTER_ACTION(Ensemble,"ENSEMBLE")
      72             : 
      73          28 : void Ensemble::registerKeywords(Keywords& keys) {
      74          28 :   Function::registerKeywords(keys);
      75          28 :   keys.use("ARG");
      76          56 :   keys.addFlag("REWEIGHT",false,"simple REWEIGHT using the latest ARG as energy");
      77          56 :   keys.addFlag("CENTRAL",false,"calculate a central moment instead of a standard moment");
      78          56 :   keys.add("optional","TEMP","the system temperature - this is only needed if you are reweighting");
      79          56 :   keys.add("optional","MOMENT","the moment you want to calculate in alternative to the mean or the variance");
      80          56 :   keys.add("optional","POWER","the power of the mean (and moment)");
      81          28 :   ActionWithValue::useCustomisableComponents(keys);
      82          28 : }
      83             : 
      84          27 : Ensemble::Ensemble(const ActionOptions&ao):
      85             :   Action(ao),
      86             :   Function(ao),
      87          27 :   do_reweight(false),
      88          27 :   do_moments(false),
      89          27 :   do_central(false),
      90          27 :   do_powers(false),
      91          27 :   kbt(-1.0),
      92          27 :   moment(0),
      93          27 :   power(0)
      94             : {
      95          27 :   parseFlag("REWEIGHT", do_reweight);
      96          27 :   double temp=0.0;
      97          27 :   parse("TEMP",temp);
      98          27 :   if(do_reweight) {
      99          12 :     if(temp>0.0) kbt=plumed.getAtoms().getKBoltzmann()*temp;
     100           0 :     else kbt=plumed.getAtoms().getKbT();
     101          12 :     if(kbt==0.0) error("Unless the MD engine passes the temperature to plumed, with REWEIGHT you must specify TEMP");
     102             :   }
     103             : 
     104          27 :   parse("MOMENT",moment);
     105          27 :   if(moment==1) error("MOMENT can be any number but for 0 and 1");
     106          27 :   if(moment!=0) do_moments=true;
     107          27 :   parseFlag("CENTRAL", do_central);
     108          27 :   if(!do_moments&&do_central) error("To calculate a CENTRAL moment you need to define for which MOMENT");
     109             : 
     110          27 :   parse("POWER",power);
     111          27 :   if(power==1) error("POWER can be any number but for 0 and 1");
     112          27 :   if(power!=0) do_powers=true;
     113             : 
     114          27 :   checkRead();
     115             : 
     116          27 :   master = (comm.Get_rank()==0);
     117          27 :   ens_dim=0;
     118          27 :   my_repl=0;
     119          27 :   if(master) {
     120          17 :     ens_dim=multi_sim_comm.Get_size();
     121          17 :     my_repl=multi_sim_comm.Get_rank();
     122             :   }
     123          27 :   comm.Bcast(ens_dim,0);
     124          27 :   comm.Bcast(my_repl,0);
     125          27 :   if(ens_dim<2) log.printf("WARNING: ENSEMBLE with one replica is not doing any averaging!\n");
     126             : 
     127             :   // prepare output components, the number depending on reweighing or not
     128          27 :   narg = getNumberOfArguments();
     129          27 :   if(do_reweight) narg--;
     130             : 
     131             :   // these are the averages
     132        3044 :   for(unsigned i=0; i<narg; i++) {
     133        3017 :     std::string s=getPntrToArgument(i)->getName();
     134        3017 :     addComponentWithDerivatives(s);
     135        3017 :     getPntrToComponent(i)->setNotPeriodic();
     136             :   }
     137             :   // these are the moments
     138          27 :   if(do_moments) {
     139           0 :     for(unsigned i=0; i<narg; i++) {
     140           0 :       std::string s=getPntrToArgument(i)->getName()+"_m";
     141           0 :       addComponentWithDerivatives(s);
     142           0 :       getPntrToComponent(i+narg)->setNotPeriodic();
     143             :     }
     144             :   }
     145             : 
     146          27 :   log.printf("  averaging over %u replicas.\n", ens_dim);
     147          27 :   if(do_reweight) log.printf("  doing simple REWEIGHT using the latest ARGUMENT as energy.\n");
     148          27 :   if(do_moments&&!do_central)  log.printf("  calculating also the %lf standard moment\n", moment);
     149          27 :   if(do_moments&&do_central)   log.printf("  calculating also the %lf central moment\n", moment);
     150          27 :   if(do_powers)                log.printf("  calculating the %lf power of the mean (and moment)\n", power);
     151          27 : }
     152             : 
     153         125 : void Ensemble::calculate() {
     154             :   double norm = 0.0;
     155         125 :   double fact = 0.0;
     156             : 
     157             :   // calculate the weights either from BIAS
     158         125 :   if(do_reweight) {
     159             :     std::vector<double> bias;
     160           0 :     bias.resize(ens_dim);
     161           0 :     if(master) {
     162           0 :       bias[my_repl] = getArgument(narg);
     163           0 :       if(ens_dim>1) multi_sim_comm.Sum(&bias[0], ens_dim);
     164             :     }
     165           0 :     comm.Sum(&bias[0], ens_dim);
     166           0 :     const double maxbias = *(std::max_element(bias.begin(), bias.end()));
     167           0 :     for(unsigned i=0; i<ens_dim; ++i) {
     168           0 :       bias[i] = exp((bias[i]-maxbias)/kbt);
     169           0 :       norm += bias[i];
     170             :     }
     171           0 :     fact = bias[my_repl]/norm;
     172             :     // or arithmetic ones
     173             :   } else {
     174         125 :     norm = static_cast<double>(ens_dim);
     175         125 :     fact = 1.0/norm;
     176             :   }
     177             : 
     178         125 :   const double fact_kbt = fact/kbt;
     179             : 
     180         125 :   std::vector<double> mean(narg);
     181         125 :   std::vector<double> dmean(narg,fact);
     182             :   // calculate the mean
     183         125 :   if(master) {
     184        2106 :     for(unsigned i=0; i<narg; ++i) mean[i] = fact*getArgument(i);
     185          99 :     if(ens_dim>1) multi_sim_comm.Sum(&mean[0], narg);
     186             :   }
     187         125 :   comm.Sum(&mean[0], narg);
     188             : 
     189             :   std::vector<double> v_moment, dv_moment;
     190             :   // calculate other moments
     191         125 :   if(do_moments) {
     192           0 :     v_moment.resize(narg);
     193           0 :     dv_moment.resize(narg);
     194             :     // standard moment
     195           0 :     if(!do_central) {
     196           0 :       if(master) {
     197           0 :         for(unsigned i=0; i<narg; ++i) {
     198           0 :           const double tmp = fact*std::pow(getArgument(i),moment-1);
     199           0 :           v_moment[i]      = tmp*getArgument(i);
     200           0 :           dv_moment[i]     = moment*tmp;
     201             :         }
     202           0 :         if(ens_dim>1) multi_sim_comm.Sum(&v_moment[0], narg);
     203             :       } else {
     204           0 :         for(unsigned i=0; i<narg; ++i) {
     205           0 :           const double tmp = fact*std::pow(getArgument(i),moment-1);
     206           0 :           dv_moment[i]     = moment*tmp;
     207             :         }
     208             :       }
     209             :       // central moment
     210             :     } else {
     211           0 :       if(master) {
     212           0 :         for(unsigned i=0; i<narg; ++i) {
     213           0 :           const double tmp = std::pow(getArgument(i)-mean[i],moment-1);
     214           0 :           v_moment[i]      = fact*tmp*(getArgument(i)-mean[i]);
     215           0 :           dv_moment[i]     = moment*tmp*(fact-fact/norm);
     216             :         }
     217           0 :         if(ens_dim>1) multi_sim_comm.Sum(&v_moment[0], narg);
     218             :       } else {
     219           0 :         for(unsigned i=0; i<narg; ++i) {
     220           0 :           const double tmp = std::pow(getArgument(i)-mean[i],moment-1);
     221           0 :           dv_moment[i]     = moment*tmp*(fact-fact/norm);
     222             :         }
     223             :       }
     224             :     }
     225           0 :     comm.Sum(&v_moment[0], narg);
     226             :   }
     227             : 
     228             :   // calculate powers of moments
     229         125 :   if(do_powers) {
     230          72 :     for(unsigned i=0; i<narg; ++i) {
     231          48 :       const double tmp1 = std::pow(mean[i],power-1);
     232          48 :       mean[i]          *= tmp1;
     233          48 :       dmean[i]         *= power*tmp1;
     234          48 :       if(do_moments) {
     235           0 :         const double tmp2 = std::pow(v_moment[i],power-1);
     236           0 :         v_moment[i]      *= tmp2;
     237           0 :         dv_moment[i]     *= power*tmp2;
     238             :       }
     239             :     }
     240             :   }
     241             : 
     242             :   // set components
     243        3358 :   for(unsigned i=0; i<narg; ++i) {
     244             :     // set mean
     245        3233 :     Value* v=getPntrToComponent(i);
     246        3233 :     v->set(mean[i]);
     247        6466 :     setDerivative(v, i, dmean[i]);
     248        3233 :     if(do_reweight) {
     249           0 :       const double w_tmp = fact_kbt*(getArgument(i) - mean[i]);
     250           0 :       setDerivative(v, narg, w_tmp);
     251             :     }
     252        3233 :     if(do_moments) {
     253             :       // set moments
     254           0 :       Value* u=getPntrToComponent(i+narg);
     255           0 :       u->set(v_moment[i]);
     256           0 :       setDerivative(u, i, dv_moment[i]);
     257           0 :       if(do_reweight) {
     258           0 :         const double w_tmp = fact_kbt*(pow(getArgument(i),moment) - v_moment[i]);
     259           0 :         setDerivative(u, narg, w_tmp);
     260             :       }
     261             :     }
     262             :   }
     263         125 : }
     264             : 
     265             : }
     266             : }

Generated by: LCOV version 1.15