Flow123d  last_with_con_2.0.0-4-g42e6930
unit_si.cc
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.cc
15  * @brief
16  */
17 
18 #include <sstream>
19 
20 #include "fields/unit_si.hh"
21 #include "system/asserts.hh"
22 
23 
24 using namespace std;
25 
26 
28  exponents_.resize(UnitSI::n_base_units);
29  std::fill(exponents_.begin(), exponents_.end(), 0);
30  undef_ = true;
31 }
32 
34  static UnitSI unit = UnitSI().m().kg().s(-2);
35  return unit;
36 }
37 
39  static UnitSI unit = UnitSI().m(2).kg().s(-2);
40  return unit;
41 }
42 
44  static UnitSI unit = UnitSI().m(2).kg().s(-3);
45  return unit;
46 }
47 
49  static UnitSI unit = UnitSI().m(-1).kg().s(-2);
50  return unit;
51 }
52 
54  static UnitSI unit = UnitSI().m(0);
55  return unit;
56 }
57 
59  return dimensionless();
60 }
61 
62 UnitSI & UnitSI::m(int exp) {
63  exponents_[UnitSI::order_m] = exp;
64  undef_ = false;
65  return *this;
66 }
67 
68 UnitSI & UnitSI::kg(int exp) {
69  exponents_[UnitSI::order_kg] = exp;
70  undef_ = false;
71  return *this;
72 }
73 
74 UnitSI & UnitSI::s(int exp) {
75  exponents_[UnitSI::order_s] = exp;
76  undef_ = false;
77  return *this;
78 }
79 
80 UnitSI & UnitSI::A(int exp) {
81  exponents_[UnitSI::order_A] = exp;
82  undef_ = false;
83  return *this;
84 }
85 
86 UnitSI & UnitSI::K(int exp) {
87  exponents_[UnitSI::order_K] = exp;
88  undef_ = false;
89  return *this;
90 }
91 
92 UnitSI & UnitSI::mol(int exp) {
93  exponents_[UnitSI::order_mol] = exp;
94  undef_ = false;
95  return *this;
96 }
97 
98 UnitSI & UnitSI::cd(int exp) {
99  exponents_[UnitSI::order_cd] = exp;
100  undef_ = false;
101  return *this;
102 }
103 
104 UnitSI & UnitSI::md(int exp) {
105  exponents_[UnitSI::order_md] = exp;
106  undef_ = false;
107  return *this;
108 }
109 
110 
111 
112 
113 
114 
115 std::string UnitSI::format_latex() const {
116  OutputFormat form;
117  form.exp_open="^{";
118  form.exp_close="}";
119  form.delimiter="";
120  return format(form);
121 }
122 
123 
124 
125 std::string UnitSI::format_text() const {
126  OutputFormat form;
127  form.exp_open="(";
128  form.exp_close=")";
129  form.delimiter=".";
130  return format(form);
131 }
132 
133 
134 // Symbols for base SI units.
135 const std::string &UnitSI::unit_symbol(unsigned int idx) {
136  static std::vector<std::string> unit_symbols={"m","md","kg","s","A","K","mol","cd" };
137  return unit_symbols[idx];
138 }
139 
140 
141 
142 std::string UnitSI::format(OutputFormat form) const {
143  ASSERT(is_def()).error("UnitSI object must be defined!");
144 
145  std::stringstream output;
146 
147  // format of meter (can be m^{n} or m^{n-d})
148  if (exponents_[ UnitSI::order_m ] || exponents_[ UnitSI::order_md ]) {
149  output << unit_symbol( UnitSI::order_m );
150  if (exponents_[ UnitSI::order_m ]!=1 || exponents_[ UnitSI::order_md ]) {
151  output << form.exp_open;
152  if (exponents_[ UnitSI::order_m ]) {
153  output << exponents_[ UnitSI::order_m ];
154  if (exponents_[ UnitSI::order_md ]>0) output << "+";
155  }
156  if (exponents_[ UnitSI::order_md ]) {
157  if (exponents_[ UnitSI::order_md ]==-1) output << "-";
158  else if (exponents_[ UnitSI::order_md ]!=1) output << exponents_[ UnitSI::order_md ];
159  output << "d"; // Use 'd' for dimension in exponent, converting m^x md^y to m^(x+yd)
160  }
161  output << form.exp_close;
162  }
163  }
164 
165  // format of other units
166  for (unsigned int i=2; i<UnitSI::n_base_units; i++)
167  if (exponents_[i]) {
168  if (output.str().size() > 0) output << form.delimiter;
169  output << unit_symbol(i);
170  if (exponents_[i] != 1) output << form.exp_open << exponents_[i] << form.exp_close;
171  }
172 
173  if (output.str().size()==0) { //dimensionless quantity, contains only "$["
174  output << "-";
175  }
176 
177  return output.str();
178 }
179 
180 
181 std::string UnitSI::json() const {
182  stringstream ss;
183  ss << "{ ";
184  for(unsigned int i=0; i < n_base_units; i++ ) {
185  if (i!=0) ss << ", ";
186  ss << string("\"")
187  << unit_symbol(i)
188  << "\" : "
189  << exponents_[i];
190  }
191  ss << " }";
192  return ss.str();
193 }
194 
195 void UnitSI::undef(bool val) {
196  undef_ = val;
197 }
198 
199 bool UnitSI::is_def() const {
200  return !undef_;
201 }
202 
203 
204 UnitSI operator *(const UnitSI &a, const UnitSI &b) {
205  UnitSI tmp;
206 
207  if (a.is_def() && b.is_def()) {
208  tmp.undef_ = false;
209  for (unsigned int i=0; i<7; i++) {
210  tmp.exponents_[i] = a.exponents_[i] + b.exponents_[i];
211  }
212  }
213 
214  return tmp;
215 }
216 
217 UnitSI operator /(const UnitSI &a, const UnitSI &b) {
218  UnitSI tmp;
219 
220  if (a.is_def() && b.is_def()) {
221  tmp.undef_ = false;
222  for (unsigned int i=0; i<7; i++) {
223  tmp.exponents_[i] = a.exponents_[i] - b.exponents_[i];
224  }
225  }
226 
227  return tmp;
228 }
std::string format(OutputFormat form) const
Generic output formating method.
Definition: unit_si.cc:142
bool is_def() const
Return true if the unit is defined.
Definition: unit_si.cc:199
Variable parts of output format. Used in the format method.
Definition: unit_si.hh:112
std::string format_text() const
Definition: unit_si.cc:125
std::vector< int > exponents_
Definition: unit_si.hh:131
static UnitSI & Pa()
Returns Pascal.
Definition: unit_si.cc:48
static UnitSI & one()
Returns dimensionless unit.
Definition: unit_si.cc:58
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3141
Definitions of ASSERTS.
std::string format_latex() const
Definition: unit_si.cc:115
UnitSI & K(int exp=1)
Definition: unit_si.cc:86
UnitSI & A(int exp=1)
Definition: unit_si.cc:80
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
UnitSI operator*(const UnitSI &a, const UnitSI &b)
Product of two units.
Definition: unit_si.cc:204
UnitSI operator/(const UnitSI &a, const UnitSI &b)
Proportion of two units.
Definition: unit_si.cc:217
std::string delimiter
Definition: unit_si.hh:113
UnitSI & cd(int exp=1)
Definition: unit_si.cc:98
static UnitSI & W()
Returns Watt.
Definition: unit_si.cc:43
UnitSI & s(int exp=1)
Definition: unit_si.cc:74
UnitSI & kg(int exp=1)
Definition: unit_si.cc:68
static UnitSI & J()
Returns Joule.
Definition: unit_si.cc:38
std::string exp_close
Definition: unit_si.hh:113
static UnitSI & N()
Definition: unit_si.cc:33
static const std::string & unit_symbol(unsigned int idx)
Definition: unit_si.cc:135
bool undef_
Definition: unit_si.hh:139
UnitSI & mol(int exp=1)
Definition: unit_si.cc:92
UnitSI & m(int exp=1)
Methods set values of exponents for SI units with similar name.
Definition: unit_si.cc:62
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:104
static UnitSI & dimensionless()
Returns dimensionless unit.
Definition: unit_si.cc:53
void undef(bool val=true)
Definition: unit_si.cc:195
std::string exp_open
Definition: unit_si.hh:113
UnitSI()
Constructor.
Definition: unit_si.cc:27
std::string json() const
Definition: unit_si.cc:181