LCOV - code coverage report
Current view: top level - analysis - OutputColvarFile.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 44 47 93.6 %
Date: 2021-11-18 15:22:58 Functions: 10 12 83.3 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2015-2020 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 "AnalysisBase.h"
      23             : #include "reference/ReferenceAtoms.h"
      24             : #include "reference/ReferenceArguments.h"
      25             : #include "core/ActionRegister.h"
      26             : #include "core/PlumedMain.h"
      27             : 
      28             : namespace PLMD {
      29             : namespace analysis {
      30             : 
      31             : //+PLUMEDOC ANALYSIS OUTPUT_ANALYSIS_DATA_TO_COLVAR
      32             : /*
      33             : This can be used to output the data that has been stored in an Analysis object.
      34             : 
      35             : The most useful application of this method is to output all projections of all the
      36             : points that were stored in an analysis object that performs some form of dimensionality
      37             : reduction.  If you use the USE_DIMRED_DATA_FROM option below projections of all the
      38             : stored points will be output to a file.  The positions of these projections will be calculated
      39             : using that dimensionality reduction algorithms out-of-sample extension algorithm.
      40             : 
      41             : \par Examples
      42             : 
      43             : */
      44             : //+ENDPLUMEDOC
      45             : 
      46          60 : class OutputColvarFile : public AnalysisBase {
      47             : private:
      48             :   std::string fmt;
      49             :   std::string filename;
      50             :   bool output_for_all_replicas;
      51             :   std::vector<unsigned> preps;
      52             :   std::vector<std::string> req_vals;
      53             : public:
      54             :   static void registerKeywords( Keywords& keys );
      55             :   explicit OutputColvarFile( const ActionOptions& );
      56           0 :   void performTask( const unsigned&, const unsigned&, MultiValue& ) const { plumed_error(); }
      57             :   void performAnalysis();
      58             : };
      59             : 
      60        7396 : PLUMED_REGISTER_ACTION(OutputColvarFile,"OUTPUT_ANALYSIS_DATA_TO_COLVAR")
      61             : 
      62          21 : void OutputColvarFile::registerKeywords( Keywords& keys ) {
      63          42 :   AnalysisBase::registerKeywords( keys ); keys.use("ARG");
      64          84 :   keys.add("compulsory","FILE","the name of the file to output to");
      65         105 :   keys.add("compulsory","REPLICA","0","the replicas for which you would like to output this information");
      66         105 :   keys.add("compulsory","STRIDE","0","the frequency with which to perform the required analysis and to output the data.  The default value of 0 tells plumed to use all the data");
      67          84 :   keys.add("optional","FMT","the format to output the data using");
      68          21 : }
      69             : 
      70          20 : OutputColvarFile::OutputColvarFile( const ActionOptions& ao ):
      71             :   Action(ao),
      72             :   AnalysisBase(ao),
      73             :   fmt("%f"),
      74          60 :   output_for_all_replicas(false)
      75             : {
      76          60 :   parse("FILE",filename); parse("FMT",fmt);
      77          40 :   if( !getRestart() ) { OFile ofile; ofile.link(*this); ofile.setBackupString("analysis"); ofile.backupAllFiles(filename); }
      78          40 :   log.printf("  printing data to file named %s \n",filename.c_str() );
      79          20 :   if( getArguments().size()==0 ) {
      80          24 :     std::vector<std::string> tmp_vals( my_input_data->getArgumentNames() );
      81          36 :     req_vals.resize( tmp_vals.size() ); for(unsigned i=0; i<tmp_vals.size(); ++i) req_vals[i]=tmp_vals[i];
      82             :   } else {
      83          48 :     req_vals.resize( getArguments().size() ); for(unsigned i=0; i<req_vals.size(); ++i) req_vals[i]=getPntrToArgument(i)->getName();
      84             :   }
      85          20 :   if( req_vals.size()==0 ) {
      86           6 :     log.printf("  outputting weights from input action \n");
      87             :   } else {
      88          28 :     log.printf("  outputting %s", req_vals[0].c_str() );
      89          52 :     for(unsigned i=1; i<req_vals.size(); ++i) log.printf(",", req_vals[i].c_str() );
      90          14 :     log.printf("\n");
      91             :   }
      92          60 :   std::vector<std::string> rep_data; parseVector("REPLICA",rep_data);
      93          20 :   if( rep_data.size()==1 ) {
      94          20 :     if( rep_data[0]=="all" ) output_for_all_replicas=true;
      95             :     else {
      96          40 :       preps.resize(1); Tools::convert( rep_data[0], preps[0] );
      97             :     }
      98             :   } else {
      99           0 :     preps.resize( rep_data.size() );
     100           0 :     for(unsigned i=0; i<rep_data.size(); ++i) Tools::convert( rep_data[i], preps[i] );
     101             :   }
     102          20 : }
     103             : 
     104          24 : void OutputColvarFile::performAnalysis() {
     105          24 :   if( !output_for_all_replicas ) {
     106          24 :     bool found=false; unsigned myrep=plumed.multi_sim_comm.Get_rank();
     107          63 :     for(unsigned i=0; i<preps.size(); ++i) {
     108          24 :       if( myrep==preps[i] ) { found=true; break; }
     109             :     }
     110          29 :     if( !found ) return;
     111             :   }
     112             :   // Output the embedding as long lists of data
     113          38 :   OFile gfile; gfile.link(*this);
     114          38 :   gfile.setBackupString("analysis");
     115          38 :   gfile.fmtField(fmt+" ");
     116          38 :   gfile.open( filename.c_str() );
     117             : 
     118             :   // Print embedding coordinates
     119        2599 :   for(unsigned i=0; i<getNumberOfDataPoints(); ++i) {
     120        2580 :     DataCollectionObject& mydata=getStoredData(i, false);
     121       22796 :     for(unsigned j=0; j<req_vals.size(); ++j) gfile.printField( req_vals[j], mydata.getArgumentValue(req_vals[j]) );
     122        5160 :     gfile.printField( "weight", getWeight(i) ); gfile.printField();
     123             :   }
     124          19 :   gfile.close();
     125             : }
     126             : 
     127             : }
     128        5517 : }

Generated by: LCOV version 1.14