LCOV - code coverage report
Current view: top level - generic - DumpDerivatives.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 48 48 100.0 %
Date: 2024-10-18 13:59:31 Functions: 7 9 77.8 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2011-2023 The plumed team
       3             :    (see the PEOPLE file at the root of the distribution for a list of names)
       4             : 
       5             :    See http://www.plumed.org for more information.
       6             : 
       7             :    This file is part of plumed, version 2.
       8             : 
       9             :    plumed 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             :    plumed 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 plumed.  If not, see <http://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : #include "core/ActionPilot.h"
      23             : #include "core/ActionWithValue.h"
      24             : #include "core/ActionWithArguments.h"
      25             : #include "core/ActionRegister.h"
      26             : #include "tools/File.h"
      27             : 
      28             : namespace PLMD {
      29             : namespace generic {
      30             : 
      31             : //+PLUMEDOC PRINTANALYSIS DUMPDERIVATIVES
      32             : /*
      33             : Dump the derivatives with respect to the input parameters for one or more objects (generally CVs, functions or biases).
      34             : 
      35             : For a CV this line in input instructs plumed to print the derivative of the CV with respect to the atom positions
      36             : and the cell vectors (virial-like form).  In contrast, for a function or bias the derivative with respect to the input "CVs"
      37             : will be output.  This command is most often used to test whether or not analytic derivatives have been implemented correctly.  This
      38             : can be done by outputting the derivatives calculated analytically and numerically.  You can control the buffering of output using the \ref FLUSH keyword.
      39             : 
      40             : \par Examples
      41             : 
      42             : The following input instructs plumed to write a file called deriv that contains both the
      43             : analytical and numerical derivatives of the distance between atoms 1 and 2.
      44             : \plumedfile
      45             : DISTANCE ATOMS=1,2 LABEL=distance
      46             : DISTANCE ATOMS=1,2 LABEL=distanceN NUMERICAL_DERIVATIVES
      47             : DUMPDERIVATIVES ARG=distance,distanceN STRIDE=1 FILE=deriv
      48             : \endplumedfile
      49             : 
      50             : (See also \ref DISTANCE)
      51             : 
      52             : */
      53             : //+ENDPLUMEDOC
      54             : 
      55             : class DumpDerivatives :
      56             :   public ActionPilot,
      57             :   public ActionWithArguments
      58             : {
      59             :   std::string file;
      60             :   std::string fmt;
      61             :   OFile of;
      62             : public:
      63       12339 :   void calculate() override {}
      64             :   explicit DumpDerivatives(const ActionOptions&);
      65             :   static void registerKeywords(Keywords& keys);
      66       12297 :   void apply() override {}
      67             :   void update() override;
      68             :   ~DumpDerivatives();
      69             : };
      70             : 
      71             : PLUMED_REGISTER_ACTION(DumpDerivatives,"DUMPDERIVATIVES")
      72             : 
      73         255 : void DumpDerivatives::registerKeywords(Keywords& keys) {
      74         255 :   Action::registerKeywords(keys);
      75         255 :   ActionPilot::registerKeywords(keys);
      76         255 :   ActionWithArguments::registerKeywords(keys);
      77         510 :   keys.addInputKeyword("compulsory","ARG","scalar","the labels of the values whose derivatives should be output");
      78         510 :   keys.add("compulsory","STRIDE","1","the frequency with which the derivatives should be output");
      79         510 :   keys.add("compulsory","FILE","the name of the file on which to output the derivatives");
      80         510 :   keys.add("compulsory","FMT","%15.10f","the format with which the derivatives should be output");
      81         255 :   keys.use("RESTART");
      82         255 :   keys.use("UPDATE_FROM");
      83         255 :   keys.use("UPDATE_UNTIL");
      84         255 : }
      85             : 
      86         253 : DumpDerivatives::DumpDerivatives(const ActionOptions&ao):
      87             :   Action(ao),
      88             :   ActionPilot(ao),
      89             :   ActionWithArguments(ao),
      90         253 :   fmt("%15.10f")
      91             : {
      92         506 :   parse("FILE",file);
      93         253 :   if( file.length()==0 ) error("name of output file was not specified");
      94         253 :   parse("FMT",fmt);
      95         253 :   fmt=" "+fmt;
      96         253 :   of.link(*this);
      97         253 :   of.open(file);
      98         253 :   log.printf("  on file %s\n",file.c_str());
      99         253 :   log.printf("  with format %s\n",fmt.c_str());
     100             :   unsigned nargs=getNumberOfArguments();
     101         253 :   if( nargs==0 ) error("no arguments specified");
     102         253 :   (getPntrToArgument(0)->getPntrToAction())->turnOnDerivatives();
     103         253 :   if( getPntrToArgument(0)->getRank()>0 ) error("cannot dump derivatives of non-scalar objects");
     104         253 :   unsigned npar=getPntrToArgument(0)->getNumberOfDerivatives();
     105         253 :   if( npar==0 ) error("one or more arguments has no derivatives");
     106         843 :   for(unsigned i=1; i<nargs; i++) {
     107         590 :     (getPntrToArgument(i)->getPntrToAction())->turnOnDerivatives();
     108         590 :     if( getPntrToArgument(i)->getRank()>0 ) error("cannot dump derivatives of non-scalar objects");
     109         590 :     if( npar!=getPntrToArgument(i)->getNumberOfDerivatives() ) error("the number of derivatives must be the same in all values being dumped");
     110             :   }
     111         253 :   checkRead();
     112         253 : }
     113             : 
     114             : 
     115        9112 : void DumpDerivatives::update() {
     116        9112 :   unsigned npar=getPntrToArgument(0)->getNumberOfDerivatives();
     117      698622 :   for(unsigned ipar=0; ipar<npar; ipar++) {
     118      689510 :     of.fmtField(" %f");
     119      689510 :     of.printField("time",getTime());
     120      689510 :     of.printField("parameter",(int)ipar);
     121     3452310 :     for(unsigned i=0; i<getNumberOfArguments(); i++) {
     122     2762800 :       of.fmtField(fmt);
     123     2762800 :       of.printField(getPntrToArgument(i)->getName(),getPntrToArgument(i)->getDerivative(ipar) );
     124             :     }
     125      689510 :     of.printField();
     126             :   }
     127        9112 : }
     128             : 
     129         506 : DumpDerivatives::~DumpDerivatives() {
     130         506 : }
     131             : 
     132             : }
     133             : 
     134             : 
     135             : }

Generated by: LCOV version 1.16