示例#1
0
def get_ureg():
    r"""Get the unit registry."""
    global _ureg_unyt
    if _ureg_unyt is None:
        _ureg_unyt = unyt.UnitRegistry('mks')
        _ureg_unyt.add("ac",
                       4046.86,
                       dimensions=unyt.dimensions.area,
                       tex_repr=r"\rm{ac}",
                       offset=0.0,
                       prefixable=False)
        _ureg_unyt.add("a",
                       100.0,
                       dimensions=unyt.dimensions.area,
                       tex_repr=r"\rm{a}",
                       offset=0.0,
                       prefixable=True)
        _ureg_unyt.add("j",
                       1.0,
                       dimensions=unyt.dimensions.energy,
                       tex_repr=r"\rm{J}",
                       offset=0.0,
                       prefixable=True)
        # _ureg_unyt.add("cel", 1.0, dimensions=unyt.dimensions.temperature,
        #                tex_repr=r"^\circ\rm{C}", offset=-273.15, prefixable=True)
        # _ureg_unyt.add("j", 1.0, dimensions=unyt.dimensions.specific_flux,
        #                tex_repr=r"\rm{Jy}", prefixable=True)
        # _ureg_unyt.add("CH2O", 1.0, dimensions=unyt.dimensions.dimensionless,
        #                tex_repr=r"\rm{CH2O}", offset=0.0, prefixable=False)
        unyt._unit_lookup_table.inv_name_alternatives["acre"] = "ac"
        unyt._unit_lookup_table.inv_name_alternatives["are"] = "a"
        unyt._unit_lookup_table.inv_name_alternatives["hectare"] = "ha"
        unyt._unit_lookup_table.inv_name_alternatives["days"] = "day"
    return _ureg_unyt
示例#2
0
    def __init__(self, *args, **kwargs):

        # Open the HDF5 file
        super(SwiftSnapshot, self).__init__(h5py.File(*args, **kwargs))

        # Read unit information
        self.base_units_cgs = {}
        if "Units" in self.obj:
            for name in self.obj["Units"].attrs.keys():
                m = re.match(r"^.*\(U_(.)\)$", name)
                if m is not None:
                    self.base_units_cgs[m.group(
                        1)] = self.obj["Units"].attrs[name][0]
        else:
            raise KeyError("Unable to find Units group in file %s" %
                           self.obj.filename)

        # Read cosmology information required for unit conversions (if present)
        self.cosmology = {}
        if "Cosmology" in self.obj:
            try:
                self.cosmology["h"] = self.obj["Cosmology"].attrs["h"][0]
            except KeyError:
                pass
            try:
                self.cosmology["a"] = self.obj["Cosmology"].attrs[
                    "Scale-factor"][0]
            except KeyError:
                pass

        # Create a unit system containing h and a for this snapshot
        self.reg = unyt.UnitRegistry()
        self.reg.add("a", self.cosmology["a"], unyt.dimensions.dimensionless)
        self.reg.add("h", self.cosmology["h"], unyt.dimensions.dimensionless)
示例#3
0
import re
import numpy as np
import unyt
from yggdrasil import tools
_ureg_unyt = unyt.UnitRegistry('mks')
_unit_quantity = unyt.array.unyt_quantity
_unit_array = unyt.array.unyt_array
_ureg_unyt.add("ac",
               4046.86,
               dimensions=unyt.dimensions.area,
               tex_repr=r"\rm{ac}",
               offset=0.0,
               prefixable=False)
_ureg_unyt.add("a",
               100.0,
               dimensions=unyt.dimensions.area,
               tex_repr=r"\rm{a}",
               offset=0.0,
               prefixable=True)
unyt._unit_lookup_table.inv_name_alternatives["acre"] = "ac"
unyt._unit_lookup_table.inv_name_alternatives["are"] = "a"
unyt._unit_lookup_table.inv_name_alternatives["hectare"] = "ha"


def convert_R_unit_string(r_str):
    r"""Convert R unit string to string that the Python package can
    understand.

    Args:
        r_str (str): R units string to convert.
示例#4
0
"""Define stress/strain tensors."""

from numpy import array, empty
import unyt as u
from unyt.array import unyt_array

# set unit regsitry
us_unit_system = u.UnitSystem(name='US',
                              length_unit='inch',
                              mass_unit='lb',
                              time_unit='s',
                              temperature_unit='degF',
                              angle_unit='deg')

# add strain unit
ureg = u.UnitRegistry()
ureg.add('strain',
         base_value=1.0,
         dimensions=u.dimensions.dimensionless,
         tex_repr=r"\rm{\varepsilon}",
         prefixable=True)
u.unit_registry.default_unit_registry = ureg


class Tensor(unyt_array):
    """Base 3x3 symmetric tensor.

    Parameters
    ----------
    matrix : numpy.array_like
        an array-like object with six values
示例#5
0
import numpy as np
from cis_interface import backwards
import unyt
import pint
_ureg_unyt = unyt.UnitRegistry()
_ureg_pint = pint.UnitRegistry()
_ureg_pint.define('micro_mole = 1e-6 * mole = uMol = umol')
_use_unyt = True


def has_units(obj):
    r"""Determine if a Python object has associated units.

    Args:
        obj (object): Object to be tested for units.

    Returns:
        bool: True if the object has units, False otherwise.

    """
    return hasattr(obj, 'units')


def get_units(obj):
    r"""Get the string representation of the units.

    Args:
        obj (object): Object to get units for.

    Returns:
        str: Units, empty if input object has none.