Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2016-2018 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 Grid;
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 : // copy grid values
46 : static void copyGridValues(Grid* grid_pntr_orig, Grid* grid_pntr_copy);
47 : 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&);
48 : //
49 : template<typename T> static std::vector<std::string> getLabelsOfAvailableActions(const ActionSet&);
50 : template<typename T> static T getPointerFromLabel(const std::string&, const ActionSet&, std::string&);
51 : template<typename T> static std::vector<T> getPointersFromLabels(const std::vector<std::string>&, const ActionSet&, std::string&);
52 :
53 : };
54 :
55 : inline
56 53 : void VesTools::convertDbl2Str(const double value,std::string& str, unsigned int precision) {
57 106 : std::ostringstream ostr;
58 53 : ostr<<std::setprecision(precision)<<value;
59 106 : str=ostr.str();
60 53 : }
61 :
62 :
63 : inline
64 : void VesTools::convertDbl2Str(const double value,std::string& str) {
65 : unsigned int precision = std::numeric_limits<double>::digits10 + 1;
66 53 : convertDbl2Str(value,str,precision);
67 : }
68 :
69 :
70 : template<typename T>
71 : std::vector<std::string> VesTools::getLabelsOfAvailableActions(const ActionSet& actionset) {
72 : std::vector<std::string> avail_action_str(0);
73 : std::vector<T> avail_action_pntrs = actionset.select<T>();
74 : for(unsigned int i=0; i<avail_action_pntrs.size(); i++) {
75 : avail_action_str.push_back(avail_action_pntrs[i]->getLabel());
76 : }
77 : return avail_action_str;
78 : }
79 :
80 :
81 : template<typename T>
82 236 : T VesTools::getPointerFromLabel(const std::string& action_label, const ActionSet& actionset, std::string& error_msg) {
83 472 : std::vector<std::string> action_labels(1);
84 : action_labels[0] = action_label;
85 236 : std::vector<T> action_pntrs = getPointersFromLabels<T>(action_labels,actionset,error_msg);
86 472 : return action_pntrs[0];
87 : }
88 :
89 :
90 : template<typename T>
91 566 : std::vector<T> VesTools::getPointersFromLabels(const std::vector<std::string>& action_labels, const ActionSet& actionset, std::string& error_msg) {
92 1132 : std::vector<T> action_pntrs(action_labels.size(),NULL);
93 : error_msg = "";
94 1132 : std::vector<std::string> missing(0);
95 2905 : for(unsigned int i=0; i<action_labels.size(); i++) {
96 591 : action_pntrs[i] = actionset.selectWithLabel<T>(action_labels[i]);
97 591 : if(action_pntrs[i]==NULL) {
98 0 : missing.push_back(action_labels[i]);
99 : }
100 : }
101 : // error handling
102 566 : if(missing.size()>0) {
103 0 : if(missing.size()==1) {
104 0 : error_msg = "label "+missing[0]+" does not exist\n";
105 : }
106 0 : else if(missing.size()>1) {
107 0 : std::string tmp="";
108 0 : for(unsigned int j=0; j<missing.size(); j++) {tmp +=missing[j]+" ";}
109 0 : error_msg = "labels "+tmp+"do not exist\n";
110 : }
111 0 : std::vector<T> avail_action_pntrs = actionset.select<T>();
112 0 : if(avail_action_pntrs.size()>0) {
113 : error_msg += " Hint! the actions defined in the input file that can be used here are: \n";
114 0 : for(unsigned int i=0; i<avail_action_pntrs.size(); i++) {
115 0 : error_msg += " " + avail_action_pntrs[i]->getName() + " with label " + avail_action_pntrs[i]->getLabel() + "\n";
116 : }
117 : }
118 : else {
119 : error_msg += " Hint! no actions defined in the input file that can be used here, they should be defined before this actions\n";
120 : }
121 : }
122 566 : return action_pntrs;
123 : }
124 :
125 :
126 :
127 : }
128 : }
129 :
130 : #endif
|