Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2011-2023 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_FunctionTemplateBase_h 23 : #define __PLUMED_function_FunctionTemplateBase_h 24 : 25 : #include "core/ActionWithValue.h" 26 : #include "core/ActionWithArguments.h" 27 : #include "core/ActionWithVector.h" 28 : #include "tools/Matrix.h" 29 : 30 : namespace PLMD { 31 : namespace function { 32 : 33 10054 : class FunctionTemplateBase { 34 : protected: 35 : /// Are we using derivatives 36 : bool noderiv = false; 37 : /// Parse a keyword from the input as a value 38 : template<class T> 39 : void parse( Action* action, const std::string&key, T&t ); 40 : /// Parse a keyword from the input as a vector 41 : template<class T> 42 : void parseVector( Action* action, const std::string&key,std::vector<T>&t); 43 : /// Parse a keyword from the input as a flag 44 : void parseFlag( Action* action, const std::string&key, bool&t ); 45 : public: 46 : /// Override this function if you have not implemented the derivatives 47 0 : virtual bool derivativesImplemented() { return true; } 48 : //// 49 : virtual std::vector<std::string> getComponentsPerLabel() const ; 50 0 : virtual bool getDerivativeZeroIfValueIsZero() const { return false; } 51 : virtual std::string getGraphInfo( const std::string& lab ) const ; 52 : virtual void registerKeywords( Keywords& keys ) = 0; 53 : virtual void read( ActionWithArguments* action ) = 0; 54 0 : virtual bool doWithTasks() const { return true; } 55 : virtual std::vector<Value*> getArgumentsToCheck( const std::vector<Value*>& args ); 56 : bool allComponentsRequired( const std::vector<Value*>& args, const std::vector<ActionWithVector*>& actions ); 57 0 : virtual bool zeroRank() const { return false; } 58 : virtual void setPeriodicityForOutputs( ActionWithValue* action ); 59 0 : virtual void setPrefactor( ActionWithArguments* action, const double pref ) {} 60 0 : virtual unsigned getArgStart() const { return 0; } 61 : virtual void setup( ActionWithValue* action ); 62 : virtual void calc( const ActionWithArguments* action, const std::vector<double>& args, std::vector<double>& vals, Matrix<double>& derivatives ) const = 0; 63 : }; 64 : 65 : template<class T> 66 3143 : void FunctionTemplateBase::parse( Action* action, const std::string&key, T&t ) { 67 3143 : action->parse(key,t); 68 3143 : } 69 : 70 : template<class T> 71 8137 : void FunctionTemplateBase::parseVector( Action* action, const std::string&key,std::vector<T>&t) { 72 8137 : action->parseVector(key,t); 73 8134 : } 74 : 75 : inline 76 : void FunctionTemplateBase::parseFlag( Action* action, const std::string&key, bool&t ) { 77 390 : action->parseFlag(key,t); 78 390 : } 79 : 80 : inline 81 0 : std::vector<std::string> FunctionTemplateBase::getComponentsPerLabel() const { 82 0 : std::vector<std::string> comps; return comps; 83 : } 84 : 85 : inline 86 10661 : void FunctionTemplateBase::setup( ActionWithValue* action ) { 87 10661 : noderiv=action->doNotCalculateDerivatives(); 88 : // Check for grids in input 89 10661 : ActionWithArguments* aarg=dynamic_cast<ActionWithArguments*>( action ); plumed_assert( aarg ); 90 32999 : for(unsigned i=0; i<aarg->getNumberOfArguments(); ++i) { 91 23553 : if( aarg->getPntrToArgument(i)->getRank()>0 && aarg->getPntrToArgument(i)->hasDerivatives() ) { noderiv=false; break; } 92 : } 93 10661 : } 94 : 95 : inline 96 4468 : void FunctionTemplateBase::setPeriodicityForOutputs( ActionWithValue* action ) { 97 4468 : plumed_massert( action->getNumberOfComponents()==1,"you must defined a setPeriodicityForOutputs function in your function class"); 98 8936 : if( action->keywords.exists("PERIODIC") ) { 99 8430 : std::vector<std::string> period; parseVector(action,"PERIODIC",period); 100 4215 : if( period.size()==1 ) { 101 4211 : if( period[0]!="NO") action->error("input to PERIODIC keyword does not make sense"); 102 4211 : action->setNotPeriodic(); return; 103 4 : } else if( period.size()!=2 ) action->error("input to PERIODIC keyword does not make sense"); 104 4 : action->setPeriodic( period[0], period[1] ); 105 4468 : } else action->setNotPeriodic(); 106 : } 107 : 108 : inline 109 1255 : std::vector<Value*> FunctionTemplateBase::getArgumentsToCheck( const std::vector<Value*>& args ) { 110 1255 : return args; 111 : } 112 : 113 : inline 114 2221 : bool FunctionTemplateBase::allComponentsRequired( const std::vector<Value*>& args, const std::vector<ActionWithVector*>& actions ) { 115 2221 : std::vector<Value*> checkArgs = getArgumentsToCheck( args ); 116 3998 : for(unsigned i=0; i<checkArgs.size(); ++i ) { 117 2260 : ActionWithVector* av = dynamic_cast<ActionWithVector*>( checkArgs[i]->getPntrToAction() ); 118 2260 : if( !av ) return false; 119 : bool found=false; 120 18121 : for(unsigned j=0; j<actions.size(); ++j) { 121 17638 : if( actions[j]==av ) { found=true; break; } 122 : } 123 2260 : if( !found ) return true; 124 : } 125 : return false; 126 : } 127 : 128 : inline 129 23 : std::string FunctionTemplateBase::getGraphInfo( const std::string& name ) const { 130 23 : std::size_t und = name.find_last_of("_"); return name.substr(0,und); 131 : } 132 : 133 : } 134 : } 135 : #endif