Exemplo n.º 1
0
def bochner_norm(T, space_norm2, U, mu=None, order=2):
    '''
      L^2-in-time, X-in-space
    '''
    nt = len(U)
    time_grid = OnedGrid(domain=(0., T), num_intervals=nt - 1)
    assert len(U) == time_grid.size(1)
    qq = time_grid.quadrature_points(0, order=order)
    integral = 0.
    for entity in np.arange(time_grid.size(0)):
        # get quadrature
        qq_e = qq[entity]  # points
        ww = time_grid.reference_element.quadrature(order)[1]  # weights
        ie = time_grid.integration_elements(0)[entity]  # integration element
        # create shape function evaluations
        a = time_grid.centers(1)[entity]
        b = time_grid.centers(1)[entity + 1]
        SF = np.array((1. / (a - b) * qq_e[..., 0] - b / (a - b),
                       1. / (b - a) * qq_e[..., 0] - a / (b - a)))
        U_a = U._list[entity]
        U_b = U._list[entity + 1]
        values = np.zeros(len(qq_e))
        for ii in np.arange(len(qq_e)):
            # compute U(t)
            U_t = U_a.copy()
            U_t.scal(SF[0][ii])
            U_t.axpy(SF[1][ii], U_b)
            # compute the X-norm of U(t)
            values[ii] = space_norm2(make_listvectorarray(U_t), mu)
        integral += np.dot(values, ww) * ie
    return np.sqrt(integral)
Exemplo n.º 2
0
    def discretize_CircleDomain():
        ni = int(m.ceil(domain_description.width / diameter))
        grid = OnedGrid(domain=domain_description.domain,
                        num_intervals=ni,
                        identify_left_right=True)
        bi = EmptyBoundaryInfo(grid)

        return grid, bi
Exemplo n.º 3
0
def bochner_norm(T, space_norm2, U, mu=None, order=2):
    '''
      L^2-in-time, X-in-space
    '''
    nt = len(U)
    time_grid = OnedGrid(domain=(0., T), num_intervals=nt-1)
    assert len(U) == time_grid.size(1)
    qq = time_grid.quadrature_points(0, order=order)
    integral = 0.
    for entity in np.arange(time_grid.size(0)):
        # get quadrature
        qq_e = qq[entity] # points
        ww = time_grid.reference_element.quadrature(order)[1] # weights
        ie = time_grid.integration_elements(0)[entity] # integration element
        # create shape function evaluations
        a = time_grid.centers(1)[entity]
        b = time_grid.centers(1)[entity + 1]
        SF = np.array((1./(a - b)*qq_e[..., 0] - b/(a - b),
                       1./(b - a)*qq_e[..., 0] - a/(b - a)))
        U_a = U._list[entity]
        U_b = U._list[entity + 1]
        values = np.zeros(len(qq_e))
        for ii in np.arange(len(qq_e)):
            # compute U(t)
            U_t = U_a.copy()
            U_t.scal(SF[0][ii])
            U_t.axpy(SF[1][ii], U_b)
            # compute the X-norm of U(t)
            values[ii] = space_norm2(make_listvectorarray(U_t), mu)
        integral += np.dot(values, ww)*ie
    return np.sqrt(integral)
Exemplo n.º 4
0
 def prolong(coarse_disc, coarse_U):
     time_grid_ref = OnedGrid(domain=(0., T), num_intervals=nt)
     time_grid = OnedGrid(domain=(0., T), num_intervals=(len(coarse_U) - 1))
     U_fine = [None for ii in time_grid_ref.centers(1)]
     for n in np.arange(len(time_grid_ref.centers(1))):
         t_n = time_grid_ref.centers(1)[n]
         coarse_entity = min((time_grid.centers(1) <= t_n).nonzero()[0][-1],
                             time_grid.size(0) - 1)
         a = time_grid.centers(1)[coarse_entity]
         b = time_grid.centers(1)[coarse_entity + 1]
         SF = np.array((1./(a - b)*t_n - b/(a - b),
                        1./(b - a)*t_n - a/(b - a)))
         U_t = coarse_U.copy(ind=coarse_entity)
         U_t.scal(SF[0][0])
         U_t.axpy(SF[1][0], coarse_U, x_ind=(coarse_entity + 1))
         U_fine[n] = wrapper[example.prolong(coarse_disc._impl, U_t._list[0]._impl)]
     return make_listvectorarray(U_fine)
Exemplo n.º 5
0
    def compute_norm(self, level, id):
        self._compute_reference_solution()
        self._prolong_onto_reference(level)
        diff = [
            ref.vector_copy() - sol.vector_copy() for ref, sol in zip(
                self._solution[-1], self._solution_as_reference[level])
        ]
        time_norm_id, space_norm_id = id.split('-')
        time_norm_id, space_norm_id = time_norm_id.strip(
        ), space_norm_id.strip()
        if space_norm_id == 'L2':

            def space_norm2(U):
                return U * (self._l2_product * U)
        elif space_norm_id == 'elliptic_mu_bar':

            def space_norm2(U):
                return U * (self._elliptic_mu_bar_product * U)
        else:
            assert False
        if time_norm_id == 'L_oo':
            return np.max(np.sqrt([space_norm2(U) for U in diff]))
        elif time_norm_id == 'L2':
            T = self._T
            nt = len(diff)
            time_integration_order = 1
            time_grid = OnedGrid(domain=(0., T), num_intervals=nt - 1)
            qq = time_grid.quadrature_points(0, order=time_integration_order)
            ww = time_grid.reference_element.quadrature(
                time_integration_order)[1]  # weights
            integral = 0.
            for entity in np.arange(time_grid.size(0)):
                # get quadrature
                qq_e = qq[entity]  # points
                ie = time_grid.integration_elements(0)[entity]
                # create shape function evaluations
                a = time_grid.centers(1)[entity]
                b = time_grid.centers(1)[entity + 1]
                SF = np.array((
                    1. / (a - b) * qq_e[..., 0] - b / (a - b),  # P1 in time
                    1. / (b - a) * qq_e[..., 0] - a / (b - a)))
                U_a = diff[entity]
                U_b = diff[entity + 1]
                values = np.zeros(len(qq_e))
                for ii in np.arange(len(qq_e)):
                    U_t = U_a.copy()
                    U_t.scal(SF[0][ii])
                    U_t.axpy(SF[1][ii], U_b)
                    values[ii] = space_norm2(U_t)
                integral += np.dot(values, ww) * ie
            return np.sqrt(integral)
        else:
            assert False
Exemplo n.º 6
0
    def discretize_LineDomain():
        ni = int(m.ceil(domain_description.width / diameter))
        grid = OnedGrid(domain=domain_description.domain, num_intervals=ni)

        def indicator_factory(dd, bt):
            def indicator(X):
                L = np.logical_and(float_cmp(X[:, 0], dd.domain[0]), dd.left == bt)
                R = np.logical_and(float_cmp(X[:, 0], dd.domain[1]), dd.right == bt)
                return np.logical_or(L, R)
            return indicator

        indicators = {bt: indicator_factory(domain_description, bt)
                      for bt in domain_description.boundary_types}
        bi = GenericBoundaryInfo.from_indicators(grid, indicators)

        return grid, bi
Exemplo n.º 7
0
def discretize(n, nt, blocks):
    h = 1. / blocks
    ops = [
        WrappedDiffusionOperator.create(n, h * i, h * (i + 1))
        for i in range(blocks)
    ]
    pfs = [
        ProjectionParameterFunctional('diffusion_coefficients', (blocks, ),
                                      (i, )) for i in range(blocks)
    ]
    operator = LincombOperator(ops, pfs)

    initial_data = operator.source.zeros()

    # use data property of WrappedVector to setup rhs
    # note that we cannot use the data property of ListVectorArray,
    # since ListVectorArray will always return a copy
    rhs_vec = operator.range.zeros()
    rhs_data = rhs_vec._list[0].data
    rhs_data[:] = np.ones(len(rhs_data))
    rhs_data[0] = 0
    rhs_data[len(rhs_data) - 1] = 0
    rhs = VectorFunctional(rhs_vec)

    # hack together a visualizer ...
    grid = OnedGrid(domain=(0, 1), num_intervals=n)
    visualizer = Matplotlib1DVisualizer(grid)

    time_stepper = ExplicitEulerTimeStepper(nt)
    parameter_space = CubicParameterSpace(operator.parameter_type, 0.1, 1)

    d = InstationaryDiscretization(T=1e-0,
                                   operator=operator,
                                   rhs=rhs,
                                   initial_data=initial_data,
                                   time_stepper=time_stepper,
                                   num_values=20,
                                   parameter_space=parameter_space,
                                   visualizer=visualizer,
                                   name='C++-Discretization',
                                   cache_region=None)
    return d
Exemplo n.º 8
0
     ((2, 4), dict(identify_left_right=True)
      ), ((2, 4), dict(
          identify_bottom_top=True
      )), ((2, 4), dict(identify_left_right=True, identify_bottom_top=True)
           ), ((2, 1),
               dict(identify_left_right=True)),
     ((1, 2), dict(identify_bottom_top=True)
      ), ((2, 2), dict(identify_left_right=True, identify_bottom_top=True)),
     ((42, 30), dict(
         identify_left_right=True)), ((42, 30),
                                      dict(identify_bottom_top=True)),
     ((42, 30), dict(identify_left_right=True, identify_bottom_top=True))]
]

oned_grid_generators = [
    lambda kwargs=kwargs: OnedGrid(**kwargs) for kwargs in [
        dict(domain=np.array((-2, 2)), num_intervals=10),
        dict(domain=np.array((-4, -2)), num_intervals=100),
        dict(domain=np.array((-4, -2)),
             num_intervals=100,
             identify_left_right=True),
        dict(domain=np.array((2, 3)), num_intervals=10),
        dict(domain=np.array((2, 3)),
             num_intervals=10,
             identify_left_right=True),
        dict(domain=np.array((1, 2)), num_intervals=10000)
    ]
]

unstructured_grid_generators = [
    lambda: UnstructuredTriangleGrid(
Exemplo n.º 9
0
 def _prolong_onto_reference(self, level):
     if level in self._solution_as_reference:
         return
     # reconstruct, if reduced
     if 'reductor' in self._d_data[level]:
         assert False  # not yet implemented
         reconstructed_soluion = self._d_data[level][
             'reductor'].reconstruct(self._solution[level])
     else:
         reconstructed_soluion = self._solution[level]
     if 'block_space' in self._d_data[level]:
         coarse_solution = tuple(
             make_discrete_function(self._d_data[level]['block_space'],
                                    vec.impl) for vec in self._d_data[level]
             ['unblock'](reconstructed_soluion)._list)
     else:
         coarse_solution = tuple(
             make_discrete_function(self._d_data[level]['space'], vec.impl)
             for vec in reconstructed_soluion._list)
     # prolong in space
     coarse_in_time_fine_in_space = tuple(
         make_discrete_function(self._d_data[-1]['space'])
         for nt in range(len(coarse_solution)))
     for coarse, fine in zip(coarse_solution, coarse_in_time_fine_in_space):
         prolong(coarse, fine)
     # prolong in time
     T = self._T
     coarse_time_grid = OnedGrid(domain=(0., T),
                                 num_intervals=(len(self._solution[level]) -
                                                1))
     fine_time_grid = OnedGrid(domain=(0., T),
                               num_intervals=len(self._solution[-1]) - 1)
     self._solution_as_reference[level] = [
         None for ii in fine_time_grid.centers(1)
     ]
     for n in np.arange(len(fine_time_grid.centers(1))):
         t_n = fine_time_grid.centers(1)[n]
         coarse_entity = min(
             (coarse_time_grid.centers(1) <= t_n).nonzero()[0][-1],
             coarse_time_grid.size(0) - 1)
         a = coarse_time_grid.centers(1)[coarse_entity]
         b = coarse_time_grid.centers(1)[coarse_entity + 1]
         SF = np.array((
             1. / (a - b) * t_n - b / (a - b),  # P1 in tim
             1. / (b - a) * t_n - a / (b - a)))
         U_t = coarse_in_time_fine_in_space[coarse_entity].vector_copy()
         U_t.scal(SF[0][0])
         U_t.axpy(
             SF[1][0],
             coarse_in_time_fine_in_space[coarse_entity + 1].vector_copy())
         self._solution_as_reference[level][n] = make_discrete_function(
             self._d_data[-1]['space'], U_t)
Exemplo n.º 10
0
 def prolong(coarse_disc, coarse_U):
     time_grid_ref = OnedGrid(domain=(0., T), num_intervals=nt)
     time_grid = OnedGrid(domain=(0., T), num_intervals=(len(coarse_U) - 1))
     U_fine = [None for ii in time_grid_ref.centers(1)]
     for n in np.arange(len(time_grid_ref.centers(1))):
         t_n = time_grid_ref.centers(1)[n]
         coarse_entity = min((time_grid.centers(1) <= t_n).nonzero()[0][-1],
                             time_grid.size(0) - 1)
         a = time_grid.centers(1)[coarse_entity]
         b = time_grid.centers(1)[coarse_entity + 1]
         SF = np.array((1. / (a - b) * t_n - b / (a - b),
                        1. / (b - a) * t_n - a / (b - a)))
         U_t = coarse_U.copy(ind=coarse_entity)
         U_t.scal(SF[0][0])
         U_t.axpy(SF[1][0], coarse_U, x_ind=(coarse_entity + 1))
         U_fine[n] = wrapper[example.prolong(coarse_disc._impl,
                                             U_t._list[0]._impl)]
     return make_listvectorarray(U_fine)