antioch-0.4.0
units_unit.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // Antioch - A Gas Dynamics Thermochemistry Library
5 //
6 // Copyright (C) 2014-2016 Paul T. Bauman, Benjamin S. Kirk,
7 // Sylvain Plessis, Roy H. Stonger
8 //
9 // Copyright (C) 2013 The PECOS Development Team
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the Version 2.1 GNU Lesser General
13 // Public License as published by the Free Software Foundation.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
23 // Boston, MA 02110-1301 USA
24 //
25 //-----------------------------------------------------------------------el-
26 
27 /* test on Units management
28  * - is_homogeneous
29  * - factor_to_some_unit
30  * - developSymbol
31  * - harmonizedSymbol
32  * - operations:
33  * - addition
34  * - substraction
35  * - multiplication
36  * - multiple combinations with parenthesises
37  */
38 
39 // C++
40 #include <limits>
41 #include <vector>
42 
43 //Antioch
44 #include "antioch/units.h"
45 #include "antioch/unit_defs.h"
46 
47 template <typename T>
49 {
50  Antioch::Units<T> test("W");
51 
52  if(!test.is_homogeneous("J/s"))return 1;
53  if(!test.is_homogeneous("N.m.s-1"))return 2;
54  if(!test.is_homogeneous("kg.m2/s3"))return 3;
55  if(!test.is_homogeneous("m2/kg-1.s-3"))return 4;
56  if(!test.is_homogeneous("m.m.kg/s/s/s"))return 5;
57  if(!test.is_homogeneous("erg/s"))return 6;
58 
59  std::cout << "Homogeneity done" << std::endl;
60 
61  return 0;
62 
63 }
64 
65 template <typename T>
66 int test_factor(int nAddition)
67 {
68  using std::abs;
69 
70  int return_flag(0);
71 
72  const T tol = std::numeric_limits<T>::epsilon() * 2.;
73  const T unity = 1.L;
74 
75 // testing SI derived
76  Antioch::Units<T> test("W");
77  if(abs((test.get_SI_factor()-unity)/unity) > tol)
78  {
79  std::cerr << std::scientific << std::setprecision(16)
80  << "mismatch in unit SI description, unity is not found" << std::endl
81  << "unit is " << test << std::endl;
82  return_flag = 1;
83  }
84  test.set_unit("J");
85  if(abs((test.get_SI_factor()-unity)/unity) > tol)
86  {
87  std::cerr << std::scientific << std::setprecision(16)
88  << "mismatch in unit SI description, unity is not found" << std::endl
89  << "unit is " << test << std::endl;
90  return_flag = 1;
91  }
92  test.set_unit("Hz");
93  if(abs((test.get_SI_factor()-unity)/unity) > tol)
94  {
95  std::cerr << std::scientific << std::setprecision(16)
96  << "mismatch in unit SI description, unity is not found" << std::endl
97  << "unit is " << test << std::endl;
98  return_flag = 1;
99  }
100  test.set_unit("N");
101  if(abs((test.get_SI_factor()-unity)/unity) > tol)
102  {
103  std::cerr << std::scientific << std::setprecision(16)
104  << "mismatch in unit SI description, unity is not found" << std::endl
105  << "unit is " << test << std::endl;
106  return_flag = 1;
107  }
108  test.set_unit("Pa");
109  if(abs((test.get_SI_factor()-unity)/unity) > tol)
110  {
111  std::cerr << std::scientific << std::setprecision(16)
112  << "mismatch in unit SI description, unity is not found" << std::endl
113  << "unit is " << test << std::endl;
114  return_flag = 1;
115  }
116  test.set_unit("C");
117  if(abs((test.get_SI_factor()-unity)/unity) > tol)
118  {
119  std::cerr << std::scientific << std::setprecision(16)
120  << "mismatch in unit SI description, unity is not found" << std::endl
121  << "unit is " << test << std::endl;
122  return_flag = 1;
123  }
124  test.set_unit("Bq");
125  if(abs((test.get_SI_factor()-unity)/unity) > tol)
126  {
127  std::cerr << std::scientific << std::setprecision(16)
128  << "mismatch in unit SI description, unity is not found" << std::endl
129  << "unit is " << test << std::endl;
130  return_flag = 1;
131  }
132  test.set_unit("kg");
133  if(abs((test.get_SI_factor()-unity)/unity) > tol)
134  {
135  std::cerr << std::scientific << std::setprecision(16)
136  << "mismatch in unit SI description, unity is not found" << std::endl
137  << "unit is " << test << std::endl;
138  return_flag = 1;
139  }
140  test.set_unit("mg");
141  T milli(1e-6L);
142  if(abs((test.get_SI_factor()-milli)/milli) > tol)
143  {
144  std::cerr << std::scientific << std::setprecision(16)
145  << "mismatch in unit SI description, unity is not found" << std::endl
146  << "unit is " << test << std::endl;
147  return_flag = 1;
148  }
149 
150 
151 
152 
153 //testing composed unit
154  srand(time(0)); //seed
155  std::string uu("m");
156  std::vector<std::string> add;
157  for(int i = 0; i < nAddition; i++)
158  {
159  int indice = rand() % Antioch::UnitBaseStorage::known_units().n_known_units();
160  add.push_back(Antioch::UnitBaseStorage::known_units().stored(indice).symbol());
161  }
162 
163  int ndot(0);
164  for(int i = 0; i < nAddition; i++)
165  {
166  int indice(0);
167  if(ndot > 0)indice = rand() % ndot;
168  size_t where(0);
169  for(int j = 0; j < indice; j++)
170  {
171  where = uu.find(".",where);
172  }
173  if(where == 0)where = uu.size();
174  std::string ad = "." + add[i];
175  uu.insert(where,ad);
176 
177  if(ndot > 0)indice = rand() % ndot;
178  where = 0;
179  for(int j = 0; j < indice; j++)
180  {
181  where = uu.find(".",where);
182  }
183  if(where == 0)where = uu.size();
184  ad = "/" + add[i];
185  uu.insert(where,ad);
186  ndot++;
187  }
188 
189  test.set_unit(uu);
190  if(abs((test.get_SI_factor()-unity)/unity) > tol)
191  {
192  std::cerr << std::scientific << std::setprecision(16)
193  << "mismatch in unit combination, unity is not found" << std::endl
194  << "unit is " << test << std::endl;
195  return_flag = 1;
196  }
197 
198  std::cout << "Combination done at ";
199  (nAddition < 100)?std::cout << "low":std::cout << "high";
200  std::cout << " level" << std::endl;
201 
202  T RJ = 8.3144621L;
203  T Rcal = 1.9858775L;
204  T Runc = 0.0000034L;
205  test.set_unit("J/mol/K");
206  Antioch::Units<T> sec("cal/mol/K");
207  T Rcalc = RJ * test.factor_to_some_unit("cal/mol/K");
208  if(abs((Rcalc - Rcal)/Rcal) > tol && abs((Rcalc - Rcal)/Rcal) > abs(Runc/Rcal))
209  {
210  std::cerr << std::scientific << std::setprecision(16)
211  << "mismatch in R calculation" << std::endl
212  << "Rtheo = " << Rcal << " cal/mol/K" << std::endl
213  << "Rcal = " << Rcalc << " cal/mol/K" << std::endl
214  << "relative error = " << abs((Rcalc - Rcal)/Rcal) << std::endl
215  << "uncertainty in calorie = " << Runc << std::endl
216  << "tol = " << tol << std::endl;
217  return_flag = 1;
218  }
219 
220  // now combining with parenthesises E = m * c * c, [E] = [m] + [c] + [c]
221  Antioch::Units<T> m("g"), c("m/s");
222  Antioch::Units<T> E = m + c + c;
223 
224  if(!E.is_homogeneous("J"))
225  {
226  std::cerr << "E = m * c *c failed, output unit is " << E.get_symbol() << std::endl;
227  return_flag = 1;
228  }
229 
230  return return_flag;
231 }
232 
233 template <typename T>
234 int tester()
235 {
236  return (
237  test_homogeneity<T>() ||
238  test_factor<T>(10) ||
239  test_factor<T>(200)
240  );
241 }
242 
243 
244 int main()
245 {
246  return (
247  tester<float>() ||
248  tester<double>() ||
249  tester<long double>()
250  );
251 }
int tester()
Definition: units_unit.C:234
const std::string get_symbol() const
String symbol getter.
Definition: units.h:330
int test_homogeneity()
Definition: units_unit.C:48
void set_unit(const std::string &sym, std::string na)
Units setter, sets the unit to the given symbol and name.
Definition: units.h:1144
T get_SI_factor() const
Multiplicative coefficient getter.
Definition: units.h:334
T factor_to_some_unit(const Units< T > &target) const
Calculates the factor to any given unit.
Definition: units.h:755
Advanced unit class.
int test_factor(int nAddition)
Definition: units_unit.C:66
int main()
Definition: units_unit.C:244
const UnitBaseConstant::UnitBaseStore< long double > known_units()
Definition: unit_defs.h:65
bool is_homogeneous(const Units< T > &rhs) const
Homogenity testing with another Units.
Definition: units.h:892
An advanced unit class.
Definition: units.h:111

Generated on Thu Jul 7 2016 11:09:47 for antioch-0.4.0 by  doxygen 1.8.8