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 "ActionRegister.h" 23 : #include "Function.h" 24 : 25 : namespace PLMD { 26 : namespace function { 27 : 28 : //+PLUMEDOC FUNCTION SORT 29 : /* 30 : This function can be used to sort colvars according to their magnitudes. 31 : 32 : \par Description of components 33 : 34 : This function sorts its arguments according to their magnitudes. The lowest argument will be 35 : labelled <em>label</em>.1, the second lowest will be labelled <em>label</em>.2 and so on. 36 : 37 : \par Examples 38 : 39 : The following input tells plumed to print the distance of the closest and of 40 : the farthest atoms to atom 1, chosen among atoms from 2 to 5 41 : \plumedfile 42 : d12: DISTANCE ATOMS=1,2 43 : d13: DISTANCE ATOMS=1,3 44 : d14: DISTANCE ATOMS=1,4 45 : d15: DISTANCE ATOMS=1,5 46 : sort: SORT ARG=d12,d13,d14,d15 47 : PRINT ARG=sort.1,sort.4 48 : \endplumedfile 49 : 50 : */ 51 : //+ENDPLUMEDOC 52 : 53 : 54 : class Sort : 55 : public Function { 56 : public: 57 : explicit Sort(const ActionOptions&); 58 : void calculate() override; 59 : static void registerKeywords(Keywords& keys); 60 : }; 61 : 62 : 63 13808 : PLUMED_REGISTER_ACTION(Sort,"SORT") 64 : 65 16 : void Sort::registerKeywords(Keywords& keys) { 66 16 : Function::registerKeywords(keys); 67 16 : keys.use("ARG"); 68 16 : useCustomisableComponents(keys); 69 16 : } 70 : 71 12 : Sort::Sort(const ActionOptions&ao): 72 : Action(ao), 73 12 : Function(ao) { 74 35 : for(unsigned i=0; i<getNumberOfArguments(); ++i) { 75 : std::string s; 76 24 : Tools::convert(i+1,s); 77 24 : if(getPntrToArgument(i)->isPeriodic()) { 78 3 : error("Cannot sort periodic values (check argument "+s+")"); 79 : } 80 23 : addComponentWithDerivatives(s); 81 23 : getPntrToComponent(i)->setNotPeriodic(); 82 : } 83 11 : checkRead(); 84 : 85 13 : } 86 : 87 11 : void Sort::calculate() { 88 11 : std::vector<std::pair<double,int> > vals(getNumberOfArguments()); 89 54 : for(unsigned i=0; i<getNumberOfArguments(); ++i) { 90 43 : vals[i].first=getArgument(i); 91 : // In this manner I remember from which argument the component depends: 92 43 : vals[i].second=i; 93 : } 94 : // STL sort sorts based on first element (value) then second (index) 95 11 : std::sort(vals.begin(),vals.end()); 96 54 : for(int i=0; i<getNumberOfComponents(); ++i) { 97 43 : Value* v=getPntrToComponent(i); 98 43 : v->set(vals[i].first); 99 43 : setDerivative(v,vals[i].second,1.0); 100 : } 101 11 : } 102 : 103 : } 104 : } 105 : 106 :