LCOV - code coverage report
Current view: top level - dimred - OutputPCAProjections.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 39 40 97.5 %
Date: 2021-11-18 15:22:58 Functions: 10 12 83.3 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2016-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 "analysis/AnalysisBase.h"
      23             : #include "reference/ReferenceAtoms.h"
      24             : #include "reference/ReferenceArguments.h"
      25             : #include "core/ActionRegister.h"
      26             : #include "core/PlumedMain.h"
      27             : #include "core/ActionSet.h"
      28             : #include "core/Atoms.h"
      29             : #include "core/SetupMolInfo.h"
      30             : #include "tools/PDB.h"
      31             : #include "PCA.h"
      32             : 
      33             : namespace PLMD {
      34             : namespace dimred {
      35             : 
      36             : //+PLUMEDOC DIMRED OUTPUT_PCA_PROJECTION
      37             : /*
      38             : This is used to output the projection calculated by principle component analysis
      39             : 
      40             : \par Examples
      41             : 
      42             : */
      43             : //+ENDPLUMEDOC
      44             : 
      45           6 : class OutputPCAProjection : public analysis::AnalysisBase {
      46             : private:
      47             :   PDB mypdb;
      48             :   PCA* mypca;
      49             :   std::string fmt;
      50             :   std::string filename;
      51             : public:
      52             :   static void registerKeywords( Keywords& keys );
      53             :   explicit OutputPCAProjection( const ActionOptions& );
      54           0 :   void performTask( const unsigned&, const unsigned&, MultiValue& ) const { plumed_error(); }
      55             :   void performAnalysis();
      56             : };
      57             : 
      58        7360 : PLUMED_REGISTER_ACTION(OutputPCAProjection,"OUTPUT_PCA_PROJECTION")
      59             : 
      60           3 : void OutputPCAProjection::registerKeywords( Keywords& keys ) {
      61           3 :   analysis::AnalysisBase::registerKeywords( keys );
      62          12 :   keys.add("compulsory","FILE","the name of the file to output to");
      63          12 :   keys.add("optional","FMT","the format to use in the output file");
      64          15 :   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");
      65           3 : }
      66             : 
      67           2 : OutputPCAProjection::OutputPCAProjection( const ActionOptions& ao ):
      68             :   Action(ao),
      69             :   analysis::AnalysisBase(ao),
      70           2 :   fmt("%f")
      71             : {
      72             :   // Setup the PCA object
      73           2 :   mypca = dynamic_cast<PCA*>( my_input_data );
      74           2 :   if( !mypca ) error("input must be a PCA object");
      75             : 
      76             :   // Get setup the pdb
      77           2 :   mypdb.setAtomNumbers( my_input_data->getAtomIndexes() );
      78           2 :   mypdb.setArgumentNames( (mypca->my_input_data)->getArgumentNames() );
      79             : 
      80             :   // Find a moldata object
      81           4 :   std::vector<SetupMolInfo*> moldat=plumed.getActionSet().select<SetupMolInfo*>();
      82           3 :   if( moldat.empty() ) warning("PDB output files do not have atom types unless you use MOLDATA");
      83             : 
      84           6 :   parse("FILE",filename); parse("FMT",fmt);
      85           4 :   if( !getRestart() ) { OFile ofile; ofile.link(*this); ofile.setBackupString("analysis"); ofile.backupAllFiles(filename); }
      86           4 :   log.printf("  printing data to file named %s \n",filename.c_str() );
      87           2 : }
      88             : 
      89           2 : void OutputPCAProjection::performAnalysis() {
      90             :   // Find a moldata object
      91           4 :   std::vector<SetupMolInfo*> moldat=plumed.getActionSet().select<SetupMolInfo*>();
      92           2 :   if( moldat.size()>1 ) error("you should only have one MOLINFO action in your input file");
      93           2 :   SetupMolInfo* mymoldat=NULL; if( moldat.size()==1 ) mymoldat=moldat[0];
      94             :   // Output the embedding in plumed pdb format
      95           6 :   OFile afile; afile.link(*this); afile.setBackupString("analysis");
      96           4 :   mypdb.setAtomPositions( (mypca->myref)->getReferencePositions() );
      97          12 :   for(unsigned j=0; j<mypca->getArguments().size(); ++j) mypdb.setArgumentValue( (mypca->getArguments()[j])->getName(), (mypca->myref)->getReferenceArgument(j) );
      98             :   // And output the first frame
      99           6 :   afile.open( filename.c_str() ); afile.printf("REMARK TYPE=%s \n", mypca->mtype.c_str() );
     100           2 :   if( plumed.getAtoms().usingNaturalUnits() ) mypdb.print( 1.0, mymoldat, afile, fmt );
     101           4 :   else mypdb.print( atoms.getUnits().getLength()/0.1, mymoldat, afile, fmt );
     102             :   // And now ouput the eigenvectors
     103          10 :   for(unsigned dim=0; dim<mypca->nlow; ++dim) {
     104           4 :     afile.printf("REMARK TYPE=DIRECTION \n");
     105           8 :     mypdb.setAtomPositions( mypca->directions[dim].getReferencePositions() );
     106          24 :     for(unsigned j=0; j<mypca->getArguments().size(); ++j) mypdb.setArgumentValue( (mypca->getArguments()[j])->getName(), mypca->directions[dim].getReferenceArgument(j) );
     107           4 :     if( plumed.getAtoms().usingNaturalUnits() ) mypdb.print( 1.0, mymoldat, afile, fmt );
     108           8 :     else mypdb.print( atoms.getUnits().getLength()/0.1, mymoldat, afile, fmt );
     109             :   }
     110           2 :   afile.close();
     111           2 : }
     112             : 
     113             : }
     114        5517 : }

Generated by: LCOV version 1.14