LCOV - code coverage report
Current view: top level - generic - DumpForces.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 38 38 100.0 %
Date: 2024-10-11 08:09:47 Functions: 10 12 83.3 %

          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             : {
      57             :   std::string file;
      58             :   std::string fmt;
      59             :   OFile of;
      60             : public:
      61         400 :   void calculate() override {}
      62             :   explicit DumpForces(const ActionOptions&);
      63             :   static void registerKeywords(Keywords& keys);
      64         400 :   void apply() override {}
      65             :   void update() override;
      66             :   ~DumpForces();
      67             : };
      68             : 
      69       10475 : PLUMED_REGISTER_ACTION(DumpForces,"DUMPFORCES")
      70             : 
      71          29 : void DumpForces::registerKeywords(Keywords& keys) {
      72          29 :   Action::registerKeywords(keys);
      73          29 :   ActionPilot::registerKeywords(keys);
      74          29 :   ActionWithArguments::registerKeywords(keys);
      75          29 :   keys.use("ARG");
      76          58 :   keys.add("compulsory","STRIDE","1","the frequency with which the forces should be output");
      77          58 :   keys.add("compulsory","FILE","the name of the file on which to output the forces");
      78          58 :   keys.add("compulsory","FMT","%15.10f","the format with which the derivatives should be output");
      79          29 :   keys.use("RESTART");
      80          29 :   keys.use("UPDATE_FROM");
      81          29 :   keys.use("UPDATE_UNTIL");
      82          29 : }
      83             : 
      84          28 : DumpForces::DumpForces(const ActionOptions&ao):
      85             :   Action(ao),
      86             :   ActionPilot(ao),
      87             :   ActionWithArguments(ao),
      88          28 :   fmt("%15.10f")
      89             : {
      90          56 :   parse("FILE",file);
      91          28 :   if( file.length()==0 ) error("name of file was not specified");
      92          28 :   parse("FMT",fmt);
      93          28 :   fmt=" "+fmt;
      94          28 :   of.link(*this);
      95          28 :   of.open(file);
      96          28 :   log.printf("  on file %s\n",file.c_str());
      97          28 :   log.printf("  with format %s\n",fmt.c_str());
      98          28 :   if( getNumberOfArguments()==0 ) error("no arguments have been specified");
      99          28 :   checkRead();
     100          28 : }
     101             : 
     102             : 
     103         400 : void DumpForces::update() {
     104         400 :   of.fmtField(" %f");
     105         400 :   of.printField("time",getTime());
     106        4992 :   for(unsigned i=0; i<getNumberOfArguments(); i++) {
     107        4592 :     of.fmtField(fmt);
     108        4592 :     of.printField(getPntrToArgument(i)->getName(),getPntrToArgument(i)->getForce());
     109             :   }
     110         400 :   of.printField();
     111         400 : }
     112             : 
     113          56 : DumpForces::~DumpForces() {
     114          84 : }
     115             : 
     116             : }
     117             : 
     118             : 
     119             : }

Generated by: LCOV version 1.15