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.add("numbered","ARG","the input to this function"); 50 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"); 51 33794 : keys.addActionNameSuffix("_SCALAR"); keys.addActionNameSuffix("_VECTOR"); keys.addActionNameSuffix("_MATRIX"); keys.addActionNameSuffix("_GRID"); 52 7713 : T tfunc; tfunc.registerKeywords( keys ); 53 7713 : } 54 : 55 : template <class T> 56 4631 : FunctionShortcut<T>::FunctionShortcut(const ActionOptions&ao): 57 : Action(ao), 58 4631 : ActionShortcut(ao) 59 : { 60 9262 : std::vector<std::string> args; parseVector("ARG",args); 61 12286 : std::string allargs=args[0]; for(unsigned i=1; i<args.size(); ++i) allargs += "," + args[i]; 62 4631 : std::vector<Value*> vals; ActionWithArguments::interpretArgumentList( args, plumed.getActionSet(), this, vals ); 63 4636 : if( vals.size()==0 ) error("found no input arguments to function"); 64 4631 : createAction( this, vals, allargs ); 65 4644 : } 66 : 67 : template <class T> 68 4633 : void FunctionShortcut<T>::createAction( ActionShortcut* action, const std::vector<Value*>& vals, const std::string& allargs ) { 69 4633 : unsigned maxrank=vals[0]->getRank(); bool isgrid=false; 70 12355 : for(unsigned i=0; i<vals.size(); ++i) { 71 7722 : if( vals[i]->getRank()>0 && vals[i]->hasDerivatives() ) isgrid=true; 72 : if( vals[i]->getRank()>maxrank ) maxrank=vals[i]->getRank(); 73 : } 74 4633 : if( isgrid ) { 75 1299 : if( actionRegister().check( action->getName() + "_GRID") ) action->readInputLine( action->getShortcutLabel() + ": " + action->getName() + "_GRID ARG=" + allargs + " " + action->convertInputLineToString() ); 76 0 : else plumed_merror("there is no action registered that allows you to do " + action->getName() + " with functions on a grid"); 77 4200 : } else if( maxrank==0 ) { 78 4351 : if( actionRegister().check( action->getName() + "_SCALAR") ) action->readInputLine( action->getShortcutLabel() + ": " + action->getName() + "_SCALAR ARG=" + allargs + " " + action->convertInputLineToString() ); 79 0 : else plumed_merror("there is no action registered that allows you to do " + action->getName() + " with scalars"); 80 2758 : } else if( maxrank==1 ) { 81 6792 : if( actionRegister().check( action->getName() + "_VECTOR") ) action->readInputLine( action->getShortcutLabel() + ": " + action->getName() + "_VECTOR ARG=" + allargs + " " + action->convertInputLineToString() ); 82 0 : else plumed_merror("there is no action registered that allows you to do " + action->getName() + " with vectors"); 83 494 : } else if( maxrank==2 ) { 84 1482 : if( actionRegister().check( action->getName() + "_MATRIX") ) action->readInputLine( action->getShortcutLabel() + ": " + action->getName() + "_MATRIX ARG=" + allargs + " " + action->convertInputLineToString() ); 85 0 : else plumed_merror("there is no action registered that allows you to do " + action->getName() + " with matrices"); 86 0 : } else plumed_error(); 87 4628 : } 88 : 89 : } 90 : } 91 : #endif