Skip to content

Commit

Permalink
[FTheoryTools] Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
HereAround committed Jul 19, 2024
1 parent 133d1c4 commit cb8c1f6
Showing 1 changed file with 24 additions and 83 deletions.
107 changes: 24 additions & 83 deletions experimental/FTheoryTools/src/Serialization/weierstrass_models.jl
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
@register_serialization_type WeierstrassModel uses_params



###########################################################################
# This function saves the types of the data that define a Weierstrass model
###########################################################################

function save_type_params(s::SerializerState, w::WeierstrassModel)
save_data_dict(s) do
save_object(s, encode_type(WeierstrassModel), :name)
base = base_space(w)
ambient = ambient_space(w)
weierstrass_polynomial_ring = parent(weierstrass_polynomial(w))
base, ambient, wp_ring = base_space(w), ambient_space(w), parent(weierstrass_polynomial(w))

save_data_dict(s, :params) do
if serialize_with_id(base)
parent_ref = save_as_ref(s, base)
save_object(s, parent_ref, :base_space)
else
save_typed_object(s, base, :base_space)
end

if serialize_with_id(ambient)
parent_ref = save_as_ref(s, ambient)
save_object(s, parent_ref, :ambient_space)
else
save_typed_object(s, ambient, :ambient_space)
for (obj, key) in [(base, :base_space), (ambient, :ambient_space), (wp_ring, :weierstrass_polynomial_ring)]
if serialize_with_id(obj)
save_object(s, save_as_ref(s, obj), key)
else
save_typed_object(s, obj, key)
end
end

if serialize_with_id(weierstrass_polynomial_ring)
parent_ref = save_as_ref(s, weierstrass_polynomial_ring)
save_object(s, parent_ref, :weierstrass_polynomial_ring)
else
save_typed_object(s, weierstrass_polynomial_ring, :weierstrass_polynomial_ring)
end

end
end
end


###########################################################################
# This function loads the types of the data that define a Weierstrass model
###########################################################################
Expand All @@ -51,85 +34,43 @@ function load_type_params(s::DeserializerState, ::Type{<: WeierstrassModel})
)
end


#########################################
# This function saves a Weierstrass model
#########################################

function save_object(s::SerializerState, w::WeierstrassModel)
# Currently, only serialize Tate models with toric defining data
@req base_space(w) isa NormalToricVariety "Currently, we only serialize Weierstrass models defined over a toric base space"
@req ambient_space(w) isa NormalToricVariety "Currently, we only serialize Weierstrass models defined within a toric ambient space"
@req base_space(w) isa NormalToricVariety "We only serialize Weierstrass models defined over a toric base space"
@req ambient_space(w) isa NormalToricVariety "We only serialize Weierstrass models defined within a toric ambient space"

# Save information
save_data_dict(s) do

# Save explicit_model_sections(w)
if !is_empty(explicit_model_sections(w))
save_typed_object(s, explicit_model_sections(w), :explicit_model_sections)
end

# Save defining_section_parametrization(w)
if !is_empty(defining_section_parametrization(w))
save_typed_object(s, defining_section_parametrization(w), :defining_section_parametrization)
end

# save_defining_classes(w)
if !is_empty(defining_classes(w))
save_typed_object(s, defining_classes(w), :defining_classes)
for (data, key) in [
(explicit_model_sections(w), :explicit_model_sections),
(defining_section_parametrization(w), :defining_section_parametrization),
(defining_classes(w), :defining_classes)
]
!isempty(data) && save_typed_object(s, data, key)
end

# Weierstrass polynomial
save_object(s, weierstrass_polynomial(w), :weierstrass_polynomial)

# Boolean values, that are always known for Weierstrass models
save_data_array(s, :boolean_data) do
save_object(s, is_partially_resolved(w))
end
end
end


#########################################
# This function loads a Weierstrass model
#########################################

function load_object(s::DeserializerState, ::Type{<: WeierstrassModel}, params::Tuple{NormalToricVariety, NormalToricVariety, MPolyDecRing})

# Extract base and ambient space
base_space = params[1]
ambient_space = params[2]

# Extract Weierstrass polynomial
pw = load_object(s, MPolyDecRingElem, params[3], :weierstrass_polynomial)

# Extract explicit_model_sections
if haskey(s, :explicit_model_sections)
explicit_model_sections = load_typed_object(s, :explicit_model_sections)
else
explicit_model_sections = Dict{String, MPolyRingElem}()
end

# Extract defining_section_parametrization
if haskey(s, :defining_section_parametrization)
defining_section_parametrization = load_typed_object(s, :defining_section_parametrization)
else
defining_section_parametrization = Dict{String, MPolyRingElem}()
end

# Extract defining_classes
if haskey(s, :defining_classes)
defining_classes = load_typed_object(s, :defining_classes)
else
defining_classes = Dict{String, ToricDivisorClass}()
end

# Construct the model
base_space, ambient_space, wp_ring = params
pw = load_object(s, MPolyDecRingElem, wp_ring, :weierstrass_polynomial)
explicit_model_sections = haskey(s, :explicit_model_sections) ? load_typed_object(s, :explicit_model_sections) : Dict{String, MPolyRingElem}()
defining_section_parametrization = haskey(s, :defining_section_parametrization) ? load_typed_object(s, :defining_section_parametrization) : Dict{String, MPolyRingElem}()
defining_classes = haskey(s, :defining_classes) ? load_typed_object(s, :defining_classes) : Dict{String, ToricDivisorClass}()
model = WeierstrassModel(explicit_model_sections, defining_section_parametrization, pw, base_space, ambient_space)
model.defining_classes = defining_classes

# Set boolean attributes
bools = load_object(s, Vector, Bool, :boolean_data)
set_attribute!(model, :partially_resolved, bools[1])

# Return the loaded model
set_attribute!(model, :partially_resolved, load_object(s, Vector{Bool}, :boolean_data)[1])
return model
end

0 comments on commit cb8c1f6

Please sign in to comment.