Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2012-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 : #include "core/ActionRegister.h" 23 : #include "FunctionShortcut.h" 24 : #include "FunctionOfScalar.h" 25 : #include "FunctionOfVector.h" 26 : #include "FunctionTemplateBase.h" 27 : 28 : namespace PLMD { 29 : namespace function { 30 : 31 : //+PLUMEDOC FUNCTION HIGHEST 32 : /* 33 : This function can be used to find the highest colvar by magnitude in a set. 34 : 35 : \par Examples 36 : 37 : */ 38 : //+ENDPLUMEDOC 39 : 40 : //+PLUMEDOC COLVAR HIGHEST_SCALAR 41 : /* 42 : Calculate the highest of a set of sclalar arguments 43 : 44 : \par Examples 45 : 46 : */ 47 : //+ENDPLUMEDOC 48 : 49 : //+PLUMEDOC COLVAR HIGHEST_VECTOR 50 : /* 51 : Calculate the largest element in a vector of inputs 52 : 53 : \par Examples 54 : 55 : */ 56 : //+ENDPLUMEDOC 57 : 58 : //+PLUMEDOC FUNCTION LOWEST 59 : /* 60 : This function can be used to find the lowest colvar by magnitude in a set. 61 : 62 : \par Examples 63 : 64 : */ 65 : //+ENDPLUMEDOC 66 : 67 : //+PLUMEDOC COLVAR LOWEST_SCALAR 68 : /* 69 : Calculate the lowest of a set of sclalar arguments 70 : 71 : \par Examples 72 : 73 : */ 74 : //+ENDPLUMEDOC 75 : 76 : //+PLUMEDOC COLVAR LOWEST_VECTOR 77 : /* 78 : Calculate the lowest element in a vector of inputs 79 : 80 : \par Examples 81 : 82 : */ 83 : //+ENDPLUMEDOC 84 : 85 316 : class Highest : public FunctionTemplateBase { 86 : private: 87 : bool min, scalar_out; 88 : public: 89 : void registerKeywords( Keywords& keys ) override ; 90 : void read( ActionWithArguments* action ) override; 91 48 : bool zeroRank() const override { return scalar_out; } 92 5338 : bool doWithTasks() const override { return !scalar_out; } 93 : void calc( const ActionWithArguments* action, const std::vector<double>& args, std::vector<double>& vals, Matrix<double>& derivatives ) const override; 94 : }; 95 : 96 : typedef FunctionShortcut<Highest> HighestShortcut; 97 : PLUMED_REGISTER_ACTION(HighestShortcut,"HIGHEST") 98 : PLUMED_REGISTER_ACTION(HighestShortcut,"LOWEST") 99 : typedef FunctionOfScalar<Highest> ScalarHighest; 100 : PLUMED_REGISTER_ACTION(ScalarHighest,"HIGHEST_SCALAR") 101 : PLUMED_REGISTER_ACTION(ScalarHighest,"LOWEST_SCALAR") 102 : typedef FunctionOfVector<Highest> VectorHighest; 103 : PLUMED_REGISTER_ACTION(VectorHighest,"HIGHEST_VECTOR") 104 : PLUMED_REGISTER_ACTION(VectorHighest,"LOWEST_VECTOR") 105 : 106 216 : void Highest::registerKeywords( Keywords& keys ) { 107 520 : if( keys.getDisplayName().find("LOWEST") ) keys.setValueDescription("scalar","the lowest of the input values"); 108 256 : else keys.setValueDescription("scalar","the highest of the input values"); 109 216 : } 110 : 111 50 : void Highest::read( ActionWithArguments* action ) { 112 50 : min=action->getName().find("LOWEST")!=std::string::npos; if( !min ) plumed_assert( action->getName().find("HIGHEST")!=std::string::npos ); 113 121 : for(unsigned i=0; i<action->getNumberOfArguments(); ++i) { 114 71 : if( action->getPntrToArgument(i)->isPeriodic() ) action->error("Cannot sort periodic values (check argument "+ action->getPntrToArgument(i)->getName() +")"); 115 : } 116 50 : scalar_out = action->getNumberOfArguments()==1; 117 50 : if( scalar_out && action->getPntrToArgument(0)->getRank()==0 ) action->error("sorting a single scalar is trivial"); 118 50 : } 119 : 120 32889 : void Highest::calc( const ActionWithArguments* action, const std::vector<double>& args, std::vector<double>& vals, Matrix<double>& derivatives ) const { 121 32889 : if( min ) { 122 32755 : vals[0] = *std::min_element(args.begin(), args.end()); 123 32755 : derivatives(0,std::min_element(args.begin(), args.end()) - args.begin()) = 1; 124 : } else { 125 134 : vals[0] = *std::max_element(args.begin(), args.end()); 126 134 : derivatives(0,std::max_element(args.begin(), args.end()) - args.begin()) = 1; 127 : } 128 32889 : } 129 : 130 : } 131 : } 132 : 133 :