def test_magic_multi_bins():
    """Test multiple energy bins MAGIC case"""

    # create a basic meshtally
    coords = [[0, 1, 2], [-1, 3, 4], [10, 12]]
    flux_data = [[1.2, 3.3], [1.6, 1.7], [1.5, 1.4], [2.6, 1.0]]
    flux_error = [[0.11, 0.013], [0.14, 0.19], [0.02, 0.16], [0.04, 0.09]]
    tally = Mesh(structured=True, structured_coords=coords)
    
    tally.particle = "neutron"
    tally.e_bounds = [0.0, 0.5, 1.0]
    tally.n_flux = IMeshTag(2, float)
    tally.n_flux[:] = flux_data
    
    tally.n_rel_error = IMeshTag(2, float)
    tally.n_rel_error[:] = flux_error
    
    tolerance = 0.15
    null_value = 0.001
    
    magic(tally, "n_flux", "n_rel_error", tolerance=tolerance, null_value=null_value)
    
    expected_ww = [[0.2307692308, 0.5], 
                   [0.3076923077, 0.001], 
                   [0.2884615385, 0.001], 
                   [0.5, 0.15151515]]
    
    assert_array_almost_equal(tally.ww_x[:], expected_ww[:])
def test_magic_single_e():
    """Test a single energy group MAGIC case"""
    
    # create mesh
    coords = [[0, 1, 2], [-1, 3, 4], [10, 12]]
    flux_data = [1.2, 3.3, 1.6, 1.7]
    flux_error = [0.11, 0.013, 0.14, 0.19]
    tally = Mesh(structured=True, structured_coords=coords)
    
    tally.particle = "neutron"
    tally.e_bounds = [0.0, 1.0]
    tally.n_flux = IMeshTag(1, float)
    tally.n_flux[:] = flux_data
    
    tally.n_rel_error = IMeshTag(1, float)
    tally.n_rel_error[:] = flux_error
    
    tolerance = 0.15
    null_value = 0.001
    
    magic(tally, "n_flux", "n_rel_error", tolerance=tolerance, null_value=null_value)
    
    expected_ww = [0.181818182, 0.5, 0.2424242, 0.001]
    
    assert_array_almost_equal(tally.ww_x[:], expected_ww[:])
Beispiel #3
0
def test_bias_spatial():
    """This test tests a user-specified biasing scheme for which the only 1
    bias group is supplied for a source distribution containing two energy 
    groups. This bias group is applied to both energy groups. In this test,
    the user-supplied bias distribution that was choosen, correspondes to 
    uniform sampling, so that results can be checked against Case 1 in the
    theory manual.
    """
    seed(1953)
    m = Mesh(structured=True,
             structured_coords=[[0, 3, 3.5], [0, 1], [0, 1]],
             mats=None)
    m.src = IMeshTag(2, float)
    m.src[:] = [[2.0, 1.0], [9.0, 3.0]]
    m.bias = IMeshTag(1, float)
    m.bias[:] = [1, 1]
    e_bounds = np.array([0, 0.5, 1.0])
    m.mesh.save("sampling_mesh.h5m")
    sampler = Sampler("sampling_mesh.h5m", "src", e_bounds, "bias")

    num_samples = 10000
    score = 1.0 / num_samples
    num_divs = 2
    num_e = 2
    spatial_tally = np.zeros(shape=(num_divs, num_divs, num_divs))
    e_tally = np.zeros(shape=(4))  # number of phase space groups
    for i in range(num_samples):
        s = sampler.particle_birth(np.array([uniform(0, 1) for x in range(6)]))
        if s[0] < 3.0:
            assert_almost_equal(s[4], 0.7)  # hand calcs
        else:
            assert_almost_equal(s[4], 2.8)  # hand calcs

        spatial_tally[int(s[0] * num_divs / 3.5),
                      int(s[1] * num_divs / 1.0),
                      int(s[2] * num_divs / 1.0)] += score

        if s[0] < 3 and s[3] < 0.5:
            e_tally[0] += score
        elif s[0] < 3 and s[3] > 0.5:
            e_tally[1] += score
        if s[0] > 3 and s[3] < 0.5:
            e_tally[2] += score
        if s[0] > 3 and s[3] > 0.5:
            e_tally[3] += score

    for i in range(0, 3):
        for j in range(0, 2):
            halfspace_sum = np.sum(np.rollaxis(spatial_tally, i)[j, :, :])
            assert (abs(halfspace_sum - 0.5) / 0.5 < 0.1)

    expected_e_tally = [4. / 7, 2. / 7, 3. / 28, 1. / 28]  # hand calcs
    for i in range(4):
        assert (abs(e_tally[i] - expected_e_tally[i]) / expected_e_tally[i] <
                0.1)
Beispiel #4
0
def test_imeshtag_broadcasting():
    m = gen_mesh()
    #  tags of length 1
    m.horse = IMeshTag(1, float)
    m.horse[:] = 2.0
    assert_array_equal(m.horse[:], [2.0] * 4)

    #  tags of length > 1
    m.grape = IMeshTag(2, float)
    #  test broadcasing
    m.grape[[2, 0]] = [7.0, 8.0]
    assert_array_equal(m.grape[:],
                       [[7.0, 8.0], [0.0, 0.0], [7.0, 8.0], [0.0, 0.0]])
Beispiel #5
0
def test_analog_multiple_hex():
    """This test tests that particle are sampled uniformly from a uniform source
    defined on eight mesh volume elements in two energy groups. This is done
    using the exact same method ass test_analog_multiple_hex.
    """
    seed(1953)
    m = Mesh(structured=True,
             structured_coords=[[0, 0.5, 1], [0, 0.5, 1], [0, 0.5, 1]],
             mats=None)
    m.src = IMeshTag(2, float)
    m.src[:] = np.ones(shape=(8, 2))
    m.mesh.save("sampling_mesh.h5m")
    sampler = Sampler("sampling_mesh.h5m", "src", np.array([0, 0.5, 1]), False)

    num_samples = 5000
    score = 1.0 / num_samples
    num_divs = 2
    tally = np.zeros(shape=(num_divs, num_divs, num_divs, num_divs))
    for i in range(num_samples):
        s = sampler.particle_birth([uniform(0, 1) for x in range(6)])
        assert_equal(s[4], 1.0)
        tally[int(s[0] * num_divs),
              int(s[1] * num_divs),
              int(s[2] * num_divs),
              int(s[3] * num_divs)] += score

    for i in range(0, 4):
        for j in range(0, 2):
            halfspace_sum = np.sum(np.rollaxis(tally, i)[j, :, :, :])
            assert (abs(halfspace_sum - 0.5) / 0.5 < 0.1)
Beispiel #6
0
def test_analog_single_hex():
    """This test tests that particles of sampled evenly within the phase-space 
    of a single mesh volume element with one energy group in an analog sampling
    scheme. This done by dividing each dimension (x, y, z, E) in half, then 
    sampling particles and tallying on the basis of which of the 2^4 = 8 regions
    of phase space the particle is born into. 
    """
    seed(1953)
    m = Mesh(structured=True,
             structured_coords=[[0, 1], [0, 1], [0, 1]],
             mats=None)
    m.src = IMeshTag(1, float)
    m.src[0] = 1.0
    m.mesh.save("sampling_mesh.h5m")
    sampler = Sampler("sampling_mesh.h5m", "src", np.array([0, 1]), False)

    num_samples = 5000
    score = 1.0 / num_samples
    num_divs = 2
    tally = np.zeros(shape=(num_divs, num_divs, num_divs, num_divs))

    for i in range(num_samples):
        s = sampler.particle_birth(np.array([uniform(0, 1) for x in range(6)]))
        assert_equal(s[4], 1.0)  # analog: all weights must be one
        tally[int(s[0] * num_divs),
              int(s[1] * num_divs),
              int(s[2] * num_divs),
              int(s[3] * num_divs)] += score

    # Test that each half-space of phase space (e.g. x > 0.5) is sampled about
    # half the time.
    for i in range(0, 4):
        for j in range(0, 2):
            assert (abs(np.sum(np.rollaxis(tally, i)[j, :, :, :]) - 0.5) <
                    0.05)
Beispiel #7
0
def test_photon_sampling_setup_structured():

    phtn_src = os.path.join(thisdir, "files_test_r2s", "phtn_src")
    coords = [[0, 1, 2], [0, 1, 2], [0, 1]]
    m = Mesh(structured=True, structured_coords=coords)
    tags = {(10010000, "1 h"): "tag1", ("TOTAL", "shutdown"): "tag2"}
    photon_sampling_setup(m, phtn_src, tags)

    exp_tag1 = [[1.1, 2.2], [3.3, 4.4], [5.5, 6.6], [7.7, 8.8]]
    exp_tag2 = [[11.1, 12.2], [13.3, 14.4], [15.5, 16.6], [17.7, 18.8]]

    m.tag1 = IMeshTag(2, float)
    m.tag2 = IMeshTag(2, float)

    for i, mat, ve in m:
        assert_array_equal(m.tag1[i], exp_tag1[i])
        assert_array_equal(m.tag2[i], exp_tag2[i])
Beispiel #8
0
def test_lazytaginit():
    m = gen_mesh()
    m.cactus = IMeshTag(3, 'i')
    m.cactus[:] = np.array([42, 43, 44])
    assert_in('cactus', m.tags)
    assert_array_equal(m.cactus[0], [42, 43, 44])

    x = np.arange(len(m))[:, np.newaxis] * np.array([42, 43, 44])
    m.cactus[:] = x
    assert_array_equal(m.cactus[2], x[2])
Beispiel #9
0
def test_bias():
    """This test tests that a user-specified biasing scheme:
    1. Samples space uniformly according to the scheme.
    2. Adjusts weights accordingly. Sample calculations are provided in Case 2
       in the Theory Manual.
    """
    seed(1953)
    m = Mesh(structured=True,
             structured_coords=[[0, 3, 3.5], [0, 1], [0, 1]],
             mats=None)
    m.src = IMeshTag(2, float)
    m.src[:] = [[2.0, 1.0], [9.0, 3.0]]
    e_bounds = np.array([0, 0.5, 1.0])
    m.bias = IMeshTag(2, float)
    m.bias[:] = [[1.0, 2.0], [3.0, 3.0]]
    m.mesh.save("sampling_mesh.h5m")
    sampler = Sampler("sampling_mesh.h5m", "src", e_bounds, "bias")

    num_samples = 10000
    score = 1.0 / num_samples
    num_divs = 2
    tally = np.zeros(shape=(4))
    for i in range(num_samples):
        s = sampler.particle_birth(np.array([uniform(0, 1) for x in range(6)]))
        if s[0] < 3:
            if s[3] < 0.5:
                assert_almost_equal(s[4], 1.6)  # hand calcs
                tally[0] += score
            else:
                assert_almost_equal(s[4], 0.4)  # hand calcs
                tally[1] += score
        else:
            if s[3] < 0.5:
                assert_almost_equal(s[4], 2.4)  # hand calcs
                tally[2] += score
            else:
                assert_almost_equal(s[4], 0.8)  # hand calcs
                tally[3] += score

    expected_tally = [0.25, 0.5, 0.125, 0.125]  # hand calcs
    for a, b in zip(tally, expected_tally):
        assert (abs(a - b) / b < 0.25)
def test_cadis_multiple_e():
    """Test multiple energy group CADIS case"""

    adj_flux_tag = "adj_flux"
    q_tag = "q"
    ww_tag = "ww"
    q_bias_tag= "q_bias"

    #create meshes
    coords = [[0, 1, 2], [-1, 3, 4], [10, 12]]
    adj_flux_mesh = Mesh(structured=True, structured_coords=coords)
    q_mesh = Mesh(structured=True, structured_coords=coords)
    ww_mesh = Mesh(structured=True, structured_coords=coords)
    q_bias_mesh = Mesh(structured=True, structured_coords=coords)
    
    #add tags to input meshes
    adj_flux_mesh.adj_flux = IMeshTag(2, float)
    q_mesh.q = IMeshTag(2, float)

    #create data for input meshes
    adj_flux_data = [[1.1, 1.2], [1.3, 1.4], [0.0, 1.6], [1.7, 1.9]]
    q_data = [[2.9, 2.8], [2.6, 2.5], [2.4, 2.2], [2.9, 0.0]]

    #data data to mesh
    adj_flux_mesh.adj_flux[:] = adj_flux_data
    q_mesh.q[:] = q_data

    #run cadis
    cadis(adj_flux_mesh, adj_flux_tag, q_mesh, q_tag,
          ww_mesh, ww_tag, q_bias_mesh, q_bias_tag, beta=5)
    
    #expected results
    expected_q_bias = [[0.0306200806, 0.0322518718], [0.0324438472, 0.0335956998],
                       [0.0, 0.0337876752], [0.0473219428, 0.0]]
    expected_ww = [[0.3208302538, 0.2940943993], [0.2714717532, 0.2520809137],
                   [0.0, 0.2205707995], [0.2075960465, 0.1857438311]]

    ww_mesh.ww = IMeshTag(2, float)
    q_bias_mesh.q_bias = IMeshTag(2, float)
    
    assert_array_almost_equal(ww_mesh.ww[:], expected_ww[:])
    assert_array_almost_equal(q_bias_mesh.q_bias[:], expected_q_bias[:])
Beispiel #11
0
def test_uniform():
    """This test tests that the uniform biasing scheme:
    1. Samples space uniformly. This is checked using the same method
       described in test_analog_single_hex().
    2. Adjusts weights accordingly. Sample calculations are provided in Case 1
       in the Theory Manual.
    """
    seed(1953)
    m = Mesh(structured=True,
             structured_coords=[[0, 3, 3.5], [0, 1], [0, 1]],
             mats=None)
    m.src = IMeshTag(2, float)
    m.src[:] = [[2.0, 1.0], [9.0, 3.0]]
    e_bounds = np.array([0, 0.5, 1.0])
    m.mesh.save("sampling_mesh.h5m")
    sampler = Sampler("sampling_mesh.h5m", "src", e_bounds, True)

    num_samples = 10000
    score = 1.0 / num_samples
    num_divs = 2
    num_e = 2
    spatial_tally = np.zeros(shape=(num_divs, num_divs, num_divs))
    e_tally = np.zeros(shape=(4))  # number of phase space groups
    for i in range(num_samples):
        s = sampler.particle_birth(np.array([uniform(0, 1) for x in range(6)]))
        if s[0] < 3.0:
            assert_almost_equal(s[4], 0.7)  # hand calcs
        else:
            assert_almost_equal(s[4], 2.8)  # hand calcs

        spatial_tally[int(s[0] * num_divs / 3.5),
                      int(s[1] * num_divs / 1.0),
                      int(s[2] * num_divs / 1.0)] += score

        if s[0] < 3 and s[3] < 0.5:
            e_tally[0] += score
        elif s[0] < 3 and s[3] > 0.5:
            e_tally[1] += score
        if s[0] > 3 and s[3] < 0.5:
            e_tally[2] += score
        if s[0] > 3 and s[3] > 0.5:
            e_tally[3] += score

    for i in range(0, 3):
        for j in range(0, 2):
            halfspace_sum = np.sum(np.rollaxis(spatial_tally, i)[j, :, :])
            assert (abs(halfspace_sum - 0.5) / 0.5 < 0.1)

    expected_e_tally = [4. / 7, 2. / 7, 3. / 28, 1. / 28]  # hand calcs
    for i in range(4):
        assert(abs(e_tally[i] - expected_e_tally[i]) \
               /expected_e_tally[i] < 0.1)
def test_magic_below_tolerance():
    """Test MAGIC case when all flux errors are below the default tolerance"""
    
    # create mesh
    coords = [[0, 1, 2], [-1, 3, 4], [10, 12]]
    flux_data = [1.2, 3.3, 1.6, 1.7]
    flux_error = [0.11, 0.013, 0.14, 0.19]
    tally = Mesh(structured=True, structured_coords=coords)
    
    tally.particle = "neutron"
    tally.e_bounds = [0.0, 0.5, 1.0]
    tally.n_total_flux = IMeshTag(1, float)
    tally.n_total_flux[:] = flux_data
    
    tally.n_rel_error = IMeshTag(1, float)
    tally.n_rel_error[:] = flux_error
    
    magic(tally, "n_total_flux", "n_rel_error")
    
    expected_ww = [0.181818182, 0.5, 0.2424242, 0.2575757576]
    
    assert_array_almost_equal(tally.ww_x[:], expected_ww[:])
Beispiel #13
0
 def _create_mesh(self, part_data, error_data):
     """This will create the mesh object with the name of the tally
     specified by the user. One mesh object contains both the part_data and
     the error_data.
     """
     super(UsrbinTally, self).__init__(
         structured_coords=[self.x_bounds, self.y_bounds, self.z_bounds],
         structured=True,
         structured_ordering='zyx',
         mats=None)
     self.part_data_tag = IMeshTag(size=1,
                                   dtype=float,
                                   mesh=self,
                                   name="part_data_{0}".format(
                                       self.particle))
     self.error_data_tag = IMeshTag(size=1,
                                    dtype=float,
                                    mesh=self,
                                    name="error_data_{0}".format(
                                        self.particle))
     self.part_data_tag[:] = part_data
     self.error_data_tag[:] = error_data
def test_cadis_single_e():
    """Test single energy group cadis case"""
    adj_flux_tag = "adj_flux"
    q_tag = "q"
    ww_tag = "ww"
    q_bias_tag= "q_bias"

    #create meshes
    coords = [[0, 1, 2], [-1, 3, 4], [10, 12]]
    adj_flux_mesh = Mesh(structured=True, structured_coords=coords)
    q_mesh = Mesh(structured=True, structured_coords=coords)
    ww_mesh = Mesh(structured=True, structured_coords=coords)
    q_bias_mesh = Mesh(structured=True, structured_coords=coords)
    
    #add tags to meshes
    adj_flux_mesh.adj_flux = IMeshTag(1, float)
    q_mesh.q = IMeshTag(1, float)

    #create data for input meshes
    adj_flux_data = [1.1, 1.3, 1.5, 1.7]
    q_data = [2.9, 2.6, 2.4, 2.2]

    #data data to mesh
    adj_flux_mesh.adj_flux[:] = adj_flux_data
    q_mesh.q[:] = q_data

    #run CADIS
    cadis(adj_flux_mesh, adj_flux_tag, q_mesh, q_tag,
          ww_mesh, ww_tag, q_bias_mesh, q_bias_tag, beta=5)
    
    #checkout output meshes
    expected_ww = [0.3995338, 0.33806706, 0.29299145, 0.258521908]
    expected_q_bias = [0.04652859, 0.04929988, 0.052508751, 0.0545507858]
    ww_mesh.ww = IMeshTag(1, float)
    q_bias_mesh.q_bias = IMeshTag(1, float)
    
    assert_array_almost_equal(ww_mesh.ww[:], expected_ww[:])

    assert_array_almost_equal(q_bias_mesh.q_bias[:], expected_q_bias[:])
Beispiel #15
0
def test_imeshtag_fancy_indexing():
    m = gen_mesh()

    #  tags of length 1
    m.horse = IMeshTag(1, float)
    #  test fancy indexing
    m.horse[[2, 0]] = [3.0, 1.0]
    assert_array_equal(m.horse[:], [1.0, 0.0, 3.0, 0.0])
    m.horse[[2]] = [7.0]
    assert_array_equal(m.horse[:], [1.0, 0.0, 7.0, 0.0])

    #  tags of length > 1
    m.grape = IMeshTag(2, float)
    #  test fancy indexing
    m.grape[[2, 0]] = [[3.0, 4.0], [5.0, 6.0]]
    assert_array_equal(m.grape[:],
                       [[5.0, 6.0], [0.0, 0.0], [3.0, 4.0], [0.0, 0.0]])
    m.grape[[2]] = [[13.0, 14.0]]
    assert_array_equal(m.grape[:],
                       [[5.0, 6.0], [0.0, 0.0], [13.0, 14.0], [0.0, 0.0]])
    m.grape[1] = [23.0, 24.0]
    assert_array_equal(m.grape[:],
                       [[5.0, 6.0], [23.0, 24.0], [13.0, 14.0], [0.0, 0.0]])
Beispiel #16
0
def test_imeshtag():
    mats = {
        0: Material({
            'H1': 1.0,
            'K39': 1.0
        }, density=42.0),
        1: Material({
            'H1': 0.1,
            'O16': 1.0
        }, density=43.0),
        2: Material({'He4': 42.0}, density=44.0),
        3: Material({'Tm171': 171.0}, density=45.0),
    }
    m = gen_mesh(mats=mats)
    m.f = IMeshTag(mesh=m, name='f')
    m.f[:] = [1.0, 2.0, 3.0, 4.0]

    # Getting tags
    assert_equal(m.f[0], 1.0)
    assert_array_equal(m.f[::2], [1.0, 3.0])
    mask = np.array([True, False, True, True], dtype=bool)
    assert_array_equal(m.f[mask], [1.0, 3.0, 4.0])
    assert_array_equal(m.f[1, 0, 1, 3], [2.0, 1.0, 2.0, 4.0])

    # setting tags
    m.f[0] = 65.0
    assert_equal(m.f[0], 65.0)

    m.f[::2] = 18.0
    m.f[1::2] = [36.0, 54.0]
    assert_array_equal(m.f[:], np.array([18.0, 36.0, 18.0, 54.0]))

    mask = np.array([True, False, True, True], dtype=bool)
    m.f[mask] = 9.0
    mask = np.array([True, True, False, False], dtype=bool)
    m.f[mask] = (19.0, 29.0)
    assert_array_equal(m.f[:], np.array([19.0, 29.0, 9.0, 9.0]))

    m.f[[2]] = 28.0
    m.f[3, 1] = 6.0, 4128.0
    assert_array_equal(m.f[1:], np.array([4128.0, 28.0, 6.0]))

    # deleting tag
    del m.f[:]
Beispiel #17
0
def test_analog_single_tet():
    """This test tests uniform sampling within a single tetrahedron. This is
    done by dividing the tetrahedron in 4 smaller tetrahedrons and ensuring
    that each sub-tet is sampled equally.
    """
    seed(1953)
    mesh = iMesh.Mesh()
    v1 = [0, 0, 0]
    v2 = [1, 0, 0]
    v3 = [0, 1, 0]
    v4 = [0, 0, 1]
    verts = mesh.createVtx([v1, v2, v3, v4])
    mesh.createEnt(iMesh.Topology.tetrahedron, verts)
    m = Mesh(structured=False, mesh=mesh)
    m.src = IMeshTag(1, float)
    m.src[:] = np.array([1])
    m.mesh.save("tet.h5m")
    center = m.ve_center(list(m.iter_ve())[0])

    subtets = [[center, v1, v2, v3], [center, v1, v2, v4],
               [center, v1, v3, v4], [center, v2, v3, v4]]

    sampler = Sampler("tet.h5m", "src", np.array([0, 1]), False)
    num_samples = 5000
    score = 1.0 / num_samples
    tally = np.zeros(shape=(4))
    for i in range(num_samples):
        s = sampler.particle_birth([uniform(0, 1) for x in range(6)])
        assert_equal(s[4], 1.0)
        for i, tet in enumerate(subtets):
            if point_in_tet(tet, [s[0], s[1], s[2]]):
                tally[i] += score
                break

    for t in tally:
        assert (abs(t - 0.25) / 0.25 < 0.2)
Beispiel #18
0
def test_imeshtag_expand():
    m = Mesh(structured=True, structured_coords=[[-1, 0, 1], [0, 1], [0, 1]])
    m.clam = IMeshTag(2, float)
    m.clam[:] = [[1.1, 2.2], [3.3, 4.4]]
    m.clam.expand()
    m.clam_000 = IMeshTag(1, float)
    assert_array_equal(m.clam_000[:], [1.1, 3.3])
    m.clam_001 = IMeshTag(1, float)
    assert_array_equal(m.clam_001[:], [2.2, 4.4])

    # corner case: mesh with a single volume element
    m = Mesh(structured=True, structured_coords=[[0, 1], [0, 1], [0, 1]])
    m.clam = IMeshTag(2, float)
    m.clam[:] = [[1.1, 2.2]]
    m.clam.expand()
    m.clam_000 = IMeshTag(1, float)
    assert_array_equal(m.clam_000[:], 1.1)
    m.clam_001 = IMeshTag(1, float)
    assert_array_equal(m.clam_001[:], 2.2)
Beispiel #19
0
def test_issue360():
    a = Mesh(structured=True, structured_coords=[[0, 1, 2], [0, 1], [0, 1]])
    a.cat = IMeshTag(3, float)
    a.cat[:] = [[0.11, 0.22, 0.33], [0.44, 0.55, 0.66]]
    a.cat[:] = np.array([[0.11, 0.22, 0.33], [0.44, 0.55, 0.66]])