Skip to content

Commit

Permalink
Simplify evaluatete_at_grid_nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
lijas committed Oct 29, 2024
1 parent 0e26c10 commit 8c32252
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
23 changes: 6 additions & 17 deletions src/Dofs/DofHandler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -927,13 +927,13 @@ function evaluate_at_grid_nodes(dh::DofHandler, u::AbstractVector, fieldname::Sy
end

# Internal method that have the vtk option to allocate the output differently
function _evaluate_at_grid_nodes(dh::DofHandler, u::AbstractVector{T}, fieldname::Symbol, ::Val{vtk}=Val(false)) where {T, vtk}
function _evaluate_at_grid_nodes(dh::DofHandler{sdim}, u::AbstractVector{T}, fieldname::Symbol, ::Val{vtk}=Val(false)) where {T, vtk, sdim}

Check warning on line 930 in src/Dofs/DofHandler.jl

View check run for this annotation

Codecov / codecov/patch

src/Dofs/DofHandler.jl#L930

Added line #L930 was not covered by tests
# Make sure the field exists
fieldname getfieldnames(dh) || error("Field $fieldname not found.")
# Figure out the return type (scalar or vector)
field_idx = find_field(dh, fieldname)
ip = getfieldinterpolation(dh, field_idx)
RT = ip isa ScalarInterpolation ? T : Vec{n_components(ip),T}
RT = shape_value_type(ip, T)

Check warning on line 936 in src/Dofs/DofHandler.jl

View check run for this annotation

Codecov / codecov/patch

src/Dofs/DofHandler.jl#L936

Added line #L936 was not covered by tests
if vtk
# VTK output of solution field (or L2 projected scalar data)
n_c = n_components(ip)
Expand All @@ -954,37 +954,26 @@ function _evaluate_at_grid_nodes(dh::DofHandler, u::AbstractVector{T}, fieldname
ip_geo = geometric_interpolation(CT)
local_node_coords = reference_coordinates(ip_geo)
qr = QuadratureRule{getrefshape(ip)}(zeros(length(local_node_coords)), local_node_coords)
if ip isa VectorizedInterpolation
# TODO: Remove this hack when embedding works...
cv = CellValues(qr, ip.ip, ip_geo)
else
cv = CellValues(qr, ip, ip_geo)
end
cv = CellValues(qr, ip, ip_geo^sdim; update_gradients = Val(false), update_hessians = Val(false), update_detJdV = Val(false))

Check warning on line 957 in src/Dofs/DofHandler.jl

View check run for this annotation

Codecov / codecov/patch

src/Dofs/DofHandler.jl#L957

Added line #L957 was not covered by tests
drange = dof_range(sdh, field_idx)
# Function barrier
_evaluate_at_grid_nodes!(data, sdh, u, cv, drange, RT)
_evaluate_at_grid_nodes!(data, sdh, u, cv, drange)

Check warning on line 960 in src/Dofs/DofHandler.jl

View check run for this annotation

Codecov / codecov/patch

src/Dofs/DofHandler.jl#L960

Added line #L960 was not covered by tests
end
return data
end

# Loop over the cells and use shape functions to compute the value
function _evaluate_at_grid_nodes!(data::Union{Vector,Matrix}, sdh::SubDofHandler,
u::AbstractVector{T}, cv::CellValues, drange::UnitRange, ::Type{RT}) where {T, RT}
u::AbstractVector{T}, cv::CellValues, drange::UnitRange) where {T}
ue = zeros(T, length(drange))
# TODO: Remove this hack when embedding works...
if RT <: Vec && function_interpolation(cv) isa ScalarInterpolation
uer = reinterpret(RT, ue)
else
uer = ue
end
for cell in CellIterator(sdh)
# Note: We are only using the shape functions: no reinit!(cv, cell) necessary
@assert getnquadpoints(cv) == length(cell.nodes)
for (i, I) in pairs(drange)
ue[i] = u[cell.dofs[I]]
end
for (qp, nodeid) in pairs(cell.nodes)
val = function_value(cv, qp, uer)
val = function_value(cv, qp, ue)

Check warning on line 976 in src/Dofs/DofHandler.jl

View check run for this annotation

Codecov / codecov/patch

src/Dofs/DofHandler.jl#L976

Added line #L976 was not covered by tests
if data isa Matrix # VTK
data[1:length(val), nodeid] .= val
data[(length(val)+1):end, nodeid] .= 0 # purge the NaN
Expand Down
4 changes: 4 additions & 0 deletions src/interpolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ n_components(::VectorInterpolation{vdim}) where {vdim} = vdim
# Number of components that are allowed to prescribe in e.g. Dirichlet BC
n_dbc_components(ip::Interpolation) = n_components(ip)

shape_value_type(::ScalarInterpolation, T::Type) = T
shape_value_type(::VectorInterpolation{vdim}, T::Type) where {vdim} = Vec{vdim,T}

Check warning on line 61 in src/interpolations.jl

View check run for this annotation

Codecov / codecov/patch

src/interpolations.jl#L60-L61

Added lines #L60 - L61 were not covered by tests
#shape_value_type(::MatrixInterpolation, T::Type) = Tensor #958

# TODO: Add a fallback that errors if there are multiple dofs per edge/face instead to force
# interpolations to opt-out instead of silently do nothing.
"""
Expand Down

0 comments on commit 8c32252

Please sign in to comment.