tokenfabrikker/tests/test_utbygging.py
Kjell Tore Guttormsen 5c91cadf4c
feat(kode): add the build-out, the power system and the cash flow
- utbygging.py: when each facility starts and which vintages of IT hardware it
  gets (S22, S33), with chips as a stock, replacement before new facilities and
  the export rule.
- kraftsystem.py: the fitted price lines and the power and capacity balances
  year by year (S23, S24, S25, S38).
- kontantstrom.py: the yearly cash flows per facility, the additionality rule
  by iteration, the residual value and the net present value for Norway (S26,
  S34, S35, S40, S45), for the solutions A, B, B', C and R.
- hendelser.py and data/aarshendelser.npy: the stream of random numbers for the
  export rule, one uniform number per draw and year 2028-2100, from its own seed.
- inndata.py: one set of values in the units of the equations.
- ligninger.py: one function per equation for chapters 4 to 6.

Tested on a made-up set of values only (tests/conftest.py); no result for the
hypothesis has been computed. 271 tests pass. Seven are documented misses
against a source: the five from before, and the price lines for NO3 and NO4,
which miss one of NVE's twelve scenarios by 6.0 and 7.7 per cent.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-12 11:58:24 +01:00

289 lines
11 KiB
Python

"""The build-out (chapter 4): whole calendar years, the limits of S22, vintages and export stops.
Made-up round numbers only.
"""
from __future__ import annotations
import math
import pytest
from tokenfabrikker import ligninger as lg
from tokenfabrikker import utbygging as ub
# ------------------------------------------------- whole years (clarification 21)
@pytest.mark.parametrize(
("tidspunkt", "aar"),
[(2047.0, 2047), (2047.0000000000005, 2047), (2047.3, 2048), (2046.999, 2047)],
)
def test_forste_driftsaar_rundes_opp(tidspunkt: float, aar: int) -> None:
assert lg.forste_driftsaar(tidspunkt=tidspunkt) == aar
@pytest.mark.parametrize(
("varighet", "hele"), [(14.4, 14), (14.5, 15), (14.6, 15), (0.2, 1), (60.0, 60), (1.49, 1)]
)
def test_levetid_rundes_til_naermeste_hele_aar(varighet: float, hele: int) -> None:
assert lg.avrundet_varighet(varighet=varighet) == hele
def test_avrundet_varighet_avviser_null() -> None:
with pytest.raises(ValueError, match="must be positive"):
lg.avrundet_varighet(varighet=0.0)
def test_byggeaar_hele_aar_faar_like_andeler() -> None:
assert lg.byggeaar_andeler(tau=4.0) == (0.25, 0.25, 0.25, 0.25)
def test_byggeaar_brokdelen_ligger_tidligst() -> None:
andeler = lg.byggeaar_andeler(tau=2.5)
assert andeler == pytest.approx((0.4, 0.4, 0.2))
assert math.fsum(andeler) == pytest.approx(1.0)
def test_byggetid_under_ett_aar_betales_aaret_for_oppstart() -> None:
assert lg.byggeaar_andeler(tau=0.4) == pytest.approx((1.0,))
assert lg.byggeaar_andeler(tau=1.0) == (1.0,)
def test_diskonteringsfaktor() -> None:
assert lg.diskonteringsfaktor(rho=0.1, t=2029, T_0=2027) == pytest.approx(1 / 1.21)
assert lg.diskonteringsfaktor(rho=0.1, t=2027, T_0=2027) == 1.0
# ------------------------------------------------------------- S33, S22, S39, S41
def test_s33_byggetiden_forsinker_forste_kraft() -> None:
felles = {"T_0": 2027.0, "tau_reg_min": 5.0}
assert lg.s33_tidligste_tidspunkt_kjernekraft(**felles, tau_F=20.0, tau_N=9.5) == 2047.0
assert lg.s33_tidligste_tidspunkt_kjernekraft(**felles, tau_F=15.0, tau_N=17.1) == 2049.1
assert lg.s33_tidligste_tidspunkt_nett(T_0=2027.0, tau_nett=7.5) == 2034.5
def test_s22_de_tre_leddene() -> None:
assert lg.s22_grense_reaktor(n_reaktor=1500.0, P_IT=1000.0, P_G_N=1500.0) == 1000.0
# 4.38 TWh per year, all of it counted in a dry year, covers 500 MW at full load
assert lg.s22_grense_fornybar(g_fornybar=4.38e6, w_tort=1.0, lam=1.0, PUE=1.0) == pytest.approx(
500.0
)
assert lg.s22_grense_brikker(s_brikker=0.04, G_server=25_000.0, utskifting=400.0) == 600.0
assert lg.s22_reaktoreffekt_B(lam=0.7, PUE=1.2, P_IT=1000.0, KF=0.84) == pytest.approx(1000.0)
def test_s39_enheter_i_B_rundes_opp_for_programmet() -> None:
per_enhet = 8760.0 * 0.8 * 1000.0
assert lg.s39_enheter_B(E_D_sum=2.0 * per_enhet, KF=0.8, P_ref=1000.0) == 2
assert lg.s39_enheter_B(E_D_sum=2.01 * per_enhet, KF=0.8, P_ref=1000.0) == 3
assert lg.s39_enheter_B(E_D_sum=0.0, KF=0.8, P_ref=1000.0) == 0
def test_s39_portefoljen_fylles_i_fast_rekkefolge() -> None:
felles = {"w_tort_vann": 0.8, "E_vann_rest": 2e6, "E_vind_rest": 3e6}
bare_vann = lg.s39_portefolje_C(E_torr=0.8e6, **felles)
assert (bare_vann.vann, bare_vann.vind, bare_vann.havvind) == pytest.approx((1e6, 0.0, 0.0))
alle = lg.s39_portefolje_C(E_torr=7e6, **felles)
# hydropower gives 1.6 TWh in a dry year, wind 3, and offshore wind the remaining 2.4
assert (alle.vann, alle.vind, alle.havvind) == pytest.approx((2e6, 3e6, 2.4e6))
assert alle.torraar(w_tort_vann=0.8) == pytest.approx(7e6)
assert alle.sum == pytest.approx(7.4e6)
@pytest.mark.parametrize("E_normal", [0.5e6, 2e6, 4e6, 9e6])
def test_s39_takt_og_portefolje_er_hverandres_inverse(E_normal: float) -> None:
felles = {"w_tort_vann": 0.8, "E_vann_rest": 2e6, "E_vind_rest": 3e6}
torr = lg.s39_torraarsenergi_av_takt(E_normal=E_normal, **felles)
assert lg.s39_portefolje_C(E_torr=torr, **felles).sum == pytest.approx(E_normal)
def test_s41_fordeles_paa_aarene_til_forste_anlegg() -> None:
per_aar = lg.s41_programkostnad(
andel_myndighet=0.2, aarsverk_prog=1000.0, c_aarsverk=1e6, t_1=2037, T_0=2027
)
assert per_aar == pytest.approx(20e6)
with pytest.raises(ValueError, match="after the decision year"):
lg.s41_programkostnad(
andel_myndighet=0.2, aarsverk_prog=1000.0, c_aarsverk=1e6, t_1=2027, T_0=2027
)
# ---------------------------------------------------------------- the export rule
def test_eksportstopp_varer_avrundet_antall_aar() -> None:
u = (0.9, 0.05, 0.9, 0.9, 0.9, 0.9)
assert ub.eksportstopp(u=u, phi_X=0.1, D_X=2.4) == (False, True, True, False, False, False)
assert ub.eksportstopp(u=u, phi_X=0.1, D_X=2.5) == (False, True, True, True, False, False)
def test_eksportstopp_ny_regel_forlenger_stoppen() -> None:
u = (0.05, 0.9, 0.05, 0.9, 0.9, 0.9)
assert ub.eksportstopp(u=u, phi_X=0.1, D_X=3.0) == (True, True, True, True, True, False)
def test_ingen_eksportregel_naar_sannsynligheten_er_null() -> None:
assert ub.eksportstopp(u=(0.0, 0.5, 0.99), phi_X=0.0, D_X=4.0) == (False, False, False)
def test_eksportstopp_stopper_ved_sluttaaret() -> None:
assert ub.eksportstopp(u=(0.9, 0.9, 0.0), phi_X=0.1, D_X=4.0) == (False, False, True)
# ------------------------------------------------------------------ the schedule
def _plan(**endringer: object) -> ub.Plan:
argumenter: dict[str, object] = {
"N_anlegg": 3,
"T_0": 2027,
"T_s": 2040,
"t_min": 2030,
"P_IT": 1000.0,
"L_H": 3,
"L_N": 20,
"brikker_per_aar": 500.0,
"kraftrom": ub.kraftrom_ubegrenset,
"stopp": (False,) * 13,
}
argumenter.update(endringer)
return ub.utbyggingsplan(**argumenter) # type: ignore[arg-type]
def test_brikker_samles_opp_fra_beslutningsaaret_og_utskifting_gaar_foran() -> None:
"""Hand-computed: 500 MW of chips a year, facilities of 1 000 MW, vintages of three years.
Stock after deliveries: 2028 500, 2029 1 000, 2030 1 500 (facility 1 starts, 500 left),
2031 1 000 (facility 2 starts, 0 left), 2032 500, 2033 1 000 (facility 1's vintage is
replaced before facility 3 can start), 2034 500 (facility 2's vintage has expired and
waits), 2035 1 000 (facility 2 replaced), 2036 500 (facility 1 waits), 2037 1 000
(facility 1 replaced), 2038 500 (facility 2 waits), 2039 1 000 (facility 2 replaced),
2040 500 (facility 1 waits). Facility 3 never gets chips.
"""
plan = _plan()
assert [a.start for a in plan.anlegg] == [2030, 2031]
assert plan.anlegg[0].aarganger == (2030, 2033, 2037)
assert plan.anlegg[1].aarganger == (2031, 2035, 2039)
assert not plan.gjennomforbar
assert plan.t_1 == 2030
def test_utlevd_aargang_staar_uten_inntekt_til_den_byttes() -> None:
plan = _plan()
assert plan.aargang(1, 2033) == 2031
assert plan.aargang(1, 2034) is None # expired, no chips in stock
assert plan.aargang(1, 2035) == 2035
assert plan.aargang(0, 2036) is None
assert plan.aargang(0, 2029) is None # not in operation yet
assert plan.i_drift(1, 2034) # the facility is still operated
def test_den_som_har_ventet_lengst_faar_brikker_forst() -> None:
"""Chips for one facility a year from 2030, vintages of two years, a stop in 2033.
Facility 1 starts in 2030 and is replaced in 2032; facility 2 starts in 2031. Its vintage
expires in 2033, during the stop, so it waits. In 2034 facility 1 expires too, and there
are chips for one: facility 2, which expired first. Facility 1 follows in 2035. After
that each is replaced the year it expires.
"""
stopp = [False] * 13
stopp[2033 - 2028] = True
plan = _plan(
N_anlegg=2,
L_H=2,
brikker_per_aar=1000.0,
t_min=2030,
stopp=tuple(stopp),
forste_brikkeaar=2030,
)
assert plan.anlegg[0].aarganger == (2030, 2032, 2035, 2037, 2039)
assert plan.anlegg[1].aarganger == (2031, 2034, 2036, 2038, 2040)
assert plan.antall_med_last(2033) == 1
assert plan.antall_med_last(2034) == 1
assert plan.antall_i_drift(2034) == 2
assert not plan.har_last(0, 2034)
assert plan.har_last(1, 2034)
def test_reaktortakten_begrenser_antall_anlegg() -> None:
"""1 000 MW of reactors a year and 2 500 MW per facility: room for k after 2.5 k years."""
plan = _plan(
brikker_per_aar=5000.0,
L_H=20,
kraftrom=ub.kraftrom_reaktor(n_reaktor=1000.0, P_G_N=2500.0, T_0=2027),
)
assert [a.start for a in plan.anlegg] == [2030, 2032, 2035]
assert plan.gjennomforbar
def test_flere_anlegg_kan_starte_samme_aar() -> None:
plan = _plan(brikker_per_aar=5000.0, L_H=20, t_min=2035)
assert [a.start for a in plan.anlegg] == [2035, 2035, 2035]
assert plan.antall_i_drift(2034) == 0
assert plan.antall_i_drift(2035) == 3
def test_fornybar_takt_regnes_i_toerraarsenergi() -> None:
"""2 TWh a year: hydropower first (2 TWh potential, 0.8 in a dry year), then wind.
Dry-year energy after x years is 1.6 + 2 (x - 1) TWh for x >= 1. One facility needs
5 TWh: room for one after 2.7 years (first whole year 2030), for two after 5.2 (2033).
"""
rom = ub.kraftrom_fornybar(
g_fornybar=2e6, w_tort_vann=0.8, E_vann_rest=2e6, E_vind_rest=1e6, E_D=5e6, T_0=2027
)
assert rom(2028) == pytest.approx(1.6 / 5)
assert rom(2030) == pytest.approx(5.6 / 5)
plan = _plan(N_anlegg=2, brikker_per_aar=5000.0, L_H=20, t_min=2028, kraftrom=rom)
assert [a.start for a in plan.anlegg] == [2030, 2033]
def test_eksportstopp_gir_verken_leveranser_nye_anlegg_eller_nye_aarganger() -> None:
"""Chips for two facilities a year, but a stop in 2030-2031 and in 2033.
Facility 1 starts in 2032 instead of 2030; nothing is delivered during the stop, so the
stock in 2032 is what 2028, 2029 and 2032 gave. Its vintage expires in 2033, inside the
second stop, and is replaced in 2034.
"""
stopp = [False] * 13
for aar in (2030, 2031, 2033):
stopp[aar - 2028] = True
plan = _plan(N_anlegg=1, L_H=1, brikker_per_aar=2000.0, stopp=tuple(stopp))
assert plan.anlegg[0].start == 2032
assert plan.anlegg[0].aarganger[:3] == (2032, 2034, 2035)
assert plan.aargang(0, 2033) is None
def test_anlegget_drives_L_N_aar_og_fornyes_ikke_etter_det() -> None:
plan = _plan(N_anlegg=1, L_N=4, L_H=3, brikker_per_aar=5000.0)
anlegg = plan.anlegg[0]
assert (anlegg.start, anlegg.siste_driftsaar) == (2030, 2033)
assert anlegg.aarganger == (2030, 2033)
assert plan.i_drift(0, 2033)
assert not plan.i_drift(0, 2034)
assert plan.aargang(0, 2034) is None
def test_plan_uten_anlegg_har_ikke_noe_forste_aar() -> None:
plan = _plan(brikker_per_aar=10.0)
assert plan.anlegg == ()
with pytest.raises(ValueError, match="no facility"):
_ = plan.t_1
def test_feil_antall_aar_i_eksportstrommen_avvises() -> None:
with pytest.raises(ValueError, match="one export flag per year"):
_plan(stopp=(False,) * 5)
def test_losningenes_egenskaper() -> None:
assert not ub.Losning.A.paa_nett
assert all(a.paa_nett for a in ub.Losning if a is not ub.Losning.A)
assert [a for a in ub.Losning if a.kjernekraft] == [
ub.Losning.A,
ub.Losning.B,
ub.Losning.B_MERKET,
]