• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Units - A C++ library for zero-overhead dimensional analysis and
2 // unit/quantity manipulation and conversion
3 //
4 // Copyright (C) 2003-2008 Matthias Christian Schabel
5 // Copyright (C) 2008 Steven Watanabe
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See
8 // accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 
11 /**
12 \file
13 
14 \brief kitchen_sink.cpp
15 
16 \details
17 More extensive quantity tests.
18 
19 Output:
20 @verbatim
21 
22 //[kitchen_sink_output_1
23 S1 :    2
24 X1 :    2
25 X2 :    (4/3)
26 U1 :    N
27 U2 :    J
28 Q1 :    1 N
29 Q2 :    2 J
30 //]
31 
32 //[kitchen_sink_output_2
33 U1*S1 : 2 N
34 S1*U1 : 2 N
35 U1/S1 : 0.5 N
36 S1/U1 : 2 m^-1 kg^-1 s^2
37 //]
38 
39 //[kitchen_sink_output_3
40 U1+U1 : N
41 U1-U1 : N
42 U1*U1 : m^2 kg^2 s^-4
43 U1/U1 : dimensionless
44 U1*U2 : m^3 kg^2 s^-4
45 U1/U2 : m^-1
46 U1^X  : m^2 kg^2 s^-4
47 X1vU1 : m^(1/2) kg^(1/2) s^-1
48 U1^X2 : m^(4/3) kg^(4/3) s^(-8/3)
49 X2vU1 : m^(3/4) kg^(3/4) s^(-3/2)
50 //]
51 
52 //[kitchen_sink_output_4
53 Q1*S1 : 2 N
54 S1*Q1 : 2 N
55 Q1/S1 : 0.5 N
56 S1/Q1 : 2 m^-1 kg^-1 s^2
57 //]
58 
59 //[kitchen_sink_output_5
60 U1*Q1 : 1 m^2 kg^2 s^-4
61 Q1*U1 : 1 m^2 kg^2 s^-4
62 U1/Q1 : 1 dimensionless
63 Q1/U1 : 1 dimensionless
64 //]
65 
66 //[kitchen_sink_output_6
67 +Q1   : 1 N
68 -Q1   : -1 N
69 Q1+Q1 : 2 N
70 Q1-Q1 : 0 N
71 Q1*Q1 : 1 m^2 kg^2 s^-4
72 Q1/Q1 : 1 dimensionless
73 Q1*Q2 : 2 m^3 kg^2 s^-4
74 Q1/Q2 : 0.5 m^-1
75 Q1^X1 : 1 m^2 kg^2 s^-4
76 X1vQ1 : 1 m^(1/2) kg^(1/2) s^-1
77 Q1^X2 : 1 m^(4/3) kg^(4/3) s^(-8/3)
78 X2vQ1 : 1 m^(3/4) kg^(3/4) s^(-3/2)
79 //]
80 
81 //[kitchen_sink_output_7
82 l1 == l2    false
83 l1 != l2    true
84 l1 <= l2    true
85 l1 < l2     true
86 l1 >= l2    false
87 l1 > l2     false
88 //]
89 
90 dimless = 1
91 
92 //[kitchen_sink_output_8
93 v1 = 2 m s^-1
94 //]
95 
96 //[kitchen_sink_output_9
97 F  = 1 N
98 dx = 1 m
99 E  = 1 J
100 //]
101 
102 //[kitchen_sink_output_10
103 r = 5e-07 m
104 P = 101325 Pa
105 V = 5.23599e-19 m^3
106 T = 310 K
107 n = 2.05835e-17 mol
108 R = 8.314472 m^2 kg s^-2 K^-1 mol^-1 (rel. unc. = 1.8e-06)
109 //]
110 
111 //[kitchen_sink_output_11
112 theta            = 0.375 rd
113 sin(theta)       = 0.366273 dimensionless
114 asin(sin(theta)) = 0.375 rd
115 //]
116 
117 //[kitchen_sink_output_12
118 V   = (12.5,0) V
119 I   = (3,4) A
120 Z   = (1.5,-2) Ohm
121 I*Z = (12.5,0) V
122 //]
123 
124 //[kitchen_sink_output_13
125 x+y-w         = 0.48(+/-0.632772) m
126 w*x           = 9.04(+/-0.904885) m^2
127 x/y           = 0.666667(+/-0.149071) dimensionless
128 //]
129 
130 //[kitchen_sink_output_14
131 w*y^2/(u*x)^2 = 10.17(+/-3.52328) m^-1
132 w/(u*x)^(1/2) = 3.19612(+/-0.160431) dimensionless
133 //]
134 
135 //[kitchen_sink_output_15
136 I*w   = m^2 kg s^-1 rad^-1
137 I*w/L = dimensionless
138 I*w^2 = J
139 //]
140 
141 //[kitchen_sink_output_16
142 1 F
143 1 kat
144 1 S
145 1 C
146 1 V
147 1 J
148 1 N
149 1 Hz
150 1 lx
151 1 H
152 1 lm
153 1 Wb
154 1 T
155 1 W
156 1 Pa
157 1 Ohm
158 //]
159 
160 //[kitchen_sink_output_18
161 1 farad
162 1 katal
163 1 siemen
164 1 coulomb
165 1 volt
166 1 joule
167 1 newton
168 1 hertz
169 1 lux
170 1 henry
171 1 lumen
172 1 weber
173 1 tesla
174 1 watt
175 1 pascal
176 1 ohm
177 //]
178 
179 @endverbatim
180 **/
181 
182 #include <cmath>
183 #include <complex>
184 #include <iostream>
185 
186 #include <boost/typeof/std/complex.hpp>
187 
188 #include <boost/units/cmath.hpp>
189 #include <boost/units/io.hpp>
190 #include <boost/units/systems/si.hpp>
191 #include <boost/units/systems/si/codata/physico-chemical_constants.hpp>
192 #include <boost/units/systems/si/io.hpp>
193 
194 #include "measurement.hpp"
195 
196 namespace boost {
197 
198 namespace units {
199 
200 //[kitchen_sink_function_snippet_3
201 /// the physical definition of work - computed for an arbitrary unit system
202 template<class System,class Y>
203 constexpr
204 quantity<unit<energy_dimension,System>,Y>
work(quantity<unit<force_dimension,System>,Y> F,quantity<unit<length_dimension,System>,Y> dx)205 work(quantity<unit<force_dimension,System>,Y> F,
206      quantity<unit<length_dimension,System>,Y> dx)
207 {
208     return F*dx;
209 }
210 //]
211 
212 //[kitchen_sink_function_snippet_4
213 /// the ideal gas law in si units
214 template<class Y>
215 constexpr
216 quantity<si::amount,Y>
idealGasLaw(const quantity<si::pressure,Y> & P,const quantity<si::volume,Y> & V,const quantity<si::temperature,Y> & T)217 idealGasLaw(const quantity<si::pressure,Y>& P,
218             const quantity<si::volume,Y>& V,
219             const quantity<si::temperature,Y>& T)
220 {
221     using namespace boost::units::si;
222 
223     using namespace constants::codata;
224     return (P*V/(R*T));
225 }
226 //]
227 
228 } // namespace units
229 
230 } // namespace boost
231 
main()232 int main()
233 {
234     using namespace boost::units;
235     using namespace boost::units::si;
236 
237     {
238     //[kitchen_sink_snippet_1
239     /// scalar
240     const double    s1 = 2;
241 
242     const long                  x1 = 2;
243     const static_rational<4,3>  x2;
244 
245     /// define some units
246     force       u1 = newton;
247     energy      u2 = joule;
248 
249     /// define some quantities
250     quantity<force>      q1(1.0*u1);
251     quantity<energy>     q2(2.0*u2);
252     //]
253 
254     /// check scalar, unit, and quantity io
255     std::cout << "S1 :    " << s1 << std::endl
256               << "X1 :    " << x1 << std::endl
257               << "X2 :    " << x2 << std::endl
258               << "U1 :    " << u1 << std::endl
259               << "U2 :    " << u2 << std::endl
260               << "Q1 :    " << q1 << std::endl
261               << "Q2 :    " << q2 << std::endl
262               << std::endl;
263 
264     /// check scalar-unit algebra
265     std::cout //<< "U1+S1 : " << u1+s1 << std::endl    // illegal
266               //<< "S1+U1 : " << s1+u1 << std::endl    // illegal
267               //<< "U1-S1 : " << u1-s1 << std::endl    // illegal
268               //<< "S1-U1 : " << s1-u1 << std::endl    // illegal
269               << "U1*S1 : " << u1*s1 << std::endl
270               << "S1*U1 : " << s1*u1 << std::endl
271               << "U1/S1 : " << u1/s1 << std::endl
272               << "S1/U1 : " << s1/u1 << std::endl
273               << std::endl;
274 
275     /// check unit-unit algebra
276     std::cout << "U1+U1 : " << u1+u1 << std::endl
277               << "U1-U1 : " << u1-u1 << std::endl
278               << "U1*U1 : " << u1*u1 << std::endl
279               << "U1/U1 : " << u1/u1 << std::endl
280               //<< "U1+U2 : " << u1+u2 << std::endl     // illegal
281               //<< "U1-U2 : " << u1-u2 << std::endl     // illegal
282               << "U1*U2 : " << u1*u2 << std::endl
283               << "U1/U2 : " << u1/u2 << std::endl
284               << "U1^X  : " << pow<2>(u1) << std::endl
285               << "X1vU1 : " << root<2>(u1) << std::endl
286               << "U1^X2 : " << pow<static_rational<4,3> >(u1) << std::endl
287               << "X2vU1 : " << root<static_rational<4,3> >(u1) << std::endl
288               << std::endl;
289 
290     /// check scalar-quantity algebra
291     std::cout //<< "Q1+S1 : " << q1+s1 << std::endl    // illegal
292               //<< "S1+Q1 : " << s1+q1 << std::endl    // illegal
293               //<< "Q1-S1 : " << q1-s1 << std::endl    // illegal
294               //<< "S1-Q1 : " << s1-q1 << std::endl    // illegal
295               << "Q1*S1 : " << q1*s1 << std::endl
296               << "S1*Q1 : " << s1*q1 << std::endl
297               << "Q1/S1 : " << q1/s1 << std::endl
298               << "S1/Q1 : " << s1/q1 << std::endl
299               << std::endl;
300 
301     /// check unit-quantity algebra
302     std::cout //<< "U1+Q1 : " << u1+q1 << std::endl    // illegal
303               //<< "Q1+U1 : " << q1+u1 << std::endl    // illegal
304               //<< "U1-Q1 : " << u1-q1 << std::endl    // illegal
305               //<< "Q1-U1 : " << q1-u1 << std::endl    // illegal
306               << "U1*Q1 : " << u1*q1 << std::endl
307               << "Q1*U1 : " << q1*u1 << std::endl
308               << "U1/Q1 : " << u1/q1 << std::endl
309               << "Q1/U1 : " << q1/u1 << std::endl
310               << std::endl;
311 
312     /// check quantity-quantity algebra
313     std::cout << "+Q1   : " << +q1 << std::endl
314               << "-Q1   : " << -q1 << std::endl
315               << "Q1+Q1 : " << q1+q1 << std::endl
316               << "Q1-Q1 : " << q1-q1 << std::endl
317               << "Q1*Q1 : " << q1*q1 << std::endl
318               << "Q1/Q1 : " << q1/q1 << std::endl
319               //<< "Q1+Q2 : " << q1+q2 << std::endl    // illegal
320               //<< "Q1-Q2 : " << q1-q2 << std::endl    // illegal
321               << "Q1*Q2 : " << q1*q2 << std::endl
322               << "Q1/Q2 : " << q1/q2 << std::endl
323               << "Q1^X1 : " << pow<2>(q1) << std::endl
324               << "X1vQ1 : " << root<2>(q1) << std::endl
325               << "Q1^X2 : " << pow<static_rational<4,3> >(q1) << std::endl
326               << "X2vQ1 : " << root<static_rational<4,3> >(q1) << std::endl
327               << std::endl;
328 
329     //[kitchen_sink_snippet_2
330     /// check comparison tests
331     quantity<length>    l1(1.0*meter),
332                         l2(2.0*meters);
333     //]
334 
335     std::cout << std::boolalpha
336               << "l1 == l2" << "\t" << (l1 == l2) << std::endl
337               << "l1 != l2" << "\t" << (l1 != l2) << std::endl
338               << "l1 <= l2" << "\t" << (l1 <= l2) << std::endl
339               << "l1 < l2 " << "\t" << (l1  < l2) << std::endl
340               << "l1 >= l2" << "\t" << (l1 >= l2) << std::endl
341               << "l1 > l2 " << "\t" << (l1  > l2) << std::endl
342               << std::endl;
343 
344     //[kitchen_sink_snippet_3
345     /// check implicit unit conversion from dimensionless to value_type
346     const double    dimless = (q1/q1);
347     //]
348 
349     std::cout << "dimless = " << dimless << std::endl
350               << std::endl;
351 
352     quantity<velocity>  v1 = 2.0*meters/second;
353 
354     std::cout << "v1 = " << v1 << std::endl
355               << std::endl;
356 
357     //[kitchen_sink_snippet_4
358     /// test calcuation of work
359     quantity<force>       F(1.0*newton);
360     quantity<length>      dx(1.0*meter);
361     quantity<energy>      E(work(F,dx));
362     //]
363 
364     std::cout << "F  = " << F << std::endl
365               << "dx = " << dx << std::endl
366               << "E  = " << E << std::endl
367               << std::endl;
368 
369     {
370     //[kitchen_sink_snippet_5
371     /// test ideal gas law
372     quantity<temperature>   T = (273.+37.)*kelvin;
373     quantity<pressure>      P = 1.01325e5*pascals;
374     quantity<length>        r = 0.5e-6*meters;
375     quantity<volume>        V = (4.0/3.0)*3.141592*pow<3>(r);
376     quantity<amount>        n(idealGasLaw(P,V,T));
377     //]
378 
379     std::cout << "r = " << r << std::endl
380               << "P = " << P << std::endl
381               << "V = " << V << std::endl
382               << "T = " << T << std::endl
383               << "n = " << n << std::endl
384               #if BOOST_UNITS_HAS_TYPEOF
385               << "R = " << constants::codata::R << std::endl
386               #else
387               << "no typeof" << std::endl
388               #endif // BOOST_UNITS_HAS_TYPEOF
389               << std::endl;
390     }
391 
392     //[kitchen_sink_snippet_6
393     /// test trig stuff
394     quantity<plane_angle>           theta = 0.375*radians;
395     quantity<dimensionless>         sin_theta = sin(theta);
396     quantity<plane_angle>           thetap = asin(sin_theta);
397     //]
398 
399     std::cout << "theta            = " << theta << std::endl
400               << "sin(theta)       = " << sin_theta << std::endl
401               << "asin(sin(theta)) = " << thetap << std::endl
402               << std::endl;
403 
404     /// test implicit conversion of dimensionless to value
405     double  tmp = sin_theta;
406 
407     tmp = sin_theta;
408 
409     /// test implicit conversion from value to dimensionless
410     quantity<dimensionless>     tmpp = tmp;
411 
412     tmpp = tmp;
413 
414     /// check complex quantities
415     typedef std::complex<double>    complex_type;
416 
417     //[kitchen_sink_snippet_7
418     quantity<electric_potential,complex_type> v = complex_type(12.5,0.0)*volts;
419     quantity<current,complex_type>            i = complex_type(3.0,4.0)*amperes;
420     quantity<resistance,complex_type>         z = complex_type(1.5,-2.0)*ohms;
421     //]
422 
423     std::cout << "V   = " << v << std::endl
424               << "I   = " << i << std::endl
425               << "Z   = " << z << std::endl
426               << "I*Z = " << i*z << std::endl
427               << std::endl;
428 
429     /// check quantities using user-defined type encapsulating error propagation
430 
431     //[kitchen_sink_snippet_8
432     quantity<length,measurement<double> >
433         u(measurement<double>(1.0,0.0)*meters),
434         w(measurement<double>(4.52,0.02)*meters),
435         x(measurement<double>(2.0,0.2)*meters),
436         y(measurement<double>(3.0,0.6)*meters);
437     //]
438 
439     std::cout << "x+y-w         = " << x+y-w << std::endl
440               << "w*x           = " << w*x << std::endl
441               << "x/y           = " << x/y << std::endl
442               << "w*y^2/(u*x)^2 = " << w*y*y/pow<2>(u*x) << std::endl
443               << "w/(u*x)^(1/2) = " << w/pow< static_rational<1,2> >(u*x)
444               << std::endl << std::endl;
445     }
446 
447     /// check moment of inertia/angular momentum/rotational energy
448 
449     //[kitchen_sink_snippet_9
450     std::cout << symbol_format
451               << "I*w   = " << moment_of_inertia()*angular_velocity() << std::endl
452               << "I*w/L = " << moment_of_inertia()*angular_velocity()/angular_momentum() << std::endl
453               << "I*w^2 = " << moment_of_inertia()*pow<2>(angular_velocity()) << std::endl
454               << std::endl;
455     //]
456 
457     //[kitchen_sink_snippet_10
458 //    std::cout << typename_format
459 //              << quantity<capacitance>(1.0*farad) << std::endl
460 //              << quantity<catalytic_activity>(1.0*katal) << std::endl
461 //              << quantity<conductance>(1.0*siemen) << std::endl
462 //              << quantity<electric_charge>(1.0*coulomb) << std::endl
463 //              << quantity<electric_potential>(1.0*volt) << std::endl
464 //              << quantity<energy>(1.0*joule) << std::endl
465 //              << quantity<force>(1.0*newton) << std::endl
466 //              << quantity<frequency>(1.0*hertz) << std::endl
467 //              << quantity<illuminance>(1.0*lux) << std::endl
468 //              << quantity<inductance>(1.0*henry) << std::endl
469 //              << quantity<luminous_flux>(1.0*lumen) << std::endl
470 //              << quantity<magnetic_flux>(1.0*weber) << std::endl
471 //              << quantity<magnetic_flux_density>(1.0*tesla) << std::endl
472 //              << quantity<power>(1.0*watt) << std::endl
473 //              << quantity<pressure>(1.0*pascals) << std::endl
474 //              << quantity<resistance>(1.0*ohm) << std::endl
475 //              << std::endl;
476     //]
477 
478     //[kitchen_sink_snippet_11
479 //    std::cout << raw_format
480 //              << quantity<capacitance>(1.0*farad) << std::endl
481 //              << quantity<catalytic_activity>(1.0*katal) << std::endl
482 //              << quantity<conductance>(1.0*siemen) << std::endl
483 //              << quantity<electric_charge>(1.0*coulomb) << std::endl
484 //              << quantity<electric_potential>(1.0*volt) << std::endl
485 //              << quantity<energy>(1.0*joule) << std::endl
486 //              << quantity<force>(1.0*newton) << std::endl
487 //              << quantity<frequency>(1.0*hertz) << std::endl
488 //              << quantity<illuminance>(1.0*lux) << std::endl
489 //              << quantity<inductance>(1.0*henry) << std::endl
490 //              << quantity<luminous_flux>(1.0*lumen) << std::endl
491 //              << quantity<magnetic_flux>(1.0*weber) << std::endl
492 //              << quantity<magnetic_flux_density>(1.0*tesla) << std::endl
493 //              << quantity<power>(1.0*watt) << std::endl
494 //              << quantity<pressure>(1.0*pascals) << std::endl
495 //              << quantity<resistance>(1.0*ohm) << std::endl
496 //              << std::endl;
497     //]
498 
499     //[kitchen_sink_snippet_12
500     std::cout << symbol_format
501               << quantity<capacitance>(1.0*farad) << std::endl
502               << quantity<catalytic_activity>(1.0*katal) << std::endl
503               << quantity<conductance>(1.0*siemen) << std::endl
504               << quantity<electric_charge>(1.0*coulomb) << std::endl
505               << quantity<electric_potential>(1.0*volt) << std::endl
506               << quantity<energy>(1.0*joule) << std::endl
507               << quantity<force>(1.0*newton) << std::endl
508               << quantity<frequency>(1.0*hertz) << std::endl
509               << quantity<illuminance>(1.0*lux) << std::endl
510               << quantity<inductance>(1.0*henry) << std::endl
511               << quantity<luminous_flux>(1.0*lumen) << std::endl
512               << quantity<magnetic_flux>(1.0*weber) << std::endl
513               << quantity<magnetic_flux_density>(1.0*tesla) << std::endl
514               << quantity<power>(1.0*watt) << std::endl
515               << quantity<pressure>(1.0*pascals) << std::endl
516               << quantity<resistance>(1.0*ohm) << std::endl
517               << std::endl;
518     //]
519 
520     //[kitchen_sink_snippet_13
521     std::cout << name_format
522               << quantity<capacitance>(1.0*farad) << std::endl
523               << quantity<catalytic_activity>(1.0*katal) << std::endl
524               << quantity<conductance>(1.0*siemen) << std::endl
525               << quantity<electric_charge>(1.0*coulomb) << std::endl
526               << quantity<electric_potential>(1.0*volt) << std::endl
527               << quantity<energy>(1.0*joule) << std::endl
528               << quantity<force>(1.0*newton) << std::endl
529               << quantity<frequency>(1.0*hertz) << std::endl
530               << quantity<illuminance>(1.0*lux) << std::endl
531               << quantity<inductance>(1.0*henry) << std::endl
532               << quantity<luminous_flux>(1.0*lumen) << std::endl
533               << quantity<magnetic_flux>(1.0*weber) << std::endl
534               << quantity<magnetic_flux_density>(1.0*tesla) << std::endl
535               << quantity<power>(1.0*watt) << std::endl
536               << quantity<pressure>(1.0*pascals) << std::endl
537               << quantity<resistance>(1.0*ohm) << std::endl
538               << std::endl;
539     //]
540 
541     return 0;
542 }
543