Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Updates due to JuliaDiff/TaylorSeries.jl#361

* Remove comments and small fix

* Update CI (use TaylorSeries branch from fork); use `diffeq!` in non-parsed jetcoeffs! methods

* Update CI

* Use diffeq! in preamble

* Retain only `local` declarations in preamble

* Trigger CI

* Update CI

* Update Project.toml

* Update CI

* Rename diffeq! -> ode!

* Update ci.yml

* Trigger CI

* Update README.md

* Rename ode! -> solcoeff!

* Bump patch version
  • Loading branch information
PerezHz authored Jul 22, 2024
1 parent 7b4082a commit e32cd4d
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 37 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on:
- 'LICENSE.md'
- 'README.md'
pull_request:
branches:
- main
tags: '*'

jobs:
pre_job:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -54,6 +58,8 @@ jobs:
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
### Uncomment line below to test using a dependency branch
# - run: julia --project=. -e 'import Pkg; Pkg.add(url="https://github.com/PerezHz/TaylorSeries.jl", rev="jp/evaluate-tn"); Pkg.instantiate()'
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TaylorIntegration"
uuid = "92b13dbe-c966-51a2-8445-caca9f8a7d42"
repo = "https://github.com/PerezHz/TaylorIntegration.jl.git"
version = "0.15.2"
version = "0.15.3"

[deps]
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
Expand Down Expand Up @@ -38,7 +38,7 @@ RecursiveArrayTools = "2, 3"
Reexport = "1"
Requires = "1"
StaticArrays = "0.12.5, 1"
TaylorSeries = "0.17"
TaylorSeries = "0.18"
Test = "<0.0.1, 1"
julia = "1.6"

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ Comments, suggestions, contributions and improvements are welcome and appreciate

## Acknowledgments

We acknowledge financial support from DGAPA-PAPIIT grants IG-100616 and IG-100819.
We acknowledge financial support from DGAPA-PAPIIT grants IG-100616, IG-100819 and IG-101122.
23 changes: 23 additions & 0 deletions src/TaylorIntegration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,27 @@ function __init__()
end
end

@inline function solcoeff!(a::Taylor1{T}, b::Taylor1{T}, k::Int) where {T<:TaylorSeries.NumberNotSeries}
a[k] = b[k-1]/k
return nothing
end

@inline function solcoeff!(res::Taylor1{Taylor1{T}}, a::Taylor1{Taylor1{T}},
k::Int) where {T<:TaylorSeries.NumberNotSeries}
for l in eachindex(a[k-1])
res[k][l] = a[k-1][l]/k
end
return nothing
end

@inline function solcoeff!(res::Taylor1{TaylorN{T}}, a::Taylor1{TaylorN{T}},
k::Int) where {T<:TaylorSeries.NumberNotSeries}
for l in eachindex(a[k-1])
for m in eachindex(a[k-1][l])
res[k][l][m] = a[k-1][l][m]/k
end
end
return nothing
end

end #module
7 changes: 2 additions & 5 deletions src/integrator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ function jetcoeffs!(eqsdiff::Function, t::Taylor1{T}, x::Taylor1{U}, params) whe
ordnext = ord+1

# # Set `taux`, `xaux`, auxiliary Taylor1 variables to order `ord`
# @inbounds taux = Taylor1( t.coeffs[1:ordnext] )
@inbounds xaux = Taylor1( x.coeffs[1:ordnext] )

# Equations of motion
dx = eqsdiff(xaux, params, t)

# Recursion relation
@inbounds x[ordnext] = dx[ord]/ordnext
@inbounds solcoeff!(x, dx, ordnext)
end
nothing
end
Expand Down Expand Up @@ -67,8 +66,6 @@ function jetcoeffs!(eqsdiff!::Function, t::Taylor1{T},
for ord in 0:order-1
ordnext = ord+1

# # Set `taux`, auxiliary Taylor1 variable to order `ord`
# @inbounds taux = Taylor1( t.coeffs[1:ordnext] )
# Set `xaux`, auxiliary vector of Taylor1 to order `ord`
for j in eachindex(x)
@inbounds xaux[j] = Taylor1( x[j].coeffs[1:ordnext] )
Expand All @@ -79,7 +76,7 @@ function jetcoeffs!(eqsdiff!::Function, t::Taylor1{T},

# Recursion relations
for j in eachindex(x)
@inbounds x[j].coeffs[ordnext+1] = dx[j].coeffs[ordnext]/ordnext
@inbounds solcoeff!(x[j], dx[j], ordnext)
end
end
nothing
Expand Down
72 changes: 48 additions & 24 deletions src/parse_eqs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ mutable struct BookKeeping
v_array4 :: Vector{Symbol}
v_preamb :: Vector{Union{Symbol,Expr}}
retvar :: Symbol
numaux :: Int

function BookKeeping()
return new(Dict{Symbol, Expr}(), Dict{Union{Symbol,Expr}, Number}(),
Dict{Symbol, Expr}(), Symbol[], Symbol[], Symbol[], Symbol[], Symbol[], Symbol[],
Union{Symbol,Expr}[], :nothing)
Union{Symbol,Expr}[], :nothing, 0)
end
end

Expand Down Expand Up @@ -123,7 +124,7 @@ const _HEAD_ALLOC_TAYLOR1_VECTOR = sanitize(:(
# Constants for the initial declaration and initialization of arrays
const _DECL_ARRAY = sanitize( Expr(:block,
:(__var1 = Array{Taylor1{_S}}(undef, __var2)),
:( for i in CartesianIndices(__var1) __var1[i] = Taylor1( zero(constant_term(__x[1])), order ) end ))
:( for i in eachindex(__var1) __var1[i] = Taylor1( zero(constant_term(__x[1])), order ) end ))
);


Expand All @@ -147,25 +148,25 @@ function _make_parsed_jetcoeffs(ex::Expr)
new_jetcoeffs, new_allocjetcoeffs = _newhead(fn, fnargs)

# Transform the graph representation of the body of the functions:
# defspreamble: inicializations used for the zeroth order (preamble)
# defspreamble: initializations used for the zeroth order (preamble)
# defsprealloc: definitions (declarations) of auxiliary Taylor1's
# fnbody: transformed function body, using mutating functions from TaylorSeries;
# used later within the recursion loop
# bkkeep: book-keeping structure having info of the variables
defspreamble, defsprealloc, fnbody, bkkeep = _preamble_body(fnbody, fnargs)

# Create body of recursion loop; temporary assignements may be needed.
# rec_preamb: recursion loop for the preamble (first order correction)
# rec_fnbody: recursion loop for the body-function (recursion loop for higher orders)
rec_preamb, rec_fnbody = _recursionloop(fnargs, bkkeep)
# rec_fnbody: recursion loop for the body-function (recursion loop for all orders)
rec_fnbody = _recursionloop(fnargs, bkkeep)

# Expr for the for-loop block for the recursion (of the `x` variable)
forloopblock = Expr(:for, :(ord = 1:order-1), Expr(:block, :(ordnext = ord + 1)) )
# Add rec_fnbody to forloopblock
forloopblock = Expr(:for, :(ord = 0:order-1), Expr(:block, :(ordnext = ord + 1)) )

# Add rec_fnbody to `forloopblock`
push!(forloopblock.args[2].args, fnbody.args[1].args..., rec_fnbody)

# Add preamble and recursion body to `new_jetcoeffs`
push!(new_jetcoeffs.args[2].args, defspreamble..., rec_preamb)
push!(new_jetcoeffs.args[2].args, defspreamble...)

# Push preamble and forloopblock to `new_jetcoeffs` and return line
push!(new_jetcoeffs.args[2].args, forloopblock, Meta.parse("return nothing"))
Expand Down Expand Up @@ -337,11 +338,11 @@ of the original diferential equations function.
"""
function _preamble_body(fnbody, fnargs)
# Inicialize BookKeeping struct
# Initialize BookKeeping struct
bkkeep = BookKeeping()

# Rename vars to have the body in non-indexed form; bkkeep has different entries
# for bookkeeping variables/symbolds, including indexed ones
# for bookkeeping variables/symbols, including indexed ones
fnbody, bkkeep.d_indx = _rename_indexedvars(fnbody)

# Create `newfnbody` which corresponds to `fnbody`, cleaned (without irrelevant comments)
Expand All @@ -362,15 +363,22 @@ function _preamble_body(fnbody, fnargs)
preamble = subs(preamble, bkkeep.d_assign)
prealloc = subs(prealloc, bkkeep.d_assign)

# Include the assignement of indexed auxiliary variables
# Include the assignment of indexed auxiliary variables
defsprealloc = _defs_allocs!(prealloc, fnargs, bkkeep, false)
preamble = subs(preamble, bkkeep.d_indx)
defspreamble = Expr[preamble.args...]

# Retain only `local` declarations from `preamble` in `new_preamble`
new_preamble = Expr(:block,)
for (i, arg) in enumerate(preamble.args)
(arg.head == :local) && push!(new_preamble.args, arg)
end
defspreamble = Expr[new_preamble.args...]

# Bring back substitutions
newfnbody = subs(newfnbody, bkkeep.d_indx)

# Define retvar; for scalar eqs is the last entry included in v_newvars
bkkeep.retvar = length(fnargs) == 3 ? subs(bkkeep.v_newvars[end], bkkeep.d_indx) : fnargs[1]
bkkeep.retvar = length(fnargs) == 3 ? subs(bkkeep.v_newvars[end-bkkeep.numaux], bkkeep.d_indx) : fnargs[1]

return defspreamble, defsprealloc, newfnbody, bkkeep
end
Expand Down Expand Up @@ -834,7 +842,7 @@ function _replacecalls!(bkkeep::BookKeeping, fnold::Expr, newvar::Symbol)
end

elseif ll == 3
# Binary call; no auxiliary expressions needed
# Binary call
newarg2 = fnold.args[3]

# Replacements
Expand All @@ -859,6 +867,28 @@ function _replacecalls!(bkkeep::BookKeeping, fnold::Expr, newvar::Symbol)
# Dict(:_res => newvar, :_arg1 => :(constant_term($(newarg1))),
# :_arg2 => :(constant_term($(newarg2))), :_k => :ord))

# Auxiliary expression
if aux_fnexpr.head != :nothing
newaux = genname()

aux_alloc = :( _res = Taylor1($(aux_fnexpr.args[2]), order) )
aux_alloc = subs(aux_alloc,
Dict(:_res => newaux, :_arg1 => :(constant_term($(newarg1))), :_aux => newaux))

aux_fnexpr = Expr(:block,
:(TaylorSeries.zero!(_res)),
:(_res.coeffs[1] = $(aux_fnexpr.args[2])) )
aux_fnexpr = subs(aux_fnexpr,
Dict(:_res => newaux, :_arg1 => :(constant_term($(newarg1))), :_aux => newaux))

fnexpr = subs(fnexpr, Dict(:_aux => newaux))
if newvar bkkeep.v_arraydecl
push!(bkkeep.v_arraydecl, newaux)
else
bkkeep.numaux += 1
push!(bkkeep.v_newvars, newaux)
end
end
else
# Recognized call, but not a unary or binary call; copy expression
fnexpr = :($newvar = $fnold)
Expand Down Expand Up @@ -1037,27 +1067,21 @@ function _recursionloop(fnargs, bkkeep::BookKeeping)
ll = length(fnargs)

if ll == 3
rec_preamb = sanitize( :( $(fnargs[1]).coeffs[2] = $(bkkeep.retvar).coeffs[1] ) )
rec_fnbody = sanitize( :( $(fnargs[1]).coeffs[ordnext+1] = $(bkkeep.retvar).coeffs[ordnext]/ordnext ) )
rec_fnbody = sanitize( :( TaylorIntegration.solcoeff!($(fnargs[1]), $(bkkeep.retvar), ordnext) ) )

elseif ll == 4
bkkeep.retvar = fnargs[1]
rec_preamb = sanitize(:(
for __idx in eachindex($(fnargs[2]))
$(fnargs[2])[__idx].coeffs[2] = $(bkkeep.retvar)[__idx].coeffs[1]
end))
rec_fnbody = sanitize(:(
for __idx in eachindex($(fnargs[2]))
$(fnargs[2])[__idx].coeffs[ordnext+1] =
$(bkkeep.retvar)[__idx].coeffs[ordnext]/ordnext
TaylorIntegration.solcoeff!($(fnargs[2])[__idx], $(bkkeep.retvar)[__idx], ordnext)
end))

else
throw(ArgumentError(
"Wrong number of arguments in the definition of the function $fn"))
end

return rec_preamb, rec_fnbody
return rec_fnbody
end


Expand Down
8 changes: 3 additions & 5 deletions test/taylorize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ import Logging: Warn
end


# Pendulum integrtf = 100.0
# Pendulum integration
@testset "Integration of the pendulum and DiffEqs interface" begin
@taylorize function pendulum!(dx::Array{T,1}, x::Array{T,1}, p, t) where {T}
dx[1] = x[2]
Expand Down Expand Up @@ -1297,10 +1297,8 @@ import Logging: Warn

# Include not recognized functions as they appear
@test newex1.args[2].args[2] == :(aa = __ralloc.v0[1])
@test newex1.args[2].args[3] == :(aa = my_simple_function(q, p, t))
@test newex2.args[2].args[2] == :(aa = my_simple_function(q, p, t))
@test newex1.args[2].args[6].args[2].args[2] ==
:(aa = my_simple_function(q, p, t))
@test newex1.args[2].args[3].args[2].args[2] == :(aa = my_simple_function(q, p, t))

# Return line
@test newex1.args[2].args[end] == :(return nothing)
Expand Down Expand Up @@ -1331,7 +1329,7 @@ import Logging: Warn
end
end)

@test newex1.args[2].args[6].args[2].args[3] == Base.remove_linenums!(ex)
@test newex1.args[2].args[3].args[2].args[3] == Base.remove_linenums!(ex)

# Throws no error
ex = :(
Expand Down

2 comments on commit e32cd4d

@PerezHz
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register

Release notes:

In this release we update to TaylorSeries v0.18 and deal with corresponding changes in the internals of @taylorize macro.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/111558

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.15.3 -m "<description of version>" e32cd4d9295fc8988d0714fb6232a30d82c06dc4
git push origin v0.15.3

Please sign in to comment.