-
Notifications
You must be signed in to change notification settings - Fork 98
/
FESolvers.jl
176 lines (145 loc) · 3.83 KB
/
FESolvers.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
170
171
172
173
174
175
176
"""
"""
abstract type FESolver <: GridapType end
"""
uh, cache = solve!(uh,solver,op)
This function changes the state of the input and can render it in a corrupted state.
It is recommended to rewrite the input `uh` with the output as illustrated to prevent any
issue.
"""
function solve!(uh,solver::FESolver,op::FEOperator)
solve!(uh,solver,op,nothing)
end
function solve!(uh,solver::NonlinearSolver,op::FEOperator)
solve!(uh,NonlinearFESolver(solver),op)
end
function solve!(uh,solver::LinearSolver,op::FEOperator)
solve!(uh,LinearFESolver(solver),op)
end
"""
uh, cache = solve!(uh,solver,op,cache)
This function changes the state of the input and can render it in a corrupted state.
It is recommended to rewrite the input `uh` with the output as illustrated to prevent any
issue. If `cache===nothing`, then it creates a new cache object.
"""
function solve!(uh,solver::FESolver,op::FEOperator,cache)
@abstractmethod
end
function solve!(uh,solver::NonlinearSolver,op::FEOperator,cache)
solve!(uh,NonlinearFESolver(solver),op,cache)
end
function solve!(uh,solver::LinearSolver,op::FEOperator,cache)
solve!(uh,LinearFESolver(solver),op,cache)
end
"""
Solve that allocates, and sets initial guess to zero
and returns the solution
"""
function solve(nls::FESolver,op::FEOperator)
U = get_trial(op)
uh = zero(U)
vh, cache = solve!(uh,nls,op)
vh
end
function solve(nls::NonlinearSolver,op::FEOperator)
solve(NonlinearFESolver(nls),op)
end
function solve(nls::LinearSolver,op::FEOperator)
solve(LinearFESolver(nls),op)
end
function solve(op::AffineFEOperator)
solver = LinearFESolver()
solve(solver,op)
end
"""
"""
function solve(op::FEOperator)
solver = NonlinearFESolver()
solve(solver,op)
end
"""
"""
function test_fe_solver(
nls::FESolver,
op::FEOperator,
x0::AbstractVector,
x::AbstractVector,
pred::Function=isapprox)
trial = get_trial(op)
u = FEFunction(trial,copy(x0))
u, cache = solve!(u,nls,op)
@test pred(get_free_dof_values(u),x)
u = FEFunction(trial,copy(x0))
u,cache = solve!(u,nls,op,cache)
@test pred(get_free_dof_values(u),x)
u = FEFunction(trial,copy(x0))
u,cache = solve!(u,nls,op,cache)
@test pred(get_free_dof_values(u),x)
end
"""
The solver that solves a LinearFEOperator
"""
struct LinearFESolver <: FESolver
ls::LinearSolver
end
"""
"""
function LinearFESolver()
ls = LUSolver()
LinearFESolver(ls)
end
function solve!(uh,solver::LinearFESolver,op::FEOperator, cache)
@unreachable "Cannot solve a generic FEOperator with a LinearFESolver"
end
function solve!(u,solver::LinearFESolver,feop::AffineFEOperator,cache::Nothing)
x = get_free_dof_values(u)
op = get_algebraic_operator(feop)
cache = solve!(x,solver.ls,op)
trial = get_trial(feop)
u_new = FEFunction(trial,x)
(u_new, cache)
end
function solve!(u,solver::LinearFESolver,feop::AffineFEOperator, cache)
x = get_free_dof_values(u)
op = get_algebraic_operator(feop)
cache = solve!(x,solver.ls,op,cache)
trial = get_trial(feop)
u_new = FEFunction(trial,x)
(u_new,cache)
end
"""
A general NonlinearFESolver
"""
struct NonlinearFESolver <: FESolver
nls::NonlinearSolver
end
"""
"""
function FESolver(nls::NonlinearSolver)
NonlinearFESolver(nls)
end
function FESolver()
NonlinearFESolver()
end
"""
"""
function NonlinearFESolver()
nls = NLSolver(show_trace=false,method=:newton)
NonlinearFESolver(nls)
end
function solve!(u,solver::NonlinearFESolver,feop::FEOperator,cache::Nothing)
x = get_free_dof_values(u)
op = get_algebraic_operator(feop)
cache = solve!(x,solver.nls,op)
trial = get_trial(feop)
u_new = FEFunction(trial,x)
(u_new, cache)
end
function solve!(u,solver::NonlinearFESolver,feop::FEOperator,cache)
x = get_free_dof_values(u)
op = get_algebraic_operator(feop)
cache = solve!(x,solver.nls,op,cache)
trial = get_trial(feop)
u_new = FEFunction(trial,x)
(u_new,cache)
end