LCOV - code coverage report
Current view: top level - maze - Optimizer.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 13 13 100.0 %
Date: 2024-10-18 14:00:25 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             : Copyright (c) 2019 Jakub Rydzewski (jr@fizyka.umk.pl). All rights reserved.
       3             : 
       4             : See http://www.maze-code.github.io for more information.
       5             : 
       6             : This file is part of maze.
       7             : 
       8             : maze is free software: you can redistribute it and/or modify it under the
       9             : terms of the GNU Lesser General Public License as published by the Free
      10             : Software Foundation, either version 3 of the License, or (at your option)
      11             : any later version.
      12             : 
      13             : maze is distributed in the hope that it will be useful, but WITHOUT ANY
      14             : WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      15             : FOR A PARTICULAR PURPOSE.
      16             : 
      17             : See the 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 maze. If not, see <https://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : #ifndef __PLUMED_maze_Optimizer_h
      23             : #define __PLUMED_maze_Optimizer_h
      24             : 
      25             : /**
      26             :  * @file Optimizer.h
      27             :  *
      28             :  * @author J. Rydzewski (jr@fizyka.umk.pl)
      29             :  */
      30             : 
      31             : #include "colvar/Colvar.h"
      32             : #include "tools/Communicator.h"
      33             : #include "tools/OpenMP.h"
      34             : #include "tools/NeighborList.h"
      35             : #include "tools/Vector.h"
      36             : 
      37             : #include "Core.h"
      38             : #include "Loss.h"
      39             : 
      40             : #include <memory>
      41             : 
      42             : #define PLUMED_OPT_INIT(ao) Action(ao), Optimizer(ao)
      43             : 
      44             : namespace PLMD {
      45             : namespace maze {
      46             : 
      47             : /**
      48             :  * @ingroup INHERIT
      49             :  *
      50             :  * @class Optimizer Optimizer.h "maze/Optimizer.h"
      51             :  *
      52             :  * @brief Base class for implementing optimizers for ligand unbinding.
      53             :  *
      54             :  * An optimizer is defined as a colvar that can be passed to Optimizer_Bias.
      55             :  */
      56             : class Optimizer: public colvar::Colvar {
      57             : public:
      58             :   /**
      59             :    * PLMD constructor.
      60             :    *
      61             :    * @param[in] ao PLMD::ActionOptions&
      62             :    */
      63             :   explicit Optimizer(const ActionOptions&);
      64             : 
      65             :   /**
      66             :    * Destructor.
      67             :    */
      68           7 :   ~Optimizer() { /* Nothing to do. */ }
      69             : 
      70             :   /**
      71             :    * Registers PLMD keywords.
      72             :    *
      73             :    * @param[in] keys PLMD keywords
      74             :    */
      75             :   static void registerKeywords(Keywords& keys);
      76             : 
      77             :   /**
      78             :    * The pairing function needs to be overridden by a specific optimizer.
      79             :    *
      80             :    * @param[in] distance distance between a pair of atoms
      81             :    */
      82             :   virtual double pairing(double distance) const;
      83             : 
      84             :   /**
      85             :    * Optimal values needed for biasing are computed by methods overridding the
      86             :    * optimize function.
      87             :    */
      88             :   virtual void optimize() = 0;
      89             : 
      90             :   /**
      91             :    * Calculate the optimal direction of pulling.
      92             :    */
      93             :   void calculate();
      94             : 
      95             :   /**
      96             :    * Prepare the neighbor list.
      97             :    */
      98             :   void prepare();
      99             : 
     100             :   /**
     101             :    * Score a ligand-protein configuration.
     102             :    *
     103             :    * @return score
     104             :    */
     105             :   double score();
     106             : 
     107             :   /**
     108             :    * Calculate sampling radius as the minimal distance between two groups in
     109             :    * neighbors list.
     110             :    *
     111             :    * @return minimal distance of ligand-protein atom pairs
     112             :    */
     113             :   double sampling_radius();
     114             : 
     115             :   /**
     116             :    * Load new positions of atoms in the neighbor list.
     117             :    */
     118             :   void update_nl();
     119             : 
     120             :   /**
     121             :    * Calculate the center of mass.
     122             :    *
     123             :    * @return center of mass
     124             :    */
     125             :   Vector center_of_mass() const;
     126             : 
     127             : public:
     128             :   /**
     129             :    * Getters and setters.
     130             :    */
     131             : 
     132             :   std::string get_label() const;
     133             :   void set_label(const std::string&);
     134             : 
     135             :   // Start optimizer at time = 0.
     136             :   void start_step_0();
     137             : 
     138             :   // Start optimizer at time = optimizer stride.
     139             :   void start_step_stride();
     140             : 
     141             :   Vector get_opt() const;
     142             :   void set_opt(Vector);
     143             : 
     144             :   double get_opt_value() const;
     145             :   void set_opt_value(double);
     146             : 
     147             :   unsigned int get_optimizer_stride() const;
     148             :   void set_optimizer_stride(unsigned int);
     149             : 
     150             :   bool is_pbc_on() const;
     151             :   void pbc_on();
     152             :   void pbc_off();
     153             : 
     154             :   unsigned int get_n_iterations() const;
     155             :   void set_n_iterations(unsigned int);
     156             : 
     157             :   double get_sampling_radius() const;
     158             :   void set_sampling_radius(double);
     159             : 
     160             :   unsigned int get_rank_openmp() const;
     161             :   void set_rank_openmp(unsigned int);
     162             : 
     163             :   unsigned int get_stride_openmp() const;
     164             :   void set_stride_openmp(unsigned int);
     165             : 
     166             :   unsigned int get_n_threads_openmp() const;
     167             :   void set_n_threads_openmp(unsigned int);
     168             : 
     169             :   unsigned int get_nl_stride() const;
     170             :   void set_nl_stride(unsigned int);
     171             : 
     172             :   double get_nl_cutofff() const;
     173             :   void set_nl_cutoff(double);
     174             : 
     175             : protected:
     176             :   //! Optimizer label.
     177             :   std::string label_;
     178             : 
     179             :   //! Start either at time =  0 or time = optimizer stride.
     180             :   bool first_step_;
     181             : 
     182             :   //! Biasing direction.
     183             :   Vector opt_;
     184             : 
     185             :   //! Current loss function value.
     186             :   double opt_value_;
     187             : 
     188             :   //! Optimizer stride.
     189             :   unsigned int optimizer_stride_;
     190             : 
     191             :   //! Periodic boundary conditions.
     192             :   bool pbc_;
     193             : 
     194             :   //! Number of global iterations.
     195             :   unsigned int n_iter_;
     196             : 
     197             :   //! Sampling radius.
     198             :   double sampling_r_;
     199             : 
     200             :   /**
     201             :    * OpenMP
     202             :    */
     203             :   unsigned int rank_;
     204             :   unsigned int stride_;
     205             :   unsigned int n_threads_;
     206             : 
     207             :   //! Neighbor list of ligand-protein atom pairs.
     208             :   std::unique_ptr<NeighborList> neighbor_list_;
     209             : 
     210             :   //! Neighbor list cut-off.
     211             :   double nl_cutoff_;
     212             : 
     213             :   //! Neighbor list stride.
     214             :   int nl_stride_;
     215             : 
     216             : private:
     217             :   bool serial_;
     218             :   bool validate_list_;
     219             :   bool first_time_;
     220             : 
     221             :   //! Pointer to the loss function.
     222             :   Loss* loss_;
     223             :   std::vector<Loss*> vec_loss_;
     224             : 
     225             : public:
     226             :   /*
     227             :    * Pointers to PLMD components.
     228             :    */
     229             : 
     230             :   //! Biased cv.
     231             :   Value* value_x_;
     232             :   Value* value_y_;
     233             :   Value* value_z_;
     234             : 
     235             :   //! Loss value.
     236             :   Value* value_action_;
     237             :   //! Sampling radiues value.
     238             :   Value* value_sampling_radius_;
     239             : };
     240             : 
     241             : /*
     242             :  * Getters and setters.
     243             :  */
     244             : 
     245             : inline void Optimizer::set_nl_cutoff(double nl_cutoff) {
     246             :   nl_cutoff_=nl_cutoff;
     247             : }
     248             : 
     249             : inline double Optimizer::get_nl_cutofff() const {
     250             :   return nl_cutoff_;
     251             : }
     252             : 
     253             : inline void Optimizer::set_nl_stride(unsigned int nl_stride) {
     254             :   nl_stride_=nl_stride;
     255             : }
     256             : 
     257             : inline unsigned int Optimizer::get_nl_stride() const {
     258             :   return nl_stride_;
     259             : }
     260             : 
     261             : inline void Optimizer::set_n_threads_openmp(unsigned int n_threads) {
     262             :   n_threads_=n_threads;
     263             : }
     264             : 
     265             : inline unsigned int Optimizer::get_n_threads_openmp() const {
     266         320 :   return n_threads_;
     267             : }
     268             : 
     269             : inline void Optimizer::set_stride_openmp(unsigned int stride) {
     270             :   stride_=stride;
     271             : }
     272             : 
     273             : inline unsigned int Optimizer::get_stride_openmp() const {
     274             :   return stride_;
     275             : }
     276             : 
     277             : inline void Optimizer::set_rank_openmp(unsigned int rank) {
     278             :   rank_=rank;
     279             : }
     280             : 
     281             : inline unsigned int Optimizer::get_rank_openmp() const {
     282             :   return rank_;
     283             : }
     284             : 
     285             : inline void Optimizer::set_sampling_radius(double sampling_r) {
     286             :   sampling_r_=sampling_r;
     287             : }
     288             : 
     289             : inline double Optimizer::get_sampling_radius() const {
     290             :   return sampling_r_;
     291             : }
     292             : 
     293             : inline void Optimizer::set_n_iterations(unsigned int n_iter) {
     294             :   n_iter_=n_iter;
     295             : }
     296             : 
     297             : inline unsigned int Optimizer::get_n_iterations() const {
     298          33 :   return n_iter_;
     299             : }
     300             : 
     301             : inline void Optimizer::pbc_off() {
     302             :   pbc_=false;
     303             : }
     304             : 
     305             : inline void Optimizer::pbc_on() {
     306             :   pbc_=true;
     307             : }
     308             : 
     309             : inline bool Optimizer::is_pbc_on() const {
     310             :   return pbc_==true;
     311             : }
     312             : 
     313             : inline void Optimizer::set_optimizer_stride(unsigned int optimizer_stride) {
     314             :   optimizer_stride_=optimizer_stride;
     315             : }
     316             : 
     317             : inline unsigned int Optimizer::get_optimizer_stride() const {
     318           1 :   return optimizer_stride_;
     319             : }
     320             : 
     321             : inline void Optimizer::set_opt_value(double opt_value) {
     322          46 :   opt_value_=opt_value;
     323          30 : }
     324             : 
     325             : inline double Optimizer::get_opt_value() const {
     326             :   return opt_value_;
     327             : }
     328             : 
     329             : // cppcheck-suppress passedByValue
     330             : inline void Optimizer::set_opt(Vector opt) {
     331          44 :   opt_=opt;
     332             : }
     333             : 
     334             : inline Vector Optimizer::get_opt() const {
     335           3 :   return opt_;
     336             : }
     337             : 
     338             : inline void Optimizer::set_label(const std::string& label) {
     339           7 :   label_=label;
     340           7 : }
     341             : 
     342             : inline std::string Optimizer::get_label() const {
     343           1 :   return label_;
     344             : }
     345             : 
     346             : inline void Optimizer::start_step_0() {
     347           5 :   first_step_=false;
     348             : }
     349             : 
     350             : inline void Optimizer::start_step_stride() {
     351           2 :   first_step_=true;
     352             : }
     353             : 
     354             : } // namespace maze
     355             : } // namespace PLMD
     356             : 
     357             : #endif // __PLUMED_maze_Optimizer_h

Generated by: LCOV version 1.16