xref: /petsc/src/dm/dt/interface/dt.c (revision ea78f98c112368f404cd6d4fff6d4dfe73e5a1e7)
137045ce4SJed Brown /* Discretization tools */
237045ce4SJed Brown 
30c35b76eSJed Brown #include <petscdt.h>            /*I "petscdt.h" I*/
437045ce4SJed Brown #include <petscblaslapack.h>
5af0996ceSBarry Smith #include <petsc/private/petscimpl.h>
6af0996ceSBarry Smith #include <petsc/private/dtimpl.h>
7665c2dedSJed Brown #include <petscviewer.h>
859804f93SMatthew G. Knepley #include <petscdmplex.h>
959804f93SMatthew G. Knepley #include <petscdmshell.h>
1037045ce4SJed Brown 
1198c04793SMatthew G. Knepley #if defined(PETSC_HAVE_MPFR)
1298c04793SMatthew G. Knepley #include <mpfr.h>
1398c04793SMatthew G. Knepley #endif
1498c04793SMatthew G. Knepley 
15*ea78f98cSLisandro Dalcin const char *const PetscDTNodeTypes[] = {"gaussjacobi", "equispaced", "tanhsinh", "PETSCDTNODES_", NULL};
16d4afb720SToby Isaac 
17e6a796c3SToby Isaac static PetscBool GolubWelschCite       = PETSC_FALSE;
18e6a796c3SToby Isaac const char       GolubWelschCitation[] = "@article{GolubWelsch1969,\n"
190bfcf5a5SMatthew G. Knepley                                          "  author  = {Golub and Welsch},\n"
200bfcf5a5SMatthew G. Knepley                                          "  title   = {Calculation of Quadrature Rules},\n"
210bfcf5a5SMatthew G. Knepley                                          "  journal = {Math. Comp.},\n"
220bfcf5a5SMatthew G. Knepley                                          "  volume  = {23},\n"
230bfcf5a5SMatthew G. Knepley                                          "  number  = {106},\n"
240bfcf5a5SMatthew G. Knepley                                          "  pages   = {221--230},\n"
250bfcf5a5SMatthew G. Knepley                                          "  year    = {1969}\n}\n";
260bfcf5a5SMatthew G. Knepley 
27c4762a1bSJed Brown /* Numerical tests in src/dm/dt/tests/ex1.c show that when computing the nodes and weights of Gauss-Jacobi
2894e21283SToby Isaac    quadrature rules:
29e6a796c3SToby Isaac 
3094e21283SToby Isaac    - in double precision, Newton's method and Golub & Welsch both work for moderate degrees (< 100),
3194e21283SToby Isaac    - in single precision, Newton's method starts producing incorrect roots around n = 15, but
3294e21283SToby Isaac      the weights from Golub & Welsch become a problem before then: they produces errors
3394e21283SToby Isaac      in computing the Jacobi-polynomial Gram matrix around n = 6.
3494e21283SToby Isaac 
3594e21283SToby Isaac    So we default to Newton's method (required fewer dependencies) */
3694e21283SToby Isaac PetscBool PetscDTGaussQuadratureNewton_Internal = PETSC_TRUE;
372cd22861SMatthew G. Knepley 
382cd22861SMatthew G. Knepley PetscClassId PETSCQUADRATURE_CLASSID = 0;
392cd22861SMatthew G. Knepley 
4040d8ff71SMatthew G. Knepley /*@
4140d8ff71SMatthew G. Knepley   PetscQuadratureCreate - Create a PetscQuadrature object
4240d8ff71SMatthew G. Knepley 
43d083f849SBarry Smith   Collective
4440d8ff71SMatthew G. Knepley 
4540d8ff71SMatthew G. Knepley   Input Parameter:
4640d8ff71SMatthew G. Knepley . comm - The communicator for the PetscQuadrature object
4740d8ff71SMatthew G. Knepley 
4840d8ff71SMatthew G. Knepley   Output Parameter:
4940d8ff71SMatthew G. Knepley . q  - The PetscQuadrature object
5040d8ff71SMatthew G. Knepley 
5140d8ff71SMatthew G. Knepley   Level: beginner
5240d8ff71SMatthew G. Knepley 
5340d8ff71SMatthew G. Knepley .seealso: PetscQuadratureDestroy(), PetscQuadratureGetData()
5440d8ff71SMatthew G. Knepley @*/
5521454ff5SMatthew G. Knepley PetscErrorCode PetscQuadratureCreate(MPI_Comm comm, PetscQuadrature *q)
5621454ff5SMatthew G. Knepley {
5721454ff5SMatthew G. Knepley   PetscErrorCode ierr;
5821454ff5SMatthew G. Knepley 
5921454ff5SMatthew G. Knepley   PetscFunctionBegin;
6021454ff5SMatthew G. Knepley   PetscValidPointer(q, 2);
612cd22861SMatthew G. Knepley   ierr = DMInitializePackage();CHKERRQ(ierr);
622cd22861SMatthew G. Knepley   ierr = PetscHeaderCreate(*q,PETSCQUADRATURE_CLASSID,"PetscQuadrature","Quadrature","DT",comm,PetscQuadratureDestroy,PetscQuadratureView);CHKERRQ(ierr);
6321454ff5SMatthew G. Knepley   (*q)->dim       = -1;
64a6b92713SMatthew G. Knepley   (*q)->Nc        =  1;
65bcede257SMatthew G. Knepley   (*q)->order     = -1;
6621454ff5SMatthew G. Knepley   (*q)->numPoints = 0;
6721454ff5SMatthew G. Knepley   (*q)->points    = NULL;
6821454ff5SMatthew G. Knepley   (*q)->weights   = NULL;
6921454ff5SMatthew G. Knepley   PetscFunctionReturn(0);
7021454ff5SMatthew G. Knepley }
7121454ff5SMatthew G. Knepley 
72c9638911SMatthew G. Knepley /*@
73c9638911SMatthew G. Knepley   PetscQuadratureDuplicate - Create a deep copy of the PetscQuadrature object
74c9638911SMatthew G. Knepley 
75d083f849SBarry Smith   Collective on q
76c9638911SMatthew G. Knepley 
77c9638911SMatthew G. Knepley   Input Parameter:
78c9638911SMatthew G. Knepley . q  - The PetscQuadrature object
79c9638911SMatthew G. Knepley 
80c9638911SMatthew G. Knepley   Output Parameter:
81c9638911SMatthew G. Knepley . r  - The new PetscQuadrature object
82c9638911SMatthew G. Knepley 
83c9638911SMatthew G. Knepley   Level: beginner
84c9638911SMatthew G. Knepley 
85c9638911SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureDestroy(), PetscQuadratureGetData()
86c9638911SMatthew G. Knepley @*/
87c9638911SMatthew G. Knepley PetscErrorCode PetscQuadratureDuplicate(PetscQuadrature q, PetscQuadrature *r)
88c9638911SMatthew G. Knepley {
89a6b92713SMatthew G. Knepley   PetscInt         order, dim, Nc, Nq;
90c9638911SMatthew G. Knepley   const PetscReal *points, *weights;
91c9638911SMatthew G. Knepley   PetscReal       *p, *w;
92c9638911SMatthew G. Knepley   PetscErrorCode   ierr;
93c9638911SMatthew G. Knepley 
94c9638911SMatthew G. Knepley   PetscFunctionBegin;
95c9638911SMatthew G. Knepley   PetscValidPointer(q, 2);
96c9638911SMatthew G. Knepley   ierr = PetscQuadratureCreate(PetscObjectComm((PetscObject) q), r);CHKERRQ(ierr);
97c9638911SMatthew G. Knepley   ierr = PetscQuadratureGetOrder(q, &order);CHKERRQ(ierr);
98c9638911SMatthew G. Knepley   ierr = PetscQuadratureSetOrder(*r, order);CHKERRQ(ierr);
99a6b92713SMatthew G. Knepley   ierr = PetscQuadratureGetData(q, &dim, &Nc, &Nq, &points, &weights);CHKERRQ(ierr);
100c9638911SMatthew G. Knepley   ierr = PetscMalloc1(Nq*dim, &p);CHKERRQ(ierr);
101f0a0bfafSMatthew G. Knepley   ierr = PetscMalloc1(Nq*Nc, &w);CHKERRQ(ierr);
102580bdb30SBarry Smith   ierr = PetscArraycpy(p, points, Nq*dim);CHKERRQ(ierr);
103580bdb30SBarry Smith   ierr = PetscArraycpy(w, weights, Nc * Nq);CHKERRQ(ierr);
104a6b92713SMatthew G. Knepley   ierr = PetscQuadratureSetData(*r, dim, Nc, Nq, p, w);CHKERRQ(ierr);
105c9638911SMatthew G. Knepley   PetscFunctionReturn(0);
106c9638911SMatthew G. Knepley }
107c9638911SMatthew G. Knepley 
10840d8ff71SMatthew G. Knepley /*@
10940d8ff71SMatthew G. Knepley   PetscQuadratureDestroy - Destroys a PetscQuadrature object
11040d8ff71SMatthew G. Knepley 
111d083f849SBarry Smith   Collective on q
11240d8ff71SMatthew G. Knepley 
11340d8ff71SMatthew G. Knepley   Input Parameter:
11440d8ff71SMatthew G. Knepley . q  - The PetscQuadrature object
11540d8ff71SMatthew G. Knepley 
11640d8ff71SMatthew G. Knepley   Level: beginner
11740d8ff71SMatthew G. Knepley 
11840d8ff71SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureGetData()
11940d8ff71SMatthew G. Knepley @*/
120bfa639d9SMatthew G. Knepley PetscErrorCode PetscQuadratureDestroy(PetscQuadrature *q)
121bfa639d9SMatthew G. Knepley {
122bfa639d9SMatthew G. Knepley   PetscErrorCode ierr;
123bfa639d9SMatthew G. Knepley 
124bfa639d9SMatthew G. Knepley   PetscFunctionBegin;
12521454ff5SMatthew G. Knepley   if (!*q) PetscFunctionReturn(0);
1262cd22861SMatthew G. Knepley   PetscValidHeaderSpecific((*q),PETSCQUADRATURE_CLASSID,1);
12721454ff5SMatthew G. Knepley   if (--((PetscObject)(*q))->refct > 0) {
12821454ff5SMatthew G. Knepley     *q = NULL;
12921454ff5SMatthew G. Knepley     PetscFunctionReturn(0);
13021454ff5SMatthew G. Knepley   }
13121454ff5SMatthew G. Knepley   ierr = PetscFree((*q)->points);CHKERRQ(ierr);
13221454ff5SMatthew G. Knepley   ierr = PetscFree((*q)->weights);CHKERRQ(ierr);
13321454ff5SMatthew G. Knepley   ierr = PetscHeaderDestroy(q);CHKERRQ(ierr);
13421454ff5SMatthew G. Knepley   PetscFunctionReturn(0);
13521454ff5SMatthew G. Knepley }
13621454ff5SMatthew G. Knepley 
137bcede257SMatthew G. Knepley /*@
138a6b92713SMatthew G. Knepley   PetscQuadratureGetOrder - Return the order of the method
139bcede257SMatthew G. Knepley 
140bcede257SMatthew G. Knepley   Not collective
141bcede257SMatthew G. Knepley 
142bcede257SMatthew G. Knepley   Input Parameter:
143bcede257SMatthew G. Knepley . q - The PetscQuadrature object
144bcede257SMatthew G. Knepley 
145bcede257SMatthew G. Knepley   Output Parameter:
146bcede257SMatthew G. Knepley . order - The order of the quadrature, i.e. the highest degree polynomial that is exactly integrated
147bcede257SMatthew G. Knepley 
148bcede257SMatthew G. Knepley   Level: intermediate
149bcede257SMatthew G. Knepley 
150bcede257SMatthew G. Knepley .seealso: PetscQuadratureSetOrder(), PetscQuadratureGetData(), PetscQuadratureSetData()
151bcede257SMatthew G. Knepley @*/
152bcede257SMatthew G. Knepley PetscErrorCode PetscQuadratureGetOrder(PetscQuadrature q, PetscInt *order)
153bcede257SMatthew G. Knepley {
154bcede257SMatthew G. Knepley   PetscFunctionBegin;
1552cd22861SMatthew G. Knepley   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
156bcede257SMatthew G. Knepley   PetscValidPointer(order, 2);
157bcede257SMatthew G. Knepley   *order = q->order;
158bcede257SMatthew G. Knepley   PetscFunctionReturn(0);
159bcede257SMatthew G. Knepley }
160bcede257SMatthew G. Knepley 
161bcede257SMatthew G. Knepley /*@
162a6b92713SMatthew G. Knepley   PetscQuadratureSetOrder - Return the order of the method
163bcede257SMatthew G. Knepley 
164bcede257SMatthew G. Knepley   Not collective
165bcede257SMatthew G. Knepley 
166bcede257SMatthew G. Knepley   Input Parameters:
167bcede257SMatthew G. Knepley + q - The PetscQuadrature object
168bcede257SMatthew G. Knepley - order - The order of the quadrature, i.e. the highest degree polynomial that is exactly integrated
169bcede257SMatthew G. Knepley 
170bcede257SMatthew G. Knepley   Level: intermediate
171bcede257SMatthew G. Knepley 
172bcede257SMatthew G. Knepley .seealso: PetscQuadratureGetOrder(), PetscQuadratureGetData(), PetscQuadratureSetData()
173bcede257SMatthew G. Knepley @*/
174bcede257SMatthew G. Knepley PetscErrorCode PetscQuadratureSetOrder(PetscQuadrature q, PetscInt order)
175bcede257SMatthew G. Knepley {
176bcede257SMatthew G. Knepley   PetscFunctionBegin;
1772cd22861SMatthew G. Knepley   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
178bcede257SMatthew G. Knepley   q->order = order;
179bcede257SMatthew G. Knepley   PetscFunctionReturn(0);
180bcede257SMatthew G. Knepley }
181bcede257SMatthew G. Knepley 
182a6b92713SMatthew G. Knepley /*@
183a6b92713SMatthew G. Knepley   PetscQuadratureGetNumComponents - Return the number of components for functions to be integrated
184a6b92713SMatthew G. Knepley 
185a6b92713SMatthew G. Knepley   Not collective
186a6b92713SMatthew G. Knepley 
187a6b92713SMatthew G. Knepley   Input Parameter:
188a6b92713SMatthew G. Knepley . q - The PetscQuadrature object
189a6b92713SMatthew G. Knepley 
190a6b92713SMatthew G. Knepley   Output Parameter:
191a6b92713SMatthew G. Knepley . Nc - The number of components
192a6b92713SMatthew G. Knepley 
193a6b92713SMatthew G. Knepley   Note: We are performing an integral int f(x) . w(x) dx, where both f and w (the weight) have Nc components.
194a6b92713SMatthew G. Knepley 
195a6b92713SMatthew G. Knepley   Level: intermediate
196a6b92713SMatthew G. Knepley 
197a6b92713SMatthew G. Knepley .seealso: PetscQuadratureSetNumComponents(), PetscQuadratureGetData(), PetscQuadratureSetData()
198a6b92713SMatthew G. Knepley @*/
199a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureGetNumComponents(PetscQuadrature q, PetscInt *Nc)
200a6b92713SMatthew G. Knepley {
201a6b92713SMatthew G. Knepley   PetscFunctionBegin;
2022cd22861SMatthew G. Knepley   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
203a6b92713SMatthew G. Knepley   PetscValidPointer(Nc, 2);
204a6b92713SMatthew G. Knepley   *Nc = q->Nc;
205a6b92713SMatthew G. Knepley   PetscFunctionReturn(0);
206a6b92713SMatthew G. Knepley }
207a6b92713SMatthew G. Knepley 
208a6b92713SMatthew G. Knepley /*@
209a6b92713SMatthew G. Knepley   PetscQuadratureSetNumComponents - Return the number of components for functions to be integrated
210a6b92713SMatthew G. Knepley 
211a6b92713SMatthew G. Knepley   Not collective
212a6b92713SMatthew G. Knepley 
213a6b92713SMatthew G. Knepley   Input Parameters:
214a6b92713SMatthew G. Knepley + q  - The PetscQuadrature object
215a6b92713SMatthew G. Knepley - Nc - The number of components
216a6b92713SMatthew G. Knepley 
217a6b92713SMatthew G. Knepley   Note: We are performing an integral int f(x) . w(x) dx, where both f and w (the weight) have Nc components.
218a6b92713SMatthew G. Knepley 
219a6b92713SMatthew G. Knepley   Level: intermediate
220a6b92713SMatthew G. Knepley 
221a6b92713SMatthew G. Knepley .seealso: PetscQuadratureGetNumComponents(), PetscQuadratureGetData(), PetscQuadratureSetData()
222a6b92713SMatthew G. Knepley @*/
223a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureSetNumComponents(PetscQuadrature q, PetscInt Nc)
224a6b92713SMatthew G. Knepley {
225a6b92713SMatthew G. Knepley   PetscFunctionBegin;
2262cd22861SMatthew G. Knepley   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
227a6b92713SMatthew G. Knepley   q->Nc = Nc;
228a6b92713SMatthew G. Knepley   PetscFunctionReturn(0);
229a6b92713SMatthew G. Knepley }
230a6b92713SMatthew G. Knepley 
23140d8ff71SMatthew G. Knepley /*@C
23240d8ff71SMatthew G. Knepley   PetscQuadratureGetData - Returns the data defining the quadrature
23340d8ff71SMatthew G. Knepley 
23440d8ff71SMatthew G. Knepley   Not collective
23540d8ff71SMatthew G. Knepley 
23640d8ff71SMatthew G. Knepley   Input Parameter:
23740d8ff71SMatthew G. Knepley . q  - The PetscQuadrature object
23840d8ff71SMatthew G. Knepley 
23940d8ff71SMatthew G. Knepley   Output Parameters:
24040d8ff71SMatthew G. Knepley + dim - The spatial dimension
241805e7170SToby Isaac . Nc - The number of components
24240d8ff71SMatthew G. Knepley . npoints - The number of quadrature points
24340d8ff71SMatthew G. Knepley . points - The coordinates of each quadrature point
24440d8ff71SMatthew G. Knepley - weights - The weight of each quadrature point
24540d8ff71SMatthew G. Knepley 
24640d8ff71SMatthew G. Knepley   Level: intermediate
24740d8ff71SMatthew G. Knepley 
24895452b02SPatrick Sanan   Fortran Notes:
24995452b02SPatrick Sanan     From Fortran you must call PetscQuadratureRestoreData() when you are done with the data
2501fd49c25SBarry Smith 
25140d8ff71SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureSetData()
25240d8ff71SMatthew G. Knepley @*/
253a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureGetData(PetscQuadrature q, PetscInt *dim, PetscInt *Nc, PetscInt *npoints, const PetscReal *points[], const PetscReal *weights[])
25421454ff5SMatthew G. Knepley {
25521454ff5SMatthew G. Knepley   PetscFunctionBegin;
2562cd22861SMatthew G. Knepley   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
25721454ff5SMatthew G. Knepley   if (dim) {
25821454ff5SMatthew G. Knepley     PetscValidPointer(dim, 2);
25921454ff5SMatthew G. Knepley     *dim = q->dim;
26021454ff5SMatthew G. Knepley   }
261a6b92713SMatthew G. Knepley   if (Nc) {
262a6b92713SMatthew G. Knepley     PetscValidPointer(Nc, 3);
263a6b92713SMatthew G. Knepley     *Nc = q->Nc;
264a6b92713SMatthew G. Knepley   }
26521454ff5SMatthew G. Knepley   if (npoints) {
266a6b92713SMatthew G. Knepley     PetscValidPointer(npoints, 4);
26721454ff5SMatthew G. Knepley     *npoints = q->numPoints;
26821454ff5SMatthew G. Knepley   }
26921454ff5SMatthew G. Knepley   if (points) {
270a6b92713SMatthew G. Knepley     PetscValidPointer(points, 5);
27121454ff5SMatthew G. Knepley     *points = q->points;
27221454ff5SMatthew G. Knepley   }
27321454ff5SMatthew G. Knepley   if (weights) {
274a6b92713SMatthew G. Knepley     PetscValidPointer(weights, 6);
27521454ff5SMatthew G. Knepley     *weights = q->weights;
27621454ff5SMatthew G. Knepley   }
27721454ff5SMatthew G. Knepley   PetscFunctionReturn(0);
27821454ff5SMatthew G. Knepley }
27921454ff5SMatthew G. Knepley 
280907761f8SToby Isaac static PetscErrorCode PetscDTJacobianInverse_Internal(PetscInt m, PetscInt n, const PetscReal J[], PetscReal Jinv[])
281907761f8SToby Isaac {
282907761f8SToby Isaac   PetscScalar    *Js, *Jinvs;
283907761f8SToby Isaac   PetscInt       i, j, k;
284907761f8SToby Isaac   PetscBLASInt   bm, bn, info;
285907761f8SToby Isaac   PetscErrorCode ierr;
286907761f8SToby Isaac 
287907761f8SToby Isaac   PetscFunctionBegin;
288d4afb720SToby Isaac   if (!m || !n) PetscFunctionReturn(0);
289907761f8SToby Isaac   ierr = PetscBLASIntCast(m, &bm);CHKERRQ(ierr);
290907761f8SToby Isaac   ierr = PetscBLASIntCast(n, &bn);CHKERRQ(ierr);
291907761f8SToby Isaac #if defined(PETSC_USE_COMPLEX)
292907761f8SToby Isaac   ierr = PetscMalloc2(m*n, &Js, m*n, &Jinvs);CHKERRQ(ierr);
29328222859SToby Isaac   for (i = 0; i < m*n; i++) Js[i] = J[i];
294907761f8SToby Isaac #else
295907761f8SToby Isaac   Js = (PetscReal *) J;
296907761f8SToby Isaac   Jinvs = Jinv;
297907761f8SToby Isaac #endif
298907761f8SToby Isaac   if (m == n) {
299907761f8SToby Isaac     PetscBLASInt *pivots;
300907761f8SToby Isaac     PetscScalar *W;
301907761f8SToby Isaac 
302907761f8SToby Isaac     ierr = PetscMalloc2(m, &pivots, m, &W);CHKERRQ(ierr);
303907761f8SToby Isaac 
304907761f8SToby Isaac     ierr = PetscArraycpy(Jinvs, Js, m * m);CHKERRQ(ierr);
305907761f8SToby Isaac     PetscStackCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bm, &bm, Jinvs, &bm, pivots, &info));
306907761f8SToby Isaac     if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetrf %D",(PetscInt)info);
307907761f8SToby Isaac     PetscStackCallBLAS("LAPACKgetri", LAPACKgetri_(&bm, Jinvs, &bm, pivots, W, &bm, &info));
308907761f8SToby Isaac     if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetri %D",(PetscInt)info);
309907761f8SToby Isaac     ierr = PetscFree2(pivots, W);CHKERRQ(ierr);
310907761f8SToby Isaac   } else if (m < n) {
311907761f8SToby Isaac     PetscScalar *JJT;
312907761f8SToby Isaac     PetscBLASInt *pivots;
313907761f8SToby Isaac     PetscScalar *W;
314907761f8SToby Isaac 
315907761f8SToby Isaac     ierr = PetscMalloc1(m*m, &JJT);CHKERRQ(ierr);
316907761f8SToby Isaac     ierr = PetscMalloc2(m, &pivots, m, &W);CHKERRQ(ierr);
317907761f8SToby Isaac     for (i = 0; i < m; i++) {
318907761f8SToby Isaac       for (j = 0; j < m; j++) {
319907761f8SToby Isaac         PetscScalar val = 0.;
320907761f8SToby Isaac 
321907761f8SToby Isaac         for (k = 0; k < n; k++) val += Js[i * n + k] * Js[j * n + k];
322907761f8SToby Isaac         JJT[i * m + j] = val;
323907761f8SToby Isaac       }
324907761f8SToby Isaac     }
325907761f8SToby Isaac 
326907761f8SToby Isaac     PetscStackCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bm, &bm, JJT, &bm, pivots, &info));
327907761f8SToby Isaac     if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetrf %D",(PetscInt)info);
328907761f8SToby Isaac     PetscStackCallBLAS("LAPACKgetri", LAPACKgetri_(&bm, JJT, &bm, pivots, W, &bm, &info));
329907761f8SToby Isaac     if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetri %D",(PetscInt)info);
330907761f8SToby Isaac     for (i = 0; i < n; i++) {
331907761f8SToby Isaac       for (j = 0; j < m; j++) {
332907761f8SToby Isaac         PetscScalar val = 0.;
333907761f8SToby Isaac 
334907761f8SToby Isaac         for (k = 0; k < m; k++) val += Js[k * n + i] * JJT[k * m + j];
335907761f8SToby Isaac         Jinvs[i * m + j] = val;
336907761f8SToby Isaac       }
337907761f8SToby Isaac     }
338907761f8SToby Isaac     ierr = PetscFree2(pivots, W);CHKERRQ(ierr);
339907761f8SToby Isaac     ierr = PetscFree(JJT);CHKERRQ(ierr);
340907761f8SToby Isaac   } else {
341907761f8SToby Isaac     PetscScalar *JTJ;
342907761f8SToby Isaac     PetscBLASInt *pivots;
343907761f8SToby Isaac     PetscScalar *W;
344907761f8SToby Isaac 
345907761f8SToby Isaac     ierr = PetscMalloc1(n*n, &JTJ);CHKERRQ(ierr);
346907761f8SToby Isaac     ierr = PetscMalloc2(n, &pivots, n, &W);CHKERRQ(ierr);
347907761f8SToby Isaac     for (i = 0; i < n; i++) {
348907761f8SToby Isaac       for (j = 0; j < n; j++) {
349907761f8SToby Isaac         PetscScalar val = 0.;
350907761f8SToby Isaac 
351907761f8SToby Isaac         for (k = 0; k < m; k++) val += Js[k * n + i] * Js[k * n + j];
352907761f8SToby Isaac         JTJ[i * n + j] = val;
353907761f8SToby Isaac       }
354907761f8SToby Isaac     }
355907761f8SToby Isaac 
356d4afb720SToby Isaac     PetscStackCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bn, &bn, JTJ, &bn, pivots, &info));
357907761f8SToby Isaac     if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetrf %D",(PetscInt)info);
358907761f8SToby Isaac     PetscStackCallBLAS("LAPACKgetri", LAPACKgetri_(&bn, JTJ, &bn, pivots, W, &bn, &info));
359907761f8SToby Isaac     if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetri %D",(PetscInt)info);
360907761f8SToby Isaac     for (i = 0; i < n; i++) {
361907761f8SToby Isaac       for (j = 0; j < m; j++) {
362907761f8SToby Isaac         PetscScalar val = 0.;
363907761f8SToby Isaac 
364907761f8SToby Isaac         for (k = 0; k < n; k++) val += JTJ[i * n + k] * Js[j * n + k];
365907761f8SToby Isaac         Jinvs[i * m + j] = val;
366907761f8SToby Isaac       }
367907761f8SToby Isaac     }
368907761f8SToby Isaac     ierr = PetscFree2(pivots, W);CHKERRQ(ierr);
369907761f8SToby Isaac     ierr = PetscFree(JTJ);CHKERRQ(ierr);
370907761f8SToby Isaac   }
371907761f8SToby Isaac #if defined(PETSC_USE_COMPLEX)
37228222859SToby Isaac   for (i = 0; i < m*n; i++) Jinv[i] = PetscRealPart(Jinvs[i]);
373907761f8SToby Isaac   ierr = PetscFree2(Js, Jinvs);CHKERRQ(ierr);
374907761f8SToby Isaac #endif
375907761f8SToby Isaac   PetscFunctionReturn(0);
376907761f8SToby Isaac }
377907761f8SToby Isaac 
378907761f8SToby Isaac /*@
379907761f8SToby Isaac    PetscQuadraturePushForward - Push forward a quadrature functional under an affine transformation.
380907761f8SToby Isaac 
381907761f8SToby Isaac    Collecive on PetscQuadrature
382907761f8SToby Isaac 
383907761f8SToby Isaac    Input Arguments:
384907761f8SToby Isaac +  q - the quadrature functional
385907761f8SToby Isaac .  imageDim - the dimension of the image of the transformation
386907761f8SToby Isaac .  origin - a point in the original space
387907761f8SToby Isaac .  originImage - the image of the origin under the transformation
388907761f8SToby Isaac .  J - the Jacobian of the image: an [imageDim x dim] matrix in row major order
38928222859SToby Isaac -  formDegree - transform the quadrature weights as k-forms of this form degree (if the number of components is a multiple of (dim choose formDegree), it is assumed that they represent multiple k-forms) [see PetscDTAltVPullback() for interpretation of formDegree]
390907761f8SToby Isaac 
391907761f8SToby Isaac    Output Arguments:
392907761f8SToby Isaac .  Jinvstarq - a quadrature rule where each point is the image of a point in the original quadrature rule, and where the k-form weights have been pulled-back by the pseudoinverse of J to the k-form weights in the image space.
393907761f8SToby Isaac 
394907761f8SToby Isaac    Note: the new quadrature rule will have a different number of components if spaces have different dimensions.  For example, pushing a 2-form forward from a two dimensional space to a three dimensional space changes the number of components from 1 to 3.
395907761f8SToby Isaac 
3966c877ef6SSatish Balay    Level: intermediate
3976c877ef6SSatish Balay 
398907761f8SToby Isaac .seealso: PetscDTAltVPullback(), PetscDTAltVPullbackMatrix()
399907761f8SToby Isaac @*/
40028222859SToby Isaac PetscErrorCode PetscQuadraturePushForward(PetscQuadrature q, PetscInt imageDim, const PetscReal origin[], const PetscReal originImage[], const PetscReal J[], PetscInt formDegree, PetscQuadrature *Jinvstarq)
401907761f8SToby Isaac {
402907761f8SToby Isaac   PetscInt         dim, Nc, imageNc, formSize, Ncopies, imageFormSize, Npoints, pt, i, j, c;
403907761f8SToby Isaac   const PetscReal *points;
404907761f8SToby Isaac   const PetscReal *weights;
405907761f8SToby Isaac   PetscReal       *imagePoints, *imageWeights;
406907761f8SToby Isaac   PetscReal       *Jinv;
407907761f8SToby Isaac   PetscReal       *Jinvstar;
408907761f8SToby Isaac   PetscErrorCode   ierr;
409907761f8SToby Isaac 
410907761f8SToby Isaac   PetscFunctionBegin;
411d4afb720SToby Isaac   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
41228222859SToby Isaac   if (imageDim < PetscAbsInt(formDegree)) SETERRQ2(PetscObjectComm((PetscObject)q), PETSC_ERR_ARG_INCOMP, "Cannot represent a %D-form in %D dimensions", PetscAbsInt(formDegree), imageDim);
413907761f8SToby Isaac   ierr = PetscQuadratureGetData(q, &dim, &Nc, &Npoints, &points, &weights);CHKERRQ(ierr);
41428222859SToby Isaac   ierr = PetscDTBinomialInt(dim, PetscAbsInt(formDegree), &formSize);CHKERRQ(ierr);
415907761f8SToby Isaac   if (Nc % formSize) SETERRQ2(PetscObjectComm((PetscObject)q), PETSC_ERR_ARG_INCOMP, "Number of components %D is not a multiple of formSize %D\n", Nc, formSize);
416907761f8SToby Isaac   Ncopies = Nc / formSize;
41728222859SToby Isaac   ierr = PetscDTBinomialInt(imageDim, PetscAbsInt(formDegree), &imageFormSize);CHKERRQ(ierr);
418907761f8SToby Isaac   imageNc = Ncopies * imageFormSize;
419907761f8SToby Isaac   ierr = PetscMalloc1(Npoints * imageDim, &imagePoints);CHKERRQ(ierr);
420907761f8SToby Isaac   ierr = PetscMalloc1(Npoints * imageNc, &imageWeights);CHKERRQ(ierr);
421907761f8SToby Isaac   ierr = PetscMalloc2(imageDim * dim, &Jinv, formSize * imageFormSize, &Jinvstar);CHKERRQ(ierr);
422d4afb720SToby Isaac   ierr = PetscDTJacobianInverse_Internal(imageDim, dim, J, Jinv);CHKERRQ(ierr);
42328222859SToby Isaac   ierr = PetscDTAltVPullbackMatrix(imageDim, dim, Jinv, formDegree, Jinvstar);CHKERRQ(ierr);
424907761f8SToby Isaac   for (pt = 0; pt < Npoints; pt++) {
425907761f8SToby Isaac     const PetscReal *point = &points[pt * dim];
426907761f8SToby Isaac     PetscReal       *imagePoint = &imagePoints[pt * imageDim];
427907761f8SToby Isaac 
428907761f8SToby Isaac     for (i = 0; i < imageDim; i++) {
429907761f8SToby Isaac       PetscReal val = originImage[i];
430907761f8SToby Isaac 
431907761f8SToby Isaac       for (j = 0; j < dim; j++) val += J[i * dim + j] * (point[j] - origin[j]);
432907761f8SToby Isaac       imagePoint[i] = val;
433907761f8SToby Isaac     }
434907761f8SToby Isaac     for (c = 0; c < Ncopies; c++) {
435907761f8SToby Isaac       const PetscReal *form = &weights[pt * Nc + c * formSize];
436907761f8SToby Isaac       PetscReal       *imageForm = &imageWeights[pt * imageNc + c * imageFormSize];
437907761f8SToby Isaac 
438907761f8SToby Isaac       for (i = 0; i < imageFormSize; i++) {
439907761f8SToby Isaac         PetscReal val = 0.;
440907761f8SToby Isaac 
441907761f8SToby Isaac         for (j = 0; j < formSize; j++) val += Jinvstar[i * formSize + j] * form[j];
442907761f8SToby Isaac         imageForm[i] = val;
443907761f8SToby Isaac       }
444907761f8SToby Isaac     }
445907761f8SToby Isaac   }
446907761f8SToby Isaac   ierr = PetscQuadratureCreate(PetscObjectComm((PetscObject)q), Jinvstarq);CHKERRQ(ierr);
447907761f8SToby Isaac   ierr = PetscQuadratureSetData(*Jinvstarq, imageDim, imageNc, Npoints, imagePoints, imageWeights);CHKERRQ(ierr);
448907761f8SToby Isaac   ierr = PetscFree2(Jinv, Jinvstar);CHKERRQ(ierr);
449907761f8SToby Isaac   PetscFunctionReturn(0);
450907761f8SToby Isaac }
451907761f8SToby Isaac 
45240d8ff71SMatthew G. Knepley /*@C
45340d8ff71SMatthew G. Knepley   PetscQuadratureSetData - Sets the data defining the quadrature
45440d8ff71SMatthew G. Knepley 
45540d8ff71SMatthew G. Knepley   Not collective
45640d8ff71SMatthew G. Knepley 
45740d8ff71SMatthew G. Knepley   Input Parameters:
45840d8ff71SMatthew G. Knepley + q  - The PetscQuadrature object
45940d8ff71SMatthew G. Knepley . dim - The spatial dimension
460e2b35d93SBarry Smith . Nc - The number of components
46140d8ff71SMatthew G. Knepley . npoints - The number of quadrature points
46240d8ff71SMatthew G. Knepley . points - The coordinates of each quadrature point
46340d8ff71SMatthew G. Knepley - weights - The weight of each quadrature point
46440d8ff71SMatthew G. Knepley 
465c99e0549SMatthew G. Knepley   Note: This routine owns the references to points and weights, so they must be allocated using PetscMalloc() and the user should not free them.
466f2fd9e53SMatthew G. Knepley 
46740d8ff71SMatthew G. Knepley   Level: intermediate
46840d8ff71SMatthew G. Knepley 
46940d8ff71SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureGetData()
47040d8ff71SMatthew G. Knepley @*/
471a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureSetData(PetscQuadrature q, PetscInt dim, PetscInt Nc, PetscInt npoints, const PetscReal points[], const PetscReal weights[])
47221454ff5SMatthew G. Knepley {
47321454ff5SMatthew G. Knepley   PetscFunctionBegin;
4742cd22861SMatthew G. Knepley   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
47521454ff5SMatthew G. Knepley   if (dim >= 0)     q->dim       = dim;
476a6b92713SMatthew G. Knepley   if (Nc >= 0)      q->Nc        = Nc;
47721454ff5SMatthew G. Knepley   if (npoints >= 0) q->numPoints = npoints;
47821454ff5SMatthew G. Knepley   if (points) {
47921454ff5SMatthew G. Knepley     PetscValidPointer(points, 4);
48021454ff5SMatthew G. Knepley     q->points = points;
48121454ff5SMatthew G. Knepley   }
48221454ff5SMatthew G. Knepley   if (weights) {
48321454ff5SMatthew G. Knepley     PetscValidPointer(weights, 5);
48421454ff5SMatthew G. Knepley     q->weights = weights;
48521454ff5SMatthew G. Knepley   }
486f9fd7fdbSMatthew G. Knepley   PetscFunctionReturn(0);
487f9fd7fdbSMatthew G. Knepley }
488f9fd7fdbSMatthew G. Knepley 
489d9bac1caSLisandro Dalcin static PetscErrorCode PetscQuadratureView_Ascii(PetscQuadrature quad, PetscViewer v)
490d9bac1caSLisandro Dalcin {
491d9bac1caSLisandro Dalcin   PetscInt          q, d, c;
492d9bac1caSLisandro Dalcin   PetscViewerFormat format;
493d9bac1caSLisandro Dalcin   PetscErrorCode    ierr;
494d9bac1caSLisandro Dalcin 
495d9bac1caSLisandro Dalcin   PetscFunctionBegin;
496c74b4a09SMatthew G. Knepley   if (quad->Nc > 1) {ierr = PetscViewerASCIIPrintf(v, "Quadrature of order %D on %D points (dim %D) with %D components\n", quad->order, quad->numPoints, quad->dim, quad->Nc);CHKERRQ(ierr);}
497c74b4a09SMatthew G. Knepley   else              {ierr = PetscViewerASCIIPrintf(v, "Quadrature of order %D on %D points (dim %D)\n", quad->order, quad->numPoints, quad->dim);CHKERRQ(ierr);}
498d9bac1caSLisandro Dalcin   ierr = PetscViewerGetFormat(v, &format);CHKERRQ(ierr);
499d9bac1caSLisandro Dalcin   if (format != PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(0);
500d9bac1caSLisandro Dalcin   for (q = 0; q < quad->numPoints; ++q) {
501c74b4a09SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(v, "p%D (", q);CHKERRQ(ierr);
502d9bac1caSLisandro Dalcin     ierr = PetscViewerASCIIUseTabs(v, PETSC_FALSE);CHKERRQ(ierr);
503d9bac1caSLisandro Dalcin     for (d = 0; d < quad->dim; ++d) {
504d9bac1caSLisandro Dalcin       if (d) {ierr = PetscViewerASCIIPrintf(v, ", ");CHKERRQ(ierr);}
505d9bac1caSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(v, "%+g", (double)quad->points[q*quad->dim+d]);CHKERRQ(ierr);
506d9bac1caSLisandro Dalcin     }
507d9bac1caSLisandro Dalcin     ierr = PetscViewerASCIIPrintf(v, ") ");CHKERRQ(ierr);
508c74b4a09SMatthew G. Knepley     if (quad->Nc > 1) {ierr = PetscViewerASCIIPrintf(v, "w%D (", q);CHKERRQ(ierr);}
509d9bac1caSLisandro Dalcin     for (c = 0; c < quad->Nc; ++c) {
510d9bac1caSLisandro Dalcin       if (c) {ierr = PetscViewerASCIIPrintf(v, ", ");CHKERRQ(ierr);}
511c74b4a09SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(v, "%+g", (double)quad->weights[q*quad->Nc+c]);CHKERRQ(ierr);
512d9bac1caSLisandro Dalcin     }
513d9bac1caSLisandro Dalcin     if (quad->Nc > 1) {ierr = PetscViewerASCIIPrintf(v, ")");CHKERRQ(ierr);}
514d9bac1caSLisandro Dalcin     ierr = PetscViewerASCIIPrintf(v, "\n");CHKERRQ(ierr);
515d9bac1caSLisandro Dalcin     ierr = PetscViewerASCIIUseTabs(v, PETSC_TRUE);CHKERRQ(ierr);
516d9bac1caSLisandro Dalcin   }
517d9bac1caSLisandro Dalcin   PetscFunctionReturn(0);
518d9bac1caSLisandro Dalcin }
519d9bac1caSLisandro Dalcin 
52040d8ff71SMatthew G. Knepley /*@C
52140d8ff71SMatthew G. Knepley   PetscQuadratureView - Views a PetscQuadrature object
52240d8ff71SMatthew G. Knepley 
523d083f849SBarry Smith   Collective on quad
52440d8ff71SMatthew G. Knepley 
52540d8ff71SMatthew G. Knepley   Input Parameters:
526d9bac1caSLisandro Dalcin + quad  - The PetscQuadrature object
52740d8ff71SMatthew G. Knepley - viewer - The PetscViewer object
52840d8ff71SMatthew G. Knepley 
52940d8ff71SMatthew G. Knepley   Level: beginner
53040d8ff71SMatthew G. Knepley 
53140d8ff71SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureGetData()
53240d8ff71SMatthew G. Knepley @*/
533f9fd7fdbSMatthew G. Knepley PetscErrorCode PetscQuadratureView(PetscQuadrature quad, PetscViewer viewer)
534f9fd7fdbSMatthew G. Knepley {
535d9bac1caSLisandro Dalcin   PetscBool      iascii;
536f9fd7fdbSMatthew G. Knepley   PetscErrorCode ierr;
537f9fd7fdbSMatthew G. Knepley 
538f9fd7fdbSMatthew G. Knepley   PetscFunctionBegin;
539d9bac1caSLisandro Dalcin   PetscValidHeader(quad, 1);
540d9bac1caSLisandro Dalcin   if (viewer) PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
541d9bac1caSLisandro Dalcin   if (!viewer) {ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject) quad), &viewer);CHKERRQ(ierr);}
542d9bac1caSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
543d9bac1caSLisandro Dalcin   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
544d9bac1caSLisandro Dalcin   if (iascii) {ierr = PetscQuadratureView_Ascii(quad, viewer);CHKERRQ(ierr);}
545d9bac1caSLisandro Dalcin   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
546bfa639d9SMatthew G. Knepley   PetscFunctionReturn(0);
547bfa639d9SMatthew G. Knepley }
548bfa639d9SMatthew G. Knepley 
54989710940SMatthew G. Knepley /*@C
55089710940SMatthew G. Knepley   PetscQuadratureExpandComposite - Return a quadrature over the composite element, which has the original quadrature in each subelement
55189710940SMatthew G. Knepley 
55289710940SMatthew G. Knepley   Not collective
55389710940SMatthew G. Knepley 
55489710940SMatthew G. Knepley   Input Parameter:
55589710940SMatthew G. Knepley + q - The original PetscQuadrature
55689710940SMatthew G. Knepley . numSubelements - The number of subelements the original element is divided into
55789710940SMatthew G. Knepley . v0 - An array of the initial points for each subelement
55889710940SMatthew G. Knepley - jac - An array of the Jacobian mappings from the reference to each subelement
55989710940SMatthew G. Knepley 
56089710940SMatthew G. Knepley   Output Parameters:
56189710940SMatthew G. Knepley . dim - The dimension
56289710940SMatthew G. Knepley 
56389710940SMatthew G. Knepley   Note: Together v0 and jac define an affine mapping from the original reference element to each subelement
56489710940SMatthew G. Knepley 
565f5f57ec0SBarry Smith  Not available from Fortran
566f5f57ec0SBarry Smith 
56789710940SMatthew G. Knepley   Level: intermediate
56889710940SMatthew G. Knepley 
56989710940SMatthew G. Knepley .seealso: PetscFECreate(), PetscSpaceGetDimension(), PetscDualSpaceGetDimension()
57089710940SMatthew G. Knepley @*/
57189710940SMatthew G. Knepley PetscErrorCode PetscQuadratureExpandComposite(PetscQuadrature q, PetscInt numSubelements, const PetscReal v0[], const PetscReal jac[], PetscQuadrature *qref)
57289710940SMatthew G. Knepley {
57389710940SMatthew G. Knepley   const PetscReal *points,    *weights;
57489710940SMatthew G. Knepley   PetscReal       *pointsRef, *weightsRef;
575a6b92713SMatthew G. Knepley   PetscInt         dim, Nc, order, npoints, npointsRef, c, p, cp, d, e;
57689710940SMatthew G. Knepley   PetscErrorCode   ierr;
57789710940SMatthew G. Knepley 
57889710940SMatthew G. Knepley   PetscFunctionBegin;
5792cd22861SMatthew G. Knepley   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
58089710940SMatthew G. Knepley   PetscValidPointer(v0, 3);
58189710940SMatthew G. Knepley   PetscValidPointer(jac, 4);
58289710940SMatthew G. Knepley   PetscValidPointer(qref, 5);
58389710940SMatthew G. Knepley   ierr = PetscQuadratureCreate(PETSC_COMM_SELF, qref);CHKERRQ(ierr);
58489710940SMatthew G. Knepley   ierr = PetscQuadratureGetOrder(q, &order);CHKERRQ(ierr);
585a6b92713SMatthew G. Knepley   ierr = PetscQuadratureGetData(q, &dim, &Nc, &npoints, &points, &weights);CHKERRQ(ierr);
58689710940SMatthew G. Knepley   npointsRef = npoints*numSubelements;
58789710940SMatthew G. Knepley   ierr = PetscMalloc1(npointsRef*dim,&pointsRef);CHKERRQ(ierr);
588a6b92713SMatthew G. Knepley   ierr = PetscMalloc1(npointsRef*Nc, &weightsRef);CHKERRQ(ierr);
58989710940SMatthew G. Knepley   for (c = 0; c < numSubelements; ++c) {
59089710940SMatthew G. Knepley     for (p = 0; p < npoints; ++p) {
59189710940SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
59289710940SMatthew G. Knepley         pointsRef[(c*npoints + p)*dim+d] = v0[c*dim+d];
59389710940SMatthew G. Knepley         for (e = 0; e < dim; ++e) {
59489710940SMatthew G. Knepley           pointsRef[(c*npoints + p)*dim+d] += jac[(c*dim + d)*dim+e]*(points[p*dim+e] + 1.0);
59589710940SMatthew G. Knepley         }
59689710940SMatthew G. Knepley       }
59789710940SMatthew G. Knepley       /* Could also use detJ here */
598a6b92713SMatthew G. Knepley       for (cp = 0; cp < Nc; ++cp) weightsRef[(c*npoints+p)*Nc+cp] = weights[p*Nc+cp]/numSubelements;
59989710940SMatthew G. Knepley     }
60089710940SMatthew G. Knepley   }
60189710940SMatthew G. Knepley   ierr = PetscQuadratureSetOrder(*qref, order);CHKERRQ(ierr);
602a6b92713SMatthew G. Knepley   ierr = PetscQuadratureSetData(*qref, dim, Nc, npointsRef, pointsRef, weightsRef);CHKERRQ(ierr);
60389710940SMatthew G. Knepley   PetscFunctionReturn(0);
60489710940SMatthew G. Knepley }
60589710940SMatthew G. Knepley 
60694e21283SToby Isaac /* Compute the coefficients for the Jacobi polynomial recurrence,
60794e21283SToby Isaac  *
60894e21283SToby Isaac  * J^{a,b}_n(x) = (cnm1 + cnm1x * x) * J^{a,b}_{n-1}(x) - cnm2 * J^{a,b}_{n-2}(x).
60994e21283SToby Isaac  */
61094e21283SToby Isaac #define PetscDTJacobiRecurrence_Internal(n,a,b,cnm1,cnm1x,cnm2) \
61194e21283SToby Isaac do {                                                            \
61294e21283SToby Isaac   PetscReal _a = (a);                                           \
61394e21283SToby Isaac   PetscReal _b = (b);                                           \
61494e21283SToby Isaac   PetscReal _n = (n);                                           \
61594e21283SToby Isaac   if (n == 1) {                                                 \
61694e21283SToby Isaac     (cnm1) = (_a-_b) * 0.5;                                     \
61794e21283SToby Isaac     (cnm1x) = (_a+_b+2.)*0.5;                                   \
61894e21283SToby Isaac     (cnm2) = 0.;                                                \
61994e21283SToby Isaac   } else {                                                      \
62094e21283SToby Isaac     PetscReal _2n = _n+_n;                                      \
62194e21283SToby Isaac     PetscReal _d = (_2n*(_n+_a+_b)*(_2n+_a+_b-2));              \
62294e21283SToby Isaac     PetscReal _n1 = (_2n+_a+_b-1.)*(_a*_a-_b*_b);               \
62394e21283SToby Isaac     PetscReal _n1x = (_2n+_a+_b-1.)*(_2n+_a+_b)*(_2n+_a+_b-2);  \
62494e21283SToby Isaac     PetscReal _n2 = 2.*((_n+_a-1.)*(_n+_b-1.)*(_2n+_a+_b));     \
62594e21283SToby Isaac     (cnm1) = _n1 / _d;                                          \
62694e21283SToby Isaac     (cnm1x) = _n1x / _d;                                        \
62794e21283SToby Isaac     (cnm2) = _n2 / _d;                                          \
62894e21283SToby Isaac   }                                                             \
62994e21283SToby Isaac } while (0)
63094e21283SToby Isaac 
631fbdc3dfeSToby Isaac /*@
632fbdc3dfeSToby Isaac   PetscDTJacobiNorm - Compute the weighted L2 norm of a Jacobi polynomial.
633fbdc3dfeSToby Isaac 
634fbdc3dfeSToby Isaac   $\| P^{\alpha,\beta}_n \|_{\alpha,\beta}^2 = \int_{-1}^1 (1 + x)^{\alpha} (1 - x)^{\beta} P^{\alpha,\beta}_n (x)^2 dx.$
635fbdc3dfeSToby Isaac 
636fbdc3dfeSToby Isaac   Input Arguments:
637fbdc3dfeSToby Isaac - alpha - the left exponent > -1
638fbdc3dfeSToby Isaac . beta - the right exponent > -1
639fbdc3dfeSToby Isaac + n - the polynomial degree
640fbdc3dfeSToby Isaac 
641fbdc3dfeSToby Isaac   Output Arguments:
642fbdc3dfeSToby Isaac . norm - the weighted L2 norm
643fbdc3dfeSToby Isaac 
644fbdc3dfeSToby Isaac   Level: beginner
645fbdc3dfeSToby Isaac 
646fbdc3dfeSToby Isaac .seealso: PetscDTJacobiEval()
647fbdc3dfeSToby Isaac @*/
648fbdc3dfeSToby Isaac PetscErrorCode PetscDTJacobiNorm(PetscReal alpha, PetscReal beta, PetscInt n, PetscReal *norm)
649fbdc3dfeSToby Isaac {
650fbdc3dfeSToby Isaac   PetscReal twoab1;
651fbdc3dfeSToby Isaac   PetscReal gr;
652fbdc3dfeSToby Isaac 
653fbdc3dfeSToby Isaac   PetscFunctionBegin;
654fbdc3dfeSToby Isaac   if (alpha <= -1.) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Exponent alpha %g <= -1. invalid\n", (double) alpha);
655fbdc3dfeSToby Isaac   if (beta <= -1.) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Exponent beta %g <= -1. invalid\n", (double) beta);
656fbdc3dfeSToby Isaac   if (n < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "n %D < 0 invalid\n", n);
657fbdc3dfeSToby Isaac   twoab1 = PetscPowReal(2., alpha + beta + 1.);
658fbdc3dfeSToby Isaac #if defined(PETSC_HAVE_LGAMMA)
659fbdc3dfeSToby Isaac   if (!n) {
660fbdc3dfeSToby Isaac     gr = PetscExpReal(PetscLGamma(alpha+1.) + PetscLGamma(beta+1.) - PetscLGamma(alpha+beta+2.));
661fbdc3dfeSToby Isaac   } else {
662fbdc3dfeSToby Isaac     gr = PetscExpReal(PetscLGamma(n+alpha+1.) + PetscLGamma(n+beta+1.) - (PetscLGamma(n+1.) + PetscLGamma(n+alpha+beta+1.))) / (n+n+alpha+beta+1.);
663fbdc3dfeSToby Isaac   }
664fbdc3dfeSToby Isaac #else
665fbdc3dfeSToby Isaac   {
666fbdc3dfeSToby Isaac     PetscInt alphai = (PetscInt) alpha;
667fbdc3dfeSToby Isaac     PetscInt betai = (PetscInt) beta;
668fbdc3dfeSToby Isaac     PetscInt i;
669fbdc3dfeSToby Isaac 
670fbdc3dfeSToby Isaac     gr = n ? (1. / (n+n+alpha+beta+1.)) : 1.;
671fbdc3dfeSToby Isaac     if ((PetscReal) alphai == alpha) {
672fbdc3dfeSToby Isaac       if (!n) {
673fbdc3dfeSToby Isaac         for (i = 0; i < alphai; i++) gr *= (i+1.) / (beta+i+1.);
674fbdc3dfeSToby Isaac         gr /= (alpha+beta+1.);
675fbdc3dfeSToby Isaac       } else {
676fbdc3dfeSToby Isaac         for (i = 0; i < alphai; i++) gr *= (n+i+1.) / (n+beta+i+1.);
677fbdc3dfeSToby Isaac       }
678fbdc3dfeSToby Isaac     } else if ((PetscReal) betai == beta) {
679fbdc3dfeSToby Isaac       if (!n) {
680fbdc3dfeSToby Isaac         for (i = 0; i < betai; i++) gr *= (i+1.) / (alpha+i+2.);
681fbdc3dfeSToby Isaac         gr /= (alpha+beta+1.);
682fbdc3dfeSToby Isaac       } else {
683fbdc3dfeSToby Isaac         for (i = 0; i < betai; i++) gr *= (n+i+1.) / (n+alpha+i+1.);
684fbdc3dfeSToby Isaac       }
685fbdc3dfeSToby Isaac     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"lgamma() - math routine is unavailable.");
686fbdc3dfeSToby Isaac   }
687fbdc3dfeSToby Isaac #endif
688fbdc3dfeSToby Isaac   *norm = PetscSqrtReal(twoab1 * gr);
689fbdc3dfeSToby Isaac   PetscFunctionReturn(0);
690fbdc3dfeSToby Isaac }
691fbdc3dfeSToby Isaac 
69294e21283SToby Isaac static PetscErrorCode PetscDTJacobiEval_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscInt k, const PetscReal *points, PetscInt ndegree, const PetscInt *degrees, PetscReal *p)
69394e21283SToby Isaac {
69494e21283SToby Isaac   PetscReal ak, bk;
69594e21283SToby Isaac   PetscReal abk1;
69694e21283SToby Isaac   PetscInt i,l,maxdegree;
69794e21283SToby Isaac 
69894e21283SToby Isaac   PetscFunctionBegin;
69994e21283SToby Isaac   maxdegree = degrees[ndegree-1] - k;
70094e21283SToby Isaac   ak = a + k;
70194e21283SToby Isaac   bk = b + k;
70294e21283SToby Isaac   abk1 = a + b + k + 1.;
70394e21283SToby Isaac   if (maxdegree < 0) {
70494e21283SToby Isaac     for (i = 0; i < npoints; i++) for (l = 0; l < ndegree; l++) p[i*ndegree+l] = 0.;
70594e21283SToby Isaac     PetscFunctionReturn(0);
70694e21283SToby Isaac   }
70794e21283SToby Isaac   for (i=0; i<npoints; i++) {
70894e21283SToby Isaac     PetscReal pm1,pm2,x;
70994e21283SToby Isaac     PetscReal cnm1, cnm1x, cnm2;
71094e21283SToby Isaac     PetscInt  j,m;
71194e21283SToby Isaac 
71294e21283SToby Isaac     x    = points[i];
71394e21283SToby Isaac     pm2  = 1.;
71494e21283SToby Isaac     PetscDTJacobiRecurrence_Internal(1,ak,bk,cnm1,cnm1x,cnm2);
71594e21283SToby Isaac     pm1 = (cnm1 + cnm1x*x);
71694e21283SToby Isaac     l    = 0;
71794e21283SToby Isaac     while (l < ndegree && degrees[l] - k < 0) {
71894e21283SToby Isaac       p[l++] = 0.;
71994e21283SToby Isaac     }
72094e21283SToby Isaac     while (l < ndegree && degrees[l] - k == 0) {
72194e21283SToby Isaac       p[l] = pm2;
72294e21283SToby Isaac       for (m = 0; m < k; m++) p[l] *= (abk1 + m) * 0.5;
72394e21283SToby Isaac       l++;
72494e21283SToby Isaac     }
72594e21283SToby Isaac     while (l < ndegree && degrees[l] - k == 1) {
72694e21283SToby Isaac       p[l] = pm1;
72794e21283SToby Isaac       for (m = 0; m < k; m++) p[l] *= (abk1 + 1 + m) * 0.5;
72894e21283SToby Isaac       l++;
72994e21283SToby Isaac     }
73094e21283SToby Isaac     for (j=2; j<=maxdegree; j++) {
73194e21283SToby Isaac       PetscReal pp;
73294e21283SToby Isaac 
73394e21283SToby Isaac       PetscDTJacobiRecurrence_Internal(j,ak,bk,cnm1,cnm1x,cnm2);
73494e21283SToby Isaac       pp   = (cnm1 + cnm1x*x)*pm1 - cnm2*pm2;
73594e21283SToby Isaac       pm2  = pm1;
73694e21283SToby Isaac       pm1  = pp;
73794e21283SToby Isaac       while (l < ndegree && degrees[l] - k == j) {
73894e21283SToby Isaac         p[l] = pp;
73994e21283SToby Isaac         for (m = 0; m < k; m++) p[l] *= (abk1 + j + m) * 0.5;
74094e21283SToby Isaac         l++;
74194e21283SToby Isaac       }
74294e21283SToby Isaac     }
74394e21283SToby Isaac     p += ndegree;
74494e21283SToby Isaac   }
74594e21283SToby Isaac   PetscFunctionReturn(0);
74694e21283SToby Isaac }
74794e21283SToby Isaac 
74837045ce4SJed Brown /*@
749fbdc3dfeSToby Isaac   PetscDTJacobiEvalJet - Evaluate the jet (function and derivatives) of the Jacobi polynomials basis up to a given degree.  The Jacobi polynomials with indices $\alpha$ and $\beta$ are orthogonal with respect to the weighted inner product $\langle f, g \rangle = \int_{-1}^1 (1+x)^{\alpha} (1-x)^{\beta) f(x) g(x) dx$.
750fbdc3dfeSToby Isaac 
751fbdc3dfeSToby Isaac   Input Arguments:
752fbdc3dfeSToby Isaac + alpha - the left exponent of the weight
753fbdc3dfeSToby Isaac . beta - the right exponetn of the weight
754fbdc3dfeSToby Isaac . npoints - the number of points to evaluate the polynomials at
755fbdc3dfeSToby Isaac . points - [npoints] array of point coordinates
756fbdc3dfeSToby Isaac . degree - the maximm degree polynomial space to evaluate, (degree + 1) will be evaluated total.
757fbdc3dfeSToby Isaac - k - the maximum derivative to evaluate in the jet, (k + 1) will be evaluated total.
758fbdc3dfeSToby Isaac 
759fbdc3dfeSToby Isaac   Output Argments:
760fbdc3dfeSToby Isaac - p - an array containing the evaluations of the Jacobi polynomials's jets on the points.  the size is (degree + 1) x
761fbdc3dfeSToby Isaac   (k + 1) x npoints, which also describes the order of the dimensions of this three-dimensional array: the first
762fbdc3dfeSToby Isaac   (slowest varying) dimension is polynomial degree; the second dimension is derivative order; the third (fastest
763fbdc3dfeSToby Isaac   varying) dimension is the index of the evaluation point.
764fbdc3dfeSToby Isaac 
765fbdc3dfeSToby Isaac   Level: advanced
766fbdc3dfeSToby Isaac 
767fbdc3dfeSToby Isaac .seealso: PetscDTJacobiEval(), PetscDTPKDEvalJet()
768fbdc3dfeSToby Isaac @*/
769fbdc3dfeSToby Isaac PetscErrorCode PetscDTJacobiEvalJet(PetscReal alpha, PetscReal beta, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt k, PetscReal p[])
770fbdc3dfeSToby Isaac {
771fbdc3dfeSToby Isaac   PetscInt        i, j, l;
772fbdc3dfeSToby Isaac   PetscInt       *degrees;
773fbdc3dfeSToby Isaac   PetscReal      *psingle;
774fbdc3dfeSToby Isaac   PetscErrorCode  ierr;
775fbdc3dfeSToby Isaac 
776fbdc3dfeSToby Isaac   PetscFunctionBegin;
777fbdc3dfeSToby Isaac   if (degree == 0) {
778fbdc3dfeSToby Isaac     PetscInt zero = 0;
779fbdc3dfeSToby Isaac 
780fbdc3dfeSToby Isaac     for (i = 0; i <= k; i++) {
781fbdc3dfeSToby Isaac       ierr = PetscDTJacobiEval_Internal(npoints, alpha, beta, i, points, 1, &zero, &p[i*npoints]);CHKERRQ(ierr);
782fbdc3dfeSToby Isaac     }
783fbdc3dfeSToby Isaac     PetscFunctionReturn(0);
784fbdc3dfeSToby Isaac   }
785fbdc3dfeSToby Isaac   ierr = PetscMalloc1(degree + 1, &degrees);CHKERRQ(ierr);
786fbdc3dfeSToby Isaac   ierr = PetscMalloc1((degree + 1) * npoints, &psingle);CHKERRQ(ierr);
787fbdc3dfeSToby Isaac   for (i = 0; i <= degree; i++) degrees[i] = i;
788fbdc3dfeSToby Isaac   for (i = 0; i <= k; i++) {
789fbdc3dfeSToby Isaac     ierr = PetscDTJacobiEval_Internal(npoints, alpha, beta, i, points, degree + 1, degrees, psingle);CHKERRQ(ierr);
790fbdc3dfeSToby Isaac     for (j = 0; j <= degree; j++) {
791fbdc3dfeSToby Isaac       for (l = 0; l < npoints; l++) {
792fbdc3dfeSToby Isaac         p[(j * (k + 1) + i) * npoints + l] = psingle[l * (degree + 1) + j];
793fbdc3dfeSToby Isaac       }
794fbdc3dfeSToby Isaac     }
795fbdc3dfeSToby Isaac   }
796fbdc3dfeSToby Isaac   ierr = PetscFree(psingle);CHKERRQ(ierr);
797fbdc3dfeSToby Isaac   ierr = PetscFree(degrees);CHKERRQ(ierr);
798fbdc3dfeSToby Isaac   PetscFunctionReturn(0);
799fbdc3dfeSToby Isaac }
800fbdc3dfeSToby Isaac 
801fbdc3dfeSToby Isaac /*@
80294e21283SToby Isaac    PetscDTJacobiEval - evaluate Jacobi polynomials for the weight function $(1.+x)^{\alpha} (1.-x)^{\beta}$
80394e21283SToby Isaac                        at points
80494e21283SToby Isaac 
80594e21283SToby Isaac    Not Collective
80694e21283SToby Isaac 
80794e21283SToby Isaac    Input Arguments:
80894e21283SToby Isaac +  npoints - number of spatial points to evaluate at
80994e21283SToby Isaac .  alpha - the left exponent > -1
81094e21283SToby Isaac .  beta - the right exponent > -1
81194e21283SToby Isaac .  points - array of locations to evaluate at
81294e21283SToby Isaac .  ndegree - number of basis degrees to evaluate
81394e21283SToby Isaac -  degrees - sorted array of degrees to evaluate
81494e21283SToby Isaac 
81594e21283SToby Isaac    Output Arguments:
81694e21283SToby Isaac +  B - row-oriented basis evaluation matrix B[point*ndegree + degree] (dimension npoints*ndegrees, allocated by caller) (or NULL)
81794e21283SToby Isaac .  D - row-oriented derivative evaluation matrix (or NULL)
81894e21283SToby Isaac -  D2 - row-oriented second derivative evaluation matrix (or NULL)
81994e21283SToby Isaac 
82094e21283SToby Isaac    Level: intermediate
82194e21283SToby Isaac 
82294e21283SToby Isaac .seealso: PetscDTGaussQuadrature()
82394e21283SToby Isaac @*/
82494e21283SToby Isaac PetscErrorCode PetscDTJacobiEval(PetscInt npoints,PetscReal alpha, PetscReal beta, const PetscReal *points,PetscInt ndegree,const PetscInt *degrees,PetscReal *B,PetscReal *D,PetscReal *D2)
82594e21283SToby Isaac {
82694e21283SToby Isaac   PetscErrorCode ierr;
82794e21283SToby Isaac 
82894e21283SToby Isaac   PetscFunctionBegin;
82994e21283SToby Isaac   if (alpha <= -1.) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"alpha must be > -1.");
83094e21283SToby Isaac   if (beta <= -1.) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"beta must be > -1.");
83194e21283SToby Isaac   if (!npoints || !ndegree) PetscFunctionReturn(0);
83294e21283SToby Isaac   if (B)  {ierr = PetscDTJacobiEval_Internal(npoints, alpha, beta, 0, points, ndegree, degrees, B);CHKERRQ(ierr);}
83394e21283SToby Isaac   if (D)  {ierr = PetscDTJacobiEval_Internal(npoints, alpha, beta, 1, points, ndegree, degrees, D);CHKERRQ(ierr);}
83494e21283SToby Isaac   if (D2) {ierr = PetscDTJacobiEval_Internal(npoints, alpha, beta, 2, points, ndegree, degrees, D2);CHKERRQ(ierr);}
83594e21283SToby Isaac   PetscFunctionReturn(0);
83694e21283SToby Isaac }
83794e21283SToby Isaac 
83894e21283SToby Isaac /*@
83994e21283SToby Isaac    PetscDTLegendreEval - evaluate Legendre polynomials at points
84037045ce4SJed Brown 
84137045ce4SJed Brown    Not Collective
84237045ce4SJed Brown 
84337045ce4SJed Brown    Input Arguments:
84437045ce4SJed Brown +  npoints - number of spatial points to evaluate at
84537045ce4SJed Brown .  points - array of locations to evaluate at
84637045ce4SJed Brown .  ndegree - number of basis degrees to evaluate
84737045ce4SJed Brown -  degrees - sorted array of degrees to evaluate
84837045ce4SJed Brown 
84937045ce4SJed Brown    Output Arguments:
8500298fd71SBarry Smith +  B - row-oriented basis evaluation matrix B[point*ndegree + degree] (dimension npoints*ndegrees, allocated by caller) (or NULL)
8510298fd71SBarry Smith .  D - row-oriented derivative evaluation matrix (or NULL)
8520298fd71SBarry Smith -  D2 - row-oriented second derivative evaluation matrix (or NULL)
85337045ce4SJed Brown 
85437045ce4SJed Brown    Level: intermediate
85537045ce4SJed Brown 
85637045ce4SJed Brown .seealso: PetscDTGaussQuadrature()
85737045ce4SJed Brown @*/
85837045ce4SJed Brown PetscErrorCode PetscDTLegendreEval(PetscInt npoints,const PetscReal *points,PetscInt ndegree,const PetscInt *degrees,PetscReal *B,PetscReal *D,PetscReal *D2)
85937045ce4SJed Brown {
86094e21283SToby Isaac   PetscErrorCode ierr;
86137045ce4SJed Brown 
86237045ce4SJed Brown   PetscFunctionBegin;
86394e21283SToby Isaac   ierr = PetscDTJacobiEval(npoints, 0., 0., points, ndegree, degrees, B, D, D2);CHKERRQ(ierr);
86437045ce4SJed Brown   PetscFunctionReturn(0);
86537045ce4SJed Brown }
86637045ce4SJed Brown 
867fbdc3dfeSToby Isaac /*@
868fbdc3dfeSToby Isaac   PetscDTIndexToGradedOrder - convert an index into a tuple of monomial degrees in a graded order (that is, if the degree sum of tuple x is less than the degree sum of tuple y, then the index of x is smaller than the index of y)
869fbdc3dfeSToby Isaac 
870fbdc3dfeSToby Isaac   Input Parameters:
871fbdc3dfeSToby Isaac + len - the desired length of the degree tuple
872fbdc3dfeSToby Isaac - index - the index to convert: should be >= 0
873fbdc3dfeSToby Isaac 
874fbdc3dfeSToby Isaac   Output Parameter:
875fbdc3dfeSToby Isaac . degtup - will be filled with a tuple of degrees
876fbdc3dfeSToby Isaac 
877fbdc3dfeSToby Isaac   Level: beginner
878fbdc3dfeSToby Isaac 
879fbdc3dfeSToby Isaac   Note: for two tuples x and y with the same degree sum, partial degree sums over the final elements of the tuples
880fbdc3dfeSToby Isaac   acts as a tiebreaker.  For example, (2, 1, 1) and (1, 2, 1) have the same degree sum, but the degree sum over the
881fbdc3dfeSToby Isaac   last two elements is smaller for the former, so (2, 1, 1) < (1, 2, 1).
882fbdc3dfeSToby Isaac 
883fbdc3dfeSToby Isaac .seealso: PetscDTGradedOrderToIndex()
884fbdc3dfeSToby Isaac @*/
885fbdc3dfeSToby Isaac PetscErrorCode PetscDTIndexToGradedOrder(PetscInt len, PetscInt index, PetscInt degtup[])
886fbdc3dfeSToby Isaac {
887fbdc3dfeSToby Isaac   PetscInt i, total;
888fbdc3dfeSToby Isaac   PetscInt sum;
889fbdc3dfeSToby Isaac 
890fbdc3dfeSToby Isaac   PetscFunctionBeginHot;
891fbdc3dfeSToby Isaac   if (len < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative");
892fbdc3dfeSToby Isaac   if (index < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index must be non-negative");
893fbdc3dfeSToby Isaac   total = 1;
894fbdc3dfeSToby Isaac   sum = 0;
895fbdc3dfeSToby Isaac   while (index >= total) {
896fbdc3dfeSToby Isaac     index -= total;
897fbdc3dfeSToby Isaac     total = (total * (len + sum)) / (sum + 1);
898fbdc3dfeSToby Isaac     sum++;
899fbdc3dfeSToby Isaac   }
900fbdc3dfeSToby Isaac   for (i = 0; i < len; i++) {
901fbdc3dfeSToby Isaac     PetscInt c;
902fbdc3dfeSToby Isaac 
903fbdc3dfeSToby Isaac     degtup[i] = sum;
904fbdc3dfeSToby Isaac     for (c = 0, total = 1; c < sum; c++) {
905fbdc3dfeSToby Isaac       /* going into the loop, total is the number of way to have a tuple of sum exactly c with length len - 1 - i */
906fbdc3dfeSToby Isaac       if (index < total) break;
907fbdc3dfeSToby Isaac       index -= total;
908fbdc3dfeSToby Isaac       total = (total * (len - 1 - i + c)) / (c + 1);
909fbdc3dfeSToby Isaac       degtup[i]--;
910fbdc3dfeSToby Isaac     }
911fbdc3dfeSToby Isaac     sum -= degtup[i];
912fbdc3dfeSToby Isaac   }
913fbdc3dfeSToby Isaac   PetscFunctionReturn(0);
914fbdc3dfeSToby Isaac }
915fbdc3dfeSToby Isaac 
916fbdc3dfeSToby Isaac /*@
917fbdc3dfeSToby Isaac   PetscDTGradedOrderToIndex - convert a tuple into an index in a graded order, the inverse of PetscDTIndexToGradedOrder().
918fbdc3dfeSToby Isaac 
919fbdc3dfeSToby Isaac   Input Parameters:
920fbdc3dfeSToby Isaac + len - the length of the degree tuple
921fbdc3dfeSToby Isaac - degtup - tuple with this length
922fbdc3dfeSToby Isaac 
923fbdc3dfeSToby Isaac   Output Parameter:
924fbdc3dfeSToby Isaac . index - index in graded order: >= 0
925fbdc3dfeSToby Isaac 
926fbdc3dfeSToby Isaac   Level: Beginner
927fbdc3dfeSToby Isaac 
928fbdc3dfeSToby Isaac   Note: for two tuples x and y with the same degree sum, partial degree sums over the final elements of the tuples
929fbdc3dfeSToby Isaac   acts as a tiebreaker.  For example, (2, 1, 1) and (1, 2, 1) have the same degree sum, but the degree sum over the
930fbdc3dfeSToby Isaac   last two elements is smaller for the former, so (2, 1, 1) < (1, 2, 1).
931fbdc3dfeSToby Isaac 
932fbdc3dfeSToby Isaac .seealso: PetscDTIndexToGradedOrder()
933fbdc3dfeSToby Isaac @*/
934fbdc3dfeSToby Isaac PetscErrorCode PetscDTGradedOrderToIndex(PetscInt len, const PetscInt degtup[], PetscInt *index)
935fbdc3dfeSToby Isaac {
936fbdc3dfeSToby Isaac   PetscInt i, idx, sum, total;
937fbdc3dfeSToby Isaac 
938fbdc3dfeSToby Isaac   PetscFunctionBeginHot;
939fbdc3dfeSToby Isaac   if (len < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative");
940fbdc3dfeSToby Isaac   for (i = 0, sum = 0; i < len; i++) sum += degtup[i];
941fbdc3dfeSToby Isaac   idx = 0;
942fbdc3dfeSToby Isaac   total = 1;
943fbdc3dfeSToby Isaac   for (i = 0; i < sum; i++) {
944fbdc3dfeSToby Isaac     idx += total;
945fbdc3dfeSToby Isaac     total = (total * (len + i)) / (i + 1);
946fbdc3dfeSToby Isaac   }
947fbdc3dfeSToby Isaac   for (i = 0; i < len - 1; i++) {
948fbdc3dfeSToby Isaac     PetscInt c;
949fbdc3dfeSToby Isaac 
950fbdc3dfeSToby Isaac     total = 1;
951fbdc3dfeSToby Isaac     sum -= degtup[i];
952fbdc3dfeSToby Isaac     for (c = 0; c < sum; c++) {
953fbdc3dfeSToby Isaac       idx += total;
954fbdc3dfeSToby Isaac       total = (total * (len - 1 - i + c)) / (c + 1);
955fbdc3dfeSToby Isaac     }
956fbdc3dfeSToby Isaac   }
957fbdc3dfeSToby Isaac   *index = idx;
958fbdc3dfeSToby Isaac   PetscFunctionReturn(0);
959fbdc3dfeSToby Isaac }
960fbdc3dfeSToby Isaac 
961e3aa2e09SToby Isaac static PetscBool PKDCite = PETSC_FALSE;
962e3aa2e09SToby Isaac const char       PKDCitation[] = "@article{Kirby2010,\n"
963e3aa2e09SToby Isaac                                  "  title={Singularity-free evaluation of collapsed-coordinate orthogonal polynomials},\n"
964e3aa2e09SToby Isaac                                  "  author={Kirby, Robert C},\n"
965e3aa2e09SToby Isaac                                  "  journal={ACM Transactions on Mathematical Software (TOMS)},\n"
966e3aa2e09SToby Isaac                                  "  volume={37},\n"
967e3aa2e09SToby Isaac                                  "  number={1},\n"
968e3aa2e09SToby Isaac                                  "  pages={1--16},\n"
969e3aa2e09SToby Isaac                                  "  year={2010},\n"
970e3aa2e09SToby Isaac                                  "  publisher={ACM New York, NY, USA}\n}\n";
971e3aa2e09SToby Isaac 
972fbdc3dfeSToby Isaac /*@
973fbdc3dfeSToby Isaac   PetscDTPKDEvalJet - Evaluate the jet (function and derivatives) of the Prioriol-Koornwinder-Dubiner (PKD) basis for
974fbdc3dfeSToby Isaac   the space of polynomials up to a given degree.  The PKD basis is L2-orthonormal on the biunit simplex (which is used
975fbdc3dfeSToby Isaac   as the reference element for finite elements in PETSc), which makes it a stable basis to use for evaluating
976fbdc3dfeSToby Isaac   polynomials in that domain.
977fbdc3dfeSToby Isaac 
978fbdc3dfeSToby Isaac   Input Arguments:
979fbdc3dfeSToby Isaac + dim - the number of variables in the multivariate polynomials
980fbdc3dfeSToby Isaac . npoints - the number of points to evaluate the polynomials at
981fbdc3dfeSToby Isaac . points - [npoints x dim] array of point coordinates
982fbdc3dfeSToby Isaac . degree - the degree (sum of degrees on the variables in a monomial) of the polynomial space to evaluate.  There are ((dim + degree) choose dim) polynomials in this space.
983fbdc3dfeSToby Isaac - k - the maximum order partial derivative to evaluate in the jet.  There are (dim + k choose dim) partial derivatives
984fbdc3dfeSToby Isaac   in the jet.  Choosing k = 0 means to evaluate just the function and no derivatives
985fbdc3dfeSToby Isaac 
986fbdc3dfeSToby Isaac   Output Argments:
987fbdc3dfeSToby Isaac - p - an array containing the evaluations of the PKD polynomials' jets on the points.  The size is ((dim + degree)
988fbdc3dfeSToby Isaac   choose dim) x ((dim + k) choose dim) x npoints, which also describes the order of the dimensions of this
989fbdc3dfeSToby Isaac   three-dimensional array: the first (slowest varying) dimension is basis function index; the second dimension is jet
990fbdc3dfeSToby Isaac   index; the third (fastest varying) dimension is the index of the evaluation point.
991fbdc3dfeSToby Isaac 
992fbdc3dfeSToby Isaac   Level: advanced
993fbdc3dfeSToby Isaac 
994fbdc3dfeSToby Isaac   Note: The ordering of the basis functions, and the ordering of the derivatives in the jet, both follow the graded
995fbdc3dfeSToby Isaac   ordering of PetscDTIndexToGradedOrder() and PetscDTGradedOrderToIndex().  For example, in 3D, the polynomial with
996fbdc3dfeSToby Isaac   leading monomial x^3,y^1,z^2, which as degree tuple (2,0,1), which by PetscDTGradedOrderToIndex() has index 12 (it is the 13th basis function in the space);
997fbdc3dfeSToby Isaac   the partial derivative $\partial_x \partial_z$ has order tuple (1,0,1), appears at index 6 in the jet (it is the 7th partial derivative in the jet).
998fbdc3dfeSToby Isaac 
999e3aa2e09SToby Isaac   The implementation uses Kirby's singularity-free evaluation algorithm, https://doi.org/10.1145/1644001.1644006.
1000e3aa2e09SToby Isaac 
1001fbdc3dfeSToby Isaac .seealso: PetscDTGradedOrderToIndex(), PetscDTIndexToGradedOrder(), PetscDTJacobiEvalJet()
1002fbdc3dfeSToby Isaac @*/
1003fbdc3dfeSToby Isaac PetscErrorCode PetscDTPKDEvalJet(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt k, PetscReal p[])
1004fbdc3dfeSToby Isaac {
1005fbdc3dfeSToby Isaac   PetscInt        degidx, kidx, d, pt;
1006fbdc3dfeSToby Isaac   PetscInt        Nk, Ndeg;
1007fbdc3dfeSToby Isaac   PetscInt       *ktup, *degtup;
1008fbdc3dfeSToby Isaac   PetscReal      *scales, initscale, scaleexp;
1009fbdc3dfeSToby Isaac   PetscErrorCode  ierr;
1010fbdc3dfeSToby Isaac 
1011fbdc3dfeSToby Isaac   PetscFunctionBegin;
1012e3aa2e09SToby Isaac   ierr = PetscCitationsRegister(PKDCitation, &PKDCite);CHKERRQ(ierr);
1013fbdc3dfeSToby Isaac   ierr = PetscDTBinomialInt(dim + k, k, &Nk);CHKERRQ(ierr);
1014fbdc3dfeSToby Isaac   ierr = PetscDTBinomialInt(degree + dim, degree, &Ndeg);CHKERRQ(ierr);
1015fbdc3dfeSToby Isaac   ierr = PetscMalloc2(dim, &degtup, dim, &ktup);CHKERRQ(ierr);
1016fbdc3dfeSToby Isaac   ierr = PetscMalloc1(Ndeg, &scales);CHKERRQ(ierr);
1017fbdc3dfeSToby Isaac   initscale = 1.;
1018fbdc3dfeSToby Isaac   if (dim > 1) {
1019fbdc3dfeSToby Isaac     ierr = PetscDTBinomial(dim,2,&scaleexp);CHKERRQ(ierr);
1020fbdc3dfeSToby Isaac     initscale = PetscPowReal(2.,scaleexp*0.5);CHKERRQ(ierr);
1021fbdc3dfeSToby Isaac   }
1022fbdc3dfeSToby Isaac   for (degidx = 0; degidx < Ndeg; degidx++) {
1023fbdc3dfeSToby Isaac     PetscInt e, i;
1024fbdc3dfeSToby Isaac     PetscInt m1idx = -1, m2idx = -1;
1025fbdc3dfeSToby Isaac     PetscInt n;
1026fbdc3dfeSToby Isaac     PetscInt degsum;
1027fbdc3dfeSToby Isaac     PetscReal alpha;
1028fbdc3dfeSToby Isaac     PetscReal cnm1, cnm1x, cnm2;
1029fbdc3dfeSToby Isaac     PetscReal norm;
1030fbdc3dfeSToby Isaac 
1031fbdc3dfeSToby Isaac     ierr = PetscDTIndexToGradedOrder(dim, degidx, degtup);CHKERRQ(ierr);
1032fbdc3dfeSToby Isaac     for (d = dim - 1; d >= 0; d--) if (degtup[d]) break;
1033fbdc3dfeSToby Isaac     if (d < 0) { /* constant is 1 everywhere, all derivatives are zero */
1034fbdc3dfeSToby Isaac       scales[degidx] = initscale;
1035fbdc3dfeSToby Isaac       for (e = 0; e < dim; e++) {
1036fbdc3dfeSToby Isaac         ierr = PetscDTJacobiNorm(e,0.,0,&norm);CHKERRQ(ierr);
1037fbdc3dfeSToby Isaac         scales[degidx] /= norm;
1038fbdc3dfeSToby Isaac       }
1039fbdc3dfeSToby Isaac       for (i = 0; i < npoints; i++) p[degidx * Nk * npoints + i] = 1.;
1040fbdc3dfeSToby Isaac       for (i = 0; i < (Nk - 1) * npoints; i++) p[(degidx * Nk + 1) * npoints + i] = 0.;
1041fbdc3dfeSToby Isaac       continue;
1042fbdc3dfeSToby Isaac     }
1043fbdc3dfeSToby Isaac     n = degtup[d];
1044fbdc3dfeSToby Isaac     degtup[d]--;
1045fbdc3dfeSToby Isaac     ierr = PetscDTGradedOrderToIndex(dim, degtup, &m1idx);CHKERRQ(ierr);
1046fbdc3dfeSToby Isaac     if (degtup[d] > 0) {
1047fbdc3dfeSToby Isaac       degtup[d]--;
1048fbdc3dfeSToby Isaac       ierr = PetscDTGradedOrderToIndex(dim, degtup, &m2idx);CHKERRQ(ierr);
1049fbdc3dfeSToby Isaac       degtup[d]++;
1050fbdc3dfeSToby Isaac     }
1051fbdc3dfeSToby Isaac     degtup[d]++;
1052fbdc3dfeSToby Isaac     for (e = 0, degsum = 0; e < d; e++) degsum += degtup[e];
1053fbdc3dfeSToby Isaac     alpha = 2 * degsum + d;
1054fbdc3dfeSToby Isaac     PetscDTJacobiRecurrence_Internal(n,alpha,0.,cnm1,cnm1x,cnm2);
1055fbdc3dfeSToby Isaac 
1056fbdc3dfeSToby Isaac 
1057fbdc3dfeSToby Isaac     scales[degidx] = initscale;
1058fbdc3dfeSToby Isaac     for (e = 0, degsum = 0; e < dim; e++) {
1059fbdc3dfeSToby Isaac       PetscInt  f;
1060fbdc3dfeSToby Isaac       PetscReal ealpha;
1061fbdc3dfeSToby Isaac       PetscReal enorm;
1062fbdc3dfeSToby Isaac 
1063fbdc3dfeSToby Isaac       ealpha = 2 * degsum + e;
1064fbdc3dfeSToby Isaac       for (f = 0; f < degsum; f++) scales[degidx] *= 2.;
1065fbdc3dfeSToby Isaac       ierr = PetscDTJacobiNorm(ealpha,0.,degtup[e],&enorm);CHKERRQ(ierr);
1066fbdc3dfeSToby Isaac       scales[degidx] /= enorm;
1067fbdc3dfeSToby Isaac       degsum += degtup[e];
1068fbdc3dfeSToby Isaac     }
1069fbdc3dfeSToby Isaac 
1070fbdc3dfeSToby Isaac     for (pt = 0; pt < npoints; pt++) {
1071fbdc3dfeSToby Isaac       /* compute the multipliers */
1072fbdc3dfeSToby Isaac       PetscReal thetanm1, thetanm1x, thetanm2;
1073fbdc3dfeSToby Isaac 
1074fbdc3dfeSToby Isaac       thetanm1x = dim - (d+1) + 2.*points[pt * dim + d];
1075fbdc3dfeSToby Isaac       for (e = d+1; e < dim; e++) thetanm1x += points[pt * dim + e];
1076fbdc3dfeSToby Isaac       thetanm1x *= 0.5;
1077fbdc3dfeSToby Isaac       thetanm1 = (2. - (dim-(d+1)));
1078fbdc3dfeSToby Isaac       for (e = d+1; e < dim; e++) thetanm1 -= points[pt * dim + e];
1079fbdc3dfeSToby Isaac       thetanm1 *= 0.5;
1080fbdc3dfeSToby Isaac       thetanm2 = thetanm1 * thetanm1;
1081fbdc3dfeSToby Isaac 
1082fbdc3dfeSToby Isaac       for (kidx = 0; kidx < Nk; kidx++) {
1083fbdc3dfeSToby Isaac         PetscInt f;
1084fbdc3dfeSToby Isaac 
1085fbdc3dfeSToby Isaac         ierr = PetscDTIndexToGradedOrder(dim, kidx, ktup);CHKERRQ(ierr);
1086fbdc3dfeSToby Isaac         /* first sum in the same derivative terms */
1087fbdc3dfeSToby Isaac         p[(degidx * Nk + kidx) * npoints + pt] = (cnm1 * thetanm1 + cnm1x * thetanm1x) * p[(m1idx * Nk + kidx) * npoints + pt];
1088fbdc3dfeSToby Isaac         if (m2idx >= 0) {
1089fbdc3dfeSToby Isaac           p[(degidx * Nk + kidx) * npoints + pt] -= cnm2 * thetanm2 * p[(m2idx * Nk + kidx) * npoints + pt];
1090fbdc3dfeSToby Isaac         }
1091fbdc3dfeSToby Isaac 
1092fbdc3dfeSToby Isaac         for (f = d; f < dim; f++) {
1093fbdc3dfeSToby Isaac           PetscInt km1idx, mplty = ktup[f];
1094fbdc3dfeSToby Isaac 
1095fbdc3dfeSToby Isaac           if (!mplty) continue;
1096fbdc3dfeSToby Isaac           ktup[f]--;
1097fbdc3dfeSToby Isaac           ierr = PetscDTGradedOrderToIndex(dim, ktup, &km1idx);CHKERRQ(ierr);
1098fbdc3dfeSToby Isaac 
1099fbdc3dfeSToby Isaac           /* the derivative of  cnm1x * thetanm1x  wrt x variable f is 0.5 * cnm1x if f > d otherwise it is cnm1x */
1100fbdc3dfeSToby Isaac           /* the derivative of  cnm1  * thetanm1   wrt x variable f is 0 if f == d, otherwise it is -0.5 * cnm1 */
1101fbdc3dfeSToby Isaac           /* the derivative of -cnm2  * thetanm2   wrt x variable f is 0 if f == d, otherwise it is cnm2 * thetanm1 */
1102fbdc3dfeSToby Isaac           if (f > d) {
1103fbdc3dfeSToby Isaac             PetscInt f2;
1104fbdc3dfeSToby Isaac 
1105fbdc3dfeSToby Isaac             p[(degidx * Nk + kidx) * npoints + pt] += mplty * 0.5 * (cnm1x - cnm1) * p[(m1idx * Nk + km1idx) * npoints + pt];
1106fbdc3dfeSToby Isaac             if (m2idx >= 0) {
1107fbdc3dfeSToby Isaac               p[(degidx * Nk + kidx) * npoints + pt] += mplty * cnm2 * thetanm1 * p[(m2idx * Nk + km1idx) * npoints + pt];
1108fbdc3dfeSToby Isaac               /* second derivatives of -cnm2  * thetanm2   wrt x variable f,f2 is like - 0.5 * cnm2 */
1109fbdc3dfeSToby Isaac               for (f2 = f; f2 < dim; f2++) {
1110fbdc3dfeSToby Isaac                 PetscInt km2idx, mplty2 = ktup[f2];
1111fbdc3dfeSToby Isaac                 PetscInt factor;
1112fbdc3dfeSToby Isaac 
1113fbdc3dfeSToby Isaac                 if (!mplty2) continue;
1114fbdc3dfeSToby Isaac                 ktup[f2]--;
1115fbdc3dfeSToby Isaac                 ierr = PetscDTGradedOrderToIndex(dim, ktup, &km2idx);CHKERRQ(ierr);
1116fbdc3dfeSToby Isaac 
1117fbdc3dfeSToby Isaac                 factor = mplty * mplty2;
1118fbdc3dfeSToby Isaac                 if (f == f2) factor /= 2;
1119fbdc3dfeSToby Isaac                 p[(degidx * Nk + kidx) * npoints + pt] -= 0.5 * factor * cnm2 * p[(m2idx * Nk + km2idx) * npoints + pt];
1120fbdc3dfeSToby Isaac                 ktup[f2]++;
1121fbdc3dfeSToby Isaac               }
11223034baaeSToby Isaac             }
1123fbdc3dfeSToby Isaac           } else {
1124fbdc3dfeSToby Isaac             p[(degidx * Nk + kidx) * npoints + pt] += mplty * cnm1x * p[(m1idx * Nk + km1idx) * npoints + pt];
1125fbdc3dfeSToby Isaac           }
1126fbdc3dfeSToby Isaac           ktup[f]++;
1127fbdc3dfeSToby Isaac         }
1128fbdc3dfeSToby Isaac       }
1129fbdc3dfeSToby Isaac     }
1130fbdc3dfeSToby Isaac   }
1131fbdc3dfeSToby Isaac   for (degidx = 0; degidx < Ndeg; degidx++) {
1132fbdc3dfeSToby Isaac     PetscReal scale = scales[degidx];
1133fbdc3dfeSToby Isaac     PetscInt i;
1134fbdc3dfeSToby Isaac 
1135fbdc3dfeSToby Isaac     for (i = 0; i < Nk * npoints; i++) p[degidx*Nk*npoints + i] *= scale;
1136fbdc3dfeSToby Isaac   }
1137fbdc3dfeSToby Isaac   ierr = PetscFree(scales);CHKERRQ(ierr);
1138fbdc3dfeSToby Isaac   ierr = PetscFree2(degtup, ktup);CHKERRQ(ierr);
1139fbdc3dfeSToby Isaac   PetscFunctionReturn(0);
1140fbdc3dfeSToby Isaac }
1141fbdc3dfeSToby Isaac 
1142e6a796c3SToby Isaac /* solve the symmetric tridiagonal eigenvalue system, writing the eigenvalues into eigs and the eigenvectors into V
1143e6a796c3SToby Isaac  * with lds n; diag and subdiag are overwritten */
1144e6a796c3SToby Isaac static PetscErrorCode PetscDTSymmetricTridiagonalEigensolve(PetscInt n, PetscReal diag[], PetscReal subdiag[],
1145e6a796c3SToby Isaac                                                             PetscReal eigs[], PetscScalar V[])
1146e6a796c3SToby Isaac {
1147e6a796c3SToby Isaac   char jobz = 'V'; /* eigenvalues and eigenvectors */
1148e6a796c3SToby Isaac   char range = 'A'; /* all eigenvalues will be found */
1149e6a796c3SToby Isaac   PetscReal VL = 0.; /* ignored because range is 'A' */
1150e6a796c3SToby Isaac   PetscReal VU = 0.; /* ignored because range is 'A' */
1151e6a796c3SToby Isaac   PetscBLASInt IL = 0; /* ignored because range is 'A' */
1152e6a796c3SToby Isaac   PetscBLASInt IU = 0; /* ignored because range is 'A' */
1153e6a796c3SToby Isaac   PetscReal abstol = 0.; /* unused */
1154e6a796c3SToby Isaac   PetscBLASInt bn, bm, ldz; /* bm will equal bn on exit */
1155e6a796c3SToby Isaac   PetscBLASInt *isuppz;
1156e6a796c3SToby Isaac   PetscBLASInt lwork, liwork;
1157e6a796c3SToby Isaac   PetscReal workquery;
1158e6a796c3SToby Isaac   PetscBLASInt  iworkquery;
1159e6a796c3SToby Isaac   PetscBLASInt *iwork;
1160e6a796c3SToby Isaac   PetscBLASInt info;
1161e6a796c3SToby Isaac   PetscReal *work = NULL;
1162e6a796c3SToby Isaac   PetscErrorCode ierr;
1163e6a796c3SToby Isaac 
1164e6a796c3SToby Isaac   PetscFunctionBegin;
1165e6a796c3SToby Isaac #if !defined(PETSCDTGAUSSIANQUADRATURE_EIG)
1166e6a796c3SToby Isaac   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "A LAPACK symmetric tridiagonal eigensolver could not be found");
1167e6a796c3SToby Isaac #endif
1168e6a796c3SToby Isaac   ierr = PetscBLASIntCast(n, &bn);CHKERRQ(ierr);
1169e6a796c3SToby Isaac   ierr = PetscBLASIntCast(n, &ldz);CHKERRQ(ierr);
1170e6a796c3SToby Isaac #if !defined(PETSC_MISSING_LAPACK_STEGR)
1171e6a796c3SToby Isaac   ierr = PetscMalloc1(2 * n, &isuppz);CHKERRQ(ierr);
1172e6a796c3SToby Isaac   lwork = -1;
1173e6a796c3SToby Isaac   liwork = -1;
1174e6a796c3SToby Isaac   PetscStackCallBLAS("LAPACKstegr",LAPACKstegr_(&jobz,&range,&bn,diag,subdiag,&VL,&VU,&IL,&IU,&abstol,&bm,eigs,V,&ldz,isuppz,&workquery,&lwork,&iworkquery,&liwork,&info));
1175e6a796c3SToby Isaac   if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEGR error");
1176e6a796c3SToby Isaac   lwork = (PetscBLASInt) workquery;
1177e6a796c3SToby Isaac   liwork = (PetscBLASInt) iworkquery;
1178e6a796c3SToby Isaac   ierr = PetscMalloc2(lwork, &work, liwork, &iwork);CHKERRQ(ierr);
1179e6a796c3SToby Isaac   ierr = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr);
1180e6a796c3SToby Isaac   PetscStackCallBLAS("LAPACKstegr",LAPACKstegr_(&jobz,&range,&bn,diag,subdiag,&VL,&VU,&IL,&IU,&abstol,&bm,eigs,V,&ldz,isuppz,work,&lwork,iwork,&liwork,&info));
1181e6a796c3SToby Isaac   ierr = PetscFPTrapPop();CHKERRQ(ierr);
1182e6a796c3SToby Isaac   if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEGR error");
1183e6a796c3SToby Isaac   ierr = PetscFree2(work, iwork);CHKERRQ(ierr);
1184e6a796c3SToby Isaac   ierr = PetscFree(isuppz);CHKERRQ(ierr);
1185e6a796c3SToby Isaac #elif !defined(PETSC_MISSING_LAPACK_STEQR)
1186e6a796c3SToby Isaac   jobz = 'I'; /* Compute eigenvalues and eigenvectors of the
1187e6a796c3SToby Isaac                  tridiagonal matrix.  Z is initialized to the identity
1188e6a796c3SToby Isaac                  matrix. */
1189e6a796c3SToby Isaac   ierr = PetscMalloc1(PetscMax(1,2*n-2),&work);CHKERRQ(ierr);
1190e6a796c3SToby Isaac   PetscStackCallBLAS("LAPACKsteqr",LAPACKsteqr_("I",&bn,diag,subdiag,V,&ldz,work,&info));
1191e6a796c3SToby Isaac   ierr = PetscFPTrapPop();CHKERRQ(ierr);
1192e6a796c3SToby Isaac   if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEQR error");
1193e6a796c3SToby Isaac   ierr = PetscFree(work);CHKERRQ(ierr);
1194e6a796c3SToby Isaac   ierr = PetscArraycpy(eigs,diag,n);CHKERRQ(ierr);
1195e6a796c3SToby Isaac #endif
1196e6a796c3SToby Isaac   PetscFunctionReturn(0);
1197e6a796c3SToby Isaac }
1198e6a796c3SToby Isaac 
1199e6a796c3SToby Isaac /* Formula for the weights at the endpoints (-1 and 1) of Gauss-Lobatto-Jacobi
1200e6a796c3SToby Isaac  * quadrature rules on the interval [-1, 1] */
1201e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussLobattoJacobiEndweights_Internal(PetscInt n, PetscReal alpha, PetscReal beta, PetscReal *leftw, PetscReal *rightw)
1202e6a796c3SToby Isaac {
1203e6a796c3SToby Isaac   PetscReal twoab1;
1204e6a796c3SToby Isaac   PetscInt  m = n - 2;
1205e6a796c3SToby Isaac   PetscReal a = alpha + 1.;
1206e6a796c3SToby Isaac   PetscReal b = beta + 1.;
1207e6a796c3SToby Isaac   PetscReal gra, grb;
1208e6a796c3SToby Isaac 
1209e6a796c3SToby Isaac   PetscFunctionBegin;
1210e6a796c3SToby Isaac   twoab1 = PetscPowReal(2., a + b - 1.);
1211e6a796c3SToby Isaac #if defined(PETSC_HAVE_LGAMMA)
1212e6a796c3SToby Isaac   grb = PetscExpReal(2. * PetscLGamma(b+1.) + PetscLGamma(m+1.) + PetscLGamma(m+a+1.) -
1213e6a796c3SToby Isaac                      (PetscLGamma(m+b+1) + PetscLGamma(m+a+b+1.)));
1214e6a796c3SToby Isaac   gra = PetscExpReal(2. * PetscLGamma(a+1.) + PetscLGamma(m+1.) + PetscLGamma(m+b+1.) -
1215e6a796c3SToby Isaac                      (PetscLGamma(m+a+1) + PetscLGamma(m+a+b+1.)));
1216e6a796c3SToby Isaac #else
1217e6a796c3SToby Isaac   {
1218e6a796c3SToby Isaac     PetscInt alphai = (PetscInt) alpha;
1219e6a796c3SToby Isaac     PetscInt betai = (PetscInt) beta;
122094e21283SToby Isaac     PetscErrorCode ierr;
1221e6a796c3SToby Isaac 
1222e6a796c3SToby Isaac     if ((PetscReal) alphai == alpha && (PetscReal) betai == beta) {
1223e6a796c3SToby Isaac       PetscReal binom1, binom2;
1224e6a796c3SToby Isaac 
1225e6a796c3SToby Isaac       ierr = PetscDTBinomial(m+b, b, &binom1);CHKERRQ(ierr);
1226e6a796c3SToby Isaac       ierr = PetscDTBinomial(m+a+b, b, &binom2);CHKERRQ(ierr);
1227e6a796c3SToby Isaac       grb = 1./ (binom1 * binom2);
1228e6a796c3SToby Isaac       ierr = PetscDTBinomial(m+a, a, &binom1);CHKERRQ(ierr);
1229e6a796c3SToby Isaac       ierr = PetscDTBinomial(m+a+b, a, &binom2);CHKERRQ(ierr);
1230e6a796c3SToby Isaac       gra = 1./ (binom1 * binom2);
1231e6a796c3SToby Isaac     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"lgamma() - math routine is unavailable.");
1232e6a796c3SToby Isaac   }
1233e6a796c3SToby Isaac #endif
1234e6a796c3SToby Isaac   *leftw = twoab1 * grb / b;
1235e6a796c3SToby Isaac   *rightw = twoab1 * gra / a;
1236e6a796c3SToby Isaac   PetscFunctionReturn(0);
1237e6a796c3SToby Isaac }
1238e6a796c3SToby Isaac 
1239e6a796c3SToby Isaac /* Evaluates the nth jacobi polynomial with weight parameters a,b at a point x.
1240e6a796c3SToby Isaac    Recurrence relations implemented from the pseudocode given in Karniadakis and Sherwin, Appendix B */
1241e6a796c3SToby Isaac PETSC_STATIC_INLINE PetscErrorCode PetscDTComputeJacobi(PetscReal a, PetscReal b, PetscInt n, PetscReal x, PetscReal *P)
1242e6a796c3SToby Isaac {
124394e21283SToby Isaac   PetscReal pn1, pn2;
124494e21283SToby Isaac   PetscReal cnm1, cnm1x, cnm2;
1245e6a796c3SToby Isaac   PetscInt  k;
1246e6a796c3SToby Isaac 
1247e6a796c3SToby Isaac   PetscFunctionBegin;
1248e6a796c3SToby Isaac   if (!n) {*P = 1.0; PetscFunctionReturn(0);}
124994e21283SToby Isaac   PetscDTJacobiRecurrence_Internal(1,a,b,cnm1,cnm1x,cnm2);
125094e21283SToby Isaac   pn2 = 1.;
125194e21283SToby Isaac   pn1 = cnm1 + cnm1x*x;
125294e21283SToby Isaac   if (n == 1) {*P = pn1; PetscFunctionReturn(0);}
1253e6a796c3SToby Isaac   *P  = 0.0;
1254e6a796c3SToby Isaac   for (k = 2; k < n+1; ++k) {
125594e21283SToby Isaac     PetscDTJacobiRecurrence_Internal(k,a,b,cnm1,cnm1x,cnm2);
1256e6a796c3SToby Isaac 
125794e21283SToby Isaac     *P  = (cnm1 + cnm1x*x)*pn1 - cnm2*pn2;
1258e6a796c3SToby Isaac     pn2 = pn1;
1259e6a796c3SToby Isaac     pn1 = *P;
1260e6a796c3SToby Isaac   }
1261e6a796c3SToby Isaac   PetscFunctionReturn(0);
1262e6a796c3SToby Isaac }
1263e6a796c3SToby Isaac 
1264e6a796c3SToby Isaac /* Evaluates the first derivative of P_{n}^{a,b} at a point x. */
1265e6a796c3SToby Isaac PETSC_STATIC_INLINE PetscErrorCode PetscDTComputeJacobiDerivative(PetscReal a, PetscReal b, PetscInt n, PetscReal x, PetscInt k, PetscReal *P)
1266e6a796c3SToby Isaac {
1267e6a796c3SToby Isaac   PetscReal      nP;
1268e6a796c3SToby Isaac   PetscInt       i;
1269e6a796c3SToby Isaac   PetscErrorCode ierr;
1270e6a796c3SToby Isaac 
1271e6a796c3SToby Isaac   PetscFunctionBegin;
127217a42bb7SSatish Balay   *P = 0.0;
127317a42bb7SSatish Balay   if (k > n) PetscFunctionReturn(0);
1274e6a796c3SToby Isaac   ierr = PetscDTComputeJacobi(a+k, b+k, n-k, x, &nP);CHKERRQ(ierr);
1275e6a796c3SToby Isaac   for (i = 0; i < k; i++) nP *= (a + b + n + 1. + i) * 0.5;
1276e6a796c3SToby Isaac   *P = nP;
1277e6a796c3SToby Isaac   PetscFunctionReturn(0);
1278e6a796c3SToby Isaac }
1279e6a796c3SToby Isaac 
1280e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussJacobiQuadrature_Newton_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscReal x[], PetscReal w[])
1281e6a796c3SToby Isaac {
1282e6a796c3SToby Isaac   PetscInt       maxIter = 100;
128394e21283SToby Isaac   PetscReal      eps     = PetscExpReal(0.75 * PetscLogReal(PETSC_MACHINE_EPSILON));
1284200b5abcSJed Brown   PetscReal      a1, a6, gf;
1285e6a796c3SToby Isaac   PetscInt       k;
1286e6a796c3SToby Isaac   PetscErrorCode ierr;
1287e6a796c3SToby Isaac 
1288e6a796c3SToby Isaac   PetscFunctionBegin;
1289e6a796c3SToby Isaac 
1290e6a796c3SToby Isaac   a1 = PetscPowReal(2.0, a+b+1);
129194e21283SToby Isaac #if defined(PETSC_HAVE_LGAMMA)
1292200b5abcSJed Brown   {
1293200b5abcSJed Brown     PetscReal a2, a3, a4, a5;
129494e21283SToby Isaac     a2 = PetscLGamma(a + npoints + 1);
129594e21283SToby Isaac     a3 = PetscLGamma(b + npoints + 1);
129694e21283SToby Isaac     a4 = PetscLGamma(a + b + npoints + 1);
129794e21283SToby Isaac     a5 = PetscLGamma(npoints + 1);
129894e21283SToby Isaac     gf = PetscExpReal(a2 + a3 - (a4 + a5));
1299200b5abcSJed Brown   }
1300e6a796c3SToby Isaac #else
1301e6a796c3SToby Isaac   {
1302e6a796c3SToby Isaac     PetscInt ia, ib;
1303e6a796c3SToby Isaac 
1304e6a796c3SToby Isaac     ia = (PetscInt) a;
1305e6a796c3SToby Isaac     ib = (PetscInt) b;
130694e21283SToby Isaac     gf = 1.;
130794e21283SToby Isaac     if (ia == a && ia >= 0) { /* compute ratio of rising factorals wrt a */
130894e21283SToby Isaac       for (k = 0; k < ia; k++) gf *= (npoints + 1. + k) / (npoints + b + 1. + k);
130994e21283SToby Isaac     } else if (b == b && ib >= 0) { /* compute ratio of rising factorials wrt b */
131094e21283SToby Isaac       for (k = 0; k < ib; k++) gf *= (npoints + 1. + k) / (npoints + a + 1. + k);
131194e21283SToby Isaac     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"lgamma() - math routine is unavailable.");
1312e6a796c3SToby Isaac   }
1313e6a796c3SToby Isaac #endif
1314e6a796c3SToby Isaac 
131594e21283SToby Isaac   a6   = a1 * gf;
1316e6a796c3SToby Isaac   /* Computes the m roots of P_{m}^{a,b} on [-1,1] by Newton's method with Chebyshev points as initial guesses.
1317e6a796c3SToby Isaac    Algorithm implemented from the pseudocode given by Karniadakis and Sherwin and Python in FIAT */
1318e6a796c3SToby Isaac   for (k = 0; k < npoints; ++k) {
131994e21283SToby Isaac     PetscReal r = PetscCosReal(PETSC_PI * (1. - (4.*k + 3. + 2.*b) / (4.*npoints + 2.*(a + b + 1.)))), dP;
1320e6a796c3SToby Isaac     PetscInt  j;
1321e6a796c3SToby Isaac 
1322e6a796c3SToby Isaac     if (k > 0) r = 0.5 * (r + x[k-1]);
1323e6a796c3SToby Isaac     for (j = 0; j < maxIter; ++j) {
1324e6a796c3SToby Isaac       PetscReal s = 0.0, delta, f, fp;
1325e6a796c3SToby Isaac       PetscInt  i;
1326e6a796c3SToby Isaac 
1327e6a796c3SToby Isaac       for (i = 0; i < k; ++i) s = s + 1.0 / (r - x[i]);
1328e6a796c3SToby Isaac       ierr = PetscDTComputeJacobi(a, b, npoints, r, &f);CHKERRQ(ierr);
1329e6a796c3SToby Isaac       ierr = PetscDTComputeJacobiDerivative(a, b, npoints, r, 1, &fp);CHKERRQ(ierr);
1330e6a796c3SToby Isaac       delta = f / (fp - f * s);
1331e6a796c3SToby Isaac       r     = r - delta;
1332e6a796c3SToby Isaac       if (PetscAbsReal(delta) < eps) break;
1333e6a796c3SToby Isaac     }
1334e6a796c3SToby Isaac     x[k] = r;
1335e6a796c3SToby Isaac     ierr = PetscDTComputeJacobiDerivative(a, b, npoints, x[k], 1, &dP);CHKERRQ(ierr);
1336e6a796c3SToby Isaac     w[k] = a6 / (1.0 - PetscSqr(x[k])) / PetscSqr(dP);
1337e6a796c3SToby Isaac   }
1338e6a796c3SToby Isaac   PetscFunctionReturn(0);
1339e6a796c3SToby Isaac }
1340e6a796c3SToby Isaac 
134194e21283SToby Isaac /* Compute the diagonals of the Jacobi matrix used in Golub & Welsch algorithms for Gauss-Jacobi
1342e6a796c3SToby Isaac  * quadrature weight calculations on [-1,1] for exponents (1. + x)^a (1.-x)^b */
1343e6a796c3SToby Isaac static PetscErrorCode PetscDTJacobiMatrix_Internal(PetscInt nPoints, PetscReal a, PetscReal b, PetscReal *d, PetscReal *s)
1344e6a796c3SToby Isaac {
1345e6a796c3SToby Isaac   PetscInt       i;
1346e6a796c3SToby Isaac 
1347e6a796c3SToby Isaac   PetscFunctionBegin;
1348e6a796c3SToby Isaac   for (i = 0; i < nPoints; i++) {
134994e21283SToby Isaac     PetscReal A, B, C;
1350e6a796c3SToby Isaac 
135194e21283SToby Isaac     PetscDTJacobiRecurrence_Internal(i+1,a,b,A,B,C);
135294e21283SToby Isaac     d[i] = -A / B;
135394e21283SToby Isaac     if (i) s[i-1] *= C / B;
135494e21283SToby Isaac     if (i < nPoints - 1) s[i] = 1. / B;
1355e6a796c3SToby Isaac   }
1356e6a796c3SToby Isaac   PetscFunctionReturn(0);
1357e6a796c3SToby Isaac }
1358e6a796c3SToby Isaac 
1359e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussJacobiQuadrature_GolubWelsch_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscReal *x, PetscReal *w)
1360e6a796c3SToby Isaac {
1361e6a796c3SToby Isaac   PetscReal mu0;
1362e6a796c3SToby Isaac   PetscReal ga, gb, gab;
1363e6a796c3SToby Isaac   PetscInt i;
1364e6a796c3SToby Isaac   PetscErrorCode ierr;
1365e6a796c3SToby Isaac 
1366e6a796c3SToby Isaac   PetscFunctionBegin;
1367e6a796c3SToby Isaac   ierr = PetscCitationsRegister(GolubWelschCitation, &GolubWelschCite);CHKERRQ(ierr);
1368e6a796c3SToby Isaac 
1369e6a796c3SToby Isaac #if defined(PETSC_HAVE_TGAMMA)
1370e6a796c3SToby Isaac   ga  = PetscTGamma(a + 1);
1371e6a796c3SToby Isaac   gb  = PetscTGamma(b + 1);
1372e6a796c3SToby Isaac   gab = PetscTGamma(a + b + 2);
1373e6a796c3SToby Isaac #else
1374e6a796c3SToby Isaac   {
1375e6a796c3SToby Isaac     PetscInt ia, ib;
1376e6a796c3SToby Isaac 
1377e6a796c3SToby Isaac     ia = (PetscInt) a;
1378e6a796c3SToby Isaac     ib = (PetscInt) b;
1379e6a796c3SToby Isaac     if (ia == a && ib == b && ia + 1 > 0 && ib + 1 > 0 && ia + ib + 2 > 0) { /* All gamma(x) terms are (x-1)! terms */
1380e6a796c3SToby Isaac       ierr = PetscDTFactorial(ia, &ga);CHKERRQ(ierr);
1381e6a796c3SToby Isaac       ierr = PetscDTFactorial(ib, &gb);CHKERRQ(ierr);
1382e6a796c3SToby Isaac       ierr = PetscDTFactorial(ia + ib + 1, &gb);CHKERRQ(ierr);
1383e6a796c3SToby Isaac     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"tgamma() - math routine is unavailable.");
1384e6a796c3SToby Isaac   }
1385e6a796c3SToby Isaac #endif
1386e6a796c3SToby Isaac   mu0 = PetscPowReal(2.,a + b + 1.) * ga * gb / gab;
1387e6a796c3SToby Isaac 
1388e6a796c3SToby Isaac #if defined(PETSCDTGAUSSIANQUADRATURE_EIG)
1389e6a796c3SToby Isaac   {
1390e6a796c3SToby Isaac     PetscReal *diag, *subdiag;
1391e6a796c3SToby Isaac     PetscScalar *V;
1392e6a796c3SToby Isaac 
1393e6a796c3SToby Isaac     ierr = PetscMalloc2(npoints, &diag, npoints, &subdiag);CHKERRQ(ierr);
1394e6a796c3SToby Isaac     ierr = PetscMalloc1(npoints*npoints, &V);CHKERRQ(ierr);
1395e6a796c3SToby Isaac     ierr = PetscDTJacobiMatrix_Internal(npoints, a, b, diag, subdiag);CHKERRQ(ierr);
1396e6a796c3SToby Isaac     for (i = 0; i < npoints - 1; i++) subdiag[i] = PetscSqrtReal(subdiag[i]);
1397e6a796c3SToby Isaac     ierr = PetscDTSymmetricTridiagonalEigensolve(npoints, diag, subdiag, x, V);CHKERRQ(ierr);
139894e21283SToby Isaac     for (i = 0; i < npoints; i++) w[i] = PetscSqr(PetscRealPart(V[i * npoints])) * mu0;
1399e6a796c3SToby Isaac     ierr = PetscFree(V);CHKERRQ(ierr);
1400e6a796c3SToby Isaac     ierr = PetscFree2(diag, subdiag);CHKERRQ(ierr);
1401e6a796c3SToby Isaac   }
1402e6a796c3SToby Isaac #else
1403e6a796c3SToby Isaac   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "A LAPACK symmetric tridiagonal eigensolver could not be found");
1404e6a796c3SToby Isaac #endif
140594e21283SToby Isaac   { /* As of March 2, 2020, The Sun Performance Library breaks the LAPACK contract for xstegr and xsteqr: the
140694e21283SToby Isaac        eigenvalues are not guaranteed to be in ascending order.  So we heave a passive aggressive sigh and check that
140794e21283SToby Isaac        the eigenvalues are sorted */
140894e21283SToby Isaac     PetscBool sorted;
140994e21283SToby Isaac 
141094e21283SToby Isaac     ierr = PetscSortedReal(npoints, x, &sorted);CHKERRQ(ierr);
141194e21283SToby Isaac     if (!sorted) {
141294e21283SToby Isaac       PetscInt *order, i;
141394e21283SToby Isaac       PetscReal *tmp;
141494e21283SToby Isaac 
141594e21283SToby Isaac       ierr = PetscMalloc2(npoints, &order, npoints, &tmp);CHKERRQ(ierr);
141694e21283SToby Isaac       for (i = 0; i < npoints; i++) order[i] = i;
141794e21283SToby Isaac       ierr = PetscSortRealWithPermutation(npoints, x, order);CHKERRQ(ierr);
141894e21283SToby Isaac       ierr = PetscArraycpy(tmp, x, npoints);CHKERRQ(ierr);
141994e21283SToby Isaac       for (i = 0; i < npoints; i++) x[i] = tmp[order[i]];
142094e21283SToby Isaac       ierr = PetscArraycpy(tmp, w, npoints);CHKERRQ(ierr);
142194e21283SToby Isaac       for (i = 0; i < npoints; i++) w[i] = tmp[order[i]];
142294e21283SToby Isaac       ierr = PetscFree2(order, tmp);CHKERRQ(ierr);
142394e21283SToby Isaac     }
142494e21283SToby Isaac   }
1425e6a796c3SToby Isaac   PetscFunctionReturn(0);
1426e6a796c3SToby Isaac }
1427e6a796c3SToby Isaac 
1428e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussJacobiQuadrature_Internal(PetscInt npoints,PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[], PetscBool newton)
1429e6a796c3SToby Isaac {
1430e6a796c3SToby Isaac   PetscErrorCode ierr;
1431e6a796c3SToby Isaac 
1432e6a796c3SToby Isaac   PetscFunctionBegin;
1433e6a796c3SToby Isaac   if (npoints < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of points must be positive");
1434e6a796c3SToby Isaac   /* If asking for a 1D Lobatto point, just return the non-Lobatto 1D point */
1435e6a796c3SToby Isaac   if (alpha <= -1.) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"alpha must be > -1.");
1436e6a796c3SToby Isaac   if (beta <= -1.) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"beta must be > -1.");
1437e6a796c3SToby Isaac 
1438e6a796c3SToby Isaac   if (newton) {
1439e6a796c3SToby Isaac     ierr = PetscDTGaussJacobiQuadrature_Newton_Internal(npoints, alpha, beta, x, w);CHKERRQ(ierr);
1440e6a796c3SToby Isaac   } else {
1441e6a796c3SToby Isaac     ierr = PetscDTGaussJacobiQuadrature_GolubWelsch_Internal(npoints, alpha, beta, x, w);CHKERRQ(ierr);
1442e6a796c3SToby Isaac   }
1443e6a796c3SToby Isaac   if (alpha == beta) { /* symmetrize */
1444e6a796c3SToby Isaac     PetscInt i;
1445e6a796c3SToby Isaac     for (i = 0; i < (npoints + 1) / 2; i++) {
1446e6a796c3SToby Isaac       PetscInt  j  = npoints - 1 - i;
1447e6a796c3SToby Isaac       PetscReal xi = x[i];
1448e6a796c3SToby Isaac       PetscReal xj = x[j];
1449e6a796c3SToby Isaac       PetscReal wi = w[i];
1450e6a796c3SToby Isaac       PetscReal wj = w[j];
1451e6a796c3SToby Isaac 
1452e6a796c3SToby Isaac       x[i] = (xi - xj) / 2.;
1453e6a796c3SToby Isaac       x[j] = (xj - xi) / 2.;
1454e6a796c3SToby Isaac       w[i] = w[j] = (wi + wj) / 2.;
1455e6a796c3SToby Isaac     }
1456e6a796c3SToby Isaac   }
1457e6a796c3SToby Isaac   PetscFunctionReturn(0);
1458e6a796c3SToby Isaac }
1459e6a796c3SToby Isaac 
146094e21283SToby Isaac /*@
146194e21283SToby Isaac   PetscDTGaussJacobiQuadrature - quadrature for the interval [a, b] with the weight function
146294e21283SToby Isaac   $(x-a)^\alpha (x-b)^\beta$.
146394e21283SToby Isaac 
146494e21283SToby Isaac   Not collective
146594e21283SToby Isaac 
146694e21283SToby Isaac   Input Parameters:
146794e21283SToby Isaac + npoints - the number of points in the quadrature rule
146894e21283SToby Isaac . a - the left endpoint of the interval
146994e21283SToby Isaac . b - the right endpoint of the interval
147094e21283SToby Isaac . alpha - the left exponent
147194e21283SToby Isaac - beta - the right exponent
147294e21283SToby Isaac 
147394e21283SToby Isaac   Output Parameters:
147494e21283SToby Isaac + x - array of length npoints, the locations of the quadrature points
147594e21283SToby Isaac - w - array of length npoints, the weights of the quadrature points
147694e21283SToby Isaac 
147794e21283SToby Isaac   Level: intermediate
147894e21283SToby Isaac 
147994e21283SToby Isaac   Note: this quadrature rule is exact for polynomials up to degree 2*npoints - 1.
148094e21283SToby Isaac @*/
148194e21283SToby Isaac PetscErrorCode PetscDTGaussJacobiQuadrature(PetscInt npoints,PetscReal a, PetscReal b, PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[])
1482e6a796c3SToby Isaac {
148394e21283SToby Isaac   PetscInt       i;
1484e6a796c3SToby Isaac   PetscErrorCode ierr;
1485e6a796c3SToby Isaac 
1486e6a796c3SToby Isaac   PetscFunctionBegin;
148794e21283SToby Isaac   ierr = PetscDTGaussJacobiQuadrature_Internal(npoints, alpha, beta, x, w, PetscDTGaussQuadratureNewton_Internal);CHKERRQ(ierr);
148894e21283SToby Isaac   if (a != -1. || b != 1.) { /* shift */
148994e21283SToby Isaac     for (i = 0; i < npoints; i++) {
149094e21283SToby Isaac       x[i] = (x[i] + 1.) * ((b - a) / 2.) + a;
149194e21283SToby Isaac       w[i] *= (b - a) / 2.;
149294e21283SToby Isaac     }
149394e21283SToby Isaac   }
1494e6a796c3SToby Isaac   PetscFunctionReturn(0);
1495e6a796c3SToby Isaac }
1496e6a796c3SToby Isaac 
1497e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussLobattoJacobiQuadrature_Internal(PetscInt npoints,PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[], PetscBool newton)
1498e6a796c3SToby Isaac {
1499e6a796c3SToby Isaac   PetscInt       i;
1500e6a796c3SToby Isaac   PetscErrorCode ierr;
1501e6a796c3SToby Isaac 
1502e6a796c3SToby Isaac   PetscFunctionBegin;
1503e6a796c3SToby Isaac   if (npoints < 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of points must be positive");
1504e6a796c3SToby Isaac   /* If asking for a 1D Lobatto point, just return the non-Lobatto 1D point */
1505e6a796c3SToby Isaac   if (alpha <= -1.) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"alpha must be > -1.");
1506e6a796c3SToby Isaac   if (beta <= -1.) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"beta must be > -1.");
1507e6a796c3SToby Isaac 
1508e6a796c3SToby Isaac   x[0] = -1.;
1509e6a796c3SToby Isaac   x[npoints-1] = 1.;
151094e21283SToby Isaac   if (npoints > 2) {
151194e21283SToby Isaac     ierr = PetscDTGaussJacobiQuadrature_Internal(npoints-2, alpha+1., beta+1., &x[1], &w[1], newton);CHKERRQ(ierr);
151294e21283SToby Isaac   }
1513e6a796c3SToby Isaac   for (i = 1; i < npoints - 1; i++) {
1514e6a796c3SToby Isaac     w[i] /= (1. - x[i]*x[i]);
1515e6a796c3SToby Isaac   }
1516e6a796c3SToby Isaac   ierr = PetscDTGaussLobattoJacobiEndweights_Internal(npoints, alpha, beta, &w[0], &w[npoints-1]);CHKERRQ(ierr);
1517e6a796c3SToby Isaac   PetscFunctionReturn(0);
1518e6a796c3SToby Isaac }
1519e6a796c3SToby Isaac 
152037045ce4SJed Brown /*@
152194e21283SToby Isaac   PetscDTGaussLobattoJacobiQuadrature - quadrature for the interval [a, b] with the weight function
152294e21283SToby Isaac   $(x-a)^\alpha (x-b)^\beta$, with endpoints a and b included as quadrature points.
152394e21283SToby Isaac 
152494e21283SToby Isaac   Not collective
152594e21283SToby Isaac 
152694e21283SToby Isaac   Input Parameters:
152794e21283SToby Isaac + npoints - the number of points in the quadrature rule
152894e21283SToby Isaac . a - the left endpoint of the interval
152994e21283SToby Isaac . b - the right endpoint of the interval
153094e21283SToby Isaac . alpha - the left exponent
153194e21283SToby Isaac - beta - the right exponent
153294e21283SToby Isaac 
153394e21283SToby Isaac   Output Parameters:
153494e21283SToby Isaac + x - array of length npoints, the locations of the quadrature points
153594e21283SToby Isaac - w - array of length npoints, the weights of the quadrature points
153694e21283SToby Isaac 
153794e21283SToby Isaac   Level: intermediate
153894e21283SToby Isaac 
153994e21283SToby Isaac   Note: this quadrature rule is exact for polynomials up to degree 2*npoints - 3.
154094e21283SToby Isaac @*/
154194e21283SToby Isaac PetscErrorCode PetscDTGaussLobattoJacobiQuadrature(PetscInt npoints,PetscReal a, PetscReal b, PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[])
154294e21283SToby Isaac {
154394e21283SToby Isaac   PetscInt       i;
154494e21283SToby Isaac   PetscErrorCode ierr;
154594e21283SToby Isaac 
154694e21283SToby Isaac   PetscFunctionBegin;
154794e21283SToby Isaac   ierr = PetscDTGaussLobattoJacobiQuadrature_Internal(npoints, alpha, beta, x, w, PetscDTGaussQuadratureNewton_Internal);CHKERRQ(ierr);
154894e21283SToby Isaac   if (a != -1. || b != 1.) { /* shift */
154994e21283SToby Isaac     for (i = 0; i < npoints; i++) {
155094e21283SToby Isaac       x[i] = (x[i] + 1.) * ((b - a) / 2.) + a;
155194e21283SToby Isaac       w[i] *= (b - a) / 2.;
155294e21283SToby Isaac     }
155394e21283SToby Isaac   }
155494e21283SToby Isaac   PetscFunctionReturn(0);
155594e21283SToby Isaac }
155694e21283SToby Isaac 
155794e21283SToby Isaac /*@
1558e6a796c3SToby Isaac    PetscDTGaussQuadrature - create Gauss-Legendre quadrature
155937045ce4SJed Brown 
156037045ce4SJed Brown    Not Collective
156137045ce4SJed Brown 
156237045ce4SJed Brown    Input Arguments:
156337045ce4SJed Brown +  npoints - number of points
156437045ce4SJed Brown .  a - left end of interval (often-1)
156537045ce4SJed Brown -  b - right end of interval (often +1)
156637045ce4SJed Brown 
156737045ce4SJed Brown    Output Arguments:
156837045ce4SJed Brown +  x - quadrature points
156937045ce4SJed Brown -  w - quadrature weights
157037045ce4SJed Brown 
157137045ce4SJed Brown    Level: intermediate
157237045ce4SJed Brown 
157337045ce4SJed Brown    References:
157496a0c994SBarry Smith .   1. - Golub and Welsch, Calculation of Quadrature Rules, Math. Comp. 23(106), 1969.
157537045ce4SJed Brown 
157637045ce4SJed Brown .seealso: PetscDTLegendreEval()
157737045ce4SJed Brown @*/
157837045ce4SJed Brown PetscErrorCode PetscDTGaussQuadrature(PetscInt npoints,PetscReal a,PetscReal b,PetscReal *x,PetscReal *w)
157937045ce4SJed Brown {
158037045ce4SJed Brown   PetscInt       i;
1581e6a796c3SToby Isaac   PetscErrorCode ierr;
158237045ce4SJed Brown 
158337045ce4SJed Brown   PetscFunctionBegin;
158494e21283SToby Isaac   ierr = PetscDTGaussJacobiQuadrature_Internal(npoints, 0., 0., x, w, PetscDTGaussQuadratureNewton_Internal);CHKERRQ(ierr);
158594e21283SToby Isaac   if (a != -1. || b != 1.) { /* shift */
158637045ce4SJed Brown     for (i = 0; i < npoints; i++) {
1587e6a796c3SToby Isaac       x[i] = (x[i] + 1.) * ((b - a) / 2.) + a;
1588e6a796c3SToby Isaac       w[i] *= (b - a) / 2.;
158937045ce4SJed Brown     }
159037045ce4SJed Brown   }
159137045ce4SJed Brown   PetscFunctionReturn(0);
159237045ce4SJed Brown }
1593194825f6SJed Brown 
15948272889dSSatish Balay /*@C
15958272889dSSatish Balay    PetscDTGaussLobattoLegendreQuadrature - creates a set of the locations and weights of the Gauss-Lobatto-Legendre
15968272889dSSatish Balay                       nodes of a given size on the domain [-1,1]
15978272889dSSatish Balay 
15988272889dSSatish Balay    Not Collective
15998272889dSSatish Balay 
16008272889dSSatish Balay    Input Parameter:
16018272889dSSatish Balay +  n - number of grid nodes
1602f2e8fe4dShannah_mairs -  type - PETSCGAUSSLOBATTOLEGENDRE_VIA_LINEAR_ALGEBRA or PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON
16038272889dSSatish Balay 
16048272889dSSatish Balay    Output Arguments:
16058272889dSSatish Balay +  x - quadrature points
16068272889dSSatish Balay -  w - quadrature weights
16078272889dSSatish Balay 
16088272889dSSatish Balay    Notes:
16098272889dSSatish Balay     For n > 30  the Newton approach computes duplicate (incorrect) values for some nodes because the initial guess is apparently not
16108272889dSSatish Balay           close enough to the desired solution
16118272889dSSatish Balay 
16128272889dSSatish Balay    These are useful for implementing spectral methods based on Gauss-Lobatto-Legendre (GLL) nodes
16138272889dSSatish Balay 
1614a8d69d7bSBarry Smith    See  https://epubs.siam.org/doi/abs/10.1137/110855442  https://epubs.siam.org/doi/abs/10.1137/120889873 for better ways to compute GLL nodes
16158272889dSSatish Balay 
16168272889dSSatish Balay    Level: intermediate
16178272889dSSatish Balay 
16188272889dSSatish Balay .seealso: PetscDTGaussQuadrature()
16198272889dSSatish Balay 
16208272889dSSatish Balay @*/
1621916e780bShannah_mairs PetscErrorCode PetscDTGaussLobattoLegendreQuadrature(PetscInt npoints,PetscGaussLobattoLegendreCreateType type,PetscReal *x,PetscReal *w)
16228272889dSSatish Balay {
1623e6a796c3SToby Isaac   PetscBool      newton;
16248272889dSSatish Balay   PetscErrorCode ierr;
16258272889dSSatish Balay 
16268272889dSSatish Balay   PetscFunctionBegin;
16278272889dSSatish Balay   if (npoints < 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must provide at least 2 grid points per element");
162894e21283SToby Isaac   newton = (PetscBool) (type == PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON);
1629e6a796c3SToby Isaac   ierr = PetscDTGaussLobattoJacobiQuadrature_Internal(npoints, 0., 0., x, w, newton);CHKERRQ(ierr);
16308272889dSSatish Balay   PetscFunctionReturn(0);
16318272889dSSatish Balay }
16328272889dSSatish Balay 
1633744bafbcSMatthew G. Knepley /*@
1634744bafbcSMatthew G. Knepley   PetscDTGaussTensorQuadrature - creates a tensor-product Gauss quadrature
1635744bafbcSMatthew G. Knepley 
1636744bafbcSMatthew G. Knepley   Not Collective
1637744bafbcSMatthew G. Knepley 
1638744bafbcSMatthew G. Knepley   Input Arguments:
1639744bafbcSMatthew G. Knepley + dim     - The spatial dimension
1640a6b92713SMatthew G. Knepley . Nc      - The number of components
1641744bafbcSMatthew G. Knepley . npoints - number of points in one dimension
1642744bafbcSMatthew G. Knepley . a       - left end of interval (often-1)
1643744bafbcSMatthew G. Knepley - b       - right end of interval (often +1)
1644744bafbcSMatthew G. Knepley 
1645744bafbcSMatthew G. Knepley   Output Argument:
1646744bafbcSMatthew G. Knepley . q - A PetscQuadrature object
1647744bafbcSMatthew G. Knepley 
1648744bafbcSMatthew G. Knepley   Level: intermediate
1649744bafbcSMatthew G. Knepley 
1650744bafbcSMatthew G. Knepley .seealso: PetscDTGaussQuadrature(), PetscDTLegendreEval()
1651744bafbcSMatthew G. Knepley @*/
1652a6b92713SMatthew G. Knepley PetscErrorCode PetscDTGaussTensorQuadrature(PetscInt dim, PetscInt Nc, PetscInt npoints, PetscReal a, PetscReal b, PetscQuadrature *q)
1653744bafbcSMatthew G. Knepley {
1654a6b92713SMatthew G. Knepley   PetscInt       totpoints = dim > 1 ? dim > 2 ? npoints*PetscSqr(npoints) : PetscSqr(npoints) : npoints, i, j, k, c;
1655744bafbcSMatthew G. Knepley   PetscReal     *x, *w, *xw, *ww;
1656744bafbcSMatthew G. Knepley   PetscErrorCode ierr;
1657744bafbcSMatthew G. Knepley 
1658744bafbcSMatthew G. Knepley   PetscFunctionBegin;
1659744bafbcSMatthew G. Knepley   ierr = PetscMalloc1(totpoints*dim,&x);CHKERRQ(ierr);
1660a6b92713SMatthew G. Knepley   ierr = PetscMalloc1(totpoints*Nc,&w);CHKERRQ(ierr);
1661744bafbcSMatthew G. Knepley   /* Set up the Golub-Welsch system */
1662744bafbcSMatthew G. Knepley   switch (dim) {
1663744bafbcSMatthew G. Knepley   case 0:
1664744bafbcSMatthew G. Knepley     ierr = PetscFree(x);CHKERRQ(ierr);
1665744bafbcSMatthew G. Knepley     ierr = PetscFree(w);CHKERRQ(ierr);
1666744bafbcSMatthew G. Knepley     ierr = PetscMalloc1(1, &x);CHKERRQ(ierr);
1667a6b92713SMatthew G. Knepley     ierr = PetscMalloc1(Nc, &w);CHKERRQ(ierr);
1668744bafbcSMatthew G. Knepley     x[0] = 0.0;
1669a6b92713SMatthew G. Knepley     for (c = 0; c < Nc; ++c) w[c] = 1.0;
1670744bafbcSMatthew G. Knepley     break;
1671744bafbcSMatthew G. Knepley   case 1:
1672a6b92713SMatthew G. Knepley     ierr = PetscMalloc1(npoints,&ww);CHKERRQ(ierr);
1673a6b92713SMatthew G. Knepley     ierr = PetscDTGaussQuadrature(npoints, a, b, x, ww);CHKERRQ(ierr);
1674a6b92713SMatthew G. Knepley     for (i = 0; i < npoints; ++i) for (c = 0; c < Nc; ++c) w[i*Nc+c] = ww[i];
1675a6b92713SMatthew G. Knepley     ierr = PetscFree(ww);CHKERRQ(ierr);
1676744bafbcSMatthew G. Knepley     break;
1677744bafbcSMatthew G. Knepley   case 2:
1678744bafbcSMatthew G. Knepley     ierr = PetscMalloc2(npoints,&xw,npoints,&ww);CHKERRQ(ierr);
1679744bafbcSMatthew G. Knepley     ierr = PetscDTGaussQuadrature(npoints, a, b, xw, ww);CHKERRQ(ierr);
1680744bafbcSMatthew G. Knepley     for (i = 0; i < npoints; ++i) {
1681744bafbcSMatthew G. Knepley       for (j = 0; j < npoints; ++j) {
1682744bafbcSMatthew G. Knepley         x[(i*npoints+j)*dim+0] = xw[i];
1683744bafbcSMatthew G. Knepley         x[(i*npoints+j)*dim+1] = xw[j];
1684a6b92713SMatthew G. Knepley         for (c = 0; c < Nc; ++c) w[(i*npoints+j)*Nc+c] = ww[i] * ww[j];
1685744bafbcSMatthew G. Knepley       }
1686744bafbcSMatthew G. Knepley     }
1687744bafbcSMatthew G. Knepley     ierr = PetscFree2(xw,ww);CHKERRQ(ierr);
1688744bafbcSMatthew G. Knepley     break;
1689744bafbcSMatthew G. Knepley   case 3:
1690744bafbcSMatthew G. Knepley     ierr = PetscMalloc2(npoints,&xw,npoints,&ww);CHKERRQ(ierr);
1691744bafbcSMatthew G. Knepley     ierr = PetscDTGaussQuadrature(npoints, a, b, xw, ww);CHKERRQ(ierr);
1692744bafbcSMatthew G. Knepley     for (i = 0; i < npoints; ++i) {
1693744bafbcSMatthew G. Knepley       for (j = 0; j < npoints; ++j) {
1694744bafbcSMatthew G. Knepley         for (k = 0; k < npoints; ++k) {
1695744bafbcSMatthew G. Knepley           x[((i*npoints+j)*npoints+k)*dim+0] = xw[i];
1696744bafbcSMatthew G. Knepley           x[((i*npoints+j)*npoints+k)*dim+1] = xw[j];
1697744bafbcSMatthew G. Knepley           x[((i*npoints+j)*npoints+k)*dim+2] = xw[k];
1698a6b92713SMatthew G. Knepley           for (c = 0; c < Nc; ++c) w[((i*npoints+j)*npoints+k)*Nc+c] = ww[i] * ww[j] * ww[k];
1699744bafbcSMatthew G. Knepley         }
1700744bafbcSMatthew G. Knepley       }
1701744bafbcSMatthew G. Knepley     }
1702744bafbcSMatthew G. Knepley     ierr = PetscFree2(xw,ww);CHKERRQ(ierr);
1703744bafbcSMatthew G. Knepley     break;
1704744bafbcSMatthew G. Knepley   default:
1705744bafbcSMatthew G. Knepley     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct quadrature rule for dimension %d", dim);
1706744bafbcSMatthew G. Knepley   }
1707744bafbcSMatthew G. Knepley   ierr = PetscQuadratureCreate(PETSC_COMM_SELF, q);CHKERRQ(ierr);
17082f5fb066SToby Isaac   ierr = PetscQuadratureSetOrder(*q, 2*npoints-1);CHKERRQ(ierr);
1709a6b92713SMatthew G. Knepley   ierr = PetscQuadratureSetData(*q, dim, Nc, totpoints, x, w);CHKERRQ(ierr);
1710d9bac1caSLisandro Dalcin   ierr = PetscObjectChangeTypeName((PetscObject)*q,"GaussTensor");CHKERRQ(ierr);
1711744bafbcSMatthew G. Knepley   PetscFunctionReturn(0);
1712744bafbcSMatthew G. Knepley }
1713744bafbcSMatthew G. Knepley 
1714f5f57ec0SBarry Smith /*@
1715e6a796c3SToby Isaac   PetscDTStroudConicalQuadrature - create Stroud conical quadrature for a simplex
1716494e7359SMatthew G. Knepley 
1717494e7359SMatthew G. Knepley   Not Collective
1718494e7359SMatthew G. Knepley 
1719494e7359SMatthew G. Knepley   Input Arguments:
1720494e7359SMatthew G. Knepley + dim     - The simplex dimension
1721a6b92713SMatthew G. Knepley . Nc      - The number of components
1722dcce0ee2SMatthew G. Knepley . npoints - The number of points in one dimension
1723494e7359SMatthew G. Knepley . a       - left end of interval (often-1)
1724494e7359SMatthew G. Knepley - b       - right end of interval (often +1)
1725494e7359SMatthew G. Knepley 
1726744bafbcSMatthew G. Knepley   Output Argument:
1727552aa4f7SMatthew G. Knepley . q - A PetscQuadrature object
1728494e7359SMatthew G. Knepley 
1729494e7359SMatthew G. Knepley   Level: intermediate
1730494e7359SMatthew G. Knepley 
1731494e7359SMatthew G. Knepley   References:
173296a0c994SBarry Smith .  1. - Karniadakis and Sherwin.  FIAT
1733494e7359SMatthew G. Knepley 
1734e6a796c3SToby Isaac   Note: For dim == 1, this is Gauss-Legendre quadrature
1735e6a796c3SToby Isaac 
1736744bafbcSMatthew G. Knepley .seealso: PetscDTGaussTensorQuadrature(), PetscDTGaussQuadrature()
1737494e7359SMatthew G. Knepley @*/
1738e6a796c3SToby Isaac PetscErrorCode PetscDTStroudConicalQuadrature(PetscInt dim, PetscInt Nc, PetscInt npoints, PetscReal a, PetscReal b, PetscQuadrature *q)
1739494e7359SMatthew G. Knepley {
1740fbdc3dfeSToby Isaac   PetscInt       totprev, totrem;
1741fbdc3dfeSToby Isaac   PetscInt       totpoints;
1742fbdc3dfeSToby Isaac   PetscReal     *p1, *w1;
1743fbdc3dfeSToby Isaac   PetscReal     *x, *w;
1744fbdc3dfeSToby Isaac   PetscInt       i, j, k, l, m, pt, c;
1745fbdc3dfeSToby Isaac   PetscErrorCode ierr;
1746494e7359SMatthew G. Knepley 
1747494e7359SMatthew G. Knepley   PetscFunctionBegin;
1748494e7359SMatthew G. Knepley   if ((a != -1.0) || (b != 1.0)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must use default internal right now");
1749fbdc3dfeSToby Isaac   totpoints = 1;
1750fbdc3dfeSToby Isaac   for (i = 0, totpoints = 1; i < dim; i++) totpoints *= npoints;
1751dcce0ee2SMatthew G. Knepley   ierr = PetscMalloc1(totpoints*dim, &x);CHKERRQ(ierr);
1752dcce0ee2SMatthew G. Knepley   ierr = PetscMalloc1(totpoints*Nc, &w);CHKERRQ(ierr);
1753fbdc3dfeSToby Isaac   ierr = PetscMalloc2(npoints, &p1, npoints, &w1);CHKERRQ(ierr);
1754fbdc3dfeSToby Isaac   for (i = 0; i < totpoints*Nc; i++) w[i] = 1.;
1755fbdc3dfeSToby Isaac   for (i = 0, totprev = 1, totrem = totpoints / npoints; i < dim; i++) {
1756fbdc3dfeSToby Isaac     PetscReal mul;
1757fbdc3dfeSToby Isaac 
1758fbdc3dfeSToby Isaac     mul = PetscPowReal(2.,-i);
1759fbdc3dfeSToby Isaac     ierr = PetscDTGaussJacobiQuadrature(npoints, -1., 1., i, 0.0, p1, w1);CHKERRQ(ierr);
1760fbdc3dfeSToby Isaac     for (pt = 0, l = 0; l < totprev; l++) {
1761fbdc3dfeSToby Isaac       for (j = 0; j < npoints; j++) {
1762fbdc3dfeSToby Isaac         for (m = 0; m < totrem; m++, pt++) {
1763fbdc3dfeSToby Isaac           for (k = 0; k < i; k++) x[pt*dim+k] = (x[pt*dim+k]+1.)*(1.-p1[j])*0.5 - 1.;
1764fbdc3dfeSToby Isaac           x[pt * dim + i] = p1[j];
1765fbdc3dfeSToby Isaac           for (c = 0; c < Nc; c++) w[pt*Nc + c] *= mul * w1[j];
1766494e7359SMatthew G. Knepley         }
1767494e7359SMatthew G. Knepley       }
1768494e7359SMatthew G. Knepley     }
1769fbdc3dfeSToby Isaac     totprev *= npoints;
1770fbdc3dfeSToby Isaac     totrem /= npoints;
1771494e7359SMatthew G. Knepley   }
1772fbdc3dfeSToby Isaac   ierr = PetscFree2(p1, w1);CHKERRQ(ierr);
177321454ff5SMatthew G. Knepley   ierr = PetscQuadratureCreate(PETSC_COMM_SELF, q);CHKERRQ(ierr);
17742f5fb066SToby Isaac   ierr = PetscQuadratureSetOrder(*q, 2*npoints-1);CHKERRQ(ierr);
1775dcce0ee2SMatthew G. Knepley   ierr = PetscQuadratureSetData(*q, dim, Nc, totpoints, x, w);CHKERRQ(ierr);
1776fbdc3dfeSToby Isaac   ierr = PetscObjectChangeTypeName((PetscObject)*q,"StroudConical");CHKERRQ(ierr);
1777494e7359SMatthew G. Knepley   PetscFunctionReturn(0);
1778494e7359SMatthew G. Knepley }
1779494e7359SMatthew G. Knepley 
1780f5f57ec0SBarry Smith /*@
1781b3c0f97bSTom Klotz   PetscDTTanhSinhTensorQuadrature - create tanh-sinh quadrature for a tensor product cell
1782b3c0f97bSTom Klotz 
1783b3c0f97bSTom Klotz   Not Collective
1784b3c0f97bSTom Klotz 
1785b3c0f97bSTom Klotz   Input Arguments:
1786b3c0f97bSTom Klotz + dim   - The cell dimension
1787b3c0f97bSTom Klotz . level - The number of points in one dimension, 2^l
1788b3c0f97bSTom Klotz . a     - left end of interval (often-1)
1789b3c0f97bSTom Klotz - b     - right end of interval (often +1)
1790b3c0f97bSTom Klotz 
1791b3c0f97bSTom Klotz   Output Argument:
1792b3c0f97bSTom Klotz . q - A PetscQuadrature object
1793b3c0f97bSTom Klotz 
1794b3c0f97bSTom Klotz   Level: intermediate
1795b3c0f97bSTom Klotz 
1796b3c0f97bSTom Klotz .seealso: PetscDTGaussTensorQuadrature()
1797b3c0f97bSTom Klotz @*/
1798b3c0f97bSTom Klotz PetscErrorCode PetscDTTanhSinhTensorQuadrature(PetscInt dim, PetscInt level, PetscReal a, PetscReal b, PetscQuadrature *q)
1799b3c0f97bSTom Klotz {
1800b3c0f97bSTom Klotz   const PetscInt  p     = 16;                        /* Digits of precision in the evaluation */
1801b3c0f97bSTom Klotz   const PetscReal alpha = (b-a)/2.;                  /* Half-width of the integration interval */
1802b3c0f97bSTom Klotz   const PetscReal beta  = (b+a)/2.;                  /* Center of the integration interval */
1803b3c0f97bSTom Klotz   const PetscReal h     = PetscPowReal(2.0, -level); /* Step size, length between x_k */
1804d84b4d08SMatthew G. Knepley   PetscReal       xk;                                /* Quadrature point x_k on reference domain [-1, 1] */
1805b3c0f97bSTom Klotz   PetscReal       wk    = 0.5*PETSC_PI;              /* Quadrature weight at x_k */
1806b3c0f97bSTom Klotz   PetscReal      *x, *w;
1807b3c0f97bSTom Klotz   PetscInt        K, k, npoints;
1808b3c0f97bSTom Klotz   PetscErrorCode  ierr;
1809b3c0f97bSTom Klotz 
1810b3c0f97bSTom Klotz   PetscFunctionBegin;
1811b3c0f97bSTom Klotz   if (dim > 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %d not yet implemented", dim);
1812b3c0f97bSTom Klotz   if (!level) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a number of significant digits");
1813b3c0f97bSTom Klotz   /* Find K such that the weights are < 32 digits of precision */
1814b3c0f97bSTom Klotz   for (K = 1; PetscAbsReal(PetscLog10Real(wk)) < 2*p; ++K) {
18159add2064SThomas Klotz     wk = 0.5*h*PETSC_PI*PetscCoshReal(K*h)/PetscSqr(PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(K*h)));
1816b3c0f97bSTom Klotz   }
1817b3c0f97bSTom Klotz   ierr = PetscQuadratureCreate(PETSC_COMM_SELF, q);CHKERRQ(ierr);
1818b3c0f97bSTom Klotz   ierr = PetscQuadratureSetOrder(*q, 2*K+1);CHKERRQ(ierr);
1819b3c0f97bSTom Klotz   npoints = 2*K-1;
1820b3c0f97bSTom Klotz   ierr = PetscMalloc1(npoints*dim, &x);CHKERRQ(ierr);
1821b3c0f97bSTom Klotz   ierr = PetscMalloc1(npoints, &w);CHKERRQ(ierr);
1822b3c0f97bSTom Klotz   /* Center term */
1823b3c0f97bSTom Klotz   x[0] = beta;
1824b3c0f97bSTom Klotz   w[0] = 0.5*alpha*PETSC_PI;
1825b3c0f97bSTom Klotz   for (k = 1; k < K; ++k) {
18269add2064SThomas Klotz     wk = 0.5*alpha*h*PETSC_PI*PetscCoshReal(k*h)/PetscSqr(PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(k*h)));
18271118d4bcSLisandro Dalcin     xk = PetscTanhReal(0.5*PETSC_PI*PetscSinhReal(k*h));
1828b3c0f97bSTom Klotz     x[2*k-1] = -alpha*xk+beta;
1829b3c0f97bSTom Klotz     w[2*k-1] = wk;
1830b3c0f97bSTom Klotz     x[2*k+0] =  alpha*xk+beta;
1831b3c0f97bSTom Klotz     w[2*k+0] = wk;
1832b3c0f97bSTom Klotz   }
1833a6b92713SMatthew G. Knepley   ierr = PetscQuadratureSetData(*q, dim, 1, npoints, x, w);CHKERRQ(ierr);
1834b3c0f97bSTom Klotz   PetscFunctionReturn(0);
1835b3c0f97bSTom Klotz }
1836b3c0f97bSTom Klotz 
1837b3c0f97bSTom Klotz PetscErrorCode PetscDTTanhSinhIntegrate(void (*func)(PetscReal, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, PetscReal *sol)
1838b3c0f97bSTom Klotz {
1839b3c0f97bSTom Klotz   const PetscInt  p     = 16;        /* Digits of precision in the evaluation */
1840b3c0f97bSTom Klotz   const PetscReal alpha = (b-a)/2.;  /* Half-width of the integration interval */
1841b3c0f97bSTom Klotz   const PetscReal beta  = (b+a)/2.;  /* Center of the integration interval */
1842b3c0f97bSTom Klotz   PetscReal       h     = 1.0;       /* Step size, length between x_k */
1843b3c0f97bSTom Klotz   PetscInt        l     = 0;         /* Level of refinement, h = 2^{-l} */
1844b3c0f97bSTom Klotz   PetscReal       osum  = 0.0;       /* Integral on last level */
1845b3c0f97bSTom Klotz   PetscReal       psum  = 0.0;       /* Integral on the level before the last level */
1846b3c0f97bSTom Klotz   PetscReal       sum;               /* Integral on current level */
1847446c295cSMatthew G. Knepley   PetscReal       yk;                /* Quadrature point 1 - x_k on reference domain [-1, 1] */
1848b3c0f97bSTom Klotz   PetscReal       lx, rx;            /* Quadrature points to the left and right of 0 on the real domain [a, b] */
1849b3c0f97bSTom Klotz   PetscReal       wk;                /* Quadrature weight at x_k */
1850b3c0f97bSTom Klotz   PetscReal       lval, rval;        /* Terms in the quadature sum to the left and right of 0 */
1851b3c0f97bSTom Klotz   PetscInt        d;                 /* Digits of precision in the integral */
1852b3c0f97bSTom Klotz 
1853b3c0f97bSTom Klotz   PetscFunctionBegin;
1854b3c0f97bSTom Klotz   if (digits <= 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a positive number of significant digits");
1855b3c0f97bSTom Klotz   /* Center term */
1856b3c0f97bSTom Klotz   func(beta, &lval);
1857b3c0f97bSTom Klotz   sum = 0.5*alpha*PETSC_PI*lval;
1858b3c0f97bSTom Klotz   /* */
1859b3c0f97bSTom Klotz   do {
1860b3c0f97bSTom Klotz     PetscReal lterm, rterm, maxTerm = 0.0, d1, d2, d3, d4;
1861b3c0f97bSTom Klotz     PetscInt  k = 1;
1862b3c0f97bSTom Klotz 
1863b3c0f97bSTom Klotz     ++l;
1864b3c0f97bSTom Klotz     /* PetscPrintf(PETSC_COMM_SELF, "LEVEL %D sum: %15.15f\n", l, sum); */
1865b3c0f97bSTom Klotz     /* At each level of refinement, h --> h/2 and sum --> sum/2 */
1866b3c0f97bSTom Klotz     psum = osum;
1867b3c0f97bSTom Klotz     osum = sum;
1868b3c0f97bSTom Klotz     h   *= 0.5;
1869b3c0f97bSTom Klotz     sum *= 0.5;
1870b3c0f97bSTom Klotz     do {
18719add2064SThomas Klotz       wk = 0.5*h*PETSC_PI*PetscCoshReal(k*h)/PetscSqr(PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(k*h)));
1872446c295cSMatthew G. Knepley       yk = 1.0/(PetscExpReal(0.5*PETSC_PI*PetscSinhReal(k*h)) * PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(k*h)));
1873446c295cSMatthew G. Knepley       lx = -alpha*(1.0 - yk)+beta;
1874446c295cSMatthew G. Knepley       rx =  alpha*(1.0 - yk)+beta;
1875b3c0f97bSTom Klotz       func(lx, &lval);
1876b3c0f97bSTom Klotz       func(rx, &rval);
1877b3c0f97bSTom Klotz       lterm   = alpha*wk*lval;
1878b3c0f97bSTom Klotz       maxTerm = PetscMax(PetscAbsReal(lterm), maxTerm);
1879b3c0f97bSTom Klotz       sum    += lterm;
1880b3c0f97bSTom Klotz       rterm   = alpha*wk*rval;
1881b3c0f97bSTom Klotz       maxTerm = PetscMax(PetscAbsReal(rterm), maxTerm);
1882b3c0f97bSTom Klotz       sum    += rterm;
1883b3c0f97bSTom Klotz       ++k;
1884b3c0f97bSTom Klotz       /* Only need to evaluate every other point on refined levels */
1885b3c0f97bSTom Klotz       if (l != 1) ++k;
18869add2064SThomas Klotz     } while (PetscAbsReal(PetscLog10Real(wk)) < p); /* Only need to evaluate sum until weights are < 32 digits of precision */
1887b3c0f97bSTom Klotz 
1888b3c0f97bSTom Klotz     d1 = PetscLog10Real(PetscAbsReal(sum - osum));
1889b3c0f97bSTom Klotz     d2 = PetscLog10Real(PetscAbsReal(sum - psum));
1890b3c0f97bSTom Klotz     d3 = PetscLog10Real(maxTerm) - p;
189109d48545SBarry Smith     if (PetscMax(PetscAbsReal(lterm), PetscAbsReal(rterm)) == 0.0) d4 = 0.0;
189209d48545SBarry Smith     else d4 = PetscLog10Real(PetscMax(PetscAbsReal(lterm), PetscAbsReal(rterm)));
1893b3c0f97bSTom Klotz     d  = PetscAbsInt(PetscMin(0, PetscMax(PetscMax(PetscMax(PetscSqr(d1)/d2, 2*d1), d3), d4)));
18949add2064SThomas Klotz   } while (d < digits && l < 12);
1895b3c0f97bSTom Klotz   *sol = sum;
1896e510cb1fSThomas Klotz 
1897b3c0f97bSTom Klotz   PetscFunctionReturn(0);
1898b3c0f97bSTom Klotz }
1899b3c0f97bSTom Klotz 
1900497880caSRichard Tran Mills #if defined(PETSC_HAVE_MPFR)
190129f144ccSMatthew G. Knepley PetscErrorCode PetscDTTanhSinhIntegrateMPFR(void (*func)(PetscReal, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, PetscReal *sol)
190229f144ccSMatthew G. Knepley {
1903e510cb1fSThomas Klotz   const PetscInt  safetyFactor = 2;  /* Calculate abcissa until 2*p digits */
190429f144ccSMatthew G. Knepley   PetscInt        l            = 0;  /* Level of refinement, h = 2^{-l} */
190529f144ccSMatthew G. Knepley   mpfr_t          alpha;             /* Half-width of the integration interval */
190629f144ccSMatthew G. Knepley   mpfr_t          beta;              /* Center of the integration interval */
190729f144ccSMatthew G. Knepley   mpfr_t          h;                 /* Step size, length between x_k */
190829f144ccSMatthew G. Knepley   mpfr_t          osum;              /* Integral on last level */
190929f144ccSMatthew G. Knepley   mpfr_t          psum;              /* Integral on the level before the last level */
191029f144ccSMatthew G. Knepley   mpfr_t          sum;               /* Integral on current level */
191129f144ccSMatthew G. Knepley   mpfr_t          yk;                /* Quadrature point 1 - x_k on reference domain [-1, 1] */
191229f144ccSMatthew G. Knepley   mpfr_t          lx, rx;            /* Quadrature points to the left and right of 0 on the real domain [a, b] */
191329f144ccSMatthew G. Knepley   mpfr_t          wk;                /* Quadrature weight at x_k */
191429f144ccSMatthew G. Knepley   PetscReal       lval, rval;        /* Terms in the quadature sum to the left and right of 0 */
191529f144ccSMatthew G. Knepley   PetscInt        d;                 /* Digits of precision in the integral */
191629f144ccSMatthew G. Knepley   mpfr_t          pi2, kh, msinh, mcosh, maxTerm, curTerm, tmp;
191729f144ccSMatthew G. Knepley 
191829f144ccSMatthew G. Knepley   PetscFunctionBegin;
191929f144ccSMatthew G. Knepley   if (digits <= 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a positive number of significant digits");
192029f144ccSMatthew G. Knepley   /* Create high precision storage */
1921c9f744b5SMatthew G. Knepley   mpfr_inits2(PetscCeilReal(safetyFactor*digits*PetscLogReal(10.)/PetscLogReal(2.)), alpha, beta, h, sum, osum, psum, yk, wk, lx, rx, tmp, maxTerm, curTerm, pi2, kh, msinh, mcosh, NULL);
192229f144ccSMatthew G. Knepley   /* Initialization */
192329f144ccSMatthew G. Knepley   mpfr_set_d(alpha, 0.5*(b-a), MPFR_RNDN);
192429f144ccSMatthew G. Knepley   mpfr_set_d(beta,  0.5*(b+a), MPFR_RNDN);
192529f144ccSMatthew G. Knepley   mpfr_set_d(osum,  0.0,       MPFR_RNDN);
192629f144ccSMatthew G. Knepley   mpfr_set_d(psum,  0.0,       MPFR_RNDN);
192729f144ccSMatthew G. Knepley   mpfr_set_d(h,     1.0,       MPFR_RNDN);
192829f144ccSMatthew G. Knepley   mpfr_const_pi(pi2, MPFR_RNDN);
192929f144ccSMatthew G. Knepley   mpfr_mul_d(pi2, pi2, 0.5, MPFR_RNDN);
193029f144ccSMatthew G. Knepley   /* Center term */
193129f144ccSMatthew G. Knepley   func(0.5*(b+a), &lval);
193229f144ccSMatthew G. Knepley   mpfr_set(sum, pi2, MPFR_RNDN);
193329f144ccSMatthew G. Knepley   mpfr_mul(sum, sum, alpha, MPFR_RNDN);
193429f144ccSMatthew G. Knepley   mpfr_mul_d(sum, sum, lval, MPFR_RNDN);
193529f144ccSMatthew G. Knepley   /* */
193629f144ccSMatthew G. Knepley   do {
193729f144ccSMatthew G. Knepley     PetscReal d1, d2, d3, d4;
193829f144ccSMatthew G. Knepley     PetscInt  k = 1;
193929f144ccSMatthew G. Knepley 
194029f144ccSMatthew G. Knepley     ++l;
194129f144ccSMatthew G. Knepley     mpfr_set_d(maxTerm, 0.0, MPFR_RNDN);
194229f144ccSMatthew G. Knepley     /* PetscPrintf(PETSC_COMM_SELF, "LEVEL %D sum: %15.15f\n", l, sum); */
194329f144ccSMatthew G. Knepley     /* At each level of refinement, h --> h/2 and sum --> sum/2 */
194429f144ccSMatthew G. Knepley     mpfr_set(psum, osum, MPFR_RNDN);
194529f144ccSMatthew G. Knepley     mpfr_set(osum,  sum, MPFR_RNDN);
194629f144ccSMatthew G. Knepley     mpfr_mul_d(h,   h,   0.5, MPFR_RNDN);
194729f144ccSMatthew G. Knepley     mpfr_mul_d(sum, sum, 0.5, MPFR_RNDN);
194829f144ccSMatthew G. Knepley     do {
194929f144ccSMatthew G. Knepley       mpfr_set_si(kh, k, MPFR_RNDN);
195029f144ccSMatthew G. Knepley       mpfr_mul(kh, kh, h, MPFR_RNDN);
195129f144ccSMatthew G. Knepley       /* Weight */
195229f144ccSMatthew G. Knepley       mpfr_set(wk, h, MPFR_RNDN);
195329f144ccSMatthew G. Knepley       mpfr_sinh_cosh(msinh, mcosh, kh, MPFR_RNDN);
195429f144ccSMatthew G. Knepley       mpfr_mul(msinh, msinh, pi2, MPFR_RNDN);
195529f144ccSMatthew G. Knepley       mpfr_mul(mcosh, mcosh, pi2, MPFR_RNDN);
195629f144ccSMatthew G. Knepley       mpfr_cosh(tmp, msinh, MPFR_RNDN);
195729f144ccSMatthew G. Knepley       mpfr_sqr(tmp, tmp, MPFR_RNDN);
195829f144ccSMatthew G. Knepley       mpfr_mul(wk, wk, mcosh, MPFR_RNDN);
195929f144ccSMatthew G. Knepley       mpfr_div(wk, wk, tmp, MPFR_RNDN);
196029f144ccSMatthew G. Knepley       /* Abscissa */
196129f144ccSMatthew G. Knepley       mpfr_set_d(yk, 1.0, MPFR_RNDZ);
196229f144ccSMatthew G. Knepley       mpfr_cosh(tmp, msinh, MPFR_RNDN);
196329f144ccSMatthew G. Knepley       mpfr_div(yk, yk, tmp, MPFR_RNDZ);
196429f144ccSMatthew G. Knepley       mpfr_exp(tmp, msinh, MPFR_RNDN);
196529f144ccSMatthew G. Knepley       mpfr_div(yk, yk, tmp, MPFR_RNDZ);
196629f144ccSMatthew G. Knepley       /* Quadrature points */
196729f144ccSMatthew G. Knepley       mpfr_sub_d(lx, yk, 1.0, MPFR_RNDZ);
196829f144ccSMatthew G. Knepley       mpfr_mul(lx, lx, alpha, MPFR_RNDU);
196929f144ccSMatthew G. Knepley       mpfr_add(lx, lx, beta, MPFR_RNDU);
197029f144ccSMatthew G. Knepley       mpfr_d_sub(rx, 1.0, yk, MPFR_RNDZ);
197129f144ccSMatthew G. Knepley       mpfr_mul(rx, rx, alpha, MPFR_RNDD);
197229f144ccSMatthew G. Knepley       mpfr_add(rx, rx, beta, MPFR_RNDD);
197329f144ccSMatthew G. Knepley       /* Evaluation */
197429f144ccSMatthew G. Knepley       func(mpfr_get_d(lx, MPFR_RNDU), &lval);
197529f144ccSMatthew G. Knepley       func(mpfr_get_d(rx, MPFR_RNDD), &rval);
197629f144ccSMatthew G. Knepley       /* Update */
197729f144ccSMatthew G. Knepley       mpfr_mul(tmp, wk, alpha, MPFR_RNDN);
197829f144ccSMatthew G. Knepley       mpfr_mul_d(tmp, tmp, lval, MPFR_RNDN);
197929f144ccSMatthew G. Knepley       mpfr_add(sum, sum, tmp, MPFR_RNDN);
198029f144ccSMatthew G. Knepley       mpfr_abs(tmp, tmp, MPFR_RNDN);
198129f144ccSMatthew G. Knepley       mpfr_max(maxTerm, maxTerm, tmp, MPFR_RNDN);
198229f144ccSMatthew G. Knepley       mpfr_set(curTerm, tmp, MPFR_RNDN);
198329f144ccSMatthew G. Knepley       mpfr_mul(tmp, wk, alpha, MPFR_RNDN);
198429f144ccSMatthew G. Knepley       mpfr_mul_d(tmp, tmp, rval, MPFR_RNDN);
198529f144ccSMatthew G. Knepley       mpfr_add(sum, sum, tmp, MPFR_RNDN);
198629f144ccSMatthew G. Knepley       mpfr_abs(tmp, tmp, MPFR_RNDN);
198729f144ccSMatthew G. Knepley       mpfr_max(maxTerm, maxTerm, tmp, MPFR_RNDN);
198829f144ccSMatthew G. Knepley       mpfr_max(curTerm, curTerm, tmp, MPFR_RNDN);
198929f144ccSMatthew G. Knepley       ++k;
199029f144ccSMatthew G. Knepley       /* Only need to evaluate every other point on refined levels */
199129f144ccSMatthew G. Knepley       if (l != 1) ++k;
199229f144ccSMatthew G. Knepley       mpfr_log10(tmp, wk, MPFR_RNDN);
199329f144ccSMatthew G. Knepley       mpfr_abs(tmp, tmp, MPFR_RNDN);
1994c9f744b5SMatthew G. Knepley     } while (mpfr_get_d(tmp, MPFR_RNDN) < safetyFactor*digits); /* Only need to evaluate sum until weights are < 32 digits of precision */
199529f144ccSMatthew G. Knepley     mpfr_sub(tmp, sum, osum, MPFR_RNDN);
199629f144ccSMatthew G. Knepley     mpfr_abs(tmp, tmp, MPFR_RNDN);
199729f144ccSMatthew G. Knepley     mpfr_log10(tmp, tmp, MPFR_RNDN);
199829f144ccSMatthew G. Knepley     d1 = mpfr_get_d(tmp, MPFR_RNDN);
199929f144ccSMatthew G. Knepley     mpfr_sub(tmp, sum, psum, MPFR_RNDN);
200029f144ccSMatthew G. Knepley     mpfr_abs(tmp, tmp, MPFR_RNDN);
200129f144ccSMatthew G. Knepley     mpfr_log10(tmp, tmp, MPFR_RNDN);
200229f144ccSMatthew G. Knepley     d2 = mpfr_get_d(tmp, MPFR_RNDN);
200329f144ccSMatthew G. Knepley     mpfr_log10(tmp, maxTerm, MPFR_RNDN);
2004c9f744b5SMatthew G. Knepley     d3 = mpfr_get_d(tmp, MPFR_RNDN) - digits;
200529f144ccSMatthew G. Knepley     mpfr_log10(tmp, curTerm, MPFR_RNDN);
200629f144ccSMatthew G. Knepley     d4 = mpfr_get_d(tmp, MPFR_RNDN);
200729f144ccSMatthew G. Knepley     d  = PetscAbsInt(PetscMin(0, PetscMax(PetscMax(PetscMax(PetscSqr(d1)/d2, 2*d1), d3), d4)));
2008b0649871SThomas Klotz   } while (d < digits && l < 8);
200929f144ccSMatthew G. Knepley   *sol = mpfr_get_d(sum, MPFR_RNDN);
201029f144ccSMatthew G. Knepley   /* Cleanup */
201129f144ccSMatthew G. Knepley   mpfr_clears(alpha, beta, h, sum, osum, psum, yk, wk, lx, rx, tmp, maxTerm, curTerm, pi2, kh, msinh, mcosh, NULL);
201229f144ccSMatthew G. Knepley   PetscFunctionReturn(0);
201329f144ccSMatthew G. Knepley }
2014d525116cSMatthew G. Knepley #else
2015fbfcfee5SBarry Smith 
2016d525116cSMatthew G. Knepley PetscErrorCode PetscDTTanhSinhIntegrateMPFR(void (*func)(PetscReal, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, PetscReal *sol)
2017d525116cSMatthew G. Knepley {
2018d525116cSMatthew G. Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "This method will not work without MPFR. Reconfigure using --download-mpfr --download-gmp");
2019d525116cSMatthew G. Knepley }
202029f144ccSMatthew G. Knepley #endif
202129f144ccSMatthew G. Knepley 
2022194825f6SJed Brown /* Overwrites A. Can only handle full-rank problems with m>=n
2023194825f6SJed Brown  * A in column-major format
2024194825f6SJed Brown  * Ainv in row-major format
2025194825f6SJed Brown  * tau has length m
2026194825f6SJed Brown  * worksize must be >= max(1,n)
2027194825f6SJed Brown  */
2028194825f6SJed Brown static PetscErrorCode PetscDTPseudoInverseQR(PetscInt m,PetscInt mstride,PetscInt n,PetscReal *A_in,PetscReal *Ainv_out,PetscScalar *tau,PetscInt worksize,PetscScalar *work)
2029194825f6SJed Brown {
2030194825f6SJed Brown   PetscErrorCode ierr;
2031194825f6SJed Brown   PetscBLASInt   M,N,K,lda,ldb,ldwork,info;
2032194825f6SJed Brown   PetscScalar    *A,*Ainv,*R,*Q,Alpha;
2033194825f6SJed Brown 
2034194825f6SJed Brown   PetscFunctionBegin;
2035194825f6SJed Brown #if defined(PETSC_USE_COMPLEX)
2036194825f6SJed Brown   {
2037194825f6SJed Brown     PetscInt i,j;
2038dcca6d9dSJed Brown     ierr = PetscMalloc2(m*n,&A,m*n,&Ainv);CHKERRQ(ierr);
2039194825f6SJed Brown     for (j=0; j<n; j++) {
2040194825f6SJed Brown       for (i=0; i<m; i++) A[i+m*j] = A_in[i+mstride*j];
2041194825f6SJed Brown     }
2042194825f6SJed Brown     mstride = m;
2043194825f6SJed Brown   }
2044194825f6SJed Brown #else
2045194825f6SJed Brown   A = A_in;
2046194825f6SJed Brown   Ainv = Ainv_out;
2047194825f6SJed Brown #endif
2048194825f6SJed Brown 
2049194825f6SJed Brown   ierr = PetscBLASIntCast(m,&M);CHKERRQ(ierr);
2050194825f6SJed Brown   ierr = PetscBLASIntCast(n,&N);CHKERRQ(ierr);
2051194825f6SJed Brown   ierr = PetscBLASIntCast(mstride,&lda);CHKERRQ(ierr);
2052194825f6SJed Brown   ierr = PetscBLASIntCast(worksize,&ldwork);CHKERRQ(ierr);
2053194825f6SJed Brown   ierr = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr);
2054001a771dSBarry Smith   PetscStackCallBLAS("LAPACKgeqrf",LAPACKgeqrf_(&M,&N,A,&lda,tau,work,&ldwork,&info));
2055194825f6SJed Brown   ierr = PetscFPTrapPop();CHKERRQ(ierr);
2056194825f6SJed Brown   if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"xGEQRF error");
2057194825f6SJed Brown   R = A; /* Upper triangular part of A now contains R, the rest contains the elementary reflectors */
2058194825f6SJed Brown 
2059194825f6SJed Brown   /* Extract an explicit representation of Q */
2060194825f6SJed Brown   Q = Ainv;
2061580bdb30SBarry Smith   ierr = PetscArraycpy(Q,A,mstride*n);CHKERRQ(ierr);
2062194825f6SJed Brown   K = N;                        /* full rank */
2063c964aadfSJose E. Roman   PetscStackCallBLAS("LAPACKorgqr",LAPACKorgqr_(&M,&N,&K,Q,&lda,tau,work,&ldwork,&info));
2064194825f6SJed Brown   if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"xORGQR/xUNGQR error");
2065194825f6SJed Brown 
2066194825f6SJed Brown   /* Compute A^{-T} = (R^{-1} Q^T)^T = Q R^{-T} */
2067194825f6SJed Brown   Alpha = 1.0;
2068194825f6SJed Brown   ldb = lda;
2069001a771dSBarry Smith   PetscStackCallBLAS("BLAStrsm",BLAStrsm_("Right","Upper","ConjugateTranspose","NotUnitTriangular",&M,&N,&Alpha,R,&lda,Q,&ldb));
2070194825f6SJed Brown   /* Ainv is Q, overwritten with inverse */
2071194825f6SJed Brown 
2072194825f6SJed Brown #if defined(PETSC_USE_COMPLEX)
2073194825f6SJed Brown   {
2074194825f6SJed Brown     PetscInt i;
2075194825f6SJed Brown     for (i=0; i<m*n; i++) Ainv_out[i] = PetscRealPart(Ainv[i]);
2076194825f6SJed Brown     ierr = PetscFree2(A,Ainv);CHKERRQ(ierr);
2077194825f6SJed Brown   }
2078194825f6SJed Brown #endif
2079194825f6SJed Brown   PetscFunctionReturn(0);
2080194825f6SJed Brown }
2081194825f6SJed Brown 
2082194825f6SJed Brown /* Computes integral of L_p' over intervals {(x0,x1),(x1,x2),...} */
2083194825f6SJed Brown static PetscErrorCode PetscDTLegendreIntegrate(PetscInt ninterval,const PetscReal *x,PetscInt ndegree,const PetscInt *degrees,PetscBool Transpose,PetscReal *B)
2084194825f6SJed Brown {
2085194825f6SJed Brown   PetscErrorCode ierr;
2086194825f6SJed Brown   PetscReal      *Bv;
2087194825f6SJed Brown   PetscInt       i,j;
2088194825f6SJed Brown 
2089194825f6SJed Brown   PetscFunctionBegin;
2090785e854fSJed Brown   ierr = PetscMalloc1((ninterval+1)*ndegree,&Bv);CHKERRQ(ierr);
2091194825f6SJed Brown   /* Point evaluation of L_p on all the source vertices */
2092194825f6SJed Brown   ierr = PetscDTLegendreEval(ninterval+1,x,ndegree,degrees,Bv,NULL,NULL);CHKERRQ(ierr);
2093194825f6SJed Brown   /* Integral over each interval: \int_a^b L_p' = L_p(b)-L_p(a) */
2094194825f6SJed Brown   for (i=0; i<ninterval; i++) {
2095194825f6SJed Brown     for (j=0; j<ndegree; j++) {
2096194825f6SJed Brown       if (Transpose) B[i+ninterval*j] = Bv[(i+1)*ndegree+j] - Bv[i*ndegree+j];
2097194825f6SJed Brown       else           B[i*ndegree+j]   = Bv[(i+1)*ndegree+j] - Bv[i*ndegree+j];
2098194825f6SJed Brown     }
2099194825f6SJed Brown   }
2100194825f6SJed Brown   ierr = PetscFree(Bv);CHKERRQ(ierr);
2101194825f6SJed Brown   PetscFunctionReturn(0);
2102194825f6SJed Brown }
2103194825f6SJed Brown 
2104194825f6SJed Brown /*@
2105194825f6SJed Brown    PetscDTReconstructPoly - create matrix representing polynomial reconstruction using cell intervals and evaluation at target intervals
2106194825f6SJed Brown 
2107194825f6SJed Brown    Not Collective
2108194825f6SJed Brown 
2109194825f6SJed Brown    Input Arguments:
2110194825f6SJed Brown +  degree - degree of reconstruction polynomial
2111194825f6SJed Brown .  nsource - number of source intervals
2112194825f6SJed Brown .  sourcex - sorted coordinates of source cell boundaries (length nsource+1)
2113194825f6SJed Brown .  ntarget - number of target intervals
2114194825f6SJed Brown -  targetx - sorted coordinates of target cell boundaries (length ntarget+1)
2115194825f6SJed Brown 
2116194825f6SJed Brown    Output Arguments:
2117194825f6SJed Brown .  R - reconstruction matrix, utarget = sum_s R[t*nsource+s] * usource[s]
2118194825f6SJed Brown 
2119194825f6SJed Brown    Level: advanced
2120194825f6SJed Brown 
2121194825f6SJed Brown .seealso: PetscDTLegendreEval()
2122194825f6SJed Brown @*/
2123194825f6SJed Brown PetscErrorCode PetscDTReconstructPoly(PetscInt degree,PetscInt nsource,const PetscReal *sourcex,PetscInt ntarget,const PetscReal *targetx,PetscReal *R)
2124194825f6SJed Brown {
2125194825f6SJed Brown   PetscErrorCode ierr;
2126194825f6SJed Brown   PetscInt       i,j,k,*bdegrees,worksize;
2127194825f6SJed Brown   PetscReal      xmin,xmax,center,hscale,*sourcey,*targety,*Bsource,*Bsinv,*Btarget;
2128194825f6SJed Brown   PetscScalar    *tau,*work;
2129194825f6SJed Brown 
2130194825f6SJed Brown   PetscFunctionBegin;
2131194825f6SJed Brown   PetscValidRealPointer(sourcex,3);
2132194825f6SJed Brown   PetscValidRealPointer(targetx,5);
2133194825f6SJed Brown   PetscValidRealPointer(R,6);
2134194825f6SJed Brown   if (degree >= nsource) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Reconstruction degree %D must be less than number of source intervals %D",degree,nsource);
213576bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
2136194825f6SJed Brown     for (i=0; i<nsource; i++) {
213757622a8eSBarry Smith       if (sourcex[i] >= sourcex[i+1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Source interval %D has negative orientation (%g,%g)",i,(double)sourcex[i],(double)sourcex[i+1]);
2138194825f6SJed Brown     }
2139194825f6SJed Brown     for (i=0; i<ntarget; i++) {
214057622a8eSBarry Smith       if (targetx[i] >= targetx[i+1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Target interval %D has negative orientation (%g,%g)",i,(double)targetx[i],(double)targetx[i+1]);
2141194825f6SJed Brown     }
214276bd3646SJed Brown   }
2143194825f6SJed Brown   xmin = PetscMin(sourcex[0],targetx[0]);
2144194825f6SJed Brown   xmax = PetscMax(sourcex[nsource],targetx[ntarget]);
2145194825f6SJed Brown   center = (xmin + xmax)/2;
2146194825f6SJed Brown   hscale = (xmax - xmin)/2;
2147194825f6SJed Brown   worksize = nsource;
2148dcca6d9dSJed Brown   ierr = PetscMalloc4(degree+1,&bdegrees,nsource+1,&sourcey,nsource*(degree+1),&Bsource,worksize,&work);CHKERRQ(ierr);
2149dcca6d9dSJed Brown   ierr = PetscMalloc4(nsource,&tau,nsource*(degree+1),&Bsinv,ntarget+1,&targety,ntarget*(degree+1),&Btarget);CHKERRQ(ierr);
2150194825f6SJed Brown   for (i=0; i<=nsource; i++) sourcey[i] = (sourcex[i]-center)/hscale;
2151194825f6SJed Brown   for (i=0; i<=degree; i++) bdegrees[i] = i+1;
2152194825f6SJed Brown   ierr = PetscDTLegendreIntegrate(nsource,sourcey,degree+1,bdegrees,PETSC_TRUE,Bsource);CHKERRQ(ierr);
2153194825f6SJed Brown   ierr = PetscDTPseudoInverseQR(nsource,nsource,degree+1,Bsource,Bsinv,tau,nsource,work);CHKERRQ(ierr);
2154194825f6SJed Brown   for (i=0; i<=ntarget; i++) targety[i] = (targetx[i]-center)/hscale;
2155194825f6SJed Brown   ierr = PetscDTLegendreIntegrate(ntarget,targety,degree+1,bdegrees,PETSC_FALSE,Btarget);CHKERRQ(ierr);
2156194825f6SJed Brown   for (i=0; i<ntarget; i++) {
2157194825f6SJed Brown     PetscReal rowsum = 0;
2158194825f6SJed Brown     for (j=0; j<nsource; j++) {
2159194825f6SJed Brown       PetscReal sum = 0;
2160194825f6SJed Brown       for (k=0; k<degree+1; k++) {
2161194825f6SJed Brown         sum += Btarget[i*(degree+1)+k] * Bsinv[k*nsource+j];
2162194825f6SJed Brown       }
2163194825f6SJed Brown       R[i*nsource+j] = sum;
2164194825f6SJed Brown       rowsum += sum;
2165194825f6SJed Brown     }
2166194825f6SJed Brown     for (j=0; j<nsource; j++) R[i*nsource+j] /= rowsum; /* normalize each row */
2167194825f6SJed Brown   }
2168194825f6SJed Brown   ierr = PetscFree4(bdegrees,sourcey,Bsource,work);CHKERRQ(ierr);
2169194825f6SJed Brown   ierr = PetscFree4(tau,Bsinv,targety,Btarget);CHKERRQ(ierr);
2170194825f6SJed Brown   PetscFunctionReturn(0);
2171194825f6SJed Brown }
2172916e780bShannah_mairs 
2173916e780bShannah_mairs /*@C
2174916e780bShannah_mairs    PetscGaussLobattoLegendreIntegrate - Compute the L2 integral of a function on the GLL points
2175916e780bShannah_mairs 
2176916e780bShannah_mairs    Not Collective
2177916e780bShannah_mairs 
2178916e780bShannah_mairs    Input Parameter:
2179916e780bShannah_mairs +  n - the number of GLL nodes
2180916e780bShannah_mairs .  nodes - the GLL nodes
2181916e780bShannah_mairs .  weights - the GLL weights
2182f0fc11ceSJed Brown -  f - the function values at the nodes
2183916e780bShannah_mairs 
2184916e780bShannah_mairs    Output Parameter:
2185916e780bShannah_mairs .  in - the value of the integral
2186916e780bShannah_mairs 
2187916e780bShannah_mairs    Level: beginner
2188916e780bShannah_mairs 
2189916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature()
2190916e780bShannah_mairs 
2191916e780bShannah_mairs @*/
2192916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreIntegrate(PetscInt n,PetscReal *nodes,PetscReal *weights,const PetscReal *f,PetscReal *in)
2193916e780bShannah_mairs {
2194916e780bShannah_mairs   PetscInt          i;
2195916e780bShannah_mairs 
2196916e780bShannah_mairs   PetscFunctionBegin;
2197916e780bShannah_mairs   *in = 0.;
2198916e780bShannah_mairs   for (i=0; i<n; i++) {
2199916e780bShannah_mairs     *in += f[i]*f[i]*weights[i];
2200916e780bShannah_mairs   }
2201916e780bShannah_mairs   PetscFunctionReturn(0);
2202916e780bShannah_mairs }
2203916e780bShannah_mairs 
2204916e780bShannah_mairs /*@C
2205916e780bShannah_mairs    PetscGaussLobattoLegendreElementLaplacianCreate - computes the Laplacian for a single 1d GLL element
2206916e780bShannah_mairs 
2207916e780bShannah_mairs    Not Collective
2208916e780bShannah_mairs 
2209916e780bShannah_mairs    Input Parameter:
2210916e780bShannah_mairs +  n - the number of GLL nodes
2211916e780bShannah_mairs .  nodes - the GLL nodes
2212f0fc11ceSJed Brown -  weights - the GLL weights
2213916e780bShannah_mairs 
2214916e780bShannah_mairs    Output Parameter:
2215916e780bShannah_mairs .  A - the stiffness element
2216916e780bShannah_mairs 
2217916e780bShannah_mairs    Level: beginner
2218916e780bShannah_mairs 
2219916e780bShannah_mairs    Notes:
2220916e780bShannah_mairs     Destroy this with PetscGaussLobattoLegendreElementLaplacianDestroy()
2221916e780bShannah_mairs 
2222916e780bShannah_mairs    You can access entries in this array with AA[i][j] but in memory it is stored in contiguous memory, row oriented (the array is symmetric)
2223916e780bShannah_mairs 
2224916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianDestroy()
2225916e780bShannah_mairs 
2226916e780bShannah_mairs @*/
2227916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementLaplacianCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA)
2228916e780bShannah_mairs {
2229916e780bShannah_mairs   PetscReal        **A;
2230916e780bShannah_mairs   PetscErrorCode  ierr;
2231916e780bShannah_mairs   const PetscReal  *gllnodes = nodes;
2232916e780bShannah_mairs   const PetscInt   p = n-1;
2233916e780bShannah_mairs   PetscReal        z0,z1,z2 = -1,x,Lpj,Lpr;
2234916e780bShannah_mairs   PetscInt         i,j,nn,r;
2235916e780bShannah_mairs 
2236916e780bShannah_mairs   PetscFunctionBegin;
2237916e780bShannah_mairs   ierr = PetscMalloc1(n,&A);CHKERRQ(ierr);
2238916e780bShannah_mairs   ierr = PetscMalloc1(n*n,&A[0]);CHKERRQ(ierr);
2239916e780bShannah_mairs   for (i=1; i<n; i++) A[i] = A[i-1]+n;
2240916e780bShannah_mairs 
2241916e780bShannah_mairs   for (j=1; j<p; j++) {
2242916e780bShannah_mairs     x  = gllnodes[j];
2243916e780bShannah_mairs     z0 = 1.;
2244916e780bShannah_mairs     z1 = x;
2245916e780bShannah_mairs     for (nn=1; nn<p; nn++) {
2246916e780bShannah_mairs       z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.));
2247916e780bShannah_mairs       z0 = z1;
2248916e780bShannah_mairs       z1 = z2;
2249916e780bShannah_mairs     }
2250916e780bShannah_mairs     Lpj=z2;
2251916e780bShannah_mairs     for (r=1; r<p; r++) {
2252916e780bShannah_mairs       if (r == j) {
2253916e780bShannah_mairs         A[j][j]=2./(3.*(1.-gllnodes[j]*gllnodes[j])*Lpj*Lpj);
2254916e780bShannah_mairs       } else {
2255916e780bShannah_mairs         x  = gllnodes[r];
2256916e780bShannah_mairs         z0 = 1.;
2257916e780bShannah_mairs         z1 = x;
2258916e780bShannah_mairs         for (nn=1; nn<p; nn++) {
2259916e780bShannah_mairs           z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.));
2260916e780bShannah_mairs           z0 = z1;
2261916e780bShannah_mairs           z1 = z2;
2262916e780bShannah_mairs         }
2263916e780bShannah_mairs         Lpr     = z2;
2264916e780bShannah_mairs         A[r][j] = 4./(((PetscReal)p)*(((PetscReal)p)+1.)*Lpj*Lpr*(gllnodes[j]-gllnodes[r])*(gllnodes[j]-gllnodes[r]));
2265916e780bShannah_mairs       }
2266916e780bShannah_mairs     }
2267916e780bShannah_mairs   }
2268916e780bShannah_mairs   for (j=1; j<p+1; j++) {
2269916e780bShannah_mairs     x  = gllnodes[j];
2270916e780bShannah_mairs     z0 = 1.;
2271916e780bShannah_mairs     z1 = x;
2272916e780bShannah_mairs     for (nn=1; nn<p; nn++) {
2273916e780bShannah_mairs       z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.));
2274916e780bShannah_mairs       z0 = z1;
2275916e780bShannah_mairs       z1 = z2;
2276916e780bShannah_mairs     }
2277916e780bShannah_mairs     Lpj     = z2;
2278916e780bShannah_mairs     A[j][0] = 4.*PetscPowRealInt(-1.,p)/(((PetscReal)p)*(((PetscReal)p)+1.)*Lpj*(1.+gllnodes[j])*(1.+gllnodes[j]));
2279916e780bShannah_mairs     A[0][j] = A[j][0];
2280916e780bShannah_mairs   }
2281916e780bShannah_mairs   for (j=0; j<p; j++) {
2282916e780bShannah_mairs     x  = gllnodes[j];
2283916e780bShannah_mairs     z0 = 1.;
2284916e780bShannah_mairs     z1 = x;
2285916e780bShannah_mairs     for (nn=1; nn<p; nn++) {
2286916e780bShannah_mairs       z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.));
2287916e780bShannah_mairs       z0 = z1;
2288916e780bShannah_mairs       z1 = z2;
2289916e780bShannah_mairs     }
2290916e780bShannah_mairs     Lpj=z2;
2291916e780bShannah_mairs 
2292916e780bShannah_mairs     A[p][j] = 4./(((PetscReal)p)*(((PetscReal)p)+1.)*Lpj*(1.-gllnodes[j])*(1.-gllnodes[j]));
2293916e780bShannah_mairs     A[j][p] = A[p][j];
2294916e780bShannah_mairs   }
2295916e780bShannah_mairs   A[0][0]=0.5+(((PetscReal)p)*(((PetscReal)p)+1.)-2.)/6.;
2296916e780bShannah_mairs   A[p][p]=A[0][0];
2297916e780bShannah_mairs   *AA = A;
2298916e780bShannah_mairs   PetscFunctionReturn(0);
2299916e780bShannah_mairs }
2300916e780bShannah_mairs 
2301916e780bShannah_mairs /*@C
2302916e780bShannah_mairs    PetscGaussLobattoLegendreElementLaplacianDestroy - frees the Laplacian for a single 1d GLL element
2303916e780bShannah_mairs 
2304916e780bShannah_mairs    Not Collective
2305916e780bShannah_mairs 
2306916e780bShannah_mairs    Input Parameter:
2307916e780bShannah_mairs +  n - the number of GLL nodes
2308916e780bShannah_mairs .  nodes - the GLL nodes
2309916e780bShannah_mairs .  weights - the GLL weightss
2310916e780bShannah_mairs -  A - the stiffness element
2311916e780bShannah_mairs 
2312916e780bShannah_mairs    Level: beginner
2313916e780bShannah_mairs 
2314916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianCreate()
2315916e780bShannah_mairs 
2316916e780bShannah_mairs @*/
2317916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementLaplacianDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA)
2318916e780bShannah_mairs {
2319916e780bShannah_mairs   PetscErrorCode ierr;
2320916e780bShannah_mairs 
2321916e780bShannah_mairs   PetscFunctionBegin;
2322916e780bShannah_mairs   ierr = PetscFree((*AA)[0]);CHKERRQ(ierr);
2323916e780bShannah_mairs   ierr = PetscFree(*AA);CHKERRQ(ierr);
2324916e780bShannah_mairs   *AA  = NULL;
2325916e780bShannah_mairs   PetscFunctionReturn(0);
2326916e780bShannah_mairs }
2327916e780bShannah_mairs 
2328916e780bShannah_mairs /*@C
2329916e780bShannah_mairs    PetscGaussLobattoLegendreElementGradientCreate - computes the gradient for a single 1d GLL element
2330916e780bShannah_mairs 
2331916e780bShannah_mairs    Not Collective
2332916e780bShannah_mairs 
2333916e780bShannah_mairs    Input Parameter:
2334916e780bShannah_mairs +  n - the number of GLL nodes
2335916e780bShannah_mairs .  nodes - the GLL nodes
2336916e780bShannah_mairs .  weights - the GLL weights
2337916e780bShannah_mairs 
2338916e780bShannah_mairs    Output Parameter:
2339916e780bShannah_mairs .  AA - the stiffness element
2340916e780bShannah_mairs -  AAT - the transpose of AA (pass in NULL if you do not need this array)
2341916e780bShannah_mairs 
2342916e780bShannah_mairs    Level: beginner
2343916e780bShannah_mairs 
2344916e780bShannah_mairs    Notes:
2345916e780bShannah_mairs     Destroy this with PetscGaussLobattoLegendreElementGradientDestroy()
2346916e780bShannah_mairs 
2347916e780bShannah_mairs    You can access entries in these arrays with AA[i][j] but in memory it is stored in contiguous memory, row oriented
2348916e780bShannah_mairs 
2349916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianDestroy()
2350916e780bShannah_mairs 
2351916e780bShannah_mairs @*/
2352916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementGradientCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA,PetscReal ***AAT)
2353916e780bShannah_mairs {
2354916e780bShannah_mairs   PetscReal        **A, **AT = NULL;
2355916e780bShannah_mairs   PetscErrorCode  ierr;
2356916e780bShannah_mairs   const PetscReal  *gllnodes = nodes;
2357916e780bShannah_mairs   const PetscInt   p = n-1;
2358e6a796c3SToby Isaac   PetscReal        Li, Lj,d0;
2359916e780bShannah_mairs   PetscInt         i,j;
2360916e780bShannah_mairs 
2361916e780bShannah_mairs   PetscFunctionBegin;
2362916e780bShannah_mairs   ierr = PetscMalloc1(n,&A);CHKERRQ(ierr);
2363916e780bShannah_mairs   ierr = PetscMalloc1(n*n,&A[0]);CHKERRQ(ierr);
2364916e780bShannah_mairs   for (i=1; i<n; i++) A[i] = A[i-1]+n;
2365916e780bShannah_mairs 
2366916e780bShannah_mairs   if (AAT) {
2367916e780bShannah_mairs     ierr = PetscMalloc1(n,&AT);CHKERRQ(ierr);
2368916e780bShannah_mairs     ierr = PetscMalloc1(n*n,&AT[0]);CHKERRQ(ierr);
2369916e780bShannah_mairs     for (i=1; i<n; i++) AT[i] = AT[i-1]+n;
2370916e780bShannah_mairs   }
2371916e780bShannah_mairs 
2372916e780bShannah_mairs   if (n==1) {A[0][0] = 0.;}
2373916e780bShannah_mairs   d0 = (PetscReal)p*((PetscReal)p+1.)/4.;
2374916e780bShannah_mairs   for  (i=0; i<n; i++) {
2375916e780bShannah_mairs     for  (j=0; j<n; j++) {
2376916e780bShannah_mairs       A[i][j] = 0.;
2377e6a796c3SToby Isaac       ierr = PetscDTComputeJacobi(0., 0., p, gllnodes[i], &Li);CHKERRQ(ierr);
2378e6a796c3SToby Isaac       ierr = PetscDTComputeJacobi(0., 0., p, gllnodes[j], &Lj);CHKERRQ(ierr);
2379916e780bShannah_mairs       if (i!=j)             A[i][j] = Li/(Lj*(gllnodes[i]-gllnodes[j]));
2380916e780bShannah_mairs       if ((j==i) && (i==0)) A[i][j] = -d0;
2381916e780bShannah_mairs       if (j==i && i==p)     A[i][j] = d0;
2382916e780bShannah_mairs       if (AT) AT[j][i] = A[i][j];
2383916e780bShannah_mairs     }
2384916e780bShannah_mairs   }
2385916e780bShannah_mairs   if (AAT) *AAT = AT;
2386916e780bShannah_mairs   *AA  = A;
2387916e780bShannah_mairs   PetscFunctionReturn(0);
2388916e780bShannah_mairs }
2389916e780bShannah_mairs 
2390916e780bShannah_mairs /*@C
2391916e780bShannah_mairs    PetscGaussLobattoLegendreElementGradientDestroy - frees the gradient for a single 1d GLL element obtained with PetscGaussLobattoLegendreElementGradientCreate()
2392916e780bShannah_mairs 
2393916e780bShannah_mairs    Not Collective
2394916e780bShannah_mairs 
2395916e780bShannah_mairs    Input Parameter:
2396916e780bShannah_mairs +  n - the number of GLL nodes
2397916e780bShannah_mairs .  nodes - the GLL nodes
2398916e780bShannah_mairs .  weights - the GLL weights
2399916e780bShannah_mairs .  AA - the stiffness element
2400916e780bShannah_mairs -  AAT - the transpose of the element
2401916e780bShannah_mairs 
2402916e780bShannah_mairs    Level: beginner
2403916e780bShannah_mairs 
2404916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianCreate(), PetscGaussLobattoLegendreElementAdvectionCreate()
2405916e780bShannah_mairs 
2406916e780bShannah_mairs @*/
2407916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementGradientDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA,PetscReal ***AAT)
2408916e780bShannah_mairs {
2409916e780bShannah_mairs   PetscErrorCode ierr;
2410916e780bShannah_mairs 
2411916e780bShannah_mairs   PetscFunctionBegin;
2412916e780bShannah_mairs   ierr = PetscFree((*AA)[0]);CHKERRQ(ierr);
2413916e780bShannah_mairs   ierr = PetscFree(*AA);CHKERRQ(ierr);
2414916e780bShannah_mairs   *AA  = NULL;
2415916e780bShannah_mairs   if (*AAT) {
2416916e780bShannah_mairs     ierr = PetscFree((*AAT)[0]);CHKERRQ(ierr);
2417916e780bShannah_mairs     ierr = PetscFree(*AAT);CHKERRQ(ierr);
2418916e780bShannah_mairs     *AAT  = NULL;
2419916e780bShannah_mairs   }
2420916e780bShannah_mairs   PetscFunctionReturn(0);
2421916e780bShannah_mairs }
2422916e780bShannah_mairs 
2423916e780bShannah_mairs /*@C
2424916e780bShannah_mairs    PetscGaussLobattoLegendreElementAdvectionCreate - computes the advection operator for a single 1d GLL element
2425916e780bShannah_mairs 
2426916e780bShannah_mairs    Not Collective
2427916e780bShannah_mairs 
2428916e780bShannah_mairs    Input Parameter:
2429916e780bShannah_mairs +  n - the number of GLL nodes
2430916e780bShannah_mairs .  nodes - the GLL nodes
2431f0fc11ceSJed Brown -  weights - the GLL weightss
2432916e780bShannah_mairs 
2433916e780bShannah_mairs    Output Parameter:
2434916e780bShannah_mairs .  AA - the stiffness element
2435916e780bShannah_mairs 
2436916e780bShannah_mairs    Level: beginner
2437916e780bShannah_mairs 
2438916e780bShannah_mairs    Notes:
2439916e780bShannah_mairs     Destroy this with PetscGaussLobattoLegendreElementAdvectionDestroy()
2440916e780bShannah_mairs 
2441916e780bShannah_mairs    This is the same as the Gradient operator multiplied by the diagonal mass matrix
2442916e780bShannah_mairs 
2443916e780bShannah_mairs    You can access entries in this array with AA[i][j] but in memory it is stored in contiguous memory, row oriented
2444916e780bShannah_mairs 
2445916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianCreate(), PetscGaussLobattoLegendreElementAdvectionDestroy()
2446916e780bShannah_mairs 
2447916e780bShannah_mairs @*/
2448916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementAdvectionCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA)
2449916e780bShannah_mairs {
2450916e780bShannah_mairs   PetscReal       **D;
2451916e780bShannah_mairs   PetscErrorCode  ierr;
2452916e780bShannah_mairs   const PetscReal  *gllweights = weights;
2453916e780bShannah_mairs   const PetscInt   glln = n;
2454916e780bShannah_mairs   PetscInt         i,j;
2455916e780bShannah_mairs 
2456916e780bShannah_mairs   PetscFunctionBegin;
2457916e780bShannah_mairs   ierr = PetscGaussLobattoLegendreElementGradientCreate(n,nodes,weights,&D,NULL);CHKERRQ(ierr);
2458916e780bShannah_mairs   for (i=0; i<glln; i++){
2459916e780bShannah_mairs     for (j=0; j<glln; j++) {
2460916e780bShannah_mairs       D[i][j] = gllweights[i]*D[i][j];
2461916e780bShannah_mairs     }
2462916e780bShannah_mairs   }
2463916e780bShannah_mairs   *AA = D;
2464916e780bShannah_mairs   PetscFunctionReturn(0);
2465916e780bShannah_mairs }
2466916e780bShannah_mairs 
2467916e780bShannah_mairs /*@C
2468916e780bShannah_mairs    PetscGaussLobattoLegendreElementAdvectionDestroy - frees the advection stiffness for a single 1d GLL element
2469916e780bShannah_mairs 
2470916e780bShannah_mairs    Not Collective
2471916e780bShannah_mairs 
2472916e780bShannah_mairs    Input Parameter:
2473916e780bShannah_mairs +  n - the number of GLL nodes
2474916e780bShannah_mairs .  nodes - the GLL nodes
2475916e780bShannah_mairs .  weights - the GLL weights
2476916e780bShannah_mairs -  A - advection
2477916e780bShannah_mairs 
2478916e780bShannah_mairs    Level: beginner
2479916e780bShannah_mairs 
2480916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementAdvectionCreate()
2481916e780bShannah_mairs 
2482916e780bShannah_mairs @*/
2483916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementAdvectionDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA)
2484916e780bShannah_mairs {
2485916e780bShannah_mairs   PetscErrorCode ierr;
2486916e780bShannah_mairs 
2487916e780bShannah_mairs   PetscFunctionBegin;
2488916e780bShannah_mairs   ierr = PetscFree((*AA)[0]);CHKERRQ(ierr);
2489916e780bShannah_mairs   ierr = PetscFree(*AA);CHKERRQ(ierr);
2490916e780bShannah_mairs   *AA  = NULL;
2491916e780bShannah_mairs   PetscFunctionReturn(0);
2492916e780bShannah_mairs }
2493916e780bShannah_mairs 
2494916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementMassCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA)
2495916e780bShannah_mairs {
2496916e780bShannah_mairs   PetscReal        **A;
2497916e780bShannah_mairs   PetscErrorCode  ierr;
2498916e780bShannah_mairs   const PetscReal  *gllweights = weights;
2499916e780bShannah_mairs   const PetscInt   glln = n;
2500916e780bShannah_mairs   PetscInt         i,j;
2501916e780bShannah_mairs 
2502916e780bShannah_mairs   PetscFunctionBegin;
2503916e780bShannah_mairs   ierr = PetscMalloc1(glln,&A);CHKERRQ(ierr);
2504916e780bShannah_mairs   ierr = PetscMalloc1(glln*glln,&A[0]);CHKERRQ(ierr);
2505916e780bShannah_mairs   for (i=1; i<glln; i++) A[i] = A[i-1]+glln;
2506916e780bShannah_mairs   if (glln==1) {A[0][0] = 0.;}
2507916e780bShannah_mairs   for  (i=0; i<glln; i++) {
2508916e780bShannah_mairs     for  (j=0; j<glln; j++) {
2509916e780bShannah_mairs       A[i][j] = 0.;
2510916e780bShannah_mairs       if (j==i)     A[i][j] = gllweights[i];
2511916e780bShannah_mairs     }
2512916e780bShannah_mairs   }
2513916e780bShannah_mairs   *AA  = A;
2514916e780bShannah_mairs   PetscFunctionReturn(0);
2515916e780bShannah_mairs }
2516916e780bShannah_mairs 
2517916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementMassDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA)
2518916e780bShannah_mairs {
2519916e780bShannah_mairs   PetscErrorCode ierr;
2520916e780bShannah_mairs 
2521916e780bShannah_mairs   PetscFunctionBegin;
2522916e780bShannah_mairs   ierr = PetscFree((*AA)[0]);CHKERRQ(ierr);
2523916e780bShannah_mairs   ierr = PetscFree(*AA);CHKERRQ(ierr);
2524916e780bShannah_mairs   *AA  = NULL;
2525916e780bShannah_mairs   PetscFunctionReturn(0);
2526916e780bShannah_mairs }
2527d4afb720SToby Isaac 
2528d4afb720SToby Isaac /*@
2529d4afb720SToby Isaac   PetscDTIndexToBary - convert an index into a barycentric coordinate.
2530d4afb720SToby Isaac 
2531d4afb720SToby Isaac   Input Parameters:
2532d4afb720SToby Isaac + len - the desired length of the barycentric tuple (usually 1 more than the dimension it represents, so a barycentric coordinate in a triangle has length 3)
2533d4afb720SToby Isaac . sum - the value that the sum of the barycentric coordinates (which will be non-negative integers) should sum to
2534d4afb720SToby Isaac - index - the index to convert: should be >= 0 and < Binomial(len - 1 + sum, sum)
2535d4afb720SToby Isaac 
2536d4afb720SToby Isaac   Output Parameter:
2537d4afb720SToby Isaac . coord - will be filled with the barycentric coordinate
2538d4afb720SToby Isaac 
2539d4afb720SToby Isaac   Level: beginner
2540d4afb720SToby Isaac 
2541d4afb720SToby Isaac   Note: the indices map to barycentric coordinates in lexicographic order, where the first index is the
2542d4afb720SToby Isaac   least significant and the last index is the most significant.
2543d4afb720SToby Isaac 
2544fbdc3dfeSToby Isaac .seealso: PetscDTBaryToIndex()
2545d4afb720SToby Isaac @*/
2546d4afb720SToby Isaac PetscErrorCode PetscDTIndexToBary(PetscInt len, PetscInt sum, PetscInt index, PetscInt coord[])
2547d4afb720SToby Isaac {
2548d4afb720SToby Isaac   PetscInt c, d, s, total, subtotal, nexttotal;
2549d4afb720SToby Isaac 
2550d4afb720SToby Isaac   PetscFunctionBeginHot;
2551d4afb720SToby Isaac   if (len < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative");
2552d4afb720SToby Isaac   if (index < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index must be non-negative");
2553d4afb720SToby Isaac   if (!len) {
2554d4afb720SToby Isaac     if (!sum && !index) PetscFunctionReturn(0);
2555d4afb720SToby Isaac     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid index or sum for length 0 barycentric coordinate");
2556d4afb720SToby Isaac   }
2557d4afb720SToby Isaac   for (c = 1, total = 1; c <= len; c++) {
2558d4afb720SToby Isaac     /* total is the number of ways to have a tuple of length c with sum */
2559d4afb720SToby Isaac     if (index < total) break;
2560d4afb720SToby Isaac     total = (total * (sum + c)) / c;
2561d4afb720SToby Isaac   }
2562d4afb720SToby Isaac   if (c > len) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index out of range");
2563d4afb720SToby Isaac   for (d = c; d < len; d++) coord[d] = 0;
2564d4afb720SToby Isaac   for (s = 0, subtotal = 1, nexttotal = 1; c > 0;) {
2565d4afb720SToby Isaac     /* subtotal is the number of ways to have a tuple of length c with sum s */
2566d4afb720SToby Isaac     /* nexttotal is the number of ways to have a tuple of length c-1 with sum s */
2567d4afb720SToby Isaac     if ((index + subtotal) >= total) {
2568d4afb720SToby Isaac       coord[--c] = sum - s;
2569d4afb720SToby Isaac       index -= (total - subtotal);
2570d4afb720SToby Isaac       sum = s;
2571d4afb720SToby Isaac       total = nexttotal;
2572d4afb720SToby Isaac       subtotal = 1;
2573d4afb720SToby Isaac       nexttotal = 1;
2574d4afb720SToby Isaac       s = 0;
2575d4afb720SToby Isaac     } else {
2576d4afb720SToby Isaac       subtotal = (subtotal * (c + s)) / (s + 1);
2577d4afb720SToby Isaac       nexttotal = (nexttotal * (c - 1 + s)) / (s + 1);
2578d4afb720SToby Isaac       s++;
2579d4afb720SToby Isaac     }
2580d4afb720SToby Isaac   }
2581d4afb720SToby Isaac   PetscFunctionReturn(0);
2582d4afb720SToby Isaac }
2583d4afb720SToby Isaac 
2584d4afb720SToby Isaac /*@
2585d4afb720SToby Isaac   PetscDTBaryToIndex - convert a barycentric coordinate to an index
2586d4afb720SToby Isaac 
2587d4afb720SToby Isaac   Input Parameters:
2588d4afb720SToby Isaac + len - the desired length of the barycentric tuple (usually 1 more than the dimension it represents, so a barycentric coordinate in a triangle has length 3)
2589d4afb720SToby Isaac . sum - the value that the sum of the barycentric coordinates (which will be non-negative integers) should sum to
2590d4afb720SToby Isaac - coord - a barycentric coordinate with the given length and sum
2591d4afb720SToby Isaac 
2592d4afb720SToby Isaac   Output Parameter:
2593d4afb720SToby Isaac . index - the unique index for the coordinate, >= 0 and < Binomial(len - 1 + sum, sum)
2594d4afb720SToby Isaac 
2595d4afb720SToby Isaac   Level: beginner
2596d4afb720SToby Isaac 
2597d4afb720SToby Isaac   Note: the indices map to barycentric coordinates in lexicographic order, where the first index is the
2598d4afb720SToby Isaac   least significant and the last index is the most significant.
2599d4afb720SToby Isaac 
2600d4afb720SToby Isaac .seealso: PetscDTIndexToBary
2601d4afb720SToby Isaac @*/
2602d4afb720SToby Isaac PetscErrorCode PetscDTBaryToIndex(PetscInt len, PetscInt sum, const PetscInt coord[], PetscInt *index)
2603d4afb720SToby Isaac {
2604d4afb720SToby Isaac   PetscInt c;
2605d4afb720SToby Isaac   PetscInt i;
2606d4afb720SToby Isaac   PetscInt total;
2607d4afb720SToby Isaac 
2608d4afb720SToby Isaac   PetscFunctionBeginHot;
2609d4afb720SToby Isaac   if (len < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative");
2610d4afb720SToby Isaac   if (!len) {
2611d4afb720SToby Isaac     if (!sum) {
2612d4afb720SToby Isaac       *index = 0;
2613d4afb720SToby Isaac       PetscFunctionReturn(0);
2614d4afb720SToby Isaac     }
2615d4afb720SToby Isaac     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid index or sum for length 0 barycentric coordinate");
2616d4afb720SToby Isaac   }
2617d4afb720SToby Isaac   for (c = 1, total = 1; c < len; c++) total = (total * (sum + c)) / c;
2618d4afb720SToby Isaac   i = total - 1;
2619d4afb720SToby Isaac   c = len - 1;
2620d4afb720SToby Isaac   sum -= coord[c];
2621d4afb720SToby Isaac   while (sum > 0) {
2622d4afb720SToby Isaac     PetscInt subtotal;
2623d4afb720SToby Isaac     PetscInt s;
2624d4afb720SToby Isaac 
2625d4afb720SToby Isaac     for (s = 1, subtotal = 1; s < sum; s++) subtotal = (subtotal * (c + s)) / s;
2626d4afb720SToby Isaac     i   -= subtotal;
2627d4afb720SToby Isaac     sum -= coord[--c];
2628d4afb720SToby Isaac   }
2629d4afb720SToby Isaac   *index = i;
2630d4afb720SToby Isaac   PetscFunctionReturn(0);
2631d4afb720SToby Isaac }
2632