ODE Class
- class ODE(method='dopri5', adjoint=False, requires_grad=True, default_type=torch.float64)
Bases:
ModuleBase class to define, solve, and visualize a system of ODEs.
- Parameters:
method (str) – Name of ODE solver to use. Default is
dopri5.adjoint (bool) – Whether or not to use the adjoint method for backpropagating through ODE solutions. Default is
False.requires_grad (bool) – Whether or not gradients should be computed for the tensors defining the ODE system.
default_type (type) – Set the default
torch.Tensortype.
- Variables:
odeint (
torchdiffeq.odeintortorchdiffeq.odeint_adjoint) – Numerical integrator for a system of ODEs given an initial value. The adjoint method will not be used ifrequires_gradisFalse.
- get_batch(batch_time, batch_size)
Sample a batch of
t,y0, andy.- Parameters:
- Returns:
t (
torch.tensorof shape(batch_time)) – Tensor ofbatch_timetime points.y0 (
torch.tensorof shape(batch_size,...,D)) – Initial states forbatch_sizeinitial conditions.y (
torch.tensorof shape(batch_time,batch_size,...,D)) – Solution evaluated atbatch_timetime points forbatch_sizeinitial conditions.
- plot_frame(ax, y, ntype=None, vmin=None, vmax=None, alpha=0.9, extent=None)
Plot a single frame of an ODE solution.
- Parameters:
ax (
matplotlib.axes) – Axis object on which to display the solution.y (
torch.tensor) – Solution of the system to plot. The input should be either reshaped to the dimensions of the simulation box or have a final dimension of 2 (only valid forntype = none).ntype (str) –
Type of normalization to apply when displaying the image. The options are:
none– No normalization; point cloud datasym– Symmetric linear normalization scaleunit– Linear normalization between 0 and 1mod– Linear normalization modulo \(2 \pi\)log– Logarithmic normalization scalesymlog– Symmetric logarithmic normalization scaleNone– Linear normalization between the min. and max. values ofy
The default is
None.vmin (float, optional) – Minimum normalization value.
vmax (float, optional) – Maximum normalization value.
alpha (float between 0 and 1, optional) – Opacity of the image.
extent (list or tuple of floats (left, right, bottom, top), optional) – Coordinates of the bounding box of the image.
- Returns:
sm – Object mapping scalar data to RGBA color values.
- Return type:
matplotlib.cm.ScalarMappable
- plot_series(y, ntype=None, vmin=None, vmax=None, clabel=None, colors=None)
Plot a time series of frames of an ODE solution.
- Parameters:
y (
torch.tensor) – Solution of the system to plot. The input should be either reshaped to the dimensions of the simulation box or have a final dimension of 2 (only valid forntype = none). Alternately,ycan be a list of 2 solution tensors, which will then be overlaid (only valid forntype = none).ntype (str) –
Type of normalization to apply when displaying the image. The options are:
none– No normalization; point cloud datasym– Symmetric linear normalization scaleunit– Linear normalization between 0 and 1mod– Linear normalization modulo \(2 \pi\)log– Logarithmic normalization scalesymlog– Symmetric logarithmic normalization scaleNone– Linear normalization between the min. and max. values ofy
The default is
None.vmin (float, optional) – Minimum normalization value.
vmax (float, optional) – Maximum normalization value.
clabel (str, optional) – Text used to label the colorbar.
- solve(t, y0=None, device='cpu', rtol=1e-07, atol=1e-09)
Numerically integrate the ODE system and return the solution at times
t.- Parameters:
t (
torch.tensor) – 1-dimensional tensor of evaluation times.y0 (
torch.tensorof shape(M,...,D)) – Initial state of the system forMinitial conditions.Ddenotes the flattened system size.device (str) – The name of the device on which the computation will be performed (e.g.
cpuorcuda).rtol (float) – Upper bound on relative error. Default is \(10^{-7}\).
atol (float) – Upper bound on absolute error. Default is \(10^{-9}\).
- Returns:
y – Solution evaluated at
Ttime points forMinitial conditions.Ddenotes the flattened system size.- Return type:
torch.tensorof shape(T,M,...,D)
- trim(t0=0)
Trim the solution at early time points to exclude initial transients.
- Parameters:
t0 (int) – The first index into
tat which to return the solution in order to exclude initial transients. The initial state will be set to the solution at this point and initial time set to 0 at this point.
- class Kuramoto(args, method='dopri5', default_type=torch.float64)
Bases:
ODEClass to define, initialize, solve, and visualize the Kuramoto model of coupled oscillators:
\begin{eqnarray*} \frac{d\theta_i}{dt} = \omega + K\sum_{j\in N(i)}\sin(\theta_j-\theta_i) \end{eqnarray*}- Parameters:
args (dict) –
Dictionary of parameters defining the ODE system:
N (int) – Dimension of the simulation box
(N x N)L (float) – Length of the real-space simulation box
(L x L)v (float) – Intrinsic frequency of the oscillators
K (float) – Coupling strength
s (float) – Length scale of striped pattern
method (str) – Name of ODE solver to use. Default is
dopri5.default_type (type) – Set the default
torch.Tensortype.
- Variables:
conv (
torch.nn.Conv2d) – Convolution operator coupling neighboring oscillators.
- forward(t, y)
Evaluate the ODE system at a specified time
tand statey.- Parameters:
t (
torch.tensor) – 1-dimensional tensor of the evaluation time points.y (
torch.tensorof shape(M,1,D)) – State of the system forMinitial conditions.Ddenotes the flattened system size.
- Returns:
dy/dt – Derivative of the system.
- Return type:
torch.tensorof shape(M,1,D)
- init_state(M=1, seed=12)
Randomly generate the initial state(s) of the ODE system.
- Parameters:
- Variables:
y0 (
torch.tensorof shape(M,1,D)) – Initial state of the system forMinitial conditions.Ddenotes the flattened system size.
- class LotkaVolterra(args, method='dopri5', default_type=torch.float64)
Bases:
ODEClass to define, initialize, solve, and visualize a point cloud evolving according to the Lotka-Volterra model:
\begin{eqnarray*} \frac{dx}{dt} & = & \alpha x - \beta xy \\ \frac{dy}{dt} & = & \delta xy - \gamma y \end{eqnarray*}- Parameters:
args (dict) –
Dictionary of parameters defining the ODE system:
N (int) – Dimension of the simulation box
(N x N)L (float) – Length of the real-space simulation box
(L x L)R (float) – Radius of particles
alpha, beta, gamma, delta (float) – Interaction parameters
method (str) – Name of ODE solver to use. Default is
dopri5.default_type (type) – Set the default
torch.Tensortype.
- forward(t, y)
Evaluate the ODE system at a specified time
tand statey.- Parameters:
t (
torch.tensor) – 1-dimensional tensor of the evaluation time points.y (
torch.tensorof shape(M,N,2)) – State of the system forMinitial conditions.
- Returns:
dy/dt – Derivative of the system.
- Return type:
torch.tensorof shape(M,N,2)
- init_state(M=1, seed=12)
Randomly generate the initial state(s) of the ODE system.
- class ODEGraph(method='dopri5', adjoint=False, requires_grad=True, default_type=torch.float64)
Bases:
MessagePassingBase class to define, solve, and visualize a system of ODEs on a graph.
- Parameters:
method (str) – Name of ODE solver to use. Default is
dopri5.adjoint (bool) – Whether or not to use the adjoint method for backpropagating through ODE solutions. Default is
False.requires_grad (bool) – Whether or not gradients should be computed for the tensors defining the ODE system.
default_type (type) – Set the default
torch.Tensortype.
- Variables:
odeint (
torchdiffeq.odeintortorchdiffeq.odeint_adjoint) – Numerical integrator for a system of ODEs given an initial value. The adjoint method will not be used ifrequires_gradisFalse.
- forward(x, edge_index)
Update node embeddings
xas specified by themessage.edge_indexgives the indices of source and target nodes connected by edges.- Parameters:
x (
torch.tensor) – Current node embeddings.edge_index (
torch.tensorof shape(N,2)) – Edge indices fromNsources (first column) toNtargets (second column).
- Returns:
x’ – Updated node embeddings.
- Return type:
torch.tensor
- get_batch(batch_time, batch_size)
Sample a batch of
t,y0, andy.- Parameters:
- Returns:
t (
torch.tensorof shape(batch_time)) – Tensor ofbatch_timetime points.y0 (
torch.tensorof shape(batch_size,...,D)) – Initial states forbatch_sizeinitial conditions.y (
torch.tensorof shape(batch_time,batch_size,...,D)) – Solution evaluated atbatch_timetime points forbatch_sizeinitial conditions.
- get_eval(T, n=6, d=10)
Get an array of logarithmically spaced evaluation time points.
- plot_frame(ax, y, ntype=None, vmin=None, vmax=None, alpha=0.9, extent=None)
Plot a single frame of an ODE solution.
- Parameters:
ax (
matplotlib.axes) – Axis object on which to display the solution.y (
torch.tensor) – Solution of the system to plot. The input should be either reshaped to the dimensions of the simulation box or have a final dimension of 3.ntype (str) –
Type of normalization to apply when displaying the image. The options are:
none– No normalization; point cloud datasym– Symmetric linear normalization scaleunit– Linear normalization between 0 and 1mod– Linear normalization modulo \(2 \pi\)log– Logarithmic normalization scalesymlog– Symmetric logarithmic normalization scaleNone– Linear normalization between the min. and max. values ofy
The default is
None.vmin (float, optional) – Minimum normalization value.
vmax (float, optional) – Maximum normalization value.
alpha (float between 0 and 1, optional) – Opacity of the image.
extent (list or tuple of floats (left, right, bottom, top), optional) – Coordinates of the bounding box of the image.
- Returns:
sm – Object mapping scalar data to RGBA color values.
- Return type:
matplotlib.cm.ScalarMappable
- plot_series(y, ntype=None, vmin=None, vmax=None, clabel=None)
Plot a time series of frames of an ODE solution.
- Parameters:
y (
torch.tensor) – Solution of the system to plot. The input should be either reshaped to the dimensions of the simulation box or have a final dimension of 2.ntype (str) –
Type of normalization to apply when displaying the image. The options are:
none– No normalization; point cloud datasym– Symmetric linear normalization scaleunit– Linear normalization between 0 and 1mod– Linear normalization modulo \(2 \pi\)log– Logarithmic normalization scalesymlog– Symmetric logarithmic normalization scaleNone– Linear normalization between the min. and max. values ofy
The default is
None.vmin (float, optional) – Minimum normalization value.
vmax (float, optional) – Maximum normalization value.
clabel (str, optional) – Text used to label the colorbar.
- solve(t, y0=None, device='cpu', rtol=1e-06, atol=1e-06)
Numerically integrate the ODE system and return the solution at times
t.- Parameters:
t (
torch.tensor) – 1-dimensional tensor of evaluation times.y0 (
torch.tensorof shape(M,...,D)) – Initial state of the system forMinitial conditions.Ddenotes the flattened system size.device (str) – The name of the device on which the computation will be performed (e.g.
cpuorcuda).rtol (float) – Upper bound on relative error. Default is \(10^{-6}\).
atol (float) – Upper bound on absolute error. Default is \(10^{-6}\).
- Returns:
y – Solution evaluated at
Ttime points forMinitial conditions.Ddenotes the flattened system size.- Return type:
torch.tensorof shape(T,M,...,D)
- trim(t0=0)
Trim the solution at early time points to exclude initial transients.
- Parameters:
t0 (int) – The first index into
tat which to return the solution in order to exclude initial transients. The initial state will be set to the solution at this point and initial time set to 0 at this point.
- class Swarm(args, method='dopri5', default_type=torch.float64)
Bases:
ODEGraphClass to define, solve, and visualize a system of ODEs governing agents with pairwise-interactions.
- Parameters:
args (dict) –
Dictionary of parameters defining the ODE system:
N (int) – Dimension of the simulation box
(N x N)L (float) – Length of the real-space simulation box
D (int) – Dimensionality of system (2 or 3)
R (float) – Particle radius
J (float) – Spatial-phase coupling strength
K (float) – Phase-phase coupling strength
rc (float) – Cutoff radius of interaction
Nc (float) – Maximum number of interactions
method (str) – Name of ODE solver to use. Default is
dopri5.default_type (type) – Set the default
torch.Tensortype.
- Variables:
odeint (
torchdiffeq.odeintortorchdiffeq.odeint_adjoint) – Numerical integrator for a system of ODEs given an initial value. The adjoint method will not be used ifrequires_gradisFalse.
- forward(t, y)
Evaluate the ODE system at a specified time
tand statey. Constructs the adjacency graphs based on the cutoff radius and current particle positions, then evaluates and propagates messages along constructed edges.- Parameters:
t (
torch.tensor) – 1-dimensional tensor of the evaluation time points.y (
torch.tensorof shape(M,N,D)) – State of the system forMinitial conditions.
- Returns:
dy/dt – Derivative of the system.
- Return type:
torch.tensorof shape(M,N,D)
- init_state(M=1, seed=12)
Randomly generate the initial state(s) of the ODE system.
- Parameters:
- Variables:
y0 (
torch.tensorof shape(M,1,D)) – Initial state of the system forMinitial conditions.Ddenotes the flattened system size.
- message(x_i, x_j)
Construct the message to node
ifrom each neighboring nodej.- Parameters:
x_i (
torch.tensor) – Node embedding for nodei.x_j (
torch.tensor) – Node embedding for nodej.
- Returns:
m_ij – Message between nodes
iandj.- Return type:
torch.tensor