Exemple #1
0
def test_cgs_equivalent():
    """
    Check cgs equivalent of a unit.

    """
    Msun_cgs = mass_sun_grams
    Mpc_cgs = cm_per_mpc

    u1 = Unit("Msun * Mpc**-3")
    u2 = Unit("g * cm**-3")
    u3 = u1.get_cgs_equivalent()

    yield assert_true, u2.expr == u3.expr
    yield assert_true, u2 == u3

    yield assert_allclose, u1.cgs_value, Msun_cgs / Mpc_cgs**3, 1e-12
    yield assert_true, u2.cgs_value == 1
    yield assert_true, u3.cgs_value == 1

    mass_density = mass / length**3

    yield assert_true, u1.dimensions == mass_density
    yield assert_true, u2.dimensions == mass_density
    yield assert_true, u3.dimensions == mass_density

    yield assert_allclose, get_conversion_factor(u1, u3)[0], \
        Msun_cgs / Mpc_cgs**3, 1e-12
    def setup_particle_fields(self, ptype, ftype='gas', num_neighbors=64 ):
        
	skip_output_units = ("code_length",)
        for f, (units, aliases, dn) in sorted(self.known_particle_fields):
            units = self.ds.field_units.get((ptype, f), units)
            if (f in aliases or ptype not in self.ds.particle_types_raw) and \
                units not in skip_output_units:
                u = Unit(units, registry = self.ds.unit_registry)
                output_units = str(u.get_cgs_equivalent())
            else:
                output_units = units
            if (ptype, f) not in self.field_list:
                continue
            self.add_output_field((ptype, f),
                units = units, particle_type = True,
                display_name = dn, output_units = output_units)
            for alias in aliases:
                self.alias((ptype, alias), (ptype, f), units = output_units)

        # We'll either have particle_position or particle_position_[xyz]
        if (ptype, "particle_position") in self.field_list or \
           (ptype, "particle_position") in self.field_aliases:
            particle_scalar_functions(ptype,
                   "particle_position", "particle_velocity",
                   self)
        else:
            # We need to check to make sure that there's a "known field" that
            # overlaps with one of the vector fields.  For instance, if we are
            # in the Stream frontend, and we have a set of scalar position
            # fields, they will overlap with -- and be overridden by -- the
            # "known" vector field that the frontend creates.  So the easiest
            # thing to do is to simply remove the on-disk field (which doesn't
            # exist) and replace it with a derived field.
            if (ptype, "particle_position") in self and \
                 self[ptype, "particle_position"]._function == NullFunc:
                self.pop((ptype, "particle_position"))
            particle_vector_functions(ptype,
                    ["particle_position_%s" % ax for ax in 'xyz'],
                    ["particle_velocity_%s" % ax for ax in 'xyz'],
                    self)
        particle_deposition_functions(ptype, "particle_position",
            "particle_mass", self)
        standard_particle_fields(self, ptype)
        # Now we check for any leftover particle fields
        for field in sorted(self.field_list):
            if field in self: continue
            if not isinstance(field, tuple):
                raise RuntimeError
            if field[0] not in self.ds.particle_types:
                continue
            self.add_output_field(field, 
                                  units = self.ds.field_units.get(field, ""),
                                  particle_type = True)
        self.setup_smoothed_fields(ptype, 
                                   num_neighbors=num_neighbors,
                                   ftype=ftype)
Exemple #3
0
    def setup_particle_fields(self, ptype, ftype='gas', num_neighbors=64):
        skip_output_units = ()
        for f, (units, aliases, dn) in sorted(self.known_particle_fields):
            units = self.ds.field_units.get((ptype, f), units)
            if (f in aliases or ptype not in self.ds.particle_types_raw) and \
                units not in skip_output_units:
                u = Unit(units, registry=self.ds.unit_registry)
                output_units = str(u.get_cgs_equivalent())
            else:
                output_units = units
            if (ptype, f) not in self.field_list:
                continue
            self.add_output_field((ptype, f),
                                  units=units,
                                  particle_type=True,
                                  display_name=dn,
                                  output_units=output_units,
                                  take_log=False)
            for alias in aliases:
                self.alias((ptype, alias), (ptype, f), units=output_units)

        # We'll either have particle_position or particle_position_[xyz]
        if (ptype, "particle_position") in self.field_list or \
           (ptype, "particle_position") in self.field_aliases:
            particle_scalar_functions(ptype, "particle_position",
                                      "particle_velocity", self)
        else:
            # We need to check to make sure that there's a "known field" that
            # overlaps with one of the vector fields.  For instance, if we are
            # in the Stream frontend, and we have a set of scalar position
            # fields, they will overlap with -- and be overridden by -- the
            # "known" vector field that the frontend creates.  So the easiest
            # thing to do is to simply remove the on-disk field (which doesn't
            # exist) and replace it with a derived field.
            if (ptype, "particle_position") in self and \
                 self[ptype, "particle_position"]._function == NullFunc:
                self.pop((ptype, "particle_position"))
            particle_vector_functions(
                ptype, ["particle_position_%s" % ax for ax in 'xyz'],
                ["particle_velocity_%s" % ax for ax in 'xyz'], self)
        particle_deposition_functions(ptype, "particle_position",
                                      "particle_mass", self)
        standard_particle_fields(self, ptype)
        # Now we check for any leftover particle fields
        for field in sorted(self.field_list):
            if field in self: continue
            if not isinstance(field, tuple):
                raise RuntimeError
            if field[0] not in self.ds.particle_types:
                continue
            self.add_output_field(field,
                                  units=self.ds.field_units.get(field, ""),
                                  particle_type=True)
        self.setup_smoothed_fields(ptype,
                                   num_neighbors=num_neighbors,
                                   ftype=ftype)
 def alias(self, alias_name, original_name, units=None):
     if original_name not in self: return
     if units is None:
         # We default to CGS here, but in principle, this can be pluggable
         # as well.
         u = Unit(self[original_name].units, registry=self.ds.unit_registry)
         units = str(u.get_cgs_equivalent())
     self.field_aliases[alias_name] = original_name
     self.add_field(alias_name,
                    function=TranslationFunc(original_name),
                    particle_type=self[original_name].particle_type,
                    display_name=self[original_name].display_name,
                    units=units)
 def alias(self, alias_name, original_name, units = None):
     if original_name not in self: return
     if units is None:
         # We default to CGS here, but in principle, this can be pluggable
         # as well.
         u = Unit(self[original_name].units,
                   registry = self.ds.unit_registry)
         units = str(u.get_cgs_equivalent())
     self.field_aliases[alias_name] = original_name
     self.add_field(alias_name,
         function = TranslationFunc(original_name),
         particle_type = self[original_name].particle_type,
         display_name = self[original_name].display_name,
         units = units)
Exemple #6
0
    def setup_particle_fields(self, ptype, ftype="gas", num_neighbors=64):
        skip_output_units = ()
        for f, (units, aliases, dn) in sorted(self.known_particle_fields):
            units = self.ds.field_units.get((ptype, f), units)
            if (f in aliases or ptype not in self.ds.particle_types_raw
                ) and units not in skip_output_units:
                u = Unit(units, registry=self.ds.unit_registry)
                output_units = str(u.get_cgs_equivalent())
            else:
                output_units = units
            if (ptype, f) not in self.field_list:
                continue
            self.add_output_field(
                (ptype, f),
                sampling_type="particle",
                units=units,
                display_name=dn,
                output_units=output_units,
                take_log=False,
            )
            for alias in aliases:
                self.alias((ptype, alias), (ptype, f), units=output_units)

        ppos_fields = [f"particle_position_{ax}" for ax in "xyz"]
        pvel_fields = [f"particle_velocity_{ax}" for ax in "xyz"]
        particle_vector_functions(ptype, ppos_fields, pvel_fields, self)

        particle_deposition_functions(ptype, "particle_position",
                                      "particle_mass", self)
        standard_particle_fields(self, ptype)
        # Now we check for any leftover particle fields
        for field in sorted(self.field_list):
            if field in self:
                continue
            if not isinstance(field, tuple):
                raise RuntimeError
            if field[0] not in self.ds.particle_types:
                continue
            self.add_output_field(
                field,
                sampling_type="particle",
                units=self.ds.field_units.get(field, ""),
            )
        self.setup_smoothed_fields(ptype,
                                   num_neighbors=num_neighbors,
                                   ftype=ftype)