Skip to content
Closed
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
71 changes: 71 additions & 0 deletions julia/MOLE.jl/examples/hyperbolic1D.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#=
Julia implementation of the 1D Advection equation with periodic boundary conditions
from MATLAB MOLE example
=#

using Printf
using Plots
import MOLE: Operators, BCs

# Domain limits
west = 0;
east = 1;

a = 1; #velocity


k = 2; #Operator's order of accuracy
m = 50; # number of cells
dx = (east - west) / m;

t = 1; #Simulation time
dt = dx/abs(a); #CFL condition for explicit schemes

D = Operators.div(k,m,dx); #1D Mimetic divergence operator
I = Operators.interpol(m, 0.5); #1D 2nd order interpolator

# 1D Staggered grid
grid = [west, west+dx/2:dx:east-dx/2, east];

# Initial Condition
q = 2 * π .* grid
println(typeof(q))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jlbaranda , if this println statement was placed here for debugging purposes, please remove it. It is not present in the MATLAB counterpart. Thank you

U = sin.(q)';

#Periodic Boundary Condition imposed on the divergence operator
D[1, 2] = 1/(2*dx);
D[1, end-1] = -1/(2*dx);
D[end, 1] = -1/(2*dx);
D[end, end-1] = -1/(2*dx);

#Premultiply out of the time loop (since it does not change)
D = -a*dt*2*D*I;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the 2 in the RHS needed?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think I have gathered from the comment below that this is using the leapfrog scheme. We should document this earlier, near the top of the file, not just in a comment near the plotting routine. Thank you!


#=
We could have also defined
D = -a*dt*2*I*D
if the grid was defined as:
grid = west : dx :east (nodal)
=#

U2 = U + D/2*U #Compute one step using Euler's method


#Time integration loop
for i in 1: t/dt
#Plot approximation
plot(grid, U2, label="Approximated")
#Plot exact solution
plot!(grid, sin.(2*π .*(grid - a*i*dt)), label="exact")
#Plot attributes
title!("1D Advection Equation with Periodic BC");
xlabel!("x");
ylabel!("u(x,t)");
axis!([west east -1.5 1.5]);
sleep(0.04);
U3 = U + D*U2; #Compute next step using leapfrog scheme
U = U2;
U2 = U3;
end


4 changes: 2 additions & 2 deletions julia/MOLE.jl/src/Operators/Operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ module Operators
include("gradient.jl")
include("divergence.jl")
include("laplacian.jl")

end # module Operators
include("interpol.jl")
end # module Operators
40 changes: 40 additions & 0 deletions julia/MOLE.jl/src/Operators/interpol.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using SparseArrays
#=
SPDX-License-Identifier: GPL-3.0-or-later
© 2008-2024 San Diego State University Research Foundation (SDSURF).
See LICENSE file or https://www.gnu.org/licenses/gpl-3.0.html for details.
=#

function interpol(m::Int,c::Float64)
"""
implementation of MATLAB interpol.m operator for Julia
INPUTS:
'm::Int' : number of cells
'c::Float' : left interpolation coefficient
OUTPUTS:
I : a (m+1)×(m+2) one-dimensional interpolator of 2nd-order
"""

#Assertions:
@assert m>=4 ["m >= 4"];
@assert c >= 0 && c <= 1 ["0 <= c <= 1"];

#Dimensions of I:
n_rows = m+1;
n_cols = m+2;

I = spzeros(n_rows, n_cols);

I[1,1] = 1;
I[end,end]=1;

#Average between two continuous cells
avg = [c 1-c];

j = 2;
for i = 2: n_rows - 1
I[i, j:j+2-1] = avg;
j = j + 1;
end
end