Skip to content

Commit

Permalink
✨ Implement copy for all structures
Browse files Browse the repository at this point in the history
  • Loading branch information
ronisbr committed Jun 12, 2024
1 parent a64422d commit bd0a680
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/SatelliteToolboxSgp4.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ using SatelliteToolboxBase

@reexport using SatelliteToolboxTle

import Base: copy

############################################################################################
# Types #
############################################################################################
Expand All @@ -32,6 +34,7 @@ const _Y = string(crayon"yellow bold")
# Includes #
############################################################################################

include("copy.jl")
include("sgp4_model.jl")
include("tle.jl")

Expand Down
46 changes: 46 additions & 0 deletions src/copy.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## Description #############################################################################
#
# Implement the copy for the structures related to the SGP4 propagator.
#
############################################################################################

# Define the copy function for the structure `Sgp4DeepSpace`.
@generated function Base.copy(sgp4ds::Sgp4DeepSpace{T}) where T<:Number
fields = fieldnames(sgp4ds)

expressions = Expr[:(new_sgp4ds = Sgp4DeepSpace{$T}())]
sizehint!(expressions, length(fields) + 1)

@inbounds for f in fields
push!(expressions, :(new_sgp4ds.$f = sgp4ds.$f))
end

return :(
$(expressions...);
return new_sgp4ds
)
end

# Define the copy function for the structure `Sgp4Propagator`.
@generated function Base.copy(
sgp4d::Sgp4Propagator{Tepoch, T}
) where {Tepoch<:Number, T<:Number}
fields = fieldnames(sgp4d)

expressions = Expr[:(new_sgp4d = Sgp4Propagator{$Tepoch, $T}())]
sizehint!(expressions, length(fields) + 1)

@inbounds for f in fields
if f != :sgp4ds
push!(expressions, :(new_sgp4d.$f = sgp4d.$f))
else
# `sgp4ds` is the only mutable element inside this structure.
push!(expressions, :(new_sgp4d.$f = copy(sgp4d.$f)))
end
end

return :(
$(expressions...);
return new_sgp4d
)
end
20 changes: 20 additions & 0 deletions src/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,24 @@ PrecompileTools.@compile_workload begin
max_iterations = 1,
)
end

# == Structure Copying =================================================================

sgp4d = sgp4_init(
tle"""
AMAZONIA 1
1 47699U 21015A 23083.68657856 -.00000044 10000-8 43000-4 0 9990
2 47699 98.4304 162.1097 0001247 136.2017 223.9283 14.40814394108652""";
sgp4c = sgp4c_wgs84
)
copy(sgp4d)

sgp4d_f32 = sgp4_init(
tle"""
AMAZONIA 1
1 47699U 21015A 23083.68657856 -.00000044 10000-8 43000-4 0 9990
2 47699 98.4304 162.1097 0001247 136.2017 223.9283 14.40814394108652""";
sgp4c = sgp4c_wgs84_f32
)
copy(sgp4d_f32)
end
43 changes: 43 additions & 0 deletions test/copy.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## Description #############################################################################
#
# Test structure copying.
#
############################################################################################

@testset "Sgp4Propagator and Spg4DeepSpace" begin
for sgp4c in (sgp4c_wgs84, sgp4c_wgs84_f32)

sgp4d = sgp4_init(
tle"""
1 24208U 96044A 06177.04061740 -.00000094 00000-0 10000-3 0 1600
2 24208 3.8536 80.0121 0026640 311.0977 48.3000 1.00778054 36119
""";
sgp4c = sgp4c
)

# We need to propagate the orbit to initialize all the internal terms related to the
# deep space structure.
sgp4!(sgp4d, 0)

new_sgp4d = copy(sgp4d)

@test typeof(new_sgp4d) == typeof(sgp4d)

for f in fieldnames(typeof(sgp4d))
if f != :sgp4ds
@test getfield(new_sgp4d, f) == getfield(sgp4d, f)
end
end

for f in fieldnames(typeof(sgp4d.sgp4ds))
@test getfield(new_sgp4d.sgp4ds, f) == getfield(sgp4d.sgp4ds, f)
end

# Test if both structures does not share the same memory region.
new_sgp4d.epoch = 100
@test new_sgp4d.epoch != sgp4d.epoch

new_sgp4d.sgp4ds.atime = 123
@test new_sgp4d.sgp4ds.atime != sgp4d.sgp4ds.atime
end
end
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ end
@testset "SGP4 TLEs" verbose = true begin
include("./tle.jl")
end

@testset "Copy Structures" verbose = true begin
include("./copy.jl")
end

0 comments on commit bd0a680

Please sign in to comment.