1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2    Copyright (c) 2012-2021 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 #ifndef __PLUMED_tools_HistogramBead_h
23 #define __PLUMED_tools_HistogramBead_h
24 
25 #include <string>
26 #include <vector>
27 #include "Exception.h"
28 #include "Tools.h"
29 
30 namespace PLMD {
31 
32 class Keywords;
33 class Log;
34 
35 /**
36 \ingroup TOOLBOX
37 A class for calculating whether or not values are within a given range using : \f$ \sum_i \int_a^b G( s_i, \sigma*(b-a) ) \f$
38 */
39 
40 class HistogramBead {
41 private:
42   bool init;
43   double lowb;
44   double highb;
45   double width;
46   double cutoff;
47   enum {gaussian,triangular} type;
48   enum {unset,periodic,notperiodic} periodicity;
49   double min, max, max_minus_min, inv_max_minus_min;
50   double difference( const double& d1, const double& d2 ) const ;
51 public:
52   static void registerKeywords( Keywords& keys );
53   static void generateBins( const std::string& params, std::vector<std::string>& bins );
54   HistogramBead();
55   std::string description() const ;
56   bool hasBeenSet() const;
57   void isNotPeriodic();
58   void isPeriodic( const double& mlow, const double& mhigh );
59   void setKernelType( const std::string& ktype );
60   void set(const std::string& params, std::string& errormsg);
61   void set(double l, double h, double w);
62   double calculate(double x, double&df) const;
63   double calculateWithCutoff( double x, double& df ) const;
64   double lboundDerivative( const double& x ) const;
65   double uboundDerivative( const double& x ) const;
66   double getlowb() const ;
67   double getbigb() const ;
68   double getCutoff() const ;
69 };
70 
71 inline
hasBeenSet()72 bool HistogramBead::hasBeenSet() const {
73   return init;
74 }
75 
76 inline
isNotPeriodic()77 void HistogramBead::isNotPeriodic() {
78   periodicity=notperiodic;
79 }
80 
81 inline
isPeriodic(const double & mlow,const double & mhigh)82 void HistogramBead::isPeriodic( const double& mlow, const double& mhigh ) {
83   periodicity=periodic; min=mlow; max=mhigh;
84   max_minus_min=max-min;
85   plumed_massert(max_minus_min>0, "your function has a very strange domain?");
86   inv_max_minus_min=1.0/max_minus_min;
87 }
88 
89 inline
getlowb()90 double HistogramBead::getlowb() const { return lowb; }
91 
92 inline
getbigb()93 double HistogramBead::getbigb() const { return highb; }
94 
95 inline
getCutoff()96 double HistogramBead::getCutoff() const { return cutoff*width; }
97 
98 inline
difference(const double & d1,const double & d2)99 double HistogramBead::difference( const double& d1, const double& d2 ) const {
100   if(periodicity==notperiodic) {
101     return d2-d1;
102   } else if(periodicity==periodic) {
103     // Make sure the point is in the target range
104     double newx=d1*inv_max_minus_min;
105     newx=Tools::pbc(newx);
106     newx*=max_minus_min;
107     return d2-newx;
108   } else plumed_merror("periodicty was not set");
109   return 0;
110 }
111 
112 }
113 
114 #endif
115