tokenfabrikker/tests/test_miljo.py
Kjell Tore Guttormsen d4aebad0f9
feat(kode): bring business economics, ownership, scale and verdicts in line with clarifications 41-61
The accident cost stays in S11 and S31 when the additionality rule binds;
the peak year of M6 is taken among operating years; tax depreciation and
carried losses lose real value with price growth; the special tax deducts
the corporate tax, has a return allowance and carries a negative basis with
interest, so it only meets return above r_e; ownership forms are compared
only with forms that pass the EEA assessment; the overall verdict needs a
negative median to fell a solution and takes whether conditions can be
named; M5 does not hold when both (i) and (ii) are broken; a non-positive
price without the programme is an error in M3. Made-up numbers only.

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

152 lines
6.8 KiB
Python

"""Environment (chapter 10): S32 year by year and the rules of M5.
Made-up round numbers only; the source values in the tests of M5 are invented. Nothing
here says anything about the hypothesis.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from tokenfabrikker import kontantstrom as ks
from tokenfabrikker import ligninger as lg
from tokenfabrikker import miljo
from tokenfabrikker.dommer import Dom
from tokenfabrikker.kontantstrom import Knapphet
from tokenfabrikker.miljo import Kildeverdier
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."""
PORTEFOLJE = {"vann": 2e6, "vind": 3e6, "havvind": E_D - 1.6e6 - 3e6}
"""The made-up portfolio of one facility in C: normal-year production per technology, MWh."""
U_FORNYBAR = (0.004 * 2e6 + 0.011 * 3e6 + 0.012 * PORTEFOLJE["havvind"]) / sum(PORTEFOLJE.values())
"""Its emissions per MWh, weighted by production."""
def _regn(inn: Inndata, losning: Losning, *, N_anlegg: int = 1) -> 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.NORMALISERING, dim=dim)
def test_s32_er_summen_av_kraft_maskinvare_og_bygg() -> None:
U = lg.s32_utslipp(kraft=500.0, u_H=1000.0, u_B=2000.0, P_IT=10.0, nye_aarganger=2, nye_bygg=1)
assert U == pytest.approx(500.0 + 20_000.0 + 20_000.0)
@pytest.mark.parametrize("losning", [Losning.C, Losning.R])
def test_utslipp_aar_for_aar_med_fornybar_kraft(oppdiktet: LagInndata, losning: Losning) -> None:
"""One facility from 2030: building and first vintage in 2030, a vintage every five years.
The building lasts 15 years and is renewed in 2045. R uses the same figure for power
as C: the portfolio the facility has in C.
"""
inn = oppdiktet()
res = _regn(inn, losning)
U = miljo.utslipp_livslop(inn, res)
kraft = U_FORNYBAR * E_D
assert miljo.kraftutslipp_per_mwh(inn, res, 0) == pytest.approx(U_FORNYBAR)
assert not U[:3].any()
assert U[2030 - 2027] == pytest.approx(kraft + 1000.0 * 1000.0 + 2000.0 * 1000.0)
assert U[2031 - 2027] == pytest.approx(kraft)
assert U[2035 - 2027] == pytest.approx(kraft + 1000.0 * 1000.0)
assert U[2045 - 2027] == pytest.approx(kraft + 1000.0 * 1000.0 + 2000.0 * 1000.0)
assert not U[2050 - 2027 :].any()
@pytest.mark.parametrize("losning", [Losning.A, Losning.B, Losning.B_MERKET])
def test_kjernekraftens_utslipp_regnes_paa_det_datasenteret_bruker(
oppdiktet: LagInndata, losning: Losning
) -> None:
inn = oppdiktet()
res = _regn(inn, losning)
U = miljo.utslipp_livslop(inn, res)
assert U[2036 - 2027] == pytest.approx(0.012 * E_D)
def test_anlegg_uten_brikker_bruker_ikke_kraft(oppdiktet: LagInndata) -> None:
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)
U = miljo.utslipp_livslop(inn, _regn(inn, Losning.R))
assert U[2036 - 2027] == 0.0
assert U[2038 - 2027] > 1000.0 * 1000.0
@pytest.mark.parametrize("losning", [Losning.C, Losning.R])
def test_hvert_anlegg_bruker_sin_egen_portefolje(oppdiktet: LagInndata, losning: Losning) -> None:
"""The first facility takes the hydropower and the wind; the second gets offshore wind only.
In R, facility k uses the figure of the portfolio facility k has in C.
"""
inn = oppdiktet()
res = _regn(inn, losning, N_anlegg=2)
assert miljo.kraftutslipp_per_mwh(inn, res, 0) == pytest.approx(U_FORNYBAR)
assert miljo.kraftutslipp_per_mwh(inn, res, 1) == pytest.approx(0.012)
U = miljo.utslipp_livslop(inn, res)
# 2034: both facilities are in operation in both solutions, and no vintage is new
assert U[2034 - 2027] == pytest.approx((U_FORNYBAR + 0.012) * E_D)
def test_brukt_brensel_og_areal(oppdiktet: LagInndata) -> None:
inn = oppdiktet()
res = _regn(inn, Losning.B_MERKET)
tonn = miljo.brukt_brensel(inn, res)
assert tonn[2036 - 2027] == pytest.approx(3e-6 * res.verk[0][0].energi)
assert not miljo.brukt_brensel(inn, _regn(inn, Losning.R)).any()
# A: only what the data centre uses is produced
assert miljo.brukt_brensel(inn, _regn(inn, Losning.A))[2036 - 2027] == pytest.approx(3e-6 * E_D)
# 0.1 km2 per TWh for nuclear power, 1.5 for wind, and 0.001 km2 per MW IT for the plot
assert miljo.areal(inn, teknologi="kjernekraft", energi=E_D, P_IT=1000.0) == pytest.approx(
0.7884 + 1.0
)
assert miljo.areal(inn, teknologi="vind_direkte", energi=E_D, P_IT=1000.0) == pytest.approx(
11.826 + 1.0
)
# ------------------------------------------------------------------------ M5
def test_m5_utslipp_kilde_for_kilde() -> None:
"""Invented source values. Nuclear must not be above the lower of wind and hydro in any."""
under = Kildeverdier("X", kjernekraft=5.0, vind=11.0, vann=6.0, vind_hoyeste=50.0)
mellom = Kildeverdier("Y", kjernekraft=12.0, vind=11.0, vann=24.0, vind_hoyeste=56.0)
over = Kildeverdier("Z", kjernekraft=60.0, vind=11.0, vann=24.0, vind_hoyeste=56.0)
assert miljo.m5_utslipp(Losning.A, [under, under]) is Dom.HOLDER
assert miljo.m5_utslipp(Losning.B, [under, mellom]) is Dom.BETINGELSER
assert miljo.m5_utslipp(Losning.A, [under, mellom, over]) is Dom.HOLDER_IKKE
lik = Kildeverdier("W", kjernekraft=6.0, vind=11.0, vann=6.0, vind_hoyeste=50.0)
assert miljo.m5_utslipp(Losning.A, [lik]) is Dom.HOLDER
# C and R meet (i) by definition
assert miljo.m5_utslipp(Losning.C, [over]) is Dom.HOLDER
assert miljo.m5_utslipp(Losning.R, []) is Dom.HOLDER
with pytest.raises(ValueError, match="at least one source"):
miljo.m5_utslipp(Losning.A, [])
def test_m5_areal_mot_vind_paa_land() -> None:
felles = {"vind_direkte": 1.5, "vind_plan": 35.0}
assert miljo.m5_areal(Losning.A, kjernekraft=0.1, **felles) is Dom.HOLDER
assert miljo.m5_areal(Losning.A, kjernekraft=1.5, **felles) is Dom.HOLDER
assert miljo.m5_areal(Losning.B, kjernekraft=10.0, **felles) is Dom.BETINGELSER
assert miljo.m5_areal(Losning.A, kjernekraft=40.0, **felles) is Dom.HOLDER_IKKE
assert miljo.m5_areal(Losning.C, kjernekraft=40.0, **felles) is Dom.HOLDER
def test_m5_samlet() -> None:
assert miljo.m5(Dom.HOLDER, Dom.HOLDER) is Dom.HOLDER
assert miljo.m5(Dom.BETINGELSER, Dom.HOLDER) is Dom.BETINGELSER
assert miljo.m5(Dom.HOLDER, Dom.BETINGELSER) is Dom.BETINGELSER
# "one of (i) and (ii) broken": with both broken it does not hold
assert miljo.m5(Dom.BETINGELSER, Dom.BETINGELSER) is Dom.HOLDER_IKKE
assert miljo.m5(Dom.HOLDER, Dom.HOLDER_IKKE) is Dom.HOLDER_IKKE
assert miljo.m5(Dom.HOLDER, Dom.HOLDER, kostnadsfort=False) is Dom.HOLDER_IKKE