bluemira.magnetostatics.fem_utils ================================= .. py:module:: bluemira.magnetostatics.fem_utils .. autoapi-nested-parse:: Finite element method utilities Attributes ---------- .. autoapisummary:: bluemira.magnetostatics.fem_utils.old_m_to_m Classes ------- .. autoapisummary:: bluemira.magnetostatics.fem_utils.BluemiraFemFunction bluemira.magnetostatics.fem_utils.Association Functions --------- .. autoapisummary:: bluemira.magnetostatics.fem_utils.convert_to_points_array bluemira.magnetostatics.fem_utils.model_to_mesh bluemira.magnetostatics.fem_utils.extract_geometry bluemira.magnetostatics.fem_utils.calc_bb_tree bluemira.magnetostatics.fem_utils.closest_point_in_mesh bluemira.magnetostatics.fem_utils.calculate_area bluemira.magnetostatics.fem_utils.integrate_f bluemira.magnetostatics.fem_utils.pyvista_plot_show_save bluemira.magnetostatics.fem_utils.plot_fem_scalar_field bluemira.magnetostatics.fem_utils.error_L2 bluemira.magnetostatics.fem_utils.eval_f bluemira.magnetostatics.fem_utils.plot_scalar_field bluemira.magnetostatics.fem_utils.read_from_msh bluemira.magnetostatics.fem_utils.create_j_function bluemira.magnetostatics.fem_utils.compute_B_from_Psi bluemira.magnetostatics.fem_utils.plot_meshtags Module Contents --------------- .. py:data:: old_m_to_m .. py:function:: convert_to_points_array(x: numpy.typing.ArrayLike) -> numpy.typing.NDArray[numpy.float64] Convert points to array :returns: Array of points. :rtype: x .. py:function:: 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. .. rubric:: Notes Patches dolfinx.io.gmshio.model_to_mesh to allow non sequential dimensions .. py:function:: 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. :rtype: x .. py:function:: 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. .. py:class:: BluemiraFemFunction(*args, **kwargs) Bases: :py:obj:`dolfinx.fem.Function` .. autoapi-inheritance-diagram:: bluemira.magnetostatics.fem_utils.BluemiraFemFunction :parts: 1 :private-bases: A supporting class that extends the BluemiraFemFunction implementing the __call__ function to return the interpolated function value on the specified points. .. rubric:: 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. .. py:attribute:: _bb_tree .. py:method:: interpolate(u, cells=None) Interpolate function and cache bb_tree .. py:method:: __call__(points: numpy.ndarray) Call function :param points: points at which function is evaluated :returns: * *res* -- interpolated function value for new_points * *new_points* -- points for which interpolation was possible .. rubric:: Notes Overwrite of the call function such that the behaviour is similar to the one expected for a Callable. .. py:method:: _eval_new(points: numpy.ndarray | list) Supporting function for __call__ :param points: points at which function is evaluated :returns: * *res* -- interpolated function value for new_points * *new_points* -- points for which interpolation was possible .. py:function:: closest_point_in_mesh(mesh: dolfinx.mesh.Mesh, points: numpy.ndarray) -> numpy.ndarray Calculate closest point in mesh :param mesh: mesh :param points: points at which a function is evaluated :returns: closest point(s) in a mesh :rtype: new_points TODO hopefully remove in dolfinx >0.7.1 .. py:function:: calculate_area(mesh: dolfinx.mesh.Mesh, boundaries: object | None = None, tag: int | None = None) -> float Calculate the area of a sub-domain :param mesh: mesh of the FEM model :param boundaries: boundaries mesh tags :param tag: subdomain tag :returns: area of the subdomain .. py:function:: 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 :param f: function to be integrated in the subdomain :param mesh: mesh of the FEM model :param boundaries: boundaries mesh tags :param tag: subdomain tag (default None). When None, the integral is made on the whole mesh domain. :returns: area of the subdomain .. py:function:: pyvista_plot_show_save(filename: str = 'field.svg') Show or save figure from pyvista :Yields: the pyvista plotter .. py:function:: 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. :param field: function to be plotted :param filename: file in which the plot is stored .. rubric:: Notes if pyvista.OFF_SCREEN is False the plot is shown on the screen, otherwise the file with the plot is saved. .. py:function:: 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. :param uh: first function :param u_ex: second function or an expression (in case, for example, of exact solution) :param degree_raise: increase of polynomial degree with respect uh degree (useful when comparing with an exact solution) :returns: integral error .. py:function:: eval_f(function: dolfinx.fem.Function, points: numpy.ndarray) -> tuple[numpy.ndarray, Ellipsis] Evaluate the value of a dolfinx function in the specified points :param function: reference function :param points: points on which the function shall be calculated :returns: * *values* -- values of the function at the specified points * *points_on_proc* -- specified points .. py:function:: 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. :param x: x coordinate array :param z: z coordinate array :param data: value array :param levels: Number of contour levels to plot or values for contour levels :param axis: axis onto which to plot :param contour: Whether or not to plot contour lines :param tofill: Whether or not to plot filled contours :returns: Matplotlib axis on which the plot ocurred .. py:function:: 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 :param filename: :type filename: Name of ``.msh`` file. :param comm: :type comm: MPI communicator to create the mesh on. :param rank: :type rank: Rank of ``comm`` responsible for reading the ``.msh`` file. :param gdim: :type gdim: Geometric dimension of the mesh :returns: * ``(mesh, cell_tags, facet_tags), labels`` with meshtags for * *associated physical groups for cells and facets.* .. py:class:: Association Mesh associations (density current, boundaries tag, target current) .. py:attribute:: v :type: BluemiraFemFunction | collections.abc.Callable | float .. py:attribute:: tag :type: int .. py:attribute:: Itot :type: float | None :value: None .. py:function:: 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. :param mesh: mesh of the FEM model :type mesh: Mesh :param cell_tags: mesh cell tags :param values: 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. :param eltype: 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 :rtype: J :raises ValueError: value not callable or a number .. rubric:: Notes If multiple functions are defined on the same subdomain, the contributions of each function are summed up. .. py:function:: 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. :param psi: the magnetic flux function in the mesh domain :param eltype: Element type identified (e.g. ("P", 1)) for the magnetic flux density function :returns: Magnetic flux density function in the mesh domain :rtype: B .. py:function:: 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. :param mesh: Mesh :param meshtags: Mesh tags :param filename: Full path for plot save