Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the capability to check whether a B-series is energy-preserving #117

Merged
merged 56 commits into from
Jun 13, 2023
Merged
Changes from 1 commit
Commits
Show all changes
56 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
d6b31aa
requested changes to the PR: added function for bseries 'BSeries_Ener…
Sondar74 May 25, 2023
a33897f
generalized the function 'bush_detector_ for any iniital index. Chang…
Sondar74 May 27, 2023
5a370b6
added the files 'examples.jl' with four examples; also added the file…
Sondar74 May 27, 2023
dadfcd3
simplified the function 'symfact_normalization', and realized that th…
Sondar74 May 27, 2023
19226c8
added docstrings. Deleted the function 'BMinus'. Eliminated the varia…
Sondar74 May 30, 2023
729d51e
added docstrings and modified the CSRK.jl. Added tests to the runtest…
Sondar74 May 30, 2023
76b2c75
added Energy_Preserving.jl to src
Sondar74 May 30, 2023
0176998
fixed first function in CSRK.jl
Sondar74 May 30, 2023
e2c0b5a
added a module in Energy_Preserving.jl. In the file 'BSeries.jl' I ad…
Sondar74 Jun 1, 2023
9cf5463
Merge branch 'main' into Energy_Preserving
ranocha Jun 1, 2023
7e3241b
added 'include("Energy_Preserving.jl")' to the BSeries file, and wrot…
Sondar74 Jun 1, 2023
cd641fd
Committing local changes
Sondar74 Jun 1, 2023
dc4f53d
Merge branch 'Energy_Preserving' of github.com:Sondar74/BSeries.jl in…
Sondar74 Jun 1, 2023
a2ac846
added the code of Energy_Preserving to src/BSeries.jl
Sondar74 Jun 2, 2023
f94a357
removed files from this branch: examples.jl, Energy_Preserving.jl and…
Sondar74 Jun 3, 2023
47bc3ac
removed the file 'src/Energ_Preserving.jl'
Sondar74 Jun 3, 2023
aeab865
Modified the docstring and added main reference for 'is_energy_preser…
Sondar74 Jun 3, 2023
6009ced
added the line 'export is_energy_preserving, OrderMethod'
Sondar74 Jun 3, 2023
e492566
removed Revise from the file 'Project.toml'
Sondar74 Jun 4, 2023
2df8690
changes in BSeries.jl: changed the name of 'OrderMethod', added the f…
Sondar74 Jun 4, 2023
fb05541
removed Combinatorics from dependencies
Sondar74 Jun 4, 2023
ccda171
Modifications so that the 5 tests passed
Sondar74 Jun 4, 2023
ea448af
Merge branch 'main' into Energy_Preserving
ranocha Jun 5, 2023
2a1f36e
changed the version of Combinatorics to "1"
Jun 5, 2023
41d0378
Merge branch 'main' into Energy_Preserving
ranocha Jun 5, 2023
7facfe9
Update src/BSeries.jl
Sondar74 Jun 5, 2023
a127182
Update src/BSeries.jl
Sondar74 Jun 5, 2023
ee46aac
moved the line of Combinatorics alphabetically
Jun 5, 2023
fd0c5c5
changed docstrngs in secondary functions for comments
Jun 6, 2023
967b381
removed comments from renormalize_bseries and used 'eachindex'
Sondar74 Jun 6, 2023
a10e026
several modifications, but the tests still passed. changed dictionari…
Sondar74 Jun 6, 2023
22009c0
Merge branch 'Energy_Preserving' of github.com:Sondar74/BSeries.jl in…
Sondar74 Jun 6, 2023
9edcbe9
changed dictionaries for arrays, used the word 'ribs' for some variab…
Sondar74 Jun 6, 2023
a0dc442
eliminated the function 'get_permutations'
Sondar74 Jun 6, 2023
fbdddd1
modified the function 'renormalize_bseries!' so that it receives a bs…
Sondar74 Jun 7, 2023
e473c5b
added the 'max_order' parameter to 'energ_preserving_order(A,b,max_or…
Sondar74 Jun 7, 2023
f7b6cb9
modified the function 'energy_preserving_order' so that its input is …
Sondar74 Jun 7, 2023
c81e645
added references to RK methods in the tests
Sondar74 Jun 8, 2023
c24c174
changed 'spine/ribs' for 'trunk/branch'
Sondar74 Jun 8, 2023
386d330
modified the function 'is_energy_preserving' to iterate over each ord…
Sondar74 Jun 8, 2023
950efb4
added comments to the new functino 'low_order_energy_preserving'
Sondar74 Jun 8, 2023
8dd1138
added 'assert'. Deleted 'add_one_to_the_left' function and fixed orto…
Sondar74 Jun 11, 2023
e8bb63a
put the function 'get_trunk' inside the function 'get_branches', so t…
Sondar74 Jun 11, 2023
c25c64c
modified the function 'equivalent_trees': now its faster and the func…
Sondar74 Jun 12, 2023
f946d11
changed variables names in the function 'equivalent_trees'
Sondar74 Jun 12, 2023
bfd76b5
modified the 'runtest' file for higher orders for the AVF method
Sondar74 Jun 13, 2023
5ffbcd5
modified the comments about the function 'low_order_energy_preserving'
Sondar74 Jun 13, 2023
497514b
format
ranocha Jun 13, 2023
a47b44d
Merge branch 'Energy_Preserving' of github.com:Sondar74/BSeries.jl in…
ranocha Jun 13, 2023
8398ca1
format
ranocha Jun 13, 2023
29691b3
fix typo
ranocha Jun 13, 2023
f755901
Merge branch 'main' into Energy_Preserving
ranocha Jun 13, 2023
63b3091
format
ranocha Jun 13, 2023
f21b42f
Merge branch 'Energy_Preserving' of github.com:Sondar74/BSeries.jl in…
ranocha Jun 13, 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
228 changes: 136 additions & 92 deletions src/BSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1485,9 +1485,9 @@ This code is based on the Theorem 2 of
[DOI: 10.1007/s10208-010-9073-1](https://link.springer.com/article/10.1007/s10208-010-9073-1)
"""
function is_energy_preserving(map_series)
#check if the first element is map or flow
@assert typeof(map_series) == TruncatedBSeries{RootedTree{Int64, Vector{Int64}}, Rational{Int64}}
flow_series = modified_equation(map_series)
#check if the first element is map or flow
@assert flow_series[rootedtree([1])] == 1
Sondar74 marked this conversation as resolved.
Show resolved Hide resolved
#renormalize by multiplying by symmetry factor
renormalize_bseries!(flow_series)
#save all the coefficients in an array: it is easier
Expand Down Expand Up @@ -1556,6 +1556,46 @@ function low_order_energy_preserving(trees, coefficients)
end


"""
renormalize_bseries!(series)

This function receives a 'TruncatedBSeries' and returns
a new 'TruncatedBseries' whose coefficients are divided
by the symmetry of the corresponding tree.
"""
function renormalize_bseries!(series)
trees_array = collect(keys(series))
l = length(trees_array)
for i in 1:l
factor = symmetry(trees_array[i])
series[trees_array[i]] = series[trees_array[i]]*(1//factor)
end
end
ranocha marked this conversation as resolved.
Show resolved Hide resolved

"""
energy_preserving_order(rk,max_order)
This function tells up to what order a method 'rk' is Energy Preserving.
It requires a 'max_order' so that it doesnt blow up if the order up to which
the method is energy_preserving is too big or infinite.
"""
function energy_preserving_order(rk,max_order)
p = 0
not_energy_preserving = false
while not_energy_preserving == false
#make sure not to pass the max_order
if p > max_order
return max_order
end
#generate bseries
if is_energy_preserving(rk,p+1) == false
not_energy_preserving = true
end
p = p + 1
end
return p-1
end


# get_branches()

#It is not enough to generate the branches and swap them. The level_sequences must be modified
Expand Down Expand Up @@ -1610,6 +1650,7 @@ end
#for a given tree (the input also in level-sequence form).
#branches
function rightmost_energy_preserving_tree(a::Vector{Int})
#println("no_adjoint: ",a)
#we want to generate all the branches with respect to the rightmost trunk
branches = get_branches(a)
#we obtain the number of branches the right-most trunk has
Expand All @@ -1624,6 +1665,7 @@ function rightmost_energy_preserving_tree(a::Vector{Int})
last_jplus1_occurrence = findlast(x -> x == j+1, energy_preserving_partner)
energy_preserving_partner = vcat(energy_preserving_partner[1:last_j_occurrence], branches[j], energy_preserving_partner[last_jplus1_occurrence:end])
end
#println("adjoint: ",energy_preserving_partner)
energy_preserving_partner = canonicalarray(energy_preserving_partner)
return energy_preserving_partner
end
Expand All @@ -1640,59 +1682,101 @@ end

#This function generates all the equivalent trees
#for a given level_sequence

function equivalent_trees(array)
tree = rootedtree(array)
l = length(array)
#we get the permutations of the array
superarray = collect(permutations(array))
lperm = length(superarray)
#eliminate all the arrays except for those for which the first element is equal to 1
for i in reverse(1:lperm)
if superarray[i][1] != 1
splice!(superarray, i)
end
end
lperm = length(superarray)
#now, check if the levelsequence is the level sequence represents a RootedTree:
#the condition is that each number m cannot be followed by a number n such that
#n-m > 1. If that's the case, eliminate this array with 'splice!'
RTree_array = true
for i in reverse(1:lperm)
for j in 1:(l-1)
if (superarray[i][j+1] - superarray[i][j]) > 1
RTree_array = false
end
end
if RTree_array == false
splice!(superarray, i)
end
RTree_array = true
end
lperm = length(superarray)
for i in reverse(1:lperm)
onetree = rootedtree(superarray[i])
isthesame_tree = onetree == tree
if isthesame_tree == false
splice!(superarray, i)
end
end
repeated_tree = false
lperm = length(superarray)
#we delete any repeated tree
for i in reverse(1:lperm)
for j in 1:(i-1)
if superarray[i] == superarray[j]
repeated_tree = true
function equivalent_trees(tree)
equiv_energy_preserving_set = []
l = length(tree)
for i in 1:l-1
# is this node a leaf
if tree[i+1] <= tree[i]
# make a copy of 'tree' to cut and paste with
graft = copy(tree)
leaf_value = graft[i]
#println("leaf: ", leaf_value)
#println("new tree: ", graft)
# initialize the length of the subtree we are currently working with
length_subtree = l
# initialize the initial component from which we will start looping
# in order to cut and paste every subtree at the right of the array
initial_component = 1
left_index = 0
right_index = 0
# loop for sending subtrees to the right
new_j = i
for j in 2:leaf_value
#println("looking for: ", j)
#make sure that there are more than one 'j'
#in the current level sequence
j_counter = 0
for node in initial_component:l
if graft[node] == j
j_counter += 1
end
end
if j_counter == 1
continue
end
# Find the nearest 'j' on the left side
for k in new_j:-1:initial_component
if graft[k] == j
left_index = k
break
end
end
# Find the nearest 'j' on the right side
right_flag = false
for k in new_j:l
if graft[k] == j
right_flag = true
right_index = k
break
end
end
if right_flag == false
right_index = l+1
end
if right_index == 0
continue
end
#println(left_index,"---",right_index)
# get the subtree to be transplanted
if left_index == right_index
plantout_subtree = [j]
#println(plantout_subtree)
# remove these componentes from the original tree
splice!(graft, left_index)
else
plantout_subtree = graft[left_index:right_index-1]
#println(plantout_subtree)
# remove these componentes from the original tree
splice!(graft, left_index:right_index-1)
end
length_subtree = length(plantout_subtree)
initial_component = l - length_subtree + 1
#println("initial component: ", initial_component)
# add the componentes to the right
graft = vcat(graft, plantout_subtree)
#println("graft: ", graft)
#new_j for finding the reference (red in paper)
new_j = l + new_j - length_subtree - left_index + 1
#println("new_j: ", new_j)
if new_j == l
break
end
end
end
if repeated_tree == true
splice!(superarray, i)
# obtain the energy_preserving_partner of 'graft'
# add this to 'equiv__energy_preserving_set'
#push!(equiv__energy_preserving_set, rightmost_energy_preserving_tree(graft))
push!(equiv_energy_preserving_set, graft)
#println(equiv_energy_preserving_set)
#println("-----------------------")
end
repeated_tree = false
end

return superarray
#include the original tree
push!(equiv_energy_preserving_set, tree)
#sort!(equiv_energy_preserving_set)
#eliminate repeated
unique!(equiv_energy_preserving_set)
return equiv_energy_preserving_set
end


Expand All @@ -1701,46 +1785,6 @@ function num_branches(a)
return k - 1
end

"""
renormalize_bseries!(series)

This function receives a 'TruncatedBSeries' and returns
a new 'TruncatedBseries' whose coefficients are divided
by the symmetry of the corresponding tree.
"""
function renormalize_bseries!(series)
trees_array = collect(keys(series))
l = length(trees_array)
for i in 1:l
factor = symmetry(trees_array[i])
series[trees_array[i]] = series[trees_array[i]]*(1//factor)
end
end

"""
energy_preserving_order(rk,max_order)
This function tells up to what order a method 'rk' is Energy Preserving.
It requires a 'max_order' so that it doesnt blow up if the order up to which
the method is energy_preserving is too big or infinite.
"""
function energy_preserving_order(rk,max_order)
p = 0
not_energy_preserving = false
while not_energy_preserving == false
#make sure not to pass the max_order
if p > max_order
return max_order
end
#generate bseries
if is_energy_preserving(rk,p+1) == false
not_energy_preserving = true
end
p = p + 1
end
return p-1
end


# energy_preserving_trees_test(trees,coefficients)

#this function is the application of Theorem 2 of the paper
Expand Down