Skip to content

Commit 09bb42c

Browse files
committed
cleanup
1 parent 04b395f commit 09bb42c

2 files changed

Lines changed: 36 additions & 44 deletions

File tree

docs/photonics/examples/refinement.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from skfem import Basis, ElementTriP0, adaptive_theta
2828
from skfem.io.meshio import from_meshio
2929

30-
from femwell.maxwell.waveguide import compute_modes, eval_error_estimator
30+
from femwell.maxwell.waveguide import compute_modes
3131
from femwell.mesh import mesh_from_OrderedDict
3232

3333
# %%
@@ -71,17 +71,16 @@
7171
epsilon[basis0.get_dofs(elements=subdomain)] = e
7272

7373
modes = compute_modes(
74-
basis0, epsilon, wavelength=1.5, num_modes=1, order=1, metallic_boundaries=boundaries[num]
74+
basis0, epsilon, wavelength=1.5, order=1, metallic_boundaries=boundaries[num]
7575
)
76-
modes[0].show(modes[0].E.real, direction="x")
76+
modes[0].plot(modes[0].E.real, direction="x")
77+
plt.show()
7778
error = modes[0].n_eff.real - neff_values_paper[num]
7879
errors.append(error)
7980
nelements.append(mesh.nelements)
8081
print(f"Error {i:2d}: {error: .7f}, Elements: {mesh.nelements:10d}")
8182

82-
elements_to_refine = adaptive_theta(
83-
eval_error_estimator(modes[0].basis, modes[0].E, epsilon), theta=0.5
84-
)
83+
elements_to_refine = adaptive_theta(modes[0].eval_error_estimator(), theta=0.5)
8584

8685
fig, ax = plt.subplots()
8786
mesh.restrict(elements=elements_to_refine).draw(color="red", linewidth=2, ax=ax)

femwell/maxwell/waveguide.py

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,37 @@ def plot_intensity(
463463

464464
return fig, ax
465465

466+
def eval_error_estimator(self):
467+
# facet jump
468+
fbasis = [InteriorFacetBasis(self.basis.mesh, self.basis.elem, side=i) for i in [0, 1]]
469+
fbasis_epsilon = [
470+
InteriorFacetBasis(
471+
self.basis.mesh, self.basis_epsilon_r.elem, side=i, quadrature=fbasis[0].quadrature
472+
)
473+
for i in [0, 1]
474+
]
475+
w = {f"u{str(i + 1)}": fbasis[i].interpolate(self.E) for i in [0, 1]}
476+
w2 = {f"epsilon{str(i + 1)}": fbasis_epsilon[i].interpolate(self.epsilon_r) for i in [0, 1]}
477+
478+
# norm_0 = np.linalg.norm(w["u1"][0]*w2["epsilon1"])
479+
# norm_1 = np.linalg.norm(grad(w["u1"][1]))*10
480+
norm_0 = norm_1 = 1
481+
482+
@Functional
483+
def edge_jump(w):
484+
return w.h * (
485+
np.abs(dot(grad(w["u1"][1]) - grad(w["u2"][1]), w.n)) ** 2 / norm_1**2
486+
+ np.abs(dot(w["u1"][0] * w["epsilon1"][0] - w["u2"][0] * w["epsilon2"][0], w.n))
487+
** 2
488+
/ norm_0**2
489+
)
490+
491+
tmp = np.zeros(self.basis.mesh.facets.shape[1])
492+
tmp[fbasis[0].find] = edge_jump.elemental(fbasis[0], **w, **w2)
493+
eta_E = np.sum(0.5 * tmp[self.basis.mesh.t2f], axis=0)
494+
495+
return eta_E
496+
466497

467498
@dataclass(frozen=True)
468499
class Modes:
@@ -800,44 +831,6 @@ def plot_mode(basis, mode, plot_vectors=False, colorbar=True, title="E", directi
800831
return fig, axs
801832

802833

803-
def eval_error_estimator(basis, u, epsilon):
804-
# @Functional
805-
# def interior_residual(w):
806-
# h = w.h
807-
# x, y = w.x
808-
# return h**2 # * load_func(x, y) ** 2
809-
810-
# eta_K = interior_residual.elemental(basis, w=basis.interpolate(u))
811-
812-
# facet jump
813-
fbasis = [InteriorFacetBasis(basis.mesh, basis.elem, side=i) for i in [0, 1]]
814-
fbasis_epsilon = [
815-
InteriorFacetBasis(basis.mesh, ElementTriP0(), side=i, quadrature=fbasis[0].quadrature)
816-
for i in [0, 1]
817-
]
818-
w = {f"u{str(i + 1)}": fbasis[i].interpolate(u) for i in [0, 1]}
819-
w2 = {f"epsilon{str(i + 1)}": fbasis_epsilon[i].interpolate(epsilon) for i in [0, 1]}
820-
821-
# norm_0 = np.linalg.norm(w["u1"][0]*w2["epsilon1"])
822-
# norm_1 = np.linalg.norm(grad(w["u1"][1]))*10
823-
norm_0 = norm_1 = 1
824-
825-
@Functional
826-
def edge_jump(w):
827-
return w.h * (
828-
np.abs(dot(grad(w["u1"][1]) - grad(w["u2"][1]), w.n)) ** 2 / norm_1**2
829-
+ np.abs(dot(w["u1"][0] * w["epsilon1"][0] - w["u2"][0] * w["epsilon2"][0], w.n)) ** 2
830-
/ norm_0**2
831-
)
832-
833-
tmp = np.zeros(basis.mesh.facets.shape[1])
834-
tmp[fbasis[0].find] = edge_jump.elemental(fbasis[0], **w, **w2)
835-
eta_E = np.sum(0.5 * tmp[basis.mesh.t2f], axis=0)
836-
837-
return eta_E
838-
# return eta_K + eta_E
839-
840-
841834
if __name__ == "__main__":
842835
from collections import OrderedDict
843836

0 commit comments

Comments
 (0)