{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The Helmholtz Equation with FEniCSx\n",
"Authors: Stefano Tronci and [Jørgen S. Dokken](https://knowledgebase.acoustics.ac.uk/community/bios.html#jorgen-s-dokken)\n",
"Edits: [Andrew Gibbs](https://knowledgebase.acoustics.ac.uk/community/bios.html#andrew-gibbs)\n",
"\n",
":::{note}\n",
"You can run this code directly in your browser by clicking on the rocket logo ( ) at the top of the page, and clicking 'Binder'. This will open a Jupyter Notebook in a [Binder](https://mybinder.org/) environment which is set up to contain everything you need to run the code. **Don't forget to save a local copy if you make any changes!**\n",
"\n",
"If you prefer, you can download the Jupyter Notebook file to run locally, by clicking the download logo ( ) at the top of the page and selecting '.ipynb'.\n",
"\n",
"If you are new to using Jupyter Notebooks, [this guide](https://www.dataquest.io/blog/jupyter-notebook-tutorial/) will help you get started.\n",
":::\n",
"\n",
"## Prerequisites\n",
"\n",
"If you launch this page using Binder as described above, the necessary environment and dependencies are already set up. If you prefer to run the code presented in this tutorial in your own environment, you will need:\n",
"\n",
"* A working Python environment for your platform. For more information, refer to [the Python website](https://www.python.org/);\n",
"* A working FEniCS installation within your Python environment. For more information, refer to [the FEniCSx documentation](https://docs.fenicsproject.org/dolfinx/main/python/installation);\n",
"* The following Python packages installed in your environment:\n",
" * `numpy`;\n",
" * `pyvista`;\n",
" * `ipygany`;\n",
" \n",
"A basic knowledge of Partial Differential Equations (PDEs) and FEM will be beneficial.\n",
"\n",
"\n",
"## Introduction\n",
"\n",
"This tutorial cover the basics on how to solve the Helmholtz equation with Neumann boundary conditions by using FEniCS.\n",
"\n",
"The governing PDE for this problem is the Helmholtz equation {eq}`eq-helmholtz` in some domain $\\Omega$. We will supplement it with appropriate boundary conditions.\n",
"\n",
"#### Boundary Conditions\n",
"```{note}\n",
"This information may be included in Huiqing's boundary condition pages.\n",
"```\n",
"The Helmholtz equation can be completed with Dirchlet, Neumann and Robin boundary conditions:\n",
"\n",
"* Dirchlet conditions impose a specific value of $\\tilde{p}$ on $\\partial \\Omega_{D}$.\n",
"\n",
"$$\n",
"\\tilde{p}=p_{D} \\quad\\quad \\text{on} \\quad \\partial\\Omega_{D}\n",
"$$\n",
"\n",
"* Neumann conditions specify a normal particle velocity $v_{n}$ on $ \\partial \\Omega_{N}$.\n",
"\n",
"$$\n",
"v_{n}=-\\frac{1}{j\\rho_{0}\\omega}\\frac{\\partial p}{\\partial n}= \\bar{v}_{n} \\quad\\quad \\text{on} \\quad \\partial\\Omega_{N}\n",
"$$\n",
"\n",
" \n",
"\n",
"where $n$ is the outward normal vector of the boundary surface.\n",
"\n",
"* Robin conditions specify an acoustic impedance $Z$ on $\\partial \\Omega_{R}$ as well as an optional particle velocity.\n",
"\n",
"$$\n",
"p=Zv_{n}=-\\frac{Z}{j\\omega\\rho_{0}}\\frac{\\partial p}{\\partial n}= -\\frac{1}{j\\omega\\rho_{0}A}\\frac{\\partial p}{\\partial n}\\quad\\quad \\text{on} \\quad \\partial\\Omega_{R}\n",
"$$\n",
"\n",
"\n",
"### The Weak Form\n",
"\n",
"We need to convert our PDE into a weak form to solve our problem with FEniCS. This is done by recognising that $u$ is a vector of an appropriate Sobolev space. Then, another test function $\\phi$ is taken from the Sobolev space, with the requirement of being $0$ on $\\partial \\Omega$. Multiplication of the governing PDE with $\\phi$ and integration by parts lead to the weak form. The weak form reads:\n",
"\n",
"$$\n",
"\\int_\\Omega \\nabla u \\cdot \\nabla \\overline\\phi d\\mathbf{x} - k^2 \\int_\\Omega u \\overline\\phi d\\mathbf{x} = \\sum_{l=0}^{L-1}\\int_{\\partial \\Omega_l} g_l \\overline\\phi ds\n",
"$$\n",
"\n",
"Where we simplified notation by dropping the functions arguments. $d\\mathbf{x}$ denotes the volume element of $\\Omega$ while $ds$ denotes the surface element in $\\partial \\Omega$. $\\overline\\cdot$ denotes complex conjugation. Note that the equation above has the following form:\n",
"\n",
"$$\n",
"a \\left(u,\\phi\\right) = L\\left(\\phi\\right)\n",
"$$\n",
"\n",
"Where $a$ and $L$ are the bilinear and linear form respectively. FEniCS requires the specification of these two quantities.\n",
"\n",
":::{note}\n",
"The Neumann boundary conditions feature directly in the weak form. The same goes for Robin conditions. However, Dirchlet boundary conditions don't and are treated separately.\n",
":::\n",
"\n",
"### More Details\n",
"\n",
"A similar tutorial for legacy FEniCS is presented at the links.\n",
"\n",
"* [Intro to FEniCS - Part 1](https://computational-acoustics.gitlab.io/website/posts/30-intro-to-fenics-part-1/).\n",
"* [Intro to FEniCS - Part 2 ](https://computational-acoustics.gitlab.io/website/posts/31-intro-to-fenics-part-2/).\n",
"* [Intro to FEniCS - Part 3 ](https://computational-acoustics.gitlab.io/website/posts/32-intro-to-fenics-part-3/).\n",
"\n",
"This tutorial focuses more on the code, whilst the ones above focus more on the derivation of the weak form.\n",
"\n",
"For more details about acoustics, FEniCS and computational methods the following books are recommended:\n",
"\n",
"* [Computational Acoustics of Noise Propagation in Fluids](https://link.springer.com/book/10.1007%2F978-3-540-77448-8).\n",
"* [Solving PDEs in Python](https://link.springer.com/book/10.1007/978-3-319-52462-7).\n",
"* [Fundamentals of Acoustics](https://www.wiley.com/en-gb/Fundamentals+of+Acoustics,+4th+Edition-p-9780471847892).\n",
"* [The FEniCSx tutorial](https://jorgensd.github.io/dolfinx-tutorial/)\n",
"\n",
"## Setup\n",
"\n",
"We now setup our simulation.\n",
"\n",
"First, we decide the shape of $\\Omega$, then the properties of the medium and then the boundary conditions.\n",
"\n",
"### Domain\n",
"\n",
"We choose a rectangular room with the following geometry:\n",
"\n",
"| Dimension | Symbol | Value |\n",
"|-----------|-----------|------------|\n",
"| Length | $d_{x_0}$ | $4$ meters |\n",
"| Width | $d_{x_1}$ | $5$ meters |\n",
"| Height | $d_{x_2}$ | $3$ meters |\n",
"\n",
"Hence $\\Omega = \\left(0, d_{x_0} \\right) \\times \\left(0, d_{x_1} \\right) \\times \\left(0, d_{x_2} \\right)$.\n",
"\n",
"\n",
"### Medium Properties\n",
"\n",
"We choose air at room temperature:\n",
"\n",
"| Property | Symbol | Value |\n",
"|----------------|--------|-----------------------------------|\n",
"| Speed of Sound | $c$ | $343$ meters per second |\n",
"| Density | $\\rho$ | $1.205$ kilograms per cubic meter |\n",
"\n",
"\n",
"### Boundaries\n",
"\n",
"We partition $\\partial \\Omega$ in the $6$ walls of the room. We make $5$ of the rigid (normal particle velocity set to $0$) while we make one having uniform velocity.\n",
"\n",
"| Boundary Definition | Particle Velocity Definition |\n",
"|------------------------------------------------------------------------------------------|---------------------------------------|\n",
"| $ \\partial \\Omega_0 \\doteq \\left\\{ \\mathbf{x} \\in \\overline{\\Omega} : x_0 = 0 \\right\\} $ | $ w_l \\left(\\mathbf{x}\\right) = 0 $ |\n",
"| $ \\partial \\Omega_1 \\doteq \\left\\{ \\mathbf{x} \\in \\overline{\\Omega} : x_0 = d_{x_0} \\right\\} $ | $ w_l \\left(\\mathbf{x}\\right) = 0 $ |\n",
"| $ \\partial \\Omega_2 \\doteq \\left\\{ \\mathbf{x} \\in \\overline{\\Omega} : x_1 = 0 \\right\\} $ | $ w_l \\left(\\mathbf{x}\\right) = 10 $ |\n",
"| $ \\partial \\Omega_3 \\doteq \\left\\{ \\mathbf{x} \\in \\overline{\\Omega} : x_1 = d_{x_1} \\right\\} $ | $ w_l \\left(\\mathbf{x}\\right) = 0 $ |\n",
"| $ \\partial \\Omega_4 \\doteq \\left\\{ \\mathbf{x} \\in \\overline{\\Omega} : x_2 = 0 \\right\\} $ | $ w_l \\left(\\mathbf{x}\\right) = 0 $ |\n",
"| $ \\partial \\Omega_5 \\doteq \\left\\{ \\mathbf{x} \\in \\overline{\\Omega} : x_2 = d_{x_2} \\right\\} $ | $ w_l \\left(\\mathbf{x}\\right) = 0 $ |\n",
"\n",
"### The Code\n",
"\n",
"All the ingredients are ready to put together the simulation.\n",
"\n",
"#### Importing the Needed Packages\n",
"\n",
"We start by importing what we need:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import dolfinx\n",
"import ufl\n",
"from petsc4py import PETSc\n",
"import pyvista\n",
"from mpi4py import MPI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Simulation Parameters\n",
"\n",
"We then define all the parameters:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"nu = 57.17 # Frequency of the Simulation, Hz\n",
"c = 343 # Speed of sound in air, m/s\n",
"rho = 1.205 # Density of air, kg/m^3\n",
"w = 10 # Velocity normal to boundary, for inflow Neumann condition, m/s\n",
"d_x0 = 4. # Room size along x0, m\n",
"d_x1 = 5. # Room size along x1, m\n",
"d_x2 = 3. # Room size along x2, m\n",
"tol = 1e-10 # Tolerance for boundary condition definitions\n",
"\n",
"omega = 2 * np.pi * nu # Angular frequency, rad/s\n",
"k = omega / c # Wave number, rad/m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Domain Definition and Meshing\n",
"\n",
"FEniCS solves our PDE through FEM. FEM makes use of two steps:\n",
"\n",
"* Derivation of the weak form;\n",
"* Domain meshing;\n",
"\n",
"We already derived the weak form for our problem. Domain meshing is instead what allows to reach an approximate numerical solution for our PDE. Meshing means that $\\Omega$ is cut into many non-overlapping subvolumes. We already mentioned how our uknown pressure spatial part $u$ is a vector in a Sobolev space. Through meshing we simplify this space to one that has _finite dimension_. This also transforms the integrals of the weak form in sums, which are then expressed as matrix-vector operations. We do not need to worry about these details: we specify the mesh and FEniCS will take care of the rest.\n",
"\n",
"FEM solutions converge to the actual PDE solution the finer the mesh and the higher the order. Each element of the mesh needs to be smaller than one tenth of the wavelenght for wave solutions to be satisfyingly accurate. Here we will match the element size to one 11-th of the wavelength:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"s = c / (11 * nu) # Element Size\n",
"n_x0 = np.intp(np.ceil(d_x0 / s)) # Number of elements for each direction\n",
"n_x1 = np.intp(np.ceil(d_x1 / s))\n",
"n_x2 = np.intp(np.ceil(d_x2 / s))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now it is the time do define the mesh. Since we have a rectangular room we use the `dolfinx.mesh.create_box` [function](https://docs.fenicsproject.org/dolfinx/main/python/generated/dolfinx.mesh.html?highlight=create_box#dolfinx.mesh.create_box):"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"mesh = dolfinx.mesh.create_box(\n",
" MPI.COMM_WORLD,\n",
" [[0, 0, 0], [d_x0, d_x1, d_x2]],\n",
" [n_x0, n_x1, n_x2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We then define the element type as a second order Lagrange element. This means that whith each element we express the unknown field as a linear superposition of second order Lagrange polynomials. FEM finds an approximate solution to the PDE by finding the coefficients of this superposition."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"P = ufl.FiniteElement(\"Lagrange\", mesh.ufl_cell(), 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Function Space\n",
"\n",
"Now we can go ahead and define a function space for the approximate solution."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"V = dolfinx.fem.FunctionSpace(mesh, P)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The formulation of the weak form in the simplified space is the same as in the original Sobolev space, except that the integrals reduce to sums. We do not need to worry about it as we simply need to specify the terms of the weak form.\n",
"\n",
"#### Bilinear Form \n",
"\n",
"In this function space `V` we pickup a generic function `u` from the space `V`, which contains all the possible approximate solutions (i.e. the Trial Function). Then, we take any test function `phi` with which we formulate the weak form."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"u = ufl.TrialFunction(V)\n",
"phi = ufl.TestFunction(V)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are now ready to specify the bilinear form"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"k_sq = dolfinx.fem.Constant(mesh, PETSc.ScalarType(k**2))\n",
"a = (\n",
" ufl.inner(ufl.nabla_grad(u), ufl.nabla_grad(phi)) * ufl.dx\n",
" - k_sq * ufl.inner(u, phi) * ufl.dx\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
":::{note}\n",
"You should pay attention to the fact that we use `inner` and `nabla_grad`. For complex-valued problems you need to use `ufl.inner` and *always* have the `TestFunction` as the second argument. However, they will for vector fields. You should always pick the most appropriate function for your problem. For an overview, see [page 25](https://link.springer.com/content/pdf/10.1007%2F978-3-319-52462-7.pdf#%5B%7B%22num%22%3A302%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C0%2C666%2Cnull%5D) and [page 58](https://link.springer.com/content/pdf/10.1007%2F978-3-319-52462-7.pdf#%5B%7B%22num%22%3A430%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C0%2C666%2Cnull%5D) of [Solving PDEs in Python](https://link.springer.com/content/pdf/10.1007%2F978-3-319-52462-7.pdf) and the [UFL-documentation](https://fenics.readthedocs.io/projects/ufl/en/latest/manual/form_language.html?highlight=complex#complex-values)\n",
":::\n",
"\n",
"\n",
"#### Boundary Conditions\n",
"\n",
"As we seen in the definition of the bilinear form `a` FEniCS does not have a symbol for integral. We specify instead the integration variable. Above we used `fenics.dx` which is the integration variable for the volume. If we had different submodules we could define custom `dx` values to specify integration within the subvolumes. We will do something along these lines to specify the boundaries.\n",
"\n",
"To tell FEniCS that we have 6 sub-boundaries we need to create 6 markers, that indicate what the coordinates of each region is.\n",
"\n",
"We then create a `MeshTag`, a collection of facets with different integers markers for each region.\n",
"\n",
"First, let's create the sub-domain markers."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def BX0(x):\n",
" return np.isclose(x[0], 0)\n",
"\n",
"\n",
"def BXL(x):\n",
" return np.isclose(x[0], d_x0)\n",
"\n",
"\n",
"def BY0(x):\n",
" return np.isclose(x[1], 0)\n",
"\n",
"\n",
"def BYL(x):\n",
" return np.isclose(x[1], d_x1)\n",
"\n",
"\n",
"def BZ0(x):\n",
" return np.isclose(x[2], 0)\n",
"\n",
"\n",
"def BZL(x):\n",
" return np.isclose(x[2], d_x2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are using the `numpy` function [isclose](https://numpy.org/doc/stable/reference/generated/numpy.isclose.html) since we are dealing with the mesh coordinates, which is expressed as floating point values. This means that there might be small errors; a point `x[0]` can have the value `2.22e-16` instead of `0`.\n",
"\n",
"Now, we find all facets on the boundary of our mesh using [dolfinx.mesh.locate_entities_boundary](https://docs.fenicsproject.org/dolfinx/v0.4.1/python/generated/dolfinx.mesh.html?highlight=locate_entities_boundary#dolfinx.mesh.locate_entities_boundary)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"X0_facets = dolfinx.mesh.locate_entities_boundary(mesh, mesh.topology.dim - 1, BX0)\n",
"X0_values = np.full_like(X0_facets, 1)\n",
"XL_facets = dolfinx.mesh.locate_entities_boundary(mesh, mesh.topology.dim - 1, BXL)\n",
"XL_values = np.full_like(XL_facets, 2)\n",
"Y0_facets = dolfinx.mesh.locate_entities_boundary(mesh, mesh.topology.dim - 1, BY0)\n",
"Y0_values = np.full_like(Y0_facets, 3)\n",
"YL_facets = dolfinx.mesh.locate_entities_boundary(mesh, mesh.topology.dim - 1, BYL)\n",
"YL_values = np.full_like(YL_facets, 4)\n",
"Z0_facets = dolfinx.mesh.locate_entities_boundary(mesh, mesh.topology.dim - 1, BZ0)\n",
"Z0_values = np.full_like(Z0_facets, 5)\n",
"ZL_facets = dolfinx.mesh.locate_entities_boundary(mesh, mesh.topology.dim - 1, BZL)\n",
"ZL_values = np.full_like(ZL_facets, 6)\n",
"facets = np.hstack([X0_facets, XL_facets, Y0_facets, YL_facets, Z0_facets, ZL_facets])\n",
"values = np.hstack([X0_values, XL_values, Y0_values, YL_values, Z0_values, ZL_values])\n",
"sort_order = np.argsort(facets)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we make the collection of facets and values"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"facet_tags = dolfinx.mesh.meshtags(mesh, mesh.topology.dim - 1, facets[sort_order], values[sort_order])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that this is done we can define the custom integration variables `ds` for our boundary. `ds` will return the measure for each different boundary if we use, as input, the ID for that boundary. For example, `ds(5)` will return the integration variable for the `bz0` boundary. Note that we apply a non-zero velocity to `by0`, which has ID `3`."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"ds = ufl.Measure('ds', domain=mesh, subdomain_data=facet_tags)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we simply need to define the acoustic fluxes. We need two: one for the rigid walls and one for the active walls"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# The flux is 0 for all rigid walls\n",
"g_rig = dolfinx.fem.Constant(mesh, PETSc.ScalarType(0))\n",
"\n",
"# The flux is this for the active walls\n",
"g_in = dolfinx.fem.Constant(mesh, 1j * omega * rho * w)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Linear Form \n",
"We create the linear form, by splitting the boundary integral into the 6 regions."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"L = ufl.inner(g_rig, phi) * ds((1, 2, 4, 5, 6)) + ufl.inner(g_in, phi) * ds(3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Solution\n",
"\n",
"Now that we have both the bilinear form `a` and the linear form `L` properly defined it is easy to solve for the approximate PDE solution. We create a function to hold the discrete solution `uh` with a new function from `V` in which we want to store our result. Then, we create a `dolfinx.fem.LinearProblem` class, that sets up all the structures need to solve the linear algebra problem."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"uh = dolfinx.fem.Function(V)\n",
"problem = dolfinx.fem.petsc.LinearProblem(a, L, u=uh)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We next call `problem.solve()` to solve the PDE."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Coefficient(FunctionSpace(Mesh(VectorElement(FiniteElement('Lagrange', tetrahedron, 1), dim=3), 0), FiniteElement('Lagrange', tetrahedron, 2)), 0)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"problem.solve()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Visualizing Results\n",
"\n",
"`u` contains the real and imaginary parts of the field. We visualize each of them separatly."
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"pyvista.set_jupyter_backend(\"pythreejs\")\n",
"p_mesh = pyvista.UnstructuredGrid(*dolfinx.plot.create_vtk_mesh(mesh, mesh.topology.dim))\n",
"pyvista_cells, cell_types, geometry = dolfinx.plot.create_vtk_mesh(V)\n",
"grid = pyvista.UnstructuredGrid(pyvista_cells, cell_types, geometry)\n",
"grid.point_data[\"u_real\"] = uh.x.array.real\n",
"grid.point_data[\"u_imag\"] = uh.x.array.imag\n",
"grid.set_active_scalars(\"u_real\")"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "535b510b0b844f9cbfc505fedee224d5",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Renderer(camera=PerspectiveCamera(aspect=1.3333333333333333, children=(DirectionalLight(intensity=0.25, positi…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"p_real = pyvista.Plotter()\n",
"p_real.add_text(\"uh real\", position=\"upper_edge\", font_size=14, color=\"black\")\n",
"p_real.add_mesh(grid, show_edges=True, style=\"points\", point_size=25)\n",
"p_real.add_mesh(p_mesh, show_edges=True, style=\"wireframe\")\n",
"if not pyvista.OFF_SCREEN:\n",
" p_real.show()\n",
"\n",
"with dolfinx.io.VTXWriter(mesh.comm, \"output.bp\", [uh]) as vtx:\n",
" vtx.write(0.0)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "36c42b5803234b98a1f6c2d2571aca35",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Renderer(camera=PerspectiveCamera(aspect=1.3333333333333333, children=(DirectionalLight(intensity=0.25, positi…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"p_imag = pyvista.Plotter()\n",
"\n",
"grid.set_active_scalars(\"u_imag\")\n",
"p_imag.add_mesh(p_mesh, show_edges=True, style=\"wireframe\")\n",
"p_imag.add_text(\"uh imag\", position=\"upper_edge\", font_size=14, color=\"black\")\n",
"p_imag.add_mesh(grid, show_edges=True, style=\"points\", point_size=25)\n",
"if not pyvista.OFF_SCREEN:\n",
" p_imag.show()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### A Comment About the Solution\n",
"\n",
"You have probably noticed that the real part of the solution is pretty much 0 everywhere. This is to be expected because:\n",
"\n",
"* The material is lossless;\n",
"* The flux is purely imaginary;"
]
}
],
"metadata": {
"jupytext": {
"formats": "ipynb,py:light"
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}