コード例 #1
0
ファイル: test_greenop.py プロジェクト: sbrisard/janus
 def test_init_invalid_dimension(self):
     if self.dim == 2:
         mat = material.create(1.0, 0.3, 3)
         cls = material._GreenOperatorForStrains2D
     elif self.dim == 3:
         mat = material.create(1.0, 0.3, 2)
         cls = material._GreenOperatorForStrains3D
     else:
         raise ValueError()
     with pytest.raises(ValueError):
         cls(mat)
コード例 #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)
コード例 #3
0
 def test_apply_by_freq(self, n, x, flag):
     """flag allows the specification of various calling sequences:
       - flag = 0: apply_by_freq(x)
       - flag = 1: apply_by_freq(x, x)
       - flag = 2: apply_by_freq(x, y)
     """
     dim = len(n)
     green = self.greend(material.create(MU, NU, dim).green_operator(),
                         n, 1.)
     for b in multi_indices(n):
         green.set_frequency(b)
         g = np.asarray(green.to_memoryview())
         expected = np.dot(g, x)
         if flag == 0:
             base = None
         elif flag == 1:
             base = x
         elif flag == 2:
             base = np.empty_like(expected)
         else:
             raise(ValueError)
         actual = green.apply_by_freq(x, base)
         if flag != 0:
             #assert get_base(actual) is base
             assert actual.base is base
         assert_allclose(actual, expected, ULP, ULP)
コード例 #4
0
 def test_apply_by_freq(self, n, x, flag):
     """flag allows the specification of various calling sequences:
       - flag = 0: apply_by_freq(x)
       - flag = 1: apply_by_freq(x, x)
       - flag = 2: apply_by_freq(x, y)
     """
     dim = len(n)
     green = self.greend(
         material.create(MU, NU, dim).green_operator(), n, 1.)
     for b in multi_indices(n):
         green.set_frequency(b)
         g = np.asarray(green.to_memoryview())
         expected = np.dot(g, x)
         if flag == 0:
             base = None
         elif flag == 1:
             base = x
         elif flag == 2:
             base = np.empty_like(expected)
         else:
             raise (ValueError)
         actual = green.apply_by_freq(x, base)
         if flag != 0:
             #assert get_base(actual) is base
             assert actual.base is base
         assert_allclose(actual, expected, ULP, ULP)
コード例 #5
0
    def test_apply(self, path_to_ref, rtol, flag):
        comm = MPI.COMM_WORLD
        root = 0
        rank = comm.rank

        # Load reference data
        n = None
        expected = None
        if rank == root:
            npz_file = np.load(path_to_ref)
            x = npz_file['x']
            expected = npz_file['y']
            n = x.shape[:-1]

        # Broadcast global size of grid
        n = comm.bcast(n, root)

        # Create local FFT object, and send local grid-size to root process
        transform = fft.create_real(n, comm, flags=FFTW_ESTIMATE)
        n_locs = comm.gather((transform.ishape[0], transform.offset0), root)

        green = self.greend(material.create(0.75, 0.3, len(n))
                                    .green_operator(),
                            n, 1., transform)

        # Scatter x
        x_locs = None
        if rank == root:
            x_locs = [x[offset0:offset0 + n0] for n0, offset0 in n_locs]
        x_loc = comm.scatter(x_locs, root)

        # if flag == 0:
        #     y_loc = green.apply(x_loc)
        # elif flag == 1:
        #     y_loc = green.apply(x_loc, x_loc)
        # elif flag == 2:
        #     base = np.empty(transform.ishape + (green.oshape[-1],),
        #                     dtype=np.float64)
        #     y_loc = green.apply(x_loc, base)
        # else:
        #     raise ValueError()

        y_loc = np.empty(transform.ishape + (green.oshape[-1],),
                        dtype=np.float64)
        green.apply(x_loc, y_loc)

        # Gather y
        y_locs = comm.gather(y_loc, root)
        if rank == root:
            actual = np.empty_like(expected)
            for y_loc, (n0, offset0) in zip(y_locs, n_locs):
                actual[offset0:offset0 + n0] = y_loc
            assert_allclose(actual, expected, rtol, 10 * ULP)
コード例 #6
0
    def test_apply(self, path_to_ref, rtol, flag):
        comm = MPI.COMM_WORLD
        root = 0
        rank = comm.rank

        # Load reference data
        n = None
        expected = None
        if rank == root:
            npz_file = np.load(path_to_ref)
            x = npz_file['x']
            expected = npz_file['y']
            n = x.shape[:-1]

        # Broadcast global size of grid
        n = comm.bcast(n, root)

        # Create local FFT object, and send local grid-size to root process
        transform = fft.create_real(n, comm, flags=FFTW_ESTIMATE)
        n_locs = comm.gather((transform.ishape[0], transform.offset0), root)

        green = self.greend(
            material.create(0.75, 0.3, len(n)).green_operator(), n, 1.,
            transform)

        # Scatter x
        x_locs = None
        if rank == root:
            x_locs = [x[offset0:offset0 + n0] for n0, offset0 in n_locs]
        x_loc = comm.scatter(x_locs, root)

        # if flag == 0:
        #     y_loc = green.apply(x_loc)
        # elif flag == 1:
        #     y_loc = green.apply(x_loc, x_loc)
        # elif flag == 2:
        #     base = np.empty(transform.ishape + (green.oshape[-1],),
        #                     dtype=np.float64)
        #     y_loc = green.apply(x_loc, base)
        # else:
        #     raise ValueError()

        y_loc = np.empty(transform.ishape + (green.oshape[-1], ),
                         dtype=np.float64)
        green.apply(x_loc, y_loc)

        # Gather y
        y_locs = comm.gather(y_loc, root)
        if rank == root:
            actual = np.empty_like(expected)
            for y_loc, (n0, offset0) in zip(y_locs, n_locs):
                actual[offset0:offset0 + n0] = y_loc
            assert_allclose(actual, expected, rtol, 10 * ULP)
コード例 #7
0
 def test_to_memoryview(self, n, flag):
     dim = len(n)
     greenc = material.create(MU, NU, dim).green_operator()
     greend = self.greend(greenc, n, 1.0)
     k = np.empty((dim, ), dtype=np.float64)
     for b in multi_indices(n):
         expected = self.to_memoryview_expected(greenc, n, b)
         greend.set_frequency(b)
         if flag == 0:
             actual = greend.to_memoryview()
         elif flag == 1:
             base = np.empty_like(expected)
             actual = greend.to_memoryview(base)
             assert actual.base is base
         else:
             raise ValueError
         assert_allclose(actual, expected, ULP, ULP)
コード例 #8
0
 def test_to_memoryview(self, n, flag):
     dim = len(n)
     greenc = material.create(MU, NU, dim).green_operator()
     greend = self.greend(greenc, n, 1.0)
     k = np.empty((dim,), dtype=np.float64)
     for b in multi_indices(n):
         expected = self.to_memoryview_expected(greenc, n, b)
         greend.set_frequency(b)
         if flag == 0:
             actual = greend.to_memoryview()
         elif flag == 1:
             base = np.empty_like(expected)
             actual = greend.to_memoryview(base)
             assert actual.base is base
         else:
             raise ValueError
         assert_allclose(actual, expected, ULP, ULP)
コード例 #9
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)
コード例 #10
0
def test_poisson_from_bulk_and_shear_moduli(g, nu, dim):
    mat = material.create(g, nu, dim)
    k = mat.k
    assert material.poisson_from_bulk_and_shear_moduli(k, g, dim) == nu
コード例 #11
0
ファイル: square_basic.py プロジェクト: sbrisard/janus
    # End: apply


# Begin: params
if __name__ == "__main__":
    dim = 2  # Spatial dimension
    sym = (dim * (dim + 1)) // 2  # Dim. of space of second rank, symmetric tensors
    n = 256  # Number of cells along each side of the grid
    mu_i, nu_i = 100, 0.2  # Shear modulus and Poisson ratio of inclusion
    mu_m, nu_m = 1, 0.3  # Shear modulus and Poisson ratio of matrix
    mu_0, nu_0 = 50, 0.3  # Shear modulus and Poisson ratio of ref. mat.
    num_cells = n ** dim  # Total number of cells
    # End: params
    # Begin: instantiate example
    example = Example(
        mat_i=material.create(mu_i, nu_i, dim),
        mat_m=material.create(mu_m, nu_m, dim),
        mat_0=material.create(mu_0, nu_0, dim),
        n=n,
        dim=dim,
    )
    # End: instantiate example
    # Begin: define strains
    avg_eps = np.zeros((sym,), dtype=np.float64)
    avg_eps[-1] = 1.0

    eps = np.empty(example.green.ishape, dtype=np.float64)
    new_eps = np.empty_like(eps)
    # End: define strains
    # Begin: iterate
    num_iter = 400
コード例 #12
0
ファイル: test_greenop.py プロジェクト: sbrisard/janus
 def material(self):
     return material.create(self.mu, self.nu, self.dim)
コード例 #13
0
ファイル: square_basic.py プロジェクト: BraMurgas/janus
        self.green.apply(out, out)
    # End: apply


# Begin: params
if __name__ == '__main__':
    dim = 2                # Spatial dimension
    sym = (dim*(dim+1))//2 # Dim. of space of second rank, symmetric tensors
    n = 256                # Number of cells along each side of the grid
    mu_i, nu_i = 100, 0.2  # Shear modulus and Poisson ratio of inclusion
    mu_m, nu_m = 1, 0.3    # Shear modulus and Poisson ratio of matrix
    mu_0, nu_0 = 50, 0.3   # Shear modulus and Poisson ratio of ref. mat.
    num_cells = n**dim     # Total number of cells
    # End: params
    # Begin: instantiate example
    example = Example(mat_i=material.create(mu_i, nu_i, dim),
                      mat_m=material.create(mu_m, nu_m, dim),
                      mat_0=material.create(mu_0, nu_0, dim),
                      n=n,
                      dim=dim)
    # End: instantiate example
    # Begin: define strains
    avg_eps = np.zeros((sym,), dtype=np.float64)
    avg_eps[-1] = 1.0

    eps = np.empty(example.green.ishape, dtype=np.float64)
    new_eps = np.empty_like(eps)
    # End: define strains
    # Begin: iterate
    num_iter = 400
    res = np.empty((num_iter,), dtype=np.float64)
コード例 #14
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)
コード例 #15
0
 def test_read_only_attribute(self, name, value):
     mat = material.create(1.0, 0.3, 3)
     with pytest.raises(AttributeError):
         setattr(mat, name, value)
コード例 #16
0
 def test_init_invalid_params(self, g, nu, dim):
     with pytest.raises(ValueError):
         mat = material.create(g, nu, dim)
コード例 #17
0
 def test_init(self, g, k, nu, dim):
     mat = material.create(g, nu, dim)
     assert mat.g == g
     assert mat.k == k
     assert mat.nu == nu
コード例 #18
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)
コード例 #19
0
ファイル: test_memory_leak.py プロジェクト: ljuillen/janus
def truncated_factory():
    return truncated(material.create(1.0, 0.3, 2).green_operator(),
                     (128, 128),
                     1.)
コード例 #20
0
ファイル: square_inclusion.py プロジェクト: ljuillen/janus
        self.tau2eps = operators.BlockDiagonalOperator2D(ops)

    def create_vector(self):
        return PETSc.Vec().createMPI(size=(self.n1 * self.n1 * 3),
                                     bsize=(self.n0 * self.n1 * 3))

    def mult(self, mat, x, y):
        x_arr = x.array.reshape((self.n0, self.n1, 3))
        y_arr = y.array.reshape((self.n0, self.n1, 3))
        self.green.apply(x_arr, y_arr)
        y_arr += np.asarray(self.tau2eps.apply(x_arr))

if __name__ == '__main__':
    comm = MPI.COMM_WORLD

    mat_i = material.create(10.0, 0.2, dim=2)
    mat_m = material.create(1.0, 0.3, dim=2)
    mat_0 = material.create(0.9, 0.3, dim=2)

    a = 0.5
    n = 256

    example = SquareInclusion(a, mat_i, mat_m, mat_0, n)
    local_shape = (example.n0, example.n1, 3)

    x = example.create_vector()
    y = example.create_vector()
    b = example.create_vector()

    x_arr = x.array.reshape(local_shape)
    y_arr = y.array.reshape(local_shape)
コード例 #21
0
import sys

sys.path.append('..')

import numpy as np
import skimage.io

skimage.io.use_plugin('freeimage', 'imread')
skimage.io.use_plugin('freeimage', 'imsave')

import janus.green as green
import janus.material.elastic.linear.isotropic as material

mat = material.create(1.0, 0.3, 2)
n0 = 256
n1 = 256
n = (n0, n1)

greenc = mat.green_operator()
greend = green.FilteredGreenOperator2D(greenc, n, 1.)

g = np.empty((n0, n1), dtype=np.float64)
b = np.empty((2,), dtype=np.intc)

tau = np.array([0., 0., 1.])
eta = np.empty_like(tau)

for b0 in range(n0):
    b[0] = b0
    for b1 in range(n1):
        b[1] = b1