Skip to content

Commit 8a1101d

Browse files
author
fchen121
committed
Update FDE_to_ODE to handle multiple FDEs
1 parent 6fd7d31 commit 8a1101d

File tree

1 file changed

+62
-50
lines changed

1 file changed

+62
-50
lines changed

src/systems/diffeqs/basic_transformations.jl

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -184,71 +184,83 @@ function change_of_variables(
184184
return new_sys
185185
end
186186

187-
function FDE_to_ODE(eqs, alpha, epsilon, T; initial=0)
187+
function FDE_to_ODE(eqs, alphas, epsilon, T; initials=0)
188188
@independent_variables t
189189
@variables x(t)
190190
D = Differential(t)
191-
192-
alpha_0 = alpha
193-
if (alpha > 1)
194-
coeff = 1/(alpha - 1)
195-
m = 2
196-
while (alpha - m > 0)
197-
coeff /= alpha - m
198-
m += 1
191+
i = 0
192+
all_eqs = Equation[]
193+
all_def = Pair{Num, Int64}[]
194+
195+
function FDE_helper(sub_eq, α; initial=0)
196+
alpha_0 = α
197+
if> 1)
198+
coeff = 1/- 1)
199+
m = 2
200+
while- m > 0)
201+
coeff /= α - m
202+
m += 1
203+
end
204+
alpha_0 = α - m + 1
199205
end
200-
alpha_0 = alpha - m + 1
201-
end
202-
203-
δ = (gamma(alpha_0+1) * epsilon)^(1/alpha_0)
204-
a = pi/2*(1-(1-alpha_0)/((2-alpha_0) * log(epsilon^-1)))
205-
h = 2*pi*a / log(1 + (2/epsilon * (cos(a))^(alpha_0 - 1)))
206206

207-
x_sub = (gamma(2-alpha_0) * epsilon)^(1/(1-alpha_0))
208-
x_sup = -log(gamma(1-alpha_0) * epsilon)
209-
M = floor(Int, log(x_sub / T) / h)
210-
N = ceil(Int, log(x_sup / δ) / h)
207+
δ = (gamma(alpha_0+1) * epsilon)^(1/alpha_0)
208+
a = pi/2*(1-(1-alpha_0)/((2-alpha_0) * log(epsilon^-1)))
209+
h = 2*pi*a / log(1 + (2/epsilon * (cos(a))^(alpha_0 - 1)))
211210

212-
function c_i(index)
213-
h * sin(pi * alpha_0) / pi * exp((1-alpha_0)*h*index)
214-
end
211+
x_sub = (gamma(2-alpha_0) * epsilon)^(1/(1-alpha_0))
212+
x_sup = -log(gamma(1-alpha_0) * epsilon)
213+
M = floor(Int, log(x_sub / T) / h)
214+
N = ceil(Int, log(x_sup / δ) / h)
215215

216-
function γ_i(index)
217-
exp(h * index)
218-
end
216+
function c_i(index)
217+
h * sin(pi * alpha_0) / pi * exp((1-alpha_0)*h*index)
218+
end
219219

220-
new_eqs = Equation[]
221-
rhs = initial
222-
def = Pair{Num, Int64}[]
223-
224-
if (alpha < 1)
225-
for index in range(M, N-1; step=1)
226-
new_z = Symbol(:z, :_, index-M)
227-
new_z = ModelingToolkit.unwrap(only(@variables $new_z(t)))
228-
new_eq = D(new_z) ~ eqs - γ_i(index)*new_z
229-
push!(new_eqs, new_eq)
230-
push!(def, new_z=>0)
231-
rhs += c_i(index)*new_z
220+
function γ_i(index)
221+
exp(h * index)
232222
end
233-
else
234-
for index in range(M, N-1; step=1)
235-
new_z = Symbol(:z, :_, index-M)
236-
start = eqs
237-
γ = γ_i(index)
238-
for k in range(1, m; step=1)
239-
new_z = Symbol(:z, :_, index-M, :_, k)
223+
224+
new_eqs = Equation[]
225+
rhs = initial
226+
def = Pair{Num, Int64}[]
227+
228+
if< 1)
229+
for index in range(M, N-1; step=1)
230+
new_z = Symbol(:z, :_, i)
231+
i += 1
240232
new_z = ModelingToolkit.unwrap(only(@variables $new_z(t)))
241-
new_eq = D(new_z) ~ start - γ*new_z
242-
start = k * new_z
233+
new_eq = D(new_z) ~ sub_eq - γ_i(index)*new_z
243234
push!(new_eqs, new_eq)
244235
push!(def, new_z=>0)
236+
rhs += c_i(index)*new_z
237+
end
238+
else
239+
for index in range(M, N-1; step=1)
240+
new_z = Symbol(:z, :_, i)
241+
i += 1
242+
γ = γ_i(index)
243+
for k in range(1, m; step=1)
244+
new_z = Symbol(:z, :_, index-M, :_, k)
245+
new_z = ModelingToolkit.unwrap(only(@variables $new_z(t)))
246+
new_eq = D(new_z) ~ sub_eq - γ*new_z
247+
sub_eq = k * new_z
248+
push!(new_eqs, new_eq)
249+
push!(def, new_z=>0)
250+
end
251+
rhs += coeff*c_i(index)*new_z
245252
end
246-
rhs += coeff*c_i(index)*new_z
247253
end
254+
push!(new_eqs, x ~ rhs)
255+
return (new_eqs, def)
256+
end
257+
258+
for (eq, alpha) in zip(eqs, alphas)
259+
(new_eqs, def) = FDE_helper(eq, alpha)
260+
append!(all_eqs, new_eqs)
261+
append!(all_def, def)
248262
end
249-
eq = x ~ rhs
250-
push!(new_eqs, eq)
251-
@named sys = System(new_eqs, t; defaults=def)
263+
@named sys = System(all_eqs, t; defaults=all_def)
252264
return mtkcompile(sys)
253265
end
254266

0 commit comments

Comments
 (0)