Skip to content
Merged
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
38 changes: 34 additions & 4 deletions julia/MOLE.jl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,38 @@ Once you have built the documentation (either from the REPL or the command line)

The MOLE library contains examples demonstrating how to use the operators, in a broad range of partial differential equations (PDEs). More information on the mathematical content can be found in the [main MOLE documentation](https://mole-docs.readthedocs.io/en/main/examples/index.html).

Currently, the following examples are available in the MOLE Julia package.
The examples are organized by PDE type and dimension in the base MOLE documentation:

- Elliptic Problems
- 1D Examples
- `elliptic1D`: A script that solves the 1D Poisson's equation with Robin boundary conditions using mimetic operators.
- **Base Examples Documentation:** https://csrc-sdsu.github.io/mole/stable/examples/

Currently, not all of them are available in the MOLE Julia package. They will be added in the near future.

## Formatter

To test the JuliaFormatter locally, please make sure to have a working julia installation and add the `JuliaFormatter` package to your either global or local environment. At a global, system level, this would be:

```
julia -e 'using Pkg; Pkg.add("JuliaFormatter")'
```

Now that your environment has the JuliaFormatter package, do the following to apply format style changes to the MOLE.jl directory:

1. From the `mole/julia/MOLE.jl` directory, run

```
julia -e 'using JuliaFormatter; format(".", overwrite=true, verbose=true) || error("Formatting issues detected")'
```

2. Review the generated style changes applied by the formatter with

```
git status
```

and

```
git diff
```

Stage the modified files and commit the necessary changes.
13 changes: 5 additions & 8 deletions julia/MOLE.jl/docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
push!(LOAD_PATH,"../src/")
push!(LOAD_PATH, "../src/")
using Documenter, MOLE, SparseArrays

makedocs(
Expand All @@ -12,15 +12,12 @@ makedocs(
"1D Elliptic Problems" => [
"Overview" => "examples/Elliptic/1D/index.md",
"Elliptic 1D" => "examples/Elliptic/1D/Elliptic1D.md",
"Elliptic 1D Add Scalar Boundary Conditions" =>
"examples/Elliptic/1D/Elliptic1D-add-Scalar-BC.md",
"Elliptic 1D Add Scalar Boundary Conditions" => "examples/Elliptic/1D/Elliptic1D-add-Scalar-BC.md",
],
"2D Elliptic Problems" => [
"Overview" => "examples/Elliptic/2D/index.md",
"X Dirichlet Y Dirichlet" =>
"examples/Elliptic/2D/Elliptic2D-X-Dirichlet-Y-Dirichlet.md",
"X Periodic Y Dirichlet" =>
"examples/Elliptic/2D/Elliptic2D-X-Periodic-Y-Dirichlet.md",
"X Dirichlet Y Dirichlet" => "examples/Elliptic/2D/Elliptic2D-X-Dirichlet-Y-Dirichlet.md",
"X Periodic Y Dirichlet" => "examples/Elliptic/2D/Elliptic2D-X-Periodic-Y-Dirichlet.md",
],
],
"Hyperbolic Problems" => [
Expand All @@ -40,4 +37,4 @@ makedocs(
],
],
],
)
)
18 changes: 9 additions & 9 deletions julia/MOLE.jl/examples/elliptic/elliptic1D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ dx = (east-west)/m # step length
path = joinpath(@__DIR__, "output") # Output path to store generated plots
mkpath(path)

L = Operators.lap(k,m,dx)
L = Operators.lap(k, m, dx)

# Impose Robin boundary condition on laplacian operator
a = 1.0
b = 1.0
L = L + BCs.robinBC(k,m,dx,a,b)
L = L + BCs.robinBC(k, m, dx, a, b)

# 1D staggered grid
grid = [west; (west+(dx/2)):dx:(east-(dx/2)); east]
grid = [west; (west + (dx / 2)):dx:(east - (dx / 2)); east]

# RHS
U = exp.(grid)
Expand All @@ -46,13 +46,13 @@ U[end] = 2*exp(1)
U = L\U

# Plot result
p = Plots.scatter(grid, U, label="Approximated", show = false)
plot!(p, grid, exp.(grid), label="Analytical", show = false)
p = Plots.scatter(grid, U, label = "Approximated", show = false)
plot!(p, grid, exp.(grid), label = "Analytical", show = false)
plot!(
p,
xlabel="x",
ylabel="u(x)",
title="Poisson's equation with Robin BC",
show = false
xlabel = "x",
ylabel = "u(x)",
title = "Poisson's equation with Robin BC",
show = false,
)
Plots.png(p, joinpath(path, "elliptic1D_output.png"))
26 changes: 13 additions & 13 deletions julia/MOLE.jl/examples/elliptic/elliptic1DaddScalarBC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ import MOLE: Operators, BCs
west = 0.0
east = 1.0

k = 6 # operator order of accuracy
m = 2k + 1 # minimum number of cells to attain desired accuracy
k = 6 # operator order of accuracy
m = 2k + 1 # minimum number of cells to attain desired accuracy
dx = (east - west) / m # step length

path = joinpath(@__DIR__, "output") # Output path to store generated plots
mkpath(path)

# 1D staggered grid
grid = [west; (west + dx/2):dx:(east - dx/2); east]
grid = [west; (west + dx / 2):dx:(east - dx / 2); east]

# Mimetic Laplacian operator
L = Operators.lap(k, m, dx)

# Robin boundary conditions: dc*u + nc*(du/dn) = v
dc = (1.0, 1.0)
nc = (1.0, 1.0)
v = (0.0, 2 * exp(1.0))
v = (0.0, 2 * exp(1.0))

bc = BCs.ScalarBC1D(dc, nc, v)

Expand All @@ -54,14 +54,14 @@ L0, rhs0 = BCs.addScalarBC!(sparse(L), rhs, bc, k, m, dx)
u = L0 \ rhs0

# Plot
p = scatter(grid, u; label="Approximated", marker=:circle, show = false)
plot!(p, grid, exp.(grid); label="Analytical", show = false)
p = scatter(grid, u; label = "Approximated", marker = :circle, show = false)
plot!(p, grid, exp.(grid); label = "Analytical", show = false)
plot!(
p,
title="Poisson's equation with Robin BC using addScalarBC",
xlabel="x",
ylabel="u(x)",
legend=:topleft,
show = false
p,
title = "Poisson's equation with Robin BC using addScalarBC",
xlabel = "x",
ylabel = "u(x)",
legend = :topleft,
show = false,
)
Plots.png(p, joinpath(path, "elliptic1DaddScalarBC_output.png"))
Plots.png(p, joinpath(path, "elliptic1DaddScalarBC_output.png"))
46 changes: 23 additions & 23 deletions julia/MOLE.jl/examples/elliptic/elliptic2DXDirichletYDirichlet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ path = joinpath(@__DIR__, "output") # Output path to store generated plots
mkpath(path)

# Grid
xc = [0; (dx / 2.0) : dx : (pi - dx / 2.0); pi]
yc = [0; (dy / 2.0) : dy : (pi - dy / 2.0); pi]
xc = [0; (dx / 2.0):dx:(pi - dx / 2.0); pi]
yc = [0; (dy / 2.0):dy:(pi - dy / 2.0); pi]
X = ones(1, n + 2) .* xc
Y = yc' .* ones(m + 2, 1)

Expand All @@ -45,11 +45,11 @@ ue = exp.(X) .* cos.(Y)
# Boundary Conditions
dc = (1.0, 1.0, 1.0, 1.0)
nc = (0.0, 0.0, 0.0, 0.0)
v = (vec(ue[1,2:end-1]'), vec(ue[end,2:end-1]'), vec(ue[:,1]), vec(ue[:, end]))
v = (vec(ue[1, 2:(end - 1)]'), vec(ue[end, 2:(end - 1)]'), vec(ue[:, 1]), vec(ue[:, end]))
bc = BCs.ScalarBC2D(dc, nc, v)

# Operator
A = - Operators.lap(k, m, dx, n, dy, dc=dc, nc=nc)
A = - Operators.lap(k, m, dx, n, dy, dc = dc, nc = nc)

# RHS
b = zeros(m + 2, n + 2)
Expand All @@ -64,37 +64,37 @@ ua = Matrix(reshape(ua, m + 2, n + 2))

Plots.png(
Plots.heatmap(
xc,
yc,
ua,
title = "Approximate Solution",
xlabel = "X",
ylabel = "Y",
colorbar_title = "u(x,y)",
xc,
yc,
ua,
title = "Approximate Solution",
xlabel = "X",
ylabel = "Y",
colorbar_title = "u(x,y)",
aspect_ratio = :equal,
colormap = :jet1,
show = false
show = false,
),
joinpath(path, "elliptic2DXDYD_approximate.png")
joinpath(path, "elliptic2DXDYD_approximate.png"),
)

Plots.png(
Plots.heatmap(
xc,
yc,
ue,
title = "Exact Solution",
xlabel = "X",
ylabel = "Y",
colorbar_title = "u(x,y)",
xc,
yc,
ue,
title = "Exact Solution",
xlabel = "X",
ylabel = "Y",
colorbar_title = "u(x,y)",
aspect_ratio = :equal,
colormap = :jet1,
show = false
show = false,
),
joinpath(path, "elliptic2DXDYD_exact.png")
joinpath(path, "elliptic2DXDYD_exact.png"),
)

max_err = maximum(abs, ue - ua)
println("Maximum error: $max_err")
rel_err = 100 * max_err ./ (maximum(ue) - minimum(ue))
println("Relative error: $rel_err")
println("Relative error: $rel_err")
44 changes: 22 additions & 22 deletions julia/MOLE.jl/examples/elliptic/elliptic2DXPerYDirichlet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ path = joinpath(@__DIR__, "output") # Output path to store generated plots
mkpath(path)

# Grid
xc = (dx / 2.0) : dx : (1.0 - dx / 2.0)
yc = [0; (dy / 2.0) : dy : (1.0 - dy / 2.0); 1.0]
xc = (dx / 2.0):dx:(1.0 - dx / 2.0)
yc = [0; (dy / 2.0):dy:(1.0 - dy / 2.0); 1.0]
X = ones(1, n + 2) .* xc
Y = yc' .* ones(m, 1)

Expand All @@ -49,7 +49,7 @@ v = ([0.0], [0.0], vec(zeros(m, 1)), vec(zeros(m, 1)))
bc = BCs.ScalarBC2D(dc, nc, v)

# Operator
A = - Operators.lap(k, m, dx, n, dy, dc=dc, nc=nc)
A = - Operators.lap(k, m, dx, n, dy, dc = dc, nc = nc)

# RHS
b = 2.0 * sin.(2.0 * pi .* X) .* (1.0 .+ 2.0 * pi^2 .* Y .* (1.0 .- Y))
Expand All @@ -64,37 +64,37 @@ ua = Matrix(reshape(ua, m, n + 2))

Plots.png(
Plots.heatmap(
xc,
yc,
ua,
title = "Approximate Solution",
xlabel = "X",
ylabel = "Y",
colorbar_title = "u(x,y)",
xc,
yc,
ua,
title = "Approximate Solution",
xlabel = "X",
ylabel = "Y",
colorbar_title = "u(x,y)",
aspect_ratio = :equal,
colormap = :jet1,
show = false
show = false,
),
joinpath(path, "elliptic2DXPYD_approximate.png")
joinpath(path, "elliptic2DXPYD_approximate.png"),
)

Plots.png(
Plots.heatmap(
xc,
yc,
ue,
title = "Exact Solution",
xlabel = "X",
ylabel = "Y",
colorbar_title = "u(x,y)",
xc,
yc,
ue,
title = "Exact Solution",
xlabel = "X",
ylabel = "Y",
colorbar_title = "u(x,y)",
aspect_ratio = :equal,
colormap = :jet1,
show = false
show = false,
),
joinpath(path, "elliptic2DXPYD_exact.png")
joinpath(path, "elliptic2DXPYD_exact.png"),
)

max_err = maximum(abs, ue - ua)
println("Maximum error: $max_err")
rel_err = 100 * max_err ./ (maximum(ue) - minimum(ue))
println("Realative error: $rel_err")
println("Realative error: $rel_err")
Loading
Loading