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 "function/FunctionShortcut.h" 23 : #include "function/FunctionOfScalar.h" 24 : #include "function/FunctionOfVector.h" 25 : #include "core/ActionRegister.h" 26 : #include "function/FunctionTemplateBase.h" 27 : 28 : #include <cmath> 29 : 30 : namespace PLMD { 31 : namespace refdist { 32 : 33 : //+PLUMEDOC FUNCTION DIFFERENCE 34 : /* 35 : Calculate the differences between two scalars 36 : 37 : \par Examples 38 : 39 : */ 40 : //+ENDPLUMEDOC 41 : 42 : //+PLUMEDOC FUNCTION DIFFERENCE_SCALAR 43 : /* 44 : Calculate the differences between two scalars 45 : 46 : \par Examples 47 : 48 : */ 49 : //+ENDPLUMEDOC 50 : 51 : //+PLUMEDOC FUNCTION DIFFERENCE_VECTOR 52 : /* 53 : Calculate the differences between the elements of two vectors 54 : 55 : \par Examples 56 : 57 : */ 58 : //+ENDPLUMEDOC 59 : 60 : 61 1105 : class Difference : public function::FunctionTemplateBase { 62 : private: 63 : bool periodic; 64 : std::string min0, max0; 65 : public: 66 : void registerKeywords(Keywords& keys) override ; 67 : void read( ActionWithArguments* action ) override; 68 : void setPeriodicityForOutputs( ActionWithValue* action ) override; 69 : void calc( const ActionWithArguments* action, const std::vector<double>& args, std::vector<double>& vals, Matrix<double>& derivatives ) const override; 70 : }; 71 : 72 : 73 : typedef function::FunctionShortcut<Difference> DifferenceShortcut; 74 : PLUMED_REGISTER_ACTION(DifferenceShortcut,"DIFFERENCE") 75 : typedef function::FunctionOfScalar<Difference> ScalarDifference; 76 : PLUMED_REGISTER_ACTION(ScalarDifference,"DIFFERENCE_SCALAR") 77 : typedef function::FunctionOfVector<Difference> VectorDifference; 78 : PLUMED_REGISTER_ACTION(VectorDifference,"DIFFERENCE_VECTOR") 79 : 80 737 : void Difference::registerKeywords(Keywords& keys) { 81 1474 : keys.setValueDescription("scalar/vector","a function that measures the difference"); 82 737 : } 83 : 84 184 : void Difference::read( ActionWithArguments* action ) { 85 184 : if( action->getNumberOfArguments()!=2 ) action->error("should be two arguments to this action"); 86 184 : if( action->getPntrToArgument(0)->getRank()==action->getPntrToArgument(1)->getRank() ) { 87 51 : std::vector<unsigned> shape( action->getPntrToArgument(0)->getShape() ); 88 61 : for(unsigned i=0; i<shape.size(); ++i) { 89 10 : if( shape[i]!=action->getPntrToArgument(1)->getShape()[i] ) action->error("shapes of input actions do not match"); 90 : } 91 : } 92 : 93 184 : periodic=false; 94 184 : if( action->getPntrToArgument(0)->isPeriodic() ) { 95 43 : periodic=true; action->getPntrToArgument(0)->getDomain( min0, max0 ); 96 43 : if( !action->getPntrToArgument(1)->isConstant() && !action->getPntrToArgument(1)->isPeriodic() ) { 97 0 : action->error("period for input variables " + action->getPntrToArgument(0)->getName() + " and " + action->getPntrToArgument(1)->getName() + " should be the same 0"); 98 : } 99 43 : if( !action->getPntrToArgument(1)->isConstant() ) { 100 1 : std::string min1, max1; action->getPntrToArgument(1)->getDomain( min1, max1 ); 101 1 : if( min0!=min0 || max0!=max1 ) action->error("domain for input variables should be the same"); 102 42 : } else action->getPntrToArgument(1)->setDomain( min0, max0 ); 103 141 : } else if( action->getPntrToArgument(1)->isPeriodic() ) { 104 0 : periodic=true; action->getPntrToArgument(1)->getDomain( min0, max0 ); 105 0 : if( !action->getPntrToArgument(1)->isConstant() ) { 106 0 : action->error("period for input variables " + action->getPntrToArgument(0)->getName() + " and " + action->getPntrToArgument(1)->getName() + " should be the same 1"); 107 0 : } else action->getPntrToArgument(0)->setDomain( min0, max0 ); 108 : } 109 184 : } 110 : 111 184 : void Difference::setPeriodicityForOutputs( ActionWithValue* action ) { 112 184 : if( periodic ) action->setPeriodic( min0, max0 ); 113 141 : else action->setNotPeriodic(); 114 184 : } 115 : 116 2877849 : void Difference::calc( const ActionWithArguments* action, const std::vector<double>& args, std::vector<double>& vals, Matrix<double>& derivatives ) const { 117 2877849 : plumed_dbg_assert( args.size()==2 ); vals[0] = action->getPntrToArgument(0)->difference( args[1], args[0] ); derivatives(0,0) = 1.0; derivatives(0,1)=-1; 118 2877849 : } 119 : 120 : } 121 : } 122 : 123 :