elastodynamicsx.plot

Module contents

The plot module contains convenience tools for plotting

elastodynamicsx.plot.live_plotter(u: Function, refresh_step: int = 1, **kwargs) Plotter[source]

Convenience function to initialize a plotter for live-plotting within a time- or frequency-domain computation

Parameters:
  • u – The FEM function to be live-plotted

  • refresh_step – Refresh each # step

Keyword Arguments:

kwargs – passed to plotter

Example

Time-domain problems:

tStepper = TimeStepper.build(...)
u_res = tStepper.timescheme.u  # The solution
p = live_plotter(u_res, refresh_step=10)
tStepper.solve(..., live_plotter=p)

Frequency-domain problems:

fdsolver = FrequencyDomainSolver(...)
u_res = fem.Function(V, name='solution')
p = live_plotter(u_res)
fdsolver.solve(omega=omegas,
               out=u_res.x.petsc_vec,
               callbacks=[],
               live_plotter=p)
elastodynamicsx.plot.plot_mesh(mesh: Mesh, cell_tags: MeshTags | None = None, **kwargs) Plotter[source]

Plot the mesh with colored subdomains

Parameters:
  • mesh – a dolfinx mesh

  • cell_tags – (optional) a dolfinx MeshTag instance

Returns:

The pyvista.Plotter

Adapted from:

https://jsdokken.com/dolfinx-tutorial/chapter3/em.html

Example

from mpi4py import MPI
from dolfinx.mesh import create_unit_square
from elastodynamicsx.utils import make_tags

domain = create_unit_square(MPI.COMM_WORLD, 10, 10)

Omegas = [(1, lambda x: x[1] <= 0.5),
          (2, lambda x: x[1] >= 0.5)]
cell_tags = make_tags(domain, Omegas, 'domains')

p = plot_mesh(domain, cell_tags=cell_tags)
p.show()
elastodynamicsx.plot.plotter(*args: List[Function] | Mesh, **kwargs) Plotter[source]

A generic function to plot a mesh or one/several fields

Parameters:

*args – FEM functions to be plotted, sharing the same underlying function space

Keyword Arguments:

**kwargs – passed to either CustomScalarPlotter or CustomVectorPlotter

Example

u1 = fem.Function(V)
u2 = fem.Function(V)
p = plotter(u1, u2, labels=['first', 'second'])
p.show()
class elastodynamicsx.plot.CustomScalarPlotter(*args, **kwargs)[source]

Bases: Plotter

Not intended to be instanciated by user; better use the plotter function

Parameters:
  • u1 – fem.Function; the underlying function space is a scalar function space

  • u2 – fem.Function; the underlying function space is a scalar function space

  • ... – fem.Function; the underlying function space is a scalar function space

Keyword Arguments:
  • refresh_step – (default=1) The refresh step, if used for live-plotting

  • sleep – (default=0.01) The sleep time after refresh, if used for live-plotting

  • complex – (default=’real’) The way complex functions are plotted. Possible options: ‘real’, ‘imag’, ‘abs’, ‘angle’

  • labels – list of labels

  • **kwargs – any valid kwarg for pyvista.Plotter and pyvista.Plotter.add_mesh

default_cmap = <matplotlib.colors.LinearSegmentedColormap object>
update_scalars(*all_scalars, **kwargs)[source]

Calls pyvista.Plotter.update_scalars for all subplots

live_plotter_start()[source]
live_plotter_stop()[source]
live_plotter_update_function(i: int, vec: Vec) None[source]
add_time_browser(update_fields_function: Callable, timesteps: ndarray, **kwargs_slider)[source]
class elastodynamicsx.plot.CustomVectorPlotter(*args, **kwargs)[source]

Bases: Plotter

Parameters:
  • u1 – fem.Function; the underlying function space is a scalar function space

  • u2 – fem.Function; the underlying function space is a scalar function space

  • ... – fem.Function; the underlying function space is a scalar function space

Keyword Arguments:
  • refresh_step – (default=1) The refresh step, if used for live-plotting

  • sleep – (default=0.01) The sleep time after refresh, if used for live-plotting

  • complex – (default=’real’) The way complex functions are plotted. Possible options: ‘real’, ‘imag’, ‘abs’, ‘angle’

  • labels – list of labels

  • warp_factor – (default=1 or 0.5/max(clim) if provided) Factor for plotting mesh deformation

  • ref_mesh – (default=False) Whether to show the undeformed mesh

  • **kwargs – any valid kwarg for pyvista.Plotter and pyvista.Plotter.add_mesh

default_cmap = <matplotlib.colors.ListedColormap object>
update_vectors(*all_vectors, render: bool = True) None[source]

Calls pyvista.Plotter.update_coordinates and .update_scalars for all subplots

Parameters:

all_vectors – tuple of np.ndarray, e.g. all_vectors = (u1.x, u2.x, …) where u1, u2 are dolfinx.fem.Function

live_plotter_start()[source]
live_plotter_stop()[source]
live_plotter_update_function(i: int, vec: Vec) None[source]
add_time_browser(update_fields_function: Callable, timesteps: ndarray, **kwargs_slider) None[source]
elastodynamicsx.plot.spy_petscMatrix(Z: Mat, *args, **kwargs) AxesImage[source]

matplotlib.pyplot.spy with Z being a petsc4py.PETSc.Mat object

Parameters:
  • Z – The array to be plotted, of type petsc4py.PETSc.Mat

  • args – Passed to matplotlib.pyplot.spy (see doc)

Keyword Arguments:

kwargs – Passed to matplotlib.pyplot.spy (see doc)

Returns:

See doc of matplotlib.pyplot.spy

elastodynamicsx.plot.plot

elastodynamicsx.plot.plot.plot_mesh(mesh: Mesh, cell_tags: MeshTags | None = None, **kwargs) Plotter[source]

Plot the mesh with colored subdomains

Parameters:
  • mesh – a dolfinx mesh

  • cell_tags – (optional) a dolfinx MeshTag instance

Returns:

The pyvista.Plotter

Adapted from:

https://jsdokken.com/dolfinx-tutorial/chapter3/em.html

Example

from mpi4py import MPI
from dolfinx.mesh import create_unit_square
from elastodynamicsx.utils import make_tags

domain = create_unit_square(MPI.COMM_WORLD, 10, 10)

Omegas = [(1, lambda x: x[1] <= 0.5),
          (2, lambda x: x[1] >= 0.5)]
cell_tags = make_tags(domain, Omegas, 'domains')

p = plot_mesh(domain, cell_tags=cell_tags)
p.show()
elastodynamicsx.plot.plot.live_plotter(u: Function, refresh_step: int = 1, **kwargs) Plotter[source]

Convenience function to initialize a plotter for live-plotting within a time- or frequency-domain computation

Parameters:
  • u – The FEM function to be live-plotted

  • refresh_step – Refresh each # step

Keyword Arguments:

kwargs – passed to plotter

Example

Time-domain problems:

tStepper = TimeStepper.build(...)
u_res = tStepper.timescheme.u  # The solution
p = live_plotter(u_res, refresh_step=10)
tStepper.solve(..., live_plotter=p)

Frequency-domain problems:

fdsolver = FrequencyDomainSolver(...)
u_res = fem.Function(V, name='solution')
p = live_plotter(u_res)
fdsolver.solve(omega=omegas,
               out=u_res.x.petsc_vec,
               callbacks=[],
               live_plotter=p)
elastodynamicsx.plot.plot.plotter(*args: List[Function] | Mesh, **kwargs) Plotter[source]

A generic function to plot a mesh or one/several fields

Parameters:

*args – FEM functions to be plotted, sharing the same underlying function space

Keyword Arguments:

**kwargs – passed to either CustomScalarPlotter or CustomVectorPlotter

Example

u1 = fem.Function(V)
u2 = fem.Function(V)
p = plotter(u1, u2, labels=['first', 'second'])
p.show()
class elastodynamicsx.plot.plot.CustomScalarPlotter(*args, **kwargs)[source]

Bases: Plotter

Not intended to be instanciated by user; better use the plotter function

Parameters:
  • u1 – fem.Function; the underlying function space is a scalar function space

  • u2 – fem.Function; the underlying function space is a scalar function space

  • ... – fem.Function; the underlying function space is a scalar function space

Keyword Arguments:
  • refresh_step – (default=1) The refresh step, if used for live-plotting

  • sleep – (default=0.01) The sleep time after refresh, if used for live-plotting

  • complex – (default=’real’) The way complex functions are plotted. Possible options: ‘real’, ‘imag’, ‘abs’, ‘angle’

  • labels – list of labels

  • **kwargs – any valid kwarg for pyvista.Plotter and pyvista.Plotter.add_mesh

default_cmap = <matplotlib.colors.LinearSegmentedColormap object>
grids: List[UnstructuredGrid]
update_scalars(*all_scalars, **kwargs)[source]

Calls pyvista.Plotter.update_scalars for all subplots

live_plotter_start()[source]
live_plotter_stop()[source]
live_plotter_update_function(i: int, vec: Vec) None[source]
add_time_browser(update_fields_function: Callable, timesteps: ndarray, **kwargs_slider)[source]
class elastodynamicsx.plot.plot.CustomVectorPlotter(*args, **kwargs)[source]

Bases: Plotter

Parameters:
  • u1 – fem.Function; the underlying function space is a scalar function space

  • u2 – fem.Function; the underlying function space is a scalar function space

  • ... – fem.Function; the underlying function space is a scalar function space

Keyword Arguments:
  • refresh_step – (default=1) The refresh step, if used for live-plotting

  • sleep – (default=0.01) The sleep time after refresh, if used for live-plotting

  • complex – (default=’real’) The way complex functions are plotted. Possible options: ‘real’, ‘imag’, ‘abs’, ‘angle’

  • labels – list of labels

  • warp_factor – (default=1 or 0.5/max(clim) if provided) Factor for plotting mesh deformation

  • ref_mesh – (default=False) Whether to show the undeformed mesh

  • **kwargs – any valid kwarg for pyvista.Plotter and pyvista.Plotter.add_mesh

default_cmap = <matplotlib.colors.ListedColormap object>
grids: List[UnstructuredGrid]
update_vectors(*all_vectors, render: bool = True) None[source]

Calls pyvista.Plotter.update_coordinates and .update_scalars for all subplots

Parameters:

all_vectors – tuple of np.ndarray, e.g. all_vectors = (u1.x, u2.x, …) where u1, u2 are dolfinx.fem.Function

live_plotter_start()[source]
live_plotter_stop()[source]
live_plotter_update_function(i: int, vec: Vec) None[source]
add_time_browser(update_fields_function: Callable, timesteps: ndarray, **kwargs_slider) None[source]
elastodynamicsx.plot.plot.spy_petscMatrix(Z: Mat, *args, **kwargs) AxesImage[source]

matplotlib.pyplot.spy with Z being a petsc4py.PETSc.Mat object

Parameters:
  • Z – The array to be plotted, of type petsc4py.PETSc.Mat

  • args – Passed to matplotlib.pyplot.spy (see doc)

Keyword Arguments:

kwargs – Passed to matplotlib.pyplot.spy (see doc)

Returns:

See doc of matplotlib.pyplot.spy