xref: /libCEED/examples/python/tutorial-6-shell.ipynb (revision edab612303ce5c583510db4ec9520fcda426d3c1)
1*edab6123Sjeremylt{
2*edab6123Sjeremylt "cells": [
3*edab6123Sjeremylt  {
4*edab6123Sjeremylt   "cell_type": "markdown",
5*edab6123Sjeremylt   "metadata": {},
6*edab6123Sjeremylt   "source": [
7*edab6123Sjeremylt    "# Standalone libCEED examples\n",
8*edab6123Sjeremylt    "\n",
9*edab6123Sjeremylt    "This is a tutorial 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",
10*edab6123Sjeremylt    "\n",
11*edab6123Sjeremylt    "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.readthedocs.io/)."
12*edab6123Sjeremylt   ]
13*edab6123Sjeremylt  },
14*edab6123Sjeremylt  {
15*edab6123Sjeremylt   "cell_type": "markdown",
16*edab6123Sjeremylt   "metadata": {},
17*edab6123Sjeremylt   "source": [
18*edab6123Sjeremylt    "## Common notation\n",
19*edab6123Sjeremylt    "\n",
20*edab6123Sjeremylt    "For most of our examples, the spatial discretization\n",
21*edab6123Sjeremylt    "uses high-order finite elements/spectral elements, namely, the high-order Lagrange\n",
22*edab6123Sjeremylt    "polynomials defined over $P$ non-uniformly spaced nodes, the\n",
23*edab6123Sjeremylt    "Gauss-Legendre-Lobatto (GLL) points, and quadrature points $\\{q_i\\}_{i=1}^Q$, with\n",
24*edab6123Sjeremylt    "corresponding weights $\\{w_i\\}_{i=1}^Q$ (typically the ones given by Gauss\n",
25*edab6123Sjeremylt    "or Gauss-Lobatto quadratures, that are built in the library).\n",
26*edab6123Sjeremylt    "\n",
27*edab6123Sjeremylt    "We discretize the domain, $\\Omega \\subset \\mathbb{R}^d$ (with $d=1,2,3$,\n",
28*edab6123Sjeremylt    "typically) by letting $\\Omega = \\bigcup_{e=1}^{N_e}\\Omega_e$, with $N_e$\n",
29*edab6123Sjeremylt    "disjoint elements. For most examples we use unstructured meshes for which the elements\n",
30*edab6123Sjeremylt    "are hexahedra (although this is not a requirement in libCEED).\n",
31*edab6123Sjeremylt    "\n",
32*edab6123Sjeremylt    "The physical coordinates are denoted by $\\mathbf{x}=(x,y,z)\\in\\Omega_e$,\n",
33*edab6123Sjeremylt    "while the reference coordinates are represented as\n",
34*edab6123Sjeremylt    "$\\boldsymbol{X}=(X,Y,Z) \\equiv (X_1,X_2,X_3) \\in I=[-1,1]^3$\n",
35*edab6123Sjeremylt    "(for $d=3$).\n",
36*edab6123Sjeremylt    "\n",
37*edab6123Sjeremylt    "### Ex1-Volume\n",
38*edab6123Sjeremylt    "\n",
39*edab6123Sjeremylt    "#### Mathematical formulation\n",
40*edab6123Sjeremylt    "\n",
41*edab6123Sjeremylt    "This example is located in the subdirectory `examples/ceed`. It illustrates a\n",
42*edab6123Sjeremylt    "simple usage of libCEED to compute the volume of a given body using a matrix-free\n",
43*edab6123Sjeremylt    "application of the mass operator. Arbitrary mesh and solution orders in 1D, 2D and 3D\n",
44*edab6123Sjeremylt    "are supported from the same code.\n",
45*edab6123Sjeremylt    "\n",
46*edab6123Sjeremylt    "This example shows how to compute line/surface/volume integrals of a 1D, 2D, or 3D\n",
47*edab6123Sjeremylt    "domain $\\Omega$ respectively, by applying the mass operator to a vector of\n",
48*edab6123Sjeremylt    "$1$s. It computes:\n",
49*edab6123Sjeremylt    "\n",
50*edab6123Sjeremylt    "$$\n",
51*edab6123Sjeremylt    "   I = \\int_{\\Omega} 1 \\, dV .\n",
52*edab6123Sjeremylt    "$$\n",
53*edab6123Sjeremylt    "\n",
54*edab6123Sjeremylt    "We write here the vector $u(x)\\equiv 1$ in the Galerkin approximation,\n",
55*edab6123Sjeremylt    "and find the volume of $\\Omega$ as\n",
56*edab6123Sjeremylt    "\n",
57*edab6123Sjeremylt    "$$\n",
58*edab6123Sjeremylt    "   \\sum_e \\int_{\\Omega_e} v(x) 1 \\, dV\n",
59*edab6123Sjeremylt    "$$\n",
60*edab6123Sjeremylt    "\n",
61*edab6123Sjeremylt    "with $v(x) \\in \\mathcal{V}_p = \\{ v \\in H^{1}(\\Omega_e) \\,|\\, v \\in P_p(\\boldsymbol{I}), e=1,\\ldots,N_e \\}$,\n",
62*edab6123Sjeremylt    "the test functions.\n"
63*edab6123Sjeremylt   ]
64*edab6123Sjeremylt  },
65*edab6123Sjeremylt  {
66*edab6123Sjeremylt   "cell_type": "markdown",
67*edab6123Sjeremylt   "metadata": {},
68*edab6123Sjeremylt   "source": [
69*edab6123Sjeremylt    "#### Executing the example\n",
70*edab6123Sjeremylt    "\n",
71*edab6123Sjeremylt    "Clone or download libCEED by running"
72*edab6123Sjeremylt   ]
73*edab6123Sjeremylt  },
74*edab6123Sjeremylt  {
75*edab6123Sjeremylt   "cell_type": "code",
76*edab6123Sjeremylt   "execution_count": null,
77*edab6123Sjeremylt   "metadata": {},
78*edab6123Sjeremylt   "outputs": [],
79*edab6123Sjeremylt   "source": [
80*edab6123Sjeremylt    "! git clone https://github.com/CEED/libCEED.git"
81*edab6123Sjeremylt   ]
82*edab6123Sjeremylt  },
83*edab6123Sjeremylt  {
84*edab6123Sjeremylt   "cell_type": "markdown",
85*edab6123Sjeremylt   "metadata": {},
86*edab6123Sjeremylt   "source": [
87*edab6123Sjeremylt    "Then move to the libCEED folder"
88*edab6123Sjeremylt   ]
89*edab6123Sjeremylt  },
90*edab6123Sjeremylt  {
91*edab6123Sjeremylt   "cell_type": "code",
92*edab6123Sjeremylt   "execution_count": null,
93*edab6123Sjeremylt   "metadata": {},
94*edab6123Sjeremylt   "outputs": [],
95*edab6123Sjeremylt   "source": [
96*edab6123Sjeremylt    "cd libCEED"
97*edab6123Sjeremylt   ]
98*edab6123Sjeremylt  },
99*edab6123Sjeremylt  {
100*edab6123Sjeremylt   "cell_type": "markdown",
101*edab6123Sjeremylt   "metadata": {},
102*edab6123Sjeremylt   "source": [
103*edab6123Sjeremylt    "And compile the library by running"
104*edab6123Sjeremylt   ]
105*edab6123Sjeremylt  },
106*edab6123Sjeremylt  {
107*edab6123Sjeremylt   "cell_type": "code",
108*edab6123Sjeremylt   "execution_count": null,
109*edab6123Sjeremylt   "metadata": {},
110*edab6123Sjeremylt   "outputs": [],
111*edab6123Sjeremylt   "source": [
112*edab6123Sjeremylt    "! make"
113*edab6123Sjeremylt   ]
114*edab6123Sjeremylt  },
115*edab6123Sjeremylt  {
116*edab6123Sjeremylt   "cell_type": "markdown",
117*edab6123Sjeremylt   "metadata": {},
118*edab6123Sjeremylt   "source": [
119*edab6123Sjeremylt    "Move to the examples folder "
120*edab6123Sjeremylt   ]
121*edab6123Sjeremylt  },
122*edab6123Sjeremylt  {
123*edab6123Sjeremylt   "cell_type": "code",
124*edab6123Sjeremylt   "execution_count": null,
125*edab6123Sjeremylt   "metadata": {},
126*edab6123Sjeremylt   "outputs": [],
127*edab6123Sjeremylt   "source": [
128*edab6123Sjeremylt    "cd examples/"
129*edab6123Sjeremylt   ]
130*edab6123Sjeremylt  },
131*edab6123Sjeremylt  {
132*edab6123Sjeremylt   "cell_type": "markdown",
133*edab6123Sjeremylt   "metadata": {},
134*edab6123Sjeremylt   "source": [
135*edab6123Sjeremylt    "Then move to the standalone libCEED's examples folder"
136*edab6123Sjeremylt   ]
137*edab6123Sjeremylt  },
138*edab6123Sjeremylt  {
139*edab6123Sjeremylt   "cell_type": "code",
140*edab6123Sjeremylt   "execution_count": null,
141*edab6123Sjeremylt   "metadata": {},
142*edab6123Sjeremylt   "outputs": [],
143*edab6123Sjeremylt   "source": [
144*edab6123Sjeremylt    "cd ceed/"
145*edab6123Sjeremylt   ]
146*edab6123Sjeremylt  },
147*edab6123Sjeremylt  {
148*edab6123Sjeremylt   "cell_type": "markdown",
149*edab6123Sjeremylt   "metadata": {},
150*edab6123Sjeremylt   "source": [
151*edab6123Sjeremylt    "And compile the examples by running"
152*edab6123Sjeremylt   ]
153*edab6123Sjeremylt  },
154*edab6123Sjeremylt  {
155*edab6123Sjeremylt   "cell_type": "code",
156*edab6123Sjeremylt   "execution_count": null,
157*edab6123Sjeremylt   "metadata": {},
158*edab6123Sjeremylt   "outputs": [],
159*edab6123Sjeremylt   "source": [
160*edab6123Sjeremylt    "! make"
161*edab6123Sjeremylt   ]
162*edab6123Sjeremylt  },
163*edab6123Sjeremylt  {
164*edab6123Sjeremylt   "cell_type": "markdown",
165*edab6123Sjeremylt   "metadata": {},
166*edab6123Sjeremylt   "source": [
167*edab6123Sjeremylt    "Now run `ex1-volume` by running"
168*edab6123Sjeremylt   ]
169*edab6123Sjeremylt  },
170*edab6123Sjeremylt  {
171*edab6123Sjeremylt   "cell_type": "code",
172*edab6123Sjeremylt   "execution_count": 1,
173*edab6123Sjeremylt   "metadata": {},
174*edab6123Sjeremylt   "outputs": [
175*edab6123Sjeremylt    {
176*edab6123Sjeremylt     "name": "stdout",
177*edab6123Sjeremylt     "output_type": "stream",
178*edab6123Sjeremylt     "text": [
179*edab6123Sjeremylt      "Selected options: [command line option] : <current value>\n",
180*edab6123Sjeremylt      "  Ceed specification [-c] : /cpu/self\n",
181*edab6123Sjeremylt      "  Mesh dimension     [-d] : 3\n",
182*edab6123Sjeremylt      "  Mesh order         [-m] : 4\n",
183*edab6123Sjeremylt      "  Solution order     [-o] : 4\n",
184*edab6123Sjeremylt      "  Num. 1D quadr. pts [-q] : 6\n",
185*edab6123Sjeremylt      "  Approx. # unknowns [-s] : 262144\n",
186*edab6123Sjeremylt      "  QFunction source   [-g] : gallery\n",
187*edab6123Sjeremylt      "\n",
188*edab6123Sjeremylt      "Mesh size: nx = 16, ny = 16, nz = 16\n",
189*edab6123Sjeremylt      "Number of mesh nodes     : 274625\n",
190*edab6123Sjeremylt      "Number of solution nodes : 274625\n",
191*edab6123Sjeremylt      "Computing the quadrature data for the mass operator ... done.\n",
192*edab6123Sjeremylt      "Computing the mesh volume using the formula: vol = 1^T.M.1 ... done.\n",
193*edab6123Sjeremylt      "Exact mesh volume    :  2.3561944901923\n",
194*edab6123Sjeremylt      "Computed mesh volume :  2.3561944901921\n",
195*edab6123Sjeremylt      "Volume error         : -2.7444713168734e-13\n"
196*edab6123Sjeremylt     ]
197*edab6123Sjeremylt    }
198*edab6123Sjeremylt   ],
199*edab6123Sjeremylt   "source": [
200*edab6123Sjeremylt    "! ./ex1-volume -d 3 -g"
201*edab6123Sjeremylt   ]
202*edab6123Sjeremylt  },
203*edab6123Sjeremylt  {
204*edab6123Sjeremylt   "cell_type": "markdown",
205*edab6123Sjeremylt   "metadata": {},
206*edab6123Sjeremylt   "source": [
207*edab6123Sjeremylt    "This example shows how to compute line/surface/volume integrals of a 1D, 2D, or 3D domain Ω respectively, by applying the mass operator to a vector of 1s. The command line option `-d` specifies the dimensionality of the domain Ω. The option `-g` specifies that the mass operator is, in this case, selected from a gallery of available built-in operators in the library."
208*edab6123Sjeremylt   ]
209*edab6123Sjeremylt  },
210*edab6123Sjeremylt  {
211*edab6123Sjeremylt   "cell_type": "markdown",
212*edab6123Sjeremylt   "metadata": {},
213*edab6123Sjeremylt   "source": [
214*edab6123Sjeremylt    "### Ex2-Surface\n",
215*edab6123Sjeremylt    "\n",
216*edab6123Sjeremylt    "#### Mathematical formulation\n",
217*edab6123Sjeremylt    "\n",
218*edab6123Sjeremylt    "This example is located in the subdirectory `examples/ceed`. It computes the\n",
219*edab6123Sjeremylt    "surface area of a given body using matrix-free application of a diffusion operator.\n",
220*edab6123Sjeremylt    "Arbitrary mesh and solution orders in 1D, 2D and 3D are supported from the same code.\n",
221*edab6123Sjeremylt    "\n",
222*edab6123Sjeremylt    "Similarly to the Ex1-Volume example, it computes:\n",
223*edab6123Sjeremylt    "\n",
224*edab6123Sjeremylt    "\\begin{equation}\\label{eq-ex2-surface}\\tag{eq. 1}\n",
225*edab6123Sjeremylt    "   I =  \\int_{\\partial \\Omega} 1 \\, dS ,\n",
226*edab6123Sjeremylt    "\\end{equation}\n",
227*edab6123Sjeremylt    "\n",
228*edab6123Sjeremylt    "by applying the divergence theorem.\n",
229*edab6123Sjeremylt    "In particular, we select $u(\\mathbf x) = x_0 + x_1 + x_2$, for which $\\nabla u = [1, 1, 1]^T$, and thus $\\nabla u \\cdot \\hat{\\mathbf n} = 1$.\n",
230*edab6123Sjeremylt    "\n",
231*edab6123Sjeremylt    "Given Laplace's equation,\n",
232*edab6123Sjeremylt    "\n",
233*edab6123Sjeremylt    "$$\n",
234*edab6123Sjeremylt    "   \\nabla \\cdot \\nabla u = 0, \\textrm{ for  } \\mathbf{x} \\in \\Omega ,\n",
235*edab6123Sjeremylt    "$$\n",
236*edab6123Sjeremylt    "\n",
237*edab6123Sjeremylt    "let us multiply by a test function $v$ and integrate by parts to obtain\n",
238*edab6123Sjeremylt    "\n",
239*edab6123Sjeremylt    "$$\n",
240*edab6123Sjeremylt    "   \\int_\\Omega \\nabla v \\cdot \\nabla u \\, dV - \\int_{\\partial \\Omega} v \\nabla u \\cdot \\hat{\\mathbf n}\\, dS = 0 .\n",
241*edab6123Sjeremylt    "$$\n",
242*edab6123Sjeremylt    "\n",
243*edab6123Sjeremylt    "Since we have chosen $u$ such that $\\nabla u \\cdot \\hat{\\mathbf n} = 1$, the boundary integrand is $v 1 \\equiv v$. Hence, similar to the previous example, we can evaluate the surface integral by applying the volumetric Laplacian as follows\n",
244*edab6123Sjeremylt    "\n",
245*edab6123Sjeremylt    "$$\n",
246*edab6123Sjeremylt    "   \\int_\\Omega \\nabla v \\cdot \\nabla u \\, dV \\approx \\sum_e \\int_{\\partial \\Omega_e} v(x) 1 \\, dS .\n",
247*edab6123Sjeremylt    "$$"
248*edab6123Sjeremylt   ]
249*edab6123Sjeremylt  },
250*edab6123Sjeremylt  {
251*edab6123Sjeremylt   "cell_type": "markdown",
252*edab6123Sjeremylt   "metadata": {},
253*edab6123Sjeremylt   "source": [
254*edab6123Sjeremylt    "### Executing the example\n",
255*edab6123Sjeremylt    "\n",
256*edab6123Sjeremylt    "Assuming the steps above, you should be in the `examples/ceed/` subdirectory and have already compiled the example.\n",
257*edab6123Sjeremylt    "\n",
258*edab6123Sjeremylt    "Now run `ex2-surface` by running "
259*edab6123Sjeremylt   ]
260*edab6123Sjeremylt  },
261*edab6123Sjeremylt  {
262*edab6123Sjeremylt   "cell_type": "code",
263*edab6123Sjeremylt   "execution_count": 2,
264*edab6123Sjeremylt   "metadata": {},
265*edab6123Sjeremylt   "outputs": [
266*edab6123Sjeremylt    {
267*edab6123Sjeremylt     "name": "stdout",
268*edab6123Sjeremylt     "output_type": "stream",
269*edab6123Sjeremylt     "text": [
270*edab6123Sjeremylt      "Selected options: [command line option] : <current value>\n",
271*edab6123Sjeremylt      "  Ceed specification [-c] : /cpu/self\n",
272*edab6123Sjeremylt      "  Mesh dimension     [-d] : 3\n",
273*edab6123Sjeremylt      "  Mesh order         [-m] : 4\n",
274*edab6123Sjeremylt      "  Solution order     [-o] : 4\n",
275*edab6123Sjeremylt      "  Num. 1D quadr. pts [-q] : 6\n",
276*edab6123Sjeremylt      "  Approx. # unknowns [-s] : 262144\n",
277*edab6123Sjeremylt      "  QFunction source   [-g] : gallery\n",
278*edab6123Sjeremylt      "\n",
279*edab6123Sjeremylt      "Mesh size: nx = 16, ny = 16, nz = 16\n",
280*edab6123Sjeremylt      "Number of mesh nodes     : 274625\n",
281*edab6123Sjeremylt      "Number of solution nodes : 274625\n",
282*edab6123Sjeremylt      "Computing the quadrature data for the diffusion operator ... done.\n",
283*edab6123Sjeremylt      "Computing the mesh surface area using the formula: sa = 1^T.|K.x| ... done.\n",
284*edab6123Sjeremylt      "Exact mesh surface area    :  6\n",
285*edab6123Sjeremylt      "Computed mesh surface area :  5.9773703490853\n",
286*edab6123Sjeremylt      "Surface area error         : -0.022629650914673\n"
287*edab6123Sjeremylt     ]
288*edab6123Sjeremylt    }
289*edab6123Sjeremylt   ],
290*edab6123Sjeremylt   "source": [
291*edab6123Sjeremylt    "! ./ex2-surface -d 3 -g"
292*edab6123Sjeremylt   ]
293*edab6123Sjeremylt  },
294*edab6123Sjeremylt  {
295*edab6123Sjeremylt   "cell_type": "markdown",
296*edab6123Sjeremylt   "metadata": {},
297*edab6123Sjeremylt   "source": [
298*edab6123Sjeremylt    "This example computes the surface area of a given body using matrix-free application of a Laplace's (diffusion) operator. The command line option `-d` specifies the dimensionality of the domain Ω. The option `-g` specifies that the Laplace's operator is, in this case, selected from a gallery of available built-in operators in the library."
299*edab6123Sjeremylt   ]
300*edab6123Sjeremylt  }
301*edab6123Sjeremylt ],
302*edab6123Sjeremylt "metadata": {
303*edab6123Sjeremylt  "kernelspec": {
304*edab6123Sjeremylt   "display_name": "Python 3",
305*edab6123Sjeremylt   "language": "python",
306*edab6123Sjeremylt   "name": "python3"
307*edab6123Sjeremylt  },
308*edab6123Sjeremylt  "language_info": {
309*edab6123Sjeremylt   "codemirror_mode": {
310*edab6123Sjeremylt    "name": "ipython",
311*edab6123Sjeremylt    "version": 3
312*edab6123Sjeremylt   },
313*edab6123Sjeremylt   "file_extension": ".py",
314*edab6123Sjeremylt   "mimetype": "text/x-python",
315*edab6123Sjeremylt   "name": "python",
316*edab6123Sjeremylt   "nbconvert_exporter": "python",
317*edab6123Sjeremylt   "pygments_lexer": "ipython3",
318*edab6123Sjeremylt   "version": "3.8.5"
319*edab6123Sjeremylt  }
320*edab6123Sjeremylt },
321*edab6123Sjeremylt "nbformat": 4,
322*edab6123Sjeremylt "nbformat_minor": 4
323*edab6123Sjeremylt}
324