r"""The timeline (chapter 6, section «Tidslinjen»; S50, chapter 17.2, clarifications 75 and 99). Reads ``tidslinje.json`` (MW IT in operation per year, solution, step and type of power: median, p10, p90 and the shares of the draws with at least 100 MW and at least 1 000 MW) and, for the step the verdicts are read on and the share of paths that cannot be built, ``dommer.json``. To explain why the share of the draws with capacity in operation has a ceiling and falls towards 2100, it also reads three inputs as data, never through the model code: ``modell/parametere.json`` (the ranges of phi_X, D_X, L_N, L_H, T0 and T_slutt), ``data/trekninger.npy`` with ``data/trekninger.json`` (the drawn values) and ``data/aarshendelser.npy`` (the stream for the export rule). From them it computes, under the rules as the code states them (``utbygging.eksportstopp``, ``ligninger.avrundet_varighet``, ``ligninger.forste_driftsaar``, S33), two kinds of derived numbers: the share of the draws with an export stop in force in a year, and the share of the draws in which a facility that started in its earliest possible year (S33) would have passed the end of its life before T_slutt. Writes the tables ``tidslinje-start.tex`` and ``tidslinje-neste.tex`` and the figure data ``tidslinje-andel.dat`` and ``tidslinje-neste.dat``, and returns every number ``kap6-tidslinje.tex`` shows, under keys that start with ``tidslinje.``. Standard library only. """ from __future__ import annotations import ast import json import math import sys from array import array from typing import TYPE_CHECKING, Any from resultater.felles import LOSNING, Tall, fra_fil, hent, les, norsk if TYPE_CHECKING: from collections.abc import Callable from pathlib import Path from resultater.felles import Steg TID = "tidslinje.json" DOM = "dommer.json" PARAM = "modell/parametere.json" TREKN = "data/trekninger.npy" HEND = "data/aarshendelser.npy" LOSNINGER = ("A", "B", "B'", "C", "R", "T-F", "T-K") # In the figures A, B and B' are one line: the module checks that their series are identical. FIGUR = ("A", "C", "R", "T-F", "T-K") ALLE = "alle" KJERNE_NETT = "kjernekraft paa nettet" FORNYBAR = "ny fornybar kraft" TELLER = "normalisering" LOSNING_TEX = {"A": "A", "B": "B", "B'": "$B'$", "C": "C", "R": "R", "T-F": "T-F", "T-K": "T-K"} HALV = 0.5 # Clarification 6: a tolerance in the last digit when a point in time is rounded up to a year. TOLERANSE = 1e-12 # The body of a table ends with its bottom rule: \bottomrule cannot follow \input in a tabular. BUNN = "\\bottomrule\n" class Samler: """Collects the numbers under their keys and refuses one key with two values.""" def __init__(self) -> None: """Start empty.""" self.tall: dict[str, Tall] = {} def __call__(self, nokkel: str, tall: Tall) -> Tall: """Store one number and give it back.""" if nokkel in self.tall and self.tall[nokkel] != tall: melding = f"{nokkel} har to ulike verdier" raise ValueError(melding) self.tall[nokkel] = tall return tall def _aar(aar: int, fil: str, forklaring: str) -> Tall: """A derived calendar year, shown without a thousands space.""" return Tall(str(aar), fil, (), aar, "avledet", 0, forklaring) def _aar_fra_fil(data: Any, fil: str, sti: tuple[Steg, ...]) -> Tall: # noqa: ANN401 """A calendar year that stands in a file, shown without a thousands space.""" t = fra_fil(data, fil, sti, "tekst") return Tall(str(t.raa), t.fil, t.sti, t.raa, t.omregning, 0) def _andel(teller: int, n: int, fil: str, forklaring: str) -> Tall: """A derived share, kept as a share and shown in per cent (rounded in decimal, felles.norsk).""" verdi = teller / n return Tall( norsk(verdi, 1, "100"), fil, (), verdi, "avledet", 1, forklaring + "; shown in per cent" ) def _pst(t: Tall) -> str: return t.vist if t.raa is None else t.vist + r"~\%" # ------------------------------------------------------------------ the timeline file def _trinn_indeks(t: Any, trinn: int) -> int: # noqa: ANN401 return list(t["stige"]).index(trinn) def _serie_sti(los: str, trinn: int, typ: str = ALLE) -> tuple[Steg, ...]: return ("per_losning", los, ("trinn", trinn), "typer", typ) def _serie(t: Any, los: str, trinn: int, typ: str = ALLE) -> list[dict[str, Any]]: # noqa: ANN401 return hent(t, _serie_sti(los, trinn, typ)) def _forste(rader: list[dict[str, Any]], vilkaar: Callable[[dict[str, Any]], bool]) -> int | None: return next((r["aar"] for r in rader if vilkaar(r)), None) def _sjekk_like(t: Any) -> None: # noqa: ANN401 """A, B and B' are drawn as one line; stop if their series are not identical on every step.""" for trinn in t["stige"]: a = hent(t, ("per_losning", "A", ("trinn", trinn), "typer", ALLE)) for los in ("B", "B'"): if hent(t, ("per_losning", los, ("trinn", trinn), "typer", ALLE)) != a: melding = f"tidslinjen for {los} er ikke lik A på trinn {trinn}" raise ValueError(melding) def _start(t: Any, s: Samler, los: str, trinn: int, navn: str) -> list[str]: # noqa: ANN401 """When a solution first has capacity, its highest share and its share in the last year.""" k = f"tidslinje.{navn}.{LOSNING[los]}" rader = _serie(t, los, trinn) sti = _serie_sti(los, trinn) hvor = f"per_losning.{los}[trinn={trinn}].typer.alle" celler = [] forste = _forste(rader, lambda r: r["andel_minst_100_MW"] > 0) median = _forste(rader, lambda r: r["median"] > 0) halv = _forste(rader, lambda r: r["andel_minst_1000_MW"] >= HALV) for nokkel, verdi, hva in ( ("forste-aar", forste, "andel_minst_100_MW > 0"), ("median-aar", median, "median > 0"), ("halv-1000-aar", halv, "andel_minst_1000_MW >= 0.5"), ): if verdi is None: tall = Tall("--", TID, (), None, "avledet", 0, f"no year in {hvor} with {hva}") else: tall = _aar(verdi, TID, f"the first year in {hvor} with {hva}") celler.append(s(f"{k}.{nokkel}", tall).vist) topp = max(r["andel_minst_100_MW"] for r in rader) toppaar = _forste(rader, lambda r: r["andel_minst_100_MW"] == topp) if toppaar is None: # pragma: no cover - max is always attained raise ValueError(hvor) s( f"{k}.toppaar", _aar(toppaar, TID, f"the first year of the largest andel_minst_100_MW in {hvor}"), ) s(f"{k}.topp", fra_fil(t, TID, (*sti, ("aar", toppaar), "andel_minst_100_MW"), "pst", 1)) slutt = rader[-1]["aar"] s(f"{k}.slutt", fra_fil(t, TID, (*sti, ("aar", slutt), "andel_minst_100_MW"), "pst", 1)) celler += [ _pst(s.tall[f"{k}.topp"]), s.tall[f"{k}.toppaar"].vist, _pst(s.tall[f"{k}.slutt"]), ] return celler def _neste(t: Any, d: Any, s: Samler, los: str, trinn: int) -> list[str]: # noqa: ANN401 """The larger step: paths that cannot be built, first median above zero, the largest median.""" k = f"tidslinje.neste.{LOSNING[los]}" rader = _serie(t, los, trinn) sti = _serie_sti(los, trinn) hvor = f"per_losning.{los}[trinn={trinn}].typer.alle" ib = s( f"{k}.ikke-byggbar", fra_fil( d, DOM, ( "per_scenario", ("navn", TELLER), "trinn", ("trinn", trinn), "losninger", los, "M1", "ikke_byggbar", ), "pst", 1, ), ) median = _forste(rader, lambda r: r["median"] > 0) m_aar = s( f"{k}.median-aar", _aar(median, TID, f"the first year in {hvor} with median > 0") if median is not None else Tall("--", TID, (), None, "avledet", 0, f"no year in {hvor} with median > 0"), ) maks = max(r["median"] for r in rader) maks_aar = _forste(rader, lambda r: r["median"] == maks) if maks_aar is None: # pragma: no cover - max is always attained raise ValueError(hvor) s(f"{k}.maks-aar", _aar(maks_aar, TID, f"the first year of the largest median in {hvor}")) mk = s(f"{k}.maks", fra_fil(t, TID, (*sti, ("aar", maks_aar), "median"), "tall", 0)) slutt = rader[-1]["aar"] sl = s(f"{k}.slutt", fra_fil(t, TID, (*sti, ("aar", slutt), "median"), "tall", 0)) p90 = s(f"{k}.slutt-p90", fra_fil(t, TID, (*sti, ("aar", slutt), "p90"), "tall", 0)) return [_pst(ib), m_aar.vist, mk.vist, s.tall[f"{k}.maks-aar"].vist, sl.vist, p90.vist] def _forste_aar( s: Samler, nokkel: str, *, rader: list[dict[str, Any]], hvor: str, felt: str, terskel: float, ) -> int | None: """Store the first year a field is above zero (``terskel`` 0) or at least ``terskel``.""" if terskel == 0: aar, hva = _forste(rader, lambda r: r[felt] > 0), f"{felt} > 0" else: aar, hva = _forste(rader, lambda r: r[felt] >= terskel), f"{felt} >= {terskel}" if aar is not None: s(nokkel, _aar(aar, TID, f"the first year in {hvor} with {hva}")) return aar def _storste( t: Any, # noqa: ANN401 s: Samler, nokkel: str, *, sti: tuple[Steg, ...], felt: str, omregning: tuple[str, int], ) -> int: """Store the largest value of a field over the years, and the first year it is reached.""" rader = hent(t, sti) hoyest = max(r[felt] for r in rader) aar = next(r["aar"] for r in rader if r[felt] == hoyest) hvor = ".".join(f"[{x[0]}={x[1]}]" if isinstance(x, tuple) else str(x) for x in sti) s(f"{nokkel}-aar", _aar(aar, TID, f"the first year of the largest {felt} in {hvor}")) s(nokkel, fra_fil(t, TID, (*sti, ("aar", aar), felt), *omregning)) return aar def _tk_overgang(t: Any, s: Samler, trinn: int, navn: str) -> None: # noqa: ANN401 """T-K: when modules on nuclear power on the grid appear, and how much renewable is there.""" k = f"tidslinje.{navn}.TK" kjerne = _serie(t, "T-K", trinn, KJERNE_NETT) hvor = f"per_losning.T-K[trinn={trinn}].typer.{KJERNE_NETT}" sti = _serie_sti("T-K", trinn, KJERNE_NETT) fsti = _serie_sti("T-K", trinn, FORNYBAR) _forste_aar( s, f"{k}.kjerne-forste-aar", rader=kjerne, hvor=hvor, felt="andel_minst_100_MW", terskel=0 ) _storste(t, s, f"{k}.kjerne-topp", sti=sti, felt="andel_minst_100_MW", omregning=("pst", 2)) median = _forste_aar( s, f"{k}.kjerne-median-aar", rader=kjerne, hvor=hvor, felt="median", terskel=0 ) if median is None: return s(f"{k}.kjerne-median", fra_fil(t, TID, (*sti, ("aar", median), "median"), "tall", 0)) _forste_aar( s, f"{k}.kjerne-halv-aar", rader=kjerne, hvor=hvor, felt="andel_minst_100_MW", terskel=HALV ) _storste(t, s, f"{k}.kjerne-maks", sti=sti, felt="median", omregning=("tall", 0)) s(f"{k}.fornybar-ved-kjerne", fra_fil(t, TID, (*fsti, ("aar", median), "median"), "tall", 0)) _storste(t, s, f"{k}.fornybar-maks", sti=fsti, felt="median", omregning=("tall", 0)) # ------------------------------------------------------------------ the inputs read as data def _les_npy(sti: Path) -> tuple[int, int, array[float]]: """Read a two-dimensional float64 table in numpy's .npy format, version 1 or 2, C order.""" raa = sti.read_bytes() if raa[:6] != b"\x93NUMPY": melding = f"{sti} er ikke en .npy-fil" raise ValueError(melding) versjon = raa[6] if versjon == 1: lengde, start = int.from_bytes(raa[8:10], "little"), 10 elif versjon in {2, 3}: lengde, start = int.from_bytes(raa[8:12], "little"), 12 else: melding = f"{sti}: ukjent .npy-versjon {versjon}" raise ValueError(melding) hode = ast.literal_eval(raa[start : start + lengde].decode("latin1")) if hode["descr"] != " tuple[Steg, ...]: # noqa: ANN401 """The path to one parameter's distribution in the parameter file.""" hent(p, ("parametere", ("id", pid))) return ("parametere", ("id", pid), "fordeling") def _rundet(varighet: float) -> int: """Clarification 21 as ``ligninger.avrundet_varighet``: nearest year, halves up, at least 1.""" return max(1, math.floor(varighet + 0.5)) def _tak(tidspunkt: float) -> int: """Clarification 21 as ``ligninger.forste_driftsaar``: up to a whole year, with tolerance.""" return math.ceil(tidspunkt * (1.0 - TOLERANSE)) def _inndata(rot: Path, s: Samler, aar_stopp: tuple[int, ...]) -> None: """The ranges behind the explanation, and what the draws and the stream give under the rules.""" p = json.loads((rot / PARAM).read_text(encoding="utf-8")) s("tidslinje.param.phi-lav", fra_fil(p, PARAM, (*_parameter(p, "phi_X"), "lav"), "pst", 0)) s("tidslinje.param.phi-hoy", fra_fil(p, PARAM, (*_parameter(p, "phi_X"), "hoy"), "pst", 0)) s("tidslinje.param.DX-lav", fra_fil(p, PARAM, (*_parameter(p, "D_X"), "lav"), "tall", 0)) s("tidslinje.param.DX-hoy", fra_fil(p, PARAM, (*_parameter(p, "D_X"), "hoy"), "tall", 0)) s("tidslinje.param.LN-lav", fra_fil(p, PARAM, (*_parameter(p, "L_N"), "lav"), "tall", 0)) s("tidslinje.param.LN-modus", fra_fil(p, PARAM, (*_parameter(p, "L_N"), "modus"), "tall", 0)) s("tidslinje.param.LN-hoy", fra_fil(p, PARAM, (*_parameter(p, "L_N"), "hoy"), "tall", 0)) s("tidslinje.param.LH-lav", fra_fil(p, PARAM, (*_parameter(p, "L_H"), "verdier", 0), "tall", 0)) s( "tidslinje.param.LH-hoy", fra_fil(p, PARAM, (*_parameter(p, "L_H"), "verdier", -1), "tall", 0), ) T0 = int(hent(p, (*_parameter(p, "T0"), "verdi"))) Ts = int(hent(p, (*_parameter(p, "T_slutt"), "verdi"))) tau_reg = float(hent(p, (*_parameter(p, "tau_reg_min"), "verdi"))) s("tidslinje.param.Ts", _aar_fra_fil(p, PARAM, (*_parameter(p, "T_slutt"), "verdi"))) beskrivelse = json.loads((rot / "data" / "trekninger.json").read_text(encoding="utf-8")) navn = [k["navn"] for k in beskrivelse["kolonner"]] n, m, x = _les_npy(rot / TREKN) n2, aar_n, u = _les_npy(rot / HEND) hend_aar = json.loads((rot / "data" / "aarshendelser.json").read_text(encoding="utf-8"))["aar"] if ( n != n2 or m != len(navn) or aar_n != len(hend_aar) or hend_aar != list(range(T0 + 1, Ts + 1)) ): melding = "trekningene og strømmen passer ikke sammen" raise ValueError(melding) def kol(navn_: str) -> list[float]: j = navn.index(navn_) return [x[i * m + j] for i in range(n)] phi, D = kol("phi_X"), kol("D_X") i_kraft = dict.fromkeys(aar_stopp, 0) noen = 0 for i in range(n): stopp = [False] * aar_n varighet = _rundet(D[i]) for j in range(aar_n): if u[i * aar_n + j] < phi[i]: for jj in range(j, min(j + varighet, aar_n)): stopp[jj] = True noen += any(stopp) for a in aar_stopp: i_kraft[a] += stopp[a - T0 - 1] regel = ( "the export rule as utbygging.eksportstopp states it, applied to data/aarshendelser.npy " "and the columns phi_X and D_X of data/trekninger.npy: a rule starts in year t when the " "stream's number is below phi_X and lasts D_X years rounded (clarification 21); " ) for a in aar_stopp: s( f"tidslinje.stopp.{a}", _andel( i_kraft[a], n, f"{TREKN}, {HEND}", regel + f"share of the draws with a stop in force in {a}", ), ) s( "tidslinje.stopp.noen", _andel( noen, n, f"{TREKN}, {HEND}", regel + f"share of the draws with a stop in force in at least one year {T0 + 1}..{Ts}", ), ) # A facility of A (and B, B') or R cannot start before its first possible operating year # (S33), so it cannot reach the end of its life before that year plus L_N - 1. L = [_rundet(v) for v in kol("L_N")] tidligst = { "A": [ _tak(T0 + max(f, tau_reg + nn)) for f, nn in zip(kol("tau_F"), kol("tau_N"), strict=True) ], "R": [_tak(T0 + v) for v in kol("tau_nett")], } hva = { "A": "ceil(T0 + max(tau_F, tau_reg_min + tau_N)) (S33, A, B, B')", "R": "ceil(T0 + tau_nett) (S33, C, R)", } for los, t1 in tidligst.items(): utlevd = sum(t + l_ - 1 < Ts for t, l_ in zip(t1, L, strict=True)) s( f"tidslinje.utlevd.{los}", _andel( utlevd, n, f"{TREKN}, {PARAM}", f"share of the draws in which {hva[los]} + round(L_N) - 1 < T_slutt: a " "facility that starts in its earliest possible year has passed the end of its life " "before the last year; an upper bound, since a facility never starts earlier", ), ) s( f"tidslinje.tidligst.{los}", _aar(min(t1), TREKN, f"the smallest {hva[los]} over the draws"), ) # ------------------------------------------------------------------ tables and figures def _tabell_start(t: Any, s: Samler, trinn: int) -> str: # noqa: ANN401 linjer = [] for los in LOSNINGER: celler = _start(t, s, los, trinn, "dom") linjer.append(LOSNING_TEX[los] + " & " + " & ".join(celler) + r" \\") return "\n".join(linjer) + "\n" + BUNN def _tabell_neste(t: Any, d: Any, s: Samler, trinn: int) -> str: # noqa: ANN401 linjer = [] for los in LOSNINGER: celler = _neste(t, d, s, los, trinn) linjer.append(LOSNING_TEX[los] + " & " + " & ".join(celler) + r" \\") return "\n".join(linjer) + "\n" + BUNN def _dat_andel(t: Any, trinn: int) -> str: # noqa: ANN401 """The share with at least 100 MW per year on the verdicts' step, for the figure's solutions.""" linjer = [ ( f"# kilde: {TID}, per_losning.[trinn={trinn}].typer.alle[aar].andel_minst_100_MW " f"for L in {', '.join(FIGUR)} (A stands for A, B and B', whose series are identical)" ), ] linjer.append("aar " + " ".join(LOSNING[x] for x in FIGUR)) serier = [_serie(t, los, trinn) for los in FIGUR] for j, rad in enumerate(serier[0]): verdier = [repr(float(sr[j]["andel_minst_100_MW"])) for sr in serier] linjer.append(f"{rad['aar']} " + " ".join(verdier)) return "\n".join(linjer) + "\n" def _dat_neste(t: Any, trinn: int) -> str: # noqa: ANN401 """Median, p10 and p90 of MW in operation every second year on the larger step.""" linjer = [ ( f"# kilde: {TID}, per_losning.[trinn={trinn}].typer.alle[aar].median, .p10, .p90 " f"for L in {', '.join(FIGUR)}; every second year from the first" ), f"# kilde: {TID}, per_losning.T-K[trinn={trinn}].typer.{KJERNE_NETT}[aar].median, TKkjerne", ] navn = [f"{LOSNING[x]}{f}" for x in FIGUR for f in ("med", "p10", "p90")] linjer.append("aar " + " ".join(navn) + " TKkjerne") serier = [_serie(t, los, trinn) for los in FIGUR] kjerne = _serie(t, "T-K", trinn, KJERNE_NETT) for j in range(0, len(serier[0]), 2): verdier = [repr(float(sr[j][f])) for sr in serier for f in ("median", "p10", "p90")] linjer.append( f"{serier[0][j]['aar']} " + " ".join(verdier) + f" {float(kjerne[j]['median'])!r}" ) return "\n".join(linjer) + "\n" def lag(data: Path, ut: Path) -> dict[str, Tall]: """Write the tables and figure data of the timeline, and return its numbers.""" t = les(data, TID) d = les(data, DOM) s = Samler() _sjekk_like(t) if t["antall_trekninger"] != d["antall_trekninger"] or t["stige"] != d["stige"]: melding = "tidslinje.json og dommer.json hører ikke til samme kjøring" raise ValueError(melding) trinn = hent(d, ("trinn",)) s("tidslinje.domstrinn", fra_fil(d, DOM, ("trinn",), "tall", 0)) stige = list(t["stige"]) neste = stige[stige.index(trinn) + 1] s("tidslinje.nestetrinn", fra_fil(t, TID, ("stige", stige.index(neste)), "tall", 0)) s("tidslinje.forste-aar", _aar_fra_fil(t, TID, (*_serie_sti("A", trinn), 0, "aar"))) s("tidslinje.antall", fra_fil(t, TID, ("antall_trekninger",), "tall", 0)) (ut / "tidslinje-start.tex").write_text(_tabell_start(t, s, trinn), encoding="utf-8") (ut / "tidslinje-neste.tex").write_text(_tabell_neste(t, d, s, neste), encoding="utf-8") for los in LOSNINGER: s( f"tidslinje.dom.{LOSNING[los]}.ikke-byggbar", fra_fil( d, DOM, ( "per_scenario", ("navn", TELLER), "trinn", ("trinn", trinn), "losninger", los, "M1", "ikke_byggbar", ), "pst", 1, ), ) _tk_overgang(t, s, trinn, "dom") _tk_overgang(t, s, neste, "neste") (ut / "tidslinje-andel.dat").write_text(_dat_andel(t, trinn), encoding="utf-8") (ut / "tidslinje-neste.dat").write_text(_dat_neste(t, neste), encoding="utf-8") # The explanation of the ceiling and the fall: the stop shares in A's peak year and the end. toppaar = int(s.tall["tidslinje.dom.A.toppaar"].raa) # type: ignore[arg-type] sluttaar = int(_serie(t, "A", trinn)[-1]["aar"]) _inndata(data.parent.parent, s, (toppaar, sluttaar)) # The earliest year a facility of A can have passed the end of its life: its earliest # operating year in any draw plus the shortest lifetime, minus one. forste_a = int(s.tall["tidslinje.dom.A.forste-aar"].raa) # type: ignore[arg-type] ln_lav = int(s.tall["tidslinje.param.LN-lav"].raa) # type: ignore[arg-type] s( "tidslinje.A.tidligst-utlevd", _aar( forste_a + ln_lav - 1, f"{TID}, {PARAM}", "the first year in per_losning.A[trinn=].typer.alle with " "andel_minst_100_MW > 0, plus the low end of L_N in the parameter file, minus 1: the " "last operating year of the earliest facility with the shortest lifetime", ), ) return s.tall