Skip to content
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
4d4b86a
feat: Remove angle as dimension
hpohekar Nov 5, 2024
9556b7d
feat: Remove angle as dimension 1
hpohekar Nov 5, 2024
1362d5b
feat: Support Python 3.13
hpohekar Nov 5, 2024
4a46495
feat: example fix
hpohekar Nov 5, 2024
bb3c1bf
feat: configure angle
hpohekar Nov 5, 2024
a1a3c22
feat: configure angle 1
hpohekar Nov 5, 2024
e8a2730
feat: test update 1
hpohekar Nov 5, 2024
36783df
feat: test update 2
hpohekar Nov 5, 2024
05cd020
feat: test update 3
hpohekar Nov 5, 2024
751ec36
feat: test update 4
hpohekar Nov 5, 2024
da3fe17
feat: test update 5
hpohekar Nov 5, 2024
8f75f05
feat: test update 6
hpohekar Nov 5, 2024
fb4cb48
feat: test update 7
hpohekar Nov 5, 2024
30f20fe
feat: test update 8
hpohekar Nov 5, 2024
1204aab
feat: test update 9
hpohekar Nov 5, 2024
b83d182
feat: test update 10
hpohekar Nov 5, 2024
63af28b
feat: test update 11
hpohekar Nov 5, 2024
36ba7c4
feat: test update 12
hpohekar Nov 5, 2024
fd1315a
feat: test update 13
hpohekar Nov 5, 2024
10b02fa
feat: test update 14
hpohekar Nov 5, 2024
7cb8044
feat: test update 15
hpohekar Nov 5, 2024
5ce4abe
feat: test update 16
hpohekar Nov 5, 2024
f289311
feat: test update 17
hpohekar Nov 5, 2024
7d086d0
feat: test update 18
hpohekar Nov 5, 2024
6bb2819
feat: Contributors
hpohekar Nov 5, 2024
8ab140e
feat: refactoring 1
hpohekar Nov 5, 2024
51e988d
feat: refactoring 2
hpohekar Nov 5, 2024
54952f0
feat: refactoring 3
hpohekar Nov 5, 2024
72b68bb
feat: refactoring 4
hpohekar Nov 5, 2024
2e253cf
feat: refactoring 5
hpohekar Nov 6, 2024
0247775
feat: refactoring 6
hpohekar Nov 6, 2024
a3a00c1
feat: refactoring 7
hpohekar Nov 6, 2024
50e3c94
feat: refactoring 8
hpohekar Nov 6, 2024
9b8b6a0
feat: test fix 1
hpohekar Nov 6, 2024
9166bbd
feat: test fix 2
hpohekar Nov 6, 2024
fb31f20
feat: test fix 3
hpohekar Nov 6, 2024
d398625
remove maintenance changes
hpohekar Nov 6, 2024
0135640
Merge branch 'main' into feat/configure_angles_as_dimensions
hpohekar Nov 7, 2024
28a61cf
add more tests
hpohekar Nov 7, 2024
79060b3
Merge branch 'feat/configure_angles_as_dimensions' of https://github.…
hpohekar Nov 7, 2024
907c371
update license
hpohekar Nov 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
python-version: ['3.10', '3.11', '3.12']
python-version: ['3.10', '3.11', '3.12', '3.13']
steps:
- uses: ansys/actions/build-wheelhouse@v8
with:
Expand All @@ -72,7 +72,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
python-version: ['3.10', '3.11', '3.12']
python-version: ['3.10', '3.11', '3.12', '3.13']
fail-fast: false
steps:
- uses: ansys/actions/tests-pytest@v8
Expand Down Expand Up @@ -108,6 +108,8 @@ jobs:
with:
cname: ${{ env.DOCUMENTATION_CNAME }}
token: ${{ secrets.GITHUB_TOKEN }}
bot-user: ${{ secrets.PYANSYS_CI_BOT_USERNAME }}
bot-email: ${{ secrets.PYANSYS_CI_BOT_EMAIL }}

release:
name: "Release project to private, public PyPI and GitHub"
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ repos:
hooks:
- id: add-license-headers
args:
- --start_year=2023
- --start_year=2024
6 changes: 3 additions & 3 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

## Individual Contributors

* [Sean Pearson](https://github.com/seanpearsonuk)
* [Alexander Carvalho](https://github.com/acarvalh-work)
* [Adam Boutin](https://github.com/ansaboutin)
* [Mainak Kundu](https://github.com/mkundu1)
* [Prithwish Mukherjee](https://github.com/prmukherj)
* [Alexander Carvalho](https://github.com/acarvalh-work)
* [Adam Boutin]([https:](https://github.com/ansaboutin))
* [Sean Pearson](https://github.com/seanpearsonuk)
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) 2023 - 2024 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
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ To reach the project support team, email `[email protected] <pyansys.core@a
Installation
------------

The ``ansys.units`` package supports Python 3.10 through Python 3.12 on Windows
The ``ansys.units`` package supports Python 3.10 through Python 3.13 on Windows
and Linux.


Expand Down
2 changes: 1 addition & 1 deletion doc/source/getting_started/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Installation
============

The ``ansys-units`` package supports Python 3.10 through Python 3.12 on Windows
The ``ansys-units`` package supports Python 3.10 through Python 3.13 on Windows
and Linux.

Install the package
Expand Down
2 changes: 1 addition & 1 deletion examples/00-pyunits/basic_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@

# Combinations of these

sys_units = {dims.MASS: "slug", dims.LENGTH: "ft", dims.ANGLE: "degree"}
sys_units = {dims.MASS: "slug", dims.LENGTH: "ft"}
cgs_modified = UnitSystem(base_units=sys_units, copy_from=cgs)
cgs_modified
"""
Expand Down
66 changes: 37 additions & 29 deletions src/ansys/units/base_dimensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,46 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Provides the ``BaseDimensions`` class."""
"""
Provides the ``BaseDimensions`` class.
from enum import Enum
Supplies all valid base dimensions used in dimensional analysis.
Used as dictionary keys for defining a `Dimensions` object.
"""

class BaseDimensions(Enum):
"""
Supplies all valid base dimensions used in dimensional analysis.
from enum import Enum
import os

Used as dictionary keys for defining a `Dimensions` object.
_base_dims = {
"MASS": 0,
"LENGTH": 1,
"TIME": 2,
"TEMPERATURE": 3,
"TEMPERATURE_DIFFERENCE": 4,
"CHEMICAL_AMOUNT": 5,
"LIGHT": 6,
"CURRENT": 7,
}

Attributes
----------
MASS
LENGTH
TIME
TEMPERATURE
TEMPERATURE_DIFFERENCE
ANGLE
CHEMICAL_AMOUNT
LIGHT
CURRENT
SOLID_ANGLE
"""
_base_dims_with_angle = {
"MASS": 0,
"LENGTH": 1,
"TIME": 2,
"TEMPERATURE": 3,
"TEMPERATURE_DIFFERENCE": 4,
"CHEMICAL_AMOUNT": 5,
"LIGHT": 6,
"CURRENT": 7,
"ANGLE": 8,
"SOLID_ANGLE": 9,
}

MASS = 0
LENGTH = 1
TIME = 2
TEMPERATURE = 3
TEMPERATURE_DIFFERENCE = 4
ANGLE = 5
CHEMICAL_AMOUNT = 6
LIGHT = 7
CURRENT = 8
SOLID_ANGLE = 9
BaseDimensions = Enum(
"BaseDimensions",
(
_base_dims_with_angle
if os.getenv("PYANSYS_UNITS_ANGLE_AS_DIMENSION")
else _base_dims
),
)
11 changes: 9 additions & 2 deletions src/ansys/units/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from __future__ import annotations

import operator
import os
from typing import Union

from ansys.units import BaseDimensions, Dimensions
Expand Down Expand Up @@ -296,10 +297,16 @@ def _relative_unit_check(self, __value, op: operator = operator.add) -> Quantity
def __float__(self):
base_dims = BaseDimensions
dims = Dimensions
if os.getenv("PYANSYS_UNITS_ANGLE_AS_DIMENSION"):
angle_dims = {base_dims.ANGLE: 1.0}
solid_angle_dims = {base_dims.SOLID_ANGLE: 1.0}
else:
angle_dims = {}
solid_angle_dims = {}
if self.dimensions in [
dims(),
dims(dimensions={base_dims.ANGLE: 1.0}),
dims(dimensions={base_dims.SOLID_ANGLE: 1.0}),
dims(dimensions=angle_dims),
dims(dimensions=solid_angle_dims),
]:
return get_si_value(self)
raise InvalidFloatUsage()
Expand Down
38 changes: 20 additions & 18 deletions src/ansys/units/systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

from __future__ import annotations

import os

from ansys.units import BaseDimensions, _base_units, _unit_systems


Expand All @@ -49,11 +51,9 @@ class UnitSystem:
TIME
TEMPERATURE
TEMPERATURE_DIFFERENCE
ANGLE
CHEMICAL_AMOUNT
LIGHT
CURRENT
SOLID_ANGLE
"""

def __init__(
Expand Down Expand Up @@ -157,15 +157,6 @@ def TEMPERATURE_DIFFERENCE(self):
def TEMPERATURE_DIFFERENCE(self, new_mass):
self._set_type(unit_type=BaseDimensions.TEMPERATURE_DIFFERENCE, unit=new_mass)

@property
def ANGLE(self):
"""Angle unit of the unit system."""
return self._ANGLE

@ANGLE.setter
def ANGLE(self, new_mass):
self._set_type(unit_type=BaseDimensions.ANGLE, unit=new_mass)

@property
def CHEMICAL_AMOUNT(self):
"""Chemical Amount unit of the unit system."""
Expand Down Expand Up @@ -193,14 +184,25 @@ def CURRENT(self):
def CURRENT(self, new_mass):
self._set_type(unit_type=BaseDimensions.CURRENT, unit=new_mass)

@property
def SOLID_ANGLE(self):
"""Solid Angle unit of the unit system."""
return self._SOLID_ANGLE
if os.getenv("PYANSYS_UNITS_ANGLE_AS_DIMENSION"):

@property
def ANGLE(self):
"""Angle unit of the unit system."""
return self._ANGLE

@ANGLE.setter
def ANGLE(self, new_mass):
self._set_type(unit_type=BaseDimensions.ANGLE, unit=new_mass)

@property
def SOLID_ANGLE(self):
"""Solid Angle unit of the unit system."""
return self._SOLID_ANGLE

@SOLID_ANGLE.setter
def SOLID_ANGLE(self, new_mass):
self._set_type(unit_type=BaseDimensions.SOLID_ANGLE, unit=new_mass)
@SOLID_ANGLE.setter
def SOLID_ANGLE(self, new_mass):
self._set_type(unit_type=BaseDimensions.SOLID_ANGLE, unit=new_mass)

def __repr__(self):
units = ""
Expand Down
9 changes: 5 additions & 4 deletions src/ansys/units/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,11 @@ def _units_to_dim(
if unit_term in _base_units:
idx = _base_units[unit_term]["type"]

if BaseDimensions[idx] in dimensions:
dimensions[BaseDimensions[idx]] += unit_term_exponent
else:
dimensions[BaseDimensions[idx]] = unit_term_exponent
if idx in list(BaseDimensions.__members__.keys()):
if BaseDimensions[idx] in dimensions:
dimensions[BaseDimensions[idx]] += unit_term_exponent
else:
dimensions[BaseDimensions[idx]] = unit_term_exponent
# Retrieve derived unit composition unit string and SI factor.
elif unit_term in _derived_units:
# Recursively parse composition unit string
Expand Down
9 changes: 6 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 Down
53 changes: 53 additions & 0 deletions tests/lib_compare/test_dimensions_without_angles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright (C) 2023 - 2024 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# 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 the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import ansys.units as pyunits


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


def test_radian_addition():
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_unit_system_repr():
us = pyunits.UnitSystem()
us_dict = """MASS: kg
LENGTH: m
TIME: s
TEMPERATURE: K
TEMPERATURE_DIFFERENCE: delta_K
CHEMICAL_AMOUNT: mol
LIGHT: cd
CURRENT: A
"""

assert repr(us) == str(us_dict)
6 changes: 4 additions & 2 deletions tests/test_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ def test_repr():
assert v.__repr__() == 'Quantity (1.0, "m")'


def test_math():
def test_math(monkeypatch):
monkeypatch.setenv("PYANSYS_UNITS_ANGLE_AS_DIMENSION", "1")
deg = Quantity(90, "degree")
assert math.sin(deg) == 1.0

Expand Down Expand Up @@ -620,7 +621,8 @@ def dim_test(units, dim_dict):
print("-" * 75)


def testing_multipliers():
def testing_multipliers(monkeypatch):
monkeypatch.setenv("PYANSYS_UNITS_ANGLE_AS_DIMENSION", "1")
print(f"{'*' * 25} {testing_multipliers.__name__} {'*' * 25}")

def from_to(from_str, to_str):
Expand Down
5 changes: 3 additions & 2 deletions tests/test_systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ def test_pre_defined_unit_system():
assert us.SOLID_ANGLE == "sr"


def test_repr():
def test_repr(monkeypatch):
monkeypatch.setenv("PYANSYS_UNITS_ANGLE_AS_DIMENSION", "1")
us = UnitSystem()
us_dict = """MASS: kg
LENGTH: m
TIME: s
TEMPERATURE: K
TEMPERATURE_DIFFERENCE: delta_K
ANGLE: radian
CHEMICAL_AMOUNT: mol
LIGHT: cd
CURRENT: A
ANGLE: radian
SOLID_ANGLE: sr
"""

Expand Down
Loading