Utilities

Plotting for notebooks and quick looks: fields on 1D and 2D meshes sampled on refined elements, meshes, observation points, eigenvalues and eigenvectors, and trajectories of time-dependent problems.

hippymfem.utils.nb

Quick plots of fields on 1D and 2D meshes, for notebooks and scripts.

The counterpart of hippylib.utils.nb, drawn with matplotlib. A field is not reduced to its vertex values: every element is subdivided with MFEM’s geometry refiner and the field is evaluated at the sub-element vertices, so a P2 state or a discontinuous L2 field is drawn as what it is. 3D fields belong in ParaView (hippymfem.write_paraview()).

Every function that takes a field is collective: in parallel each rank evaluates its own elements, rank 0 gathers and draws, and the other ranks return None.

hippymfem.utils.nb.sample_field(space, vector, refine=None, component=None)[source]

Evaluate a field on a refinement of every element, gathered on rank 0.

Parameters:
  • space (FunctionSpace)

  • vector (ParVector) – True-dof values of the field.

  • refine (int, optional) – Subdivisions per element edge; the polynomial degree by default.

  • component (int, optional) – For a vector-valued field (vdim > 1, H(curl), H(div)), the component to return; the pointwise magnitude by default.

Returns:

tuple or None – On rank 0, (points, cells, values): sample points (npts, sdim), cells (ncells, 2) in 1D or (ncells, 3) in 2D with quadrilaterals split in two, and values (npts,). None on the other ranks.

hippymfem.utils.nb.plot(obj, vector=None, subplot_loc=None, mytitle=None, show_axis='off', vmin=None, vmax=None, colorbar=True, cmap=None, logscale=False, refine=None, component=None, ax=None)[source]

Plot a field, or a mesh.

plot(space, vector) draws a field and plot(mesh) draws a mesh. Returns the matplotlib artist on rank 0 and None on the other ranks.

hippymfem.utils.nb.plot_mesh(mesh, subplot_loc=None, mytitle=None, show_axis='off', ax=None, color='k', linewidth=0.4)[source]

Draw the element edges of a 2D mesh, or the vertices of a 1D one.

hippymfem.utils.nb.plot_pts(points, values=None, colorbar=True, subplot_loc=None, mytitle=None, show_axis='on', vmin=None, vmax=None, xlim=(0, 1), ylim=(0, 1), cmap=None, ax=None, marker_size=25)[source]

Scatter points in 2D, colored by values when given.

Not collective: points and values are plain arrays, so call it where they live (for observations, gather them first).

hippymfem.utils.nb.multi1_plot(objs, titles, same_colorbar=True, show_axis='off', logscale=False, vmin=None, vmax=None, cmap=None, refine=None)[source]

Plot fields side by side on one row.

objs is a sequence of (space, vector) pairs; with same_colorbar every panel shares the color range.

hippymfem.utils.nb.plot_eigenvalues(d, mytitle=None, subplot_loc=None, ax=None)[source]

Generalized eigenvalues on a log scale, with the line \(\lambda = 1\).

Eigenvalues above the line are the directions in which the data inform the parameter more than the prior does. Nonpositive values (round-off in the tail of a randomized eigensolver) cannot be drawn on a log scale and are left out. d is the same array on every rank; only rank 0 draws.

hippymfem.utils.nb.plot_eigenvectors(space, U, mytitle='Eigenvector', which=(0, 1, 2, 5, 10, 15), cmap=None, refine=None)[source]

Plot selected columns of a MultiVector on a grid of up to 2x3 panels.

hippymfem.utils.nb.show_solution(space, ic, state, same_colorbar=True, colorbar=True, mytitle=None, show_axis='off', logscale=False, times=(0.0, 0.4, 1.0, 2.0, 3.0, 4.0), cmap=None, refine=None)[source]

Plot an initial condition and a trajectory at up to five later times.

state is a TimeDependentVector; the first entry of times is drawn from ic and the others from the stored time closest to each.