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.1


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!

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:

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
        if !found
            push!(nodes′, n2)
            nodemap[i2] = next
            next += 1
    for c in grid2.cells
        t = ntuple(N) do i
            return nodemap[c.nodes[i]]
        cell′ = CellType(t)
        push!(cells′, cell′)
    return Grid(cells′, nodes′)

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


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)

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

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.

1 Like

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