Join two interval meshes into one

Hello,

I want to combine two interval meshes into one. I read this Post and I managed to make a single interval mesh with Mesh Editor of Fenics. How can I join two meshes? Here is my code

from dolfin import *
import numpy

mesh   = Mesh()
editor = MeshEditor()

topological_dim = 1
geometrical_dim = 1

num_local_vertices = 5
num_local_cells    = 4

num_global_vertices = num_local_vertices
num_global_cells    = num_local_cells

editor.open(mesh, "interval" , topological_dim, geometrical_dim)
editor.init_vertices_global(num_local_vertices, num_global_vertices)
editor.init_cells_global(num_local_cells, num_global_cells)

# Add vertices
editor.add_vertex(0, numpy.array([0.0], dtype='float'))
editor.add_vertex(0, numpy.array([0.5], dtype='float'))
editor.add_vertex(0, numpy.array([1.0], dtype='float'))
editor.add_vertex(0, numpy.array([1.5], dtype='float'))
editor.add_vertex(0, numpy.array([2.0], dtype='float'))
# Add cell
editor.add_cell(0, numpy.array([0, 1], dtype='uint'))
editor.add_cell(1, numpy.array([1, 2], dtype='uint'))
editor.add_cell(1, numpy.array([2, 3], dtype='uint'))
editor.add_cell(1, numpy.array([3, 4], dtype='uint'))
# Close editor
editor.close()

plt.savefig("mesh.png")

If you are creating your interval mesh with the mesh editor, why do you need to join two meshes? Can’t you just create the coupled mesh in one go?
Please also note that you have not used the correct numbering in add_vertex or add_cell as you repeat the index 0 and one as input index.

Thank you for your answer. Actually, I want to make an interval mesh with two different regions. So maybe I used a wrong word for my question because I want to combine two meshes into one, including two regions. Lets say I want to build a mesh from x = [0,1] with 1^{st} region (fluid) ranging in x =[0,0.5] and 2^{nd} one (solid) in x = [0.5,1.0]

But that is still just one mesh, can’t you just use Subdomain to mark them, as shown in: https://fenicsproject.org/olddocs/dolfin/latest/python/demos/subdomains/documentation.html?highlight=subdomains

Yes I can mark them but I think that in this way I cant control easily the number of elements of each region. Anyway if there is no way to do it I will mark them

You can create your mesh with mesh editor, where you can control the number if elements in each region. See:


from dolfin import *
import numpy

mesh   = Mesh()
editor = MeshEditor()

A = 0
B = 0.75
C = 1
N_AB = 10 # Number of cells in AB
N_BC = 15 # Number of cells in BC

vertices_a = numpy.linspace(A, B, N_AB+1)
vertices_b = numpy.linspace(B, C, N_BC+1)[1:]
vertices = numpy.hstack([vertices_a, vertices_b])

topological_dim = 1
geometrical_dim = 1




num_local_vertices = N_AB + N_BC + 1
num_local_cells    = N_AB + N_BC

num_global_vertices = num_local_vertices
num_global_cells    = num_local_cells

editor.open(mesh, "interval" , topological_dim, geometrical_dim)
editor.init_vertices_global(num_local_vertices, num_global_vertices)
editor.init_cells_global(num_local_cells, num_global_cells)

# Add vertices
for i, vertex in enumerate(vertices):
    editor.add_vertex(i, numpy.array([vertex], dtype='float'))
# Add cells
for i in range(num_local_cells):
    editor.add_cell(i, numpy.array([i, i+1], dtype='uint'))

# Close editor
editor.close()

outfile = File("mesh.pvd") << mesh

Thank you so much for your answer. This is what I want