LCOV - code coverage report
Current view: top level - gridtools - EvaluateFunctionOnGrid.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 26 26 100.0 %
Date: 2024-10-18 13:59:31 Functions: 2 3 66.7 %

          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           8 :   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           4 :   EvaluateGridFunction ii; ii.registerKeywords( keys );
      78           8 :   keys.addActionNameSuffix("_SCALAR"); keys.addActionNameSuffix("_VECTOR");
      79           4 : }
      80             : 
      81           2 : EvaluateFunctionOnGrid::EvaluateFunctionOnGrid(const ActionOptions&ao):
      82             :   Action(ao),
      83           2 :   ActionShortcut(ao)
      84             : {
      85             :   // Get the grid that we are evaluating here
      86           4 :   std::vector<std::string> gridn(1); parse("GRID",gridn[0]);
      87           2 :   std::vector<Value*> gridv; ActionWithArguments::interpretArgumentList( gridn, plumed.getActionSet(), this, gridv );
      88             :   // Read the input arguments from the input file
      89           4 :   std::vector<std::string> argn; parseVector("ARG",argn);
      90             :   // Now get the arguments
      91           2 :   ActionWithGrid* ag = dynamic_cast<ActionWithGrid*>( gridv[0]->getPntrToAction() );
      92           2 :   if( !ag ) error("argument to GRID argument is not a grid");
      93           2 :   if( argn.size()==0 ) {
      94           2 :     std::vector<std::string> argn2( ag->getGridCoordinateNames() );
      95           4 :     argn.resize( argn2.size() ); for(unsigned i=0; i<argn2.size(); ++i) argn[i]=argn2[i];
      96           2 :   }
      97           2 :   if( argn.size()!=gridv[0]->getRank() ) error("found wrong number of arguments in Evaluate function on grid");
      98             :   // Now use this information to create a gridobject
      99           4 :   if( ag->getGridCoordinatesObject().getGridType()=="fibonacci" ) error("cannot interpolate on fibonacci sphere");
     100             :   // Now get the actual values we are using
     101           2 :   std::vector<Value*> vals; ActionWithArguments::interpretArgumentList( argn, plumed.getActionSet(), this, vals );
     102           2 :   if( vals.size()==0 ) error("found no input arguments to function");
     103           4 :   std::string allargs = gridn[0]; for(unsigned i=0; i<argn.size(); ++i) allargs += "," + argn[i];
     104           2 :   function::FunctionShortcut<int>::createAction( this, vals, allargs );
     105           4 : }
     106             : 
     107             : 
     108             : }
     109             : }

Generated by: LCOV version 1.16