base, python: Add a Temperature type and associated param
Add a class to represent a temperature. The class stores temperatures in Kelvin and provides helper methods to convert to/from Celsius. The corresponding param type automatically converts from Kelvin, Celsius, and Fahrenheit to the underlying C++ type. Change-Id: I5783cc4f4fecbea5aba9821dfc71bfa77c3f75a9 Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com> Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/39218 Maintainer: Jason Lowe-Power <power.jg@gmail.com> Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br> Reviewed-by: Gabe Black <gabe.black@gmail.com> Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
@@ -292,3 +292,25 @@ def toCurrent(value):
|
||||
|
||||
def toEnergy(value):
|
||||
return toMetricFloat(value, 'energy', 'J')
|
||||
|
||||
def toTemperature(value):
|
||||
"""Convert a string value specified to a temperature in Kelvin"""
|
||||
|
||||
magnitude, unit = toNum(value,
|
||||
target_type='temperature',
|
||||
units=('K', 'C', 'F'),
|
||||
prefixes=metric_prefixes,
|
||||
converter=float)
|
||||
if unit == 'K':
|
||||
kelvin = magnitude
|
||||
elif unit == 'C':
|
||||
kelvin = magnitude + 273.15
|
||||
elif unit == 'F':
|
||||
kelvin = (magnitude + 459.67) / 1.8
|
||||
else:
|
||||
raise ValueError(f"'{value}' needs a valid temperature unit.")
|
||||
|
||||
if kelvin < 0:
|
||||
raise ValueError(f"{value} is an invalid temperature")
|
||||
|
||||
return kelvin
|
||||
|
||||
Reference in New Issue
Block a user