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

Allow evaluating FE functions at arbitrary points #523

Merged
merged 68 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
6064415
Allow evaluating FE functions at arbitrary points
eschnett Jan 26, 2021
f123693
Add function to evaluate FE functions
eschnett Jan 26, 2021
7e785be
New function inverse_map
ericneiva Jan 4, 2021
027fa14
bugfixed in CellField change_domain
ericneiva Jan 19, 2021
81bf213
Move code to the right location
eschnett Jan 26, 2021
0fd60ff
Reformat code
eschnett Jan 26, 2021
a7350a4
Merge branch 'master' into eschnett/evaluate
eschnett Jan 26, 2021
b6a18d4
Import return_cache
eschnett Jan 26, 2021
06b3524
Remove dependency on Bernstein.jl
eschnett Jan 27, 2021
c363d2d
Change sign convention for distance from simplex
eschnett Jan 27, 2021
0fcba26
Merge branch 'master' into eschnett/evaluate
eschnett Jan 27, 2021
489a377
Simplify finding minimum distance from simplices
eschnett Jan 27, 2021
01ef2f1
Calculate k-d tree only when needed
eschnett Jan 27, 2021
5103e07
simplexify: return tet simplex instead of hex simplex in 1d
eschnett Jan 28, 2021
bc6d049
Move evaluation function to MultiFields module
eschnett Jan 28, 2021
d1548f1
Comment code
eschnett Jan 28, 2021
c658948
Merge branch 'master' into eschnett/evaluate
eschnett Jan 31, 2021
921ced8
Merge branch 'master' into eschnett/evaluate
eschnett Feb 2, 2021
e7b2ddc
Merge branch 'master' into eschnett/evaluate
eschnett Feb 3, 2021
1e92b75
Merge branch 'master' into eschnett/evaluate
eschnett Feb 5, 2021
4f33d9b
Merge branch 'master' into eschnett/evaluate
eschnett Feb 28, 2021
9bd3a22
Add various comments with ideas or questions
eschnett Feb 28, 2021
c6ebff7
Add progress output while running test case
eschnett Feb 28, 2021
af56037
Shorten code
eschnett Feb 28, 2021
6b081cf
Rewrite code, making use of existing Gridap features
eschnett Mar 4, 2021
7c6038d
Merge branch 'master' into eschnett/evaluate
eschnett Mar 4, 2021
d6187d1
Commit current state
eschnett Mar 4, 2021
b2f4775
Merge branch 'master' into eschnett/evaluate
eschnett Mar 5, 2021
d5f15e6
Merge branch 'affine_map' into eschnett/evaluate
eschnett Mar 5, 2021
7544e8c
Things seem to work now.
eschnett Mar 5, 2021
0d765f1
Remove left-over comments
eschnett Mar 5, 2021
06ec403
Correct error bounds in self check
eschnett Mar 5, 2021
73672a3
Avoid temporary array
eschnett Mar 5, 2021
43afc11
Allow more general cell index types
eschnett Mar 5, 2021
f8998e3
Correct indentation
eschnett Mar 5, 2021
2f2e82f
Use vertex coordinates, not node coordinates to locate cells
eschnett Mar 6, 2021
aab614f
Merge branch 'master' into eschnett/evaluate
eschnett Mar 9, 2021
d02b072
Merge branch 'master' into eschnett/evaluate
eschnett Mar 9, 2021
3a393cd
Merge branch 'master' into eschnett/evaluate
eschnett Mar 25, 2021
db06d38
Merge branch 'master' into eschnett/evaluate
eschnett Mar 29, 2021
2c7bacd
Merge branch 'master' into eschnett/evaluate
eschnett Apr 9, 2021
7fccd3c
Merge branch 'master' into eschnett/evaluate
eschnett Apr 15, 2021
8d11d5c
Commit all changes
eschnett Apr 26, 2021
28a8563
Merge branch 'master' into eschnett/evaluate
eschnett Apr 26, 2021
6873673
Add tests for inverse maps
eschnett Apr 28, 2021
c1628dc
Merge branch 'master' into eschnett/evaluate
eschnett Apr 29, 2021
7197cec
Correct module imports
eschnett Apr 29, 2021
785876a
Make evaluate! for CellField more efficient
eschnett Apr 30, 2021
b65f9f6
Test evaluating cell fields on arrays of points
eschnett Apr 30, 2021
57ac76a
Remove duplicate include statement
eschnett Apr 30, 2021
4fef3f1
Implement efficient vectorized evaluation
eschnett Apr 30, 2021
a2fd337
Merge branch 'master' of github.com:gridap/Gridap.jl into HEAD
Balaje May 29, 2021
7548314
Move code to src/CellData from src/MultiField
Balaje May 31, 2021
5823b9a
Fixing comment in CellFields and remove repeating InverseFields Tests
Balaje May 31, 2021
a818db8
Merge pull request #1 from Balaje/eschnett/evaluate
eschnett Jun 1, 2021
0f6663f
Add more tests for evaluate
Balaje Jun 1, 2021
008a46a
Remove unused deps in MultiField.jl, more test for evaluate
Balaje Jun 1, 2021
e034de1
Fix an error
Balaje Jun 1, 2021
54962d9
Fix error in MultiField.jl
Balaje Jun 1, 2021
5c445d1
Remove comment
Balaje Jun 1, 2021
8ade674
Change to not export distance
Balaje Jun 1, 2021
0b4d2d9
Remove unused argument from point_to_cell
Balaje Jun 1, 2021
7a79a6e
Add interface for AbstractVector{<:Point}
Balaje Jun 2, 2021
620ee7f
Fix function names
Balaje Jun 2, 2021
0af55ff
Change from CellPoint to compute_cell_points_from_vector_of_points
Balaje Jun 2, 2021
4ff072c
Merge branch 'master' of github.com:gridap/Gridap.jl into tmp
Balaje Jun 2, 2021
b7f83ba
Update NEWS.md
Balaje Jun 2, 2021
6ce661f
Merge pull request #2 from Balaje/eschnett/evaluate
eschnett Jun 2, 2021
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
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "0.15.1"
[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
Bernstein = "e9b0fb4c-9cb7-4f61-9c14-701a41c684d7"
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Expand All @@ -18,6 +19,7 @@ JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
LineSearches = "d3d80556-e9d4-5f37-9878-2ab0fcc64255"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56"
NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce"
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Expand All @@ -27,6 +29,7 @@ WriteVTK = "64499a7a-5c06-52f2-abe2-ccb03c286192"
[compat]
AbstractTrees = "0.3.3"
BSON = "0.2.5"
Bernstein = "1.0.1"
BlockArrays = "0.12.12, 0.13"
Combinatorics = "1.0.0"
DocStringExtensions = "0.8.1"
Expand All @@ -38,6 +41,7 @@ JLD2 = "0.1.11, 0.3"
JSON = "0.21.0"
LineSearches = "7.0.1"
NLsolve = "4.3.0"
NearestNeighbors = "0.4.8"
QuadGK = "2.3.1, 2.4"
StaticArrays = "0.12.1, 1.0"
WriteVTK = "1.7, 1.8"
Expand Down
4 changes: 4 additions & 0 deletions src/CellData/CellData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module CellData
using Test
using DocStringExtensions
using FillArrays
using Bernstein
using NearestNeighbors
using StaticArrays

using Gridap.Helpers
using Gridap.Arrays
Expand All @@ -21,6 +24,7 @@ using Gridap.Integration
import Gridap.Arrays: lazy_append
import Gridap.Arrays: get_array
import Gridap.Arrays: evaluate!
import Gridap.Arrays: return_cache
import Gridap.Fields: gradient
import Gridap.Fields: ∇∇
import Gridap.Fields: integrate
Expand Down
103 changes: 93 additions & 10 deletions src/CellData/CellFields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function change_domain(a::CellField,::ReferenceDomain,::PhysicalDomain)
trian = get_triangulation(a)
cell_map = get_cell_map(trian)
cell_invmap = lazy_map(inverse_map,cell_map)
cell_field_ref = get_data(cell_field)
cell_field_ref = get_data(a)
cell_field_phys = lazy_map(Broadcasting(∘),cell_field_ref,cell_invmap)
GenericCellField(cell_field_phys,trian,PhysicalDomain())
end
Expand Down Expand Up @@ -204,14 +204,97 @@ DomainStyle(::Type{GenericCellField{DS}}) where DS = DS()

(a::CellField)(x) = evaluate(a,x)

struct CellFieldCache
kdtree::KDTree{SVector{D,T},Euclidean,T} where {D,T}
end

function return_cache(f::CellField,x::Point)
# Create k-d tree for mesh
trian = get_triangulation(f)
node_coordinates = get_node_coordinates(trian)
@assert !isempty(node_coordinates)
sample_coord = node_coordinates[1]
D = length(sample_coord)
T = eltype(sample_coord)
@assert D > 0
kdtree = KDTree(map(nc -> SVector(nc.data), node_coordinates))
kdtree::KDTree{SVector{D,T},Euclidean,T}
return CellFieldCache(kdtree)
end

function evaluate!(cache,f::CellField,x::Point)
@notimplemented """\n
Evaluation of a CellField at a given Point is not implemented yet.
cache::CellFieldCache

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably encapsulate this code in a function and do dispatching with respect to the triangulation type. The code below is for an unstructured (simplicial) mesh. We could also consider the case for a structured Cartesian mesh (which is much easier, no tree involved).

# Get k-d tree for mesh from cache
trian = get_triangulation(f)
node_coordinates = get_node_coordinates(trian)
@assert !isempty(node_coordinates)
sample_coord = node_coordinates[1]
D = length(sample_coord)
T = eltype(sample_coord)
@assert D > 0
kdtree = cache.kdtree::KDTree{SVector{D,T},Euclidean,T}

# Find neighbouring cells
id,dist = nn(kdtree, SVector(x.data))
cells = Int[]
# TODO: Use better algorithm
# Francesc Verdugo @fverdugo 14:07
# You have to start with a DiscreteModel, then
# topo=get_grid_topology(model)
# vertex_to_cells = get_faces(topo,0,num_cell_dims(model))
for (c,ids) in enumerate(trian.cell_node_ids)
id ∈ ids && push!(cells, c)
end
@assert !isempty(cells)

# Find polytope
reffes = trian.reffes
@assert length(reffes) == 1
reffe = reffes[1].reffe
polytope = reffe.polytope
extrusion = polytope.extrusion
@assert length(extrusion) == D

# Loop over neighbouring cells and calculate distances
dists = T[]

if all(e == TET_AXIS for e in extrusion)
# Calculate barycentric coordinates for a point x in the given cell
function bary(cell, x)
local ids = trian.cell_node_ids[cell]
@assert length(ids) == D+1 "simplex has correct number of vertices"
local ys = zero(SVector{D+1,SVector{D,T}})
for (n,id) in enumerate(ids)
local y = SVector(node_coordinates[id].data)
ys = setindex(ys, y, n)
end
local λ = cartesian2barycentric(ys, SVector(x.data))
return λ
end

This is a feature that we want to have at some point in Gridap.
If you are ready to help with this implementation, please contact the
Gridap administrators.
"""
for c in cells
λ = bary(c, x)
# Negative distances are outside, positive distance are inside the simplex
dist = min(minimum(λ), 1 - maximum(λ))
push!(dists, dist)
end

dist, m = findmax(dists)
cell = cells[m]
λ = bary(cell, x)

# Ensure the point is inside one of the cells, up to round-off errors
@assert dist ≥ -1000 * eps(T) "Point is not inside a cell"

eschnett marked this conversation as resolved.
Show resolved Hide resolved
# TODO: Actually evaluate basis functions
values = f.cell_dof_values[cell]
fx = sum(λ[n] * values[n] for n in 1:D+1)
else
@assert false "Unsupported polytope"
end

return fx
end

function evaluate!(cache,f::CellField,x::CellPoint)
Expand Down Expand Up @@ -324,11 +407,11 @@ struct OperationCellField{DS} <: CellField
r = Fields.BroadcastingFieldOpMap(op.op)(axi...)
catch
@unreachable """\n
It is not possible to perform operation $(op.op) on the given cell fields.
It is not possible to perform the operation "$(op.op)" on the given cell fields.

See the caught error for more information. (If you are using Visual
See the caught error for more information. (If you are using the Visual
Studio Code REPL you might not see the caught error, please use the
command-line REPL).
command-line REPL instead).
"""
end
end
Expand Down
8 changes: 8 additions & 0 deletions src/Fields/AffineMaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,11 @@ function lazy_map(
cell_∇∇bt = lazy_map(transpose,cell_∇∇b)
cell_∇∇bt
end

function inverse_map(f::AffineMap)
Jt = f.gradient
y0 = f.origin
invJt = inv(Jt)
x0 = -y0⋅invJt
AffineMap(invJt,x0)
end