Skip to content

Commit

Permalink
feat: Configure angle as dimension (#309)
Browse files Browse the repository at this point in the history
* feat: Remove angle as dimension

* feat: Remove angle as dimension 1

* feat: Support Python 3.13

* feat: example fix

* feat: configure angle

* feat: configure angle 1

* feat: test update 1

* feat: test update 2

* feat: test update 3

* feat: test update 4

* feat: test update 5

* feat: test update 6

* feat: test update 7

* feat: test update 8

* feat: test update 9

* feat: test update 10

* feat: test update 11

* feat: test update 12

* feat: test update 13

* feat: test update 14

* feat: test update 15

* feat: test update 16

* feat: test update 17

* feat: test update 18

* feat: Contributors

* feat: refactoring 1

* feat: refactoring 2

* feat: refactoring 3

* feat: refactoring 4

* feat: refactoring 5

* feat: refactoring 6

* feat: refactoring 7

* feat: refactoring 8

* feat: test fix 1

* feat: test fix 2

* feat: test fix 3

* remove maintenance changes

* add more tests

* update license
  • Loading branch information
hpohekar authored Nov 7, 2024
1 parent 6780b64 commit d9c4ef2
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 5 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 - 2025 ANSYS, Inc. and/or its affiliates.
Copyright (c) 2024 ANSYS, Inc. and/or its affiliates.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
11 changes: 11 additions & 0 deletions src/ansys/units/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from __future__ import annotations

import os
from typing import Optional, Union

from ansys.units import (
Expand Down Expand Up @@ -99,10 +100,12 @@ def __init__(
self._dimensions = Dimensions(_dimensions)
if dimensions and self._dimensions != dimensions:
raise InconsistentDimensions()
self._dimensions = self._remove_angle_as_dim(self._dimensions)

elif dimensions:
self._dimensions = dimensions
self._name = _dim_to_units(dimensions=dimensions, system=system)
self._dimensions = self._remove_angle_as_dim(self._dimensions)
else:
self._name = ""
self._dimensions = Dimensions()
Expand All @@ -117,6 +120,14 @@ def __init__(
units=self.name
)

def _remove_angle_as_dim(self, dimensions):
if not os.getenv("PYANSYS_UNITS_ANGLE_AS_DIMENSION", None):
if BaseDimensions.ANGLE in dimensions._dimensions:
del dimensions._dimensions[BaseDimensions.ANGLE]
if BaseDimensions.SOLID_ANGLE in dimensions._dimensions:
del dimensions._dimensions[BaseDimensions.SOLID_ANGLE]
return dimensions

def _to_string(self):
"""
Creates a string representation of the unit.
Expand Down
106 changes: 103 additions & 3 deletions tests/lib_compare/test_angle_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def test_pint_angles_are_dimensionless():
assert str(angle_in_radians_dimensions) == "dimensionless"


def test_pyunits_angles_have_angle_dimensions():
def test_pyunits_angles_have_angle_dimensions(monkeypatch):
monkeypatch.setenv("PYANSYS_UNITS_ANGLE_AS_DIMENSION", "1")
from ansys.units import BaseDimensions, Dimensions
from ansys.units.quantity import Quantity

Expand Down Expand Up @@ -79,7 +80,8 @@ def test_pint_angle_and_dimensionless_are_convertible():
assert num_deg_rom_rad == util.one_degree_in_radians


def test_pyunits_angle_and_dimensionless_are_not_convertible():
def test_pyunits_angle_and_dimensionless_are_not_convertible(monkeypatch):
monkeypatch.setenv("PYANSYS_UNITS_ANGLE_AS_DIMENSION", "1")
from ansys.units.quantity import IncompatibleDimensions, Quantity

no_dim = Quantity(1.0, "")
Expand Down Expand Up @@ -169,7 +171,8 @@ def test_pint_conversion_between_Hz_and_rps_and_radians_per_second():
# mean is "This is the inevitable outcome of the way we do things."


def test_ansunits_frequency_and_angular_frequency_are_not_convertible():
def test_ansunits_frequency_and_angular_frequency_are_not_convertible(monkeypatch):
monkeypatch.setenv("PYANSYS_UNITS_ANGLE_AS_DIMENSION", "1")
from ansys.units.quantity import IncompatibleDimensions, Quantity

# ansunits avoids the pint complications by simply not allowing
Expand All @@ -180,3 +183,100 @@ def test_ansunits_frequency_and_angular_frequency_are_not_convertible():
rad_per_s = Quantity(1.0, "radian s^-1")
with pytest.raises(IncompatibleDimensions):
rad_per_s.to("Hz")


def test_degree_addition():
import ansys.units as pyunits

degree = pyunits.Quantity(1.0, "degree")
assert not degree.dimensions
assert degree.is_dimensionless
assert degree + 1 == pyunits.Quantity(58.29577951308232, "degree")


def test_degree_subtraction():
import ansys.units as pyunits

degree = pyunits.Quantity(120, "degree")
assert not degree.dimensions
assert degree.is_dimensionless
assert degree - 1 == pyunits.Quantity(62.70422048691768, "degree")


def test_degree_division():
import ansys.units as pyunits

degree = pyunits.Quantity(120, "degree")
assert not degree.dimensions
assert degree.is_dimensionless
assert degree / 2 == pyunits.Quantity(60, "degree")


def test_degree_multiplication():
import ansys.units as pyunits

degree = pyunits.Quantity(10, "degree")
assert not degree.dimensions
assert degree.is_dimensionless
assert degree * 2 == pyunits.Quantity(20.0, "degree")


def test_degree_power():
import ansys.units as pyunits

degree = pyunits.Quantity(10, "degree")
assert not degree.dimensions
assert degree.is_dimensionless
assert degree**2 == pyunits.Quantity(100, "degree^2")


def test_radian_addition():
import ansys.units as pyunits

radian = pyunits.Quantity(1.0, "radian")
assert not radian.dimensions
assert radian.is_dimensionless
assert radian + 1 == pyunits.Quantity(2.0, "radian")
assert (radian + 1).to("degree") == pyunits.Quantity(114.59155902616465, "degree")


def test_radian_subtraction():
import ansys.units as pyunits

radian = pyunits.Quantity(2.0, "radian")
assert not radian.dimensions
assert radian.is_dimensionless
assert radian - 1 == pyunits.Quantity(1.0, "radian")
assert (radian - 1).to("degree") == pyunits.Quantity(57.29577951308232, "degree")


def test_radian_division():
import ansys.units as pyunits

radian = pyunits.Quantity(2.0, "radian")
assert not radian.dimensions
assert radian.is_dimensionless
assert radian / 2 == pyunits.Quantity(1.0, "radian")
assert (radian / 2).to("degree") == pyunits.Quantity(57.29577951308232, "degree")


def test_radian_multiplication():
import ansys.units as pyunits

radian = pyunits.Quantity(2.0, "radian")
assert not radian.dimensions
assert radian.is_dimensionless
assert radian * 2 == pyunits.Quantity(4.0, "radian")
assert (radian * 2).to("degree") == pyunits.Quantity(229.1831180523293, "degree")


def test_radian_power():
import ansys.units as pyunits

radian = pyunits.Quantity(2.0, "radian")
assert not radian.dimensions
assert radian.is_dimensionless
assert radian**2 == pyunits.Quantity(4.0, "radian^2")
assert (radian**2).to("degree^2") == pyunits.Quantity(
13131.225400046977, "degree^2"
)
3 changes: 2 additions & 1 deletion tests/test_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,8 @@ def test_quantity_table():
assert api_test.units == Unit("kg m^-1.5 s^-2.5 A^3 cd W^2 K^-2")


def testing_units_to_dimensions():
def testing_units_to_dimensions(monkeypatch):
monkeypatch.setenv("PYANSYS_UNITS_ANGLE_AS_DIMENSION", "1")
print(f"{'*' * 25} {testing_units_to_dimensions.__name__} {'*' * 25}")
dims = BaseDimensions

Expand Down

0 comments on commit d9c4ef2

Please sign in to comment.