tokenfabrikker/tests/test_skala.py
Kjell Tore Guttormsen e216702d69
feat(kode): first coding of business economics, ownership, scale, environment and verdicts
Chapters 7 to 11 of the specification, on the made-up set only:

- nivaaisert: the power cost per facility, year and vintage on levelised
  form (S9, S11 with clarifications 2, 11, 13 and 15), S15, S19, S27, S42
- eierskap: the business cash flow with every asset booked, tax, S28, S29
  and S30 for the four ownership forms, the criteria over the draws
- skala: S31 year by year, the first year with stable operation, M6
- miljo: S32 year by year, the rules of M5 (i) and (ii)
- dommer: S37, the verdict per target, the step of the ladder, the overall
  verdicts; tested on invented outcomes
- kontantstrom: each asset is recorded as it is paid, and the price of
  compute exposes the competitor's cost and the markup

This is an intermediate step. The readings are not settled: an independent
reader is coding the same from the specification alone, a first comparison
agrees on the levelised cost, S19, S42, the cash flows, M3 and M4 and
differs on depreciation at the final year, tax, debt and two definitions.
Chapter 16 gets the clarifications before any result is computed.
Nothing is computed for the hypothesis; the parameter file is unchanged.

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

254 lines
10 KiB
Python

"""Scale (chapter 9): S31 year by year, the first year with stable operation and M6.
Made-up round numbers only. Nothing here says anything about the hypothesis.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
import numpy as np
import pytest
from tokenfabrikker import kontantstrom as ks
from tokenfabrikker import ligninger as lg
from tokenfabrikker import nivaaisert as nv
from tokenfabrikker import skala
from tokenfabrikker.kontantstrom import Knapphet
from tokenfabrikker.utbygging import Losning
if TYPE_CHECKING:
from conftest import LagInndata
from tokenfabrikker.inndata import Inndata
E_D = 8760.0 * 0.75 * 1.2 * 1000.0
"""Yearly energy to one made-up data centre: 7 884 000 MWh."""
def _crf(rho: float, levetid: float) -> float:
return float(rho / (1.0 - (1.0 + rho) ** -levetid))
def _lcoc_v() -> float:
"""S15 for the made-up competitor, per MW IT and year."""
return 100e6 * _crf(0.08, 15) + 200e6 * _crf(0.08, 5) + 3e6 + 8760.0 * 0.75 * 1.2 * 800.0
BYGG = 110e9 * _crf(0.08, 15)
"""The yearly cost of the made-up building for 1 000 MW IT."""
MASKINVARE = 200e9 * _crf(0.08, 5)
"""The yearly cost of one made-up vintage of IT hardware for 1 000 MW IT."""
NETT = 5e6 * 600.0 * _crf(0.06, 40)
"""The yearly cost of the grid connection of one made-up facility without power plant."""
def _regn(
inn: Inndata,
losning: Losning,
*,
N_anlegg: int = 1,
knapphet: Knapphet = Knapphet.NORMALISERING,
) -> ks.Programresultat:
dim = ks.dimensjoner(inn, losning)
plan = ks.lag_plan(inn, losning, dim, N_anlegg=N_anlegg)
return ks.regn_program(inn, losning, plan, knapphet, dim=dim)
def test_s31_er_summen_av_leddene() -> None:
B_6 = lg.s31_nettobidrag(
u_e=0.25,
overskudd_utover_avkastning=100.0,
Delta_W=20.0,
K_prog=5.0,
V=3.0,
utslipp=2.0,
)
assert B_6 == pytest.approx(75.0 + 20.0 - 5.0 - 3.0 - 2.0)
def test_R_bidraget_aar_for_aar_og_M6(oppdiktet: LagInndata) -> None:
"""One facility in R to 2040, every term by hand.
Revenue minus operation, power with losses, the yearly cost of building, hardware and
grid connection, and disconnection; plus the gain for Norway of a higher power price,
minus what is displaced in the grid queue; and in 2030 the emissions from the building.
"""
inn = oppdiktet(T_slutt=2040)
res = _regn(inn, Losning.R)
ut = skala.skala(inn, res)
pris = 700.0 - 10e-6 * (10e6 - 0.5 * E_D)
utkobling = 100.0 * _lcoc_v() / (8760.0 * 1.2) * 600.0
overskudd = (
1000.0 * _lcoc_v() - 2.5e9 - 1.05 * pris * E_D - BYGG - MASKINVARE - NETT - utkobling
)
i_drift = overskudd + (pris - 600.0) * 10e6 - 0.5 * E_D * 70.0
assert not ut.B_6[:3].any()
assert ut.B_6[3] == pytest.approx(i_drift - 1e9)
assert ut.B_6[4:] == pytest.approx(i_drift)
assert ut.stabilt_aar == 2030
assert ut.M6 == pytest.approx((i_drift - 1e9) / 500e9)
assert ut.M6_snitt == pytest.approx((i_drift - 1e9 / 5.0) / 500e9)
assert ut.M6_median == pytest.approx((i_drift - 1e9) / 250e9)
assert ut.M6_2050 is None
# at the made-up cost-based price the contribution is negative, so no year beats 2027
assert i_drift < 0
assert (ut.M6_topp, ut.toppaar) == (0.0, 2027)
# with 40 % owned outside Norway only 60 % of the surplus above normal return counts
utenlandsk = skala.skala(inn, res, u_e=0.4)
assert utenlandsk.B_6[4] == pytest.approx(
0.6 * overskudd + (pris - 600.0) * 10e6 - 0.5 * E_D * 70.0
)
def test_toppaaret_ligger_i_aarene_med_knapphetspaaslag(oppdiktet: LagInndata) -> None:
"""«Knapphet varer»: vintages from before 2040 get the markup, so the best year is 2031-2039.
2030 carries the emissions from the building; from 2031 every year to 2039 is the same.
"""
inn = oppdiktet(T_slutt=2045)
ut = skala.skala(inn, _regn(inn, Losning.R, knapphet=Knapphet.VARER))
assert ut.toppaar == 2031
assert ut.M6_topp == pytest.approx(float(ut.B_6[2031 - 2027]) / 500e9)
assert ut.M6_topp > 0
assert ut.B_6[2039 - 2027] == pytest.approx(ut.B_6[2031 - 2027])
assert ut.B_6[2040 - 2027] < ut.B_6[2039 - 2027]
def test_maalet_i_2050_er_null_naar_anlegget_er_gaatt_ut(oppdiktet: LagInndata) -> None:
"""The made-up facility starts in 2030 and lives 20 years: nothing is left in 2050."""
inn = oppdiktet()
ut = skala.skala(inn, _regn(inn, Losning.R))
assert ut.M6_2050 == 0.0
assert ut.B_6[2049 - 2027] != 0.0
def test_stabil_drift_er_forste_aar_alle_anleggene_er_i_drift(oppdiktet: LagInndata) -> None:
inn = oppdiktet()
res = _regn(inn, Losning.R, N_anlegg=3)
assert [a.start for a in res.plan.anlegg] == [2030, 2031, 2032]
assert skala.forste_aar_med_stabil_drift(res) == 2032
ut = skala.skala(inn, res)
assert ut.M6 == pytest.approx(float(ut.B_6[2032 - 2027]) / 500e9)
assert ut.M6_snitt == pytest.approx(float(ut.B_6[5:10].mean()) / 500e9)
def test_uten_aar_med_alle_i_drift_finnes_ikke_M6(oppdiktet: LagInndata) -> None:
"""250 MW of chips a year and facilities that live three years: 2033-2035 and 2037-2039.
Both facilities start, so the path is built, but they are never in operation together.
"""
inn = oppdiktet(s_brikker=0.01, L_N=3.0)
res = _regn(inn, Losning.R, N_anlegg=2)
assert [(a.start, a.siste_driftsaar) for a in res.plan.anlegg] == [(2033, 2035), (2037, 2039)]
ut = skala.skala(inn, res)
assert ut.stabilt_aar is None
assert ut.M6 is None
assert ut.M6_snitt is None
assert ut.M6_median is None
assert ut.B_6[2033 - 2027] != 0.0
def test_anlegg_uten_brikker_baerer_bygg_og_nett_men_ikke_maskinvare(
oppdiktet: LagInndata,
) -> None:
"""An export stop from 2035 to 2037: no revenue, no power and no vintage in operation."""
u = tuple(0.0 if aar == 2035 else 0.5 for aar in range(2028, 2061))
inn = oppdiktet(u_eksport=u, phi_X=0.1, D_X=3.0)
res = _regn(inn, Losning.R)
ut = skala.skala(inn, res)
assert ut.B_6[2036 - 2027] == pytest.approx(-2.5e9 - BYGG - NETT)
assert ut.B_6[2036 - 2027] < ut.B_6[2034 - 2027]
def test_A_uten_brikker_betaler_ikke_variable_kostnader(oppdiktet: LagInndata) -> None:
"""In A the plant's capital and fixed costs run on; fuel, waste fee and variable do not."""
u = tuple(0.0 if aar == 2040 else 0.5 for aar in range(2028, 2061))
inn = oppdiktet(u_eksport=u, phi_X=0.1, D_X=3.0)
res = _regn(inn, Losning.A)
assert not res.plan.har_last(0, 2041)
c_A = nv.kraftkostnad_dedikert(inn, res, 0, 0.08)
ut = skala.overskudd_utover_avkastning(inn, res)
assert ut[2041 - 2027] == pytest.approx(-2.5e9 - BYGG - (c_A - 180.0) * E_D)
med_last = 1000.0 * res.dim.Gamma * _lcoc_v() - 2.5e9 - BYGG - MASKINVARE - c_A * E_D
assert ut[2039 - 2027] == pytest.approx(med_last)
def test_kraftverk_som_tilleggsregelen_binder_for_teller_null(oppdiktet: LagInndata) -> None:
"""Two facilities in B with a cheap reactor: the units neither add nor take in S31."""
inn = oppdiktet(c_N_ref=10e6, f_N=0.3e6, s_brikker=0.12)
res = _regn(inn, Losning.B, N_anlegg=2)
assert res.varsigma[ks.KJERNEKRAFT] < 1.0
j = 2040 - 2027
nett = 5e6 * (600.0 + res.dim.P_G_N) * _crf(0.06, 40)
per_anlegg = (
1000.0 * _lcoc_v() - 2.5e9 - 1.05 * res.kraftaar[j].pris * E_D - BYGG - MASKINVARE - nett
)
ut = skala.overskudd_utover_avkastning(inn, res)
assert ut[j] == pytest.approx(2.0 * per_anlegg - float(res.F_a[j]))
# without the rule the surplus of the units is credited
dim = ks.dimensjoner(inn, Losning.B)
uten = ks.regn_program(
inn, Losning.B, res.plan, Knapphet.NORMALISERING, dim=dim, tilleggsregel=False
)
assert skala.overskudd_utover_avkastning(inn, uten)[j] > ut[j]
@pytest.mark.parametrize("losning", list(Losning))
@pytest.mark.parametrize(
"endringer",
[{}, {"c_N_ref": 10e6, "f_N": 0.3e6}, {"T_slutt": 2045, "L_B": 7.0, "mu_rel": 0.9}],
)
def test_bidraget_er_inntekt_minus_nivaaisert_kostnad(
oppdiktet: LagInndata, losning: Losning, endringer: dict[str, float]
) -> None:
"""Two ways to the same number.
S31 is built from the yearly amounts of the cash flow and the annuity of each asset.
The price side (S15 with S9 or S11) is built per MWh. In every year all the operated
facilities use power, P_IT (mu Gamma y - LCOC_a) summed over them is the same amount.
"""
inn = oppdiktet(u_eksport=None, **endringer)
res = _regn(inn, losning, N_anlegg=3, knapphet=Knapphet.VARER)
kost = nv.kraftkostnad(inn, res)
ut = skala.overskudd_utover_avkastning(inn, res)
sjekket = 0
for j, t in enumerate(res.aar):
i_drift = [k for k in range(3) if res.plan.i_drift(k, t)]
if not i_drift or not all(res.plan.har_last(k, t) for k in i_drift):
continue
sum_margin = 0.0
for k in i_drift:
t_v = res.plan.aargang(k, t)
assert t_v is not None
LCOC = lg.s15_lcoc(
c_bygg=inn["c_bygg"],
d_bygg=inn["d_bygg"],
c_H=lg.s13_it_kostnad(c_H0=inn["c_H0"], gamma_H=inn["gamma_H"], t_v=t_v),
rho_D=inn["rho_D"],
L_B=inn["L_B"],
L_H=inn["L_H"],
o_D=inn["o_D"],
d_o_D=inn["d_o_D"],
lam=inn["lambda"],
PUE=inn["PUE"],
c_a=float(kost.c_a[k, j]),
)
sum_margin += inn["P_IT"] * (inn["mu_rel"] * res.dim.Gamma * res.y(t_v) - LCOC)
assert ut[j] == pytest.approx(sum_margin, rel=1e-9, abs=1.0)
sjekket += 1
assert sjekket >= 10
def test_programmets_enheter_i_B_teller_ogsaa_etter_at_anleggene_er_gaatt_ut(
oppdiktet: LagInndata,
) -> None:
"""Staggered starts: a unit from 2055 still runs in 2058, after the last facility is out."""
inn = oppdiktet()
res = _regn(inn, Losning.B, N_anlegg=2)
assert [(f.gruppe.start, f.gruppe.siste_driftsaar) for f in res.felles][-1] == (2055, 2074)
assert res.plan.antall_i_drift(2058) == 0
ut = skala.overskudd_utover_avkastning(inn, res)
assert ut[2058 - 2027] < 0
assert np.count_nonzero(ut[2056 - 2027 :]) == 5