def test_expand_elements2(): """Inspired by #86""" natmat = Material({'C': 1.0}) expmat = natmat.expand_elements() afrac = expmat.to_atom_frac() assert_almost_equal(data.natural_abund(60120000), afrac[60120000]) assert_almost_equal(data.natural_abund(60130000), afrac[60130000])
def test_grab_materials_compendium(): mats = grab_materials_compendium("../pyne/dbgen/materials_compendium.csv") assert len(mats) == 372 # this tests a material where we don't do any element expansion a150tep_comp = mats["A-150 Tissue-Equivalent Plastic (A150TEP)"].comp expected_mat = Material({ "H": 0.101327, "C": 0.775501, "N": 0.035057, "O": 0.052316, "F": 0.017422, "Ca": 0.018378, }) expected_mat.normalize() expected_mat = expected_mat.expand_elements() for key, value in expected_mat.comp.items(): assert_close(a150tep_comp[key], value) # this tests a material where we do do element expansion pubr = mats["Plutonium Bromide"] bromium = sum((frac for nuc, frac in pubr.comp.items() if nucname.zzzaaa(nuc) // 1000 == 35)) assert_close(bromium, 0.500617) assert_close(pubr[942380000], 0.000250)
def test_multimaterial_mix_density(): mat1 = Material(nucvec={ 120240000: 0.3, 300000000: 0.2, 10010000: 0.1 }, density=1.0) mat2 = Material(nucvec={ 60120000: 0.2, 280640000: 0.5, 10010000: 0.12 }, density=2.0) # mixing by hand to get density 1.5 when 50:50 by volume of density 1 & 2 mix = MultiMaterial({mat1: 0.5, mat2: 0.5}) mat3 = mix.mix_by_volume() # calculated mass fracs by hand, same problem as above stated in mass fraction terms # rather than volume fraction terms. mix = MultiMaterial({mat1: 1 / 3., mat2: 2 / 3.}) mat4 = mix.mix_by_mass() assert_equal(mat3.density, 1.5) assert_equal(mat4.density, 1.5) assert_equal(mat3.density, mat4.density)
def test_comptag(): 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) def d2(mesh, i): """I square the density.""" return mesh.density[i]**2 m.density2 = ComputedTag(d2, m, 'density2') # Getting tags assert_equal(m.density2[0], 42.0**2) assert_array_equal(m.density2[::2], np.array([42.0, 44.0])**2) mask = np.array([True, False, True, True], dtype=bool) assert_array_equal(m.density2[mask], np.array([42.0, 44.0, 45.0])**2) assert_array_equal(m.density2[1, 0, 1, 3], np.array([43.0, 42.0, 43.0, 45.0])**2)
def test_read_mcnp_wcomments(): expected_material = Material(nucvec={ 922350000: 0.04, 922380000: 0.96 }, mass=-1.0, density=19.1, metadata={ "comments": (" first line of comments second line of " "comments third line of comments forth " "line of comments"), "mat_number": "1", "name": " leu", "source": " Some http://URL.com", "table_ids": { '922350': "15c" } }) expected_material.mass = -1.0 # to avoid reassignment to +1.0 read_materials = mats_from_inp('mcnp_inp_comments.txt') assert_equal(expected_material, read_materials[1])
def test_setitem_sequence(): mat = Material(nucvec) mat_id = id(mat) mat[922380, 922350] = 42 assert_equal(mat_id, id(mat)) assert_equal(mat.mass, 91.0) assert_equal(mat[10010], 1.0) assert_equal(mat[922350], 42.0) assert_equal(mat[922380], 42.0) mat = Material(nucvec) mat[922380, 'H2', 'h1'] = 0.0 assert_equal(mat.mass, 7.0) assert_equal(len(mat), 10) for nuc in mat: if (nuc in [10010, 10020, 922380]): assert_equal(mat[nuc], 0.0) else: assert_equal(mat[nuc], 1.0) mat = Material(nucvec) mat['U235', 'H2', 'h1'] = 2 assert_equal(mat.mass, 13.0) assert_equal(len(mat), 10) for nuc in mat: if (nuc in [10010, 10020, 922350]): assert_equal(mat[nuc], 2.0) else: assert_equal(mat[nuc], 1.0)
def test_matlib(): mats = { 0: Material({ 'H1': 1.0, 'K39': 1.0 }, density=1.1), 1: Material({ 'H1': 0.1, 'O16': 1.0 }, density=2.2), 2: Material({'He4': 42.0}, density=3.3), 3: Material({'Tm171': 171.0}, density=4.4), } m = gen_mesh(mats=mats) for i, ve in enumerate( m.mesh.iterate(iBase.Type.region, iMesh.Topology.all)): assert_is(m.mats[i], mats[i]) assert_equal(m.mesh.getTagHandle('idx')[ve], i) m.write_hdf5('test_matlib.h5m') shutil.copy('test_matlib.h5m', 'test_matlib2.h5m') m2 = Mesh(mesh='test_matlib2.h5m') # MOAB fails to flush for i, mat, ve in m2: assert_equal(len(mat.comp), len(mats[i].comp)) for key in mats[i].iterkeys(): assert_equal(mat.comp[key], mats[i].comp[key]) assert_equal(mat.density, mats[i].density) assert_equal(m2.idx[i], i)
def test_multi_material(): mat1 = Material(nucvec={ 120240: 0.3, 300000: 0.2, 10010: 0.1 }, density=2.71) mat2 = Material(nucvec={60120: 0.2, 280640: 0.5, 10010: 0.12}, density=8.0) mix = MultiMaterial({mat1: 0.5, mat2: 0.21}) mat3 = mix.mix_by_mass() mat4 = mix.mix_by_volume() assert_equal(mat3.density, -1.0) assert_equal(mat3.comp[10010], 0.16065498683155846) assert_equal(mat3.comp[60120], 0.0721401580212985) assert_equal(mat3.comp[120240], 0.352112676056338) assert_equal(mat3.comp[280640], 0.18035039505324627) assert_equal(mat3.comp[300000], 0.2347417840375587) assert_equal(mat4.density, -1.0) assert_equal(mat4.comp[10010], 0.15541581280722197) assert_equal(mat4.comp[60120], 0.13501024631333625) assert_equal(mat4.comp[120240], 0.2232289950576606) assert_equal(mat4.comp[280640], 0.33752561578334067) assert_equal(mat4.comp[300000], 0.14881933003844042)
def run_nuclide(nuc): bateman = defaultdict(emptytime) bateman[nuc][0] = 1 crammed = defaultdict(emptytime) crammed[nuc][0] = 1 diagexp = defaultdict(emptytime) diagexp[nuc][0] = 1 n0 = Material({nuc: 1.0}, mass=1.0, atoms_per_molecule=1.0) for i, t in enumerate(TIMES[1:], 1): # compute Bateman try: b1 = n0.decay(t).to_atom_frac() except RuntimeError: # decay can't handle all of the same nuclides CRAM can b1 = {} for key, val in b1.items(): n = nucname.name(key) bateman[n][i] = val # compute CRAM c1 = n0.cram(DECAY_MATS[t], order=16).to_atom_frac() for key, val in c1.items(): n = nucname.name(key) crammed[n][i] = val # compute e^x of the diagonal of the decay matrix, ie the nuc itself nuc_idx = cram.NUCS_IDX[nuc] mat_idx = cram.IJ[nuc_idx, nuc_idx] diagexp[nuc][i] = exp(-DECAY_MATS[t][mat_idx]).evalf(n=30) return bateman, crammed, diagexp
def test_ma_2(self): mat = Material(nucvec) mat1 = mat.sub_ma("MA Material") assert_equal(mat1.comp[952420], 1.0/2.0) assert_equal(mat1.comp[962440], 1.0/2.0) assert_equal(mat1.mass, 2.0) assert_equal(mat1.name, 'MA Material')
def test_matlib(): mats = { 0: Material({ 'H1': 1.0, 'K39': 1.0 }, density=1.1), 1: Material({ 'H1': 0.1, 'O16': 1.0 }, density=2.2), 2: Material({'He4': 42.0}, density=3.3), 3: Material({'Tm171': 171.0}, density=4.4), } m = gen_mesh(mats=mats) for i, ve in enumerate(mesh_iterate(m.mesh)): assert_is(m.mats[i], mats[i]) assert_equal( m.mesh.tag_get_data(m.mesh.tag_get_handle('idx'), ve, flat=True)[0], i) m.write_hdf5('test_matlib.h5m') shutil.copy('test_matlib.h5m', 'test_matlib2.h5m') m2 = Mesh(mesh='test_matlib2.h5m') # MOAB fails to flush for i, mat, ve in m2: assert_equal(len(mat.comp), len(mats[i].comp)) for key in mats[i].iterkeys(): assert_equal(mat.comp[key], mats[i].comp[key]) assert_equal(mat.density, mats[i].density) assert_equal(m2.idx[i], i)
def test_setitem_slice_str(): mat = Material(nucvec) mat['U':'Np'] = 42 assert_equal(mat.mass, 91.0) assert_equal(mat[10010], 1.0) assert_equal(mat[922350], 42.0) assert_equal(mat[922380], 42.0) mat = Material(nucvec) mat[:'U238'] = 0.0 assert_equal(mat.mass, 5.0) for nuc in mat: if (nuc < 922380): assert_equal(mat[nuc], 0.0) else: assert_equal(mat[nuc], 1.0) mat = Material(nucvec) mat['U235':] = 2 assert_equal(mat.mass, 15.0) for nuc in mat: if (922350 <= nuc): assert_equal(mat[nuc], 2.0) else: assert_equal(mat[nuc], 1.0)
def test_iter(): mats = { 0: Material({ 'H1': 1.0, 'K39': 1.0 }, density=42.0, metadata={'mat_number': 1}), 1: Material({ 'H1': 0.1, 'O16': 1.0 }, density=43.0, metadata={'mat_number': 2}), 2: Material({'He4': 42.0}, density=44.0, metadata={'mat_number': 3}), 3: Material({'Tm171': 171.0}, density=45.0, metadata={'mat_number': 4}), } m = gen_mesh(mats=mats) j = 0 idx_tag = m.mesh.tag_get_handle('idx') for i, mat, ve in m: assert_equal(j, i) assert_equal(mats[i], mat) assert_equal(j, m.mesh.tag_get_data(idx_tag, ve, flat=True)[0]) j += 1
def test_fp_2(self): mat = Material(nucvec) mat1 = mat.sub_fp() assert_equal(mat1.comp[10010], 1.0 / 3.0) assert_equal(mat1.comp[80160], 1.0 / 3.0) assert_equal(mat1.comp[691690], 1.0 / 3.0) assert_equal(mat1.mass, 3.0)
def test_setitem_slice_int(): mat = Material(nucvec) mat_id = id(mat) mat[920000:930000] = 42 assert_equal(mat_id, id(mat)) assert_equal(mat.mass, 91.0) assert_equal(mat[10010], 1.0) assert_equal(mat[922350], 42.0) assert_equal(mat[922380], 42.0) mat = Material(nucvec) mat[:922380] = 0.0 assert_equal(mat.mass, 5.0) for nuc in mat: if (nuc < 922380): assert_equal(mat[nuc], 0.0) else: assert_equal(mat[nuc], 1.0) mat = Material(nucvec) mat[922350:] = 2 assert_equal(mat.mass, 15.0) for nuc in mat: if (922350 <= nuc): assert_equal(mat[nuc], 2.0) else: assert_equal(mat[nuc], 1.0)
class TestMaterialOperatorOverloading(TestCase): "Tests that the Material operator overloads work." u235 = Material({922350: 1.0}) u238 = Material({922380: 1.0}) def test_add_num(self): mat = self.u235 + 30.0 assert_equal(mat.mass, 31.0) def test_radd_num(self): mat = 90 + self.u235 assert_equal(mat.mass, 91.0) def test_add_mat(self): mat = self.u235 + self.u238 assert_equal(mat.comp, {922350: 0.5, 922380: 0.5}) assert_equal(mat.mass, 2.0) def test_mul_num(self): mat = self.u235 * 2.0 assert_equal(mat.mass, 2.0) def test_rmul_num(self): mat = 150 * self.u235 assert_equal(mat.mass, 150.0) def test_div_num(self): mat = self.u235 / 10 assert_equal(mat.mass, 0.1)
def test_against_nuc_data(): nuc_data = pyne_conf.NUC_DATA_PATH if not os.path.isfile(nuc_data): raise RuntimeError("Tests require nuc_data.h5. Please run nuc_data_make.") obs_matslib = MaterialLibrary(nuc_data, datapath="/material_library/materials", nucpath="/material_library/nucid") gasoline = Material({ "H": 0.157000, "C": 0.843000, }, density=0.721, metadata={"name": "Gasoline"}).expand_elements() pubr3 = Material({ "Br": 0.500617, "Pu-238": 0.000250, "Pu-239": 0.466923, "Pu-240": 0.029963, "Pu-241": 0.001998, "Pu-242": 0.000250 }, density=6.75, metadata={"name": "Plutonium Bromide"}).expand_elements() obs_gasoline = obs_matslib["Gasoline"] assert_equal(set(obs_gasoline.comp.items()), set(gasoline.comp.items())) assert_equal(obs_gasoline.density, gasoline.density) assert_equal(obs_gasoline.metadata["name"], gasoline.metadata["name"]) obs_pubr3 = obs_matslib["Plutonium Bromide"] assert_equal(set(obs_pubr3.comp.items()), set(pubr3.comp.items())) assert_equal(obs_pubr3.density, pubr3.density) assert_equal(obs_pubr3.metadata["name"], pubr3.metadata["name"])
def test_sub_mat_int_2(self): mat = Material(nucvec) mat1 = mat.sub_mat([922350, 922380, 80160]) assert_almost_equal(mat1.comp[80160], 0.3333333333333) assert_almost_equal(mat1.comp[922350], 0.3333333333333) assert_almost_equal(mat1.comp[922380], 0.3333333333333) assert_equal(mat1.mass, 3.0)
def test_fp_2(self): mat = Material(nucvec) mat1 = mat.sub_fp() assert_equal(mat1.comp[10010000], 1.0/3.0) assert_equal(mat1.comp[80160000], 1.0/3.0) assert_equal(mat1.comp[691690000], 1.0/3.0) assert_equal(mat1.mass, 3.0)
def test_delitem_slice_int(): mat = Material(nucvec) del mat[920000:930000] assert_equal(mat.mass, 7.0) assert_equal(mat[10010], 1.0) assert_raises(KeyError, lambda: mat[922350]) assert_raises(KeyError, lambda: mat[922380]) mat = Material(nucvec) del mat[:922380] assert_equal(mat.mass, 5.0) for nuc in mat: if (nuc < 922380): assert_raises(KeyError, lambda: mat[nuc]) else: assert_equal(mat[nuc], 1.0) mat = Material(nucvec) del mat[922350:] assert_equal(mat.mass, 3.0) for nuc in mat: if (922350 <= nuc): assert_raises(KeyError, lambda: mat[nuc]) else: assert_equal(mat[nuc], 1.0)
def test_delitem_slice_str(): mat = Material(nucvec) del mat['U':'Np'] assert_equal(mat.mass, 7.0) assert_equal(mat[10010], 1.0) assert_raises(KeyError, lambda: mat[922350]) assert_raises(KeyError, lambda: mat[922380]) mat = Material(nucvec) del mat[:'U238'] assert_equal(mat.mass, 5.0) for nuc in mat: if (nuc < 922380): assert_raises(KeyError, lambda: mat[nuc]) else: assert_equal(mat[nuc], 1.0) mat = Material(nucvec) del mat['U235':] assert_equal(mat.mass, 3.0) for nuc in mat: if (922350 <= nuc): assert_raises(KeyError, lambda: mat[nuc]) else: assert_equal(mat[nuc], 1.0)
def test_sub_mat_int_2(self): mat = Material(nucvec) mat1 = mat.sub_mat([922350000, 922380000, 80160000]) assert_almost_equal(mat1.comp[80160000], 0.3333333333333) assert_almost_equal(mat1.comp[922350000], 0.3333333333333) assert_almost_equal(mat1.comp[922380000], 0.3333333333333) assert_equal(mat1.mass, 3.0)
def test_mcnp(): leu = Material(nucvec={'U235': 0.04, 'U238': 0.96}, metadata={'mat_number': 2, 'table_ids': {'92235':'15c', '92238':'25c'}, 'mat_name':'LEU', 'source':'Some URL', 'comments': ('this is a long comment that will definitly ' 'go over the 80 character limit, for science'), 'name':'leu'}, density=19.1) mass = leu.mcnp() mass_exp = ('C name: leu\n' 'C density = 19.1\n' 'C source: Some URL\n' 'C comments: this is a long comment that will definitly go over the 80 character\n' 'C limit, for science\n' 'm2\n' ' 92235.15c -4.0000E-02\n' ' 92238.25c -9.6000E-01\n') assert_equal(mass, mass_exp) atom = leu.mcnp(frac_type='atom') atom_exp = ('C name: leu\n' 'C density = 19.1\n' 'C source: Some URL\n' 'C comments: this is a long comment that will definitly go over the 80 character\n' 'C limit, for science\n' 'm2\n' ' 92235.15c 4.0491E-02\n' ' 92238.25c 9.5951E-01\n') assert_equal(atom, atom_exp)
def test_set_mat_int_2(): mat = Material(nucvec) mat1 = mat.set_mat([922350000, 922380000, 80160000], 2) comp = 2. / (nclides + 3.) assert_almost_equal(mat1.comp[80160000], comp) assert_almost_equal(mat1.comp[922350000], comp) assert_almost_equal(mat1.comp[922380000], comp) assert_equal(mat1.mass, nclides + 3)
def test_expand_elements1(): natmat = Material({'C': 1.0, 902320000: 0.5, 'PU': 4.0, 'U': 3.0}, metadata={'y': 1.0}) expmat = natmat.expand_elements() assert_true(60120000 in expmat.comp) assert_false(60000000 in expmat.comp) assert_true(natmat.metadata == expmat.metadata) assert_false(natmat.metadata is expmat.metadata)
def test_load_json(): leu = {"U238": 0.96, "U235": 0.04} exp = Material(leu) obs = Material() json = jsoncpp.Value({"mass": 1.0, "comp": leu, "density": -1.0, "metadata": {}, "atoms_per_molecule": -1.0}) obs.load_json(json) assert_equal(exp, obs)
def test_sub_mat_int_2(self): mat = Material(nucvec) mat1 = mat.sub_mat([922350, 922380, 80160], "New Material") assert_almost_equal(mat1.comp[80160], 0.3333333333333) assert_almost_equal(mat1.comp[922350], 0.3333333333333) assert_almost_equal(mat1.comp[922380], 0.3333333333333) assert_equal(mat1.mass, 3.0) assert_equal(mat1.name, 'New Material')
def test_sub_mat_attr_2(self): mat = Material(nucvec) mat1 = mat.sub_mat(["U235", "U238", "80160", "H1"]) assert_almost_equal(mat1.comp[10010000], 0.25) assert_almost_equal(mat1.comp[80160000], 0.25) assert_almost_equal(mat1.comp[922350000], 0.25) assert_almost_equal(mat1.comp[922380000], 0.25) assert_equal(mat1.mass, 4.0)
def test_fp_2(self): mat = Material(nucvec) mat1 = mat.sub_fp("FP Material") assert_equal(mat1.comp[10010], 1.0/3.0) assert_equal(mat1.comp[80160], 1.0/3.0) assert_equal(mat1.comp[691690], 1.0/3.0) assert_equal(mat1.mass, 3.0) assert_equal(mat1.name, 'FP Material')
def test_tru_2(self): mat = Material(nucvec) mat1 = mat.sub_tru() assert_equal(mat1.comp[942390000], 1.0/4.0) assert_equal(mat1.comp[942410000], 1.0/4.0) assert_equal(mat1.comp[952420000], 1.0/4.0) assert_equal(mat1.comp[962440000], 1.0/4.0) assert_equal(mat1.mass, 4.0)
def test_sub_mat_int_1(self): mat = Material(nucvec, -1, "Old Material") mat1 = mat.sub_mat([922350, 922380, 80160]) assert_almost_equal(mat1.comp[80160], 0.3333333333333) assert_almost_equal(mat1.comp[922350], 0.3333333333333) assert_almost_equal(mat1.comp[922380], 0.3333333333333) assert_equal(mat1.mass, 3.0) assert_equal(mat1.name, '')
def test_to_atom_frac(): h2o = {10010000: 0.11191487328808077, 80160000: 0.8880851267119192} mat = Material(h2o, atoms_per_molecule=3.0) af = mat.to_atom_frac() assert_equal(mat.atoms_per_molecule, 3.0) assert_equal(af[10010000], 2.0) assert_equal(af[80160000], 1.0) assert_equal(mat.molecular_mass(), 18.01056468403)
def test_tru_2(self): mat = Material(nucvec) mat1 = mat.sub_tru() assert_equal(mat1.comp[942390], 1.0 / 4.0) assert_equal(mat1.comp[942410], 1.0 / 4.0) assert_equal(mat1.comp[952420], 1.0 / 4.0) assert_equal(mat1.comp[962440], 1.0 / 4.0) assert_equal(mat1.mass, 4.0)
def test_to_atom_frac(): h2o = {10010: 0.11191487328808077, 80160: 0.8880851267119192} mat = Material(h2o, atoms_per_mol=3.0) af = mat.to_atom_frac() assert_equal(mat.atoms_per_mol, 3.0) assert_equal(af[10010], 2.0) assert_equal(af[80160], 1.0) assert_equal(mat.molecular_weight(), 18.01056468403)
def test_sub_mat_attr_2(self): mat = Material(nucvec) mat1 = mat.sub_mat(["U235", "U238", "80160", "H1"]) assert_almost_equal(mat1.comp[10010], 0.25) assert_almost_equal(mat1.comp[80160], 0.25) assert_almost_equal(mat1.comp[922350], 0.25) assert_almost_equal(mat1.comp[922380], 0.25) assert_equal(mat1.mass, 4.0)
def __init__(self, mass_msol, composition): self.mass_g = mass_msol * msun_to_cgs self.material = Material(self._normalize_composition(composition)) self._pad_material() atomic_masses = self.get_masses() self.n_per_g = [ 1 / atomic_masses[item] for item in self.get_all_children_nuc_name() ]
def test_getitem_str(): mat = Material(nucvec) assert_equal(mat['U235'], 1.0) assert_raises(RuntimeError, lambda: mat['word']) mat = Material(leu) assert_equal(mat['U235'], 0.04) assert_equal(mat['U238'], 0.96) assert_raises(KeyError, lambda: mat['U234'])
def test_getitem_int(): mat = Material(nucvec) assert_equal(mat[922350], 1.0) assert_raises(KeyError, lambda: mat[42]) mat = Material(leu) assert_equal(mat[922350], 0.04) assert_equal(mat[922380], 0.96) assert_raises(KeyError, lambda: mat[922340])
def test_tru_2(self): mat = Material(nucvec) mat1 = mat.sub_tru("TRU Material") assert_equal(mat1.comp[942390], 1.0/4.0) assert_equal(mat1.comp[942410], 1.0/4.0) assert_equal(mat1.comp[952420], 1.0/4.0) assert_equal(mat1.comp[962440], 1.0/4.0) assert_equal(mat1.mass, 4.0) assert_equal(mat1.name, 'TRU Material')
def test_sub_mat_attr_1(self): mat = Material(nucvec, -1, "Old Material") mat1 = mat.sub_mat(["U235", "U238", "80160", "H1"]) assert_almost_equal(mat1.comp[10010], 0.25) assert_almost_equal(mat1.comp[80160], 0.25) assert_almost_equal(mat1.comp[922350], 0.25) assert_almost_equal(mat1.comp[922380], 0.25) assert_equal(mat1.mass, 4.0) assert_equal(mat1.name, '')
def test_natural_elements(): water = Material() water.from_atom_frac({10000000: 2.0, 80000000: 1.0}) expected_comp = { 10000000: 0.11189838783149784, 80000000: 0.8881016121685023 } for key in expected_comp: assert_almost_equal(water.comp[key], expected_comp[key])
def transmute(self, x, t=None, phi=None, tol=None, log=None, *args, **kwargs): """Transmutes a material into its daughters. Parameters ---------- x : Material or similar Input material for transmutation. t : float Transmutations time [sec]. phi : float or array of floats Neutron flux vector [n/cm^2/sec]. Currently this must either be a scalar or match the group structure of EAF. tol : float Tolerance level for chain truncation. log : file-like or None The log file object should be written. A None imples the log is not desired. Returns ------- y : Material The output material post-transmutation. """ if not isinstance(x, Material): x = Material(x) if t is not None: self.t = t if phi is not None: self.phi = phi if log is not None: self.log = log if tol is not None: self.tol = tol x_atoms = x.to_atom_frac() y_atoms = {} for nuc, adens in x_atoms.items(): # Find output for root of unit density and scale all output by # actual nuclide density and add to final output. partial = self._transmute_partial(nuc) for part_nuc, part_adens in partial.items(): y_atoms[part_nuc] = part_adens * adens + y_atoms.get( part_nuc, 0.0) mw_x = x.molecular_mass() y = from_atom_frac(y_atoms, atoms_per_molecule=x.atoms_per_molecule) # even though it doesn't look like it, the following line is actually # mass_y = MW_y * mass_x / MW_x y.mass *= x.mass / mw_x return y
def 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, 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") 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") output_material = True irradiation_setup(meshtal, cell_mats, alara_params, tally_num, geom, 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) 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(alara_inp) os.remove(alara_matlib) os.remove(fluxin) os.remove(output_mesh) return [m_out, f1, f2, f3]
def test_act_2(self): mat = Material(nucvec) mat1 = mat.sub_act() assert_equal(mat1.comp[922350], 1.0 / 6.0) assert_equal(mat1.comp[922380], 1.0 / 6.0) assert_equal(mat1.comp[942390], 1.0 / 6.0) assert_equal(mat1.comp[942410], 1.0 / 6.0) assert_equal(mat1.comp[952420], 1.0 / 6.0) assert_equal(mat1.comp[962440], 1.0 / 6.0) assert_equal(mat1.mass, 6.0)
def test_act_2(self): mat = Material(nucvec) mat1 = mat.sub_act() assert_equal(mat1.comp[922350000], 1.0/6.0) assert_equal(mat1.comp[922380000], 1.0/6.0) assert_equal(mat1.comp[942390000], 1.0/6.0) assert_equal(mat1.comp[942410000], 1.0/6.0) assert_equal(mat1.comp[952420000], 1.0/6.0) assert_equal(mat1.comp[962440000], 1.0/6.0) assert_equal(mat1.mass, 6.0)
def test_hdf5_protocol_1(): if 'proto1.h5' in os.listdir('.'): os.remove('proto1.h5') # Test material writing leu = Material({'U235': 0.04, 'U238': 0.96}, 4.2, "LEU", 1.0) leu.write_hdf5('proto1.h5', chunksize=10) for i in range(2, 11): leu = Material({'U235': 0.04, 'U238': 0.96}, i*4.2, "LEU", 1.0*i) leu.write_hdf5('proto1.h5') # Loads with protocol 1 now. m = Material() m.from_hdf5('proto1.h5', '/material', -3, 1) assert_equal(m.name, 'LEU') assert_equal(m.atoms_per_mol, 8.0) assert_equal(m.mass, 33.6) assert_equal(m.comp, {922350: 0.04, 922380: 0.96}) m = from_hdf5('proto1.h5', '/material', 3, 1) assert_equal(m.name, 'LEU') assert_equal(m.atoms_per_mol, 4.0) assert_equal(m.mass, 16.8) assert_equal(m.comp, {922350: 0.04, 922380: 0.96}) os.remove('proto1.h5')
def test_sub_range(self): mat = Material(nucvec) mat1 = mat.sub_range(920000000, 930000000) assert_equal(mat1.mass, 2.0) for nuc in mat1: assert_true(920000000 <= nuc < 930000000) mat1 = mat.sub_range(upper="U238") assert_equal(mat1.mass, 4.0) for nuc in mat1: assert_true(nuc < 922380000)
def test_act_2(self): mat = Material(nucvec) mat1 = mat.sub_act("ACT Material") assert_equal(mat1.comp[922350], 1.0/6.0) assert_equal(mat1.comp[922380], 1.0/6.0) assert_equal(mat1.comp[942390], 1.0/6.0) assert_equal(mat1.comp[942410], 1.0/6.0) assert_equal(mat1.comp[952420], 1.0/6.0) assert_equal(mat1.comp[962440], 1.0/6.0) assert_equal(mat1.mass, 6.0) assert_equal(mat1.name, 'ACT Material')
def test_iter(): mat = Material(nucvec) for nuc in mat: assert_equal(mat[nuc], 1.0) mat = Material(leu) keys = set([922350000, 922380000]) values = set([0.04, 0.96]) items = set([(922350000, 0.04), (922380000, 0.96)]) assert_equal(set(mat), keys) assert_equal(set(mat.values()), values) assert_equal(set(mat.items()), items)
def test_matlib_json(): filename = "matlib.json" water = Material() water.from_atom_frac({10000000: 2.0, 80000000: 1.0}) water.metadata["name"] = "Aqua sera." lib = {"leu": Material(leu), "nucvec": nucvec, "aqua": water} wmatlib = MaterialLibrary(lib) wmatlib.write_json(filename) rmatlib = MaterialLibrary() rmatlib.from_json(filename) assert_equal(set(wmatlib), set(rmatlib)) for key in rmatlib: assert_mat_almost_equal(wmatlib[key], rmatlib[key]) os.remove(filename)
def transmute(self, x, t=None, phi=None, tol=None, log=None, *args, **kwargs): """Transmutes a material into its daughters. Parameters ---------- x : Material or similar Input material for transmutation. t : float Transmutations time [sec]. phi : float or array of floats Neutron flux vector [n/cm^2/sec]. Currently this must either be a scalar or match the group structure of EAF. tol : float Tolerance level for chain truncation. log : file-like or None The log file object should be written. A None imples the log is not desired. Returns ------- y : Material The output material post-transmutation. """ if not isinstance(x, Material): x = Material(x) if t is not None: self.t = t if phi is not None: self.phi = phi if log is not None: self.log = log if tol is not None: self.tol = tol x_atoms = x.to_atom_frac() y_atoms = {} for nuc, adens in x_atoms.items(): # Find output for root of unit density and scale all output by # actual nuclide density and add to final output. partial = self._transmute_partial(nuc) for part_nuc, part_adens in partial.items(): y_atoms[part_nuc] = part_adens * adens + y_atoms.get(part_nuc, 0.0) mw_x = x.molecular_mass() y = from_atom_frac(y_atoms, atoms_per_molecule=x.atoms_per_molecule) # even though it doesn't look likt it, the following line is actually # mass_y = MW_y * mass_x / MW_x y.mass *= x.mass / mw_x return y
def test_metadata(): mat = Material(leu) assert_equal(len(mat.metadata), 0) mat.metadata['units'] = 'kg' assert_equal(len(mat.metadata), 1) assert_equal(mat.metadata['units'], 'kg') mat.metadata = {'comment': 'rawr', 'amount': 42.0} assert_equal(mat.metadata.keys(), ['amount', 'comment']) assert_true(isinstance(mat.metadata, jsoncpp.Value)) aview = mat.metadata aview['omnomnom'] = [1, 2, 5, 3] assert_equal(len(mat.metadata), 3) assert_equal(list(mat.metadata['omnomnom']), [1, 2, 5, 3])
def xs_gen(self, idx, nucs): """Runs the cross-section generation portion of char. idx : a list of perturbation indices that could be supplied to range() or slice(). nucs : a set of nuclides to run (zzaaam-form). """ nucs_in_serpent = (nucs & set(self.env['core_transmute_in_serpent'])) nucs_not_in_serpent = (nucs & set(self.env['core_transmute_not_in_serpent'])) # Loop over the perturbation steps for n in range(*idx): # Grab the Material at this time. ms_n = Material() ms_n.from_hdf5(self.env['reactor'] + ".h5", "/Ti0", n, protocol=0) # Calc restricted mass streams ms_n_in_serpent = ms_n[self.env['core_transmute_in_serpent']] ms_n_not_in_serpent = ms_n[self.env['core_transmute_not_in_serpent']] # Read in some common parameters from the data file with tb.openFile(self.env['reactor'] + ".h5", 'r') as rx_h5: E_g = np.array(rx_h5.root.energy[n][::-1]) E_n = np.array(rx_h5.root.hi_res.energy.read()[::-1]) phi_n = np.array(rx_h5.root.hi_res.phi_g[n][::-1]) # Run and write the high resolution flux if (phi_n < 0.0).all(): res, det = self.n_code.run_flux_g_pert(n, ms_n_in_serpent) self.n_code.write_flux_g(n, res, det) with tb.openFile(self.env['reactor'] + ".h5", 'r') as rx_h5: phi_n = np.array(rx_h5.root.hi_res.phi_g[n][::-1]) # # Loop over all output nuclides... # # ...that are valid in serpent for nuc in nucs_in_serpent: res, det = self.n_code.run_xs_gen_pert(nuc, n, ms_n_in_serpent, E_n, E_g, phi_n) self.n_code.write_xs_gen(nuc, n, res, det) # ...that are NOT valid in serpent for nuc in nucs_not_in_serpent: xsd = self.n_code.run_xs_mod_pert(nuc, n, E_n, E_g, phi_n) self.n_code.write_xs_mod(nuc, n, xsd)
def test_grab_materials_compendium(): mats = grab_materials_compendium('../pyne/dbgen/materials_compendium.csv') assert(len(mats) == 372) # this tests a material where we don't do any element expansion a150tep_comp = mats["A-150 Tissue-Equivalent Plastic (A150TEP)"].comp expected_mat = Material({"H": 0.101327, "C": 0.775501, "N": 0.035057, "O": 0.052316, "F": 0.017422, "Ca": 0.018378}) expected_mat.normalize() expected_mat = expected_mat.expand_elements() for key, value in expected_mat.comp.items(): assert_close(a150tep_comp[key], value) # this tests a material where we do do element expansion pubr = mats["Plutonium Bromide"] bromium = sum((frac for nuc, frac in pubr.comp.items() if nucname.zzzaaa(nuc) // 1000 == 35)) assert_close(bromium, 0.500617) assert_close(pubr[942380000], 0.000250)
def AtomicDensityToMassDensity(heu_atom): print "\n\n",heu_atom print "========heu_atom molecular mass ",heu_atom.molecular_mass() heu=Material() heu.from_atom_frac(heu_atom) print "\n\n",heu print "ERROR: heu molecular mass is not correct heu.molecular_mass() = ", heu.molecular_mass() heu.metadata=heu_atom.metadata heu.metadata['density_g_per_cc']=heu.mass heu.normalize() return heu
def make_materials_compendium(nuc_data): """Adds materials compendium to nuc_data.h5.""" # open nuc_data, make nuc_zz an array with tb.openFile(nuc_data, 'r+', filters=tb.Filters(complevel=5, complib='zlib', shuffle=True, fletcher32=False)) as f: f.createGroup('/', 'materials_library') f.createArray('/materials_library', 'nuc_zz', np.array(list(nuc_zz))) # Writes elements for which we have compositional data to file for zz in elemental_mats: if 0 == len(elemental_mats[zz]): continue element = Material(elemental_mats[zz], mass = 1.0, attrs = {'name':nucname.name(zz)}) element.write_hdf5(nuc_data, datapath="/materials_library/materials", nucpath="/materials_library/nuc_zz", chunksize=70) # Writes materials from mats to file, and names them. for i in range(len(mats)): mats[i].mass = 1.0 mats[i].density = float(densities[i]) mats[i].attrs = {'name': names[i]} mats[i].write_hdf5(nuc_data, datapath="/materials_library/materials", nucpath="/materials_library/nuc_zz", chunksize=70)
def _file_changed(self): ms = Material() rpart = self.file.rpartition('.') # HDF5 does not work! # Should launch a view to select group and row here row = None groupname = '' if (rpart[2] in ['h5', 'hdf5', 'H5', 'HDF5']) and (groupname != ''): if isinstance(row, int): ms.load_from_hdf5(str(self.file), groupname, row) else: ms_out.load_from_hdf5(str(self.file), groupname) elif (rpart[2] in ['txt', 'TXT']): ms.load_from_text(str(self.file)) self.update_from_material(ms) self.material = ms
def test_matlib_hdf5(): filename = "matlib.h5" if filename in os.listdir('.'): os.remove(filename) water = Material() water.from_atom_frac({10000000: 2.0, 80000000: 1.0}) water.metadata["name"] = "Aqua sera." lib = {"leu": Material(leu), "nucvec": nucvec, "aqua": water} wmatlib = MaterialLibrary(lib) wmatlib.write_hdf5(filename) rmatlib = MaterialLibrary() rmatlib.from_hdf5(filename) os.remove(filename) # Round trip! rmatlib.write_hdf5(filename) wmatlib = MaterialLibrary(filename) assert_equal(set(wmatlib), set(rmatlib)) for key in rmatlib: assert_mat_almost_equal(wmatlib[key], rmatlib[key]) os.remove(filename)