"""The power system (chapter 5): S38, S23, S24 and S25. Made-up round numbers, except the test that holds the fitted lines against NVE's own twelve scenarios (the table in the parameter file). """ from __future__ import annotations from typing import TYPE_CHECKING import pytest from tokenfabrikker import inndata, kraftsystem from tokenfabrikker import ligninger as lg from tokenfabrikker import parametere as pm if TYPE_CHECKING: from conftest import LagInndata # --------------------------------------------------------------------------- S38 def test_s38_linja_gaar_gjennom_punkter_som_ligger_paa_linje() -> None: balanse = [-2.0, 0.0, 3.0, 10.0] alpha, b = lg.s38_tilpass_linje(S=balanse, P=[700.0 - 10.0 * s for s in balanse]) assert (alpha, b) == pytest.approx((700.0, -10.0)) def test_s38_minste_kvadrater_haandregnet() -> None: """Points (0, 1), (1, 3), (2, 2): slope 0.5 and intercept 1.5.""" alpha, b = lg.s38_tilpass_linje(S=[0.0, 1.0, 2.0], P=[1.0, 3.0, 2.0]) assert (alpha, b) == pytest.approx((1.5, 0.5)) def test_s38_avviser_for_faa_eller_like_punkter() -> None: with pytest.raises(ValueError, match="at least two points"): lg.s38_tilpass_linje(S=[1.0], P=[1.0]) with pytest.raises(ValueError, match="all balances are equal"): lg.s38_tilpass_linje(S=[1.0, 1.0], P=[1.0, 2.0]) def test_s38_brattere_under_knekken_og_sammenhengende_i_den() -> None: felles = {"alpha": 700.0, "b": -10e-6, "b_ut": 2.0} assert lg.s38_pris(**felles, S=10e6) == pytest.approx(600.0) assert lg.s38_pris(**felles, S=-3.6e6) == pytest.approx(736.0) # one TWh below the kink: twice the slope assert lg.s38_pris(**felles, S=-4.6e6) == pytest.approx(756.0) assert lg.s38_pris(**felles, S=-3.6e6 - 1e-3) == pytest.approx(736.0) @pytest.mark.fasit @pytest.mark.parametrize( "omraade", [ "NO1", "NO2", pytest.param( "NO3", marks=pytest.mark.xfail( strict=True, reason="the line misses NVE's scenario N7 (no growth in offshore wind) by 6.0 %", ), ), pytest.param( "NO4", marks=pytest.mark.xfail( strict=True, reason="the line misses NVE's scenario N7 (no growth in offshore wind) by 7.7 %", ), ), "NO5", "Norge", ], ) def test_linjene_gjenskaper_nves_tolv_scenarioer_innenfor_fem_prosent(omraade: str) -> None: """S38 against the source: the fitted line gives NVE's price in each of its twelve points.""" scenarioer = pm.les_parametere().pris_balanse() linje = inndata.tilpass_prislinjer(scenarioer)[omraade] avvik = [ abs(lg.s38_pris(alpha=linje.alpha, b=linje.b, b_ut=1.0, S=s.balanse) / s.pris[omraade] - 1) for s in scenarioer ] assert max(avvik) < 0.05 @pytest.mark.fasit def test_linja_for_norge_faller_med_bedre_kraftbalanse() -> None: linje = inndata.tilpass_prislinjer(pm.les_parametere().pris_balanse())["Norge"] assert linje.b < 0 # about 10 kr/MWh per TWh, between NVE's weakest and strongest scenario assert -12.0 < linje.b * 1e6 < -9.0 # ---------------------------------------------------------------- S23, S24, S25 def test_s23_netto_uttak_og_prisvirkning() -> None: Delta_Q = lg.s23_netto_uttak(delta_V=0.5, Q_D_nett=8e6, Q_G_nett=1e6) assert Delta_Q == pytest.approx(3e6) felles = {"alpha": 700.0, "b": -10e-6, "b_ut": 2.0, "S_0": 10e6, "Delta_Q": Delta_Q} assert lg.s23_prisvirkning(**felles) == pytest.approx(30.0) assert lg.s23_prisvirkning(**felles, m_v=1.5, s_v=1.2) == pytest.approx(54.0) def test_s23_overskudd_av_ny_kraft_senker_prisen() -> None: assert lg.s23_prisvirkning( alpha=700.0, b=-10e-6, b_ut=2.0, S_0=10e6, Delta_Q=-2e6 ) == pytest.approx(-20.0) def test_s24_effektbalanse_med_og_uten_storste_enhet() -> None: Delta_R, Delta_R_utfall = lg.s24_effektbalanse( effektbidrag=[980.0, 450.0], delta_V=0.5, PUE=1.2, P_IT_sum=2000.0, storste_enhet=980.0 ) assert Delta_R == pytest.approx(230.0) assert Delta_R_utfall == pytest.approx(-750.0) def test_s25_prisoppgang_er_gevinst_for_et_land_som_eksporterer() -> None: assert lg.s25_velferd(Delta_p=30.0, S_0=10e6) == pytest.approx(300e6) assert lg.s25_velferd(Delta_p=30.0, S_0=-5e6) == pytest.approx(-150e6) def test_kraftaar_setter_ligningene_sammen(oppdiktet: LagInndata) -> None: inn = oppdiktet() aar = kraftsystem.kraftaar( inn, Q_D_nett=8e6, Q_G_nett=1e6, Q_G_nett_tort=0.8e6, effektbidrag=[100.0], P_IT_sum=1000.0, storste_enhet=40.0, ) assert aar.Delta_Q == pytest.approx(3e6) assert aar.Delta_Q_tort == pytest.approx(3.2e6) assert aar.pris == pytest.approx(630.0) assert aar.Delta_p == pytest.approx(30.0) assert (aar.Delta_R, aar.Delta_R_utfall) == pytest.approx((-500.0, -540.0)) assert aar.velferd(S_0=inn["S0"]) == pytest.approx(300e6) def test_relativ_prisvirkning_per_omraade(oppdiktet: LagInndata) -> None: inn = oppdiktet() normal = kraftsystem.relativ_prisvirkning(inn, Delta_Q=3e6, tort=False) assert set(normal) == {"NO1", "NO2", "NO3", "NO4", "NO5"} assert normal["NO1"] == pytest.approx(30.0 / 600.0) # the price level of the dry year cancels; the factor on the effect does not tort = kraftsystem.relativ_prisvirkning(inn, Delta_Q=3e6, tort=True) assert tort["NO1"] == pytest.approx(1.2 * 30.0 / 600.0) def test_relativ_prisvirkning_krever_positiv_pris_uten_satsingen(oppdiktet: LagInndata) -> None: """A price of zero or less without the programme would turn the sign of the ratio.""" inn = oppdiktet(S0=80e6) with pytest.raises(ValueError, match="not positive"): kraftsystem.relativ_prisvirkning(inn, Delta_Q=3e6, tort=False)