/
TransientFESolutions.jl
169 lines (140 loc) · 4.23 KB
/
TransientFESolutions.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#######################
# TransientFESolution #
#######################
"""
abstract type TransientFESolution <: GridapType end
Wrapper around a `TransientFEOperator` and `ODESolver` that represents the
solution at a set of time steps. It is an iterator that computes the solution
at each time step in a lazy fashion when accessing the solution.
# Mandatory
- [`iterate(tfesltn)`](@ref)
- [`iterate(tfesltn, state)`](@ref)
"""
abstract type TransientFESolution <: GridapType end
"""
Base.iterate(tfesltn::TransientFESolution) -> ((Real, FEFunction), StateType)
Allocate a cache and perform one step of the `ODEOperator` with the `ODESolver`
attached to the `TransientFESolution`.
"""
function Base.iterate(tfesltn::TransientFESolution)
@abstractmethod
end
"""
Base.iterate(tfesltn::TransientFESolution) -> ((Real, FEFunction), StateType)
Perform one step of the `ODEOperator` with the `ODESolver` attached to the
`TransientFESolution`.
"""
function Base.iterate(tfesltn::TransientFESolution, state)
@abstractmethod
end
Base.IteratorSize(::Type{<:TransientFESolution}) = Base.SizeUnknown()
##############################
# GenericTransientFESolution #
##############################
"""
struct GenericTransientFESolution <: TransientFESolution end
Generic wrapper for the evolution of an `TransientFEOperator` with an
`ODESolver`.
"""
struct GenericTransientFESolution <: TransientFESolution
odesltn::ODESolution
trial
end
# Constructors
function GenericTransientFESolution(
odeslvr::ODESolver, tfeop::TransientFEOperator,
t0::Real, tF::Real, uhs0::Tuple{Vararg{CellField}}
)
odeop = get_algebraic_operator(tfeop)
us0 = get_free_dof_values.(uhs0)
odesltn = solve(odeslvr, odeop, t0, tF, us0)
trial = get_trial(tfeop)
GenericTransientFESolution(odesltn, trial)
end
function GenericTransientFESolution(
odeslvr::ODESolver, tfeop::TransientFEOperator,
t0::Real, tF::Real, uh0::CellField,
)
uhs0 = (uh0,)
GenericTransientFESolution(odeslvr, tfeop, t0, tF, uhs0)
end
function Base.iterate(tfesltn::GenericTransientFESolution)
ode_it = iterate(tfesltn.odesltn)
if isnothing(ode_it)
return nothing
end
ode_it_data, ode_it_state = ode_it
tF, uF = ode_it_data
Uh = allocate_space(tfesltn.trial)
Uh = evaluate!(Uh, tfesltn.trial, tF)
uhF = FEFunction(Uh, uF)
tfe_it_data = (tF, uhF)
tfe_it_state = (Uh, ode_it_state)
(tfe_it_data, tfe_it_state)
end
function Base.iterate(tfesltn::GenericTransientFESolution, state)
Uh, ode_it_state = state
ode_it = iterate(tfesltn.odesltn, ode_it_state)
if isnothing(ode_it)
return nothing
end
ode_it_data, ode_it_state = ode_it
tF, uF = ode_it_data
Uh = evaluate!(Uh, tfesltn.trial, tF)
uhF = FEFunction(Uh, uF)
tfe_it_data = (tF, uhF)
tfe_it_state = (Uh, ode_it_state)
(tfe_it_data, tfe_it_state)
end
##############################
# Default behaviour of solve #
##############################
"""
solve(
odeslvr::ODESolver, tfeop::TransientFEOperator,
t0::Real, tF::Real, uhs0
) -> TransientFESolution
Create a `TransientFESolution` wrapper around the `TransientFEOperator` and
`ODESolver`, starting at time `t0` with state `us0`, to be evolved until `tF`.
"""
function Algebra.solve(
odeslvr::ODESolver, tfeop::TransientFEOperator,
t0::Real, tF::Real, uhs0::Tuple{Vararg{CellField}}
)
GenericTransientFESolution(odeslvr, tfeop, t0, tF, uhs0)
end
function Algebra.solve(
odeslvr::ODESolver, tfeop::TransientFEOperator,
t0::Real, tF::Real, uh0::CellField
)
uhs0 = (uh0,)
solve(odeslvr, tfeop, t0, tF, uhs0)
end
########
# Test #
########
"""
test_tfe_solution(tfesltn::TransientFESolution) -> Bool
Test the interface of `TransientFESolution` specializations.
"""
function test_tfe_solution(tfesltn::TransientFESolution)
for (t_n, uh_n) in tfesltn
@test t_n isa Real
@test uh_n isa FEFunction
end
true
end
"""
test_tfe_solver(
odeslvr::ODESolver, tfeop::TransientFEOperator,
t0::Real, tF::Real, uhs0
) -> Bool
Test the interface of `ODESolver` specializations on `TransientFEOperator`s.
"""
function test_tfe_solver(
odeslvr::ODESolver, tfeop::TransientFEOperator,
t0::Real, tF::Real, uhs0::Tuple{Vararg{AbstractVector}}
)
tfesltn = solve(odeslvr, tfeop, t0, tF, uhs0)
test_tfe_solution(tfesltn)
end