Skip to content

Commit 2209856

Browse files
committed
Ensure that first derivative operators within the FiniteElementCoordinate struct can only see either a NaturalBC or a PeriodicBC. Create a function interface that returns a NaturalBC if a DirichletBC is stored in the FiniteElementCoordinate struct.
1 parent d8228ca commit 2209856

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

‎src/FiniteElementAssembly.jl‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ struct NaturalBC <: AbstractBoundaryCondition end
2828
struct DirichletBC <: AbstractBoundaryCondition end
2929
struct PeriodicBC <: AbstractBoundaryCondition end
3030

31+
# logic to ensure that only NaturalBC or PeriodicBC are used in first derivatives
32+
# - periodic boundary conditions are passed on
33+
function first_derivative_boundary_condition(bc::PeriodicBC)
34+
return PeriodicBC()
35+
end
36+
# - Dirichlet boundary conditions are converted to natural boundary conditions
37+
function first_derivative_boundary_condition(bc::Union{NaturalBC,DirichletBC})
38+
return NaturalBC()
39+
end
40+
3141
"""
3242
struct containing information for first derivatives
3343
"""
@@ -69,12 +79,13 @@ struct FirstDerivativeData{TFloat <: Real, TMatrix <: AbstractSparseArray{TFloat
6979
# dummy arrays
7080
dummy_rhs = Array{Float64,1,}(undef,n)
7181
dummy_df = Array{Float64,1,}(undef,n)
72-
82+
# boundary condition for first derivative matrices, only NaturalBC or PeriodicBC allowed
83+
first_derivative_bc = first_derivative_boundary_condition(boundary_condition)
7384
# assemble the global matrices with a sparse construction
7485
MM1D = assemble_1D_operator(MM, ngrid, nelement,
75-
n, igrid_full, boundary_condition)
86+
n, igrid_full, first_derivative_bc)
7687
PP1D = assemble_1D_operator(PP, ngrid, nelement,
77-
n, igrid_full, boundary_condition)
88+
n, igrid_full, first_derivative_bc)
7889
MM1D_sparse = sparse(MM1D)
7990
PP1D_sparse = sparse(PP1D)
8091
lu_MM1D = lu(MM1D_sparse)

‎test/CalculusTests.jl‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ using FiniteElementAssembly: FiniteElementCoordinate,
99
first_derivative!,
1010
ScalarCoordinateInputs,
1111
include_boundary_points, exclude_boundary_points,
12-
exclude_lower_boundary_point, exclude_upper_boundary_point
12+
exclude_lower_boundary_point, exclude_upper_boundary_point,
13+
NaturalBC, DirichletBC
1314
using FiniteElementAssembly: integral
1415

1516
"""
@@ -24,16 +25,18 @@ function runtests()
2425
rng = StableRNG(42)
2526

2627
@testset "GaussLegendre derivatives and fundamental theorem of calculus, testing exact polynomials" begin
27-
@testset "$nelement $ngrid $boundary_points" for nelement ∈ (1:5), ngrid ∈ (3:17),
28+
@testset "$nelement $ngrid $boundary_points $bc" for nelement ∈ (1:5), ngrid ∈ (3:17),
2829
boundary_points in (include_boundary_points, exclude_boundary_points,
29-
exclude_lower_boundary_point, exclude_upper_boundary_point)
30+
exclude_lower_boundary_point, exclude_upper_boundary_point),
31+
bc in (NaturalBC(), DirichletBC())
3032

3133
# define inputs needed for the test
3234
coord_min = -1.0
3335
coord_max = 1.0
3436
# create the coordinate struct 'x'
3537
x = FiniteElementCoordinate("coord", ScalarCoordinateInputs(ngrid,
36-
nelement, coord_min, coord_max, boundary_points))
38+
nelement, coord_min, coord_max, boundary_points),
39+
bc=bc)
3740
# test polynomials up to order ngrid-1
3841
for n ∈ 0:ngrid-1
3942
# create array for the function f(x) to be differentiated/integrated

0 commit comments

Comments
 (0)