# How to discretize and solve a 3D Poisson Equation with Dirichlet boundary conditions owing to DiffEqOperators

**URL:** https://discourse.julialang.org/t/how-to-discretize-and-solve-a-3d-poisson-equation-with-dirichlet-boundary-conditions-owing-to-diffeqoperators/40796
**Category:** General Usage
**Tags:** diffeq
**Created:** [June 5, 2020, 7:25am UTC](https://discourse.julialang.org/t/how-to-discretize-and-solve-a-3d-poisson-equation-with-dirichlet-boundary-conditions-owing-to-diffeqoperators/40796 "2020-06-05T07:25:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![RenaudV](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@RenaudV](https://discourse.julialang.org/u/RenaudV)
#### Post date: [June 5, 2020, 7:25am UTC](https://discourse.julialang.org/t/how-to-discretize-and-solve-a-3d-poisson-equation-with-dirichlet-boundary-conditions-owing-to-diffeqoperators/40796/1 "2020-06-05T07:25:27Z")

</div>

I love the following example of solving a 1D Poisson equation with DiffEqOperators.  
However , I struggle to do the analog 3D example. Can somebody indicate me the right steps?

**The 1D formulation is:**

using DiffEqOperators, Plots  
#Poisson Eq in 1D

# `Δu = f` with boundary conditions `u(0) = 0` and `u(1) = 0`

Δx = 0.1  
x = Δx:Δx:1-Δx # Solve only for the interior: the endpoints are known to be zero!  
N = length(x)  
f = sin.(2π\*x)

Δ = CenteredDifference(2, 2, Δx, N)  
bc = Dirichlet0BC(Float64)  
u = (Δ\*bc) \ f

u\_analytic(x) = -sin(2π\*x)/4(π^2)

plot(x, u)  
plot!(x, u\_analytic.(x))

**An equivalent 1D formulation, which works well, is:**

Δx = 0.1  
x = Δx:Δx:1-Δx # Solve only for the interior: the endpoints are known to be zero!  
N = length(x)  
f = sin.(2π_x)  
Δ = CenteredDifference(2, 2, Δx, N)  
M = zeros(N,N)  
for i in 1:N  
for j in 1:N  
if i==j M[i,j] = 1.0 end  
end  
end  
Qx = Dirichlet0BC{1}(Float64, size(M))  
MwBC = Qx_M

u\_analytic(x) = -sin(2π_x)/4(π^2)  
u = (Δ_MwBC) \ f

plot(x, u)  
plot!(x, u\_analytic.(x))

**In 3D, I could define Δ as:**

Δxx = CenteredDifference{1}(2,2,Δx,N)  
Δyy = CenteredDifference{2}(2,2,Δx,N)  
Δzz = CenteredDifference{3}(2,2,Δx,N)  
Δ = Δxx + Δyy + Δzz

Then I could define a 3D matrix M and boundary conditions as:  
M = zeros(N, N, N)  
for i in 1:N  
for j in 1:N  
for k in 1:N  
if i==j M[i, j, k] = 1.0 end  
end  
end  
end

Qx = Dirichlet0BC{1}(Float64, size(M))  
Qy = Dirichlet0BC{2}(Float64, size(M))  
Qz = Dirichlet0BC{3}(Float64, size(M))  
Q = compose(Qx, Qy, Qz)

MwBC = Q\*M

Finally the Laplace operator would be placed in M  
mult!(M, Δ, MwBC)

Is that correct ?  
But then, what to do ?  
Thanks in advance for any kind of answer …

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 7, 2020, 4:45am UTC](https://discourse.julialang.org/t/how-to-discretize-and-solve-a-3d-poisson-equation-with-dirichlet-boundary-conditions-owing-to-diffeqoperators/40796/2 "2020-06-07T04:45:23Z")

</div>

Hey,  
The package isn’t quite ready for this. The PR to fix 3D BCs is still a work in progress but is making a lot of progress over the last week: [Rebased #168 by ChrisRackauckas · Pull Request #246 · SciML/DiffEqOperators.jl · GitHub](https://github.com/SciML/DiffEqOperators.jl/pull/246)

---

<div class="post-metadata">

### Author: ![RenaudV](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@RenaudV](https://discourse.julialang.org/u/RenaudV)
#### Post date: [June 8, 2020, 6:37am UTC](https://discourse.julialang.org/t/how-to-discretize-and-solve-a-3d-poisson-equation-with-dirichlet-boundary-conditions-owing-to-diffeqoperators/40796/3 "2020-06-08T06:37:14Z")

</div>

Dear Chris, thanks a lot for your answer. I thought indeed the package was not really ready for 3D yet. The work in progress looks very exciting. Thanks a lot for all this and good luck with its development. I’d love to use it and help, but I am afraid I am too far from the required knowledge for that …!
