Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate gmsh plugin #159

Merged
merged 10 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions docs/api_design.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ Meshing
.. autosummary::
:toctree: _autosummary/

create_physical_mesh
mesh_from_polygons
MeshTracker
cleanup_component
fuse_polygons
round_coordinates
tile_shapes
to_polygons
get_layer_overlaps_z
get_layers_at_z
list_unique_layer_stack_z
Expand Down
3 changes: 1 addition & 2 deletions docs/notebooks/meshing_01_intro.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@
from gdsfactory.technology import LayerStack
from skfem.io import from_meshio

from gplugins.gmsh.get_mesh import get_mesh
from gplugins.gmsh.mesh import create_physical_mesh
from gplugins.gmsh.get_mesh import create_physical_mesh, get_mesh

gf.config.rich_output()
PDK = get_generic_pdk()
Expand Down
3 changes: 1 addition & 2 deletions docs/notebooks/meshing_02_2D_xy_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
from gdsfactory.technology import LayerStack
from skfem.io import from_meshio

from gplugins.gmsh.get_mesh import get_mesh
from gplugins.gmsh.mesh import create_physical_mesh
from gplugins.gmsh.get_mesh import create_physical_mesh, get_mesh

gf.config.rich_output()
PDK = get_generic_pdk()
Expand Down
3 changes: 1 addition & 2 deletions docs/notebooks/meshing_03_2D_uz_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
from gdsfactory.technology import LayerStack
from skfem.io import from_meshio

from gplugins.gmsh.get_mesh import get_mesh
from gplugins.gmsh.mesh import create_physical_mesh
from gplugins.gmsh.get_mesh import create_physical_mesh, get_mesh

gf.config.rich_output()
PDK = get_generic_pdk()
Expand Down
60 changes: 7 additions & 53 deletions docs/notebooks/meshing_04_refinement.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@
# # %matplotlib widget

# +
from itertools import product

import gdsfactory as gf
import meshio
import numpy as np
from gdsfactory.generic_tech import get_generic_pdk
from gdsfactory.pdk import get_layer_stack
from gdsfactory.technology import LayerStack
from skfem.io import from_meshio

from gplugins.gmsh.get_mesh import get_mesh
from gplugins.gmsh.mesh import create_physical_mesh
from gplugins.gmsh.get_mesh import create_physical_mesh, get_mesh

PDK = get_generic_pdk()
PDK.activate()
Expand Down Expand Up @@ -99,7 +96,7 @@ def mesh_with_physicals(mesh, filename):
#
# An advantage of finite-volume and finite-element schemes is the ability for different nodes to have different characteristics lengths.
#
# This simply achieved to first order here by supplying a `resolutions` dict with keys referencing the `LayerStack` names, and for value a second dict with keys `resolution` and `distance` which control, respectively, the characteristic length within a region and the dropoff away from interfaces with this region.
# This simply achieved to first order here by supplying a `resolutions` dict with keys referencing the `LayerStack` names, and for value a second dict with keys `resolution` and `DistMax / SizeMax` (see gmsh documentation) which control, respectively, the characteristic length within a region and the dropoff away from interfaces with this region.
#
# For example, to refine within the core only, one could use:

Expand All @@ -120,7 +117,7 @@ def mesh_with_physicals(mesh, filename):

# Adding a dropoff at the interface:

resolutions = {"core": {"resolution": 0.05, "distance": 5}}
resolutions = {"core": {"resolution": 0.05, "DistMax": 1, "SizeMax": 0.2}}
mesh = get_mesh(
component=waveguide_trimmed,
type="uz",
Expand All @@ -138,10 +135,10 @@ def mesh_with_physicals(mesh, filename):
# Refining multiple elements simultaneously:

resolutions = {
"core": {"resolution": 0.05, "distance": 1},
"slab90": {"resolution": 0.02, "distance": 1},
"via_contact": {"resolution": 0.2, "distance": 0},
"oxide": {"resolution": 1, "distance": 0},
"core": {"resolution": 0.05, "DistMax": 1, "SizeMax": 0.2},
"slab90": {"resolution": 0.02, "DistMax": 1, "SizeMax": 0.2},
"via_contact": {"resolution": 0.2},
"oxide": {"resolution": 1},
}
mesh = get_mesh(
component=waveguide_trimmed,
Expand All @@ -156,46 +153,3 @@ def mesh_with_physicals(mesh, filename):
mesh = mesh_with_physicals(mesh, filename)
mesh = from_meshio(mesh)
mesh.draw().show()

# ## Fine mesh refinement
#
# You can fine mesh refine with the `global_meshsize_array` (default `None`) and `global_meshsize_interpolant_func` (default `scipy.interpolate.NearestNDInterpolator`) arguments, which define the characteristic length callback used by gmsh to select characteristic lengths at a local level.
#
# The `global_meshsize_array` has form [x,y,z,lc], with `x,y,z` in mesh units; here, `x` is `u` $\in$ [-2, 10] considering the y-coordinates of the xsection_bounds and the background padding, `y` $\in$ [-2,3], and `z` is always 0. These values could be estimated from the component bounding box.
#
# In practice, this array would most likely result from a physical simulation using the simulation domain coordinates, which would also yield a higher quality mesh by virtue of being smoother.
#
# For instance, if one wants to refine in a circle around the waveguide core, and have some asymmetry about the y-axis, a meshsize array like so could be defined:

# +
xs = np.linspace(-2, 10, 800)
ys = np.linspace(-2, 3, 800)
global_meshsize_array = []

ls_large = 1
ls_small = 0.05

r = 0.75
for x, y in product(xs, ys):
if (x - 4.0) ** 2 + (y) ** 2 <= r**2:
global_meshsize_array.append([x, y, 0, ls_small])
else:
global_meshsize_array.append([x, y, 0, ls_large])

global_meshsize_array = np.array(global_meshsize_array)

mesh = get_mesh(
component=waveguide_trimmed,
type="uz",
xsection_bounds=[(4, -4), (4, 4)],
layer_stack=filtered_layer_stack,
filename=f"{filename}.msh",
background_tag="oxide",
background_padding=(2.0, 2.0, 2.0, 2.0),
global_meshsize_array=global_meshsize_array,
default_resolution_min=ls_small,
default_resolution_max=ls_large,
)
mesh = mesh_with_physicals(mesh, filename)
mesh = from_meshio(mesh)
mesh.draw().show()
3 changes: 1 addition & 2 deletions docs/notebooks/meshing_05_3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
from gdsfactory.technology import LayerStack
from skfem.io import from_meshio

from gplugins.gmsh.get_mesh import get_mesh
from gplugins.gmsh.mesh import create_physical_mesh
from gplugins.gmsh.get_mesh import create_physical_mesh, get_mesh

PDK = get_generic_pdk()
PDK.activate()
Expand Down
19 changes: 2 additions & 17 deletions gplugins/gmsh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@
map_unique_layer_stack_z,
order_layer_stack,
)
from gplugins.gmsh.get_mesh import get_mesh
from gplugins.gmsh.mesh import create_physical_mesh, mesh_from_polygons
from gplugins.gmsh.meshtracker import MeshTracker
from gplugins.gmsh.parse_gds import (
cleanup_component,
fuse_polygons,
round_coordinates,
tile_shapes,
to_polygons,
)
from gplugins.gmsh.get_mesh import create_physical_mesh, get_mesh
from gplugins.gmsh.uz_xsection_mesh import (
get_u_bounds_layers,
get_u_bounds_polygons,
Expand All @@ -26,10 +17,6 @@
from gplugins.gmsh.xy_xsection_mesh import xy_xsection_mesh

__all__ = [
"MeshTracker",
"cleanup_component",
"create_physical_mesh",
"fuse_polygons",
"get_layer_overlaps_z",
"get_layers_at_z",
"get_u_bounds_layers",
Expand All @@ -38,12 +25,10 @@
"get_mesh",
"list_unique_layer_stack_z",
"map_unique_layer_stack_z",
"mesh_from_polygons",
"order_layer_stack",
"round_coordinates",
"tile_shapes",
"to_polygons",
"uz_xsection_mesh",
"xy_xsection_mesh",
"create_physical_mesh",
]
__version__ = "0.0.2"
143 changes: 0 additions & 143 deletions gplugins/gmsh/break_geometry.py

This file was deleted.

39 changes: 39 additions & 0 deletions gplugins/gmsh/define_polysurfaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import Any

from gdsfactory.technology import LayerStack
from meshwell.polysurface import PolySurface
from shapely.affinity import scale


def define_polysurfaces(
polygons_dict: dict,
layer_stack: LayerStack,
model: Any,
resolutions: dict,
scale_factor: float = 1,
):
"""Define meshwell polysurfaces dimtags from gdsfactory information."""
polysurfaces_list = []

if resolutions is None:
resolutions = {}

for layername in polygons_dict.keys():
if polygons_dict[layername].is_empty:
continue

polysurfaces_list.append(
PolySurface(
polygons=scale(
polygons_dict[layername],
*(scale_factor,) * 2,
origin=(0, 0, 0),
),
model=model,
resolution=resolutions.get(layername, None),
mesh_order=layer_stack.layers.get(layername).mesh_order,
physical_name=layername,
)
)

return polysurfaces_list
Loading