# Neighborhood information in Julia mesh Libraries

**URL:** <https://discourse.julialang.org/t/neighborhood-information-in-julia-mesh-libraries/97875>\
**Category:** General Usage\
**Tags:** meshio, mesh, meshes\
**Created:** [April 24, 2023, 7:09pm UTC](https://discourse.julialang.org/t/neighborhood-information-in-julia-mesh-libraries/97875 "2023-04-24T19:09:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mm-crj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mm-crj/32/45906_2.png) [@mm-crj](https://discourse.julialang.org/u/mm-crj)\
**Post date:** [April 24, 2023, 7:09pm UTC](https://discourse.julialang.org/t/neighborhood-information-in-julia-mesh-libraries/97875/1 "2023-04-24T19:09:33Z")

</div>

I have a mesh in msh format from Gmsh. I am importing it to Julia via Gmsh.jl. I need information about the neighbours of each cell in the grid. However none of the Julia libraries seem to provide that information. I have looked into `Mesh.jl`, `Gridap.jl` and `MeshIO.jl`. Does anyone know a library in julia that list the neighbors of all the cells?

I know it is not that difficult to write from the info Gmsh provides, however this is such a basic necessity for FVM and FEM methods to calculate flux that I would guess that such a function should exist.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [April 24, 2023, 7:40pm UTC](https://discourse.julialang.org/t/neighborhood-information-in-julia-mesh-libraries/97875/2 "2023-04-24T19:40:21Z")

</div>

MeshCore.jl

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [April 25, 2023, 12:46am UTC](https://discourse.julialang.org/t/neighborhood-information-in-julia-mesh-libraries/97875/3 "2023-04-25T00:46:56Z")

</div>

> [@mm-crj](#):
>
> I have a mesh in msh format from Gmsh. I am importing it to Julia via Gmsh.jl. I need information about the neighbours of each cell in the grid. However none of the Julia libraries seem to provide that information. I have looked into `Mesh.jl` and `MeshIO.jl`. Does anyone know a library that can atleast get me the adjacency matrix?

We do provide this as basic functionality. Take a look at the topological relations:

[https://juliageometry.github.io/Meshes.jl/stable/meshes.html#Relations](https://juliageometry.github.io/Meshes.jl/stable/meshes.html#Relations)

Or if you just need the adjacency matrix you can use the `adjacencymatrix` function implemented in terms of these relations:

> <https://github.com/JuliaGeometry/Meshes.jl/blob/a0487c6824d6ee9d7389edc25ae937f1e4cf26fd/src/matrices.jl#L109-L130>

## MWE:

```julia
julia> using Meshes

julia> grid = CartesianGrid(10,10)
10×10 CartesianGrid{2,Float64}
  minimum: Point(0.0, 0.0)
  maximum: Point(10.0, 10.0)
  spacing: (1.0, 1.0)

julia> adjacencymatrix(grid)
100×100 SparseArrays.SparseMatrixCSC{Int64, Int64} with 360 stored entries:
⠪⣢⡀⠀⠑⢄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠈⠪⡢⡀⠀⠘⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠑⢄⠀⠈⠪⡢⡀⠀⠉⢆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠈⠒⢄⠀⠈⠺⡢⡀⠀⠑⢢⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠣⢄⠀⠈⠠⡦⡀⠀⠑⠤⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠱⣀⠀⠈⠪⣢⡀⠀⠘⢄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠑⡄⠀⠈⠠⡢⡀⠀⠈⢆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠒⢄⠀⠈⠺⡢⡀⠀⠑⢢⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠢⢄⠀⠈⠊⡤⡀⠀⠑⠤⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠱⣀⠀⠈⠪⣢⡀⠀⠑⢄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⡄⠀⠈⠊⡠⡀⠀⠘⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠑⢄⠀⠈⠫⡢⡀⠀⠉⢆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠒⢄⠀⠈⠚⡠⡀⠀⠑⠢⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠣⢄⠀⠈⠪⡦⡀⠀⠑⠤⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠱⡀⠀⠈⠪⠂⡀⠀⠘⢄⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠑⡄⠀⠈⠫⡢⡀⠀⠉⢆⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠒⢄⠀⠈⠺⠂⡀⠀⠑⢢⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠣⢄⠀⠈⠪⡦⡀⠀⠑⠤⡀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠱⣀⠀⠈⠪⡢⡀⠀⠑⢄
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⡄⠀⠈⠪⡢⡀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠑⢄⠀⠈⠫⡢
 

```

---

<div class="post-metadata">

**Author:** ![mm-crj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mm-crj/32/45906_2.png) [@mm-crj](https://discourse.julialang.org/u/mm-crj)\
**Post date:** [April 25, 2023, 9:43am UTC](https://discourse.julialang.org/t/neighborhood-information-in-julia-mesh-libraries/97875/4 "2023-04-25T09:43:28Z")

</div>

I apologise for not reading the documentation well enough. However, I must admit it’s not that obvious to someone who is not really familiar with the package why this functionality must be under Relations and not under Neighbors.

Be that as it may, what I wanted to know more about, is the interoperability. Is there a way to import a mesh to `Mesh.jl`. I did it via

```julia
connec = connect.(connec, Ngon)
mesh = SimpleMesh(points, connec)

```

And that mesh it seems is of a different type than `Mesh` so the `adjacencymatrix(mesh)` doesn’t work for a `SimpleMesh`. Is there a different way to import or convert to `Mesh` type or any information regarding how to access the information inside `Adjacency{D}(t)` would be really helpful.

Thank you!

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [April 25, 2023, 12:16pm UTC](https://discourse.julialang.org/t/neighborhood-information-in-julia-mesh-libraries/97875/5 "2023-04-25T12:16:19Z")

</div>

The SimpleMesh constructor has an option to build topological relations. If you activate this option the adjacencymatrix should work.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [April 25, 2023, 1:38pm UTC](https://discourse.julialang.org/t/neighborhood-information-in-julia-mesh-libraries/97875/6 "2023-04-25T13:38:55Z")

</div>

Here is the docstring:

[https://juliageometry.github.io/Meshes.jl/stable/meshes.html#Meshes.SimpleMesh](https://juliageometry.github.io/Meshes.jl/stable/meshes.html#Meshes.SimpleMesh)

The option I mentioned is relations=true

---

<div class="post-metadata">

**Author:** ![mm-crj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mm-crj/32/45906_2.png) [@mm-crj](https://discourse.julialang.org/u/mm-crj)\
**Post date:** [April 25, 2023, 1:52pm UTC](https://discourse.julialang.org/t/neighborhood-information-in-julia-mesh-libraries/97875/7 "2023-04-25T13:52:58Z")

</div>

Yes, I tried that. It seems that function is incomplete in some sense. It gets stuck [here](https://github.com/JuliaGeometry/Meshes.jl/blob/a0487c6824d6ee9d7389edc25ae937f1e4cf26fd/src/toporelations/adjacency.jl#L105-L108).

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [April 25, 2023, 2:05pm UTC](https://discourse.julialang.org/t/neighborhood-information-in-julia-mesh-libraries/97875/8 "2023-04-25T14:05:51Z")

</div>

Oh sorry, we did not implement this case yet. Feel free to try to implement it yourself in terms of the relations already defined or if you are familiar with the half-edge topology data structure.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [April 25, 2023, 2:07pm UTC](https://discourse.julialang.org/t/neighborhood-information-in-julia-mesh-libraries/97875/9 "2023-04-25T14:07:35Z")

</div>

@mm-crj can you open an issue with a MWE, a small mesh where the adjacency matrix is not implemented? We can try to address the issue over the week.

---

<div class="post-metadata">

**Author:** ![mm-crj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mm-crj/32/45906_2.png) [@mm-crj](https://discourse.julialang.org/u/mm-crj)\
**Post date:** [April 25, 2023, 2:41pm UTC](https://discourse.julialang.org/t/neighborhood-information-in-julia-mesh-libraries/97875/10 "2023-04-25T14:41:55Z")

</div>

I will try to create a minimum working example. I think what doesn’t work is the adjacency of the _cells/elements_ for a half edge topology. Although the implementation seems doable(as I already have a half working example of my own), I am not sure if I can afford the time to learn the data structures.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [April 27, 2023, 5:10pm UTC](https://discourse.julialang.org/t/neighborhood-information-in-julia-mesh-libraries/97875/11 "2023-04-27T17:10:41Z")

</div>

The functionality is now available in the master branch of Meshes.jl, i.e., the `adjacencymatrix` function now works with half-edge topologies and general 2-manifold meshes.

---

<div class="post-metadata">

**Author:** ![mm-crj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mm-crj/32/45906_2.png) [@mm-crj](https://discourse.julialang.org/u/mm-crj)\
**Post date:** [May 7, 2023, 10:25am UTC](https://discourse.julialang.org/t/neighborhood-information-in-julia-mesh-libraries/97875/12 "2023-05-07T10:25:07Z")

</div>

Thank you for the quick fix!
