LCOV - code coverage report
Current view: top level - tools - CheckInRange.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 33 53 62.3 %
Date: 2025-03-25 09:33:27 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2011-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 "CheckInRange.h"
      23             : #include "Tools.h"
      24             : 
      25             : namespace PLMD {
      26             : 
      27          43 : bool CheckInRange::setBounds( const unsigned& n, const std::vector<std::string>& str_lower, const std::vector<std::string>& str_upper, std::string& errors ) {
      28          43 :   if( str_upper.size()!=n && str_upper.size()>0 ) {
      29             :     errors="wrong number of arguments for LESS_THAN_OR_EQUAL keyword";
      30           0 :     return false;
      31             :   }
      32          43 :   if( str_lower.size()!=n && str_lower.size()>0 ) {
      33             :     errors="wrong number of arguments for GREATER_THAN_OR_EQUAL keyword";
      34           0 :     return false;
      35             :   }
      36          43 :   if( str_upper.size()>0 && str_lower.size()>0 ) {
      37          14 :     lower.resize( str_lower.size() );
      38          14 :     upper.resize( str_upper.size() );
      39          28 :     for(unsigned i=0; i<upper.size(); ++i) {
      40          14 :       if( str_lower[i]=="none" ) {
      41           0 :         lower[i] = -std::numeric_limits<double>::max();
      42             :       } else {
      43          14 :         Tools::convert( str_lower[i], lower[i] );
      44             :       }
      45          14 :       if( str_upper[i]=="none" ) {
      46           0 :         upper[i] = std::numeric_limits<double>::max();
      47             :       } else {
      48          14 :         Tools::convert( str_upper[i], upper[i] );
      49             :       }
      50             :     }
      51          29 :   } else if( str_upper.size()>0 ) {
      52           0 :     upper.resize( str_upper.size() );
      53           0 :     for(unsigned i=0; i<upper.size(); ++i) {
      54           0 :       if( str_upper[i]=="none" ) {
      55           0 :         upper[i] = std::numeric_limits<double>::max();
      56             :       } else {
      57           0 :         Tools::convert( str_upper[i], upper[i] );
      58             :       }
      59             :     }
      60          29 :   } else if( str_lower.size()>0 ) {
      61           3 :     lower.resize( str_lower.size() );
      62           6 :     for(unsigned i=0; i<lower.size(); ++i) {
      63           3 :       if( str_lower[i]=="none" ) {
      64           0 :         lower[i] = -std::numeric_limits<double>::max();
      65             :       } else {
      66           3 :         Tools::convert( str_lower[i], lower[i] );
      67             :       }
      68             :     }
      69             :   }
      70             :   return true;
      71             : }
      72             : 
      73          43 : bool CheckInRange::wereSet() const {
      74          43 :   return lower.size()>0 || upper.size()>0;
      75             : }
      76             : 
      77          17 : std::string CheckInRange::report( const std::vector<std::string>& a ) const {
      78          17 :   if( upper.size()>0 && lower.size()>0 ) {
      79             :     std::string str_l, str_u;
      80          14 :     Tools::convert( upper[0], str_u );
      81          14 :     Tools::convert( lower[0], str_l );
      82          28 :     std::string out="only printing indices of atoms that have " + str_l + " <= " + a[0] + " <=" + str_u;
      83          14 :     for(unsigned i=1; i<upper.size(); ++i) {
      84           0 :       Tools::convert( upper[i], str_u );
      85           0 :       Tools::convert( lower[i], str_l );
      86           0 :       out += " and " + str_l + " <= " + a[i] + " <=" + str_u;
      87             :     }
      88             :     return out;
      89             :   }
      90           3 :   if( upper.size()>0 ) {
      91             :     std::string str_u;
      92           0 :     Tools::convert( upper[0], str_u );
      93           0 :     std::string out="only printing indices of atoms that have " + a[0] + " <= " + str_u;
      94           0 :     for(unsigned i=1; i<upper.size(); ++i) {
      95           0 :       Tools::convert( upper[i], str_u );
      96           0 :       out += " and " + a[i] + " <= " + str_u;
      97             :     }
      98             :     return out;
      99             :   }
     100             :   std::string str_l;
     101           3 :   Tools::convert( lower[0], str_l );
     102           6 :   std::string out="only printing indices of atoms that have " + str_l + " <= " + a[0];
     103           3 :   for(unsigned i=1; i<lower.size(); ++i) {
     104           0 :     Tools::convert( lower[i], str_l );
     105           0 :     out += " and " + str_l + " <= " + a[i];
     106             :   }
     107             :   return out;
     108             : }
     109             : 
     110      139130 : bool CheckInRange::check( const std::vector<double>& vals ) const {
     111      191478 :   for(unsigned j=0; j<vals.size(); ++j) {
     112       67788 :     if( upper.size()>0 && vals[j]>upper[j] ) {
     113             :       return false;
     114             :     }
     115       58176 :     if( lower.size()>0 && vals[j]<lower[j] ) {
     116             :       return false;
     117             :     }
     118             :   }
     119             :   return true;
     120             : }
     121             : 
     122             : }

Generated by: LCOV version 1.16