Skip to content

Commit

Permalink
Move WeylGroup serialization to src
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Dec 20, 2024
1 parent 43d9fb8 commit 65a82d9
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 100 deletions.
42 changes: 0 additions & 42 deletions experimental/LieAlgebras/src/serialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,45 +240,3 @@ end
function load_type_params(s::DeserializerState, ::Type{<:LieAlgebraModuleElem})
return load_typed_object(s)
end

###############################################################################
#
# Weyl groups
#
###############################################################################

@register_serialization_type WeylGroup uses_id

function save_object(s::SerializerState, W::WeylGroup)
save_data_dict(s) do
save_typed_object(s, root_system(W), :root_system)
end
end

function load_object(s::DeserializerState, ::Type{WeylGroup})
R = load_typed_object(s, :root_system)
return weyl_group(R)
end

@register_serialization_type WeylGroupElem uses_params

function save_object(s::SerializerState, x::WeylGroupElem)
save_object(s, word(x))
end

function load_object(s::DeserializerState, ::Type{WeylGroupElem}, W::WeylGroup)
return W(load_object(s, Vector, UInt8); normalize=false)
end

function save_type_params(s::SerializerState, x::WeylGroupElem)
save_data_dict(s) do
save_object(s, encode_type(typeof(x)), :name)
parent_x = parent(x)
parent_ref = save_as_ref(s, parent_x)
save_object(s, parent_ref, :params)
end
end

function load_type_params(s::DeserializerState, ::Type{WeylGroupElem})
return load_typed_object(s)
end
58 changes: 0 additions & 58 deletions experimental/LieAlgebras/test/WeylGroup-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,62 +118,4 @@
end
end
end

@testset "Serialization" begin
mktempdir() do path
@testset "simple saving and loading" begin
W = weyl_group((:A, 2), (:B, 4))

test_save_load_roundtrip(path, W) do loaded
# nothing, cause `W === loaded` anyway
end

x = rand(W)
test_save_load_roundtrip(path, x) do loaded
@test parent(loaded) === W
@test word(loaded) == word(x)
end

test_save_load_roundtrip(path, gens(W)) do loaded
@test length(loaded) == ngens(W)
@test all(
word(loaded[i]) == word(gen(W, i)) for i in 1:ngens(W)
)
end
end

@testset "cyclic reference between R and W survives" begin
Oscar.reset_global_serializer_state()

R_filename = joinpath(path, "R.mrdi")
W_filename = joinpath(path, "W.mrdi")

R = root_system(:D, 5)
W = weyl_group(R)

save(R_filename, R)
save(W_filename, W)

Oscar.reset_global_serializer_state()

loaded_R = load(R_filename)
loaded_W = load(W_filename)

@test loaded_R === root_system(loaded_W)
@test loaded_W === weyl_group(loaded_R)

loaded_R = loaded_W = nothing # unset all references

Oscar.reset_global_serializer_state()

loaded_W = load(W_filename)
loaded_R = load(R_filename)

@test loaded_R === root_system(loaded_W)
@test loaded_W === weyl_group(loaded_R)

loaded_R = loaded_W = nothing # unset all references
end
end
end
end
42 changes: 42 additions & 0 deletions src/Serialization/LieTheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,45 @@ function load_type_params(
)
return load_typed_object(s)
end

###############################################################################
#
# Weyl groups
#
###############################################################################

@register_serialization_type WeylGroup uses_id

function save_object(s::SerializerState, W::WeylGroup)
save_data_dict(s) do
save_typed_object(s, root_system(W), :root_system)
end
end

function load_object(s::DeserializerState, ::Type{WeylGroup})
R = load_typed_object(s, :root_system)
return weyl_group(R)
end

@register_serialization_type WeylGroupElem uses_params

function save_object(s::SerializerState, x::WeylGroupElem)
save_object(s, word(x))
end

function load_object(s::DeserializerState, ::Type{WeylGroupElem}, W::WeylGroup)
return W(load_object(s, Vector, UInt8); normalize=false)
end

function save_type_params(s::SerializerState, x::WeylGroupElem)
save_data_dict(s) do
save_object(s, encode_type(typeof(x)), :name)
parent_x = parent(x)
parent_ref = save_as_ref(s, parent_x)
save_object(s, parent_ref, :params)
end
end

function load_type_params(s::DeserializerState, ::Type{WeylGroupElem})
return load_typed_object(s)
end
56 changes: 56 additions & 0 deletions test/Serialization/LieTheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,61 @@
end
end
end

@testset "WeylGroup" begin
@testset "simple saving and loading" begin
W = weyl_group((:A, 2), (:B, 4))

test_save_load_roundtrip(path, W) do loaded
# nothing, cause `W === loaded` anyway
end

x = rand(W)
test_save_load_roundtrip(path, x) do loaded
@test parent(loaded) === W
@test word(loaded) == word(x)
end

test_save_load_roundtrip(path, gens(W)) do loaded
@test length(loaded) == ngens(W)
@test all(
word(loaded[i]) == word(gen(W, i)) for i in 1:ngens(W)
)
end
end

@testset "cyclic reference between R and W survives" begin
Oscar.reset_global_serializer_state()

R_filename = joinpath(path, "R.mrdi")
W_filename = joinpath(path, "W.mrdi")

R = root_system(:D, 5)
W = weyl_group(R)

save(R_filename, R)
save(W_filename, W)

Oscar.reset_global_serializer_state()

loaded_R = load(R_filename)
loaded_W = load(W_filename)

@test loaded_R === root_system(loaded_W)
@test loaded_W === weyl_group(loaded_R)

loaded_R = loaded_W = nothing # unset all references

Oscar.reset_global_serializer_state()

loaded_W = load(W_filename)
loaded_R = load(R_filename)

@test loaded_R === root_system(loaded_W)
@test loaded_W === weyl_group(loaded_R)

loaded_R = loaded_W = nothing # unset all references
end
end
end
end

0 comments on commit 65a82d9

Please sign in to comment.