SimulationUnitSystem#

class gala.units.SimulationUnitSystem(length: Unit | Annotated[Quantity, PhysicalType('length')] = None, mass: Unit | Annotated[Quantity, PhysicalType('mass')] = None, time: Unit | Annotated[Quantity, PhysicalType('time')] = None, velocity: Quantity, PhysicalType({'speed', 'velocity'})] = None, G: float | Quantity = 1.0, angle: Unit | Annotated[Quantity, PhysicalType('angle')] = Unit('rad'))[source]#

Bases: UnitSystem

Represents a system of units for a (dynamical) simulation.

A common assumption is that G=1. If this is the case, then you only have to specify two of the three fundamental unit types (length, mass, time), and the rest will be derived from these. Alternatively, you may specify a velocity instead of one of the three, and the remaining units will be derived.

Parameters:
lengthUnit, Quantity, optional

The length unit or quantity.

massUnit, Quantity, optional

The mass unit or quantity.

timeUnit, Quantity, optional

The time unit or quantity.

velocityUnit, Quantity, optional

The velocity unit or quantity.

Gfloat, Quantity, optional

The value of the gravitational constant to use. Default is 1.0.

angleUnit, Quantity, optional

The angle unit. Default is astropy.units.radian.

Examples

To convert simulation positions and velocities to physical units, you can use this unit system:

usys = SimulationUnitSystem(length=10 * u.kpc, time=50 * u.Myr)
(sim_pos * usys["length"]).to(u.kpc)
(sim_vel * usys["velocity"]).to(u.km/u.s)

Or, to convert positions and velocities from physical units to simulation units:

(100 * u.kpc).to(usys["length"])

Methods Summary

decompose(q)

A thin wrapper around astropy.units.Quantity.decompose() that knows how to handle Quantities with physical types with non-default representations.

from_string(name)

Create a UnitSystem instance from a name.

get_constant(name)

Retrieve a constant with specified name in this unit system.

to_dict()

Return a dictionary representation of the unit system with keys set by the physical types and values set by the unit objects.

Methods Documentation

decompose(q)#

A thin wrapper around astropy.units.Quantity.decompose() that knows how to handle Quantities with physical types with non-default representations.

Parameters:
qQuantity

An instance of an astropy Quantity object.

Returns:
qQuantity

A new quantity, decomposed to be represented in this unit system.

classmethod from_string(name: str) UnitSystem#

Create a UnitSystem instance from a name.

Parameters:
namestr

The name of the unit system. Examples of valid names are ‘galactic’, ‘solarsystem’, and ‘dimensionless’.

Returns:
usysUnitSystem

The corresponding unit system instance.

Examples

Create the galactic unit system from its name:

>>> usys = UnitSystem.from_string('galactic')
>>> usys['length']
Unit("kpc")
get_constant(name)#

Retrieve a constant with specified name in this unit system.

Parameters:
namestr

The name of the constant, e.g., G.

Returns:
constfloat

The value of the constant represented in this unit system.

Examples

We will get the value of the speed of light in a custom unit system:

>>> usys = UnitSystem(u.kpc, u.Myr, u.Msun, u.radian)
>>> usys.get_constant('c')
306.6013937855506
to_dict()#

Return a dictionary representation of the unit system with keys set by the physical types and values set by the unit objects.