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 10155 : 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() { 48 0 : return true; 49 : } 50 : //// 51 : virtual std::vector<std::string> getComponentsPerLabel() const ; 52 0 : virtual bool getDerivativeZeroIfValueIsZero() const { 53 0 : return false; 54 : } 55 : virtual std::string getGraphInfo( const std::string& lab ) const ; 56 : virtual void registerKeywords( Keywords& keys ) = 0; 57 : virtual void read( ActionWithArguments* action ) = 0; 58 0 : virtual bool doWithTasks() const { 59 0 : return true; 60 : } 61 : virtual std::vector<Value*> getArgumentsToCheck( const std::vector<Value*>& args ); 62 : bool allComponentsRequired( const std::vector<Value*>& args, const std::vector<ActionWithVector*>& actions ); 63 0 : virtual bool zeroRank() const { 64 0 : return false; 65 : } 66 : virtual void setPeriodicityForOutputs( ActionWithValue* action ); 67 0 : virtual void setPrefactor( ActionWithArguments* action, const double pref ) {} 68 0 : virtual unsigned getArgStart() const { 69 0 : return 0; 70 : } 71 : virtual void setup( ActionWithValue* action ); 72 : virtual void calc( const ActionWithArguments* action, const std::vector<double>& args, std::vector<double>& vals, Matrix<double>& derivatives ) const = 0; 73 : }; 74 : 75 : template<class T> 76 3134 : void FunctionTemplateBase::parse( Action* action, const std::string&key, T&t ) { 77 3134 : action->parse(key,t); 78 3134 : } 79 : 80 : template<class T> 81 8129 : void FunctionTemplateBase::parseVector( Action* action, const std::string&key,std::vector<T>&t) { 82 8129 : action->parseVector(key,t); 83 8126 : } 84 : 85 : inline 86 : void FunctionTemplateBase::parseFlag( Action* action, const std::string&key, bool&t ) { 87 391 : action->parseFlag(key,t); 88 391 : } 89 : 90 : inline 91 0 : std::vector<std::string> FunctionTemplateBase::getComponentsPerLabel() const { 92 : std::vector<std::string> comps; 93 0 : return comps; 94 : } 95 : 96 : inline 97 10693 : void FunctionTemplateBase::setup( ActionWithValue* action ) { 98 10693 : noderiv=action->doNotCalculateDerivatives(); 99 : // Check for grids in input 100 10693 : ActionWithArguments* aarg=dynamic_cast<ActionWithArguments*>( action ); 101 10693 : plumed_assert( aarg ); 102 33072 : for(unsigned i=0; i<aarg->getNumberOfArguments(); ++i) { 103 23594 : if( aarg->getPntrToArgument(i)->getRank()>0 && aarg->getPntrToArgument(i)->hasDerivatives() ) { 104 1215 : noderiv=false; 105 1215 : break; 106 : } 107 : } 108 10693 : } 109 : 110 : inline 111 4475 : void FunctionTemplateBase::setPeriodicityForOutputs( ActionWithValue* action ) { 112 4475 : plumed_massert( action->getNumberOfComponents()==1,"you must defined a setPeriodicityForOutputs function in your function class"); 113 4475 : if( action->keywords.exists("PERIODIC") ) { 114 : std::vector<std::string> period; 115 8426 : parseVector(action,"PERIODIC",period); 116 4213 : if( period.size()==1 ) { 117 4209 : if( period[0]!="NO") { 118 0 : action->error("input to PERIODIC keyword does not make sense"); 119 : } 120 4209 : action->setNotPeriodic(); 121 : return; 122 4 : } else if( period.size()!=2 ) { 123 0 : action->error("input to PERIODIC keyword does not make sense"); 124 : } 125 4 : action->setPeriodic( period[0], period[1] ); 126 4213 : } else { 127 262 : action->setNotPeriodic(); 128 : } 129 : } 130 : 131 : inline 132 1282 : std::vector<Value*> FunctionTemplateBase::getArgumentsToCheck( const std::vector<Value*>& args ) { 133 1282 : return args; 134 : } 135 : 136 : inline 137 2248 : bool FunctionTemplateBase::allComponentsRequired( const std::vector<Value*>& args, const std::vector<ActionWithVector*>& actions ) { 138 2248 : std::vector<Value*> checkArgs = getArgumentsToCheck( args ); 139 4061 : for(unsigned i=0; i<checkArgs.size(); ++i ) { 140 2296 : ActionWithVector* av = dynamic_cast<ActionWithVector*>( checkArgs[i]->getPntrToAction() ); 141 2296 : if( !av ) { 142 : return false; 143 : } 144 : bool found=false; 145 18172 : for(unsigned j=0; j<actions.size(); ++j) { 146 17689 : if( actions[j]==av ) { 147 : found=true; 148 : break; 149 : } 150 : } 151 2296 : if( !found ) { 152 : return true; 153 : } 154 : } 155 : return false; 156 : } 157 : 158 : inline 159 23 : std::string FunctionTemplateBase::getGraphInfo( const std::string& name ) const { 160 23 : std::size_t und = name.find_last_of("_"); 161 23 : return name.substr(0,und); 162 : } 163 : 164 : } 165 : } 166 : #endif