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/dynamics.h>
11
12 int
zsl_phy_dyn_newton(zsl_real_t m,zsl_real_t accel,zsl_real_t * f)13 zsl_phy_dyn_newton(zsl_real_t m, zsl_real_t accel, zsl_real_t *f)
14 {
15 if (m < 0) {
16 *f = NAN;
17 return -EINVAL;
18 }
19
20 *f = m * accel;
21
22 return 0;
23 }
24
25 int
zsl_phy_dyn_fric(zsl_real_t n,zsl_real_t mu,zsl_real_t * f)26 zsl_phy_dyn_fric(zsl_real_t n, zsl_real_t mu, zsl_real_t *f)
27 {
28 if (mu < 0 || mu > 1) {
29 *f = NAN;
30 return -EINVAL;
31 }
32
33 *f = n * mu;
34
35 return 0;
36 }
37
38 int
zsl_phy_dyn_normal(zsl_real_t m,zsl_real_t theta,zsl_real_t * n)39 zsl_phy_dyn_normal(zsl_real_t m, zsl_real_t theta, zsl_real_t *n)
40 {
41 if (m < 0) {
42 *n = NAN;
43 return -EINVAL;
44 }
45
46 *n = m * ZSL_GRAV_EARTH * ZSL_COS(theta);
47
48 return 0;
49 }
50
51 int
zsl_phy_dyn_pendul_period(zsl_real_t l,zsl_real_t * t)52 zsl_phy_dyn_pendul_period(zsl_real_t l, zsl_real_t *t)
53 {
54 if (l < 0) {
55 *t = NAN;
56 return -EINVAL;
57 }
58
59 *t = 2.0 * ZSL_PI * ZSL_SQRT(l / ZSL_GRAV_EARTH);
60
61 return 0;
62 }
63
64 int
zsl_phy_dyn_pendul_max_speed(zsl_real_t l,zsl_real_t oi,zsl_real_t * vmax)65 zsl_phy_dyn_pendul_max_speed(zsl_real_t l, zsl_real_t oi, zsl_real_t *vmax)
66 {
67 if (l < 0) {
68 *vmax = NAN;
69 return -EINVAL;
70 }
71
72 *vmax = ZSL_SQRT(2.0 * ZSL_GRAV_EARTH * l * (1.0 - ZSL_COS(oi)));
73
74 return 0;
75 }
76
77 int
zsl_phy_dyn_tension(zsl_real_t m,zsl_real_t a,zsl_real_t * t)78 zsl_phy_dyn_tension(zsl_real_t m, zsl_real_t a, zsl_real_t *t)
79 {
80 if (m < 0) {
81 *t = NAN;
82 return -EINVAL;
83 }
84
85 *t = m * ZSL_GRAV_EARTH + m * a;
86
87 return 0;
88 }
89
90 int
zsl_phy_dyn_lever(zsl_real_t d1,zsl_real_t f1,zsl_real_t d2,zsl_real_t * f2)91 zsl_phy_dyn_lever(zsl_real_t d1, zsl_real_t f1, zsl_real_t d2, zsl_real_t *f2)
92 {
93 if (d1 < 0 || d2 <= 0) {
94 *f2 = NAN;
95 return -EINVAL;
96 }
97
98 *f2 = (d1 * f1) / d2;
99
100 return 0;
101 }
102