diff --git a/.github/actions/install-dependencies/action.yml b/.github/actions/install-dependencies/action.yml index 2e3dbdf6..c7db29e5 100644 --- a/.github/actions/install-dependencies/action.yml +++ b/.github/actions/install-dependencies/action.yml @@ -6,4 +6,4 @@ runs: - name: Install apt dependencies and upgrade pip shell: bash -el {0} run: | - apt-get update && apt-get install -y libxrender1 libgl1-mesa-dev mesa-utils + apt-get update && apt-get install -y libxrender1 libgl1-mesa-dev mesa-utils gzip \ No newline at end of file diff --git a/chapter3/em.ipynb b/chapter3/em.ipynb index 4a983877..23a1998c 100644 --- a/chapter3/em.ipynb +++ b/chapter3/em.ipynb @@ -44,7 +44,7 @@ "\n", "which holds for an isotropic linear magnetic medium.\n", "Here, $\\mu$ is the magnetic permability of the material.\n", - "Now, since $B$ is solenodial (divergence free) accoording to Maxwell's equations, we known that $B$ must be the curl of some vector field $A$. This field is called the magnetic vector potential. Since the problem is static and thus $\\frac{\\partial D}{\\partial t}=0$, it follows that\n", + "Now, since $B$ is solenodial (divergence free) according to Maxwell's equations, we known that $B$ must be the curl of some vector field $A$. This field is called the magnetic vector potential. Since the problem is static and thus $\\frac{\\partial D}{\\partial t}=0$, it follows that\n", "\n", "$$\n", "J = \\nabla \\times H = \\nabla \\times(\\mu^{-1} B)=\\nabla \\times (\\mu^{-1}\\nabla \\times A ) = -\\nabla \\cdot (\\mu^{-1}\\nabla A).\n", diff --git a/chapter3/em.py b/chapter3/em.py index e8dd582f..6efe178a 100644 --- a/chapter3/em.py +++ b/chapter3/em.py @@ -53,7 +53,7 @@ # # which holds for an isotropic linear magnetic medium. # Here, $\mu$ is the magnetic permability of the material. -# Now, since $B$ is solenodial (divergence free) accoording to Maxwell's equations, we known that $B$ must be the curl of some vector field $A$. This field is called the magnetic vector potential. Since the problem is static and thus $\frac{\partial D}{\partial t}=0$, it follows that +# Now, since $B$ is solenodial (divergence free) according to Maxwell's equations, we known that $B$ must be the curl of some vector field $A$. This field is called the magnetic vector potential. Since the problem is static and thus $\frac{\partial D}{\partial t}=0$, it follows that # # $$ # J = \nabla \times H = \nabla \times(\mu^{-1} B)=\nabla \times (\mu^{-1}\nabla \times A ) = -\nabla \cdot (\mu^{-1}\nabla A). diff --git a/chapter3/subdomains.ipynb b/chapter3/subdomains.ipynb index c43de222..250e3490 100644 --- a/chapter3/subdomains.ipynb +++ b/chapter3/subdomains.ipynb @@ -5,11 +5,13 @@ "metadata": {}, "source": [ "# Defining subdomains for different materials\n", + "\n", "Author: Jørgen S. Dokken\n", "\n", "Solving PDEs in domains made up of different materials is a frequently encountered task. In FEniCSx, we handle these problems by defining a Discontinous cell-wise constant function.\n", "Such a function can be created over any mesh in the following way\n", - "## Subdomains on built-in meshes" + "\n", + "## Subdomains on built-in meshes\n" ] }, { @@ -56,7 +58,7 @@ }, "source": [ "We will use a simple example with two materials in two dimensions to demonstrate the idea. The whole domain will be $\\Omega=[0,1]\\times[0,1]$, which consists of two subdomains\n", - "$\\Omega_0=[0,1]\\times [0,1/2]$ and $\\Omega_1=[0,1]\\times[1/2, 1]$. We start by creating two python functions, where each returns `True` if the input coordinate is inside its domain." + "$\\Omega_0=[0,1]\\times [0,1/2]$ and $\\Omega_1=[0,1]\\times[1/2, 1]$. We start by creating two python functions, where each returns `True` if the input coordinate is inside its domain.\n" ] }, { @@ -84,14 +86,16 @@ "$$\n", "-\\nabla \\cdot [\\kappa (x,y)\\nabla u(x, y)]= 1 \\qquad \\text{in } \\Omega,\n", "$$\n", + "\n", "$$\n", "u=u_D=1 \\qquad \\text{on } \\partial\\Omega_D=[0,y], y\\in[0,1]\n", "$$\n", + "\n", "$$\n", "-\\frac{\\partial u}{\\partial n}=0 \\qquad \\text{on } \\partial\\Omega\\setminus \\partial\\Omega_D\n", "$$\n", "\n", - "Our next step is to define $\\kappa$" + "Our next step is to define $\\kappa$\n" ] }, { @@ -112,7 +116,7 @@ "In the previous code block, we found which cells (triangular elements) satisfy the condition for being in $\\Omega_0, \\Omega_1$. As the $DG-0$ function contains only one degree of freedom per cell, there is a one to one mapping between the cell indicies and the degrees of freedom. We let $\\kappa=\\begin{cases}\n", "1 &\\text{if } x\\in\\Omega_0\\\\\n", "0.1& \\text{if } x\\in\\Omega_1\\\\\n", - "\\end{cases}$" + "\\end{cases}$\n" ] }, { @@ -129,7 +133,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We are now ready to define our variational formulation and Dirichlet boundary condition after using integration by parts" + "We are now ready to define our variational formulation and Dirichlet boundary condition after using integration by parts\n" ] }, { @@ -151,7 +155,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We can now solve and visualize the solution of the problem" + "We can now solve and visualize the solution of the problem\n" ] }, { @@ -217,7 +221,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We clearly observe different behavior in the two regions, which both have the same Dirichlet boundary condition on the left side, where $x=0$." + "We clearly observe different behavior in the two regions, which both have the same Dirichlet boundary condition on the left side, where $x=0$.\n" ] }, { @@ -227,7 +231,8 @@ }, "source": [ "## Interpolation with Python-function\n", - "As we saw in the first approach, in many cases, we can use the geometrical coordinates to determine which coefficient we should use. Using the unstructured mesh from the previous example, we illustrate an alternative approach using interpolation:" + "\n", + "As we saw in the first approach, in many cases, we can use the geometrical coordinates to determine which coefficient we should use. Using the unstructured mesh from the previous example, we illustrate an alternative approach using interpolation:\n" ] }, { @@ -261,7 +266,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We verify this by assembling the error between this new function and the old one" + "We verify this by assembling the error between this new function and the old one\n" ] }, { @@ -280,7 +285,8 @@ "metadata": {}, "source": [ "## Subdomains defined from external mesh data\n", - "Let us now consider the same problem, but using GMSH to generate the mesh and subdomains. We will then in turn show how to use this data to generate discontinuous functions in DOLFINx." + "\n", + "Let us now consider the same problem, but using GMSH to generate the mesh and subdomains. We will then in turn show how to use this data to generate discontinuous functions in DOLFINx.\n" ] }, { @@ -329,7 +335,8 @@ "metadata": {}, "source": [ "## Read in MSH files with DOLFINx\n", - "You can read in MSH files with DOLFINx, which will read them in on a single process, and then distribute them over the available ranks in the MPI communicator." + "\n", + "You can read in MSH files with DOLFINx, which will read them in on a single process, and then distribute them over the available ranks in the MPI communicator.\n" ] }, { @@ -353,14 +360,17 @@ }, "source": [ "## Convert msh-files to XDMF using meshio\n", + "\n", "We will use `meshio` to read in the `msh` file, and convert it to a more suitable IO format. Meshio requires `h5py`, and can be installed on linux with the following commands:\n", + "\n", "```{code}\n", "export HDF5_MPI=\"ON\"\n", "export CC=mpicc\n", "export HDF5_DIR=\"/usr/lib/x86_64-linux-gnu/hdf5/mpich/\"\n", "pip3 install --no-cache-dir --no-binary=h5py h5py meshio\n", "```\n", - "We start by creating a convenience function for extracting data for a single cell type, and creating a new `meshio.Mesh`." + "\n", + "We start by creating a convenience function for extracting data for a single cell type, and creating a new `meshio.Mesh`.\n" ] }, { @@ -385,7 +395,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This function returns a meshio mesh, including physical markers for the given type. The `prune_z` argument is for cases where we want to use two dimensional meshes. The last coordinate in the mesh (as it is generated in a 3D space) has to be removed for DOLFINx to consider this as a two dimensional geometry." + "This function returns a meshio mesh, including physical markers for the given type. The `prune_z` argument is for cases where we want to use two dimensional meshes. The last coordinate in the mesh (as it is generated in a 3D space) has to be removed for DOLFINx to consider this as a two dimensional geometry.\n" ] }, { @@ -401,8 +411,8 @@ " # Create and save one file for the mesh, and one file for the facets\n", " triangle_mesh = create_mesh(msh, \"triangle\", prune_z=True)\n", " line_mesh = create_mesh(msh, \"line\", prune_z=True)\n", - " meshio.write(\"mesh.xdmf\", triangle_mesh)\n", - " meshio.write(\"mt.xdmf\", line_mesh)\n", + " meshio.write(\"mesh.xdmf\", triangle_mesh, compression=None)\n", + " meshio.write(\"mt.xdmf\", line_mesh, compression=None)\n", "MPI.COMM_WORLD.barrier()" ] }, @@ -412,7 +422,7 @@ "source": [ "We have now written the mesh and the cell markers to one file, and the facet markers in a separate file. We can now read this data in DOLFINx using `XDMFFile.read_mesh` and `XDMFFile.read_meshtags`. The `dolfinx.MeshTags` stores the index of the entity, along with the value of the marker in two one dimensional arrays.\n", "\n", - "Note that we have generated and written the mesh on only one processor. However, the `xdmf`-format supports parallel IO, and we can thus read the mesh in parallel." + "Note that we have generated and written the mesh on only one processor. However, the `xdmf`-format supports parallel IO, and we can thus read the mesh in parallel.\n" ] }, { @@ -433,7 +443,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We have now read in the mesh and corresponding cell and facet data. We can now create our discontinuous function `kappa` as follows" + "We have now read in the mesh and corresponding cell and facet data. We can now create our discontinuous function `kappa` as follows\n" ] }, { @@ -454,7 +464,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We can also efficiently use the facet data `ft` to create the Dirichlet boundary condition" + "We can also efficiently use the facet data `ft` to create the Dirichlet boundary condition\n" ] }, { @@ -475,7 +485,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We can now solve the problem in a similar fashion as above" + "We can now solve the problem in a similar fashion as above\n" ] }, { diff --git a/chapter3/subdomains.py b/chapter3/subdomains.py index 33baff2e..3f368778 100644 --- a/chapter3/subdomains.py +++ b/chapter3/subdomains.py @@ -14,11 +14,14 @@ # --- # # Defining subdomains for different materials +# # Author: Jørgen S. Dokken # # Solving PDEs in domains made up of different materials is a frequently encountered task. In FEniCSx, we handle these problems by defining a Discontinous cell-wise constant function. # Such a function can be created over any mesh in the following way +# # ## Subdomains on built-in meshes +# # + from dolfinx import default_scalar_type @@ -54,6 +57,7 @@ # We will use a simple example with two materials in two dimensions to demonstrate the idea. The whole domain will be $\Omega=[0,1]\times[0,1]$, which consists of two subdomains # $\Omega_0=[0,1]\times [0,1/2]$ and $\Omega_1=[0,1]\times[1/2, 1]$. We start by creating two python functions, where each returns `True` if the input coordinate is inside its domain. +# # + @@ -74,14 +78,17 @@ def Omega_1(x): # $$ # -\nabla \cdot [\kappa (x,y)\nabla u(x, y)]= 1 \qquad \text{in } \Omega, # $$ +# # $$ # u=u_D=1 \qquad \text{on } \partial\Omega_D=[0,y], y\in[0,1] # $$ +# # $$ # -\frac{\partial u}{\partial n}=0 \qquad \text{on } \partial\Omega\setminus \partial\Omega_D # $$ # # Our next step is to define $\kappa$ +# kappa = Function(Q) cells_0 = locate_entities(mesh, mesh.topology.dim, Omega_0) @@ -91,11 +98,13 @@ def Omega_1(x): # 1 &\text{if } x\in\Omega_0\\ # 0.1& \text{if } x\in\Omega_1\\ # \end{cases}$ +# kappa.x.array[cells_0] = np.full_like(cells_0, 1, dtype=default_scalar_type) kappa.x.array[cells_1] = np.full_like(cells_1, 0.1, dtype=default_scalar_type) -# We are now ready to define our variational formulation and Dirichlet boundary condition after using integration by parts +# We are now ready to define our variational formulation and Dirichlet boundary condition after using integration by parts +# V = functionspace(mesh, ("Lagrange", 1)) u, v = TrialFunction(V), TestFunction(V) @@ -106,6 +115,7 @@ def Omega_1(x): bcs = [dirichletbc(default_scalar_type(1), dofs, V)] # We can now solve and visualize the solution of the problem +# # + problem = LinearProblem( @@ -152,9 +162,12 @@ def Omega_1(x): # We clearly observe different behavior in the two regions, which both have the same Dirichlet boundary condition on the left side, where $x=0$. +# # ## Interpolation with Python-function +# # As we saw in the first approach, in many cases, we can use the geometrical coordinates to determine which coefficient we should use. Using the unstructured mesh from the previous example, we illustrate an alternative approach using interpolation: +# def eval_kappa(x): @@ -172,13 +185,16 @@ def eval_kappa(x): kappa2.interpolate(eval_kappa) # We verify this by assembling the error between this new function and the old one +# # Difference in kappa's error = mesh.comm.allreduce(assemble_scalar(form((kappa - kappa2) ** 2 * dx))) print(error) # ## Subdomains defined from external mesh data +# # Let us now consider the same problem, but using GMSH to generate the mesh and subdomains. We will then in turn show how to use this data to generate discontinuous functions in DOLFINx. +# gmsh.initialize() proc = MPI.COMM_WORLD.rank @@ -215,7 +231,9 @@ def eval_kappa(x): gmsh.finalize() # ## Read in MSH files with DOLFINx +# # You can read in MSH files with DOLFINx, which will read them in on a single process, and then distribute them over the available ranks in the MPI communicator. +# mesh_data = gmshio.read_from_msh("mesh.msh", MPI.COMM_WORLD, gdim=2) mesh = mesh_data.mesh @@ -225,14 +243,18 @@ def eval_kappa(x): facet_markers = mesh_data.facet_tags # ## Convert msh-files to XDMF using meshio +# # We will use `meshio` to read in the `msh` file, and convert it to a more suitable IO format. Meshio requires `h5py`, and can be installed on linux with the following commands: +# # ```{code} # export HDF5_MPI="ON" # export CC=mpicc # export HDF5_DIR="/usr/lib/x86_64-linux-gnu/hdf5/mpich/" # pip3 install --no-cache-dir --no-binary=h5py h5py meshio # ``` +# # We start by creating a convenience function for extracting data for a single cell type, and creating a new `meshio.Mesh`. +# def create_mesh(mesh, cell_type, prune_z=False): @@ -248,6 +270,7 @@ def create_mesh(mesh, cell_type, prune_z=False): # This function returns a meshio mesh, including physical markers for the given type. The `prune_z` argument is for cases where we want to use two dimensional meshes. The last coordinate in the mesh (as it is generated in a 3D space) has to be removed for DOLFINx to consider this as a two dimensional geometry. +# if proc == 0: # Read in mesh @@ -256,13 +279,14 @@ def create_mesh(mesh, cell_type, prune_z=False): # Create and save one file for the mesh, and one file for the facets triangle_mesh = create_mesh(msh, "triangle", prune_z=True) line_mesh = create_mesh(msh, "line", prune_z=True) - meshio.write("mesh.xdmf", triangle_mesh) - meshio.write("mt.xdmf", line_mesh) + meshio.write("mesh.xdmf", triangle_mesh, compression=None) + meshio.write("mt.xdmf", line_mesh, compression=None) MPI.COMM_WORLD.barrier() # We have now written the mesh and the cell markers to one file, and the facet markers in a separate file. We can now read this data in DOLFINx using `XDMFFile.read_mesh` and `XDMFFile.read_meshtags`. The `dolfinx.MeshTags` stores the index of the entity, along with the value of the marker in two one dimensional arrays. # # Note that we have generated and written the mesh on only one processor. However, the `xdmf`-format supports parallel IO, and we can thus read the mesh in parallel. +# with XDMFFile(MPI.COMM_WORLD, "mesh.xdmf", "r") as xdmf: mesh = xdmf.read_mesh(name="Grid") @@ -272,6 +296,7 @@ def create_mesh(mesh, cell_type, prune_z=False): ft = xdmf.read_meshtags(mesh, name="Grid") # We have now read in the mesh and corresponding cell and facet data. We can now create our discontinuous function `kappa` as follows +# Q = functionspace(mesh, ("DG", 0)) kappa = Function(Q) @@ -281,6 +306,7 @@ def create_mesh(mesh, cell_type, prune_z=False): kappa.x.array[top_cells] = np.full_like(top_cells, 0.1, dtype=default_scalar_type) # We can also efficiently use the facet data `ft` to create the Dirichlet boundary condition +# V = functionspace(mesh, ("Lagrange", 1)) u_bc = Function(V) @@ -290,6 +316,7 @@ def create_mesh(mesh, cell_type, prune_z=False): bcs = [dirichletbc(default_scalar_type(1), left_dofs, V)] # We can now solve the problem in a similar fashion as above +# # + u, v = TrialFunction(V), TestFunction(V)