Created
March 9, 2023 20:05
-
-
Save gwbischof/6143100dfcc4f0e13d46c26e8af94b59 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import threading | |
class ThermalMaterial: | |
""" | |
A material that you can heat and cool. | |
Parameters | |
---------- | |
thermal_mass: float, optional | |
How the material's energy relates to its temperature. | |
start_temp: float, optional | |
Starting temperature of the material. | |
ambient_temp: float, optional | |
The temperature of the environment. | |
heater_power: float, optional | |
The rate at which the heater is adding energy to the sample. | |
cooling_constant: float, optional | |
How readily the sample releases energy to the environment. | |
""" | |
def __init__( | |
self, | |
thermal_mass=100, | |
start_temp=100, | |
ambient_temp=0, | |
heater_power=0, | |
cooling_constant=1, | |
): | |
self.energy = start_temp * thermal_mass | |
self.thermal_mass = thermal_mass | |
self.ambient_temp = ambient_temp | |
self._heater_power = heater_power | |
self.cooling_constant = cooling_constant | |
self.time = time.time() | |
self.run = True | |
threading.Thread(target=self._simulate).start() | |
@property | |
def temperature(self): | |
return self.energy / self.thermal_mass | |
@property | |
def heater_power(self): | |
return self._heater_power | |
@heater_power.setter | |
def heater_power(self, value): | |
self._heater_power = value | |
def stop(self): | |
self.run = False | |
def _cooling(self): | |
return -1 * self.cooling_constant * (self.temperature - self.ambient_temp) | |
def _heating(self): | |
return self.heater_power | |
def _simulate(self): | |
while self.run: | |
now = time.time() | |
time_delta = now - self.time | |
self.time = now | |
self.energy += self._cooling() * time_delta | |
self.energy += self._heating() * time_delta | |
time.sleep(0.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment