Skip to content

Commit d7c56f0

Browse files
authored
Merge pull request #172 from Cthonios/bcs/dirichlet-rework
reworking dirichlet bcs to hold onto velocity and acceleration values…
2 parents 2673552 + 78470e2 commit d7c56f0

File tree

10 files changed

+174
-167
lines changed

10 files changed

+174
-167
lines changed

Project.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
name = "FiniteElementContainers"
22
uuid = "d08262e4-672f-4e7f-a976-f2cea5767631"
33
authors = ["Craig M. Hamel <[email protected]> and contributors"]
4-
version = "0.8.1"
4+
version = "0.9.0"
55

66
[deps]
77
Atomix = "a9b6321e-bd34-4604-b9c9-b65b8de01458"
88
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
9+
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
910
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
1011
Krylov = "ba0b0d4f-ebba-5204-a429-3ac8c609bfb7"
1112
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
@@ -57,9 +58,8 @@ Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
5758
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
5859
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
5960
Exodus = "f57ae99e-f805-4780-bdca-96e224be1e5a"
60-
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
6161
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
6262
TestSetExtensions = "98d24dd4-01ad-11ea-1b02-c9a08f80db04"
6363

6464
[targets]
65-
test = ["Adapt", "AMDGPU", "Aqua", "CUDA", "Exodus", "ForwardDiff", "Test", "TestSetExtensions"]
65+
test = ["Adapt", "AMDGPU", "Aqua", "CUDA", "Exodus", "Test", "TestSetExtensions"]

ext/FiniteElementContainersAdaptExt.jl

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,22 @@ end
4949

5050
# Boundary Conditions
5151
function Adapt.adapt_structure(to, bk::FiniteElementContainers.BCBookKeeping{V}) where V
52-
blocks = adapt(to, bk.blocks)
53-
side_nodes = adapt(to, bk.side_nodes)
54-
return FiniteElementContainers.BCBookKeeping{typeof(side_nodes), typeof(blocks)}(
55-
blocks,
52+
return FiniteElementContainers.BCBookKeeping(
53+
adapt(to, bk.blocks),
5654
adapt(to, bk.dofs),
5755
adapt(to, bk.elements),
5856
adapt(to, bk.nodes),
5957
adapt(to, bk.sides),
60-
side_nodes
58+
adapt(to, bk.side_nodes)
6159
)
6260
end
6361

6462
function Adapt.adapt_structure(to, bc::FiniteElementContainers.DirichletBCContainer)
6563
bk = adapt(to, bc.bookkeeping)
6664
vals = adapt(to, bc.vals)
67-
return FiniteElementContainers.DirichletBCContainer{
68-
typeof(bk), eltype(vals), typeof(vals)
69-
}(
70-
bk, vals
71-
)
65+
vals_dot = adapt(to, bc.vals_dot)
66+
vals_dot_dot = adapt(to, bc.vals_dot_dot)
67+
return FiniteElementContainers.DirichletBCContainer(bk, vals, vals_dot, vals_dot_dot)
7268
end
7369

7470
function Adapt.adapt_structure(to, bc::FiniteElementContainers.NeumannBCContainer)
@@ -77,12 +73,7 @@ function Adapt.adapt_structure(to, bc::FiniteElementContainers.NeumannBCContaine
7773
surf_conns = adapt(to, bc.surface_conns)
7874
ref_fe = adapt(to, bc.ref_fe)
7975
vals = adapt(to, bc.vals)
80-
return FiniteElementContainers.NeumannBCContainer{
81-
typeof(bk), typeof(el_conns), typeof(surf_conns),
82-
typeof(ref_fe), eltype(vals), typeof(vals)
83-
}(
84-
bk, el_conns, surf_conns, ref_fe, vals
85-
)
76+
return FiniteElementContainers.NeumannBCContainer(bk, el_conns, surf_conns, ref_fe, vals)
8677
end
8778

8879
# DofManagers
@@ -116,11 +107,7 @@ function Adapt.adapt_structure(to, fspace::FunctionSpace)
116107
coords = adapt(to, fspace.coords)
117108
elem_conns = adapt(to, fspace.elem_conns)
118109
ref_fes = adapt(to, fspace.ref_fes)
119-
return FunctionSpace(
120-
coords,
121-
elem_conns,
122-
ref_fes
123-
)
110+
return FunctionSpace(coords, elem_conns, ref_fes)
124111
end
125112

126113
function Adapt.adapt_structure(to, var::T) where T <: FiniteElementContainers.AbstractFunction

src/FiniteElementContainers.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export L2QuadratureField
3232
export DofManager
3333
export create_field
3434
export create_unknowns
35-
export update_bcs!
3635
export update_dofs!
36+
export update_field_dirichlet_bcs!
3737
export update_field_unknowns!
3838

3939
# Formulations
@@ -133,6 +133,7 @@ export MappedSurfaceInterpolants
133133
import KernelAbstractions as KA
134134
using Atomix
135135
using DocStringExtensions
136+
using ForwardDiff
136137
using Krylov
137138
using LinearAlgebra
138139
using ReferenceFiniteElements

src/Parameters.jl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ function Parameters(
113113
dirichlet_bcs, dirichlet_bc_funcs = create_dirichlet_bcs(
114114
mesh, assembler.dof, dirichlet_bcs
115115
)
116-
# update_dofs!(assembler.dof, )
117116

118117
if neumann_bcs !== NamedTuple()
119118
syms = map(x -> Symbol("neumann_bc_$x"), 1:length(neumann_bcs))
@@ -184,13 +183,6 @@ function create_parameters(
184183
return Parameters(mesh, assembler, physics, props, dirichlet_bcs, neumann_bcs, times)
185184
end
186185

187-
function update_bcs!(p::Parameters)
188-
for bc in values(p.dirichlet_bcs)
189-
_update_bcs!(bc, p.h1_field, KA.get_backend(bc))
190-
end
191-
return nothing
192-
end
193-
194186
"""
195187
$(TYPEDSIGNATURES)
196188
This method is used to update the stored bc values.
@@ -213,13 +205,13 @@ function update_dofs!(asm::SparseMatrixAssembler, p::Parameters)
213205
end
214206

215207
function _update_for_assembly!(p::Parameters, dof::DofManager, Uu)
216-
update_bcs!(p)
208+
update_field_dirichlet_bcs!(p.h1_field, p.dirichlet_bcs)
217209
update_field_unknowns!(p.h1_field, dof, Uu)
218210
return nothing
219211
end
220212

221213
function _update_for_assembly!(p::Parameters, dof::DofManager, Uu, Vu)
222-
update_bcs!(p)
214+
update_field_dirichlet_bcs!(p.h1_field, p.dirichlet_bcs)
223215
update_field_unknowns!(p.h1_field, dof, Uu)
224216
update_field_unknowns!(p.h1_hvp_scratch_field, dof, Vu)
225217
return nothing

src/assemblers/QuadratureQuantity.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ function assemble_quadrature_quantity!(
2020
fspace = function_space(dof)
2121
t = current_time(p.times)
2222
Δt = time_step(p.times)
23-
update_bcs!(p)
24-
update_field_unknowns!(p.h1_field, dof, Uu)
23+
_update_for_assembly!(p, dof, Uu)
2524
for (b, (field, conns, block_physics, state_old, state_new, props)) in enumerate(zip(
2625
values(storage),
2726
values(fspace.elem_conns),

src/assemblers/SparseMatrixAssembler.jl

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,16 @@ $(TYPEDSIGNATURES)
115115
TODO add symbol to interface
116116
"""
117117
function SparseArrays.sparse!(
118-
assembler::SparseMatrixAssembler, ::Val{:stiffness};
119-
unknowns_only=true
118+
assembler::SparseMatrixAssembler, ::Val{:stiffness}
120119
)
121120
pattern = assembler.pattern
122121
storage = assembler.stiffness_storage
123-
if unknowns_only
124-
return @views SparseArrays.sparse!(
125-
pattern.Is, pattern.Js, storage[assembler.pattern.unknown_dofs],
126-
length(pattern.klasttouch), length(pattern.klasttouch), +, pattern.klasttouch,
127-
pattern.csrrowptr, pattern.csrcolval, pattern.csrnzval,
128-
pattern.csccolptr, pattern.cscrowval, pattern.cscnzval
129-
)
130-
# else
131-
# return SparseArrays.sparse!(
132-
# pattern.Is, pattern.Js, storage[assembler.pattern.unknown_dofs],
133-
# length(pattern.klasttouch), length(pattern.klasttouch), +, pattern.klasttouch,
134-
# pattern.csrrowptr, pattern.csrcolval, pattern.csrnzval,
135-
# pattern.csccolptr, pattern.cscrowval, pattern.cscnzval
136-
# )
137-
end
122+
return @views SparseArrays.sparse!(
123+
pattern.Is, pattern.Js, storage[assembler.pattern.unknown_dofs],
124+
length(pattern.klasttouch), length(pattern.klasttouch), +, pattern.klasttouch,
125+
pattern.csrrowptr, pattern.csrcolval, pattern.csrnzval,
126+
pattern.csccolptr, pattern.cscrowval, pattern.cscnzval
127+
)
138128
end
139129

140130
"""
@@ -152,13 +142,8 @@ function SparseArrays.sparse!(assembler::SparseMatrixAssembler, ::Val{:mass})
152142
)
153143
end
154144

155-
function SparseArrays.spdiagm(assembler::SparseMatrixAssembler)
156-
return SparseArrays.spdiagm(assembler.constraint_storage)
157-
end
158-
159145
function constraint_matrix(assembler::SparseMatrixAssembler)
160-
# TODO specialize to CPU/GPU
161-
return SparseArrays.spdiagm(assembler)
146+
return Diagonal(assembler.constraint_storage)
162147
end
163148

164149
function _mass(assembler::SparseMatrixAssembler, ::KA.CPU)

src/bcs/BoundaryConditions.jl

Lines changed: 45 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
struct VariableNameNotFoundError <: Exception
2+
end
3+
4+
_var_not_found_err() = throw(VariableNameNotFoundError())
5+
6+
function _dof_index_from_var_name(dof, var_name)
7+
dof_index = 0
8+
found = false
9+
for name in names(dof.var)
10+
dof_index = dof_index + 1
11+
if var_name == name
12+
found = true
13+
break
14+
end
15+
end
16+
17+
if dof_index == 0 || dof_index > length(names(dof.var))
18+
_var_not_found_err()
19+
end
20+
21+
return dof_index
22+
end
23+
124
function _unique_sort_perm(array::AbstractArray{T, 1}) where T <: Number
225
# chatgpt BS below. Make this not use a dict
326
sorted_unique = sort(unique(array))
@@ -15,8 +38,9 @@ for all types of boundary conditions.
1538
TODO need to add a domain ID for extending to Schwarz
1639
"""
1740
struct BCBookKeeping{
18-
M <: AbstractArray{<:Integer, 2},
19-
V <: AbstractArray{<:Integer, 1}
41+
I <: Integer,
42+
V <: AbstractArray{I, 1},
43+
M <: AbstractArray{I, 2}
2044
}
2145
blocks::V
2246
dofs::V
@@ -33,36 +57,9 @@ $(TYPEDSIGNATURES)
3357
function BCBookKeeping(
3458
mesh, dof::DofManager, var_name::Symbol, sset_name::Symbol
3559
)
36-
# check if var exists
37-
# var_index = 0
38-
dof_index = 0
39-
found = false
40-
# for (vi, var) in enumerate(dof.H1_vars)
41-
# for name in names(var)
42-
# dof_index = dof_index + 1
43-
# if var_name == name
44-
# var_index = vi
45-
# found = true
46-
# break
47-
# end
48-
# end
49-
# end
50-
for name in names(dof.var)
51-
dof_index = dof_index + 1
52-
if var_name == name
53-
# var_index = vi
54-
found = true
55-
break
56-
end
57-
end
58-
59-
# some error checking
60-
# @assert found == true "Failed to find variable $var_name"
61-
# @assert dof_index <= length(mapreduce(x -> names(x), +, dof.H1_vars)) "Found invalid dof index"
62-
@assert dof_index <= length(names(dof.var)) "Found invalid dof index"
60+
# get dof index associated with this var
61+
dof_index = _dof_index_from_var_name(dof, var_name)
6362

64-
# TODO
65-
# fspace = dof.H1_vars[var_index].fspace
6663
fspace = function_space(dof)
6764

6865
# get sset specific fields
@@ -71,7 +68,6 @@ function BCBookKeeping(
7168
sides = getproperty(mesh.sideset_sides, sset_name)
7269
side_nodes = getproperty(mesh.sideset_side_nodes, sset_name)
7370

74-
dofs = Vector{Int64}(undef, 0)
7571
blocks = Vector{Int64}(undef, 0)
7672

7773
# gather the blocks that are present in this sideset
@@ -86,48 +82,11 @@ function BCBookKeeping(
8682
end
8783
end
8884

89-
# setting up dofs for use in dirichlet bcs
90-
# TODO only works for H1
91-
# ND, NN = num_dofs_per_node(dof), num_nodes(dof)
92-
ND, NN = size(dof)
93-
n_total_dofs = ND * NN
94-
all_dofs = reshape(1:n_total_dofs, ND, NN)
95-
# temp_dofs = all_dofs[dof_index, temp_nodes]
96-
temp_dofs = all_dofs[dof_index, nodes]
97-
append!(dofs, temp_dofs)
98-
# end
99-
100-
# now sort and unique this stuff
101-
# dof_perm = _unique_sort_perm(dofs)
102-
# # el_perm = _unique_sort_perm(elements)
103-
104-
# # do permutations
105-
# # TODO need to do this in different BC types
106-
# # since different BC types might want things
107-
# # organized differently
108-
# # blocks_new = blocks[el_perm]
109-
# dofs_new = dofs[dof_perm]
110-
# # elements_new = elements[el_perm]
111-
# nodes_new = nodes[dof_perm]
112-
# # sides_new = sides[el_perm]
113-
# # side_nodes_new = side_nodes[:, el_perm]
114-
115-
# # resize!(blocks, length(blocks_new))
116-
# resize!(dofs, length(dofs_new))
117-
# # resize!(elements, length(elements_new))
118-
# resize!(nodes, length(nodes_new))
119-
# # resize!(sides, length(sides_new))
120-
# # resize!(side_nodes, size(side_nodes_new)...)
121-
# # reshape(side_nodes, size(side_nodes_new))
122-
123-
# # copyto!(blocks, blocks_new)
124-
# copyto!(dofs, dofs_new)
125-
# # copyto!(elements, elements_new)
126-
# copyto!(nodes, nodes_new)
127-
# # copyto!(sides, sides_new)
128-
# # copyto!(side_nodes, side_nodes_new)
129-
130-
return BCBookKeeping{typeof(side_nodes), typeof(blocks)}(
85+
# setup dofs local to this BC
86+
all_dofs = reshape(1:length(dof), size(dof))
87+
dofs = all_dofs[dof_index, nodes]
88+
89+
return BCBookKeeping(
13190
blocks, dofs, elements, nodes, sides, side_nodes
13291
)
13392
end
@@ -158,10 +117,12 @@ $(TYPEDSIGNATURES)
158117
$(TYPEDFIELDS)
159118
"""
160119
abstract type AbstractBCContainer{
161-
B <: BCBookKeeping,
162-
T <: Union{<:Number, <:SVector},
120+
IT <: Integer,
121+
VT <: Union{<:Number, <:SVector},
163122
N,
164-
V <: AbstractArray{T, N}
123+
IV <: AbstractArray{IT, 1},
124+
IM <: AbstractArray{IT, 2},
125+
VV <: AbstractArray{VT, N}
165126
} end
166127

167128
KA.get_backend(x::AbstractBCContainer) = KA.get_backend(x.vals)
@@ -171,6 +132,13 @@ function Base.show(io::IO, bc::AbstractBCContainer)
171132
println(io, "$(bc.bookkeeping)")
172133
end
173134

135+
"""
136+
$(TYPEDEF)
137+
$(TYPEDSIGNATURES)
138+
$(TYPEDFIELDS)
139+
"""
140+
abstract type AbstractBCFunction{F} end
141+
174142
"""
175143
$(TYPEDSIGNATURES)
176144
Wrapper that is generic for all architectures to
@@ -181,8 +149,6 @@ function update_bc_values!(bcs, funcs, X, t)
181149
backend = KA.get_backend(bc)
182150
_update_bc_values!(bc, func, X, t, backend)
183151
end
184-
backend = KA.get_backend(X)
185-
KA.synchronize(backend)
186152
return nothing
187153
end
188154

0 commit comments

Comments
 (0)