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/995a18119031cd9b891db9fe10b7b58336f924b304eaef942b5a86f21eade2c1.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_7902/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/4bfcd34d82880cec5080a6afdcd2b9724aeca8219d3c3b98a34bf633a9a39fa8.png
Error: 4.2371e-05+0j
../../_images/4d96e3488cceafa85d5bbe9455ffc35a8044f7e1e6d30da8dcbdc7c443e135c4.png ../../_images/b218753eae9bdc6de4a4dbc6a0dc9ad8ccab7a82159c15296f66a2970a2a4cf5.png
Error: -0.00011698+0j
../../_images/f7ecb95600df071655865b439a8bdc904596a9280538ee14b0f8bd787b44189e.png ../../_images/19eb3fbc914a14f38d0605dd749174a26b7666ac7bf7d547fe4e2c45e2d7c28e.png
Error: -0.00014164+0j
../../_images/c3cdfa17665fbb74553a36737daf83b6ab10e980b2b4000b84011b6558c361ba.png ../../_images/e8554bac8155783682a65835edecdd9ba70b52c0e88ce8eff22e389c100c41e1.png
Error: -0.00014494+0j
../../_images/ec50bde4e82586f58d48773294adc2b1ffa37e8aeebe704f83053762e66d2917.png ../../_images/6a7c4501144c85120bb0ce1a079b3ec0618288d00b9cd467e98b1a3578462a3a.png
Error: -0.00014891+0j
../../_images/7f61e777ca8dde9d6b078c56c5b2ef06a1d3fe4c4e019f711303b1dd24d3afb3.png ../../_images/075fee6503148a390f77472734daebca6e336d607a81d7c999ca75f1938a9587.png
Error: -0.00013566+0j
../../_images/a78653d8032add87bd788202901b237f681927b5f18e7f653b1c50ea6590d4bc.png ../../_images/2beddb9694412b10d631d3688412a31a81ec01bdf0c7c5309cf57e6ea43a8ee8.png
Error: -0.00013255+0j
../../_images/05f3d29378de847b5c0ef803ac377c0e4b86fbe0573bf79d7641c6db03fd6868.png ../../_images/0552a16d86a14526ebb5de10ae483f7aecc1c0efb2492dd9accaf5f3359045f4.png
Named boundaries invalidated by a call to Mesh.refined()
Error: -9.8289e-05+0j
../../_images/5949b707bdaabfa7eb1b7e379f6adca320252f9189f2d8ccd252041367f80986.png ../../_images/227aad068b48810a2970645445347dc08dc19e95ac43651fa50c3d1b26b898dc.png
Error: -5.0584e-05+0j
../../_images/b1a7642041c89591e229c9d232e19b3b56c8ebd243b1f0a5842041a4c159571b.png ../../_images/d6ce57560a3491c719fa354377e57b5fe2358f9a4d66406a1720206cc5f23082.png
Error: -4.1234e-05+0j
../../_images/3026dc6baf9a03b7b224385f083ebb51bba40d486bb39b9151a0da99548881c4.png ../../_images/008abb8efd167c8ef682a9fd88310c61f9832a2d2b1851d4d2c3f9d37f10f9d9.png
Error: -3.4453e-05+0j
../../_images/360e168799f3e014b9088cbcca0877dd3d16adf5caa9d93aeaadcd4846fe0f2a.png ../../_images/7ffe52b26628a25534e7977b532cd951f75384e4bab0aeee5db7cec8bf07446d.png
Error: -2.3801e-05+0j
../../_images/002518196fe57ca524024f11c6e43891cd205f4a1fc9c56d4b3fb2550c95b2f2.png ../../_images/e289ffef93aff642c703fea0d3326d0c17f5b10723d9bb03ea8963b83ad4f95b.png
Error: -1.5849e-05+0j
../../_images/1a5b9bbcdafa5cac13690008948a48052ab649f525d961f8e394147f44f6ceb5.png ../../_images/de8d7e9976d279599ea181d65c248fff57d855133f49fc81810463a9319777a6.png
Error: -1.139e-05+0j
../../_images/3d00352514f66fdc0f2aa7bd06f3d29a2eef972b80b658ab476f9012ed36832c.png ../../_images/2c30dfd08305fb173c3fd6dd9720f24005fa24a757a23d9be79e761449c2673b.png
Error: -9.823e-06+0j
../../_images/50a6b0a6a83fee55777cb41cd71ae89a3f933e041b8806bd199ca92889c11d08.png ../../_images/4e774b8e4731d785611f0e60828afb85f9b2633dc82d86e2eed3d377a35d57e3.png
Error: -7.9428e-06+0j
../../_images/8248b7b64d3b6557f0be79da4e6871b5ce1c9e0cadc26c47028bfeba76882d2d.png