LCOV - code coverage report
Current view: top level - function - MoreThan.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 30 46 65.2 %
Date: 2026-11-05 10:19:04 Functions: 3 3 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             : #ifndef __PLUMED_function_MoreThan_h
      23             : #define __PLUMED_function_MoreThan_h
      24             : #include "FunctionSetup.h"
      25             : #include "FunctionShortcut.h"
      26             : #include "FunctionOfScalar.h"
      27             : #include "FunctionOfVector.h"
      28             : #include "FunctionOfMatrix.h"
      29             : #include "core/ActionRegister.h"
      30             : #include "tools/SwitchingFunction.h"
      31             : 
      32             : #include <cmath>
      33             : 
      34             : namespace PLMD {
      35             : namespace function {
      36             : 
      37         182 : class MoreThan {
      38             : public:
      39             :   bool squared;
      40             : #ifdef __PLUMED_HAS_OPENACC
      41             :   SwitchingFunctionAccelerable switchingFunction;
      42             : #else
      43             :   SwitchingFunction switchingFunction;
      44             : #endif //__PLUMED_HAS_OPENACC
      45             :   static void registerKeywords( Keywords& keys );
      46             :   static void read( MoreThan& func,
      47             :                     ActionWithArguments* action,
      48             :                     FunctionOptions& options );
      49             :   static void calc( const MoreThan& func,
      50             :                     bool noderiv,
      51             :                     const View<const double>& args,
      52             :                     FunctionOutput& funcout );
      53             : #ifdef __PLUMED_HAS_OPENACC
      54             :   void toACCDevice() const {
      55             : #pragma acc enter data copyin(this[0:1],squared)
      56             :     switchingFunction.toACCDevice();
      57             :   }
      58             :   void removeFromACCDevice() const {
      59             :     switchingFunction.removeFromACCDevice();
      60             : #pragma acc exit data delete(squared,this[0:1])
      61             :   }
      62             : #endif //__PLUMED_HAS_OPENACC
      63             : };
      64             : 
      65         338 : void MoreThan::registerKeywords(Keywords& keys) {
      66         338 :   keys.add("compulsory","NN","6","The n parameter of the switching function ");
      67         338 :   keys.add("compulsory","MM","0","The m parameter of the switching function; 0 implies 2*NN");
      68         338 :   keys.add("compulsory","D_0","0.0","The d_0 parameter of the switching function");
      69         338 :   keys.add("compulsory","R_0","The r_0 parameter of the switching function");
      70         338 :   keys.add("optional","SWITCH","This keyword is used if you want to employ an alternative to the continuous switching function defined above. "
      71             :            "The following provides information on the switching functions that are available. "
      72             :            "When this keyword is present you no longer need the NN, MM, D_0 and R_0 keywords.");
      73         338 :   keys.addFlag("SQUARED",false,"is the input quantity the square of the value that you would like to apply the switching function to");
      74         676 :   keys.setValueDescription("scalar/vector/matrix","a function that is one if the if the input is more than a threshold");
      75         338 : }
      76             : 
      77          91 : void MoreThan::read( MoreThan& func, ActionWithArguments* action, FunctionOptions& options ) {
      78          91 :   options.derivativeZeroIfValueIsZero = true;
      79          91 :   if( action->getNumberOfArguments()!=1 ) {
      80           0 :     ActionWithVector* av = dynamic_cast<ActionWithVector*>( action );
      81           0 :     if( !av || (av && action->getNumberOfArguments()-av->getNumberOfMasks()!=1) ) {
      82           0 :       action->error("should only be one argument to this action");
      83             :     }
      84             :   }
      85          91 :   if( action->getPntrToArgument(0)->isPeriodic() ) {
      86           0 :     action->error("cannot use this function on periodic functions");
      87             :   }
      88             : 
      89             : 
      90             :   std::string errors;
      91             :   std::string sfinput;
      92         182 :   action->parse("SWITCH", sfinput);
      93          91 :   if(sfinput.length()>0) {
      94          91 :     func.switchingFunction.set(sfinput, errors);
      95          91 :     if( errors.length()!=0 ) {
      96           0 :       action->error("problem reading SWITCH keyword : " + errors );
      97             :     }
      98             :   } else {
      99           0 :     int nn=6;
     100           0 :     int mm=0;
     101           0 :     double d0=0.0;
     102           0 :     double r0=0.0;
     103           0 :     action->parse("R_0",r0);
     104           0 :     if(r0<=0.0) {
     105           0 :       action->error("R_0 should be explicitly specified and positive");
     106             :     }
     107           0 :     action->parse("D_0",d0);
     108           0 :     action->parse("NN",nn);
     109           0 :     action->parse("MM",mm);
     110           0 :     func.switchingFunction.set(nn,mm,r0,d0);
     111             :   }
     112          91 :   action->log<<"  using switching function with cutoff "<<func.switchingFunction.description()<<"\n";
     113          91 :   action->parseFlag("SQUARED",func.squared);
     114          91 :   if( func.squared ) {
     115           6 :     action->log<<"  input quantity is square of quantity that switching function acts upon\n";
     116             :   }
     117          91 : }
     118             : 
     119      554163 : void MoreThan::calc( const MoreThan& func, bool noderiv, const View<const double>& args, FunctionOutput& funcout ) {
     120             :   // the presence of NDEBUG seems to be ignored by nvcc...
     121             :   // plumed_dbg_assert( args.size()==1 );
     122             :   double d;
     123      554163 :   if( func.squared ) {
     124      466613 :     funcout.values[0] = 1.0 - func.switchingFunction.calculateSqr( args[0], d );
     125             :   } else {
     126       87550 :     funcout.values[0] = 1.0 - func.switchingFunction.calculate( args[0], d );
     127             :   }
     128      554163 :   if( !noderiv ) {
     129        3874 :     funcout.derivs[0][0] = -args[0]*d;
     130             :   }
     131      554163 : }
     132             : 
     133             : }
     134             : }
     135             : #endif //__PLUMED_function_MoreThan_h

Generated by: LCOV version 1.16