Path: blob/main/src/callbacks_step/averaging_dg2d.jl
2055 views
# By default, Julia/LLVM does not use fused multiply-add operations (FMAs).1# Since these FMAs can increase the performance of many numerical algorithms,2# we need to opt-in explicitly.3# See https://ranocha.de/blog/Optimizing_EC_Trixi for further details.4@muladd begin5#! format: noindent67# Create arrays with DGSEM-specific structure to store the mean values and set them all to 08function initialize_mean_values(mesh::TreeMesh{2},9equations::AbstractCompressibleEulerEquations{2},10dg::DGSEM, cache)11uEltype = eltype(cache.elements)12v_mean = zeros(uEltype,13(ndims(equations), nnodes(dg), nnodes(dg),14nelements(cache.elements)))15c_mean = zeros(uEltype, (nnodes(dg), nnodes(dg), nelements(cache.elements)))16rho_mean = zeros(uEltype, size(c_mean))17vorticity_mean = zeros(uEltype, size(c_mean))1819return (; v_mean, c_mean, rho_mean, vorticity_mean)20end2122# Create cache which holds the vorticity for the previous time step. This is needed due to the23# trapezoidal rule24function create_cache(::Type{AveragingCallback}, mesh::TreeMesh{2},25equations::AbstractCompressibleEulerEquations{2}, dg::DGSEM,26cache)27# Cache vorticity from previous time step28uEltype = eltype(cache.elements)29vorticity_prev = zeros(uEltype, (nnodes(dg), nnodes(dg), nelements(cache.elements)))30return (; vorticity_prev)31end3233# Calculate vorticity for the initial solution and store it in the cache34function initialize_cache!(averaging_callback_cache, u,35mesh::TreeMesh{2},36equations::AbstractCompressibleEulerEquations{2},37dg::DGSEM, cache)38@unpack vorticity_prev = averaging_callback_cache3940# Calculate vorticity for initial solution41calc_vorticity!(vorticity_prev, u, mesh, equations, dg, cache)4243return nothing44end4546# Update mean values using the trapezoidal rule47function calc_mean_values!(mean_values, averaging_callback_cache, u, u_prev,48integration_constant,49mesh::TreeMesh{2},50equations::AbstractCompressibleEulerEquations{2},51dg::DGSEM, cache)52@unpack v_mean, c_mean, rho_mean, vorticity_mean = mean_values53@unpack vorticity_prev = averaging_callback_cache5455@threaded for element in eachelement(dg, cache)56for j in eachnode(dg), i in eachnode(dg)57vorticity = calc_vorticity_node(u, mesh, equations, dg, cache, i, j,58element)59vorticity_prev_node = vorticity_prev[i, j, element]60vorticity_prev[i, j, element] = vorticity # Cache current vorticity for the next time step6162u_node_prim = cons2prim(get_node_vars(u, equations, dg, i, j, element),63equations)64u_prev_node_prim = cons2prim(get_node_vars(u_prev, equations, dg, i, j,65element), equations)6667rho, v1, v2, p = u_node_prim68rho_prev, v1_prev, v2_prev, p_prev = u_prev_node_prim6970c = sqrt(equations.gamma * p / rho)71c_prev = sqrt(equations.gamma * p_prev / rho_prev)7273# Calculate the contribution to the mean values using the trapezoidal rule74vorticity_mean[i, j, element] += integration_constant *75(vorticity_prev_node + vorticity)76v_mean[1, i, j, element] += integration_constant * (v1_prev + v1)77v_mean[2, i, j, element] += integration_constant * (v2_prev + v2)78c_mean[i, j, element] += integration_constant * (c_prev + c)79rho_mean[i, j, element] += integration_constant * (rho_prev + rho)80end81end8283return nothing84end85end # @muladd868788