From a42fc1c64d7c0d19ad307a87d43caeff65b6b566 Mon Sep 17 00:00:00 2001 From: Yuto Horikawa Date: Sat, 6 Jul 2024 02:00:06 -0700 Subject: [PATCH] add `_getindex` and remove some ternary operators (#144) --- src/AsteroidThermoPhysicalModels.jl | 4 ++++ src/energy_flux.jl | 24 ++++++++++++------------ src/heat_conduction.jl | 24 ++++++++++++------------ src/non_grav.jl | 8 ++++---- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/AsteroidThermoPhysicalModels.jl b/src/AsteroidThermoPhysicalModels.jl index aa7cb867..3da03acc 100644 --- a/src/AsteroidThermoPhysicalModels.jl +++ b/src/AsteroidThermoPhysicalModels.jl @@ -16,6 +16,10 @@ const SOLAR_CONST = 1366.0 # Solar constant, Φ☉ [W/m^2] const c₀ = 299792458.0 # Speed of light [m/s] const σ_SB = 5.670374419e-8 # Stefan–Boltzmann constant [W/m^2/K^4] +# utils for NonUniformThermoParams and UniformThermoParams +_getindex(a::AbstractArray, i::Integer) = a[i] +_getindex(a::Real, ::Integer) = a + include("obj.jl") include("shape.jl") include("facet.jl") diff --git a/src/energy_flux.jl b/src/energy_flux.jl index 487479b9..f1f27982 100644 --- a/src/energy_flux.jl +++ b/src/energy_flux.jl @@ -27,8 +27,8 @@ Input energy per second on the whole surface [W] function energy_in(stpm::SingleTPM) E_in = 0. for i in eachindex(stpm.shape.faces) - R_vis = (stpm.thermo_params.reflectance_vis isa Real ? stpm.thermo_params.reflectance_vis : stpm.thermo_params.reflectance_vis[i]) - R_ir = (stpm.thermo_params.reflectance_ir isa Real ? stpm.thermo_params.reflectance_ir : stpm.thermo_params.reflectance_ir[i]) + R_vis = _getindex(stpm.thermo_params.reflectance_vis, i) + R_ir = _getindex(stpm.thermo_params.reflectance_ir, i) F_sun = stpm.flux[i, 1] F_scat = stpm.flux[i, 2] F_rad = stpm.flux[i, 3] @@ -51,7 +51,7 @@ Output enegey per second from the whole surface [W] function energy_out(stpm::SingleTPM) E_out = 0. for i in eachindex(stpm.shape.faces) - ε = (stpm.thermo_params.emissivity isa Real ? stpm.thermo_params.emissivity : stpm.thermo_params.emissivity[i]) + ε = _getindex(stpm.thermo_params.emissivity, i) T = stpm.temperature[begin, i] # Surface temperature a = stpm.shape.face_areas[i] @@ -151,7 +151,7 @@ function update_flux_scat_single!(stpm::SingleTPM) for visiblefacet in stpm.shape.visiblefacets[i_face] j = visiblefacet.id fᵢⱼ = visiblefacet.f - R_vis = (stpm.thermo_params.reflectance_vis isa Real ? stpm.thermo_params.reflectance_vis : stpm.thermo_params.reflectance_vis[j]) + R_vis = _getindex(stpm.thermo_params.reflectance_vis, j) stpm.flux[i_face, 2] += fᵢⱼ * R_vis * stpm.flux[j, 1] end @@ -204,8 +204,8 @@ function update_flux_rad_single!(stpm::SingleTPM) for visiblefacet in stpm.shape.visiblefacets[i] j = visiblefacet.id fᵢⱼ = visiblefacet.f - ε = (stpm.thermo_params.emissivity isa Real ? stpm.thermo_params.emissivity : stpm.thermo_params.emissivity[j]) - R_ir = (stpm.thermo_params.reflectance_ir isa Real ? stpm.thermo_params.reflectance_ir : stpm.thermo_params.reflectance_ir[j]) + ε = _getindex(stpm.thermo_params.emissivity, j) + R_ir = _getindex(stpm.thermo_params.reflectance_ir, j) Tⱼ = stpm.temperature[begin, j] stpm.flux[i, 3] += ε * σ_SB * (1 - R_ir) * fᵢⱼ * Tⱼ^4 @@ -421,12 +421,12 @@ function mutual_heating!(btpm::BinaryTPM, rₛ, R₂₁) T₁ = btpm.pri.temperature[begin, i] T₂ = btpm.sec.temperature[begin, j] - ε₁ = (thermo_params1.emissivity isa Real ? thermo_params1.emissivity : thermo_params1.emissivity[i]) - ε₂ = (thermo_params2.emissivity isa Real ? thermo_params2.emissivity : thermo_params2.emissivity[j]) - R_vis₁ = (thermo_params1.reflectance_vis isa Real ? thermo_params1.reflectance_vis : thermo_params1.reflectance_vis[i]) - R_vis₂ = (thermo_params2.reflectance_vis isa Real ? thermo_params2.reflectance_vis : thermo_params2.reflectance_vis[j]) - R_ir₁ = (thermo_params1.reflectance_ir isa Real ? thermo_params1.reflectance_ir : thermo_params1.reflectance_ir[i]) - R_ir₂ = (thermo_params2.reflectance_ir isa Real ? thermo_params2.reflectance_ir : thermo_params2.reflectance_ir[j]) + ε₁ = _getindex(thermo_params1.emissivity, i) + ε₂ = _getindex(thermo_params2.emissivity, j) + R_vis₁ = _getindex(thermo_params1.reflectance_vis, i) + R_vis₂ = _getindex(thermo_params2.reflectance_vis, j) + R_ir₁ = _getindex(thermo_params1.reflectance_ir, i) + R_ir₂ = _getindex(thermo_params2.reflectance_ir, j) ## Mutual heating by scattered light btpm.pri.flux[i, 2] += f₁₂ * R_vis₂ * btpm.sec.flux[j, 1] diff --git a/src/heat_conduction.jl b/src/heat_conduction.jl index ff63b9de..64ef0568 100644 --- a/src/heat_conduction.jl +++ b/src/heat_conduction.jl @@ -67,9 +67,9 @@ function forward_euler!(stpm::SingleTPM, Δt) ## Zero-conductivity (thermal inertia) case if iszero(stpm.thermo_params.inertia) for i_face in 1:n_face - R_vis = (stpm.thermo_params.reflectance_vis isa Real ? stpm.thermo_params.reflectance_vis : stpm.thermo_params.reflectance_vis[i_face] ) - R_ir = (stpm.thermo_params.reflectance_ir isa Real ? stpm.thermo_params.reflectance_ir : stpm.thermo_params.reflectance_ir[i_face]) - ε = (stpm.thermo_params.emissivity isa Real ? stpm.thermo_params.emissivity : stpm.thermo_params.emissivity[i_face]) + R_vis = _getindex(stpm.thermo_params.reflectance_vis, i_face) + R_ir = _getindex(stpm.thermo_params.reflectance_ir, i_face) + ε = _getindex(stpm.thermo_params.emissivity, i_face) εσ = ε * σ_SB F_sun, F_scat, F_rad = stpm.flux[i_face, :] @@ -82,7 +82,7 @@ function forward_euler!(stpm::SingleTPM, Δt) for i_face in 1:n_face P = stpm.thermo_params.period Δz = stpm.thermo_params.Δz - l = (stpm.thermo_params.skindepth isa Real ? stpm.thermo_params.skindepth : stpm.thermo_params.skindepth[i_face]) + l = _getindex(stpm.thermo_params.skindepth, i_face) λ = (Δt/P) / (Δz/l)^2 / 4π λ ≥ 0.5 && error("The forward Euler method is unstable because λ = $λ. This should be less than 0.5.") @@ -116,7 +116,7 @@ function backward_euler!(stpm::SingleTPM, Δt) # n_face = size(T, 2) # for i_face in 1:n_face - # λ = (stpm.thermo_params.λ isa Real ? stpm.thermo_params.λ : stpm.thermo_params.λ[i_face]) + # λ = _getindex(stpm.thermo_params.λ, i_face) # stpm.SOLVER.a .= -λ # stpm.SOLVER.a[begin] = 0 @@ -242,13 +242,13 @@ function update_upper_temperature!(stpm::SingleTPM, i::Integer) #### Radiation boundary condition #### if stpm.BC_UPPER isa RadiationBoundaryCondition - P = stpm.thermo_params.period - l = (stpm.thermo_params.skindepth isa Real ? stpm.thermo_params.skindepth : stpm.thermo_params.skindepth[i] ) - Γ = (stpm.thermo_params.inertia isa Real ? stpm.thermo_params.inertia : stpm.thermo_params.inertia[i]) - R_vis = (stpm.thermo_params.reflectance_vis isa Real ? stpm.thermo_params.reflectance_vis : stpm.thermo_params.reflectance_vis[i] ) - R_ir = (stpm.thermo_params.reflectance_ir isa Real ? stpm.thermo_params.reflectance_ir : stpm.thermo_params.reflectance_ir[i]) - ε = (stpm.thermo_params.emissivity isa Real ? stpm.thermo_params.emissivity : stpm.thermo_params.emissivity[i]) - Δz = stpm.thermo_params.Δz + P = stpm.thermo_params.period + l = _getindex(stpm.thermo_params.skindepth, i) + Γ = _getindex(stpm.thermo_params.inertia, i) + R_vis = _getindex(stpm.thermo_params.reflectance_vis, i) + R_ir = _getindex(stpm.thermo_params.reflectance_ir, i) + ε = _getindex(stpm.thermo_params.emissivity, i) + Δz = stpm.thermo_params.Δz F_sun, F_scat, F_rad = stpm.flux[i, :] F_total = flux_total(R_vis, R_ir, F_sun, F_scat, F_rad) diff --git a/src/non_grav.jl b/src/non_grav.jl index 78129b03..7fd99f41 100644 --- a/src/non_grav.jl +++ b/src/non_grav.jl @@ -29,10 +29,10 @@ function update_thermal_force!(stpm::SingleTPM) ## Total emittance from face i , Eᵢ [W/m²]. ## Note that both scattered light and thermal radiation are assumed to be isotropic. - R_vis = (stpm.thermo_params.reflectance_vis isa Real ? stpm.thermo_params.reflectance_vis : stpm.thermo_params.reflectance_vis[i]) - R_ir = (stpm.thermo_params.reflectance_ir isa Real ? stpm.thermo_params.reflectance_ir : stpm.thermo_params.reflectance_ir[i]) - ε = (stpm.thermo_params.emissivity isa Real ? stpm.thermo_params.emissivity : stpm.thermo_params.emissivity[i]) - Eᵢ = R_vis * F_sun + R_vis * F_scat + R_ir * F_rad + ε * σ_SB * Tᵢ^4 + R_vis = _getindex(stpm.thermo_params.reflectance_vis, i) + R_ir = _getindex(stpm.thermo_params.reflectance_ir, i) + ε = _getindex(stpm.thermo_params.emissivity, i) + Eᵢ = R_vis * F_sun + R_vis * F_scat + R_ir * F_rad + ε * σ_SB * Tᵢ^4 ## Thermal force on each face stpm.face_forces[i] = - 2/3 * Eᵢ * aᵢ / c₀ * n̂ᵢ # The first term normal to the face