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: 2019-08-13 10:15:31 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2017-2019 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             :   long unsigned MCaccept_;
     107             :   long unsigned MCacceptScale_;
     108             :   long unsigned MCacceptFT_;
     109             :   long unsigned MCtrial_;
     110             :   unsigned MCchunksize_;
     111             : 
     112             :   // output
     113             :   Value*   valueScore;
     114             :   Value*   valueScale;
     115             :   Value*   valueOffset;
     116             :   Value*   valueAccept;
     117             :   Value*   valueAcceptScale;
     118             :   Value*   valueAcceptFT;
     119             :   std::vector<Value*> valueSigma;
     120             :   std::vector<Value*> valueSigmaMean;
     121             :   std::vector<Value*> valueFtilde;
     122             : 
     123             :   // restart
     124             :   std::string status_file_name_;
     125             :   OFile    sfile_;
     126             : 
     127             :   // others
     128             :   bool     firstTime;
     129             :   std::vector<bool> firstTimeW;
     130             :   bool     master;
     131             :   bool     do_reweight_;
     132             :   unsigned do_optsigmamean_;
     133             :   unsigned nrep_;
     134             :   unsigned replica_;
     135             : 
     136             :   // selector
     137             :   unsigned nsel_;
     138             :   std::string selector_;
     139             :   unsigned iselect;
     140             : 
     141             :   // optimize sigma mean
     142             :   std::vector< std::vector < std::vector <double> > > sigma_mean2_last_;
     143             :   unsigned optsigmamean_stride_;
     144             : 
     145             :   // average weights
     146             :   double decay_w_;
     147             :   std::vector< std::vector <double> >  average_weights_;
     148             : 
     149             :   double getEnergyMIGEN(const std::vector<double> &mean, const std::vector<double> &ftilde, const std::vector<double> &sigma,
     150             :                         const double scale, const double offset);
     151             :   double getEnergySP(const std::vector<double> &mean, const std::vector<double> &sigma,
     152             :                      const double scale, const double offset);
     153             :   double getEnergySPE(const std::vector<double> &mean, const std::vector<double> &sigma,
     154             :                       const double scale, const double offset);
     155             :   double getEnergyGJ(const std::vector<double> &mean, const std::vector<double> &sigma,
     156             :                      const double scale, const double offset);
     157             :   double getEnergyGJE(const std::vector<double> &mean, const std::vector<double> &sigma,
     158             :                       const double scale, const double offset);
     159             :   void   setMetaDer(const unsigned index, const double der);
     160             :   void getEnergyForceSP(const std::vector<double> &mean, const std::vector<double> &dmean_x, const std::vector<double> &dmean_b);
     161             :   void getEnergyForceSPE(const std::vector<double> &mean, const std::vector<double> &dmean_x, const std::vector<double> &dmean_b);
     162             :   void getEnergyForceGJ(const std::vector<double> &mean, const std::vector<double> &dmean_x, const std::vector<double> &dmean_b);
     163             :   void getEnergyForceGJE(const std::vector<double> &mean, const std::vector<double> &dmean_x, const std::vector<double> &dmean_b);
     164             :   void getEnergyForceMIGEN(const std::vector<double> &mean, const std::vector<double> &dmean_x, const std::vector<double> &dmean_b);
     165             :   double getCalcData(const unsigned index);
     166             :   void get_weights(double &fact, double &var_fact);
     167             :   void replica_averaging(const double fact, std::vector<double> &mean, std::vector<double> &dmean_b);
     168             :   void get_sigma_mean(const double fact, const double var_fact, const std::vector<double> &mean);
     169             :   void do_regression_zero(const std::vector<double> &mean);
     170             :   double doMonteCarlo(const std::vector<double> &mean);
     171             : 
     172             : 
     173             : public:
     174             :   static void registerKeywords( Keywords& keys );
     175             :   explicit MetainferenceBase(const ActionOptions&);
     176             :   ~MetainferenceBase();
     177             :   void Initialise(const unsigned input);
     178             :   void Selector();
     179             :   unsigned getNarg();
     180             :   void setNarg(const unsigned input);
     181             :   void setParameters(const std::vector<double>& input);
     182             :   void setParameter(const double input);
     183             :   void setCalcData(const unsigned index, const double datum);
     184             :   void setCalcData(const std::vector<double>& data);
     185             :   bool getDoScore();
     186             :   unsigned getWstride();
     187             :   double getScore();
     188             :   void setScore(const double score);
     189             :   void setDerivatives();
     190             :   double getMetaDer(const unsigned index);
     191             :   void writeStatus();
     192             :   void turnOnDerivatives() override;
     193             :   unsigned getNumberOfDerivatives() override;
     194             :   void lockRequests() override;
     195             :   void unlockRequests() override;
     196             :   void calculateNumericalDerivatives( ActionWithValue* a ) override;
     197             :   void apply() override;
     198             :   void setArgDerivatives(Value *v, const double &d);
     199             :   void setAtomsDerivatives(Value*v, const unsigned i, const Vector&d);
     200             :   void setBoxDerivatives(Value*v, const Tensor&d);
     201             : };
     202             : 
     203             : inline
     204             : void MetainferenceBase::setNarg(const unsigned input)
     205             : {
     206          31 :   narg = input;
     207             : }
     208             : 
     209             : inline
     210             : bool MetainferenceBase::getDoScore()
     211             : {
     212       35521 :   return doscore_;
     213             : }
     214             : 
     215             : inline
     216             : unsigned MetainferenceBase::getWstride()
     217             : {
     218        1382 :   return write_stride_;
     219             : }
     220             : 
     221             : inline
     222             : unsigned MetainferenceBase::getNarg()
     223             : {
     224        6675 :   return narg;
     225             : }
     226             : 
     227             : inline
     228             : void MetainferenceBase::setMetaDer(const unsigned index, const double der)
     229             : {
     230       11825 :   metader_[index] = der;
     231             : }
     232             : 
     233             : inline
     234             : double MetainferenceBase::getMetaDer(const unsigned index)
     235             : {
     236     1366796 :   return metader_[index];
     237             : }
     238             : 
     239             : inline
     240             : double MetainferenceBase::getCalcData(const unsigned index)
     241             : {
     242        1260 :   return calc_data_[index];
     243             : }
     244             : 
     245             : inline
     246             : void MetainferenceBase::setCalcData(const unsigned index, const double datum)
     247             : {
     248       11798 :   calc_data_[index] = datum;
     249             : }
     250             : 
     251             : inline
     252             : void MetainferenceBase::setCalcData(const std::vector<double>& data)
     253             : {
     254             :   for(unsigned i=0; i<data.size(); i++) calc_data_[i] = data[i];
     255             : }
     256             : 
     257             : inline
     258          27 : void MetainferenceBase::setParameters(const std::vector<double>& input) {
     259         336 :   for(unsigned i=0; i<input.size(); i++) parameters.push_back(input[i]);
     260          27 : }
     261             : 
     262             : inline
     263             : void MetainferenceBase::setParameter(const double input) {
     264        2356 :   parameters.push_back(input);
     265             : }
     266             : 
     267             : inline
     268             : void MetainferenceBase::setScore(const double score) {
     269        2225 :   valueScore->set(score);
     270             : }
     271             : 
     272             : inline
     273          86 : void MetainferenceBase::setDerivatives() {
     274             :   // Get appropriate number of derivatives
     275             :   // Derivatives are first for arguments and then for atoms
     276             :   unsigned nder;
     277          86 :   if( getNumberOfAtoms()>0 ) {
     278          86 :     nder = 3*getNumberOfAtoms() + 9 + getNumberOfArguments();
     279             :   } else {
     280           0 :     nder = getNumberOfArguments();
     281             :   }
     282             : 
     283             :   // Resize all derivative arrays
     284          86 :   forces.resize( nder ); forcesToApply.resize( nder );
     285       42338 :   for(int i=0; i<getNumberOfComponents(); ++i) getPntrToComponent(i)->resizeDerivatives(nder);
     286          86 : }
     287             : 
     288             : inline
     289     3480171 : void MetainferenceBase::turnOnDerivatives() {
     290     3480171 :   ActionWithValue::turnOnDerivatives();
     291     3480171 : }
     292             : 
     293             : inline
     294  4099242757 : unsigned MetainferenceBase::getNumberOfDerivatives() {
     295  4099242757 :   if( getNumberOfAtoms()>0 ) {
     296  4099242757 :     return 3*getNumberOfAtoms() + 9 + getNumberOfArguments();
     297             :   }
     298           0 :   return getNumberOfArguments();
     299             : }
     300             : 
     301             : inline
     302         691 : void MetainferenceBase::lockRequests() {
     303             :   ActionAtomistic::lockRequests();
     304             :   ActionWithArguments::lockRequests();
     305         691 : }
     306             : 
     307             : inline
     308         691 : void MetainferenceBase::unlockRequests() {
     309             :   ActionAtomistic::unlockRequests();
     310             :   ActionWithArguments::unlockRequests();
     311         691 : }
     312             : 
     313             : inline
     314          75 : void MetainferenceBase::calculateNumericalDerivatives( ActionWithValue* a=NULL ) {
     315          75 :   if( getNumberOfArguments()>0 ) {
     316          48 :     ActionWithArguments::calculateNumericalDerivatives( a );
     317             :   }
     318          75 :   if( getNumberOfAtoms()>0 ) {
     319         150 :     Matrix<double> save_derivatives( getNumberOfComponents(), getNumberOfArguments() );
     320        2480 :     for(int j=0; j<getNumberOfComponents(); ++j) {
     321        2592 :       for(unsigned i=0; i<getNumberOfArguments(); ++i) if(getPntrToComponent(j)->hasDerivatives()) save_derivatives(j,i)=getPntrToComponent(j)->getDerivative(i);
     322             :     }
     323          75 :     calculateAtomicNumericalDerivatives( a, getNumberOfArguments() );
     324        2480 :     for(int j=0; j<getNumberOfComponents(); ++j) {
     325        3408 :       for(unsigned i=0; i<getNumberOfArguments(); ++i) if(getPntrToComponent(j)->hasDerivatives()) getPntrToComponent(j)->addDerivative( i, save_derivatives(j,i) );
     326             :     }
     327             :   }
     328          75 : }
     329             : 
     330             : inline
     331         691 : void MetainferenceBase::apply() {
     332        2073 :   bool wasforced=false; forcesToApply.assign(forcesToApply.size(),0.0);
     333       64684 :   for(int i=0; i<getNumberOfComponents(); ++i) {
     334       31651 :     if( getPntrToComponent(i)->applyForce( forces ) ) {
     335             :       wasforced=true;
     336   124980884 :       for(unsigned i=0; i<forces.size(); ++i) forcesToApply[i]+=forces[i];
     337             :     }
     338             :   }
     339         691 :   if( wasforced ) {
     340         350 :     addForcesOnArguments( forcesToApply );
     341         350 :     if( getNumberOfAtoms()>0 ) setForcesOnAtoms( forcesToApply, getNumberOfArguments() );
     342             :   }
     343         691 : }
     344             : 
     345             : inline
     346             : void MetainferenceBase::setArgDerivatives(Value *v, const double &d) {
     347         160 :   v->addDerivative(0,d);
     348             : }
     349             : 
     350             : inline
     351     1865049 : void MetainferenceBase::setAtomsDerivatives(Value*v, const unsigned i, const Vector&d) {
     352     1865049 :   const unsigned noa=getNumberOfArguments();
     353     1865073 :   v->addDerivative(noa+3*i+0,d[0]);
     354     1865079 :   v->addDerivative(noa+3*i+1,d[1]);
     355     1865098 :   v->addDerivative(noa+3*i+2,d[2]);
     356     1865096 : }
     357             : 
     358             : inline
     359       11757 : void MetainferenceBase::setBoxDerivatives(Value* v,const Tensor&d) {
     360       11757 :   const unsigned noa=getNumberOfArguments();
     361             :   const unsigned nat=getNumberOfAtoms();
     362       11770 :   v->addDerivative(noa+3*nat+0,d(0,0));
     363       11765 :   v->addDerivative(noa+3*nat+1,d(0,1));
     364       11774 :   v->addDerivative(noa+3*nat+2,d(0,2));
     365       11773 :   v->addDerivative(noa+3*nat+3,d(1,0));
     366       11776 :   v->addDerivative(noa+3*nat+4,d(1,1));
     367       11776 :   v->addDerivative(noa+3*nat+5,d(1,2));
     368       11781 :   v->addDerivative(noa+3*nat+6,d(2,0));
     369       11782 :   v->addDerivative(noa+3*nat+7,d(2,1));
     370       11782 :   v->addDerivative(noa+3*nat+8,d(2,2));
     371       11782 : }
     372             : 
     373             : 
     374             : }
     375             : }
     376             : 
     377             : #endif
     378             : 

Generated by: LCOV version 1.14