LCOV - code coverage report
Current view: top level - ves - VesTools.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 21 32 65.6 %
Date: 2024-10-18 13:59:31 Functions: 5 5 100.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_VesTools_h
      23             : #define __PLUMED_ves_VesTools_h
      24             : 
      25             : #include <string>
      26             : #include <sstream>
      27             : #include <iomanip>
      28             : #include <limits>
      29             : #include <vector>
      30             : 
      31             : #include "core/ActionSet.h"
      32             : 
      33             : 
      34             : namespace PLMD {
      35             : 
      36             : class GridBase;
      37             : 
      38             : namespace ves {
      39             : 
      40             : class VesTools {
      41             : public:
      42             :   // Convert double into a string with more digits
      43             :   static void convertDbl2Str(const double value,std::string& str, unsigned int precision);
      44             :   static void convertDbl2Str(const double value,std::string& str);
      45             :   // get log2 of unsigned int
      46             :   static unsigned int log2(unsigned value);
      47             :   // copy grid values
      48             :   static void copyGridValues(GridBase* grid_pntr_orig, GridBase* grid_pntr_copy);
      49             :   static unsigned int getGridFileInfo(const std::string&, std::string&, std::vector<std::string>&, std::vector<std::string>&, std::vector<std::string>&, std::vector<bool>&, std::vector<unsigned int>&, bool&);
      50             :   //
      51             :   template<typename T> static std::vector<std::string> getLabelsOfAvailableActions(const ActionSet&);
      52             :   template<typename T> static T getPointerFromLabel(const std::string&, const ActionSet&, std::string&);
      53             :   template<typename T> static std::vector<T> getPointersFromLabels(const std::vector<std::string>&, const ActionSet&, std::string&);
      54             : 
      55             : };
      56             : 
      57             : inline
      58         167 : void VesTools::convertDbl2Str(const double value,std::string& str, unsigned int precision) {
      59         167 :   std::ostringstream ostr;
      60         167 :   ostr<<std::setprecision(precision)<<value;
      61         167 :   str=ostr.str();
      62         167 : }
      63             : 
      64             : 
      65             : inline
      66             : void VesTools::convertDbl2Str(const double value,std::string& str) {
      67             :   unsigned int precision = std::numeric_limits<double>::digits10 + 1;
      68          88 :   convertDbl2Str(value,str,precision);
      69           9 : }
      70             : 
      71             : 
      72             : inline
      73             : unsigned int log2(unsigned value) {
      74             :   unsigned int result = 0;
      75             :   while(value >>= 1) result++;
      76             :   return result;
      77             : }
      78             : 
      79             : 
      80             : template<typename T>
      81             : std::vector<std::string> VesTools::getLabelsOfAvailableActions(const ActionSet& actionset) {
      82             :   std::vector<std::string> avail_action_str(0);
      83             :   std::vector<T> avail_action_pntrs = actionset.select<T>();
      84             :   for(unsigned int i=0; i<avail_action_pntrs.size(); i++) {
      85             :     avail_action_str.push_back(avail_action_pntrs[i]->getLabel());
      86             :   }
      87             :   return avail_action_str;
      88             : }
      89             : 
      90             : 
      91             : template<typename T>
      92         296 : T VesTools::getPointerFromLabel(const std::string& action_label, const ActionSet& actionset, std::string& error_msg) {
      93         296 :   std::vector<std::string> action_labels(1);
      94             :   action_labels[0] = action_label;
      95         296 :   std::vector<T> action_pntrs = getPointersFromLabels<T>(action_labels,actionset,error_msg);
      96         592 :   return action_pntrs[0];
      97         296 : }
      98             : 
      99             : 
     100             : template<typename T>
     101         662 : std::vector<T> VesTools::getPointersFromLabels(const std::vector<std::string>& action_labels, const ActionSet& actionset, std::string& error_msg) {
     102         662 :   std::vector<T> action_pntrs(action_labels.size(),NULL);
     103             :   error_msg = "";
     104         662 :   std::vector<std::string> missing(0);
     105        1350 :   for(unsigned int i=0; i<action_labels.size(); i++) {
     106         688 :     action_pntrs[i] = actionset.selectWithLabel<T>(action_labels[i]);
     107         688 :     if(action_pntrs[i]==NULL) {
     108           0 :       missing.push_back(action_labels[i]);
     109             :     }
     110             :   }
     111             :   // error handling
     112         662 :   if(missing.size()>0) {
     113           0 :     if(missing.size()==1) {
     114           0 :       error_msg = "label "+missing[0]+" does not exist\n";
     115             :     }
     116           0 :     else if(missing.size()>1) {
     117           0 :       std::string tmp="";
     118           0 :       for(unsigned int j=0; j<missing.size(); j++) {tmp +=missing[j]+" ";}
     119           0 :       error_msg = "labels "+tmp+"do not exist\n";
     120             :     }
     121           0 :     std::vector<T> avail_action_pntrs = actionset.select<T>();
     122           0 :     if(avail_action_pntrs.size()>0) {
     123             :       error_msg += "             Hint! the actions defined in the input file that can be used here are: \n";
     124           0 :       for(unsigned int i=0; i<avail_action_pntrs.size(); i++) {
     125           0 :         error_msg += "             " + avail_action_pntrs[i]->getName() + " with label " + avail_action_pntrs[i]->getLabel() + "\n";
     126             :       }
     127             :     }
     128             :     else {
     129             :       error_msg += "             Hint! no actions defined in the input file that can be used here, they should be defined before this actions\n";
     130             :     }
     131             :   }
     132         662 :   return action_pntrs;
     133         662 : }
     134             : 
     135             : 
     136             : 
     137             : }
     138             : }
     139             : 
     140             : #endif

Generated by: LCOV version 1.16