LCOV - code coverage report
Current view: top level - ves - OutputBasisFunctions.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 111 116 95.7 %
Date: 2025-12-04 11:19:34 Functions: 2 4 50.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 "TargetDistribution.h"
      25             : 
      26             : #include "CoeffsVector.h"
      27             : #include "VesTools.h"
      28             : 
      29             : #include "core/ActionRegister.h"
      30             : #include "core/ActionSet.h"
      31             : #include "core/PlumedMain.h"
      32             : #include "core/Value.h"
      33             : #include "tools/File.h"
      34             : #include "tools/Grid.h"
      35             : 
      36             : 
      37             : namespace PLMD {
      38             : namespace ves {
      39             : 
      40             : //+PLUMEDOC VES_UTILS VES_OUTPUT_BASISFUNCTIONS
      41             : /*
      42             : Output basis functions to file.
      43             : 
      44             : This action can be used to write out to a grid file the values and derivatives of
      45             : given basis functions. This is normally used for debugging when programming new
      46             : types of basis functions. For example, it is possible to calculate the
      47             : derivatives numerically and compare to the analytically calculated
      48             : derivatives.
      49             : 
      50             : This action is normally used through the [driver](driver.md).
      51             : 
      52             : ## Examples
      53             : 
      54             : In the following input we define a Legendre polynomials basis functions
      55             : of order 14 over the interval -4.0 to 4.0 and output their values
      56             : and derivatives to files called bfL.values.data and bfL.derivs.data.
      57             : 
      58             : ```plumed
      59             : BF_LEGENDRE ...
      60             :  ORDER=14
      61             :  MINIMUM=-4.0
      62             :  MAXIMUM=4.0
      63             :  LABEL=bfL
      64             : ... BF_LEGENDRE
      65             : 
      66             : VES_OUTPUT_BASISFUNCTIONS ...
      67             :  BASIS_FUNCTIONS=bfL
      68             :  GRID_BINS=200
      69             :  FORMAT_VALUES_DERIVS=%13.6f
      70             : ... VES_OUTPUT_BASISFUNCTIONS
      71             : ```
      72             : 
      73             : This input should be run through the driver by using a command similar to the
      74             : following one where the trajectory/configuration file configuration.gro is needed to
      75             : trick the code to exit correctly.
      76             : 
      77             : ```plumed
      78             : plumed driver --plumed plumed.dat --igro configuration.gro
      79             : ```
      80             : 
      81             : */
      82             : //+ENDPLUMEDOC
      83             : 
      84             : 
      85             : class OutputBasisFunctions :
      86             :   public Action {
      87             :   std::vector<BasisFunctions*> bf_pntrs;
      88             : public:
      89             :   explicit OutputBasisFunctions(const ActionOptions&);
      90             :   TargetDistribution* setupTargetDistPntr(std::string keyword) const;
      91           0 :   void calculate() override {}
      92           0 :   void apply() override {}
      93             :   static void registerKeywords(Keywords& keys);
      94             : };
      95             : 
      96             : 
      97             : PLUMED_REGISTER_ACTION(OutputBasisFunctions,"VES_OUTPUT_BASISFUNCTIONS")
      98             : 
      99          73 : void OutputBasisFunctions::registerKeywords(Keywords& keys) {
     100          73 :   Action::registerKeywords(keys);
     101          73 :   keys.add("compulsory","BASIS_FUNCTIONS","the label of the basis functions that you want to use");
     102          73 :   keys.add("optional","GRID_BINS","the number of bins used for the grid for writing the basis function values and derivatives. The default value is 1000.");
     103          73 :   keys.add("optional","GRID_MIN","the minimum of the grid for writing the basis function values and derivatives. By default it is the minimum of the interval on which the basis functions are defined.");
     104          73 :   keys.add("optional","GRID_MAX","the maximum of the grid for writing the basis function values and derivatives. By default it is the maximum of the interval on which the basis functions are defined.");
     105          73 :   keys.add("optional","FILE_VALUES","filename of the file on which the basis function values are written. By default it is BF_LABEL.values.data.");
     106          73 :   keys.add("optional","FILE_DERIVS","filename of the file on which the basis function derivatives are written. By default it is BF_LABEL.derivs.data.");
     107          73 :   keys.add("optional","FORMAT_VALUES_DERIVS","the numerical format of the basis function values and derivatives written to file. By default it is %15.8f.\n. You can also use FORMAT_VALUES and FORMAT_DERIVS to give the numerical formats separately.");
     108          73 :   keys.add("optional","FORMAT_VALUES","the numerical format of the basis function derivatives written to file. By default it is %15.8f.\n");
     109          73 :   keys.add("optional","FORMAT_DERIVS","the numerical format of the basis function values written to file. By default it is %15.8f.\n");
     110          73 :   keys.add("optional","FILE_TARGETDIST_AVERAGES","filename of the file on which the averages over the target distributions are written. By default it is BF_LABEL.targetdist-averages.data.");
     111          73 :   keys.add("optional","FORMAT_TARGETDIST_AVERAGES","the numerical format of the target distribution averages written to file. By default it is %15.8f.\n");
     112          73 :   keys.add("optional","FILE_TARGETDIST","filename of the files on which the target distributions are written. By default it is BF_LABEL.targetdist-#.data.");
     113          73 :   keys.add("numbered","TARGET_DISTRIBUTION","the target distribution to be used.");
     114          73 :   keys.addFlag("IGNORE_PERIODICITY",false,"if the periodicity of the basis functions should be ignored.");
     115          73 :   keys.addFlag("NUMERICAL_DERIVATIVES",false,"if the derivatives of the basis functions should be calculated numerically.");
     116          73 : }
     117             : 
     118          71 : OutputBasisFunctions::OutputBasisFunctions(const ActionOptions&ao):
     119             :   Action(ao),
     120          71 :   bf_pntrs(0) {
     121          71 :   std::vector<std::string> basisset_labels(0);
     122         142 :   parseVector("BASIS_FUNCTIONS",basisset_labels);
     123          71 :   if(basisset_labels.size()>1) {
     124           0 :     plumed_merror("Only one basis set label allowed in keyword BASIS_FUNCTIONS of "+getName());
     125             :   }
     126             : 
     127          71 :   std::string error_msg = "";
     128         142 :   bf_pntrs = VesTools::getPointersFromLabels<BasisFunctions*>(basisset_labels,plumed.getActionSet(),error_msg);
     129          71 :   if(error_msg.size()>0) {
     130           0 :     plumed_merror("Error in keyword BASIS_FUNCTIONS of "+getName()+": "+error_msg);
     131             :   }
     132             : 
     133          71 :   unsigned int nbins = 1000;
     134         142 :   parse("GRID_BINS",nbins);
     135             : 
     136          71 :   std::string min_str = bf_pntrs[0]->intervalMinStr();
     137          71 :   std::string max_str = bf_pntrs[0]->intervalMaxStr();
     138          71 :   parse("GRID_MIN",min_str);
     139         142 :   parse("GRID_MAX",max_str);
     140             : 
     141          71 :   std::string fname_values = bf_pntrs[0]->getLabel()+".values.data";
     142         142 :   parse("FILE_VALUES",fname_values);
     143          71 :   std::string fname_derives = bf_pntrs[0]->getLabel()+".derivs.data";
     144         142 :   parse("FILE_DERIVS",fname_derives);
     145          71 :   std::string fname_targetdist_aver = bf_pntrs[0]->getLabel()+".targetdist-averages.data";
     146         142 :   parse("FILE_TARGETDIST_AVERAGES",fname_targetdist_aver);
     147          71 :   std::string fname_targetdist = bf_pntrs[0]->getLabel()+".targetdist-.data";
     148          71 :   parse("FILE_TARGETDIST",fname_targetdist);
     149             : 
     150          71 :   std::string fmt_values_derivs = "%15.8f";
     151          71 :   parse("FORMAT_VALUES_DERIVS",fmt_values_derivs);
     152          71 :   std::string fmt_values = fmt_values_derivs;
     153          71 :   std::string fmt_derivs = fmt_values_derivs;
     154          71 :   parse("FORMAT_VALUES",fmt_values);
     155          71 :   parse("FORMAT_DERIVS",fmt_derivs);
     156             : 
     157          71 :   std::string fmt_targetdist_aver = "%15.8f";
     158          71 :   parse("FORMAT_TARGETDIST_AVERAGES",fmt_targetdist_aver);
     159             : 
     160          71 :   bool ignore_periodicity = false;
     161          71 :   parseFlag("IGNORE_PERIODICITY",ignore_periodicity);
     162             : 
     163          71 :   bool numerical_deriv = false;
     164         142 :   parseFlag("NUMERICAL_DERIVATIVES",numerical_deriv);
     165             : 
     166             :   std::vector<TargetDistribution*> targetdist_pntrs;
     167          71 :   targetdist_pntrs.push_back(NULL);
     168          71 :   std::string targetdist_label="";
     169         176 :   for(int i=1;; i++) {
     170         494 :     if(!parseNumbered("TARGET_DISTRIBUTION",i,targetdist_label)) {
     171             :       break;
     172             :     }
     173         176 :     std::string error_msg = "";
     174         176 :     TargetDistribution* pntr_tmp = VesTools::getPointerFromLabel<TargetDistribution*>(targetdist_label,plumed.getActionSet(),error_msg);
     175         176 :     if(error_msg.size()>0) {
     176           0 :       plumed_merror("Error in keyword TARGET_DISTRIBUTION of "+getName()+": "+error_msg);
     177             :     }
     178         176 :     targetdist_pntrs.push_back(pntr_tmp);
     179         176 :   }
     180          71 :   checkRead();
     181             :   //
     182          71 :   OFile ofile_values;
     183          71 :   ofile_values.link(*this);
     184          71 :   ofile_values.enforceBackup();
     185          71 :   ofile_values.open(fname_values);
     186          71 :   OFile ofile_derivs;
     187          71 :   ofile_derivs.link(*this);
     188          71 :   ofile_derivs.enforceBackup();
     189          71 :   ofile_derivs.open(fname_derives);
     190          71 :   bf_pntrs[0]->writeBasisFunctionsToFile(ofile_values,ofile_derivs,min_str,max_str,nbins,ignore_periodicity,fmt_values,fmt_derivs,numerical_deriv);
     191          71 :   ofile_values.close();
     192          71 :   ofile_derivs.close();
     193             :   //
     194          71 :   std::vector<std::string> grid_min(1);
     195          71 :   grid_min[0]=bf_pntrs[0]->intervalMinStr();
     196          71 :   std::vector<std::string> grid_max(1);
     197          71 :   grid_max[0]=bf_pntrs[0]->intervalMaxStr();
     198          71 :   std::vector<unsigned int> grid_bins(1);
     199          71 :   grid_bins[0]=nbins;
     200          71 :   std::vector<std::unique_ptr<Value>> arguments(1);
     201         142 :   arguments[0]= Tools::make_unique<Value>(nullptr,"arg",false);
     202          71 :   if(bf_pntrs[0]->arePeriodic() && !ignore_periodicity) {
     203          46 :     arguments[0]->setDomain(bf_pntrs[0]->intervalMinStr(),bf_pntrs[0]->intervalMaxStr());
     204             :   } else {
     205          48 :     arguments[0]->setNotPeriodic();
     206             :   }
     207             : 
     208          71 :   OFile ofile_targetdist_aver;
     209          71 :   ofile_targetdist_aver.link(*this);
     210          71 :   ofile_targetdist_aver.enforceBackup();
     211          71 :   ofile_targetdist_aver.open(fname_targetdist_aver);
     212             : 
     213         318 :   for(unsigned int i=0; i<targetdist_pntrs.size(); i++) {
     214             :     std::string is;
     215         247 :     Tools::convert(i,is);
     216             :     //
     217         247 :     if(targetdist_pntrs[i]!=NULL) {
     218         352 :       targetdist_pntrs[i]->setupGrids(Tools::unique2raw(arguments),grid_min,grid_max,grid_bins);
     219         176 :       plumed_massert(targetdist_pntrs[i]->getDimension()==1,"the target distribution must be one dimensional");
     220         176 :       targetdist_pntrs[i]->updateTargetDist();
     221             :     }
     222             :     //
     223         247 :     std::vector<double> bf_integrals = bf_pntrs[0]->getTargetDistributionIntegrals(targetdist_pntrs[i]);
     224         494 :     CoeffsVector targetdist_averages = CoeffsVector("aver.targetdist-"+is,Tools::unique2raw(arguments),bf_pntrs,comm,false);
     225         247 :     targetdist_averages.setValues(bf_integrals);
     226         247 :     if(fmt_targetdist_aver.size()>0) {
     227             :       targetdist_averages.setOutputFmt(fmt_targetdist_aver);
     228             :     }
     229         247 :     targetdist_averages.writeToFile(ofile_targetdist_aver,true);
     230         247 :     if(targetdist_pntrs[i]!=NULL) {
     231             :       Grid* targetdist_grid_pntr = targetdist_pntrs[i]->getTargetDistGridPntr();
     232         176 :       std::string fname = FileBase::appendSuffix(fname_targetdist,is);
     233         176 :       OFile ofile;
     234         176 :       ofile.link(*this);
     235         176 :       ofile.enforceBackup();
     236         176 :       ofile.open(fname);
     237         176 :       targetdist_grid_pntr->writeToFile(ofile);
     238         176 :       ofile.close();
     239         176 :     }
     240         247 :   }
     241          71 :   ofile_targetdist_aver.close();
     242             : 
     243             : 
     244         213 : }
     245             : 
     246             : 
     247             : 
     248             : }
     249             : }

Generated by: LCOV version 1.16