1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use crate::io::pandapower::Transformer;
use bevy_archive::prelude::SnapshotRegistry;
use bevy_ecs::prelude::*;
use rustpower_proc_marco::DeferBundle;
use nalgebra::Complex;
use nalgebra::Matrix2;
use super::{
bus::SnaptShotRegGroup,
line::{FromBus, StandardModelType, ToBus},
};
#[derive(Component, Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Port4MatPatch(pub Matrix2<Complex<f64>>);
// #[derive(Component, Debug, Clone, serde::Serialize, serde::Deserialize)]
// pub struct Port4 {
// pub from_port: Vector2<i64>,
// pub to_port: Vector2<i64>,
// }
/// Represents the electrical and modeling parameters of a transformer.
#[derive(Component, Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TransformerDevice {
/// Dielectric factor (unitless), used to scale impedance. Common default is 1.0.
pub df: f64,
/// No-load current as a percentage of rated current (%). Used to model magnetizing branch.
pub i0_percent: f64,
/// Iron losses (core losses) in kilowatts (kW).
pub pfe_kw: f64,
/// Short-circuit voltage (%), representing the magnitude of leakage impedance.
pub vk_percent: f64,
/// Resistive portion of the short-circuit voltage (%), used to separate R/X ratio.
pub vkr_percent: f64,
/// Phase shift angle in degrees (°), used for phase-shifting transformers.
pub shift_degree: f64,
/// Rated apparent power of the transformer in megavolt-amperes (MVA).
pub sn_mva: f64,
/// Rated voltage of the high-voltage side (kV).
pub vn_hv_kv: f64,
/// Rated voltage of the low-voltage side (kV).
pub vn_lv_kv: f64,
/// Optional upper limit on transformer loading in percentage (%).
pub max_loading_percent: Option<f64>,
/// Number of parallel transformers. Used to scale impedance or capacity.
pub parallel: i32,
/// Optional tap changer configuration.
#[serde(flatten)]
pub tap: Option<TapChanger>,
}
#[cfg(feature = "arrow")]
/// Represents the electrical and modeling parameters of a transformer.
#[derive(Component, Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TransformerDeviceArrow {
/// Dielectric factor (unitless), used to scale impedance. Common default is 1.0.
pub df: f64,
/// No-load current as a percentage of rated current (%). Used to model magnetizing branch.
pub i0_percent: f64,
/// Iron losses (core losses) in kilowatts (kW).
pub pfe_kw: f64,
/// Short-circuit voltage (%), representing the magnitude of leakage impedance.
pub vk_percent: f64,
/// Resistive portion of the short-circuit voltage (%), used to separate R/X ratio.
pub vkr_percent: f64,
/// Phase shift angle in degrees (°), used for phase-shifting transformers.
pub shift_degree: f64,
/// Rated apparent power of the transformer in megavolt-amperes (MVA).
pub sn_mva: f64,
/// Rated voltage of the high-voltage side (kV).
pub vn_hv_kv: f64,
/// Rated voltage of the low-voltage side (kV).
pub vn_lv_kv: f64,
/// Optional upper limit on transformer loading in percentage (%).
pub max_loading_percent: Option<f64>,
/// Number of parallel transformers. Used to scale impedance or capacity.
pub parallel: i32,
/// Optional tap changer configuration.
pub tap: Option<TapChanger>,
}
#[cfg(feature = "arrow")]
impl From<TransformerDeviceArrow> for TransformerDevice {
fn from(value: TransformerDeviceArrow) -> Self {
TransformerDevice {
df: value.df,
i0_percent: value.i0_percent,
pfe_kw: value.pfe_kw,
vk_percent: value.vk_percent,
vkr_percent: value.vkr_percent,
shift_degree: value.shift_degree,
sn_mva: value.sn_mva,
vn_hv_kv: value.vn_hv_kv,
vn_lv_kv: value.vn_lv_kv,
max_loading_percent: value.max_loading_percent,
parallel: value.parallel,
tap: value.tap,
}
}
}
#[cfg(feature = "arrow")]
impl From<&TransformerDevice> for TransformerDeviceArrow {
fn from(value: &TransformerDevice) -> Self {
TransformerDeviceArrow {
df: value.df,
i0_percent: value.i0_percent,
pfe_kw: value.pfe_kw,
vk_percent: value.vk_percent,
vkr_percent: value.vkr_percent,
shift_degree: value.shift_degree,
sn_mva: value.sn_mva,
vn_hv_kv: value.vn_hv_kv,
vn_lv_kv: value.vn_lv_kv,
max_loading_percent: value.max_loading_percent,
parallel: value.parallel,
tap: value.tap.clone(),
}
}
}
/// Configuration of a tap changer for voltage or phase regulation.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TapChanger {
/// Side on which the tap changer is installed, e.g., "hv" or "lv".
pub side: Option<String>,
/// Neutral tap position (typically 0.0).
pub neutral: Option<f64>,
/// Maximum tap position.
pub max: Option<f64>,
/// Minimum tap position.
pub min: Option<f64>,
/// Current tap position.
pub pos: Option<f64>,
/// Phase shift per tap in degrees (°), for phase shifter modeling.
pub step_degree: Option<f64>,
/// Voltage change per tap in percentage (%), for tap ratio modeling.
pub step_percent: Option<f64>,
/// Indicates whether this tap changer acts as a phase shifter.
pub is_phase_shifter: bool,
}
/// ECS bundle representing a transformer entity.
#[derive(Debug, Clone, DeferBundle)]
pub struct TransformerBundle {
/// Transformer device parameters.
pub device: TransformerDevice,
/// The high-voltage side connection (from bus).
pub from_bus: FromBus,
/// The low-voltage side connection (to bus).
pub to_bus: ToBus,
/// Optional transformer name.
pub name: Option<Name>,
/// Optional standard type string (e.g., "25MVA_110/10kV_OFAF").
pub std_type: Option<StandardModelType>,
}
impl From<&Transformer> for TransformerBundle {
fn from(t: &Transformer) -> Self {
Self {
device: TransformerDevice {
df: t.df,
i0_percent: t.i0_percent,
pfe_kw: t.pfe_kw,
vk_percent: t.vk_percent,
vkr_percent: t.vkr_percent,
shift_degree: t.shift_degree,
sn_mva: t.sn_mva,
vn_hv_kv: t.vn_hv_kv,
vn_lv_kv: t.vn_lv_kv,
max_loading_percent: t.max_loading_percent,
parallel: t.parallel,
tap: Some(TapChanger {
side: t.tap_side.clone(),
neutral: t.tap_neutral,
max: t.tap_max,
min: t.tap_min,
pos: t.tap_pos,
step_degree: t.tap_step_degree,
step_percent: t.tap_step_percent,
is_phase_shifter: t.tap_phase_shifter,
}),
},
from_bus: FromBus(t.hv_bus as i64),
to_bus: ToBus(t.lv_bus as i64),
name: t.name.as_ref().map(|x| Name::new(x.clone())),
std_type: t.std_type.as_ref().map(|x| StandardModelType(x.clone())),
}
}
}
pub struct TransSnapShotReg;
impl SnaptShotRegGroup for TransSnapShotReg {
fn register_snap_shot(reg: &mut SnapshotRegistry) {
reg.register_named::<TransformerDevice>("trafo");
#[cfg(feature = "arrow")]
{
use bevy_archive::prelude::vec_snapshot_factory::ArrowSnapshotFactory;
reg.get_factory_mut("trafo").unwrap().arrow = Some(ArrowSnapshotFactory::new_with::<
TransformerDevice,
TransformerDeviceArrow,
>());
}
}
}
pub mod trans_systems {
use nalgebra::{Complex, ComplexField};
use super::*;
use crate::basic::ecs::elements::OutOfService;
pub fn setup_transformer(
mut commands: Commands,
q: Query<(Entity, &TransformerDevice), Without<OutOfService>>,
oos: Query<Entity, (With<TransformerDevice>, With<OutOfService>)>,
) {
// Out-of-service transformers contribute nothing to the Y-bus:
// drop their admittance children and matrix patch.
for entity in &oos {
commands.entity(entity).despawn_related::<Children>();
commands.entity(entity).remove::<Port4MatPatch>();
}
q.iter().for_each(|(entity, transformer)| {
setup_transformer_admittance(&mut commands, entity, transformer);
});
}
fn setup_transformer_admittance(
commands: &mut Commands,
parent: Entity,
dev: &TransformerDevice,
) {
commands.entity(parent).despawn_related::<Children>();
let tap_m = dev.tap.as_ref().map_or(1.0, |tap| {
let pos = tap.pos.unwrap_or(0.0);
let neutral = tap.neutral.unwrap_or(0.0);
let step = tap.step_percent.unwrap_or(0.0);
1.0 + (pos - neutral) * 0.01 * step
});
let v_base = dev.vn_lv_kv;
let z_base = v_base * v_base / dev.sn_mva;
let vk = dev.vk_percent * 0.01;
let vkr = dev.vkr_percent * 0.01;
let z = z_base * vk;
let re = z_base * vkr;
let im = (z.powi(2) - re.powi(2)).sqrt();
let y = dev.parallel as f64 / Complex::new(re, im);
let re_core = z_base * 0.001 * dev.pfe_kw / dev.sn_mva;
let im_core = z_base / (0.01 * dev.i0_percent);
let z_m = Complex::new(re_core, im_core);
let a = tap_m * Complex::from_polar(1.0, dev.shift_degree.to_radians());
let a = a.recip();
let t = Matrix2::new(
a,
Complex::new(0.0, 0.0),
Complex::new(0.0, 0.0),
Complex::new(1.0, 0.0),
);
let mut g = Matrix2::new(y, -y, -y, y);
let y_m = dev.parallel as f64 / z_m;
if y_m.is_finite() {
g[(0, 0)] += 0.5 * y_m;
g[(1, 1)] += 0.5 * y_m;
}
let g = t.conjugate() * g * t;
commands.entity(parent).insert(Port4MatPatch(g));
}
}