LCOV - code coverage report
Current view: top level - function - Ensemble.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 80 132 60.6 %
Date: 2024-10-18 14:00:25 Functions: 3 5 60.0 %

          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 "tools/Communicator.h"
      24             : #include "core/ActionRegister.h"
      25             : #include "core/PlumedMain.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             :   std::string getOutputComponentDescription( const std::string& cname, const Keywords& keys ) const override ;
      67             :   void     calculate() override;
      68             :   static void registerKeywords(Keywords& keys);
      69             : };
      70             : 
      71             : 
      72             : PLUMED_REGISTER_ACTION(Ensemble,"ENSEMBLE")
      73             : 
      74          29 : void Ensemble::registerKeywords(Keywords& keys) {
      75          29 :   Function::registerKeywords(keys);
      76          29 :   keys.use("ARG");
      77          58 :   keys.addFlag("REWEIGHT",false,"simple REWEIGHT using the latest ARG as energy");
      78          58 :   keys.addFlag("CENTRAL",false,"calculate a central moment instead of a standard moment");
      79          58 :   keys.add("optional","TEMP","the system temperature - this is only needed if you are reweighting");
      80          58 :   keys.add("optional","MOMENT","the moment you want to calculate in alternative to the mean or the variance");
      81          58 :   keys.add("optional","POWER","the power of the mean (and moment)");
      82          29 :   ActionWithValue::useCustomisableComponents(keys);
      83          29 : }
      84             : 
      85          27 : Ensemble::Ensemble(const ActionOptions&ao):
      86             :   Action(ao),
      87             :   Function(ao),
      88          27 :   do_reweight(false),
      89          27 :   do_moments(false),
      90          27 :   do_central(false),
      91          27 :   do_powers(false),
      92          27 :   kbt(-1.0),
      93          27 :   moment(0),
      94          27 :   power(0)
      95             : {
      96          27 :   parseFlag("REWEIGHT", do_reweight);
      97          27 :   if(do_reweight) {
      98          12 :     kbt=getkBT();
      99          12 :     if(kbt==0.0) error("Unless the MD engine passes the temperature to plumed, with REWEIGHT you must specify TEMP");
     100          30 :   } else { double temp=0.0; parse("TEMP",temp); }
     101             : 
     102          27 :   parse("MOMENT",moment);
     103          27 :   if(moment==1) error("MOMENT can be any number but for 0 and 1");
     104          27 :   if(moment!=0) do_moments=true;
     105          27 :   parseFlag("CENTRAL", do_central);
     106          27 :   if(!do_moments&&do_central) error("To calculate a CENTRAL moment you need to define for which MOMENT");
     107             : 
     108          27 :   parse("POWER",power);
     109          27 :   if(power==1) error("POWER can be any number but for 0 and 1");
     110          27 :   if(power!=0) do_powers=true;
     111             : 
     112          27 :   checkRead();
     113             : 
     114          27 :   master = (comm.Get_rank()==0);
     115          27 :   ens_dim=0;
     116          27 :   my_repl=0;
     117          27 :   if(master) {
     118          17 :     ens_dim=multi_sim_comm.Get_size();
     119          17 :     my_repl=multi_sim_comm.Get_rank();
     120             :   }
     121          27 :   comm.Bcast(ens_dim,0);
     122          27 :   comm.Bcast(my_repl,0);
     123          27 :   if(ens_dim<2) log.printf("WARNING: ENSEMBLE with one replica is not doing any averaging!\n");
     124             : 
     125             :   // prepare output components, the number depending on reweighing or not
     126          27 :   narg = getNumberOfArguments();
     127          27 :   if(do_reweight) narg--;
     128             : 
     129             :   // these are the averages
     130        3044 :   for(unsigned i=0; i<narg; i++) {
     131        3017 :     std::string s=getPntrToArgument(i)->getName();
     132        3017 :     addComponentWithDerivatives(s);
     133        3017 :     getPntrToComponent(i)->setNotPeriodic();
     134             :   }
     135             :   // these are the moments
     136          27 :   if(do_moments) {
     137           0 :     for(unsigned i=0; i<narg; i++) {
     138           0 :       std::string s=getPntrToArgument(i)->getName()+"_m";
     139           0 :       addComponentWithDerivatives(s);
     140           0 :       getPntrToComponent(i+narg)->setNotPeriodic();
     141             :     }
     142             :   }
     143             : 
     144          27 :   log.printf("  averaging over %u replicas.\n", ens_dim);
     145          27 :   if(do_reweight) log.printf("  doing simple REWEIGHT using the latest ARGUMENT as energy.\n");
     146          27 :   if(do_moments&&!do_central)  log.printf("  calculating also the %lf standard moment\n", moment);
     147          27 :   if(do_moments&&do_central)   log.printf("  calculating also the %lf central moment\n", moment);
     148          27 :   if(do_powers)                log.printf("  calculating the %lf power of the mean (and moment)\n", power);
     149          27 : }
     150             : 
     151           0 : std::string Ensemble::getOutputComponentDescription( const std::string& cname, const Keywords& keys ) const {
     152           0 :   for(unsigned i=0; i<getNumberOfArguments(); ++i) {
     153           0 :     if( cname==getPntrToArgument(i)->getName() ) return "the average for argument " + cname;
     154           0 :     if( cname==getPntrToArgument(i)->getName() + "_m" ) return "the moment for argument " + cname;
     155             :   }
     156           0 :   plumed_error(); return "";
     157             : }
     158             : 
     159             : 
     160         125 : void Ensemble::calculate() {
     161             :   double norm = 0.0;
     162         125 :   double fact = 0.0;
     163             : 
     164             :   // calculate the weights either from BIAS
     165         125 :   if(do_reweight) {
     166             :     std::vector<double> bias;
     167           0 :     bias.resize(ens_dim);
     168           0 :     if(master) {
     169           0 :       bias[my_repl] = getArgument(narg);
     170           0 :       if(ens_dim>1) multi_sim_comm.Sum(&bias[0], ens_dim);
     171             :     }
     172           0 :     comm.Sum(&bias[0], ens_dim);
     173           0 :     const double maxbias = *(std::max_element(bias.begin(), bias.end()));
     174           0 :     for(unsigned i=0; i<ens_dim; ++i) {
     175           0 :       bias[i] = exp((bias[i]-maxbias)/kbt);
     176           0 :       norm += bias[i];
     177             :     }
     178           0 :     fact = bias[my_repl]/norm;
     179             :     // or arithmetic ones
     180             :   } else {
     181         125 :     norm = static_cast<double>(ens_dim);
     182         125 :     fact = 1.0/norm;
     183             :   }
     184             : 
     185         125 :   const double fact_kbt = fact/kbt;
     186             : 
     187         125 :   std::vector<double> mean(narg);
     188         125 :   std::vector<double> dmean(narg,fact);
     189             :   // calculate the mean
     190         125 :   if(master) {
     191        2106 :     for(unsigned i=0; i<narg; ++i) mean[i] = fact*getArgument(i);
     192          99 :     if(ens_dim>1) multi_sim_comm.Sum(&mean[0], narg);
     193             :   }
     194         125 :   comm.Sum(&mean[0], narg);
     195             : 
     196             :   std::vector<double> v_moment, dv_moment;
     197             :   // calculate other moments
     198         125 :   if(do_moments) {
     199           0 :     v_moment.resize(narg);
     200           0 :     dv_moment.resize(narg);
     201             :     // standard moment
     202           0 :     if(!do_central) {
     203           0 :       if(master) {
     204           0 :         for(unsigned i=0; i<narg; ++i) {
     205           0 :           const double tmp = fact*std::pow(getArgument(i),moment-1);
     206           0 :           v_moment[i]      = tmp*getArgument(i);
     207           0 :           dv_moment[i]     = moment*tmp;
     208             :         }
     209           0 :         if(ens_dim>1) multi_sim_comm.Sum(&v_moment[0], narg);
     210             :       } else {
     211           0 :         for(unsigned i=0; i<narg; ++i) {
     212           0 :           const double tmp = fact*std::pow(getArgument(i),moment-1);
     213           0 :           dv_moment[i]     = moment*tmp;
     214             :         }
     215             :       }
     216             :       // central moment
     217             :     } else {
     218           0 :       if(master) {
     219           0 :         for(unsigned i=0; i<narg; ++i) {
     220           0 :           const double tmp = std::pow(getArgument(i)-mean[i],moment-1);
     221           0 :           v_moment[i]      = fact*tmp*(getArgument(i)-mean[i]);
     222           0 :           dv_moment[i]     = moment*tmp*(fact-fact/norm);
     223             :         }
     224           0 :         if(ens_dim>1) multi_sim_comm.Sum(&v_moment[0], narg);
     225             :       } else {
     226           0 :         for(unsigned i=0; i<narg; ++i) {
     227           0 :           const double tmp = std::pow(getArgument(i)-mean[i],moment-1);
     228           0 :           dv_moment[i]     = moment*tmp*(fact-fact/norm);
     229             :         }
     230             :       }
     231             :     }
     232           0 :     comm.Sum(&v_moment[0], narg);
     233             :   }
     234             : 
     235             :   // calculate powers of moments
     236         125 :   if(do_powers) {
     237          72 :     for(unsigned i=0; i<narg; ++i) {
     238          48 :       const double tmp1 = std::pow(mean[i],power-1);
     239          48 :       mean[i]          *= tmp1;
     240          48 :       dmean[i]         *= power*tmp1;
     241          48 :       if(do_moments) {
     242           0 :         const double tmp2 = std::pow(v_moment[i],power-1);
     243           0 :         v_moment[i]      *= tmp2;
     244           0 :         dv_moment[i]     *= power*tmp2;
     245             :       }
     246             :     }
     247             :   }
     248             : 
     249             :   // set components
     250        3358 :   for(unsigned i=0; i<narg; ++i) {
     251             :     // set mean
     252        3233 :     Value* v=getPntrToComponent(i);
     253        3233 :     v->set(mean[i]);
     254        3233 :     setDerivative(v, i, dmean[i]);
     255        3233 :     if(do_reweight) {
     256           0 :       const double w_tmp = fact_kbt*(getArgument(i) - mean[i]);
     257           0 :       setDerivative(v, narg, w_tmp);
     258             :     }
     259        3233 :     if(do_moments) {
     260             :       // set moments
     261           0 :       Value* u=getPntrToComponent(i+narg);
     262           0 :       u->set(v_moment[i]);
     263           0 :       setDerivative(u, i, dv_moment[i]);
     264           0 :       if(do_reweight) {
     265           0 :         const double w_tmp = fact_kbt*(pow(getArgument(i),moment) - v_moment[i]);
     266           0 :         setDerivative(u, narg, w_tmp);
     267             :       }
     268             :     }
     269             :   }
     270         125 : }
     271             : 
     272             : }
     273             : }

Generated by: LCOV version 1.16