Advection-Diffusion Models#

pyoed.models.simulation_models.advection_diffusion.AdvectionDiffusion1D([...])

Implementations of the one-dimensional advection-diffusion model:

pyoed.models.simulation_models.advection_diffusion.AdvectionDiffusion2D([...])

Implementations of the two-dimensional advection-diffusion model:

pyoed.models.simulation_models.advection_diffusion.AdvectionDiffusion3D(...)

Implementations of 3D advection diffusion model

pyoed.models.simulation_models.advection_diffusion.create_AD_1D_model([...])

A simple interface to creating an instance of AdvectionDiffusion1D.

pyoed.models.simulation_models.advection_diffusion.create_AD_2D_model([...])

A simple interface to creating an instance of AdvectionDiffusion2D.

pyoed.models.simulation_models.advection_diffusion.create_AD_3D_model(...)

A simple interface to creating an instance of AdvectionDiffusion3D.

A module that provides implementation(s) of Poisson Equations model(s)

class AdvectionDiffusion1DConfigs(*, debug=False, verbose=False, output_dir='./_PYOED_RESULTS_', model_name='AdvectionDiffusion-1D', screen_output_iter=1, file_output_iter=1, time_integration=<factory>, num_prognostic_variables=None, space_discretization=<factory>, domain=(-1, 1), nu=0.1, c=0.1, nx=101, dt=0.01, t_eps=1e-06)[source]#

Bases: TimeDependentModelConfigs

Configuration class for the 1D Advection-Diffusion model.

\[\begin{split}\frac{\partial u}{\partial t} + c \frac{\partial u}{\partial x} = \nu \frac{\partial^2 u}{\partial^2 x},\, x\in[-L, L],\, t\in (0, t_f] \\\end{split}\]

This model is similar to the 1D Burgers problem, but with linear advection term. We assume Dirichlet homogeneous boundary conditions: \(u(0, t) = u(L, t) = 0,\, t \in (0, t_f]\). For initial conditions, we use the smooth function: \(u(x, 0) = - \sin(\pi x)\).

Parameters:
  • verbose (bool) – a boolean flag to control verbosity of the object.

  • debug (bool) – a boolean flag that enables adding extra functionlity in a debug mode

  • output_dir (str | Path) – the base directory where the output files will be saved.

  • model_name (str) – name of the model. Default is None.

  • screen_output_iter (int) – iteration interval for screen output. Default is 1. Note that this should be a positive integer to enforce proper effect.

  • file_out_iter – iteration interval for file output. Default is 1. Note that this should be a positive integer to enforce proper effect.

  • time_integration (dict | None) –

    dictionary holding time integration configurations:

    • scheme: string specifying the time integration scheme (e.g., ‘RK4’, ‘RK45’, ‘BDF’, etc.). Default is None.

    • stepsize: float specifying the time integration stepsize. Default is None.

    • adaptive: bool specifying whether the time integration is adaptive. Default is False.

  • num_prognostic_variables (int | None) – number of prognostic variables in the model. Default is None. Must be a positive integer if not None.

  • space_discretization (dict | None) –

    dictionary holding space discretization configurations. Contains:

    • scheme: string specifying the space discretization scheme (e.g., ‘FD’, ‘FE’, ‘BE’, etc.). Default is None.

  • domain (tuple[float, float]) – boundary of the spatial domain. Default is (-1, 1)

  • nu (float) – kinematic viscosity – diffusion coefficient

  • c (float) – constant advection velocity

  • nx (int) – number of spatial discretization points (of the domain)

  • dt (float) – default time integration step size

  • t_eps (float) – tolerance for time-step comparison

domain: tuple[float, float]#
nu: float#
c: float#
nx: int#
dt: float#
t_eps: float#
__init__(*, debug=False, verbose=False, output_dir='./_PYOED_RESULTS_', model_name='AdvectionDiffusion-1D', screen_output_iter=1, file_output_iter=1, time_integration=<factory>, num_prognostic_variables=None, space_discretization=<factory>, domain=(-1, 1), nu=0.1, c=0.1, nx=101, dt=0.01, t_eps=1e-06)#
class AdvectionDiffusion1D(configs=None)[source]#

Bases: TimeDependentModel

Implementations of the one-dimensional advection-diffusion model:

\[\begin{split}\frac{\partial u}{\partial t} + c \frac{\partial u}{\partial x} = \nu \frac{\partial^2 u}{\partial^2 x},\, x\in[-L, L],\, t\in (0, t_f] \\\end{split}\]

This model is similar to the 1D Burgers problem, but with linear advection term. We assume Dirichlet homogeneous boundary conditions: \(u(0, t) = u(L, t) = 0,\, t \in (0, t_f]\). For initial conditions, we use the smooth function: \(u(x, 0) = - \sin(\pi x)\).

Spatial grid is equally-spaced based on the configurations passed upon initialization. We are using finite differences for spatial discretization. Time integration is carried out using a simple implicit Euler method, which requires the solution of a (tridiagonal) linear system.

Parameters:

configs (AdvectionDiffusion1DConfigs | dict | None) – an object containing configurations of the one-dimensional advection-diffusion model.

__init__(configs=None)[source]#
validate_configurations(configs, raise_for_invalid=True)[source]#

Check the passed configuratios and make sure they are conformable with each other, and with current configurations once combined. This guarantees that any key-value pair passed in configs can be properly used

Note

Here only the locally-defined configurations in AdvectionDiffusion1DConfigs are validated. Finally, super classes validators are called.

Parameters:
  • configs (dict | AdvectionDiffusion1DConfigs) – full or partial (subset) configurations to be validated

  • raise_for_invalid (bool) – if True raise TypeError for invalid configrations type/key. Default True

Returns:

flag indicating whether passed configurations dictionary is valid or not

Raises:
Return type:

bool

create_initial_condition()[source]#

Create the initial condition associated with the passed model grid points We are assuming … initial condition defined as \(-\sin(\pi x)\) where \(x\) is a model grid point. :returns: numpy array (same shape as model_grid associated with the model) holding initial condition values.

state_vector(init_val=0)[source]#

Create an instance of model state vector. Here, this is a 1D Numpy array of size equal to the model grid.

Parameters:

init_val (float) – value assigned to entries of the state vector

Returns:

1d numpy array

is_state_vector(state)[source]#

Test whether the passed state vector is valid or not

integrate_state(state, tspan, checkpoints=None, verbose=False)[source]#

Simulate/integrate the model starting from the initial state over the passed checkpoints.

Parameters:
  • state – data structure holding the initial model state

  • tspan – (t0, tf) iterable with two entries specifying of the time integration window

  • checkpoints – times at which to store the computed solution, must be sorted and lie within tspan. If None (default), use points selected by the solver [t0, t1, …, tf].

  • verbose (bool) – output progress to screen if True. If set to False, nothing is printed to screen`

Returns:

a list holding the timespan, and a list holding the model trajectory with entries corresponding to the simulated model state at entries of checkpoints starting from checkpoints[0] and ending at checkpoints[-1].

Raises:

AssertionError ` if `tspan is not valid, or checkpoints are not within tspan

Jacobian_T_matvec(state, eval_at_t=None, eval_at=None, dt=None, sparse=True)[source]#

Evaluate the product of the Jacobian (of the right-hand-side) of AD model (the TLM) transposed, by a model state.

Parameters:
  • state – state to multiply the Jacobian by

  • eval_at_t – time at which the Jacobian is evaluated

  • eval_at – state around which the Jacobian is evaluated (ignored for linear models as here)

  • dt (float) – the step size

  • sparse (bool) – sparse/dense structure for the forward model matrix

Returns:

product of the Jacobian transposed (adjoint operator) by a model state.

exact_solution(t)[source]#

Exact solution for verification – note that it only works for a limited range of c and nu

get_model_grid()[source]#

return a copy of the model grid

property domain#

retrieve the model domain

property nu#

retrieve the model kinematic viscosity

property c#

retrieve the model advection velocity

property nx#

retrieve the model spatial discretization points

property dt#

retrieve the model time step size

property t_eps#

retrieve the model time epsilon tolerance

class AdvectionDiffusion2DConfigs(*, debug=False, verbose=False, output_dir='./_PYOED_RESULTS_', model_name='AdvectionDiffusion-2D', screen_output_iter=1, file_output_iter=1, time_integration=<factory>, num_prognostic_variables=None, space_discretization=<factory>, domain=((0, 1), (0, 1)), nu=0.01, cx=0.5, cy=0.5, nx=101, ny=101, dt=0.001, t_eps=1e-06)[source]#

Bases: TimeDependentModelConfigs

Configuration class for the 2D Advection-Diffusion model.

\[\frac{\partial u}{\partial t} + c_x \frac{\partial u}{\partial x} + c_y \frac{\partial u}{\partial y} = \nu (\frac{\partial^2 u}{\partial^2 x} + \frac{\partial^2 u}{\partial^2 y} ),\, x\in[0, L_x],\, y\in[0, L_y],\, t\in (0, t_f]\]

We assume a time-dependent non-zero Dirichlet boundary conditions, extracted from the following exact solution:

\[u(x,y,t) = \frac{1}{1+4t}\exp\left\{-\frac{(x-0.25-c_xt)^2+(y-0.25-c_yt)^2}{\nu(1+4t)}\right\}\]

Initial conditions are defined as \(u(x,y,0) = \exp\left\{-\frac{(x-x_0)^2+(y-y_0)^2}{\nu}\right\}\).

Parameters:
  • verbose (bool) – a boolean flag to control verbosity of the object.

  • debug (bool) – a boolean flag that enables adding extra functionlity in a debug mode

  • output_dir (str | Path) – the base directory where the output files will be saved.

  • model_name (str) – name of the model. Default is None.

  • screen_output_iter (int) – iteration interval for screen output. Default is 1. Note that this should be a positive integer to enforce proper effect.

  • file_out_iter – iteration interval for file output. Default is 1. Note that this should be a positive integer to enforce proper effect.

  • time_integration (dict | None) –

    dictionary holding time integration configurations:

    • scheme: string specifying the time integration scheme (e.g., ‘RK4’, ‘RK45’, ‘BDF’, etc.). Default is None.

    • stepsize: float specifying the time integration stepsize. Default is None.

    • adaptive: bool specifying whether the time integration is adaptive. Default is False.

  • num_prognostic_variables (int | None) – number of prognostic variables in the model. Default is None. Must be a positive integer if not None.

  • space_discretization (dict | None) –

    dictionary holding space discretization configurations. Contains:

    • scheme: string specifying the space discretization scheme (e.g., ‘FD’, ‘FE’, ‘BE’, etc.). Default is None.

  • domain (tuple[tuple[float, float], tuple[float, float]]) – boundary of the spatial domain. Default is ((0,1),(0,1))

  • nu (float) – kinematic viscosity – diffusion coefficient

  • cx (float) – constant advection velocity in the x-direction

  • cy (float) – constant advection velocity in the y-direction

  • nx (int) – number of spatial discretization points (of the domain) in the x-direction

  • ny (int) – number of spatial discretization points (of the domain) in the y-direction

  • dt (float) – default time integration step size

  • t_eps (float) – tolerance for time-step comparison (default 1e-6)

domain: tuple[tuple[float, float], tuple[float, float]]#
nu: float#
cx: float#
cy: float#
nx: int#
ny: int#
dt: float#
t_eps: float#
__init__(*, debug=False, verbose=False, output_dir='./_PYOED_RESULTS_', model_name='AdvectionDiffusion-2D', screen_output_iter=1, file_output_iter=1, time_integration=<factory>, num_prognostic_variables=None, space_discretization=<factory>, domain=((0, 1), (0, 1)), nu=0.01, cx=0.5, cy=0.5, nx=101, ny=101, dt=0.001, t_eps=1e-06)#
class AdvectionDiffusion2D(configs=None)[source]#

Bases: TimeDependentModel

Implementations of the two-dimensional advection-diffusion model:

\[\frac{\partial u}{\partial t} + c_x \frac{\partial u}{\partial x} + c_y \frac{\partial u}{\partial y} = \nu (\frac{\partial^2 u}{\partial^2 x} + \frac{\partial^2 u}{\partial^2 y} ),\, x\in[0, L_x],\, y\in[0, L_y],\, t\in (0, t_f]\]

We assume a time-dependent non-zero Dirichlet boundary conditions, extracted from the following exact solution:

\[u(x,y,t) = \frac{1}{1+4t}\exp\left\{-\frac{(x-0.25-c_xt)^2+(y-0.25-c_yt)^2}{\nu(1+4t)}\right\}\]

Initial conditions are defined as \(u(x,y,0) = \exp\left\{-\frac{(x-x_0)^2+(y-y_0)^2}{\nu}\right\}\).

Spatial grid is equally-spaced based on the configurations passed upon initialization. We are using finite differences for spatial discretization. Time integration is carried out using an explicit fourth-order Runge-Kutta method, which requires some attention to ensure stability.

Parameters:

configs (AdvectionDiffusion2DConfigs | dict | None) – an object containing configurations of the two-dimensional advection-diffusion model.

__init__(configs=None)[source]#
validate_configurations(configs, raise_for_invalid=True)[source]#

Check the passed configuratios and make sure they are conformable with each other, and with current configurations once combined. This guarantees that any key-value pair passed in configs can be properly used

Note

Here only the locally-defined configurations in AdvectionDiffusion2DConfigs are validated. Finally, super classes validators are called.

Parameters:
  • configs (dict | AdvectionDiffusion2DConfigs) – full or partial (subset) configurations to be validated

  • raise_for_invalid (bool) – if True raise TypeError for invalid configrations type/key. Default True

Returns:

flag indicating whether passed configurations dictionary is valid or not

Raises:
Return type:

bool

create_initial_condition()[source]#

Create the initial condition associated with the passed model grid points We are assuming … initial condition defined as \(u(x,y,0) = \exp\bigg\{-\frac{(x-x_0)^2+(y-y_0)^2}{\nu}\bigg\}\) where \(x, y\) define the model grid coordinates. :returns: numpy array holding initial condition values.

set_boundary_condition(state, t)[source]#

Set the boundary conditions, defined using the followin relation:

\[u(x,y,t) = \frac{1}{1+4t}\exp\bigg\{-\frac{(x-0.25-c_xt)^2+(y-0.25-c_yt)^2}{\nu(1+4t)}\bigg\}.\]
Returns:

numpy array (same shape as model_grid associated with the model) with set boundary condition values.

state_vector(init_val=0)[source]#

Create an instance of model state vector. Here, this is a 1D Numpy array of size equal to the model grid.

Parameters:

init_val (float) – value assigned to entries of the state vector

Returns:

1D numpy array

is_state_vector(state)[source]#

Test whether the passed state vector is valid or not

integrate_state(state, tspan, checkpoints=None, verbose=False)[source]#

Simulate/integrate the model starting from the initial state over the passed checkpoints.

Parameters:
  • state – data structure holding the initial model state

  • tspan – (t0, tf) iterable with two entries specifying of the time integration window

  • checkpoints – times at which to store the computed solution, must be sorted and lie within tspan. If None (default), use points selected by the solver [t0, t1, …, tf].

  • verbose (bool) – output progress to screen if True. If set to False, nothing is printed to screen`

Returns:

a list holding the timespan, and a list holding the model trajectory with entries corresponding to the simulated model state at entries of checkpoints starting from checkpoints[0] and ending at checkpoints[-1].

Raises:

AssertionError if tspan is not valid, or checkpoints are not within tspan

Jacobian_T_matvec(state, eval_at_t=None, eval_at=None, dt=None, sparse=True)[source]#

Evaluate and return the product of the Jacobian (of the right-hand-side) of the model (TLM) transposed, by a model state.

Parameters:
  • state – state to multiply the Jacobian by

  • eval_at_t – time at which the Jacobian is evaluated

  • eval_at – state around which the Jacobian is evaluated

  • dt (float) – the step size

Returns:

the product of the Jacobian transposed (adjoint operator) by a model state.

exact_solution(t)[source]#

Exact solution for verification

get_model_grid()[source]#

return a copy of the model grid

property domain#

retrieve the model domain

property nu#

retrieve the model kinematic viscosity

property cx#

retrieve the model advection velocity in the x-direction

property cy#

retrieve the model advection velocity in the y-direction

property nx#

retrieve the model spatial discretization points

property ny#

retrieve the model spatial discretization points

property dt#

retrieve the model time step size

property t_eps#

retrieve the model time epsilon tolerance

class AdvectionDiffusion3D(*args, **kwargs)[source]#

Bases: TimeDependentModel

Implementations of 3D advection diffusion model

__init__(*args, **kwargs)[source]#
create_AD_1D_model(domain=(-1, 1), nu=0.05, c=0.75, nx=51, dt=0.01, output_dir='./_PYOED_RESULTS_')[source]#

A simple interface to creating an instance of AdvectionDiffusion1D.

Parameters:
  • domain – boundary of the spatial domain. Default is (-1, 1)

  • nu (float) – kinematic viscosity – diffusion coefficient

  • c (float) – constant advection velocity

  • nx (int) – number of spatial discretization points (of the domain)

  • dt (float) – default time integration step size

Output_dir:

Path to folder (will be created if doesn’t exist) in which to write reports, output, etc.

Returns:

an instance of AdvectionDiffusion1D

create_AD_2D_model(domain=((0, 1), (0, 1)), nu=0.01, cx=0.5, cy=0.75, nx=51, ny=51, dt=0.01, output_dir='./_PYOED_RESULTS_')[source]#

A simple interface to creating an instance of AdvectionDiffusion2D.

Parameters:
  • domain – boundary of the spatial domain.

  • nu (float) – kinematic viscosity – diffusion coefficient

  • cx (float) – constant advection velocity in the x-direction

  • cy (float) – constant advection velocity in the y-direction

  • nx (int) – number of spatial discretization points (of the domain) in the x-direction

  • ny (int) – number of spatial discretization points in the y-direction

  • dt (float) – default time integration step size

Output_dir:

Path to folder (will be created if doesn’t exist) in which to write reports, output, etc.

Returns:

an instance of AdvectionDiffusion1D

create_AD_3D_model(*args, **kwargs)[source]#

A simple interface to creating an instance of AdvectionDiffusion3D.