remove notebooks dir

parent 1a3edb7d
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/pymor/pymor/master)
{
"cells": [
{
"cell_type": "raw",
"metadata": {},
"source": [
"This file is part of the pyMOR project (http://www.pymor.org).\n",
"Copyright 2013-2020 pyMOR developers and contributors. All rights reserved.\n",
"License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Create heat equation model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import scipy.linalg as spla\n",
"import scipy.sparse as sps\n",
"import matplotlib.pyplot as plt\n",
"\n",
"from pymor.basic import *\n",
"from pymor.models.iosys import LinearDelayModel\n",
"from pymor.reductors.interpolation import DelayBHIReductor, TFBHIReductor"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"p = InstationaryProblem(\n",
" StationaryProblem(\n",
" domain=LineDomain([0.,1.], left='robin', right='robin'),\n",
" diffusion=ConstantFunction(1., 1),\n",
" robin_data=(ConstantFunction(1., 1), ExpressionFunction('(x[...,0] < 1e-10) * 1.', 1)),\n",
" outputs=(('l2_boundary', ExpressionFunction('(x[...,0] > (1 - 1e-10)) * 1.', 1)),)\n",
" ),\n",
" ConstantFunction(0., 1),\n",
" T=3.\n",
")\n",
"\n",
"fom, _ = discretize_instationary_cg(p, diameter=1/100, nt=100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lti = fom.to_lti()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Add delayed feedback from output to input"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tau = 1.\n",
"g = 5.\n",
"Atau = sps.coo_matrix(([g], ([0], [lti.order - 1])), (lti.order, lti.order)).tocsc()\n",
"Atau = NumpyMatrixOperator(Atau, source_id=lti.solution_space.id, range_id=lti.solution_space.id)\n",
"td_lti = LinearDelayModel(lti.A, (Atau,), (tau,), lti.B, lti.C, E=lti.E)\n",
"print(td_lti)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"w = np.logspace(-1, 2.5, 500)\n",
"td_lti.mag_plot(w, ax=ax)\n",
"ax.set_title('Magnitude plot of the FOM')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Unstructured Hermite interpolation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"interp = TFBHIReductor(td_lti)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r = 3\n",
"sigma = np.logspace(0, 1, r)\n",
"sigma = np.concatenate((1j * sigma, -1j * sigma))\n",
"b = np.ones((2*r, td_lti.dim_input))\n",
"c = np.ones((2*r, td_lti.dim_output))\n",
"\n",
"rom = interp.reduce(sigma, b, c)\n",
"err_rom = td_lti - rom"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"td_lti.mag_plot(w, ax=ax)\n",
"rom.mag_plot(w, ax=ax)\n",
"ax.set_title('Magnitude plot of the FOM and ROM')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"err_rom.mag_plot(w, ax=ax)\n",
"ax.set_title('Magnitude plot of the error')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Delay-preserving reduction by Hermite interpolation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"delay_interp = DelayBHIReductor(td_lti)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"td_rom = delay_interp.reduce(sigma, b, c)\n",
"err_td_rom = td_lti - td_rom"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"td_lti.mag_plot(w, ax=ax)\n",
"rom.mag_plot(w, ax=ax)\n",
"td_rom.mag_plot(w, ax=ax)\n",
"ax.set_title('Magnitude plot of the FOM and ROMs')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"err_rom.mag_plot(w, ax=ax, color='tab:orange')\n",
"err_td_rom.mag_plot(w, ax=ax, color='tab:green')\n",
"ax.set_title('Magnitude plot of the errors')\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"lines_to_next_cell": 2
},
"outputs": [],
"source": [
"from pymor.analyticalproblems.domaindescriptions import RectDomain\n",
"from pymor.analyticalproblems.elliptic import StationaryProblem\n",
"from pymor.discretizers.builtin import discretize_stationary_cg\n",
"from pymor.analyticalproblems.functions import ExpressionFunction, LincombFunction\n",
"from pymor.parameters.functionals import ProjectionParameterFunctional, ExpressionParameterFunctional\n",
"from time import sleep\n",
"from ipywidgets import interact, widgets\n",
"from pythreejs._example_helper import use_example_model_ids\n",
"use_example_model_ids()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rhs = ExpressionFunction('(x[..., 0] - 0.5)**2 * 1000', 2, ())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"lines_to_next_cell": 2
},
"outputs": [],
"source": [
"problem = StationaryProblem(\n",
" domain=RectDomain(),\n",
" rhs=rhs,\n",
" diffusion=LincombFunction(\n",
" [ExpressionFunction('1 - x[..., 0]', 2, ()), ExpressionFunction('x[..., 0]', 2, ())],\n",
" [ProjectionParameterFunctional('diffusionl'), 1]\n",
" ),\n",
" parameter_ranges=(0.01, 0.1),\n",
" name='2DProblem'\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"lines_to_next_cell": 2,
"scrolled": true
},
"outputs": [],
"source": [
"args = {'N': 100, 'samples': 1}\n",
"m, data = discretize_stationary_cg(problem, diameter=1. / args['N'])\n",
"U = m.solution_space.empty()\n",
"for mu in problem.parameter_space.sample_uniformly(args['samples']):\n",
" U.append(m.solve(mu))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Us = U * 1.5\n",
"m.visualize((U, Us), title='Solution for diffusionl=0.5')\n"
]
}
],
"metadata": {
"jupytext": {
"formats": "ipynb,py:percent"
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pymor.basic import *"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"N = 100\n",
"\n",
"rhs = ExpressionFunction('(x - 0.5)**2 * 1000', 1, ())\n",
"\n",
"d0 = ExpressionFunction('1 - x', 1, ())\n",
"d1 = ExpressionFunction('x', 1, ())\n",
"\n",
"f0 = ProjectionParameterFunctional('diffusionl')\n",
"f1 = 1."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"problem = StationaryProblem(\n",
" domain=LineDomain(),\n",
" rhs=rhs,\n",
" diffusion=LincombFunction([d0, d1], [f0, f1]),\n",
" dirichlet_data=ConstantFunction(value=0, dim_domain=1),\n",
" name='1DProblem'\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"parameter_space = problem.parameters.space(0.1, 1)\n",
"\n",
"discretizer = discretize_stationary_cg\n",
"m, data = discretizer(problem, diameter=1 / N)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"U = m.solution_space.empty()\n",
"for mu in parameter_space.sample_uniformly(10):\n",
" U.append(m.solve(mu))\n",
"m.visualize(U, title='Solution for diffusionl in [0.1, 1]', separate_plots=True, separate_axes=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
This diff is collapsed.
{
"cells": [
{
"cell_type": "raw",
"metadata": {},
"source": [
"This file is part of the pyMOR project (http://www.pymor.org).\n",
"Copyright 2013-2020 pyMOR developers and contributors. All rights reserved.\n",
"License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import scipy.linalg as spla\n",
"import matplotlib as mpl\n",
"import matplotlib.pyplot as plt\n",
"\n",
"from pymor.basic import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def H(s, mu):\n",
" tau = mu['tau'][0]\n",
" return np.array([[np.exp(-s) / (tau * s + 1)]])\n",
"\n",
"def dH(s, mu):\n",
" tau = mu['tau'][0]\n",
" return np.array([[-(tau * s + tau + 1) * np.exp(-s) / (tau * s + 1) ** 2]])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fom = TransferFunction(1, 1,\n",
" H, dH,\n",
" parameters={'tau': 1})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Magnitude plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mu_list_short = [0.01, 0.1, 1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"w = np.logspace(-2, 4, 100)\n",
"\n",
"fig, ax = plt.subplots()\n",
"for mu in mu_list_short:\n",
" fom.mag_plot(w, ax=ax, mu=mu, label=fr'$\\tau = {mu}$')\n",
"ax.legend()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"w_list = np.logspace(-2, 4, 100)\n",
"mu_list = np.logspace(-2, 0, 50)\n",
"\n",
"fom_w_mu = np.zeros((len(w_list), len(mu_list)))\n",
"for i, mu in enumerate(mu_list):\n",
" fom_w_mu[:, i] = spla.norm(fom.freq_resp(w_list, mu=mu), axis=(1, 2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"out = ax.contourf(w_list, mu_list, fom_w_mu.T,\n",
" norm=mpl.colors.LogNorm(),\n",
" levels=np.logspace(np.log10(fom_w_mu.min()), np.log10(fom_w_mu.max()), 100))\n",
"ax.set_xlabel(r'Frequency $\\omega$')\n",
"ax.set_ylabel(r'Parameter $\\mu$')\n",
"ax.set_xscale('log')\n",
"ax.set_yscale('log')\n",
"fig.colorbar(out, ticks=np.logspace(-4, 1, 6))\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# TF-IRKA"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r = 10\n",
"roms_tf_irka = []\n",
"for mu in mu_list_short:\n",
" tf_irka = TFIRKAReductor(fom, mu=mu)\n",
" rom = tf_irka.reduce(r, conv_crit='h2', maxit=1000, num_prev=5)\n",
" roms_tf_irka.append(rom)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"for mu, rom in zip(mu_list_short, roms_tf_irka):\n",
" poles = rom.poles()\n",
" ax.plot(poles.real, poles.imag, '.', label=fr'$\\tau = {mu}$')\n",
"ax.set_title(\"Poles of TF-IRKA's ROMs\")\n",
"ax.legend()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"for mu, rom in zip(mu_list_short, roms_tf_irka):\n",
" rom.mag_plot(w, ax=ax, label=fr'$\\tau = {mu}$')\n",
"ax.set_title(\"Magnitude plot of TF-IRKA's ROMs\")\n",
"ax.legend()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"for mu, rom in zip(mu_list_short, roms_tf_irka):\n",
" (fom - rom).mag_plot(w, ax=ax, mu=mu, label=fr'$\\tau = {mu}$')\n",
"ax.set_title(\"Magnitude plot of error systems\")\n",
"ax.legend()\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
This diff is collapsed.
This diff is collapsed.
{
"cells": [
{
"cell_type": "raw",
"metadata": {},
"source": [
"This file is part of the pyMOR project (http://www.pymor.org).\n",
"Copyright 2013-2020 pyMOR developers and contributors. All rights reserved.\n",
"License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import scipy.linalg as spla\n",
"import scipy.sparse as sps\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib as mpl\n",
"\n",
"from pymor.core.config import config\n",
"from pymor.models.iosys import LTIModel\n",
"from pymor.operators.numpy import NumpyMatrixOperator\n",
"from pymor.parameters.functionals import ProjectionParameterFunctional\n",
"from pymor.reductors.bt import BTReductor\n",
"from pymor.reductors.h2 import IRKAReductor"
]
},
{