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 : 113 : return n; 114 : } 115 : 116 : template<typename T> 117 : std::vector<T> tls::Vector2vector(const Vector& v) { 118 : std::vector<T> t= {v[0], v[1], v[2]}; 119 : 120 : return t; 121 : } 122 : 123 : template<typename T> 124 1 : Vector tls::vector2Vector(const std::vector<T>& v) { 125 1 : Vector t(v[0], v[1], v[2]); 126 : 127 1 : return t; 128 : } 129 : 130 : template<typename T> 131 30 : int tls::sgn(T val) { 132 30 : return (T(0)<val)-(val<T(0)); 133 : } 134 : 135 : template<typename T> 136 : std::vector<std::string> tls::get_labels_actions(const ActionSet& actionset) { 137 : std::vector<std::string> action_str(0); 138 : std::vector<T> action_pntrs=actionset.select<T>(); 139 : 140 : for(unsigned int i=0; i<action_pntrs.size(); i++) { 141 : action_str.push_back(action_pntrs[i]->getLabel()); 142 : } 143 : 144 : return action_str; 145 : } 146 : 147 : template<typename T> T 148 : tls::get_pointer_label( 149 : const std::string& action_label, 150 : const ActionSet& actionset, 151 : std::string& error_msg) { 152 : 153 : std::vector<std::string> action_labels(1); 154 : action_labels[0]=action_label; 155 : std::vector<T> action_pntrs=get_pointers_labels<T>(action_labels, actionset, error_msg); 156 : 157 : return action_pntrs[0]; 158 : } 159 : 160 : template<typename T> 161 8 : std::vector<T> tls::get_pointers_labels( 162 : const std::vector<std::string>& action_labels, 163 : const ActionSet& actionset, 164 : std::string& error_msg) { 165 : 166 8 : std::vector<T> action_pntrs(action_labels.size(), NULL); 167 : error_msg=""; 168 8 : std::vector<std::string> missing(0); 169 : 170 16 : for(unsigned int i=0; i<action_labels.size(); i++) { 171 8 : action_pntrs[i]=actionset.selectWithLabel<T>(action_labels[i]); 172 8 : if(action_pntrs[i]==NULL) { 173 0 : missing.push_back(action_labels[i]); 174 : } 175 : } 176 : 177 8 : return action_pntrs; 178 8 : } 179 : 180 : } // namespace maze 181 : } // namespace PLMD 182 : 183 : #endif // __PLUMED_maze_Tools_h