Skip to content

Commit fa2977b

Browse files
Add dimension (#366)
* update license version numbers * update nanobind versions * skip pypy3.11 build on mac * add dimensions class and some commodity conversion maps to actually use * update version numbers * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix compatibility * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * update runners * remove old cmake tests --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ce388f9 commit fa2977b

11 files changed

Lines changed: 208 additions & 120 deletions

‎.github/workflows/tests.yml‎

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,15 @@ on:
1010

1111
jobs:
1212
cmake-config-2004:
13-
name: CMake config check 3.4-3.11
14-
runs-on: ubuntu-20.04
13+
name: CMake config check 3.7-3.11
14+
runs-on: ubuntu-22.04
1515
steps:
1616
- uses: actions/checkout@v4
1717

1818
- name: update certificates
1919
run: |
2020
sudo apt-get install ca-certificates
2121
22-
- name: Check CMake 3.4
23-
with:
24-
cmake-version: "3.4"
25-
uses: ./.github/actions/quick_cmake
26-
27-
- name: Check CMake 3.5
28-
uses: ./.github/actions/quick_cmake
29-
with:
30-
cmake-version: "3.5"
31-
if: success() || failure()
32-
33-
- name: Check CMake 3.6
34-
uses: ./.github/actions/quick_cmake
35-
with:
36-
cmake-version: "3.6"
37-
if: success() || failure()
38-
3922
- name: Check CMake 3.7
4023
uses: ./.github/actions/quick_cmake
4124
with:
@@ -68,7 +51,7 @@ jobs:
6851

6952
cmake-config-2204:
7053
name: CMake config check 3.12+
71-
runs-on: ubuntu-22.04
54+
runs-on: ubuntu-24.04
7255
steps:
7356
- uses: actions/checkout@v4
7457

‎CHANGELOG.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@ All notable changes to this project after the 0.2.0 release will be documented i
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
66
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.12.3][] - 2025-03-04
9+
10+
Tweaks to python packaging, add beta dimension output, and start moving some commodities to the constexpr mappings
11+
12+
### Changed
13+
14+
- updated nanobind versions [#365][]
15+
- Moved commodity maps to a constexpr structure like the units [#366][]
16+
17+
### Added
18+
19+
- Add aarch64 wheels [#360][]
20+
- Added dimension output string and python class [#366][]
21+
- Added additional operators and tests for python classes [#359][]
22+
23+
[#366]: https://github.com/LLNL/units/pull/366
24+
[#365]: https://github.com/LLNL/units/pull/365
25+
[#360]: https://github.com/LLNL/units/pull/360
26+
[#359]: https://github.com/LLNL/units/pull/359
27+
828
## [0.12.0][] - 2025-01-05
929

1030
Python package release, documentation update, continued addition of new units and other updates and fixes

‎CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ endif()
2323
project(
2424
${UNITS_CMAKE_PROJECT_NAME}
2525
LANGUAGES C CXX
26-
VERSION 0.12.1
26+
VERSION 0.12.3
2727
)
2828
include(CMakeDependentOption)
2929
include(CTest)

‎pyproject.toml‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
[build-system]
2-
requires = ["scikit-build-core >=0.11", "nanobind >=2.5.0"]
2+
requires = ["scikit-build-core >=0.10", "nanobind >=2.5.0"]
3+
34
build-backend = "scikit_build_core.build"
45

56
[project]
67
name = "units_llnl"
7-
version = "0.12.2"
8+
version = "0.12.3"
89
description = "Python bindings for the LLNL units library"
910
readme = "python/README.md"
1011
requires-python = ">=3.10"
@@ -67,6 +68,7 @@ cmake.args = ["-DUNITS_BUILD_PYTHON_LIBRARY=ON", "-DUNITS_ENABLE_TESTS=OFF"]
6768
[tool.cibuildwheel]
6869
# Necessary to see build output from the actual compilation
6970
build-verbosity = 1
71+
skip = "pp311-macosx_arm64"
7072

7173
# Run pytest to ensure that the package was correctly built
7274
test-command = "pytest {project}/test/python"

‎python/units_python.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,30 @@ NB_MODULE(units_llnl_ext, mod)
589589
!is_error(measurement.units()));
590590
});
591591

592+
struct Dimension {
593+
units::precise_unit base;
594+
};
595+
596+
nb::class_<Dimension>(
597+
mod, "Dimension", "a dimensional representation of a unit")
598+
.def(nb::init<>())
599+
.def(
600+
"__init__",
601+
[](Dimension* dim, const units::precise_unit& type) {
602+
new (dim)
603+
Dimension{units::precise_unit(1.0, type.base_units())};
604+
},
605+
"unit"_a)
606+
.def(
607+
"__init__",
608+
[](Dimension* dim, const char* arg0) {
609+
new (dim) Dimension{units::default_unit(arg0)};
610+
},
611+
"dimension"_a)
612+
.def("default_unit", [](const Dimension& dim) { return (dim.base); })
613+
.def("__repr__", [](const Dimension& dim) {
614+
return units::dimensions(dim.base);
615+
});
592616
mod.def(
593617
"convert",
594618
[](double val,

‎test/test_unit_strings.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,15 @@ TEST(defaultUnits, rate)
17121712
EXPECT_EQ(default_unit("rate of mass"), precise::kg / s);
17131713
}
17141714

1715+
TEST(defaultUnits, dimensions)
1716+
{
1717+
EXPECT_EQ(dimensions(default_unit("length")), "length");
1718+
EXPECT_EQ(dimensions(default_unit("area")), "area");
1719+
EXPECT_EQ(dimensions(ft), "length");
1720+
EXPECT_EQ(dimensions(precise::pu * precise::MW), "dimensionless");
1721+
EXPECT_EQ(dimensions(precise::MW.pow(2)), "unknown");
1722+
}
1723+
17151724
TEST(commoditizedUnits, basic)
17161725
{
17171726
auto commu = unit_from_string("meter{cloth}");

‎units/commodities.cpp‎

Lines changed: 14 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Lawrence Livermore National Security, LLC;
44
See the top-level NOTICE for additional details. All rights reserved.
55
SPDX-License-Identifier: BSD-3-Clause
66
*/
7+
#include "commodity_conversion_maps.hpp"
78
#include "units.hpp"
8-
99
#include <algorithm>
1010
#include <array>
1111
#include <atomic>
@@ -97,6 +97,19 @@ enum commodity : std::uint32_t
9797
namespace UNITS_NAMESPACE {
9898
namespace commodities {
9999
using commodityMap = std::unordered_map<std::uint32_t, const char*>;
100+
using commodityNameMap = std::unordered_map<std::string, std::uint32_t>;
101+
102+
static commodityNameMap loadDefinedCommodities()
103+
{
104+
commodityNameMap knownCommodities{};
105+
for (const auto& ccode : defined_commodity_codes) {
106+
if (ccode.first != nullptr) {
107+
knownCommodities.emplace(ccode.first, ccode.second);
108+
}
109+
}
110+
return knownCommodities;
111+
}
112+
static const commodityNameMap commodity_codes = loadDefinedCommodities();
100113
static const commodityMap commodity_names{
101114
{water, "water"},
102115
// metals
@@ -173,99 +186,6 @@ namespace commodities {
173186
// that might somehow get generated
174187
};
175188

176-
using commodityNameMap = std::unordered_map<std::string, std::uint32_t>;
177-
static const commodityNameMap commodity_codes{
178-
{"_", 0}, // null commodity code, would cause some screwy things with
179-
// the strings
180-
{"__", 0}, // null commodity code, would cause some screwy things with
181-
// the strings
182-
{"___", 0}, // null commodity code, would cause some screwy things with
183-
// the strings
184-
{"____", 0}, // null commodity code, would cause some screwy things
185-
// with the strings
186-
{"_____", 0}, // null commodity code, would cause some screwy things
187-
// with the strings
188-
{"water", water},
189-
// metals
190-
{"gold", gold},
191-
{"copper", copper},
192-
{"silver", silver},
193-
{"platinum", platinum},
194-
{"palladium", palladium},
195-
{"zinc", zinc},
196-
{"tin", tin},
197-
{"lead", lead},
198-
{"aluminum", aluminum},
199-
{"alluminum_alloy", alluminum_alloy},
200-
{"nickel", nickel},
201-
{"cobalt", cobalt},
202-
{"molybdenum", molybdenum},
203-
{"carbon", carbon},
204-
205-
// energy
206-
{"oil", oil},
207-
{"heat_oil", heat_oil},
208-
{"nat_gas", nat_gas},
209-
{"brent_crude", brent_crude},
210-
{"ethanol", ethanol},
211-
{"propane", propane},
212-
// grains
213-
{"wheat", wheat},
214-
{"corn", corn},
215-
{"soybeans", soybeans},
216-
{"soybean_meal", soybean_meal},
217-
{"soybean_oil", soybean_oil},
218-
{"oats", oats},
219-
{"rice", rice},
220-
{"durum_wheat", durum_wheat},
221-
{"canola", canola},
222-
{"rough_rice", rough_rice},
223-
{"rapeseed", rapeseed},
224-
{"adzuci", adzuki},
225-
{"adzuki", adzuki},
226-
{"barley", barley},
227-
// meats
228-
{"live_cattle", live_cattle},
229-
{"feeder_cattle", feeder_cattle},
230-
{"lean_hogs", lean_hogs},
231-
{"milk", milk},
232-
233-
// soft
234-
{"cotton", cotton},
235-
{"orange_juice", orange_juice},
236-
{"sugar", sugar},
237-
{"sugar_11", sugar_11},
238-
{"coffee", coffee},
239-
{"cocoa", cocoa},
240-
{"palm_oil", palm_oil},
241-
{"rubber", rubber},
242-
{"wool", wool},
243-
{"lumber", lumber},
244-
245-
// other common unit blocks
246-
{"people", people},
247-
{"cars", vehicle},
248-
{"vehicle", vehicle},
249-
// clinical
250-
{"tissue", tissue},
251-
{"cell", cell},
252-
{"cells", cell},
253-
{"embryo", embryo},
254-
{"hahnemann", Hahnemann},
255-
{"korsakov", Korsakov},
256-
{"protein", protein},
257-
{"creatinine", creatinine},
258-
{"prot", protein},
259-
{"creat", creatinine},
260-
// computer
261-
{"voxel", voxel},
262-
{"pixel", pixel},
263-
{"vox", voxel},
264-
{"pix", pixel},
265-
{"dot", pixel},
266-
{"error", errors},
267-
{"errors", errors},
268-
};
269189
} // namespace commodities
270190

271191
namespace hashcodes {

‎units/commodity_conversion_maps.hpp‎

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,102 @@ namespace UNITS_NAMESPACE {
1717
namespace commodities {
1818
UNITS_CPP14_CONSTEXPR_OBJECT
1919
std::array<std::pair<const char*, std::uint32_t>, 1213>
20-
defined_commodity_codes{
20+
defined_commodity_codes{{
21+
{"_",
22+
0U}, // null commodity code, would cause some screwy things with
23+
// the strings
24+
{"__",
25+
0U}, // null commodity code, would cause some screwy things with
26+
// the strings
27+
{"___",
28+
0U}, // null commodity code, would cause some screwy things with
29+
// the strings
30+
{"____",
31+
0U}, // null commodity code, would cause some screwy things
32+
// with the strings
33+
{"_____",
34+
0U}, // null commodity code, would cause some screwy things
35+
// with the strings
36+
{"water", water},
37+
// metals
38+
{"gold", gold},
39+
{"copper", copper},
40+
{"silver", silver},
41+
{"platinum", platinum},
42+
{"palladium", palladium},
43+
{"zinc", zinc},
44+
{"tin", tin},
45+
{"lead", lead},
46+
{"aluminum", aluminum},
47+
{"alluminum_alloy", alluminum_alloy},
48+
{"nickel", nickel},
49+
{"cobalt", cobalt},
50+
{"molybdenum", molybdenum},
51+
{"carbon", carbon},
52+
53+
// energy
54+
{"oil", oil},
55+
{"heat_oil", heat_oil},
56+
{"nat_gas", nat_gas},
57+
{"brent_crude", brent_crude},
58+
{"ethanol", ethanol},
59+
{"propane", propane},
60+
// grains
61+
{"wheat", wheat},
62+
{"corn", corn},
63+
{"soybeans", soybeans},
64+
{"soybean_meal", soybean_meal},
65+
{"soybean_oil", soybean_oil},
66+
{"oats", oats},
67+
{"rice", rice},
68+
{"durum_wheat", durum_wheat},
69+
{"canola", canola},
70+
{"rough_rice", rough_rice},
71+
{"rapeseed", rapeseed},
72+
{"adzuci", adzuki},
73+
{"adzuki", adzuki},
74+
{"barley", barley},
75+
// meats
76+
{"live_cattle", live_cattle},
77+
{"feeder_cattle", feeder_cattle},
78+
{"lean_hogs", lean_hogs},
79+
{"milk", milk},
80+
81+
// soft
82+
{"cotton", cotton},
83+
{"orange_juice", orange_juice},
84+
{"sugar", sugar},
85+
{"sugar_11", sugar_11},
86+
{"coffee", coffee},
87+
{"cocoa", cocoa},
88+
{"palm_oil", palm_oil},
89+
{"rubber", rubber},
90+
{"wool", wool},
91+
{"lumber", lumber},
92+
93+
// other common unit blocks
94+
{"people", people},
95+
{"cars", vehicle},
96+
{"vehicle", vehicle},
97+
// clinical
98+
{"tissue", tissue},
99+
{"cell", cell},
100+
{"cells", cell},
101+
{"embryo", embryo},
102+
{"hahnemann", Hahnemann},
103+
{"korsakov", Korsakov},
104+
{"protein", protein},
105+
{"creatinine", creatinine},
106+
{"prot", protein},
107+
{"creat", creatinine},
108+
// computer
109+
{"voxel", voxel},
110+
{"pixel", pixel},
111+
{"vox", voxel},
112+
{"pix", pixel},
113+
{"dot", pixel},
114+
{"error", errors},
115+
{"errors", errors},
21116
{"AFN", generateCurrencyCode("AFN")},
22117
{"971", generateCurrencyCode("AFN")},
23118
{"ALL", generateCurrencyCode("ALL")},
@@ -468,7 +563,7 @@ namespace commodities {
468563
{"962", generateCurrencyCode("XPT")},
469564
{"XAG", generateCurrencyCode("XAG")},
470565
{"961", generateCurrencyCode("XAG")},
471-
};
566+
}};
472567

473568
} // namespace commodities
474569
} // namespace UNITS_NAMESPACE

0 commit comments

Comments
 (0)