SST  10.0.0
StructuralSimulationToolkit
unitAlgebra.h
1 // -*- c++ -*-
2 
3 // Copyright 2009-2020 NTESS. Under the terms
4 // of Contract DE-NA0003525 with NTESS, the U.S.
5 // Government retains certain rights in this software.
6 //
7 // Copyright (c) 2009-2020, NTESS
8 // All rights reserved.
9 //
10 // This file is part of the SST software package. For license
11 // information, see the LICENSE file in the top level directory of the
12 // distribution.
13 
14 #ifndef SST_CORE_UNITALGEBRA_H
15 #define SST_CORE_UNITALGEBRA_H
16 
17 #include "sst/core/sst_types.h"
18 #include "sst/core/serialization/serializable.h"
19 #include "sst/core/serialization/serializer.h"
20 
21 #include <string>
22 #include <map>
23 #include <vector>
24 #include <mutex>
25 
26 #include "sst/core/warnmacros.h"
27 #include "sst/core/decimal_fixedpoint.h"
28 
29 
30 namespace SST {
31 
32 // typedef decimal_fixedpoint<3,3> sst_dec_float;
33 typedef decimal_fixedpoint<3,3> sst_big_num;
34 
35 /**
36  * Helper class internal to UnitAlgebra.
37  *
38  * Contains information on valid units
39  */
40 class Units {
41 
42  typedef uint8_t unit_id_t;
43 
44 private:
45  friend class UnitAlgebra;
46 
47  // Static data members and functions
48  static std::recursive_mutex unit_lock;
49  static std::map<std::string,unit_id_t> valid_base_units;
50  static std::map<std::string,std::pair<Units,sst_big_num> > valid_compound_units;
51  static std::map<unit_id_t,std::string> unit_strings;
52  static unit_id_t count;
53  static bool initialized;
54 
55  static bool initialize();
56 
57  // Non-static data members and functions
58  std::vector<unit_id_t> numerator;
59  std::vector<unit_id_t> denominator;
60 
61  void reduce();
62  // Used in constructor to incrementally build up unit from string
63  void addUnit(const std::string&, sst_big_num& multiplier, bool invert);
64 
65 public:
66  // Static data members and functions
67  /** Create a new Base Unit type */
68  static void registerBaseUnit(const std::string& u);
69  /** Create a new Compound Unit type */
70  static void registerCompoundUnit(const std::string& u, const std::string& v);
71 
72  // Non-static data members and functions
73  /** Create a new instantiation of a Units with a base unit string, and multiplier
74  * \param units String representing the new unit
75  * \param multiplier Value by which to multiply to get to this unit
76  */
77  Units(const std::string& units, sst_big_num& multiplier);
78  Units() {}
79  virtual ~Units() {}
80 
81  /** Copy constructor */
82  Units(const Units&) = default;
83 
84  /** Assignment operator */
85  Units& operator= (const Units& v);
86  /** Self-multiplication operator */
87  Units& operator*= (const Units& v);
88  /** Self-division operator */
89  Units& operator/= (const Units& v);
90  /** Equality Operator */
91  bool operator== (const Units &lhs) const;
92  /** Inequality Operator */
93  bool operator!= (const Units &lhs) const {return !(*this == lhs);}
94  /** Perform a reciprocal operation. Numerator and Denominator swap. */
95  Units& invert();
96 
97  /** Return a String representation if this Unit */
98  std::string toString() const;
99 };
100 
101 /**
102  * Performs Unit math in full precision
103  *
104  * Allows operations such as multiplying a frequency by 2.
105  *
106  */
108  public SST::Core::Serialization::serializable_type<UnitAlgebra> {
109 private:
110  Units unit;
111  sst_big_num value;
112 
113  static std::string trim(const std::string& str);
114  void init(const std::string& val);
115 
116 public:
117  UnitAlgebra() {}
118  /**
119  Create a new UnitAlgebra instance, and pre-populate with a parsed value.
120 
121  \param val Value to parse. It is of the following format:
122  \code
123  val := NUMBER( )?UNITS
124  NUMBER := (-)?[0-9]+(.[0-9]+)?
125  UNITS := UNITGROUP(/UNITGROUP)
126  UNITGROUP := UNIT(-UNIT)*
127  UNIT := (SIPREFIX)?(BASEUNIT|COMPUNIT)
128  SIPREFIX := {a,f,p,n,u,m,[kKMGTPE]i?}
129  BASEUNIT := {s,B,b,events}
130  COMPUNIT := {Hz,hz,Bps,bps,event}
131  \endcode
132  */
133  UnitAlgebra(const std::string& val);
134  virtual ~UnitAlgebra();
135 
136  /** Copy constructor */
137  UnitAlgebra(const UnitAlgebra&) = default;
138 
139  /** Print to an ostream the value */
140  void print(std::ostream& stream);
141  /** Print to an ostream the value
142  * Formats the number using SI-prefixes
143  */
144  void printWithBestSI(std::ostream& stream);
145  /** Return a string representation of this value */
146  std::string toString() const;
147  /** Return a string representation of this value
148  * Formats the number using SI-prefixes
149  */
150  std::string toStringBestSI() const;
151 
152  UnitAlgebra& operator= (const std::string& v);
153 
154  /** Multiply by an argument; */
156  /** Multiply by an argument; */
157  template <typename T>
158  UnitAlgebra& operator*= (const T& v) {
159  value *= v;
160  return *this;
161  }
162 
163  /** Divide by an argument; */
165  /** Divide by an argument; */
166  template <typename T>
167  UnitAlgebra& operator/= (const T& v) {
168  value /= v;
169  return *this;
170  }
171 
172  /** Add an argument; */
174  /** Multiply by an argument; */
175  template <typename T>
176  UnitAlgebra& operator+= (const T& v) {
177  value += v;
178  return *this;
179  }
180 
181  /** Subtract an argument; */
183  /** Divide by an argument; */
184  template <typename T>
185  UnitAlgebra& operator-= (const T& v) {
186  value -= v;
187  return *this;
188  }
189 
190  /** Compare if this object is greater than the argument */
191  bool operator> (const UnitAlgebra& v) const;
192  /** Compare if this object is greater than, or equal to, the argument */
193  bool operator>= (const UnitAlgebra& v) const;
194  /** Compare if this object is less than the argument */
195  bool operator< (const UnitAlgebra& v) const;
196  /** Compare if this object is less than, or equal to, the argument */
197  bool operator<= (const UnitAlgebra& v) const;
198  /** Apply a reciprocal operation to the object */
199  UnitAlgebra& invert();
200 
201  /** Returns true if the units in the parameter string are found
202  * in this object.
203  */
204  bool hasUnits(const std::string& u) const;
205  /** Return the raw value */
206  sst_big_num getValue() const {return value;}
207  /** Return the rounded value as a 64bit integer */
208  int64_t getRoundedValue() const;
209 
210  void serialize_order(SST::Core::Serialization::serializer &ser) override {
211  // Do the unit
212  ser & unit.numerator;
213  ser & unit.denominator;
214 
215  // For value, need to convert cpp_dec_float to string and
216  // reinit from string
217  switch(ser.mode()) {
218  case SST::Core::Serialization::serializer::SIZER:
219  case SST::Core::Serialization::serializer::PACK: {
220  // std::string s = value.str(40, std::ios_base::fixed);
221  std::string s = value.toString(0);
222  ser & s;
223  break;
224  }
225  case SST::Core::Serialization::serializer::UNPACK: {
226  std::string s;
227  ser & s;
228  value = sst_big_num(s);
229  break;
230  }
231  }
232  }
233  ImplementSerializable(SST::UnitAlgebra)
234 };
235 
236 
237 // template <typename T>
238 // UnitAlgebra operator* (UnitAlgebra lhs, const T& rhs);
239 
240 // template <typename T>
241 // UnitAlgebra operator* (const T& lhs, UnitAlgebra rhs);
242 
243 // UnitAlgebra operator* (UnitAlgebra& lhs, const UnitAlgebra rhs);
244 
245 // template <typename T>
246 // UnitAlgebra operator/ (UnitAlgebra lhs, const T& rhs);
247 
248 // std::ostream& operator<< (std::ostream& os, const UnitAlgebra& r);
249 
250 // std::ostream& operator<< (std::ostream& os, const Units& r);
251 
252 template <typename T>
253 UnitAlgebra operator* (UnitAlgebra lhs, const T& rhs)
254 {
255  lhs *= rhs;
256  return lhs;
257 }
258 
259 // template <typename T>
260 // UnitAlgebra operator* (const T& lhs, UnitAlgebra rhs)
261 // {
262 // rhs *= lhs;
263 // return rhs;
264 // }
265 
266 inline UnitAlgebra operator* (UnitAlgebra lhs, const UnitAlgebra& rhs)
267 {
268  lhs *= rhs;
269  return lhs;
270 }
271 
272 template <typename T>
273 UnitAlgebra operator/ (UnitAlgebra lhs, const T& rhs)
274 {
275  lhs /= rhs;
276  return lhs;
277 }
278 
279 inline UnitAlgebra operator/ (UnitAlgebra lhs, const UnitAlgebra& rhs)
280 {
281  lhs /= rhs;
282  return lhs;
283 }
284 
285 template <typename T>
286 UnitAlgebra operator+ (UnitAlgebra lhs, const T& rhs)
287 {
288  lhs += rhs;
289  return lhs;
290 }
291 
292 inline UnitAlgebra operator+ (UnitAlgebra lhs, const UnitAlgebra& rhs)
293 {
294  lhs += rhs;
295  return lhs;
296 }
297 
298 template <typename T>
299 UnitAlgebra operator- (UnitAlgebra lhs, const T& rhs)
300 {
301  lhs -= rhs;
302  return lhs;
303 }
304 
305 inline UnitAlgebra operator- (UnitAlgebra lhs, const UnitAlgebra& rhs)
306 {
307  lhs -= rhs;
308  return lhs;
309 }
310 
311 
312 inline std::ostream& operator<< (std::ostream& os, const UnitAlgebra& r)
313 {
314  os << r.toString();
315  return os;
316 }
317 
318 inline std::ostream& operator<< (std::ostream& os, const Units& r)
319 {
320  os << r.toString();
321  return os;
322 }
323 
324 } // namespace SST
325 
326 
327 #endif //SST_CORE_UNITALGEBRA_H
bool operator>(const UnitAlgebra &v) const
Compare if this object is greater than the argument.
Definition: unitAlgebra.cc:475
Units & operator*=(const Units &v)
Self-multiplication operator.
Definition: unitAlgebra.cc:254
UnitAlgebra & operator/=(const UnitAlgebra &v)
Divide by an argument;.
Definition: unitAlgebra.cc:441
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
void print(std::ostream &stream)
Print to an ostream the value.
Definition: unitAlgebra.cc:383
std::string toString(int32_t precision=6) const
Create a string representation of this decimal_fixedpoint.
Definition: decimal_fixedpoint.h:427
bool operator>=(const UnitAlgebra &v) const
Compare if this object is greater than, or equal to, the argument.
Definition: unitAlgebra.cc:487
std::string toString() const
Return a string representation of this value.
Definition: unitAlgebra.cc:394
int64_t getRoundedValue() const
Return the rounded value as a 64bit integer.
Definition: unitAlgebra.cc:545
Units & operator=(const Units &v)
Assignment operator.
Definition: unitAlgebra.cc:247
static void registerBaseUnit(const std::string &u)
Create a new Base Unit type.
Definition: unitAlgebra.cc:193
std::string toStringBestSI() const
Return a string representation of this value Formats the number using SI-prefixes.
Definition: unitAlgebra.cc:403
bool operator==(const Units &lhs) const
Equality Operator.
Definition: unitAlgebra.cc:273
Definition: serializable.h:109
Units & invert()
Perform a reciprocal operation.
Definition: unitAlgebra.cc:287
sst_big_num getValue() const
Return the raw value.
Definition: unitAlgebra.h:206
void printWithBestSI(std::ostream &stream)
Print to an ostream the value Formats the number using SI-prefixes.
Definition: unitAlgebra.cc:388
UnitAlgebra & invert()
Apply a reciprocal operation to the object.
Definition: unitAlgebra.cc:524
std::string toString() const
Return a String representation if this Unit.
Definition: unitAlgebra.cc:296
UnitAlgebra & operator*=(const UnitAlgebra &v)
Multiply by an argument;.
Definition: unitAlgebra.cc:433
UnitAlgebra & operator+=(const UnitAlgebra &v)
Add an argument;.
Definition: unitAlgebra.cc:449
static void registerCompoundUnit(const std::string &u, const std::string &v)
Create a new Compound Unit type.
Definition: unitAlgebra.cc:204
UnitAlgebra & operator-=(const UnitAlgebra &v)
Subtract an argument;.
Definition: unitAlgebra.cc:462
bool hasUnits(const std::string &u) const
Returns true if the units in the parameter string are found in this object.
Definition: unitAlgebra.cc:533
bool operator<=(const UnitAlgebra &v) const
Compare if this object is less than, or equal to, the argument.
Definition: unitAlgebra.cc:511
bool operator!=(const Units &lhs) const
Inequality Operator.
Definition: unitAlgebra.h:93
Performs Unit math in full precision.
Definition: unitAlgebra.h:107
Units & operator/=(const Units &v)
Self-division operator.
Definition: unitAlgebra.cc:264
Helper class internal to UnitAlgebra.
Definition: unitAlgebra.h:40
bool operator<(const UnitAlgebra &v) const
Compare if this object is less than the argument.
Definition: unitAlgebra.cc:499
Definition: serializable.h:132