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_CoeffsBase_h 23 : #define __PLUMED_ves_CoeffsBase_h 24 : 25 : #include <vector> 26 : #include <string> 27 : 28 : 29 : namespace PLMD { 30 : 31 : class Action; 32 : class Value; 33 : class IFile; 34 : class OFile; 35 : 36 : namespace ves { 37 : 38 : class BasisFunctions; 39 : class VesBias; 40 : 41 : /// \ingroup TOOLBOX 42 : class CoeffsBase { 43 : public: 44 : // the type of 1D index 45 : // typedef size_t index_t; 46 : // typedef unsigned int index_t; 47 : private: 48 : std::string label_; 49 : std::string data_label_; 50 : enum CoeffsType { 51 : Generic, 52 : LinearBasisSet, 53 : MultiCoeffs_LinearBasisSet 54 : } coeffs_type_; 55 : // 56 : bool iteration_and_time_active_; 57 : unsigned int iteration_opt; 58 : double time_md; 59 : // 60 : bool active; 61 : // 62 : Action* action_pntr_; 63 : VesBias* vesbias_pntr_; 64 : // 65 : unsigned int ndimensions_; 66 : std::vector<unsigned int> indices_shape_; 67 : size_t ncoeffs_; 68 : std::vector<std::string> coeffs_descriptions_; 69 : std::vector<std::string> dimension_labels_; 70 : // 71 : std::vector<Value*> args_; 72 : std::vector<BasisFunctions*> basisf_; 73 : // 74 : bool multicoeffs_; 75 : std::vector<std::vector<Value*> > multicoeffs_args_; 76 : std::vector<std::vector<BasisFunctions*> >multicoeffs_basisf_; 77 : // Labels for fields in output/input files 78 : const std::string field_type_; 79 : const std::string field_ndimensions_; 80 : const std::string field_ncoeffs_total_; 81 : const std::string field_shape_prefix_; 82 : const std::string field_time_; 83 : const std::string field_iteration_; 84 : // 85 : std::string output_fmt_; 86 : // 87 : void initializeIndices(const std::vector<unsigned int>&, const std::vector<std::string>&); 88 : void reinitializeIndices(const std::vector<unsigned int>&); 89 : public: 90 : explicit CoeffsBase(); 91 : // 92 : explicit CoeffsBase( 93 : const std::string&, 94 : const std::vector<std::string>&, 95 : const std::vector<unsigned int>&, 96 : const bool use_iteration_counter=false); 97 : // 98 : explicit CoeffsBase( 99 : const std::string&, 100 : const std::vector<Value*>&, 101 : std::vector<BasisFunctions*>&, 102 : const bool use_iteration_counter=false); 103 : // 104 : explicit CoeffsBase( 105 : const std::string&, 106 : std::vector<std::vector<Value*> >&, 107 : std::vector<std::vector<BasisFunctions*> >&, 108 : const bool use_iteration_counter=false, 109 : const std::string& multicoeffs_label="bias" 110 : ); 111 : // 112 : ~CoeffsBase(); 113 : // 114 : std::string getLabel() const { 115 228 : return label_; 116 : } 117 : void setLabel(const std::string&); 118 : std::string getDataLabel() const { 119 4366 : return data_label_; 120 : }; 121 : void setDataLabel(const std::string&); 122 : void setLabels(const std::string&); 123 : void setLabels(const std::string&, const std::string&); 124 : // 125 : CoeffsType getType() const { 126 : return coeffs_type_; 127 : } 128 : std::string getTypeStr() const; 129 : void setType(const CoeffsType coeffs_type); 130 : void linkVesBias(VesBias*); 131 : void linkAction(Action*); 132 : VesBias* getPntrToVesBias() const { 133 : return vesbias_pntr_; 134 : } 135 : Action* getPntrToAction() const { 136 9 : return action_pntr_; 137 : } 138 : bool isGenericCoeffs() const { 139 : return coeffs_type_==Generic; 140 : } 141 : bool isLinearBasisSetCoeffs() const { 142 : return coeffs_type_==LinearBasisSet; 143 : } 144 : bool isMultiLinearBasisSetCoeffs() const { 145 : return coeffs_type_==MultiCoeffs_LinearBasisSet; 146 : } 147 : // 148 : std::vector<unsigned int> shapeOfIndices() const { 149 : return indices_shape_; 150 : } 151 : unsigned int shapeOfIndices(const unsigned int dim_index) const { 152 4157 : return indices_shape_[dim_index]; 153 : } 154 : size_t numberOfCoeffs() const { 155 243037069 : return ncoeffs_; 156 : } 157 : unsigned int numberOfDimensions() const { 158 2992522 : return ndimensions_; 159 : } 160 : // 161 : bool isActive() const { 162 22810 : return active; 163 : } 164 : void activate() { 165 22810 : active=true; 166 : } 167 : void deactivate() { 168 95 : active=false; 169 : } 170 : // 171 : size_t getIndex(const std::vector<unsigned int>&) const; 172 : std::vector<unsigned int> getIndices(const size_t) const; 173 : bool indicesExist(const std::vector<unsigned int>&) const; 174 : // 175 : std::string getCoeffDescription(const size_t index) const { 176 : return coeffs_descriptions_[index]; 177 : } 178 : std::string getCoeffDescription(const std::vector<unsigned int>&) const; 179 : std::vector<std::string> getAllCoeffsDescriptions() const { 180 2348 : return coeffs_descriptions_; 181 : } 182 : void setCoeffDescription(const size_t, const std::string&); 183 : void setCoeffDescription(const std::vector<unsigned int>&, const std::string&); 184 : void setAllCoeffsDescriptions(const std::string& description_prefix="C"); 185 : void setAllCoeffsDescriptions(const std::vector<std::string>&); 186 : // 187 : std::string getDimensionLabel(const unsigned int) const; 188 : std::vector<std::string> getAllDimensionLabels() const { 189 : return dimension_labels_; 190 : } 191 : void setDimensionLabel(const unsigned int, const std::string&); 192 : void setAllDimensionLabels(const std::string&); 193 : void setAllDimensionLabels(const std::vector<std::string>&); 194 : void writeCoeffsInfoToFile(OFile&) const; 195 : void writeTimeInfoToFile(OFile&, const double) const; 196 : void getCoeffsInfoFromFile(IFile&, const bool ignore_coeffs_info=false); 197 : void checkCoeffsInfo(const std::string&, const std::string&, const unsigned int, const size_t, const std::vector<unsigned int>&); 198 : // 199 : void turnOnIterationCounter() { 200 167 : iteration_and_time_active_=true; 201 : } 202 : void turnOffIterationCounter() { 203 : iteration_and_time_active_=false; 204 : } 205 : bool isIterationCounterActive() const { 206 3414 : return iteration_and_time_active_; 207 : } 208 : void setIterationCounter(const unsigned int); 209 : void setTime(const double); 210 : void setIterationCounterAndTime(const unsigned int, const double); 211 : unsigned int getIterationCounter() const { 212 161 : return iteration_opt; 213 : } 214 : double getTimeValue() const { 215 : return time_md; 216 : } 217 : // 218 : void setOutputFmt(const std::string& ss) { 219 432 : output_fmt_=ss; 220 613 : } 221 : void resetOutputFmt() { 222 : output_fmt_="%30.16e"; 223 : } 224 : std::string getOutputFmt() const { 225 29978 : return output_fmt_; 226 : } 227 : // 228 : void replaceLabelString(const std::string&, const std::string&); 229 : protected: 230 : void setupBasisFunctionsInfo(); 231 : void resizeIndices(const std::vector<unsigned int>&); 232 : void resizeIndices(std::vector<BasisFunctions*>&); 233 : bool sameShape(const CoeffsBase&) const; 234 : // 235 : void writeIterationCounterAndTimeToFile(OFile&) const; 236 : bool getIterationCounterAndTimeFromFile(IFile&); 237 : // 238 : 239 : }; 240 : 241 : inline 242 : void CoeffsBase::setIterationCounter(const unsigned int iteration_opt_in) { 243 : iteration_opt=iteration_opt_in; 244 : } 245 : 246 : inline 247 : void CoeffsBase::setTime(const double time_md_in) { 248 : time_md=time_md_in; 249 : } 250 : 251 : inline 252 : void CoeffsBase::setIterationCounterAndTime(const unsigned int iteration_opt_in, const double time_md_in) { 253 46613 : iteration_opt=iteration_opt_in; 254 23077 : time_md=time_md_in; 255 22780 : } 256 : 257 : inline 258 : std::string CoeffsBase::getCoeffDescription(const std::vector<unsigned int>& indices) const { 259 : return getCoeffDescription(getIndex(indices)); 260 : } 261 : 262 : inline 263 : std::string CoeffsBase::getDimensionLabel(const unsigned int dim_index) const { 264 : // plumed_massert(dim_index<numberOfDimensions(),"Trying to get the label of a dimension outside the number of dimensions"); 265 12156 : return dimension_labels_[dim_index]; 266 : } 267 : 268 : 269 : // we are flattening arrays using a column-major order 270 : inline 271 10870 : size_t CoeffsBase::getIndex(const std::vector<unsigned int>& indices) const { 272 : // plumed_dbg_assert(indices.size()==ndimensions_); 273 : // for(unsigned int i=0; i<ndimensions_; i++){ 274 : // if(indices[i]>=indices_shape_[i]){ 275 : // std::string is; 276 : // Tools::convert(i,is); 277 : // std::string msg="ERROR: the system is looking for a value outside the indices along the " + is + "dimension!"; 278 : // plumed_merror(msg); 279 : // } 280 : // } 281 10870 : size_t index=indices[ndimensions_-1]; 282 13392 : for(unsigned int i=ndimensions_-1; i>0; --i) { 283 2522 : index=index*indices_shape_[i-1]+indices[i-1]; 284 : } 285 10870 : return index; 286 : } 287 : 288 : // we are flattening arrays using a column-major order 289 : inline 290 237974265 : std::vector<unsigned int> CoeffsBase::getIndices(const size_t index) const { 291 237974265 : std::vector<unsigned int> indices(ndimensions_); 292 : size_t kk=index; 293 237974265 : indices[0]=(index%indices_shape_[0]); 294 258960719 : for(unsigned int i=1; i<ndimensions_-1; ++i) { 295 20986454 : kk=(kk-indices[i-1])/indices_shape_[i-1]; 296 20986454 : indices[i]=(kk%indices_shape_[i]); 297 : } 298 237974265 : if(ndimensions_>=2) { 299 236955949 : indices[ndimensions_-1]=((kk-indices[ndimensions_-2])/indices_shape_[ndimensions_-2]); 300 : } 301 237974265 : return indices; 302 : } 303 : 304 : 305 : 306 : 307 : } 308 : } 309 : 310 : #endif