|
1 | 1 | import pytest |
2 | 2 | import types |
| 3 | +from pathlib import Path |
3 | 4 | from unittest.mock import patch |
4 | 5 | from openmc_fusion_benchmarks.uq import ( |
5 | 6 | get_nuclide_zaid, |
6 | 7 | zaid_to_zam, |
7 | 8 | get_nuclide_gnds, |
8 | 9 | get_reaction_mt |
9 | 10 | ) |
| 11 | +from openmc_fusion_benchmarks.uq.uq_utils import ( |
| 12 | + ace_to_hdf5, |
| 13 | + get_ace_files, |
| 14 | + perturb_to_hdf5, |
| 15 | + perturb_xs_xml, |
| 16 | + remove_ace_files, |
| 17 | +) |
10 | 18 |
|
11 | 19 |
|
12 | 20 | def test_zaid_to_zam_len4(): |
@@ -151,3 +159,158 @@ def test_get_reaction_mt_unknown_reaction(): |
151 | 159 | def test_unsupported_type(): |
152 | 160 | with pytest.raises(TypeError, match="Unsupported nuclide type"): |
153 | 161 | get_nuclide_gnds((1, 1)) |
| 162 | + |
| 163 | + |
| 164 | +def test_remove_ace_files_removes_matching(tmp_path): |
| 165 | + lib_name = "mylib" |
| 166 | + extensions = ["03c", "03c.xsd", lib_name] |
| 167 | + for ext in extensions: |
| 168 | + (tmp_path / f"test.{ext}").write_text("data") |
| 169 | + |
| 170 | + remove_ace_files(str(tmp_path), lib_name) |
| 171 | + for ext in extensions: |
| 172 | + assert not (tmp_path / f"test.{ext}").exists() |
| 173 | + |
| 174 | + |
| 175 | +def test_get_ace_files_calls_sandy(monkeypatch): |
| 176 | + class FakeTape: |
| 177 | + def get_perturbations(self, *args, **kwargs): |
| 178 | + return ["p1", "p2"] |
| 179 | + |
| 180 | + def apply_perturbations(self, *args, **kwargs): |
| 181 | + return ["ace1", "ace2"] |
| 182 | + |
| 183 | + def _fake_get_endf6_file(*args, **kwargs): |
| 184 | + return FakeTape() |
| 185 | + |
| 186 | + fake_sandy = types.SimpleNamespace(get_endf6_file=_fake_get_endf6_file) |
| 187 | + monkeypatch.setattr("openmc_fusion_benchmarks.uq.uq_utils.sandy", fake_sandy, raising=False) |
| 188 | + |
| 189 | + fake_openmc = _fake_openmc_with_data(REACTION_MT={"(n,gamma)": 102}) |
| 190 | + monkeypatch.setattr("openmc_fusion_benchmarks.uq.uq_utils.openmc", fake_openmc, raising=False) |
| 191 | + |
| 192 | + ace_files = get_ace_files(2, "endfb_80", "H1", "(n,gamma)", 1, 0.001) |
| 193 | + assert ace_files == ["ace1", "ace2"] |
| 194 | + |
| 195 | + |
| 196 | +def test_ace_to_hdf5_exports_and_cleans(tmp_path, monkeypatch): |
| 197 | + monkeypatch.chdir(tmp_path) |
| 198 | + |
| 199 | + class FakeIncident: |
| 200 | + def export_to_hdf5(self, path): |
| 201 | + Path(path).write_text("h5") |
| 202 | + |
| 203 | + class FakeOpenmcData: |
| 204 | + class IncidentNeutron: |
| 205 | + @staticmethod |
| 206 | + def from_ace(_path): |
| 207 | + return FakeIncident() |
| 208 | + |
| 209 | + @staticmethod |
| 210 | + def gnds_name(z, a, m): |
| 211 | + return f"H{a}" if z == 1 else f"U{a}" |
| 212 | + |
| 213 | + @staticmethod |
| 214 | + def zam(nuclide): |
| 215 | + return fake_zam(nuclide) |
| 216 | + |
| 217 | + fake_openmc = types.SimpleNamespace(data=FakeOpenmcData()) |
| 218 | + monkeypatch.setattr("openmc_fusion_benchmarks.uq.uq_utils.openmc", fake_openmc, raising=False) |
| 219 | + |
| 220 | + removed = {"called": False} |
| 221 | + |
| 222 | + def _fake_remove(_directory, _lib): |
| 223 | + removed["called"] = True |
| 224 | + |
| 225 | + monkeypatch.setattr("openmc_fusion_benchmarks.uq.uq_utils.remove_ace_files", _fake_remove) |
| 226 | + |
| 227 | + ace_to_hdf5(2, "endfb_80", "H1", remove_ace=True) |
| 228 | + assert removed["called"] is True |
| 229 | + assert (tmp_path / "H1_endfb_80" / "H1_0_endfb_80.h5").exists() |
| 230 | + |
| 231 | + |
| 232 | +def test_perturb_to_hdf5_calls_helpers(monkeypatch): |
| 233 | + calls = {"ace": False, "h5": False} |
| 234 | + |
| 235 | + def _fake_get_ace(*args, **kwargs): |
| 236 | + calls["ace"] = True |
| 237 | + |
| 238 | + def _fake_ace_to_hdf5(*args, **kwargs): |
| 239 | + calls["h5"] = True |
| 240 | + |
| 241 | + monkeypatch.setattr("openmc_fusion_benchmarks.uq.uq_utils.get_ace_files", _fake_get_ace) |
| 242 | + monkeypatch.setattr("openmc_fusion_benchmarks.uq.uq_utils.ace_to_hdf5", _fake_ace_to_hdf5) |
| 243 | + |
| 244 | + perturb_to_hdf5(1, "endfb_80", "H1", "(n,gamma)") |
| 245 | + assert calls["ace"] is True |
| 246 | + assert calls["h5"] is True |
| 247 | + |
| 248 | + |
| 249 | +def test_perturb_xs_xml_updates_library(monkeypatch, tmp_path): |
| 250 | + xs_file = tmp_path / "cross_sections.xml" |
| 251 | + xs_h5 = tmp_path / "new_xs.h5" |
| 252 | + xs_h5.write_text("data") |
| 253 | + |
| 254 | + class FakeLibraries: |
| 255 | + def __init__(self): |
| 256 | + self.data = {"U235": {"path": "old"}} |
| 257 | + |
| 258 | + def get_by_material(self, nuclide): |
| 259 | + return self.data[nuclide] |
| 260 | + |
| 261 | + class FakeDataLibrary: |
| 262 | + def __init__(self): |
| 263 | + self.libraries = FakeLibraries() |
| 264 | + |
| 265 | + def export_to_xml(self, path): |
| 266 | + Path(path).write_text("xml") |
| 267 | + |
| 268 | + def append(self, entry): |
| 269 | + self.libraries.data[entry["materials"][0]] = {"path": entry["path"]} |
| 270 | + |
| 271 | + @classmethod |
| 272 | + def from_xml(cls, _path): |
| 273 | + return cls() |
| 274 | + |
| 275 | + fake_openmc = types.SimpleNamespace( |
| 276 | + config={"cross_sections": str(xs_file)}, |
| 277 | + data=types.SimpleNamespace(DataLibrary=FakeDataLibrary), |
| 278 | + ) |
| 279 | + monkeypatch.setattr("openmc_fusion_benchmarks.uq.uq_utils.openmc", fake_openmc, raising=False) |
| 280 | + |
| 281 | + perturb_xs_xml(str(xs_file), str(xs_h5), "U235") |
| 282 | + assert xs_file.exists() |
| 283 | + |
| 284 | + |
| 285 | +def test_perturb_xs_xml_append_on_typeerror(monkeypatch, tmp_path): |
| 286 | + xs_file = tmp_path / "cross_sections.xml" |
| 287 | + xs_h5 = tmp_path / "new_xs.h5" |
| 288 | + xs_h5.write_text("data") |
| 289 | + |
| 290 | + class FakeLibraries: |
| 291 | + def get_by_material(self, _nuclide): |
| 292 | + raise TypeError("missing") |
| 293 | + |
| 294 | + class FakeDataLibrary: |
| 295 | + def __init__(self): |
| 296 | + self.libraries = FakeLibraries() |
| 297 | + self.appended = False |
| 298 | + |
| 299 | + def export_to_xml(self, path): |
| 300 | + Path(path).write_text("xml") |
| 301 | + |
| 302 | + def append(self, _entry): |
| 303 | + self.appended = True |
| 304 | + |
| 305 | + @classmethod |
| 306 | + def from_xml(cls, _path): |
| 307 | + return cls() |
| 308 | + |
| 309 | + fake_openmc = types.SimpleNamespace( |
| 310 | + config={"cross_sections": str(xs_file)}, |
| 311 | + data=types.SimpleNamespace(DataLibrary=FakeDataLibrary), |
| 312 | + ) |
| 313 | + monkeypatch.setattr("openmc_fusion_benchmarks.uq.uq_utils.openmc", fake_openmc, raising=False) |
| 314 | + |
| 315 | + perturb_xs_xml(str(xs_file), str(xs_h5), "U235") |
| 316 | + assert xs_file.exists() |
0 commit comments