File: user_defined_material.py

package info (click to toggle)
meep-openmpi 1.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 25,828 kB
  • sloc: cpp: 27,370; python: 10,574; lisp: 1,213; makefile: 437; sh: 28
file content (106 lines) | stat: -rw-r--r-- 3,387 bytes parent folder | download | duplicates (8)
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
101
102
103
104
105
106
import unittest
import meep as mp


# Material function that recreates the ellipsoid-in-cylinder configuration of
# examples/cyl-ellipsoid.py
def my_material_func(p):
    R1X = 0.5
    R1Y = 1.0
    R2 = 3.0

    x = p.x
    y = p.y

    # test for point inside inner ellipsoid
    if (x**2 / (R1X**2) + y**2 / (R1Y**2)) < 1.0:
        nn = 1.0
    elif (x**2 / (R2**2) + y**2 / (R2**2)) < 1.0:
        nn = 3.5
    else:
        nn = 1.0

    return mp.Medium(epsilon=nn**2)


def my_epsilon_func(p):
    R1X = 0.5
    R1Y = 1.0
    R2 = 3.0

    x = p.x
    y = p.y

    if (x**2 / (R1X**2) + y**2 / (R1Y**2)) < 1.0:
        return 1.0
    elif (x**2 / (R2**2) + y**2 / (R2**2)) < 1.0:
        return 3.5
    return 1.0


class TestUserMaterials(unittest.TestCase):

    def setUp(self):
        self.resolution = 10
        self.cell = mp.Vector3(10, 10)
        self.symmetries = [mp.Mirror(mp.X), mp.Mirror(mp.Y)]
        self.boundary_layers = [mp.PML(1.0)]
        self.sources = [mp.Source(src=mp.GaussianSource(0.2, fwidth=0.1),
                                  component=mp.Ez, center=mp.Vector3())]

    def test_user_material_func(self):
        sim = mp.Simulation(cell_size=self.cell,
                            resolution=self.resolution,
                            symmetries=self.symmetries,
                            boundary_layers=self.boundary_layers,
                            sources=self.sources,
                            material_function=my_material_func)

        sim.run(until=200)
        fp = sim.get_field_point(mp.Ez, mp.Vector3(x=1))

        self.assertAlmostEqual(fp, 4.816403627871773e-4 + 0j)

    def test_epsilon_func(self):
        sim = mp.Simulation(cell_size=self.cell,
                            resolution=self.resolution,
                            symmetries=self.symmetries,
                            boundary_layers=self.boundary_layers,
                            sources=self.sources,
                            epsilon_func=my_epsilon_func)

        sim.run(until=100)
        fp = sim.get_field_point(mp.Ez, mp.Vector3(x=1))

        self.assertAlmostEqual(fp, -7.895783750440999e-4 + 0j)

    def test_geometric_obj_with_user_material(self):
        geometry = [mp.Cylinder(5, material=my_material_func)]

        sim = mp.Simulation(cell_size=self.cell,
                            resolution=self.resolution,
                            symmetries=self.symmetries,
                            geometry=geometry,
                            boundary_layers=self.boundary_layers,
                            sources=self.sources)
        sim.run(until=200)
        fp = sim.get_field_point(mp.Ez, mp.Vector3(x=1))

        self.assertAlmostEqual(fp, 4.816403627871773e-4 + 0j)

    def test_geometric_obj_with_epsilon_func(self):
        geometry = [mp.Cylinder(5, epsilon_func=my_epsilon_func)]

        sim = mp.Simulation(cell_size=self.cell,
                            resolution=self.resolution,
                            symmetries=self.symmetries,
                            geometry=geometry,
                            boundary_layers=self.boundary_layers,
                            sources=self.sources)
        sim.run(until=100)
        fp = sim.get_field_point(mp.Ez, mp.Vector3(x=1))

        self.assertAlmostEqual(fp, -7.895783750440999e-4 + 0j)

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