LCOV - code coverage report
Current view: top level - generic - DumpForces.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 37 39 94.9 %
Date: 2026-03-30 11:13:23 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/ActionWithArguments.h"
      24             : #include "core/ActionRegister.h"
      25             : #include "tools/File.h"
      26             : 
      27             : namespace PLMD {
      28             : namespace generic {
      29             : 
      30             : //+PLUMEDOC PRINTANALYSIS DUMPFORCES
      31             : /*
      32             : Dump the force acting on one of a values in a file.
      33             : 
      34             : For a CV this command will dump
      35             : the force on the CV itself. Be aware that in order to have the forces on the atoms
      36             : you should multiply the output from this argument by the output from DUMPDERIVATIVES.
      37             : Furthermore, also note that you can output the forces on multiple quantities simultaneously
      38             : by specifying more than one argument. You can control the buffering of output using the \ref FLUSH keyword.
      39             : 
      40             : 
      41             : \par Examples
      42             : 
      43             : The following input instructs plumed to write a file called forces that contains
      44             : the force acting on the distance between atoms 1 and 2.
      45             : \plumedfile
      46             : DISTANCE ATOMS=1,2 LABEL=distance
      47             : DUMPFORCES ARG=distance STRIDE=1 FILE=forces
      48             : \endplumedfile
      49             : 
      50             : */
      51             : //+ENDPLUMEDOC
      52             : 
      53             : class DumpForces :
      54             :   public ActionPilot,
      55             :   public ActionWithArguments {
      56             :   std::string file;
      57             :   std::string fmt;
      58             :   OFile of;
      59             : public:
      60         412 :   void calculate() override {}
      61             :   explicit DumpForces(const ActionOptions&);
      62             :   static void registerKeywords(Keywords& keys);
      63         412 :   void apply() override {}
      64             :   void update() override;
      65             :   ~DumpForces();
      66             : };
      67             : 
      68             : PLUMED_REGISTER_ACTION(DumpForces,"DUMPFORCES")
      69             : 
      70          34 : void DumpForces::registerKeywords(Keywords& keys) {
      71          34 :   Action::registerKeywords(keys);
      72          34 :   ActionPilot::registerKeywords(keys);
      73          34 :   ActionWithArguments::registerKeywords(keys);
      74          34 :   keys.use("ARG");
      75          68 :   keys.add("compulsory","STRIDE","1","the frequency with which the forces should be output");
      76          68 :   keys.add("compulsory","FILE","the name of the file on which to output the forces");
      77          68 :   keys.add("compulsory","FMT","%15.10f","the format with which the derivatives should be output");
      78          34 :   keys.use("RESTART");
      79          34 :   keys.use("UPDATE_FROM");
      80          34 :   keys.use("UPDATE_UNTIL");
      81          34 : }
      82             : 
      83          30 : DumpForces::DumpForces(const ActionOptions&ao):
      84             :   Action(ao),
      85             :   ActionPilot(ao),
      86             :   ActionWithArguments(ao),
      87          30 :   fmt("%15.10f") {
      88          60 :   parse("FILE",file);
      89          30 :   if( file.length()==0 ) {
      90           0 :     error("name of file was not specified");
      91             :   }
      92          30 :   parse("FMT",fmt);
      93          30 :   fmt=" "+fmt;
      94          30 :   of.link(*this);
      95          30 :   of.open(file);
      96          30 :   log.printf("  on file %s\n",file.c_str());
      97          30 :   log.printf("  with format %s\n",fmt.c_str());
      98          30 :   if( getNumberOfArguments()==0 ) {
      99           0 :     error("no arguments have been specified");
     100             :   }
     101          30 :   checkRead();
     102          30 : }
     103             : 
     104             : 
     105         412 : void DumpForces::update() {
     106         412 :   of.fmtField(" %f");
     107         412 :   of.printField("time",getTime());
     108        5100 :   for(unsigned i=0; i<getNumberOfArguments(); i++) {
     109        4688 :     of.fmtField(fmt);
     110        4688 :     of.printField(getPntrToArgument(i)->getName(),getPntrToArgument(i)->getForce());
     111             :   }
     112         412 :   of.printField();
     113         412 : }
     114             : 
     115          60 : DumpForces::~DumpForces() {
     116          60 : }
     117             : 
     118             : }
     119             : 
     120             : 
     121             : }

Generated by: LCOV version 1.16