def estimate(self, U, mu=None): assert len(U) == 1, 'Can estimate only one solution vector' if not self.rhs.parametric or self.rhs.operator_affine_part is not None: CRA = np.ones(1) else: CRA = np.ones(0) if self.rhs.parametric: CRL = self.rhs.evaluate_coefficients(self.map_parameter(mu, 'rhs')) else: CRL = np.ones(0) CR = np.hstack((CRA, CRL)) if not self.operator.parametric or self.operator.operator_affine_part is not None: COA = np.ones(1) else: COA = np.ones(0) if self.operator.parametric: COL = self.operator.evaluate_coefficients(self.map_parameter(mu, 'operator')) else: COL = np.ones(0) C = np.hstack((CR, np.dot(np.hstack((COA, COL))[..., np.newaxis], U.data).ravel())) return induced_norm(self.estimator_matrix)(NumpyVectorArray(C))
def test_induced(): grid = TriaGrid(num_intervals=(10, 10)) boundary_info = AllDirichletBoundaryInfo(grid) product = L2ProductP1(grid, boundary_info) zero = la.NumpyVectorArray(np.zeros(grid.size(2))) norm = la.induced_norm(product) value = norm(zero) np.testing.assert_almost_equal(value, 0.0)
def estimate(self, U, mu, discretization): d = discretization if len(U) > 1: raise NotImplementedError if not d.rhs.parametric: CR = np.ones(1) else: CR = d.rhs.evaluate_coefficients(mu) if not d.operator.parametric: CO = np.ones(1) else: CO = d.operator.evaluate_coefficients(mu) C = np.hstack((CR, np.dot(CO[..., np.newaxis], U.data).ravel())) return induced_norm(self.estimator_matrix)(NumpyVectorArray(C))
def __init__(self, operators, functionals, vector_operators, products=None, estimator=None, visualizer=None, cache_region='disk', name=None): self.operators = FrozenDict(operators) self.functionals = FrozenDict(functionals) self.vector_operators = FrozenDict(vector_operators) self.linear = all(op is None or op.linear for op in chain(operators.itervalues(), functionals.itervalues())) self.products = products self.estimator = estimator self.visualizer = visualizer self.cache_region = cache_region self.name = name if products: for k, v in products.iteritems(): setattr(self, '{}_product'.format(k), v) setattr(self, '{}_norm'.format(k), induced_norm(v)) if estimator is not None: self.estimate = self.__estimate if visualizer is not None: self.visualize = self.__visualize
def __init__(self, parameter_range=(0.1, 1.), name=None): super(DuneLinearEllipticCGDiscretization, self).__init__() Cachable.__init__(self, config=NO_CACHE_CONFIG) self.example = dune.LinearEllipticExampleCG() ops = list(self.example.operators()) f = self.example.functional() self.solution_dim = f.len() ops = [DuneLinearOperator(op, dim=self.solution_dim) for op in ops] operator = LinearAffinelyDecomposedOperator(ops[:-1], ops[-1], name='diffusion') operator.rename_parameter({'.coefficients': 'diffusion'}) functional = DuneLinearFunctional(f) self.operators = {'operator': operator, 'rhs': functional} self.build_parameter_type(inherits={'operator': operator}) self.h1_product = operator.assemble(mu={'diffusion': np.ones(self.example.paramSize())}) self.h1_norm = induced_norm(self.h1_product) self.parameter_space = CubicParameterSpace({'diffusion': self.example.paramSize()}, *parameter_range) self.name = name
def discretize_elliptic_cg(analytical_problem, diameter=None, domain_discretizer=None, grid=None, boundary_info=None): '''Discretize an `EllipticProblem` using finite elements. Since operators are not assembled during instatiation, calling this function is cheap if the domain discretization proceeds quickly. Parameters ---------- analytical_problem The `EllipticProblem` to discretize. diameter If not None, is passed to the domain_discretizer. domain_discretizer Discretizer to be used for discretizing the analytical domain. This has to be function `domain_discretizer(domain_description, diameter=...)`. If further arguments should be passed to the discretizer, use functools.partial. If None, `discretize_domain_default` is used. grid Instead of using a domain discretizer, the grid can be passed directly. boundary_info A `BoundaryInfo` specifying the boundary types of the grid boundary entities. Must be provided is `grid` is provided. Returns ------- discretization The discretization that has been generated. data Dict with the following entries: grid The generated grid. boundary_info The generated `BoundaryInfo`. ''' assert isinstance(analytical_problem, EllipticProblem) assert grid is None or boundary_info is not None assert boundary_info is None or grid is not None assert grid is None or domain_discretizer is None if grid is None: domain_discretizer = domain_discretizer or discretize_domain_default if diameter is None: grid, boundary_info = domain_discretizer(analytical_problem.domain) else: grid, boundary_info = domain_discretizer(analytical_problem.domain, diameter=diameter) assert isinstance(grid, (OnedGrid, TriaGrid)) Operator = DiffusionOperatorP1 Functional = L2ProductFunctionalP1 p = analytical_problem if p.diffusion_functionals is not None or len(p.diffusion_functions) > 1: L0 = Operator(grid, boundary_info, diffusion_constant=0, name='diffusion_boundary_part') Li = tuple(Operator(grid, boundary_info, diffusion_function=df, dirichlet_clear_diag=True, name='diffusion_{}'.format(i)) for i, df in enumerate(p.diffusion_functions)) if p.diffusion_functionals is None: L = LinearAffinelyDecomposedOperator(Li, L0, name='diffusion') L.rename_parameter({'.coefficients': '.diffusion_coefficients'}) else: L = LinearAffinelyDecomposedOperator(Li, L0, p.diffusion_functionals, name='diffusion') else: L = Operator(grid, boundary_info, diffusion_function=p.diffusion_functions[0], name='diffusion') F = Functional(grid, p.rhs, boundary_info, dirichlet_data=p.dirichlet_data) import matplotlib.pyplot as pl if isinstance(grid, TriaGrid): def visualize(U): assert len(U) == 1 pl.tripcolor(grid.centers(2)[:, 0], grid.centers(2)[:, 1], grid.subentities(0, 2), U.data.ravel()) pl.colorbar() pl.show() else: def visualize(U): assert len(U) == 1 pl.plot(grid.centers(1), U.data.ravel()) pl.show() pass discretization = StationaryLinearDiscretization(L, F, visualizer=visualize, name='{}_CG'.format(p.name)) discretization.h1_product = Operator(grid, boundary_info) discretization.h1_norm = induced_norm(discretization.h1_product) discretization.l2_product = L2ProductP1(grid, boundary_info) if hasattr(p, 'parameter_space'): discretization.parameter_space = p.parameter_space return discretization, {'grid': grid, 'boundary_info': boundary_info}
def __init__(self, estimator_matrix, coercivity_estimator): self.estimator_matrix = estimator_matrix self.coercivity_estimator = coercivity_estimator self.norm = induced_norm(estimator_matrix)
def __init__(self, product): self.norm = induced_norm(product) if product else None