test/python/test_nonlinear_comparison.py

Snapshot from this documentation build. SHA-256: dd8bf58dbb98bb4d62caf71214d12b23ededb03ec799b281195a326a58311cf5

    1 from argparse import Namespace
    2 from pathlib import Path
    3 import tempfile
    4 import tomllib
    5 
    6 import numpy as np
    7 import pytest
    8 
    9 from tools.reference.nonlinear_comparison import case_settings, mode_at, physical_mode, write_config
   10 
   11 
   12 def test_epoch_interpolation_preserves_complex_phase():
   13     rows = [dict(field="gas_delta",z=z,real=real,imag=imag)
   14             for z,real,imag in [(3200,2,4),(3000,4,8)]]
   15     middle = np.sqrt(3201*3001)-1
   16     assert mode_at(rows,"gas_delta",middle) == pytest.approx(3+6j)
   17     with pytest.raises(ValueError,match="missing"):
   18         mode_at(rows,"B_y",middle)
   19     with pytest.raises(ValueError,match="span"):
   20         mode_at(rows,"gas_delta",4000)
   21 
   22 
   23 def test_velocity_and_particle_clocks_are_distinct():
   24     handoff = dict(guide_field_code=2,velocity_unit_cm_s_at_handoff=800000,zhandoff=3200)
   25     assert physical_mode(0.02j,"B_y",3200,handoff) == 0.01j
   26     assert physical_mode(1j,"gas_v_x",1600,handoff) == pytest.approx(800000j*np.sqrt(1601/3201))
   27     assert physical_mode(123j,"dm_v_x",1600,handoff) == 123j
   28 
   29 
   30 def test_native_case_preserves_handoff_units_and_full_sources():
   31     handoff=dict(velocity_unit_cm_s_at_handoff=12345,
   32                  background=dict(xHII=0.9,pressure_code_mean=2.5))
   33     args=Namespace(hubble=0.674,resolution=32,backend="metal",cfl=0.2,zstart=3200,
   34         zend=3100,box_ckpc_h=1,field_pg=300,theta=np.pi/4,epsilon=0.025,
   35         outputs=9,response_table=None)
   36     settings=case_settings(handoff,Path("input-state"),args)
   37     with tempfile.TemporaryDirectory() as tmp:
   38         p=Path(tmp)/"case.toml"
   39         write_config(p,settings)
   40         values=tomllib.loads(p.read_text())
   41     assert values["chemistry"]["velocity_unit_cms"] == 12345
   42     assert values["chemistry"]["ionized_fast_zmin"] == 0
   43     assert values["physics"]["gravity"] and values["physics"]["gas_gravity"]
   44     assert values["physics"]["photon_response"] == "moments_exponential"
   45     assert values["environment"]["MHD_CHEM_FALPHA"] == 0
   46     assert values["environment"]["MHD_RADIATION_FREE_STREAM_TOL"] == 0
   47 
   48 
   49 def _args(**kw):
   50     base = dict(hubble=0.674,resolution=32,backend="cpu",cfl=0.2,zstart=3200,zend=3100,
   51         box_ckpc_h=1,field_pg=300,theta=np.pi/4,epsilon=0.025,outputs=9,response_table=None)
   52     return Namespace(**{**base,**kw})
   53 
   54 
   55 def test_mhd_solver_option_sets_toml_and_drops_glm_projection():
   56     handoff=dict(velocity_unit_cm_s_at_handoff=1,background=dict(xHII=0.9,pressure_code_mean=2.5))
   57     glm=case_settings(handoff,Path("s"),_args())
   58     assert "mhd_solver" not in glm["run"] and glm["physics"]["glm_project_every"] == 8
   59     ct=case_settings(handoff,Path("s"),_args(mhd_solver="ct"))
   60     assert ct["run"]["mhd_solver"] == "ct" and "glm_project_every" not in ct["physics"]
   61     ct["run"].pop("mhd_solver"); ct["physics"]["glm_project_every"] = 8
   62     assert ct == glm