# How to add different meshes in JuAFEM?

I am using the package JuAFEM. I want to join three different meshes and make it into one. Below is the picture attached. Plz help.

mesh= mesh1 + mesh2 + mesh3

I will declare Julia the Queen of Languages.

ps. I just tried adding some meshes with + Didnâ€™t work. Ah wellâ€¦ Julia is still pretty good!

1 Like

I donâ€™t think it can work that way. haha
Do you have any suggestions?

Not an answer, but a related note that some of us are working on a separate package for meshes in general: https://github.com/JuliaGeometry/Meshes.jl

The plan is to add functionality for mesh composition after we polish some other building blocks.

If you translate the coordinates so they align you can use this (adjust `tol` based on the meshsize):

``````using Tensors, JuAFEM

function merge_grids(grid1::Grid{dim,CellType}, grid2::Grid{dim,CellType}; tol=0.01) where {N, dim, CellType <: Cell{<:Any, N}}
cellsâ€˛ = copy(grid1.cells)
nodesâ€˛ = copy(grid1.nodes)
nodemap = Dict{Int,Int}()
next = getnnodes(grid1) + 1
for (i2, n2) in enumerate(grid2.nodes)
found = false
for (i1, n1) in enumerate(grid1.nodes)
if norm(n1.x - n2.x) < tol
nodemap[i2] = i1
found = true
break
end
end
if !found
push!(nodesâ€˛, n2)
nodemap[i2] = next
next += 1
end
end
for c in grid2.cells
t = ntuple(N) do i
return nodemap[c.nodes[i]]
end
cellâ€˛ = CellType(t)
push!(cellsâ€˛, cellâ€˛)
end
return Grid(cellsâ€˛, nodesâ€˛)
end

merge_grids(grid1::G, grid2::G, grids::G...) where G <: Grid =
merge_grids(merge_grids(grid1, grid2), grids...)
``````

Example:

``````using JuAFEM, Tensors

# 5-----6
# |     |
# |  2  |
# 3-----4
# |     |
# |  1  |
# 1-----2

coord1 = Vec{2}((0.0, 0.0))
coord4 = Vec{2}((1.0, 1.0))
coord3 = Vec{2}((0.0, 1.0))
coord6 = Vec{2}((1.0, 2.0))

grid1 = generate_grid(Triangle, (3,3), coord1, coord4)
grid2 = generate_grid(Triangle, (3,3), coord3, coord6)

merge_grids(grid1, grid2)
``````
2 Likes

Thank you for your reply, but the function file doesnot seem to be working, I run your fuction file and the example and export it to vtk and viewed on paraview but in your example the grids overlap each other. I only intend to join my grids. The co ordinates in my grid are well defined.