Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: p4est solver on a spherical shell #1749

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
02a35e5
First version of the p4est solver on a spherical shell
amrueda Nov 14, 2023
7be12aa
New strategy to compute metric terms by inheriting them from a 3D cub…
amrueda Nov 15, 2023
27f1a81
Added elixir for advection test
amrueda Nov 15, 2023
d23b56c
Discovered a problem with allocations and implemented a temporary fix
amrueda Nov 17, 2023
9338d51
Implemented Giraldo's metric terms and cleaned up a bit
amrueda Nov 17, 2023
b4d0bec
Added elixir to run convergence and FSP test with the Euler equations
amrueda Nov 17, 2023
1eb17ac
Fixed bugs in 2D cubed sphere elixir and in the transformation of 3D …
amrueda Nov 21, 2023
e180aa4
Removed hard-coded computation of rhs for the transformation of Euler…
amrueda Nov 21, 2023
2d6fc29
Removed unused code
amrueda Nov 21, 2023
37a9afb
Merge branch 'main' into spherical_shell
amrueda Nov 21, 2023
3715ea8
Format elixir
amrueda Nov 21, 2023
b30fe7d
Added test. Allocations are failing due to function `get_contravarian…
amrueda Nov 21, 2023
2942c79
Format
amrueda Nov 21, 2023
b89e333
Format
amrueda Nov 21, 2023
78436f1
Merge branch 'main' into spherical_shell
amrueda Nov 22, 2023
1521e8e
Merge branch 'main' into spherical_shell
amrueda Nov 27, 2023
78bdbfd
Merge branch 'main' into spherical_shell
amrueda Dec 4, 2023
a18fcd6
Apply custom source term (that depends on du) with a custom RHS
amrueda Dec 5, 2023
d2b5294
Using the number of dimensions of the equations to dispatch the volum…
amrueda Dec 5, 2023
245b20d
Use PtrArray for contravariant_vectors
amrueda Dec 6, 2023
32b6836
Added function static2val
amrueda Dec 7, 2023
d959d77
Initialize contravariant_vectors with full type information
amrueda Dec 8, 2023
500db95
Improved initialization of P4estElementContainer
amrueda Dec 8, 2023
2e2f68e
Merge branch 'main' into spherical_shell
amrueda Jul 24, 2024
b3c3fa3
Merge branch 'main' into spherical_shell
amrueda Jul 24, 2024
0205203
Merge pull request #13 from amrueda/spherical_shell_fix_allocations_P…
amrueda Jul 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions examples/p4est_2d_dgsem/elixir_euler_cubed_sphere_shell.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@

using OrdinaryDiffEq
using Trixi

###############################################################################
# semidiscretization of the linear advection equation

equations = CompressibleEulerEquations3D(1.4)

# Create DG solver with polynomial degree = 3 and (local) Lax-Friedrichs/Rusanov flux as surface flux
polydeg = 3
solver = DGSEM(polydeg = polydeg, surface_flux = flux_lax_friedrichs)

# Initial condition for a Gaussian density profile with constant pressure
# and the velocity of a rotating solid body
function initial_condition_advection_sphere(x, t, equations::CompressibleEulerEquations3D)
# Gaussian density
rho = 1.0 + exp(-20 * (x[1]^2 + x[3]^2))
# Constant pressure
p = 1.0

# Spherical coordinates for the point x
if sign(x[2]) == 0.0
signy = 1.0
else
signy = sign(x[2])
end
# Co-latitude
colat = acos(x[3] / sqrt(x[1]^2 + x[2]^2 + x[3]^2))
# Latitude (auxiliary variable)
lat = -colat + 0.5 * pi
# Longitude
r_xy = sqrt(x[1]^2 + x[2]^2)
if r_xy == 0.0
phi = pi / 2
else
phi = signy * acos(x[1] / r_xy)
end

# Compute the velocity of a rotating solid body
# (alpha is the angle between the rotation axis and the polar axis of the spherical coordinate system)
v0 = 1.0 # Velocity at the "equator"
alpha = 0.0 #pi / 4
v_long = v0 * (cos(lat) * cos(alpha) + sin(lat) * cos(phi) * sin(alpha))
v_lat = -v0 * sin(phi) * sin(alpha)

# Transform to Cartesian coordinate system
v1 = -cos(colat) * cos(phi) * v_lat - sin(phi) * v_long
v2 = -cos(colat) * sin(phi) * v_lat + cos(phi) * v_long
v3 = sin(colat) * v_lat

return prim2cons(SVector(rho, v1, v2, v3, p), equations)
end

# Source term function to transform the Euler equations into the linear advection equations with variable advection velocity
function source_terms_convert_to_linear_advection(u, du, x, t,
equations::CompressibleEulerEquations3D)
v1 = u[2] / u[1]
v2 = u[3] / u[1]
v3 = u[4] / u[1]

s2 = du[1] * v1 - du[2]
s3 = du[1] * v2 - du[3]
s4 = du[1] * v3 - du[4]
s5 = 0.5 * (s2 * v1 + s3 * v2 + s4 * v3) - du[5]

return SVector(0.0, s2, s3, s4, s5)
end

# Custom RHS that applies a source term that depends on du (used to convert the 3D Euler equations into the 3D linear advection
# equations with position-dependent advection speed)
function rhs_semi_custom!(du_ode, u_ode, semi, t)
# Compute standard Trixi RHS
Trixi.rhs!(du_ode, u_ode, semi, t)

# Now apply the custom source term
Trixi.@trixi_timeit Trixi.timer() "custom source term" begin
@unpack solver, equations, cache = semi
@unpack node_coordinates = cache.elements

# Wrap the solution and RHS
u = Trixi.wrap_array(u_ode, semi)
du = Trixi.wrap_array(du_ode, semi)

Trixi.@threaded for element in eachelement(solver, cache)
for j in eachnode(solver), i in eachnode(solver)
u_local = Trixi.get_node_vars(u, equations, solver, i, j, element)
du_local = Trixi.get_node_vars(du, equations, solver, i, j, element)
x_local = Trixi.get_node_coords(node_coordinates, equations, solver,
i, j, element)
source = source_terms_convert_to_linear_advection(u_local, du_local,
x_local, t, equations)
Trixi.add_to_node_vars!(du, source, equations, solver, i, j, element)
end
end
end
end

initial_condition = initial_condition_advection_sphere

mesh = Trixi.P4estMeshCubedSphere2D(5, 1.0, polydeg = polydeg, initial_refinement_level = 0)

# A semidiscretization collects data structures and functions for the spatial discretization
semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)

###############################################################################
# ODE solvers, callbacks etc.

# Create ODE problem with time span from 0.0 to π
tspan = (0.0, pi)
ode = semidiscretize(semi, tspan)

# Create custom discretization that runs with the custom RHS
ode_semi_custom = ODEProblem(rhs_semi_custom!,
ode.u0,
ode.tspan,
semi)

# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup
# and resets the timers
summary_callback = SummaryCallback()

# The AnalysisCallback allows to analyse the solution in regular intervals and prints the results
analysis_callback = AnalysisCallback(semi, interval = 10,
save_analysis = true,
extra_analysis_integrals = (Trixi.density,))

# The SaveSolutionCallback allows to save the solution to a file in regular intervals
save_solution = SaveSolutionCallback(interval = 10,
solution_variables = cons2prim)

# The StepsizeCallback handles the re-calculation of the maximum Δt after each time step
stepsize_callback = StepsizeCallback(cfl = 0.7)

# Create a CallbackSet to collect all callbacks such that they can be passed to the ODE solver
callbacks = CallbackSet(summary_callback, analysis_callback, save_solution,
stepsize_callback)

###############################################################################
# run the simulation

# OrdinaryDiffEq's `solve` method evolves the solution in time and executes the passed callbacks
sol = solve(ode_semi_custom, CarpenterKennedy2N54(williamson_condition = false),
dt = 1.0, # solve needs some value here but it will be overwritten by the stepsize_callback
save_everystep = false, callback = callbacks);

# Print the timer summary
summary_callback()
2 changes: 1 addition & 1 deletion src/Trixi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ using HDF5: HDF5, h5open, attributes, create_dataset, datatype, dataspace
using IfElse: ifelse
using LinearMaps: LinearMap
using LoopVectorization: LoopVectorization, @turbo, indices
using StaticArrayInterface: static_length # used by LoopVectorization
using StaticArrayInterface: static_length, static_size # used by LoopVectorization
using MuladdMacro: @muladd
using Octavian: Octavian, matmul!
using Polyester: Polyester, @batch # You know, the cheapest threads you can find...
Expand Down
Loading
Loading