LCOV - code coverage report
Current view: top level - maze - Random_Acceleration_MD.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 39 41 95.1 %
Date: 2024-10-18 14:00:25 Functions: 3 4 75.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             : 
      23             : /**
      24             :  * @file Random_Acceleration_MD.cpp
      25             :  *
      26             :  * @author J. Rydzewski (jr@fizyka.umk.pl)
      27             :  */
      28             : 
      29             : #include "core/ActionRegister.h"
      30             : #include "Optimizer.h"
      31             : 
      32             : namespace PLMD {
      33             : namespace maze {
      34             : 
      35             : //+PLUMEDOC MAZE_OPTIMIZER MAZE_RANDOM_ACCELERATION_MD
      36             : /*
      37             : 
      38             : Performs random acceleration MD within the protein matrix.
      39             : 
      40             : \par Examples
      41             : 
      42             : Every optimizer implemented in the maze module needs a loss function as
      43             : an argument, and it should be passed using the \ref MAZE_LOSS keyword.
      44             : 
      45             : \plumedfile
      46             : MAZE_RANDOM_ACCELERATION_MD ...
      47             :   LABEL=rw
      48             : 
      49             :   OPTIMIZER_STRIDE=_
      50             :   LOSS=l
      51             :   RMIN=_
      52             : 
      53             :   LIGAND=2635-2646
      54             :   PROTEIN=1-2634
      55             : ... MAZE_RANDOM_ACCELERATION_MD
      56             : \endplumedfile
      57             : 
      58             : As shown above, each optimizer should be provided with the LIGAND and
      59             : the PROTEIN keywords.
      60             : 
      61             : */
      62             : //+ENDPLUMEDOC
      63             : 
      64             : /**
      65             :  * @class Random_Acceleration_MD Random_Acceleration_MD.cpp
      66             :  *  "maze/Random_Acceleration_MD.cpp"
      67             :  *
      68             :  * @brief Perform RAMD simulation.
      69             :  */
      70             : class Random_Acceleration_MD: public Optimizer {
      71             : public:
      72             :   /**
      73             :    * PLMD constructor.
      74             :    *
      75             :    * @param[in] ao PLMD::ActionOptions&
      76             :    */
      77             :   explicit Random_Acceleration_MD(const ActionOptions&);
      78             : 
      79             :   /**
      80             :    * Registers PLMD keywords.
      81             :    *
      82             :    * @param[in] keys PLMD keywords
      83             :    */
      84             :   static void registerKeywords(Keywords&);
      85             : 
      86             :   /**
      87             :    * Each class deriving from Optimizer needs to override this function.
      88             :    */
      89             :   void optimize() override;
      90             : 
      91             : private:
      92             :   //! Threshold distance that the ligand needs to pass.
      93             :   double r_min_;
      94             : 
      95             :   //! Total distance.
      96             :   double total_dist_;
      97             : 
      98             :   //! Distance.
      99             :   double dist_;
     100             : 
     101             :   //! Ligand center of mass.
     102             :   Vector com_;
     103             : 
     104             :   //! PLMD value for distance.
     105             :   Value* value_dist_;
     106             : 
     107             :   //! PLMD value for total distance.
     108             :   Value* value_total_dist_;
     109             : };
     110             : 
     111             : // Register MAZE_RANDOM_ACCELERATION_MD.
     112             : PLUMED_REGISTER_ACTION(Random_Acceleration_MD, "MAZE_RANDOM_ACCELERATION_MD")
     113             : 
     114           3 : void Random_Acceleration_MD::registerKeywords(Keywords& keys) {
     115           3 :   Optimizer::registerKeywords(keys);
     116             : 
     117           3 :   keys.remove("N_ITER");
     118             : 
     119           6 :   keys.add(
     120             :     "compulsory",
     121             :     "R_MIN",
     122             :     "Minimal distance traveled before sampling a new direction of biasing."
     123             :   );
     124             : 
     125           6 :   keys.addOutputComponent(
     126             :     "dist",
     127             :     "default",
     128             :     "Distance traveled in one sampling interval."
     129             :   );
     130             : 
     131           6 :   keys.addOutputComponent(
     132             :     "tdist",
     133             :     "default",
     134             :     "Total distance traveled by biased atoms."
     135             :   );
     136           3 : }
     137             : 
     138           1 : Random_Acceleration_MD::Random_Acceleration_MD(const ActionOptions& ao)
     139             :   : PLUMED_OPT_INIT(ao),
     140           1 :     total_dist_(0.0),
     141           1 :     dist_(0.0) {
     142           1 :   log.printf("maze> Random accelerated molecular dynamics.\n");
     143             : 
     144           2 :   if(keywords.exists("R_MIN")) {
     145           1 :     parse("R_MIN", r_min_);
     146             : 
     147           1 :     plumed_massert(
     148             :       r_min_ > 0,
     149             :       "maze> R_MIN should be explicitly specified and positive.\n"
     150             :     );
     151             : 
     152           1 :     log.printf(
     153             :       "maze> R_MIN read: %f [A].\n",
     154             :       r_min_
     155             :     );
     156             :   }
     157             : 
     158           1 :   set_label("RANDOM_ACCELERATION_MD");
     159           1 :   set_opt(rnd::next_plmd_vector());
     160             :   set_opt_value(0.0);
     161             : 
     162             :   start_step_stride();
     163             : 
     164           1 :   checkRead();
     165             : 
     166           1 :   com_ = center_of_mass();
     167             : 
     168           2 :   addComponent("dist");
     169           1 :   componentIsNotPeriodic("dist");
     170           1 :   value_dist_ = getPntrToComponent("dist");
     171             : 
     172           2 :   addComponent("tdist");
     173           1 :   componentIsNotPeriodic("tdist");
     174           1 :   value_total_dist_ = getPntrToComponent("tdist");
     175           1 : }
     176             : 
     177           2 : void Random_Acceleration_MD::optimize() {
     178           2 :   Vector c = center_of_mass();
     179           2 :   Vector d;
     180             : 
     181           2 :   if (pbc_) {
     182           2 :     d = pbcDistance(c, com_);
     183             :   }
     184             :   else {
     185           0 :     d = delta(c, com_);
     186             :   }
     187             : 
     188           2 :   dist_ = d.modulo();
     189           2 :   total_dist_ += dist_;
     190             : 
     191           2 :   if(dist_ < r_min_) {
     192           0 :     set_opt(rnd::next_plmd_vector());
     193             :   }
     194             : 
     195           2 :   set_opt_value(score());
     196           2 :   com_ = c;
     197             : 
     198           2 :   value_dist_->set(dist_);
     199           2 :   value_total_dist_->set(total_dist_);
     200           2 : }
     201             : 
     202             : } // namespace maze
     203             : } // namespace PLMD

Generated by: LCOV version 1.16