LCOV - code coverage report
Current view: top level - generic - Debug.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 63 65 96.9 %
Date: 2026-11-05 10:19:04 Functions: 4 5 80.0 %

          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/ActionRegister.h"
      23             : #include "core/ActionPilot.h"
      24             : #include "core/ActionSet.h"
      25             : #include "core/PlumedMain.h"
      26             : 
      27             : namespace PLMD {
      28             : namespace generic {
      29             : 
      30             : //+PLUMEDOC GENERIC DEBUG
      31             : /*
      32             : Set some debug options.
      33             : 
      34             : This action can be used while debugging or optimizing plumed. The example
      35             : input below demonstates two useful ways you can use the DEBUG command
      36             : 
      37             : ```plumed
      38             : # print detailed (action-by-action) timers at the end of simulation
      39             : a: DEBUG DETAILED_TIMERS
      40             : # dump every two steps which are the atoms required from the MD code
      41             : b: DEBUG logRequestedAtoms STRIDE=2
      42             : ```
      43             : 
      44             : 
      45             : For diagnosing non-finite values in an action graph, set the environment
      46             : variable `PLUMED_NONFINITE_ACTION_TRACE` before starting the program. It
      47             : accepts comma- or space-separated action labels, or `*` for all labels.
      48             : The selection is read once per process and is disabled when unset. This
      49             : option is independent of the DEBUG action.
      50             : 
      51             : Selected actions are checked after their forward calculation for non-finite
      52             : values and scalar derivatives. During force propagation, selected action
      53             : forces and the atomic-position and cell force buffers are also checked.
      54             : A failure reports the step, forward/backward phase, triggering action,
      55             : target component and element. The diagnostic can add substantial overhead.
      56             : All spatial MPI ranks must use the same selection and action graph. The first failing rank is reported to every rank
      57             : before an exception is raised. Separate multi-simulation communicators are
      58             : not synchronized by this diagnostic; the MD code must terminate the other
      59             : replicas if one replica raises an exception.
      60             : It does not check all internal action state or grid-derivative layouts, and
      61             : cannot detect errors introduced later by the molecular dynamics engine.
      62             : A finite result is not evidence of a valid physical simulation.
      63             : 
      64             : */
      65             : //+ENDPLUMEDOC
      66             : class Debug:
      67             :   public ActionPilot {
      68             :   OFile ofile;
      69             :   bool logActivity;
      70             :   bool logRequestedAtoms;
      71             :   bool novirial;
      72             :   bool detailedTimers;
      73             : public:
      74             :   explicit Debug(const ActionOptions&ao);
      75             : /// Register all the relevant keywords for the action
      76             :   static void registerKeywords( Keywords& keys );
      77         221 :   void calculate() override {}
      78             :   void apply() override;
      79             : };
      80             : 
      81             : PLUMED_REGISTER_ACTION(Debug,"DEBUG")
      82             : 
      83          13 : void Debug::registerKeywords( Keywords& keys ) {
      84          13 :   Action::registerKeywords( keys );
      85          13 :   ActionPilot::registerKeywords(keys);
      86          13 :   keys.add("compulsory","STRIDE","1","the frequency with which this action is to be performed");
      87          13 :   keys.addFlag("LOGACTIVITY",false,"write in the log which actions are inactive and which are inactive,"
      88             :                " in PLUMED<2.6 and this flag can be activated only as 'logActivity'");
      89          13 :   keys.addFlag("LOGREQUESTEDATOMS",false,"write in the log which atoms have been requested at a given time,"
      90             :                " in PLUMED<2.6 and this flag can be activated only as 'logRequestedAtoms'");
      91          13 :   keys.addFlag("NOVIRIAL",false,"switch off the virial contribution for the entirety of the simulation");
      92          13 :   keys.addFlag("DETAILED_TIMERS",false,"switch on detailed timers");
      93          13 :   keys.add("optional","FILE","the name of the file on which to output these quantities");
      94          13 : }
      95             : 
      96           9 : Debug::Debug(const ActionOptions&ao):
      97             :   Action(ao),
      98             :   ActionPilot(ao),
      99           9 :   logActivity(false),
     100           9 :   logRequestedAtoms(false),
     101           9 :   novirial(false) {
     102           9 :   parseFlag("LOGACTIVITY",logActivity);
     103           9 :   if(logActivity) {
     104           2 :     log.printf("  logging activity\n");
     105             :   }
     106           9 :   parseFlag("LOGREQUESTEDATOMS",logRequestedAtoms);
     107           9 :   if(logRequestedAtoms) {
     108           2 :     log.printf("  logging requested atoms\n");
     109             :   }
     110           9 :   parseFlag("NOVIRIAL",novirial);
     111           9 :   if(novirial) {
     112           0 :     log.printf("  Switching off virial contribution\n");
     113             :   }
     114           9 :   if(novirial) {
     115           0 :     plumed.novirial=true;
     116             :   }
     117           9 :   parseFlag("DETAILED_TIMERS",detailedTimers);
     118           9 :   if(detailedTimers) {
     119           5 :     log.printf("  Detailed timing on\n");
     120           5 :     plumed.detailedTimers=true;
     121             :   }
     122           9 :   ofile.link(*this);
     123             :   std::string file;
     124          18 :   parse("FILE",file);
     125           9 :   if(file.length()>0) {
     126           2 :     ofile.open(file);
     127           2 :     log.printf("  on file %s\n",file.c_str());
     128             :   } else {
     129           7 :     log.printf("  on plumed log file\n");
     130           7 :     ofile.link(log);
     131             :   }
     132           9 :   checkRead();
     133           9 : }
     134             : 
     135         221 : void Debug::apply() {
     136         221 :   if(logActivity) {
     137          10 :     const ActionSet&actionSet(plumed.getActionSet());
     138             :     int a=0;
     139         225 :     for(const auto & p : actionSet) {
     140         215 :       if(dynamic_cast<Debug*>(p.get())) {
     141          15 :         continue;
     142             :       }
     143         200 :       if(p->isActive()) {
     144         121 :         a++;
     145             :       }
     146             :     };
     147          10 :     if(a>0) {
     148           9 :       ofile<<"activity at step "<<getStep()<<": ";
     149         207 :       for(const auto & p : actionSet) {
     150         198 :         if(dynamic_cast<Debug*>(p.get())) {
     151          13 :           continue;
     152             :         }
     153         185 :         if(p->isActive()) {
     154         121 :           ofile.printf("+");
     155             :         } else {
     156          64 :           ofile.printf("-");
     157             :         }
     158             :       };
     159           9 :       ofile.printf("\n");
     160             :     };
     161             :   };
     162         221 :   if(logRequestedAtoms) {
     163          17 :     ofile<<"requested atoms at step "<<getStep()<<": ";
     164             :     const int* l;
     165             :     int n;
     166          17 :     plumed.cmd("createFullList",&n);
     167          17 :     plumed.cmd("getFullList",&l);
     168         213 :     for(int i=0; i<n; i++) {
     169         196 :       ofile.printf(" %d",l[i]);
     170             :     }
     171          17 :     ofile.printf("\n");
     172          17 :     plumed.cmd("clearFullList");
     173             :   }
     174             : 
     175         221 : }
     176             : 
     177             : }
     178             : }
     179             : 

Generated by: LCOV version 1.16