From 25e11a9862011b0a3da3861671d280f52683a9c1 Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Mon, 7 May 2018 12:23:10 -0500 Subject: [PATCH] Symbolic reduction notebook --- .../translations/PDE-reduction-symbolic.ipynb | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 contrib/translations/PDE-reduction-symbolic.ipynb diff --git a/contrib/translations/PDE-reduction-symbolic.ipynb b/contrib/translations/PDE-reduction-symbolic.ipynb new file mode 100644 index 00000000..bffcdf9e --- /dev/null +++ b/contrib/translations/PDE-reduction-symbolic.ipynb @@ -0,0 +1,133 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pyopencl as cl\n", + "import sumpy.toys as t\n", + "import numpy as np\n", + "import numpy.linalg as la\n", + "import matplotlib.pyplot as plt\n", + "from sumpy.visualization import FieldPlotter\n", + "from pytools import add_tuples\n", + "\n", + "from sumpy.expansion.local import VolumeTaylorLocalExpansion\n", + "from sumpy.expansion.multipole import (VolumeTaylorMultipoleExpansion,\n", + " LaplaceConformingVolumeTaylorMultipoleExpansion,\n", + " HelmholtzConformingVolumeTaylorMultipoleExpansion)\n", + " \n", + "from sumpy.kernel import (YukawaKernel, HelmholtzKernel, LaplaceKernel)\n", + "\n", + "from sumpy.symbolic import make_sym_vector\n", + "\n", + "order = 2\n", + "dim = 2\n", + "\n", + "if 0:\n", + " knl = LaplaceKernel(dim)\n", + " extra_kernel_kwargs = {}\n", + " mpole_expn_reduced_class = LaplaceConformingVolumeTaylorMultipoleExpansion\n", + " \n", + "else:\n", + " helm_k = 1.2\n", + " knl = HelmholtzKernel(dim)\n", + " extra_kernel_kwargs={\"k\": helm_k}\n", + " mpole_expn_reduced_class = HelmholtzConformingVolumeTaylorMultipoleExpansion\n", + "\n", + "mpole_expn_reduced = mpole_expn_reduced_class(knl, order)\n", + "mpole_expn = VolumeTaylorMultipoleExpansion(knl, order)\n", + "local_expn = VolumeTaylorLocalExpansion(knl, order)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from pytools import factorial\n", + "\n", + "def mi_factorial(n):\n", + " return np.prod([factorial(n1) for n1 in n])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "reduced_wrangler = mpole_expn_reduced.derivative_wrangler\n", + "full_wrangler = mpole_expn.derivative_wrangler\n", + "\n", + "reduced_derivatives = list(make_sym_vector(\"deriv\", len(reduced_wrangler.stored_identifiers)))\n", + "full_derivatives = reduced_wrangler.get_full_kernel_derivatives_from_stored(reduced_derivatives, 1)\n", + "\n", + "print(reduced_derivatives)\n", + "print(full_derivatives)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "full_coeffs = list(make_sym_vector(\"coeff\", len(reduced_wrangler.get_full_coefficient_identifiers())))\n", + "\n", + "reduced_coeffs = reduced_wrangler.get_stored_mpole_coefficients_from_full(full_mpole_coefficients=full_coeffs, rscale=1)\n", + "\n", + "print(full_coeffs)\n", + "print(reduced_coeffs)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dvec = make_sym_vector(\"d\", dim)\n", + "translated_reduce_coeffs = mpole_expn_reduced.translate_from(mpole_expn_reduced, reduced_coeffs, 1, dvec, 1)\n", + "translated_full_coeffs = mpole_expn.translate_from(mpole_expn, full_coeffs, 1, dvec, 1)\n", + "translated_full_coeffs" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "eval_reduced = sum(a*b for a, b in zip(translated_reduce_coeffs, reduced_derivatives))\n", + "eval_full = sum(a*b for a, b in zip(translated_full_coeffs, full_derivatives))\n", + "\n", + "(eval_full-eval_reduced).simplify()" + ] + } + ], + "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.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} -- GitLab