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

Generated by: LCOV version 1.16