Temperature Conversions#
Temperature is special in unit systems because Celsius is an offset scale — 0 degrees C is not “zero temperature.” brainunit uses Kelvin as the base temperature unit and provides conversion functions for Celsius.
import brainunit as u
import jax.numpy as jnp
Kelvin — The Base Temperature Unit#
In brainunit, kelvin (K) is the standard SI temperature unit.
All temperature Quantity objects use kelvin.
# Create temperature quantities in kelvin
room_temp = 293.15 * u.kelvin
print('Room temperature:', room_temp)
print('Unit:', room_temp.unit)
print('Dimension:', room_temp.dim)
Room temperature: 293.15 K
Unit: K
Dimension: K
# Temperature arrays
temps = jnp.array([273.15, 293.15, 310.15, 373.15]) * u.kelvin
print('Temperature array:', temps)
Temperature array: [273.1499939 293.1499939 310.1499939 373.1499939] K
# Arithmetic with kelvin quantities
delta_T = 10.0 * u.kelvin
print('Room temp + 10K:', room_temp + delta_T)
print('Temperature difference:', (373.15 * u.kelvin) - room_temp)
Room temp + 10K: 303.15 K
Temperature difference: 80. K
Celsius to Kelvin: celsius2kelvin()#
Converts a plain numeric value (degrees Celsius) to a Quantity in kelvin.
Formula: K = C + 273.15
# Freezing point of water
print('0 C =', u.celsius2kelvin(0.0))
# Boiling point of water
print('100 C =', u.celsius2kelvin(100.0))
# Body temperature
print('37 C =', u.celsius2kelvin(37.0))
# Absolute zero
print('-273.15 C =', u.celsius2kelvin(-273.15))
0 C = 273.15 K
100 C = 373.15 K
37 C = 310.15 K
-273.15 C = 0. K
# Works with arrays too
celsius_values = jnp.array([-40., 0., 20., 37., 100.])
kelvin_values = u.celsius2kelvin(celsius_values)
print('Celsius:', celsius_values)
print('Kelvin:', kelvin_values)
Celsius: [-40. 0. 20. 37. 100.]
Kelvin: [233.1499939 273.1499939 293.1499939 310.1499939 373.1499939] K
# The result is a proper Quantity — use it in further computations
T = u.celsius2kelvin(25.0)
print('Type:', type(T))
print('Mantissa:', T.mantissa)
print('Unit:', T.unit)
Type: <class 'saiunit.Quantity'>
Mantissa: 298.15
Unit: K
Kelvin to Celsius: kelvin2celsius()#
Converts a kelvin Quantity to a plain numeric Celsius value.
Formula: C = K - 273.15
# Convert kelvin Quantities back to Celsius
print('273.15 K =', u.kelvin2celsius(273.15 * u.kelvin), 'C')
print('373.15 K =', u.kelvin2celsius(373.15 * u.kelvin), 'C')
print('310.15 K =', u.kelvin2celsius(310.15 * u.kelvin), 'C')
273.15 K = 0.0 C
373.15 K = 100.0 C
310.15 K = 37.0 C
# With arrays
kelvin_arr = jnp.array([233.15, 273.15, 293.15, 310.15, 373.15]) * u.kelvin
celsius_arr = u.kelvin2celsius(kelvin_arr)
print('Kelvin:', kelvin_arr)
print('Celsius:', celsius_arr)
Kelvin: [233.1499939 273.1499939 293.1499939 310.1499939 373.1499939] K
Celsius: [-40. 0. 20. 37. 100.]
# kelvin2celsius returns a plain array (not a Quantity)
result = u.kelvin2celsius(300.0 * u.kelvin)
print('Result:', result)
print('Type:', type(result))
Result: 26.850000000000023
Type: <class 'float'>
Error Handling#
Both functions validate their inputs.
# celsius2kelvin rejects Quantity inputs (expects plain numbers)
try:
u.celsius2kelvin(100.0 * u.kelvin) # Wrong: passing a Quantity
except TypeError as e:
print('celsius2kelvin error:', e)
celsius2kelvin error: The input value should be not be a Quantity.
# kelvin2celsius rejects non-Quantity inputs
try:
u.kelvin2celsius(100.0) # Wrong: passing a plain number
except TypeError as e:
print('kelvin2celsius error:', e)
kelvin2celsius error: The input value should be a Quantity with a temperature unit.
# kelvin2celsius rejects wrong dimensions
try:
u.kelvin2celsius(100.0 * u.meter) # Wrong: not a temperature
except TypeError as e:
print('kelvin2celsius dim error:', e)
kelvin2celsius dim error: The input value should be a Quantity with a temperature unit, but got unit m.
The zero_celsius Constant#
brainunit provides zero_celsius as a predefined constant — the kelvin value of 0 degrees Celsius.
print('zero_celsius:', u.constants.zero_celsius)
# Use it for manual conversion
celsius_25 = 25.0
kelvin_25 = celsius_25 * u.kelvin + u.constants.zero_celsius
print('25 C in K:', kelvin_25)
zero_celsius: 273.15 K
25 C in K: 298.15 K
Practical Example: Ideal Gas Law#
Use temperature conversion in a real physics calculation:
PV = nRT
# Ideal gas law: P = nRT/V
R_gas = u.constants.gas # gas constant
print('Gas constant R:', R_gas)
n = 1.0 # 1 mole
V = 0.0224 * u.meter**3 # ~22.4 liters
# At different temperatures (in Celsius)
for temp_c in [0.0, 25.0, 100.0]:
T = u.celsius2kelvin(temp_c)
P = n * R_gas * T / V
print(f' T = {temp_c} C ({T}) -> P = {P}')
Gas constant R: 8.314463 J / (K * mol)
T = 0.0 C (273.15 K) -> P = 101388.19 m^-1 kg s^-2 mol^-1
T = 25.0 C (298.15 K) -> P = 110667.73 m^-1 kg s^-2 mol^-1
T = 100.0 C (373.15 K) -> P = 138506.33 m^-1 kg s^-2 mol^-1
Roundtrip Conversion#
# Verify roundtrip: C -> K -> C
original = 37.0
in_kelvin = u.celsius2kelvin(original)
back_to_celsius = u.kelvin2celsius(in_kelvin)
print(f'{original} C -> {in_kelvin} -> {back_to_celsius} C')
37.0 C -> 310.15 K -> 37.0 C
Summary#
Function |
Input |
Output |
Formula |
|---|---|---|---|
|
Plain number (Celsius) |
|
K = C + 273.15 |
|
|
Plain number (Celsius) |
C = K - 273.15 |
|
— |
|
— |