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/electric.h>
11
12 int
zsl_phy_elec_charge_dens(zsl_real_t q,zsl_real_t v,zsl_real_t * d)13 zsl_phy_elec_charge_dens(zsl_real_t q, zsl_real_t v, zsl_real_t *d)
14 {
15 if (v <= 0) {
16 *d = NAN;
17 return -EINVAL;
18 }
19
20 *d = q / v;
21
22 return 0;
23 }
24
25 int
zsl_phy_elec_force(zsl_real_t q1,zsl_real_t q2,zsl_real_t r,zsl_real_t * f)26 zsl_phy_elec_force(zsl_real_t q1, zsl_real_t q2, zsl_real_t r, zsl_real_t *f)
27 {
28 if (r <= 0) {
29 *f = NAN;
30 return -EINVAL;
31 }
32
33 *f = (ZSL_COULOMB * q1 * q2) / (r * r);
34
35 return 0;
36 }
37
38 int
zsl_phy_elec_force2(zsl_real_t q,zsl_real_t e,zsl_real_t * f)39 zsl_phy_elec_force2(zsl_real_t q, zsl_real_t e, zsl_real_t *f)
40 {
41 *f = q * e;
42
43 return 0;
44 }
45
46 int
zsl_phy_elec_field(zsl_real_t q,zsl_real_t r,zsl_real_t * e)47 zsl_phy_elec_field(zsl_real_t q, zsl_real_t r, zsl_real_t *e)
48 {
49 if (r <= 0) {
50 *e = NAN;
51 return -EINVAL;
52 }
53
54 *e = (ZSL_COULOMB * q) / (r * r);
55
56 return 0;
57 }
58
59 int
zsl_phy_elec_pot_ener(zsl_real_t q1,zsl_real_t q2,zsl_real_t r,zsl_real_t * u)60 zsl_phy_elec_pot_ener(zsl_real_t q1, zsl_real_t q2, zsl_real_t r, zsl_real_t *u)
61 {
62 if (r <= 0) {
63 *u = NAN;
64 return -EINVAL;
65 }
66
67 *u = (ZSL_COULOMB * q1 * q2) / r;
68
69 return 0;
70 }
71
72 int
zsl_phy_elec_potential(zsl_real_t q,zsl_real_t r,zsl_real_t * v)73 zsl_phy_elec_potential(zsl_real_t q, zsl_real_t r, zsl_real_t *v)
74 {
75 if (r <= 0) {
76 *v = NAN;
77 return -EINVAL;
78 }
79
80 *v = (ZSL_COULOMB * q) / r;
81
82 return 0;
83 }
84
85 int
zsl_phy_elec_flux(zsl_real_t e,zsl_real_t a,zsl_real_t theta,zsl_real_t * fl)86 zsl_phy_elec_flux(zsl_real_t e, zsl_real_t a, zsl_real_t theta, zsl_real_t *fl)
87 {
88 if (e < 0 || a < 0) {
89 *fl = NAN;
90 return -EINVAL;
91 }
92
93 *fl = e * a * ZSL_COS(theta);
94
95 return 0;
96 }
97