Skip to content

Commit

Permalink
Restore getcoordinates (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
KnutAM authored Sep 4, 2023
1 parent 6731c5d commit 718059e
Show file tree
Hide file tree
Showing 20 changed files with 78 additions and 74 deletions.
12 changes: 6 additions & 6 deletions benchmark/helper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function _generalized_ritz_galerkin_assemble_local_matrix(grid::Ferrite.Abstract

Ke = zeros(n_basefuncs, n_basefuncs)

X = get_cell_coordinates(grid, 1)
X = getcoordinates(grid, 1)
reinit!(cellvalues, X)

for q_point in 1:getnquadpoints(cellvalues)
Expand All @@ -45,7 +45,7 @@ function _generalized_ritz_galerkin_assemble_local_matrix(grid::Ferrite.Abstract

f = zeros(n_basefuncs)

X = get_cell_coordinates(grid, 1)
X = getcoordinates(grid, 1)
for face in 1:nfaces(getcells(grid)[1])
reinit!(facevalues, X, face)

Expand Down Expand Up @@ -73,11 +73,11 @@ function _generalized_petrov_galerkin_assemble_local_matrix(grid::Ferrite.Abstra

#implicit assumption: Same geometry!
X_shape = zeros(Vec{dim,Float64}, Ferrite.getngeobasefunctions(cellvalues_shape))
get_cell_coordinates!(X_shape, grid, 1)
getcoordinates!(X_shape, grid, 1)
reinit!(cellvalues_shape, X_shape)

X_test = zeros(Vec{dim,Float64}, Ferrite.getngeobasefunctions(cellvalues_test))
get_cell_coordinates!(X_test, grid, 1)
getcoordinates!(X_test, grid, 1)
reinit!(cellvalues_test, X_test)

for q_point in 1:getnquadpoints(cellvalues_test) #assume same quadrature rule
Expand All @@ -100,8 +100,8 @@ function _generalized_petrov_galerkin_assemble_local_matrix(grid::Ferrite.Abstra

f = zeros(n_basefuncs_test)

X_shape = get_cell_coordinates(grid, 1)
X_test = get_cell_coordinates(grid, 1)
X_shape = getcoordinates(grid, 1)
X_test = getcoordinates(grid, 1)
for face in 1:nfaces(getcells(grid)[1])
reinit!(facevalues_shape, X_shape, face)
reinit!(facevalues_test, X_test, face)
Expand Down
2 changes: 1 addition & 1 deletion docs/src/literate-gallery/helmholtz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function doassemble(cellvalues::CellValues, facevalues::FaceValues,
for (cellcount, cell) in enumerate(CellIterator(dh))
fill!(Ke, 0)
fill!(fe, 0)
coords = get_cell_coordinates(cell)
coords = getcoordinates(cell)

reinit!(cellvalues, cell)
# First we derive the non boundary part of the variation problem from the destined solution `u_ana`
Expand Down
2 changes: 1 addition & 1 deletion docs/src/literate-tutorials/linear_shell.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ for cell in CellIterator(grid)
fill!(ke, 0.0)
reinit!(cv, cell)
celldofs!(celldofs, dh, cellid(cell))
get_cell_coordinates!(cellcoords, grid, cellid(cell))
getcoordinates!(cellcoords, grid, cellid(cell))

#Call the element routine
integrate_shell!(ke, cv, qr_ooplane, cellcoords, data)
Expand Down
2 changes: 1 addition & 1 deletion docs/src/literate-tutorials/ns_vs_diffeq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ end #hid
all_celldofs = celldofs(cell) #hide
v_celldofs = all_celldofs[dof_range(dh, :v)] #hide
v_cell = u[v_celldofs] #hide
coords = get_cell_coordinates(cell) #hide
coords = getcoordinates(cell) #hide
for q_point in 1:getnquadpoints(cellvalues_v) #hide
= getdetJdV(cellvalues_v, q_point) #hide
coords_qp = spatial_coordinate(cellvalues_v, q_point, coords) #hide
Expand Down
4 changes: 2 additions & 2 deletions docs/src/literate-tutorials/stokes-flow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ function setup_mean_constraint(dh, fvp)
## Loop over all the boundaries
for (ci, fi) in set
Ce .= 0
get_cell_coordinates!(element_coords, dh.grid, ci)
getcoordinates!(element_coords, dh.grid, ci)
reinit!(fvp, element_coords, fi)
celldofs!(element_dofs, dh, ci)
for qp in 1:getnquadpoints(fvp)
Expand Down Expand Up @@ -419,7 +419,7 @@ function assemble_system!(K, f, dh, cvu, cvp)
end
## rhs
for (i, I) in pairs(range_u)
x = spatial_coordinate(cvu, qp, get_cell_coordinates(cell))
x = spatial_coordinate(cvu, qp, getcoordinates(cell))
b = exp(-100 * norm(x - Vec{2}((0.75, 0.1)))^2)
bv = Vec{2}((b, 0.0))
fe[I] += (ϕᵤ[i] bv) *
Expand Down
5 changes: 3 additions & 2 deletions docs/src/reference/grid.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ getfaceset
getedgeset
getvertexset
transform_coordinates!
get_cell_coordinates
get_cell_coordinates!
getcoordinates
getcoordinates!
Ferrite.get_node_coordinate
```

### Topology
Expand Down
3 changes: 2 additions & 1 deletion docs/src/topics/grid.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,13 @@ Ferrite.getnodes(grid::SmallGrid) = grid.nodes_test
Ferrite.getnodes(grid::SmallGrid, v::Union{Int, Vector{Int}}) = grid.nodes_test[v]
Ferrite.getnnodes(grid::SmallGrid) = length(grid.nodes_test)
Ferrite.get_coordinate_eltype(::SmallGrid) = Float64
Ferrite.get_coordinate_type(::SmallGrid{dim}) where dim = Vec{dim,Float64}
Ferrite.nnodes_per_cell(grid::SmallGrid, i::Int=1) = Ferrite.nnodes(grid.cells_test[i])
Ferrite.n_faces_per_cell(grid::SmallGrid) = nfaces(eltype(grid.cells_test))
```

These definitions make many of `Ferrite`s functions work out of the box, e.g. you can now call
`get_cell_coordinates(grid, cellid)` on the `SmallGrid`.
`getcoordinates(grid, cellid)` on the `SmallGrid`.

Now, you would be able to assemble the heat equation example over the new custom `SmallGrid` type.
Note that this particular subtype isn't able to handle boundary entity sets and so, you can't describe boundaries with it.
Expand Down
4 changes: 2 additions & 2 deletions src/Dofs/apply_analytical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function _apply_analytical!(
a::AbstractVector, dh::AbstractDofHandler, celldofinds, field_dim,
ip_fun::Interpolation{RefShape}, ip_geo::Interpolation, f::Function, cellset) where {dim, RefShape<:AbstractRefShape{dim}}

coords = get_cell_coordinates(get_grid(dh), first(cellset))
coords = getcoordinates(get_grid(dh), first(cellset))
ref_points = reference_coordinates(ip_fun)
dummy_weights = zeros(length(ref_points))
qr = QuadratureRule{RefShape}(dummy_weights, ref_points)
Expand All @@ -65,7 +65,7 @@ function _apply_analytical!(
length(f(first(coords))) == field_dim || error("length(f(x)) must be equal to dimension of the field ($field_dim)")

for cellnr in cellset
get_cell_coordinates!(coords, get_grid(dh), cellnr)
getcoordinates!(coords, get_grid(dh), cellnr)
celldofs!(c_dofs, dh, cellnr)
for (i, celldofind) in enumerate(celldofinds)
f_dofs[i] = c_dofs[celldofind]
Expand Down
57 changes: 30 additions & 27 deletions src/Grid/grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ end
Get the datatype for a single point in the grid.
"""
get_coordinate_type(grid::Grid{dim,C,T}) where {dim,C,T} = Vec{dim,T} # Node is baked into the mesh type.
get_coordinate_type(::Grid{dim,C,T}) where {dim,C,T} = Vec{dim,T} # Node is baked into the mesh type.

"""
toglobal(grid::AbstractGrid, vertexidx::VertexIndex) -> Int
Expand Down Expand Up @@ -389,7 +389,6 @@ to a Node.
"Returns the number of nodes of the `i`-th cell."
@inline nnodes_per_cell(grid::AbstractGrid, i::Int=1) = nnodes(grid.cells[i])

get_node_coordinate(grid, nodeid) = get_node_coordinate(getnodes(grid, nodeid))
"Return the number type of the nodal coordinates."
@inline get_coordinate_eltype(grid::AbstractGrid) = get_coordinate_eltype(first(getnodes(grid)))

Expand Down Expand Up @@ -682,41 +681,45 @@ function addnodeset!(grid::AbstractGrid, name::String, f::Function)
end

"""
get_cell_coordinates!(x::Vector{Vec{dim,T}}, grid::AbstractGrid, cell::Int)
get_cell_coordinates!(x::Vector{Vec{dim,T}}, grid::AbstractGrid, cell::AbstractCell)
Fills the vector `x` with the coordinates of a cell defined by either its cellid or the cell object itself.
getcoordinates(grid::AbstractGrid, idx::Union{Int,CellIndex})
getcoordinates(cache::CellCache)
Get a vector with the coordinates of the cell corresponding to `idx` or `cache`
"""
@inline function get_cell_coordinates!(x::Vector{Vec{dim,T}}, grid::Ferrite.AbstractGrid, cellid::Int) where {dim,T}
cell = getcells(grid, cellid)
get_cell_coordinates!(x, grid, cell)
@inline function getcoordinates(grid::AbstractGrid, idx::Int)
CT = get_coordinate_type(grid)
cell = getcells(grid, idx)
N = nnodes(cell)
x = Vector{CT}(undef, N)
getcoordinates!(x, grid, cell)
end
@inline getcoordinates(grid::AbstractGrid, cell::CellIndex) = getcoordinates(grid, cell.idx)

@inline function get_cell_coordinates!(x::Vector{Vec{dim,T}}, grid::Ferrite.AbstractGrid, cell::Ferrite.AbstractCell) where {dim,T}
"""
getcoordinates!(x::Vector{<:Vec}, grid::AbstractGrid, idx::Union{Int,CellIndex})
getcoordinates!(x::Vector{<:Vec}, grid::AbstractGrid, cell::AbstractCell)
Mutate `x` to the coordinates of the cell corresponding to `idx` or `cell`.
"""
@inline function getcoordinates!(x::Vector{Vec{dim,T}}, grid::Ferrite.AbstractGrid, cell::Ferrite.AbstractCell) where {dim,T}
node_ids = get_node_ids(cell)
@inbounds for i in 1:length(x)
x[i] = get_node_coordinate(grid, cell.nodes[i])
x[i] = get_node_coordinate(grid, node_ids[i])
end
return x
end

@inline get_cell_coordinates!(x::Vector{Vec{dim,T}}, grid::AbstractGrid, cell::CellIndex) where {dim, T} = get_cell_coordinates!(x, grid, cell.idx)
@inline get_cell_coordinates!(x::Vector{Vec{dim,T}}, grid::AbstractGrid, face::FaceIndex) where {dim, T} = get_cell_coordinates!(x, grid, face.idx[1])

@inline function getcoordinates!(x::Vector{Vec{dim,T}}, grid::Ferrite.AbstractGrid, cellid::Int) where {dim,T}
cell = getcells(grid, cellid)
getcoordinates!(x, grid, cell)
end
@inline getcoordinates!(x::Vector{Vec{dim,T}}, grid::AbstractGrid, cell::CellIndex) where {dim, T} = getcoordinates!(x, grid, cell.idx)

"""
get_cell_coordinates(grid::AbstractGrid, cell)
Return a vector with the coordinates of the vertices of cell number `cell`.
get_node_coordinate(grid::AbstractGrid, n::Int)
Return the coordinate of the `n`th node in `grid`
"""
@inline function get_cell_coordinates(grid::AbstractGrid, cell::Int)
dim = getdim(grid)
T = get_coordinate_eltype(grid)
_cell = getcells(grid, cell)
N = nnodes(_cell)
x = Vector{Vec{dim, T}}(undef, N)
get_cell_coordinates!(x, grid, _cell)
end
@inline get_cell_coordinates(grid::AbstractGrid, cell::CellIndex) = get_cell_coordinates(grid, cell.idx)
@inline get_cell_coordinates(grid::AbstractGrid, face::FaceIndex) = get_cell_coordinates(grid, face.idx[1])
get_node_coordinate(grid, n) = get_node_coordinate(getnodes(grid, n))

function cellnodes!(global_nodes::Vector{Int}, grid::AbstractGrid, i::Int)
cell = getcells(grid, i)
Expand Down
4 changes: 2 additions & 2 deletions src/L2_projection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function _assemble_L2_matrix(fe_values, set, dh)
celldofs!(cell_dofs, dh, cellnum)

fill!(Me, 0)
Xe = get_cell_coordinates(get_grid(dh), cellnum)
Xe = getcoordinates(get_grid(dh), cellnum)
reinit!(fe_values, Xe)

## ∭( v ⋅ u )dΩ
Expand Down Expand Up @@ -194,7 +194,7 @@ function _project(vars, proj::L2Projector, fe_values::AbstractValues, M::Integer
for (ic,cellnum) in enumerate(proj.set)
celldofs!(cell_dofs, proj.dh, cellnum)
fill!(fe, 0)
Xe = get_cell_coordinates(get_grid(proj.dh), cellnum)
Xe = getcoordinates(get_grid(proj.dh), cellnum)
cell_vars = vars[ic]
reinit!(fe_values, Xe)

Expand Down
4 changes: 2 additions & 2 deletions src/PointEval/PointEvalHandler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function _get_cellcoords(points::AbstractVector{Vec{dim,T}}, grid::AbstractGrid,
possible_cells = get(node_cell_dict, node, nothing)
possible_cells === nothing && continue # if node is not part of the subdofhandler, try the next node
for cell in possible_cells
cell_coords = get_cell_coordinates(grid, cell)
cell_coords = getcoordinates(grid, cell)
is_in_cell, local_coord = point_in_cell(geom_interpol, cell_coords, points[point_idx])
if is_in_cell
cell_found = true
Expand Down Expand Up @@ -332,7 +332,7 @@ function Base.iterate(p::PointIterator, state = 1)
cid = (p.ph.cells[state])::Int
local_coord = (p.ph.local_coords[state])::Vec
n = nnodes_per_cell(p.ph.grid, cid)
get_cell_coordinates!(resize!(p.coords, n), p.ph.grid, cid)
getcoordinates!(resize!(p.coords, n), p.ph.grid, cid)
point = PointLocation(cid, local_coord, p.coords)
return (point, state + 1)
end
Expand Down
4 changes: 1 addition & 3 deletions src/deprecations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import Base: push!
# @deprecate add!(ch::ConstraintHandler, fh::FieldHandler, dbc::Dirichlet) add!(ch, dbc)

@deprecate getcoordinates(node::Node) get_node_coordinate(node) true
@deprecate getcoordinates(args...) get_cell_coordinates(args...) true
@deprecate getcoordinates!(args...) get_cell_coordinates!(args...) true
@deprecate cellcoords!(x::Vector, dh::DofHandler, args...) get_cell_coordinates!(x, dh.grid, args...) false
@deprecate cellcoords!(x::Vector, dh::DofHandler, args...) getcoordinates!(x, dh.grid, args...) false

struct Cell{refdim, nnodes, nfaces}
function Cell{refdim, nnodes, nfaces}(nodes) where {refdim, nnodes, nfaces}
Expand Down
4 changes: 2 additions & 2 deletions src/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export
getedgeset,
getvertexset,
get_node_coordinate,
get_cell_coordinates,
get_cell_coordinates!,
getcoordinates,
getcoordinates!,
onboundary,
nfaces,
addnodeset!,
Expand Down
14 changes: 7 additions & 7 deletions src/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ cell. The cache is updated for a new cell by calling `reinit!(cache, cellid)` wh
- `reinit!(cc, i)`: reinitialize the cache for cell `i`
- `cellid(cc)`: get the cell id of the currently cached cell
- `getnodes(cc)`: get the global node ids of the cell
- `get_cell_coordinates(cc)`: get the coordinates of the cell
- `getcoordinates(cc)`: get the coordinates of the cell
- `celldofs(cc)`: get the global dof ids of the cell
- `reinit!(fev, cc)`: reinitialize [`CellValues`](@ref) or [`FaceValues`](@ref)
Expand Down Expand Up @@ -78,7 +78,7 @@ function reinit!(cc::CellCache, i::Int)
end
if cc.flags.coords
resize!(cc.coords, nnodes_per_cell(cc.grid, i))
get_cell_coordinates!(cc.coords, cc.grid, i)
getcoordinates!(cc.coords, cc.grid, i)
end
if cc.dh !== nothing && cc.flags.dofs
resize!(cc.dofs, ndofs_per_cell(cc.dh, i))
Expand All @@ -91,12 +91,12 @@ end
reinit!(cv::CellValues, cc::CellCache) = reinit!(cv, cc.coords)
reinit!(fv::FaceValues, cc::CellCache, f::Int) = reinit!(fv, cc.coords, f) # TODO: Deprecate?
# TODOL enable this after InterfaceValues are merges
# reinit!(iv::InterfaceValues, ic::InterfaceCache) = reinit!(iv, FaceIndex(cellid(ic.face_a), ic.face_a.current_faceid[]), get_cell_coordinates(ic.face_a),
# FaceIndex(cellid(ic.face_b), ic.face_b.current_faceid[]), get_cell_coordinates(ic.face_b), ic.face_a.cc.grid)
# reinit!(iv::InterfaceValues, ic::InterfaceCache) = reinit!(iv, FaceIndex(cellid(ic.face_a), ic.face_a.current_faceid[]), getcoordinates(ic.face_a),
# FaceIndex(cellid(ic.face_b), ic.face_b.current_faceid[]), getcoordinates(ic.face_b), ic.face_a.cc.grid)

# Accessor functions (TODO: Deprecate? We are so inconsistent with `getxx` vs `xx`...)
getnodes(cc::CellCache) = cc.nodes
get_cell_coordinates(cc::CellCache) = cc.coords
getcoordinates(cc::CellCache) = cc.coords
celldofs(cc::CellCache) = cc.dofs
cellid(cc::CellCache) = cc.cellid[]

Expand Down Expand Up @@ -124,7 +124,7 @@ calling `reinit!(cache, fi::FaceIndex)`.
- `reinit!(fc, fi)`: reinitialize the cache for face `fi::FaceIndex`
- `cellid(fc)`: get the current cellid (`faceindex(fc)[1]`)
- `getnodes(fc)`: get the global node ids of the *cell*
- `get_cell_coordinates(fc)`: get the coordinates of the *cell*
- `getcoordinates(fc)`: get the coordinates of the *cell*
- `celldofs(fc)`: get the global dof ids of the *cell*
- `reinit!(fv, fc)`: reinitialize [`FaceValues`](@ref)
Expand All @@ -148,7 +148,7 @@ function reinit!(fc::FaceCache, face::FaceIndex)
end

# Delegate methods to the cell cache
for op = (:getnodes, :get_cell_coordinates, :cellid, :celldofs)
for op = (:getnodes, :getcoordinates, :cellid, :celldofs)
@eval begin
function $op(fc::FaceCache, args...)
return $op(fc.cc, args...)
Expand Down
3 changes: 2 additions & 1 deletion test/test_abstractgrid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Ferrite.getnodes(grid::SmallGrid, v::Union{Int, Vector{Int}}) = grid.nodes_test[v]
Ferrite.getnnodes(grid::SmallGrid) = length(grid.nodes_test)
Ferrite.get_coordinate_eltype(::SmallGrid) = Float64
Ferrite.get_coordinate_type(::SmallGrid{dim}) where dim = Vec{dim,Float64}
Ferrite.nnodes_per_cell(grid::SmallGrid, i::Int=1) = Ferrite.nnodes(grid.cells_test[i])
Ferrite.n_faces_per_cell(grid::SmallGrid) = nfaces(eltype(grid.cells_test))

Expand Down Expand Up @@ -43,7 +44,7 @@
for cellid in 1:getncells(dh.grid)
fill!(Ke, 0)
fill!(fe, 0)
coords = get_cell_coordinates(dh.grid, cellid)
coords = getcoordinates(dh.grid, cellid)
reinit!(cellvalues, coords)
for q_point in 1:getnquadpoints(cellvalues)
= getdetJdV(cellvalues, q_point)
Expand Down
4 changes: 2 additions & 2 deletions test/test_grid_dofhandler_vtk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ end
ci = CellIterator(grid)
@test length(ci) == getncells(grid)
for c in ci
get_cell_coordinates(c)
getcoordinates(c)
getnodes(c)
n += cellid(c)
end
Expand All @@ -191,7 +191,7 @@ end
@test cellid(fc) == cell_id
# @test Ferrite.faceid(fc) == face_id
@test getnodes(fc) == collect(getcells(grid, cell_id).nodes)
@test get_cell_coordinates(fc) == get_cell_coordinates(grid, cell_id)
@test getcoordinates(fc) == getcoordinates(grid, cell_id)
@test length(celldofs(fc)) == 0 # Empty because no DofHandler given

# FaceIterator, also tests `reinit!(fv::FaceValues, fc::FaceCache)`
Expand Down
Loading

0 comments on commit 718059e

Please sign in to comment.