Flow123d  JS_before_hm-1804-gf2ad740aa
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_ */
UnitSI::cd
UnitSI & cd(int exp=1)
Definition: unit_si.cc:100
UnitSI::order_s
@ order_s
Definition: unit_si.hh:121
UnitSI::unit_symbol
static const std::string & unit_symbol(unsigned int idx)
Definition: unit_si.cc:137
UnitSI::kg
UnitSI & kg(int exp=1)
Definition: unit_si.cc:70
UnitSI::dimensionless
static UnitSI & dimensionless()
Returns dimensionless unit.
Definition: unit_si.cc:55
UnitSI::md
UnitSI & md(int exp=-1)
The dimension dependent meter: md^y = m^(yd), where 'd' is dimension.
Definition: unit_si.cc:106
UnitSI::format_text
std::string format_text() const
Definition: unit_si.cc:127
UnitSI::undef_
bool undef_
Definition: unit_si.hh:157
UnitSI::multiply
void multiply(const UnitSI &other, int exp=1)
Multiply with power of given unit.
Definition: unit_si.cc:205
UnitSI::json
std::string json() const
Definition: unit_si.cc:183
UnitSI::operator*
friend UnitSI operator*(const UnitSI &a, const UnitSI &b)
Product of two units.
Definition: unit_si.cc:235
UnitSI::A
UnitSI & A(int exp=1)
Definition: unit_si.cc:82
UnitSI::format_latex
std::string format_latex() const
Definition: unit_si.cc:117
UnitSI::exponents_
std::vector< int > exponents_
Definition: unit_si.hh:149
std::vector< int >
UnitSI::convert_unit_from
double convert_unit_from(std::string actual_unit) const
Convert and check user-defined unit.
Definition: unit_si.cc:217
UnitSI::order_A
@ order_A
Definition: unit_si.hh:122
UnitSI::mol
UnitSI & mol(int exp=1)
Definition: unit_si.cc:94
UnitSI::m
UnitSI & m(int exp=1)
Methods set values of exponents for SI units with similar name.
Definition: unit_si.cc:64
UnitSI::order_md
@ order_md
Definition: unit_si.hh:119
UnitSI::W
static UnitSI & W()
Returns Watt.
Definition: unit_si.cc:45
UnitSI::order_mol
@ order_mol
Definition: unit_si.hh:124
UnitSI::UnitSI
UnitSI()
Constructor.
Definition: unit_si.cc:28
operator*
UnitSI operator*(const UnitSI &a, const UnitSI &b)
Product of two units.
Definition: unit_si.cc:235
UnitSI::N
static UnitSI & N()
Definition: unit_si.cc:35
UnitSI::order_cd
@ order_cd
Definition: unit_si.hh:125
UnitSI::OutputFormat
Variable parts of output format. Used in the format method.
Definition: unit_si.hh:130
UnitSI
Class for representation SI units of Fields.
Definition: unit_si.hh:40
UnitSI::one
static UnitSI & one()
Returns dimensionless unit.
Definition: unit_si.cc:60
UnitSI::OutputFormat::delimiter
std::string delimiter
Definition: unit_si.hh:131
UnitSI::Pa
static UnitSI & Pa()
Returns Pascal.
Definition: unit_si.cc:50
UnitSI::reset
void reset()
Reset UnitSI object (set vector of exponents to zeros and set undef flag)
Definition: unit_si.cc:212
UnitSI::OutputFormat::exp_close
std::string exp_close
Definition: unit_si.hh:131
UnitSI::UnitOrder
UnitOrder
Values determine positions of exponents in exponents_ vector.
Definition: unit_si.hh:117
UnitSI::order_m
@ order_m
Definition: unit_si.hh:118
UnitSI::undef
void undef(bool val=true)
Definition: unit_si.cc:197
operator/
UnitSI operator/(const UnitSI &a, const UnitSI &b)
Proportion of two units.
Definition: unit_si.cc:248
UnitSI::order_K
@ order_K
Definition: unit_si.hh:123
UnitSI::s
UnitSI & s(int exp=1)
Definition: unit_si.cc:76
UnitSI::K
UnitSI & K(int exp=1)
Definition: unit_si.cc:88
UnitSI::J
static UnitSI & J()
Returns Joule.
Definition: unit_si.cc:40
UnitSI::n_base_units
@ n_base_units
Definition: unit_si.hh:126
UnitSI::OutputFormat::exp_open
std::string exp_open
Definition: unit_si.hh:131
UnitSI::format
std::string format(OutputFormat form) const
Generic output formating method.
Definition: unit_si.cc:144
UnitSI::operator/
friend UnitSI operator/(const UnitSI &a, const UnitSI &b)
Proportion of two units.
Definition: unit_si.cc:248
UnitSI::is_def
bool is_def() const
Return true if the unit is defined.
Definition: unit_si.cc:201
UnitSI::order_kg
@ order_kg
Definition: unit_si.hh:120
UnitSI::operator==
bool operator==(const UnitSI &other) const
Comparison operator.
Definition: unit_si.cc:230