File: test_adjoint_utils.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 (54 lines) | stat: -rw-r--r-- 1,729 bytes parent folder | download | duplicates (3)
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
"""test_adjoint_utils.py

Check various components of the adjoint solver codebase, like
filters, which may not need explicit gradient computation
(i.e. forward and adjoint runs).

"""
import meep as mp

try:
    import meep.adjoint as mpa
except:
    import adjoint as mpa

import unittest
from enum import Enum

import numpy as np
import parameterized
from autograd import numpy as npa
from autograd import tensor_jacobian_product
from utils import ApproxComparisonTestCase

_TOL = 1e-6 if mp.is_single_precision() else 1e-14

## ensure reproducible results
rng = np.random.RandomState(9861548)


class TestAdjointUtils(ApproxComparisonTestCase):
    @parameterized.parameterized.expand(
        [
            ("1.0_1.0_20_conic", 1.0, 1.0, 20, 0.24, mpa.conic_filter),
            ("1.0_1.0_23_conic", 1.0, 1.0, 23, 0.24, mpa.conic_filter),
            ("0.887_1.56_conic", 0.887, 1.56, 20, 0.24, mpa.conic_filter),
            ("0.887_1.56_conic", 0.887, 1.56, 31, 0.24, mpa.conic_filter),
            ("0.887_1.56_gaussian", 0.887, 1.56, 20, 0.24, mpa.gaussian_filter),
            ("0.887_1.56_cylindrical", 0.887, 1.56, 20, 0.24, mpa.cylindrical_filter),
        ]
    )
    def test_filter_offset(self, test_name, Lx, Ly, resolution, radius, filter_func):
        """ensure that the filters are indeed zero-phase"""
        print("Testing ", test_name)
        Nx, Ny = int(resolution * Lx), int(resolution * Ly)
        x = np.random.rand(Nx, Ny)
        x = x + np.fliplr(x)
        x = x + np.flipud(x)
        y = filter_func(x, radius, Lx, Ly, resolution)
        self.assertClose(y, np.fliplr(y), epsilon=_TOL)
        self.assertClose(y, np.flipud(y), epsilon=_TOL)


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