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/energy.h>
11
12 int
zsl_phy_ener_kin(zsl_real_t v,zsl_real_t m,zsl_real_t * ke)13 zsl_phy_ener_kin(zsl_real_t v, zsl_real_t m, zsl_real_t *ke)
14 {
15 if (m < 0) {
16 *ke = NAN;
17 return -EINVAL;
18 }
19
20 *ke = 0.5 * m * v * v;
21
22 return 0;
23 }
24
25 int
zsl_phy_ener_rot(zsl_real_t omega,zsl_real_t i,zsl_real_t * rke)26 zsl_phy_ener_rot(zsl_real_t omega, zsl_real_t i, zsl_real_t *rke)
27 {
28 if (i < 0) {
29 *rke = NAN;
30 return -EINVAL;
31 }
32
33 *rke = 0.5 * i * omega * omega;
34
35 return 0;
36 }
37
38 int
zsl_phy_ener_grav_pot(zsl_real_t m,zsl_real_t h,zsl_real_t * ug)39 zsl_phy_ener_grav_pot(zsl_real_t m, zsl_real_t h, zsl_real_t *ug)
40 {
41 if (m < 0) {
42 *ug = NAN;
43 return -EINVAL;
44 }
45
46 *ug = m * ZSL_GRAV_EARTH * h;
47
48 return 0;
49 }
50
51 int
zsl_phy_ener_elas_pot(zsl_real_t k,zsl_real_t dist,zsl_real_t * ue)52 zsl_phy_ener_elas_pot(zsl_real_t k, zsl_real_t dist, zsl_real_t *ue)
53 {
54 if (k < 0) {
55 *ue = NAN;
56 return -EINVAL;
57 }
58
59 *ue = 0.5 * k * dist * dist;
60
61 return 0;
62 }
63
64 int
zsl_phy_ener_power(zsl_real_t ener,zsl_real_t time,zsl_real_t * power)65 zsl_phy_ener_power(zsl_real_t ener, zsl_real_t time, zsl_real_t *power)
66 {
67 if (time <= 0) {
68 *power = NAN;
69 return -EINVAL;
70 }
71
72 *power = ener / time;
73
74 return 0;
75 }
76
77 int
zsl_phy_ener_fric(zsl_real_t fric,zsl_real_t dist,zsl_real_t * e)78 zsl_phy_ener_fric(zsl_real_t fric, zsl_real_t dist, zsl_real_t *e)
79 {
80 if (dist < 0 || fric < 0) {
81 *e = NAN;
82 return -EINVAL;
83 }
84
85 *e = -fric * dist;
86
87 return 0;
88 }
89
90 int
zsl_phy_ener_mec(zsl_real_t ke,zsl_real_t rke,zsl_real_t ug,zsl_real_t ue,zsl_real_t * me)91 zsl_phy_ener_mec(zsl_real_t ke, zsl_real_t rke, zsl_real_t ug, zsl_real_t ue,
92 zsl_real_t *me)
93 {
94 *me = ke + rke + ug + ue;
95
96 return 0;
97 }
98
99 int
zsl_phy_ener_final(zsl_real_t ei1,zsl_real_t ei2,zsl_real_t ei3,zsl_real_t ei4,zsl_real_t ef1,zsl_real_t ef2,zsl_real_t ef3,zsl_real_t * ef4)100 zsl_phy_ener_final(zsl_real_t ei1, zsl_real_t ei2, zsl_real_t ei3,
101 zsl_real_t ei4, zsl_real_t ef1, zsl_real_t ef2,
102 zsl_real_t ef3, zsl_real_t *ef4)
103 {
104 /* According to the conservation of mechanical energy, the equation
105 * ei1 + ei2 + ei3 + ei4 = ef1 + ef2 + ef3 + ef4 must be always true. */
106 *ef4 = ei1 + ei2 + ei3 + ei4 - ef1 - ef2 - ef3;
107
108 return 0;
109 }
110
111 int
zsl_phy_ener_photon(zsl_real_t f,zsl_real_t * e)112 zsl_phy_ener_photon(zsl_real_t f, zsl_real_t *e)
113 {
114 if (f < 0) {
115 *e = NAN;
116 return -EINVAL;
117 }
118
119 *e = (ZSL_PLANCK * f) / 1.602176634E-19;
120
121 return 0;
122 }
123
124 int
zsl_phy_ener_photon2(zsl_real_t lambda,zsl_real_t * e)125 zsl_phy_ener_photon2(zsl_real_t lambda, zsl_real_t *e)
126 {
127 if (lambda <= 0) {
128 *e = NAN;
129 return -EINVAL;
130 }
131
132 /* Scale lamba from nm to m. */
133 lambda *= 1E-9;
134
135 *e = (ZSL_PLANCK * ZSL_LIGHT_SPEED) / (lambda * 1.602176634E-19);
136
137 return 0;
138 }
139