def _nuclei_density(field, data): ftype = field.name[0] element = field.name[1][:field.name[1].find("_")] nuclei_mass_field = "%s_nuclei_mass_density" % element if (ftype, nuclei_mass_field) in data.ds.field_info: return (data[(ftype, nuclei_mass_field)] / ChemicalFormula(element).weight / data.ds.units.physical_constants.amu_cgs) metal_field = "%s_metallicity" % element if (ftype, metal_field) in data.ds.field_info: return (data[ftype, "density"] * data[(ftype, metal_field)] / ChemicalFormula(element).weight / data.ds.units.physical_constants.amu_cgs) field_data = np.zeros_like(data[ftype, "%s_number_density" % data.ds.field_info.species_names[0]]) for species in data.ds.field_info.species_names: nucleus = species if "_" in species: nucleus = species[:species.find("_")] # num is the number of nuclei contributed by this species. num = _get_element_multiple(nucleus, element) # Since this is a loop over all species existing in this dataset, # we will encounter species that contribute nothing, so we skip them. if num == 0: continue field_data += num * data[ftype, "%s_number_density" % species] return field_data
def test_default_species_fields(): ds = data_dir_load(sloshing) sp = ds.sphere("c", (0.2, "unitary")) amu_cgs = ds.units.physical_constants.amu_cgs mueinv = 1.0 * _primordial_mass_fraction["H"] / ChemicalFormula("H").weight mueinv *= sp["index", "ones"] mueinv += 2.0 * _primordial_mass_fraction["He"] / ChemicalFormula( "He").weight mupinv = _primordial_mass_fraction["H"] / ChemicalFormula("H").weight mupinv *= sp["index", "ones"] muainv = _primordial_mass_fraction["He"] / ChemicalFormula("He").weight muainv *= sp["index", "ones"] mueinv2 = sp["gas", "El_number_density"] * amu_cgs / sp["gas", "density"] mupinv2 = sp["gas", "H_p1_number_density"] * amu_cgs / sp["gas", "density"] muainv2 = sp["gas", "He_p2_number_density"] * amu_cgs / sp["gas", "density"] assert_allclose_units(mueinv, mueinv2) assert_allclose_units(mupinv, mupinv2) assert_allclose_units(muainv, muainv2) assert_equal(sp["gas", "H_p1_number_density"], sp["gas", "H_nuclei_density"]) assert_equal(sp["gas", "He_p2_number_density"], sp["gas", "He_nuclei_density"])
def _default_nuclei_density(field, data): ftype = field.name[0] element = field.name[1][: field.name[1].find("_")] amu_cgs = data.ds.units.physical_constants.amu_cgs if element == "El": # This assumes full ionization! muinv = 1.0 * _primordial_mass_fraction["H"] / ChemicalFormula("H").weight muinv += 2.0 * _primordial_mass_fraction["He"] / ChemicalFormula("He").weight else: muinv = _primordial_mass_fraction[element] / ChemicalFormula(element).weight return data[ftype, "density"] * muinv / amu_cgs
def _default_nuclei_density(field, data): ftype = field.name[0] element = field.name[1][: field.name[1].find("_")] amu_cgs = data.ds.units.physical_constants.amu_cgs if element == "El": # This is for determining the electron number density. # If we got here, this assumes full ionization! muinv = 1.0 * _primordial_mass_fraction["H"] / ChemicalFormula("H").weight muinv += 2.0 * _primordial_mass_fraction["He"] / ChemicalFormula("He").weight else: # This is for anything else besides electrons muinv = _primordial_mass_fraction[element] / ChemicalFormula(element).weight return data[ftype, "density"] * muinv / amu_cgs
def _create_number_density_func(ftype, species): formula = ChemicalFormula(species) weight = formula.weight # This is in AMU weight *= amu_cgs def _number_density(field, data): return data[ftype, "%s_density" % species] \ / weight return _number_density
def _create_number_density_func(ftype, species): formula = ChemicalFormula(species) def _number_density(field, data): weight = formula.weight # This is in AMU weight *= data.ds.units.physical_constants.amu_cgs return data[ftype, "%s_density" % species] \ / weight return _number_density
def test_formulas(): for formula, components, charge in _molecules: f = ChemicalFormula(formula) w = sum(n * periodic_table[e].weight for e, n in components) assert_equal(f.charge, charge) assert_equal(f.weight, w) for (n, c1), (e, c2) in zip(components, f.elements): assert_equal(n, e.symbol) assert_equal(c1, c2)
def setup_species_fields(registry, ftype="gas", slice_info=None): for species in registry.species_names: # These are all the species we should be looking for fractions or # densities of. if (ftype, f"{species}_density") in registry: func = add_species_field_by_density elif (ftype, f"{species}_fraction") in registry: func = add_species_field_by_fraction else: # Skip it continue func(registry, ftype, species) # Add aliases of X_p0_<field> to X_<field>. # These are deprecated and will be removed soon. if ChemicalFormula(species).charge == 0: alias_species = species.split("_")[0] if (ftype, f"{alias_species}_density") in registry: continue add_deprecated_species_aliases(registry, "gas", alias_species, species) add_nuclei_density_fields(registry, ftype)
def setup_species_fields(registry, ftype = "gas", slice_info = None): # We have to check what type of field this is -- if it's particles, then we # set particle_type to True. particle_type = ftype not in registry.ds.fluid_types for species in registry.species_names: # These are all the species we should be looking for fractions or # densities of. if (ftype, "%s_density" % species) in registry: func = add_species_field_by_density elif (ftype, "%s_fraction" % species) in registry: func = add_species_field_by_fraction else: # Skip it continue func(registry, ftype, species, particle_type) # Adds aliases for all neutral species from their raw "MM_" # species to "MM_p0_" species to be explicit. # See YTEP-0003 for more details. if (ChemicalFormula(species).charge == 0): alias_species = "%s_p0" % species.split('_')[0] add_species_aliases(registry, "gas", alias_species, species) add_nuclei_density_fields(registry, ftype, particle_type=particle_type)
def test_default_species_fields(): # Test default case (no species fields) ds = fake_random_ds(32) sp = ds.sphere("c", (0.2, "unitary")) mu = 0.5924489101195808 assert_allclose_units(mu * sp["index", "ones"], sp["gas", "mean_molecular_weight"]) assert ("gas", "H_nuclei_density") not in ds.derived_field_list assert ("gas", "He_nuclei_density") not in ds.derived_field_list assert ("gas", "El_number_density") not in ds.derived_field_list assert ("gas", "H_p1_number_density") not in ds.derived_field_list assert ("gas", "He_p2_number_density") not in ds.derived_field_list assert ("gas", "H_p0_number_density") not in ds.derived_field_list assert ("gas", "He_p0_number_density") not in ds.derived_field_list # Test fully ionized case dsi = fake_random_ds(32, default_species_fields="ionized") spi = dsi.sphere("c", (0.2, "unitary")) amu_cgs = dsi.units.physical_constants.amu_cgs mueinv = 1.0 * _primordial_mass_fraction["H"] / ChemicalFormula("H").weight mueinv *= spi["index", "ones"] mueinv += 2.0 * _primordial_mass_fraction["He"] / ChemicalFormula( "He").weight mupinv = _primordial_mass_fraction["H"] / ChemicalFormula("H").weight mupinv *= spi["index", "ones"] muainv = _primordial_mass_fraction["He"] / ChemicalFormula("He").weight muainv *= spi["index", "ones"] mueinv2 = spi["gas", "El_number_density"] * amu_cgs / spi["gas", "density"] mupinv2 = spi["gas", "H_p1_number_density"] * amu_cgs / spi["gas", "density"] muainv2 = spi["gas", "He_p2_number_density"] * amu_cgs / spi["gas", "density"] assert_allclose_units(mueinv, mueinv2) assert_allclose_units(mupinv, mupinv2) assert_allclose_units(muainv, muainv2) assert_equal(spi["gas", "H_p1_number_density"], spi["gas", "H_nuclei_density"]) assert_equal(spi["gas", "He_p2_number_density"], spi["gas", "He_nuclei_density"]) mu = 0.5924489101195808 assert_allclose_units(mu * spi["index", "ones"], spi["gas", "mean_molecular_weight"]) # Test fully neutral case dsn = fake_random_ds(32, default_species_fields="neutral") spn = dsn.sphere("c", (0.2, "unitary")) amu_cgs = dsn.units.physical_constants.amu_cgs assert ("gas", "El_number_density") not in ds.derived_field_list mupinv = _primordial_mass_fraction["H"] / ChemicalFormula("H").weight mupinv *= spn["index", "ones"] muainv = _primordial_mass_fraction["He"] / ChemicalFormula("He").weight muainv *= spn["index", "ones"] mupinv2 = spn["gas", "H_p0_number_density"] * amu_cgs / spn["gas", "density"] muainv2 = spn["gas", "He_p0_number_density"] * amu_cgs / spn["gas", "density"] assert_allclose_units(mueinv, mueinv2) assert_allclose_units(mupinv, mupinv2) assert_allclose_units(muainv, muainv2) assert_equal(spn["gas", "H_p0_number_density"], spn["gas", "H_nuclei_density"]) assert_equal(spn["gas", "He_p0_number_density"], spn["gas", "He_nuclei_density"]) mu = 1.2285402715185552 assert_allclose_units(mu * spn["index", "ones"], spn["gas", "mean_molecular_weight"])
def _default_nuclei_density(field, data): ftype = field.name[0] element = field.name[1][:field.name[1].find("_")] return data[ftype, "density"] * _primordial_mass_fraction[element] / \ ChemicalFormula(element).weight / amu_cgs