Path: blob/main/src/solvers/dgsem_structured/containers_1d.jl
5590 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# Initialize data structures in element container8function init_elements!(elements, mesh::StructuredMesh{1}, basis::AbstractBasisSBP)9@unpack node_coordinates, boundary_node_coordinates, left_neighbors,10jacobian_matrix, contravariant_vectors, inverse_jacobian = elements1112# Calculate node coordinates, Jacobian matrix, and inverse Jacobian determinant13for cell_x in 1:size(mesh, 1)14calc_node_coordinates!(node_coordinates, cell_x, mesh.mapping, mesh, basis)1516calc_jacobian_matrix!(jacobian_matrix, cell_x, node_coordinates, basis)1718calc_inverse_jacobian!(inverse_jacobian, cell_x, jacobian_matrix)19end2021# Contravariant vectors don't make sense in 1D, they would be identical to inverse_jacobian22fill!(contravariant_vectors, NaN)2324initialize_left_neighbor_connectivity!(left_neighbors, mesh)25calc_boundary_node_coordinates!(boundary_node_coordinates, node_coordinates,26mesh, basis)2728return nothing29end3031function calc_boundary_node_coordinates!(boundary_node_coordinates,32node_coordinates,33mesh::StructuredMesh{1},34basis::LobattoLegendreBasis)35nelements = size(mesh, 1)3637dim = 1 # spatial dimension38boundary_node_coordinates[dim, 1] = node_coordinates[dim, 1, 1]39boundary_node_coordinates[dim, 2] = node_coordinates[dim, nnodes(basis), nelements]4041return nothing42end4344function calc_boundary_node_coordinates!(boundary_node_coordinates,45node_coordinates,46mesh::StructuredMesh{1},47basis::GaussLegendreBasis)48nelements = size(mesh, 1)49boundary_matrix = basis.boundary_interpolation5051dim = 1 # spatial dimension52# For structured mesh:53# Left/right boundaries are really left(-1)/right(+1) [first/second column of boundary matrix]54@views boundary_node_coordinates[dim, 1] = dot(boundary_matrix[:, 1],55node_coordinates[dim, :, 1])56@views boundary_node_coordinates[dim, 2] = dot(boundary_matrix[:, 2],57node_coordinates[dim, :, nelements])5859return nothing60end6162# Calculate physical coordinates to which every node of the reference element is mapped63# `mesh.mapping` is passed as an additional argument for type stability (function barrier)64function calc_node_coordinates!(node_coordinates, cell_x, mapping,65mesh::StructuredMesh{1},66basis::AbstractBasisSBP)67@unpack nodes = basis6869# Get cell length in reference mesh70dx = 2 / size(mesh, 1)7172# Calculate node coordinates of reference mesh73cell_x_offset = -1 + (cell_x - 1) * dx + dx / 27475for i in eachnode(basis)76# node_coordinates are the mapped reference node_coordinates77node_coordinates[1, i, cell_x] = mapping(cell_x_offset + dx / 2 * nodes[i])[1]78end7980return nothing81end8283# Calculate Jacobian matrix of the mapping from the reference element to the element in the physical domain84function calc_jacobian_matrix!(jacobian_matrix, element,85node_coordinates::AbstractArray{<:Any, 3},86basis::AbstractBasisSBP)87@views mul!(jacobian_matrix[1, 1, :, element], basis.derivative_matrix,88node_coordinates[1, :, element]) # x_ξ8990return jacobian_matrix91end9293# Calculate inverse Jacobian (determinant of Jacobian matrix of the mapping) in each node94function calc_inverse_jacobian!(inverse_jacobian::AbstractArray{<:Any, 2}, element,95jacobian_matrix)96@views inverse_jacobian[:, element] .= inv.(jacobian_matrix[1, 1, :, element])9798return inverse_jacobian99end100101# Save id of left neighbor of every element102function initialize_left_neighbor_connectivity!(left_neighbors, mesh::StructuredMesh{1})103# Neighbors in x-direction104# Inner elements105for cell_x in 2:size(mesh, 1)106left_neighbors[1, cell_x] = cell_x - 1107end108109if isperiodic(mesh)110# Periodic boundary111left_neighbors[1, 1] = size(mesh, 1)112else113# Use boundary conditions114left_neighbors[1, 1] = 0115end116117return left_neighbors118end119end # @muladd120121122