Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2016-2018 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 : #include "core/ActionRegister.h" 23 : #include "function/FunctionShortcut.h" 24 : #include "function/FunctionOfScalar.h" 25 : #include "function/FunctionOfVector.h" 26 : #include "EvaluateGridFunction.h" 27 : #include "ActionWithGrid.h" 28 : 29 : //+PLUMEDOC GRIDCALC EVALUATE_FUNCTION_FROM_GRID 30 : /* 31 : Calculate the function stored on the input grid at an arbitrary point 32 : 33 : \par Examples 34 : 35 : */ 36 : //+ENDPLUMEDOC 37 : 38 : //+PLUMEDOC GRIDCALC EVALUATE_FUNCTION_FROM_GRID_SCALAR 39 : /* 40 : Calculate the function stored on the input grid at an arbitrary point 41 : 42 : \par Examples 43 : 44 : */ 45 : //+ENDPLUMEDOC 46 : 47 : //+PLUMEDOC GRIDCALC EVALUATE_FUNCTION_FROM_GRID_VECTOR 48 : /* 49 : Calculate the function stored on the input grid for each of the points in the input vector/s 50 : 51 : \par Examples 52 : 53 : */ 54 : //+ENDPLUMEDOC 55 : 56 : 57 : namespace PLMD { 58 : namespace gridtools { 59 : 60 : class EvaluateFunctionOnGrid : public ActionShortcut { 61 : public: 62 : static void registerKeywords(Keywords&); 63 : explicit EvaluateFunctionOnGrid(const ActionOptions&); 64 : }; 65 : 66 : PLUMED_REGISTER_ACTION(EvaluateFunctionOnGrid,"EVALUATE_FUNCTION_FROM_GRID") 67 : typedef function::FunctionOfScalar<EvaluateGridFunction> ScalarEvalGrid; 68 : PLUMED_REGISTER_ACTION(ScalarEvalGrid,"EVALUATE_FUNCTION_FROM_GRID_SCALAR") 69 : typedef function::FunctionOfVector<EvaluateGridFunction> VectorEvalGrid; 70 : PLUMED_REGISTER_ACTION(VectorEvalGrid,"EVALUATE_FUNCTION_FROM_GRID_VECTOR") 71 : 72 4 : void EvaluateFunctionOnGrid::registerKeywords(Keywords& keys ) { 73 4 : ActionShortcut::registerKeywords( keys ); 74 8 : keys.addInputKeyword("compulsory","GRID","grid","the name of the grid that we are using to evaluate the function"); 75 8 : keys.addInputKeyword("optional","ARG","scalar/vector","the arguments that you would like to use when evaluating the function. If not specified these are determined from the names of the grid dimensions"); 76 4 : 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"); 77 : EvaluateGridFunction ii; 78 4 : ii.registerKeywords( keys ); 79 4 : keys.addActionNameSuffix("_SCALAR"); 80 4 : keys.addActionNameSuffix("_VECTOR"); 81 4 : } 82 : 83 2 : EvaluateFunctionOnGrid::EvaluateFunctionOnGrid(const ActionOptions&ao): 84 : Action(ao), 85 2 : ActionShortcut(ao) { 86 : // Get the grid that we are evaluating here 87 2 : std::vector<std::string> gridn(1); 88 4 : parse("GRID",gridn[0]); 89 : std::vector<Value*> gridv; 90 2 : ActionWithArguments::interpretArgumentList( gridn, plumed.getActionSet(), this, gridv ); 91 : // Read the input arguments from the input file 92 : std::vector<std::string> argn; 93 4 : parseVector("ARG",argn); 94 : // Now get the arguments 95 2 : ActionWithGrid* ag = dynamic_cast<ActionWithGrid*>( gridv[0]->getPntrToAction() ); 96 2 : if( !ag ) { 97 0 : error("argument to GRID argument is not a grid"); 98 : } 99 2 : if( argn.size()==0 ) { 100 2 : std::vector<std::string> argn2( ag->getGridCoordinateNames() ); 101 2 : argn.resize( argn2.size() ); 102 4 : for(unsigned i=0; i<argn2.size(); ++i) { 103 : argn[i]=argn2[i]; 104 : } 105 2 : } 106 2 : if( argn.size()!=gridv[0]->getRank() ) { 107 0 : error("found wrong number of arguments in Evaluate function on grid"); 108 : } 109 : // Now use this information to create a gridobject 110 4 : if( ag->getGridCoordinatesObject().getGridType()=="fibonacci" ) { 111 0 : error("cannot interpolate on fibonacci sphere"); 112 : } 113 : // Now get the actual values we are using 114 : std::vector<Value*> vals; 115 2 : ActionWithArguments::interpretArgumentList( argn, plumed.getActionSet(), this, vals ); 116 2 : if( vals.size()==0 ) { 117 0 : error("found no input arguments to function"); 118 : } 119 2 : std::string allargs = gridn[0]; 120 4 : for(unsigned i=0; i<argn.size(); ++i) { 121 4 : allargs += "," + argn[i]; 122 : } 123 2 : function::FunctionShortcut<int>::createAction( this, vals, allargs ); 124 4 : } 125 : 126 : 127 : } 128 : }