Files
polyamorphism_optimization/generate_temperatures.py
2025-08-09 13:43:03 +02:00

58 lines
1.4 KiB
Python
Executable File

#!/usr/bin/python3.12
import numpy as np
import math
def round_dynamic(x, rel_tol=1e-3, max_decimals=6, max_negative_decimals=3):
"""
Dynamically round x so that the relative error is within rel_tol.
Supports negative decimals for large values.
"""
if x == 0:
return 0.0
# Determine the required number of decimals (can be negative)
decimals = -int(math.floor(math.log10(rel_tol * abs(x))))
# Clamp within allowed range
decimals = max(-max_negative_decimals, min(max_decimals, decimals))
if decimals >= 0:
return round(x, decimals)
else:
# Round to nearest 10^(-decimals)
factor = 10 ** (-decimals)
return round(x / factor) * factor
def generate_temperature_list_dynamic(dT=10, Tmin=10, Tmax=5000, rel_tol=5e-3):
r = (300 - dT) / 300.0
temps_down = []
T = 300
while T > Tmin:
T *= r
if T < Tmin:
break
temps_down.append(T)
inv_r = 1.0 / r
temps_up = []
T = 300
while T < Tmax:
T *= inv_r
if T > Tmax:
break
temps_up.append(T)
all_T = np.array(temps_down[::-1] + [300.0] + temps_up)
# Apply dynamic rounding
rounded_T = [round_dynamic(t, rel_tol=rel_tol) for t in all_T]
rounded_T = np.unique(rounded_T)
return rounded_T
T = generate_temperature_list_dynamic(dT=30, rel_tol=5e-3)
print(len(T))
print(T)
print(np.linspace(0, 100000, len(T), endpoint=True))