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
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 = 1

mesh = from_meshio(
    mesh_from_OrderedDict(polygons_paper, {}, default_resolution_max=0.08, filename="mesh.msh")
)
mesh.draw().show()
../../_images/5a68e0c23b9e63540f15e46356b7f53acbdb820b470ef8694a711ae47f452313.png
errors = []
nelements = []

for i in range(20):
    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, order=1, metallic_boundaries=boundaries[num]
    )
    modes[0].plot(modes[0].E.real, direction="x")
    plt.show()
    error = modes[0].n_eff.real - neff_values_paper[num]
    errors.append(error)
    nelements.append(mesh.nelements)
    elements_to_refine = adaptive_theta(modes[0].eval_error_estimator(), theta=0.5)
    print(
        f"Error {i:2d}: {error: .7f},\tElements: {mesh.nelements:10d}\tSelected: {len(elements_to_refine)}"
    )

    fig, ax = plt.subplots(1, 2)
    for a in ax:
        a.set_aspect("equal")
    mesh.draw(ax=ax[0])
    mesh_restricted = mesh.restrict(elements=elements_to_refine)
    mesh_restricted.draw(color="red", linewidth=2, ax=ax[0])
    mesh = mesh.refined(elements_to_refine)
    mesh.draw(ax=ax[1])
    mesh_restricted.draw(color="red", linewidth=2, ax=ax[1]).show()

fig, axs = plt.subplots(2, 1)
axs[0].plot(errors, label="Error")
axs[0].axhline(y=0, color="r", linestyle="-")
axs[0].set_ylabel("Error")
axs[1].plot(nelements)
axs[1].semilogy()
axs[1].set_xlabel("Iteration")
axs[1].set_ylabel("Number of elements")
plt.show()
../../_images/628beb23b90f81d4e35ee7e80a998bb167cb60dc8ba1aa30f929329a2f5f6f39.png
Named boundaries invalidated by a call to Mesh.refined()
Error  0: -0.0009402,	Elements:        484	Selected: 8
../../_images/008ef4dec6c351fcad54065b922f04731d35efd2a5ae5ab25bf80b08471f6db2.png ../../_images/bd6339fb74f8c6917ced7083badd3da9279fd59814fd8eea5fcec2ce752a2888.png
Error  1: -0.0009728,	Elements:        534	Selected: 26
../../_images/80cf413b360b8c4ecc87adaf0c8694d826752ecc689e1f38334e8d00df76ac2f.png ../../_images/d423393ad2decc41dd2a7713e992ec741a655a84870ee16f71bed9d77d7add23.png
Error  2: -0.0009282,	Elements:        697	Selected: 11
../../_images/3b10da14e0239cd3ccd691c77c83296e3b675e669c15e586ae7688cab91f5636.png ../../_images/d63ed3874cf6fce9cd314de9d83a076a8e3e14d9672e0a28d8ecef13076e8332.png
Error  3: -0.0008647,	Elements:        778	Selected: 72
../../_images/e3f92aa00bffce14651f71ad5740a32de781691b7f5f23c26c8fc08861d33e46.png ../../_images/e602df43abd46227b9bb7dad464703d3ac010674738816447e4a30a7ac00a74c.png
Error  4: -0.0005305,	Elements:       1174	Selected: 44
../../_images/9d1f4bca82243f5dfb3687a74568bf3ee32ba876e950ea916de1d6f55c9f152e.png ../../_images/d950b6f18552be639a00dc66e893d33b6f5ca344997eb1f56c76fc8db9e61fa6.png
Error  5: -0.0004795,	Elements:       1442	Selected: 120
../../_images/e986dca7650c4d0ee093be33201c7f889a19c15fe5553fb88e4840ce17a80990.png ../../_images/81ed8839eeed8f2f50bb0e4a837a4279efb0b3ec9bc5d4d8003badff04debe37.png
Error  6: -0.0003474,	Elements:       2147	Selected: 100
../../_images/dfe0cbf8c14b9226cfc3750a984c47c13988c0b426ce4143bd79ed1be1d0e5b9.png ../../_images/91efea3b7b589aee40a0410f069c1f3e1d59fd64a0095a1450a6320e78e453eb.png
Error  7: -0.0002198,	Elements:       2732	Selected: 248
../../_images/17f2448dcf086342209fe9785beb6d4bf677233c5a92e11d97d57813ce31cd15.png ../../_images/accff5aa6cc755772101ba0644a5b5472813bb708fe3ae106ebb527078a99f96.png
Error  8: -0.0001822,	Elements:       4046	Selected: 247
../../_images/a87e7eea69f8699f186b44e6972ea5a67590983a88f70b8fcb168e0a2f9e37ca.png ../../_images/6f2bd8faf1871ada3ec3c6209f74b54f3e5ca38cf82211e1bcfcdb9d0a83e4fc.png
Error  9: -0.0001337,	Elements:       5551	Selected: 482
../../_images/bbfbe6e25cdf5cfa2990f8bc259f1a276a9c75ec043872d364ae274dacae2b9e.png ../../_images/eafd49bd9914e376be37b29beae40f84853ebd7d8fa49692f408ad70c35e539d.png
Error 10: -0.0000891,	Elements:       8213	Selected: 488
../../_images/944dd35830312243e653ab547d1888bb9f441d949e4292cc1569df9e31b964a7.png ../../_images/dd451dfacfecbc12eeaa5e93df1cbb612f5fbae43fa749d583c01a76abaf2e0b.png
Error 11: -0.0000643,	Elements:      11156	Selected: 994
../../_images/9d9a304fe247c4416cf7e40d3103808effb6db72019450d4ff9ec8efa37b3893.png ../../_images/171f0efaabb66bdea32b1385c957d00da63f900ea026ae744e2da560e36752f1.png
Error 12: -0.0000486,	Elements:      16390	Selected: 853
../../_images/c0360083537faeace5ba45cabde767fefa069424b65655156e8b9b3b1d7303a0.png ../../_images/5f3643d408e22712a0991cb94ad99ff4d7a13e2f7584f866a7183dd6609a5322.png
Error 13: -0.0000372,	Elements:      21480	Selected: 1874
../../_images/916c03a928052bd876039193b40cc99694c89f7fc698450ba2737bd4e62e5c3c.png ../../_images/33912a62300d24ba2e8579b27b24c429c2d05216a6ac7799399e9638e8496333.png
Error 14: -0.0000250,	Elements:      31266	Selected: 1417
../../_images/861e617778d8f0a78eeea9c84acc77f547ff3e5ee88e986a0fd1980128d5ba01.png ../../_images/0e994f3f164a5b9baf1cbc59c15761316accf61d73b1e7f514b5f844e2702378.png
Error 15: -0.0000200,	Elements:      40060	Selected: 2756
../../_images/02f5b53eef7776231a0b9afa550810ef261b578e72af928b77940315d74bb9a0.png ../../_images/1a39a33bb8209dd9361f1df3c4047ec0c5d3260e0102af05f38b0dab8c5b9fc8.png
Error 16: -0.0000140,	Elements:      54708	Selected: 4203
../../_images/1463b06dd377364ba2fd11e5b799677f867a6ada95abd37170d4f6e246184de5.png ../../_images/183222b1fa046c5baa2e539f47f527a4e9c9e20d06be0e23c8bde8dfe9ea02ec.png
Error 17: -0.0000108,	Elements:      78269	Selected: 3754
../../_images/7c63bdb8103072f461bd8d4ae56c1d34d91d99db9e3935e1791e675def0af3fd.png ../../_images/56b0642da52bf1c00140dbe3cc4491138379230ef387dff8d1437d6e2d6dd2ad.png
Error 18: -0.0000079,	Elements:      99379	Selected: 6361
../../_images/69bb86497666725e7efbf51db0b3d7016162c669d7545c459716f2393c4ea5bb.png ../../_images/eb62aaf9106f49798c10985ab28cea200068750430dab17533071db987e59657.png
Error 19: -0.0000062,	Elements:     133490	Selected: 7457
../../_images/006dba6dce1d2a2899ebc24fd03b7a938a2639d3078834aec437fec3b37f9568.png ../../_images/045a56f8ae12ae5b002258803e71742a8856f81e6a3b2b29c2b11d43e0c72162.png