Flow123d  jenkins-Flow123d-linux-release-multijob-198
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(UnitSI::n_base_units);
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 
98 
99 
100 
101 
102 std::string UnitSI::format_latex() const {
103  OutputFormat form;
104  form.exp_open="^{";
105  form.exp_close="}";
106  form.delimiter="";
107  return format(form);
108 }
109 
110 
111 
112 std::string UnitSI::format_text() const {
113  OutputFormat form;
114  form.exp_open="(";
115  form.exp_close=")";
116  form.delimiter=".";
117  return format(form);
118 }
119 
120 
121 
122 std::string UnitSI::format(OutputFormat form) const {
123  ASSERT(is_def(), "UnitSI object must be defined!");
124 
125  // Symbols for base SI units.
126  std::vector<std::string> unit_symbols={"m","d","kg","s","A","K","mol","cd" };
127 
128  std::stringstream output;
129 
130  // format of meter (can be m^{n} or m^{n-d})
131  if (exponents_[ UnitSI::order_m ] || exponents_[ UnitSI::order_md ]) {
132  output << unit_symbols[ UnitSI::order_m ];
133  if (exponents_[ UnitSI::order_m ]!=1 || exponents_[ UnitSI::order_md ]) {
134  output << form.exp_open;
135  if (exponents_[ UnitSI::order_m ]) {
136  output << exponents_[ UnitSI::order_m ];
137  if (exponents_[ UnitSI::order_md ]>0) output << "+";
138  }
139  if (exponents_[ UnitSI::order_md ]) {
140  if (exponents_[ UnitSI::order_md ]==-1) output << "-";
141  else if (exponents_[ UnitSI::order_md ]!=1) output << exponents_[ UnitSI::order_md ];
142  output << unit_symbols[ UnitSI::order_md ];
143  }
144  output << form.exp_close;
145  }
146  }
147 
148  // format of other units
149  for (unsigned int i=2; i<UnitSI::n_base_units; i++)
150  if (exponents_[i]) {
151  if (output.str().size() > 0) output << form.delimiter;
152  output << unit_symbols[i];
153  if (exponents_[i] != 1) output << form.exp_open << exponents_[i] << form.exp_close;
154  }
155 
156  if (output.str().size()==0) { //dimensionless quantity, contains only "$["
157  output << "-";
158  }
159 
160  return output.str();
161 }
162 
163 void UnitSI::undef(bool val) {
164  undef_ = val;
165 }
166 
167 bool UnitSI::is_def() const {
168  return !undef_;
169 }
170 
171 
172 UnitSI operator *(const UnitSI &a, const UnitSI &b) {
173  UnitSI tmp;
174 
175  if (a.is_def() && b.is_def()) {
176  tmp.undef_ = false;
177  for (unsigned int i=0; i<7; i++) {
178  tmp.exponents_[i] = a.exponents_[i] + b.exponents_[i];
179  }
180  }
181 
182  return tmp;
183 }
184 
185 UnitSI operator /(const UnitSI &a, const UnitSI &b) {
186  UnitSI tmp;
187 
188  if (a.is_def() && b.is_def()) {
189  tmp.undef_ = false;
190  for (unsigned int i=0; i<7; i++) {
191  tmp.exponents_[i] = a.exponents_[i] - b.exponents_[i];
192  }
193  }
194 
195  return tmp;
196 }
std::string format(OutputFormat form) const
Generic output formating mtehod.
Definition: unit_si.cc:122
bool is_def() const
Return true if the unit is defined.
Definition: unit_si.cc:167
Variable parts of output format. Used in the format method.
Definition: unit_si.hh:95
std::string format_text() const
Definition: unit_si.cc:112
std::vector< int > exponents_
Definition: unit_si.hh:108
static UnitSI & Pa()
Returns Pascal.
Definition: unit_si.cc:39
std::string format_latex() const
Definition: unit_si.cc:102
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:172
#define ASSERT(...)
Definition: global_defs.h:121
UnitSI operator/(const UnitSI &a, const UnitSI &b)
Proportion of two units.
Definition: unit_si.cc:185
std::string delimiter
Definition: unit_si.hh:96
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
std::string exp_close
Definition: unit_si.hh:96
static UnitSI & N()
Definition: unit_si.cc:24
bool undef_
Definition: unit_si.hh:117
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:163
std::string exp_open
Definition: unit_si.hh:96
UnitSI()
Constructor.
Definition: unit_si.cc:18