LCOV - code coverage report
Current view: top level - ves - TargetDistribution.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 18 22 81.8 %
Date: 2024-10-18 14:00:25 Functions: 3 6 50.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2016-2021 The VES code team
       3             :    (see the PEOPLE-VES file at the root of this folder for a list of names)
       4             : 
       5             :    See http://www.ves-code.org for more information.
       6             : 
       7             :    This file is part of VES code module.
       8             : 
       9             :    The VES code module 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             :    The VES code module 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 the VES code module.  If not, see <http://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : #ifndef __PLUMED_ves_TargetDistribution_h
      23             : #define __PLUMED_ves_TargetDistribution_h
      24             : 
      25             : #include "core/Action.h"
      26             : 
      27             : #include <vector>
      28             : #include <string>
      29             : #include <cmath>
      30             : #include <memory>
      31             : 
      32             : #define PLUMED_VES_TARGETDISTRIBUTION_INIT(ao) TargetDistribution(ao)
      33             : 
      34             : namespace PLMD {
      35             : 
      36             : /**
      37             : \ingroup INHERIT
      38             : Abstract base class for implenting new target distributions.
      39             : */
      40             : 
      41             : class Action;
      42             : class Grid;
      43             : class Value;
      44             : class Keywords;
      45             : 
      46             : namespace ves {
      47             : 
      48             : class TargetDistModifer;
      49             : class VesBias;
      50             : 
      51             : class TargetDistribution :
      52             :   public Action
      53             : {
      54             : private:
      55             :   enum TargetDistType {
      56             :     static_targetdist,
      57             :     dynamic_targetdist
      58             :   } type_;
      59             :   //
      60             :   bool force_normalization_;
      61             :   bool check_normalization_;
      62             :   bool check_nonnegative_;
      63             :   bool check_nan_inf_;
      64             :   bool shift_targetdist_to_zero_;
      65             :   // dimension of the distribution
      66             :   unsigned int dimension_;
      67             :   // grid parameters
      68             :   std::vector<Value*> grid_args_;
      69             :   //
      70             :   std::unique_ptr<Grid> targetdist_grid_pntr_;
      71             :   std::unique_ptr<Grid> log_targetdist_grid_pntr_;
      72             :   //
      73             :   std::vector<std::unique_ptr<TargetDistModifer>> targetdist_modifer_pntrs_;
      74             :   //
      75             :   Action* action_pntr_;
      76             :   VesBias* vesbias_pntr_;
      77             :   //
      78             :   bool needs_bias_grid_;
      79             :   bool needs_bias_withoutcutoff_grid_;
      80             :   bool needs_fes_grid_;
      81             :   //
      82             :   Grid* bias_grid_pntr_;
      83             :   Grid* bias_withoutcutoff_grid_pntr_;
      84             :   Grid* fes_grid_pntr_;
      85             :   //
      86             :   bool static_grid_calculated;
      87             :   //
      88             :   bool allow_bias_cutoff_;
      89             :   bool bias_cutoff_active_;
      90             :   //
      91             :   void calculateStaticDistributionGrid();
      92             :   void updateBiasCutoffForTargetDistGrid();
      93             :   void checkNanAndInf();
      94             : protected:
      95             :   void setStatic() {type_=static_targetdist;}
      96          41 :   void setDynamic() {type_=dynamic_targetdist;}
      97             :   // set the that target distribution is normalized
      98          80 :   void setForcedNormalization() {force_normalization_=true; check_normalization_=false;}
      99             :   void unsetForcedNormalization() {force_normalization_=false; check_normalization_=true;};
     100             :   //
     101           0 :   void setBiasGridNeeded() {needs_bias_grid_=true;}
     102           3 :   void setBiasWithoutCutoffGridNeeded() {needs_bias_withoutcutoff_grid_=true;}
     103          38 :   void setFesGridNeeded() {needs_fes_grid_=true;}
     104             :   //
     105             :   VesBias* getPntrToVesBias() const;
     106             :   Action* getPntrToAction() const;
     107             :   //
     108         287 :   virtual void setupAdditionalGrids(const std::vector<Value*>&, const std::vector<std::string>&, const std::vector<std::string>&, const std::vector<unsigned int>&) {}
     109             :   //
     110             :   void normalizeTargetDistGrid();
     111             :   //
     112             :   Grid& targetDistGrid() const {return *targetdist_grid_pntr_;}
     113             :   Grid& logTargetDistGrid() const {return *log_targetdist_grid_pntr_;}
     114             :   //
     115             :   Grid* getBiasGridPntr() const {return bias_grid_pntr_;}
     116        2624 :   Grid* getBiasWithoutCutoffGridPntr() const {return bias_withoutcutoff_grid_pntr_;}
     117    11814078 :   Grid* getFesGridPntr() const {return fes_grid_pntr_;}
     118             :   //
     119             :   double getBeta() const;
     120             :   //
     121             :   void applyTargetDistModiferToGrid(TargetDistModifer* modifer_pntr);
     122             :   //
     123             :   void setMinimumOfTargetDistGridToZero();
     124             :   void updateLogTargetDistGrid();
     125             :   //
     126         368 :   virtual void updateGrid() {calculateStaticDistributionGrid();}
     127             : public:
     128             :   static void registerKeywords(Keywords&);
     129             :   explicit TargetDistribution(const ActionOptions&);
     130             :   ~TargetDistribution();
     131             :   //
     132             :   bool isStatic() const {return type_==static_targetdist;}
     133         602 :   bool isDynamic() const {return type_==dynamic_targetdist;}
     134             :   // is the target distribution normalize or not
     135             :   bool forcedNormalization() const {return force_normalization_;};
     136        5194 :   bool isTargetDistGridShiftedToZero() const {return shift_targetdist_to_zero_;}
     137             :   //
     138         466 :   bool biasGridNeeded() const {return needs_bias_grid_;}
     139          45 :   bool biasWithoutCutoffGridNeeded() const {return needs_bias_withoutcutoff_grid_;}
     140         466 :   bool fesGridNeeded()  const {return needs_fes_grid_;}
     141             :   //
     142             :   void allowBiasCutoff() {allow_bias_cutoff_=true;}
     143             :   void doNotAllowBiasCutoff() {allow_bias_cutoff_=false;}
     144             :   bool isBiasCutoffAllowed() const {return allow_bias_cutoff_;}
     145             :   bool biasCutoffActive() const {return bias_cutoff_active_;}
     146             :   //
     147             :   void setDimension(const unsigned int dimension);
     148      506318 :   unsigned getDimension() const {return dimension_;}
     149             :   //
     150             :   virtual void linkVesBias(VesBias*);
     151             :   virtual void linkAction(Action*);
     152             :   //
     153             :   virtual void linkBiasGrid(Grid*);
     154             :   virtual void linkBiasWithoutCutoffGrid(Grid*);
     155             :   virtual void linkFesGrid(Grid*);
     156             :   //
     157             :   void setupBiasCutoff();
     158             :   //
     159             :   Grid* getTargetDistGridPntr() const {return targetdist_grid_pntr_.get();}
     160             :   Grid* getLogTargetDistGridPntr() const {return log_targetdist_grid_pntr_.get();}
     161             :   //
     162             :   void clearLogTargetDistGrid();
     163             :   // calculate the target distribution itself
     164             :   virtual double getValue(const std::vector<double>&) const = 0;
     165             :   //
     166             :   void setupGrids(const std::vector<Value*>&, const std::vector<std::string>&, const std::vector<std::string>&, const std::vector<unsigned int>&);
     167             :   //
     168             :   Grid getMarginal(const std::vector<std::string>&);
     169             :   //
     170             :   void updateTargetDist();
     171             :   //
     172             :   void readInRestartTargetDistGrid(const std::string&);
     173             :   //
     174             :   static double integrateGrid(const Grid*);
     175             :   static double normalizeGrid(Grid*);
     176             :   static Grid getMarginalDistributionGrid(Grid*, const std::vector<std::string>&);
     177             :   // empty standard action stuff
     178           0 :   void update() override {};
     179           0 :   void apply() override {};
     180           0 :   void calculate() override {};
     181             : };
     182             : 
     183             : 
     184             : inline
     185             : VesBias* TargetDistribution::getPntrToVesBias() const {
     186             :   plumed_massert(vesbias_pntr_!=NULL,"the VES bias has not been linked");
     187             :   return vesbias_pntr_;
     188             : }
     189             : 
     190             : 
     191             : inline
     192             : Action* TargetDistribution::getPntrToAction() const {
     193             :   plumed_massert(action_pntr_!=NULL,"the action has not been linked");
     194             :   return action_pntr_;
     195             : }
     196             : 
     197             : 
     198             : inline
     199          90 : void TargetDistribution::normalizeTargetDistGrid() {
     200          90 :   double normalization = normalizeGrid(targetdist_grid_pntr_.get());
     201          90 :   if(normalization<0.0) {plumed_merror(getName()+": something went wrong trying to normalize the target distribution, integrating over it gives a negative value.");}
     202          90 : }
     203             : 
     204             : 
     205             : 
     206             : 
     207             : }
     208             : }
     209             : #endif

Generated by: LCOV version 1.16