Skip to content

Commit 17f978b

Browse files
author
fchen121
committed
Add new function to deal with multiple term problem
1 parent ee6a7e5 commit 17f978b

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
4545
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
4646
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
4747
OrdinaryDiffEqCore = "bbf590c4-e513-4bbe-9b18-05decba2e5d8"
48+
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
4849
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
4950
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
5051
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
@@ -142,6 +143,7 @@ OrdinaryDiffEq = "6.82.0"
142143
OrdinaryDiffEqCore = "1.15.0"
143144
OrdinaryDiffEqDefault = "1.2"
144145
OrdinaryDiffEqNonlinearSolve = "1.5.0"
146+
Plots = "1.40.15"
145147
PrecompileTools = "1"
146148
Pyomo = "0.1.0"
147149
REPL = "1"

src/ModelingToolkit.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbanc
299299
hasunit, getunit, hasconnect, getconnect,
300300
hasmisc, getmisc, state_priority
301301
export liouville_transform, change_independent_variable, substitute_component, add_accumulations,
302-
noise_to_brownians, Girsanov_transform, change_of_variables, fractional_to_ordinary
302+
noise_to_brownians, Girsanov_transform, change_of_variables,
303+
fractional_to_ordinary, linear_fractional_to_ordinary
303304
export PDESystem
304305
export Differential, expand_derivatives, @derivatives
305306
export Equation, ConstrainedEquation

src/systems/diffeqs/basic_transformations.jl

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,75 @@ function fractional_to_ordinary(eqs, variables, alphas, epsilon, T; initials = 0
294294
return mtkcompile(sys)
295295
end
296296

297+
function linear_fractional_to_ordinary(degrees, coeffs, rhs, epsilon, T; initials = 0)
298+
@independent_variables t
299+
@variables x_0(t)
300+
D = Differential(t)
301+
i = 0
302+
all_eqs = Equation[]
303+
all_def = Pair{Num, Int64}[]
304+
305+
function fto_helper(sub_eq, α)
306+
δ = (gamma+1) * epsilon)^(1/α)
307+
a = pi/2*(1-(1-α)/((2-α) * log(epsilon^-1)))
308+
h = 2*pi*a / log(1 + (2/epsilon * (cos(a))^- 1)))
309+
310+
x_sub = (gamma(2-α) * epsilon)^(1/(1-α))
311+
x_sup = -log(gamma(1-α) * epsilon)
312+
M = floor(Int, log(x_sub / T) / h)
313+
N = ceil(Int, log(x_sup / δ) / h)
314+
315+
function c_i(index)
316+
h * sin(pi * α) / pi * exp((1-α)*h*index)
317+
end
318+
319+
function γ_i(index)
320+
exp(h * index)
321+
end
322+
323+
new_eqs = Equation[]
324+
def = Pair{Num, Int64}[]
325+
sum = 0
326+
for index in range(M, N-1; step=1)
327+
new_z = Symbol(:z, :_, i)
328+
i += 1
329+
new_z = ModelingToolkit.unwrap(only(@variables $new_z(t)))
330+
new_eq = D(new_z) ~ sub_eq - γ_i(index)*new_z
331+
push!(new_eqs, new_eq)
332+
push!(def, new_z=>0)
333+
sum += c_i(index)*new_z
334+
end
335+
return (new_eqs, def, sum)
336+
end
337+
338+
previous = x_0
339+
for i in range(1, ceil(Int, degrees[1]); step=1)
340+
new_x = Symbol(:x, :_, i)
341+
new_x = ModelingToolkit.unwrap(only(@variables $new_x(t)))
342+
push!(all_eqs, D(previous) ~ new_x)
343+
push!(all_def, previous => initials[i])
344+
previous = new_x
345+
end
346+
347+
new_rhs = -rhs
348+
for (degree, coeff) in zip(degrees, coeffs)
349+
rounded = ceil(Int, degree)
350+
new_x = Symbol(:x, :_, rounded)
351+
new_x = ModelingToolkit.unwrap(only(@variables $new_x(t)))
352+
if isinteger(degree)
353+
new_rhs += coeff * new_x
354+
else
355+
(new_eqs, def, sum) = fto_helper(new_x, rounded - degree)
356+
append!(all_eqs, new_eqs)
357+
append!(all_def, def)
358+
new_rhs += coeff * sum
359+
end
360+
end
361+
push!(all_eqs, 0 ~ new_rhs)
362+
@named sys = System(all_eqs, t; defaults=all_def)
363+
return mtkcompile(sys)
364+
end
365+
297366
"""
298367
change_independent_variable(
299368
sys::System, iv, eqs = [];

0 commit comments

Comments
 (0)