-
Notifications
You must be signed in to change notification settings - Fork 82
Interpolators for Julia Operators #332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
22b47b1
f674063
98c4756
e2a8ea2
2939160
d4985cf
078fa15
a36dbc1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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)) | ||
| 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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the 2 in the RHS needed?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
|
|
||
| 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 | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jlbaranda , if this
printlnstatement was placed here for debugging purposes, please remove it. It is not present in the MATLAB counterpart. Thank you