LCOV - code coverage report
Current view: top level - ves - BF_Combined.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 64 67 95.5 %
Date: 2025-12-04 11:19:34 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             : 
      23             : #include "BasisFunctions.h"
      24             : #include "VesTools.h"
      25             : 
      26             : #include "core/ActionRegister.h"
      27             : #include "core/ActionSet.h"
      28             : #include "core/PlumedMain.h"
      29             : 
      30             : 
      31             : namespace PLMD {
      32             : namespace ves {
      33             : 
      34             : //+PLUMEDOC VES_BASISF BF_COMBINED
      35             : /*
      36             : Combining other basis functions types
      37             : 
      38             : ##Examples
      39             : 
      40             : Here we define both Fourier cosine and sine expansions of order 10,
      41             : each with 11 basis functions, which are combined. This results
      42             : in a total number of 21 basis functions as only the constant from
      43             : is bf_cos is used.
      44             : 
      45             : ```plumed
      46             : bf_cos: BF_COSINE MINIMUM=-pi MAXIMUM=+pi ORDER=10
      47             : bf_sin: BF_SINE   MINIMUM=-pi MAXIMUM=+pi ORDER=10
      48             : bf_comb: BF_COMBINED BASIS_FUNCTIONS=bf_cos,bf_sin
      49             : ```
      50             : 
      51             : In principle this is the same as using BF_FOURIER with
      52             : ORDER=10 but with different ordering of the basis functions.
      53             : Note that the order used in BASIS_FUNCTIONS matters for the ordering
      54             : of the basis functions, using BASIS_FUNCTIONS=bf_sin,bf_cos would
      55             : results in a different order of the basis functions.
      56             : This should be kept in mind when restarting from previous
      57             : coefficients.
      58             : 
      59             : 
      60             : 
      61             : 
      62             : 
      63             : 
      64             : */
      65             : //+ENDPLUMEDOC
      66             : 
      67             : class BF_Combined : public BasisFunctions {
      68             :   std::vector<BasisFunctions*> basisf_pntrs_;
      69             :   void setupLabels() override;
      70             :   void setupUniformIntegrals() override;
      71             :   // void getBFandValueIndices(const unsigned int, unsigned int&, unsigned int&) const;
      72             : public:
      73             :   static void registerKeywords(Keywords&);
      74             :   explicit BF_Combined(const ActionOptions&);
      75             :   void getAllValues(const double, double&, bool&, std::vector<double>&, std::vector<double>&) const override;
      76             : };
      77             : 
      78             : 
      79             : PLUMED_REGISTER_ACTION(BF_Combined,"BF_COMBINED")
      80             : 
      81             : 
      82           4 : void BF_Combined::registerKeywords(Keywords& keys) {
      83           4 :   BasisFunctions::registerKeywords(keys);
      84           4 :   keys.remove("ORDER");
      85           4 :   keys.remove("MAXIMUM");
      86           4 :   keys.remove("MINIMUM");
      87           4 :   keys.remove("NUMERICAL_INTEGRALS");
      88           8 :   keys.remove("NGRID_POINTS");
      89           4 :   keys.add("compulsory","BASIS_FUNCTIONS","Labels of the basis functions that should be combined. Note that the order used matters for the ordering of the basis functions. This needs to be kept in mind when restarting from previous coefficients.");
      90           4 : }
      91             : 
      92             : 
      93           2 : BF_Combined::BF_Combined(const ActionOptions&ao):
      94             :   PLUMED_VES_BASISFUNCTIONS_INIT(ao),
      95           2 :   basisf_pntrs_(0) {
      96             :   std::vector<std::string> basisf_labels;
      97           2 :   parseVector("BASIS_FUNCTIONS",basisf_labels);
      98           4 :   addKeywordToList("BASIS_FUNCTIONS",basisf_labels);
      99           2 :   if(basisf_labels.size()==1) {
     100           0 :     plumed_merror("using only one basis function in BF_COMBINED does not make sense");
     101             :   }
     102           2 :   std::string error_msg = "";
     103           4 :   basisf_pntrs_ = VesTools::getPointersFromLabels<BasisFunctions*>(basisf_labels,plumed.getActionSet(),error_msg);
     104           2 :   if(error_msg.size()>0) {
     105           0 :     plumed_merror("Error in keyword BASIS_FUNCTIONS of "+getName()+": "+error_msg);
     106             :   }
     107             : 
     108             :   unsigned int nbasisf_total_ = 1;
     109             :   bool periodic = true;
     110           7 :   for(unsigned int i=0; i<basisf_pntrs_.size(); i++) {
     111           5 :     nbasisf_total_ += basisf_pntrs_[i]->getNumberOfBasisFunctions() - 1;
     112          15 :     if(basisf_pntrs_[i]->intervalMinStr()!=basisf_pntrs_[0]->intervalMinStr() || basisf_pntrs_[i]->intervalMaxStr()!=basisf_pntrs_[0]->intervalMaxStr()) {
     113           0 :       plumed_merror("all the basis functions to be combined should have same MINIMUM and MAXIMUM");
     114             :     }
     115           5 :     if(!basisf_pntrs_[i]->arePeriodic()) {
     116             :       periodic=false;
     117             :     }
     118             :   }
     119           2 :   setOrder(nbasisf_total_-1);
     120           2 :   setNumberOfBasisFunctions(nbasisf_total_);
     121           4 :   setInterval(basisf_pntrs_[0]->intervalMinStr(),basisf_pntrs_[0]->intervalMaxStr());
     122           4 :   setIntrinsicInterval("-1.0","+1.0");
     123           2 :   if(periodic) {
     124             :     setPeriodic();
     125             :   } else {
     126             :     setNonPeriodic();
     127             :   }
     128             :   setIntervalBounded();
     129           2 :   setType("combined");
     130           2 :   setDescription("Combined");
     131           2 :   setupBF();
     132           2 :   checkRead();
     133           2 : }
     134             : 
     135             : 
     136             : // void BF_Combined::getBFandValueIndices(const unsigned int n, unsigned int& bf_index, unsigned int& value_index) const {
     137             : //   bf_index = 0; value_index = 0;
     138             : //   if(n==0){
     139             : //     bf_index = 0;
     140             : //     value_index = 0;
     141             : //     return;
     142             : //   }
     143             : //   else{
     144             : //     unsigned int r=1;
     145             : //     for(unsigned int i=0; i<basisf_pntrs_.size(); i++){
     146             : //       for(unsigned int l=1; l<basisf_pntrs_[i]->numberOfBasisFunctions(); l++){
     147             : //         if(r==n){
     148             : //           bf_index = i;
     149             : //           value_index = l;
     150             : //           return;
     151             : //         }
     152             : //         r++;
     153             : //       }
     154             : //     }
     155             : //   }
     156             : // }
     157             : 
     158             : 
     159        3616 : void BF_Combined::getAllValues(const double arg, double& argT, bool& inside_range, std::vector<double>& values, std::vector<double>& derivs) const {
     160             :   // first BF, constant, argT, and inside_range taken from here
     161             :   unsigned int r=0;
     162        3616 :   std::vector<double> values_tmp(basisf_pntrs_[0]->numberOfBasisFunctions(),0.0);
     163        3616 :   std::vector<double> derivs_tmp(basisf_pntrs_[0]->numberOfBasisFunctions(),0.0);
     164        3616 :   basisf_pntrs_[0]->getAllValues(arg,argT,inside_range,values_tmp,derivs_tmp);
     165       43392 :   for(unsigned int l=0; l<basisf_pntrs_[0]->numberOfBasisFunctions(); l++) {
     166       39776 :     values[r] = values_tmp[l];
     167       39776 :     derivs[r] = derivs_tmp[l];
     168       39776 :     r++;
     169             :   }
     170             :   // other BF
     171        9043 :   for(unsigned int i=1; i<basisf_pntrs_.size(); i++) {
     172        5427 :     values_tmp.assign(basisf_pntrs_[i]->numberOfBasisFunctions(),0.0);
     173        5427 :     derivs_tmp.assign(basisf_pntrs_[i]->numberOfBasisFunctions(),0.0);
     174             :     double dummy_dbl;
     175        5427 :     bool dummy_bool=true;
     176        5427 :     basisf_pntrs_[i]->getAllValues(arg,dummy_dbl,dummy_bool,values_tmp,derivs_tmp);
     177       59697 :     for(unsigned int l=1; l<basisf_pntrs_[i]->numberOfBasisFunctions(); l++) {
     178       54270 :       values[r] = values_tmp[l];
     179       54270 :       derivs[r] = derivs_tmp[l];
     180       54270 :       r++;
     181             :     }
     182             :   }
     183        3616 : }
     184             : 
     185             : 
     186           2 : void BF_Combined::setupLabels() {
     187           2 :   setLabel(0,basisf_pntrs_[0]->getBasisFunctionLabel(0));
     188             :   unsigned int r=1;
     189           7 :   for(unsigned int i=0; i<basisf_pntrs_.size(); i++) {
     190          55 :     for(unsigned int l=1; l<basisf_pntrs_[i]->numberOfBasisFunctions(); l++) {
     191          50 :       setLabel(r,basisf_pntrs_[i]->getBasisFunctionLabel(l));
     192          50 :       r++;
     193             :     }
     194             :   }
     195           2 : }
     196             : 
     197             : 
     198           2 : void BF_Combined::setupUniformIntegrals() {
     199           2 :   setUniformIntegral(0,basisf_pntrs_[0]->getUniformIntegrals()[0]);
     200             :   unsigned int r=1;
     201           7 :   for(unsigned int i=0; i<basisf_pntrs_.size(); i++) {
     202           5 :     std::vector<double> uniform_tmp = basisf_pntrs_[i]->getUniformIntegrals();
     203          55 :     for(unsigned int l=1; l<basisf_pntrs_[i]->numberOfBasisFunctions(); l++) {
     204          50 :       setUniformIntegral(r,uniform_tmp[l]);
     205          50 :       r++;
     206             :     }
     207             :   }
     208           2 : }
     209             : 
     210             : 
     211             : }
     212             : }

Generated by: LCOV version 1.16