Skip to content

Commit

Permalink
Handle Dirichlet bc in gausslegendre diffusion matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
johnomotani committed Jun 12, 2024
1 parent c99e9f4 commit 3cff3eb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
3 changes: 2 additions & 1 deletion moment_kinetics/src/coordinates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ function define_coordinate(input, parallel_io::Bool=false; run_directory=nothing
elseif input.discretization == "gausslegendre_pseudospectral"
# create arrays needed for explicit GaussLegendre pseudospectral treatment in this
# coordinate and create the matrices for differentiation
spectral = setup_gausslegendre_pseudospectral(coord, collision_operator_dim=collision_operator_dim)
spectral = setup_gausslegendre_pseudospectral(coord, collision_operator_dim=collision_operator_dim,
dirichlet_bc=occursin("zero", coord.bc))
# obtain the local derivatives of the uniform grid with respect to the used grid
derivative!(coord.duniform_dgrid, coord.uniform_grid, coord, spectral)
else
Expand Down
23 changes: 18 additions & 5 deletions moment_kinetics/src/gauss_legendre.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ struct gausslegendre_info{TSparse, TLU} <: weak_discretization_info
Qmat::Array{mk_float,2}
end

function setup_gausslegendre_pseudospectral(coord; collision_operator_dim=true)
function setup_gausslegendre_pseudospectral(coord; collision_operator_dim=true, dirichlet_bc=true)
lobatto = setup_gausslegendre_pseudospectral_lobatto(coord,collision_operator_dim=collision_operator_dim)
radau = setup_gausslegendre_pseudospectral_radau(coord,collision_operator_dim=collision_operator_dim)

Expand All @@ -114,9 +114,9 @@ function setup_gausslegendre_pseudospectral(coord; collision_operator_dim=true)
K_matrix = allocate_float(coord.n,coord.n)
L_matrix = allocate_float(coord.n,coord.n)

setup_global_weak_form_matrix!(mass_matrix, lobatto, radau, coord, "M")
setup_global_weak_form_matrix!(K_matrix, lobatto, radau, coord, "K_with_BC_terms")
setup_global_weak_form_matrix!(L_matrix, lobatto, radau, coord, "L_with_BC_terms")
setup_global_weak_form_matrix!(mass_matrix, lobatto, radau, coord, "M"; dirichlet_bc=dirichlet_bc)
setup_global_weak_form_matrix!(K_matrix, lobatto, radau, coord, "K_with_BC_terms"; dirichlet_bc=dirichlet_bc)
setup_global_weak_form_matrix!(L_matrix, lobatto, radau, coord, "L_with_BC_terms"; dirichlet_bc=dirichlet_bc)
mass_matrix_lu = lu(sparse(mass_matrix))
Qmat = allocate_float(coord.ngrid,coord.ngrid)

Expand Down Expand Up @@ -835,7 +835,7 @@ where M is the mass matrix and K is the stiffness matrix.
function setup_global_weak_form_matrix!(QQ_global::Array{mk_float,2},
lobatto::gausslegendre_base_info,
radau::gausslegendre_base_info,
coord,option)
coord,option; dirichlet_bc=false)
QQ_j = allocate_float(coord.ngrid,coord.ngrid)
QQ_jp1 = allocate_float(coord.ngrid,coord.ngrid)

Expand Down Expand Up @@ -883,6 +883,19 @@ function setup_global_weak_form_matrix!(QQ_global::Array{mk_float,2},
QQ_global[imax[j],imin[j]-1:imax[j]] .+= QQ_j[ngrid,:]./2.0
end
end

if dirichlet_bc
# Make matrix diagonal for first/last grid points so it does not change the values
# there
if coord.irank == 0
QQ_global[1,:] .= 0.0
QQ_global[1,1] = 1.0
end
if coord.irank == coord.nrank - 1
QQ_global[end,:] .= 0.0
QQ_global[end,end] = 1.0
end
end

return nothing
end
Expand Down

1 comment on commit 3cff3eb

@mrhardman
Copy link
Collaborator

Choose a reason for hiding this comment

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

How is this feature being used currently? I believe that this feature is breaking my tests for the laplacian_derivative!(). If these arrays are being inverted to solve for f in d^2 f / d x^2 = S , then this makes sense, but otherwise, I do not believe it does. Can we make the default value to dirichlet_bc = false and explain when you would want to use it to be true? (i.e., not when simply differentiating a function, which is how these matrices are used).

Please sign in to comment.