LCOV - code coverage report
Current view: top level - gridtools - GridPrintingBase.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 53 53 100.0 %
Date: 2024-10-11 08:09:47 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          60 : void GridPrintingBase::registerKeywords( Keywords& keys ) {
      31          60 :   Action::registerKeywords( keys ); ActionPilot::registerKeywords( keys );
      32         120 :   keys.add("compulsory","GRID","the action that creates the grid you would like to output");
      33         120 :   keys.add("compulsory","STRIDE","0","the frequency with which the grid should be output to the file.  The default "
      34             :            "value of 0 ensures that the grid is only output at the end of the trajectory");
      35         120 :   keys.add("compulsory","FILE","density","the file on which to write the grid.");
      36         120 :   keys.add("compulsory","REPLICA","0","the replica for which you would like to output this information");
      37         120 :   keys.add("optional","FMT","the format that should be used to output real numbers");
      38          60 : }
      39             : 
      40          57 : GridPrintingBase::GridPrintingBase(const ActionOptions&ao):
      41             :   Action(ao),
      42             :   ActionPilot(ao),
      43         114 :   fmt("%f"),
      44          57 :   output_for_all_replicas(false)
      45             : {
      46          57 :   std::string mlab; parse("GRID",mlab);
      47          57 :   vesselbase::ActionWithVessel* mves= plumed.getActionSet().selectWithLabel<vesselbase::ActionWithVessel*>(mlab);
      48          57 :   if(!mves) error("action labelled " +  mlab + " does not exist or does not have vessels");
      49          57 :   addDependency(mves);
      50             : 
      51          57 :   for(unsigned i=0; i<mves->getNumberOfVessels(); ++i) {
      52          57 :     ingrid=dynamic_cast<GridVessel*>( mves->getPntrToVessel(i) );
      53          57 :     if( ingrid ) break;
      54             :   }
      55          57 :   if( !ingrid ) error("input action does not calculate a grid");
      56             : 
      57         114 :   parse("FILE",filename);
      58          57 :   if(filename.length()==0) error("name out output file was not specified");
      59          57 :   log.printf("  outputting grid calculated by action %s to file named %s",mves->getLabel().c_str(), filename.c_str() );
      60         114 :   if( keywords.exists("FMT") ) {
      61         110 :     parse("FMT",fmt); log.printf(" with format %s \n", fmt.c_str() );
      62             :   } else {
      63           2 :     log.printf("\n");
      64             :   }
      65         114 :   std::string rep_data; parse("REPLICA",rep_data);
      66          57 :   if( rep_data=="all" ) output_for_all_replicas=true;
      67          57 :   else { preps.resize(1); Tools::convert( rep_data, preps[0] ); }
      68          57 :   if( output_for_all_replicas ) log.printf("  outputting files for all replicas \n");
      69             :   else {
      70          57 :     log.printf("  outputting data for replicas ");
      71         114 :     for(unsigned i=0; i<preps.size(); ++i) log.printf("%d ", preps[i] );
      72             :   }
      73          57 : }
      74             : 
      75          68 : void GridPrintingBase::update() {
      76          68 :   if( !output_for_all_replicas ) {
      77          68 :     bool found=false; unsigned myrep=plumed.multi_sim_comm.Get_rank();
      78          68 :     for(unsigned i=0; i<preps.size(); ++i) {
      79          68 :       if( myrep==preps[i] ) { found=true; break; }
      80             :     }
      81          93 :     if( !found ) return;
      82             :   }
      83          68 :   if( getStep()==0 || getStride()==0 ) return ;
      84             : 
      85          43 :   OFile ofile; ofile.link(*this);
      86          43 :   ofile.setBackupString("analysis");
      87          43 :   ofile.open( filename ); printGrid( ofile );
      88          43 : }
      89             : 
      90          57 : void GridPrintingBase::runFinalJobs() {
      91          57 :   if( !output_for_all_replicas ) {
      92          57 :     bool found=false; unsigned myrep=plumed.multi_sim_comm.Get_rank();
      93          62 :     for(unsigned i=0; i<preps.size(); ++i) {
      94          57 :       if( myrep==preps[i] ) { found=true; break; }
      95             :     }
      96          82 :     if( !found ) return;
      97             :   }
      98          52 :   if( getStride()>0 ) return;
      99             : 
     100          27 :   OFile ofile; ofile.link(*this);
     101          27 :   ofile.open( filename ); printGrid( ofile );
     102          27 : }
     103             : 
     104             : }
     105             : }

Generated by: LCOV version 1.15