# How to add different meshes in JuAFEM?

**URL:** <https://discourse.julialang.org/t/how-to-add-different-meshes-in-juafem/49769>\
**Category:** General Usage\
**Tags:** juafem\
**Created:** [November 8, 2020, 5:54am UTC](https://discourse.julialang.org/t/how-to-add-different-meshes-in-juafem/49769 "2020-11-08T05:54:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hrishikesh\_Das](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hrishikesh_das/32/18408_2.png) [@Hrishikesh\_Das](https://discourse.julialang.org/u/Hrishikesh_Das)\
**Post date:** [November 8, 2020, 5:54am UTC](https://discourse.julialang.org/t/how-to-add-different-meshes-in-juafem/49769/1 "2020-11-08T05:54:06Z")

</div>

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. ![1](https://global.discourse-cdn.com/julialang/original/3X/5/d/5de7c6f2bc1d8936a8b273ca7352a516eff0c2ae.png)

 ![2](https://global.discourse-cdn.com/julialang/original/3X/e/0/e0d567fcf7b0a4d85402ad60f2e945b660a0be59.png) ![3](https://global.discourse-cdn.com/julialang/original/3X/9/3/93f477f2e19adc101b20b0aeb7346ce72a1728b2.png) ![final](https://global.discourse-cdn.com/julialang/original/3X/8/4/84223c896ced498e06d60e3a6c58b21fcee6d40e.png)

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [November 8, 2020, 10:12am UTC](https://discourse.julialang.org/t/how-to-add-different-meshes-in-juafem/49769/2 "2020-11-08T10:12:15Z")

</div>

If the answer is  
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!

---

<div class="post-metadata">

**Author:** ![Hrishikesh\_Das](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hrishikesh_das/32/18408_2.png) [@Hrishikesh\_Das](https://discourse.julialang.org/u/Hrishikesh_Das)\
**Post date:** [November 8, 2020, 3:00pm UTC](https://discourse.julialang.org/t/how-to-add-different-meshes-in-juafem/49769/3 "2020-11-08T15:00:56Z")

</div>

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

---

<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:** [November 8, 2020, 4:23pm UTC](https://discourse.julialang.org/t/how-to-add-different-meshes-in-juafem/49769/4 "2020-11-08T16:23:34Z")

</div>

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](https://github.com/JuliaGeometry/Meshes.jl)

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

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [November 8, 2020, 4:30pm UTC](https://discourse.julialang.org/t/how-to-add-different-meshes-in-juafem/49769/5 "2020-11-08T16:30:07Z")

</div>

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

```julia
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:

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![Hrishikesh\_Das](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hrishikesh_das/32/18408_2.png) [@Hrishikesh\_Das](https://discourse.julialang.org/u/Hrishikesh_Das)\
**Post date:** [November 9, 2020, 3:02am UTC](https://discourse.julialang.org/t/how-to-add-different-meshes-in-juafem/49769/6 "2020-11-09T03:02:48Z")

</div>

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.

grid1 = generate\_grid(QuadraticQuadrilateral,(10,10), Vec{2}((0.0,4.5)), Vec{2}((0.5,4.5)), Vec{2}((0.5,5.0)), Vec{2}((0.0,5.0)))

grid2 = generate\_grid(QuadraticQuadrilateral,(10,10), Vec{2}((0.0,0.0)), Vec{2}((5.0,0.0)), Vec{2}((0.5,4.5)), Vec{2}((0.0,4.5)))

grid3 = generate\_grid(QuadraticQuadrilateral,(10,10), Vec{2}((0.5,4.5)), Vec{2}((5.0,0.0)), Vec{2}((5.0,5.0)), Vec{2}((0.5,5.0)))

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [November 9, 2020, 9:52am UTC](https://discourse.julialang.org/t/how-to-add-different-meshes-in-juafem/49769/7 "2020-11-09T09:52:04Z")

</div>

Sorry, made a mistake in the process of simplifying the implementation, I corrected the code in the previous post and verified it works with your example.

---

<div class="post-metadata">

**Author:** ![Hrishikesh\_Das](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hrishikesh_das/32/18408_2.png) [@Hrishikesh\_Das](https://discourse.julialang.org/u/Hrishikesh_Das)\
**Post date:** [November 9, 2020, 2:18pm UTC](https://discourse.julialang.org/t/how-to-add-different-meshes-in-juafem/49769/8 "2020-11-09T14:18:34Z")

</div>

Thanks a ton from a beginner in Julia programming language, the code works fine.
