Polysurfaces

Polysurfaces#

The first object introduced by meshwell is the PolySurface, which adds a CAD constructor for shapely (multi)polygons.

import matplotlib.pyplot as plt
import shapely

from meshwell.cad import cad
from meshwell.mesh import mesh
from meshwell.polysurface import PolySurface
from meshwell.visualization import plot2D
# We use shapely as an API to enter (multi)polygons

# Initialize GMSH and create the mesh
polygon1 = shapely.Polygon(
    [[0, 0], [2, 0], [2, 2], [0, 2], [0, 0]],
    holes=([[0.5, 0.5], [1.5, 0.5], [1.5, 1.5], [0.5, 1.5], [0.5, 0.5]],),
)
polygon2 = shapely.Polygon([[-1, -1], [-2, -1], [-2, -2], [-1, -2], [-1, -1]])
polygon = shapely.MultiPolygon([polygon1, polygon2])

View the polygons:

plt.figure(figsize=(8, 8))
plt.plot(*polygon1.exterior.xy, "b-", label="Polygon 1 exterior")
plt.plot(*polygon1.interiors[0].xy, "b--", label="Polygon 1 hole")
plt.plot(*polygon2.exterior.xy, "r-", label="Polygon 2")
plt.axis("equal")
plt.title("Shapely Polygons")
plt.legend()
plt.show()
_images/3220f08565dae19d8eb035fa1957fbfdbf02d3bba16a9bf04e03b3afa560803a.png

Input the polygons into meshwell objects:

poly2D = PolySurface(
    polygons=polygon,
    physical_name="my_polysurface1",
)

entities_list = [poly2D]

First, generate a CAD representation:

cad(
    entities_list=entities_list,
    output_file="polysurface.xao",
)
Info    : Clearing all models and views...
Info    : Done clearing all models and views
Info    : [  0%] Difference                                                                                  
Info    : [ 10%] Difference                                                                                  
Info    : [ 20%] Difference                                                                                  
Info    : [ 30%] Difference                                                                                  
Info    : [ 40%] Difference                                                                                  
Info    : [ 50%] Difference                                                                                  
Info    : [ 70%] Difference - Filling splits of edges                                                                                
Info    : [ 80%] Difference - Making faces                                                                                
Info    : [ 90%] Difference - Adding holes                                                                                
                                                                                
Info    : [  0%] Union                                                                                  
Info    : [ 10%] Union - Performing intersection of shapes                                                                                
Info    : [ 80%] Union - Building splits of containers                                                                                
Info    : Writing 'polysurface.xao'...
Info    : Done writing 'polysurface.xao'

Then generate a mesh from the CAD:

output_mesh = mesh(
    dim=2,
    input_file="polysurface.xao",
    output_file="polysurface.msh",
    default_characteristic_length=100,
)
Info    : Clearing all models and views...
Info    : Done clearing all models and views
Info    : Reading 'polysurface.xao'...
Info    : Done reading 'polysurface.xao'
# View the mesh:

plot2D(output_mesh, wireframe=False)
_images/1f6710f880fa541b89dcb3c4573846a8b7901d8a57a87155e33a25806c56247c.png

Shapely is a convenient interface to draw complicated polygons:

polygon_hull = shapely.Polygon(
    [[0, 0], [2, 0], [2, 2], [0, 2], [0, 0]],
)
line1 = shapely.LineString([[0.5, 0.5], [1.5, 1.5]])
polygon_hole1 = shapely.buffer(line1, 0.2)
line2 = shapely.LineString([[1.5, 0.5], [0.5, 1.5]])
polygon_hole2 = shapely.buffer(line2, 0.2)
polygon = polygon_hull - polygon_hole1 - polygon_hole2
plt.figure(figsize=(8, 8))
plt.plot(*polygon.exterior.xy, "b-", label="Polygon 1 exterior")
plt.plot(*polygon.interiors[0].xy, "b--", label="Polygon 1 hole")
plt.axis("equal")
plt.title("Shapely Polygons")
plt.legend()
plt.show()
_images/4d00e1d9b83b85b0bc2d20b3c0a861d0f0d4481e6f9ec23b1d5ae31da969f65a.png
poly2D = PolySurface(
    polygons=polygon,
    physical_name="complicated",
)

entities_list = [poly2D]

cad(
    entities_list=entities_list,
    output_file="complicated.xao",
)

output_mesh = mesh(
    dim=2,
    input_file="complicated.xao",
    output_file="complicated.msh",
    n_threads=1,
    default_characteristic_length=100,
)
Info    : Clearing all models and views...
Info    : Done clearing all models and views
Info    : [  0%] Difference                                                                                  
Info    : [ 10%] Difference                                                                                  
Info    : [ 20%] Difference                                                                                  
Info    : [ 30%] Difference                                                                                  
Info    : [ 40%] Difference                                                                                  
Info    : [ 50%] Difference                                                                                  
Info    : [ 60%] Difference - Repeat intersection                                                                                
Info    : [ 70%] Difference                                                                                  
Info    : [ 80%] Difference - Filling splits of edges                                                                                
Info    : [ 90%] Difference - Making faces                                                                                
Info    : Writing 'complicated.xao'...
Info    : Done writing 'complicated.xao'
Info    : Clearing all models and views...
Info    : Done clearing all models and views
Info    : Reading 'complicated.xao'...
Info    : Done reading 'complicated.xao'
plot2D(output_mesh, wireframe=False)
_images/e688f2d7e8745078a08d3c8818627057a06d24ede1c26cc6268f3e37cb4946cb.png