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 : { 57 : public: 58 : explicit Sort(const ActionOptions&); 59 : void calculate() override; 60 : static void registerKeywords(Keywords& keys); 61 : }; 62 : 63 : 64 10442 : PLUMED_REGISTER_ACTION(Sort,"SORT") 65 : 66 13 : void Sort::registerKeywords(Keywords& keys) { 67 13 : Function::registerKeywords(keys); 68 13 : keys.use("ARG"); 69 13 : useCustomisableComponents(keys); 70 13 : } 71 : 72 12 : Sort::Sort(const ActionOptions&ao): 73 : Action(ao), 74 12 : Function(ao) 75 : { 76 35 : for(unsigned i=0; i<getNumberOfArguments(); ++i) { 77 : std::string s; 78 24 : Tools::convert(i+1,s); 79 24 : if(getPntrToArgument(i)->isPeriodic()) 80 3 : error("Cannot sort periodic values (check argument "+s+")"); 81 23 : addComponentWithDerivatives(s); 82 23 : getPntrToComponent(i)->setNotPeriodic(); 83 : } 84 11 : checkRead(); 85 : 86 13 : } 87 : 88 11 : void Sort::calculate() { 89 11 : std::vector<std::pair<double,int> > vals(getNumberOfArguments()); 90 54 : for(unsigned i=0; i<getNumberOfArguments(); ++i) { 91 43 : vals[i].first=getArgument(i); 92 : // In this manner I remember from which argument the component depends: 93 43 : vals[i].second=i; 94 : } 95 : // STL sort sorts based on first element (value) then second (index) 96 11 : std::sort(vals.begin(),vals.end()); 97 54 : for(int i=0; i<getNumberOfComponents(); ++i) { 98 43 : Value* v=getPntrToComponent(i); 99 43 : v->set(vals[i].first); 100 43 : setDerivative(v,vals[i].second,1.0); 101 : } 102 11 : } 103 : 104 : } 105 : } 106 : 107 :