LCOV - code coverage report
Current view: top level - gridtools - GridPrintingBase.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 64 69 92.8 %
Date: 2026-03-30 13:16:06 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2016-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 "GridPrintingBase.h"
      23             : #include "core/PlumedMain.h"
      24             : #include "core/ActionSet.h"
      25             : #include "vesselbase/ActionWithVessel.h"
      26             : 
      27             : namespace PLMD {
      28             : namespace gridtools {
      29             : 
      30          69 : void GridPrintingBase::registerKeywords( Keywords& keys ) {
      31          69 :   Action::registerKeywords( keys );
      32          69 :   ActionPilot::registerKeywords( keys );
      33         138 :   keys.add("compulsory","GRID","the action that creates the grid you would like to output");
      34         138 :   keys.add("compulsory","STRIDE","0","the frequency with which the grid should be output to the file.  The default "
      35             :            "value of 0 ensures that the grid is only output at the end of the trajectory");
      36         138 :   keys.add("compulsory","FILE","density","the file on which to write the grid.");
      37         138 :   keys.add("compulsory","REPLICA","0","the replica for which you would like to output this information");
      38         138 :   keys.add("optional","FMT","the format that should be used to output real numbers");
      39          69 : }
      40             : 
      41          57 : GridPrintingBase::GridPrintingBase(const ActionOptions&ao):
      42             :   Action(ao),
      43             :   ActionPilot(ao),
      44         114 :   fmt("%f"),
      45          57 :   output_for_all_replicas(false) {
      46             :   std::string mlab;
      47          57 :   parse("GRID",mlab);
      48          57 :   vesselbase::ActionWithVessel* mves= plumed.getActionSet().selectWithLabel<vesselbase::ActionWithVessel*>(mlab);
      49          57 :   if(!mves) {
      50           0 :     error("action labelled " +  mlab + " does not exist or does not have vessels");
      51             :   }
      52          57 :   addDependency(mves);
      53             : 
      54          57 :   for(unsigned i=0; i<mves->getNumberOfVessels(); ++i) {
      55          57 :     ingrid=dynamic_cast<GridVessel*>( mves->getPntrToVessel(i) );
      56          57 :     if( ingrid ) {
      57             :       break;
      58             :     }
      59             :   }
      60          57 :   if( !ingrid ) {
      61           0 :     error("input action does not calculate a grid");
      62             :   }
      63             : 
      64         114 :   parse("FILE",filename);
      65          57 :   if(filename.length()==0) {
      66           0 :     error("name out output file was not specified");
      67             :   }
      68          57 :   log.printf("  outputting grid calculated by action %s to file named %s",mves->getLabel().c_str(), filename.c_str() );
      69         114 :   if( keywords.exists("FMT") ) {
      70          55 :     parse("FMT",fmt);
      71          55 :     log.printf(" with format %s \n", fmt.c_str() );
      72             :   } else {
      73           2 :     log.printf("\n");
      74             :   }
      75             :   std::string rep_data;
      76         114 :   parse("REPLICA",rep_data);
      77          57 :   if( rep_data=="all" ) {
      78           0 :     output_for_all_replicas=true;
      79             :   } else {
      80          57 :     preps.resize(1);
      81          57 :     Tools::convert( rep_data, preps[0] );
      82             :   }
      83          57 :   if( output_for_all_replicas ) {
      84           0 :     log.printf("  outputting files for all replicas \n");
      85             :   } else {
      86          57 :     log.printf("  outputting data for replicas ");
      87         114 :     for(unsigned i=0; i<preps.size(); ++i) {
      88          57 :       log.printf("%d ", preps[i] );
      89             :     }
      90             :   }
      91          57 : }
      92             : 
      93          68 : void GridPrintingBase::update() {
      94          68 :   if( !output_for_all_replicas ) {
      95             :     bool found=false;
      96          68 :     unsigned myrep=plumed.multi_sim_comm.Get_rank();
      97          68 :     for(unsigned i=0; i<preps.size(); ++i) {
      98          68 :       if( myrep==preps[i] ) {
      99             :         found=true;
     100             :         break;
     101             :       }
     102             :     }
     103          68 :     if( !found ) {
     104          25 :       return;
     105             :     }
     106             :   }
     107          68 :   if( getStep()==0 || getStride()==0 ) {
     108          25 :     return ;
     109             :   }
     110             : 
     111          43 :   OFile ofile;
     112          43 :   ofile.link(*this);
     113          43 :   ofile.setBackupString("analysis");
     114          43 :   ofile.open( filename );
     115          43 :   printGrid( ofile );
     116          43 : }
     117             : 
     118          57 : void GridPrintingBase::runFinalJobs() {
     119          57 :   if( !output_for_all_replicas ) {
     120             :     bool found=false;
     121          57 :     unsigned myrep=plumed.multi_sim_comm.Get_rank();
     122          62 :     for(unsigned i=0; i<preps.size(); ++i) {
     123          57 :       if( myrep==preps[i] ) {
     124             :         found=true;
     125             :         break;
     126             :       }
     127             :     }
     128          57 :     if( !found ) {
     129          30 :       return;
     130             :     }
     131             :   }
     132          52 :   if( getStride()>0 ) {
     133             :     return;
     134             :   }
     135             : 
     136          27 :   OFile ofile;
     137          27 :   ofile.link(*this);
     138          27 :   ofile.open( filename );
     139          27 :   printGrid( ofile );
     140          27 : }
     141             : 
     142             : }
     143             : }

Generated by: LCOV version 1.16