LCOV - code coverage report
Current view: top level - function - Highest.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 24 28 85.7 %
Date: 2026-03-30 11:13:23 Functions: 3 5 60.0 %

          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 "core/ActionRegister.h"
      23             : #include "FunctionShortcut.h"
      24             : #include "FunctionOfScalar.h"
      25             : #include "FunctionOfVector.h"
      26             : #include "FunctionTemplateBase.h"
      27             : 
      28             : namespace PLMD {
      29             : namespace function {
      30             : 
      31             : //+PLUMEDOC FUNCTION HIGHEST
      32             : /*
      33             : This function can be used to find the highest colvar by magnitude in a set.
      34             : 
      35             : \par Examples
      36             : 
      37             : */
      38             : //+ENDPLUMEDOC
      39             : 
      40             : //+PLUMEDOC COLVAR HIGHEST_SCALAR
      41             : /*
      42             : Calculate the highest of a set of sclalar arguments
      43             : 
      44             : \par Examples
      45             : 
      46             : */
      47             : //+ENDPLUMEDOC
      48             : 
      49             : //+PLUMEDOC COLVAR HIGHEST_VECTOR
      50             : /*
      51             : Calculate the largest element in a vector of inputs
      52             : 
      53             : \par Examples
      54             : 
      55             : */
      56             : //+ENDPLUMEDOC
      57             : 
      58             : //+PLUMEDOC FUNCTION LOWEST
      59             : /*
      60             : This function can be used to find the lowest colvar by magnitude in a set.
      61             : 
      62             : \par Examples
      63             : 
      64             : */
      65             : //+ENDPLUMEDOC
      66             : 
      67             : //+PLUMEDOC COLVAR LOWEST_SCALAR
      68             : /*
      69             : Calculate the lowest of a set of sclalar arguments
      70             : 
      71             : \par Examples
      72             : 
      73             : */
      74             : //+ENDPLUMEDOC
      75             : 
      76             : //+PLUMEDOC COLVAR LOWEST_VECTOR
      77             : /*
      78             : Calculate the lowest element in a vector of inputs
      79             : 
      80             : \par Examples
      81             : 
      82             : */
      83             : //+ENDPLUMEDOC
      84             : 
      85         346 : class Highest : public FunctionTemplateBase {
      86             : private:
      87             :   bool min, scalar_out;
      88             : public:
      89             :   void registerKeywords( Keywords& keys ) override ;
      90             :   void read( ActionWithArguments* action ) override;
      91           0 :   bool zeroRank() const override {
      92          51 :     return scalar_out;
      93             :   }
      94           0 :   bool doWithTasks() const override {
      95        5341 :     return !scalar_out;
      96             :   }
      97             :   void calc( const ActionWithArguments* action, const std::vector<double>& args, std::vector<double>& vals, Matrix<double>& derivatives ) const override;
      98             : };
      99             : 
     100             : typedef FunctionShortcut<Highest> HighestShortcut;
     101             : PLUMED_REGISTER_ACTION(HighestShortcut,"HIGHEST")
     102             : PLUMED_REGISTER_ACTION(HighestShortcut,"LOWEST")
     103             : typedef FunctionOfScalar<Highest> ScalarHighest;
     104             : PLUMED_REGISTER_ACTION(ScalarHighest,"HIGHEST_SCALAR")
     105             : PLUMED_REGISTER_ACTION(ScalarHighest,"LOWEST_SCALAR")
     106             : typedef FunctionOfVector<Highest> VectorHighest;
     107             : PLUMED_REGISTER_ACTION(VectorHighest,"HIGHEST_VECTOR")
     108             : PLUMED_REGISTER_ACTION(VectorHighest,"LOWEST_VECTOR")
     109             : 
     110         240 : void Highest::registerKeywords( Keywords& keys ) {
     111         480 :   if( keys.getDisplayName().find("LOWEST") ) {
     112         188 :     keys.setValueDescription("the lowest of the input values");
     113             :   } else {
     114         292 :     keys.setValueDescription("the highest of the input values");
     115             :   }
     116         240 : }
     117             : 
     118          53 : void Highest::read( ActionWithArguments* action ) {
     119          53 :   min=action->getName().find("LOWEST")!=std::string::npos;
     120          53 :   if( !min ) {
     121          21 :     plumed_assert( action->getName().find("HIGHEST")!=std::string::npos );
     122             :   }
     123         130 :   for(unsigned i=0; i<action->getNumberOfArguments(); ++i) {
     124          77 :     if( action->getPntrToArgument(i)->isPeriodic() ) {
     125           0 :       action->error("Cannot sort periodic values (check argument "+ action->getPntrToArgument(i)->getName() +")");
     126             :     }
     127             :   }
     128          53 :   scalar_out = action->getNumberOfArguments()==1;
     129          53 :   if( scalar_out && action->getPntrToArgument(0)->getRank()==0 ) {
     130           0 :     action->error("sorting a single scalar is trivial");
     131             :   }
     132          53 : }
     133             : 
     134       41979 : void Highest::calc( const ActionWithArguments* action, const std::vector<double>& args, std::vector<double>& vals, Matrix<double>& derivatives ) const {
     135       41979 :   if( min ) {
     136       41845 :     vals[0] = *std::min_element(args.begin(), args.end());
     137       41845 :     derivatives(0,std::min_element(args.begin(), args.end()) - args.begin()) = 1;
     138             :   } else {
     139         134 :     vals[0] = *std::max_element(args.begin(), args.end());
     140         134 :     derivatives(0,std::max_element(args.begin(), args.end()) - args.begin()) = 1;
     141             :   }
     142       41979 : }
     143             : 
     144             : }
     145             : }
     146             : 
     147             : 

Generated by: LCOV version 1.16