Skip to content

Commit fba56d5

Browse files
committed
modifying physics to be block by block.
1 parent 0ec5aa2 commit fba56d5

File tree

10 files changed

+53
-91
lines changed

10 files changed

+53
-91
lines changed

examples/mechanics/Cube.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ end
5252
function mechanics_test()
5353
mesh = UnstructuredMesh("cube.g")
5454
V = FunctionSpace(mesh, H1Field, Lagrange)
55-
physics = Mechanics(ThreeDimensional())
55+
physics = (;
56+
cube=Mechanics(ThreeDimensional())
57+
)
5658

5759
u = VectorFunction(V, :displ)
5860
asm = SparseMatrixAssembler(H1Field, u)

src/FiniteElementContainers.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ include("physics/Physics.jl")
143143
include("PostProcessors.jl")
144144

145145
#
146+
include("TimeSteppers.jl")
146147
include("Parameters.jl")
147148
include("solvers/Solvers.jl")
148149
include("integrators/Integrators.jl")

src/Parameters.jl

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,3 @@
1-
# move to a seperate file
2-
abstract type AbstractTimeStepper{T} end
3-
current_time(t::AbstractTimeStepper) = sum(t.time_current)
4-
time_step(t::AbstractTimeStepper) = sum(t.Δt)
5-
6-
struct TimeStepper{T} <: AbstractTimeStepper{T}
7-
time_start::T
8-
time_end::T
9-
time_current::T
10-
Δt::T
11-
end
12-
13-
function TimeStepper(time_start_in::T, time_end_in::T, n_steps::Int) where T <: Number
14-
time_start = zeros(1)
15-
time_end = zeros(1)
16-
time_current = zeros(1)
17-
Δt = zeros(1)
18-
Δt = zeros(1)
19-
fill!(time_start, time_start_in)
20-
fill!(time_end, time_end_in)
21-
fill!(time_current, time_start_in)
22-
fill!(Δt, (time_end_in - time_start_in) / n_steps)
23-
return TimeStepper(time_start, time_end, time_current, Δt)
24-
# return TimeStepper(time_start_in, time_end_in, )
25-
end
26-
271
abstract type AbstractParameters end
282

293
struct Parameters{D, N, T, Phys, Props, S, V, H1} <: AbstractParameters
@@ -42,26 +16,13 @@ struct Parameters{D, N, T, Phys, Props, S, V, H1} <: AbstractParameters
4216
h1_field::H1
4317
end
4418

45-
# TODO only works for H1Fields currently most likely
46-
# function Parameters(dof::DofManager, physics::AbstractPhysics)
47-
# n_props = num_properties(physics)
48-
# n_state = num_states(physics)
49-
50-
# # TODO
51-
# fspace = dof.H1_vars[1].fspace
52-
53-
# for (name, ref_fe) in pairs(fspace.ref_fes)
54-
55-
# end
56-
# end
57-
5819
# TODO
5920
# 1. need to loop over bcs and vars in dof
6021
# to make organize different bcs based on fspace type
6122
# 2. group all dbcs, nbcs of similar fspaces into a single struct?
62-
# 3. figure out how to handle function pointers on the GPU
23+
# 3. figure out how to handle function pointers on the GPU - done
6324
# 4. add different fspace types
64-
# 5. convert vectors of dbcs/nbcs into namedtuples
25+
# 5. convert vectors of dbcs/nbcs into namedtuples - done
6526
function Parameters(
6627
assembler, physics,
6728
dirichlet_bcs,
@@ -76,6 +37,16 @@ function Parameters(
7637
state_old = nothing
7738
state_new = nothing
7839

40+
# for mixed spaces we'll need to do this more carefully
41+
if isa(physics, AbstractPhysics)
42+
syms = keys(values(assembler.dof.H1_vars)[1].fspace.elem_conns)
43+
physics = map(x -> physics, syms)
44+
physics = NamedTuple{tuple(syms...)}(tuple(physics...))
45+
else
46+
@assert isa(physics, NamedTuple)
47+
# TODO re-arrange physics tuple to match fspaces when appropriate
48+
end
49+
7950
if dirichlet_bcs !== nothing
8051
syms = map(x -> Symbol("dirichlet_bc_$x"), 1:length(dirichlet_bcs))
8152
# dbcs = NamedTuple{tuple(syms...)}(tuple(dbcs...))

src/TimeSteppers.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
abstract type AbstractTimeStepper{T} end
2+
current_time(t::AbstractTimeStepper) = sum(t.time_current)
3+
time_step(t::AbstractTimeStepper) = sum(t.Δt)
4+
5+
struct TimeStepper{T} <: AbstractTimeStepper{T}
6+
time_start::T
7+
time_end::T
8+
time_current::T
9+
Δt::T
10+
end
11+
12+
function TimeStepper(time_start_in::T, time_end_in::T, n_steps::Int) where T <: Number
13+
time_start = zeros(1)
14+
time_end = zeros(1)
15+
time_current = zeros(1)
16+
Δt = zeros(1)
17+
Δt = zeros(1)
18+
fill!(time_start, time_start_in)
19+
fill!(time_end, time_end_in)
20+
fill!(time_current, time_start_in)
21+
fill!(Δt, (time_end_in - time_start_in) / n_steps)
22+
return TimeStepper(time_start, time_end, time_current, Δt)
23+
# return TimeStepper(time_start_in, time_end_in, )
24+
end

src/assemblers/Assemblers.jl

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,6 @@ function _assemble_element!(global_val::H1Field, local_val, conn, e, b)
3838
return nothing
3939
end
4040

41-
# """
42-
# $(TYPEDSIGNATURES)
43-
# Assembly method for a block labelled as block_id.
44-
45-
# This is a top level method that simply dispatches to CPU/GPU specializations
46-
# based on the backend of assembler and also checks for consistent backends.
47-
48-
# This method is common acroos all value types, e.g. energy, residual, stiffness, etc.
49-
# to maintain a common method call syntax. The difference in dispatch is handled by the
50-
# sym argument.
51-
# """
52-
# function _assemble_block!(assembler, physics, sym, ref_fe, U, X, conns, block_id)
53-
# backend = KA.get_backend(assembler)
54-
# # TODO add get_backend method of ref_fe
55-
# @assert backend == KA.get_backend(U)
56-
# @assert backend == KA.get_backend(X)
57-
# @assert backend == KA.get_backend(conns)
58-
# _assemble_block!(assembler, physics, sym, ref_fe, U, X, conns, block_id, backend)
59-
# end
60-
6141
_assemble_block_method_from_sym(::Val{:mass}) = _assemble_block_mass!
6242
_assemble_block_method_from_sym(::Val{:residual}) = _assemble_block_residual!
6343
_assemble_block_method_from_sym(::Val{:residual_and_stiffness}) = _assemble_block_residual_and_stiffness!
@@ -76,29 +56,23 @@ end
7656
$(TYPEDSIGNATURES)
7757
Top level assembly method for ```H1Field``` that loops over blocks and dispatches
7858
to appropriate kernels based on sym.
59+
60+
TODO need to make sure at setup time that physics and elem_conns have the same
61+
values order. Otherwise, shenanigans.
7962
"""
8063
function assemble!(assembler, physics, U::H1Field, sym)
8164
val_sym = Val{sym}()
8265
_assemble_block_method! = _assemble_block_method_from_sym(val_sym)
8366
_zero_storage(assembler, val_sym)
8467
fspace = assembler.dof.H1_vars[1].fspace
8568
X = fspace.coords
86-
for (b, conns) in enumerate(values(fspace.elem_conns))
69+
for (b, (conns, block_physics)) in enumerate(zip(values(fspace.elem_conns), values(physics)))
8770
ref_fe = values(fspace.ref_fes)[b]
8871
backend = _check_backends(assembler, U, X, conns)
89-
_assemble_block_method!(assembler, physics, ref_fe, U, X, conns, b, backend)
72+
_assemble_block_method!(assembler, block_physics, ref_fe, U, X, conns, b, backend)
9073
end
9174
end
9275

93-
# wrapper that uses Uu and p
94-
# TODO only works on H1 fields right now
95-
function assemble!(assembler, physics, Uu, p, sym)
96-
update_field_bcs!(p.h1_field, assembler.dof, p.h1_bcs, p.t)
97-
update_field_unknowns!(p.h1_field, assembler.dof, Uu)
98-
assemble!(assembler, physics, p.h1_field, sym)
99-
return nothing
100-
end
101-
10276
"""
10377
$(TYPEDSIGNATURES)
10478
"""
@@ -117,16 +91,6 @@ $(TYPEDSIGNATURES)
11791
"""
11892
create_unknowns(asm::AbstractAssembler, type::Type{<:AbstractField}) = create_unknowns(asm.dof, type)
11993

120-
"""
121-
$(TYPEDSIGNATURES)
122-
"""
123-
function _element_level_coordinates(X::H1Field, ref_fe, conns, e)
124-
NDim = size(X, 1)
125-
NNPE = ReferenceFiniteElements.num_vertices(ref_fe)
126-
x_el = @views SMatrix{NDim, NNPE, Float64, NDim * NNPE}(X[:, conns[:, e]])
127-
return x_el
128-
end
129-
13094
"""
13195
$(TYPEDSIGNATURES)
13296
"""

src/assemblers/CPUGeneral.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function _assemble_block_residual!(assembler, physics, ref_fe, U, X, conns, bloc
3838
NNPE = ReferenceFiniteElements.num_vertices(ref_fe)
3939
NxNDof = NNPE * ND
4040
for e in axes(conns, 2)
41-
x_el = _element_level_coordinates(X, ref_fe, conns, e)
41+
x_el = _element_level_fields(X, ref_fe, conns, e)
4242
u_el = _element_level_fields(U, ref_fe, conns, e)
4343
R_el = zeros(SVector{NxNDof, eltype(assembler.residual_storage)})
4444

src/assemblers/GPUGeneral.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ KA.@kernel function _assemble_block_residual_kernel!(assembler, physics, ref_fe,
1111
NNPE = ReferenceFiniteElements.num_vertices(ref_fe)
1212
NxNDof = NNPE * ND
1313

14-
x_el = _element_level_coordinates(X, ref_fe, conns, E)
14+
x_el = _element_level_fields(X, ref_fe, conns, E)
1515
u_el = _element_level_fields(U, ref_fe, conns, E)
1616
R_el = zeros(SVector{NxNDof, Float64})
1717

@@ -52,7 +52,7 @@ KA.@kernel function _assemble_block_stiffness_kernel!(assembler, physics, ref_fe
5252
NNPE = ReferenceFiniteElements.num_vertices(ref_fe)
5353
NxNDof = NNPE * ND
5454

55-
x_el = _element_level_coordinates(X, ref_fe, conns, E)
55+
x_el = _element_level_fields(X, ref_fe, conns, E)
5656
u_el = _element_level_fields(U, ref_fe, conns, E)
5757
K_el = zeros(SMatrix{NxNDof, NxNDof, Float64, NxNDof * NxNDof})
5858

src/assemblers/SparseMatrixAssembler.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function _assemble_block_mass!(assembler, physics, ref_fe, U, X, conns, block_id
103103
NNPE = ReferenceFiniteElements.num_vertices(ref_fe)
104104
NxNDof = NNPE * ND
105105
for e in axes(conns, 2)
106-
x_el = _element_level_coordinates(X, ref_fe, conns, e)
106+
x_el = _element_level_fields(X, ref_fe, conns, e)
107107
u_el = _element_level_fields(U, ref_fe, conns, e)
108108
M_el = zeros(SMatrix{NxNDof, NxNDof, eltype(assembler.mass_storage), NxNDof * NxNDof})
109109

@@ -130,7 +130,7 @@ function _assemble_block_stiffness!(assembler, physics, ref_fe, U, X, conns, blo
130130
NNPE = ReferenceFiniteElements.num_vertices(ref_fe)
131131
NxNDof = NNPE * ND
132132
for e in axes(conns, 2)
133-
x_el = _element_level_coordinates(X, ref_fe, conns, e)
133+
x_el = _element_level_fields(X, ref_fe, conns, e)
134134
u_el = _element_level_fields(U, ref_fe, conns, e)
135135
K_el = zeros(SMatrix{NxNDof, NxNDof, eltype(assembler.stiffness_storage), NxNDof * NxNDof})
136136

@@ -158,7 +158,7 @@ function _assemble_block_residual_and_stiffness!(assembler, physics, ref_fe, U,
158158
NNPE = ReferenceFiniteElements.num_vertices(ref_fe)
159159
NxNDof = NNPE * ND
160160
for e in axes(conns, 2)
161-
x_el = _element_level_coordinates(X, ref_fe, conns, e)
161+
x_el = _element_level_fields(X, ref_fe, conns, e)
162162
u_el = _element_level_fields(U, ref_fe, conns, e)
163163
R_el = zeros(SVector{NxNDof, eltype(assembler.residual_storage)})
164164
K_el = zeros(SMatrix{NxNDof, NxNDof, eltype(assembler.stiffness_storage), NxNDof * NxNDof})

test/mechanics/TestMechanicsCUDA.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ end
7373
p = create_parameters(asm, physics; dirichlet_bcs=dbcs, times=times)
7474

7575
U = create_field(asm, H1Field)
76-
@time assemble!(asm, physics, U, :stiffness)
76+
@time assemble!(asm, p.physics, U, :stiffness)
7777
K = stiffness(asm)
7878

7979
# move to device

test/poisson/TestPoissonCUDA.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ end
5555
# need to assemble once before moving to GPU
5656
# TODO try to wrap this in the |> gpu call
5757
U = create_field(asm, H1Field)
58-
@time assemble!(asm, physics, U, :stiffness)
58+
@time assemble!(asm, p.physics, U, :stiffness)
5959
K = stiffness(asm)
6060

6161
# device movement

0 commit comments

Comments
 (0)