LCOV - code coverage report
Current view: top level - analysis - OutputColvarFile.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 47 48 97.9 %
Date: 2024-10-11 08:09:47 Functions: 6 8 75.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2015-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 "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             : 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 override { plumed_error(); }
      57             :   void performAnalysis() override;
      58             : };
      59             : 
      60       10459 : PLUMED_REGISTER_ACTION(OutputColvarFile,"OUTPUT_ANALYSIS_DATA_TO_COLVAR")
      61             : 
      62          21 : void OutputColvarFile::registerKeywords( Keywords& keys ) {
      63          21 :   AnalysisBase::registerKeywords( keys ); keys.use("ARG");
      64          42 :   keys.add("compulsory","FILE","the name of the file to output to");
      65          42 :   keys.add("compulsory","REPLICA","0","the replicas for which you would like to output this information");
      66          42 :   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          42 :   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          40 :   fmt("%f"),
      74          20 :   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          20 :   log.printf("  printing data to file named %s \n",filename.c_str() );
      79          20 :   if( getArguments().size()==0 ) {
      80          12 :     std::vector<std::string> tmp_vals( my_input_data->getArgumentNames() );
      81          30 :     req_vals.resize( tmp_vals.size() ); for(unsigned i=0; i<tmp_vals.size(); ++i) req_vals[i]=tmp_vals[i];
      82          12 :   } else {
      83          32 :     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          14 :     log.printf("  outputting %s", req_vals[0].c_str() );
      89          22 :     for(unsigned i=1; i<req_vals.size(); ++i) log.printf(",", req_vals[i].c_str() );
      90          14 :     log.printf("\n");
      91             :   }
      92          40 :   std::string rep_data; parse("REPLICA",rep_data);
      93          20 :   if( rep_data=="all" ) output_for_all_replicas=true;
      94          20 :   else { preps.resize(1); Tools::convert( rep_data, preps[0] ); }
      95          20 :   if( output_for_all_replicas ) log.printf("  outputting files for all replicas \n");
      96             :   else {
      97          20 :     log.printf("  outputting data for replicas ");
      98          40 :     for(unsigned i=0; i<preps.size(); ++i) log.printf("%d ", preps[i] );
      99             :   }
     100          20 : }
     101             : 
     102          24 : void OutputColvarFile::performAnalysis() {
     103          24 :   if( !output_for_all_replicas ) {
     104          24 :     bool found=false; unsigned myrep=plumed.multi_sim_comm.Get_rank();
     105          29 :     for(unsigned i=0; i<preps.size(); ++i) {
     106          24 :       if( myrep==preps[i] ) { found=true; break; }
     107             :     }
     108          24 :     if( !found ) return;
     109             :   }
     110             :   // Output the embedding as long lists of data
     111          19 :   OFile gfile; gfile.link(*this);
     112          19 :   gfile.setBackupString("analysis");
     113          19 :   gfile.fmtField(fmt+" ");
     114          19 :   gfile.open( filename );
     115             : 
     116             :   // Print embedding coordinates
     117        2599 :   for(unsigned i=0; i<getNumberOfDataPoints(); ++i) {
     118        2580 :     DataCollectionObject& mydata=getStoredData(i, false);
     119        6989 :     for(unsigned j=0; j<req_vals.size(); ++j) gfile.printField( req_vals[j], mydata.getArgumentValue(req_vals[j]) );
     120        5160 :     gfile.printField( "weight", getWeight(i) ); gfile.printField();
     121             :   }
     122          19 :   gfile.close();
     123          19 : }
     124             : 
     125             : }
     126             : }

Generated by: LCOV version 1.15