LCOV - code coverage report
Current view: top level - mapping - PathProjectionCalculator.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 108 117 92.3 %
Date: 2026-11-12 10:48:31 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          42 : void PathProjectionCalculator::registerKeywords(Keywords& keys) {
      33          84 :   keys.add("compulsory","METRIC","the method to use for computing the displacement vectors between the reference frames");
      34          84 :   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          84 :   keys.add("compulsory","REFERENCE","labels for actions that contain reference coordinates for each point on the path");
      36          42 : }
      37             : 
      38          16 : PathProjectionCalculator::PathProjectionCalculator( Action* act ):
      39          16 :   mypath_obj(NULL) {
      40          16 :   ActionWithArguments* aarg=dynamic_cast<ActionWithArguments*>( act );
      41          16 :   if( aarg ) {
      42          10 :     mypath_obj = aarg->getPntrToArgument(0);
      43             :     // Check that we have only one argument as input
      44          10 :     if( aarg->getNumberOfArguments()!=1 ) {
      45           0 :       act->error("should only have one argument to this function");
      46             :     }
      47             :   }
      48             :   // Ensure that values are stored in base calculation and that PLUMED doesn't try to calculate this in the stream
      49          16 :   if( mypath_obj ) {
      50          10 :     mypath_obj->buildDataStore();
      51             :   }
      52             :   // Check that the input is a matrix
      53          16 :   if( mypath_obj )
      54          10 :     if( mypath_obj->getRank()!=2 ) {
      55           0 :       act->error("the input to this action should be a matrix");
      56             :     }
      57             :   // Get the labels for the reference points
      58             :   std::vector<std::string> reference_data;
      59          16 :   act->parseVector("REFERENCE", reference_data);
      60          16 :   std::vector<colvar::RMSDVector*> allrmsd = act->plumed.getActionSet().select<colvar::RMSDVector*>();
      61          16 :   ActionWithArguments::interpretArgumentList( reference_data, act->plumed.getActionSet(), act, refargs );
      62          38 :   for(unsigned i=0; i<refargs.size(); ++i ) {
      63          22 :     Action* thisact = refargs[i]->getPntrToAction();
      64          25 :     for(unsigned j=0; j<allrmsd.size(); ++j) {
      65          13 :       if( allrmsd[j]->checkForDependency(thisact) ) {
      66          10 :         rmsd_objects.push_back( allrmsd[j] );
      67             :         break;
      68             :       }
      69             :     }
      70          22 :     if( !refargs[i]->isConstant() ) {
      71           0 :       act->error("input" + refargs[i]->getName() + " is not constant");
      72             :     }
      73          22 :     if( refargs[i]->getRank()==2 && reference_data.size()!=1 ) {
      74           0 :       act->error("should only be one matrix in input to path projection object");
      75             :     }
      76          22 :     if( refargs[i]->getRank()>0 && refargs[i]->getShape()[0]!=refargs[0]->getShape()[0] ) {
      77           0 :       act->error("mismatch in number of reference frames in input to reference_data");
      78             :     }
      79             :   }
      80             :   // Create a plumed main object to compute distances between reference configurations
      81          16 :   int s=sizeof(double);
      82          16 :   metric.cmd("setRealPrecision",&s);
      83          16 :   metric.cmd("setMDEngine","plumed");
      84          16 :   int nat=0;
      85          16 :   metric.cmd("setNatoms",&nat);
      86          16 :   metric.cmd("setNoVirial");
      87          16 :   unsigned nargs=refargs.size();
      88          16 :   if( refargs[0]->getRank()==2 ) {
      89          11 :     nargs = refargs[0]->getShape()[1];
      90             :   }
      91             :   std::string str_nargs;
      92          16 :   Tools::convert( nargs, str_nargs );
      93          16 :   std::string period_str=" PERIODIC=NO";
      94          16 :   if( mypath_obj && mypath_obj->isPeriodic() ) {
      95             :     std::string min, max;
      96           1 :     mypath_obj->getDomain( min, max );
      97           2 :     period_str=" PERIODIC=" + min + "," + max;
      98             :   }
      99          32 :   metric.readInputLine("arg1: PUT UNIT=number SHAPE=" + str_nargs + period_str, true);
     100          32 :   metric.readInputLine("arg2: PUT UNIT=number SHAPE=" + str_nargs + period_str, true);
     101          16 :   double tstep=1.0;
     102          16 :   metric.cmd("setTimestep",&tstep);
     103             :   std::string inp;
     104          32 :   act->parse("METRIC",inp);
     105             :   inp += " ARG=arg2,arg1";
     106             :   const char* cinp=inp.c_str();
     107          16 :   std::vector<std::string> input=Tools::getWords(inp);
     108          16 :   if( input.size()==1 && !actionRegister().check(input[0]) ) {
     109           0 :     metric.cmd("setPlumedDat",cinp);
     110           0 :     metric.cmd("init");
     111             :   } else {
     112          16 :     metric.cmd("init");
     113          16 :     metric.cmd("readInputLine",cinp);
     114             :   }
     115             :   // Now setup stuff to retrieve the final displacement
     116          16 :   unsigned aind = metric.getActionSet().size()-1;
     117             :   while( true ) {
     118          21 :     const ActionShortcut* as=dynamic_cast<const ActionShortcut*>( metric.getActionSet()[aind].get() );
     119          21 :     if( !as ) {
     120             :       break ;
     121             :     }
     122           5 :     aind = aind - 1;
     123             :     plumed_assert( aind>=0 );
     124           5 :   }
     125          16 :   ActionWithValue* fav = dynamic_cast<ActionWithValue*>( metric.getActionSet()[aind].get() );
     126          16 :   if( !fav ) {
     127           0 :     act->error("final value should calculate relevant value that you want as reference");
     128             :   }
     129          16 :   std::string name = (fav->copyOutput(0))->getName();
     130          16 :   if( fav->getNumberOfComponents()>1 ) {
     131             :     std::string comp;
     132          11 :     act->parse("METRIC_COMPONENT",comp);
     133          22 :     name = fav->getLabel() + "." + comp;
     134             :   }
     135             :   long rank;
     136          32 :   metric.cmd("getDataRank " + name, &rank );
     137          16 :   if( rank==0 ) {
     138           0 :     rank=1;
     139             :   }
     140          16 :   std::vector<long> ishape( rank );
     141          32 :   metric.cmd("getDataShape " + name, &ishape[0] );
     142             :   unsigned nvals=1;
     143          32 :   for(unsigned i=0; i<ishape.size(); ++i) {
     144          16 :     nvals *= ishape[i];
     145             :   }
     146          16 :   data.resize( nvals );
     147          32 :   metric.cmd("setMemoryForData " + name, &data[0] );
     148          32 : }
     149             : 
     150        2258 : unsigned PathProjectionCalculator::getNumberOfFrames() const {
     151        2258 :   return refargs[0]->getShape()[0];
     152             : }
     153             : 
     154       23195 : void PathProjectionCalculator::computeVectorBetweenFrames( const unsigned& ifrom, const unsigned& ito ) {
     155       23195 :   int step = 1;
     156       23195 :   metric.cmd("setStep",&step);
     157       23195 :   std::vector<double> valdata1( data.size() ), valdata2( data.size() );
     158       23195 :   getReferenceConfiguration( ito, valdata2 );
     159       23195 :   getReferenceConfiguration( ifrom, valdata1 );
     160       23195 :   metric.cmd("setValue arg1", &valdata1[0] );
     161       23195 :   metric.cmd("setValue arg2", &valdata2[0] );
     162       23195 :   metric.cmd("calc");
     163       23195 : }
     164             : 
     165       23195 : void PathProjectionCalculator::getDisplaceVector( const unsigned& ifrom, const unsigned& ito, std::vector<double>& displace ) {
     166       23195 :   if( displace.size()!=data.size() ) {
     167        2763 :     displace.resize( data.size() );
     168             :   }
     169       23195 :   computeVectorBetweenFrames( ifrom, ito );
     170      862743 :   for(unsigned i=0; i<data.size(); ++i) {
     171      839548 :     displace[i] = data[i];
     172             :   }
     173       23195 : }
     174             : 
     175       54790 : void PathProjectionCalculator::getReferenceConfiguration( const unsigned& iframe, std::vector<double>& refpos ) const {
     176       54790 :   if( refpos.size()!=data.size() ) {
     177           4 :     refpos.resize( data.size() );
     178             :   }
     179       54790 :   if( refargs[0]->getRank()==2 ) {
     180     2043360 :     for(unsigned i=0; i<refpos.size(); ++i) {
     181     1992276 :       refpos[i] = refargs[0]->get( iframe*refpos.size() + i );
     182             :     }
     183             :   } else {
     184       11178 :     for(unsigned i=0; i<refpos.size(); ++i) {
     185        7472 :       refpos[i] = refargs[i]->get(iframe);
     186             :     }
     187             :   }
     188       54790 : }
     189             : 
     190        8400 : void PathProjectionCalculator::setReferenceConfiguration( const unsigned& iframe, std::vector<double>& refpos ) {
     191             :   plumed_dbg_assert( refpos.size()==data.size() );
     192        8400 :   if( refargs[0]->getRank()==2 ) {
     193      328480 :     for(unsigned i=0; i<refpos.size(); ++i) {
     194      320268 :       refargs[0]->set( iframe*refpos.size() + i, refpos[i] );
     195             :     }
     196             :   } else {
     197         572 :     for(unsigned i=0; i<refpos.size(); ++i) {
     198         384 :       refargs[i]->set( iframe, refpos[i] );
     199             :     }
     200             :   }
     201        8400 : }
     202             : 
     203         108 : void PathProjectionCalculator::updateDepedentRMSDObjects() {
     204         212 :   for(unsigned i=0; i<rmsd_objects.size(); ++i) {
     205         104 :     rmsd_objects[i]->setReferenceConfigurations();
     206             :   }
     207         108 : }
     208             : 
     209             : }
     210             : }
     211             : 

Generated by: LCOV version 1.16