1bcb2dfaeSJed Brown# PETSc demos and BPs 2bcb2dfaeSJed Brown 3bcb2dfaeSJed Brown(example-petsc-area)= 4bcb2dfaeSJed Brown 5bcb2dfaeSJed Brown## Area 6bcb2dfaeSJed Brown 7bcb2dfaeSJed BrownThis example is located in the subdirectory {file}`examples/petsc`. 8bcb2dfaeSJed BrownIt demonstrates a simple usage of libCEED with PETSc to calculate the surface area of a closed surface. 9bcb2dfaeSJed BrownThe code uses higher level communication protocols for mesh handling in PETSc's DMPlex. 10bcb2dfaeSJed BrownThis example has the same mathematical formulation as {ref}`Ex1-Volume`, with the exception that the physical coordinates for this problem are $\bm{x}=(x,y,z)\in \mathbb{R}^3$, while the coordinates of the reference element are $\bm{X}=(X,Y) \equiv (X_0,X_1) \in \textrm{I} =[-1,1]^2$. 11bcb2dfaeSJed Brown 12bcb2dfaeSJed Brown(example-petsc-area-cube)= 13bcb2dfaeSJed Brown 14bcb2dfaeSJed Brown### Cube 15bcb2dfaeSJed Brown 16bcb2dfaeSJed BrownThis is one of the test cases of the computation of the {ref}`example-petsc-area` of a 2D manifold embedded in 3D. 17bcb2dfaeSJed BrownThis problem can be run with: 18bcb2dfaeSJed Brown 19bcb2dfaeSJed Brown``` 20bcb2dfaeSJed Brown./area -problem cube 21bcb2dfaeSJed Brown``` 22bcb2dfaeSJed Brown 23bcb2dfaeSJed BrownThis example uses the following coordinate transformations for the computation of the geometric factors: from the physical coordinates on the cube, denoted by $\bar{\bm{x}}=(\bar{x},\bar{y},\bar{z})$, and physical coordinates on the discrete surface, denoted by $\bm{{x}}=(x,y)$, to $\bm{X}=(X,Y) \in \textrm{I}$ on the reference element, via the chain rule 24bcb2dfaeSJed Brown 25bcb2dfaeSJed Brown$$ 26bcb2dfaeSJed Brown\frac{\partial \bm{x}}{\partial \bm{X}}_{(2\times2)} = \frac{\partial {\bm{x}}}{\partial \bar{\bm{x}}}_{(2\times3)} \frac{\partial \bar{\bm{x}}}{\partial \bm{X}}_{(3\times2)}, 27bcb2dfaeSJed Brown$$ (eq-coordinate-transforms-cube) 28bcb2dfaeSJed Brown 29bcb2dfaeSJed Brownwith Jacobian determinant given by 30bcb2dfaeSJed Brown 31bcb2dfaeSJed Brown$$ 32bcb2dfaeSJed Brown\left| J \right| = \left\|col_1\left(\frac{\partial \bar{\bm{x}}}{\partial \bm{X}}\right)\right\| \left\|col_2 \left(\frac{\partial \bar{\bm{x}}}{\partial \bm{X}}\right) \right\| 33bcb2dfaeSJed Brown$$ (eq-jacobian-cube) 34bcb2dfaeSJed Brown 35*8791656fSJed BrownWe note that in equation {eq}`eq-coordinate-transforms-cube`, the right-most Jacobian matrix ${\partial\bar{\bm{x}}}/{\partial \bm{X}}_{(3\times2)}$ is provided by the library, while ${\partial{\bm{x}}}/{\partial \bar{ \bm{x}}}_{(2\times3)}$ is provided by the user as 36bcb2dfaeSJed Brown 37bcb2dfaeSJed Brown$$ 38bcb2dfaeSJed Brown\left[ col_1\left(\frac{\partial\bar{\bm{x}}}{\partial \bm{X}}\right) / \left\| col_1\left(\frac{\partial\bar{\bm{x}}}{\partial \bm{X}}\right)\right\| , col_2\left(\frac{\partial\bar{\bm{x}}}{\partial \bm{X}}\right) / \left\| col_2\left(\frac{\partial\bar{\bm{x}}}{\partial \bm{X}}\right)\right\| \right]^T_{(2\times 3)}. 39bcb2dfaeSJed Brown$$ 40bcb2dfaeSJed Brown 41bcb2dfaeSJed Brown(example-petsc-area-sphere)= 42bcb2dfaeSJed Brown 43bcb2dfaeSJed Brown### Sphere 44bcb2dfaeSJed Brown 45bcb2dfaeSJed BrownThis problem computes the surface {ref}`example-petsc-area` of a tensor-product discrete sphere, obtained by projecting a cube inscribed in a sphere onto the surface of the sphere. 46bcb2dfaeSJed BrownThis discrete surface is sometimes referred to as a cubed-sphere (an example of such as a surface is given in figure {numref}`fig-cubed-sphere`). 47bcb2dfaeSJed BrownThis problem can be run with: 48bcb2dfaeSJed Brown 49bcb2dfaeSJed Brown``` 50bcb2dfaeSJed Brown./area -problem sphere 51bcb2dfaeSJed Brown``` 52bcb2dfaeSJed Brown 53bcb2dfaeSJed Brown(fig-cubed-sphere)= 54bcb2dfaeSJed Brown 55bcb2dfaeSJed Brown:::{figure} ../../../../img/CubedSphere.svg 56bcb2dfaeSJed BrownExample of a cubed-sphere, i.e., a tensor-product discrete sphere, obtained by 57bcb2dfaeSJed Brownprojecting a cube inscribed in a sphere onto the surface of the sphere. 58bcb2dfaeSJed Brown::: 59bcb2dfaeSJed Brown 60bcb2dfaeSJed BrownThis example uses the following coordinate transformations for the computation of the geometric factors: from the physical coordinates on the sphere, denoted by $\overset{\circ}{\bm{x}}=(\overset{\circ}{x},\overset{\circ}{y},\overset{\circ}{z})$, and physical coordinates on the discrete surface, denoted by $\bm{{x}}=(x,y,z)$ (depicted, for simplicity, as coordinates on a circle and 1D linear element in figure {numref}`fig-sphere-coords`), to $\bm{X}=(X,Y) \in \textrm{I}$ on the reference element, via the chain rule 61bcb2dfaeSJed Brown 62bcb2dfaeSJed Brown$$ 63bcb2dfaeSJed Brown\frac{\partial \overset{\circ}{\bm{x}}}{\partial \bm{X}}_{(3\times2)} = \frac{\partial \overset{\circ}{\bm{x}}}{\partial \bm{x}}_{(3\times3)} \frac{\partial\bm{x}}{\partial \bm{X}}_{(3\times2)} , 64bcb2dfaeSJed Brown$$ (eq-coordinate-transforms-sphere) 65bcb2dfaeSJed Brown 66bcb2dfaeSJed Brownwith Jacobian determinant given by 67bcb2dfaeSJed Brown 68bcb2dfaeSJed Brown$$ 69bcb2dfaeSJed Brown\left| J \right| = \left| col_1\left(\frac{\partial \overset{\circ}{\bm{x}}}{\partial \bm{X}}\right) \times col_2 \left(\frac{\partial \overset{\circ}{\bm{x}}}{\partial \bm{X}}\right)\right| . 70bcb2dfaeSJed Brown$$ (eq-jacobian-sphere) 71bcb2dfaeSJed Brown 72bcb2dfaeSJed Brown(fig-sphere-coords)= 73bcb2dfaeSJed Brown 74bcb2dfaeSJed Brown:::{figure} ../../../../img/SphereSketch.svg 75bcb2dfaeSJed BrownSketch of coordinates mapping between a 1D linear element and a circle. 76bcb2dfaeSJed BrownIn the case of a linear element the two nodes, $p_0$ and $p_1$, marked by red crosses, coincide with the endpoints of the element. 77bcb2dfaeSJed BrownTwo quadrature points, $q_0$ and $q_1$, marked by blue dots, with physical coordinates denoted by $\bm x(\bm X)$, are mapped to their corresponding radial projections on the circle, which have coordinates $\overset{\circ}{\bm{x}}(\bm x)$. 78bcb2dfaeSJed Brown::: 79bcb2dfaeSJed Brown 80*8791656fSJed BrownWe note that in equation {eq}`eq-coordinate-transforms-sphere`, the right-most Jacobian matrix ${\partial\bm{x}}/{\partial \bm{X}}_{(3\times2)}$ is provided by the library, while ${\partial \overset{\circ}{\bm{x}}}/{\partial \bm{x}}_{(3\times3)}$ is provided by the user with analytical derivatives. 81bcb2dfaeSJed BrownIn particular, for a sphere of radius 1, we have 82bcb2dfaeSJed Brown 83bcb2dfaeSJed Brown$$ 84bcb2dfaeSJed Brown\overset{\circ}{\bm x}(\bm x) = \frac{1}{\lVert \bm x \rVert} \bm x_{(3\times 1)} 85bcb2dfaeSJed Brown$$ 86bcb2dfaeSJed Brown 87bcb2dfaeSJed Brownand thus 88bcb2dfaeSJed Brown 89bcb2dfaeSJed Brown$$ 90bcb2dfaeSJed Brown\frac{\partial \overset{\circ}{\bm{x}}}{\partial \bm{x}} = \frac{1}{\lVert \bm x \rVert} \bm I_{(3\times 3)} - \frac{1}{\lVert \bm x \rVert^3} (\bm x \bm x^T)_{(3\times 3)} . 91bcb2dfaeSJed Brown$$ 92bcb2dfaeSJed Brown 93bcb2dfaeSJed Brown(example-petsc-bps)= 94bcb2dfaeSJed Brown 95bcb2dfaeSJed Brown## Bakeoff problems and generalizations 96bcb2dfaeSJed Brown 97bcb2dfaeSJed BrownThe PETSc examples in this directory include a full suite of parallel {ref}`bakeoff problems <bps>` (BPs) using a "raw" parallel decomposition (see `bpsraw.c`) and using PETSc's `DMPlex` for unstructured grid management (see `bps.c`). 98bcb2dfaeSJed BrownA generalization of these BPs to the surface of the cubed-sphere are available in `bpssphere.c`. 99bcb2dfaeSJed Brown 100bcb2dfaeSJed Brown(example-petsc-bps-sphere)= 101bcb2dfaeSJed Brown 102bcb2dfaeSJed Brown### Bakeoff problems on the cubed-sphere 103bcb2dfaeSJed Brown 104*8791656fSJed BrownFor the $L^2$ projection problems, BP1-BP2, that use the mass operator, the coordinate transformations and the corresponding Jacobian determinant, equation {eq}`eq-jacobian-sphere`, are the same as in the {ref}`example-petsc-area-sphere` example. 105*8791656fSJed BrownFor the Poisson's problem, BP3-BP6, on the cubed-sphere, in addition to equation {eq}`eq-jacobian-sphere`, the pseudo-inverse of $\partial \overset{\circ}{\bm{x}} / \partial \bm{X}$ is used to derive the contravariant metric tensor (please see figure {numref}`fig-sphere-coords` for a reference of the notation used). 106bcb2dfaeSJed BrownWe begin by expressing the Moore-Penrose (left) pseudo-inverse: 107bcb2dfaeSJed Brown 108bcb2dfaeSJed Brown$$ 109bcb2dfaeSJed Brown\frac{\partial \bm{X}}{\partial \overset{\circ}{\bm{x}}}_{(2\times 3)} \equiv \left(\frac{\partial \overset{\circ}{\bm{x}}}{\partial \bm{X}}\right)_{(2\times 3)}^{+} = \left(\frac{\partial \overset{\circ}{\bm{x}}}{\partial \bm{X}}_{(2\times3)}^T \frac{\partial\overset{\circ}{\bm{x}}}{\partial \bm{X}}_{(3\times2)} \right)^{-1} \frac{\partial \overset{\circ}{\bm{x}}}{\partial \bm{X}}_{(2\times3)}^T . 110bcb2dfaeSJed Brown$$ (eq-dxcircdX-pseudo-inv) 111bcb2dfaeSJed Brown 112bcb2dfaeSJed BrownThis enables computation of gradients of an arbitrary function $u(\overset{\circ}{\bm x})$ in the embedding space as 113bcb2dfaeSJed Brown 114bcb2dfaeSJed Brown$$ 115bcb2dfaeSJed Brown\frac{\partial u}{\partial \overset{\circ}{\bm x}}_{(1\times 3)} = \frac{\partial u}{\partial \bm X}_{(1\times 2)} \frac{\partial \bm X}{\partial \overset{\circ}{\bm x}}_{(2\times 3)} 116bcb2dfaeSJed Brown$$ 117bcb2dfaeSJed Brown 118bcb2dfaeSJed Brownand thus the weak Laplacian may be expressed as 119bcb2dfaeSJed Brown 120bcb2dfaeSJed Brown$$ 121bcb2dfaeSJed Brown\int_{\Omega} \frac{\partial v}{\partial \overset\circ{\bm x}} \left( \frac{\partial u}{\partial \overset\circ{\bm x}} \right)^T \, dS 122bcb2dfaeSJed Brown = \int_{\Omega} \frac{\partial v}{\partial \bm X} \underbrace{\frac{\partial \bm X}{\partial \overset\circ{\bm x}} \left( \frac{\partial \bm X}{\partial \overset\circ{\bm x}} \right)^T}_{\bm g_{(2\times 2)}} \left(\frac{\partial u}{\partial \bm X} \right)^T \, dS 123bcb2dfaeSJed Brown$$ (eq-weak-laplace-sphere) 124bcb2dfaeSJed Brown 125bcb2dfaeSJed Brownwhere we have identified the $2\times 2$ contravariant metric tensor $\bm g$ (sometimes written $\bm g^{ij}$), and where now $\Omega$ represents the surface of the sphere, which is a two-dimensional closed surface embedded in the three-dimensional Euclidean space $\mathbb{R}^3$. 126bcb2dfaeSJed BrownThis expression can be simplified to avoid the explicit Moore-Penrose pseudo-inverse, 127bcb2dfaeSJed Brown 128bcb2dfaeSJed Brown$$ 129bcb2dfaeSJed Brown\bm g = \left(\frac{\partial \overset{\circ}{\bm{x}}}{\partial \bm{X}}^T \frac{\partial\overset{\circ}{\bm{x}}}{\partial \bm{X}} \right)^{-1}_{(2\times 2)} \frac{\partial \overset{\circ}{\bm{x}}}{\partial \bm{X}}_{(2\times3)}^T \frac{\partial \overset{\circ}{\bm{x}}}{\partial \bm{X}}_{(3\times2)} \left(\frac{\partial \overset{\circ}{\bm{x}}}{\partial \bm{X}}^T \frac{\partial\overset{\circ}{\bm{x}}}{\partial \bm{X}} \right)^{-T}_{(2\times 2)} = \left(\frac{\partial \overset{\circ}{\bm{x}}}{\partial \bm{X}}^T \frac{\partial\overset{\circ}{\bm{x}}}{\partial \bm{X}} \right)^{-1}_{(2\times 2)} 130bcb2dfaeSJed Brown$$ 131bcb2dfaeSJed Brown 132bcb2dfaeSJed Brownwhere we have dropped the transpose due to symmetry. 133*8791656fSJed BrownThis allows us to simplify {eq}`eq-weak-laplace-sphere` as 134bcb2dfaeSJed Brown 135bcb2dfaeSJed Brown$$ 136bcb2dfaeSJed Brown\int_{\Omega} \frac{\partial v}{\partial \overset\circ{\bm x}} \left( \frac{\partial u}{\partial \overset\circ{\bm x}} \right)^T \, dS = \int_{\Omega} \frac{\partial v}{\partial \bm X} \underbrace{\left(\frac{\partial \overset{\circ}{\bm{x}}}{\partial \bm{X}}^T \frac{\partial\overset{\circ}{\bm{x}}}{\partial \bm{X}} \right)^{-1}}_{\bm g_{(2\times 2)}} \left(\frac{\partial u}{\partial \bm X} \right)^T \, dS , 137bcb2dfaeSJed Brown$$ 138bcb2dfaeSJed Brown 139bcb2dfaeSJed Brownwhich is the form implemented in `qfunctions/bps/bp3sphere.h`. 140bcb2dfaeSJed Brown 141bcb2dfaeSJed Brown(example-petsc-multigrid)= 142bcb2dfaeSJed Brown 143bcb2dfaeSJed Brown## Multigrid 144bcb2dfaeSJed Brown 145bcb2dfaeSJed BrownThis example is located in the subdirectory {file}`examples/petsc`. 146*8791656fSJed BrownIt investigates $p$-multigrid for the Poisson problem, equation {eq}`eq-variable-coeff-poisson`, using an unstructured high-order finite element discretization. 147bcb2dfaeSJed BrownAll of the operators associated with the geometric multigrid are implemented in libCEED. 148bcb2dfaeSJed Brown 149bcb2dfaeSJed Brown$$ 150bcb2dfaeSJed Brown-\nabla\cdot \left( \kappa \left( x \right) \nabla x \right) = g \left( x \right) 151bcb2dfaeSJed Brown$$ (eq-variable-coeff-poisson) 152bcb2dfaeSJed Brown 153bcb2dfaeSJed BrownThe Poisson operator can be specified with the decomposition given by the equation in figure {ref}`fig-operator-decomp`, and the restriction and prolongation operators given by interpolation basis operations, $\bm{B}$, and $\bm{B}^T$, respectively, act on the different grid levels with corresponding element restrictions, $\bm{G}$. 154bcb2dfaeSJed BrownThese three operations can be exploited by existing matrix-free multigrid software and smoothers. 155bcb2dfaeSJed BrownPreconditioning based on the libCEED finite element operator decomposition is an ongoing area of research. 156