// Local utility demand is resolved before component-scoped imports and discretionary exports. #include "sc2k/sim/utility_flow.hpp" #include "test_harness.hpp" #include #include #include namespace { sc2k::sim::UtilityConnectionBinding bound(sc2k::sim::BorderConnectionKind kind, int edgeSlot, int offset, int componentKey, int demand, int delivered, int unmet, int remaining) { sc2k::sim::UtilityConnectionBinding binding; const int row = edgeSlot == 2 ? 0 : offset; const int column = edgeSlot == 2 ? offset : 0; binding.connection = {row, column, edgeSlot, offset, kind, sc2k::sim::connection_layer(kind), sc2k::sim::connection_footprint(kind)}; binding.status = sc2k::sim::UtilityConnectionBindingStatus::Bound; binding.componentKey = componentKey; binding.localSupply = delivered + remaining; binding.effectiveSupply = delivered + remaining; binding.demand = demand; binding.delivered = delivered; binding.unmetDemand = unmet; binding.remainingSupply = remaining; return binding; } const sc2k::PowerComponentDiagnostic* power_component( const sc2k::PowerDiagnostics& diagnostics, int key) { for (const auto& component : diagnostics.components) if (component.componentKey == key) return &component; return nullptr; } const sc2k::WaterComponentDiagnostic* water_component( const sc2k::WaterDiagnostics& diagnostics, int key) { for (const auto& component : diagnostics.components) if (component.componentKey == key) return &component; return nullptr; } } // namespace int main() { using namespace sc2k; { sim::UtilityConnectionDiagnostics connections; connections.bindings = { bound(sim::BorderConnectionKind::Electricity, 2, 10, 100, 3, 0, 3, 0), bound(sim::BorderConnectionKind::Electricity, 2, 11, 100, 3, 0, 3, 0), // same component bound(sim::BorderConnectionKind::Electricity, 2, 20, 200, 2, 0, 2, 0), bound(sim::BorderConnectionKind::Electricity, 2, 30, 300, 2, 2, 0, 4), bound(sim::BorderConnectionKind::WaterPipe, 3, 40, 400, 1, 1, 0, 3), }; std::vector requests = { {20, 2, sim::BorderConnectionKind::Electricity, sim::UtilityFlowDirection::Import, 4}, {10, 2, sim::BorderConnectionKind::Electricity, sim::UtilityFlowDirection::Import, 4}, {30, 2, sim::BorderConnectionKind::Electricity, sim::UtilityFlowDirection::Export, 5}, {40, 2, sim::BorderConnectionKind::Electricity, sim::UtilityFlowDirection::Export, 1}, {50, 3, sim::BorderConnectionKind::WaterPipe, sim::UtilityFlowDirection::Export, 2}, {60, 1, sim::BorderConnectionKind::WaterPipe, sim::UtilityFlowDirection::Import, 1, 0}, {70, 2, sim::BorderConnectionKind::Electricity, sim::UtilityFlowDirection::Import, 0}, {80, 2, sim::BorderConnectionKind::Electricity, sim::UtilityFlowDirection::Import, 1}, {80, 2, sim::BorderConnectionKind::Electricity, sim::UtilityFlowDirection::Import, 2}, }; const sim::UtilityFlowPlan plan = sim::allocate_local_first_utility_flows(connections, requests); CHECK(plan.allocations.size() == 9); CHECK(plan.allocations[0].request.contractId == 10); CHECK(plan.allocations[0].status == sim::UtilityFlowAllocationStatus::Allocated); CHECK(plan.allocations[0].allocatedUnits == 4); CHECK(plan.allocations[1].request.contractId == 20); CHECK(plan.allocations[1].status == sim::UtilityFlowAllocationStatus::PartiallyAllocated); CHECK(plan.allocations[1].allocatedUnits == 1); CHECK(plan.allocations[1].unallocatedUnits == 3); CHECK(plan.allocations[2].status == sim::UtilityFlowAllocationStatus::PartiallyAllocated); CHECK(plan.allocations[2].allocatedUnits == 4); CHECK(plan.allocations[3].status == sim::UtilityFlowAllocationStatus::NoLocalSurplus); CHECK(plan.allocations[4].status == sim::UtilityFlowAllocationStatus::Allocated); CHECK(plan.allocations[4].allocatedUnits == 2); CHECK(plan.allocations[5].status == sim::UtilityFlowAllocationStatus::NoBoundConnection); CHECK(plan.allocations[6].status == sim::UtilityFlowAllocationStatus::InvalidRequest); CHECK(plan.allocations[7].status == sim::UtilityFlowAllocationStatus::DuplicateContract); CHECK(plan.allocations[8].status == sim::UtilityFlowAllocationStatus::DuplicateContract); CHECK(plan.components.size() == 4); CHECK(plan.components[0].componentKey == 100 && plan.components[0].importedUnits == 3); CHECK(plan.components[1].componentKey == 200 && plan.components[1].importedUnits == 2); CHECK(plan.components[2].componentKey == 300 && plan.components[2].exportedUnits == 4); CHECK(plan.components[3].componentKey == 400 && plan.components[3].exportedUnits == 2); CHECK(plan.powerImports == std::vector({{100, 3}, {200, 2}})); CHECK(sim::valid_component_supply_injections(plan.powerImports)); CHECK(plan.waterImports.empty()); // Canonical contract ordering makes insertion order irrelevant. std::reverse(requests.begin(), requests.end()); CHECK(sim::allocate_local_first_utility_flows(connections, requests) == plan); sim::UtilityConnectionDiagnostics nonlocal = connections; nonlocal.bindings[0].importedSupply = 1; const std::array oneRequest = {{{90, 2, sim::BorderConnectionKind::Electricity, sim::UtilityFlowDirection::Import, 1}}}; const sim::UtilityFlowPlan rejected = sim::allocate_local_first_utility_flows(nonlocal, oneRequest); CHECK(rejected.allocations.size() == 1); CHECK(rejected.allocations[0].status == sim::UtilityFlowAllocationStatus::NonlocalBaseline); } { // Contract quality stays attached to each canonical allocation when two neighbours feed one // component; the component injection is aggregated without prematurely averaging quality. sim::UtilityConnectionDiagnostics connections; connections.bindings = { bound(sim::BorderConnectionKind::WaterPipe, 2, 10, 500, 10, 0, 10, 0), bound(sim::BorderConnectionKind::WaterPipe, 3, 10, 500, 10, 0, 10, 0), }; std::array requests{{ {2, 3, sim::BorderConnectionKind::WaterPipe, sim::UtilityFlowDirection::Import, 4, 5000}, {1, 2, sim::BorderConnectionKind::WaterPipe, sim::UtilityFlowDirection::Import, 6, 1000}, }}; const sim::UtilityFlowPlan plan = sim::allocate_local_first_utility_flows(connections, requests); CHECK(plan.waterImports == std::vector({{500, 10}})); CHECK(plan.waterImportQualities == std::vector( {{500, 6, 1000, 1}, {500, 4, 5000, 2}})); std::reverse(requests.begin(), requests.end()); CHECK(sim::allocate_local_first_utility_flows(connections, requests) == plan); sim::UtilityFlowRequest missingQuality{3, 2, sim::BorderConnectionKind::WaterPipe, sim::UtilityFlowDirection::Import, 1}; CHECK(!sim::valid_utility_flow_request(missingQuality)); missingQuality.waterImportContamination = 0; CHECK(sim::valid_utility_flow_request(missingQuality)); } { // A source-free electricity component is energized directly from its canonical border key. City city{MapSize::Tiny}; for (int row = 0; row <= 3; ++row) { city.xbit.at(row, 10) = xbit::kConductive; city.xbld.at(row, 10) = row == 0 ? xbld::kPowerFirst : xbld::kHomesFirst; } const PowerDiagnostics baseline = update_power_with_diagnostics(city, nullptr, false); const int key = city.index(0, 10); const auto* dry = power_component(baseline, key); CHECK(dry != nullptr); CHECK(dry && dry->localSupply == 0 && dry->demand == 3 && dry->delivered == 0); sim::RegionalState region = sim::make_migrated_regional_state(city, 1); region.connections.push_back({0, 10, 2, 10, sim::BorderConnectionKind::Electricity, sim::BorderConnectionLayer::Electricity, 1}); const WaterDiagnostics noWater; const sim::UtilityConnectionDiagnostics bindings = sim::bind_border_utility_connections(city, region, baseline, noWater); const std::array requests = {{{1, 2, sim::BorderConnectionKind::Electricity, sim::UtilityFlowDirection::Import, 2}}}; const sim::UtilityFlowPlan plan = sim::allocate_local_first_utility_flows(bindings, requests); CHECK(plan.powerImports == std::vector({{key, 2}})); const PowerDiagnostics supplied = update_power_with_diagnostics(city, nullptr, false, plan.powerImports); CHECK(supplied.importedSupply == 2); CHECK(supplied.effectiveSupply == 2); const auto* component = power_component(supplied, key); CHECK(component != nullptr); if (component) { CHECK(component->localSupply == 0); CHECK(component->importedSupply == 2); CHECK(component->effectiveSupply == 2); CHECK(component->demand == 3); CHECK(component->delivered == 2); CHECK(component->unmetDemand == 1); CHECK(component->remainingSupply == 0); } } { // A source-free native main receives imported water without serving another component. City city{MapSize::Tiny}; sim::initialize_modern_water_network(city); sim::set_water_main(city, 0, 20, true); for (int row = 1; row <= 3; ++row) city.xbld.at(row, 20) = xbld::kHomesFirst; sim::set_water_main(city, 0, 40, true); city.xbld.at(1, 40) = xbld::kHomesFirst; const WaterDiagnostics baseline = update_water_with_diagnostics(city); const int key = city.index(0, 20); const int otherKey = city.index(0, 40); const std::array imports = {{{key, 2}}}; const WaterDiagnostics supplied = update_water_with_diagnostics(city, -1, imports); CHECK(supplied.importedSupply == 2); CHECK(supplied.effectiveSupply == 2); const auto* component = water_component(supplied, key); const auto* other = water_component(supplied, otherKey); CHECK(component != nullptr); CHECK(other != nullptr); if (component) { CHECK(component->localSupply == 0); CHECK(component->importedSupply == 2); CHECK(component->effectiveSupply == 2); CHECK(component->demand == 3); CHECK(component->delivered == 2); CHECK(component->unmetDemand == 1); } CHECK(other && other->delivered == 0 && other->unmetDemand == 1); CHECK(baseline.delivered == 0); } { // Imported hidden-link topology accepts the same component-scoped input without a local source. City city{MapSize::Tiny}; city.waterDistributionModel = WaterDistributionModel::ClassicLinks; sim::initialize_modern_water_network(city); sim::set_water_main(city, 0, 30, true); for (int row = 1; row <= 3; ++row) { city.xbld.at(row, 30) = xbld::kHomesFirst; city.legacyWaterLinks.at(row, 30) = 1; sim::sync_water_pipe_bit(city, city.index(row, 30)); } const WaterDiagnostics baseline = update_water_with_diagnostics(city); const int key = city.index(0, 30); CHECK(baseline.delivered == 0 && baseline.unmetDemand == 3); const std::array imports = {{{key, 2}}}; const WaterDiagnostics supplied = update_water_with_diagnostics(city, -1, imports); const auto* component = water_component(supplied, key); CHECK(component != nullptr); if (component) { CHECK(component->localSupply == 0); CHECK(component->importedSupply == 2); CHECK(component->demand == 3); CHECK(component->delivered == 2); CHECK(component->unmetDemand == 1); } } if (failures == 0) { std::printf("utility flow: local-first allocation and component injection passed\n"); return 0; } std::printf("utility flow: %d failure(s)\n", failures); return 1; }