-
Notifications
You must be signed in to change notification settings - Fork 7
feat(units): add an expression parser #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
bb9b69d
feat(units): add expression parser with SI dimensional analysis
HaoZeke bff8428
test(units): address review feedback on tests
HaoZeke eb8e28a
docs(units): move unit docs to Doxygen, simplify misc.rst
HaoZeke 1aa7e0e
fix: address remaining review items
HaoZeke 006284d
style: use literal µ instead of \u00b5 escape in Python tests
HaoZeke 7eb3d41
style: use literal µ instead of hex escapes in C++ sources
HaoZeke 4c8ab76
fix: ASCII-only tolower and restore documentation.py stub
HaoZeke bf743a1
fix: address Luthaf review on unit expression parser
HaoZeke 71331c1
fix: add [[deprecated]] attribute to 3-arg unit_conversion_factor
HaoZeke cb9756a
fix: make unit_conversion_factor TorchScript-compatible
HaoZeke 7ba0e25
fix: add units.hpp to umbrella header, use unit_conversion_factor in …
HaoZeke e9217ad
style: fix ruff format in units test
HaoZeke 3762bf1
fix(units): throw error on empty unit strings instead of returning 1.0
HaoZeke e8ba70f
fix(units): add overflow/underflow checks for arithmetic operations
HaoZeke 1e16a03
docs(units): document dimension comparison tolerance choice
HaoZeke 40a669c
chor(units): add DimIndex enum for dim vec indices
HaoZeke 6c2ee73
perf(units): add thread-local cache for parsed unit expressions
HaoZeke 8fd89fa
improve(units): use human-readable dimension string format
HaoZeke 3a7893d
test(units): add overflow/underflow detection tests
HaoZeke edb7548
test(units): add accumulated floating-point error tests
HaoZeke 44a8304
docs(units): clarify quantity validation behavior
HaoZeke f72635b
docs(units): add migration guide
HaoZeke 4e7a87b
fix(units): revert to backwards-compatible empty string handling
HaoZeke eaf9e34
test(units): add multithreading tests for thread-local cache
HaoZeke c325846
chore(units): even more docs
HaoZeke 86b6d1a
chore(lint): apply
HaoZeke b378421
fix(units): remove extra namespace closing brace
HaoZeke 7678e68
test(units): fix overflow tests to use same-dimension conversions
HaoZeke 7454923
test(units): fix overflow tests to use units with extreme SI factors
HaoZeke fb312d2
test(units): fix division overflow test to use same dimension
HaoZeke 0c479b7
fix: use single unit_conversion_factor op with argument dispatch
HaoZeke 3417510
fix: use double return type in lambda, update test
HaoZeke 090fbf9
docs: add comprehensive unit table with µm/µs support
HaoZeke dd9b8ee
docs: update unit documentation for expression parser API
HaoZeke 37899dd
fix(docs): add missing label and fix title underline in misc.rst
HaoZeke f29211f
fix: address review round 3 comments
HaoZeke 88c0c33
fix(docs): update all cross-refs to renamed _known-base-units label
HaoZeke ef3a1df
test: add coverage for version_compatible, unit types, and kwargs compat
HaoZeke c7a839b
chore(pragma): swallow deprecations
HaoZeke 10a5e2c
feat(units): more flexible argument management
HaoZeke 3a98e26
style: apply ruff formatting to tests/units.py
HaoZeke d987f48
refactor(units): remove thread-local cache (YAGNI per review)
HaoZeke 39bb015
Cleanup
Luthaf b0fcc4a
Use tigher tests
Luthaf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #ifndef METATOMIC_TORCH_UNITS_HPP | ||
| #define METATOMIC_TORCH_UNITS_HPP | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "metatomic/torch/exports.h" | ||
|
|
||
| namespace metatomic_torch { | ||
|
|
||
| /// Check that a given physical quantity is valid and known. This is | ||
| /// intentionally not exported with `METATOMIC_TORCH_EXPORT`, and is only | ||
| /// intended for internal use. | ||
| /// | ||
| /// Known quantities are: "length", "energy", "force", "pressure", "momentum", | ||
| /// "mass", "velocity", and "charge". | ||
| bool valid_quantity(const std::string& quantity); | ||
|
|
||
| /// Check that a given unit is valid and known for some physical quantity. This | ||
| /// is intentionally not exported with `METATOMIC_TORCH_EXPORT`, and is only | ||
| /// intended for internal use. | ||
| /// | ||
| /// This function parses the unit expression and verifies that its physical | ||
| /// dimensions match the expected dimensions for the given quantity. For example, | ||
| /// `validate_unit("energy", "eV")` succeeds, but `validate_unit("energy", "eV/A")` | ||
| /// throws an error because eV/A has dimensions of force, not energy. | ||
| void validate_unit(const std::string& quantity, const std::string& unit); | ||
|
|
||
| /// Get the multiplicative conversion factor to use to convert from | ||
| /// `from_unit` to `to_unit`. Both units are parsed as expressions (e.g. | ||
| /// "kJ/mol/A^2", "(eV*u)^(1/2)") and their dimensions must match. | ||
| /// | ||
| /// Unit expressions are built from base units combined with `*`, `/`, `^`, | ||
| /// and parentheses. Unit lookup is case-insensitive, and whitespace is | ||
| /// ignored. For example: | ||
| /// | ||
| /// - `"kJ/mol"` -- energy per mole | ||
| /// - `"eV/Angstrom^3"` -- pressure | ||
| /// - `"(eV*u)^(1/2)"` -- momentum (fractional powers) | ||
| /// - `"Hartree/Bohr"` -- force in atomic units | ||
| /// | ||
| /// Supported base units: | ||
| /// | ||
| /// - **Length**: angstrom (A), bohr, nanometer (nm), meter (m), | ||
| /// centimeter (cm), millimeter (mm), micrometer (um, µm) | ||
| /// - **Energy**: eV, meV, Hartree, Ry (rydberg), Joule (J), kcal, kJ | ||
| /// (note: kcal and kJ are bare; write kcal/mol for per-mole) | ||
| /// - **Time**: second (s), millisecond (ms), microsecond (us, µs), | ||
| /// nanosecond (ns), picosecond (ps), femtosecond (fs) | ||
| /// - **Mass**: dalton (u), kilogram (kg), gram (g), electron_mass (m_e) | ||
| /// - **Charge**: e, coulomb (c) | ||
| /// - **Dimensionless**: mol | ||
| /// - **Derived**: hbar | ||
| /// | ||
| /// Note on quantity validation: | ||
| /// The 2-argument form `unit_conversion_factor(from_unit, to_unit)` does not | ||
| /// take a quantity parameter. Dimensional compatibility is checked automatically | ||
| /// by comparing the parsed dimensions of both unit expressions. The deprecated | ||
| /// 3-argument form accepts a `quantity` parameter, but it is ignored for the | ||
| /// conversion calculation - it only emits a deprecation warning. | ||
| METATOMIC_TORCH_EXPORT double unit_conversion_factor( | ||
| const std::string& from_unit, | ||
| const std::string& to_unit | ||
| ); | ||
|
|
||
| /// @deprecated Use the 2-argument overload instead. The `quantity` parameter | ||
| /// is ignored; dimensional compatibility is checked by the parser. Emits a | ||
| /// one-time runtime deprecation warning. | ||
| [[deprecated("use the 2-argument unit_conversion_factor(from_unit, to_unit) instead")]] | ||
| METATOMIC_TORCH_EXPORT double unit_conversion_factor( | ||
|
HaoZeke marked this conversation as resolved.
|
||
| const std::string& quantity, | ||
| const std::string& from_unit, | ||
| const std::string& to_unit | ||
| ); | ||
|
|
||
| } | ||
|
|
||
| #endif | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.