Mesh refinement

Mesh refinement#

Hide code cell source
from collections import OrderedDict

import matplotlib.pyplot as plt
import numpy as np
import shapely
import shapely.affinity
from skfem import Basis, ElementTriP0, adaptive_theta
from skfem.io.meshio import from_meshio

from femwell.maxwell.waveguide import compute_modes, eval_error_estimator
from femwell.mesh import mesh_from_OrderedDict
polygons_paper = OrderedDict(
    left=shapely.LineString(((0, 0), (0, 1))),
    right=shapely.LineString(((1, 0), (1, 1))),
    top=shapely.LineString(((0, 1), (1, 1))),
    core=shapely.box(0, 0, 0.5, 0.5),
    clad=shapely.box(0, 0, 1, 1),
)

boundaries = [
    [lambda x: x[0] == np.min(x[0]), lambda x: x[0] == np.max(x[0])],
    [lambda x: x[0] == np.min(x[0]), lambda x: x[0] == np.max(x[0])],
    [lambda x: x[0] == np.min(x[0]), lambda x: x[1] == np.max(x[1])],
    [lambda x: x[0] == np.min(x[0]), lambda x: x[1] == np.max(x[1])],
]

epsilons_paper = [
    {"core": 2.25, "clad": 1},
    {"core": 8, "clad": 1},
    {"core": 1, "clad": 2.25},
    {"core": 1, "clad": 8},
]

neff_values_paper = [1.27627404, 2.65679692, 1.387926425, 2.761465320]
num = 3

mesh = from_meshio(
    mesh_from_OrderedDict(polygons_paper, {}, default_resolution_max=0.1, filename="mesh.msh")
)
mesh.draw().show()
../../_images/fd2a1e6648433f7d09b72e78db9d567e787a646edf1bc2bb426c0156db36700d.png
for i in range(16):
    basis0 = Basis(mesh, ElementTriP0())
    epsilon = basis0.zeros()
    for subdomain, e in epsilons_paper[num].items():
        epsilon[basis0.get_dofs(elements=subdomain)] = e

    modes = compute_modes(
        basis0, epsilon, wavelength=1.5, num_modes=1, order=1, metallic_boundaries=boundaries[num]
    )
    modes[0].show(modes[0].E.real, direction="x")
    print(f"Error: {modes[0].n_eff - neff_values_paper[num]:.5g}")

    mesh = mesh.refined(adaptive_theta(eval_error_estimator(modes[0].basis, modes[0].E), theta=0.5))
    mesh.draw().show()
/tmp/ipykernel_9751/275219315.py:10: DeprecationWarning: The behavior of passing an array directly to `show` is deprecated and will be removed in the future. Use `plot` instead.
  modes[0].show(modes[0].E.real, direction="x")
../../_images/723ba0fffa68911d7efbf4abc7e9ebc09bebe08b1efd57e9a3fc784d7cc70fef.png
Named boundaries invalidated by a call to Mesh.refined()
Error: 4.2371e-05+0j
../../_images/ae94ef387ad22eb50c284e2b967f63742efe12b6f246889d88f79b4b06159a8a.png ../../_images/107c42ef26139ae78eee71677a4ba797ce7c2b71dfdbb7fa31fe6037ae4f2604.png
Error: -0.00011698+0j
../../_images/e99b5722290db5c732d16a275f63dbfd20c5b85e2cddad68c65165172806025b.png ../../_images/4205b6331117e9c5e0232aff3a1161edbaf1e426bc03b7b5cf30ae6139af845c.png
Error: -0.00014164+0j
../../_images/b6986a5a2075546b7740b2cf37a2ab15594a2b282e5bf3c03b530d7d4b971d3e.png ../../_images/c2958b4751c5d09f16eda3fd485deccb8c9bde89fd668a06be148a89ae0ef4c7.png
Error: -0.00014494+0j
../../_images/5624abc1052b18199c08b9138596da630df040cfa1aa589dab246829b4c7f090.png ../../_images/8ea06e318ee2365cc3a2e5783569d58f5bb68e316901636fa61c95d16bc60652.png
Error: -0.00014891+0j
../../_images/55f7db06d32d295a6c0a2039819f2a41b553e257168e6243d4faa8e8335d1b19.png ../../_images/334e141f178db0e2d39cd1ccb2f6a3652f2b8d0fc0fc86d78e74fd47be7df3c8.png
Error: -0.00013566+0j
../../_images/f0a8012f1a3d277dac14bb03874a99bfb00e137be5f5da3f7e7b528cfb31fd17.png ../../_images/f1188e222039c7b5b8d48ff6dc6e75a8e24e7a1e25f4764dac08dd7ed7383036.png
Error: -0.00013255+0j
../../_images/827a43478c7f9504c634d68762f5a33d232f05c15ca8fdff6270b30a6eeca639.png ../../_images/b8bc069e09b1b8120866838a90e27a62b0eb77ef4fef3c4862b85ce4e3c1dc0c.png
Error: -9.8289e-05+0j
../../_images/40ab09858dbb07965821ab592cb30399d9387313c6e16e66bb4a0e40edba9ae8.png ../../_images/3539df4fb2ea9e11673d02a1225de30acfe801fff7c4d30330f95e8e9e4a786b.png
Error: -5.0584e-05+0j
../../_images/d5362bfc954cfa37bffc5e7db1468c7e3309a201f8cd3c4b1b243b3ff42ac3af.png ../../_images/c9b219b1032b531000d34b0eabfb9d80b1d028f234a203c64ce204b6c41e4d4b.png
Error: -4.1234e-05+0j
../../_images/94cbe435e948705c82cc3ee2a781c005838cad4cdbd099e3888d979fd0b2841f.png ../../_images/e15d43ea5528e6bc8985a9aae1e000566311c53af99ac66d2fb71b8760d9e202.png
Error: -3.4453e-05+0j
../../_images/15bd8d211d3a7eaa5685d1d9a1b1ee2935303522c733c4cb5fe8b6de92abe123.png ../../_images/0cbbc36b57eefc957a1424391812018f90bb3d788b4c3712cde615ec9d138d6c.png
Error: -2.3801e-05+0j
../../_images/2e321e9c5e22ecc8f3b56df1edc5a35a207afdce3ee5cf72de234c6aede53904.png ../../_images/034799c5554855f719140c22bd964e1b469be11add8c0c6add54df823c56febe.png
Error: -1.5849e-05+0j
../../_images/0ebede46a9ceed91aaa30680f3e0a11b073e91be3381e82f64f88a9c34ff0457.png ../../_images/c0789c29bc3c45eaa85f705fef461f3b97eeed8b0ec4e33325aac8c53fa77b2d.png
Error: -1.139e-05+0j
../../_images/bc52232998e29ac24f888f9c36ddddc6056ca92c5155509230bd565428bf3983.png ../../_images/42ffb2714f11380ffd84159deb073eb122d9a807b9f1f042061437fa1c97b74c.png
Error: -9.823e-06+0j
../../_images/9cc40ced502125f7cc0c9704b47e9cb4bcec47d94213b7b0713d798cfd389359.png ../../_images/135b90a8e825cbfead15c48664099a2936be2f7a0fe788d8bda0a813ad1a21eb.png
Error: -7.9428e-06+0j
../../_images/f505f4418eefda6d196b9e4e86d94c07fc18e9b6c7b87b34152d1a6fafb03b10.png