LCOV - code coverage report
Current view: top level - generic - DumpVector.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 68 73 93.2 %
Date: 2026-11-12 10:48:31 Functions: 8 9 88.9 %

          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 "core/ActionWithArguments.h"
      23             : #include "core/ActionWithValue.h"
      24             : #include "core/ActionPilot.h"
      25             : #include "core/ActionRegister.h"
      26             : #include "core/PlumedMain.h"
      27             : #include "tools/Communicator.h"
      28             : #include "tools/OFile.h"
      29             : 
      30             : namespace PLMD {
      31             : namespace generic {
      32             : 
      33             : //+PLUMEDOC PRINTANALYSIS DUMPVECTOR
      34             : /*
      35             : Print a vector to a file
      36             : 
      37             : \par Examples
      38             : 
      39             : */
      40             : //+ENDPLUMEDOC
      41             : 
      42             : class DumpVector :
      43             :   public ActionWithArguments,
      44             :   public ActionPilot {
      45             : private:
      46             :   bool onefile;
      47             :   std::vector<std::string> argnames;
      48             :   std::string fmt, filename;
      49             :   void buildArgnames();
      50             : public:
      51             :   static void registerKeywords( Keywords& keys );
      52             :   explicit DumpVector(const ActionOptions&ao);
      53          80 :   ~DumpVector() {}
      54          16 :   void calculate() override {}
      55          16 :   void apply() override {}
      56             :   void update() override ;
      57             : };
      58             : 
      59             : PLUMED_REGISTER_ACTION(DumpVector,"DUMPVECTOR")
      60             : 
      61          50 : void DumpVector::registerKeywords( Keywords& keys ) {
      62          50 :   Action::registerKeywords( keys );
      63          50 :   ActionPilot::registerKeywords( keys );
      64          50 :   ActionWithArguments::registerKeywords( keys );
      65          50 :   keys.use("ARG");
      66         100 :   keys.add("compulsory","STRIDE","0","the frequency with which the grid should be output to the file.");
      67         100 :   keys.add("compulsory","FILE","density","the file on which to write the vetors");
      68         100 :   keys.add("optional","FMT","the format that should be used to output real numbers");
      69         100 :   keys.addFlag("PRINT_ONE_FILE",false,"output vectors one after the other in a single file");
      70          50 : }
      71             : 
      72          40 : DumpVector::DumpVector(const ActionOptions&ao):
      73             :   Action(ao),
      74             :   ActionWithArguments(ao),
      75             :   ActionPilot(ao),
      76          40 :   fmt("%f") {
      77          40 :   if( getNumberOfArguments()==0 ) {
      78           0 :     error("found no arguments");
      79             :   }
      80          40 :   buildArgnames();
      81          40 :   parse("FILE",filename);
      82          80 :   parseFlag("PRINT_ONE_FILE", onefile);
      83          40 :   if(filename.length()==0) {
      84           0 :     error("name out output file was not specified");
      85             :   }
      86             : 
      87          40 :   log.printf("  outputting data with label %s to file named %s",getPntrToArgument(0)->getName().c_str(), filename.c_str() );
      88          40 :   parse("FMT",fmt);
      89          40 :   log.printf(" with format %s \n", fmt.c_str() );
      90          40 :   fmt = " " + fmt;
      91          40 :   if( onefile ) {
      92           0 :     log.printf("  printing all grids on a single file \n");
      93             :   } else {
      94          40 :     log.printf("  printing all grids on separate files \n");
      95             :   }
      96          40 : }
      97             : 
      98          44 : void DumpVector::buildArgnames() {
      99          44 :   argnames.resize(0);
     100             :   unsigned nvals = getPntrToArgument(0)->getShape()[0];
     101             :   if( getPntrToArgument(0)->getRank()==2 ) {
     102             :     nvals = getPntrToArgument(0)->getShape()[0];
     103             :   }
     104         109 :   for(unsigned i=0; i<getNumberOfArguments(); ++i) {
     105          65 :     if( getPntrToArgument(i)->getShape()[0]!=nvals ) {
     106           0 :       error("all arguments should have same number of values");
     107             :     }
     108          65 :     if( getPntrToArgument(i)->getRank()==1 ) {
     109          28 :       argnames.push_back( getPntrToArgument(i)->getName() );
     110          37 :     } else if( getPntrToArgument(i)->getRank()==2 ) {
     111          37 :       (getPntrToArgument(i)->getPntrToAction())->getMatrixColumnTitles( argnames );
     112             :     }
     113          65 :     getPntrToArgument(i)->buildDataStore();
     114             :   }
     115          44 : }
     116             : 
     117          50 : void DumpVector::update() {
     118          50 :   OFile ofile;
     119          50 :   ofile.link(*this);
     120          50 :   if( onefile ) {
     121           0 :     ofile.enforceRestart();
     122             :   } else {
     123         100 :     ofile.setBackupString("analysis");
     124             :   }
     125          50 :   ofile.open( filename );
     126             : 
     127             :   unsigned totargs = 0;
     128         128 :   for(unsigned i=0; i<getNumberOfArguments(); ++i) {
     129          78 :     if( getPntrToArgument(i)->getRank()==1 ) {
     130          35 :       totargs += 1;
     131          43 :     } else if( getPntrToArgument(i)->getRank()==2 ) {
     132          43 :       totargs += getPntrToArgument(i)->getShape()[1];
     133             :     }
     134             :   }
     135          50 :   if( totargs!=argnames.size() ) {
     136           4 :     buildArgnames();
     137             :   }
     138             : 
     139          50 :   unsigned nvals = getPntrToArgument(0)->getShape()[0];
     140        5240 :   for(unsigned i=0; i<nvals; ++i) {
     141             :     unsigned n=0;
     142        5190 :     ofile.fmtField(" %f");
     143        5190 :     ofile.printField("time",getTime());
     144        5190 :     ofile.printField("parameter",int(i));
     145       13187 :     for(unsigned j=0; j<getNumberOfArguments(); j++) {
     146        7997 :       if( getPntrToArgument(j)->getRank()==1 ) {
     147        4997 :         ofile.fmtField(fmt);
     148        4997 :         ofile.printField(argnames[n],getPntrToArgument(j)->get(i) );
     149        4997 :         n++;
     150        3000 :       } else if( getPntrToArgument(j)->getRank()==2 ) {
     151        3000 :         unsigned ncols = getPntrToArgument(j)->getShape()[1];
     152       13741 :         for(unsigned k=0; k<ncols; ++k) {
     153       10741 :           ofile.fmtField(fmt);
     154       10741 :           ofile.printField(argnames[n],getPntrToArgument(j)->get(i*ncols+k));
     155       10741 :           n++;
     156             :         }
     157             :       }
     158             :     }
     159        5190 :     ofile.printField();
     160             :   }
     161          50 : }
     162             : 
     163             : }
     164             : }

Generated by: LCOV version 1.16