def test_get_result_error_from_openmc_sp(): if not HAVE_PYMOAB or sys.version_info[0] == 2: raise SkipTest try: import openmc except: raise SkipTest filename = os.path.join(os.getcwd(), "files_test_openmc", "statepoint.10.ebin2.ves6.h5") tally_num = 1 dims = np.array([3, 2, 1]) m = MeshTally() structured_coords = openmc_utils.get_structured_coords_from_openmc_sp( filename, tally_id=tally_num) super(MeshTally, m).__init__(structured_coords=structured_coords, structured=True, mats=()) num_ves = len(m) ve_vol = m.structured_hex_volume(0, 0, 0) ( result, rel_err, res_tot, rel_err_tot, ) = openmc_utils.get_result_error_from_openmc_sp(filename, m) # read expected data from statepoint.10.ebin2.ves6.h5 sp = openmc.StatePoint(filename) tally = sp.get_tally(id=1) flux = tally.get_slice(scores=["flux"]) num_e_groups = len(flux.mean.flatten()) // num_ves exp_result = np.divide(flux.mean.flatten(), ve_vol) exp_result = np.reshape(exp_result, newshape=(num_e_groups, num_ves)) exp_result = exp_result.transpose() exp_res_tot = np.sum(exp_result, axis=1) exp_rel_err = flux.std_dev / flux.mean exp_rel_err = np.reshape(exp_rel_err, newshape=(num_e_groups, num_ves)) exp_rel_err = exp_rel_err.transpose() exp_rel_err_tot = np.zeros_like(exp_res_tot) std_dev = np.reshape(flux.std_dev.flatten(), newshape=(num_e_groups, num_ves)) std_dev = std_dev.transpose() var_tot = np.sum(np.square(std_dev), axis=1) exp_rel_err_tot = np.sqrt(var_tot) / (exp_res_tot * ve_vol) # changes the order of exp results exp_result = openmc_utils.result_changes_order(exp_result, dims) exp_rel_err = openmc_utils.result_changes_order(exp_rel_err, dims) exp_res_tot = openmc_utils.result_changes_order(exp_res_tot, dims) exp_rel_err_tot = openmc_utils.result_changes_order(exp_rel_err_tot, dims) # compare the data and expected answer assert_array_almost_equal(result, exp_result) assert_array_almost_equal(rel_err, exp_rel_err) assert_array_almost_equal(res_tot, exp_res_tot) assert_array_almost_equal(rel_err_tot, exp_rel_err_tot)
def test_create_meshtally(): if not HAVE_PYMOAB or sys.version_info[0] == 2: raise SkipTest try: import openmc except: raise SkipTest # mesh read from openmc state point file # Parameters of the tally and mesh # mesh = openmc_utils.Mesh(mesh_id=1, name="n_flux") # mesh.dimension= [3, 2, 1] # mesh.lower_left = (-40.0, -12.5, -2.5) # mesh.upper_right = (40.0, 12.5, 2.5) # energy_bins = np.array([0.0, 1.0, 20.0]) * 1e6 filename = os.path.join(os.getcwd(), "files_test_openmc", "statepoint.10.ebin2.ves6.h5") tally_num = 1 dims = np.array([3, 2, 1]) tag_names = ("n_flux", "n_flux_err", "n_flux_total", "n_flux_total_err") mesh = openmc_utils.create_meshtally(filename, tally_num, particle='neutron', tag_names=tag_names) num_ves = len(mesh) # check mesh attributes assert_equal(num_ves, 6) assert (mesh.structured) # structured_coords assert_array_almost_equal(mesh.structured_coords[0], [(-40.0 + x * 80.0 / 3) for x in range(0, 4)]) assert_array_almost_equal(mesh.structured_coords[1], [(-12.5 + x * 25.0 / 2) for x in range(0, 3)]) assert_array_almost_equal(mesh.structured_coords[2], [(-2.5 + x * 5.0 / 1) for x in range(0, 2)]) ve_vol = (80.0 / 3) * (25.0 / 2) * (5.0 / 1) # read expected data from statepoint.10.ebin2.ves6.h5 sp = openmc.StatePoint(filename) tally = sp.get_tally(id=1) flux = tally.get_slice(scores=['flux']) num_e_groups = len(flux.mean.flatten()) // num_ves exp_result = np.divide(flux.mean.flatten(), ve_vol) exp_result = np.reshape(exp_result, newshape=(num_e_groups, num_ves)) exp_result = exp_result.transpose() exp_rel_err = flux.std_dev / flux.mean exp_rel_err = np.reshape(exp_rel_err, newshape=(num_e_groups, num_ves)) exp_rel_err = exp_rel_err.transpose() # changes the order of exp results exp_result = openmc_utils.result_changes_order(exp_result, dims) exp_rel_err = openmc_utils.result_changes_order(exp_rel_err, dims) # compare assert_array_almost_equal(mesh.n_flux[:], exp_result) assert_array_almost_equal(mesh.n_flux_err[:], exp_rel_err)
def test_flux_changes_order(): # test mesh dimensions: (3, 2, 1), num_e_groups: 2 dims = np.array([3, 2, 1]) result = np.array([['x0y0z0e0', 'x0y0z0e1'], ['x1y0z0e0', 'x1y0z0e1'], ['x2y0z0e0', 'x2y0z0e1'], ['x0y1z0e0', 'x0y1z0e1'], ['x1y1z0e0', 'x1y1z0e1'], ['x2y1z0e0', 'x2y1z0e1']]) exp_result = np.array([['x0y0z0e0', 'x0y0z0e1'], ['x0y1z0e0', 'x0y1z0e1'], ['x1y0z0e0', 'x1y0z0e1'], ['x1y1z0e0', 'x1y1z0e1'], ['x2y0z0e0', 'x2y0z0e1'], ['x2y1z0e0', 'x2y1z0e1']]) result_ = openmc_utils.result_changes_order(result, dims) assert_array_equal(result_, exp_result)