CliMA / ClimateMachine.jl

Climate Machine: an Earth System Model that automatically learns from data
https://clima.github.io/ClimateMachine.jl/latest/
Other
453 stars 78 forks source link

Cartesian to Spherical coordinates #182

Open fxgiraldo opened 5 years ago

fxgiraldo commented 5 years ago

Just wanted to pass it by everyone to see if this is the sort of map that you all need to go from Cartesian to Spherical. You can find the code here: https://github.com/fxgiraldo/CLIMA/blob/5b181c1e7802cc9107cd0f2c533e373a65f72548/src/DGmethods/test/Advection/advection_sphere.jl#L33-L67

The code here runs advection on the full 3D sphere. It is the first test of CLIMA on the sphere.

Before committing it to CLIMA, we would like feedback on the following:

Here is what the snippet of code looks like:

#{{{ Cartesian->Spherical
@inline function cartesian_to_spherical(x,y,z,format)
#Conversion Constants
if format == "degrees"
c=180/π
elseif format == "radians"
c=1.0
end
λ_max=2πc
λ_min=0c
ϕ_max=+0.5πc
ϕ_min=-0.5πc

#Conversion functions
r = hypot(x,y,z)
λ=atan(y,x)*c
ϕ=asin(z/r)*c

#Stay within bounds

#= if (λ < λ_min)
λ = λ + λ_max
end
if (λ > λ_max)
λ = λ - λ_max
end
if (ϕ < ϕ_min)
ϕ = ϕ + ϕ_max
end
if (ϕ > ϕ_max)
ϕ = ϕ - ϕ_max
end
=#
return r, λ, ϕ
end
#}}} Cartesian->Spherical
simonbyrne commented 5 years ago

cross ref: #179

One thing we should consider is using types to convey semantic meaning:

struct XYZCoord{T}
    x::T
    y::T
    z::T
end

struct LatLongAltCoord{T}
    latitude::T
    longitude::T
    altitude::T
end
tapios commented 5 years ago