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

Generated by: LCOV version 1.16