tokenfabrikker/tests/test_nivaaisert.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

405 lines
17 KiB
Python

"""The levelised cost of power and compute over a path: S9 and S11 per year, S15, S19, S27, S42.
Everything is computed on the made-up set in ``conftest.py``, and the expected numbers are
built up here from the round values of that set. They say nothing about the hypothesis.
"""
from __future__ import annotations
import math
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.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."""
E_ENHET = 8760.0 * 0.8 * 1000.0
"""Yearly production of one made-up reactor of 1 000 MW."""
def _crf(rho: float, levetid: float) -> float:
return float(rho / (1.0 - (1.0 + rho) ** -levetid))
def _sff(rho: float, levetid: float) -> float:
return float(rho / ((1.0 + rho) ** levetid - 1.0))
def _byggetid(rho: float, tau: float) -> float:
"""S3: the factor for interest during construction."""
return float(((1.0 + rho) ** tau - 1.0) / (tau * math.log1p(rho)))
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
def _lcoc(c_a: float) -> float:
"""S15 for the made-up Norwegian facility with power at ``c_a`` kr/MWh."""
return 110e6 * _crf(0.08, 15) + 200e6 * _crf(0.08, 5) + 2.5e6 + 8760.0 * 0.75 * 1.2 * c_a
def _pris(antall_med_last: int, ny_kraft: float = 0.0) -> float:
"""The made-up price line: 700 kr/MWh minus 10 kr per TWh of power balance."""
return 700.0 - 10e-6 * (10e6 - 0.5 * antall_med_last * E_D + ny_kraft)
def _utkobling(mw: float) -> float:
"""S47, disconnection: 100 hours of the competitor-cost price for ``mw`` missing."""
return 100.0 * _lcoc_v() / (8760.0 * 1.2) * mw
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)
# ------------------------------------------------------- the equations one at a time
def test_s47_utkobling_er_den_andre_armen() -> None:
felles = {"h_knapp": 100.0, "mu": 1.0, "y": 105.12e6, "PUE": 1.2}
assert lg.s47_utkobling(Delta_R_utfall=-600.0, **felles) == pytest.approx(600e6)
assert lg.s47_utkobling(Delta_R_utfall=50.0, **felles) == 0.0
# the full S47 takes the cheaper arm
billig = lg.s47_effektkostnad(
Delta_R=-600.0, Delta_R_utfall=-600.0, c_fast=0.5e6, c_res=1e6, **felles
)
assert billig == pytest.approx(300e6)
def test_s27_kraftkostnad_slik_en_bedrift_betaler() -> None:
"""Price 500, tax 70; a plant with LCOE 600 and value factor 1.1 covering twice the use."""
ledd = lg.Portefoljeledd(x=1.0, LCOE=600.0, psi=1.1, regelen_binder=True)
c = lg.s27_kraftkostnad_bedrift_nett(
p_z=500.0, t_el=70.0, portefolje=[ledd], E_G=2e6, E_D=1e6, tariff=30e6, F_utkobling=10e6
)
# the additionality rule does not apply in M2: the plant's net term counts in full
assert c == pytest.approx(500.0 + 70.0 + (600.0 - 550.0) * 2.0 + 40.0)
assert lg.s27_nettariff(t_nett=0.3e6, PUE=1.2, P_IT=1000.0) == pytest.approx(360e6)
assert lg.s27_kraftkostnad_bedrift_dedikert(c_A=900.0, t_el=70.0) == 970.0
def test_s27_og_s42() -> None:
assert lg.s27_margin_bedrift(mu=0.9, Gamma=1.0, y=100.0, LCOC_bed=80.0) == pytest.approx(10.0)
sum_42 = lg.s42_naaverdivektet_margin(
marginer=[10.0, -22.0], aarganger=[2027, 2029], rho_D=0.1, T_0=2027
)
assert sum_42 == pytest.approx(10.0 - 22.0 / 1.21)
# ------------------------------------------------------------ R: bought from the grid
def test_R_kraftkostnad_per_aar_og_aargang(oppdiktet: LagInndata) -> None:
"""One facility in R to 2040: c_a = (1 + n_tap) p + (H_a + F_a) / E_D in every year.
H_a is the annuity of the grid connection for 600 MW (half the draw displaces private
data centres), F_a the disconnection arm for the same 600 MW.
"""
inn = oppdiktet(T_slutt=2040)
res = _regn(inn, Losning.R)
kost = nv.kraftkostnad(inn, res)
H_a = 5e6 * 600.0 * _crf(0.06, 40)
c_a = 1.05 * _pris(1) + (H_a + _utkobling(600.0)) / E_D
assert np.isnan(kost.c_a[0, :3]).all()
assert kost.c_a[0, 3:] == pytest.approx(c_a)
assert not kost.bedrift
assert kost.S42 is None
assert kost.F is res.F_a
assert [(a.anlegg, a.t_v, a.aar) for a in kost.aarganger] == [
(0, 2030, (2030, 2031, 2032, 2033, 2034)),
(0, 2035, (2035, 2036, 2037, 2038, 2039)),
(0, 2040, (2040,)),
]
for aargang in kost.aarganger:
assert aargang.c_a == pytest.approx(c_a)
assert aargang.LCOC == pytest.approx(_lcoc(c_a))
assert aargang.inntekt == pytest.approx(_lcoc_v())
margin = aargang.margin
assert margin is not None
assert margin.kraftfortrinn == pytest.approx(8760.0 * 0.75 * 1.2 * (800.0 - c_a))
assert margin.knapphet == 0.0
assert margin.drift_bygg_skatt == pytest.approx(3e6 - 2.5e6 - 10e6 * _crf(0.08, 15))
assert margin.usolgt == 0.0
assert margin.sum == pytest.approx(aargang.overskudd)
def test_kraftkostnaden_vektes_med_naaverdi_over_aargangens_aar(oppdiktet: LagInndata) -> None:
"""Two facilities in R that start in 2030 and 2031 (clarification 15).
The second facility weakens the power balance, so the price is higher from 2031. The
first vintage of facility 1 has one year at the low cost and four at the high, each
discounted with rho_D to the vintage's first year.
"""
inn = oppdiktet(T_slutt=2040)
res = _regn(inn, Losning.R, N_anlegg=2)
assert [a.start for a in res.plan.anlegg] == [2030, 2031]
kost = nv.kraftkostnad(inn, res)
faste = (5e6 * 600.0 * _crf(0.06, 40) + _utkobling(600.0)) / E_D
lav, hoy = 1.05 * _pris(1) + faste, 1.05 * _pris(2) + faste
assert kost.c_a[0, 3] == pytest.approx(lav)
assert kost.c_a[0, 4] == pytest.approx(hoy)
assert kost.c_a[1, 4] == pytest.approx(hoy)
assert np.isnan(kost.c_a[1, 3])
vekter = [1.08**-x for x in range(5)]
ventet = (lav * vekter[0] + hoy * math.fsum(vekter[1:])) / math.fsum(vekter)
forste = next(a for a in kost.aarganger if (a.anlegg, a.t_v) == (0, 2030))
assert forste.c_a == pytest.approx(ventet)
assert lav < forste.c_a < hoy
def test_anlegg_uten_brikker_har_ingen_kraftkostnad(oppdiktet: LagInndata) -> None:
"""An export stop from 2035 to 2037: the vintage from 2030 is replaced in 2038."""
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)
kost = nv.kraftkostnad(inn, res)
assert np.isnan(kost.c_a[0, 2035 - 2027 : 2038 - 2027]).all()
assert not np.isnan(kost.c_a[0, 2038 - 2027])
assert [a.aar[0] for a in kost.aarganger][:2] == [2030, 2038]
assert kost.aarganger[0].aar == (2030, 2031, 2032, 2033, 2034)
# ------------------------------------------------------------- A: the dedicated plant
def test_A_kraftkostnaden_er_S9(oppdiktet: LagInndata) -> None:
"""One facility in A: units of 50 MW at 100 mill. kr/MW, four years to build, 8 % return."""
inn = oppdiktet()
res = _regn(inn, Losning.A)
N_u = res.dim.N_u
enhet = (
100e6 * 50.0 * _byggetid(0.08, 4.0) * _crf(0.08, 20)
+ 1e6 * 50.0
+ 5e6 * 50.0 * _sff(0.08, 20)
)
buffer = 600.0 * (10e6 * _crf(0.08, 20) + 0.4e6)
forsyning = 2.0 * 50.0 * 10e6 * _crf(0.08, 40) + 0.02 * N_u * 50.0 * 10e6 * _crf(0.08, 20)
ulykke = N_u * 1e-5 * (1e12 * 50.0 / 1000.0 - 8e9)
c_A = (N_u * enhet + buffer + forsyning + ulykke) / E_D + 50.0 + 100.0 + 30.0
kost = nv.kraftkostnad(inn, res)
i_drift = [j for j, t in enumerate(res.aar) if res.plan.har_last(0, t)]
assert kost.c_a[0, i_drift] == pytest.approx(c_A)
assert nv.kraftkostnad_dedikert(inn, res, 0, 0.08) == pytest.approx(c_A)
assert not kost.F.any()
# as a business: the electricity tax also applies behind the meter, and there is no tariff
bedrift = nv.kraftkostnad(inn, res, bedrift=True)
assert bedrift.c_a[0, i_drift] == pytest.approx(c_A + 70.0)
# the revenue carries the revenue factor of S44
assert kost.aarganger[0].inntekt == pytest.approx(res.dim.Gamma * _lcoc_v())
margin = kost.aarganger[0].margin
assert margin is not None
assert margin.usolgt == pytest.approx((1.0 - res.dim.Gamma) * _lcoc_v())
def test_A_enhetene_nummereres_paa_tvers_av_anleggene(oppdiktet: LagInndata) -> None:
"""With learning the second facility's units are cheaper than the first's."""
inn = oppdiktet(ell=0.1, s_brikker=0.12)
res = _regn(inn, Losning.A, N_anlegg=2)
forste = nv.kraftkostnad_dedikert(inn, res, 0, 0.08)
andre = nv.kraftkostnad_dedikert(inn, res, 1, 0.08)
assert andre < forste
# ------------------------------------------------------ B and B': nuclear on the grid
def _lcoe_stor_enhet() -> float:
"""S5 for made-up units of 1 000 MW on the grid, without the accident cost, kr/MWh."""
aarlig = (
100e6 * 1000.0 * _byggetid(0.06, 4.0) * _crf(0.06, 20)
+ 1e6 * 1000.0
+ 5e6 * 1000.0 * _sff(0.06, 20)
)
return aarlig / E_ENHET + 50.0 + 70.0 + 30.0
def test_B_kraftkostnaden_bruker_programmets_enheter(oppdiktet: LagInndata) -> None:
"""Three facilities that start together in 2035 and four units of 1 000 MW (clarification 2).
The four units produce 28.0 TWh and the facilities use 23.7: the price falls. Each
MWh used carries the units' net cost times production over use for the programme.
"""
inn = oppdiktet(s_brikker=0.12)
res = _regn(inn, Losning.B, N_anlegg=3)
assert res.varsigma == {ks.KJERNEKRAFT: 1.0}
j = 2040 - 2027
pris = _pris(3, ny_kraft=4 * E_ENHET)
assert res.kraftaar[j].pris == pytest.approx(pris)
ulykke = 4 * 1e-5 * (1e12 - 8e9) / (4 * E_ENHET)
netto = (_lcoe_stor_enhet() + ulykke - pris) * 4 * E_ENHET / (3 * E_D)
H_a = 5e6 * (600.0 + 0.75 * 1.2 * 1000.0 / 0.8) * _crf(0.06, 40)
c_a = 1.05 * pris + netto + (H_a + float(res.F_a[j]) / 3.0) / E_D
kost = nv.kraftkostnad(inn, res)
assert kost.c_a[:, j] == pytest.approx(c_a)
def test_bindende_tilleggsregel_gir_nettoledd_null(oppdiktet: LagInndata) -> None:
"""A cheap reactor that pays its way on the grid neither adds nor takes (clarification 11)."""
inn = oppdiktet(c_N_ref=10e6, f_N=0.3e6, s_brikker=0.12)
for losning in (Losning.B, Losning.B_MERKET):
res = _regn(inn, losning, N_anlegg=2)
assert res.varsigma[ks.KJERNEKRAFT] < 1.0
j = 2040 - 2027
H_a = 5e6 * (600.0 + res.dim.P_G_N) * _crf(0.06, 40)
c_a = 1.05 * res.kraftaar[j].pris + (H_a + float(res.F_a[j]) / 2.0) / E_D
assert nv.kraftkostnad(inn, res).c_a[:, j] == pytest.approx(c_a)
# a business is credited the plant's surplus: S45 does not apply in M2
bedrift = nv.kraftkostnad(inn, res, bedrift=True)
uten_verk = res.kraftaar[j].pris + 70.0 + (0.3e6 * 1200.0 + float(bedrift.F[j]) / 2.0) / E_D
assert (bedrift.c_a[:, j] < uten_verk).all()
def test_B_merket_bruker_anleggets_eget_verk(oppdiktet: LagInndata) -> None:
"""B': the plant of A on the grid. Small units: fuel 100 kr/MWh and a lower capacity factor."""
inn = oppdiktet()
res = _regn(inn, Losning.B_MERKET)
N_u = res.dim.N_u
verk = res.verk[0][0]
assert verk.energi == pytest.approx(8760.0 * 0.8 * 0.96 / 0.98 * N_u * 50.0)
aarlig = N_u * (
100e6 * 50.0 * _byggetid(0.06, 4.0) * _crf(0.06, 20)
+ 1e6 * 50.0
+ 5e6 * 50.0 * _sff(0.06, 20)
)
ulykke = N_u * 1e-5 * (1e12 * 50.0 / 1000.0 - 8e9)
lcoe = (aarlig + ulykke) / verk.energi + 50.0 + 100.0 + 30.0
assert nv.lcoe_kjernekraft(
inn, res, numre=range(1, N_u + 1), E_N=verk.energi, rho=0.06
) == pytest.approx(lcoe)
j = 2040 - 2027
pris = res.kraftaar[j].pris
H_a = 5e6 * (600.0 + N_u * 50.0) * _crf(0.06, 40)
c_a = 1.05 * pris + (lcoe - pris) * verk.energi / E_D + (H_a + float(res.F_a[j])) / E_D
assert nv.kraftkostnad(inn, res).c_a[0, j] == pytest.approx(c_a)
# ----------------------------------------------------------------- C: renewable power
def test_C_bruker_anleggets_egen_portefolje(oppdiktet: LagInndata) -> None:
"""One facility in C: hydropower 2 TWh, wind 3 and offshore wind 3.284 (clarification 10).
Hydropower has interest during construction in its investment figure already; the two
others get the factor of S3. A technology the additionality rule binds for counts zero.
"""
inn = oppdiktet()
res = _regn(inn, Losning.C)
lcoe = {
"vann": 20e6 * _crf(0.06, 40) / 3000.0 + 70.0,
"vind": 14e6 * _byggetid(0.06, 2.0) * _crf(0.06, 30) / 3000.0 + 120.0,
"havvind": (50e6 * _byggetid(0.06, 3.0) * _crf(0.06, 30) + 0.9e6) / 4500.0,
}
for teknologi, ventet in lcoe.items():
assert nv.lcoe_fornybar(inn, teknologi, 0.06) == pytest.approx(ventet)
energi = {"vann": 2e6, "vind": 3e6, "havvind": E_D - 1.6e6 - 3e6}
psi = {"vann": 1.2, "vind": 0.9, "havvind": 1.0}
assert res.varsigma["vann"] < 1.0
j = 2035 - 2027
pris = res.kraftaar[j].pris
netto = math.fsum(
energi[i] * (lcoe[i] - psi[i] * pris) for i in energi if res.varsigma[i] >= 1.0
)
H_a = 5e6 * 600.0 * _crf(0.06, 40)
c_a = 1.05 * pris + (netto + H_a + float(res.F_a[j])) / E_D
assert nv.kraftkostnad(inn, res).c_a[0, j] == pytest.approx(c_a)
# ------------------------------------------------------------------- M2: S27 and S42
def test_R_bedriftsokonomi_haandregnet(oppdiktet: LagInndata) -> None:
"""One facility in R to 2040 as a business pays for it.
Price plus electricity tax (70 kr/MWh), the grid tariff on 1 200 MW at 0.3 mill. kr per
MW, and disconnection for the 600 MW that is missing; no grid investment and no losses.
S42 sums the three vintages with rho_D to their first year.
"""
inn = oppdiktet(T_slutt=2040)
res = _regn(inn, Losning.R)
kost = nv.kraftkostnad(inn, res, bedrift=True)
c_bed = _pris(1) + 70.0 + (0.3e6 * 1200.0 + _utkobling(600.0)) / E_D
assert kost.c_a[0, 3:] == pytest.approx(c_bed)
assert kost.F[3:] == pytest.approx(_utkobling(600.0))
assert not kost.F[:3].any()
margin = _lcoc_v() - _lcoc(c_bed)
for aargang in kost.aarganger:
assert aargang.LCOC == pytest.approx(_lcoc(c_bed))
assert aargang.overskudd == pytest.approx(margin)
assert aargang.margin is None
ventet = margin * (1.08**-3 + 1.08**-8 + 1.08**-13)
assert kost.S42 == pytest.approx(ventet)
def test_M2_bruker_bare_utkoblingsarmen(oppdiktet: LagInndata) -> None:
"""With cheap firm capacity S47 picks that arm in M1; a business still pays disconnection."""
inn = oppdiktet(T_slutt=2040, c_fast=0.1e6)
res = _regn(inn, Losning.R)
j = 2035 - 2027
assert res.F_a[j] == pytest.approx(0.1e6 * 600.0)
assert nv.effektkostnad_bedrift(inn, res)[j] == pytest.approx(_utkobling(600.0))
@pytest.mark.parametrize("losning", list(Losning))
@pytest.mark.parametrize("knapphet", list(Knapphet))
def test_s19_er_en_regnskapsidentitet(
oppdiktet: LagInndata, losning: Losning, knapphet: Knapphet
) -> None:
"""The four parts of S19 sum to mu Gamma y - LCOC_a for every vintage in every solution."""
inn = oppdiktet(mu_rel=0.95, s_brikker=0.08)
res = _regn(inn, losning, N_anlegg=2, knapphet=knapphet)
kost = nv.kraftkostnad(inn, res)
assert len(kost.aarganger) == sum(len(a.aarganger) for a in res.plan.anlegg)
for aargang in kost.aarganger:
assert aargang.margin is not None
assert aargang.margin.sum == pytest.approx(aargang.overskudd)
assert aargang.inntekt == pytest.approx(0.95 * res.dim.Gamma * res.y(aargang.t_v))
def test_knapphetspaaslaget_staar_i_marginen_i_ti_aar(oppdiktet: LagInndata) -> None:
inn = oppdiktet()
res = _regn(inn, Losning.R, knapphet=Knapphet.VARER)
kost = nv.kraftkostnad(inn, res)
paaslag = 150e6 / _lcoc_v() - 1.0
for aargang in kost.aarganger:
assert aargang.margin is not None
ventet = paaslag * _lcoc_v() if aargang.t_v < 2040 else 0.0
assert aargang.margin.knapphet == pytest.approx(ventet)
def test_andre_avkastningskrav_kan_settes_inn(oppdiktet: LagInndata) -> None:
"""The cost of capital of an ownership form replaces the three required returns."""
inn = oppdiktet()
res = _regn(inn, Losning.B_MERKET)
som_for = nv.Renter(rho_D=0.08, rho_N=0.06, rho_A=0.08)
assert nv.renter_i_hovedkjoringen(inn, res) == som_for
hoved = nv.kraftkostnad(inn, res, bedrift=True)
samme = nv.kraftkostnad(inn, res, bedrift=True, renter=som_for)
assert samme.aarganger == hoved.aarganger
billig = nv.kraftkostnad(
inn, res, bedrift=True, renter=nv.Renter(rho_D=0.02, rho_N=0.02, rho_A=0.02)
)
assert billig.aarganger[0].LCOC < hoved.aarganger[0].LCOC
assert billig.aarganger[0].c_a < hoved.aarganger[0].c_a