{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Laplace 2D \n",
"\n",
"This example shows how to declare a bilinear form using sympde, then create and evaluate a GLT symbol.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We first start by importing what is needed from sympde and gelato:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# imports from sympde, to write bilinear forms\n",
"from sympde.core import Constant\n",
"from sympde.calculus import grad, dot\n",
"from sympde.topology import ScalarFunctionSpace\n",
"from sympde.topology import Domain\n",
"from sympde.topology import elements_of\n",
"from sympde.expr import BilinearForm\n",
"from sympde.expr import integral"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# imports from gelato\n",
"from gelato import gelatize, GltExpr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A domain is created as follows"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"domain = Domain('Omega', dim=2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then we declare a space of function over our domain"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"V = ScalarFunctionSpace('V', domain)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and define dummy test functions living in the space $V$"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"u,v = elements_of(V, names='u,v')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finaly, we declare a blinear form, as a lambda expression"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# declaring a constant from sympde\n",
"c = Constant('c')\n",
"\n",
"expr = dot(grad(v), grad(u)) + c*v*u\n",
"a = BilinearForm((u,v), integral(domain, expr))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can create the associated GLT expression to the bilinear form $a$"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"glt = GltExpr(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following instruction inspects what is a GltExpr: it is a lambda expression, that has two tuples as inputs: fourier space variables denoted by $(t_x, t_y)$ and no space variables in this case."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"GltExpr([tx, ty], [], BilinearForm(((u,), (v,)), DomainIntegral(Dot(Grad(u), Grad(v)), Omega) + DomainIntegral(c*u*v, Omega)))\n"
]
}
],
"source": [
"print(glt)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use a partial evaluation of the GLT expression, by providing the spline degrees"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"c*(13*cos(tx)/30 + cos(2*tx)/60 + 11/20)*(13*cos(ty)/30 + cos(2*ty)/60 + 11/20)/(nx*ny) + nx*(-2*cos(tx)/3 - cos(2*tx)/3 + 1)*(13*cos(ty)/30 + cos(2*ty)/60 + 11/20)/ny + ny*(13*cos(tx)/30 + cos(2*tx)/60 + 11/20)*(-2*cos(ty)/3 - cos(2*ty)/3 + 1)/nx\n"
]
}
],
"source": [
"print(glt(degrees=[2,2]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or numerically, evaluate it, although we are relaying on sympy to perform the evaluation, which is not the right way to proceed. One may use the *lambdify* function from sympy, or rely on *PsyDac* to generate automaticaly the discrete GltExpr, when having more complicated expressions (involving a mpping or fields)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.00385771212059162*c + 0.0493788050561308\n"
]
}
],
"source": [
"print(glt(tx=0.1, ty=0.2, degrees=[2,2], n_elements=[16,16]))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# css style\n",
"from IPython.core.display import HTML\n",
"def css_styling():\n",
" styles = open(\"../styles/custom.css\", \"r\").read()\n",
" return HTML(styles)\n",
"css_styling()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}