ac_power/
newtypes.rs

1// Copyright 2023 Enphase Energy, Inc and Universal Interoperability for
2// Grid-Forming Inverters (UNIFI) Consortium.
3//
4//    Licensed under the Apache License, Version 2.0 (the "License");
5//    you may not use this file except in compliance with the License.
6//    You may obtain a copy of the License at
7//
8//        http://www.apache.org/licenses/LICENSE-2.0
9//
10//    Unless required by applicable law or agreed to in writing, software
11//    distributed under the License is distributed on an "AS IS" BASIS,
12//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13//    See the License for the specific language governing permissions and
14//    limitations under the License.
15
16use crate::trig::{Cos, Sin};
17use core::ops::{Mul, MulAssign};
18use core::primitive::f32;
19use derive_more::{Add, AddAssign, From, Into, Neg, Sub};
20
21macro_rules! impl_trig_ops {
22    ($tr: ty, $nt: ty) => {
23        impl Mul<$nt> for $tr {
24            fn mul(self, rhs: $nt) -> $nt {
25                rhs * self
26            }
27            type Output = $nt;
28        }
29
30        impl Mul<$tr> for $nt {
31            fn mul(self, other: $tr) -> $nt {
32                self * f32::from(other)
33            }
34            type Output = $nt;
35        }
36
37        impl MulAssign<$tr> for $nt {
38            fn mul_assign(&mut self, other: $tr) {
39                self.0 *= f32::from(other);
40            }
41        }
42    };
43}
44
45macro_rules! impl_number {
46    ($t:ty) => {
47        impl Mul<f32> for $t {
48            fn mul(self, other: f32) -> $t {
49                (self.0 * other).into()
50            }
51            type Output = $t;
52        }
53
54        impl Mul<$t> for f32 {
55            fn mul(self, other: $t) -> $t {
56                (self * other.0).into()
57            }
58            type Output = $t;
59        }
60
61        impl_trig_ops!(Sin, $t);
62        impl_trig_ops!(Cos, $t);
63    };
64}
65
66/// A newtype representing an electric voltage (wraps f32)
67#[derive(Neg, AddAssign, Add, Sub, Debug, Copy, Clone, PartialEq, From, Into)]
68pub struct Voltage(f32);
69
70/// A newtype representing an electric current (wraps f32)
71#[derive(Neg, AddAssign, Add, Sub, Debug, Copy, Clone, PartialEq, From, Into)]
72pub struct Current(f32);
73
74/// A newtype representing an electric power (wraps f32)
75#[derive(Neg, AddAssign, Add, Sub, Debug, Copy, Clone, PartialEq, From, Into)]
76pub struct Power(f32);
77
78/// A newtype representing an electric impedance (wraps f32)
79#[derive(Neg, AddAssign, Add, Sub, Debug, Copy, Clone, PartialEq, From, Into)]
80pub struct Impedance(f32);
81
82// derive operations for the new-types
83impl_number!(Voltage);
84impl_number!(Current);
85impl_number!(Power);
86impl_number!(Impedance);
87
88// derive special multiplies that yeild a unit change (i.e. Voltage * Current = Power)
89impl Mul<Current> for Voltage {
90    fn mul(self, rhs: Current) -> Power {
91        Power(self.0 * rhs.0)
92    }
93    type Output = Power;
94}
95
96impl Mul<Voltage> for Current {
97    fn mul(self, rhs: Voltage) -> Power {
98        Power(self.0 * rhs.0)
99    }
100    type Output = Power;
101}
102
103impl Mul<Current> for Impedance {
104    fn mul(self, rhs: Current) -> Voltage {
105        Voltage(self.0 * rhs.0)
106    }
107    type Output = Voltage;
108}
109
110impl Mul<Impedance> for Current {
111    fn mul(self, rhs: Impedance) -> Voltage {
112        Voltage(self.0 * rhs.0)
113    }
114    type Output = Voltage;
115}