1 /*
2 * Copyright (c) 2019 Kevin Townsend (KTOWN)
3 * Copyright (c) 2021 Marti Riba Pons
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <math.h>
9 #include <errno.h>
10 #include <zsl/zsl.h>
11 #include <zsl/physics/electricity.h>
12
13 int
zsl_phy_elcty_current(zsl_real_t q,zsl_real_t t,zsl_real_t * i)14 zsl_phy_elcty_current(zsl_real_t q, zsl_real_t t, zsl_real_t *i)
15 {
16 if (t <= 0) {
17 *i = NAN;
18 return -EINVAL;
19 }
20
21 *i = q / t;
22
23 return 0;
24 }
25
26 int
zsl_phy_elcty_res_series(struct zsl_vec * v,zsl_real_t * r)27 zsl_phy_elcty_res_series(struct zsl_vec *v, zsl_real_t *r)
28 {
29 for (size_t j = 0; j < v->sz; j++) {
30 if (v->data[j] < 0) {
31 *r = NAN;
32 return -EINVAL;
33 }
34 }
35
36 *r = 0.0;
37 for (size_t j = 0; j < v->sz; j++) {
38 *r += v->data[j];
39 }
40
41 return 0;
42 }
43
44 int
zsl_phy_elcty_res_parallel(struct zsl_vec * v,zsl_real_t * r)45 zsl_phy_elcty_res_parallel(struct zsl_vec *v, zsl_real_t *r)
46 {
47 for (size_t j = 0; j < v->sz; j++) {
48 if (v->data[j] <= 0) {
49 *r = NAN;
50 return -EINVAL;
51 }
52 }
53
54 zsl_real_t r_temp = 0.0;
55
56 for (size_t j = 0; j < v->sz; j++) {
57 r_temp += 1. / v->data[j];
58 }
59
60 *r = 1. / r_temp;
61
62 return 0;
63 }
64
65 int
zsl_phy_elcty_cap_series(struct zsl_vec * v,zsl_real_t * c)66 zsl_phy_elcty_cap_series(struct zsl_vec *v, zsl_real_t *c)
67 {
68 for (size_t j = 0; j < v->sz; j++) {
69 if (v->data[j] <= 0) {
70 *c = NAN;
71 return -EINVAL;
72 }
73 }
74
75 zsl_real_t c_temp = 0.0;
76
77 for (size_t j = 0; j < v->sz; j++) {
78 c_temp += 1. / v->data[j];
79 }
80
81 *c = 1. / c_temp;
82
83 return 0;
84 }
85
86 int
zsl_phy_elcty_cap_parallel(struct zsl_vec * v,zsl_real_t * c)87 zsl_phy_elcty_cap_parallel(struct zsl_vec *v, zsl_real_t *c)
88 {
89 for (size_t j = 0; j < v->sz; j++) {
90 if (v->data[j] < 0) {
91 *c = NAN;
92 return -EINVAL;
93 }
94 }
95
96 *c = 0.0;
97 for (size_t j = 0; j < v->sz; j++) {
98 *c += v->data[j];
99 }
100
101 return 0;
102 }
103
104 int
zsl_phy_elcty_resistivity(zsl_real_t r,zsl_real_t a,zsl_real_t l,zsl_real_t * rty)105 zsl_phy_elcty_resistivity(zsl_real_t r, zsl_real_t a, zsl_real_t l, zsl_real_t *rty)
106 {
107 if (a < 0.0 || l <= 0.0 || r < 0.0) {
108 *rty = NAN;
109 return -EINVAL;
110 }
111
112 *rty = r * (a / l);
113
114 return 0;
115 }
116
117 int
zsl_phy_elcty_ohm_law(zsl_real_t i,zsl_real_t r,zsl_real_t * v)118 zsl_phy_elcty_ohm_law(zsl_real_t i, zsl_real_t r, zsl_real_t *v)
119 {
120 if (r < 0.0) {
121 *v = NAN;
122 return -EINVAL;
123 }
124
125 *v = i * r;
126
127 return 0;
128 }
129
130 int
zsl_phy_elcty_power_vi(zsl_real_t v,zsl_real_t i,zsl_real_t * p)131 zsl_phy_elcty_power_vi(zsl_real_t v, zsl_real_t i, zsl_real_t *p)
132 {
133 *p = v * i;
134
135 return 0;
136 }
137
138 int
zsl_phy_elcty_power_ir(zsl_real_t i,zsl_real_t r,zsl_real_t * p)139 zsl_phy_elcty_power_ir(zsl_real_t i, zsl_real_t r, zsl_real_t *p)
140 {
141 if (r < 0.0) {
142 *p = NAN;
143 return -EINVAL;
144 }
145
146 *p = i * i * r;
147
148 return 0;
149 }
150
151 int
zsl_phy_elcty_power_vr(zsl_real_t v,zsl_real_t r,zsl_real_t * p)152 zsl_phy_elcty_power_vr(zsl_real_t v, zsl_real_t r, zsl_real_t *p)
153 {
154 if (r <= 0.0) {
155 *p = NAN;
156 return -EINVAL;
157 }
158
159 *p = v * v / r;
160
161 return 0;
162 }
163