forked from patrick-kidger/diffrax
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunit_aware_nonlinear_heat_pde.py
144 lines (115 loc) · 3.94 KB
/
unit_aware_nonlinear_heat_pde.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright 2024 BDP Ecosystem Limited. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from typing import Callable
import brainunit as u
import equinox as eqx # https://github.com/patrick-kidger/equinox
import jax
import jax.numpy as jnp
import matplotlib.pyplot as plt
from jaxtyping import Array, Float # https://github.com/google/jaxtyping
import diffrax
jax.config.update("jax_enable_x64", True)
# Represents the interval [x0, x_final] discretised into n equally-spaced points.
class SpatialDiscretisation(eqx.Module):
x0: float = eqx.field(static=True)
x_final: float = eqx.field(static=True)
vals: Float[Array, "n"]
@classmethod
def discretise_fn(cls, x0: float, x_final: float, n: int, fn: Callable):
if n < 2:
raise ValueError("Must discretise [x0, x_final] into at least two points")
vals = jax.vmap(fn)(jnp.linspace(x0, x_final, n))
return cls(x0, x_final, vals)
@property
def δx(self):
return (self.x_final - self.x0) / (len(self.vals) - 1)
def binop(self, other, fn):
if isinstance(other, SpatialDiscretisation):
if self.x0 != other.x0 or self.x_final != other.x_final:
raise ValueError("Mismatched spatial discretisations")
other = other.vals
return SpatialDiscretisation(self.x0, self.x_final, fn(self.vals, other))
def __add__(self, other):
return self.binop(other, lambda x, y: x + y)
def __mul__(self, other):
return self.binop(other, lambda x, y: x * y)
def __radd__(self, other):
return self.binop(other, lambda x, y: y + x)
def __rmul__(self, other):
return self.binop(other, lambda x, y: y * x)
def __sub__(self, other):
return self.binop(other, lambda x, y: x - y)
def __rsub__(self, other):
return self.binop(other, lambda x, y: y - x)
def __truediv__(self, other):
return self.binop(other, lambda x, y: x / y)
def laplacian(y: SpatialDiscretisation) -> SpatialDiscretisation:
y_next = jnp.roll(y.vals, shift=1)
y_prev = jnp.roll(y.vals, shift=-1)
Δy = (y_next - 2 * y.vals + y_prev) / (y.δx ** 2)
# Dirichlet boundary condition
Δy = Δy.at[0].set(0)
Δy = Δy.at[-1].set(0)
return SpatialDiscretisation(y.x0, y.x_final, Δy)
# Problem
def vector_field(t, y, args):
dydt = (1 - y) * laplacian(y)
return dydt / u.ms
term = diffrax.ODETerm(vector_field)
# initial condition
ic = lambda x: x ** 2
# Spatial discretisation
x0 = -1
x_final = 1
n = 200
y0 = SpatialDiscretisation.discretise_fn(x0, x_final, n, ic)
# Temporal discretisation
t0 = 0 * u.ms
t_final = 1 * u.ms
δt = 0.0001 * u.ms
saveat = diffrax.SaveAt(ts=u.math.linspace(t0, t_final, 50))
# Tolerances
rtol = 1e-10
atol = 1e-10
stepsize_controller = diffrax.PIDController(
pcoeff=0.3, icoeff=0.4, rtol=rtol, atol=atol, dtmax=0.001 * u.ms
)
solver = diffrax.Tsit5()
sol = diffrax.diffeqsolve(
term,
solver,
t0,
t_final,
δt,
y0,
saveat=saveat,
stepsize_controller=stepsize_controller,
max_steps=None,
)
plt.figure(figsize=(5, 5))
t_final = t_final.to_decimal(u.ms)
t0 = t0.to_decimal(u.ms)
plt.imshow(
sol.ys.vals,
origin="lower",
extent=(x0, x_final, t0, t_final),
aspect=(x_final - x0) / (t_final - t0),
cmap="inferno",
)
plt.xlabel("x")
plt.ylabel("t", rotation=0)
plt.clim(0, 1)
plt.colorbar()
plt.show()