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_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 111540 : class CoeffsBase 43 : { 44 : public: 45 : // the type of 1D index 46 : // typedef size_t index_t; 47 : // typedef unsigned int index_t; 48 : private: 49 : std::string label_; 50 : std::string data_label_; 51 : enum CoeffsType { 52 : Generic, 53 : LinearBasisSet, 54 : MultiCoeffs_LinearBasisSet 55 : } coeffs_type_; 56 : // 57 : bool iteration_and_time_active_; 58 : unsigned int iteration_opt; 59 : double time_md; 60 : // 61 : bool active; 62 : // 63 : Action* action_pntr_; 64 : VesBias* vesbias_pntr_; 65 : // 66 : unsigned int ndimensions_; 67 : std::vector<unsigned int> indices_shape_; 68 : size_t ncoeffs_; 69 : std::vector<std::string> coeffs_descriptions_; 70 : std::vector<std::string> dimension_labels_; 71 : // 72 : std::vector<Value*> args_; 73 : std::vector<BasisFunctions*> basisf_; 74 : // 75 : bool multicoeffs_; 76 : std::vector<std::vector<Value*> > multicoeffs_args_; 77 : std::vector<std::vector<BasisFunctions*> >multicoeffs_basisf_; 78 : // Labels for fields in output/input files 79 : const std::string field_type_; 80 : const std::string field_ndimensions_; 81 : const std::string field_ncoeffs_total_; 82 : const std::string field_shape_prefix_; 83 : const std::string field_time_; 84 : const std::string field_iteration_; 85 : // 86 : std::string output_fmt_; 87 : // 88 : void initializeIndices(const std::vector<unsigned int>&, const std::vector<std::string>&); 89 : void reinitializeIndices(const std::vector<unsigned int>&); 90 : public: 91 : explicit CoeffsBase(); 92 : // 93 : explicit CoeffsBase( 94 : const std::string&, 95 : const std::vector<std::string>&, 96 : const std::vector<unsigned int>&, 97 : const bool use_iteration_counter=false); 98 : // 99 : explicit CoeffsBase( 100 : const std::string&, 101 : std::vector<Value*>&, 102 : std::vector<BasisFunctions*>&, 103 : const bool use_iteration_counter=false); 104 : // 105 : explicit CoeffsBase( 106 : const std::string&, 107 : std::vector<std::vector<Value*> >&, 108 : std::vector<std::vector<BasisFunctions*> >&, 109 : const bool use_iteration_counter=false, 110 : const std::string& multicoeffs_label="bias" 111 : ); 112 : // 113 : ~CoeffsBase(); 114 : // 115 : std::string getLabel() const {return label_;} 116 : void setLabel(const std::string&); 117 : std::string getDataLabel() const {return data_label_;}; 118 : void setDataLabel(const std::string&); 119 : void setLabels(const std::string&); 120 : void setLabels(const std::string&, const std::string&); 121 : // 122 : CoeffsType getType() const {return coeffs_type_;} 123 : std::string getTypeStr() const; 124 : void setType(const CoeffsType coeffs_type); 125 : void linkVesBias(VesBias*); 126 : void linkAction(Action*); 127 : VesBias* getPntrToVesBias() const {return vesbias_pntr_;} 128 9 : Action* getPntrToAction() const {return action_pntr_;} 129 : bool isGenericCoeffs() const {return coeffs_type_==Generic;} 130 : bool isLinearBasisSetCoeffs() const {return coeffs_type_==LinearBasisSet;} 131 : bool isMultiLinearBasisSetCoeffs() const {return coeffs_type_==MultiCoeffs_LinearBasisSet;} 132 : // 133 : std::vector<unsigned int> shapeOfIndices() const {return indices_shape_;} 134 3978 : unsigned int shapeOfIndices(const unsigned int dim_index) const {return indices_shape_[dim_index];} 135 216687554 : size_t numberOfCoeffs() const {return ncoeffs_;} 136 2845226 : unsigned int numberOfDimensions() const {return ndimensions_;} 137 : // 138 770 : bool isActive() const {return active;} 139 1540 : void activate() {active=true;} 140 180 : void deactivate() {active=false;} 141 : // 142 : size_t getIndex(const std::vector<unsigned int>&) const; 143 : std::vector<unsigned int> getIndices(const size_t) const; 144 : bool indicesExist(const std::vector<unsigned int>&) const; 145 : // 146 : std::string getCoeffDescription(const size_t index) const {return coeffs_descriptions_[index];} 147 : std::string getCoeffDescription(const std::vector<unsigned int>&) const; 148 2182 : std::vector<std::string> getAllCoeffsDescriptions() const {return coeffs_descriptions_;} 149 : void setCoeffDescription(const size_t, const std::string&); 150 : void setCoeffDescription(const std::vector<unsigned int>&, const std::string&); 151 : void setAllCoeffsDescriptions(const std::string& description_prefix="C"); 152 : void setAllCoeffsDescriptions(const std::vector<std::string>&); 153 : // 154 : std::string getDimensionLabel(const unsigned int) const; 155 : std::vector<std::string> getAllDimensionLabels() const {return dimension_labels_;} 156 : void setDimensionLabel(const unsigned int, const std::string&); 157 : void setAllDimensionLabels(const std::string&); 158 : void setAllDimensionLabels(const std::vector<std::string>&); 159 : void writeCoeffsInfoToFile(OFile&) const; 160 : void writeTimeInfoToFile(OFile&, const double) const; 161 : void getCoeffsInfoFromFile(IFile&, const bool ignore_coeffs_info=false); 162 : void checkCoeffsInfo(const std::string&, const std::string&, const unsigned int, const size_t, const std::vector<unsigned int>&); 163 : // 164 308 : void turnOnIterationCounter() {iteration_and_time_active_=true;} 165 : void turnOffIterationCounter() {iteration_and_time_active_=false;} 166 3238 : bool isIterationCounterActive() const {return iteration_and_time_active_;} 167 : void setIterationCounter(const unsigned int); 168 : void setTime(const double); 169 : void setIterationCounterAndTime(const unsigned int, const double); 170 153 : unsigned int getIterationCounter() const {return iteration_opt;} 171 : double getTimeValue() const {return time_md;} 172 : // 173 408 : void setOutputFmt(const std::string& ss) { output_fmt_=ss; } 174 : void resetOutputFmt() {output_fmt_="%30.16e";} 175 : std::string getOutputFmt() const {return output_fmt_;} 176 : // 177 : protected: 178 : void setupBasisFunctionsInfo(); 179 : void resizeIndices(const std::vector<unsigned int>&); 180 : void resizeIndices(std::vector<BasisFunctions*>&); 181 : bool sameShape(const CoeffsBase&) const; 182 : // 183 : void writeIterationCounterAndTimeToFile(OFile&) const; 184 : bool getIterationCounterAndTimeFromFile(IFile&); 185 : // 186 : 187 : }; 188 : 189 : inline 190 : void CoeffsBase::setIterationCounter(const unsigned int iteration_opt_in) { 191 : iteration_opt=iteration_opt_in; 192 : } 193 : 194 : inline 195 : void CoeffsBase::setTime(const double time_md_in) { 196 : time_md=time_md_in; 197 : } 198 : 199 : inline 200 : void CoeffsBase::setIterationCounterAndTime(const unsigned int iteration_opt_in, const double time_md_in) { 201 4804 : iteration_opt=iteration_opt_in; 202 4804 : time_md=time_md_in; 203 : } 204 : 205 : inline 206 : std::string CoeffsBase::getCoeffDescription(const std::vector<unsigned int>& indices) const { 207 : return getCoeffDescription(getIndex(indices)); 208 : } 209 : 210 : inline 211 : std::string CoeffsBase::getDimensionLabel(const unsigned int dim_index) const { 212 : // plumed_massert(dim_index<numberOfDimensions(),"Trying to get the label of a dimension outside the number of dimensions"); 213 8000 : return dimension_labels_[dim_index]; 214 : } 215 : 216 : 217 : // we are flattening arrays using a column-major order 218 : inline 219 : size_t CoeffsBase::getIndex(const std::vector<unsigned int>& indices) const { 220 : // plumed_dbg_assert(indices.size()==ndimensions_); 221 : // for(unsigned int i=0; i<ndimensions_; i++){ 222 : // if(indices[i]>=indices_shape_[i]){ 223 : // std::string is; 224 : // Tools::convert(i,is); 225 : // std::string msg="ERROR: the system is looking for a value outside the indices along the " + is + "dimension!"; 226 : // plumed_merror(msg); 227 : // } 228 : // } 229 21696 : size_t index=indices[ndimensions_-1]; 230 13348 : for(unsigned int i=ndimensions_-1; i>0; --i) { 231 7500 : index=index*indices_shape_[i-1]+indices[i-1]; 232 : } 233 : return index; 234 : } 235 : 236 : // we are flattening arrays using a column-major order 237 : inline 238 212113655 : std::vector<unsigned int> CoeffsBase::getIndices(const size_t index) const { 239 212113655 : std::vector<unsigned int> indices(ndimensions_); 240 : size_t kk=index; 241 424227310 : indices[0]=(index%indices_shape_[0]); 242 212113655 : for(unsigned int i=1; i<ndimensions_-1; ++i) { 243 0 : kk=(kk-indices[i-1])/indices_shape_[i-1]; 244 0 : indices[i]=(kk%indices_shape_[i]); 245 : } 246 212113655 : if(ndimensions_>=2) { 247 845308524 : indices[ndimensions_-1]=((kk-indices[ndimensions_-2])/indices_shape_[ndimensions_-2]); 248 : } 249 212113655 : return indices; 250 : } 251 : 252 : 253 : 254 : 255 : } 256 : } 257 : 258 : #endif