def __init__(self, a, mat_i, mat_m, mat_0, n):
        self.n0 = n
        self.n1 = n
        self.shape = (3 * self.n0 * self.n1,
                      3 * self.n0 * self.n1)
        self.dtype = np.float64
        transform = fft.create_real((self.n0, self.n1))
        self.green = green.filtered(mat_0.green_operator(),
                                              transform.ishape,
                                              1., transform)
        aux_i = operators.isotropic_4((mat_i.k - mat_0.k) / 2.,
                                      (mat_i.g - mat_0.g) / 2.,
                                      dim=2)
        aux_m = operators.isotropic_4((mat_m.k - mat_0.k) / 2.,
                                      (mat_m.g - mat_0.g) / 2.,
                                      dim=2)

        op_loc = np.empty(transform.ishape, dtype=object)

        imax = int(np.ceil(n * a - 0.5))

        for i0 in range(self.n0):
            for i1 in range(self.n1):
                if (i0 < imax) and (i1 < imax):
                    op_loc[i0, i1] = aux_i
                else:
                    op_loc[i0, i1] = aux_m

        self.eps2tau = operators.BlockDiagonalOperator2D(op_loc)
Beispiel #2
0
 def test_apply(self, path_to_ref, rtol, flag):
     npz_file = np.load(path_to_ref)
     x = npz_file['x']
     expected = npz_file['y']
     # This is a workaround to avoid
     # ResourceWarning: unclosed file <_io.BufferedReader name='xxx.npz'>
     #     self.zip.close()
     npz_file.zip.fp.close()
     n = x.shape[:-1]
     transform = fft.create_real(n)
     green = self.greend(material.create(MU, NU, len(n)).green_operator(),
                         n,
                         1.,
                         transform=transform)
     if flag == 0:
         actual = green.apply(x)
     elif flag == 1:
         actual = green.apply(x, x)
     elif flag == 2:
         base = np.zeros(transform.ishape + (green.oshape[-1], ),
                         np.float64)
         actual = green.apply(x, base)
     else:
         raise ValueError()
     assert_allclose(actual, expected, rtol, 10 * ULP)
Beispiel #3
0
 def __init__(self, mat_i, mat_m, mat_0, n, a=0.5, dim=3):
     self.mat_i = mat_i
     self.mat_m = mat_m
     self.n = n
     shape = tuple(itertools.repeat(n, dim))
     # ...
     # End: init
     # Begin: create (C_i - C_0) and (C_m - C_0)
     # ...
     delta_C_i = isotropic_4(dim*(mat_i.k-mat_0.k),
                             2*(mat_i.g-mat_0.g), dim)
     delta_C_m = isotropic_4(dim*(mat_m.k-mat_0.k),
                             2*(mat_m.g-mat_0.g), dim)
     # ...
     # End: create (C_i - C_0) and (C_m - C_0)
     # Begin: create local operator ε ↦ (C-C_0):ε
     # ...
     ops = np.empty(shape, dtype=object)
     ops[:, :] = delta_C_m
     imax = int(np.ceil(n*a-0.5))
     ops[:imax, :imax] = delta_C_i
     self.eps_to_tau = operators.BlockDiagonalOperator2D(ops)
     # ...
     # End: create local operator ε ↦ (C-C_0):ε
     # Begin: create non-local operator ε ↦ Γ_0[ε]
     # ...
     self.green = green.truncated(mat_0.green_operator(),
                                  shape, 1.,
                                  fft.create_real(shape))
Beispiel #4
0
    def __init__(self, a, mat_i, mat_m, mat_0, n):
        self.n0 = n
        self.n1 = n
        self.shape = (3 * self.n0 * self.n1, 3 * self.n0 * self.n1)
        self.dtype = np.float64
        transform = fft.create_real((self.n0, self.n1))
        self.green = green.filtered(mat_0.green_operator(), transform.ishape,
                                    1., transform)
        aux_i = operators.isotropic_4((mat_i.k - mat_0.k) / 2.,
                                      (mat_i.g - mat_0.g) / 2.,
                                      dim=2)
        aux_m = operators.isotropic_4((mat_m.k - mat_0.k) / 2.,
                                      (mat_m.g - mat_0.g) / 2.,
                                      dim=2)

        op_loc = np.empty(transform.ishape, dtype=object)

        imax = int(np.ceil(n * a - 0.5))

        for i0 in range(self.n0):
            for i1 in range(self.n1):
                if (i0 < imax) and (i1 < imax):
                    op_loc[i0, i1] = aux_i
                else:
                    op_loc[i0, i1] = aux_m

        self.eps2tau = operators.BlockDiagonalOperator2D(op_loc)
Beispiel #5
0
 def __init__(self, mat_i, mat_m, mat_0, n, a=0.5, dim=3):
     self.mat_i = mat_i
     self.mat_m = mat_m
     self.n = n
     shape = tuple(itertools.repeat(n, dim))
     # ...
     # End: init
     # Begin: create (C_i - C_0) and (C_m - C_0)
     # ...
     delta_C_i = isotropic_4(dim * (mat_i.k - mat_0.k), 2 * (mat_i.g - mat_0.g), dim)
     delta_C_m = isotropic_4(dim * (mat_m.k - mat_0.k), 2 * (mat_m.g - mat_0.g), dim)
     # ...
     # End: create (C_i - C_0) and (C_m - C_0)
     # Begin: create local operator ε ↦ (C-C_0):ε
     # ...
     ops = np.empty(shape, dtype=object)
     ops[:, :] = delta_C_m
     imax = int(np.ceil(n * a - 0.5))
     ops[:imax, :imax] = delta_C_i
     self.eps_to_tau = operators.BlockDiagonalOperator2D(ops)
     # ...
     # End: create local operator ε ↦ (C-C_0):ε
     # Begin: create non-local operator ε ↦ Γ_0[ε]
     # ...
     self.green = green.truncated(mat_0.green_operator(), shape, 1.0, fft.create_real(shape))
 def test_apply(self, path_to_ref, rtol, flag):
     npz_file = np.load(path_to_ref)
     x = npz_file['x']
     expected = npz_file['y']
     # This is a workaround to avoid
     # ResourceWarning: unclosed file <_io.BufferedReader name='xxx.npz'>
     #     self.zip.close()
     npz_file.zip.fp.close()
     n = x.shape[:-1]
     transform = fft.create_real(n)
     green = self.greend(material.create(MU, NU, len(n)).green_operator(),
                         n, 1., transform=transform)
     if flag == 0:
         actual = green.apply(x)
     elif flag == 1:
         actual = green.apply(x, x)
     elif flag == 2:
         base = np.zeros(transform.ishape + (green.oshape[-1],), np.float64)
         actual = green.apply(x, base)
     else:
         raise ValueError()
     assert_allclose(actual, expected, rtol, 10 * ULP)
Beispiel #7
0
 def pytest_generate_tests(self, metafunc):
     if metafunc.function.__name__ == 'test_init_invalid_params':
         g2 = material.create(MU, NU, 2).green_operator()
         g3 = material.create(MU, NU, 3).green_operator()
         params = [(g3, (9, 9), 1., None), (g2, (-1, 9), 1., None),
                   (g2, (9, -1), 1., None), (g2, (9, 9), -1., None),
                   (g2, (9, 9), 1., fft.create_real((8, 9))),
                   (g2, (9, 9), 1., fft.create_real((9, 8))),
                   (g2, (9, 9, 9), 1., None), (g3, (-1, 9, 9), 1., None),
                   (g3, (9, -1, 9), 1., None), (g3, (9, 9, -1), 1., None),
                   (g3, (9, 9, 9), -1., None)]
         metafunc.parametrize('greenc, n, h, transform', params)
     elif metafunc.function.__name__ == 'test_to_memoryview':
         params = [(n, flag) for n in GRID_SIZES for flag in [0, 1]]
         metafunc.parametrize('n, flag', params)
     elif metafunc.function.__name__ == 'test_to_memoryview_invalid_params':
         g2 = self.greend(
             material.create(MU, NU, 2).green_operator(), (8, 16), 1.0)
         g3 = self.greend(
             material.create(MU, NU, 3).green_operator(), (8, 16, 32), 1.0)
         params = [
             (g2, (g2.oshape[-1], g2.ishape[-1] + 1)),
             (g2, (g2.oshape[-1] + 1, g2.ishape[-1])),
             (g3, (g3.oshape[-1], g3.ishape[-1] + 1)),
             (g3, (g3.oshape[-1] + 1, g3.ishape[-1])),
         ]
         metafunc.parametrize('greend, out_shape', params)
     elif metafunc.function.__name__ == 'test_apply_by_freq':
         x = [
             np.array([0.3, -0.4, 0.5]),
             np.array([0.1, -0.2, 0.3, -0.4, 0.5, -0.6])
         ]
         params = [(n, x[len(n) - 2], flag) for n in GRID_SIZES
                   for flag in range(3)]
         metafunc.parametrize('n, x, flag', params)
     elif metafunc.function.__name__ == 'test_apply_by_freq_invalid_params':
         g2 = self.greend(
             material.create(MU, NU, 2).green_operator(), (8, 16), 1.0)
         g3 = self.greend(
             material.create(MU, NU, 3).green_operator(), (8, 16, 32), 1.0)
         params = [
             (g2, g2.ishape[-1], g2.oshape[-1] + 1),
             (g2, g2.ishape[-1] + 1, g2.oshape[-1]),
             (g3, g3.ishape[-1], g3.oshape[-1] + 1),
             (g3, g3.ishape[-1] + 1, g3.oshape[-1]),
         ]
         metafunc.parametrize('greend, x_size, y_size', params)
     elif metafunc.function.__name__ == 'test_apply':
         if self.operates_in_place:
             flags = [0, 1, 2]
         else:
             flags = [0, 2]
         params = [
             i + (j, ) for i in self.params_test_apply() for j in flags
         ]
         metafunc.parametrize('path_to_ref, rtol, flag', params)
     elif metafunc.function.__name__ == 'test_apply_invalid_params':
         g = self.greend(
             material.create(MU, NU, 2).green_operator(), (8, 16), 1.0)
         i0, i1, i2 = g.ishape
         o0, o1, o2 = g.oshape
         params2 = [(g, (i0 + 1, i1, i2), (o0, o1, o2)),
                    (g, (i0, i1 + 1, i2), (o0, o1, o2)),
                    (g, (i0, i1, i2 + 1), (o0, o1, o2)),
                    (g, (i0, i1, i2), (o0 + 1, o1, o2)),
                    (g, (i0, i1, i2), (o0, o1 + 1, o2)),
                    (g, (i0, i1, i2), (o0, o1, o2 + 1))]
         g = self.greend(
             material.create(MU, NU, 3).green_operator(), (8, 16, 32), 1.0)
         i0, i1, i2, i3 = g.ishape
         o0, o1, o2, o3 = g.oshape
         params3 = [(g, (i0 + 1, i1, i2, i3), (o0, o1, o2, o3)),
                    (g, (i0, i1 + 1, i2, i3), (o0, o1, o2, o3)),
                    (g, (i0, i1, i2 + 1, i3), (o0, o1, o2, o3)),
                    (g, (i0, i1, i2, i3 + 1), (o0, o1, o2, o3)),
                    (g, (i0, i1, i2, i3), (o0 + 1, o1, o2, o3)),
                    (g, (i0, i1, i2, i3), (o0, o1 + 1, o2, o3)),
                    (g, (i0, i1, i2, i3), (o0, o1, o2 + 1, o3)),
                    (g, (i0, i1, i2, i3), (o0, o1, o2, o3 + 1))]
         metafunc.parametrize('greend, x_shape, y_shape', params2 + params3)
 def pytest_generate_tests(self, metafunc):
     if metafunc.function.__name__ == 'test_init_invalid_params':
         g2 = material.create(MU, NU, 2).green_operator()
         g3 = material.create(MU, NU, 3).green_operator()
         params = [(g3, (9, 9), 1., None),
                   (g2, (-1, 9), 1., None),
                   (g2, (9, -1), 1., None),
                   (g2, (9, 9), -1., None),
                   (g2, (9, 9), 1., fft.create_real((8, 9))),
                   (g2, (9, 9), 1., fft.create_real((9, 8))),
                   (g2, (9, 9, 9), 1., None),
                   (g3, (-1, 9, 9), 1., None),
                   (g3, (9, -1, 9), 1., None),
                   (g3, (9, 9, -1), 1., None),
                   (g3, (9, 9, 9), -1., None)]
         metafunc.parametrize('greenc, n, h, transform', params)
     elif metafunc.function.__name__ == 'test_to_memoryview':
         params = [(n, flag) for n in GRID_SIZES for flag in [0, 1]]
         metafunc.parametrize('n, flag', params)
     elif metafunc.function.__name__ == 'test_to_memoryview_invalid_params':
         g2 = self.greend(material.create(MU, NU, 2).green_operator(),
                          (8, 16), 1.0)
         g3 = self.greend(material.create(MU, NU, 3).green_operator(),
                          (8, 16, 32), 1.0)
         params = [(g2, (g2.oshape[-1], g2.ishape[-1] + 1)),
                   (g2, (g2.oshape[-1] + 1, g2.ishape[-1])),
                   (g3, (g3.oshape[-1], g3.ishape[-1] + 1)),
                   (g3, (g3.oshape[-1] + 1, g3.ishape[-1])),]
         metafunc.parametrize('greend, out_shape', params)
     elif metafunc.function.__name__ == 'test_apply_by_freq':
         x = [np.array([0.3, -0.4, 0.5]),
              np.array([0.1, -0.2, 0.3, -0.4, 0.5, -0.6])]
         params = [(n, x[len(n) - 2], flag) for n in GRID_SIZES
                   for flag in range(3)]
         metafunc.parametrize('n, x, flag', params)
     elif metafunc.function.__name__ == 'test_apply_by_freq_invalid_params':
         g2 = self.greend(material.create(MU, NU, 2).green_operator(),
                          (8, 16), 1.0)
         g3 = self.greend(material.create(MU, NU, 3).green_operator(),
                          (8, 16, 32), 1.0)
         params = [(g2, g2.ishape[-1], g2.oshape[-1] + 1),
                   (g2, g2.ishape[-1] + 1, g2.oshape[-1]),
                   (g3, g3.ishape[-1], g3.oshape[-1] + 1),
                   (g3, g3.ishape[-1] + 1, g3.oshape[-1]),]
         metafunc.parametrize('greend, x_size, y_size', params)
     elif metafunc.function.__name__ == 'test_apply':
         if self.operates_in_place:
             flags = [0, 1, 2]
         else:
             flags = [0, 2]
         params = [i + (j,) for i in self.params_test_apply() for j in flags]
         metafunc.parametrize('path_to_ref, rtol, flag', params)
     elif metafunc.function.__name__ == 'test_apply_invalid_params':
         g = self.greend(material.create(MU, NU, 2).green_operator(),
                         (8, 16), 1.0)
         i0, i1, i2 = g.ishape
         o0, o1, o2 = g.oshape
         params2 = [(g, (i0 + 1, i1, i2), (o0, o1, o2)),
                   (g, (i0, i1 + 1, i2), (o0, o1, o2)),
                   (g, (i0, i1, i2 + 1), (o0, o1, o2)),
                   (g, (i0, i1, i2), (o0 + 1, o1, o2)),
                   (g, (i0, i1, i2), (o0, o1 + 1, o2)),
                   (g, (i0, i1, i2), (o0, o1, o2 + 1))]
         g = self.greend(material.create(MU, NU, 3).green_operator(),
                         (8, 16, 32), 1.0)
         i0, i1, i2, i3 = g.ishape
         o0, o1, o2, o3 = g.oshape
         params3 = [(g, (i0 + 1, i1, i2, i3), (o0, o1, o2, o3)),
                    (g, (i0, i1 + 1, i2, i3), (o0, o1, o2, o3)),
                    (g, (i0, i1, i2 + 1, i3), (o0, o1, o2, o3)),
                    (g, (i0, i1, i2, i3 + 1), (o0, o1, o2, o3)),
                    (g, (i0, i1, i2, i3), (o0 + 1, o1, o2, o3)),
                    (g, (i0, i1, i2, i3), (o0, o1 + 1, o2, o3)),
                    (g, (i0, i1, i2, i3), (o0, o1, o2 + 1, o3)),
                    (g, (i0, i1, i2, i3), (o0, o1, o2, o3 + 1))]
         metafunc.parametrize('greend, x_shape, y_shape', params2 + params3)