Flow123d  JS_before_hm-937-g93502c2
unit_si.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file unit_si.hh
15  * @brief
16  */
17 
18 #ifndef UNIT_SI_HH_
19 #define UNIT_SI_HH_
20 
21 #include <vector>
22 #include <string>
23 
24 
25 /**
26  * @brief Class for representation SI units of Fields.
27  *
28  * Units are set through exponents of basic SI units. These exponents are set in methods with same
29  * name as unit symbols (e.g. kg(), K() etc).
30  *
31  * Class contains method that provides formated string representing full unit symbol (usable in
32  * LaTeX output).
33  *
34  * UnitSI object contains flag that says if it is defined
35  * - undefined object can't be formated
36  * - if any exponent is set, flag is set to defined
37  *
38  * Class contains static methods that return frequently used derived units (Watt, Pascal etc).
39  */
40 class UnitSI {
41 public:
42  /// Constructor
43  UnitSI();
44 
45  /// Methods return frequently used derived units
46  /// Returns Newton
47  static UnitSI & N();
48  /// Returns Joule
49  static UnitSI & J();
50  /// Returns Watt
51  static UnitSI & W();
52  /// Returns Pascal
53  static UnitSI & Pa();
54  /// Returns dimensionless unit
55  static UnitSI & dimensionless();
56  /// Returns dimensionless unit
57  static UnitSI & one();
58 
59  /// Methods set values of exponents for SI units with similar name
60  UnitSI & m(int exp = 1);
61  UnitSI & kg(int exp = 1);
62  UnitSI & s(int exp = 1);
63  UnitSI & A(int exp = 1);
64  UnitSI & K(int exp = 1);
65  UnitSI & mol(int exp = 1);
66  UnitSI & cd(int exp = 1);
67  /// The dimension dependent meter: md^y = m^(yd), where 'd' is dimension.
68  UnitSI & md(int exp = -1);
69 
70  /**
71  * Makes unit description string in Latex format, e.g. "$[m.kg^{2}.s^{-2}]$"
72  *
73  * Have assert for undefined units.
74  */
75  std::string format_latex() const;
76 
77  std::string format_text() const;
78 
79  /**
80  * Machine readable JSON format. Units are stored as a record with keys given by
81  * SI units strings (corresponding to setters). Values of the keys are integer.
82  */
83  std::string json() const;
84 
85  /**
86  * Set flag that unit is undefined.
87  *
88  * Default value is true (set in constructor).
89  * If any exponent is set, @p undef_ flag is unset.
90  * In all fields unit must be defined by user.
91  */
92  void undef(bool val = true);
93 
94  /// Return true if the unit is defined.
95  bool is_def() const;
96 
97  /// Multiply with power of given unit
98  void multiply(const UnitSI &other, int exp = 1);
99 
100  /// Reset UnitSI object (set vector of exponents to zeros and set undef flag)
101  void reset();
102 
103  /**
104  * @brief Convert and check user-defined unit.
105  *
106  * - converts unit SI define by user as string
107  * - checks if converted unit is equal with self
108  * - return multiplicated coeficient
109  */
110  double convert_unit_from(std::string actual_unit) const;
111 
112  /// Comparison operator
113  bool operator==(const UnitSI &other) const;
114 
115 private:
116  /// Values determine positions of exponents in exponents_ vector
117  enum UnitOrder {
127  };
128 
129  /// Variable parts of output format. Used in the @p format method.
130  struct OutputFormat {
131  std::string exp_open, exp_close, delimiter;
132  };
133 
134  /**
135  * Symbols for individual units. Can not use static variable due to usage in static initialization.
136  */
137  static const std::string &unit_symbol(unsigned int idx);
138 
139  /// Generic output formating method.
140  std::string format(OutputFormat form) const;
141 
142 
143  /**
144  * Stores exponents of base SI units in the order given by the
145  * UnitOrder enum
146  *
147  * where md represents value of exponent depended on dimension (m^{-d})
148  */
150 
151 
152  /**
153  * Flag if object is undefined.
154  *
155  * Value is set on true in constructor, when any exponent is changed, false value is set.
156  */
157  bool undef_;
158 
159  /// Product of two units.
160  friend UnitSI operator *(const UnitSI &a, const UnitSI &b);
161  /// Proportion of two units.
162  friend UnitSI operator /(const UnitSI &a, const UnitSI &b);
163 };
164 
165 
166 /// Product of two units.
167 UnitSI operator *(const UnitSI &a, const UnitSI &b);
168 
169 /// Proportion of two units.
170 UnitSI operator /(const UnitSI &a, const UnitSI &b);
171 
172 
173 #endif /* UNIT_SI_HH_ */
std::string format(OutputFormat form) const
Generic output formating method.
Definition: unit_si.cc:144
bool is_def() const
Return true if the unit is defined.
Definition: unit_si.cc:201
Variable parts of output format. Used in the format method.
Definition: unit_si.hh:130
std::string format_text() const
Definition: unit_si.cc:127
std::vector< int > exponents_
Definition: unit_si.hh:149
static UnitSI & Pa()
Returns Pascal.
Definition: unit_si.cc:50
double convert_unit_from(std::string actual_unit) const
Convert and check user-defined unit.
Definition: unit_si.cc:217
static UnitSI & one()
Returns dimensionless unit.
Definition: unit_si.cc:60
UnitOrder
Values determine positions of exponents in exponents_ vector.
Definition: unit_si.hh:117
std::string format_latex() const
Definition: unit_si.cc:117
UnitSI & K(int exp=1)
Definition: unit_si.cc:88
UnitSI & A(int exp=1)
Definition: unit_si.cc:82
friend UnitSI operator/(const UnitSI &a, const UnitSI &b)
Proportion of two units.
Definition: unit_si.cc:248
std::string delimiter
Definition: unit_si.hh:131
UnitSI & cd(int exp=1)
Definition: unit_si.cc:100
static UnitSI & W()
Returns Watt.
Definition: unit_si.cc:45
UnitSI & s(int exp=1)
Definition: unit_si.cc:76
UnitSI & kg(int exp=1)
Definition: unit_si.cc:70
static UnitSI & J()
Returns Joule.
Definition: unit_si.cc:40
friend UnitSI operator*(const UnitSI &a, const UnitSI &b)
Product of two units.
Definition: unit_si.cc:235
std::string exp_close
Definition: unit_si.hh:131
void multiply(const UnitSI &other, int exp=1)
Multiply with power of given unit.
Definition: unit_si.cc:205
static UnitSI & N()
Definition: unit_si.cc:35
static const std::string & unit_symbol(unsigned int idx)
Definition: unit_si.cc:137
bool undef_
Definition: unit_si.hh:157
bool operator==(const UnitSI &other) const
Comparison operator.
Definition: unit_si.cc:230
void reset()
Reset UnitSI object (set vector of exponents to zeros and set undef flag)
Definition: unit_si.cc:212
UnitSI & mol(int exp=1)
Definition: unit_si.cc:94
UnitSI & m(int exp=1)
Methods set values of exponents for SI units with similar name.
Definition: unit_si.cc:64
Class for representation SI units of Fields.
Definition: unit_si.hh:40
UnitSI & md(int exp=-1)
The dimension dependent meter: md^y = m^(yd), where &#39;d&#39; is dimension.
Definition: unit_si.cc:106
static UnitSI & dimensionless()
Returns dimensionless unit.
Definition: unit_si.cc:55
void undef(bool val=true)
Definition: unit_si.cc:197
std::string exp_open
Definition: unit_si.hh:131
UnitSI()
Constructor.
Definition: unit_si.cc:28
std::string json() const
Definition: unit_si.cc:183