File: test_n2f_periodic.py

package info (click to toggle)
meep-openmpi 1.25.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 64,556 kB
  • sloc: cpp: 32,214; python: 27,958; lisp: 1,225; makefile: 505; sh: 249; ansic: 131; javascript: 5
file content (100 lines) | stat: -rw-r--r-- 2,876 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import math
import unittest

import numpy as np
from numpy import linalg as LA

import meep as mp


class TestNear2FarPeriodicBoundaries(unittest.TestCase):
    def test_nea2far_periodic(self):
        dpml = 1.0  # PML thickness
        dsub = 3.0  # substrate thickness
        dpad = 20.0  # padding between grating and PML
        gp = 10.0  # grating period
        gh = 0.5  # grating height
        gdc = 0.5  # grating duty cycle

        sx = dpml + dsub + gh + dpad + dpml
        sy = gp

        pml_layers = [mp.PML(thickness=dpml, direction=mp.X)]

        wvl = 0.5  # center wavelength
        fcen = 1 / wvl  # center frequency
        df = 0.05 * fcen  # frequency width

        src_pt = mp.Vector3(-0.5 * sx + dpml + 0.5 * dsub)
        sources = [
            mp.Source(
                mp.GaussianSource(fcen, fwidth=df),
                component=mp.Ez,
                center=src_pt,
                size=mp.Vector3(y=sy),
            )
        ]

        glass = mp.Medium(index=1.5)
        geometry = [
            mp.Block(
                material=glass,
                size=mp.Vector3(dpml + dsub, mp.inf, mp.inf),
                center=mp.Vector3(-0.5 * sx + 0.5 * (dpml + dsub)),
            ),
            mp.Block(
                material=glass,
                size=mp.Vector3(gh, gdc * gp, mp.inf),
                center=mp.Vector3(-0.5 * sx + dpml + dsub + 0.5 * gh),
            ),
        ]

        k_point = mp.Vector3(0, 0, 0)

        symmetries = [mp.Mirror(mp.Y)]

        n2f_pt = mp.Vector3(-0.5 * sx + dpml + dsub + gh + 1.0)
        dft_pt = mp.Vector3(0.5 * sx - dpml)

        res = [20, 25, 30]
        norm = np.empty(3)

        for j in range(3):
            sim = mp.Simulation(
                resolution=res[j],
                cell_size=mp.Vector3(sx, sy),
                boundary_layers=pml_layers,
                geometry=geometry,
                k_point=k_point,
                sources=sources,
                symmetries=symmetries,
            )

            n2f_obj = sim.add_near2far(
                fcen,
                0,
                1,
                mp.Near2FarRegion(center=n2f_pt, size=mp.Vector3(y=sy)),
                nperiods=10,
            )
            dft_obj = sim.add_dft_fields(
                [mp.Ez], fcen, 0, 1, center=dft_pt, size=mp.Vector3(y=sy)
            )

            sim.run(until_after_sources=300)

            n2f_Ez = sim.get_farfields(
                n2f_obj, res[j], center=dft_pt, size=mp.Vector3(y=sy)
            )
            dft_Ez = sim.get_dft_array(dft_obj, mp.Ez, 0)

            norm[j] = LA.norm(n2f_Ez["Ez"] - dft_Ez[1:-1])
            print(f"norm:, {res[j]}, {norm[j]:.5f}")
            sim.reset_meep()

        self.assertGreater(norm[0], norm[1])
        self.assertGreater(norm[1], norm[2])


if __name__ == "__main__":
    unittest.main()