|
7 | 7 | from sympy import Expr, Number, Symbol |
8 | 8 | from devito import (Constant, Dimension, Grid, Function, solve, TimeFunction, Eq, # noqa |
9 | 9 | Operator, SubDimension, norm, Le, Ge, Gt, Lt, Abs, sin, cos, |
10 | | - Min, Max) |
| 10 | + Min, Max, Re, Im, switchconfig) |
11 | 11 | from devito.finite_differences.differentiable import SafeInv, Weights |
12 | 12 | from devito.ir import Expression, FindNodes, ccode |
13 | 13 | from devito.symbolics import (retrieve_functions, retrieve_indexed, evalrel, # noqa |
@@ -874,3 +874,123 @@ def test_assumptions(self, op, expr, assumptions, expected): |
874 | 874 | assumptions = eval(assumptions) |
875 | 875 | expected = eval(expected) |
876 | 876 | assert evalrel(op, eqn, assumptions) == expected |
| 877 | + |
| 878 | + |
| 879 | +class TestComplexParts: |
| 880 | + # TODO: Add a cxx switchconfig |
| 881 | + def setup_basic(self, dtype): |
| 882 | + grid = Grid(shape=(5,), extent=(4.,)) |
| 883 | + f = Function(name='f', grid=grid, dtype=dtype) |
| 884 | + f.data_with_halo[:] = np.arange(7) + 1j*np.arange(7, 14)[::-1] |
| 885 | + |
| 886 | + f_real = Function(name='f_real', grid=grid) |
| 887 | + f_imag = Function(name='f_imag', grid=grid) |
| 888 | + return f, f_real, f_imag |
| 889 | + |
| 890 | + def run_operator(self, eqs, cxx): |
| 891 | + if cxx: |
| 892 | + with switchconfig(language='CXX'): |
| 893 | + Operator(eqs)() |
| 894 | + else: |
| 895 | + Operator(eqs)() |
| 896 | + |
| 897 | + @pytest.mark.parametrize('cxx', [False, True]) |
| 898 | + def test_printing(self, cxx): |
| 899 | + f, f_real, f_imag = self.setup_basic(np.complex64) |
| 900 | + |
| 901 | + eq_re = Eq(f_real, Re(f)) |
| 902 | + eq_im = Eq(f_imag, Im(f)) |
| 903 | + |
| 904 | + if cxx: |
| 905 | + with switchconfig(language='CXX'): |
| 906 | + op = Operator([eq_re, eq_im]) |
| 907 | + assert "f_real[x + 1] = std::real(f[x + 1])" in str(op.ccode) |
| 908 | + assert "f_imag[x + 1] = std::imag(f[x + 1])" in str(op.ccode) |
| 909 | + |
| 910 | + else: |
| 911 | + op = Operator([eq_re, eq_im]) |
| 912 | + assert "f_real[x + 1] = crealf(f[x + 1])" in str(op.ccode) |
| 913 | + assert "f_imag[x + 1] = cimagf(f[x + 1])" in str(op.ccode) |
| 914 | + |
| 915 | + @pytest.mark.parametrize('cxx', [False, True]) |
| 916 | + @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) |
| 917 | + def test_trivial(self, cxx, dtype): |
| 918 | + f, f_real, f_imag = self.setup_basic(dtype) |
| 919 | + |
| 920 | + eq_re = Eq(f_real, Re(f+1.)) |
| 921 | + eq_im = Eq(f_imag, Im(f+1.)) |
| 922 | + |
| 923 | + self.run_operator([eq_re, eq_im], cxx) |
| 924 | + |
| 925 | + rcheck = np.array([2., 3., 4., 5., 6.]) |
| 926 | + icheck = np.array([12., 11., 10., 9., 8.]) |
| 927 | + assert np.all(np.isclose(f_real.data, rcheck)) |
| 928 | + assert np.all(np.isclose(f_imag.data, icheck)) |
| 929 | + |
| 930 | + @pytest.mark.parametrize('cxx', [False, True]) |
| 931 | + @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) |
| 932 | + def test_trivial_imag(self, cxx, dtype): |
| 933 | + f, f_real, f_imag = self.setup_basic(dtype) |
| 934 | + |
| 935 | + eq_re = Eq(f_real, Re(f+1j)) |
| 936 | + eq_im = Eq(f_imag, Im(f+1j)) |
| 937 | + |
| 938 | + self.run_operator([eq_re, eq_im], cxx) |
| 939 | + |
| 940 | + rcheck = np.array([1., 2., 3., 4., 5.]) |
| 941 | + icheck = np.array([13., 12., 11., 10., 9.]) |
| 942 | + assert np.all(np.isclose(f_real.data, rcheck)) |
| 943 | + assert np.all(np.isclose(f_imag.data, icheck)) |
| 944 | + |
| 945 | + @pytest.mark.parametrize('cxx', [False, True]) |
| 946 | + def test_deriv(self, cxx): |
| 947 | + f, f_real, f_imag = self.setup_basic(np.complex64) |
| 948 | + |
| 949 | + eq_re = Eq(f_real, Re(f.dx)) |
| 950 | + eq_im = Eq(f_imag, Im(f.dx)) |
| 951 | + |
| 952 | + self.run_operator([eq_re, eq_im], cxx) |
| 953 | + |
| 954 | + assert np.all(np.isclose(f_real.data, 1.)) |
| 955 | + assert np.all(np.isclose(f_imag.data, -1.)) |
| 956 | + |
| 957 | + @pytest.mark.parametrize('cxx', [False, True]) |
| 958 | + def test_outer_deriv(self, cxx): |
| 959 | + f, f_real, f_imag = self.setup_basic(np.complex64) |
| 960 | + |
| 961 | + eq_re = Eq(f_real, Re(f).dx) |
| 962 | + eq_im = Eq(f_imag, Im(f).dx) |
| 963 | + |
| 964 | + self.run_operator([eq_re, eq_im], cxx) |
| 965 | + |
| 966 | + assert np.all(np.isclose(f_real.data, 1.)) |
| 967 | + assert np.all(np.isclose(f_imag.data, -1.)) |
| 968 | + |
| 969 | + @pytest.mark.parametrize('cxx', [False, True]) |
| 970 | + def test_mul(self, cxx): |
| 971 | + grid = Grid(shape=(5,)) |
| 972 | + |
| 973 | + f = Function(name='f', grid=grid, dtype=np.complex64) |
| 974 | + g = Function(name='g', grid=grid) |
| 975 | + h = Function(name='h', grid=grid, dtype=np.complex64) |
| 976 | + f.data[:] = 1 + 1j |
| 977 | + g.data[:] = 2 |
| 978 | + h.data[:] = 2j |
| 979 | + |
| 980 | + fg_re = Function(name='fg_re', grid=grid) |
| 981 | + fg_im = Function(name='fg_im', grid=grid) |
| 982 | + fh_re = Function(name='fh_re', grid=grid) |
| 983 | + fh_im = Function(name='fh_im', grid=grid) |
| 984 | + |
| 985 | + eq_fg_re = Eq(fg_re, Re(f*g)) |
| 986 | + eq_fg_im = Eq(fg_im, Im(f*g)) |
| 987 | + eq_fh_re = Eq(fh_re, Re(f*h)) |
| 988 | + eq_fh_im = Eq(fh_im, Im(f*h)) |
| 989 | + |
| 990 | + self.run_operator([eq_fg_re, eq_fg_im, eq_fh_re, eq_fh_im], cxx) |
| 991 | + |
| 992 | + assert np.all(np.isclose(fg_re.data, 2.)) |
| 993 | + assert np.all(np.isclose(fg_im.data, 2.)) |
| 994 | + |
| 995 | + assert np.all(np.isclose(fh_re.data, -2.)) |
| 996 | + assert np.all(np.isclose(fh_im.data, 2.)) |
0 commit comments