dynadojo.systems.heat.HeatEquation#
- class dynadojo.systems.heat.HeatEquation#
Bases:
SimpleSystem
2D Heat Equation. Adapted from [1]. Models how heat dissipates across a 2D square plate.
References
Example
You can download the source image here.
import numpy as np from PIL import Image import matplotlib.pyplot as plt def grayscale(png_path, basewidth=30): image = Image.open(png_path) wpercent = (basewidth / float(image.size[0])) hsize = int((float(image.size[1]) * float(wpercent))) image = image.resize((basewidth,hsize), Image.Resampling.LANCZOS) grayscale_image = image.convert("L") grey = np.array(grayscale_image) return grey dino = grayscale("../graphics/dino.png") print(dino.shape) latent_dim = dino.shape[0] ** 2 plt.imshow(dino, cmap='gray') plt.axis('off') # Turn off axis labels and ticks plt.show()
from dynadojo.systems.heat import HeatEquation from dynadojo.wrappers import SystemChecker from dynadojo.utils.heat import plot # Create the init conds from the image x0 = np.expand_dims(dino.flatten()[::-1], axis=0) x0 *= 2 n = 1 timesteps = 20 embed_dim = latent_dim system = SystemChecker(HeatEquation(latent_dim, embed_dim, noise_scale=0)) x = system.make_data(x0, timesteps=timesteps) plot(x[0], timesteps, system._system.dt, savefile="../graphics/hot_dino.gif")
Methods
__init__
([latent_dim, embed_dim, alpha, dx, ...])Initialize the class.
calc_control_cost
(control)Calculates the L2 norm / dimension of every vector in the control
calc_dynamics
(t, x)Calculates the dynamics for the system.
calc_error
(x, y)Returns the MSE error normalized by the embedded dimension.
make_data
(init_conds, control, timesteps[, ...])Uses the
calc_dynamics()
method to generate data.make_init_conds
(n[, in_dist])Uniformly samples embedded-dimensional points from an inside or outside distribution
Attributes
The controller matrix.
The embedded dimension for the system.
The embedder matrix.
The latent dimension for the system.
The random seed for the system.
- __init__(latent_dim=4, embed_dim=4, alpha=2, dx=1, IND_range=(0, 100), OOD_range=(-100, 0), seed=None, **kwargs)#
Initialize the class.
- Parameters:
alpha (float) – Thermal diffusivity. Should be positive.
dx (float) – \(\delta x\).
latent_dim (int) – The length of the square plate. Must be a perfect square.
embed_dim (int) – Must be the same as the latent dimension.
seed (int or None, optional) – Seed for random number generation.
embedder_sv_range (tuple) – The singular value range for the embedder matrix. Singular values are non-negative by convention. The singular values should exclude 0 to ensure the embedder is invertible.
controller_sv_range (tuple) – The singular value range for the controller matrix.
IND_range (tuple) – The in-distribution range of possible starting temperatures.
OOD_Range (tuple) – The out-of-distribution range of possible starting temperatures.
**kwargs – Additional keyword arguments.
- calc_control_cost(control)#
Calculates the L2 norm / dimension of every vector in the control
- Parameters:
control (ndarray) –
- Return type:
float
- calc_dynamics(t, x)#
Calculates the dynamics for the system. Your class must implement this.
- calc_error(x, y)#
Returns the MSE error normalized by the embedded dimension.
- Return type:
float
- property controller#
The controller matrix. For example, in a system \(\dot{x} = Ax + Bu\), the controller is \(B\).
- property embed_dim#
The embedded dimension for the system.
- property embedder#
The embedder matrix. An invertible map from the latent space to the embedding space.
- property latent_dim#
The latent dimension for the system.
- make_data(init_conds, control, timesteps, noisy=False)#
Uses the
calc_dynamics()
method to generate data. Mathematically, data is generated like \(\dot{x} = f(x) + Bu\). Where \(f(x)\) is given bycalc_dynamics()
.- Parameters:
init_conds (numpy.ndarray) – (n, embed_dim) Initial conditions matrix.
control (numpy.ndarray) – (n, timesteps, embed_dim) Controls tensor.
timesteps (int) – Timesteps per training trajectory (per action horizon).
noisy (bool, optional) – If True, add noise to trajectories. Defaults to False. If False, no noise is added.
- Returns:
(n, timesteps, embed_dim) Trajectories tensor.
- Return type:
numpy.ndarray
- make_init_conds(n, in_dist=True)#
Uniformly samples embedded-dimensional points from an inside or outside distribution
Note
Systems developers determine what counts as in vs out-of-distribution. DynaDojo doesn’t provide any verification that this distinction makes sense or even exists. See
LDSystem
for a principled example.- Parameters:
n (int) – Number of initial conditions.
in_dist (bool, optional) – If True, generate in-distribution initial conditions. Defaults to True. If False, generate out-of-distribution initial conditions.
- Returns:
(n, embed_dim) Initial conditions matrix.
- Return type:
numpy.ndarray
- property seed#
The random seed for the system.