Last active
October 27, 2020 08:35
-
-
Save renefritze/57db73707f97ed0bd4eb600a1a6f9ab3 to your computer and use it in GitHub Desktop.
binder test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pymor[full] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
np.warnings.filterwarnings('ignore') | |
from matplotlib import pyplot as plt | |
from pymor.analyticalproblems.elliptic import StationaryProblem | |
from pymor.analyticalproblems.functions import ( | |
ConstantFunction, | |
ExpressionFunction, | |
GenericFunction, | |
LincombFunction, | |
) | |
from pymor.analyticalproblems.domaindescriptions import RectDomain | |
from pymor.parameters.functionals import ProjectionParameterFunctional | |
problem = StationaryProblem( | |
domain=RectDomain(([0, 0], [1, 1]), | |
left='dirichlet', top='neumann', right='neumann', bottom='neumann'), | |
rhs=ConstantFunction(0, dim_domain=2), | |
diffusion=LincombFunction( | |
[ExpressionFunction('1.*(x[..., 1] < 0.1)', dim_domain=2), | |
ExpressionFunction('1.*(x[..., 1] > 0.1)', dim_domain=2)], | |
[ProjectionParameterFunctional('diffusion', 1, 0), | |
1] | |
), | |
advection=GenericFunction( | |
mapping=lambda x: np.vstack([np.zeros(x.shape[0]), x[..., 0]]).T, | |
dim_domain=2, shape_range=(2,)), | |
reaction=LincombFunction( | |
[ExpressionFunction('1.*(x[..., 1] > 0.9)', dim_domain=2), | |
ExpressionFunction('1.*(x[..., 1] < 0.9)', dim_domain=2)], | |
[ProjectionParameterFunctional('reaction', 1, 0), | |
1] | |
), | |
dirichlet_data=ConstantFunction(0, dim_domain=2), | |
neumann_data=ExpressionFunction('-1.*(x[..., 0] > (1 - 1e-7))', dim_domain=2), | |
outputs=(('l2', ExpressionFunction('20.*(x[..., 0] > 0.95)', dim_domain=2)),), | |
parameter_ranges={'diffusion': (0.1, 1), 'reaction': (0, 10)}, | |
) | |
from pymor.discretizers.builtin.grids.rect import RectGrid | |
from pymor.discretizers.builtin.cg import discretize_stationary_cg | |
fom, fom_data = discretize_stationary_cg( | |
problem, diameter=np.sqrt(2)/100, grid_type=RectGrid) | |
fom.enable_caching('memory') | |
U = fom.solve(mu={'diffusion': [1], 'reaction': [0]}) | |
U = fom.solution_space.empty() | |
for diff in (0.1, 1): | |
for react in (0, 10): | |
U.append(fom.solve(mu={'diffusion': [diff], 'reaction': [react]})) | |
def io_fom(mu): | |
return fom.output(mu=mu).to_numpy()[0][0] | |
io_fom([1, 0]) | |
for mu in problem.parameter_space.sample_uniformly(2): | |
print(f'output({mu}) = {fom.output(mu=mu).to_numpy()[0][0]}') | |
timing_mus = problem.parameter_space.sample_randomly(10) | |
for mu in timing_mus: | |
fom.solve(mu) | |
for mu in timing_mus: | |
io_fom(mu) | |
from pymor.reductors.coercive import CoerciveRBReductor | |
from pymor.algorithms.greedy import rb_greedy | |
from pymor.parameters.functionals import ExpressionParameterFunctional | |
training_set = problem.parameter_space.sample_uniformly(10) | |
product = fom.h1_0_semi_product | |
rb_reductor = CoerciveRBReductor( | |
fom, | |
product=product, | |
coercivity_estimator=ExpressionParameterFunctional('1', fom.parameters) | |
) | |
greedy_data = rb_greedy(fom, rb_reductor, training_set) | |
for mu in greedy_data['max_err_mus'][:4]: | |
U = fom.solve(mu=mu) | |
assert U |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment