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-11-05 10:19:04 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             : Consider the following PLUMED input:
      35             : 
      36             : ```plumed
      37             : r: DISTANCE ATOMS=1,2
      38             : V: RESTRAINT ARG=r AT=0.2 KAPPA=100
      39             : ```
      40             : 
      41             : This input will ultimately apply forces on the $x$, $y$ and $z$ components of two
      42             : atoms as well as the 9 cell vectors.  The force on the $i$th of these 15 quantities
      43             : is given by:
      44             : 
      45             : $$
      46             : F_i = - \frac{\textrm{d}V}{\textrm{d}r}\frac{\partial r}{\partial x_i}
      47             : $$
      48             : 
      49             : where $x_i$ is the $x$, $y$ or $z$ component of the position of one of the two atoms or one of the cell
      50             : vectors.  If we modify the input above by adding the DUMPFORCES command as shown below:
      51             : 
      52             : ```plumed
      53             : r: DISTANCE ATOMS=1,2
      54             : V: RESTRAINT ARG=r AT=0.2 KAPPA=100
      55             : DUMPFORCES ARG=r FILE=forces STRIDE=5 FMT=%10.5f
      56             : ```
      57             : 
      58             : we can then monitor the value of $-\frac{\textrm{d}V}{\textrm{d}r}$ in the output file
      59             : `forces`.  As explained by the equation above to get the forces on the atom this 'input force' needs to
      60             : be multiplied by $\frac{\partial r}{\partial x_i}$.  To view the various components of the $\frac{\partial r}{\partial x_i}$
      61             : you would use the [DUMPDERIVATIVES](DUMPDERIVATIVES.md) command.  To control the buffering of output you use the
      62             : [FLUSH](FLUSH.md) command.
      63             : 
      64             : ## DUMPFORCES and RESTART
      65             : 
      66             : If you run a calculation with the following input:
      67             : 
      68             : ```plumed
      69             : r: DISTANCE ATOMS=1,2
      70             : V: RESTRAINT ARG=r AT=0.2 KAPPA=100
      71             : DUMPFORCES ARG=r FILE=forces
      72             : ```
      73             : 
      74             : and a file called forces is already present in the directory where the calculation is running, the existing file is backed up
      75             : and renamed to `bck.0.forces` so that new data can be output to a new file called `forces`.  If you would like to append to the
      76             : existing file you can use the RESTART command as shown below:
      77             : 
      78             : ```plumed
      79             : r: DISTANCE ATOMS=1,2
      80             : V: RESTRAINT ARG=r AT=0.2 KAPPA=100
      81             : DUMPFORCES ARG=r FILE=forces RESTART=YES
      82             : ```
      83             : 
      84             : You can achieve the same result by using the [RESTART](RESTART.md) action as shown below:
      85             : 
      86             : ```plumed
      87             : RESTART
      88             : r: DISTANCE ATOMS=1,2
      89             : V: RESTRAINT ARG=r AT=0.2 KAPPA=100
      90             : DUMPFORCES ARG=r FILE=forces
      91             : ```
      92             : 
      93             : However, the advantage of using the RESTART keyword is that you can apped to some files and back up others.
      94             : If you use the [RESTART](RESTART.md) action instead data will be appended to all output files.
      95             : 
      96             : ## Switching printing on and off
      97             : 
      98             : You can use the UPDATE_FROM and UPDATE_UNTIL flags to make the DUMPFORCES command only output data at certain points during the trajectory.
      99             : To see how this works consider the following example:
     100             : 
     101             : ```plumed
     102             : r: DISTANCE ATOMS=1,2
     103             : V: RESTRAINT ARG=r AT=0.2 KAPPA=100
     104             : DUMPFORCES ...
     105             :   ARG=r FILE=forces
     106             :   UPDATE_FROM=100 UPDATE_UNTIL=500
     107             :   STRIDE=1
     108             : ...
     109             : ```
     110             : 
     111             : During the first 100 ps of the simulation with this input the force is not output to the file called forces.
     112             : The force is instead first output after the first 100 ps of trajectory have elapsed.  Furthermore, output of the force stops
     113             : once the trajectory is longer than 500 ps. In other words, the force is only output during the 400 ps time interval after the first
     114             : 100 ps of the simulation.
     115             : 
     116             : */
     117             : //+ENDPLUMEDOC
     118             : 
     119             : class DumpForces :
     120             :   public ActionPilot,
     121             :   public ActionWithArguments {
     122             :   std::string file;
     123             :   std::string fmt;
     124             :   OFile of;
     125             : public:
     126        4755 :   void calculate() override {}
     127             :   explicit DumpForces(const ActionOptions&);
     128             :   static void registerKeywords(Keywords& keys);
     129        4755 :   void apply() override {}
     130             :   void update() override;
     131             :   ~DumpForces();
     132             : };
     133             : 
     134             : PLUMED_REGISTER_ACTION(DumpForces,"DUMPFORCES")
     135             : 
     136          55 : void DumpForces::registerKeywords(Keywords& keys) {
     137          55 :   Action::registerKeywords(keys);
     138          55 :   ActionPilot::registerKeywords(keys);
     139          55 :   ActionWithArguments::registerKeywords(keys);
     140         110 :   keys.addInputKeyword("compulsory","ARG","scalar/vector/matrix/grid","the labels of the values whose forces should be output");
     141          55 :   keys.add("compulsory","STRIDE","1","the frequency with which the forces should be output");
     142          55 :   keys.add("compulsory","FILE","the name of the file on which to output the forces");
     143          55 :   keys.add("compulsory","FMT","%15.10f","the format with which the derivatives should be output");
     144          55 :   keys.use("RESTART");
     145          55 :   keys.use("UPDATE_FROM");
     146          55 :   keys.use("UPDATE_UNTIL");
     147          55 : }
     148             : 
     149          51 : DumpForces::DumpForces(const ActionOptions&ao):
     150             :   Action(ao),
     151             :   ActionPilot(ao),
     152             :   ActionWithArguments(ao),
     153          51 :   fmt("%15.10f") {
     154         102 :   parse("FILE",file);
     155          51 :   if( file.length()==0 ) {
     156           0 :     error("name of file was not specified");
     157             :   }
     158          51 :   parse("FMT",fmt);
     159          51 :   fmt=" "+fmt;
     160          51 :   of.link(*this);
     161          51 :   of.open(file);
     162          51 :   log.printf("  on file %s\n",file.c_str());
     163          51 :   log.printf("  with format %s\n",fmt.c_str());
     164          51 :   if( getNumberOfArguments()==0 ) {
     165           0 :     error("no arguments have been specified");
     166             :   }
     167          51 :   checkRead();
     168          51 : }
     169             : 
     170             : 
     171        3085 : void DumpForces::update() {
     172        3085 :   of.fmtField(" %f");
     173        3085 :   of.printField("time",getTime());
     174       13539 :   for(unsigned i=0; i<getNumberOfArguments(); i++) {
     175       10454 :     of.fmtField(fmt);
     176       10454 :     getPntrToArgument(i)->printForce(of);
     177             :   }
     178        3085 :   of.printField();
     179        3085 : }
     180             : 
     181         102 : DumpForces::~DumpForces() {
     182         102 : }
     183             : 
     184             : }
     185             : 
     186             : 
     187             : }

Generated by: LCOV version 1.16