kitchen_sink.cpp 17 KB

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