xref: /libCEED/examples/python/tutorial-5-operator.ipynb (revision 2fc995f6102555f4a428ce850576f23ccfe8b680)
1edab6123Sjeremylt{
2edab6123Sjeremylt "cells": [
3edab6123Sjeremylt  {
4edab6123Sjeremylt   "cell_type": "markdown",
5edab6123Sjeremylt   "metadata": {},
6edab6123Sjeremylt   "source": [
7edab6123Sjeremylt    "# libCEED for Python examples\n",
8edab6123Sjeremylt    "\n",
9edab6123Sjeremylt    "This is a tutorial to illustrate the main feautures of the Python interface for [libCEED](https://github.com/CEED/libCEED/), the low-level API library for efficient high-order discretization methods developed by the co-design [Center for Efficient Exascale Discretizations](https://ceed.exascaleproject.org/) (CEED) of the [Exascale Computing Project](https://www.exascaleproject.org/) (ECP).\n",
10edab6123Sjeremylt    "\n",
1113964f07SJed Brown    "While libCEED's focus is on high-order finite/spectral element method implementations, the approach is mostly algebraic and thus applicable to other discretizations in factored form, as explained in the [user manual](https://libceed.org/)."
12edab6123Sjeremylt   ]
13edab6123Sjeremylt  },
14edab6123Sjeremylt  {
15edab6123Sjeremylt   "cell_type": "markdown",
16edab6123Sjeremylt   "metadata": {},
17edab6123Sjeremylt   "source": [
18edab6123Sjeremylt    "## Setting up libCEED for Python\n",
19edab6123Sjeremylt    "\n",
20edab6123Sjeremylt    "Install libCEED for Python by running"
21edab6123Sjeremylt   ]
22edab6123Sjeremylt  },
23edab6123Sjeremylt  {
24edab6123Sjeremylt   "cell_type": "code",
25edab6123Sjeremylt   "execution_count": null,
26edab6123Sjeremylt   "metadata": {},
27edab6123Sjeremylt   "outputs": [],
28edab6123Sjeremylt   "source": [
29edab6123Sjeremylt    "! python -m pip install libceed"
30edab6123Sjeremylt   ]
31edab6123Sjeremylt  },
32edab6123Sjeremylt  {
33edab6123Sjeremylt   "cell_type": "markdown",
34edab6123Sjeremylt   "metadata": {},
35edab6123Sjeremylt   "source": [
36edab6123Sjeremylt    "## CeedOperator\n",
37edab6123Sjeremylt    "\n",
3813964f07SJed Brown    "Here we show some basic examples to illustrate the `libceed.Operator` class. In libCEED, a `libceed.Operator` defines the finite/spectral element operator associated to a `libceed.QFunction` (see [the API documentation](https://libceed.org/en/latest/libCEEDapi.html#finite-element-operator-decomposition))."
39edab6123Sjeremylt   ]
40edab6123Sjeremylt  },
41edab6123Sjeremylt  {
42edab6123Sjeremylt   "cell_type": "markdown",
43edab6123Sjeremylt   "metadata": {},
44edab6123Sjeremylt   "source": [
45edab6123Sjeremylt    "* In the following example, we create and apply a CeedOperator for the mass matrix in 1D. By applying this operator to a vector of 1's, we compute the length of this 1D domain, similar to Ex1-Volume in the [tutorial-6-shell tutorial](./tutorial-6-shell.ipynb)"
46edab6123Sjeremylt   ]
47edab6123Sjeremylt  },
48edab6123Sjeremylt  {
49edab6123Sjeremylt   "cell_type": "code",
50edab6123Sjeremylt   "execution_count": null,
51edab6123Sjeremylt   "metadata": {},
52edab6123Sjeremylt   "outputs": [],
53edab6123Sjeremylt   "source": [
54edab6123Sjeremylt    "import libceed\n",
55edab6123Sjeremylt    "import numpy as np\n",
56edab6123Sjeremylt    "\n",
57edab6123Sjeremylt    "ceed = libceed.Ceed()\n",
58edab6123Sjeremylt    "\n",
59edab6123Sjeremylt    "nelem = 15\n",
60edab6123Sjeremylt    "p = 5\n",
61edab6123Sjeremylt    "q = 8\n",
62edab6123Sjeremylt    "nx = nelem + 1\n",
63edab6123Sjeremylt    "nu = nelem*(p-1) + 1\n",
64edab6123Sjeremylt    "\n",
65edab6123Sjeremylt    "# Vectors\n",
66edab6123Sjeremylt    "x = ceed.Vector(nx)\n",
67edab6123Sjeremylt    "x_array = np.zeros(nx)\n",
68edab6123Sjeremylt    "for i in range(nx):\n",
69edab6123Sjeremylt    "  x_array[i] = i / (nx - 1.0)\n",
70edab6123Sjeremylt    "x.set_array(x_array, cmode=libceed.USE_POINTER)\n",
71edab6123Sjeremylt    "\n",
72edab6123Sjeremylt    "qdata = ceed.Vector(nelem*q)\n",
73edab6123Sjeremylt    "u = ceed.Vector(nu)\n",
74edab6123Sjeremylt    "v = ceed.Vector(nu)\n",
75edab6123Sjeremylt    "\n",
76edab6123Sjeremylt    "# Restrictions\n",
77edab6123Sjeremylt    "indx = np.zeros(nx*2, dtype=\"int32\")\n",
78edab6123Sjeremylt    "for i in range(nx):\n",
79edab6123Sjeremylt    "  indx[2*i+0] = i\n",
80edab6123Sjeremylt    "  indx[2*i+1] = i+1\n",
81edab6123Sjeremylt    "rx = ceed.ElemRestriction(nelem, 2, 1, 1, nx, indx, cmode=libceed.USE_POINTER)\n",
82edab6123Sjeremylt    "\n",
83edab6123Sjeremylt    "indu = np.zeros(nelem*p, dtype=\"int32\")\n",
84edab6123Sjeremylt    "for i in range(nelem):\n",
85edab6123Sjeremylt    "  for j in range(p):\n",
86edab6123Sjeremylt    "    indu[p*i+j] = i*(p-1) + j\n",
87edab6123Sjeremylt    "ru = ceed.ElemRestriction(nelem, p, 1, 1, nu, indu, cmode=libceed.USE_POINTER)\n",
88edab6123Sjeremylt    "strides = np.array([1, q, q], dtype=\"int32\")\n",
89edab6123Sjeremylt    "rui = ceed.StridedElemRestriction(nelem, q, 1, q*nelem, strides)\n",
90edab6123Sjeremylt    "\n",
91edab6123Sjeremylt    "# Bases\n",
92edab6123Sjeremylt    "bx = ceed.BasisTensorH1Lagrange(1, 1, 2, q, libceed.GAUSS)\n",
93edab6123Sjeremylt    "bu = ceed.BasisTensorH1Lagrange(1, 1, p, q, libceed.GAUSS)\n",
94edab6123Sjeremylt    "\n",
95edab6123Sjeremylt    "# QFunctions\n",
96edab6123Sjeremylt    "qf_setup = ceed.QFunctionByName(\"Mass1DBuild\")\n",
97edab6123Sjeremylt    "qf_mass = ceed.QFunctionByName(\"MassApply\")\n",
98edab6123Sjeremylt    "\n",
99edab6123Sjeremylt    "# Setup operator\n",
100edab6123Sjeremylt    "op_setup = ceed.Operator(qf_setup)\n",
101edab6123Sjeremylt    "op_setup.set_field(\"dx\", rx, bx, libceed.VECTOR_ACTIVE)\n",
102edab6123Sjeremylt    "op_setup.set_field(\"weights\", libceed.ELEMRESTRICTION_NONE, bx,\n",
103edab6123Sjeremylt    "                   libceed.VECTOR_NONE)\n",
104a36217cbSJeremy L Thompson    "op_setup.set_field(\"qdata\", rui, libceed.BASIS_NONE,\n",
105edab6123Sjeremylt    "                   libceed.VECTOR_ACTIVE)\n",
10628d09c20SJeremy L Thompson    "op_setup.check()\n",
107edab6123Sjeremylt    "print('Setup operator: ', op_setup)\n",
108edab6123Sjeremylt    "\n",
109edab6123Sjeremylt    "# Mass operator\n",
110edab6123Sjeremylt    "op_mass = ceed.Operator(qf_mass)\n",
111edab6123Sjeremylt    "op_mass.set_field(\"u\", ru, bu, libceed.VECTOR_ACTIVE)\n",
112a36217cbSJeremy L Thompson    "op_mass.set_field(\"qdata\", rui, libceed.BASIS_NONE, qdata)\n",
113edab6123Sjeremylt    "op_mass.set_field(\"v\", ru, bu, libceed.VECTOR_ACTIVE)\n",
11428d09c20SJeremy L Thompson    "op_mass.check()\n",
115edab6123Sjeremylt    "print('Mass operator: ', op_mass)\n",
116edab6123Sjeremylt    "\n",
117edab6123Sjeremylt    "# Setup\n",
118edab6123Sjeremylt    "op_setup.apply(x, qdata)\n",
119edab6123Sjeremylt    "\n",
120edab6123Sjeremylt    "# Apply mass matrix\n",
121edab6123Sjeremylt    "u.set_value(1)\n",
122edab6123Sjeremylt    "op_mass.apply(u, v)\n",
123edab6123Sjeremylt    "\n",
124edab6123Sjeremylt    "# Check\n",
125edab6123Sjeremylt    "with v.array_read() as v_array:\n",
126edab6123Sjeremylt    "  print('The length of the domain is l = %4.2f'%np.sum(v_array))"
127edab6123Sjeremylt   ]
128*2fc995f6SJeremy L Thompson  },
129*2fc995f6SJeremy L Thompson  {
130*2fc995f6SJeremy L Thompson   "cell_type": "markdown",
131*2fc995f6SJeremy L Thompson   "metadata": {},
132*2fc995f6SJeremy L Thompson   "source": [
133*2fc995f6SJeremy L Thompson    "* In the next example, we create and apply a CeedOperator for the Poisson operator in 1D. By applying this operator to a vector with a linear function, we compute the 'surface area' of this 1D domain, similar to Ex2-Surface in the [tutorial-6-shell tutorial](./tutorial-6-shell.ipynb)"
134*2fc995f6SJeremy L Thompson   ]
135*2fc995f6SJeremy L Thompson  },
136*2fc995f6SJeremy L Thompson  {
137*2fc995f6SJeremy L Thompson   "cell_type": "code",
138*2fc995f6SJeremy L Thompson   "execution_count": null,
139*2fc995f6SJeremy L Thompson   "metadata": {},
140*2fc995f6SJeremy L Thompson   "outputs": [],
141*2fc995f6SJeremy L Thompson   "source": [
142*2fc995f6SJeremy L Thompson    "import libceed\n",
143*2fc995f6SJeremy L Thompson    "import numpy as np\n",
144*2fc995f6SJeremy L Thompson    "\n",
145*2fc995f6SJeremy L Thompson    "ceed = libceed.Ceed()\n",
146*2fc995f6SJeremy L Thompson    "\n",
147*2fc995f6SJeremy L Thompson    "nelem = 15\n",
148*2fc995f6SJeremy L Thompson    "p = 5\n",
149*2fc995f6SJeremy L Thompson    "q = 8\n",
150*2fc995f6SJeremy L Thompson    "nx = nelem + 1\n",
151*2fc995f6SJeremy L Thompson    "nu = nelem*(p-1) + 1\n",
152*2fc995f6SJeremy L Thompson    "\n",
153*2fc995f6SJeremy L Thompson    "# Vectors\n",
154*2fc995f6SJeremy L Thompson    "x = ceed.Vector(nx)\n",
155*2fc995f6SJeremy L Thompson    "x_array = np.zeros(nx)\n",
156*2fc995f6SJeremy L Thompson    "for i in range(nx):\n",
157*2fc995f6SJeremy L Thompson    "  x_array[i] = i / (nx - 1.0)\n",
158*2fc995f6SJeremy L Thompson    "x.set_array(x_array, cmode=libceed.USE_POINTER)\n",
159*2fc995f6SJeremy L Thompson    "\n",
160*2fc995f6SJeremy L Thompson    "qdata = ceed.Vector(nelem*q)\n",
161*2fc995f6SJeremy L Thompson    "u = ceed.Vector(nu)\n",
162*2fc995f6SJeremy L Thompson    "v = ceed.Vector(nu)\n",
163*2fc995f6SJeremy L Thompson    "\n",
164*2fc995f6SJeremy L Thompson    "# Restrictions\n",
165*2fc995f6SJeremy L Thompson    "indx = np.zeros(nx*2, dtype=\"int32\")\n",
166*2fc995f6SJeremy L Thompson    "for i in range(nx):\n",
167*2fc995f6SJeremy L Thompson    "  indx[2*i+0] = i\n",
168*2fc995f6SJeremy L Thompson    "  indx[2*i+1] = i+1\n",
169*2fc995f6SJeremy L Thompson    "rx = ceed.ElemRestriction(nelem, 2, 1, 1, nx, indx, cmode=libceed.USE_POINTER)\n",
170*2fc995f6SJeremy L Thompson    "\n",
171*2fc995f6SJeremy L Thompson    "indu = np.zeros(nelem*p, dtype=\"int32\")\n",
172*2fc995f6SJeremy L Thompson    "for i in range(nelem):\n",
173*2fc995f6SJeremy L Thompson    "  for j in range(p):\n",
174*2fc995f6SJeremy L Thompson    "    indu[p*i+j] = i*(p-1) + j\n",
175*2fc995f6SJeremy L Thompson    "ru = ceed.ElemRestriction(nelem, p, 1, 1, nu, indu, cmode=libceed.USE_POINTER)\n",
176*2fc995f6SJeremy L Thompson    "strides = np.array([1, q, q], dtype=\"int32\")\n",
177*2fc995f6SJeremy L Thompson    "rui = ceed.StridedElemRestriction(nelem, q, 1, q*nelem, strides)\n",
178*2fc995f6SJeremy L Thompson    "\n",
179*2fc995f6SJeremy L Thompson    "# Bases\n",
180*2fc995f6SJeremy L Thompson    "bx = ceed.BasisTensorH1Lagrange(1, 1, 2, q, libceed.GAUSS)\n",
181*2fc995f6SJeremy L Thompson    "bu = ceed.BasisTensorH1Lagrange(1, 1, p, q, libceed.GAUSS)\n",
182*2fc995f6SJeremy L Thompson    "\n",
183*2fc995f6SJeremy L Thompson    "# QFunctions\n",
184*2fc995f6SJeremy L Thompson    "qf_setup = ceed.QFunctionByName(\"Poisson1DBuild\")\n",
185*2fc995f6SJeremy L Thompson    "qf_mass = ceed.QFunctionByName(\"Poisson1DApply\")\n",
186*2fc995f6SJeremy L Thompson    "\n",
187*2fc995f6SJeremy L Thompson    "# Setup operator\n",
188*2fc995f6SJeremy L Thompson    "op_setup = ceed.Operator(qf_setup)\n",
189*2fc995f6SJeremy L Thompson    "op_setup.set_field(\"dx\", rx, bx, libceed.VECTOR_ACTIVE)\n",
190*2fc995f6SJeremy L Thompson    "op_setup.set_field(\"weights\", libceed.ELEMRESTRICTION_NONE, bx,\n",
191*2fc995f6SJeremy L Thompson    "                   libceed.VECTOR_NONE)\n",
192*2fc995f6SJeremy L Thompson    "op_setup.set_field(\"qdata\", rui, libceed.BASIS_NONE,\n",
193*2fc995f6SJeremy L Thompson    "                   libceed.VECTOR_ACTIVE)\n",
194*2fc995f6SJeremy L Thompson    "op_setup.check()\n",
195*2fc995f6SJeremy L Thompson    "print('Setup operator: ', op_setup)\n",
196*2fc995f6SJeremy L Thompson    "\n",
197*2fc995f6SJeremy L Thompson    "# Poisson operator\n",
198*2fc995f6SJeremy L Thompson    "op_poisson = ceed.Operator(qf_mass)\n",
199*2fc995f6SJeremy L Thompson    "op_poisson.set_field(\"du\", ru, bu, libceed.VECTOR_ACTIVE)\n",
200*2fc995f6SJeremy L Thompson    "op_poisson.set_field(\"qdata\", rui, libceed.BASIS_NONE, qdata)\n",
201*2fc995f6SJeremy L Thompson    "op_poisson.set_field(\"dv\", ru, bu, libceed.VECTOR_ACTIVE)\n",
202*2fc995f6SJeremy L Thompson    "op_poisson.check()\n",
203*2fc995f6SJeremy L Thompson    "print('Poisson operator: ', op_poisson)\n",
204*2fc995f6SJeremy L Thompson    "\n",
205*2fc995f6SJeremy L Thompson    "# Setup\n",
206*2fc995f6SJeremy L Thompson    "op_setup.apply(x, qdata)\n",
207*2fc995f6SJeremy L Thompson    "\n",
208*2fc995f6SJeremy L Thompson    "# Apply Poisson operator\n",
209*2fc995f6SJeremy L Thompson    "with u.array_write() as u_array:\n",
210*2fc995f6SJeremy L Thompson    "  [points, _] = ceed.lobatto_quadrature(p)\n",
211*2fc995f6SJeremy L Thompson    "  for elem in range(nelem):\n",
212*2fc995f6SJeremy L Thompson    "      for point in range(p):\n",
213*2fc995f6SJeremy L Thompson    "          u_array[elem * (p - 1) + point] = (1.0 + 2.0 * elem + points[point])/(2.0 * nelem)\n",
214*2fc995f6SJeremy L Thompson    "op_poisson.apply(u, v)\n",
215*2fc995f6SJeremy L Thompson    "\n",
216*2fc995f6SJeremy L Thompson    "# Check\n",
217*2fc995f6SJeremy L Thompson    "with v.array_read() as v_array:\n",
218*2fc995f6SJeremy L Thompson    "  print('The surface area of the domain is dl = %4.2f'%np.sum(abs(v_array)))"
219*2fc995f6SJeremy L Thompson   ]
220edab6123Sjeremylt  }
221edab6123Sjeremylt ],
222edab6123Sjeremylt "metadata": {
223edab6123Sjeremylt  "kernelspec": {
224*2fc995f6SJeremy L Thompson   "display_name": "Python 3 (ipykernel)",
225edab6123Sjeremylt   "language": "python",
226edab6123Sjeremylt   "name": "python3"
227edab6123Sjeremylt  },
228edab6123Sjeremylt  "language_info": {
229edab6123Sjeremylt   "codemirror_mode": {
230edab6123Sjeremylt    "name": "ipython",
231edab6123Sjeremylt    "version": 3
232edab6123Sjeremylt   },
233edab6123Sjeremylt   "file_extension": ".py",
234edab6123Sjeremylt   "mimetype": "text/x-python",
235edab6123Sjeremylt   "name": "python",
236edab6123Sjeremylt   "nbconvert_exporter": "python",
237edab6123Sjeremylt   "pygments_lexer": "ipython3",
238*2fc995f6SJeremy L Thompson   "version": "3.13.2"
239edab6123Sjeremylt  }
240edab6123Sjeremylt },
241edab6123Sjeremylt "nbformat": 4,
242edab6123Sjeremylt "nbformat_minor": 4
243edab6123Sjeremylt}
244