LCOV - code coverage report
Current view: top level - refdist - Difference.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 30 39 76.9 %
Date: 2025-11-25 13:55:50 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2011-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 "function/FunctionShortcut.h"
      23             : #include "function/FunctionOfScalar.h"
      24             : #include "function/FunctionOfVector.h"
      25             : #include "core/ActionRegister.h"
      26             : #include "function/FunctionTemplateBase.h"
      27             : 
      28             : #include <cmath>
      29             : 
      30             : namespace PLMD {
      31             : namespace refdist {
      32             : 
      33             : //+PLUMEDOC FUNCTION DIFFERENCE
      34             : /*
      35             : Calculate the differences between two scalars
      36             : 
      37             : \par Examples
      38             : 
      39             : */
      40             : //+ENDPLUMEDOC
      41             : 
      42             : //+PLUMEDOC FUNCTION DIFFERENCE_SCALAR
      43             : /*
      44             : Calculate the differences between two scalars
      45             : 
      46             : \par Examples
      47             : 
      48             : */
      49             : //+ENDPLUMEDOC
      50             : 
      51             : //+PLUMEDOC FUNCTION DIFFERENCE_VECTOR
      52             : /*
      53             : Calculate the differences between the elements of two vectors
      54             : 
      55             : \par Examples
      56             : 
      57             : */
      58             : //+ENDPLUMEDOC
      59             : 
      60             : 
      61        1105 : class Difference : public function::FunctionTemplateBase {
      62             : private:
      63             :   bool periodic;
      64             :   std::string min0, max0;
      65             : public:
      66             :   void registerKeywords(Keywords& keys) override ;
      67             :   void read( ActionWithArguments* action ) override;
      68             :   void setPeriodicityForOutputs( ActionWithValue* action ) override;
      69             :   void calc( const ActionWithArguments* action, const std::vector<double>& args, std::vector<double>& vals, Matrix<double>& derivatives ) const override;
      70             : };
      71             : 
      72             : 
      73             : typedef function::FunctionShortcut<Difference> DifferenceShortcut;
      74             : PLUMED_REGISTER_ACTION(DifferenceShortcut,"DIFFERENCE")
      75             : typedef function::FunctionOfScalar<Difference> ScalarDifference;
      76             : PLUMED_REGISTER_ACTION(ScalarDifference,"DIFFERENCE_SCALAR")
      77             : typedef function::FunctionOfVector<Difference> VectorDifference;
      78             : PLUMED_REGISTER_ACTION(VectorDifference,"DIFFERENCE_VECTOR")
      79             : 
      80         737 : void Difference::registerKeywords(Keywords& keys) {
      81         737 :   keys.setValueDescription("a function that measures the difference");
      82         737 : }
      83             : 
      84         184 : void Difference::read( ActionWithArguments* action ) {
      85         184 :   if( action->getNumberOfArguments()!=2 ) {
      86           0 :     action->error("should be two arguments to this action");
      87             :   }
      88         184 :   if( action->getPntrToArgument(0)->getRank()==action->getPntrToArgument(1)->getRank() ) {
      89          51 :     std::vector<unsigned> shape( action->getPntrToArgument(0)->getShape() );
      90          61 :     for(unsigned i=0; i<shape.size(); ++i) {
      91          10 :       if( shape[i]!=action->getPntrToArgument(1)->getShape()[i] ) {
      92           0 :         action->error("shapes of input actions do not match");
      93             :       }
      94             :     }
      95             :   }
      96             : 
      97         184 :   periodic=false;
      98         184 :   if( action->getPntrToArgument(0)->isPeriodic() ) {
      99          43 :     periodic=true;
     100             :     action->getPntrToArgument(0)->getDomain( min0, max0 );
     101          43 :     if( !action->getPntrToArgument(1)->isConstant() && !action->getPntrToArgument(1)->isPeriodic() ) {
     102           0 :       action->error("period for input variables " + action->getPntrToArgument(0)->getName() + " and " + action->getPntrToArgument(1)->getName() + " should be the same 0");
     103             :     }
     104          43 :     if( !action->getPntrToArgument(1)->isConstant() ) {
     105             :       std::string min1, max1;
     106           1 :       action->getPntrToArgument(1)->getDomain( min1, max1 );
     107           1 :       if( min0!=min0 || max0!=max1 ) {
     108           0 :         action->error("domain for input variables should be the same");
     109             :       }
     110             :     } else {
     111          42 :       action->getPntrToArgument(1)->setDomain( min0, max0 );
     112             :     }
     113         141 :   } else if( action->getPntrToArgument(1)->isPeriodic() ) {
     114           0 :     periodic=true;
     115           0 :     action->getPntrToArgument(1)->getDomain( min0, max0 );
     116           0 :     if( !action->getPntrToArgument(1)->isConstant() ) {
     117           0 :       action->error("period for input variables " + action->getPntrToArgument(0)->getName() + " and " + action->getPntrToArgument(1)->getName() + " should be the same 1");
     118             :     } else {
     119           0 :       action->getPntrToArgument(0)->setDomain( min0, max0 );
     120             :     }
     121             :   }
     122         184 : }
     123             : 
     124         184 : void Difference::setPeriodicityForOutputs( ActionWithValue* action ) {
     125         184 :   if( periodic ) {
     126          43 :     action->setPeriodic( min0, max0 );
     127             :   } else {
     128         141 :     action->setNotPeriodic();
     129             :   }
     130         184 : }
     131             : 
     132     2877849 : void Difference::calc( const ActionWithArguments* action, const std::vector<double>& args, std::vector<double>& vals, Matrix<double>& derivatives ) const {
     133             :   plumed_dbg_assert( args.size()==2 );
     134     2877849 :   vals[0] = action->getPntrToArgument(0)->difference( args[1], args[0] );
     135     2877849 :   derivatives(0,0) = 1.0;
     136     2877849 :   derivatives(0,1)=-1;
     137     2877849 : }
     138             : 
     139             : }
     140             : }
     141             : 
     142             : 

Generated by: LCOV version 1.16