Skip to content

Commit

Permalink
perf: decrease systems variable read/write and hashing time (flybywir…
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwalschots authored Oct 21, 2021
1 parent 9f565ae commit d3754f4
Show file tree
Hide file tree
Showing 45 changed files with 3,236 additions and 2,141 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/lint-rust.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ set -ex

cargo fmt -- --check

cargo clippy --all-targets --all-features -- -D warnings -A clippy::too_many_arguments
cargo clippy --all-targets --all-features -- -D warnings -A clippy::too_many_arguments -A deprecated
69 changes: 46 additions & 23 deletions src/systems/a320_hydraulic_simulation_graphs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use plotlib::style::LineStyle;
use plotlib::view::ContinuousView;
use std::time::Duration;
pub use systems::hydraulic::*;
use systems::{shared::ElectricalBusType, simulation::UpdateContext};
use systems::{
electrical::Electricity,
shared::ElectricalBusType,
simulation::{test::TestVariableRegistry, InitContext, UpdateContext},
};
use uom::si::{
acceleration::foot_per_second_squared,
angle::radian,
Expand All @@ -18,7 +22,6 @@ use uom::si::{
volume_rate::gallon_per_second,
};

extern crate rustplotlib;
use rustplotlib::Figure;

struct TestHydraulicLoopController {
Expand Down Expand Up @@ -229,14 +232,18 @@ fn green_loop_edp_simulation(path: &str) {
let edp1_var_names = vec!["Delta Vol Max".to_string(), "pump rpm".to_string()];
let mut edp1_history = History::new(edp1_var_names);

let mut edp1 = engine_driven_pump();
let mut electricity = Electricity::new();
let mut registry: TestVariableRegistry = Default::default();
let mut init_context = InitContext::new(&mut electricity, &mut registry);

let mut edp1 = engine_driven_pump(&mut init_context);
let mut edp1_controller = TestPumpController::commanding_pressurise();

let mut green_loop = hydraulic_loop("GREEN");
let mut green_loop = hydraulic_loop(&mut init_context, "GREEN");
let green_loop_controller = TestHydraulicLoopController::commanding_open_fire_shutoff_valve();

let edp_rpm = 3000.;
let context = context(Duration::from_millis(100));
let context = context(&mut init_context, Duration::from_millis(100));

let green_acc_var_names = vec![
"Loop Pressure".to_string(),
Expand Down Expand Up @@ -383,21 +390,25 @@ fn yellow_green_ptu_loop_simulation(path: &str) {
];
let mut accu_yellow_history = History::new(yellow_acc_var_names);

let mut epump = electric_pump();
let mut electricity = Electricity::new();
let mut registry: TestVariableRegistry = Default::default();
let mut init_context = InitContext::new(&mut electricity, &mut registry);

let mut epump = electric_pump(&mut init_context);
let mut epump_controller = TestPumpController::commanding_depressurise();
let mut yellow_loop = hydraulic_loop("YELLOW");
let mut yellow_loop = hydraulic_loop(&mut init_context, "YELLOW");

let mut edp1 = engine_driven_pump();
let mut edp1 = engine_driven_pump(&mut init_context);
let mut edp1_controller = TestPumpController::commanding_depressurise();

let mut green_loop = hydraulic_loop("GREEN");
let mut green_loop = hydraulic_loop(&mut init_context, "GREEN");

let loop_controller = TestHydraulicLoopController::commanding_open_fire_shutoff_valve();

let mut ptu = PowerTransferUnit::new();
let mut ptu = PowerTransferUnit::new(&mut init_context);
let mut ptu_controller = TestPowerTransferUnitController::commanding_disabled();

let context = context(Duration::from_millis(100));
let context = context(&mut init_context, Duration::from_millis(100));

loop_history.init(
0.0,
Expand Down Expand Up @@ -645,23 +656,27 @@ fn yellow_epump_plus_edp2_with_ptu(path: &str) {
];
let mut accu_yellow_history = History::new(yellow_acc_var_names);

let mut epump = electric_pump();
let mut electricity = Electricity::new();
let mut registry: TestVariableRegistry = Default::default();
let mut init_context = InitContext::new(&mut electricity, &mut registry);

let mut epump = electric_pump(&mut init_context);
let mut epump_controller = TestPumpController::commanding_depressurise();
let mut yellow_loop = hydraulic_loop("YELLOW");
let mut yellow_loop = hydraulic_loop(&mut init_context, "YELLOW");

let mut edp2 = engine_driven_pump();
let mut edp2 = engine_driven_pump(&mut init_context);
let mut edp2_controller = TestPumpController::commanding_depressurise();

let edp_rpm = 3300.;

let mut green_loop = hydraulic_loop("GREEN");
let mut green_loop = hydraulic_loop(&mut init_context, "GREEN");

let loop_controller = TestHydraulicLoopController::commanding_open_fire_shutoff_valve();

let mut ptu = PowerTransferUnit::new();
let mut ptu = PowerTransferUnit::new(&mut init_context);
let ptu_controller = TestPowerTransferUnitController::commanding_enabled();

let context = context(Duration::from_millis(100));
let context = context(&mut init_context, Duration::from_millis(100));

loop_history.init(
0.0,
Expand Down Expand Up @@ -790,9 +805,10 @@ fn yellow_epump_plus_edp2_with_ptu(path: &str) {
accu_yellow_history.show_matplotlib("yellow_epump_plus_edp2_with_ptu()_Yellow_acc", &path);
}

fn hydraulic_loop(loop_color: &str) -> HydraulicLoop {
fn hydraulic_loop(context: &mut InitContext, loop_color: &str) -> HydraulicLoop {
match loop_color {
"GREEN" => HydraulicLoop::new(
context,
loop_color,
true,
false,
Expand All @@ -806,6 +822,7 @@ fn hydraulic_loop(loop_color: &str) -> HydraulicLoop {
Pressure::new::<psi>(1750.),
),
"YELLOW" => HydraulicLoop::new(
context,
loop_color,
false,
true,
Expand All @@ -819,6 +836,7 @@ fn hydraulic_loop(loop_color: &str) -> HydraulicLoop {
Pressure::new::<psi>(1750.),
),
_ => HydraulicLoop::new(
context,
loop_color,
false,
false,
Expand All @@ -834,16 +852,21 @@ fn hydraulic_loop(loop_color: &str) -> HydraulicLoop {
}
}

fn electric_pump() -> ElectricPump {
ElectricPump::new("DEFAULT", ElectricalBusType::AlternatingCurrentEssential)
fn electric_pump(context: &mut InitContext) -> ElectricPump {
ElectricPump::new(
context,
"DEFAULT",
ElectricalBusType::AlternatingCurrentEssential,
)
}

fn engine_driven_pump() -> EngineDrivenPump {
EngineDrivenPump::new("DEFAULT")
fn engine_driven_pump(context: &mut InitContext) -> EngineDrivenPump {
EngineDrivenPump::new(context, "DEFAULT")
}

fn context(delta_time: Duration) -> UpdateContext {
fn context(context: &mut InitContext, delta_time: Duration) -> UpdateContext {
UpdateContext::new(
context,
delta_time,
Velocity::new::<knot>(250.),
Length::new::<foot>(5000.),
Expand Down
78 changes: 33 additions & 45 deletions src/systems/a320_systems/src/electrical/alternating_current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use super::{
A320ElectricalOverheadPanel, A320EmergencyElectricalOverheadPanel,
};
use std::time::Duration;
use systems::simulation::InitContext;
use systems::{
electrical::{
AlternatingCurrentElectricalSystem, Contactor, ElectricalBus,
ElectricalElementIdentifierProvider, Electricity, EmergencyGenerator, EngineGenerator,
ExternalPowerSource, TransformerRectifier,
AlternatingCurrentElectricalSystem, Contactor, ElectricalBus, Electricity,
EmergencyGenerator, EngineGenerator, ExternalPowerSource, TransformerRectifier,
},
shared::{
AuxiliaryPowerUnitElectrical, DelayedTrueLogicGate, ElectricalBusType, EngineCorrectedN2,
Expand Down Expand Up @@ -37,46 +37,34 @@ pub(super) struct A320AlternatingCurrentElectrical {
ext_pwr_to_ac_gnd_flt_service_bus_and_tr_2_contactor: Contactor,
}
impl A320AlternatingCurrentElectrical {
pub fn new(identifier_provider: &mut impl ElectricalElementIdentifierProvider) -> Self {
pub fn new(context: &mut InitContext) -> Self {
A320AlternatingCurrentElectrical {
main_power_sources: A320MainPowerSources::new(identifier_provider),
ac_ess_feed_contactors: A320AcEssFeedContactors::new(identifier_provider),
ac_bus_1: ElectricalBus::new(
ElectricalBusType::AlternatingCurrent(1),
identifier_provider,
),
ac_bus_2: ElectricalBus::new(
ElectricalBusType::AlternatingCurrent(2),
identifier_provider,
),
ac_ess_bus: ElectricalBus::new(
ElectricalBusType::AlternatingCurrentEssential,
identifier_provider,
),
main_power_sources: A320MainPowerSources::new(context),
ac_ess_feed_contactors: A320AcEssFeedContactors::new(context),
ac_bus_1: ElectricalBus::new(context, ElectricalBusType::AlternatingCurrent(1)),
ac_bus_2: ElectricalBus::new(context, ElectricalBusType::AlternatingCurrent(2)),
ac_ess_bus: ElectricalBus::new(context, ElectricalBusType::AlternatingCurrentEssential),
ac_ess_shed_bus: ElectricalBus::new(
context,
ElectricalBusType::AlternatingCurrentEssentialShed,
identifier_provider,
),
ac_ess_shed_contactor: Contactor::new("8XH", identifier_provider),
tr_1: TransformerRectifier::new(1, identifier_provider),
tr_2: TransformerRectifier::new(2, identifier_provider),
ac_bus_2_to_tr_2_contactor: Contactor::new("14PU", identifier_provider),
tr_ess: TransformerRectifier::new(3, identifier_provider),
ac_ess_to_tr_ess_contactor: Contactor::new("15XE1", identifier_provider),
emergency_gen_contactor: Contactor::new("2XE", identifier_provider),
static_inv_to_ac_ess_bus_contactor: Contactor::new("15XE2", identifier_provider),
ac_ess_shed_contactor: Contactor::new(context, "8XH"),
tr_1: TransformerRectifier::new(context, 1),
tr_2: TransformerRectifier::new(context, 2),
ac_bus_2_to_tr_2_contactor: Contactor::new(context, "14PU"),
tr_ess: TransformerRectifier::new(context, 3),
ac_ess_to_tr_ess_contactor: Contactor::new(context, "15XE1"),
emergency_gen_contactor: Contactor::new(context, "2XE"),
static_inv_to_ac_ess_bus_contactor: Contactor::new(context, "15XE2"),
ac_stat_inv_bus: ElectricalBus::new(
context,
ElectricalBusType::AlternatingCurrentStaticInverter,
identifier_provider,
),
ac_gnd_flt_service_bus: ElectricalBus::new(
context,
ElectricalBusType::AlternatingCurrentGndFltService,
identifier_provider,
),
ext_pwr_to_ac_gnd_flt_service_bus_and_tr_2_contactor: Contactor::new(
"12XN",
identifier_provider,
),
ext_pwr_to_ac_gnd_flt_service_bus_and_tr_2_contactor: Contactor::new(context, "12XN"),
}
}

Expand Down Expand Up @@ -358,18 +346,18 @@ struct A320MainPowerSources {
ext_pwr_contactor: Contactor,
}
impl A320MainPowerSources {
fn new(identifier_provider: &mut impl ElectricalElementIdentifierProvider) -> Self {
fn new(context: &mut InitContext) -> Self {
A320MainPowerSources {
engine_1_gen: EngineGenerator::new(1, identifier_provider),
engine_2_gen: EngineGenerator::new(2, identifier_provider),
engine_1_gen: EngineGenerator::new(context, 1),
engine_2_gen: EngineGenerator::new(context, 2),
engine_generator_contactors: [
Contactor::new("9XU1", identifier_provider),
Contactor::new("9XU2", identifier_provider),
Contactor::new(context, "9XU1"),
Contactor::new(context, "9XU2"),
],
bus_tie_1_contactor: Contactor::new("11XU1", identifier_provider),
bus_tie_2_contactor: Contactor::new("11XU2", identifier_provider),
apu_gen_contactor: Contactor::new("3XS", identifier_provider),
ext_pwr_contactor: Contactor::new("3XG", identifier_provider),
bus_tie_1_contactor: Contactor::new(context, "11XU1"),
bus_tie_2_contactor: Contactor::new(context, "11XU2"),
apu_gen_contactor: Contactor::new(context, "3XS"),
ext_pwr_contactor: Contactor::new(context, "3XG"),
}
}

Expand Down Expand Up @@ -491,10 +479,10 @@ pub(super) struct A320AcEssFeedContactors {
impl A320AcEssFeedContactors {
pub const AC_ESS_FEED_TO_AC_BUS_2_DELAY_IN_SECONDS: Duration = Duration::from_secs(3);

fn new(identifier_provider: &mut impl ElectricalElementIdentifierProvider) -> Self {
fn new(context: &mut InitContext) -> Self {
A320AcEssFeedContactors {
ac_ess_feed_contactor_1: Contactor::new("3XC1", identifier_provider),
ac_ess_feed_contactor_2: Contactor::new("3XC2", identifier_provider),
ac_ess_feed_contactor_1: Contactor::new(context, "3XC1"),
ac_ess_feed_contactor_2: Contactor::new(context, "3XC2"),
ac_ess_feed_contactor_delay_logic_gate: DelayedTrueLogicGate::new(
A320AcEssFeedContactors::AC_ESS_FEED_TO_AC_BUS_2_DELAY_IN_SECONDS,
),
Expand Down
Loading

0 comments on commit d3754f4

Please sign in to comment.