From 61ceb7f224c4c93e93721c3b5b0e81f94387ef64 Mon Sep 17 00:00:00 2001 From: Dan Howe Date: Mon, 10 Sep 2018 19:06:02 +1000 Subject: [PATCH] Add dimensions() function --- scaling/froude.py | 24 ++++++++++++++++++++++++ tests/froude_test.py | 3 +++ 2 files changed, 27 insertions(+) diff --git a/scaling/froude.py b/scaling/froude.py index 9b2fc68..ba017b2 100644 --- a/scaling/froude.py +++ b/scaling/froude.py @@ -71,3 +71,27 @@ def model_to_proto(x_model, length_scale, from_unit, to_unit): length_scale_factor = length_scale return _convert(x_model, length_scale_factor, from_unit, to_unit) + + +def dimensions(unit): + """Get unit dimensions. + + Args: + unit (str): unit name or symbol + + Returns: + string containing unit dimensions + """ + + # Get dimensions of unit + dims = UREG(unit).dimensionality + + dim_data = {'L': '[length]', 'M': '[mass]', 'T': '[time]'} + + s = '' + for symbol, key in dim_data.items(): + exponent = dims[key] + if exponent != 0: + s += '{}^{:g} '.format(symbol, exponent) + + return s.strip() diff --git a/tests/froude_test.py b/tests/froude_test.py index 5b36462..82d7344 100644 --- a/tests/froude_test.py +++ b/tests/froude_test.py @@ -9,3 +9,6 @@ def test_froude_length(): def test_froude_force(): assert froude.model_to_proto(1, 10, 'N', 'N') == 1000 + +def test_unit_dimensions(): + assert froude.dimensions('N') == 'L^1 M^1 T^-2'