Пример #1
0
    def _parse_parameter_file(self):

        xmin, xmax = self._handle.attrs["RootGridX1"][:2]
        ymin, ymax = self._handle.attrs["RootGridX2"][:2]
        zmin, zmax = self._handle.attrs["RootGridX3"][:2]

        self.domain_left_edge = np.array([xmin, ymin, zmin], dtype="float64")
        self.domain_right_edge = np.array([xmax, ymax, zmax], dtype="float64")

        self.geometry = geom_map[self._handle.attrs["Coordinates"].decode("utf-8")]
        self.domain_width = self.domain_right_edge - self.domain_left_edge
        self.domain_dimensions = self._handle.attrs["RootGridSize"]

        self._field_map = {}
        k = 0
        for dname, num_var in zip(
            self._handle.attrs["DatasetNames"], self._handle.attrs["NumVariables"]
        ):
            for j in range(num_var):
                fname = self._handle.attrs["VariableNames"][k].decode("ascii", "ignore")
                self._field_map[fname] = (dname.decode("ascii", "ignore"), j)
                k += 1

        self.refine_by = 2
        dimensionality = 3
        if self.domain_dimensions[2] == 1:
            dimensionality = 2
        if self.domain_dimensions[1] == 1:
            dimensionality = 1
        self.dimensionality = dimensionality
        self.current_time = self._handle.attrs["Time"]
        self.unique_identifier = self.parameter_filename.__hash__()
        self.cosmological_simulation = False
        self.num_ghost_zones = 0
        self.field_ordering = "fortran"
        self.boundary_conditions = [1] * 6
        self._periodicity = tuple(
            self.specified_parameters.get("periodicity", (True, True, True))
        )
        if "gamma" in self.specified_parameters:
            self.gamma = float(self.specified_parameters["gamma"])
        else:
            self.gamma = 5.0 / 3.0

        self.current_redshift = 0.0
        self.omega_lambda = 0.0
        self.omega_matter = 0.0
        self.hubble_constant = 0.0
        self.cosmological_simulation = 0
        self.parameters["Time"] = self.current_time  # Hardcode time conversion for now.
        self.parameters[
            "HydroMethod"
        ] = 0  # Hardcode for now until field staggering is supported.
        if "gamma" in self.specified_parameters:
            self.parameters["Gamma"] = self.specified_parameters["gamma"]
        else:
            self.parameters["Gamma"] = 5.0 / 3.0
        self.mu = self.specified_parameters.get(
            "mu", compute_mu(self.default_species_fields)
        )
Пример #2
0
 def _number_density(field, data):
     mu = getattr(data.ds, "mu",
                  compute_mu(data.ds.default_species_fields))
     return data[ftype, "density"] / (pc.mh * mu)
Пример #3
0
    def _parse_parameter_file(self):
        self._handle = open(self.parameter_filename, "rb")
        # Read the start of a grid to get simulation parameters.
        grid = {}
        grid["read_field"] = None
        line = self._handle.readline()
        while grid["read_field"] is None:
            parse_line(line, grid)
            splitup = line.strip().split()
            if chk23("X_COORDINATES") in splitup:
                grid["left_edge"] = np.zeros(3)
                grid["dds"] = np.zeros(3)
                v = np.fromfile(self._handle, dtype=">f8", count=2)
                grid["left_edge"][0] = v[0] - 0.5 * (v[1] - v[0])
                grid["dds"][0] = v[1] - v[0]
            if chk23("Y_COORDINATES") in splitup:
                v = np.fromfile(self._handle, dtype=">f8", count=2)
                grid["left_edge"][1] = v[0] - 0.5 * (v[1] - v[0])
                grid["dds"][1] = v[1] - v[0]
            if chk23("Z_COORDINATES") in splitup:
                v = np.fromfile(self._handle, dtype=">f8", count=2)
                grid["left_edge"][2] = v[0] - 0.5 * (v[1] - v[0])
                grid["dds"][2] = v[1] - v[0]
            if check_break(line):
                break
            line = self._handle.readline()

        self.domain_left_edge = grid["left_edge"]
        mylog.info(
            "Temporarily setting domain_right_edge = -domain_left_edge. "
            "This will be corrected automatically if it is not the case.")
        self.domain_right_edge = -self.domain_left_edge
        self.domain_width = self.domain_right_edge - self.domain_left_edge
        self.domain_dimensions = np.round(self.domain_width /
                                          grid["dds"]).astype("int32")
        refine_by = None
        if refine_by is None:
            refine_by = 2
        self.refine_by = refine_by
        dimensionality = 3
        if grid["dimensions"][2] == 1:
            dimensionality = 2
        if grid["dimensions"][1] == 1:
            dimensionality = 1
        if dimensionality <= 2:
            self.domain_dimensions[2] = np.int32(1)
        if dimensionality == 1:
            self.domain_dimensions[1] = np.int32(1)
        if dimensionality != 3 and self.nprocs > 1:
            raise RuntimeError(
                "Virtual grids are only supported for 3D outputs!")
        self.dimensionality = dimensionality
        self.current_time = grid["time"]
        self.unique_identifier = self.parameter_filename.__hash__()
        self.cosmological_simulation = False
        self.num_ghost_zones = 0
        self.field_ordering = "fortran"
        self.boundary_conditions = [1] * 6
        self._periodicity = tuple(
            self.specified_parameters.get("periodicity", (True, True, True)))
        if "gamma" in self.specified_parameters:
            self.gamma = float(self.specified_parameters["gamma"])
        else:
            self.gamma = 5.0 / 3.0
        dataset_dir = os.path.dirname(self.parameter_filename)
        dname = os.path.split(self.parameter_filename)[-1]
        if dataset_dir.endswith("id0"):
            dname = "id0/" + dname
            dataset_dir = dataset_dir[:-3]

        gridlistread = sglob(
            os.path.join(dataset_dir, f"id*/{dname[4:-9]}-id*{dname[-9:]}"))
        if "id0" in dname:
            gridlistread += sglob(
                os.path.join(dataset_dir,
                             f"id*/lev*/{dname[4:-9]}*-lev*{dname[-9:]}"))
        else:
            gridlistread += sglob(
                os.path.join(dataset_dir,
                             f"lev*/{dname[:-9]}*-lev*{dname[-9:]}"))
        ndots = dname.count(".")
        gridlistread = [
            fn for fn in gridlistread
            if os.path.basename(fn).count(".") == ndots
        ]
        self.nvtk = len(gridlistread) + 1

        self.current_redshift = 0.0
        self.omega_lambda = 0.0
        self.omega_matter = 0.0
        self.hubble_constant = 0.0
        self.cosmological_simulation = 0
        self.parameters[
            "Time"] = self.current_time  # Hardcode time conversion for now.
        self.parameters[
            "HydroMethod"] = 0  # Hardcode for now until field staggering is supported.
        if "gamma" in self.specified_parameters:
            self.parameters["Gamma"] = self.specified_parameters["gamma"]
        else:
            self.parameters["Gamma"] = 5.0 / 3.0
        self.geometry = self.specified_parameters.get("geometry", "cartesian")
        self._handle.close()
        self.mu = self.specified_parameters.get(
            "mu", compute_mu(self.default_species_fields))
Пример #4
0
    def __init__(
        self,
        filename,
        dataset_type="gadget_binary",
        additional_fields=(),
        unit_base=None,
        index_order=None,
        index_filename=None,
        kdtree_filename=None,
        kernel_name=None,
        bounding_box=None,
        header_spec="default",
        field_spec="default",
        ptype_spec="default",
        long_ids=False,
        units_override=None,
        mean_molecular_weight=None,
        header_offset=0,
        unit_system="cgs",
        use_dark_factor=False,
        w_0=-1.0,
        w_a=0.0,
        default_species_fields=None,
    ):
        if self._instantiated:
            return
        # Check if filename is a directory
        if os.path.isdir(filename):
            # Get the .0 snapshot file. We know there's only 1 and it's valid since we
            # came through _is_valid in load()
            for f in os.listdir(filename):
                fname = os.path.join(filename, f)
                fext = os.path.splitext(fname)[-1]
                if (
                    (".0" in f)
                    and (fext not in {".ewah", ".kdtree"})
                    and os.path.isfile(fname)
                ):
                    filename = os.path.join(filename, f)
                    break
        self._header = GadgetBinaryHeader(filename, header_spec)
        header_size = self._header.size
        if header_size != [256]:
            only_on_root(
                mylog.warning,
                "Non-standard header size is detected! "
                "Gadget-2 standard header is 256 bytes, but yours is %s. "
                "Make sure a non-standard header is actually expected. "
                "Otherwise something is wrong, "
                "and you might want to check how the dataset is loaded. "
                "Further information about header specification can be found in "
                "https://yt-project.org/docs/dev/examining/loading_data.html#header-specification.",
                header_size,
            )
        self._field_spec = self._setup_binary_spec(field_spec, gadget_field_specs)
        self._ptype_spec = self._setup_binary_spec(ptype_spec, gadget_ptype_specs)
        self.storage_filename = None
        if long_ids:
            self._id_dtype = "u8"
        else:
            self._id_dtype = "u4"
        self.long_ids = long_ids
        self.header_offset = header_offset
        if unit_base is not None and "UnitLength_in_cm" in unit_base:
            # We assume this is comoving, because in the absence of comoving
            # integration the redshift will be zero.
            unit_base["cmcm"] = 1.0 / unit_base["UnitLength_in_cm"]
        self._unit_base = unit_base
        if bounding_box is not None:
            # This ensures that we know a bounding box has been applied
            self._domain_override = True
            bbox = np.array(bounding_box, dtype="float64")
            if bbox.shape == (2, 3):
                bbox = bbox.transpose()
            self.domain_left_edge = bbox[:, 0]
            self.domain_right_edge = bbox[:, 1]
        else:
            self.domain_left_edge = self.domain_right_edge = None
        if units_override is not None:
            raise RuntimeError(
                "units_override is not supported for GadgetDataset. "
                + "Use unit_base instead."
            )

        # Set dark energy parameters before cosmology object is created
        self.use_dark_factor = use_dark_factor
        self.w_0 = w_0
        self.w_a = w_a

        super().__init__(
            filename,
            dataset_type=dataset_type,
            unit_system=unit_system,
            index_order=index_order,
            index_filename=index_filename,
            kdtree_filename=kdtree_filename,
            kernel_name=kernel_name,
            default_species_fields=default_species_fields,
        )
        if self.cosmological_simulation:
            self.time_unit.convert_to_units("s/h")
            self.length_unit.convert_to_units("kpccm/h")
            self.mass_unit.convert_to_units("g/h")
        else:
            self.time_unit.convert_to_units("s")
            self.length_unit.convert_to_units("kpc")
            self.mass_unit.convert_to_units("Msun")
        if mean_molecular_weight is None:
            self.mu = compute_mu(self.default_species_fields)
        else:
            self.mu = mean_molecular_weight
Пример #5
0
def test_default_mu():
    assert_allclose(compute_mu(None), 0.5924489101195808)
    assert_allclose(compute_mu("ionized"), 0.5924489101195808)
    assert_allclose(compute_mu("neutral"), 1.2285402715185552)