<commit_before>import os
import h5py

import numpy as np
import pytest
import openmc

from tests.testing_harness import PyAPITestHarness


@pytest.fixture
def model(request):
    marker = request.node.get_closest_marker("surf_src_op")
    surf_src_op = marker.args[0]

    openmc_model = openmc.model.Model()

    # Materials
    # None

    # Geometry
    # Concentric void spheres
    # - Innermost sphere to bank surface sources
    # - Second shell to tally cell flux
    # - Outermost sphere as vacuum boundary
    sph_1 = openmc.Sphere(r=1.0, surface_id=1)  # Surface to bank/write sources.
    sph_2 = openmc.Sphere(r=2.0, surface_id=2)
    sph_3 = openmc.Sphere(r=2.5, surface_id=3)
    sph_4 = openmc.Sphere(r=4.0, surface_id=4, boundary_type='vacuum')
    cell_1 = openmc.Cell(1, region=-sph_1)
    cell_2 = openmc.Cell(2, region=+sph_1&-sph_2)
    cell_3 = openmc.Cell(3, region=+sph_2&-sph_3)  # Cell to tally flux.
    cell_4 = openmc.Cell(4, region=+sph_3&-sph_4)
    root = openmc.Universe(universe_id=1,
                           cells=[cell_1, cell_2, cell_3, cell_4])
    openmc_model.geometry = openmc.Geometry(root)

    # Settings
    openmc_model.settings.run_mode = 'fixed source'
    openmc_model.settings.particles = 1000
    openmc_model.settings.batches = 10
    openmc_model.settings.seed = 1

    if surf_src_op == 'write':
        point = openmc.stats.Point((0, 0, 0))
        pt_src = openmc.Source(space=point)
        openmc_model.settings.source = pt_src

        openmc_model.settings.surf_src_write = {'surf_ids': [1],
                                                'max_surf_banks': 1000}
    elif surf_src_op == 'read':
        openmc_model.settings.surf_src_read = {'path': 'surface_source_true.h5'}

    # Tallies
    tal = openmc.Tally(1)
    cell_filter = openmc.CellFilter(cell_3, 1)
    tal.filters = [cell_filter]
    tal.scores = ['flux']
    openmc_model.tallies.append(tal)

    return openmc_model


class SurfaceSourceTestHarness(PyAPITestHarness):
    def _test_output_created(self):
        """Make sure surface_source.h5 has also been created."""
        super()._test_output_created()
        # Check if 'surface_source.h5' has been created.
        if self._model.settings.surf_src_write:
            assert os.path.exists('surface_source.h5'), \
                'Surface source file does not exist.'

    def _compare_output(self):
        """Make sure the current surface_source.h5 agree with the reference."""
        if self._model.settings.surf_src_write:
            with h5py.File("surface_source_true.h5", 'r') as f:
                src_true = f['source_bank'][()]
            with h5py.File("surface_source.h5", 'r') as f:
                src_test = f['source_bank'][()]
            for (true_p, test_p) in zip(np.sort(src_true), np.sort(src_test)):
                np.testing.assert_array_equal(true_p, test_p)

    def execute_test(self):
        """Build input XMLs, run OpenMC, check output and results."""
        try:
            self._build_inputs()
            inputs = self._get_inputs()
            self._write_inputs(inputs)
            self._compare_inputs()
            self._run_openmc()<commit_msg>Convert dtye from mixed to a float for comparison assertion<commit_after>import os
import h5py

import numpy as np
import pytest
import openmc

from tests.testing_harness import PyAPITestHarness


@pytest.fixture
def model(request):
    marker = request.node.get_closest_marker("surf_src_op")
    surf_src_op = marker.args[0]

    openmc_model = openmc.model.Model()

    # Materials
    # None

    # Geometry
    # Concentric void spheres
    # - Innermost sphere to bank surface sources
    # - Second shell to tally cell flux
    # - Outermost sphere as vacuum boundary
    sph_1 = openmc.Sphere(r=1.0, surface_id=1)  # Surface to bank/write sources.
    sph_2 = openmc.Sphere(r=2.0, surface_id=2)
    sph_3 = openmc.Sphere(r=2.5, surface_id=3)
    sph_4 = openmc.Sphere(r=4.0, surface_id=4, boundary_type='vacuum')
    cell_1 = openmc.Cell(1, region=-sph_1)
    cell_2 = openmc.Cell(2, region=+sph_1&-sph_2)
    cell_3 = openmc.Cell(3, region=+sph_2&-sph_3)  # Cell to tally flux.
    cell_4 = openmc.Cell(4, region=+sph_3&-sph_4)
    root = openmc.Universe(universe_id=1,
                           cells=[cell_1, cell_2, cell_3, cell_4])
    openmc_model.geometry = openmc.Geometry(root)

    # Settings
    openmc_model.settings.run_mode = 'fixed source'
    openmc_model.settings.particles = 1000
    openmc_model.settings.batches = 10
    openmc_model.settings.seed = 1

    if surf_src_op == 'write':
        point = openmc.stats.Point((0, 0, 0))
        pt_src = openmc.Source(space=point)
        openmc_model.settings.source = pt_src

        openmc_model.settings.surf_src_write = {'surf_ids': [1],
                                                'max_surf_banks': 1000}
    elif surf_src_op == 'read':
        openmc_model.settings.surf_src_read = {'path': 'surface_source_true.h5'}

    # Tallies
    tal = openmc.Tally(1)
    cell_filter = openmc.CellFilter(cell_3, 1)
    tal.filters = [cell_filter]
    tal.scores = ['flux']
    openmc_model.tallies.append(tal)

    return openmc_model


class SurfaceSourceTestHarness(PyAPITestHarness):
    def _test_output_created(self):
        """Make sure surface_source.h5 has also been created."""
        super()._test_output_created()
        # Check if 'surface_source.h5' has been created.
        if self._model.settings.surf_src_write:
            assert os.path.exists('surface_source.h5'), \
                'Surface source file does not exist.'

    def _compare_output(self):
        """Make sure the current surface_source.h5 agree with the reference."""
        if self._model.settings.surf_src_write:
            with h5py.File("surface_source_true.h5", 'r') as f:
                src_true = f['source_bank'][()]
                # Convert dtye from mixed to a float for comparison assertion
                src_true.dtype = 'float64'
            with h5py.File("surface_source.h5", 'r') as f:
                src_test = f['source_bank'][()]
                # Convert dtye from mixed to a float for comparison assertion
                src_test.dtype = 'float64'
            np.testing.assert_allclose(np.sort(src_true), np.sort(src_test),
                                       atol=1e-07)

    def execute_test(self):
        """Build input XMLs, run OpenMC, check output and results."""
        try:
            self._build_inputs()
            inputs = self._get_inputs()
            self._write_inputs(inputs)
            self._compare_inputs()
            self._run_openmc()
{'fraction_non_alphanumeric': 0.10884969093924318, 'fraction_numerical': 0.02065430423639379, 'mean_word_length': 2.576280323450135, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5386890', 'n_tokens_mistral': 2281, 'n_tokens_neox': 2192, 'n_words': 457}