LCOV - code coverage report
Current view: top level - maze - Tools.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 13 14 92.9 %
Date: 2024-10-18 14:00:25 Functions: 4 4 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_Tools_h
      23             : #define __PLUMED_maze_Tools_h
      24             : 
      25             : /**
      26             :  * @file Tools.h
      27             :  *
      28             :  * @author J. Rydzewski
      29             :  */
      30             : 
      31             : #include "core/ActionSet.h"
      32             : #include "tools/Vector.h"
      33             : #include "Core.h"
      34             : 
      35             : namespace PLMD {
      36             : namespace maze {
      37             : 
      38             : /**
      39             :  * @class tls Tools.h "maze/Tools.h"
      40             :  *
      41             :  * @brief Helper functions.
      42             :  */
      43             : class tls {
      44             : public:
      45             :   template<typename T> static int sgn(T val);
      46             : 
      47             :   template<typename T>
      48             :   static std::vector<std::string> get_labels_actions(const ActionSet&);
      49             : 
      50             :   template<typename T>
      51             :   static T get_pointer_label(
      52             :     const std::string&,
      53             :     const ActionSet&, std::string&
      54             :   );
      55             : 
      56             :   template<typename T>
      57             :   static std::vector<T> get_pointers_labels(
      58             :     const std::vector<std::string>&,
      59             :     const ActionSet&, std::string&
      60             :   );
      61             : 
      62             :   template<typename T>
      63             :   static std::vector<T> Vector2vector(const Vector&);
      64             : 
      65             :   template<typename T>
      66             :   static Vector vector2Vector(const std::vector<T>&);
      67             : 
      68             :   template<typename T>
      69             :   static T vector_l(const std::vector<T>&);
      70             : 
      71             :   template<typename T>
      72             :   static std::vector<T> vector_n(const std::vector<T>&);
      73             : 
      74             :   template<typename T>
      75             :   static T mean(const std::vector<T>&);
      76             : 
      77             :   template<typename T>
      78             :   static T std(const std::vector<T>&);
      79             : 
      80             :   struct delete_ptr {
      81             :     template<typename T> void operator()(T t) {
      82             :       delete t;
      83             :     }
      84             :   };
      85             : };
      86             : 
      87             : template<typename T>
      88             : T tls::std(const std::vector<T>& v) {
      89             :   T m=mean(v);
      90             :   T sq_sum=std::inner_product(v.begin(), v.end(), v.begin(), 0.0);
      91             : 
      92             :   return std::sqrt(sq_sum/v.size()-m*m);
      93             : }
      94             : 
      95             : template<typename T>
      96             : T tls::mean(const std::vector<T>& v) {
      97             :   return std::accumulate(v.begin(), v.end(), 0.0)/v.size();
      98             : }
      99             : 
     100             : template<typename T>
     101             : T tls::vector_l(const std::vector<T>& v) {
     102             :   return std::sqrt(std::inner_product(v.begin(), v.end(), v.begin(), 0.0));
     103             : }
     104             : 
     105             : template<typename T>
     106             : std::vector<T> tls::vector_n(const std::vector<T>& v) {
     107             :   double l=vector_l(v);
     108             :   std::vector<double> n;
     109             :   for(std::size_t i=0; i<v.size(); ++i)
     110             :     n.push_back(v[i]/l);
     111             : 
     112             :   return n;
     113             : }
     114             : 
     115             : template<typename T>
     116             : std::vector<T> tls::Vector2vector(const Vector& v) {
     117             :   std::vector<T> t= {v[0], v[1], v[2]};
     118             : 
     119             :   return t;
     120             : }
     121             : 
     122             : template<typename T>
     123           1 : Vector tls::vector2Vector(const std::vector<T>& v) {
     124           1 :   Vector t(v[0], v[1], v[2]);
     125             : 
     126           1 :   return t;
     127             : }
     128             : 
     129             : template<typename T>
     130          30 : int tls::sgn(T val) {
     131          30 :   return (T(0)<val)-(val<T(0));
     132             : }
     133             : 
     134             : template<typename T>
     135             : std::vector<std::string> tls::get_labels_actions(const ActionSet& actionset) {
     136             :   std::vector<std::string> action_str(0);
     137             :   std::vector<T> action_pntrs=actionset.select<T>();
     138             : 
     139             :   for(unsigned int i=0; i<action_pntrs.size(); i++)
     140             :     action_str.push_back(action_pntrs[i]->getLabel());
     141             : 
     142             :   return action_str;
     143             : }
     144             : 
     145             : template<typename T> T
     146             : tls::get_pointer_label(
     147             :   const std::string& action_label,
     148             :   const ActionSet& actionset,
     149             :   std::string& error_msg) {
     150             : 
     151             :   std::vector<std::string> action_labels(1);
     152             :   action_labels[0]=action_label;
     153             :   std::vector<T> action_pntrs=get_pointers_labels<T>(action_labels, actionset, error_msg);
     154             : 
     155             :   return action_pntrs[0];
     156             : }
     157             : 
     158             : template<typename T>
     159           8 : std::vector<T> tls::get_pointers_labels(
     160             :   const std::vector<std::string>& action_labels,
     161             :   const ActionSet& actionset,
     162             :   std::string& error_msg) {
     163             : 
     164           8 :   std::vector<T> action_pntrs(action_labels.size(), NULL);
     165             :   error_msg="";
     166           8 :   std::vector<std::string> missing(0);
     167             : 
     168          16 :   for(unsigned int i=0; i<action_labels.size(); i++) {
     169           8 :     action_pntrs[i]=actionset.selectWithLabel<T>(action_labels[i]);
     170           8 :     if(action_pntrs[i]==NULL)
     171           0 :       missing.push_back(action_labels[i]);
     172             :   }
     173             : 
     174           8 :   return action_pntrs;
     175           8 : }
     176             : 
     177             : } // namespace maze
     178             : } // namespace PLMD
     179             : 
     180             : #endif // __PLUMED_maze_Tools_h

Generated by: LCOV version 1.16