Beispiel #1
0
def elasticity_calculate_errors(ref_level=9,  levels=(1, 3, 5, 7), steps=10):
    ref_disc = ElasticityExample(ref_level)

    log = logging.getLogger()
    errors = {}
    min_lambda, max_lambda = 1., 10.
    lambdas = np.linspace(min_lambda, max_lambda, steps)
    for lmbda in lambdas:
        param = {"lambda": [lmbda], "mu": [1.]}
        try:
            ref_sol = ref_disc.solve(param)
        except RuntimeError as e:
            log.error('fine solve failed lvl {} -- lambda {}'.format(ref_level, lmbda))
            for coarse_level in levels:
                if not errors.has_key(coarse_level):
                    errors[coarse_level] = []
                errors[coarse_level].append(np.nan)
            continue
        for coarse_level in levels:
            if not errors.has_key(coarse_level):
                errors[coarse_level] = []
            coarse_disc = ElasticityExample(coarse_level)
            try:
                coarse_sol = coarse_disc.solve(param)
            except RuntimeError as e:
                log.error('coarse solve failed lvl {} -- lambda {}'.format(coarse_level, lmbda))
                errors[coarse_level].append(np.nan)
                continue
            coarse_disc.visualize([coarse_sol], ['coarse_lvl_{}_lambda_{}.vtk'.format(coarse_level, lmbda)])
            prolonged = coarse_disc.transfer_to(ref_level - coarse_level, coarse_sol)
            prolonged -= ref_sol
            errors[coarse_level].append(coarse_disc.h1_0_semi_norm(prolonged)/coarse_disc.h1_0_semi_norm(ref_sol))
    for coarse_level in errors.keys():
        plt.plot(lambdas, errors[coarse_level], MARKERS[coarse_level])

    open('elas__{}__ref-{}_lambda-min_{}-max_{}_-_steps_{}.dump'.format(levels, ref_level, min_lambda, max_lambda, steps), 'wb').write(dumps((errors, lambdas)))

    elasticity_error_curves(errors, lambdas)
Beispiel #2
0
from pymor.discretizations.basic import StationaryDiscretization
from pymor.tools.timing import Timer

from pydealii_bindings import ElasticityExample
from pydealii.pymor.operator import DealIIMatrixOperator
from pydealii.pymor.vectorarray import DealIIVector


class PyVis(object):
    def __init__(self, cpp_disc):
        self._cpp_disc = cpp_disc

    def visualize(self, U, _, filename):
        self._cpp_disc.visualize(U._list[0].impl, filename)

cpp_disc = ElasticityExample(refine_steps=7)

param = {"lambda": [1.], "mu": [1.]}
# u = cpp_disc.solve(param)
parameter_type = Parameter(param).parameter_type
# cpp_disc.visualize(u, "highdim_solution_cpp.vtk")

lambda_fn, mu_fn = [GenericParameterFunctional(lambda mu: mu[n], Parameter({n: [1.]}).parameter_type) for n in ['lambda', 'mu']]
LOW, HIGH = 1, 10
ops = [DealIIMatrixOperator(getattr(cpp_disc, name)()) for name in ['lambda_mat', 'mu_mat']]
op = LincombOperator(ops, (lambda_fn, mu_fn))
rhs = VectorFunctional(ListVectorArray([DealIIVector(cpp_disc.rhs())]))
viz = PyVis(cpp_disc)
h1_op = DealIIMatrixOperator(cpp_disc.h1_mat(), "h1_0_semi")
energy_op = DealIIMatrixOperator(cpp_disc.mu_mat(), "energy")
py_disc = StationaryDiscretization(op, rhs, products={"energy": energy_op},