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_FunctionShortcut_h 23 : #define __PLUMED_function_FunctionShortcut_h 24 : 25 : #include "core/ActionShortcut.h" 26 : #include "core/PlumedMain.h" 27 : #include "core/ActionSet.h" 28 : #include "core/ActionRegister.h" 29 : #include "core/ActionWithArguments.h" 30 : #include "core/Value.h" 31 : 32 : namespace PLMD { 33 : namespace function { 34 : 35 : template <class T> 36 : class FunctionShortcut : public ActionShortcut { 37 : private: 38 : /// The function that is being computed 39 : T myfunc; 40 : public: 41 : static void registerKeywords(Keywords&); 42 : explicit FunctionShortcut(const ActionOptions&); 43 : static void createAction( ActionShortcut* action, const std::vector<Value*>& vals, const std::string& allargs ); 44 : }; 45 : 46 : template <class T> 47 7713 : void FunctionShortcut<T>::registerKeywords(Keywords& keys ) { 48 7713 : ActionShortcut::registerKeywords( keys ); 49 15426 : keys.reserve("compulsory","PERIODIC","if the output of your function is periodic then you should specify the periodicity of the function. If the output is not periodic you must state this using PERIODIC=NO"); 50 33794 : keys.addActionNameSuffix("_SCALAR"); keys.addActionNameSuffix("_VECTOR"); keys.addActionNameSuffix("_MATRIX"); keys.addActionNameSuffix("_GRID"); 51 7713 : T tfunc; tfunc.registerKeywords( keys ); 52 33544 : if( keys.getDisplayName()=="SUM" || keys.getDisplayName()=="CUSTOM" || keys.getDisplayName()=="MATHEVAL" ) { 53 11804 : keys.addInputKeyword("compulsory","ARG","scalar/vector/matrix/grid","the values input to this function"); 54 3622 : } else keys.addInputKeyword("compulsory","ARG","scalar/vector/matrix","the values input to this function"); 55 7713 : } 56 : 57 : template <class T> 58 4631 : FunctionShortcut<T>::FunctionShortcut(const ActionOptions&ao): 59 : Action(ao), 60 4631 : ActionShortcut(ao) 61 : { 62 9262 : std::vector<std::string> args; parseVector("ARG",args); 63 12286 : std::string allargs=args[0]; for(unsigned i=1; i<args.size(); ++i) allargs += "," + args[i]; 64 4631 : std::vector<Value*> vals; ActionWithArguments::interpretArgumentList( args, plumed.getActionSet(), this, vals ); 65 4636 : if( vals.size()==0 ) error("found no input arguments to function"); 66 4631 : createAction( this, vals, allargs ); 67 4644 : } 68 : 69 : template <class T> 70 4633 : void FunctionShortcut<T>::createAction( ActionShortcut* action, const std::vector<Value*>& vals, const std::string& allargs ) { 71 4633 : unsigned maxrank=vals[0]->getRank(); bool isgrid=false; 72 12355 : for(unsigned i=0; i<vals.size(); ++i) { 73 7722 : if( vals[i]->getRank()>0 && vals[i]->hasDerivatives() ) isgrid=true; 74 : if( vals[i]->getRank()>maxrank ) maxrank=vals[i]->getRank(); 75 : } 76 4633 : if( isgrid ) { 77 1299 : if( actionRegister().check( action->getName() + "_GRID") ) action->readInputLine( action->getShortcutLabel() + ": " + action->getName() + "_GRID ARG=" + allargs + " " + action->convertInputLineToString() ); 78 0 : else plumed_merror("there is no action registered that allows you to do " + action->getName() + " with functions on a grid"); 79 4200 : } else if( maxrank==0 ) { 80 4351 : if( actionRegister().check( action->getName() + "_SCALAR") ) action->readInputLine( action->getShortcutLabel() + ": " + action->getName() + "_SCALAR ARG=" + allargs + " " + action->convertInputLineToString() ); 81 0 : else plumed_merror("there is no action registered that allows you to do " + action->getName() + " with scalars"); 82 2758 : } else if( maxrank==1 ) { 83 6792 : if( actionRegister().check( action->getName() + "_VECTOR") ) action->readInputLine( action->getShortcutLabel() + ": " + action->getName() + "_VECTOR ARG=" + allargs + " " + action->convertInputLineToString() ); 84 0 : else plumed_merror("there is no action registered that allows you to do " + action->getName() + " with vectors"); 85 494 : } else if( maxrank==2 ) { 86 1482 : if( actionRegister().check( action->getName() + "_MATRIX") ) action->readInputLine( action->getShortcutLabel() + ": " + action->getName() + "_MATRIX ARG=" + allargs + " " + action->convertInputLineToString() ); 87 0 : else plumed_merror("there is no action registered that allows you to do " + action->getName() + " with matrices"); 88 0 : } else plumed_error(); 89 4628 : } 90 : 91 : } 92 : } 93 : #endif