Skip to main content

powerio_matrix/
lib.rs

1//! Sparse matrix and graph projections from PowerIO networks.
2//!
3//! Outputs include signed incidence, weighted bus Laplacian, MATPOWER Bp/Bpp,
4//! Y bus, PTDF, LODF, adjacency, LACPF, and petgraph views. Builders take the
5//! dense [`IndexedNetwork`] view of a [`Network`]. The crate reexports
6//! [`powerio`] types and functions.
7//!
8//! ```
9//! use powerio_matrix::{parse_file, IndexedNetwork, build_bprime, BuildOptions};
10//!
11//! # let case = concat!(env!("CARGO_MANIFEST_DIR"), "/../tests/data/case14.m");
12//! let net = parse_file(case, None)?.network;   // re-exported from powerio
13//! let g = IndexedNetwork::new(&net);           // dense [0, n) analysis view
14//! let bprime = build_bprime(&g, &BuildOptions::default())?;
15//! assert_eq!(bprime.rows(), g.n());            // Bp is n×n
16//! # Ok::<(), powerio_matrix::Error>(())
17//! ```
18//!
19//! # Conventions
20//!
21//! The DC bus susceptance matrix and other weighted bus Laplacians use the
22//! positive M-matrix form: stored nonzero off-diagonal entries are negative,
23//! diagonals are nonnegative, and `diag = Σ|off-diag|`. Source bus IDs remain on
24//! the model; [`IndexedNetwork`] maps them to dense indices in `[0, n)`. `tap == 0` means
25//! `tap = 1`. `build_bprime` and `build_bdoubleprime` follow MATPOWER `makeB`;
26//! Y_bus keeps tap magnitudes and phase shifts.
27//! Branch terminal admittance is stored per unit. DC incidence uses `b = 1/x`
28//! by default. [`DcConvention::Matpower`] uses `1/(x·τ)` and phase shift
29//! injection. The full reference across every matrix is in
30//! [the matrix guide](https://eigenergy.github.io/powerio/guide/matrices.html).
31
32// Re-export the powerio data layer so one import covers model and matrix types, and so
33// the matrix modules' `crate::Error` / `crate::network` / `crate::format` paths
34// resolve unchanged after the split.
35pub use powerio::{
36    Branch, Bus, BusId, BusType, ConnectivityReport, Conversion, DisplayData, DisplayFormat,
37    ElementCounts, Error, ErrorCategory, Extras, GenCost, GenCostPatch, GenCostPolicyReport,
38    Generator, Hvdc, IndexCore, IndexedNetwork, Load, MissingGenCostPolicy, Network,
39    NormalizeOptions, NormalizedNetwork, POWER_MODELS_ANGLE_BOUND_PAD, Parsed, PwdDisplay,
40    PwdSubstation, PypsaCsvOutputs, Result, ScenarioMismatch, Shunt, SourceFormat, Storage,
41    TargetFormat, WriteOptions, convert_file, convert_file_with_options, convert_str,
42    convert_str_with_options, display_format_from_name, error, format, gen_cost, geo, indexed,
43    network, parse_display_bytes, parse_display_file, parse_file, parse_gen_cost_csv,
44    parse_matpower, parse_matpower_file, parse_pandapower_json, parse_powermodels_json,
45    parse_powerworld, parse_pslf, parse_psse, parse_str, read_pypsa_csv_folder,
46    target_format_from_name, write_as, write_as_with_options, write_egret_json, write_matpower,
47    write_pandapower_json, write_powermodels_json, write_powerworld, write_psse,
48    write_pypsa_csv_folder,
49};
50
51/// Compressed sparse row matrix used by the projection builders.
52pub type SparseMatrix = sprs::CsMat<f64>;
53
54pub mod io;
55pub mod matrix;
56pub mod pipeline;
57pub mod synth;
58
59pub use matrix::{
60    BuildOptions, DcConvention, GroundedIndexMap, IncidenceParts, MatrixStats, Scheme,
61    SensitivityMatrices, SensitivityMatrixMetadata, SensitivityMetadata, SensitivityOptions,
62    SensitivitySolver, SensitivitySolverPath, ZeroImpedanceRule, ZeroImpedanceSkips,
63    build_adjacency, build_bdoubleprime, build_bprime, build_flow_map, build_incidence,
64    build_lacpf, build_lodf, build_ptdf, build_ptdf_lodf, build_ptdf_lodf_with_options,
65    build_weighted_laplacian, build_ybus, ground_at, ground_at_each, reference_indicator,
66    sddm_check, skipped_zero_impedance, susceptance_diag, unit_vector,
67};
68pub use pipeline::{
69    MatrixKind, Pipeline, PipelineOutputs, RhsKind, build_kind, matrix_stats_for_kind,
70    zero_impedance_rule_for_kind, zero_impedance_skips_for_kind,
71};
72
73#[cfg(feature = "gridfm")]
74pub use io::gridfm::{
75    GridfmOptions, GridfmOutputs, GridfmRead, GridfmSnapshot, GridfmTables, gridfm_base_case,
76    gridfm_record_batches, gridfm_record_batches_batch, gridfm_scenario_ids, numbered_snapshots,
77    read_gridfm_dataset, read_gridfm_network, read_gridfm_scenarios, write_gridfm_batch,
78    write_gridfm_dataset,
79};
80#[cfg(feature = "gridfm")]
81pub use io::{dataset_scenario_ids, read_dataset_dir};