Skip to content

Commit 92287c3

Browse files
author
J. Gergaud
committed
maj
1 parent 4c9064d commit 92287c3

File tree

10 files changed

+377
-98
lines changed

10 files changed

+377
-98
lines changed

Project.toml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
name = "CTDiffFlow"
22
uuid = "110d5101-4da8-4772-9137-015210ab14e3"
3-
authors = ["Olivier Cots <olivier.cots@toulouse-inp.fr>"]
43
version = "0.1.0"
4+
authors = ["Olivier Cots <olivier.cots@toulouse-inp.fr>"]
5+
6+
[deps]
7+
DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
8+
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
9+
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
10+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
11+
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
12+
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
13+
SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1"
14+
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
515

616
[compat]
17+
DifferentiationInterface = "0.7.9"
18+
ForwardDiff = "1.2.2"
19+
LaTeXStrings = "1.4.0"
20+
LinearAlgebra = "1.12.0"
21+
OrdinaryDiffEq = "6.102.1"
22+
Plots = "1.41.1"
23+
SciMLSensitivity = "7.90.0"
24+
Zygote = "0.7.10"
725
julia = "1.11"

article/Jgarticle.sty

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
\renewcommand{\thesubsubsection}{\thesubsection.\arabic{subsubsection}}
6060
\renewcommand{\labelenumi}{(\roman{enumi})}
6161
\renewcommand{\theenumi}{\roman{enumi}}
62-
\renewcommand{\labelitemi}{aaa}%$\bullet$}
6362
%
6463
% Redefinition d'environnement pour la version française
6564
% ------------------------------------------------------

article/figures/time-steps.jl

Lines changed: 105 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,90 +11,154 @@ using Zygote: Zygote
1111
using SciMLSensitivity
1212
using DifferentiationInterface
1313

14-
include("../../src/DiffFlow.jl")
15-
using .DiffFlow
14+
include("../../src/CTDiffFlow.jl")
15+
using .CTDiffFlow
1616
#
17-
# Definition of the second member
18-
function bruss(x,par,t)
19-
λ = par[1]
20-
x₁ = x[1]
21-
x₂ = x[2]
22-
return [1+x₁^2*x₂-+1)*x₁ , λ*x₁-x₁^2*x₂]
17+
18+
# Derivative of the flow example 1
19+
# ẋ = [λ₁x₁ ; λ₂x₂ ; (λ₁-λ₂)x₃
20+
# x(t0) = x₀(λ) = [λ₂, 1, 1]
21+
# x(t,t0,x_0(λ),λ) = diag(exp(λ₁t),exp(λ₂t),exp((λ₁-λ₂)t))x₀(λ)
22+
# (∂x(t,t0,x_0(λ),λ)/∂x0) = diag(exp(λ₁t),exp(λ₂t),exp((λ₁-λ₂)t))
23+
# ∂x(t,t0,x_0(λ),λ)/∂λ = [λ₂texp(λ₁t) exp(λ₁t) ; 0 texp(λ₂t) ; texp((λ₁-λ₂)t)) -texp((λ₁-λ₂)t))]
24+
#
25+
# ∂x0_flow
26+
27+
A(λ) = [λ[1] 0 0 ; 0 λ[2] 0 ; 0 0 λ[1]-λ[2]]
28+
fun1(x,λ,t) = A(λ)*x
29+
function my_∂x0_flow(tspan,x0,λ; ode_kwargs...)
30+
n = length(x0)
31+
In = Matrix(I(n))
32+
ivp_fun1 = ODEProblem(fun1, In , tspan, λ)
33+
alg = get(ode_kwargs, :alg, Tsit5())
34+
sol = solve(ivp_fun1, alg=alg; ode_kwargs...)
35+
return sol
36+
end
37+
38+
function my_∂x0_flow(t0,x0,tf,λ; ode_kwargs...)
39+
sol = my_∂x0_flow((t0,tf),x0,λ; ode_kwargs...)
40+
return sol.u[end]
41+
end
42+
43+
function my_∂λ_flow(tspan,xX0,λ; ode_kwargs...)
44+
#n,p = size(xX0)
45+
fun2(xX,λ,t) = [fun1(xX[:,1],λ,t) fun1(xX[:,2:end],λ,t)+[xX[1,1] 0 ; 0 xX[2,1] ; xX[3,1] xX[3,1]] ]
46+
47+
ivp_fun = ODEProblem(fun1, xX0 , tspan, λ)
48+
alg = get(ode_kwargs, :alg, Tsit5())
49+
sol = solve(ivp_fun, alg=alg; ode_kwargs...)
50+
return sol
51+
end
52+
53+
function my_∂λ_flow(t0,xX0,tf,λ; ode_kwargs...)
54+
sol = my_∂λ_flow((t0,tf),xX0,λ; ode_kwargs...)
55+
return sol.u[end]
2356
end
2457

25-
t0 = 0.; tf = 1.
26-
tspan = (t0,tf)
27-
λ = [3.]
28-
p = length(λ)
29-
x01 = 1.3
30-
funx0(λ) = [x01, λ[1]]
31-
tol = 1.e-4
32-
n = 2
3358

34-
plt1 = plot(); plt2 = plot()
59+
λ = [1.,2]; p = length(λ);
60+
t0 = 0. ; tf = 1.; tspan = (t0,tf);
61+
funx0(λ) = [λ[2],1.,1];
62+
n = length(funx0(λ))
63+
sol_∂xO_flow = diagm([exp(tf*λ[1]),exp(tf*λ[2]),exp(tf*(λ[1]-λ[2]))])
64+
sol_∂λ_flow = [λ[2]*exp(λ[1]*tf) exp(λ[1]*tf)
65+
0. tf*exp(λ[2]*tf)
66+
tf*exp((λ[1]-λ[2])*tf) tf*exp((λ[1]-λ[2])*tf)]
3567
algo = Tsit5()
3668
reltol = 1.e-4; abstol = 1.e-4
3769

3870
# Flow
39-
ivp = ODEProblem(bruss, funx0(λ), (t0,tf), λ)
71+
#x0λ = funx0(λ)
72+
ivp = ODEProblem(fun1, funx0(λ), (t0,tf), λ)
4073
sol_ivp = solve(ivp, alg=algo; reltol=reltol,abstol=abstol)
4174
println("Flow")
4275
println("Times for the initial flow T = ")
4376
println(sol_ivp.t)
44-
dict_T = Dict(:ivp => sol_ivp.t)
77+
dict_T_var = Dict(:ivp => sol_ivp.t)
78+
dict_T_auto_diff = Dict(:ivp => sol_ivp.t)
4579
dt = sol_ivp.t[2]
4680

4781
# -------------------------------
4882
println("Vatiational equation")
4983
# ------------------------------
5084

51-
∂λ_flow_var = DiffFlow.build_∂λ_flow_var(bruss,t0,funx0,tf,λ)
85+
xX0 = [funx0(λ) [0 1 ; 0 0 ; 0 0]]
86+
println(my_∂λ_flow(t0,xX0,tf,λ))
87+
88+
RelTol = [reltol*ones(n,1) Inf*ones(n,2)]/sqrt(p+1)
89+
AbsTol = [abstol*ones(n,1) Inf*ones(n,2)]/sqrt(p+1)
90+
91+
# plante car 0.*Inf = NaN lors de l'initialisation du pas
92+
sol1 = my_∂λ_flow((t0,tf),xX0,λ;reltol=RelTol,abstol=AbsTol, dt=dt)
93+
94+
∂λ_flow_var = CTDiffFlow.build_∂λ_flow_var(fun1,t0,funx0,tf,λ)
5295

53-
#println(∂λ_flow_var(t0,funx0,tf,λ;reltol=reltol,abstol=abstol))
5496
sol_var = ∂λ_flow_var((t0,tf),funx0,λ;reltol=reltol,abstol=abstol)
97+
dict_T_var[:var_reltol] = sol_var.t
5598
sol_var = ∂λ_flow_var((t0,tf),funx0,λ;reltol=reltol,abstol=abstol,internalnorm = (u,t)->norm(u))
56-
println("Times default values= ", sol_var.t)
57-
dict_T[:var_reltol] = sol_var.t
58-
RelTol = [reltol*ones(n,1) Inf*ones(n,1)]/sqrt(p+1)
59-
AbsTol = [abstol*ones(n,1) Inf*ones(n,1)]/sqrt(p+1)
99+
dict_T_var[:var_reltol_internalnorm] = sol_var.t
100+
101+
my_Inf = prevfloat(typemax(Float64))
102+
RelTol = [reltol*ones(n,1) my_Inf*ones(n,2)]/sqrt(p+1)
103+
AbsTol = [abstol*ones(n,1) my_Inf*ones(n,2)]/sqrt(p+1)
104+
105+
106+
println("AbsTol = ", AbsTol)
107+
println("RelTol = ", RelTol)
60108
sol_var = ∂λ_flow_var((t0,tf),funx0,λ;reltol=RelTol,abstol=AbsTol)
61-
sol_var = ∂λ_flow_var((t0,tf),funx0,λ;reltol=RelTol,abstol=AbsTol, dt=dt)#,internalnorm = (u,t)->norm(u) )
109+
println(sol_ivp.t - sol_var.t)
110+
#println("dt = ",dt)
111+
sol_var = ∂λ_flow_var((t0,tf),funx0,λ;reltol=RelTol,abstol=AbsTol,dt=dt)
62112
println("RelTol = ", RelTol)
63113
println("Times = ", sol_var.t)
64114
println(sol_ivp.t - sol_var.t)
65-
dict_T[:var_RelTol] = sol_var.t
115+
dict_T_var[:var_RelTol] = sol_var.t
116+
xX_var_tf = sol_var.u[end][:,2:end]
117+
66118

67119
# ne fonctionne pas
68120
#@btime ∂λ_flow_var(t0,funx0,tf,λ;reltol=reltol,abstol=abstol)
69121

70122
println("Automatic differentiation")
71123
println("with ForwardDiff")
72-
∂λ_flow = DiffFlow.build_∂λ_flow(bruss,t0,funx0,tf,λ)
73-
sol_diff_auto_flow, T = ∂λ_flow(t0,funx0,tf,λ;reltol=RelTol,abstol=AbsTol,print_times=true)
124+
∂λ_flow = CTDiffFlow.build_∂λ_flow(fun1,t0,funx0,tf,λ)
125+
sol_diff_auto_flow, T = ∂λ_flow(t0,funx0,tf,λ;reltol=reltol,abstol=abstol,print_times=true)
74126
println("Times : ", T)
75-
dict_T[:diff_auto_ForwardDiff] = T
76-
sol_diff_auto_flow, T = ∂λ_flow(tspan,funx0,λ;reltol=RelTol,abstol=AbsTol,print_times=true)
77-
#=
127+
dict_T_auto_diff[:diff_auto_ForwardDiff] = T
128+
129+
#sol_diff_auto_flow, T = ∂λ_flow(tspan,funx0,λ;reltol=reltol,abstol=abstol,print_times=true)
130+
78131
println("internalnorm = (u,t)->norm(u)")
79-
println(∂λ_flow(t0,funx0,tf,λ;reltol=reltol,abstol=abstol,internalnorm = (u,t)->norm(u),print_times=true))
132+
sol_diff_auto_flow, T = ∂λ_flow(t0,funx0,tf,λ;reltol=reltol,abstol=abstol,internalnorm = (u,t)->norm(u),print_times=true)
133+
println("Times : ", T)
134+
dict_T_auto_diff[:diff_auto_ForwardDiff_internalnorm] = T
80135
#@btime ∂λ_flow(t0,funx0,tf,λ;reltol=reltol,abstol=abstol)
81-
=#
136+
82137
println("With Zygote")
83138
# with Zygote
84-
∂λ_flow = DiffFlow.build_∂λ_flow(bruss,t0,funx0,tf,λ; backend=AutoZygote())
85-
sol_diff_auto_flow_tf, T = ∂λ_flow(t0,funx0,tf,λ;reltol=reltol,abstol=abstol,print_times=true)
86-
dict_T[:diff_auto_Zygote] = T
87-
sol_diff_auto_flow,T = ∂λ_flow(tspan,funx0,λ;reltol=reltol,abstol=abstol,print_times=true)
139+
∂λ_flow = CTDiffFlow.build_∂λ_flow(fun1,t0,funx0,tf,λ; backend=AutoZygote())
140+
println("t0= ", t0)
141+
println("tf= ", tf)
142+
println("λ = ", λ)
143+
N = 5
144+
sol_diff_auto_flow_tf, T = ∂λ_flow(t0,funx0,tf,λ;reltol=reltol,abstol=abstol, alg = Euler(),adaptive=false, dt = (tf-t0)/N,print_times=true)
145+
dict_T_auto_diff[:diff_auto_Zygote] = T
146+
xX_auto_diff_Zygote = sol_diff_auto_flow_tf
88147

89-
reshape(sol_diff_auto_flow,2,7)
148+
[sol_∂λ_flow sol_diff_auto_flow sol_diff_auto_flow_tf]
149+
#sort(dict_T; rev = true)
150+
151+
#sol_diff_auto_flow,T = ∂λ_flow(tspan,funx0,λ;reltol=reltol,abstol=abstol,print_times=true)
152+
#println(sol_diff_auto_flow)
153+
#reshape(sol_diff_auto_flow,2,7)
90154

91155
#@btime ∂λ_flow(t0,funx0,tf,λ;reltol=reltol,abstol=abstol)
92156

93157
println("With Mooncake")
94158
# plante
95-
#∂λ_flow = DiffFlow.build_∂λ_flow(bruss,t0,funx0,tf,λ; backend=AutoMooncake())
159+
#∂λ_flow = CTDiffFlow.build_∂λ_flow(fun1,t0,funx0,tf,λ; backend=AutoMooncake())
96160
#println(∂λ_flow(t0,funx0,tf,λ;reltol=reltol,abstol=abstol,print_times=true))
97161
#@btime ∂λ_flow(t0,funx0,tf,λ;reltol=reltol,abstol=abstol)
98162

99-
#plot(plt1,plt2)
163+
100164

article/main.aux

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,24 @@
1515
\@writefile{lof}{\contentsline {figure}{\numberline {1}{\ignorespaces Derivative computing by finite differences. $t_f=20, \lambda $ ranging from 2.88 to 3.08, $Tol=RelTol=AbsTol=10^{-4}$. Top graphs is for $\delta \lambda =4Tol$ and bottom graphs for $\delta \lambda =\sqrt {Tol}$. The numerical integrattion is done with Tsit5().}}{2}{figure.1}\protected@file@percent }
1616
\@writefile{toc}{\contentsline {subsection}{\numberline {3.3}Variationnal equation }{2}{subsection.3.3}\protected@file@percent }
1717
\newlabel{varlambda}{{1}{2}{Variationnal equation}{ivpref.3.1}{}}
18-
\bibstyle{plain}
19-
\bibdata{./main}
20-
\bibcite{Bock81}{1}
18+
\citation{Bock81}
2119
\@writefile{lof}{\contentsline {figure}{\numberline {2}{\ignorespaces Derivative computing with the variational equation. $t_f=20, \lambda $ ranging from 2.88 to 3.08, $Tol=RelTol=AbsTol=10^{-4}$. The numerical integrattion is done with Tsit5().}}{3}{figure.2}\protected@file@percent }
2220
\@writefile{toc}{\contentsline {subsection}{\numberline {3.4}Automatic differentiation of the flow}{3}{subsection.3.4}\protected@file@percent }
2321
\@writefile{lof}{\contentsline {figure}{\numberline {3}{\ignorespaces Derivative computing by automatic differentiation of the flow. $t_f=20, \lambda $ ranging from 2.88 to 3.08, $Tol=RelTol=AbsTol=10^{-4}$. The numerical integrattion is done with Tsit5(), the automatic differentiation is ForwardDiff.}}{3}{figure.3}\protected@file@percent }
22+
\citation{HaNoWa93}
23+
\@writefile{toc}{\contentsline {section}{\numberline {4}Tests on the time steps}{4}{section.4}\protected@file@percent }
24+
\@writefile{toc}{\contentsline {subsection}{\numberline {4.1}Example}{4}{subsection.4.1}\protected@file@percent }
25+
\@writefile{toc}{\contentsline {subsection}{\numberline {4.2}Step grids with variationnal equations}{4}{subsection.4.2}\protected@file@percent }
26+
\bibstyle{plain}
27+
\bibdata{./main}
28+
\bibcite{Bock81}{1}
29+
\@writefile{lot}{\contentsline {table}{\numberline {1}{\ignorespaces \it Results with variationnal equations.}}{5}{table.1}\protected@file@percent }
30+
\newlabel{table:steps_var_equation}{{1}{5}{\it Results with variationnal equations}{table.1}{}}
31+
\@writefile{toc}{\contentsline {subsection}{\numberline {4.3}Step grids with automatic differentiation of the flow}{5}{subsection.4.3}\protected@file@percent }
32+
\@writefile{lot}{\contentsline {table}{\numberline {2}{\ignorespaces it Results with automatic differentiation.}}{5}{table.2}\protected@file@percent }
33+
\newlabel{table:steps_auto_diff}{{2}{5}{it Results with automatic differentiation}{table.2}{}}
34+
\@writefile{toc}{\contentsline {subsection}{\numberline {4.4}Comparaison of the solutions for the methods with the same step grid}{5}{subsection.4.4}\protected@file@percent }
35+
\@writefile{toc}{\contentsline {subsection}{\numberline {4.5}conclusion}{5}{subsection.4.5}\protected@file@percent }
2436
\bibcite{HaNoWa93}{2}
25-
\@writefile{toc}{\contentsline {section}{\numberline {4}Test on the time steps}{4}{section.4}\protected@file@percent }
26-
\@writefile{toc}{\contentsline {section}{\numberline {5}Conclusion and perspectives}{4}{section.5}\protected@file@percent }
27-
\gdef \@abspage@last{4}
37+
\@writefile{toc}{\contentsline {section}{\numberline {5}Conclusion and perspectives}{6}{section.5}\protected@file@percent }
38+
\gdef \@abspage@last{6}

0 commit comments

Comments
 (0)