1import hashlib
2
3import openmc
4import openmc.mgxs
5from openmc.examples import pwr_pin_cell
6
7from tests.testing_harness import PyAPITestHarness
8
9
10class MGXSTestHarness(PyAPITestHarness):
11    def __init__(self, *args, **kwargs):
12        super().__init__(*args, **kwargs)
13
14        # Initialize a two-group structure
15        energy_groups = openmc.mgxs.EnergyGroups(group_edges=[0, 0.625, 20.e6])
16
17        # Initialize MGXS Library for a few cross section types
18        self.mgxs_lib = openmc.mgxs.Library(self._model.geometry)
19        self.mgxs_lib.by_nuclide = False
20
21        # Test all MGXS types
22        self.mgxs_lib.mgxs_types = openmc.mgxs.MGXS_TYPES + \
23                                   openmc.mgxs.MDGXS_TYPES
24        self.mgxs_lib.energy_groups = energy_groups
25        self.mgxs_lib.num_delayed_groups = 6
26        self.mgxs_lib.legendre_order = 3
27        self.mgxs_lib.domain_type = 'mesh'
28
29        # Instantiate a tally mesh
30        mesh = openmc.RegularMesh(mesh_id=1)
31        mesh.dimension = [2, 2]
32        mesh.lower_left = [-100., -100.]
33        mesh.width = [100., 100.]
34
35        self.mgxs_lib.domains = [mesh]
36        self.mgxs_lib.build_library()
37
38        # Add tallies
39        self.mgxs_lib.add_to_tallies_file(self._model.tallies, merge=False)
40
41    def _get_results(self, hash_output=False):
42        """Digest info in the statepoint and return as a string."""
43
44        # Read the statepoint file.
45        sp = openmc.StatePoint(self._sp_name)
46
47        # Load the MGXS library from the statepoint
48        self.mgxs_lib.load_from_statepoint(sp)
49
50        # Build a condensed 1-group MGXS Library
51        one_group = openmc.mgxs.EnergyGroups([0., 20.e6])
52        condense_lib = self.mgxs_lib.get_condensed_library(one_group)
53
54        # Build a string from Pandas Dataframe for each 1-group MGXS
55        outstr = ''
56        for domain in condense_lib.domains:
57            for mgxs_type in condense_lib.mgxs_types:
58                mgxs = condense_lib.get_mgxs(domain, mgxs_type)
59                df = mgxs.get_pandas_dataframe()
60                outstr += df.to_string() + '\n'
61
62        # Hash the results if necessary
63        if hash_output:
64            sha512 = hashlib.sha512()
65            sha512.update(outstr.encode('utf-8'))
66            outstr = sha512.hexdigest()
67
68        return outstr
69
70
71def test_mgxs_library_condense():
72    # Use the pincell model
73    model = pwr_pin_cell()
74    harness = MGXSTestHarness('statepoint.10.h5', model)
75    harness.main()
76