LCOV - code coverage report
Current view: top level - generic - Print.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 58 60 96.7 %
Date: 2026-03-30 13:16:06 Functions: 11 13 84.6 %

          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             : 
      26             : namespace PLMD {
      27             : namespace generic {
      28             : 
      29             : //+PLUMEDOC PRINTANALYSIS PRINT
      30             : /*
      31             : Print quantities to a file.
      32             : 
      33             : This directive can be used multiple times
      34             : in the input so you can print files with different strides or print different quantities
      35             : to different files.  You can control the buffering of output using the \subpage FLUSH keyword.
      36             : Output file is either appended or backed up depending on the presence of the \ref RESTART action.
      37             : A per-action `RESTART` keyword can be used as well.
      38             : 
      39             : Notice that printing happens in the so-called "update" phase. This implies that printing
      40             : is affected by the presence of \ref UPDATE_IF actions. In addition, one might decide to start
      41             : and stop printing at preassigned values of time using the `UPDATE_FROM` and `UPDATE_UNTIL` keywords.
      42             : Keep into account that even on steps when the action is not updated (and thus the file is not printed)
      43             : the argument will be activated. In other words, if you use `UPDATE_FROM` to start printing at a given time,
      44             : the collective variables this PRINT statement depends on will be computed also before that time.
      45             : 
      46             : \par Examples
      47             : 
      48             : The following input instructs plumed to print the distance between atoms 3 and 5 on a file
      49             : called COLVAR every 10 steps, and the distance and total energy on a file called COLVAR_ALL
      50             : every 1000 steps.
      51             : \plumedfile
      52             : # compute distance:
      53             : distance: DISTANCE ATOMS=2,5
      54             : # compute total energy (potential)
      55             : energy: ENERGY
      56             : # print distance on a file
      57             : PRINT ARG=distance          STRIDE=10   FILE=COLVAR
      58             : # print both variables on another file
      59             : PRINT ARG=distance,energy   STRIDE=1000 FILE=COLVAR_ALL
      60             : \endplumedfile
      61             : 
      62             : Notice that \ref DISTANCE and \ref ENERGY are computed respectively every 10 and 1000 steps, that is
      63             : only when required.
      64             : 
      65             : */
      66             : //+ENDPLUMEDOC
      67             : 
      68             : class Print :
      69             :   public ActionPilot,
      70             :   public ActionWithArguments {
      71             :   std::string file;
      72             :   OFile ofile;
      73             :   std::string fmt;
      74             : // small internal utility
      75             : /////////////////////////////////////////
      76             : // these are crazy things just for debug:
      77             : // they allow to change regularly the
      78             : // printed argument
      79             :   int rotate;
      80             :   int rotateCountdown;
      81             :   int rotateLast;
      82             :   std::vector<Value*> rotateArguments;
      83             : /////////////////////////////////////////
      84             : public:
      85      214454 :   void calculate() override {}
      86             :   void prepare() override;
      87             :   explicit Print(const ActionOptions&);
      88             :   static void registerKeywords(Keywords& keys);
      89      214332 :   void apply() override {}
      90             :   void update() override;
      91             :   ~Print();
      92             : };
      93             : 
      94       15497 : PLUMED_REGISTER_ACTION(Print,"PRINT")
      95             : 
      96         860 : void Print::registerKeywords(Keywords& keys) {
      97         860 :   Action::registerKeywords(keys);
      98         860 :   ActionPilot::registerKeywords(keys);
      99         860 :   ActionWithArguments::registerKeywords(keys);
     100         860 :   keys.use("ARG");
     101        1720 :   keys.add("compulsory","STRIDE","1","the frequency with which the quantities of interest should be output");
     102        1720 :   keys.add("optional","FILE","the name of the file on which to output these quantities");
     103        1720 :   keys.add("optional","FMT","the format that should be used to output real numbers");
     104        1720 :   keys.add("hidden","_ROTATE","some funky thing implemented by GBussi");
     105         860 :   keys.use("RESTART");
     106         860 :   keys.use("UPDATE_FROM");
     107         860 :   keys.use("UPDATE_UNTIL");
     108         860 : }
     109             : 
     110         856 : Print::Print(const ActionOptions&ao):
     111             :   Action(ao),
     112             :   ActionPilot(ao),
     113             :   ActionWithArguments(ao),
     114         856 :   fmt("%f"),
     115        1712 :   rotate(0) {
     116         856 :   ofile.link(*this);
     117        1712 :   parse("FILE",file);
     118         856 :   if(file.length()>0) {
     119         856 :     ofile.open(file);
     120         856 :     log.printf("  on file %s\n",file.c_str());
     121             :   } else {
     122           0 :     log.printf("  on plumed log file\n");
     123           0 :     ofile.link(log);
     124             :   }
     125         856 :   parse("FMT",fmt);
     126         856 :   fmt=" "+fmt;
     127         856 :   log.printf("  with format %s\n",fmt.c_str());
     128        6493 :   for(unsigned i=0; i<getNumberOfArguments(); ++i) {
     129        5637 :     ofile.setupPrintValue( getPntrToArgument(i) );
     130             :   }
     131             : /////////////////////////////////////////
     132             : // these are crazy things just for debug:
     133             : // they allow to change regularly the
     134             : // printed argument
     135         856 :   parse("_ROTATE",rotate);
     136         856 :   if(rotate>0) {
     137           1 :     rotateCountdown=rotate;
     138           4 :     for(unsigned i=0; i<getNumberOfArguments(); ++i) {
     139           3 :       rotateArguments.push_back( getPntrToArgument(i) );
     140             :     }
     141           1 :     std::vector<Value*> a(1,rotateArguments[0]);
     142           1 :     requestArguments(std::vector<Value*>(1,rotateArguments[0]));
     143           1 :     rotateLast=0;
     144             :   }
     145             : /////////////////////////////////////////
     146         856 :   checkRead();
     147         856 : }
     148             : 
     149      214577 : void Print::prepare() {
     150             : /////////////////////////////////////////
     151             : // these are crazy things just for debug:
     152             : // they allow to change regularly the
     153             : // printed argument
     154      214577 :   if(rotate>0) {
     155           5 :     rotateCountdown--;
     156           5 :     if(rotateCountdown==0) {
     157           2 :       rotateCountdown=rotate;
     158           2 :       rotateLast++;
     159           2 :       rotateLast%=rotateArguments.size();
     160           4 :       requestArguments(std::vector<Value*>(1,rotateArguments[rotateLast]));
     161             :     }
     162             :   }
     163             : /////////////////////////////////////////
     164      214577 : }
     165             : 
     166      213709 : void Print::update() {
     167      213709 :   ofile.fmtField(" %f");
     168      213709 :   ofile.printField("time",getTime());
     169      751910 :   for(unsigned i=0; i<getNumberOfArguments(); i++) {
     170      538201 :     ofile.fmtField(fmt);
     171      538201 :     ofile.printField( getPntrToArgument(i), getArgument(i) );
     172             :   }
     173      213709 :   ofile.printField();
     174      213709 : }
     175             : 
     176        1712 : Print::~Print() {
     177        1712 : }
     178             : 
     179             : }
     180             : 
     181             : 
     182             : }

Generated by: LCOV version 1.16