Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2011-2017 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 "LessThan.h" 23 : #include "FunctionShortcut.h" 24 : #include "FunctionOfVector.h" 25 : #include "FunctionOfMatrix.h" 26 : #include "core/ActionRegister.h" 27 : 28 : #include <cmath> 29 : 30 : namespace PLMD { 31 : namespace function { 32 : 33 : //+PLUMEDOC FUNCTION LESS_THAN 34 : /* 35 : Use a switching function to determine how many of the input variables are less than a certain cutoff. 36 : 37 : \par Examples 38 : 39 : */ 40 : //+ENDPLUMEDOC 41 : 42 : //+PLUMEDOC FUNCTION LESS_THAN_VECTOR 43 : /* 44 : Use a switching function to determine how many components of the vector are less than a certain cutoff. 45 : 46 : \par Examples 47 : 48 : */ 49 : //+ENDPLUMEDOC 50 : 51 : //+PLUMEDOC COLVAR LESS_THAN_MATRIX 52 : /* 53 : Transform all the elements of a matrix using a switching function that is one when the input value is smaller than a threshold 54 : 55 : \par Examples 56 : 57 : */ 58 : //+ENDPLUMEDOC 59 : 60 : typedef FunctionShortcut<LessThan> LessThanShortcut; 61 : PLUMED_REGISTER_ACTION(LessThanShortcut,"LESS_THAN") 62 : typedef FunctionOfVector<LessThan> VectorLessThan; 63 : PLUMED_REGISTER_ACTION(VectorLessThan,"LESS_THAN_VECTOR") 64 : typedef FunctionOfMatrix<LessThan> MatrixLessThan; 65 : PLUMED_REGISTER_ACTION(MatrixLessThan,"LESS_THAN_MATRIX") 66 : 67 274 : void LessThan::registerKeywords(Keywords& keys) { 68 548 : keys.add("compulsory","NN","6","The n parameter of the switching function "); 69 548 : keys.add("compulsory","MM","0","The m parameter of the switching function; 0 implies 2*NN"); 70 548 : keys.add("compulsory","D_0","0.0","The d_0 parameter of the switching function"); 71 548 : keys.add("compulsory","R_0","The r_0 parameter of the switching function"); 72 548 : keys.add("optional","SWITCH","This keyword is used if you want to employ an alternative to the continuous swiching function defined above. " 73 : "The following provides information on the \\ref switchingfunction that are available. " 74 : "When this keyword is present you no longer need the NN, MM, D_0 and R_0 keywords."); 75 548 : keys.addFlag("SQUARED",false,"is the input quantity the square of the value that you would like to apply the switching function to"); 76 548 : keys.setValueDescription("scalar/vector/matrix","a function that is one if the input is less than a threshold"); 77 274 : } 78 : 79 67 : void LessThan::read( ActionWithArguments* action ) { 80 67 : if( action->getNumberOfArguments()!=1 ) action->error("should only be one argument to less_than actions"); 81 67 : if( action->getPntrToArgument(0)->isPeriodic() ) action->error("cannot use this function on periodic functions"); 82 : 83 : 84 : std::string sw,errors; 85 134 : action->parse("SWITCH",sw); 86 67 : if(sw.length()>0) { 87 67 : switchingFunction.set(sw,errors); 88 67 : if( errors.length()!=0 ) action->error("problem reading SWITCH keyword : " + errors ); 89 : } else { 90 0 : int nn=6; int mm=0; double d0=0.0; double r0=0.0; action->parse("R_0",r0); 91 0 : if(r0<=0.0) action->error("R_0 should be explicitly specified and positive"); 92 0 : action->parse("D_0",d0); action->parse("NN",nn); action->parse("MM",mm); 93 0 : switchingFunction.set(nn,mm,r0,d0); 94 : } 95 67 : action->log<<" using switching function with cutoff "<<switchingFunction.description()<<"\n"; 96 67 : action->parseFlag("SQUARED",squared); 97 67 : if( squared ) action->log<<" input quantity is square of quantity that switching function acts upon\n"; 98 67 : } 99 : 100 187997 : void LessThan::calc( const ActionWithArguments* action, const std::vector<double>& args, std::vector<double>& vals, Matrix<double>& derivatives ) const { 101 : plumed_dbg_assert( args.size()==1 ); 102 187997 : if( squared ) vals[0] = switchingFunction.calculateSqr( args[0], derivatives(0,0) ); 103 187997 : else vals[0] = switchingFunction.calculate( args[0], derivatives(0,0) ); 104 187997 : derivatives(0,0) = args[0]*derivatives(0,0); 105 187997 : } 106 : 107 : } 108 : } 109 : 110 :