Combine Rectangles at various angles

I want to build a geometry which looks like a “Y” - 2 rectangles each rotated by 30 degrees off vertical, and a third below. I have found making the three rectangles separately, I can rotate the meshes, but then I cannot recombine them into one domain.

Is it possible to combine meshes, or is it possible to rotate a CSG geometry object before generating a mesh?

Here is an example

from fenics import *
from mshr import *

p1 = Point(0, 0)
p2 = Point(1, 2)
r1 = Rectangle(p1, p2)
mesh1 = generate_mesh(r1, 10)

p3 = Point(0.5, 2)
p4 = Point(1.5, 4)
r2 = Rectangle(p3, p4)
mesh2 = generate_mesh(r2, 10)

p6 = Point(-0.5, 4)
r3 = Rectangle(p3, p6)
mesh3 = generate_mesh(r3, 10)

mesh3.rotate(-30)
mesh2.rotate(30)

# unsupported operand
domain_mesh = mesh1 + mesh2 + mesh3

# would like
# r2.rotate(30)
# r3.rotate(-30)
# domain = r1 + r2 + r3
# mesh = generate_mesh(domain, 10)

This should be doable with mshr.Polygon. Since mshr is not maintained, I would suggest using an external mesh generator, such as Gmsh (with/without pygmsh). Another nice tool that I have used for smaller applications is @nschloe 's excellent dmsh.

See the following for something that should give you what you need:

import numpy as np
import dmsh as dm
import meshio
from dolfin import *

# define the corners of the polygon 
Lo, bo = 2., 1
polygon = np.array([
    [0., 0],
    [bo, 0],
    [bo, Lo], 
    [Lo * np.cos(pi/6) + bo, Lo * np.sin(pi/6) + Lo],
    [0.5*bo + Lo * np.cos(pi/6), 3**(0.5) * bo/2. + Lo + Lo * np.sin(pi/6)],
    [0.5*bo, Lo + 3**(0.5) * bo/2.],
    [0.5*bo - Lo * np.cos(pi/6), 3**(0.5) * bo/2. + Lo + Lo * np.sin(pi/6)],
    [-Lo * np.cos(pi/6), Lo * np.sin(pi/6) + Lo],
    [0, Lo],
], float)

points, cells = dm.generate(dm.Polygon(polygon), 0.1 )
meshio.xdmf.write("Yshaped.xdmf", meshio.Mesh(points, cells = {"triangle": cells}))
mesh = Mesh()
XDMFFile("Yshaped.xdmf").read(mesh)
plot(mesh)
1 Like

Thank you! That did exactly what I wanted!