Flow123d  jenkins-Flow123d-windows32-release-multijob-51
unit_si.cc
Go to the documentation of this file.
1 /*
2  * unit_si.cc
3  *
4  * Created on: Aug 30, 2012
5  * Author: jb
6  */
7 
8 
9 #include <sstream>
10 
11 #include "fields/unit_si.hh"
12 #include "system/xio.h"
13 
14 
15 using namespace std;
16 
17 
19  exponents_.resize(8);
20  std::fill(exponents_.begin(), exponents_.end(), 0);
21  undef_ = true;
22 }
23 
25  static UnitSI unit = UnitSI().m().kg().s(-2);
26  return unit;
27 }
28 
30  static UnitSI unit = UnitSI().m(2).kg().s(-2);
31  return unit;
32 }
33 
35  static UnitSI unit = UnitSI().m(2).kg().s(-3);
36  return unit;
37 }
38 
40  static UnitSI unit = UnitSI().m(-1).kg().s(-2);
41  return unit;
42 }
43 
45  static UnitSI unit = UnitSI().m(0);
46  return unit;
47 }
48 
49 UnitSI & UnitSI::m(int exp) {
50  exponents_[UnitSI::order_m] = exp;
51  undef_ = false;
52  return *this;
53 }
54 
55 UnitSI & UnitSI::kg(int exp) {
56  exponents_[UnitSI::order_kg] = exp;
57  undef_ = false;
58  return *this;
59 }
60 
61 UnitSI & UnitSI::s(int exp) {
62  exponents_[UnitSI::order_s] = exp;
63  undef_ = false;
64  return *this;
65 }
66 
67 UnitSI & UnitSI::A(int exp) {
68  exponents_[UnitSI::order_A] = exp;
69  undef_ = false;
70  return *this;
71 }
72 
73 UnitSI & UnitSI::K(int exp) {
74  exponents_[UnitSI::order_K] = exp;
75  undef_ = false;
76  return *this;
77 }
78 
79 UnitSI & UnitSI::mol(int exp) {
80  exponents_[UnitSI::order_mol] = exp;
81  undef_ = false;
82  return *this;
83 }
84 
85 UnitSI & UnitSI::cd(int exp) {
86  exponents_[UnitSI::order_cd] = exp;
87  undef_ = false;
88  return *this;
89 }
90 
91 UnitSI & UnitSI::md(int exp) {
92  exponents_[UnitSI::order_md] = exp;
93  undef_ = false;
94  return *this;
95 }
96 
97 std::string UnitSI::format() const {
98  // Values represent symbols of base SI units in same order as units are stored in exponents_ vector
99  static std::vector<std::string> unit_symbols={"m","kg","s","A","K","mol","cd","d" };
100 
101  ASSERT(is_def(), "UnitSI object must be defined!");
102  std::stringstream output;
103  output << "$[";
104 
105  // format of meter (can be m^{n} or m^{n-d})
106  if (exponents_[ UnitSI::order_m ] || exponents_[ UnitSI::order_md ]) {
107  output << unit_symbols[ UnitSI::order_m ];
108  if (exponents_[ UnitSI::order_m ]!=1 || exponents_[ UnitSI::order_md ]) {
109  output << "^{";
110  if (exponents_[ UnitSI::order_m ]) {
111  output << exponents_[ UnitSI::order_m ];
112  if (exponents_[ UnitSI::order_md ]>0) output << "+";
113  }
114  if (exponents_[ UnitSI::order_md ]) {
115  if (exponents_[ UnitSI::order_md ]==-1) output << "-";
116  else if (exponents_[ UnitSI::order_md ]!=1) output << exponents_[ UnitSI::order_md ];
117  output << unit_symbols[ UnitSI::order_md ];
118  }
119  output << "}";
120  }
121  }
122 
123  // format of other units
124  for (unsigned int i=1; i<7; i++)
125  if (exponents_[i]) {
126  if (exponents_[i] == 1) {
127  output << unit_symbols[i];
128  } else {
129  output << unit_symbols[i] << "^{" << exponents_[i] << "}";
130  }
131  }
132 
133  if (output.str().size()==2) { //dimensionless quantity, contains only "$["
134  output << "-";
135  }
136  output << "]$";
137 
138  return output.str();
139 }
140 
141 void UnitSI::undef(bool val) {
142  undef_ = val;
143 }
144 
145 bool UnitSI::is_def() const {
146  return !undef_;
147 }
148 
149 
150 UnitSI operator *(const UnitSI &a, const UnitSI &b) {
151  UnitSI tmp;
152 
153  if (a.is_def() && b.is_def()) {
154  tmp.undef_ = false;
155  for (unsigned int i=0; i<7; i++) {
156  tmp.exponents_[i] = a.exponents_[i] + b.exponents_[i];
157  }
158  }
159 
160  return tmp;
161 }
162 
163 UnitSI operator /(const UnitSI &a, const UnitSI &b) {
164  UnitSI tmp;
165 
166  if (a.is_def() && b.is_def()) {
167  tmp.undef_ = false;
168  for (unsigned int i=0; i<7; i++) {
169  tmp.exponents_[i] = a.exponents_[i] - b.exponents_[i];
170  }
171  }
172 
173  return tmp;
174 }
bool is_def() const
Return true if the unit is defined.
Definition: unit_si.cc:145
std::vector< int > exponents_
Definition: unit_si.hh:97
static UnitSI & Pa()
Returns Pascal.
Definition: unit_si.cc:39
std::string format() const
Definition: unit_si.cc:97
UnitSI & K(int exp=1)
Definition: unit_si.cc:73
UnitSI & A(int exp=1)
Definition: unit_si.cc:67
I/O functions with filename storing, able to track current line in opened file. All standard stdio fu...
UnitSI operator*(const UnitSI &a, const UnitSI &b)
Product of two units.
Definition: unit_si.cc:150
#define ASSERT(...)
Definition: global_defs.h:121
UnitSI operator/(const UnitSI &a, const UnitSI &b)
Proportion of two units.
Definition: unit_si.cc:163
UnitSI & cd(int exp=1)
Definition: unit_si.cc:85
static UnitSI & W()
Returns Watt.
Definition: unit_si.cc:34
UnitSI & s(int exp=1)
Definition: unit_si.cc:61
UnitSI & kg(int exp=1)
Definition: unit_si.cc:55
static UnitSI & J()
Returns Joule.
Definition: unit_si.cc:29
static UnitSI & N()
Definition: unit_si.cc:24
bool undef_
Definition: unit_si.hh:104
UnitSI & mol(int exp=1)
Definition: unit_si.cc:79
UnitSI & m(int exp=1)
Methods set values of exponents for SI units with similar name.
Definition: unit_si.cc:49
Class for representation SI units of Fields.
Definition: unit_si.hh:31
UnitSI & md(int exp=-1)
Method sets value of exponent for m^{-d}, where d is dimension of region.
Definition: unit_si.cc:91
static UnitSI & dimensionless()
Returns dimensionless unit.
Definition: unit_si.cc:44
void undef(bool val=true)
Definition: unit_si.cc:141
UnitSI()
Constructor.
Definition: unit_si.cc:18