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/f1e81d63b8631476d2da6f646ce50b20636b471df0e4d2508f6899466cfb7420.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/b1d22451db07c1b5666009e0df4915d8851a84a49ca11662b53588c484b5c7b3.png
Named boundaries invalidated by a call to Mesh.refined()
Error  0: -0.0009402,	Elements:        484	Selected: 8
../../_images/ea230bfb1347f7aaa0fc8fbcc4bfa937fbfeb9f4f4e64248609a40dafbefc566.png ../../_images/0333fb4e75be0a83941e4f79ee26b94cab48102969e64bf9a1ac9800a767b426.png
Error  1: -0.0009728,	Elements:        534	Selected: 26
../../_images/6c7d31465fee17ef578de8d1b309ce1328126781559ebe7ce7f8baa18556c664.png ../../_images/eff75c4a36413dc2b5e1f1f3e7e79b456101dfb575f1b201ba759d30c40da71f.png
Error  2: -0.0009282,	Elements:        697	Selected: 11
../../_images/a41e3bd918491be11b8f4baa1d8a0c31aa6e39c54bdf9d1bb4763a22f912cc0c.png ../../_images/b84bcf67d136e2ac4c254b36271dd7c11fd0b1f4e64b1cb678a337fe40fdf9b5.png
Error  3: -0.0008647,	Elements:        778	Selected: 72
../../_images/51fe711803e7adbadb06ee36f9a25a2ba1290d7e8aeb81af7025b6e5cfe58e99.png ../../_images/620cf5f46c1a26733b1937c751955722cd07f01e0e5e0b4f436b435dd5ce819d.png
Error  4: -0.0005305,	Elements:       1174	Selected: 44
../../_images/afa24d73d6383e236b0a4e26944b8fe8489a8fece1f5e851ba983ebcfc53acaf.png ../../_images/1f4cb9059684cc216824fd5bca91cfc64d8a0779a832b3d71dc44c7fed5ade97.png
Error  5: -0.0004795,	Elements:       1442	Selected: 120
../../_images/534a92fceee0b010388bb214ce7de4ff9ad967f81e02e6b9fd6f5c848b00221e.png ../../_images/38b44c43d9a96b49c25a17d0cdc2b46805ba8a488ec341768a09e9abab64102e.png
Error  6: -0.0003474,	Elements:       2147	Selected: 100
../../_images/5334b9f7d3b0b7c11ad298e5f64109bd6e610ae32efa0559c03659c49602de3c.png ../../_images/e9a02e63d14d84b781b1e91cf57d6cf6b29d0d463e0eb919fa4fdabfa5c89e04.png
Error  7: -0.0002198,	Elements:       2732	Selected: 248
../../_images/c52ecb0d94c9a627aa579c55ec5b5184384700a2a02d9adcf1cfc91e798ba888.png ../../_images/96e2ba2aa7d95dfd1dd646a37ed2bc23fd72d421a83223681639434991a3b06d.png
Error  8: -0.0001822,	Elements:       4046	Selected: 247
../../_images/ee1c20d0201ac508b016af8e8b4f051539b234546d9872a3d4e8dd41ed1849c3.png ../../_images/d8847067a1c2ea9ab851013ab8e6c0a1a2624ce85df7c6ba60f55ab2e09f03e6.png
Error  9: -0.0001337,	Elements:       5551	Selected: 482
../../_images/67ca79476dcddcd11798f0c20169fed581122d481393309f962996d54c3aca8a.png ../../_images/2218d43126066f1e3be1edd21b42acc8adb8a0b0463dc8c5e44cb52d142c6a9d.png
Error 10: -0.0000891,	Elements:       8213	Selected: 488
../../_images/312ac8ffe2ac105cb9bdfa4ff7a6983c4c4c871626872e33dc5f5649054132ef.png ../../_images/b247a579727f0c57cdae99b79677506ba99e78abb23e949c99ec6c65d4ed4a8b.png
Error 11: -0.0000643,	Elements:      11156	Selected: 994
../../_images/db57144379936f278e6fd805680b0f33f8643e85ca21f9b65da4cbcba524dc97.png ../../_images/1b38f81931503eecd1f5613ff2d709a0b881ac8f30a6ee3c16d32ed73977fbb8.png
Error 12: -0.0000486,	Elements:      16390	Selected: 853
../../_images/19b5b3cc2b44c96051e8d8542ad751ba7b1f3ad65e44b24e8851d148d6132c73.png ../../_images/40bd27e407bffe6c4cd60261ff06c3dfa9b8676fc8cc212ba2dfd650f9c97cd7.png
Error 13: -0.0000372,	Elements:      21480	Selected: 1874
../../_images/44b8c9c745c538d059519fe3f3539ef79f8d1d3e084ee41f737d938f12679645.png ../../_images/077574a26478a4b1c8b41b2c766624d47e36e502075ea9f4503c94bf751d71dc.png
Error 14: -0.0000250,	Elements:      31266	Selected: 1417
../../_images/82011bf8da3feb550126374466046e5d905c77d32aa258985448a7173d8c1276.png ../../_images/ee3159b38de215edd7330deaf2fd6197c402a7df5ce5409b3c1b2ba6d26332c6.png
Error 15: -0.0000200,	Elements:      40060	Selected: 2756
../../_images/987866e3dd0b197c10d117593e3e85f7309a1ec82b89363e305b1a513e99395d.png ../../_images/c9c863f8dec03bb7bae31f2c98be6e8239a7e375e6821ccf1e549210aa9f6924.png
Error 16: -0.0000140,	Elements:      54708	Selected: 4203
../../_images/b3ccf27d20ae25b8f202479998ee267903af17bbd9625ae40cdc596aef938784.png ../../_images/47f32dfe095057992a5003e873f701e02a5562fe5a14133f3052dd6a2e3ca80c.png
Error 17: -0.0000108,	Elements:      78269	Selected: 3754
../../_images/7c361cc6b99100c23537bb67449f67e3ec8430943acffe4088234ed40daad1ca.png ../../_images/74ced0a29920d3ff3f157e9a06c6b4740c39d7ef761ba84a1c851db9caf216fe.png
Error 18: -0.0000079,	Elements:      99379	Selected: 6361
../../_images/de8576e0193deb26d7794e40baac84129b5f0ab566c62477cee3ef36bd9a91c6.png ../../_images/eafddcc4f8fd0ba3f6382ac97735031e8aa7a4b2de627d847c54316bdba5c209.png
Error 19: -0.0000062,	Elements:     133490	Selected: 7457
../../_images/b5fa35c1282cacce8b709ea21f6e35090bfebd1a66fe78179c0e1cd737bc7d6f.png ../../_images/8a99f748d9901980670be1948495448ff1b7bd03762ff7f3b7a57bbf8c8ab638.png