LCOV - code coverage report
Current view: top level - isdb - MetainferenceBase.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 68 70 97.1 %
Date: 2021-11-18 15:22:58 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2017-2020 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             : #ifndef __PLUMED_isdb_MetainferenceBase_h
      23             : #define __PLUMED_isdb_MetainferenceBase_h
      24             : 
      25             : #include "core/ActionWithValue.h"
      26             : #include "core/ActionAtomistic.h"
      27             : #include "core/ActionWithArguments.h"
      28             : #include "core/PlumedMain.h"
      29             : #include "tools/Random.h"
      30             : #include "tools/OpenMP.h"
      31             : 
      32             : #define PLUMED_METAINF_INIT(ao) Action(ao),MetainferenceBase(ao)
      33             : 
      34             : namespace PLMD {
      35             : namespace isdb {
      36             : 
      37             : /**
      38             : \ingroup INHERIT
      39             : This is the abstract base class to use for implementing new ISDB Metainference actions, within it there is
      40             : information as to how to go about implementing a new Metainference action.
      41             : */
      42             : 
      43             : class MetainferenceBase :
      44             :   public ActionAtomistic,
      45             :   public ActionWithArguments,
      46             :   public ActionWithValue
      47             : {
      48             : private:
      49             :   std::vector<double> forces;
      50             :   std::vector<double> forcesToApply;
      51             : 
      52             :   // activate metainference
      53             :   bool doscore_;
      54             :   unsigned write_stride_;
      55             :   // number of experimental data
      56             :   unsigned narg;
      57             :   // experimental data
      58             :   std::vector<double> parameters;
      59             :   // metainference derivatives
      60             :   std::vector<double> metader_;
      61             :   // vector of back-calculated experimental data
      62             :   std::vector<double> calc_data_;
      63             : 
      64             :   // noise type
      65             :   unsigned noise_type_;
      66             :   enum { GAUSS, MGAUSS, OUTLIERS, MOUTLIERS, GENERIC };
      67             :   unsigned gen_likelihood_;
      68             :   enum { LIKE_GAUSS, LIKE_LOGN };
      69             :   bool   doscale_;
      70             :   unsigned scale_prior_;
      71             :   enum { SC_GAUSS, SC_FLAT };
      72             :   double scale_;
      73             :   double scale_mu_;
      74             :   double scale_min_;
      75             :   double scale_max_;
      76             :   double Dscale_;
      77             :   // scale is data scaling factor
      78             :   // noise type
      79             :   unsigned offset_prior_;
      80             :   bool   dooffset_;
      81             :   double offset_;
      82             :   double offset_mu_;
      83             :   double offset_min_;
      84             :   double offset_max_;
      85             :   double Doffset_;
      86             :   // scale and offset regression
      87             :   bool doregres_zero_;
      88             :   int  nregres_zero_;
      89             :   // sigma is data uncertainty
      90             :   std::vector<double> sigma_;
      91             :   std::vector<double> sigma_min_;
      92             :   std::vector<double> sigma_max_;
      93             :   std::vector<double> Dsigma_;
      94             :   // sigma_mean is uncertainty in the mean estimate
      95             :   std::vector<double> sigma_mean2_;
      96             :   // this is the estimator of the mean value per replica for generic metainference
      97             :   std::vector<double> ftilde_;
      98             :   double Dftilde_;
      99             : 
     100             :   // temperature in kbt
     101             :   double   kbt_;
     102             : 
     103             :   // Monte Carlo stuff
     104             :   std::vector<Random> random;
     105             :   unsigned MCsteps_;
     106             :   unsigned MCstride_;
     107             :   long unsigned MCaccept_;
     108             :   long unsigned MCacceptScale_;
     109             :   long unsigned MCacceptFT_;
     110             :   long unsigned MCtrial_;
     111             :   unsigned MCchunksize_;
     112             : 
     113             :   // output
     114             :   Value*   valueScore;
     115             :   Value*   valueScale;
     116             :   Value*   valueOffset;
     117             :   Value*   valueAccept;
     118             :   Value*   valueAcceptScale;
     119             :   Value*   valueAcceptFT;
     120             :   std::vector<Value*> valueSigma;
     121             :   std::vector<Value*> valueSigmaMean;
     122             :   std::vector<Value*> valueFtilde;
     123             : 
     124             :   // restart
     125             :   std::string status_file_name_;
     126             :   OFile    sfile_;
     127             : 
     128             :   // others
     129             :   bool     firstTime;
     130             :   std::vector<bool> firstTimeW;
     131             :   bool     master;
     132             :   bool     do_reweight_;
     133             :   unsigned do_optsigmamean_;
     134             :   unsigned nrep_;
     135             :   unsigned replica_;
     136             : 
     137             :   // selector
     138             :   unsigned nsel_;
     139             :   std::string selector_;
     140             :   unsigned iselect;
     141             : 
     142             :   // optimize sigma mean
     143             :   std::vector< std::vector < std::vector <double> > > sigma_mean2_last_;
     144             :   unsigned optsigmamean_stride_;
     145             : 
     146             :   // average weights
     147             :   double decay_w_;
     148             :   std::vector< std::vector <double> >  average_weights_;
     149             : 
     150             :   double getEnergyMIGEN(const std::vector<double> &mean, const std::vector<double> &ftilde, const std::vector<double> &sigma,
     151             :                         const double scale, const double offset);
     152             :   double getEnergySP(const std::vector<double> &mean, const std::vector<double> &sigma,
     153             :                      const double scale, const double offset);
     154             :   double getEnergySPE(const std::vector<double> &mean, const std::vector<double> &sigma,
     155             :                       const double scale, const double offset);
     156             :   double getEnergyGJ(const std::vector<double> &mean, const std::vector<double> &sigma,
     157             :                      const double scale, const double offset);
     158             :   double getEnergyGJE(const std::vector<double> &mean, const std::vector<double> &sigma,
     159             :                       const double scale, const double offset);
     160             :   void   setMetaDer(const unsigned index, const double der);
     161             :   double getEnergyForceSP(const std::vector<double> &mean, const std::vector<double> &dmean_x, const std::vector<double> &dmean_b);
     162             :   double getEnergyForceSPE(const std::vector<double> &mean, const std::vector<double> &dmean_x, const std::vector<double> &dmean_b);
     163             :   double getEnergyForceGJ(const std::vector<double> &mean, const std::vector<double> &dmean_x, const std::vector<double> &dmean_b);
     164             :   double getEnergyForceGJE(const std::vector<double> &mean, const std::vector<double> &dmean_x, const std::vector<double> &dmean_b);
     165             :   double getEnergyForceMIGEN(const std::vector<double> &mean, const std::vector<double> &dmean_x, const std::vector<double> &dmean_b);
     166             :   double getCalcData(const unsigned index);
     167             :   void get_weights(double &fact, double &var_fact);
     168             :   void replica_averaging(const double fact, std::vector<double> &mean, std::vector<double> &dmean_b);
     169             :   void get_sigma_mean(const double fact, const double var_fact, const std::vector<double> &mean);
     170             :   void do_regression_zero(const std::vector<double> &mean);
     171             :   void doMonteCarlo(const std::vector<double> &mean);
     172             : 
     173             : 
     174             : public:
     175             :   static void registerKeywords( Keywords& keys );
     176             :   explicit MetainferenceBase(const ActionOptions&);
     177             :   ~MetainferenceBase();
     178             :   void Initialise(const unsigned input);
     179             :   void Selector();
     180             :   unsigned getNarg();
     181             :   void setNarg(const unsigned input);
     182             :   void setParameters(const std::vector<double>& input);
     183             :   void setParameter(const double input);
     184             :   void setCalcData(const unsigned index, const double datum);
     185             :   void setCalcData(const std::vector<double>& data);
     186             :   bool getDoScore();
     187             :   unsigned getWstride();
     188             :   double getScore();
     189             :   void setScore(const double score);
     190             :   void setDerivatives();
     191             :   double getMetaDer(const unsigned index);
     192             :   void writeStatus();
     193             :   void turnOnDerivatives();
     194             :   unsigned getNumberOfDerivatives();
     195             :   void lockRequests();
     196             :   void unlockRequests();
     197             :   void calculateNumericalDerivatives( ActionWithValue* a );
     198             :   void apply();
     199             :   void setArgDerivatives(Value *v, const double &d);
     200             :   void setAtomsDerivatives(Value*v, const unsigned i, const Vector&d);
     201             :   void setBoxDerivatives(Value*v, const Tensor&d);
     202             : };
     203             : 
     204             : inline
     205             : void MetainferenceBase::setNarg(const unsigned input)
     206             : {
     207          31 :   narg = input;
     208             : }
     209             : 
     210             : inline
     211             : bool MetainferenceBase::getDoScore()
     212             : {
     213       35563 :   return doscore_;
     214             : }
     215             : 
     216             : inline
     217             : unsigned MetainferenceBase::getWstride()
     218             : {
     219        1382 :   return write_stride_;
     220             : }
     221             : 
     222             : inline
     223             : unsigned MetainferenceBase::getNarg()
     224             : {
     225        6675 :   return narg;
     226             : }
     227             : 
     228             : inline
     229             : void MetainferenceBase::setMetaDer(const unsigned index, const double der)
     230             : {
     231       11827 :   metader_[index] = der;
     232             : }
     233             : 
     234             : inline
     235             : double MetainferenceBase::getMetaDer(const unsigned index)
     236             : {
     237     1366796 :   return metader_[index];
     238             : }
     239             : 
     240             : inline
     241             : double MetainferenceBase::getCalcData(const unsigned index)
     242             : {
     243        1260 :   return calc_data_[index];
     244             : }
     245             : 
     246             : inline
     247             : void MetainferenceBase::setCalcData(const unsigned index, const double datum)
     248             : {
     249       11827 :   calc_data_[index] = datum;
     250             : }
     251             : 
     252             : inline
     253             : void MetainferenceBase::setCalcData(const std::vector<double>& data)
     254             : {
     255             :   for(unsigned i=0; i<data.size(); i++) calc_data_[i] = data[i];
     256             : }
     257             : 
     258             : inline
     259          27 : void MetainferenceBase::setParameters(const std::vector<double>& input) {
     260         477 :   for(unsigned i=0; i<input.size(); i++) parameters.push_back(input[i]);
     261          27 : }
     262             : 
     263             : inline
     264             : void MetainferenceBase::setParameter(const double input) {
     265        2356 :   parameters.push_back(input);
     266             : }
     267             : 
     268             : inline
     269             : void MetainferenceBase::setScore(const double score) {
     270        2225 :   valueScore->set(score);
     271             : }
     272             : 
     273             : inline
     274          86 : void MetainferenceBase::setDerivatives() {
     275             :   // Get appropriate number of derivatives
     276             :   // Derivatives are first for arguments and then for atoms
     277             :   unsigned nder;
     278          86 :   if( getNumberOfAtoms()>0 ) {
     279          86 :     nder = 3*getNumberOfAtoms() + 9 + getNumberOfArguments();
     280             :   } else {
     281           0 :     nder = getNumberOfArguments();
     282             :   }
     283             : 
     284             :   // Resize all derivative arrays
     285          86 :   forces.resize( nder ); forcesToApply.resize( nder );
     286       42252 :   for(int i=0; i<getNumberOfComponents(); ++i) getPntrToComponent(i)->resizeDerivatives(nder);
     287          86 : }
     288             : 
     289             : inline
     290     3480171 : void MetainferenceBase::turnOnDerivatives() {
     291     3480171 :   ActionWithValue::turnOnDerivatives();
     292     3480171 : }
     293             : 
     294             : inline
     295  4099242757 : unsigned MetainferenceBase::getNumberOfDerivatives() {
     296  4099242757 :   if( getNumberOfAtoms()>0 ) {
     297  4099242757 :     return 3*getNumberOfAtoms() + 9 + getNumberOfArguments();
     298             :   }
     299           0 :   return getNumberOfArguments();
     300             : }
     301             : 
     302             : inline
     303         691 : void MetainferenceBase::lockRequests() {
     304             :   ActionAtomistic::lockRequests();
     305             :   ActionWithArguments::lockRequests();
     306         691 : }
     307             : 
     308             : inline
     309         691 : void MetainferenceBase::unlockRequests() {
     310             :   ActionAtomistic::unlockRequests();
     311             :   ActionWithArguments::unlockRequests();
     312         691 : }
     313             : 
     314             : inline
     315          75 : void MetainferenceBase::calculateNumericalDerivatives( ActionWithValue* a=NULL ) {
     316          75 :   if( getNumberOfArguments()>0 ) {
     317          48 :     ActionWithArguments::calculateNumericalDerivatives( a );
     318             :   }
     319          75 :   if( getNumberOfAtoms()>0 ) {
     320         150 :     Matrix<double> save_derivatives( getNumberOfComponents(), getNumberOfArguments() );
     321        2405 :     for(int j=0; j<getNumberOfComponents(); ++j) {
     322        3517 :       for(unsigned i=0; i<getNumberOfArguments(); ++i) if(getPntrToComponent(j)->hasDerivatives()) save_derivatives(j,i)=getPntrToComponent(j)->getDerivative(i);
     323             :     }
     324          75 :     calculateAtomicNumericalDerivatives( a, getNumberOfArguments() );
     325        2405 :     for(int j=0; j<getNumberOfComponents(); ++j) {
     326        4573 :       for(unsigned i=0; i<getNumberOfArguments(); ++i) if(getPntrToComponent(j)->hasDerivatives()) getPntrToComponent(j)->addDerivative( i, save_derivatives(j,i) );
     327             :     }
     328             :   }
     329          75 : }
     330             : 
     331             : inline
     332         691 : void MetainferenceBase::apply() {
     333        1382 :   bool wasforced=false; forcesToApply.assign(forcesToApply.size(),0.0);
     334       63993 :   for(int i=0; i<getNumberOfComponents(); ++i) {
     335       31651 :     if( getPntrToComponent(i)->applyForce( forces ) ) {
     336             :       wasforced=true;
     337   166645292 :       for(unsigned i=0; i<forces.size(); ++i) forcesToApply[i]+=forces[i];
     338             :     }
     339             :   }
     340         691 :   if( wasforced ) {
     341         350 :     addForcesOnArguments( forcesToApply );
     342         350 :     if( getNumberOfAtoms()>0 ) setForcesOnAtoms( forcesToApply, getNumberOfArguments() );
     343             :   }
     344         691 : }
     345             : 
     346             : inline
     347             : void MetainferenceBase::setArgDerivatives(Value *v, const double &d) {
     348         160 :   v->addDerivative(0,d);
     349             : }
     350             : 
     351             : inline
     352     1865108 : void MetainferenceBase::setAtomsDerivatives(Value*v, const unsigned i, const Vector&d) {
     353     1865108 :   const unsigned noa=getNumberOfArguments();
     354     1865108 :   v->addDerivative(noa+3*i+0,d[0]);
     355     1865108 :   v->addDerivative(noa+3*i+1,d[1]);
     356     1865108 :   v->addDerivative(noa+3*i+2,d[2]);
     357     1865108 : }
     358             : 
     359             : inline
     360       11782 : void MetainferenceBase::setBoxDerivatives(Value* v,const Tensor&d) {
     361       11782 :   const unsigned noa=getNumberOfArguments();
     362             :   const unsigned nat=getNumberOfAtoms();
     363       11782 :   v->addDerivative(noa+3*nat+0,d(0,0));
     364       11782 :   v->addDerivative(noa+3*nat+1,d(0,1));
     365       11782 :   v->addDerivative(noa+3*nat+2,d(0,2));
     366       11782 :   v->addDerivative(noa+3*nat+3,d(1,0));
     367       11782 :   v->addDerivative(noa+3*nat+4,d(1,1));
     368       11782 :   v->addDerivative(noa+3*nat+5,d(1,2));
     369       11782 :   v->addDerivative(noa+3*nat+6,d(2,0));
     370       11782 :   v->addDerivative(noa+3*nat+7,d(2,1));
     371       11782 :   v->addDerivative(noa+3*nat+8,d(2,2));
     372       11782 : }
     373             : 
     374             : 
     375             : }
     376             : }
     377             : 
     378             : #endif
     379             : 

Generated by: LCOV version 1.14