All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Sort.cpp
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 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-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
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 largest argument will be
41 labelled <em>label</em>.1, the second largest will be labelled <em>label</em>.2 and so on.
42 
43 \par Examples
44 The following input tells plumed to print the distance of the closest and of
45 the farthest atoms to atom 1, chosen among atoms from 2 to 5
46 \verbatim
47 d12: DISTANCE ATOMS=1,2
48 d13: DISTANCE ATOMS=1,3
49 d14: DISTANCE ATOMS=1,4
50 d15: DISTANCE ATOMS=1,5
51 sort: SORT ARG=d12,d13,d14,d15
52 PRINT ARG=sort.1,sort.4
53 \endverbatim
54 (See also \ref PRINT and \ref DISTANCE).
55 
56 */
57 //+ENDPLUMEDOC
58 
59 
60 class Sort :
61  public Function
62 {
63 public:
64  Sort(const ActionOptions&);
65  void calculate();
66  static void registerKeywords(Keywords& keys);
67 };
68 
69 
70 PLUMED_REGISTER_ACTION(Sort,"SORT")
71 
72 void Sort::registerKeywords(Keywords& keys){
73  Function::registerKeywords(keys);
74  keys.use("ARG");
75  ActionWithValue::useCustomisableComponents(keys);
76 }
77 
78 Sort::Sort(const ActionOptions&ao):
79 Action(ao),
80 Function(ao)
81 {
82  for(unsigned i=0;i<getNumberOfArguments();++i){
83  string s;
84  Tools::convert(i+1,s);
85  if(getPntrToArgument(i)->isPeriodic())
86  error("Cannot sort periodic values (check argument "+s+")");
89  }
90  checkRead();
91 
92 }
93 
95  vector<pair<double,int> > vals(getNumberOfArguments());
96  for(unsigned i=0;i<getNumberOfArguments();++i){
97  vals[i].first=getArgument(i);
98 // In this manner I remember from which argument the component depends:
99  vals[i].second=i;
100  }
101 // STL sort sorts based on first element (value) then second (index)
102  sort(vals.begin(),vals.end());
103  for(unsigned i=0;i<getNumberOfComponents();++i){
105  v->set(vals[i].first);
106  setDerivative(v,vals[i].second,1.0);
107  }
108 }
109 
110 }
111 }
112 
113 
A class for holding the value of a function together with its derivatives.
Definition: Value.h:46
static bool convert(const std::string &str, double &t)
Convert a string to a double, reading it.
Definition: Tools.cpp:74
void error(const std::string &msg) const
Crash calculation and print documentation.
Definition: Action.cpp:195
void checkRead()
Check if Action was properly read.
Definition: Action.cpp:161
STL namespace.
Value * getPntrToArgument(const unsigned n)
Return a pointer to specific argument.
void set(double)
Set the value of the function.
Definition: Value.h:174
void addComponentWithDerivatives(const std::string &name)
Definition: Function.cpp:58
This class holds the keywords and their documentation.
Definition: Keywords.h:36
void setNotPeriodic()
Set the function not periodic.
Definition: Value.cpp:87
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
Base class for all the input Actions.
Definition: Action.h:60
double getArgument(const unsigned n) const
Returns the value of an argument.
void calculate()
Calculate an Action.
Definition: Sort.cpp:94
int getNumberOfComponents() const
Returns the number of values defined.
This is the abstract base class to use for implementing new CV function, within it there is informati...
Definition: Function.h:37
Value * getPntrToComponent(int i)
Return a pointer to the component by index.
unsigned getNumberOfArguments() const
Returns the number of arguments.
void setDerivative(int, double)
Definition: Function.h:59
Provides the keyword SORT
Definition: Sort.cpp:60