LCOV - code coverage report
Current view: top level - ves - CoeffsBase.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 33 33 100.0 %
Date: 2024-10-18 13:59:31 Functions: 2 2 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_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             : {
      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             :     const 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         228 :   std::string getLabel() const {return label_;}
     116             :   void setLabel(const std::string&);
     117        4366 :   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        4157 :   unsigned int shapeOfIndices(const unsigned int dim_index) const {return indices_shape_[dim_index];}
     135   243037069 :   size_t numberOfCoeffs() const {return ncoeffs_;}
     136     2992522 :   unsigned int numberOfDimensions() const {return ndimensions_;}
     137             :   //
     138       22810 :   bool isActive() const {return active;}
     139       22810 :   void activate() {active=true;}
     140          95 :   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        2348 :   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         167 :   void turnOnIterationCounter() {iteration_and_time_active_=true;}
     165             :   void turnOffIterationCounter() {iteration_and_time_active_=false;}
     166        3414 :   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         161 :   unsigned int getIterationCounter() const {return iteration_opt;}
     171             :   double getTimeValue() const {return time_md;}
     172             :   //
     173         679 :   void setOutputFmt(const std::string& ss) { output_fmt_=ss; }
     174             :   void resetOutputFmt() {output_fmt_="%30.16e";}
     175       29978 :   std::string getOutputFmt() const {return output_fmt_;}
     176             :   //
     177             :   void replaceLabelString(const std::string&, const std::string&);
     178             : protected:
     179             :   void setupBasisFunctionsInfo();
     180             :   void resizeIndices(const std::vector<unsigned int>&);
     181             :   void resizeIndices(std::vector<BasisFunctions*>&);
     182             :   bool sameShape(const CoeffsBase&) const;
     183             :   //
     184             :   void writeIterationCounterAndTimeToFile(OFile&) const;
     185             :   bool getIterationCounterAndTimeFromFile(IFile&);
     186             :   //
     187             : 
     188             : };
     189             : 
     190             : inline
     191             : void CoeffsBase::setIterationCounter(const unsigned int iteration_opt_in) {
     192             :   iteration_opt=iteration_opt_in;
     193             : }
     194             : 
     195             : inline
     196             : void CoeffsBase::setTime(const double time_md_in) {
     197             :   time_md=time_md_in;
     198             : }
     199             : 
     200             : inline
     201             : void CoeffsBase::setIterationCounterAndTime(const unsigned int iteration_opt_in, const double time_md_in) {
     202       46613 :   iteration_opt=iteration_opt_in;
     203       23077 :   time_md=time_md_in;
     204       22780 : }
     205             : 
     206             : inline
     207             : std::string CoeffsBase::getCoeffDescription(const std::vector<unsigned int>& indices) const {
     208             :   return getCoeffDescription(getIndex(indices));
     209             : }
     210             : 
     211             : inline
     212             : std::string CoeffsBase::getDimensionLabel(const unsigned int dim_index) const {
     213             :   // plumed_massert(dim_index<numberOfDimensions(),"Trying to get the label of a dimension outside the number of dimensions");
     214       12156 :   return dimension_labels_[dim_index];
     215             : }
     216             : 
     217             : 
     218             : // we are flattening arrays using a column-major order
     219             : inline
     220       10870 : size_t CoeffsBase::getIndex(const std::vector<unsigned int>& indices) const {
     221             :   // plumed_dbg_assert(indices.size()==ndimensions_);
     222             :   // for(unsigned int i=0; i<ndimensions_; i++){
     223             :   //   if(indices[i]>=indices_shape_[i]){
     224             :   //     std::string is;
     225             :   //     Tools::convert(i,is);
     226             :   //     std::string msg="ERROR: the system is looking for a value outside the indices along the " + is + "dimension!";
     227             :   //     plumed_merror(msg);
     228             :   //   }
     229             :   // }
     230       10870 :   size_t index=indices[ndimensions_-1];
     231       13392 :   for(unsigned int i=ndimensions_-1; i>0; --i) {
     232        2522 :     index=index*indices_shape_[i-1]+indices[i-1];
     233             :   }
     234       10870 :   return index;
     235             : }
     236             : 
     237             : // we are flattening arrays using a column-major order
     238             : inline
     239   237974265 : std::vector<unsigned int> CoeffsBase::getIndices(const size_t index) const {
     240   237974265 :   std::vector<unsigned int> indices(ndimensions_);
     241             :   size_t kk=index;
     242   237974265 :   indices[0]=(index%indices_shape_[0]);
     243   258960719 :   for(unsigned int i=1; i<ndimensions_-1; ++i) {
     244    20986454 :     kk=(kk-indices[i-1])/indices_shape_[i-1];
     245    20986454 :     indices[i]=(kk%indices_shape_[i]);
     246             :   }
     247   237974265 :   if(ndimensions_>=2) {
     248   236955949 :     indices[ndimensions_-1]=((kk-indices[ndimensions_-2])/indices_shape_[ndimensions_-2]);
     249             :   }
     250   237974265 :   return indices;
     251             : }
     252             : 
     253             : 
     254             : 
     255             : 
     256             : }
     257             : }
     258             : 
     259             : #endif

Generated by: LCOV version 1.16