LCOV - code coverage report
Current view: top level - generic - DumpForces.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 39 42 92.9 %
Date: 2025-04-08 21:11:17 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
      56             : ```
      57             : 
      58             : we can then monitor the value of $-\frac{\textrm{d}V}{\textrm{d}r}\frac{\partial 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             : */
      65             : //+ENDPLUMEDOC
      66             : 
      67             : class DumpForces :
      68             :   public ActionPilot,
      69             :   public ActionWithArguments {
      70             :   std::string file;
      71             :   std::string fmt;
      72             :   OFile of;
      73             : public:
      74         412 :   void calculate() override {}
      75             :   explicit DumpForces(const ActionOptions&);
      76             :   static void registerKeywords(Keywords& keys);
      77         412 :   void apply() override {}
      78             :   void update() override;
      79             :   ~DumpForces();
      80             : };
      81             : 
      82             : PLUMED_REGISTER_ACTION(DumpForces,"DUMPFORCES")
      83             : 
      84          32 : void DumpForces::registerKeywords(Keywords& keys) {
      85          32 :   Action::registerKeywords(keys);
      86          32 :   ActionPilot::registerKeywords(keys);
      87          32 :   ActionWithArguments::registerKeywords(keys);
      88          64 :   keys.addInputKeyword("compulsory","ARG",Keywords::argType::scalar,"the labels of the values whose forces should be output");
      89          32 :   keys.add("compulsory","STRIDE","1","the frequency with which the forces should be output");
      90          32 :   keys.add("compulsory","FILE","the name of the file on which to output the forces");
      91          32 :   keys.add("compulsory","FMT","%15.10f","the format with which the derivatives should be output");
      92          32 :   keys.use("RESTART");
      93          32 :   keys.use("UPDATE_FROM");
      94          32 :   keys.use("UPDATE_UNTIL");
      95          32 : }
      96             : 
      97          30 : DumpForces::DumpForces(const ActionOptions&ao):
      98             :   Action(ao),
      99             :   ActionPilot(ao),
     100             :   ActionWithArguments(ao),
     101          30 :   fmt("%15.10f") {
     102          60 :   parse("FILE",file);
     103          30 :   if( file.length()==0 ) {
     104           0 :     error("name of file was not specified");
     105             :   }
     106          30 :   parse("FMT",fmt);
     107          30 :   fmt=" "+fmt;
     108          30 :   of.link(*this);
     109          30 :   of.open(file);
     110          30 :   log.printf("  on file %s\n",file.c_str());
     111          30 :   log.printf("  with format %s\n",fmt.c_str());
     112          30 :   if( getNumberOfArguments()==0 ) {
     113           0 :     error("no arguments have been specified");
     114             :   }
     115        3087 :   for(unsigned i=0; i<getNumberOfArguments(); ++i) {
     116        3057 :     if( getPntrToArgument(i)->getRank()>0 ) {
     117           0 :       error("can only use DUMPFORCES to output forces on rank zero objects");
     118             :     }
     119             :   }
     120          30 :   checkRead();
     121          30 : }
     122             : 
     123             : 
     124         412 : void DumpForces::update() {
     125         412 :   of.fmtField(" %f");
     126         412 :   of.printField("time",getTime());
     127        5100 :   for(unsigned i=0; i<getNumberOfArguments(); i++) {
     128        4688 :     of.fmtField(fmt);
     129        4688 :     of.printField(getPntrToArgument(i)->getName(),getPntrToArgument(i)->getForce());
     130             :   }
     131         412 :   of.printField();
     132         412 : }
     133             : 
     134          60 : DumpForces::~DumpForces() {
     135          60 : }
     136             : 
     137             : }
     138             : 
     139             : 
     140             : }

Generated by: LCOV version 1.16