LCOV - code coverage report
Current view: top level - function - Sort.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 28 28 100.0 %
Date: 2020-11-18 11:20:57 Functions: 10 11 90.9 %

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

Generated by: LCOV version 1.13