Skip to content

Commit

Permalink
Update TPM.jl
Browse files Browse the repository at this point in the history
Change the arguments of the struct to store the TPM result from `time_begin` and `time_end ` to `times_to_save`.
  • Loading branch information
MasanoriKanamaru committed Nov 21, 2023
1 parent ae414e8 commit c43b907
Showing 1 changed file with 56 additions and 64 deletions.
120 changes: 56 additions & 64 deletions src/TPM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,54 +235,49 @@ Output data format for `SingleTPM`
- `torque` : Thermal torque on the asteroid [N ⋅ m]
## Saved only at the time steps desired by the user
- `time_begin` : Time to start storing temperature
- `time_end` : Time to finish storing temperature
- `surf_temp` : Surface temperature, a matrix in size of `(Ns, Nt)`.
- `times_to_save` : Timesteps to save temperature
- `surf_temp` : Surface temperature, a matrix in size of `(Ns, Nt)`.
- `Ns` : Number of faces
- `Nt` : Number of time steps to save surface temperature
- `face_temp` : Temperature as a function of depth and time, `Dict` with face ID as key and a matrix `(Nz, Nt)` as an entry.
- `face_temp` : Temperature as a function of depth and time, `Dict` with face ID as key and a matrix `(Nz, Nt)` as an entry.
- `Nz` : The number of the depth nodes
- `Nt` : The number of time steps to save temperature
"""
struct SingleTPMResult
E_in ::Vector{Float64}
E_out ::Vector{Float64}
E_cons ::Vector{Union{Float64, Missing}}
force ::Vector{SVector{3, Float64}}
torque ::Vector{SVector{3, Float64}}

time_begin ::Float64
time_end ::Float64
surf_temp ::Matrix{Float64}
face_temp ::Dict{Int, Matrix{Float64}}
E_in ::Vector{Float64}
E_out ::Vector{Float64}
E_cons ::Vector{Union{Float64, Missing}}
force ::Vector{SVector{3, Float64}}
torque ::Vector{SVector{3, Float64}}

times_to_save ::Vector{Float64}
surf_temp ::Matrix{Float64}
face_temp ::Dict{Int, Matrix{Float64}}
end


"""
Outer constructor of `SingleTPMResult`
# Arguments
- `stpm` : Thermophysical model for a single asteroid
- `ephem` : Ephemerides
- `time_begin` : Time to start storing temperature
- `time_end` : Time to finish storing temperature
- `face_ID` : Indices of faces at which you want to save temperature distribution in depth direction
- `stpm` : Thermophysical model for a single asteroid
- `ephem` : Ephemerides
- `times_to_save` : Timesteps to save temperature
- `face_ID` : Face indices where to save subsurface temperature
"""
function SingleTPMResult(stpm::SingleTPM, ephem, time_begin::Real, time_end::Real, face_ID::Vector{Int})
function SingleTPMResult(stpm::SingleTPM, ephem, times_to_save::Vector{Float64}, face_ID::Vector{Int})
E_in = zeros(length(ephem.time))
E_out = zeros(length(ephem.time))
E_cons = Vector{Union{Float64, Missing}}(missing, length(ephem.time))
force = zeros(SVector{3, Float64}, length(ephem.time))
torque = zeros(SVector{3, Float64}, length(ephem.time))

Nt_save = count(@. time_begin ephem.time < time_end) # Number of time steps to save temperature

surf_temp = zeros(length(stpm.shape.faces), Nt_save)
surf_temp = zeros(length(stpm.shape.faces), length(times_to_save))
face_temp = Dict{Int, Matrix{Float64}}(
nₛ => zeros(stpm.thermo_params.Nz, Nt_save) for nₛ in face_ID
nₛ => zeros(stpm.thermo_params.Nz, length(times_to_save)) for nₛ in face_ID
)

return SingleTPMResult(E_in, E_out, E_cons, force, torque, time_begin, time_end, surf_temp, face_temp)
return SingleTPMResult(E_in, E_out, E_cons, force, torque, times_to_save, surf_temp, face_temp)
end


Expand All @@ -305,16 +300,15 @@ end
Outer constructor of `BinaryTPMResult`
# Arguments
- `btpm` : Thermophysical model for a binary asteroid
- `ephem` : Ephemerides
- `time_begin` : Time to start storing temperature (Common to both the primary and the secondary)
- `time_end` : Time to finish storing temperature (Common to both the primary and the secondary)
- `face_ID_pri` : Face indices at which you want to save temperature as a function of depth for the primary
- `face_ID_sec` : Face indices at which you want to save temperature as a function of depth for the secondary
- `btpm` : Thermophysical model for a binary asteroid
- `ephem` : Ephemerides
- `times_to_save` : Timesteps to save temperature (Common to both the primary and the secondary)
- `face_ID_pri` : Face indices at which you want to save temperature as a function of depth for the primary
- `face_ID_sec` : Face indices at which you want to save temperature as a function of depth for the secondary
"""
function BinaryTPMResult(btpm::BinaryTPM, ephem, time_begin::Real, time_end::Real, face_ID_pri::Vector{Int}, face_ID_sec::Vector{Int})
result_pri = SingleTPMResult(btpm.pri, ephem, time_begin, time_end, face_ID_pri)
result_sec = SingleTPMResult(btpm.sec, ephem, time_begin, time_end, face_ID_sec)
function BinaryTPMResult(btpm::BinaryTPM, ephem, times_to_save::Vector{Float64}, face_ID_pri::Vector{Int}, face_ID_sec::Vector{Int})
result_pri = SingleTPMResult(btpm.pri, ephem, times_to_save, face_ID_pri)
result_sec = SingleTPMResult(btpm.sec, ephem, times_to_save, face_ID_sec)

return BinaryTPMResult(result_pri, result_sec)
end
Expand All @@ -331,7 +325,7 @@ Save the results of TPM at the time step `nₜ` to `result`.
- `ephem` : Ephemerides
- `nₜ` : Time step to save data
"""
function update_TPM_result!(result::SingleTPMResult, stpm::SingleTPM, ephem, nₜ::Integer)
function update_TPM_result!(result::SingleTPMResult, stpm::SingleTPM, ephem, nₜ::Integer, times_to_save::Vector{Float64})
result.E_in[nₜ] = energy_in(stpm)
result.E_out[nₜ] = energy_out(stpm)
result.force[nₜ] = stpm.force
Expand All @@ -350,9 +344,8 @@ function update_TPM_result!(result::SingleTPMResult, stpm::SingleTPM, ephem, n
result.E_cons[nₜ] = ΣE_out / ΣE_in
end

if result.time_begin ephem.time[nₜ] < result.time_end # if you want to save temperature at this time step
nₜ_offset = count(@. ephem.time < result.time_begin) # Index-offset before storing temperature
nₜ_save = nₜ - nₜ_offset
if t in times_to_save # In the step of saving temperature
nₜ_save = findfirst(isequal(t), times_to_save)

result.surf_temp[:, nₜ_save] .= surface_temperature(stpm)

Expand All @@ -374,9 +367,9 @@ Save the results of TPM at the time step `nₜ` to `result`.
- `ephem` : Ephemerides
- `nₜ` : Time step
"""
function update_TPM_result!(result::BinaryTPMResult, btpm::BinaryTPM, ephem, nₜ::Integer)
update_TPM_result!(result.pri, btpm.pri, ephem, nₜ)
update_TPM_result!(result.sec, btpm.sec, ephem, nₜ)
function update_TPM_result!(result::BinaryTPMResult, btpm::BinaryTPM, ephem, nₜ::Integer, times_to_save::Vector{Float64})
update_TPM_result!(result.pri, btpm.pri, ephem, nₜ, times_to_save)
update_TPM_result!(result.sec, btpm.sec, ephem, nₜ, times_to_save)
end


Expand All @@ -395,7 +388,7 @@ Export the result of `SingleTPM` to CSV files.
- Save the depths of the calculation nodes
- Save README for the data file
"""
function export_TPM_results(dirpath, result::SingleTPMResult, stpm::SingleTPM, ephem)
function export_TPM_results(dirpath, result::SingleTPMResult, stpm::SingleTPM, ephem, times_to_save::Vector{Float64})

df = DataFrame()
df.time = ephem.time
Expand All @@ -411,13 +404,14 @@ function export_TPM_results(dirpath, result::SingleTPMResult, stpm::SingleTPM, e

CSV.write(joinpath(dirpath, "data.csv"), df)

header = string.(ephem.time[@. result.time_begin ephem.time < result.time_end]) # Time to save temperature in String

##= Surface temperature =##
header = string.(times_to_save)
CSV.write(
joinpath(dirpath, "surf_temp.csv"),
DataFrame(result.surf_temp, header)
)

##= Subsurface temperature =##
for (nₛ, temp) in result.face_temp
CSV.write(
joinpath(dirpath, "face_temp_$(lpad(nₛ, 7, '0')).csv"),
Expand All @@ -438,15 +432,15 @@ Export the result of `BinaryTPM` to CSV files.
- `btpm` : Thermophysical model for a binary asteroid
- `ephem` : Ephemerides
"""
function export_TPM_results(dirpath, result::BinaryTPMResult, btpm::BinaryTPM, ephem)
function export_TPM_results(dirpath, result::BinaryTPMResult, btpm::BinaryTPM, ephem, times_to_save::Vector{Float64})
dirpath_pri = joinpath(dirpath, "pri")
dirpath_sec = joinpath(dirpath, "sec")

mkpath(dirpath_pri)
mkpath(dirpath_sec)

export_TPM_results(dirpath_pri, result.pri, btpm.pri, ephem)
export_TPM_results(dirpath_sec, result.sec, btpm.sec, ephem)
export_TPM_results(dirpath_pri, result.pri, btpm.pri, ephem, times_to_save)
export_TPM_results(dirpath_sec, result.sec, btpm.sec, ephem, times_to_save)
end


Expand Down Expand Up @@ -554,20 +548,19 @@ end
Run TPM for a single asteroid.
# Arguments
- `stpm` : Thermophysical model for a single asteroid
- `ephem` : Ephemerides
- `stpm` : Thermophysical model for a single asteroid
- `ephem` : Ephemerides
- `ephem.time` : Ephemeris times
- `ephem.sun` : Sun's position in the asteroid-fixed frame (Not normalized)
- `time_begin` : Time to start saving temperature
- `time_end` : Time to finish saving temperature
- `face_ID` : Face indices where to save subsurface termperature
- `times_to_save` : Timesteps to save temperature
- `face_ID` : Face indices where to save subsurface termperature
# Keyword arguments
- `show_progress` : Flag to show the progress meter
"""
function run_TPM!(stpm::SingleTPM, ephem, time_begin::Real, time_end::Real, face_ID::Vector{Int}; show_progress=true)
function run_TPM!(stpm::SingleTPM, ephem, times_to_save::Vector{Float64}, face_ID::Vector{Int}; show_progress=true)

result = SingleTPMResult(stpm, ephem, time_begin, time_end, face_ID)
result = SingleTPMResult(stpm, ephem, times_to_save, face_ID)

## ProgressMeter setting
if show_progress
Expand All @@ -584,7 +577,7 @@ function run_TPM!(stpm::SingleTPM, ephem, time_begin::Real, time_end::Real, face

update_thermal_force!(stpm)

update_TPM_result!(result, stpm, ephem, nₜ) # Save data
update_TPM_result!(result, stpm, ephem, nₜ, times_to_save) # Save data

## Update the progress meter
if show_progress
Expand All @@ -609,25 +602,24 @@ end
Run TPM for a binary asteroid.
# Arguments
- `btpm` : Thermophysical model for a binary asteroid
- `ephem` : Ephemerides
- `btpm` : Thermophysical model for a binary asteroid
- `ephem` : Ephemerides
- `time` : Ephemeris times
- `sun1` : Sun's position in the primary's frame
- `sun2` : Sun's position in the secondary's frame
- `sec` : Secondary's position in the primary's frame
- `P2S` : Rotation matrix from primary to secondary frames
- `S2P` : Rotation matrix from secondary to primary frames
- `time_begin` : Time to start saving temperature
- `time_end` : Time to finish saving temperature
- `face_ID_pri` : Face indices where to save subsurface termperature for the primary
- `face_ID_sec` : Face indices where to save subsurface termperature for the secondary
- `times_to_save` : Timesteps to save temperature
- `face_ID_pri` : Face indices where to save subsurface termperature for the primary
- `face_ID_sec` : Face indices where to save subsurface termperature for the secondary
# Keyword arguments
- `show_progress` : Flag to show the progress meter
"""
function run_TPM!(btpm::BinaryTPM, ephem, time_begin::Real, time_end::Real, face_ID_pri::Vector{Int}, face_ID_sec::Vector{Int}; show_progress=true)
function run_TPM!(btpm::BinaryTPM, ephem, times_to_save::Vector{Float64}, face_ID_pri::Vector{Int}, face_ID_sec::Vector{Int}; show_progress=true)

result = BinaryTPMResult(btpm, ephem, time_begin, time_end, face_ID_pri, face_ID_sec)
result = BinaryTPMResult(btpm, ephem, times_to_save, face_ID_pri, face_ID_sec)

## ProgressMeter setting
if show_progress
Expand All @@ -650,7 +642,7 @@ function run_TPM!(btpm::BinaryTPM, ephem, time_begin::Real, time_end::Real, face

update_thermal_force!(btpm)

update_TPM_result!(result, btpm, ephem, nₜ) # Save data
update_TPM_result!(result, btpm, ephem, nₜ, times_to_save) # Save data

## Update the progress meter
if show_progress
Expand Down

0 comments on commit c43b907

Please sign in to comment.