From fb3fc691ab77748e13b4caf9b5b97f1761721670 Mon Sep 17 00:00:00 2001 From: Dan Howe Date: Thu, 6 Sep 2018 17:14:38 +1000 Subject: [PATCH] Initial commit --- scaling/froude.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 scaling/froude.py diff --git a/scaling/froude.py b/scaling/froude.py new file mode 100644 index 0000000..126a095 --- /dev/null +++ b/scaling/froude.py @@ -0,0 +1,73 @@ +import pint + + +def _convert(x, length_scale_factor, from_unit, to_unit): + + # Initialise unit definitions + ureg = pint.UnitRegistry() + + # Calculate unit conversion factor + from_unit = ureg[from_unit] + to_unit = ureg[to_unit] + unit_conversion_factor = (1 * from_unit).to(to_unit).magnitude + + # Define Froude scaling relationships + froude_l_exponent = 1 + froude_t_exponent = 1 / 2 + froude_m_exponent = 3 + + # Calculate Froude scaling factor + froude_scale_factor = length_scale_factor**( + from_unit.dimensionality['[length]'] * froude_l_exponent + + from_unit.dimensionality['[time]'] * froude_t_exponent + + from_unit.dimensionality['[mass]'] * froude_m_exponent) + + # Scale time (dataframe only) + try: + x.index *= length_scale_factor**froude_t_exponent + except AttributeError: + pass + + # Scale values + x *= froude_scale_factor + + # Convert to output units + x *= unit_conversion_factor + + return x + + +def proto_to_model(x_proto, length_scale, from_unit, to_unit): + """Convert prototype value(s) to model value(s) in specified units. + + Args: + x_proto: prototype values (array_like, or pandas dataframe) + length_scale: ratio between prototype and model dimensions (float) + from_unit (str): unit of input value(s) + to_unit (str): unit of output value(s) + + Returns: + input values in model scale + """ + + length_scale_factor = 1 / length_scale + + return _convert(x_model, length_scale_factor, from_unit, to_unit) + + +def model_to_proto(x_model, length_scale, from_unit, to_unit): + """Convert model value(s) to prototype value(s) in specified units. + + Args: + x_model: model values (array_like, or pandas dataframe) + length_scale: ratio between prototype and model dimensions (float) + from_unit (str): unit of input value(s) + to_unit (str): unit of output value(s) + + Returns: + input values in prototype scale + """ + + length_scale_factor = length_scale + + return _convert(x, length_scale_factor, from_unit, to_unit)