1 /*
2 * Copyright (c) 2019 Kevin Townsend (KTOWN)
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <math.h>
8 #include <errno.h>
9 #include <zsl/zsl.h>
10 #include <zsl/physics/thermo.h>
11
12 int
zsl_phy_thermo_fahren_cels(zsl_real_t t1,zsl_real_t * t2,bool ctf)13 zsl_phy_thermo_fahren_cels(zsl_real_t t1, zsl_real_t *t2, bool ctf)
14 {
15 if (ctf == true) {
16 *t2 = ((9 * t1) / 5) + 32;
17 return 0;
18 }
19
20 *t2 = ((t1 - 32) * 5) / 9;
21
22 return 0;
23 }
24
25 int
zsl_phy_thermo_cels_kel(zsl_real_t t1,zsl_real_t * t2,bool ktc)26 zsl_phy_thermo_cels_kel(zsl_real_t t1, zsl_real_t *t2, bool ktc)
27 {
28 if (ktc == true) {
29 *t2 = t1 - 273.15;
30 return 0;
31 }
32
33 *t2 = t1 + 273.15;
34
35 return 0;
36 }
37
38 int
zsl_phy_thermo_heat_fusion(zsl_real_t m,zsl_real_t lh,zsl_real_t * q)39 zsl_phy_thermo_heat_fusion(zsl_real_t m, zsl_real_t lh, zsl_real_t *q)
40 {
41 if (m < 0 || lh < 0) {
42 *q = NAN;
43 return -EINVAL;
44 }
45
46 *q = m * lh;
47
48 return 0;
49 }
50
51 int
zsl_phy_thermo_heat(zsl_real_t m,zsl_real_t sh,zsl_real_t t,zsl_real_t * q)52 zsl_phy_thermo_heat(zsl_real_t m, zsl_real_t sh, zsl_real_t t, zsl_real_t *q)
53 {
54 if (m < 0 || sh < 0) {
55 *q = NAN;
56 return -EINVAL;
57 }
58
59 *q = m * sh * t;
60
61 return 0;
62 }
63
64 int
zsl_phy_thermo_expan(zsl_real_t l0,zsl_real_t a,zsl_real_t t,zsl_real_t * l)65 zsl_phy_thermo_expan(zsl_real_t l0, zsl_real_t a, zsl_real_t t, zsl_real_t *l)
66 {
67 if (l0 < 0 || a < 0) {
68 *l = NAN;
69 return -EINVAL;
70 }
71
72 *l = l0 + l0 * a * t;
73
74 return 0;
75 }
76
77 int
zsl_phy_thermo_mean_free_path(zsl_real_t nv,zsl_real_t d,zsl_real_t * lambda)78 zsl_phy_thermo_mean_free_path(zsl_real_t nv, zsl_real_t d, zsl_real_t *lambda)
79 {
80 if (nv <= 0 || d <= 0) {
81 *lambda = NAN;
82 return -EINVAL;
83 }
84
85 *lambda = 1.0E6 / (ZSL_PI * ZSL_SQRT(2.0) * nv * d * d);
86
87 return 0;
88 }
89
90 int
zsl_phy_thermo_effic_heat_engine(zsl_real_t qc,zsl_real_t qh,zsl_real_t * e)91 zsl_phy_thermo_effic_heat_engine(zsl_real_t qc, zsl_real_t qh, zsl_real_t *e)
92 {
93 if (qh == 0 || qc > qh) {
94 *e = NAN;
95 return -EINVAL;
96 }
97
98 *e = 1.0 - qc / qh;
99
100 return 0;
101 }
102
103 int
zsl_phy_thermo_carnot_engine(zsl_real_t tc,zsl_real_t qc,zsl_real_t th,zsl_real_t * qh)104 zsl_phy_thermo_carnot_engine(zsl_real_t tc, zsl_real_t qc, zsl_real_t th,
105 zsl_real_t *qh)
106 {
107 if (tc <= 0 || th < tc ) {
108 *qh = NAN;
109 return -EINVAL;
110 }
111
112 *qh = (qc * th) / tc;
113
114 return 0;
115 }
116