ngsPETSc.utils.firedrake_webgui
===============================

.. py:module:: ngsPETSc.utils.firedrake_webgui

.. autoapi-nested-parse::

   Firedrake mesh/function visualization via webgui jupyter widgets.

   Supports:
     * 2D triangular and 3D tetrahedral meshes.
     * Scalar and vector function spaces (Lagrange/CG, DG, RT, Nedelec, ...).
     * High polynomial orders (sub-triangulation level controllable via
       ``subdivision``; defaults to the source element degree, capped at 10).
     * Live updates via :meth:`FiredrakeScene.Redraw` for time-dependent
       simulations and :func:`ipywidgets`-driven parameter sliders.



Attributes
----------

.. autoapisummary::

   ngsPETSc.utils.firedrake_webgui._POINTEVAL_FAMILIES
   ngsPETSc.utils.firedrake_webgui._MAX_SUBDIVISION
   ngsPETSc.utils.firedrake_webgui._MANA_STOPS
   ngsPETSc.utils.firedrake_webgui._NAMED_COLORMAPS
   ngsPETSc.utils.firedrake_webgui._DEFAULT_COLORMAP
   ngsPETSc.utils.firedrake_webgui._COLORMAP_RESOLUTION
   ngsPETSc.utils.firedrake_webgui._intrules
   ngsPETSc.utils.firedrake_webgui._UFC_TET_VERTS
   ngsPETSc.utils.firedrake_webgui._UFC_TET_FACET_VERTS


Classes
-------

.. autoapisummary::

   ngsPETSc.utils.firedrake_webgui.FiredrakeScene


Functions
---------

.. autoapisummary::

   ngsPETSc.utils.firedrake_webgui._make_trig
   ngsPETSc.utils.firedrake_webgui._make_quad
   ngsPETSc.utils.firedrake_webgui.get_intrules
   ngsPETSc.utils.firedrake_webgui._sample_colormap
   ngsPETSc.utils.firedrake_webgui._get_mesh_and_func
   ngsPETSc.utils.firedrake_webgui._value_shape
   ngsPETSc.utils.firedrake_webgui._ref_lattice_tri
   ngsPETSc.utils.firedrake_webgui._facet_ref_pts_3d
   ngsPETSc.utils.firedrake_webgui._interp_for_vis
   ngsPETSc.utils.firedrake_webgui._tabulate
   ngsPETSc.utils.firedrake_webgui._per_cell_eval
   ngsPETSc.utils.firedrake_webgui._per_cell_coords
   ngsPETSc.utils.firedrake_webgui._orient_tris_2d
   ngsPETSc.utils.firedrake_webgui._flatten_funcvals
   ngsPETSc.utils.firedrake_webgui._build_2d_wireframe
   ngsPETSc.utils.firedrake_webgui._build_2d
   ngsPETSc.utils.firedrake_webgui._build_2d_boundary_segments
   ngsPETSc.utils.firedrake_webgui._build_3d_boundary
   ngsPETSc.utils.firedrake_webgui._build_3d_boundary_edges
   ngsPETSc.utils.firedrake_webgui._func_minmax
   ngsPETSc.utils.firedrake_webgui._default_subdivision
   ngsPETSc.utils.firedrake_webgui.Draw


Module Contents
---------------

.. py:data:: _POINTEVAL_FAMILIES
   :value: ('Lagrange', 'Continuous Lagrange', 'CG', 'Discontinuous Lagrange', 'DG', 'DQ', 'Q', 'DPC')


.. py:data:: _MAX_SUBDIVISION
   :value: 10


.. py:data:: _MANA_STOPS
   :value: [(0.0, (0.098039, 0.137255, 0.352941)), (0.03125, (0.207283, 0.138936, 0.373669)), (0.0625,...


.. py:data:: _NAMED_COLORMAPS

.. py:data:: _DEFAULT_COLORMAP
   :value: 'mana'


.. py:data:: _COLORMAP_RESOLUTION
   :value: 256


.. py:function:: _make_trig(N, x0=0, y0=0, dx=1, dy=1)

.. py:function:: _make_quad(N, x0=0, y0=0, dx=1, dy=1)

.. py:data:: _intrules

.. py:function:: get_intrules(dim: int, order: int)

.. py:function:: _sample_colormap(spec, n=_COLORMAP_RESOLUTION)

   Resample a ParaView-style stop list into ``n`` RGB triples.

   ``spec`` may be:
     * ``None`` — use webgui's default colormap (returns ``None``);
     * a string naming an entry in :data:`_NAMED_COLORMAPS`;
     * a list of ``(t, (r, g, b))`` tuples with ``t`` in ``[0, 1]``;
     * an ``(N, 3)`` array of RGB rows assumed to span ``[0, 1]`` evenly.


.. py:function:: _get_mesh_and_func(obj, order)

.. py:function:: _value_shape(func_or_space)

   Return the function's value shape via UFL on the bound domain.


.. py:function:: _ref_lattice_tri(n)

   Uniform lattice on UFC reference triangle.

   Returns ``(pts, tris)`` with ``pts`` of shape ``(npts, 2)`` and
   ``tris`` of shape ``(ntris, 3)``. Sub-triangles are CCW so their
   normals point in +z.


.. py:data:: _UFC_TET_VERTS

.. py:data:: _UFC_TET_FACET_VERTS
   :value: ((1, 2, 3), (0, 2, 3), (0, 1, 3), (0, 1, 2))


.. py:function:: _facet_ref_pts_3d(local_facet, ref_pts_2d)

   Map 2D reference triangle points to a face of the UFC tet.

   ``ref_pts_2d`` are (s, t) coordinates with s, t >= 0, s + t <= 1.
   Returns 3D ref-tet coordinates of shape ``(npts, 3)``.


.. py:function:: _interp_for_vis(func, mesh, n)

   Return a sample-able function and its value rank.

   For Lagrange / DG-style spaces the original ``func`` is returned.
   Non-pointwise families (RT, Nedelec, BDM, ...) are interpolated
   into a *discontinuous* DG_n / vector-DG_n space so that any genuine
   cross-cell discontinuity in the field — only the
   normal/tangential trace of an H(div)/H(curl) field is continuous —
   is preserved by the renderer.


.. py:function:: _tabulate(func_or_coords, ref_pts)

   FIAT-tabulate basis (order 0) of a Function/Coordinate at ref_pts.

   Returns the basis array. Scalar element shape is ``(ndof, npts)``;
   vector element shape is ``(ndof, vdim, npts)``.


.. py:function:: _per_cell_eval(func, ref_pts)

   Evaluate ``func`` at reference points within every cell.

   Returns array of shape ``(ncells, npts)`` for scalar or
   ``(ncells, npts, vdim)`` for vector functions.


.. py:function:: _per_cell_coords(mesh, ref_pts)

   Evaluate ``mesh.coordinates`` at reference points per cell.


.. py:function:: _orient_tris_2d(verts, tris)

   Force CCW winding so normals point +z.


.. py:function:: _flatten_funcvals(vals)

   Flatten per-cell, per-vertex function values to (nverts,) or (nverts, vdim).


.. py:function:: _build_2d_wireframe(data, mesh, func, n, encoding)

.. py:function:: _build_2d(data, mesh, func, n, encoding)

   Build vertex / triangle / value arrays for a 2D mesh.


.. py:function:: _build_2d_boundary_segments(mesh, n, npts, encoding)

   Sub-segmented boundary of a 2D mesh, indexing into the per-cell lattice.


.. py:function:: _build_3d_boundary(mesh, func, n, encoding)

   Build per-facet sub-triangulated boundary of a 3D tet mesh.


.. py:function:: _build_3d_boundary_edges(mesh, ef, parent_verts, local_facets, n, npts, encoding)

   Geometry edges between boundary regions with different markers,
   sub-divided into the per-facet lattice index space.


.. py:function:: _func_minmax(func)

.. py:function:: _default_subdivision(func, mesh)

   Pick a sensible sub-triangulation level.


.. py:class:: FiredrakeScene(obj, mesh=None, subdivision=None, colormap=_DEFAULT_COLORMAP, order=1, **kwargs)

   Bases: :py:obj:`webgui_jupyter_widgets.BaseWebGuiScene`


   A webgui scene for a Firedrake mesh or function.

   Parameters
   ----------
   obj : firedrake.MeshGeometry or firedrake.Function
       Object to visualise.
   mesh : firedrake.MeshGeometry, optional
       Override mesh (when ``obj`` is a function but you want to show
       it on a different mesh).
   subdivision : int, optional
       Sub-triangles per element edge. Defaults to the source element
       degree, capped at 10.
   colormap : str, list, ndarray, or None, optional
       Colour map used by the on-screen colour bar. Strings select a
       built-in (``"mana"`` (default), ``"cool_to_warm"``, ``"ngs"``);
       a stop list ``[(t, (r, g, b)), ...]`` or an ``(N, 3)`` RGB array
       is also accepted. Pass ``None`` to fall back to webgui's default
       rainbow (equivalent to ``"ngs"``).


   .. py:attribute:: obj


   .. py:attribute:: _mesh_override
      :value: None



   .. py:attribute:: subdivision
      :value: None



   .. py:attribute:: colormap
      :value: 'mana'



   .. py:attribute:: kwargs


   .. py:attribute:: encoding
      :value: 'b64'



   .. py:attribute:: order
      :value: 1



   .. py:method:: GetData(set_minmax=True)


   .. py:method:: Redraw(obj=None)

      Push fresh data to an already-displayed widget.

      Pass ``obj`` to swap to a different Function/Mesh (useful for
      slider callbacks) or call without args to re-encode the
      current object after its ``dat`` has been mutated in place
      (the time-stepping pattern).



.. py:function:: Draw(obj, mesh=None, subdivision=None, colormap=_DEFAULT_COLORMAP, order=1, **kwargs)

   Draw a Firedrake Mesh or Function in Jupyter.


