SST  9.1.0
StructuralSimulationToolkit
unitAlgebra.h
1 // -*- c++ -*-
2 
3 // Copyright 2009-2019 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-2019, 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(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(std::string u);
69  /** Create a new Compound Unit type */
70  static void registerCompoundUnit(std::string u, 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(std::string units, sst_big_num& multiplier);
78  Units() {}
79  virtual ~Units() {}
80 
81  /** Assignment operator */
82  Units& operator= (const Units& v);
83  /** Self-multiplication operator */
84  Units& operator*= (const Units& v);
85  /** Self-division operator */
86  Units& operator/= (const Units& v);
87  /** Equality Operator */
88  bool operator== (const Units &lhs) const;
89  /** Inequality Operator */
90  bool operator!= (const Units &lhs) const {return !(*this == lhs);}
91  /** Perform a reciprocal operation. Numerator and Denominator swap. */
92  Units& invert();
93 
94  /** Return a String representation if this Unit */
95  std::string toString() const;
96 };
97 
98 /**
99  * Performs Unit math in full precision
100  *
101  * Allows operations such as multiplying a frequency by 2.
102  *
103  */
105  public SST::Core::Serialization::serializable_type<UnitAlgebra> {
106 private:
107  Units unit;
108  sst_big_num value;
109 
110  static std::string trim(std::string str);
111  void init(std::string val);
112 
113 public:
114  UnitAlgebra() {}
115  /**
116  Create a new UnitAlgebra instance, and pre-populate with a parsed value.
117 
118  \param val Value to parse. It is of the following format:
119  \code
120  val := NUMBER( )?UNITS
121  NUMBER := (-)?[0-9]+(.[0-9]+)?
122  UNITS := UNITGROUP(/UNITGROUP)
123  UNITGROUP := UNIT(-UNIT)*
124  UNIT := (SIPREFIX)?(BASEUNIT|COMPUNIT)
125  SIPREFIX := {a,f,p,n,u,m,[kKMGTPE]i?}
126  BASEUNIT := {s,B,b,events}
127  COMPUNIT := {Hz,hz,Bps,bps,event}
128  \endcode
129  */
130  UnitAlgebra(std::string val);
131  virtual ~UnitAlgebra();
132 
133  /** Print to an ostream the value */
134  void print(std::ostream& stream);
135  /** Print to an ostream the value
136  * Formats the number using SI-prefixes
137  */
138  void printWithBestSI(std::ostream& stream);
139  /** Return a string representation of this value */
140  std::string toString() const;
141  /** Return a string representation of this value
142  * Formats the number using SI-prefixes
143  */
144  std::string toStringBestSI() const;
145 
146  UnitAlgebra& operator= (const std::string& v);
147 
148  /** Multiply by an argument; */
150  /** Multiply by an argument; */
151  template <typename T>
152  UnitAlgebra& operator*= (const T& v) {
153  value *= v;
154  return *this;
155  }
156 
157  /** Divide by an argument; */
159  /** Divide by an argument; */
160  template <typename T>
161  UnitAlgebra& operator/= (const T& v) {
162  value /= v;
163  return *this;
164  }
165 
166  /** Add an argument; */
168  /** Multiply by an argument; */
169  template <typename T>
170  UnitAlgebra& operator+= (const T& v) {
171  value += v;
172  return *this;
173  }
174 
175  /** Subtract an argument; */
177  /** Divide by an argument; */
178  template <typename T>
179  UnitAlgebra& operator-= (const T& v) {
180  value -= v;
181  return *this;
182  }
183 
184  /** Compare if this object is greater than the argument */
185  bool operator> (const UnitAlgebra& v) const;
186  /** Compare if this object is greater than, or equal to, the argument */
187  bool operator>= (const UnitAlgebra& v) const;
188  /** Compare if this object is less than the argument */
189  bool operator< (const UnitAlgebra& v) const;
190  /** Compare if this object is less than, or equal to, the argument */
191  bool operator<= (const UnitAlgebra& v) const;
192  /** Apply a reciprocal operation to the object */
193  UnitAlgebra& invert();
194 
195  /** Returns true if the units in the parameter string are found
196  * in this object.
197  */
198  bool hasUnits(std::string u) const;
199  /** Return the raw value */
200  sst_big_num getValue() const {return value;}
201  /** Return the rounded value as a 64bit integer */
202  int64_t getRoundedValue() const;
203 
204  void serialize_order(SST::Core::Serialization::serializer &ser) override {
205  // Do the unit
206  ser & unit.numerator;
207  ser & unit.denominator;
208 
209  // For value, need to convert cpp_dec_float to string and
210  // reinit from string
211  switch(ser.mode()) {
212  case SST::Core::Serialization::serializer::SIZER:
213  case SST::Core::Serialization::serializer::PACK: {
214  // std::string s = value.str(40, std::ios_base::fixed);
215  std::string s = value.toString(0);
216  ser & s;
217  break;
218  }
219  case SST::Core::Serialization::serializer::UNPACK: {
220  std::string s;
221  ser & s;
222  value = sst_big_num(s);
223  break;
224  }
225  }
226  }
227  ImplementSerializable(SST::UnitAlgebra)
228 };
229 
230 
231 // template <typename T>
232 // UnitAlgebra operator* (UnitAlgebra lhs, const T& rhs);
233 
234 // template <typename T>
235 // UnitAlgebra operator* (const T& lhs, UnitAlgebra rhs);
236 
237 // UnitAlgebra operator* (UnitAlgebra& lhs, const UnitAlgebra rhs);
238 
239 // template <typename T>
240 // UnitAlgebra operator/ (UnitAlgebra lhs, const T& rhs);
241 
242 // std::ostream& operator<< (std::ostream& os, const UnitAlgebra& r);
243 
244 // std::ostream& operator<< (std::ostream& os, const Units& r);
245 
246 template <typename T>
247 UnitAlgebra operator* (UnitAlgebra lhs, const T& rhs)
248 {
249  lhs *= rhs;
250  return lhs;
251 }
252 
253 // template <typename T>
254 // UnitAlgebra operator* (const T& lhs, UnitAlgebra rhs)
255 // {
256 // rhs *= lhs;
257 // return rhs;
258 // }
259 
260 inline UnitAlgebra operator* (UnitAlgebra lhs, const UnitAlgebra& rhs)
261 {
262  lhs *= rhs;
263  return lhs;
264 }
265 
266 template <typename T>
267 UnitAlgebra operator/ (UnitAlgebra lhs, const T& rhs)
268 {
269  lhs /= rhs;
270  return lhs;
271 }
272 
273 inline UnitAlgebra operator/ (UnitAlgebra lhs, const UnitAlgebra& rhs)
274 {
275  lhs /= rhs;
276  return lhs;
277 }
278 
279 template <typename T>
280 UnitAlgebra operator+ (UnitAlgebra lhs, const T& rhs)
281 {
282  lhs += rhs;
283  return lhs;
284 }
285 
286 inline UnitAlgebra operator+ (UnitAlgebra lhs, const UnitAlgebra& rhs)
287 {
288  lhs += rhs;
289  return lhs;
290 }
291 
292 template <typename T>
293 UnitAlgebra operator- (UnitAlgebra lhs, const T& rhs)
294 {
295  lhs -= rhs;
296  return lhs;
297 }
298 
299 inline UnitAlgebra operator- (UnitAlgebra lhs, const UnitAlgebra& rhs)
300 {
301  lhs -= rhs;
302  return lhs;
303 }
304 
305 
306 inline std::ostream& operator<< (std::ostream& os, const UnitAlgebra& r)
307 {
308  os << r.toString();
309  return os;
310 }
311 
312 inline std::ostream& operator<< (std::ostream& os, const Units& r)
313 {
314  os << r.toString();
315  return os;
316 }
317 
318 } // namespace SST
319 
320 
321 #endif //SST_CORE_UNITALGEBRA_H
bool operator>(const UnitAlgebra &v) const
Compare if this object is greater than the argument.
Definition: unitAlgebra.cc:478
Units & operator*=(const Units &v)
Self-multiplication operator.
Definition: unitAlgebra.cc:257
UnitAlgebra & operator/=(const UnitAlgebra &v)
Divide by an argument;.
Definition: unitAlgebra.cc:444
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:386
std::string toString(int32_t precision=6) const
Create a string representation of this decimal_fixedpoint.
Definition: decimal_fixedpoint.h:426
bool operator>=(const UnitAlgebra &v) const
Compare if this object is greater than, or equal to, the argument.
Definition: unitAlgebra.cc:490
std::string toString() const
Return a string representation of this value.
Definition: unitAlgebra.cc:397
bool hasUnits(std::string u) const
Returns true if the units in the parameter string are found in this object.
Definition: unitAlgebra.cc:536
static void registerBaseUnit(std::string u)
Create a new Base Unit type.
Definition: unitAlgebra.cc:196
int64_t getRoundedValue() const
Return the rounded value as a 64bit integer.
Definition: unitAlgebra.cc:548
static void registerCompoundUnit(std::string u, std::string v)
Create a new Compound Unit type.
Definition: unitAlgebra.cc:207
Units & operator=(const Units &v)
Assignment operator.
Definition: unitAlgebra.cc:250
std::string toStringBestSI() const
Return a string representation of this value Formats the number using SI-prefixes.
Definition: unitAlgebra.cc:406
bool operator==(const Units &lhs) const
Equality Operator.
Definition: unitAlgebra.cc:276
Definition: serializable.h:110
Units & invert()
Perform a reciprocal operation.
Definition: unitAlgebra.cc:290
sst_big_num getValue() const
Return the raw value.
Definition: unitAlgebra.h:200
void printWithBestSI(std::ostream &stream)
Print to an ostream the value Formats the number using SI-prefixes.
Definition: unitAlgebra.cc:391
UnitAlgebra & invert()
Apply a reciprocal operation to the object.
Definition: unitAlgebra.cc:527
std::string toString() const
Return a String representation if this Unit.
Definition: unitAlgebra.cc:299
UnitAlgebra & operator*=(const UnitAlgebra &v)
Multiply by an argument;.
Definition: unitAlgebra.cc:436
UnitAlgebra & operator+=(const UnitAlgebra &v)
Add an argument;.
Definition: unitAlgebra.cc:452
UnitAlgebra & operator-=(const UnitAlgebra &v)
Subtract an argument;.
Definition: unitAlgebra.cc:465
bool operator<=(const UnitAlgebra &v) const
Compare if this object is less than, or equal to, the argument.
Definition: unitAlgebra.cc:514
bool operator!=(const Units &lhs) const
Inequality Operator.
Definition: unitAlgebra.h:90
Performs Unit math in full precision.
Definition: unitAlgebra.h:104
Units & operator/=(const Units &v)
Self-division operator.
Definition: unitAlgebra.cc:267
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:502
Definition: serializable.h:133