def test_irradiation_setup_structured(): meshtal = os.path.join(thisdir, "files_test_r2s", "meshtal_2x2x1") tally_num = 4 cell_mats = { 2: Material({2004: 1.0}, density=1.0), 3: Material({ 3007: 0.4, 3006: 0.6 }, density=2.0) } alara_params = "Bogus line for testing" geom = os.path.join(thisdir, "unitbox.h5m") num_rays = 9 grid = True flux_tag = "n_flux" fluxin = os.path.join(os.getcwd(), "alara_fluxin") reverse = True alara_inp = os.path.join(os.getcwd(), "alara_inp") alara_matlib = os.path.join(os.getcwd(), "alara_matlib") output_mesh = os.path.join(os.getcwd(), "r2s_step1.h5m") irradiation_setup(meshtal, cell_mats, alara_params, tally_num, geom, num_rays, grid, flux_tag, fluxin, reverse, alara_inp, alara_matlib, output_mesh) # expected output files exp_alara_inp = os.path.join(thisdir, "files_test_r2s", "exp_alara_inp") exp_alara_matlib = os.path.join(thisdir, "files_test_r2s", "exp_alara_matlib") exp_fluxin = os.path.join(thisdir, "files_test_r2s", "exp_fluxin") # test alara input file with open(alara_inp, 'r') as f1, open(exp_alara_inp, 'r') as f2: for a, b in zip(f1.readlines(), f2.readlines()): assert_equal(a, b) # test alara matlibe file with open(alara_matlib, 'r') as f1, open(exp_alara_matlib) as f2: for a, b in zip(f1.readlines(), f2.readlines()): assert_equal(a, b) # test alara fluxin file with open(fluxin, 'r') as f1, open(exp_fluxin) as f2: for a, b in zip(f1.readlines(), f2.readlines()): assert_equal(a, b) # test r2s step 1 output mesh fluxes = [[6.93088E-07, 1.04838E-06], [6.36368E-07, 9.78475E-07], [5.16309E-07, 9.86586E-07], [6.36887E-07, 9.29879E-07]] errs = [[9.67452E-02, 7.55950E-02], [9.88806E-02, 7.61482E-02], [1.04090E-01, 7.69284E-02], [9.75826E-02, 7.54181E-02]] tot_fluxes = [1.74147E-06, 1.61484E-06, 1.50290E-06, 1.56677E-06] tot_errs = [6.01522E-02, 6.13336E-02, 6.19920E-02, 5.98742E-02] m = Mesh(structured=True, mesh=output_mesh, mats=output_mesh) for i, mat, _ in m: assert_almost_equal(mat.density, 1.962963E+00) assert_equal(len(mat.comp), 3) assert_almost_equal(mat.comp[20040000], 1.886792E-02) assert_almost_equal(mat.comp[30060000], 5.886792E-01) assert_almost_equal(mat.comp[30070000], 3.924528E-01) assert_array_equal(m.n_flux[i], fluxes[i]) assert_array_equal(m.n_flux_err[i], errs[i]) assert_almost_equal(m.n_flux_total[i], tot_fluxes[i]) assert_almost_equal(m.n_flux_err_total[i], tot_errs[i]) os.remove(alara_inp) os.remove(alara_matlib) os.remove(fluxin) os.remove(output_mesh)
def step5(cfg, cfg2, cfg5): """ This function creates the adjoint neutron source and writes the PARTISN input file for adjoint neutron transport. Parameters ---------- cfg : dictionary User input from 'general' section of config.yml file cfg2 : dictionary User input for step 2 from the config.yml file cfg5 : dictionary User input for step 3 from the config.yml file """ # Get user-input from config file num_n_groups = cfg['n_groups'] num_p_groups = cfg['p_groups'] n_geom = cfg2['n_geom_file'] decay_times = str(cfg2['decay_times']).split(' ') meshflux = cfg5['meshflux'] # Base of geometry file name basename = n_geom.split("/")[-1].split(".")[0] # The total number of photon and neutron energy groups total_num_groups = num_n_groups + num_p_groups # Read given flux file and tag to a mesh if meshflux: # Adjoint flux file is an hdf5 mesh file fw_n_err = meshflux m = Mesh(structured=True, mesh=fw_n_err, mats=None) else: raise RuntimeError("No neutron flux file given") # Size of flux tag is equal to the total number of energy groups m.ERROR_TAG = NativeMeshTag(num_n_groups, name="ERROR_TAG") fw_n_err = m.ERROR_TAG[:] # Load geometry and get material assignments load(n_geom) ml = MaterialLibrary(n_geom) mat_names = list(ml.keys()) cell_mats = cell_material_assignments(n_geom) # Load T matrix if not os.path.exists('step2_T.npy'): raise RuntimeError("T matrix from step 2 (step2_T.npy) not found") T = np.load('step2_T.npy') # Loop over mesh voxels and calculate the error in the photon source by multiplying neutron flux and T matrix dg = discretize_geom(m) for t, dt in enumerate(decay_times): temp = np.zeros(shape=(len(m), num_p_groups)) for i in range(len(m)): for row in np.atleast_1d(dg[dg["idx"] == i]): cell = row[1] if not cell_mats[cell] in mat_names: continue vol_frac = row[2] mat = mat_names.index(cell_mats[cell]) for h in range(num_p_groups): for g in range(num_n_groups): temp[i, h] += (fw_n_err[i, g]**2) * T[mat, t, g, h] * vol_frac print("err ", temp[i, h]) # Tag the mesh with the squared error in the photon source values tag_name = "sq_err_q_src_{0}".format(dt) m.err_q_src = NativeMeshTag(num_p_groups, name=tag_name) m.err_q_src[:] = temp # Save adjoint neutron source mesh file tagged with values for all decay times m.save("sq_err_q_src.h5m")
def test_photon_source_hdf5_to_mesh_subvoxel_size1(): """Tests the function photon source_h5_to_mesh under sub-voxel r2s condition.""" if not HAVE_PYMOAB: raise SkipTest filename = os.path.join(thisdir, "files_test_alara", "phtn_src") photon_source_to_hdf5(filename, chunkshape=(10, )) assert_true(os.path.exists(filename + ".h5")) sub_voxel = True mesh = Mesh(structured=True, structured_coords=[[0, 1, 2], [0, 1, 2], [0, 1]]) cell_fracs = np.zeros( 4, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [ (0, 11, 1.0, 0.0), (1, 12, 1.0, 0.0), (2, 13, 1.0, 0.0), (3, 14, 1.0, 0.0), ] cell_mats = { 11: Material({"H": 1.0}, density=1.0), 12: Material({"He": 1.0}, density=1.0), 13: Material({"He": 1.0}, density=1.0), 14: Material({}, density=0.0, metadata={"name": "void"}), } mesh.tag_cell_fracs(cell_fracs) tags = {("1001", "shutdown"): "tag1", ("TOTAL", "1 h"): "tag2"} photon_source_hdf5_to_mesh(mesh, filename + ".h5", tags, sub_voxel=sub_voxel, cell_mats=cell_mats) # create lists of lists of expected results tag1_answers = [ [1.0] + [0.0] * 41, [2.0] + [0.0] * 41, [3.0] + [0.0] * 41, [0.0] * 42, ] tag2_answers = [ [5.0] + [0.0] * 41, [6.0] + [0.0] * 41, [7.0] + [0.0] * 41, [0.0] * 42, ] for i, _, ve in mesh: assert_array_equal(mesh.tag1[ve], tag1_answers[i]) assert_array_equal(mesh.tag2[ve], tag2_answers[i]) if os.path.isfile(filename + ".h5"): os.remove(filename + ".h5")
def test_tag_source_intensity(): m = Mesh(structured=True, structured_coords=[[0, 1, 2], [0, 1, 3], [0, 1]]) source_intensity = 1.0 m = tag_source_intensity(m, source_intensity) assert_array_equal(m.source_intensity[m], source_intensity)
def test_record_to_geom(): if not HAVE_PYTAPS: raise SkipTest expected_geom = os.path.join(thisdir, "files_test_alara", "alara_record_geom.txt") expected_matlib = os.path.join(thisdir, "files_test_alara", "alara_record_matlib.txt") geom = os.path.join(os.getcwd(), "alara_record_geom") matlib = os.path.join(os.getcwd(), "alara_record_matlib") cell_fracs = np.zeros(8, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), ('rel_error', np.float64)]) cell_mats = { 11: Material({ 'H1': 1.0, 'K39': 1.0 }, density=1.1, metadata={'mat_number': 21}), 12: Material({ 'H1': 0.1, 'O16': 1.0 }, density=1.2, metadata={'mat_number': 22}), 13: Material({'He4': 42.0}, density=1.3, metadata={'mat_number': 23}) } cell_fracs[:] = [(0, 11, 0.55, 0.0), (0, 12, 0.45, 0.0), (1, 11, 0.2, 0.0), (1, 12, 0.3, 0.0), (1, 13, 0.5, 0.0), (2, 11, 1.0, 0.0), (3, 11, 0.55, 0.0), (3, 12, 0.45, 0.0)] m = Mesh(structured_coords=[[-1, 0, 1], [-1, 0, 1], [0, 1]], structured=True, mats=None) record_to_geom(m, cell_fracs, cell_mats, geom, matlib) with open(expected_geom) as f: written = f.readlines() with open(geom) as f: expected = f.readlines() assert_equal(written, expected) if os.path.isfile(geom): os.remove(geom) with open(expected_matlib) as f: written = f.readlines() with open(matlib) as f: expected = f.readlines() assert_equal(written, expected) if os.path.isfile(matlib): os.remove(matlib)
def test_record_to_geom_subvoxel(): if not HAVE_PYMOAB: raise SkipTest expected_geom = os.path.join(thisdir, "files_test_alara", "alara_record_geom_subvoxel.txt") expected_matlib = os.path.join(thisdir, "files_test_alara", "alara_record_matlib_subvoxel.txt") geom = os.path.join(os.getcwd(), "alara_record_geom") matlib = os.path.join(os.getcwd(), "alara_record_matlib") cell_fracs = np.zeros(11, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), ('rel_error', np.float64)]) cell_mats = { 11: Material({ 'H1': 1.0, 'K39': 1.0 }, density=1.1, metadata={'name': 'fake_mat'}), 12: Material({ 'H1': 0.1, 'O16': 1.0 }, density=1.2, metadata={'name': 'water'}), 13: Material({'He4': 42.0}, density=1.3, metadata={'name': 'helium'}), 14: Material({}, density=0.0, metadata={'name': 'void'}), 15: Material({}, density=0.0, metadata={'name': 'void'}), 16: Material({ 'H1': 1.0, 'K39': 1.0 }, density=1.1, metadata={'name': 'fake_mat'}) } cell_fracs[:] = [(0, 11, 0.55, 0.0), (0, 12, 0.45, 0.0), (1, 11, 0.2, 0.0), (1, 12, 0.3, 0.0), (1, 13, 0.5, 0.0), (2, 11, 0.15, 0.0), (2, 14, 0.01, 0.0), (2, 15, 0.04, 0.0), (2, 16, 0.8, 0.0), (3, 11, 0.55, 0.0), (3, 12, 0.45, 0.0)] m = Mesh(structured_coords=[[-1, 0, 1], [-1, 0, 1], [0, 1]], structured=True, mats=None) record_to_geom(m, cell_fracs, cell_mats, geom, matlib, sub_voxel=True) assert (filecmp.cmp(geom, expected_geom)) if os.path.isfile(geom): os.remove(geom) assert (filecmp.cmp(matlib, expected_matlib)) if os.path.isfile(matlib): os.remove(matlib)
def irradiation_setup_structured(flux_tag="n_flux", meshtal_file="meshtal_2x2x1"): meshtal = os.path.join(thisdir, "files_test_r2s", meshtal_file) tally_num = 4 cell_mats = { 2: Material({2004: 1.0}, density=1.0, metadata={'name': 'mat_11'}), 3: Material({ 3007: 0.4, 3006: 0.6 }, density=2.0, metadata={'name': 'mat_12'}) } alara_params = "Bogus line for testing\n" geom = os.path.join(thisdir, "unitbox.h5m") num_rays = 9 grid = True fluxin = os.path.join(os.getcwd(), "alara_fluxin") reverse = True alara_inp = os.path.join(os.getcwd(), "alara_inp") alara_matlib = os.path.join(os.getcwd(), "alara_matlib") output_mesh = os.path.join(os.getcwd(), "r2s_step1.h5m") output_material = True cell_fracs = np.zeros(8, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), ('rel_error', np.float64)]) cell_fracs[:] = [(0, 2, 0.037037037037037035, 0.5443310539518174), (0, 3, 0.9629629629629631, 0.010467904883688123), (1, 2, 0.037037037037037035, 0.5443310539518174), (1, 3, 0.9629629629629629, 0.010467904883688454), (2, 2, 0.037037037037037035, 0.5443310539518174), (2, 3, 0.9629629629629629, 0.010467904883688454), (3, 2, 0.037037037037037035, 0.5443310539518174), (3, 3, 0.9629629629629629, 0.010467904883688454)] irradiation_setup(meshtal, cell_mats, cell_fracs, alara_params, tally_num, num_rays, grid, flux_tag, fluxin, reverse, alara_inp, alara_matlib, output_mesh, output_material) # expected output files exp_alara_inp = os.path.join(thisdir, "files_test_r2s", "exp_alara_inp") exp_alara_matlib = os.path.join(thisdir, "files_test_r2s", "exp_alara_matlib") exp_fluxin = os.path.join(thisdir, "files_test_r2s", "exp_fluxin") # test files f1 = filecmp.cmp(alara_inp, exp_alara_inp) f2 = filecmp.cmp(alara_matlib, exp_alara_matlib) f3 = filecmp.cmp(fluxin, exp_fluxin) m = Mesh(structured=True, mesh=output_mesh, mats=output_mesh) out = [ m.n_flux[:].tolist(), m.n_flux_err[:].tolist(), m.n_flux_total[:].tolist(), m.n_flux_err_total[:].tolist(), [x.comp.items() for y, x, z in m], [x.density for y, x, z in m] ] n_flux = out[0] n_flux_err = out[1] n_flux_total = out[2] n_flux_err_total = out[3] densities = out[5] comps = np.zeros(shape=(len(out[4])), dtype=dict) for i, comp in enumerate(out[4]): comps[i] = {} for nucid in comp: comps[i][nucid[0]] = nucid[1] # test r2s step 1 output mesh fluxes = [[6.93088E-07, 1.04838E-06], [6.36368E-07, 9.78475E-07], [5.16309E-07, 9.86586E-07], [6.36887E-07, 9.29879E-07]] errs = [[9.67452E-02, 7.55950E-02], [9.88806E-02, 7.61482E-02], [1.04090E-01, 7.69284E-02], [9.75826E-02, 7.54181E-02]] tot_fluxes = [1.74147E-06, 1.61484E-06, 1.50290E-06, 1.56677E-06] tot_errs = [6.01522E-02, 6.13336E-02, 6.19920E-02, 5.98742E-02] i = 0 for nf, nfe, nft, nfte, comp, density in izip(n_flux, n_flux_err, n_flux_total, n_flux_err_total, comps, densities): assert_almost_equal(density, 1.962963E+00) assert_equal(len(comp), 3) assert_almost_equal(comp[20040000], 1.886792E-02) assert_almost_equal(comp[30060000], 5.886792E-01) assert_almost_equal(comp[30070000], 3.924528E-01) assert_array_equal(nf, fluxes[i]) assert_array_equal(nfe, errs[i]) assert_almost_equal(nft, tot_fluxes[i]) assert_almost_equal(nfte, tot_errs[i]) i += 1 os.remove(alara_inp) os.remove(alara_matlib) os.remove(fluxin) os.remove(output_mesh) return [f1, f2, f3]
def irradiation_setup_unstructured(): flux_tag = "n_flux" meshtal_file = os.path.join(thisdir, "files_test_r2s", "meshtal_2x2x1") meshtal = Meshtal( meshtal_file, { 4: (flux_tag, flux_tag + "_err", flux_tag + "_total", flux_tag + "_err_total") }) # Explicitly make this mesh unstructured, it will now iterate in yxz # order which is MOAB structured mesh creation order. meshtal = Mesh(structured=False, mesh=meshtal.tally[4].mesh) meshtal_mesh_file = os.path.join(thisdir, "meshtal.h5m") meshtal.mesh.save(meshtal_mesh_file) cell_mats = { 2: Material({2004: 1.0}, density=1.0, metadata={'mat_number': 11}), 3: Material({ 3007: 0.4, 3006: 0.6 }, density=2.0, metadata={'mat_number': 12}) } alara_params = "Bogus line for testing\n" geom = os.path.join(thisdir, "unitbox.h5m") flux_tag = "n_flux" fluxin = os.path.join(os.getcwd(), "alara_fluxin") reverse = True alara_inp = os.path.join(os.getcwd(), "alara_inp") alara_matlib = os.path.join(os.getcwd(), "alara_matlib") output_mesh = os.path.join(os.getcwd(), "r2s_step1.h5m") output_material = True irradiation_setup(flux_mesh=meshtal_mesh_file, cell_mats=cell_mats, alara_params=alara_params, geom=geom, flux_tag=flux_tag, fluxin=fluxin, reverse=reverse, alara_inp=alara_inp, alara_matlib=alara_matlib, output_mesh=output_mesh, output_material=output_material) # expected output files exp_alara_inp = os.path.join(thisdir, "files_test_r2s", "exp_alara_inp_un") exp_alara_matlib = os.path.join(thisdir, "files_test_r2s", "exp_alara_matlib") exp_fluxin = os.path.join(thisdir, "files_test_r2s", "exp_fluxin_un") # test files f1 = filecmp.cmp(alara_inp, exp_alara_inp) f2 = filecmp.cmp(alara_matlib, exp_alara_matlib) f3 = filecmp.cmp(fluxin, exp_fluxin) m = Mesh(structured=True, mesh=output_mesh, mats=output_mesh) m_out = [ m.n_flux[:].tolist(), m.n_flux_err[:].tolist(), m.n_flux_total[:].tolist(), m.n_flux_err_total[:].tolist(), [x.comp.items() for y, x, z in m], [x.density for y, x, z in m] ] os.remove(meshtal_mesh_file) os.remove(alara_inp) os.remove(alara_matlib) os.remove(fluxin) os.remove(output_mesh) return [m_out, f1, f2, f3]
def test_ve_center(): m = Mesh(structured=True, structured_coords=[[-1, 3, 5], [-1, 1], [-1, 1]]) exp_centers = [(1, 0, 0), (4, 0, 0)] for i, mat, ve in m: assert_equal(m.ve_center(ve), exp_centers[i])
def write_partisn_input_options(): if not HAVE_PYMOAB: raise SkipTest """Test PARTISN input file creation with a slew of keyword arguments """ THIS_DIR = os.path.dirname(os.path.realpath(__file__)) hdf5 = os.path.join(THIS_DIR, 'files_test_partisn', 'partisn_test_geom.h5m') input_file = os.path.join(THIS_DIR, 'files_test_partisn', 'partisn_options.inp') file_expected = os.path.join(THIS_DIR, 'files_test_partisn', 'partisn_options_expected.inp') sc = [-5., 0., 10., 15.], [-5., 5.], [-5., 5.] mesh = Mesh(structured_coords=sc, structured=True) ngroup = 66 dg = np.zeros(4, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), ('rel_error', np.float64)]) dg[:] = [(0, 1, 1.0, 0.0), (1, 1, 0.5, 0.04714045207910317), (1, 2, 0.5, 0.04714045207910317), (2, 2, 1.0, 0.0)] mat_assigns = { 1: 'mat:Helium, Natural', 2: 'mat:Mercury', 5: 'mat:Graveyard', 6: u'mat:Vacuum' } cards = { "block1": { "isn": 6, "maxlcm": 6000000, "maxscm": 3000000, }, "block2": { "hello": "from block2" }, "block3": { "i2lp1": 0, "ifido": 1, "ihm": 227, "ihs": 11, "iht": 10, "ititl": 1, "kwikrd": 1, "lib": "xsf21-71", "lng": 175, "maxord": 5, "savbxs": 1 }, "block4": { "hello": "from block4" }, "block5": { "source": "<this is a dummy source>" } } with warnings.catch_warnings(record=True) as w: partisn.write_partisn_input( mesh, hdf5, ngroup, input_file=input_file, dg=dg, mat_assigns=mat_assigns, fine_per_coarse=3, cards=cards, num_rays=9) # include num_rays to get warning # verify we get a warning from including num_rays and dg out1 = len(w) == 1 out2 = filecmp.cmp(input_file, file_expected) os.remove(input_file) return out1 and out2
def resolve_mesh(mesh_reference, tally_num=None, flux_tag="n_flux", output_material=False): """This function creates a method that will consume many mesh-like objects (e.g. mesh, an h5m file, a meshtal file, etc) and returns a robust PyNE mesh object accordingly. Parameters ---------- mesh_reference : Mesh object, unstructured mesh file, Meshtal, meshtal file, or PyNE Meshtal object. The source of the neutron flux information. This can be a PyNE Meshtal object, a pyne Mesh object, or the filename an MCNP meshtal file, or the filename of an unstructured mesh tagged with fluxes. tally_num : int The MCNP FMESH4 tally number of the neutron flux tally within the meshtal file. flux_tag : str, optional The tag name for the neutron flux. output_material : bool, optional If true, output mesh will have materials as determined by dagmc.discretize_geom(). Returns ------- m : PyNE mesh object The PyNE mesh object of the flux data. """ # define tag_names according to the flux_tag tag_names = ( flux_tag, flux_tag + "_err", flux_tag + "_total", flux_tag + "_err_total", ) # mesh_reference is Mesh object if isinstance(mesh_reference, Mesh): m = mesh_reference # mesh_reference is a file path elif isinstance(mesh_reference, str) and not isfile(mesh_reference): raise ValueError("File {0} not found!".format(mesh_reference)) # mesh_reference is unstructured mesh file elif (isinstance(mesh_reference, str) and isfile(mesh_reference) and mesh_reference.endswith(".h5m")): m = Mesh(structured=False, mesh=mesh_reference) # mesh_reference is a openmc statepoint file elif (isinstance(mesh_reference, str) and isfile(mesh_reference) and mesh_reference.endswith(".h5")): m = openmc_utils.create_meshtally(mesh_reference, tally_id=tally_num, tag_names=tag_names) # mesh_reference is Meshtal or meshtal file elif tally_num is not None: # mesh_reference is meshtal file if isinstance(mesh_reference, str) and isfile(mesh_reference): mesh_reference = Meshtal(mesh_reference, {tally_num: tag_names}, meshes_have_mats=output_material) m = mesh_reference.tally[tally_num] # mesh_reference is Meshtal object elif isinstance(mesh_reference, Meshtal): m = mesh_reference.tally[tally_num] else: raise ValueError("meshtal argument not a Mesh object, Meshtal" " object, MCNP meshtal file or meshtal.h5m file.") # mesh_references is a Meshtal or statepoint file but no tally_num provided else: raise ValueError( "Need to provide a tally number when reading a Meshtal or" " statepoint file") return m
def get_zones_with_void(): """Test the _get_zones function if a void is present. """ try: from pyne import dagmc except: raise SkipTest if not HAVE_PYMOAB: raise SkipTest # hdf5 test file THIS_DIR = os.path.dirname(os.path.realpath(__file__)) hdf5 = THIS_DIR + '/files_test_partisn/partisn_test_geom.h5m' data_hdf5path = '/materials' nuc_hdf5path = '/nucid' dagmc.load(hdf5) # mesh xvals = [-5., 0., 10., 15., 15.1] yvals = [-5., 0., 5.] zvals = [-5., 0., 5.] mesh = Mesh(structured_coords=[xvals, yvals, zvals], structured=True, structured_ordering='xyz') # more inputs bounds = {'x': xvals, 'y': yvals, 'z': zvals} num_rays = 400 grid = True dg = None mat_assigns = None unique_names = { 'mat:Helium, Natural': 'HELIUMNA', 'mat:Mercury': 'MERCURY1' } voxel_zones, zones = partisn._get_zones(mesh, hdf5, bounds, num_rays, grid, dg, mat_assigns, unique_names) # expected results voxel_zones_expected = np.array([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [0, 0, 0, 0]]) zones_expected = { 1: { 'vol_frac': [1.0], 'mat': [u'HELIUMNA'] }, 2: { 'vol_frac': [0.5, 0.5], 'mat': [u'HELIUMNA', u'MERCURY1'] }, 3: { 'vol_frac': [1.0], 'mat': [u'MERCURY1'] } } vz_tf = False z_tf = False if voxel_zones.all() == voxel_zones_expected.all(): vz_tf = True if zones == zones_expected: z_tf = True #assert(voxel_zones.all() == voxel_zones_expected.all()) #assert(zones == zones_expected) return [vz_tf, z_tf]
def get_zones_iteration_order(): """Test that _get_zones function gives results in zyx order. """ try: from pyne import dagmc except: raise SkipTest if not HAVE_PYMOAB: raise SkipTest # hdf5 test file THIS_DIR = os.path.dirname(os.path.realpath(__file__)) hdf5 = THIS_DIR + '/files_test_partisn/fractal_box.h5m' data_hdf5path = '/materials' nuc_hdf5path = '/nucid' # Load dagmc geometry dagmc.load(hdf5) bounds = [-5., 0., 5.] sc = [bounds] * 3 mesh = Mesh(structured_coords=sc, structured=True) bounds = {'x': bounds, 'y': bounds, 'z': bounds} num_rays = 9 grid = True dg = None mat_assigns = None unique_names = { 'mat:m1': 'M1', 'mat:m2': 'M2', 'mat:m3': 'M3', 'mat:m4': 'M4' } voxel_zones, zones = partisn._get_zones(mesh, hdf5, bounds, num_rays, grid, dg, mat_assigns, unique_names) voxel_zones_expected = np.array([[1, 2], [1, 4], [1, 3], [1, 4]]) zones_expected = { 1: { 'vol_frac': [1.0], 'mat': ['M2'] }, 2: { 'vol_frac': [1.0], 'mat': ['M4'] }, 3: { 'vol_frac': [1.0], 'mat': ['M1'] }, 4: { 'vol_frac': [1.0], 'mat': ['M3'] } } vz_tf = voxel_zones.all() == voxel_zones_expected.all() z_tf = zones == zones_expected return [vz_tf, z_tf]
def main(): msg = ("This script reads a mesh (.h5m) file and, for each vector tag of\n" "length N, creates N scalar tags. This is useful for visualizing\n" "data on mesh with programs that do not support vector tags.\n") parser = argparse.ArgumentParser(msg) parser.add_argument("mesh_file", help="The path the mesh file") parser.add_argument( "-o", dest="output", default="expanded_tags.vtk", help="The name of the output file", ) parser.add_argument( "-t", "--tags", dest="tags", default=None, help=("Instead of expanding all tags, only expand tags\n" "within this list. Tag names listed here should\n" "be seperated by commas without spaces.\n"), ) args = parser.parse_args() try: from pymoab import types except ImportError: warn( "The PyMOAB optional dependency could not be imported. " "Some aspects of the mesh module may be incomplete.", ImportWarning, ) try: m = Mesh(structured=True, mesh=args.mesh_file) except types.MB_TAG_NOT_FOUND: sys.stderr.write("Structured mesh not found\n \ trying unstructured mesh \n") m = Mesh(structured=False, mesh=args.mesh_file) if args.tags is not None: tags = args.tags.split(",") else: tags = [] for name, tag in m.tags.items(): if isinstance(tag, NativeMeshTag) and name != "idx": tags.append(name) for tag in tags: m.tag = NativeMeshTag(name=tag) # there may be vector tags try: # this line fails if the tag is a scalar tag m.tag.size = len(m.tag[0]) except TypeError: sys.stderr.write("Vector tag not found, assuming scalar tag \n") m.tag.size = 1 if m.tag.size > 1: print("Expanding tag: {}".format(tag)) m.tag.expand() print("Saving file {}".format(args.output)) m.write_hdf5(args.output) print("Complete")
def test_wwinp_p(): if not HAVE_PYMOAB: raise SkipTest thisdir = os.path.dirname(__file__) wwinp_file = os.path.join(thisdir, "mcnp_wwinp_wwinp_p.txt") expected_h5m = os.path.join(thisdir, "mcnp_wwinp_mesh_p.h5m") expected_sm = Mesh(mesh=expected_h5m, structured=True) output = os.path.join(os.getcwd(), "test_wwinp") # Read in the wwinp file to an object and check resulting attributes. ww1 = mcnp.Wwinp() ww1.read_wwinp(wwinp_file) assert_equal(ww1.ni, 2) assert_equal(ww1.nr, 10) assert_equal(ww1.ne, [0, 7]) assert_equal(ww1.nf, [1, 8, 6]) assert_equal(ww1.origin, [-100, -100, -100]) assert_equal(ww1.nc, [1, 3, 1]) assert_equal(ww1.nwg, 1) assert_equal(ww1.cm, [[100], [-50, 60, 100], [100]]) assert_equal(ww1.fm, [[1], [1, 3, 4], [6]]) assert_equal(ww1.e[0], []) assert_array_equal( ww1.e[1], [0.1, 0.14678, 0.21544, 0.31623, 0.46416, 0.68129, 1.0000] ) assert_equal( ww1.bounds, [ [-100.0, 100], [ -100.0, -50.0, -13.333333333333336, 23.333333333333329, 60.0, 70.0, 80.0, 90.0, 100.0, ], [ -100.0, -66.666666666666657, -33.333333333333329, 0.0, 33.333333333333343, 66.666666666666657, 100.0, ], ], ) expected_ves = list(expected_sm.structured_iterate_hex("zyx")) written_ves = list(expected_sm.structured_iterate_hex("zyx")) for expected_ve, written_ve in zip(expected_ves, written_ves): expected = expected_sm.ww_p[expected_ve] written = ww1.ww_p[written_ve] assert_array_equal(written, expected) # Create an new object based off of only the mesh attribute of the first # object and check resutling attributes. ww2 = mcnp.Wwinp() ww2.read_mesh(ww1.mesh) assert_equal(ww2.ni, 2) assert_equal(ww2.nr, 10) assert_equal(ww2.ne, [0, 7]) assert_equal(ww2.nf, [1, 8, 6]) assert_equal(ww2.origin, [-100, -100, -100]) assert_equal(ww2.nc, [1, 3, 1]) assert_equal(ww2.nwg, 1) assert_equal(ww2.cm, [[100], [-50, 60, 100], [100]]) assert_equal(ww2.fm, [[1], [1, 3, 4], [6]]) assert_equal(ww2.e[0], []) assert_array_equal( ww2.e[1], [0.1, 0.14678, 0.21544, 0.31623, 0.46416, 0.68129, 1.0000] ) assert_equal( ww2.bounds, [ [-100.0, 100], [ -100.0, -50.0, -13.333333333333336, 23.333333333333329, 60.0, 70.0, 80.0, 90.0, 100.0, ], [ -100.0, -66.666666666666657, -33.333333333333329, 0.0, 33.333333333333343, 66.666666666666657, 100.0, ], ], ) expected_ves = list(expected_sm.structured_iterate_hex("zyx")) written_ves = list(expected_sm.structured_iterate_hex("zyx")) for expected_ve, written_ve in zip(expected_ves, written_ves): expected = expected_sm.ww_p[expected_ve] written = ww2.ww_p[written_ve] assert_array_equal(written, expected) # write a new wwinp file and verify that is same wwinp file used as an # input to this test ww2.write_wwinp(output) expected_output = wwinp_file with open(output) as f: written = f.readlines() with open(expected_output) as f: expected = f.readlines() # check to make sure file are the same except for the data/time info # on line 1 assert_equal(written[0].split()[:-2], expected[0].split()[:-2]) assert_equal(len(written), len(expected)) for i in range(1, len(expected)): for j in range(0, len(expected[i].split())): assert_equal(float(written[i].split()[j]), float(expected[i].split()[j])) os.remove(output)
def test_unstructured_mesh_from_file(): filename = os.path.join(os.path.dirname(__file__), "files_mesh_test/unstr.h5m") sm = Mesh(mesh=filename)
def irradiation_setup(flux_mesh, cell_mats, alara_params, tally_num=4, geom=None, num_rays=10, grid=False, flux_tag="n_flux", fluxin="alara_fluxin", reverse=False, alara_inp="alara_geom", alara_matlib="alara_matlib", output_mesh="r2s_step1.h5m"): """This function is used to setup the irradiation inputs after the first R2S transport step. Parameters ---------- flux_mesh : PyNE Meshtal object, Mesh object, or str The source of the neutron flux information. This can be a PyNE Meshtal object, a pyne Mesh object, or the filename an MCNP meshtal file, or the filename of an unstructured mesh tagged with fluxes. tally_num : int The MCNP FMESH4 tally number of the neutron flux tally within the meshtal file. cell_mats : dict Maps geometry cell numbers to PyNE Material objects. alara_params : str The ALARA input blocks specifying everything except the geometry and materials. This can either be passed as string or as a file name. geom : str, optional The file name of a DAGMC-loadable faceted geometry. This is only necessary if the geometry is not already loaded into memory. num_rays : int, optional The number of rays to fire down a mesh row for geometry discretization. This number must be a perfect square if grid=True. grid : bool, optional The if False, geometry discretization will be done with randomly fired rays. If true, a grid of sqrt(num_rays) x sqrt(num_rays) rays is used for each mesh row. flux_tag : str, optional The iMesh tag for the neutron flux. fluxin : str, optional The name of the ALARA fluxin file to be created. reverse : bool, optional If True the fluxes in the fluxin file will be printed in the reverse order of how they appear within the flux vector tag. Since MCNP and the Meshtal class order fluxes from low energy to high energy, this option should only be true if the transmutation data being used is ordered from high energy to low energy. alara_inp : str, optional The name of the ALARA input file to be created. alara_matlib : str, optional The name of the alara_matlib file to be created. output_mesh : str, optional A mesh containing all the fluxes and materials used for irradiation setup. """ if geom is not None and isfile(geom): load(geom) # flux_mesh is Mesh object if isinstance(flux_mesh, Mesh): m = flux_mesh # flux_mesh is unstructured mesh file elif isinstance(flux_mesh, str) and isfile(flux_mesh) \ and flux_mesh.endswith(".h5m"): m = Mesh(structured=False, mesh=flux_mesh) # flux_mesh is Meshtal or meshtal file else: # flux_mesh is meshtal file if isinstance(flux_mesh, str) and isfile(flux_mesh): flux_mesh = Meshtal(flux_mesh, { tally_num: (flux_tag, flux_tag + "_err", flux_tag + "_total", flux_tag + "_err_total") }, meshes_have_mats=True) m = flux_mesh.tally[tally_num] # flux_mesh is Meshtal object elif instance(flux_mesh, Meshtal): m = flux_mesh.tally[tally_num] else: raise ValueError("meshtal argument not a Mesh object, Meshtal" " object, MCNP meshtal file or meshtal.h5m file.") if m.structured: vol_fracs = discretize_geom(m, num_rays=num_rays, grid=grid) else: vol_fracs = discretize_geom(m) m.cell_fracs_to_mats(vol_fracs, cell_mats) mesh_to_fluxin(m, flux_tag, fluxin, reverse) mesh_to_geom(m, alara_inp, alara_matlib) if isfile(alara_params): with open(alara_params, 'r') as f: alara_params = f.read(alara_params) with open(alara_inp, 'a') as f: f.write("\n" + alara_params) m.write_hdf5(output_mesh)
def test_unstructured_mesh_from_instance(): filename = os.path.join(os.path.dirname(__file__), "files_mesh_test/unstr.h5m") mesh = mb_core.Core() mesh.load_file(filename) sm = Mesh(mesh=mesh)
def irradiation_setup_unstructured(flux_tag="n_flux"): meshtal_filename = "meshtal_2x2x1" meshtal_file = os.path.join(thisdir, "files_test_r2s", meshtal_filename) meshtal = Meshtal( meshtal_file, { 4: (flux_tag, flux_tag + "_err", flux_tag + "_total", flux_tag + "_err_total") }) # Explicitly make this mesh unstructured, it will now iterate in yxz # order which is MOAB structured mesh creation order. meshtal = Mesh(structured=False, mesh=meshtal.tally[4].mesh) meshtal_mesh_file = os.path.join(thisdir, "meshtal.h5m") meshtal.write_hdf5(meshtal_mesh_file, write_mats=False) if flux_tag != "n_flux": # if not using n_flux makes a mesh containing n_flux tag, and then # makes a new tag called flux_tag, to use later in the test flux_tag_name = "n_flux" meshtal = Meshtal( meshtal_file, { 4: (flux_tag_name, flux_tag_name + "_err", flux_tag_name + "_total", flux_tag_name + "_err_total") }) # Explicitly make this mesh unstructured, it will now iterate in yxz # order which is MOAB structured mesh creation order. meshtal = Mesh(structured=False, mesh=meshtal.tally[4].mesh) meshtal_mesh_file = os.path.join(thisdir, "meshtal.h5m") meshtal.write_hdf5(meshtal_mesh_file, write_mats=False) new_mesh = Mesh(structured=False, mesh=meshtal_mesh_file) new_mesh.TALLY_TAG = NativeMeshTag(2, float) # 2 egroups new_mesh.TALLY_TAG = meshtal.n_flux[:] # overwrite the mesh file new_mesh.write_hdf5(meshtal_mesh_file, write_mats=False) cell_mats = { 2: Material({2004: 1.0}, density=1.0, metadata={'name': 'mat_11'}), 3: Material({ 3007: 0.4, 3006: 0.6 }, density=2.0, metadata={'name': 'mat_12'}) } alara_params = "Bogus line for testing\n" geom = os.path.join(thisdir, "unitbox.h5m") fluxin = os.path.join(os.getcwd(), "alara_fluxin") reverse = True alara_inp = os.path.join(os.getcwd(), "alara_inp") alara_matlib = os.path.join(os.getcwd(), "alara_matlib") output_mesh = os.path.join(os.getcwd(), "r2s_step1.h5m") output_material = True cell_fracs = np.zeros(4, dtype=[('idx', np.int64), ('cell', np.int64), ('vol_frac', np.float64), ('rel_error', np.float64)]) cell_fracs[:] = [(0, 3, 1.0, 1.0), (1, 3, 1.0, 1.0), (2, 3, 1.0, 1.0), (3, 3, 1.0, 1.0)] irradiation_setup(flux_mesh=meshtal_mesh_file, cell_mats=cell_mats, cell_fracs=cell_fracs, alara_params=alara_params, flux_tag=flux_tag, fluxin=fluxin, reverse=reverse, alara_inp=alara_inp, alara_matlib=alara_matlib, output_mesh=output_mesh, output_material=output_material) # expected output files exp_alara_inp = os.path.join(thisdir, "files_test_r2s", "exp_alara_inp_un") exp_alara_matlib = os.path.join(thisdir, "files_test_r2s", "exp_alara_matlib") exp_fluxin = os.path.join(thisdir, "files_test_r2s", "exp_fluxin_un") # test files f1 = filecmp.cmp(alara_inp, exp_alara_inp) f2 = filecmp.cmp(alara_matlib, exp_alara_matlib) f3 = filecmp.cmp(fluxin, exp_fluxin) m = Mesh(structured=True, mesh=output_mesh, mats=output_mesh) out = [ m.n_flux[:].tolist(), m.n_flux_err[:].tolist(), m.n_flux_total[:].tolist(), m.n_flux_err_total[:].tolist(), [x.comp.items() for y, x, z in m], [x.density for y, x, z in m] ] n_flux = out[0] n_flux_err = out[1] n_flux_total = out[2] n_flux_err_total = out[3] densities = out[5] comps = np.zeros(shape=(len(out[4])), dtype=dict) for i, comp in enumerate(out[4]): comps[i] = {} for nucid in comp: comps[i][nucid[0]] = nucid[1] # test r2s step 1 output mesh fluxes = [[6.93088E-07, 1.04838E-06], [6.36368E-07, 9.78475E-07], [5.16309E-07, 9.86586E-07], [6.36887E-07, 9.29879E-07]] errs = [[9.67452E-02, 7.55950E-02], [9.88806E-02, 7.61482E-02], [1.04090E-01, 7.69284E-02], [9.75826E-02, 7.54181E-02]] tot_fluxes = [1.74147E-06, 1.61484E-06, 1.50290E-06, 1.56677E-06] tot_errs = [6.01522E-02, 6.13336E-02, 6.19920E-02, 5.98742E-02] i = 0 for nf, nfe, nft, nfte, comp, density in izip(n_flux, n_flux_err, n_flux_total, n_flux_err_total, comps, densities): assert_almost_equal(density, 2.0) assert_equal(len(comp), 2) assert_almost_equal(comp[30060000], 0.6) assert_almost_equal(comp[30070000], 0.4) assert_array_equal(nf, fluxes[i]) assert_array_equal(nfe, errs[i]) assert_almost_equal(nft, tot_fluxes[i]) assert_almost_equal(nfte, tot_errs[i]) i += 1 os.remove(meshtal_mesh_file) os.remove(alara_inp) os.remove(alara_matlib) os.remove(fluxin) os.remove(output_mesh) return [f1, f2, f3]
def test_issue360(): a = Mesh(structured=True, structured_coords=[[0, 1, 2], [0, 1], [0, 1]]) a.cat = NativeMeshTag(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]])
def test_tag_e_bounds(): m = Mesh(structured=True, structured_coords=[[0, 1, 2], [0, 1, 3], [0, 1]]) e_bounds = np.array([0.0, 0.1, 20]) m = tag_e_bounds(m, e_bounds) assert_array_equal(m.e_bounds[m], e_bounds)
def test_mesh_to_geom(): if not HAVE_PYTAPS: raise SkipTest 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), 4: Material({'C12': 1.0}, density=47.0), 5: Material({'1002': 1.0}, density=5.0), } m = Mesh(structured_coords=[[0, 1, 2, 3], [0, 1, 2], [0, 1]], mats=mats, structured=True) geom = mcnp.mesh_to_geom(m) exp_geom = ("Generated from PyNE Mesh\n" "1 1 42.0 1 -2 5 -6 8 -9\n" "2 2 43.0 1 -2 6 -7 8 -9\n" "3 3 44.0 2 -3 5 -6 8 -9\n" "4 4 45.0 2 -3 6 -7 8 -9\n" "5 5 47.0 3 -4 5 -6 8 -9\n" "6 6 5.0 3 -4 6 -7 8 -9\n" "7 0 -1:4:-5:7:-8:9\n" "\n" "1 px 0.0\n" "2 px 1.0\n" "3 px 2.0\n" "4 px 3.0\n" "5 py 0.0\n" "6 py 1.0\n" "7 py 2.0\n" "8 pz 0.0\n" "9 pz 1.0\n" "\n" "C density = 42.0\n" "m1\n" " 1001 -5.0000E-01\n" " 19039 -5.0000E-01\n" "C density = 43.0\n" "m2\n" " 1001 -9.0909E-02\n" " 8016 -9.0909E-01\n" "C density = 44.0\n" "m3\n" " 2004 -1.0000E+00\n" "C density = 45.0\n" "m4\n" " 69171 -1.0000E+00\n" "C density = 47.0\n" "m5\n" " 6012 -1.0000E+00\n" "C density = 5.0\n" "m6\n" " 1002 -1.0000E+00\n") assert_equal(geom, exp_geom)
def test_tag_decay_time(): m = Mesh(structured=True, structured_coords=[[0, 1, 2], [0, 1, 3], [0, 1]]) decay_time = 1.0 m = tag_decay_time(m, decay_time) assert_array_equal(m.decay_time[m], decay_time)
def test_single_meshtally_meshtal(): """Test a meshtal file containing a single mesh tally.""" if not HAVE_PYMOAB: raise SkipTest thisdir = os.path.dirname(__file__) meshtal_file = os.path.join(thisdir, "mcnp_meshtal_single_meshtal.txt") expected_h5m = os.path.join(thisdir, "mcnp_meshtal_single_mesh.h5m") expected_sm = Mesh(mesh=expected_h5m, structured=True) tags = {4: ["n_result", "n_rel_error", "n_total_result", "n_total_rel_error"]} meshtal_object = mcnp.Meshtal(meshtal_file, tags, meshes_have_mats=True) assert_not_equal(meshtal_object.tally[4].mats, None) # test Meshtal attributes assert_equal(meshtal_object.version, "5.mpi") assert_equal(meshtal_object.ld, "09282010") assert_equal(meshtal_object.title, "Input file to general test meshtal file") assert_equal(meshtal_object.histories, 100000) # test MeshTally attributes assert_equal(meshtal_object.tally[4].tally_number, 4) assert_equal(meshtal_object.tally[4].particle, "neutron") assert_equal(meshtal_object.tally[4].dose_response, True) assert_equal(meshtal_object.tally[4].x_bounds, (-200.00, -66.67, 66.67, 200.00)) assert_equal( meshtal_object.tally[4].y_bounds, (-200.00, -120.00, -40.00, 40.00, 120.00, 200.00), ) assert_equal(meshtal_object.tally[4].z_bounds, (-200.00, -50.00, 100.00, 200.00)) assert_equal( meshtal_object.tally[4].e_bounds, (0.00e00, 1.00e-01, 2.00e-01, 1.00e00) ) # test vector tags for v_e, expected_v_e in zip( meshtal_object.tally[4].structured_iterate_hex("xyz"), expected_sm.structured_iterate_hex("xyz"), ): written = meshtal_object.tally[4].n_result[v_e] expected = expected_sm.n_result[expected_v_e] assert_array_equal(written, expected) for v_e, expected_v_e in zip( meshtal_object.tally[4].structured_iterate_hex("xyz"), expected_sm.structured_iterate_hex("xyz"), ): written = meshtal_object.tally[4].n_rel_error[v_e] expected = expected_sm.n_rel_error[expected_v_e] assert_array_equal(written, expected) # test total tag for v_e, expected_v_e in zip( meshtal_object.tally[4].structured_iterate_hex("xyz"), expected_sm.structured_iterate_hex("xyz"), ): written = meshtal_object.tally[4].n_total_result[v_e] expected = expected_sm.n_total_result[expected_v_e] assert_equal(written, expected) for v_e, expected_v_e in zip( meshtal_object.tally[4].structured_iterate_hex("xyz"), expected_sm.structured_iterate_hex("xyz"), ): written = meshtal_object.tally[4].n_total_rel_error[v_e] expected = expected_sm.n_total_rel_error[expected_v_e] assert_equal(written, expected)
def step1(cfg): """This function writes the PARTISN input file for the adjoint photon transport Parameters ---------- cfg : dictionary User input for step 1 from the config.yml file """ # Get user-input from config file geom = cfg["geom_file"] cells = [cfg["src_cell"]] src_vol = [float(cfg["src_vol"])] try: origin_x, origin_y, origin_z = cfg["origin"].split(" ") except: print("Too few entries in origin location") xmesh = cfg["xmesh"] xints = cfg["xints"] ymesh = cfg["ymesh"] yints = cfg["yints"] zmesh = cfg["zmesh"] zints = cfg["zints"] # Create structured mesh sc = [ np.linspace(float(origin_x), float(xmesh), float(xints) + 1), np.linspace(float(origin_y), float(ymesh), float(yints) + 1), np.linspace(float(origin_z), float(zmesh), float(zints) + 1), ] m = Mesh(structured=True, structured_coords=sc) m.write_hdf5("blank_mesh.h5m") # Generate 42 photon energy bins [eV] # First bin has been replaced with 1 for log interpolation photon_bins = np.array( [ 1e-6, 0.01, 0.02, 0.03, 0.045, 0.06, 0.07, 0.075, 0.1, 0.15, 0.2, 0.3, 0.4, 0.45, 0.51, 0.512, 0.6, 0.7, 0.8, 1, 1.33, 1.34, 1.5, 1.66, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 10, 12, 14, 20, 30, 50, ] ) # ICRP 74 flux-to-dose conversion factors in pico-Sv/s per photon flux de = np.array( [ 0.01, 0.015, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.8, 1, 2, 4, 6, 8, 10, ] ) df = np.array( [ 0.0485, 0.1254, 0.205, 0.2999, 0.3381, 0.3572, 0.378, 0.4066, 0.4399, 0.5172, 0.7523, 1.0041, 1.5083, 1.9958, 2.4657, 2.9082, 3.7269, 4.4834, 7.4896, 12.0153, 15.9873, 19.9191, 23.76, ] ) # Convert to Sv/s per photon FLUX pico = 1.0e-12 df = df * pico # Convert pointwise data to group data for log interpolation photon_spectrum = pointwise_collapse(photon_bins, de, df, logx=True, logy=True) # Anything below 0.01 MeV should be assigned the DF value of 0.01 MeV photon_spectrum[0] = df[0] # Total number of groups is 217 (42 photon + 175 neutron) spectra = [np.append(photon_spectrum, np.zeros(175))] # The spectrum is normalized by PyNE, so we need to mutliply by the sum of # intensities in the spectrum. # Additionally, we divide by the volume of the source cell in order to get # source density. intensities = [np.sum(spectra) / src_vol] # Load geometry into DAGMC load(geom) # Generate isotropic photon volume source source, dg = isotropic_vol_source(geom, m, cells, spectra, intensities) # PARTISN input ngroup = 217 # total number of energy groups cards = _cards(source) # block 1, 3, 5 input values names_dict = _names_dict() # dictionary of isotopes (PyNE nucids to bxslib names) write_partisn_input( m, geom, ngroup, cards=cards, dg=dg, names_dict=names_dict, data_hdf5path="/materials", nuc_hdf5path="/nucid", fine_per_coarse=1, )
def test_multiple_meshtally_meshtal(): """Test a meshtal file containing 4 mesh tallies including neutron and photon, single energy group and multiple energy group. """ if not HAVE_PYMOAB: raise SkipTest thisdir = os.path.dirname(__file__) meshtal_file = os.path.join(thisdir, "mcnp_meshtal_multiple_meshtal.txt") expected_h5m_4 = os.path.join(thisdir, "mcnp_meshtal_tally_4.h5m") expected_sm_4 = Mesh(mesh=expected_h5m_4, structured=True) expected_h5m_14 = os.path.join(thisdir, "mcnp_meshtal_tally_14.h5m") expected_sm_14 = Mesh(mesh=expected_h5m_14, structured=True) expected_h5m_24 = os.path.join(thisdir, "mcnp_meshtal_tally_24.h5m") expected_sm_24 = Mesh(mesh=expected_h5m_24, structured=True) expected_h5m_34 = os.path.join(thisdir, "mcnp_meshtal_tally_34.h5m") expected_sm_34 = Mesh(mesh=expected_h5m_34, structured=True) tags = { 4: ["n_result", "n_rel_error", "n_total_result", "n_total_rel_error"], 14: ["n_result", "n_rel_error", "n_total_result", "n_total_rel_error"], 24: ["p_result", "p_rel_error", "p_total_result", "p_total_rel_error"], 34: ["p_result", "p_rel_error", "p_total_result", "p_total_rel_error"], } meshtal_object = mcnp.Meshtal(meshtal_file, tags) assert_equal(meshtal_object.version, "5") assert_equal(meshtal_object.tally[4].mats, None) # test meshtally 4 for v_e, expected_v_e in zip( meshtal_object.tally[4].structured_iterate_hex("xyz"), expected_sm_4.structured_iterate_hex("xyz"), ): written = meshtal_object.tally[4].n_result[v_e] expected = expected_sm_4.n_result[expected_v_e] assert_array_equal(written, expected) for v_e, expected_v_e in zip( meshtal_object.tally[4].structured_iterate_hex("xyz"), expected_sm_4.structured_iterate_hex("xyz"), ): written = meshtal_object.tally[4].n_rel_error[v_e] expected = expected_sm_4.n_rel_error[expected_v_e] assert_array_equal(written, expected) for v_e, expected_v_e in zip( meshtal_object.tally[4].structured_iterate_hex("xyz"), expected_sm_4.structured_iterate_hex("xyz"), ): written = meshtal_object.tally[4].n_total_result[v_e] expected = expected_sm_4.n_total_result[expected_v_e] assert_equal(written, expected) for v_e, expected_v_e in zip( meshtal_object.tally[4].structured_iterate_hex("xyz"), expected_sm_4.structured_iterate_hex("xyz"), ): written = meshtal_object.tally[4].n_total_rel_error[v_e] expected = expected_sm_4.n_total_rel_error[expected_v_e] assert_equal(written, expected) # test meshtally 14 for v_e, expected_v_e in zip( meshtal_object.tally[14].structured_iterate_hex("xyz"), expected_sm_14.structured_iterate_hex("xyz"), ): written = meshtal_object.tally[14].n_result[v_e] expected = expected_sm_14.n_result[expected_v_e] assert_array_equal(written, expected) for v_e, expected_v_e in zip( meshtal_object.tally[14].structured_iterate_hex("xyz"), expected_sm_14.structured_iterate_hex("xyz"), ): written = meshtal_object.tally[14].n_rel_error[v_e] expected = expected_sm_14.n_rel_error[expected_v_e] assert_array_equal(written, expected) # test meshtally 24 for v_e, expected_v_e in zip( meshtal_object.tally[24].structured_iterate_hex("xyz"), expected_sm_24.structured_iterate_hex("xyz"), ): written = meshtal_object.tally[24].p_result[v_e] expected = expected_sm_24.p_result[expected_v_e] assert_array_equal(written, expected) for v_e, expected_v_e in zip( meshtal_object.tally[24].structured_iterate_hex("xyz"), expected_sm_24.structured_iterate_hex("xyz"), ): written = meshtal_object.tally[24].p_rel_error[v_e] expected = expected_sm_24.p_rel_error[expected_v_e] assert_array_equal(written, expected) for v_e, expected_v_e in zip( meshtal_object.tally[24].structured_iterate_hex("xyz"), expected_sm_24.structured_iterate_hex("xyz"), ): written = meshtal_object.tally[24].p_total_result[v_e] expected = expected_sm_24.p_total_result[expected_v_e] assert_equal(written, expected) for v_e, expected_v_e in zip( meshtal_object.tally[24].structured_iterate_hex("xyz"), expected_sm_24.structured_iterate_hex("xyz"), ): written = meshtal_object.tally[24].p_total_rel_error[v_e] expected = expected_sm_24.p_total_rel_error[expected_v_e] assert_equal(written, expected) # test meshtally 34 for v_e, expected_v_e in zip( meshtal_object.tally[34].structured_iterate_hex("xyz"), expected_sm_34.structured_iterate_hex("xyz"), ): written = meshtal_object.tally[34].p_result[v_e] expected = expected_sm_34.p_result[expected_v_e] assert_array_equal(written, expected) for v_e, expected_v_e in zip( meshtal_object.tally[34].structured_iterate_hex("xyz"), expected_sm_34.structured_iterate_hex("xyz"), ): written = meshtal_object.tally[34].p_rel_error[v_e] expected = expected_sm_34.p_rel_error[expected_v_e] assert_array_equal(written, expected)
def test_write_fluxin_multiple_subvoxel(): """This function tests the flux_mesh_to_fluxin function for a multiple energy group case under sub-voxel r2s. """ if not HAVE_PYMOAB: raise SkipTest output_name = "fluxin_subvoxel.out" forward_fluxin = os.path.join(thisdir, "files_test_alara", "fluxin_multiple_forward_subvoxel.txt") reverse_fluxin = os.path.join(thisdir, "files_test_alara", "fluxin_multiple_reverse_subvoxel.txt") output = os.path.join(os.getcwd(), output_name) flux_mesh = Mesh(structured=True, structured_coords=[[0, 1, 2], [0, 1, 2], [0, 1]]) flux_data = [ [1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28], ] flux_mesh.tag("flux", flux_data, "nat_mesh", size=7, dtype=float) cell_fracs = np.zeros( 6, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_fracs[:] = [ (0, 11, 1, 0.0), (1, 11, 0.5, 0.0), (1, 12, 0.5, 0.0), (2, 11, 0.5, 0.0), (2, 13, 0.5, 0.0), (3, 13, 1, 0.0), ] cell_mats = { 11: Material({"H": 1.0}, density=1.0), 12: Material({"He": 1.0}, density=1.0), 13: Material({}, density=0.0, metadata={"name": "void"}), } # test forward writting mesh_to_fluxin(flux_mesh, "flux", output_name, False, True, cell_fracs, cell_mats) with open(output) as f: written = f.readlines() with open(forward_fluxin) as f: expected = f.readlines() assert_equal(written, expected) if os.path.isfile(output): os.remove(output) # test reverse writting mesh_to_fluxin(flux_mesh, "flux", output_name, True, True, cell_fracs, cell_mats) with open(output) as f: written = f.readlines() with open(reverse_fluxin) as f: expected = f.readlines() assert_equal(written, expected) if os.path.isfile(output): os.remove(output)
def test_mesh_to_geom(): if not HAVE_PYMOAB: raise SkipTest 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), 4: Material({"C12": 1.0}, density=47.0), 5: Material({"1002": 1.0}, density=5.0), } m = Mesh( structured_coords=[[0, 1, 2, 3], [0, 1, 2], [0, 1]], mats=mats, structured=True ) geom = mcnp.mesh_to_geom(m) exp_geom = ( "Generated from PyNE Mesh\n" "1 1 42.0 1 -2 5 -6 8 -9\n" "2 2 43.0 1 -2 6 -7 8 -9\n" "3 3 44.0 2 -3 5 -6 8 -9\n" "4 4 45.0 2 -3 6 -7 8 -9\n" "5 5 47.0 3 -4 5 -6 8 -9\n" "6 6 5.0 3 -4 6 -7 8 -9\n" "7 0 -1:4:-5:7:-8:9\n" "\n" "1 px 0.0\n" "2 px 1.0\n" "3 px 2.0\n" "4 px 3.0\n" "5 py 0.0\n" "6 py 1.0\n" "7 py 2.0\n" "8 pz 0.0\n" "9 pz 1.0\n" "\n" "C name: 0\n" "C density = 42.00000\n" "m1\n" " 1001 -2.1000e+01\n" " 19039 -2.1000e+01\n" "C name: 1\n" "C density = 43.00000\n" "m2\n" " 1001 -3.9091e+00\n" " 8016 -3.9091e+01\n" "C name: 2\n" "C density = 44.00000\n" "m3\n" " 2004 -4.4000e+01\n" "C name: 3\n" "C density = 45.00000\n" "m4\n" " 69171 -4.5000e+01\n" "C name: 4\n" "C density = 47.00000\n" "m5\n" " 6012 -4.7000e+01\n" "C name: 5\n" "C density = 5.00000\n" "m6\n" " 1002 -5.0000e+00\n" ) assert_equal(geom, exp_geom)
def test_record_to_geom_subvoxel(): if not HAVE_PYMOAB: raise SkipTest expected_geom = os.path.join(thisdir, "files_test_alara", "alara_record_geom_subvoxel.txt") expected_matlib = os.path.join(thisdir, "files_test_alara", "alara_record_matlib_subvoxel.txt") geom = os.path.join(os.getcwd(), "alara_record_geom") matlib = os.path.join(os.getcwd(), "alara_record_matlib") cell_fracs = np.zeros( 11, dtype=[ ("idx", np.int64), ("cell", np.int64), ("vol_frac", np.float64), ("rel_error", np.float64), ], ) cell_mats = { 11: Material({ "H1": 1.0, "K39": 1.0 }, density=1.1, metadata={"name": "fake_mat"}), 12: Material({ "H1": 0.1, "O16": 1.0 }, density=1.2, metadata={"name": "water"}), 13: Material({"He4": 42.0}, density=1.3, metadata={"name": "helium"}), 14: Material({}, density=0.0, metadata={"name": "void"}), 15: Material({}, density=0.0, metadata={"name": "void"}), 16: Material({ "H1": 1.0, "K39": 1.0 }, density=1.1, metadata={"name": "fake_mat"}), } cell_fracs[:] = [ (0, 11, 0.55, 0.0), (0, 12, 0.45, 0.0), (1, 11, 0.2, 0.0), (1, 12, 0.3, 0.0), (1, 13, 0.5, 0.0), (2, 11, 0.15, 0.0), (2, 14, 0.01, 0.0), (2, 15, 0.04, 0.0), (2, 16, 0.8, 0.0), (3, 11, 0.55, 0.0), (3, 12, 0.45, 0.0), ] m = Mesh(structured_coords=[[-1, 0, 1], [-1, 0, 1], [0, 1]], structured=True, mats=None) record_to_geom(m, cell_fracs, cell_mats, geom, matlib, sub_voxel=True) assert filecmp.cmp(geom, expected_geom) if os.path.isfile(geom): os.remove(geom) assert filecmp.cmp(matlib, expected_matlib) if os.path.isfile(matlib): os.remove(matlib)
def test_irradiation_setup_unstructured(): flux_tag = "n_flux" meshtal_file = os.path.join(thisdir, "files_test_r2s", "meshtal_2x2x1") meshtal = Meshtal( meshtal_file, { 4: (flux_tag, flux_tag + "_err", flux_tag + "_total", flux_tag + "_err_total") }) # Explicitly make this mesh unstructured, it will now iterate in yxz # order which is MOAB structured mesh creation order. meshtal = Mesh(structured=False, mesh=meshtal.tally[4].mesh) meshtal_mesh_file = os.path.join(thisdir, "meshtal.h5m") meshtal.mesh.save(meshtal_mesh_file) cell_mats = { 2: Material({2004: 1.0}, density=1.0), 3: Material({ 3007: 0.4, 3006: 0.6 }, density=2.0) } alara_params = "Bogus line for testing" geom = os.path.join(thisdir, "unitbox.h5m") flux_tag = "n_flux" fluxin = os.path.join(os.getcwd(), "alara_fluxin") reverse = True alara_inp = os.path.join(os.getcwd(), "alara_inp") alara_matlib = os.path.join(os.getcwd(), "alara_matlib") output_mesh = os.path.join(os.getcwd(), "r2s_step1.h5m") irradiation_setup(flux_mesh=meshtal_mesh_file, cell_mats=cell_mats, alara_params=alara_params, geom=geom, flux_tag=flux_tag, fluxin=fluxin, reverse=reverse, alara_inp=alara_inp, alara_matlib=alara_matlib, output_mesh=output_mesh) # expected output files exp_alara_inp = os.path.join(thisdir, "files_test_r2s", "exp_alara_inp") exp_alara_matlib = os.path.join(thisdir, "files_test_r2s", "exp_alara_matlib_un") exp_fluxin = os.path.join(thisdir, "files_test_r2s", "exp_fluxin_un") # test alara input file with open(alara_inp, 'r') as f1, open(exp_alara_inp, 'r') as f2: for a, b in zip(f1.readlines(), f2.readlines()): assert_equal(a, b) # test alara matlibe file with open(alara_matlib, 'r') as f1, open(exp_alara_matlib) as f2: for a, b in zip(f1.readlines(), f2.readlines()): assert_equal(a, b) # test alara fluxin file with open(fluxin, 'r') as f1, open(exp_fluxin) as f2: for a, b in zip(f1.readlines(), f2.readlines()): assert_equal(a, b) # test r2s step 1 output mesh fluxes = [[6.93088E-07, 1.04838E-06], [6.36368E-07, 9.78475E-07], [5.16309E-07, 9.86586E-07], [6.36887E-07, 9.29879E-07]] errs = [[9.67452E-02, 7.55950E-02], [9.88806E-02, 7.61482E-02], [1.04090E-01, 7.69284E-02], [9.75826E-02, 7.54181E-02]] tot_fluxes = [1.74147E-06, 1.61484E-06, 1.50290E-06, 1.56677E-06] tot_errs = [6.01522E-02, 6.13336E-02, 6.19920E-02, 5.98742E-02] m = Mesh(structured=True, mesh=output_mesh, mats=output_mesh) for i, mat, _ in m: assert_almost_equal(mat.density, 2.0) assert_equal(len(mat.comp), 2) assert_almost_equal(mat.comp[30060000], 0.6) assert_almost_equal(mat.comp[30070000], 0.4) assert_array_equal(m.n_flux[i], fluxes[i]) assert_array_equal(m.n_flux_err[i], errs[i]) assert_almost_equal(m.n_flux_total[i], tot_fluxes[i]) assert_almost_equal(m.n_flux_err_total[i], tot_errs[i]) os.remove(meshtal_mesh_file) os.remove(alara_inp) os.remove(alara_matlib) os.remove(fluxin) os.remove(output_mesh)