Refactor input and output units

master
Dan Howe 6 years ago
parent 1c3ca1849a
commit 6e15b63087

@ -9,18 +9,18 @@ TIME_EXPONENT = 1 / 2
MASS_EXPONENT = 3 MASS_EXPONENT = 3
def _convert(x, length_scale_factor, from_unit, to_unit): def _convert(x, length_scale_factor, input_unit, target_unit):
# Calculate unit conversion factor # Calculate unit conversion factor
from_unit = UREG(from_unit) input_unit = UREG(input_unit)
to_unit = UREG(to_unit) target_unit = UREG(target_unit)
unit_conversion_factor = from_unit.to(to_unit).magnitude unit_conversion_factor = input_unit.to(target_unit).magnitude
# Calculate Froude scaling factor # Calculate Froude scaling factor
froude_scale_factor = length_scale_factor**( froude_scale_factor = length_scale_factor**(
from_unit.dimensionality['[length]'] * LENGTH_EXPONENT + input_unit.dimensionality['[length]'] * LENGTH_EXPONENT +
from_unit.dimensionality['[time]'] * TIME_EXPONENT + input_unit.dimensionality['[time]'] * TIME_EXPONENT +
from_unit.dimensionality['[mass]'] * MASS_EXPONENT) input_unit.dimensionality['[mass]'] * MASS_EXPONENT)
# Scale values # Scale values
x_scaled = x * froude_scale_factor x_scaled = x * froude_scale_factor
@ -37,14 +37,14 @@ def _convert(x, length_scale_factor, from_unit, to_unit):
return x_scaled return x_scaled
def proto_to_model(x_proto, length_scale, from_unit, to_unit): def proto_to_model(x_proto, length_scale, input_unit, target_unit):
"""Convert prototype value(s) to model value(s) in specified units. """Convert prototype value(s) to model value(s) in specified units.
Args: Args:
x_proto: prototype values (array_like, or pandas dataframe) x_proto: prototype values (array_like, or pandas dataframe)
length_scale: ratio between prototype and model dimensions (float) length_scale: ratio between prototype and model dimensions (float)
from_unit (str): unit of input value(s) input_unit: unit of input (string)
to_unit (str): unit of output value(s) target_unit: unit of output (string)
Returns: Returns:
input values in model scale input values in model scale
@ -52,17 +52,17 @@ def proto_to_model(x_proto, length_scale, from_unit, to_unit):
length_scale_factor = 1 / length_scale length_scale_factor = 1 / length_scale
return _convert(x_proto, length_scale_factor, from_unit, to_unit) return _convert(x_proto, length_scale_factor, input_unit, target_unit)
def model_to_proto(x_model, length_scale, from_unit, to_unit): def model_to_proto(x_model, length_scale, input_unit, target_unit):
"""Convert model value(s) to prototype value(s) in specified units. """Convert model value(s) to prototype value(s) in specified units.
Args: Args:
x_model: model values (array_like, or pandas dataframe) x_model: model values (array_like, or pandas dataframe)
length_scale: ratio between prototype and model dimensions (float) length_scale: ratio between prototype and model dimensions (float)
from_unit (str): unit of input value(s) input_unit: unit of input (string)
to_unit (str): unit of output value(s) target_unit: unit of output (string)
Returns: Returns:
input values in prototype scale input values in prototype scale
@ -70,14 +70,14 @@ def model_to_proto(x_model, length_scale, from_unit, to_unit):
length_scale_factor = length_scale length_scale_factor = length_scale
return _convert(x_model, length_scale_factor, from_unit, to_unit) return _convert(x_model, length_scale_factor, input_unit, target_unit)
def dimensions(unit): def dimensions(unit):
"""Get unit dimensions. """Get unit dimensions.
Args: Args:
unit (str): unit name or symbol unit: unit name or symbol (string)
Returns: Returns:
string containing unit dimensions string containing unit dimensions
@ -101,7 +101,7 @@ def scaling_exponent(unit):
"""Convert prototype value(s) to model value(s) in specified units. """Convert prototype value(s) to model value(s) in specified units.
Args: Args:
unit (str): unit of quantity to be scaled unit: unit of quantity to be scaled (string)
Returns: Returns:
scaling factor scaling factor

Loading…
Cancel
Save