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/1cf3db3890a183ae17ff8f55cbbfee0cf58ff637d5a8a8e8cbb034f87e5551f3.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/914965c78368cae4909fa0415fbb347bc344f194bdf79088215aa28ec810cca8.png
Named boundaries invalidated by a call to Mesh.refined()
Error  0: -0.0009402,	Elements:        484	Selected: 8
../../_images/baf6fe80ffea9e9a0480c51555918cd39c9b072031abf9c9c3a4b53d977d4422.png ../../_images/f7dc17a8afec265381b7eb3448268de8381b124f199a0b1dfcf9edaba670e506.png
Error  1: -0.0009728,	Elements:        534	Selected: 26
../../_images/db0747bab91edc6a54f0e57dd109259cb850f7891fd5112d97dffd41e894a53e.png ../../_images/382e609800b173c16d76d77aef3340796bd98780afd7e6126206fbdd1162ed65.png
Error  2: -0.0009282,	Elements:        697	Selected: 11
../../_images/6df80ffdac3d762ab3efd44ab6004ace75e3942c294eda7a45088e27260eee4b.png ../../_images/c9f1320b769f2920b61cdbd530178a1fe0e16db9c35f172fd6d8f2f98d1826e7.png
Error  3: -0.0008647,	Elements:        778	Selected: 72
../../_images/127457b512c873d5d4ec57fa17574eed311d5c3055ad30efb9c6dc48a3ff6977.png ../../_images/20792e9a231cc60e1a8c1655d3650490b4e12d022807cc8fe4fed2939511e7b4.png
Error  4: -0.0005305,	Elements:       1174	Selected: 44
../../_images/cb64589ae70c9bfb4c16b75761107585e422410906af4fa5e655d4b4d4be7106.png ../../_images/194282c0e1cb5770b6cc6523810f2efeda29639bfa2c72a5d541b6d0adf52654.png
Error  5: -0.0004795,	Elements:       1442	Selected: 120
../../_images/33264f573ee40709f54317db144f10c93dfabdec99004a4867cad536fee72236.png ../../_images/064f337b1b9b15098de21ae07425f18d140bc0a228020d7cb0784d9339b1181e.png
Error  6: -0.0003474,	Elements:       2147	Selected: 100
../../_images/0c71f6e531c3b91e2dfd3579d8eb8a1c4577219ea72d44ebb63cbe05040dec62.png ../../_images/98276870f636b86eeb2a6c4ba1dc6a6b4bed642229dff4125147b0712952cc6b.png
Error  7: -0.0002198,	Elements:       2732	Selected: 248
../../_images/bdb988185c4a688ed6970e71109075d3b77094884b68d5a0805a279744be75df.png ../../_images/9dcca80ff9ffaab948f5c169014dd2aaf6160ce29ebb6c6d092e9e4ad3374fc7.png
Error  8: -0.0001822,	Elements:       4046	Selected: 247
../../_images/25991fbfd96b2e741cf2e0e8b2f2135356b36fc22edcb785cf53248c18851090.png ../../_images/344e75c17b3fecf06dee4645334d1603d92432bc429b8a6b2ce2feab2ed694cf.png
Error  9: -0.0001337,	Elements:       5551	Selected: 482
../../_images/30766d0838c389a44ec025d3c1da6e4b18d2c509ef19c6e2622a31889009f63b.png ../../_images/ec0594fb07f5c10e10e843b35a22693b564fceac2058b64e1e581a5075d51da5.png
Error 10: -0.0000891,	Elements:       8213	Selected: 488
../../_images/5fba4b9eb71aa2e0463e5b8c1a6e10a594cc1295c20936fbd9a68ef8809695b5.png ../../_images/585707e0d89b3b525f598d5f1477bd4fee869c3f9906ebec8049f053c8d1238d.png
Error 11: -0.0000643,	Elements:      11156	Selected: 994
../../_images/66b4de38af8fb6836e833a0b8302fd6b058f210970b4d1d02170517d47ea9bb8.png ../../_images/02c400159b1fc7e956844e9ded6e9de74775cb44147ff3e4ac740d648587f2a6.png
Error 12: -0.0000486,	Elements:      16390	Selected: 853
../../_images/b7855a332e02d3e6aae989a2710b8101c97e6ad6b68140fdb463aeff62a9ab28.png ../../_images/7cb1d842ff22b786b20c336985164fec940236baa96847d3c550c31748e37c86.png
Error 13: -0.0000372,	Elements:      21480	Selected: 1874
../../_images/709a3569b7b96b4e8554996bc9a17e152dd75b716fa3e2e8e993b343f6327584.png ../../_images/65f63afe3344bdf9c2d96de85c8e1f1d2c6dd971cee862795ef409908203a845.png
Error 14: -0.0000250,	Elements:      31266	Selected: 1417
../../_images/73877107f5fa699be8cc8715dd75c367b2f4e0ec2dd12ccad529b3a368fc72db.png ../../_images/4fe6f3ab6e22192537638a4eea4e4984fee411119bad9f01a8a1c012762dfa59.png
Error 15: -0.0000200,	Elements:      40060	Selected: 2756
../../_images/a56b0df6ca3bb18034290fd3d10d597f6da5b0dda0a54f79dbf6a92ae08f2a23.png ../../_images/85571d427113f86b4332b263dda9fc7b97abd413b5bd357420bfb32a695a9843.png
Error 16: -0.0000140,	Elements:      54708	Selected: 4203
../../_images/36943394e5fcd4907171ee7c6dc21c6d212d455b44f98d16d38ffb6836c8c0d1.png ../../_images/9602b25517caf6271911432f00432f0fce0f627ab8c854b6ae77fbc8a93920e1.png
Error 17: -0.0000108,	Elements:      78269	Selected: 3754
../../_images/ccfdb190715c49493848668f052125c77bb8dbcda04752d260fdb5073405f5ac.png ../../_images/978254dfb5c9bdb01f730cc0d8b511438423f891f1bd0f99f06a8c501f65d65f.png
Error 18: -0.0000079,	Elements:      99379	Selected: 6361
../../_images/efbc4a545370859acf16910c27279212743e2b3ddf345d1d1a453f129fa6c732.png ../../_images/a865828c2683ffb340282438145bd5500c1024c9e3c5d4197e42e9652e63892e.png
Error 19: -0.0000062,	Elements:     133490	Selected: 7457
../../_images/0e8a5f4bb28c0c05be32e7e1843c0e0f249abd828d9bedb0133bcd8be2a70de2.png ../../_images/c09f2a4342dbe1fc740ce6b1d1767488fe7d576f1962e33d4b489759774358ac.png