use gridoxide::cgmes::{cgmes_to_buses_and_branches, load_profiles};
use gridoxide::network::{build_ybus, stamp_shunts};
use gridoxide::run_power_flow_analysis_from_ybus;
fn main() {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
let base = root.join("tests/data/CGMES-Test-Configurations/v3.0/MicroGrid/MicroGid-BaseCase");
let dir = base.join("MicroGrid-BE-MAS");
if !dir.exists() {
eprintln!(
"{} not found — run `git submodule update --init tests/data/CGMES-Test-Configurations`",
dir.display()
);
std::process::exit(1);
}
let eq_bd = base.join("MicroGrid-BD-MAS/20171002T0930Z_ENTSO-E_EQ_BD_2.xml");
let eq = dir.join("20210325T1530Z_1D_BE_EQ_001.xml");
let ssh = dir.join("20210325T1530Z_1D_BE_SSH_001.xml");
let tp = dir.join("20210325T1530Z_1D_BE_TP_001.xml");
let sv = dir.join("20210325T1530Z_1D_BE_SV_001.xml");
let ds = load_profiles(&[&eq_bd, &eq, &ssh, &tp, &sv]).expect("failed to decode CGMES profiles");
let tn_mrids = ds.by_type["TopologicalNode"].clone();
let s_base_va = 100e6;
let (buses, lines, transformers, shunts) =
cgmes_to_buses_and_branches(&ds, s_base_va).expect("conversion failed");
let n = buses.len();
let mut ybus = build_ybus(n, &lines, &transformers);
stamp_shunts(&mut ybus, &shunts);
let result = run_power_flow_analysis_from_ybus(buses, ybus).buses;
let mut out = Vec::with_capacity(tn_mrids.len());
for (idx, mrid) in tn_mrids.iter().enumerate() {
let bus = &result[idx];
out.push(serde_json::json!({
"mrid": mrid,
"u_rated": bus.u_rated,
"voltage_mag": bus.voltage_mag,
"voltage_ang": bus.voltage_ang,
"bus_type": bus.bus_type,
}));
}
println!("{}", serde_json::to_string(&out).unwrap());
}