-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathForwardEuler.jl
More file actions
78 lines (64 loc) · 1.87 KB
/
ForwardEuler.jl
File metadata and controls
78 lines (64 loc) · 1.87 KB
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
"""
f-method ODE solver
"""
struct ForwardEuler <: ODESolver
nls::NonlinearSolver
dt::Float64
end
function solve_step!(uf::AbstractVector,
solver::ForwardEuler,
op::ODEOperator,
u0::AbstractVector,
t0::Real,
cache) # -> (uF,tF)
if cache === nothing
ode_cache = allocate_cache(op)
vf = similar(u0)
nl_cache = nothing
else
ode_cache, vf, nl_cache = cache
end
dt = solver.dt
tf = t0+dt
# The space should have the boundary conditions at tf
ode_cache = update_cache!(ode_cache,op,t0)
nlop = ForwardEulerNonlinearOperator(op,t0,dt,u0,ode_cache,vf)
nl_cache = solve!(uf,solver.nls,nlop,nl_cache)
cache = (ode_cache, vf, nl_cache)
return (uf,tf,cache)
end
"""
Nonlinear operator that represents the Forward Euler nonlinear operator at a
given time step, i.e., A(t,u_n,(u_n+1-u_n)/dt)
"""
struct ForwardEulerNonlinearOperator <: NonlinearOperator
odeop::ODEOperator
tf::Float64
dt::Float64
u0::AbstractVector
ode_cache
vf::AbstractVector
end
function residual!(b::AbstractVector,op::ForwardEulerNonlinearOperator,x::AbstractVector)
vf = op.vf
vf = (x-op.u0)/op.dt
residual!(b,op.odeop,op.tf,(op.u0,vf),op.ode_cache)
end
function jacobian!(A::AbstractMatrix,op::ForwardEulerNonlinearOperator,x::AbstractVector)
vf = op.vf
vf = (x-op.u0)/op.dt
z = zero(eltype(A))
fillstored!(A,z)
jacobians!(A,op.odeop,op.tf,(op.u0,vf),(0,1/op.dt),op.ode_cache)
end
function allocate_residual(op::ForwardEulerNonlinearOperator,x::AbstractVector)
allocate_residual(op.odeop,x,op.ode_cache)
end
function allocate_jacobian(op::ForwardEulerNonlinearOperator,x::AbstractVector)
allocate_jacobian(op.odeop,x,op.ode_cache)
end
function zero_initial_guess(op::ForwardEulerNonlinearOperator)
x0 = similar(op.u0)
fill!(x0,zero(eltype(x0)))
x0
end