L-shape mesh generation with right element

Hi,
I would like to construct an L-shape domain with ‘right’ element( this is the aim).

To do so, I have constructed two rectangles and would like to add them together. How can I implement this?

from dolfin import *
import numpy as np
import matplotlib.pyplot as plt
from mshr import *

num_elem = 4
mesh1 = RectangleMesh(Point(0, 0), Point(1, 0.5), num_elem, num_elem)
plot(mesh1)
plt.show()
mesh2 = RectangleMesh(Point(0., 0.5), Point(0.5, 1), int(num_elem/2), int(num_elem/2))
plot(mesh2)
plt.show()
domain = mesh1 + mesh2

I don’t get what you mean by “right” element. But, for what its worth, the way to go would be something like:

from mshr import Rectangle, generate_mesh
from dolfin import *

rect1 = Rectangle(Point(0, 0), Point(0.5, 1))
rect2 = Rectangle(Point(0, 0), Point(1, 0.5))
mesh = generate_mesh(rect1 + rect2, 50)

Note that mshr is no longer actively maintained

Thank you for the reply.

what I mean is that I need my elements to be ‘right’ triangle (attached) not as polygons as in the case of Rectangle. Figure_1

Use gmsh for this with the Transfinite Surface command.

Another ad-hoc approach would be to generate a coarse mesh and then successively refine it.

rect1 = Rectangle(Point(0, 0), Point(0.5, 1))
rect2 = Rectangle(Point(0, 0), Point(1, 0.5))
mesh = generate_mesh(rect1 + rect2, 1)
num_refinements = 3
for i in range(num_refinements):
    mesh = refine(mesh)


Thank you for the reply and help.

However, I just need a right triangle element (attached fig)
Figure_1 )
and what refine produces is a crossed mesh ( i…e right/left attached).
Figure_2

You can build your mesh using the MeshEditor.
See for instance: Bitbucket

Thank you for your reply.
Can you provide me with a small example on how to construct as simple mesh with MeshEditor?

The link above showed how to make it for a single cell. In this post I’ve now added a simple L-shape mesh with “left” diagonals.

import matplotlib.pyplot as plt
from dolfin import *
import numpy
# Create mesh object and open editor
mesh = Mesh()
editor = MeshEditor()
topological_dim = 2
geometrical_dim = 2
num_local_vertices = 8
num_global_vertices = num_local_vertices  # True if run in serial
num_local_cells = 6
num_global_cells = num_local_cells
editor.open(mesh, "triangle", 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, 0.0], dtype='float'))
editor.add_vertex(1, numpy.array([1.0, 0.0], dtype='float'))
editor.add_vertex(2, numpy.array([0.0, 1.0], dtype='float'))
editor.add_vertex(3, numpy.array([1.0, 1.0], dtype='float'))
editor.add_vertex(4, numpy.array([0.0, 2.0], dtype='float'))
editor.add_vertex(5, numpy.array([1.0, 2.0], dtype='float'))
editor.add_vertex(6, numpy.array([3.0, 0.0], dtype='float'))
editor.add_vertex(7, numpy.array([3.0, 1.0], dtype='float'))
# Add cell
editor.add_cell(0, numpy.array([0, 1, 2], dtype='uint'))
editor.add_cell(1, numpy.array([1, 2, 3], dtype='uint'))
editor.add_cell(2, numpy.array([2, 3, 4], dtype='uint'))
editor.add_cell(3, numpy.array([3, 4, 5], dtype='uint'))
editor.add_cell(4, numpy.array([1, 6, 3], dtype='uint'))
editor.add_cell(5, numpy.array([6, 7, 3], dtype='uint'))
# Close editor
editor.close()

plot(mesh, color="r")
plt.savefig("mesh.png")

THANK YOU… This was extremely helpful.

Can you do something like this in dolfinx?

This is way easier in DOLFINx, as you can work directly with numpy arrays

from mpi4py import MPI
import dolfinx
import basix.ufl
import ufl
import numpy

geometrical_dim = 2
c_el = basix.ufl.element("Lagrange", "triangle", 1, shape=(geometrical_dim,))
ufl_domain = ufl.Mesh(c_el)

if MPI.COMM_WORLD.rank == 0:
    nodes = numpy.array(
        [
            [0.0, 0.0],
            [1.0, 0.0],
            [0.0, 1.0],
            [1.0, 1.0],
            [0.0, 2.0],
            [1.0, 2.0],
            [3.0, 0.0],
            [3.0, 1.0],
        ],
        dtype=numpy.float64,
    )
    cells = numpy.array(
        [[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [1, 6, 3], [6, 7, 3]],
        dtype=numpy.int64,
    )
else:
    nodes = numpy.empty((0, geometrical_dim), dtype=numpy.float64)
    cells = numpy.empty((0, 3), dtype=numpy.int64)

mesh = dolfinx.mesh.create_mesh(MPI.COMM_WORLD, cells, nodes, ufl_domain)
with dolfinx.io.XDMFFile(mesh.comm, "L_grid.xdmf", "w") as xdmf:
    xdmf.write_mesh(mesh)

This is also documented in Mesh generation — FEniCS Workshop