LCOV - code coverage report
Current view: top level - opes - ExpansionCVs.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 96 106 90.6 %
Date: 2024-10-18 14:00:25 Functions: 9 11 81.8 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2020-2021 of Michele Invernizzi.
       3             : 
       4             :    This file is part of the OPES plumed module.
       5             : 
       6             :    The OPES plumed module is free software: you can redistribute it and/or modify
       7             :    it under the terms of the GNU Lesser General Public License as published by
       8             :    the Free Software Foundation, either version 3 of the License, or
       9             :    (at your option) any later version.
      10             : 
      11             :    The OPES plumed module is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU Lesser General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU Lesser General Public License
      17             :    along with plumed.  If not, see <http://www.gnu.org/licenses/>.
      18             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      19             : #include "ExpansionCVs.h"
      20             : 
      21             : #include "tools/OpenMP.h"
      22             : 
      23             : namespace PLMD {
      24             : namespace opes {
      25             : 
      26          49 : void ExpansionCVs::registerKeywords(Keywords& keys)
      27             : {
      28          49 :   Action::registerKeywords(keys);
      29          49 :   ActionWithValue::registerKeywords(keys);
      30          49 :   ActionWithArguments::registerKeywords(keys);
      31          49 :   ActionWithValue::useCustomisableComponents(keys);
      32          98 :   keys.add("compulsory","TEMP","-1","temperature. If not specified tries to get it from MD engine");
      33          49 : }
      34             : 
      35          37 : ExpansionCVs::ExpansionCVs(const ActionOptions&ao)
      36             :   : Action(ao)
      37             :   , ActionWithValue(ao)
      38             :   , ActionWithArguments(ao)
      39          37 :   , isReady_(false)
      40          37 :   , totNumECVs_(0)
      41             : {
      42             : //set kbt_
      43          37 :   const double kB=getKBoltzmann();
      44          37 :   kbt_=getkBT();
      45          37 :   log.printf("  temperature = %g, beta = %g\n",kbt_/kB,1./kbt_);
      46             : 
      47             : //set components
      48          37 :   plumed_massert( getNumberOfArguments()!=0, "you must specify the underlying CV");
      49          90 :   for(unsigned j=0; j<getNumberOfArguments(); j++)
      50             :   {
      51          53 :     std::string name_j=getPntrToArgument(j)->getName();
      52          53 :     ActionWithValue::addComponentWithDerivatives(name_j);
      53          53 :     getPntrToComponent(j)->resizeDerivatives(1);
      54          53 :     if(getPntrToArgument(j)->isPeriodic()) //it should not be necessary, but why not
      55             :     {
      56             :       std::string min,max;
      57          17 :       getPntrToArgument(j)->getDomain(min,max);
      58          17 :       getPntrToComponent(j)->setDomain(min,max);
      59             :     }
      60             :     else
      61          36 :       getPntrToComponent(j)->setNotPeriodic();
      62             :   }
      63          37 :   plumed_massert((int)getNumberOfArguments()==getNumberOfComponents(),"Expansion CVs have same number of arguments and components");
      64          37 : }
      65             : 
      66           0 : std::string ExpansionCVs::getOutputComponentDescription( const std::string& cname, const Keywords& keys ) const {
      67           0 :   return "the value of the argument named " + cname;
      68             : }
      69             : 
      70        1847 : void ExpansionCVs::calculate()
      71             : {
      72        1847 :   std::vector<double> args(getNumberOfArguments());
      73        4470 :   for(unsigned j=0; j<getNumberOfArguments(); j++)
      74             :   {
      75        2623 :     args[j]=getArgument(j);
      76        2623 :     getPntrToComponent(j)->set(args[j]); //components are equal to arguments
      77        2623 :     getPntrToComponent(j)->addDerivative(0,1.); //the derivative of the identity is 1
      78             :   }
      79        1847 :   if(isReady_)
      80        1417 :     calculateECVs(&args[0]);
      81        1847 : }
      82             : 
      83        1847 : void ExpansionCVs::apply()
      84             : {
      85        4470 :   for(unsigned j=0; j<getNumberOfArguments(); j++)
      86             :   {
      87        2623 :     std::vector<double> force_j(1);
      88        2623 :     if(getPntrToComponent(j)->applyForce(force_j)) //a bias is applied?
      89        2623 :       getPntrToArgument(j)->addForce(force_j[0]); //just tell it to the CV!
      90             :   }
      91        1847 : }
      92             : 
      93          26 : std::vector< std::vector<unsigned> > ExpansionCVs::getIndex_k() const
      94             : {
      95          26 :   plumed_massert(isReady_ && totNumECVs_>0,"cannot access getIndex_k() of ECV before initialization");
      96          26 :   std::vector< std::vector<unsigned> > index_k(totNumECVs_,std::vector<unsigned>(getNumberOfArguments()));
      97         869 :   for(unsigned k=0; k<totNumECVs_; k++)
      98        2391 :     for(unsigned j=0; j<getNumberOfArguments(); j++)
      99        1548 :       index_k[k][j]=k; //each CV gives rise to the same number of ECVs
     100          26 :   return index_k;
     101           0 : }
     102             : 
     103             : //following methods are meant to be used only in case of linear expansions
     104          24 : std::vector<double> ExpansionCVs::getSteps(double lambda_min,double lambda_max,const unsigned lambda_steps,const std::string& msg,const bool geom_spacing, const double shift) const
     105             : {
     106          24 :   plumed_massert(!(lambda_min==lambda_max && lambda_steps>1),"cannot have multiple "+msg+"_STEPS if "+msg+"_MIN=="+msg+"_MAX");
     107          24 :   std::vector<double> lambda(lambda_steps);
     108          24 :   if(lambda_steps==1)
     109             :   {
     110           0 :     lambda[0]=(lambda_min+lambda_max)/2.;
     111           0 :     log.printf(" +++ WARNING +++ using one single %s as target = %g\n",msg.c_str(),lambda[0]);
     112             :   }
     113             :   else
     114             :   {
     115          24 :     if(geom_spacing) //geometric spacing
     116             :     { //this way lambda[k]/lambda[k+1] is constant
     117          14 :       lambda_min+=shift;
     118          14 :       lambda_max+=shift;
     119          14 :       plumed_massert(lambda_min>0,"cannot use GEOM_SPACING when %s_MIN is not greater than zero");
     120          14 :       plumed_massert(lambda_max>0,"cannot use GEOM_SPACING when %s_MAX is not greater than zero");
     121          14 :       const double log_lambda_min=std::log(lambda_min);
     122          14 :       const double log_lambda_max=std::log(lambda_max);
     123         196 :       for(unsigned k=0; k<lambda.size(); k++)
     124         182 :         lambda[k]=std::exp(log_lambda_min+k*(log_lambda_max-log_lambda_min)/(lambda_steps-1))-shift;
     125             :     }
     126             :     else //linear spacing
     127         108 :       for(unsigned k=0; k<lambda.size(); k++)
     128          98 :         lambda[k]=lambda_min+k*(lambda_max-lambda_min)/(lambda_steps-1);
     129             :   }
     130          24 :   return lambda;
     131             : }
     132             : 
     133           6 : unsigned ExpansionCVs::estimateNumSteps(const double left_side,const double right_side,const std::vector<double>& obs,const std::string& msg) const
     134             : { //for linear expansions only, it uses effective sample size (Neff) to estimate the grid spacing
     135           6 :   if(left_side==0 && right_side==0)
     136             :   {
     137           0 :     log.printf(" +++ WARNING +++ %s_MIN and %s_MAX are equal to %s, using single step\n",msg.c_str(),msg.c_str(),msg.c_str());
     138           0 :     return 1;
     139             :   }
     140           9 :   auto get_neff_HWHM=[](const double side,const std::vector<double>& obs) //HWHM = half width at half maximum. neff is in general not symmetric
     141             :   {
     142             :     //func: Neff/N-0.5 is a function between -0.5 and 0.5
     143         109 :     auto func=[](const double delta,const std::vector<double>& obs)
     144             :     {
     145             :       double sum_w=0;
     146             :       double sum_w2=0;
     147             :       //we could avoid recomputing safe_shift every time, but here speed is not a concern
     148         218 :       const double safe_shift=delta<0?*std::max_element(obs.begin(),obs.end()):*std::min_element(obs.begin(),obs.end());
     149         899 :       for(unsigned t=0; t<obs.size(); t++)
     150             :       {
     151         790 :         const double w=std::exp(-delta*(obs[t]-safe_shift)); //robust to overflow
     152         790 :         sum_w+=w;
     153         790 :         sum_w2+=w*w;
     154             :       }
     155         109 :       return sum_w*sum_w/sum_w2/obs.size()-0.5;
     156             :     };
     157             :     //here we find the root of func using the regula falsi (false position) method
     158             :     //but any method would be OK, not much precision is needed. src/tools/RootFindingBase.h looked complicated
     159             :     const double tolerance=1e-4; //seems to be a good default
     160             :     double a=0; //default is right side case
     161             :     double func_a=0.5;
     162             :     double b=side;
     163           9 :     double func_b=func(side,obs);
     164           9 :     if(func_b>=0)
     165             :       return 0.0; //no zero is present!
     166           9 :     if(b<0) //left side case
     167             :     {
     168             :       std::swap(a,b);
     169             :       std::swap(func_a,func_b);
     170             :     }
     171             :     double c=a;
     172             :     double func_c=func_a;
     173         109 :     while(std::abs(func_c)>tolerance)
     174             :     {
     175         100 :       if(func_a*func_c>0)
     176             :       {
     177             :         a=c;
     178             :         func_a=func_c;
     179             :       }
     180             :       else
     181             :       {
     182             :         b=c;
     183             :         func_b=func_c;
     184             :       }
     185         100 :       c=(a*func_b-b*func_a)/(func_b-func_a);
     186         100 :       func_c=func(c,obs); //func is evaluated only here, it might be expensive
     187             :     }
     188           9 :     return std::abs(c);
     189             :   };
     190             : 
     191             : //estimation
     192             :   double left_HWHM=0;
     193           6 :   if(left_side!=0)
     194           4 :     left_HWHM=get_neff_HWHM(left_side,obs);
     195             :   double right_HWHM=0;
     196           6 :   if(right_side!=0)
     197           5 :     right_HWHM=get_neff_HWHM(right_side,obs);
     198           6 :   if(left_HWHM==0)
     199             :   {
     200           2 :     right_HWHM*=2;
     201           2 :     if(left_side==0)
     202           2 :       log.printf(" --- %s_MIN is equal to %s\n",msg.c_str(),msg.c_str());
     203             :     else
     204           0 :       log.printf(" +++ WARNING +++ %s_MIN is very close to %s\n",msg.c_str(),msg.c_str());
     205             :   }
     206           6 :   if(right_HWHM==0)
     207             :   {
     208           1 :     left_HWHM*=2;
     209           1 :     if(right_side==0)
     210           1 :       log.printf(" --- %s_MAX is equal to %s\n",msg.c_str(),msg.c_str());
     211             :     else
     212           0 :       log.printf(" +++ WARNING +++ %s_MAX is very close to %s\n",msg.c_str(),msg.c_str());
     213             :   }
     214           6 :   const double grid_spacing=left_HWHM+right_HWHM;
     215           6 :   log.printf("   estimated %s spacing = %g\n",msg.c_str(),grid_spacing);
     216           6 :   unsigned steps=std::ceil(std::abs(right_side-left_side)/grid_spacing);
     217           6 :   if(steps<2 || grid_spacing==0)
     218             :   {
     219           0 :     log.printf(" +++ WARNING +++ %s range is very narrow, using %s_MIN and %s_MAX as only steps\n",msg.c_str(),msg.c_str(),msg.c_str());
     220             :     steps=2;
     221             :   }
     222             :   return steps;
     223             : }
     224             : 
     225             : }
     226             : }

Generated by: LCOV version 1.16