|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from dataclasses import asdict, dataclass |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any, Dict, List, Optional |
| 7 | + |
| 8 | +from .baseline import BaselineInputs, BaselineOutputs, compute_baseline |
| 9 | +from .mechanisms import CosmologyBackground |
| 10 | +from .sweep import SweepRow, run_sweep |
| 11 | + |
| 12 | + |
| 13 | +@dataclass(frozen=True) |
| 14 | +class ReportConfig: |
| 15 | + h0_km_s_mpc: float = 67.4 |
| 16 | + omega_lambda: float = 0.6889 |
| 17 | + omega_m: float = 0.3111 |
| 18 | + omega_r: float = 0.0 |
| 19 | + omega_k: float = 0.0 |
| 20 | + cutoffs: tuple[str, ...] = ("electroweak", "1TeV", "planck") |
| 21 | + |
| 22 | + mechanism: Optional[str] = None |
| 23 | + grid: Optional[List[Dict[str, Any]]] = None |
| 24 | + z_values: tuple[float, ...] = (0.0, 1.0, 2.0) |
| 25 | + |
| 26 | + |
| 27 | +def _markdown_table(rows: List[Dict[str, Any]], headers: List[str]) -> str: |
| 28 | + lines = ["| " + " | ".join(headers) + " |", "| " + " | ".join(["---"] * len(headers)) + " |"] |
| 29 | + for r in rows: |
| 30 | + lines.append("| " + " | ".join(str(r.get(h, "")) for h in headers) + " |") |
| 31 | + return "\n".join(lines) |
| 32 | + |
| 33 | + |
| 34 | +def _stringify_params(rows: List[Dict[str, Any]]) -> List[Dict[str, Any]]: |
| 35 | + out: List[Dict[str, Any]] = [] |
| 36 | + for r in rows: |
| 37 | + r2 = dict(r) |
| 38 | + if isinstance(r2.get("params"), dict): |
| 39 | + r2["params"] = json.dumps(r2["params"], sort_keys=True) |
| 40 | + out.append(r2) |
| 41 | + return out |
| 42 | + |
| 43 | + |
| 44 | +def generate_report(cfg: ReportConfig) -> Dict[str, Any]: |
| 45 | + bg = CosmologyBackground( |
| 46 | + h0_km_s_mpc=cfg.h0_km_s_mpc, |
| 47 | + omega_lambda=cfg.omega_lambda, |
| 48 | + omega_m=cfg.omega_m, |
| 49 | + omega_r=cfg.omega_r, |
| 50 | + omega_k=cfg.omega_k, |
| 51 | + ) |
| 52 | + |
| 53 | + baselines: List[Dict[str, Any]] = [] |
| 54 | + for c in cfg.cutoffs: |
| 55 | + out: BaselineOutputs = compute_baseline( |
| 56 | + BaselineInputs( |
| 57 | + h0_km_s_mpc=cfg.h0_km_s_mpc, |
| 58 | + omega_lambda=cfg.omega_lambda, |
| 59 | + cutoff_name=c, |
| 60 | + ) |
| 61 | + ) |
| 62 | + baselines.append({"cutoff": c, **out.naive_qft}) |
| 63 | + |
| 64 | + report: Dict[str, Any] = { |
| 65 | + "inputs": asdict(cfg), |
| 66 | + "observed": { |
| 67 | + "h0_km_s_mpc": cfg.h0_km_s_mpc, |
| 68 | + "omega_lambda": cfg.omega_lambda, |
| 69 | + "rho_lambda0_j_m3": bg.rho_lambda0_j_m3, |
| 70 | + }, |
| 71 | + "naive_qft": baselines, |
| 72 | + } |
| 73 | + |
| 74 | + if cfg.mechanism: |
| 75 | + grid = cfg.grid if cfg.grid is not None else [{}] |
| 76 | + rows: List[SweepRow] = run_sweep(mechanism=cfg.mechanism, grid=grid, z_values=cfg.z_values, bg=bg) |
| 77 | + report["mechanism"] = { |
| 78 | + "name": cfg.mechanism, |
| 79 | + "z_values": list(cfg.z_values), |
| 80 | + "grid": grid, |
| 81 | + "rows": [asdict(r) for r in rows], |
| 82 | + } |
| 83 | + |
| 84 | + return report |
| 85 | + |
| 86 | + |
| 87 | +def write_report(report: Dict[str, Any], out_json: Path, out_md: Path) -> None: |
| 88 | + out_json.parent.mkdir(parents=True, exist_ok=True) |
| 89 | + out_md.parent.mkdir(parents=True, exist_ok=True) |
| 90 | + |
| 91 | + out_json.write_text(json.dumps(report, indent=2, sort_keys=True)) |
| 92 | + |
| 93 | + md_lines: List[str] = [] |
| 94 | + md_lines.append("# CCW report") |
| 95 | + md_lines.append("") |
| 96 | + md_lines.append("## Observed") |
| 97 | + md_lines.append(f"- H0: {report['observed']['h0_km_s_mpc']} km/s/Mpc") |
| 98 | + md_lines.append(f"- ΩΛ: {report['observed']['omega_lambda']}") |
| 99 | + md_lines.append(f"- ρΛ,0: {report['observed']['rho_lambda0_j_m3']:.3e} J/m³") |
| 100 | + md_lines.append("") |
| 101 | + |
| 102 | + md_lines.append("## Naive QFT cutoffs") |
| 103 | + table_rows = [ |
| 104 | + { |
| 105 | + "cutoff": r.get("cutoff"), |
| 106 | + "E_cut (J)": f"{r.get('e_cutoff_joule', 0.0):.3e}", |
| 107 | + "rho_naive (J/m³)": f"{r.get('rho_naive_j_m3', 0.0):.3e}", |
| 108 | + "ratio": f"{r.get('rho_naive_over_rho_lambda', 0.0):.3e}", |
| 109 | + } |
| 110 | + for r in report.get("naive_qft", []) |
| 111 | + ] |
| 112 | + md_lines.append(_markdown_table(table_rows, ["cutoff", "E_cut (J)", "rho_naive (J/m³)", "ratio"])) |
| 113 | + md_lines.append("") |
| 114 | + |
| 115 | + if "mechanism" in report: |
| 116 | + md_lines.append("## Mechanism") |
| 117 | + md_lines.append(f"- name: {report['mechanism']['name']}") |
| 118 | + md_lines.append("") |
| 119 | + md_lines.append( |
| 120 | + _markdown_table( |
| 121 | + _stringify_params(report["mechanism"]["rows"]), |
| 122 | + ["mechanism", "z", "rho_de_j_m3", "w_de", "params"], |
| 123 | + ) |
| 124 | + ) |
| 125 | + |
| 126 | + out_md.write_text("\n".join(md_lines) + "\n") |
0 commit comments