Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
### Elliptic3D Dirichlet Boundary Conditions in All Directions

Solves the 3D Poisson equation on a cube with non-homogeneous Dirichlet boundary conditions on all six faces, and validates the numerical solution against a known exact solution.

$$
-\nabla^2 u(x,y,z) = -6(x+y+z)
$$

with domain $(x,y,z) \in [-1,1]^3$ and boundary conditions:

$$
u(x,y,z) = x^3 + y^3 + z^3 \quad \text{on all six faces of the cube}
$$

Written out face by face:

$$
\begin{aligned}
u(-1,y,z) &= -1 + y^3 + z^3, & u(1,y,z) &= 1 + y^3 + z^3, \\
u(x,-1,z) &= -1 + x^3 + z^3, & u(x,1,z) &= 1 + x^3 + z^3, \\
u(x,y,-1) &= -1 + x^3 + y^3, & u(x,y,1) &= 1 + x^3 + y^3.
\end{aligned}
$$

#### Mathematical Background

The exact solution is

$$
u(x,y,z) = x^3 + y^3 + z^3
$$

Its second derivatives are $u_{xx} = 6x$, $u_{yy} = 6y$, $u_{zz} = 6z$, so

$$
-\nabla^2 u = -(u_{xx} + u_{yy} + u_{zz}) = -6(x+y+z),
$$

which is the forcing term above. The boundary values are obtained by evaluating the exact solution on each face. Because the exact solution is known everywhere, this example is a direct check on the accuracy of the 3D mimetic Laplacian together with `addScalarBC3D`.

The boundary conditions are given in the general form used by MOLE,

$$
a\,u + b\,\frac{\partial u}{\partial n} = g,
$$

with $a = 1$ and $b = 0$ on every face (pure Dirichlet), and $g$ equal to the exact solution on that face.

#### Implementation Details

The grid has $m = 29$ cells in $x$, $n = m+2 = 31$ cells in $y$, and $o = m+4 = 33$ cells in $z$, so $\Delta x = 2/m$, $\Delta y = 2/n$, $\Delta z = 2/o$. The numbers of cells are odd so that a middle slice of the cube lies exactly on a grid plane for plotting. The unknowns live at cell centers plus boundary face centers, giving $(m+2)(n+2)(o+2)$ unknowns. The operators use order of accuracy $k = 2$.

The linear system is built in three steps:

1. **Operator.** `A = -lap3D(k, m, dx, n, dy, o, dz, dc, nc)` is the (negated) 3D mimetic Laplacian.
2. **Right-hand side.** `b = -6*(X+Y+Z)`, evaluated on the grid and reshaped to a vector.
3. **Boundary conditions.** `[A0, b0] = addScalarBC3D(A, b, k, m, dx, n, dy, o, dz, dc, nc, v)` replaces the boundary rows of the system with the boundary condition equations.

The arguments of `addScalarBC3D` encode the boundary conditions. Each is ordered as (left, right, bottom, top, front, back), meaning the faces $x=-1$, $x=1$, $y=-1$, $y=1$, $z=-1$, $z=1$:

| Argument | Value | Meaning |
|---|---|---|
| `dc` | `[1;1;1;1;1;1]` | Coefficient $a$ of $u$ on each face |
| `nc` | `[0;0;0;0;0;0]` | Coefficient $b$ of $\partial u / \partial n$ on each face |
| `v` | `{bcl; bcr; bcb; bct; bcf; bcz}` | Boundary values $g$ on each face |

The boundary value vectors have different lengths so that edges and corners shared by two faces are only set once: the left/right vectors have $n \cdot o$ entries, the bottom/top vectors have $(m+2) \cdot o$ entries, and the front/back vectors have $(m+2)(n+2)$ entries. In the script they are taken from the exact solution `ue` on each face, with the shared edges trimmed off (`bcl(2:end-1,2:end-1)`, `bcb(:,2:end-1)`).

The system `A0 * u = b0` is then solved with Octave's backslash operator.

#### Results

The script plots the numerical and exact solutions on the middle YZ, XZ, and XY slices of the cube, and prints the maximum and relative error. With the default grid ($m = 29$), the maximum absolute error is about $1.2 \times 10^{-4}$, and the relative error (maximum error divided by the range of the exact solution) is about $0.002\%$.

```{figure} figures/elliptic3D_dirichlet.png
:width: 100%

Left: numerical solution on the middle XY slice ($z = 0$). Right: absolute error $|u_e - u_a|$ on the same slice. The error is zero on the Dirichlet boundaries and largest in the interior.
```

Refining the grid shows the error decreasing by roughly a factor of 8 each time the cell size is halved:

| $m$ | Maximum error |
|---|---|
| 9 | $3.6 \times 10^{-3}$ |
| 19 | $4.2 \times 10^{-4}$ |
| 39 | $5.0 \times 10^{-5}$ |

---

This example is implemented in:
- [Octave](https://github.com/csrc-sdsu/mole/blob/main/examples/octave/elliptic3DXDirichletYDirichletZDirichlet.m)

#### Variants

Related 3D examples:
- {doc}`Elliptic3D <Elliptic3D>`: Laplace equation with one face held at a constant value
- {doc}`Elliptic3D with addScalarBC3D <Elliptic3D-addScalarBC3D>`: the same problem as Elliptic3D, with boundary conditions applied by `addScalarBC3D`

The 1D version of this setup is {doc}`Elliptic1D Non-Homogeneous Dirichlet <../1D/Elliptic1D-Non-Homogeneous-Dirichlet>`.
73 changes: 73 additions & 0 deletions doc/sphinx/source/examples/Elliptic/3D/Elliptic3D-addScalarBC3D.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
### Elliptic3D with addScalarBC3D

Solves the same problem as {doc}`Elliptic3D <Elliptic3D>`, but applies the boundary conditions with the general function `addScalarBC3D` instead of `robinBC3D`. One face of a box is held at $u = 100$ and the other five at $u = 0$; the solution shows how that value diffuses into the interior.

$$
\nabla^2 u(x,y,z) = 0
$$

with domain $x \in [0,5]$, $y \in [0,6]$, $z \in [0,7]$ and boundary conditions:

$$
u(x,y,0) = 100, \qquad u = 0 \ \text{on the other five faces.}
$$

#### Mathematical Background

This is Laplace's equation, which describes steady-state situations where nothing is changing over time. For example, it models the temperature in a box whose front face is held at 100 degrees and whose other faces are held at 0, once the temperature has stopped changing. Since there is no source inside the domain, the solution is determined by the boundary values alone: it is 100 on the front face and decays toward 0 away from it.

The boundary conditions use MOLE's general form

$$
a\,u + b\,\frac{\partial u}{\partial n} = g,
$$

with $a = 1$ and $b = 0$ (pure Dirichlet) on all six faces, $g = 100$ on the front face, and $g = 0$ elsewhere.

#### Implementation Details

The grid has $m = 5$, $n = 6$, and $o = 7$ cells with unit spacing ($\Delta x = \Delta y = \Delta z = 1$). Including the boundary face centers, the solution array has size $(m+2) \times (n+2) \times (o+2) = 7 \times 8 \times 9$. The operators use order of accuracy $k = 2$.

The boundary conditions are described by three arguments, each ordered as (left, right, bottom, top, front, back):

| Argument | Value | Meaning |
|---|---|---|
| `dc` | `[1;1;1;1;1;1]` | Coefficient $a$ of $u$ on each face |
| `nc` | `[0;0;0;0;0;0]` | Coefficient $b$ of $\partial u / \partial n$ on each face |
| `v` | `{bcl; bcr; bcb; bct; bcf; bcz}` | Boundary values $g$: `bcf = 100*ones(...)`, all others zero |

The vectors in `v` have different lengths so that each edge and corner is set only once: $n \cdot o$ for left/right, $(m+2) \cdot o$ for bottom/top, and $(m+2)(n+2)$ for front/back.

The equation is discretized using mimetic finite difference operators with order of accuracy $k = 2$.

The discrete system is:

$$L u = b$$

where:

- $L$ is the 3D mimetic Laplacian, built with `lap3D(k, m, 1, n, 1, o, 1, dc, nc)`
- $b = 0$ in the interior (no source term)
- the boundary rows of $L$ and $b$ are replaced with the boundary condition equations by `addScalarBC3D`

#### Results

Running both scripts gives identical solutions (the maximum difference between them is exactly zero), which confirms that the two ways of applying the boundary condition are equivalent for this problem.

The script displays one $xy$ page of the solution (page `p = 2`, the first interior layer next to the $u = 100$ face) with `imagesc`.

```{figure} figures/elliptic3D_addScalarBC3D.png
:width: 100%

Left: the page the script displays, next to the $u = 100$ face. Values are largest in the center and drop to 0 at the edges. Right: a middle slice through the box, showing the solution decaying from 100 on the front face toward 0.
```

---

This example is implemented in:
- [Octave](https://github.com/csrc-sdsu/mole/blob/main/examples/octave/elliptic3DaddScalarBC3D.m)

#### Variants

- {doc}`Elliptic3D <Elliptic3D>`: the same problem using `robinBC3D` ([Octave](https://github.com/csrc-sdsu/mole/blob/main/examples/octave/elliptic3D.m), [C++](https://github.com/csrc-sdsu/mole/blob/main/examples/cpp/elliptic3D.cpp))
- {doc}`Elliptic3D Dirichlet in All Directions <Elliptic3D-Dirichlet-Dirichlet-Dirichlet>`: `addScalarBC3D` with non-homogeneous Dirichlet conditions and an exact solution
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions doc/sphinx/source/examples/Elliptic/3D/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ This section demonstrates three-dimensional elliptic partial differential equati
:maxdepth: 1

Elliptic3D
Elliptic3D-addScalarBC3D
Elliptic3D-Dirichlet-Dirichlet-Dirichlet
```