Karana.Math.Ktyping#

Module containing types used throughout kdFlex.

Submodules#

Attributes#

Classes#

OptionallyWithUnits

Pydantic metadata validating quantity dimensions and magnitude.

Functions#

getDefaultUnits(→ pint.facets.plain.objects.PlainUnit)

Get the default units for a quantity.

npSizeCheck(→ pydantic.AfterValidator)

Ensure that the numpy array is the given shape.

normCheck(→ pydantic.AfterValidator)

Ensure the given value is within prec of norm.

beforeVal(→ Any)

Validate an incoming object.

ser(→ int | None)

Serialize an object using its ID rather than the object itself.

SerializeAsId(→ tuple[pydantic.BeforeValidator, ...)

Create pydantic functions to serialize object as ID.

Package Contents#

Karana.Math.Ktyping.mass#
Karana.Math.Ktyping.inertia#
Karana.Math.Ktyping.length#
Karana.Math.Ktyping.angle#
Karana.Math.Ktyping.velocity#
Karana.Math.Ktyping.acceleration#
Karana.Math.Ktyping.force#
Karana.Math.Ktyping.torque#
Karana.Math.Ktyping.angular_velocity#
Karana.Math.Ktyping.angular_acceleration#
Karana.Math.Ktyping.gravitational_parameter#
Karana.Math.Ktyping.linear_spring_constant#
Karana.Math.Ktyping.linear_damping_constant#
Karana.Math.Ktyping.rotational_spring_constant#
Karana.Math.Ktyping.rotational_damping_constant#
Karana.Math.Ktyping.getDefaultUnits(quantity: pint.util.UnitsContainer) pint.facets.plain.objects.PlainUnit[source]#

Get the default units for a quantity.

Parameters:

quantity (UnitsContainer) – The quantity to get default units for.

Returns:

The default units for the provided quantity.

Return type:

PlainUnit

Karana.Math.Ktyping.momentum#
Karana.Math.Ktyping.angular_momentum#
Karana.Math.Ktyping.translational_nodal_matrix#
Karana.Math.Ktyping.rotational_nodal_matrix#
Karana.Math.Ktyping.modal_stiffness#
Karana.Math.Ktyping.modal_damping#
Karana.Math.Ktyping.MATH_EPSILON: float = 1e-12#
Karana.Math.Ktyping.NonNegativeInt#
Karana.Math.Ktyping.NonNegativeFloat#
Karana.Math.Ktyping.npSizeCheck(shape: tuple[int, Ellipsis]) pydantic.AfterValidator[source]#

Ensure that the numpy array is the given shape.

Parameters:

shape (tuple[int, ...]) – Desired shape of the array. A value of -1 indicates that, that particular axis can be any size. For example, a shape of (3, -1, 2) indicates the first axis must be size 3, the second axis can be any size, and the third axis must be size 2.

class Karana.Math.Ktyping.OptionallyWithUnits[source]#

Pydantic metadata validating quantity dimensions and magnitude.

When Pydantic specializes Angle[Vec3], source_type is NumpyQuantity[Vec3]. This metadata extracts Vec3 and builds a separate adapter for quantity.m. Consequently the generic parameter supplies both the static type of .m and its runtime Pydantic validation.

This validator will coerce types without units to have them. If no unit is supplied, then the default unit for that quantity will be added.

dimensionality: pint.util.UnitsContainer#
__get_pydantic_core_schema__(source_type: Any, handler: Any) pydantic_core.CoreSchema[source]#

Get the validation Schema.

Parameters:
  • source_type (Any) – The source type to validate.

  • handler (Any) – The handler to run for normal validation.

Returns:

The core schema to use for validation.

Return type:

CoreSchema

Karana.Math.Ktyping.normCheck(norm: float, prec: float = MATH_EPSILON) pydantic.AfterValidator[source]#

Ensure the given value is within prec of norm.

Parameters:
  • norm (float) – The value we want the norm to be.

  • prec (float) – The tolerance for the value to be within that norm.

Karana.Math.Ktyping.Vec#
Karana.Math.Ktyping.Mat#
Karana.Math.Ktyping.Vec3#
Karana.Math.Ktyping.Mat33#
Karana.Math.Ktyping.Mat66#
Karana.Math.Ktyping.Mat6n#
Karana.Math.Ktyping.Mat3n#
type Karana.Math.Ktyping.Mass = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(mass)]#
type Karana.Math.Ktyping.Length = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(length)]#
type Karana.Math.Ktyping.Angle = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(angle)]#
type Karana.Math.Ktyping.Velocity = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(velocity)]#
type Karana.Math.Ktyping.AngularVelocity = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(angular_velocity)]#
type Karana.Math.Ktyping.Acceleration = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(acceleration)]#
type Karana.Math.Ktyping.AngularAcceleration = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(angular_acceleration)]#
type Karana.Math.Ktyping.Momentum = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(momentum)]#
type Karana.Math.Ktyping.AngularMomentum = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(angular_momentum)]#
type Karana.Math.Ktyping.Inertia = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(inertia)]#
type Karana.Math.Ktyping.Force = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(force)]#
type Karana.Math.Ktyping.Torque = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(torque)]#
type Karana.Math.Ktyping.GravitationalParameter = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(gravitational_parameter)]#
type Karana.Math.Ktyping.LinearSpringConstant = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(linear_spring_constant)]#
type Karana.Math.Ktyping.RotationalSpringConstant = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(rotational_spring_constant)]#
type Karana.Math.Ktyping.LinearDampingConstant = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(linear_damping_constant)]#
type Karana.Math.Ktyping.RotationalDampingConstant = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(rotational_damping_constant)]#
type Karana.Math.Ktyping.TranslationalNodalMatrix = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(translational_nodal_matrix)]#
type Karana.Math.Ktyping.RotationalNodalMatrix = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(rotational_nodal_matrix)]#
type Karana.Math.Ktyping.ModalStiffness = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(modal_stiffness)]#
type Karana.Math.Ktyping.ModalDamping = Annotated[NumpyQuantity[Magnitude], OptionallyWithUnits(modal_damping)]#
Karana.Math.Ktyping.beforeVal(val: int | Any, allow_int: bool = False, allow_None: bool = True) Any[source]#

Validate an incoming object.

If this is an integer, then us IdMixin to look up the object that corresponds to that integer.

Parameters:
  • val (int | Any) – The value to validate.

  • allow_int (bool) – If True, then if the val does not correspond to an object that has been created, then return the integer. This overshadows allow_None if both are set. If False, then try allow_None before.

  • allow_None (bool) – If True, then if the val does not correspond to an object that has been created, then return None. If False, then throw an error val does not correspond to an object that has been created.

Returns:

The value.

Return type:

Any

Karana.Math.Ktyping.ser(val: Any) int | None[source]#

Serialize an object using its ID rather than the object itself.

Parameters:

val (Any) – Use the id() method to get the value to serialize.

Returns:

This returns an integer if Value is none None, and None otherwise.

Return type:

int | None

Karana.Math.Ktyping.SerializeAsId(allow_int: bool = False, allow_None: bool = True) tuple[pydantic.BeforeValidator, pydantic.PlainSerializer][source]#

Create pydantic functions to serialize object as ID.

This is typically used like:

class MyDataStruct(DataStruct):

body: Annotated[PhysicalBody, *SerializeAsId()]

Parameters:
  • allow_int (bool) – If True, then if the val does not correspond to an object that has been created, then return the integer. This overshadows allow_None if both are set. If False, then try allow_None before.

  • allow_None (bool) – This affects deserialization. If True, then if the ID does not correspond to an object that has been created, then return None. If False, then throw an error if val does not correspond to an object that has been created.

Returns:

A validator function and serializer function for the pydantic field to serialize as an ID.

Return type:

tuple[BeforeValidator, PlainSerializer]