LCOV - code coverage report
Current view: top level - ves - VesTools.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 21 33 63.6 %
Date: 2025-03-25 09:33:27 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) {
      76             :     result++;
      77             :   }
      78             :   return result;
      79             : }
      80             : 
      81             : 
      82             : template<typename T>
      83             : std::vector<std::string> VesTools::getLabelsOfAvailableActions(const ActionSet& actionset) {
      84             :   std::vector<std::string> avail_action_str(0);
      85             :   std::vector<T> avail_action_pntrs = actionset.select<T>();
      86             :   for(unsigned int i=0; i<avail_action_pntrs.size(); i++) {
      87             :     avail_action_str.push_back(avail_action_pntrs[i]->getLabel());
      88             :   }
      89             :   return avail_action_str;
      90             : }
      91             : 
      92             : 
      93             : template<typename T>
      94         296 : T VesTools::getPointerFromLabel(const std::string& action_label, const ActionSet& actionset, std::string& error_msg) {
      95         296 :   std::vector<std::string> action_labels(1);
      96             :   action_labels[0] = action_label;
      97         296 :   std::vector<T> action_pntrs = getPointersFromLabels<T>(action_labels,actionset,error_msg);
      98         592 :   return action_pntrs[0];
      99         296 : }
     100             : 
     101             : 
     102             : template<typename T>
     103         662 : std::vector<T> VesTools::getPointersFromLabels(const std::vector<std::string>& action_labels, const ActionSet& actionset, std::string& error_msg) {
     104         662 :   std::vector<T> action_pntrs(action_labels.size(),NULL);
     105             :   error_msg = "";
     106         662 :   std::vector<std::string> missing(0);
     107        1350 :   for(unsigned int i=0; i<action_labels.size(); i++) {
     108         688 :     action_pntrs[i] = actionset.selectWithLabel<T>(action_labels[i]);
     109         688 :     if(action_pntrs[i]==NULL) {
     110           0 :       missing.push_back(action_labels[i]);
     111             :     }
     112             :   }
     113             :   // error handling
     114         662 :   if(missing.size()>0) {
     115           0 :     if(missing.size()==1) {
     116           0 :       error_msg = "label "+missing[0]+" does not exist\n";
     117           0 :     } else if(missing.size()>1) {
     118           0 :       std::string tmp="";
     119           0 :       for(unsigned int j=0; j<missing.size(); j++) {
     120           0 :         tmp +=missing[j]+" ";
     121             :       }
     122           0 :       error_msg = "labels "+tmp+"do not exist\n";
     123             :     }
     124           0 :     std::vector<T> avail_action_pntrs = actionset.select<T>();
     125           0 :     if(avail_action_pntrs.size()>0) {
     126             :       error_msg += "             Hint! the actions defined in the input file that can be used here are: \n";
     127           0 :       for(unsigned int i=0; i<avail_action_pntrs.size(); i++) {
     128           0 :         error_msg += "             " + avail_action_pntrs[i]->getName() + " with label " + avail_action_pntrs[i]->getLabel() + "\n";
     129             :       }
     130             :     } else {
     131             :       error_msg += "             Hint! no actions defined in the input file that can be used here, they should be defined before this actions\n";
     132             :     }
     133             :   }
     134         662 :   return action_pntrs;
     135         662 : }
     136             : 
     137             : 
     138             : 
     139             : }
     140             : }
     141             : 
     142             : #endif

Generated by: LCOV version 1.16