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/26861e6f239306c68d021e6c427bbbb3147ed4ee2580bc7416ebba29b8ab5e18.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/83ec9b23d35e022348a8e28b431c176245d4e554f2ec9d5d70a0c789edf6215e.png
Named boundaries invalidated by a call to Mesh.refined()
Error  0: -0.0009402,	Elements:        484	Selected: 8
../../_images/2c3ded55189807ba6eed7af59ef3897ca343aae261c36ae50d3e35d3e8b83a13.png ../../_images/13237ab54f83c0fa09a1f80f9791e4efdc9a187991b06a4cd6526ded639daa88.png
Error  1: -0.0009728,	Elements:        534	Selected: 26
../../_images/48c6d7354680881f913cf2265093cad0aa4017129e00376347d0f6f17f928993.png ../../_images/4f14c7afe5ddd4577cb0869ba30d0341db24bc4b56a16c48f2bad1a5c7703995.png
Error  2: -0.0009282,	Elements:        697	Selected: 11
../../_images/b351378b740b5605c79463499eabef172e0f22e8bb879489c36a5151d64fc5c0.png ../../_images/d345eb132784efb72fcd5e79df720b4751fb4142bfd43be5179da9b8a135e65f.png
Error  3: -0.0008647,	Elements:        778	Selected: 72
../../_images/3d876561da18f6d6bbb93a5abd1dbd8b808eba53627718d1494eff4961b828ad.png ../../_images/ffb4ccac6c600c38d77659212e8af68a0c094ad09e67d6674a72e906f3ee4244.png
Error  4: -0.0005305,	Elements:       1174	Selected: 44
../../_images/4c8b87869dbff8dac287ea91fc7815514b295586fa9f2d89f4b3acc7b7a5dd22.png ../../_images/ab9f036b22151ebd6db4e37db007ec76d86e6bbab693aca9a3b6beb2857da477.png
Error  5: -0.0004795,	Elements:       1442	Selected: 120
../../_images/80ad7423ff3e8efbd99d1cdbd83b52c32bc5b8c6ee0bf9ae9233f3084717f723.png ../../_images/eb31a8b509d00937364918296b4c017d0417267d6144fbf2488865a9b476f367.png
Error  6: -0.0003474,	Elements:       2147	Selected: 100
../../_images/50a9dce32e2697f78dfbcaf5a6f1db21fc3c18d780c1aef125e9c94dfff5677a.png ../../_images/3bc3fab3f0c28ba4323617805a2311dc2c3ece3e2faca8670ff73d99c6c41bd5.png
Error  7: -0.0002198,	Elements:       2732	Selected: 248
../../_images/ebb6c02d8dbb1a3d6293041b1fade3881e9d960e07c4cbc72e5293bd1d6103c3.png ../../_images/8744cf767a038fa6d2c1f6958c10216e67582732a9dab91c00f86ea4cdc20fb8.png
Error  8: -0.0001822,	Elements:       4046	Selected: 247
../../_images/a4568a8f6185a38e535c4547810848651aa49b64568952bdad1e7eb0634ede3b.png ../../_images/3ac63a2a59c8e2feb3128ed4379658a0fa07ca8a7a98d62fba7b79a372d2f9b5.png
Error  9: -0.0001337,	Elements:       5551	Selected: 482
../../_images/5e8261bf0bfef630771bc9af71111aac5db11afa31670f18f6492c45822e46a9.png ../../_images/b3cbf47f60655995b5fb54f558d13a111a0646ffc8676ff73eb9b2501e7bfebf.png
Error 10: -0.0000891,	Elements:       8213	Selected: 488
../../_images/b56e85e645f47946bc0ecdd80ba9283c2d33695522a0d9b118719b4e2b77c4f9.png ../../_images/265aec9c3c55f14f5f16433091a89cecdfe62b308c5723a7c31794488d70b3cd.png
Error 11: -0.0000643,	Elements:      11156	Selected: 994
../../_images/fb6309379c6dd2bb2a5c8835a2807c583f0eb8862a63f8da7752a5202a1c75a0.png ../../_images/36ec74980a4798e415dfd1d40c2062877f0186eab9221d8599f6f6e456e040d4.png
Error 12: -0.0000486,	Elements:      16390	Selected: 853
../../_images/7859ab803825e7e6b0d8fd7545c896e979bc9c008b18ba5598472fad62183306.png ../../_images/9ea6fce9cb5cf8cbe15a8852295ef4325096ae10e8a98f81ebdc6d60aba1ac4a.png
Error 13: -0.0000372,	Elements:      21480	Selected: 1874
../../_images/0443b97fd3659823438cd0f92b60185d97a5d80106472bafa060ac4097052d95.png ../../_images/297f3564a3e71ef5c1cf30f4d1b72f2de3bdfc526705828ad4a5bf2cdea7e7d5.png
Error 14: -0.0000250,	Elements:      31266	Selected: 1417
../../_images/1bb0c2a8624d1d34d0466c8000b56a59ccd0cfb926e476e89add14981fd299be.png ../../_images/fd264d7bd3ab7cfc82146ed41dc60d0a3ee836d4ee377057b8c2c2e57ec58ee9.png
Error 15: -0.0000200,	Elements:      40060	Selected: 2756
../../_images/88f58e90167feb5aecc7f36b955e112c2821746c0a4e540e733065018cc9d2b5.png ../../_images/d1d33aa922aa66501daa2d688b068f7902888313ef077f79140e29739fcf9207.png
Error 16: -0.0000140,	Elements:      54708	Selected: 4203
../../_images/3647d28334afffe14533c795a44833f585b67bdecc008e7b8bc0e371aebc5a26.png ../../_images/8f75453378a378a195dc01a193c651582e00c5505c525d00dfcecdb274ba11d6.png
Error 17: -0.0000108,	Elements:      78269	Selected: 3754
../../_images/d4234c263986b032ea71c27da1a89eec81934d820b4b612a50bac3628fbbd27b.png ../../_images/b04e901dfe17a0e58b12f822870cf40319852cea6d50e29c00d0438f03ccdb67.png
Error 18: -0.0000079,	Elements:      99379	Selected: 6361
../../_images/3973ecb94ce0e72385d5d703d70001a4a4357728db8a2dd6ac230157c920bdd2.png ../../_images/51fffce44a3c87890bfd0f5c3b9c1b4c642b0d31cc8679f590da2ffb5e4b4a9d.png
Error 19: -0.0000062,	Elements:     133490	Selected: 7457
../../_images/4fd76e1f503008b2e26aec4973c3cd257d425af90f87a971f1a3ece2d00a19e8.png ../../_images/14f043edddb794c713573057d7016a1f6b6267631ceff1e4feb9452950c0d36b.png