示例#1
0
文件: cg2.py 项目: BarbaraV/pymor
def cg2_demo(nrhs, n, plot):
    rhs0 = GenericFunction(lambda X: np.ones(X.shape[:-1]) * 10, dim_domain=2)        # NOQA
    rhs1 = GenericFunction(lambda X: (X[..., 0] - 0.5) ** 2 * 1000, dim_domain=2)     # NOQA

    assert 0 <= nrhs <= 1, ValueError('Invalid rhs number.')
    rhs = eval('rhs{}'.format(nrhs))

    d0 = GenericFunction(lambda X: 1 - X[..., 0], dim_domain=2)
    d1 = GenericFunction(lambda X: X[..., 0], dim_domain=2)

    parameter_space = CubicParameterSpace({'diffusionl': 0}, 0.1, 1)
    f0 = ProjectionParameterFunctional('diffusionl', 0)
    f1 = GenericParameterFunctional(lambda mu: 1, {})

    print('Solving on TriaGrid(({0},{0}))'.format(n))

    print('Setup Problem ...')
    problem = EllipticProblem(domain=RectDomain(), rhs=rhs, diffusion_functions=(d0, d1),
                              diffusion_functionals=(f0, f1), name='2DProblem')

    print('Discretize ...')
    discretization, _ = discretize_elliptic_cg(problem, diameter=m.sqrt(2) / n)

    print('The parameter type is {}'.format(discretization.parameter_type))

    U = discretization.type_solution.empty(discretization.dim_solution)
    for mu in parameter_space.sample_uniformly(10):
        U.append(discretization.solve(mu))

    if plot:
        print('Plot ...')
        discretization.visualize(U, title='Solution for diffusionl in [0.1, 1]')
示例#2
0
def cg_oned_demo(nrhs, n, plot):
    rhs0 = GenericFunction(lambda X: np.ones(X.shape) * 10, dim_domain=1)
    rhs1 = GenericFunction(lambda X: (X[..., 0] - 0.5) ** 2 * 1000, dim_domain=1)

    assert 0 <= nrhs <= 1, ValueError('Invalid rhs number.')
    rhs = eval('rhs{}'.format(nrhs))

    d0 = GenericFunction(lambda X: 1 - X[..., 0], dim_domain=1)
    d1 = GenericFunction(lambda X: X[..., 0], dim_domain=1)

    parameter_space = CubicParameterSpace({'diffusionl': 1}, 0.1, 1)
    f0 = ProjectionParameterFunctional(parameter_space, 'diffusionl')
    f1 = GenericParameterFunctional(parameter_space, lambda mu: 1)

    print('Solving on OnedGrid(({0},{0}))'.format(n))

    print('Setup Problem ...')
    problem = EllipticProblem(domain=LineDomain(), rhs=rhs, diffusion_functions=(d0, d1), diffusion_functionals=(f0, f1),
                              dirichlet_data=ConstantFunction(value=0, dim_domain=1))

    print('Discretize ...')
    discretization, _ = discretize_elliptic_cg(problem, diameter=1 / n)

    print(discretization.parameter_info())

    for mu in parameter_space.sample_uniformly(4):
        print('Solving for mu = {} ...'.format(mu))
        U = discretization.solve(mu)

        if plot:
            print('Plot ...')
            discretization.visualize(U)

        print('')