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: 2025-11-25 13:55:50 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          10 :   ActionWithArguments* aarg=dynamic_cast<ActionWithArguments*>( act );
      41          10 :   if( aarg ) {
      42           6 :     mypath_obj = aarg->getPntrToArgument(0);
      43             :     // Check that we have only one argument as input
      44           6 :     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          10 :   if( mypath_obj ) {
      50           6 :     mypath_obj->buildDataStore();
      51             :   }
      52             :   // Check that the input is a matrix
      53          10 :   if( mypath_obj )
      54           6 :     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          10 :   act->parseVector("REFERENCE", reference_data);
      60          10 :   std::vector<colvar::RMSDVector*> allrmsd = act->plumed.getActionSet().select<colvar::RMSDVector*>();
      61          10 :   ActionWithArguments::interpretArgumentList( reference_data, act->plumed.getActionSet(), act, refargs );
      62          26 :   for(unsigned i=0; i<refargs.size(); ++i ) {
      63          16 :     Action* thisact = refargs[i]->getPntrToAction();
      64          16 :     for(unsigned j=0; j<allrmsd.size(); ++j) {
      65           4 :       if( allrmsd[j]->checkForDependency(thisact) ) {
      66           4 :         rmsd_objects.push_back( allrmsd[j] );
      67             :         break;
      68             :       }
      69             :     }
      70          16 :     if( !refargs[i]->isConstant() ) {
      71           0 :       act->error("input" + refargs[i]->getName() + " is not constant");
      72             :     }
      73          16 :     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          16 :     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          10 :   int s=sizeof(double);
      82          10 :   metric.cmd("setRealPrecision",&s);
      83          10 :   metric.cmd("setMDEngine","plumed");
      84          10 :   int nat=0;
      85          10 :   metric.cmd("setNatoms",&nat);
      86          10 :   metric.cmd("setNoVirial");
      87          10 :   unsigned nargs=refargs.size();
      88          10 :   if( refargs[0]->getRank()==2 ) {
      89           5 :     nargs = refargs[0]->getShape()[1];
      90             :   }
      91             :   std::string str_nargs;
      92          10 :   Tools::convert( nargs, str_nargs );
      93          10 :   std::string period_str=" PERIODIC=NO";
      94          10 :   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          20 :   metric.readInputLine("arg1: PUT UNIT=number SHAPE=" + str_nargs + period_str, true);
     100          20 :   metric.readInputLine("arg2: PUT UNIT=number SHAPE=" + str_nargs + period_str, true);
     101          10 :   double tstep=1.0;
     102          10 :   metric.cmd("setTimestep",&tstep);
     103             :   std::string inp;
     104          20 :   act->parse("METRIC",inp);
     105             :   inp += " ARG=arg2,arg1";
     106             :   const char* cinp=inp.c_str();
     107          10 :   std::vector<std::string> input=Tools::getWords(inp);
     108          10 :   if( input.size()==1 && !actionRegister().check(input[0]) ) {
     109           0 :     metric.cmd("setPlumedDat",cinp);
     110           0 :     metric.cmd("init");
     111             :   } else {
     112          10 :     metric.cmd("init");
     113          10 :     metric.cmd("readInputLine",cinp);
     114             :   }
     115             :   // Now setup stuff to retrieve the final displacement
     116          10 :   unsigned aind = metric.getActionSet().size()-1;
     117             :   while( true ) {
     118          15 :     const ActionShortcut* as=dynamic_cast<const ActionShortcut*>( metric.getActionSet()[aind].get() );
     119          15 :     if( !as ) {
     120             :       break ;
     121             :     }
     122           5 :     aind = aind - 1;
     123             :     plumed_assert( aind>=0 );
     124           5 :   }
     125          10 :   ActionWithValue* fav = dynamic_cast<ActionWithValue*>( metric.getActionSet()[aind].get() );
     126          10 :   if( !fav ) {
     127           0 :     act->error("final value should calculate relevant value that you want as reference");
     128             :   }
     129          10 :   std::string name = (fav->copyOutput(0))->getName();
     130          10 :   if( fav->getNumberOfComponents()>1 ) {
     131             :     std::string comp;
     132           5 :     act->parse("METRIC_COMPONENT",comp);
     133          10 :     name = fav->getLabel() + "." + comp;
     134             :   }
     135             :   long rank;
     136          20 :   metric.cmd("getDataRank " + name, &rank );
     137          10 :   if( rank==0 ) {
     138           0 :     rank=1;
     139             :   }
     140          10 :   std::vector<long> ishape( rank );
     141          20 :   metric.cmd("getDataShape " + name, &ishape[0] );
     142             :   unsigned nvals=1;
     143          20 :   for(unsigned i=0; i<ishape.size(); ++i) {
     144          10 :     nvals *= ishape[i];
     145             :   }
     146          10 :   data.resize( nvals );
     147          20 :   metric.cmd("setMemoryForData " + name, &data[0] );
     148          20 : }
     149             : 
     150        1114 : unsigned PathProjectionCalculator::getNumberOfFrames() const {
     151        1114 :   return refargs[0]->getShape()[0];
     152             : }
     153             : 
     154       11728 : void PathProjectionCalculator::computeVectorBetweenFrames( const unsigned& ifrom, const unsigned& ito ) {
     155       11728 :   int step = 1;
     156       11728 :   metric.cmd("setStep",&step);
     157       11728 :   std::vector<double> valdata1( data.size() ), valdata2( data.size() );
     158       11728 :   getReferenceConfiguration( ito, valdata2 );
     159       11728 :   getReferenceConfiguration( ifrom, valdata1 );
     160       11728 :   metric.cmd("setValue arg1", &valdata1[0] );
     161       11728 :   metric.cmd("setValue arg2", &valdata2[0] );
     162       11728 :   metric.cmd("calc");
     163       11728 : }
     164             : 
     165       11728 : void PathProjectionCalculator::getDisplaceVector( const unsigned& ifrom, const unsigned& ito, std::vector<double>& displace ) {
     166       11728 :   if( displace.size()!=data.size() ) {
     167        1669 :     displace.resize( data.size() );
     168             :   }
     169       11728 :   computeVectorBetweenFrames( ifrom, ito );
     170      404063 :   for(unsigned i=0; i<data.size(); ++i) {
     171      392335 :     displace[i] = data[i];
     172             :   }
     173       11728 : }
     174             : 
     175       27778 : void PathProjectionCalculator::getReferenceConfiguration( const unsigned& iframe, std::vector<double>& refpos ) const {
     176       27778 :   if( refpos.size()!=data.size() ) {
     177           2 :     refpos.resize( data.size() );
     178             :   }
     179       27778 :   if( refargs[0]->getRank()==2 ) {
     180      962880 :     for(unsigned i=0; i<refpos.size(); ++i) {
     181      938808 :       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       27778 : }
     189             : 
     190        4322 : void PathProjectionCalculator::setReferenceConfiguration( const unsigned& iframe, std::vector<double>& refpos ) {
     191             :   plumed_dbg_assert( refpos.size()==data.size() );
     192        4322 :   if( refargs[0]->getRank()==2 ) {
     193      165360 :     for(unsigned i=0; i<refpos.size(); ++i) {
     194      161226 :       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        4322 : }
     202             : 
     203          27 : void PathProjectionCalculator::updateDepedentRMSDObjects() {
     204          50 :   for(unsigned i=0; i<rmsd_objects.size(); ++i) {
     205          23 :     rmsd_objects[i]->setReferenceConfigurations();
     206             :   }
     207          27 : }
     208             : 
     209             : }
     210             : }
     211             : 

Generated by: LCOV version 1.16