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