LCOV - code coverage report
Current view: top level - mapping - PathProjectionCalculator.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 80 81 98.8 %
Date: 2024-10-18 14:00:25 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2016,2017 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 "PathProjectionCalculator.h"
      23             : #include "core/ActionWithValue.h"
      24             : #include "core/ActionWithArguments.h"
      25             : #include "core/ActionRegister.h"
      26             : #include "core/ActionSet.h"
      27             : #include "core/PlumedMain.h"
      28             : 
      29             : namespace PLMD {
      30             : namespace mapping {
      31             : 
      32          24 : void PathProjectionCalculator::registerKeywords(Keywords& keys) {
      33          48 :   keys.add("compulsory","METRIC","the method to use for computing the displacement vectors between the reference frames");
      34          48 :   keys.add("compulsory","METRIC_COMPONENT","if the final action in your metric contains multiple components this keyword is used to specify the component that should be used");
      35          48 :   keys.add("compulsory","REFERENCE","labels for actions that contain reference coordinates for each point on the path");
      36          24 : }
      37             : 
      38          10 : PathProjectionCalculator::PathProjectionCalculator( Action* act ):
      39          10 :   mypath_obj(NULL)
      40             : {
      41          10 :   ActionWithArguments* aarg=dynamic_cast<ActionWithArguments*>( act );
      42          10 :   if( aarg ) {
      43           6 :     mypath_obj = aarg->getPntrToArgument(0);
      44             :     // Check that we have only one argument as input
      45           6 :     if( aarg->getNumberOfArguments()!=1 ) act->error("should only have one argument to this function");
      46             :   }
      47             :   // Ensure that values are stored in base calculation and that PLUMED doesn't try to calculate this in the stream
      48          10 :   if( mypath_obj ) mypath_obj->buildDataStore();
      49             :   // Check that the input is a matrix
      50          10 :   if( mypath_obj ) if( mypath_obj->getRank()!=2 ) act->error("the input to this action should be a matrix");
      51             :   // Get the labels for the reference points
      52          10 :   std::vector<std::string> reference_data; act->parseVector("REFERENCE", reference_data);
      53          10 :   std::vector<colvar::RMSDVector*> allrmsd = act->plumed.getActionSet().select<colvar::RMSDVector*>();
      54          10 :   ActionWithArguments::interpretArgumentList( reference_data, act->plumed.getActionSet(), act, refargs );
      55          26 :   for(unsigned i=0; i<refargs.size(); ++i ) {
      56          16 :     Action* thisact = refargs[i]->getPntrToAction();
      57          16 :     for(unsigned j=0; j<allrmsd.size(); ++j) {
      58           4 :       if( allrmsd[j]->checkForDependency(thisact) ) { rmsd_objects.push_back( allrmsd[j] ); break; }
      59             :     }
      60          16 :     if( !refargs[i]->isConstant() ) act->error("input" + refargs[i]->getName() + " is not constant");
      61          16 :     if( refargs[i]->getRank()==2 && reference_data.size()!=1 ) act->error("should only be one matrix in input to path projection object");
      62          16 :     if( refargs[i]->getRank()>0 && refargs[i]->getShape()[0]!=refargs[0]->getShape()[0] ) act->error("mismatch in number of reference frames in input to reference_data");
      63             :   }
      64             :   // Create a plumed main object to compute distances between reference configurations
      65          10 :   int s=sizeof(double);
      66          10 :   metric.cmd("setRealPrecision",&s);
      67          10 :   metric.cmd("setMDEngine","plumed");
      68          10 :   int nat=0; metric.cmd("setNatoms",&nat); metric.cmd("setNoVirial");
      69          10 :   unsigned nargs=refargs.size(); if( refargs[0]->getRank()==2 ) nargs = refargs[0]->getShape()[1];
      70          10 :   std::string str_nargs; Tools::convert( nargs, str_nargs ); std::string period_str=" PERIODIC=NO";
      71          11 :   if( mypath_obj && mypath_obj->isPeriodic() ) { std::string min, max; mypath_obj->getDomain( min, max ); period_str=" PERIODIC=" + min + "," + max; }
      72          20 :   metric.readInputLine("arg1: PUT UNIT=number SHAPE=" + str_nargs + period_str, true);
      73          20 :   metric.readInputLine("arg2: PUT UNIT=number SHAPE=" + str_nargs + period_str, true);
      74          10 :   double tstep=1.0; metric.cmd("setTimestep",&tstep);
      75          20 :   std::string inp; act->parse("METRIC",inp); inp += " ARG=arg2,arg1"; const char* cinp=inp.c_str();
      76          10 :   std::vector<std::string> input=Tools::getWords(inp);
      77          10 :   if( input.size()==1 && !actionRegister().check(input[0]) ) {
      78           0 :     metric.cmd("setPlumedDat",cinp); metric.cmd("init");
      79             :   } else {
      80          10 :     metric.cmd("init"); metric.cmd("readInputLine",cinp);
      81             :   }
      82             :   // Now setup stuff to retrieve the final displacement
      83          10 :   unsigned aind = metric.getActionSet().size()-1;
      84             :   while( true ) {
      85          15 :     const ActionShortcut* as=dynamic_cast<const ActionShortcut*>( metric.getActionSet()[aind].get() );
      86          15 :     if( !as ) break ; aind = aind - 1; plumed_assert( aind>=0 );
      87           5 :   }
      88          10 :   ActionWithValue* fav = dynamic_cast<ActionWithValue*>( metric.getActionSet()[aind].get() );
      89          10 :   if( !fav ) act->error("final value should calculate relevant value that you want as reference");
      90          10 :   std::string name = (fav->copyOutput(0))->getName();
      91          10 :   if( fav->getNumberOfComponents()>1 ) {
      92          15 :     std::string comp; act->parse("METRIC_COMPONENT",comp); name = fav->getLabel() + "." + comp;
      93             :   }
      94          20 :   long rank; metric.cmd("getDataRank " + name, &rank );
      95          10 :   if( rank==0 ) rank=1;
      96          20 :   std::vector<long> ishape( rank ); metric.cmd("getDataShape " + name, &ishape[0] );
      97          20 :   unsigned nvals=1; for(unsigned i=0; i<ishape.size(); ++i) nvals *= ishape[i];
      98          30 :   data.resize( nvals ); metric.cmd("setMemoryForData " + name, &data[0] );
      99          20 : }
     100             : 
     101        1114 : unsigned PathProjectionCalculator::getNumberOfFrames() const {
     102        1114 :   return refargs[0]->getShape()[0];
     103             : }
     104             : 
     105       11728 : void PathProjectionCalculator::computeVectorBetweenFrames( const unsigned& ifrom, const unsigned& ito ) {
     106       11728 :   int step = 1; metric.cmd("setStep",&step);
     107       11728 :   std::vector<double> valdata1( data.size() ), valdata2( data.size() );
     108       11728 :   getReferenceConfiguration( ito, valdata2 ); getReferenceConfiguration( ifrom, valdata1 );
     109       11728 :   metric.cmd("setValue arg1", &valdata1[0] );
     110       11728 :   metric.cmd("setValue arg2", &valdata2[0] );
     111       11728 :   metric.cmd("calc");
     112       11728 : }
     113             : 
     114       11728 : void PathProjectionCalculator::getDisplaceVector( const unsigned& ifrom, const unsigned& ito, std::vector<double>& displace ) {
     115       11728 :   if( displace.size()!=data.size() ) displace.resize( data.size() );
     116      404063 :   computeVectorBetweenFrames( ifrom, ito ); for(unsigned i=0; i<data.size(); ++i) displace[i] = data[i];
     117       11728 : }
     118             : 
     119       27778 : void PathProjectionCalculator::getReferenceConfiguration( const unsigned& iframe, std::vector<double>& refpos ) const {
     120       27778 :   if( refpos.size()!=data.size() ) refpos.resize( data.size() );
     121       27778 :   if( refargs[0]->getRank()==2 ) {
     122      962880 :     for(unsigned i=0; i<refpos.size(); ++i) refpos[i] = refargs[0]->get( iframe*refpos.size() + i );
     123             :   } else {
     124       11178 :     for(unsigned i=0; i<refpos.size(); ++i) refpos[i] = refargs[i]->get(iframe);
     125             :   }
     126       27778 : }
     127             : 
     128        4322 : void PathProjectionCalculator::setReferenceConfiguration( const unsigned& iframe, std::vector<double>& refpos ) {
     129             :   plumed_dbg_assert( refpos.size()==data.size() );
     130        4322 :   if( refargs[0]->getRank()==2 ) {
     131      165360 :     for(unsigned i=0; i<refpos.size(); ++i) refargs[0]->set( iframe*refpos.size() + i, refpos[i] );
     132             :   } else {
     133         572 :     for(unsigned i=0; i<refpos.size(); ++i) refargs[i]->set( iframe, refpos[i] );
     134             :   }
     135        4322 : }
     136             : 
     137          27 : void PathProjectionCalculator::updateDepedentRMSDObjects() {
     138          50 :   for(unsigned i=0; i<rmsd_objects.size(); ++i) rmsd_objects[i]->setReferenceConfigurations();
     139          27 : }
     140             : 
     141             : }
     142             : }
     143             : 

Generated by: LCOV version 1.16