Skip to content
Open
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7853dcb
comentario
May 17, 2023
aaf5940
Merge branch 'main' into Energy_Preserving
Sondar74 May 21, 2023
afe7373
Merge branch 'main' of github.com:Sondar74/BSeries.jl into main
Sondar74 Jun 26, 2023
e4d79cc
updated 'main' branch and created new branch 'csrkupdated'. This one …
Sondar74 Jun 26, 2023
823684d
deleted extra file
Sondar74 Jun 26, 2023
0df736b
fixing 'Spell Check'
Sondar74 Jun 26, 2023
09d4629
added docstrings and removed elementary_differentials_csrk from export
Sondar74 Jun 27, 2023
335265f
modified the comments in the functions 'PolynomialA' and 'elementary_…
Sondar74 Jun 27, 2023
8c4e0b1
Modified the function 'bseries' for CSRK
Sondar74 Jun 27, 2023
7ad5325
exported 'ContinuousStageRungeKuttaMethod'
Sondar74 Jun 27, 2023
5d82c87
added one test, and modified the function 'bseries' for CSRK
Sondar74 Jun 27, 2023
c0a724d
Spell Check
Sondar74 Jun 27, 2023
c45de1c
Update src/BSeries.jl
Sondar74 Jun 28, 2023
6616b9c
Update src/BSeries.jl
Sondar74 Jun 28, 2023
1227e1a
Update src/BSeries.jl
Sondar74 Jun 28, 2023
3691f69
Update src/BSeries.jl
Sondar74 Jun 28, 2023
68cecb0
Update test/runtests.jl
Sondar74 Jun 28, 2023
5d30865
updating docstring
Sondar74 Jun 28, 2023
7387ffc
Merge branch 'csrkupdated' of github.com:Sondar74/BSeries.jl into csr…
Sondar74 Jun 28, 2023
53fd493
fixed the values of bseries()
Sondar74 Jun 28, 2023
d6a23d6
added test for floating-point and symbolic coefficientes using SymPy …
Sondar74 Jun 28, 2023
c0ba86f
added test for Symbolics.jl: same as SymEngine
Sondar74 Jun 28, 2023
b5813d2
Update test/runtests.jl
Sondar74 Jun 28, 2023
691507f
Update test/runtests.jl
Sondar74 Jun 28, 2023
02acff4
Update test/runtests.jl
Sondar74 Jun 28, 2023
fd1a23e
added Energy_Preserving test
Sondar74 Jun 28, 2023
0c29499
Merge branch 'csrkupdated' of github.com:Sondar74/BSeries.jl into csr…
Sondar74 Jun 28, 2023
c8113e6
fixed the funtion bseries(csrk, order)
Sondar74 Jul 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 103 additions & 1 deletion src/BSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ end

using Latexify: Latexify, LaTeXString
using Combinatorics: Combinatorics, permutations
using LinearAlgebra: LinearAlgebra, rank
using LinearAlgebra: LinearAlgebra, rank, dot
using SparseArrays: SparseArrays, sparse

@reexport using Polynomials: Polynomials, Polynomial
Expand All @@ -40,6 +40,8 @@ export renormalize!

export is_energy_preserving, energy_preserving_order

export elementary_differentials_csrk, CSRK
Comment thread
Sondar74 marked this conversation as resolved.
Outdated

# Types used for traits
# These traits may decide between different algorithms based on the
# corresponding complexity etc.
Expand Down Expand Up @@ -74,6 +76,20 @@ end

TruncatedBSeries{T, V}() where {T, V} = TruncatedBSeries{T, V}(OrderedDict{T, V}())

"""
CSRK struct
"""
struct ContinuousStageRungeKuttaMethod{MatT <: AbstractMatrix}
matrix::MatT
end
Comment thread
Sondar74 marked this conversation as resolved.
Outdated
Comment thread
Sondar74 marked this conversation as resolved.
Outdated

function CSRK(matrix::AbstractMatrix)
T = promote_type(eltype(matrix))
_M = T.(matrix)
return ContinuousStageRungeKuttaMethod(_M)
end


Comment thread
Sondar74 marked this conversation as resolved.
Outdated
# general interface methods of `AbstractDict` for `TruncatedBSeries`
@inline Base.iterate(series::TruncatedBSeries) = iterate(series.coef)
@inline Base.iterate(series::TruncatedBSeries, state) = iterate(series.coef, state)
Expand Down Expand Up @@ -612,6 +628,23 @@ function bseries(ros::RosenbrockMethod, order)
return series
end

"""
bseries CSRK
"""
function bseries(csrk::ContinuousStageRungeKuttaMethod, order)
Comment thread
Sondar74 marked this conversation as resolved.
csrk = csrk.matrix
V = Rational{Int64}
series = TruncatedBSeries{RootedTree{Int, Vector{Int}}, V}()
Comment thread
ranocha marked this conversation as resolved.
Outdated
series[rootedtree(Int[])] = one(Int64)
for o in 1:order
for t in RootedTreeIterator(o)
series[copy(t)] = elementary_differentials_csrk(csrk, t)
end
end

return series
end
Comment on lines +675 to +707

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please add tests. This does not work correctly right now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okey, I'm working on this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When working with the function one(), I have the problem that eltype(csrk) returns Any. How can I fix it?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

What is the csrk that you use here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is the csrk obtained from csrk = ContinuousStageRungeKuttaMethod(M), for a matrix M.

@ranocha ranocha Jun 28, 2023

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Use eltype(csrk.matrix) instead or define eltype(csrk::ContinuousStageRungeKuttaMethod) appropriately. The latter would be my preferred option, e.g.,

struct ContinuousStageRungeKuttaMethod{T, MatT <: AbstractMatrix{T}} <: RootedTrees.AbstractTimeIntegrationMethod
    A::MatT
end

Base.eltype(::ContinuousStageRungeKuttaMethod{T}) where {T} = T


# TODO: bseries(ros::RosenbrockMethod)
# should create a lazy version, optionally a memoized one

Expand Down Expand Up @@ -2016,4 +2049,73 @@ function equivalent_trees(tree)
return equivalent_trees_set
end




# This function generates a polynomial
# A_{t,z} = [t,t^2/2,..., t^s/s]*M*[1, z, ..., z^(s-1)]^T
# for a given square matrix M of dimension s and chars 't' and 'z'.
function PolynomialA(M,t,z)
s = size(M,1)
# we need variables to work with
variable1 = Sym(t)
variable2 = Sym(z)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I do not really like that this introduces a hard dependency on SymPy.jl here (which is not covered in the import statements at the top of this file or Project.toml). If I remember correctly, @ketch reported some installation issues with some Julia packages build on Python packages. If possible, I would like to avoid such issues.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since we work only with polynomials and just need to integrate, in principle everything can be done by working directly with the coefficients. This will make the code a bit harder to read but may also be faster.

# conjugate the variable 1, provided that this will be the variable
# of the left polynomial
variable1 = conjugate(variable1)
# generate the components of the polynomial with powers of t
poli_z = Array{SymPy.Sym}(undef, s)
for i in 1:s
poli_z[i] = variable2^(i-1)
end
# generate the components of the polynomial with powers of z
poli_t = Array{SymPy.Sym}(undef, s)
for i in 1:s
poli_t[i] = (1 // i)*(variable1^i)
end
# multiply matrix times vector
result = M * poli_z
# use dot product for the two vectors
result = dot(poli_t,result)
return result
end


"""
elementary_differentials_csrk(M,tree)

This function calculates the CSRK elementary differential for a given
square matrix 'M' and a given RootedTree.

"""
function elementary_differentials_csrk(M,rootedtree)
# we'll work with the level sequence
tree = rootedtree.level_sequence
m = maximum(tree)
l = length(tree)
# create the variables called 'xi' for 1 <= i <= m
variables = []
for i in 1:m
var_name = "x$i"
var = Sym(var_name)
push!(variables, var)
end
inverse_counter = l-1
# stablish initial integrand, which is the rightmost leaf (last node of the level sequence)
if l > 1
integrand = integrate(PolynomialA(M,variables[tree[end]-1],variables[tree[end]]),(variables[tree[end]],0,1))
else
# if the RootedTree is [1] or [], the elementary differential will be 1.
return 1
end
while inverse_counter > 1
pseudo_integrand = PolynomialA(M,variables[tree[inverse_counter]-1],variables[tree[inverse_counter]])*integrand
integrand = integrate(pseudo_integrand,(variables[tree[inverse_counter]],0,1))
inverse_counter -= 1
end
# multiply for the Basis_Polynomial, i.e. the Polynomial B
# return the integral with respect to x1.
return integrate(PolynomialA(M,1,variables[1])*integrand,(variables[1],0,1))
end

end # module