bluemira.magnetostatics.fem_utils

Finite element method utilities

Attributes

old_m_to_m

Classes

BluemiraFemFunction

A supporting class that extends the BluemiraFemFunction implementing

Association

Mesh associations (density current, boundaries tag, target current)

Functions

convert_to_points_array(...)

Convert points to array

model_to_mesh([model, comm, rank, gdim])

Convert gmsh model to dolfinx mesh.

extract_geometry(func, dimensions, model)

Extract model geometry

calc_bb_tree(→ dolfinx.geometry.BoundingBoxTree)

Calculate the BoundingBoxTree of a dolfinx mesh

closest_point_in_mesh(→ numpy.ndarray)

Calculate closest point in mesh

calculate_area(→ float)

Calculate the area of a sub-domain

integrate_f(→ float)

Calculate the integral of a function on the specified sub-domain

pyvista_plot_show_save([filename])

Show or save figure from pyvista

plot_fem_scalar_field(field[, filename])

Plot a scalar field given by the dolfinx function "field" in pyvista or in a file.

error_L2(→ float)

Calculate the L2 error norm between two functions.

eval_f(→ tuple[numpy.ndarray, Ellipsis])

Evaluate the value of a dolfinx function in the specified points

plot_scalar_field(→ dict[str, ...)

Plot a scalar field from numpy arrays.

read_from_msh(filename[, comm, rank, gdim, partitioner])

Wraps dolfinx.io.gmshio.read_from_msh to patch dimensional reading

create_j_function() → BluemiraFemFunction)

Create the dolfinx current density function for the whole domain given a set of

compute_B_from_Psi(→ BluemiraFemFunction)

Compute the magnetic flux density given the magnetic flux function

plot_meshtags(mesh[, meshtags, filename])

Plot dolfinx mesh with markers using pyvista.

Module Contents

bluemira.magnetostatics.fem_utils.old_m_to_m
bluemira.magnetostatics.fem_utils.convert_to_points_array(x: numpy.typing.ArrayLike) numpy.typing.NDArray[numpy.float64]

Convert points to array

Returns:

Array of points.

Return type:

x

Parameters:

x (numpy.typing.ArrayLike)

bluemira.magnetostatics.fem_utils.model_to_mesh(model: type[gmsh.model] | None = None, comm=MPI.COMM_WORLD, rank: int = 0, gdim: int | collections.abc.Sequence[int] = 3, **kwargs)

Convert gmsh model to dolfinx mesh.

Returns:

  • result – Dolfinx mesh.

  • labels – Associated names.

Parameters:
  • model (type[gmsh.model] | None)

  • rank (int)

  • gdim (int | collections.abc.Sequence[int])

Notes

Patches dolfinx.io.gmshio.model_to_mesh to allow non sequential dimensions

bluemira.magnetostatics.fem_utils.extract_geometry(func: collections.abc.Callable[[type[gmsh.model]], numpy.ndarray], dimensions: collections.abc.Iterable[int], model: type[gmsh.model])

Extract model geometry

Designed to call dolfinx.io.gmshio.extract_geometry but patch for non sequential dimensions

Returns:

Extracted model geometry.

Return type:

x

Parameters:
  • func (collections.abc.Callable[[type[gmsh.model]], numpy.ndarray])

  • dimensions (collections.abc.Iterable[int])

  • model (type[gmsh.model])

bluemira.magnetostatics.fem_utils.calc_bb_tree(mesh: dolfinx.mesh.Mesh, padding: float = 0.0) dolfinx.geometry.BoundingBoxTree

Calculate the BoundingBoxTree of a dolfinx mesh

Returns:

Bounding box tree.

Parameters:
  • mesh (dolfinx.mesh.Mesh)

  • padding (float)

Return type:

dolfinx.geometry.BoundingBoxTree

class bluemira.magnetostatics.fem_utils.BluemiraFemFunction(*args, **kwargs)

Bases: dolfinx.fem.Function

Inheritance diagram of bluemira.magnetostatics.fem_utils.BluemiraFemFunction

A supporting class that extends the BluemiraFemFunction implementing the __call__ function to return the interpolated function value on the specified points.

Notes

In dolfinx v.0.5.0 it seems not to be possible to get a function value in a point without doing all the operations incorporated in eval_f. In case this functionality is integrated in most recent version of dolfinx, this class can be removed and replaced simply by BluemiraFemFunction.

_bb_tree
interpolate(u, cells=None)

Interpolate function and cache bb_tree

__call__(points: numpy.ndarray)

Call function

Parameters:

points (numpy.ndarray) – points at which function is evaluated

Returns:

  • res – interpolated function value for new_points

  • new_points – points for which interpolation was possible

Notes

Overwrite of the call function such that the behaviour is similar to the one expected for a Callable.

_eval_new(points: numpy.ndarray | list)

Supporting function for __call__

Parameters:

points (numpy.ndarray | list) – points at which function is evaluated

Returns:

  • res – interpolated function value for new_points

  • new_points – points for which interpolation was possible

bluemira.magnetostatics.fem_utils.closest_point_in_mesh(mesh: dolfinx.mesh.Mesh, points: numpy.ndarray) numpy.ndarray

Calculate closest point in mesh

Parameters:
  • mesh (dolfinx.mesh.Mesh) – mesh

  • points (numpy.ndarray) – points at which a function is evaluated

Returns:

closest point(s) in a mesh

Return type:

new_points

TODO hopefully remove in dolfinx >0.7.1

bluemira.magnetostatics.fem_utils.calculate_area(mesh: dolfinx.mesh.Mesh, boundaries: object | None = None, tag: int | None = None) float

Calculate the area of a sub-domain

Parameters:
  • mesh (dolfinx.mesh.Mesh) – mesh of the FEM model

  • boundaries (object | None) – boundaries mesh tags

  • tag (int | None) – subdomain tag

Returns:

area of the subdomain

Return type:

float

bluemira.magnetostatics.fem_utils.integrate_f(f: dolfinx.fem.Constant | BluemiraFemFunction, mesh: dolfinx.mesh.Mesh, boundaries=None, tag: int | None = None) float

Calculate the integral of a function on the specified sub-domain

Parameters:
  • f (dolfinx.fem.Constant | BluemiraFemFunction) – function to be integrated in the subdomain

  • mesh (dolfinx.mesh.Mesh) – mesh of the FEM model

  • boundaries – boundaries mesh tags

  • tag (int | None) – subdomain tag (default None). When None, the integral is made on the whole mesh domain.

Returns:

area of the subdomain

Return type:

float

bluemira.magnetostatics.fem_utils.pyvista_plot_show_save(filename: str = 'field.svg')

Show or save figure from pyvista

Yields:

the pyvista plotter

Parameters:

filename (str)

bluemira.magnetostatics.fem_utils.plot_fem_scalar_field(field: BluemiraFemFunction, filename: str = 'field.svg')

Plot a scalar field given by the dolfinx function “field” in pyvista or in a file.

Parameters:
  • field (BluemiraFemFunction) – function to be plotted

  • filename (str) – file in which the plot is stored

Notes

if pyvista.OFF_SCREEN is False the plot is shown on the screen, otherwise the file with the plot is saved.

bluemira.magnetostatics.fem_utils.error_L2(uh: BluemiraFemFunction, u_ex: BluemiraFemFunction | dolfinx.fem.Expression, degree_raise: int = 0) float

Calculate the L2 error norm between two functions. This method has been taken from https://jsdokken.com/dolfinx-tutorial/chapter4/convergence.html.

Parameters:
  • uh (BluemiraFemFunction) – first function

  • u_ex (BluemiraFemFunction | dolfinx.fem.Expression) – second function or an expression (in case, for example, of exact solution)

  • degree_raise (int) – increase of polynomial degree with respect uh degree (useful when comparing with an exact solution)

Returns:

integral error

Return type:

float

bluemira.magnetostatics.fem_utils.eval_f(function: dolfinx.fem.Function, points: numpy.ndarray) tuple[numpy.ndarray, Ellipsis]

Evaluate the value of a dolfinx function in the specified points

Parameters:
  • function (dolfinx.fem.Function) – reference function

  • points (numpy.ndarray) – points on which the function shall be calculated

Returns:

  • values – values of the function at the specified points

  • points_on_proc – specified points

Return type:

tuple[numpy.ndarray, Ellipsis]

bluemira.magnetostatics.fem_utils.plot_scalar_field(x: numpy.ndarray, y: numpy.ndarray, data: numpy.ndarray, levels: int | numpy.ndarray = 20, ax: matplotlib.pyplot.Axes | None = None, *, contour: bool = True, tofill: bool = True, **kwargs) dict[str, matplotlib.pyplot.Axes | None]

Plot a scalar field from numpy arrays.

Parameters:
  • x (numpy.ndarray) – x coordinate array

  • z – z coordinate array

  • data (numpy.ndarray) – value array

  • levels (int | numpy.ndarray) – Number of contour levels to plot or values for contour levels

  • axis – axis onto which to plot

  • contour (bool) – Whether or not to plot contour lines

  • tofill (bool) – Whether or not to plot filled contours

  • y (numpy.ndarray)

  • ax (matplotlib.pyplot.Axes | None)

Returns:

Matplotlib axis on which the plot ocurred

Return type:

dict[str, matplotlib.pyplot.Axes | None]

bluemira.magnetostatics.fem_utils.read_from_msh(filename: str, comm=MPI.COMM_WORLD, rank: int = 0, gdim: int | tuple = 3, partitioner=None)

Wraps dolfinx.io.gmshio.read_from_msh to patch dimensional reading

Parameters:
  • filename (Name of .msh file.)

  • comm (MPI communicator to create the mesh on.)

  • rank (Rank of comm responsible for reading the .msh file.)

  • gdim (Geometric dimension of the mesh)

Returns:

  • (mesh, cell_tags, facet_tags), labels with meshtags for

  • associated physical groups for cells and facets.

class bluemira.magnetostatics.fem_utils.Association

Mesh associations (density current, boundaries tag, target current)

v: BluemiraFemFunction | collections.abc.Callable | float
tag: int
Itot: float | None = None
bluemira.magnetostatics.fem_utils.create_j_function(mesh: dolfinx.mesh.Mesh, cell_tags, values: list[Association], eltype: tuple = ('DG', 0)) BluemiraFemFunction

Create the dolfinx current density function for the whole domain given a set of current density for each sub-domain.

Parameters:
  • mesh (Mesh) – mesh of the FEM model

  • cell_tags – mesh cell tags

  • values (list[Association]) – list of association (density current, boundaries tag, target current). If target current is not None, the applied current density is rescaled in order to obtain the total target current in the subdomain identified by the boundaries tag.

  • eltype (tuple) – a tuple of type (element family, element degree)

Returns:

a dolfinx function on the function space (mesh, eltype) with the values of the density current to be applied at each cell

Return type:

J

Raises:

ValueError – value not callable or a number

Notes

If multiple functions are defined on the same subdomain, the contributions of each function are summed up.

bluemira.magnetostatics.fem_utils.compute_B_from_Psi(psi: BluemiraFemFunction, eltype: tuple, eltype1: tuple | None = None) BluemiraFemFunction

Compute the magnetic flux density given the magnetic flux function for an axisymmetric problem.

Parameters:
  • psi (BluemiraFemFunction) – the magnetic flux function in the mesh domain

  • eltype (tuple) – Element type identified (e.g. (“P”, 1)) for the magnetic flux density function

  • eltype1 (tuple | None)

Returns:

Magnetic flux density function in the mesh domain

Return type:

B

bluemira.magnetostatics.fem_utils.plot_meshtags(mesh: dolfinx.mesh.Mesh, meshtags: dolfinx.cpp.mesh.MeshTags_float64 | dolfinx.cpp.mesh.MeshTags_int32 | None = None, filename: str = 'meshtags.svg')

Plot dolfinx mesh with markers using pyvista.

Parameters:
  • mesh (dolfinx.mesh.Mesh) – Mesh

  • meshtags (dolfinx.cpp.mesh.MeshTags_float64 | dolfinx.cpp.mesh.MeshTags_int32 | None) – Mesh tags

  • filename (str) – Full path for plot save