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*d3c69ad0SToby Isaac const char *const PetscDTNodeTypes_shifted[] = {"default", "gaussjacobi", "equispaced", "tanhsinh", "PETSCDTNODES_", NULL}; 16*d3c69ad0SToby Isaac const char *const*const PetscDTNodeTypes = PetscDTNodeTypes_shifted + 1; 17*d3c69ad0SToby Isaac 18*d3c69ad0SToby Isaac const char *const PetscDTSimplexQuadratureTypes_shifted[] = {"default", "conic", "minsym", "PETSCDTSIMPLEXQUAD_", NULL}; 19*d3c69ad0SToby Isaac const char *const*const PetscDTSimplexQuadratureTypes = PetscDTSimplexQuadratureTypes_shifted + 1; 20d4afb720SToby Isaac 21e6a796c3SToby Isaac static PetscBool GolubWelschCite = PETSC_FALSE; 22e6a796c3SToby Isaac const char GolubWelschCitation[] = "@article{GolubWelsch1969,\n" 230bfcf5a5SMatthew G. Knepley " author = {Golub and Welsch},\n" 240bfcf5a5SMatthew G. Knepley " title = {Calculation of Quadrature Rules},\n" 250bfcf5a5SMatthew G. Knepley " journal = {Math. Comp.},\n" 260bfcf5a5SMatthew G. Knepley " volume = {23},\n" 270bfcf5a5SMatthew G. Knepley " number = {106},\n" 280bfcf5a5SMatthew G. Knepley " pages = {221--230},\n" 290bfcf5a5SMatthew G. Knepley " year = {1969}\n}\n"; 300bfcf5a5SMatthew G. Knepley 31c4762a1bSJed Brown /* Numerical tests in src/dm/dt/tests/ex1.c show that when computing the nodes and weights of Gauss-Jacobi 3294e21283SToby Isaac quadrature rules: 33e6a796c3SToby Isaac 3494e21283SToby Isaac - in double precision, Newton's method and Golub & Welsch both work for moderate degrees (< 100), 3594e21283SToby Isaac - in single precision, Newton's method starts producing incorrect roots around n = 15, but 3694e21283SToby Isaac the weights from Golub & Welsch become a problem before then: they produces errors 3794e21283SToby Isaac in computing the Jacobi-polynomial Gram matrix around n = 6. 3894e21283SToby Isaac 3994e21283SToby Isaac So we default to Newton's method (required fewer dependencies) */ 4094e21283SToby Isaac PetscBool PetscDTGaussQuadratureNewton_Internal = PETSC_TRUE; 412cd22861SMatthew G. Knepley 422cd22861SMatthew G. Knepley PetscClassId PETSCQUADRATURE_CLASSID = 0; 432cd22861SMatthew G. Knepley 4440d8ff71SMatthew G. Knepley /*@ 4540d8ff71SMatthew G. Knepley PetscQuadratureCreate - Create a PetscQuadrature object 4640d8ff71SMatthew G. Knepley 47d083f849SBarry Smith Collective 4840d8ff71SMatthew G. Knepley 4940d8ff71SMatthew G. Knepley Input Parameter: 5040d8ff71SMatthew G. Knepley . comm - The communicator for the PetscQuadrature object 5140d8ff71SMatthew G. Knepley 5240d8ff71SMatthew G. Knepley Output Parameter: 5340d8ff71SMatthew G. Knepley . q - The PetscQuadrature object 5440d8ff71SMatthew G. Knepley 5540d8ff71SMatthew G. Knepley Level: beginner 5640d8ff71SMatthew G. Knepley 57db781477SPatrick Sanan .seealso: `PetscQuadratureDestroy()`, `PetscQuadratureGetData()` 5840d8ff71SMatthew G. Knepley @*/ 5921454ff5SMatthew G. Knepley PetscErrorCode PetscQuadratureCreate(MPI_Comm comm, PetscQuadrature *q) 6021454ff5SMatthew G. Knepley { 6121454ff5SMatthew G. Knepley PetscFunctionBegin; 6221454ff5SMatthew G. Knepley PetscValidPointer(q, 2); 639566063dSJacob Faibussowitsch PetscCall(DMInitializePackage()); 649566063dSJacob Faibussowitsch PetscCall(PetscHeaderCreate(*q,PETSCQUADRATURE_CLASSID,"PetscQuadrature","Quadrature","DT",comm,PetscQuadratureDestroy,PetscQuadratureView)); 6521454ff5SMatthew G. Knepley (*q)->dim = -1; 66a6b92713SMatthew G. Knepley (*q)->Nc = 1; 67bcede257SMatthew G. Knepley (*q)->order = -1; 6821454ff5SMatthew G. Knepley (*q)->numPoints = 0; 6921454ff5SMatthew G. Knepley (*q)->points = NULL; 7021454ff5SMatthew G. Knepley (*q)->weights = NULL; 7121454ff5SMatthew G. Knepley PetscFunctionReturn(0); 7221454ff5SMatthew G. Knepley } 7321454ff5SMatthew G. Knepley 74c9638911SMatthew G. Knepley /*@ 75c9638911SMatthew G. Knepley PetscQuadratureDuplicate - Create a deep copy of the PetscQuadrature object 76c9638911SMatthew G. Knepley 77d083f849SBarry Smith Collective on q 78c9638911SMatthew G. Knepley 79c9638911SMatthew G. Knepley Input Parameter: 80c9638911SMatthew G. Knepley . q - The PetscQuadrature object 81c9638911SMatthew G. Knepley 82c9638911SMatthew G. Knepley Output Parameter: 83c9638911SMatthew G. Knepley . r - The new PetscQuadrature object 84c9638911SMatthew G. Knepley 85c9638911SMatthew G. Knepley Level: beginner 86c9638911SMatthew G. Knepley 87db781477SPatrick Sanan .seealso: `PetscQuadratureCreate()`, `PetscQuadratureDestroy()`, `PetscQuadratureGetData()` 88c9638911SMatthew G. Knepley @*/ 89c9638911SMatthew G. Knepley PetscErrorCode PetscQuadratureDuplicate(PetscQuadrature q, PetscQuadrature *r) 90c9638911SMatthew G. Knepley { 91a6b92713SMatthew G. Knepley PetscInt order, dim, Nc, Nq; 92c9638911SMatthew G. Knepley const PetscReal *points, *weights; 93c9638911SMatthew G. Knepley PetscReal *p, *w; 94c9638911SMatthew G. Knepley 95c9638911SMatthew G. Knepley PetscFunctionBegin; 96064a246eSJacob Faibussowitsch PetscValidPointer(q, 1); 979566063dSJacob Faibussowitsch PetscCall(PetscQuadratureCreate(PetscObjectComm((PetscObject) q), r)); 989566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetOrder(q, &order)); 999566063dSJacob Faibussowitsch PetscCall(PetscQuadratureSetOrder(*r, order)); 1009566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetData(q, &dim, &Nc, &Nq, &points, &weights)); 1019566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Nq*dim, &p)); 1029566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Nq*Nc, &w)); 1039566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(p, points, Nq*dim)); 1049566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(w, weights, Nc * Nq)); 1059566063dSJacob Faibussowitsch PetscCall(PetscQuadratureSetData(*r, dim, Nc, Nq, p, w)); 106c9638911SMatthew G. Knepley PetscFunctionReturn(0); 107c9638911SMatthew G. Knepley } 108c9638911SMatthew G. Knepley 10940d8ff71SMatthew G. Knepley /*@ 11040d8ff71SMatthew G. Knepley PetscQuadratureDestroy - Destroys a PetscQuadrature object 11140d8ff71SMatthew G. Knepley 112d083f849SBarry Smith Collective on q 11340d8ff71SMatthew G. Knepley 11440d8ff71SMatthew G. Knepley Input Parameter: 11540d8ff71SMatthew G. Knepley . q - The PetscQuadrature object 11640d8ff71SMatthew G. Knepley 11740d8ff71SMatthew G. Knepley Level: beginner 11840d8ff71SMatthew G. Knepley 119db781477SPatrick Sanan .seealso: `PetscQuadratureCreate()`, `PetscQuadratureGetData()` 12040d8ff71SMatthew G. Knepley @*/ 121bfa639d9SMatthew G. Knepley PetscErrorCode PetscQuadratureDestroy(PetscQuadrature *q) 122bfa639d9SMatthew G. Knepley { 123bfa639d9SMatthew G. Knepley PetscFunctionBegin; 12421454ff5SMatthew G. Knepley if (!*q) PetscFunctionReturn(0); 1252cd22861SMatthew G. Knepley PetscValidHeaderSpecific((*q),PETSCQUADRATURE_CLASSID,1); 12621454ff5SMatthew G. Knepley if (--((PetscObject)(*q))->refct > 0) { 12721454ff5SMatthew G. Knepley *q = NULL; 12821454ff5SMatthew G. Knepley PetscFunctionReturn(0); 12921454ff5SMatthew G. Knepley } 1309566063dSJacob Faibussowitsch PetscCall(PetscFree((*q)->points)); 1319566063dSJacob Faibussowitsch PetscCall(PetscFree((*q)->weights)); 1329566063dSJacob Faibussowitsch PetscCall(PetscHeaderDestroy(q)); 13321454ff5SMatthew G. Knepley PetscFunctionReturn(0); 13421454ff5SMatthew G. Knepley } 13521454ff5SMatthew G. Knepley 136bcede257SMatthew G. Knepley /*@ 137a6b92713SMatthew G. Knepley PetscQuadratureGetOrder - Return the order of the method 138bcede257SMatthew G. Knepley 139bcede257SMatthew G. Knepley Not collective 140bcede257SMatthew G. Knepley 141bcede257SMatthew G. Knepley Input Parameter: 142bcede257SMatthew G. Knepley . q - The PetscQuadrature object 143bcede257SMatthew G. Knepley 144bcede257SMatthew G. Knepley Output Parameter: 145bcede257SMatthew G. Knepley . order - The order of the quadrature, i.e. the highest degree polynomial that is exactly integrated 146bcede257SMatthew G. Knepley 147bcede257SMatthew G. Knepley Level: intermediate 148bcede257SMatthew G. Knepley 149db781477SPatrick Sanan .seealso: `PetscQuadratureSetOrder()`, `PetscQuadratureGetData()`, `PetscQuadratureSetData()` 150bcede257SMatthew G. Knepley @*/ 151bcede257SMatthew G. Knepley PetscErrorCode PetscQuadratureGetOrder(PetscQuadrature q, PetscInt *order) 152bcede257SMatthew G. Knepley { 153bcede257SMatthew G. Knepley PetscFunctionBegin; 1542cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 155dadcf809SJacob Faibussowitsch PetscValidIntPointer(order, 2); 156bcede257SMatthew G. Knepley *order = q->order; 157bcede257SMatthew G. Knepley PetscFunctionReturn(0); 158bcede257SMatthew G. Knepley } 159bcede257SMatthew G. Knepley 160bcede257SMatthew G. Knepley /*@ 161a6b92713SMatthew G. Knepley PetscQuadratureSetOrder - Return the order of the method 162bcede257SMatthew G. Knepley 163bcede257SMatthew G. Knepley Not collective 164bcede257SMatthew G. Knepley 165bcede257SMatthew G. Knepley Input Parameters: 166bcede257SMatthew G. Knepley + q - The PetscQuadrature object 167bcede257SMatthew G. Knepley - order - The order of the quadrature, i.e. the highest degree polynomial that is exactly integrated 168bcede257SMatthew G. Knepley 169bcede257SMatthew G. Knepley Level: intermediate 170bcede257SMatthew G. Knepley 171db781477SPatrick Sanan .seealso: `PetscQuadratureGetOrder()`, `PetscQuadratureGetData()`, `PetscQuadratureSetData()` 172bcede257SMatthew G. Knepley @*/ 173bcede257SMatthew G. Knepley PetscErrorCode PetscQuadratureSetOrder(PetscQuadrature q, PetscInt order) 174bcede257SMatthew G. Knepley { 175bcede257SMatthew G. Knepley PetscFunctionBegin; 1762cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 177bcede257SMatthew G. Knepley q->order = order; 178bcede257SMatthew G. Knepley PetscFunctionReturn(0); 179bcede257SMatthew G. Knepley } 180bcede257SMatthew G. Knepley 181a6b92713SMatthew G. Knepley /*@ 182a6b92713SMatthew G. Knepley PetscQuadratureGetNumComponents - Return the number of components for functions to be integrated 183a6b92713SMatthew G. Knepley 184a6b92713SMatthew G. Knepley Not collective 185a6b92713SMatthew G. Knepley 186a6b92713SMatthew G. Knepley Input Parameter: 187a6b92713SMatthew G. Knepley . q - The PetscQuadrature object 188a6b92713SMatthew G. Knepley 189a6b92713SMatthew G. Knepley Output Parameter: 190a6b92713SMatthew G. Knepley . Nc - The number of components 191a6b92713SMatthew G. Knepley 192a6b92713SMatthew G. Knepley Note: We are performing an integral int f(x) . w(x) dx, where both f and w (the weight) have Nc components. 193a6b92713SMatthew G. Knepley 194a6b92713SMatthew G. Knepley Level: intermediate 195a6b92713SMatthew G. Knepley 196db781477SPatrick Sanan .seealso: `PetscQuadratureSetNumComponents()`, `PetscQuadratureGetData()`, `PetscQuadratureSetData()` 197a6b92713SMatthew G. Knepley @*/ 198a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureGetNumComponents(PetscQuadrature q, PetscInt *Nc) 199a6b92713SMatthew G. Knepley { 200a6b92713SMatthew G. Knepley PetscFunctionBegin; 2012cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 202dadcf809SJacob Faibussowitsch PetscValidIntPointer(Nc, 2); 203a6b92713SMatthew G. Knepley *Nc = q->Nc; 204a6b92713SMatthew G. Knepley PetscFunctionReturn(0); 205a6b92713SMatthew G. Knepley } 206a6b92713SMatthew G. Knepley 207a6b92713SMatthew G. Knepley /*@ 208a6b92713SMatthew G. Knepley PetscQuadratureSetNumComponents - Return the number of components for functions to be integrated 209a6b92713SMatthew G. Knepley 210a6b92713SMatthew G. Knepley Not collective 211a6b92713SMatthew G. Knepley 212a6b92713SMatthew G. Knepley Input Parameters: 213a6b92713SMatthew G. Knepley + q - The PetscQuadrature object 214a6b92713SMatthew G. Knepley - Nc - The number of components 215a6b92713SMatthew G. Knepley 216a6b92713SMatthew G. Knepley Note: We are performing an integral int f(x) . w(x) dx, where both f and w (the weight) have Nc components. 217a6b92713SMatthew G. Knepley 218a6b92713SMatthew G. Knepley Level: intermediate 219a6b92713SMatthew G. Knepley 220db781477SPatrick Sanan .seealso: `PetscQuadratureGetNumComponents()`, `PetscQuadratureGetData()`, `PetscQuadratureSetData()` 221a6b92713SMatthew G. Knepley @*/ 222a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureSetNumComponents(PetscQuadrature q, PetscInt Nc) 223a6b92713SMatthew G. Knepley { 224a6b92713SMatthew G. Knepley PetscFunctionBegin; 2252cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 226a6b92713SMatthew G. Knepley q->Nc = Nc; 227a6b92713SMatthew G. Knepley PetscFunctionReturn(0); 228a6b92713SMatthew G. Knepley } 229a6b92713SMatthew G. Knepley 23040d8ff71SMatthew G. Knepley /*@C 23140d8ff71SMatthew G. Knepley PetscQuadratureGetData - Returns the data defining the quadrature 23240d8ff71SMatthew G. Knepley 23340d8ff71SMatthew G. Knepley Not collective 23440d8ff71SMatthew G. Knepley 23540d8ff71SMatthew G. Knepley Input Parameter: 23640d8ff71SMatthew G. Knepley . q - The PetscQuadrature object 23740d8ff71SMatthew G. Knepley 23840d8ff71SMatthew G. Knepley Output Parameters: 23940d8ff71SMatthew G. Knepley + dim - The spatial dimension 240805e7170SToby Isaac . Nc - The number of components 24140d8ff71SMatthew G. Knepley . npoints - The number of quadrature points 24240d8ff71SMatthew G. Knepley . points - The coordinates of each quadrature point 24340d8ff71SMatthew G. Knepley - weights - The weight of each quadrature point 24440d8ff71SMatthew G. Knepley 24540d8ff71SMatthew G. Knepley Level: intermediate 24640d8ff71SMatthew G. Knepley 24795452b02SPatrick Sanan Fortran Notes: 24895452b02SPatrick Sanan From Fortran you must call PetscQuadratureRestoreData() when you are done with the data 2491fd49c25SBarry Smith 250db781477SPatrick Sanan .seealso: `PetscQuadratureCreate()`, `PetscQuadratureSetData()` 25140d8ff71SMatthew G. Knepley @*/ 252a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureGetData(PetscQuadrature q, PetscInt *dim, PetscInt *Nc, PetscInt *npoints, const PetscReal *points[], const PetscReal *weights[]) 25321454ff5SMatthew G. Knepley { 25421454ff5SMatthew G. Knepley PetscFunctionBegin; 2552cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 25621454ff5SMatthew G. Knepley if (dim) { 257dadcf809SJacob Faibussowitsch PetscValidIntPointer(dim, 2); 25821454ff5SMatthew G. Knepley *dim = q->dim; 25921454ff5SMatthew G. Knepley } 260a6b92713SMatthew G. Knepley if (Nc) { 261dadcf809SJacob Faibussowitsch PetscValidIntPointer(Nc, 3); 262a6b92713SMatthew G. Knepley *Nc = q->Nc; 263a6b92713SMatthew G. Knepley } 26421454ff5SMatthew G. Knepley if (npoints) { 265dadcf809SJacob Faibussowitsch PetscValidIntPointer(npoints, 4); 26621454ff5SMatthew G. Knepley *npoints = q->numPoints; 26721454ff5SMatthew G. Knepley } 26821454ff5SMatthew G. Knepley if (points) { 269a6b92713SMatthew G. Knepley PetscValidPointer(points, 5); 27021454ff5SMatthew G. Knepley *points = q->points; 27121454ff5SMatthew G. Knepley } 27221454ff5SMatthew G. Knepley if (weights) { 273a6b92713SMatthew G. Knepley PetscValidPointer(weights, 6); 27421454ff5SMatthew G. Knepley *weights = q->weights; 27521454ff5SMatthew G. Knepley } 27621454ff5SMatthew G. Knepley PetscFunctionReturn(0); 27721454ff5SMatthew G. Knepley } 27821454ff5SMatthew G. Knepley 2794f9ab2b4SJed Brown /*@ 2804f9ab2b4SJed Brown PetscQuadratureEqual - determine whether two quadratures are equivalent 2814f9ab2b4SJed Brown 2824f9ab2b4SJed Brown Input Parameters: 2834f9ab2b4SJed Brown + A - A PetscQuadrature object 2844f9ab2b4SJed Brown - B - Another PetscQuadrature object 2854f9ab2b4SJed Brown 2864f9ab2b4SJed Brown Output Parameters: 2874f9ab2b4SJed Brown . equal - PETSC_TRUE if the quadratures are the same 2884f9ab2b4SJed Brown 2894f9ab2b4SJed Brown Level: intermediate 2904f9ab2b4SJed Brown 291db781477SPatrick Sanan .seealso: `PetscQuadratureCreate()` 2924f9ab2b4SJed Brown @*/ 2934f9ab2b4SJed Brown PetscErrorCode PetscQuadratureEqual(PetscQuadrature A, PetscQuadrature B, PetscBool *equal) 2944f9ab2b4SJed Brown { 2954f9ab2b4SJed Brown PetscFunctionBegin; 2964f9ab2b4SJed Brown PetscValidHeaderSpecific(A, PETSCQUADRATURE_CLASSID, 1); 2974f9ab2b4SJed Brown PetscValidHeaderSpecific(B, PETSCQUADRATURE_CLASSID, 2); 2984f9ab2b4SJed Brown PetscValidBoolPointer(equal, 3); 2994f9ab2b4SJed Brown *equal = PETSC_FALSE; 3004f9ab2b4SJed Brown if (A->dim != B->dim || A->Nc != B->Nc || A->order != B->order || A->numPoints != B->numPoints) { 3014f9ab2b4SJed Brown PetscFunctionReturn(0); 3024f9ab2b4SJed Brown } 3034f9ab2b4SJed Brown for (PetscInt i=0; i<A->numPoints*A->dim; i++) { 3044f9ab2b4SJed Brown if (PetscAbsReal(A->points[i] - B->points[i]) > PETSC_SMALL) { 3054f9ab2b4SJed Brown PetscFunctionReturn(0); 3064f9ab2b4SJed Brown } 3074f9ab2b4SJed Brown } 3084f9ab2b4SJed Brown if (!A->weights && !B->weights) { 3094f9ab2b4SJed Brown *equal = PETSC_TRUE; 3104f9ab2b4SJed Brown PetscFunctionReturn(0); 3114f9ab2b4SJed Brown } 3124f9ab2b4SJed Brown if (A->weights && B->weights) { 3134f9ab2b4SJed Brown for (PetscInt i=0; i<A->numPoints; i++) { 3144f9ab2b4SJed Brown if (PetscAbsReal(A->weights[i] - B->weights[i]) > PETSC_SMALL) { 3154f9ab2b4SJed Brown PetscFunctionReturn(0); 3164f9ab2b4SJed Brown } 3174f9ab2b4SJed Brown } 3184f9ab2b4SJed Brown *equal = PETSC_TRUE; 3194f9ab2b4SJed Brown } 3204f9ab2b4SJed Brown PetscFunctionReturn(0); 3214f9ab2b4SJed Brown } 3224f9ab2b4SJed Brown 323907761f8SToby Isaac static PetscErrorCode PetscDTJacobianInverse_Internal(PetscInt m, PetscInt n, const PetscReal J[], PetscReal Jinv[]) 324907761f8SToby Isaac { 325907761f8SToby Isaac PetscScalar *Js, *Jinvs; 326907761f8SToby Isaac PetscInt i, j, k; 327907761f8SToby Isaac PetscBLASInt bm, bn, info; 328907761f8SToby Isaac 329907761f8SToby Isaac PetscFunctionBegin; 330d4afb720SToby Isaac if (!m || !n) PetscFunctionReturn(0); 3319566063dSJacob Faibussowitsch PetscCall(PetscBLASIntCast(m, &bm)); 3329566063dSJacob Faibussowitsch PetscCall(PetscBLASIntCast(n, &bn)); 333907761f8SToby Isaac #if defined(PETSC_USE_COMPLEX) 3349566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(m*n, &Js, m*n, &Jinvs)); 33528222859SToby Isaac for (i = 0; i < m*n; i++) Js[i] = J[i]; 336907761f8SToby Isaac #else 337907761f8SToby Isaac Js = (PetscReal *) J; 338907761f8SToby Isaac Jinvs = Jinv; 339907761f8SToby Isaac #endif 340907761f8SToby Isaac if (m == n) { 341907761f8SToby Isaac PetscBLASInt *pivots; 342907761f8SToby Isaac PetscScalar *W; 343907761f8SToby Isaac 3449566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(m, &pivots, m, &W)); 345907761f8SToby Isaac 3469566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(Jinvs, Js, m * m)); 347792fecdfSBarry Smith PetscCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bm, &bm, Jinvs, &bm, pivots, &info)); 34863a3b9bcSJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetrf %" PetscInt_FMT,(PetscInt)info); 349792fecdfSBarry Smith PetscCallBLAS("LAPACKgetri", LAPACKgetri_(&bm, Jinvs, &bm, pivots, W, &bm, &info)); 35063a3b9bcSJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetri %" PetscInt_FMT,(PetscInt)info); 3519566063dSJacob Faibussowitsch PetscCall(PetscFree2(pivots, W)); 352907761f8SToby Isaac } else if (m < n) { 353907761f8SToby Isaac PetscScalar *JJT; 354907761f8SToby Isaac PetscBLASInt *pivots; 355907761f8SToby Isaac PetscScalar *W; 356907761f8SToby Isaac 3579566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(m*m, &JJT)); 3589566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(m, &pivots, m, &W)); 359907761f8SToby Isaac for (i = 0; i < m; i++) { 360907761f8SToby Isaac for (j = 0; j < m; j++) { 361907761f8SToby Isaac PetscScalar val = 0.; 362907761f8SToby Isaac 363907761f8SToby Isaac for (k = 0; k < n; k++) val += Js[i * n + k] * Js[j * n + k]; 364907761f8SToby Isaac JJT[i * m + j] = val; 365907761f8SToby Isaac } 366907761f8SToby Isaac } 367907761f8SToby Isaac 368792fecdfSBarry Smith PetscCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bm, &bm, JJT, &bm, pivots, &info)); 36963a3b9bcSJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetrf %" PetscInt_FMT,(PetscInt)info); 370792fecdfSBarry Smith PetscCallBLAS("LAPACKgetri", LAPACKgetri_(&bm, JJT, &bm, pivots, W, &bm, &info)); 37163a3b9bcSJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetri %" PetscInt_FMT,(PetscInt)info); 372907761f8SToby Isaac for (i = 0; i < n; i++) { 373907761f8SToby Isaac for (j = 0; j < m; j++) { 374907761f8SToby Isaac PetscScalar val = 0.; 375907761f8SToby Isaac 376907761f8SToby Isaac for (k = 0; k < m; k++) val += Js[k * n + i] * JJT[k * m + j]; 377907761f8SToby Isaac Jinvs[i * m + j] = val; 378907761f8SToby Isaac } 379907761f8SToby Isaac } 3809566063dSJacob Faibussowitsch PetscCall(PetscFree2(pivots, W)); 3819566063dSJacob Faibussowitsch PetscCall(PetscFree(JJT)); 382907761f8SToby Isaac } else { 383907761f8SToby Isaac PetscScalar *JTJ; 384907761f8SToby Isaac PetscBLASInt *pivots; 385907761f8SToby Isaac PetscScalar *W; 386907761f8SToby Isaac 3879566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n*n, &JTJ)); 3889566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(n, &pivots, n, &W)); 389907761f8SToby Isaac for (i = 0; i < n; i++) { 390907761f8SToby Isaac for (j = 0; j < n; j++) { 391907761f8SToby Isaac PetscScalar val = 0.; 392907761f8SToby Isaac 393907761f8SToby Isaac for (k = 0; k < m; k++) val += Js[k * n + i] * Js[k * n + j]; 394907761f8SToby Isaac JTJ[i * n + j] = val; 395907761f8SToby Isaac } 396907761f8SToby Isaac } 397907761f8SToby Isaac 398792fecdfSBarry Smith PetscCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bn, &bn, JTJ, &bn, pivots, &info)); 39963a3b9bcSJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetrf %" PetscInt_FMT,(PetscInt)info); 400792fecdfSBarry Smith PetscCallBLAS("LAPACKgetri", LAPACKgetri_(&bn, JTJ, &bn, pivots, W, &bn, &info)); 40163a3b9bcSJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetri %" PetscInt_FMT,(PetscInt)info); 402907761f8SToby Isaac for (i = 0; i < n; i++) { 403907761f8SToby Isaac for (j = 0; j < m; j++) { 404907761f8SToby Isaac PetscScalar val = 0.; 405907761f8SToby Isaac 406907761f8SToby Isaac for (k = 0; k < n; k++) val += JTJ[i * n + k] * Js[j * n + k]; 407907761f8SToby Isaac Jinvs[i * m + j] = val; 408907761f8SToby Isaac } 409907761f8SToby Isaac } 4109566063dSJacob Faibussowitsch PetscCall(PetscFree2(pivots, W)); 4119566063dSJacob Faibussowitsch PetscCall(PetscFree(JTJ)); 412907761f8SToby Isaac } 413907761f8SToby Isaac #if defined(PETSC_USE_COMPLEX) 41428222859SToby Isaac for (i = 0; i < m*n; i++) Jinv[i] = PetscRealPart(Jinvs[i]); 4159566063dSJacob Faibussowitsch PetscCall(PetscFree2(Js, Jinvs)); 416907761f8SToby Isaac #endif 417907761f8SToby Isaac PetscFunctionReturn(0); 418907761f8SToby Isaac } 419907761f8SToby Isaac 420907761f8SToby Isaac /*@ 421907761f8SToby Isaac PetscQuadraturePushForward - Push forward a quadrature functional under an affine transformation. 422907761f8SToby Isaac 423907761f8SToby Isaac Collecive on PetscQuadrature 424907761f8SToby Isaac 4254165533cSJose E. Roman Input Parameters: 426907761f8SToby Isaac + q - the quadrature functional 427907761f8SToby Isaac . imageDim - the dimension of the image of the transformation 428907761f8SToby Isaac . origin - a point in the original space 429907761f8SToby Isaac . originImage - the image of the origin under the transformation 430907761f8SToby Isaac . J - the Jacobian of the image: an [imageDim x dim] matrix in row major order 43128222859SToby 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] 432907761f8SToby Isaac 4334165533cSJose E. Roman Output Parameters: 434907761f8SToby 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. 435907761f8SToby Isaac 436907761f8SToby 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. 437907761f8SToby Isaac 4386c877ef6SSatish Balay Level: intermediate 4396c877ef6SSatish Balay 440db781477SPatrick Sanan .seealso: `PetscDTAltVPullback()`, `PetscDTAltVPullbackMatrix()` 441907761f8SToby Isaac @*/ 44228222859SToby Isaac PetscErrorCode PetscQuadraturePushForward(PetscQuadrature q, PetscInt imageDim, const PetscReal origin[], const PetscReal originImage[], const PetscReal J[], PetscInt formDegree, PetscQuadrature *Jinvstarq) 443907761f8SToby Isaac { 444907761f8SToby Isaac PetscInt dim, Nc, imageNc, formSize, Ncopies, imageFormSize, Npoints, pt, i, j, c; 445907761f8SToby Isaac const PetscReal *points; 446907761f8SToby Isaac const PetscReal *weights; 447907761f8SToby Isaac PetscReal *imagePoints, *imageWeights; 448907761f8SToby Isaac PetscReal *Jinv; 449907761f8SToby Isaac PetscReal *Jinvstar; 450907761f8SToby Isaac 451907761f8SToby Isaac PetscFunctionBegin; 452d4afb720SToby Isaac PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 45363a3b9bcSJacob Faibussowitsch PetscCheck(imageDim >= PetscAbsInt(formDegree),PetscObjectComm((PetscObject)q), PETSC_ERR_ARG_INCOMP, "Cannot represent a %" PetscInt_FMT "-form in %" PetscInt_FMT " dimensions", PetscAbsInt(formDegree), imageDim); 4549566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetData(q, &dim, &Nc, &Npoints, &points, &weights)); 4559566063dSJacob Faibussowitsch PetscCall(PetscDTBinomialInt(dim, PetscAbsInt(formDegree), &formSize)); 45663a3b9bcSJacob Faibussowitsch PetscCheck(Nc % formSize == 0,PetscObjectComm((PetscObject)q), PETSC_ERR_ARG_INCOMP, "Number of components %" PetscInt_FMT " is not a multiple of formSize %" PetscInt_FMT, Nc, formSize); 457907761f8SToby Isaac Ncopies = Nc / formSize; 4589566063dSJacob Faibussowitsch PetscCall(PetscDTBinomialInt(imageDim, PetscAbsInt(formDegree), &imageFormSize)); 459907761f8SToby Isaac imageNc = Ncopies * imageFormSize; 4609566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Npoints * imageDim, &imagePoints)); 4619566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Npoints * imageNc, &imageWeights)); 4629566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(imageDim * dim, &Jinv, formSize * imageFormSize, &Jinvstar)); 4639566063dSJacob Faibussowitsch PetscCall(PetscDTJacobianInverse_Internal(imageDim, dim, J, Jinv)); 4649566063dSJacob Faibussowitsch PetscCall(PetscDTAltVPullbackMatrix(imageDim, dim, Jinv, formDegree, Jinvstar)); 465907761f8SToby Isaac for (pt = 0; pt < Npoints; pt++) { 466907761f8SToby Isaac const PetscReal *point = &points[pt * dim]; 467907761f8SToby Isaac PetscReal *imagePoint = &imagePoints[pt * imageDim]; 468907761f8SToby Isaac 469907761f8SToby Isaac for (i = 0; i < imageDim; i++) { 470907761f8SToby Isaac PetscReal val = originImage[i]; 471907761f8SToby Isaac 472907761f8SToby Isaac for (j = 0; j < dim; j++) val += J[i * dim + j] * (point[j] - origin[j]); 473907761f8SToby Isaac imagePoint[i] = val; 474907761f8SToby Isaac } 475907761f8SToby Isaac for (c = 0; c < Ncopies; c++) { 476907761f8SToby Isaac const PetscReal *form = &weights[pt * Nc + c * formSize]; 477907761f8SToby Isaac PetscReal *imageForm = &imageWeights[pt * imageNc + c * imageFormSize]; 478907761f8SToby Isaac 479907761f8SToby Isaac for (i = 0; i < imageFormSize; i++) { 480907761f8SToby Isaac PetscReal val = 0.; 481907761f8SToby Isaac 482907761f8SToby Isaac for (j = 0; j < formSize; j++) val += Jinvstar[i * formSize + j] * form[j]; 483907761f8SToby Isaac imageForm[i] = val; 484907761f8SToby Isaac } 485907761f8SToby Isaac } 486907761f8SToby Isaac } 4879566063dSJacob Faibussowitsch PetscCall(PetscQuadratureCreate(PetscObjectComm((PetscObject)q), Jinvstarq)); 4889566063dSJacob Faibussowitsch PetscCall(PetscQuadratureSetData(*Jinvstarq, imageDim, imageNc, Npoints, imagePoints, imageWeights)); 4899566063dSJacob Faibussowitsch PetscCall(PetscFree2(Jinv, Jinvstar)); 490907761f8SToby Isaac PetscFunctionReturn(0); 491907761f8SToby Isaac } 492907761f8SToby Isaac 49340d8ff71SMatthew G. Knepley /*@C 49440d8ff71SMatthew G. Knepley PetscQuadratureSetData - Sets the data defining the quadrature 49540d8ff71SMatthew G. Knepley 49640d8ff71SMatthew G. Knepley Not collective 49740d8ff71SMatthew G. Knepley 49840d8ff71SMatthew G. Knepley Input Parameters: 49940d8ff71SMatthew G. Knepley + q - The PetscQuadrature object 50040d8ff71SMatthew G. Knepley . dim - The spatial dimension 501e2b35d93SBarry Smith . Nc - The number of components 50240d8ff71SMatthew G. Knepley . npoints - The number of quadrature points 50340d8ff71SMatthew G. Knepley . points - The coordinates of each quadrature point 50440d8ff71SMatthew G. Knepley - weights - The weight of each quadrature point 50540d8ff71SMatthew G. Knepley 506c99e0549SMatthew 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. 507f2fd9e53SMatthew G. Knepley 50840d8ff71SMatthew G. Knepley Level: intermediate 50940d8ff71SMatthew G. Knepley 510db781477SPatrick Sanan .seealso: `PetscQuadratureCreate()`, `PetscQuadratureGetData()` 51140d8ff71SMatthew G. Knepley @*/ 512a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureSetData(PetscQuadrature q, PetscInt dim, PetscInt Nc, PetscInt npoints, const PetscReal points[], const PetscReal weights[]) 51321454ff5SMatthew G. Knepley { 51421454ff5SMatthew G. Knepley PetscFunctionBegin; 5152cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 51621454ff5SMatthew G. Knepley if (dim >= 0) q->dim = dim; 517a6b92713SMatthew G. Knepley if (Nc >= 0) q->Nc = Nc; 51821454ff5SMatthew G. Knepley if (npoints >= 0) q->numPoints = npoints; 51921454ff5SMatthew G. Knepley if (points) { 520dadcf809SJacob Faibussowitsch PetscValidRealPointer(points, 5); 52121454ff5SMatthew G. Knepley q->points = points; 52221454ff5SMatthew G. Knepley } 52321454ff5SMatthew G. Knepley if (weights) { 524dadcf809SJacob Faibussowitsch PetscValidRealPointer(weights, 6); 52521454ff5SMatthew G. Knepley q->weights = weights; 52621454ff5SMatthew G. Knepley } 527f9fd7fdbSMatthew G. Knepley PetscFunctionReturn(0); 528f9fd7fdbSMatthew G. Knepley } 529f9fd7fdbSMatthew G. Knepley 530d9bac1caSLisandro Dalcin static PetscErrorCode PetscQuadratureView_Ascii(PetscQuadrature quad, PetscViewer v) 531d9bac1caSLisandro Dalcin { 532d9bac1caSLisandro Dalcin PetscInt q, d, c; 533d9bac1caSLisandro Dalcin PetscViewerFormat format; 534d9bac1caSLisandro Dalcin 535d9bac1caSLisandro Dalcin PetscFunctionBegin; 53663a3b9bcSJacob Faibussowitsch if (quad->Nc > 1) PetscCall(PetscViewerASCIIPrintf(v, "Quadrature of order %" PetscInt_FMT " on %" PetscInt_FMT " points (dim %" PetscInt_FMT ") with %" PetscInt_FMT " components\n", quad->order, quad->numPoints, quad->dim, quad->Nc)); 53763a3b9bcSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(v, "Quadrature of order %" PetscInt_FMT " on %" PetscInt_FMT " points (dim %" PetscInt_FMT ")\n", quad->order, quad->numPoints, quad->dim)); 5389566063dSJacob Faibussowitsch PetscCall(PetscViewerGetFormat(v, &format)); 539d9bac1caSLisandro Dalcin if (format != PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(0); 540d9bac1caSLisandro Dalcin for (q = 0; q < quad->numPoints; ++q) { 54163a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(v, "p%" PetscInt_FMT " (", q)); 5429566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIUseTabs(v, PETSC_FALSE)); 543d9bac1caSLisandro Dalcin for (d = 0; d < quad->dim; ++d) { 5449566063dSJacob Faibussowitsch if (d) PetscCall(PetscViewerASCIIPrintf(v, ", ")); 5459566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(v, "%+g", (double)quad->points[q*quad->dim+d])); 546d9bac1caSLisandro Dalcin } 5479566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(v, ") ")); 54863a3b9bcSJacob Faibussowitsch if (quad->Nc > 1) PetscCall(PetscViewerASCIIPrintf(v, "w%" PetscInt_FMT " (", q)); 549d9bac1caSLisandro Dalcin for (c = 0; c < quad->Nc; ++c) { 5509566063dSJacob Faibussowitsch if (c) PetscCall(PetscViewerASCIIPrintf(v, ", ")); 5519566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(v, "%+g", (double)quad->weights[q*quad->Nc+c])); 552d9bac1caSLisandro Dalcin } 5539566063dSJacob Faibussowitsch if (quad->Nc > 1) PetscCall(PetscViewerASCIIPrintf(v, ")")); 5549566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(v, "\n")); 5559566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIUseTabs(v, PETSC_TRUE)); 556d9bac1caSLisandro Dalcin } 557d9bac1caSLisandro Dalcin PetscFunctionReturn(0); 558d9bac1caSLisandro Dalcin } 559d9bac1caSLisandro Dalcin 56040d8ff71SMatthew G. Knepley /*@C 56140d8ff71SMatthew G. Knepley PetscQuadratureView - Views a PetscQuadrature object 56240d8ff71SMatthew G. Knepley 563d083f849SBarry Smith Collective on quad 56440d8ff71SMatthew G. Knepley 56540d8ff71SMatthew G. Knepley Input Parameters: 566d9bac1caSLisandro Dalcin + quad - The PetscQuadrature object 56740d8ff71SMatthew G. Knepley - viewer - The PetscViewer object 56840d8ff71SMatthew G. Knepley 56940d8ff71SMatthew G. Knepley Level: beginner 57040d8ff71SMatthew G. Knepley 571db781477SPatrick Sanan .seealso: `PetscQuadratureCreate()`, `PetscQuadratureGetData()` 57240d8ff71SMatthew G. Knepley @*/ 573f9fd7fdbSMatthew G. Knepley PetscErrorCode PetscQuadratureView(PetscQuadrature quad, PetscViewer viewer) 574f9fd7fdbSMatthew G. Knepley { 575d9bac1caSLisandro Dalcin PetscBool iascii; 576f9fd7fdbSMatthew G. Knepley 577f9fd7fdbSMatthew G. Knepley PetscFunctionBegin; 578d9bac1caSLisandro Dalcin PetscValidHeader(quad, 1); 579d9bac1caSLisandro Dalcin if (viewer) PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 5809566063dSJacob Faibussowitsch if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject) quad), &viewer)); 5819566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii)); 5829566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 5839566063dSJacob Faibussowitsch if (iascii) PetscCall(PetscQuadratureView_Ascii(quad, viewer)); 5849566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 585bfa639d9SMatthew G. Knepley PetscFunctionReturn(0); 586bfa639d9SMatthew G. Knepley } 587bfa639d9SMatthew G. Knepley 58889710940SMatthew G. Knepley /*@C 58989710940SMatthew G. Knepley PetscQuadratureExpandComposite - Return a quadrature over the composite element, which has the original quadrature in each subelement 59089710940SMatthew G. Knepley 59189710940SMatthew G. Knepley Not collective 59289710940SMatthew G. Knepley 593d8d19677SJose E. Roman Input Parameters: 59489710940SMatthew G. Knepley + q - The original PetscQuadrature 59589710940SMatthew G. Knepley . numSubelements - The number of subelements the original element is divided into 59689710940SMatthew G. Knepley . v0 - An array of the initial points for each subelement 59789710940SMatthew G. Knepley - jac - An array of the Jacobian mappings from the reference to each subelement 59889710940SMatthew G. Knepley 59989710940SMatthew G. Knepley Output Parameters: 60089710940SMatthew G. Knepley . dim - The dimension 60189710940SMatthew G. Knepley 60289710940SMatthew G. Knepley Note: Together v0 and jac define an affine mapping from the original reference element to each subelement 60389710940SMatthew G. Knepley 604f5f57ec0SBarry Smith Not available from Fortran 605f5f57ec0SBarry Smith 60689710940SMatthew G. Knepley Level: intermediate 60789710940SMatthew G. Knepley 608db781477SPatrick Sanan .seealso: `PetscFECreate()`, `PetscSpaceGetDimension()`, `PetscDualSpaceGetDimension()` 60989710940SMatthew G. Knepley @*/ 61089710940SMatthew G. Knepley PetscErrorCode PetscQuadratureExpandComposite(PetscQuadrature q, PetscInt numSubelements, const PetscReal v0[], const PetscReal jac[], PetscQuadrature *qref) 61189710940SMatthew G. Knepley { 61289710940SMatthew G. Knepley const PetscReal *points, *weights; 61389710940SMatthew G. Knepley PetscReal *pointsRef, *weightsRef; 614a6b92713SMatthew G. Knepley PetscInt dim, Nc, order, npoints, npointsRef, c, p, cp, d, e; 61589710940SMatthew G. Knepley 61689710940SMatthew G. Knepley PetscFunctionBegin; 6172cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 618dadcf809SJacob Faibussowitsch PetscValidRealPointer(v0, 3); 619dadcf809SJacob Faibussowitsch PetscValidRealPointer(jac, 4); 62089710940SMatthew G. Knepley PetscValidPointer(qref, 5); 6219566063dSJacob Faibussowitsch PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, qref)); 6229566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetOrder(q, &order)); 6239566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetData(q, &dim, &Nc, &npoints, &points, &weights)); 62489710940SMatthew G. Knepley npointsRef = npoints*numSubelements; 6259566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(npointsRef*dim,&pointsRef)); 6269566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(npointsRef*Nc, &weightsRef)); 62789710940SMatthew G. Knepley for (c = 0; c < numSubelements; ++c) { 62889710940SMatthew G. Knepley for (p = 0; p < npoints; ++p) { 62989710940SMatthew G. Knepley for (d = 0; d < dim; ++d) { 63089710940SMatthew G. Knepley pointsRef[(c*npoints + p)*dim+d] = v0[c*dim+d]; 63189710940SMatthew G. Knepley for (e = 0; e < dim; ++e) { 63289710940SMatthew G. Knepley pointsRef[(c*npoints + p)*dim+d] += jac[(c*dim + d)*dim+e]*(points[p*dim+e] + 1.0); 63389710940SMatthew G. Knepley } 63489710940SMatthew G. Knepley } 63589710940SMatthew G. Knepley /* Could also use detJ here */ 636a6b92713SMatthew G. Knepley for (cp = 0; cp < Nc; ++cp) weightsRef[(c*npoints+p)*Nc+cp] = weights[p*Nc+cp]/numSubelements; 63789710940SMatthew G. Knepley } 63889710940SMatthew G. Knepley } 6399566063dSJacob Faibussowitsch PetscCall(PetscQuadratureSetOrder(*qref, order)); 6409566063dSJacob Faibussowitsch PetscCall(PetscQuadratureSetData(*qref, dim, Nc, npointsRef, pointsRef, weightsRef)); 64189710940SMatthew G. Knepley PetscFunctionReturn(0); 64289710940SMatthew G. Knepley } 64389710940SMatthew G. Knepley 64494e21283SToby Isaac /* Compute the coefficients for the Jacobi polynomial recurrence, 64594e21283SToby Isaac * 64694e21283SToby Isaac * J^{a,b}_n(x) = (cnm1 + cnm1x * x) * J^{a,b}_{n-1}(x) - cnm2 * J^{a,b}_{n-2}(x). 64794e21283SToby Isaac */ 64894e21283SToby Isaac #define PetscDTJacobiRecurrence_Internal(n,a,b,cnm1,cnm1x,cnm2) \ 64994e21283SToby Isaac do { \ 65094e21283SToby Isaac PetscReal _a = (a); \ 65194e21283SToby Isaac PetscReal _b = (b); \ 65294e21283SToby Isaac PetscReal _n = (n); \ 65394e21283SToby Isaac if (n == 1) { \ 65494e21283SToby Isaac (cnm1) = (_a-_b) * 0.5; \ 65594e21283SToby Isaac (cnm1x) = (_a+_b+2.)*0.5; \ 65694e21283SToby Isaac (cnm2) = 0.; \ 65794e21283SToby Isaac } else { \ 65894e21283SToby Isaac PetscReal _2n = _n+_n; \ 65994e21283SToby Isaac PetscReal _d = (_2n*(_n+_a+_b)*(_2n+_a+_b-2)); \ 66094e21283SToby Isaac PetscReal _n1 = (_2n+_a+_b-1.)*(_a*_a-_b*_b); \ 66194e21283SToby Isaac PetscReal _n1x = (_2n+_a+_b-1.)*(_2n+_a+_b)*(_2n+_a+_b-2); \ 66294e21283SToby Isaac PetscReal _n2 = 2.*((_n+_a-1.)*(_n+_b-1.)*(_2n+_a+_b)); \ 66394e21283SToby Isaac (cnm1) = _n1 / _d; \ 66494e21283SToby Isaac (cnm1x) = _n1x / _d; \ 66594e21283SToby Isaac (cnm2) = _n2 / _d; \ 66694e21283SToby Isaac } \ 66794e21283SToby Isaac } while (0) 66894e21283SToby Isaac 669fbdc3dfeSToby Isaac /*@ 670fbdc3dfeSToby Isaac PetscDTJacobiNorm - Compute the weighted L2 norm of a Jacobi polynomial. 671fbdc3dfeSToby Isaac 672fbdc3dfeSToby Isaac $\| P^{\alpha,\beta}_n \|_{\alpha,\beta}^2 = \int_{-1}^1 (1 + x)^{\alpha} (1 - x)^{\beta} P^{\alpha,\beta}_n (x)^2 dx.$ 673fbdc3dfeSToby Isaac 6744165533cSJose E. Roman Input Parameters: 675fbdc3dfeSToby Isaac - alpha - the left exponent > -1 676fbdc3dfeSToby Isaac . beta - the right exponent > -1 677fbdc3dfeSToby Isaac + n - the polynomial degree 678fbdc3dfeSToby Isaac 6794165533cSJose E. Roman Output Parameter: 680fbdc3dfeSToby Isaac . norm - the weighted L2 norm 681fbdc3dfeSToby Isaac 682fbdc3dfeSToby Isaac Level: beginner 683fbdc3dfeSToby Isaac 684db781477SPatrick Sanan .seealso: `PetscDTJacobiEval()` 685fbdc3dfeSToby Isaac @*/ 686fbdc3dfeSToby Isaac PetscErrorCode PetscDTJacobiNorm(PetscReal alpha, PetscReal beta, PetscInt n, PetscReal *norm) 687fbdc3dfeSToby Isaac { 688fbdc3dfeSToby Isaac PetscReal twoab1; 689fbdc3dfeSToby Isaac PetscReal gr; 690fbdc3dfeSToby Isaac 691fbdc3dfeSToby Isaac PetscFunctionBegin; 69208401ef6SPierre Jolivet PetscCheck(alpha > -1.,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Exponent alpha %g <= -1. invalid", (double) alpha); 69308401ef6SPierre Jolivet PetscCheck(beta > -1.,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Exponent beta %g <= -1. invalid", (double) beta); 69463a3b9bcSJacob Faibussowitsch PetscCheck(n >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "n %" PetscInt_FMT " < 0 invalid", n); 695fbdc3dfeSToby Isaac twoab1 = PetscPowReal(2., alpha + beta + 1.); 696fbdc3dfeSToby Isaac #if defined(PETSC_HAVE_LGAMMA) 697fbdc3dfeSToby Isaac if (!n) { 698fbdc3dfeSToby Isaac gr = PetscExpReal(PetscLGamma(alpha+1.) + PetscLGamma(beta+1.) - PetscLGamma(alpha+beta+2.)); 699fbdc3dfeSToby Isaac } else { 700fbdc3dfeSToby Isaac gr = PetscExpReal(PetscLGamma(n+alpha+1.) + PetscLGamma(n+beta+1.) - (PetscLGamma(n+1.) + PetscLGamma(n+alpha+beta+1.))) / (n+n+alpha+beta+1.); 701fbdc3dfeSToby Isaac } 702fbdc3dfeSToby Isaac #else 703fbdc3dfeSToby Isaac { 704fbdc3dfeSToby Isaac PetscInt alphai = (PetscInt) alpha; 705fbdc3dfeSToby Isaac PetscInt betai = (PetscInt) beta; 706fbdc3dfeSToby Isaac PetscInt i; 707fbdc3dfeSToby Isaac 708fbdc3dfeSToby Isaac gr = n ? (1. / (n+n+alpha+beta+1.)) : 1.; 709fbdc3dfeSToby Isaac if ((PetscReal) alphai == alpha) { 710fbdc3dfeSToby Isaac if (!n) { 711fbdc3dfeSToby Isaac for (i = 0; i < alphai; i++) gr *= (i+1.) / (beta+i+1.); 712fbdc3dfeSToby Isaac gr /= (alpha+beta+1.); 713fbdc3dfeSToby Isaac } else { 714fbdc3dfeSToby Isaac for (i = 0; i < alphai; i++) gr *= (n+i+1.) / (n+beta+i+1.); 715fbdc3dfeSToby Isaac } 716fbdc3dfeSToby Isaac } else if ((PetscReal) betai == beta) { 717fbdc3dfeSToby Isaac if (!n) { 718fbdc3dfeSToby Isaac for (i = 0; i < betai; i++) gr *= (i+1.) / (alpha+i+2.); 719fbdc3dfeSToby Isaac gr /= (alpha+beta+1.); 720fbdc3dfeSToby Isaac } else { 721fbdc3dfeSToby Isaac for (i = 0; i < betai; i++) gr *= (n+i+1.) / (n+alpha+i+1.); 722fbdc3dfeSToby Isaac } 723fbdc3dfeSToby Isaac } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"lgamma() - math routine is unavailable."); 724fbdc3dfeSToby Isaac } 725fbdc3dfeSToby Isaac #endif 726fbdc3dfeSToby Isaac *norm = PetscSqrtReal(twoab1 * gr); 727fbdc3dfeSToby Isaac PetscFunctionReturn(0); 728fbdc3dfeSToby Isaac } 729fbdc3dfeSToby Isaac 73094e21283SToby Isaac static PetscErrorCode PetscDTJacobiEval_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscInt k, const PetscReal *points, PetscInt ndegree, const PetscInt *degrees, PetscReal *p) 73194e21283SToby Isaac { 73294e21283SToby Isaac PetscReal ak, bk; 73394e21283SToby Isaac PetscReal abk1; 73494e21283SToby Isaac PetscInt i,l,maxdegree; 73594e21283SToby Isaac 73694e21283SToby Isaac PetscFunctionBegin; 73794e21283SToby Isaac maxdegree = degrees[ndegree-1] - k; 73894e21283SToby Isaac ak = a + k; 73994e21283SToby Isaac bk = b + k; 74094e21283SToby Isaac abk1 = a + b + k + 1.; 74194e21283SToby Isaac if (maxdegree < 0) { 74294e21283SToby Isaac for (i = 0; i < npoints; i++) for (l = 0; l < ndegree; l++) p[i*ndegree+l] = 0.; 74394e21283SToby Isaac PetscFunctionReturn(0); 74494e21283SToby Isaac } 74594e21283SToby Isaac for (i=0; i<npoints; i++) { 74694e21283SToby Isaac PetscReal pm1,pm2,x; 74794e21283SToby Isaac PetscReal cnm1, cnm1x, cnm2; 74894e21283SToby Isaac PetscInt j,m; 74994e21283SToby Isaac 75094e21283SToby Isaac x = points[i]; 75194e21283SToby Isaac pm2 = 1.; 75294e21283SToby Isaac PetscDTJacobiRecurrence_Internal(1,ak,bk,cnm1,cnm1x,cnm2); 75394e21283SToby Isaac pm1 = (cnm1 + cnm1x*x); 75494e21283SToby Isaac l = 0; 75594e21283SToby Isaac while (l < ndegree && degrees[l] - k < 0) { 75694e21283SToby Isaac p[l++] = 0.; 75794e21283SToby Isaac } 75894e21283SToby Isaac while (l < ndegree && degrees[l] - k == 0) { 75994e21283SToby Isaac p[l] = pm2; 76094e21283SToby Isaac for (m = 0; m < k; m++) p[l] *= (abk1 + m) * 0.5; 76194e21283SToby Isaac l++; 76294e21283SToby Isaac } 76394e21283SToby Isaac while (l < ndegree && degrees[l] - k == 1) { 76494e21283SToby Isaac p[l] = pm1; 76594e21283SToby Isaac for (m = 0; m < k; m++) p[l] *= (abk1 + 1 + m) * 0.5; 76694e21283SToby Isaac l++; 76794e21283SToby Isaac } 76894e21283SToby Isaac for (j=2; j<=maxdegree; j++) { 76994e21283SToby Isaac PetscReal pp; 77094e21283SToby Isaac 77194e21283SToby Isaac PetscDTJacobiRecurrence_Internal(j,ak,bk,cnm1,cnm1x,cnm2); 77294e21283SToby Isaac pp = (cnm1 + cnm1x*x)*pm1 - cnm2*pm2; 77394e21283SToby Isaac pm2 = pm1; 77494e21283SToby Isaac pm1 = pp; 77594e21283SToby Isaac while (l < ndegree && degrees[l] - k == j) { 77694e21283SToby Isaac p[l] = pp; 77794e21283SToby Isaac for (m = 0; m < k; m++) p[l] *= (abk1 + j + m) * 0.5; 77894e21283SToby Isaac l++; 77994e21283SToby Isaac } 78094e21283SToby Isaac } 78194e21283SToby Isaac p += ndegree; 78294e21283SToby Isaac } 78394e21283SToby Isaac PetscFunctionReturn(0); 78494e21283SToby Isaac } 78594e21283SToby Isaac 78637045ce4SJed Brown /*@ 787fbdc3dfeSToby 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$. 788fbdc3dfeSToby Isaac 7894165533cSJose E. Roman Input Parameters: 790fbdc3dfeSToby Isaac + alpha - the left exponent of the weight 791fbdc3dfeSToby Isaac . beta - the right exponetn of the weight 792fbdc3dfeSToby Isaac . npoints - the number of points to evaluate the polynomials at 793fbdc3dfeSToby Isaac . points - [npoints] array of point coordinates 794fbdc3dfeSToby Isaac . degree - the maximm degree polynomial space to evaluate, (degree + 1) will be evaluated total. 795fbdc3dfeSToby Isaac - k - the maximum derivative to evaluate in the jet, (k + 1) will be evaluated total. 796fbdc3dfeSToby Isaac 7976aad120cSJose E. Roman Output Parameters: 798fbdc3dfeSToby Isaac - p - an array containing the evaluations of the Jacobi polynomials's jets on the points. the size is (degree + 1) x 799fbdc3dfeSToby Isaac (k + 1) x npoints, which also describes the order of the dimensions of this three-dimensional array: the first 800fbdc3dfeSToby Isaac (slowest varying) dimension is polynomial degree; the second dimension is derivative order; the third (fastest 801fbdc3dfeSToby Isaac varying) dimension is the index of the evaluation point. 802fbdc3dfeSToby Isaac 803fbdc3dfeSToby Isaac Level: advanced 804fbdc3dfeSToby Isaac 805db781477SPatrick Sanan .seealso: `PetscDTJacobiEval()`, `PetscDTPKDEvalJet()` 806fbdc3dfeSToby Isaac @*/ 807fbdc3dfeSToby Isaac PetscErrorCode PetscDTJacobiEvalJet(PetscReal alpha, PetscReal beta, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt k, PetscReal p[]) 808fbdc3dfeSToby Isaac { 809fbdc3dfeSToby Isaac PetscInt i, j, l; 810fbdc3dfeSToby Isaac PetscInt *degrees; 811fbdc3dfeSToby Isaac PetscReal *psingle; 812fbdc3dfeSToby Isaac 813fbdc3dfeSToby Isaac PetscFunctionBegin; 814fbdc3dfeSToby Isaac if (degree == 0) { 815fbdc3dfeSToby Isaac PetscInt zero = 0; 816fbdc3dfeSToby Isaac 817fbdc3dfeSToby Isaac for (i = 0; i <= k; i++) { 8189566063dSJacob Faibussowitsch PetscCall(PetscDTJacobiEval_Internal(npoints, alpha, beta, i, points, 1, &zero, &p[i*npoints])); 819fbdc3dfeSToby Isaac } 820fbdc3dfeSToby Isaac PetscFunctionReturn(0); 821fbdc3dfeSToby Isaac } 8229566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(degree + 1, °rees)); 8239566063dSJacob Faibussowitsch PetscCall(PetscMalloc1((degree + 1) * npoints, &psingle)); 824fbdc3dfeSToby Isaac for (i = 0; i <= degree; i++) degrees[i] = i; 825fbdc3dfeSToby Isaac for (i = 0; i <= k; i++) { 8269566063dSJacob Faibussowitsch PetscCall(PetscDTJacobiEval_Internal(npoints, alpha, beta, i, points, degree + 1, degrees, psingle)); 827fbdc3dfeSToby Isaac for (j = 0; j <= degree; j++) { 828fbdc3dfeSToby Isaac for (l = 0; l < npoints; l++) { 829fbdc3dfeSToby Isaac p[(j * (k + 1) + i) * npoints + l] = psingle[l * (degree + 1) + j]; 830fbdc3dfeSToby Isaac } 831fbdc3dfeSToby Isaac } 832fbdc3dfeSToby Isaac } 8339566063dSJacob Faibussowitsch PetscCall(PetscFree(psingle)); 8349566063dSJacob Faibussowitsch PetscCall(PetscFree(degrees)); 835fbdc3dfeSToby Isaac PetscFunctionReturn(0); 836fbdc3dfeSToby Isaac } 837fbdc3dfeSToby Isaac 838fbdc3dfeSToby Isaac /*@ 83994e21283SToby Isaac PetscDTJacobiEval - evaluate Jacobi polynomials for the weight function $(1.+x)^{\alpha} (1.-x)^{\beta}$ 84094e21283SToby Isaac at points 84194e21283SToby Isaac 84294e21283SToby Isaac Not Collective 84394e21283SToby Isaac 8444165533cSJose E. Roman Input Parameters: 84594e21283SToby Isaac + npoints - number of spatial points to evaluate at 84694e21283SToby Isaac . alpha - the left exponent > -1 84794e21283SToby Isaac . beta - the right exponent > -1 84894e21283SToby Isaac . points - array of locations to evaluate at 84994e21283SToby Isaac . ndegree - number of basis degrees to evaluate 85094e21283SToby Isaac - degrees - sorted array of degrees to evaluate 85194e21283SToby Isaac 8524165533cSJose E. Roman Output Parameters: 85394e21283SToby Isaac + B - row-oriented basis evaluation matrix B[point*ndegree + degree] (dimension npoints*ndegrees, allocated by caller) (or NULL) 85494e21283SToby Isaac . D - row-oriented derivative evaluation matrix (or NULL) 85594e21283SToby Isaac - D2 - row-oriented second derivative evaluation matrix (or NULL) 85694e21283SToby Isaac 85794e21283SToby Isaac Level: intermediate 85894e21283SToby Isaac 859db781477SPatrick Sanan .seealso: `PetscDTGaussQuadrature()` 86094e21283SToby Isaac @*/ 86194e21283SToby Isaac PetscErrorCode PetscDTJacobiEval(PetscInt npoints,PetscReal alpha, PetscReal beta, const PetscReal *points,PetscInt ndegree,const PetscInt *degrees,PetscReal *B,PetscReal *D,PetscReal *D2) 86294e21283SToby Isaac { 86394e21283SToby Isaac PetscFunctionBegin; 86408401ef6SPierre Jolivet PetscCheck(alpha > -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"alpha must be > -1."); 86508401ef6SPierre Jolivet PetscCheck(beta > -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"beta must be > -1."); 86694e21283SToby Isaac if (!npoints || !ndegree) PetscFunctionReturn(0); 8679566063dSJacob Faibussowitsch if (B) PetscCall(PetscDTJacobiEval_Internal(npoints, alpha, beta, 0, points, ndegree, degrees, B)); 8689566063dSJacob Faibussowitsch if (D) PetscCall(PetscDTJacobiEval_Internal(npoints, alpha, beta, 1, points, ndegree, degrees, D)); 8699566063dSJacob Faibussowitsch if (D2) PetscCall(PetscDTJacobiEval_Internal(npoints, alpha, beta, 2, points, ndegree, degrees, D2)); 87094e21283SToby Isaac PetscFunctionReturn(0); 87194e21283SToby Isaac } 87294e21283SToby Isaac 87394e21283SToby Isaac /*@ 87494e21283SToby Isaac PetscDTLegendreEval - evaluate Legendre polynomials at points 87537045ce4SJed Brown 87637045ce4SJed Brown Not Collective 87737045ce4SJed Brown 8784165533cSJose E. Roman Input Parameters: 87937045ce4SJed Brown + npoints - number of spatial points to evaluate at 88037045ce4SJed Brown . points - array of locations to evaluate at 88137045ce4SJed Brown . ndegree - number of basis degrees to evaluate 88237045ce4SJed Brown - degrees - sorted array of degrees to evaluate 88337045ce4SJed Brown 8844165533cSJose E. Roman Output Parameters: 8850298fd71SBarry Smith + B - row-oriented basis evaluation matrix B[point*ndegree + degree] (dimension npoints*ndegrees, allocated by caller) (or NULL) 8860298fd71SBarry Smith . D - row-oriented derivative evaluation matrix (or NULL) 8870298fd71SBarry Smith - D2 - row-oriented second derivative evaluation matrix (or NULL) 88837045ce4SJed Brown 88937045ce4SJed Brown Level: intermediate 89037045ce4SJed Brown 891db781477SPatrick Sanan .seealso: `PetscDTGaussQuadrature()` 89237045ce4SJed Brown @*/ 89337045ce4SJed Brown PetscErrorCode PetscDTLegendreEval(PetscInt npoints,const PetscReal *points,PetscInt ndegree,const PetscInt *degrees,PetscReal *B,PetscReal *D,PetscReal *D2) 89437045ce4SJed Brown { 89537045ce4SJed Brown PetscFunctionBegin; 8969566063dSJacob Faibussowitsch PetscCall(PetscDTJacobiEval(npoints, 0., 0., points, ndegree, degrees, B, D, D2)); 89737045ce4SJed Brown PetscFunctionReturn(0); 89837045ce4SJed Brown } 89937045ce4SJed Brown 900fbdc3dfeSToby Isaac /*@ 901fbdc3dfeSToby 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) 902fbdc3dfeSToby Isaac 903fbdc3dfeSToby Isaac Input Parameters: 904fbdc3dfeSToby Isaac + len - the desired length of the degree tuple 905fbdc3dfeSToby Isaac - index - the index to convert: should be >= 0 906fbdc3dfeSToby Isaac 907fbdc3dfeSToby Isaac Output Parameter: 908fbdc3dfeSToby Isaac . degtup - will be filled with a tuple of degrees 909fbdc3dfeSToby Isaac 910fbdc3dfeSToby Isaac Level: beginner 911fbdc3dfeSToby Isaac 912fbdc3dfeSToby Isaac Note: for two tuples x and y with the same degree sum, partial degree sums over the final elements of the tuples 913fbdc3dfeSToby 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 914fbdc3dfeSToby Isaac last two elements is smaller for the former, so (2, 1, 1) < (1, 2, 1). 915fbdc3dfeSToby Isaac 916db781477SPatrick Sanan .seealso: `PetscDTGradedOrderToIndex()` 917fbdc3dfeSToby Isaac @*/ 918fbdc3dfeSToby Isaac PetscErrorCode PetscDTIndexToGradedOrder(PetscInt len, PetscInt index, PetscInt degtup[]) 919fbdc3dfeSToby Isaac { 920fbdc3dfeSToby Isaac PetscInt i, total; 921fbdc3dfeSToby Isaac PetscInt sum; 922fbdc3dfeSToby Isaac 923fbdc3dfeSToby Isaac PetscFunctionBeginHot; 92408401ef6SPierre Jolivet PetscCheck(len >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative"); 92508401ef6SPierre Jolivet PetscCheck(index >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index must be non-negative"); 926fbdc3dfeSToby Isaac total = 1; 927fbdc3dfeSToby Isaac sum = 0; 928fbdc3dfeSToby Isaac while (index >= total) { 929fbdc3dfeSToby Isaac index -= total; 930fbdc3dfeSToby Isaac total = (total * (len + sum)) / (sum + 1); 931fbdc3dfeSToby Isaac sum++; 932fbdc3dfeSToby Isaac } 933fbdc3dfeSToby Isaac for (i = 0; i < len; i++) { 934fbdc3dfeSToby Isaac PetscInt c; 935fbdc3dfeSToby Isaac 936fbdc3dfeSToby Isaac degtup[i] = sum; 937fbdc3dfeSToby Isaac for (c = 0, total = 1; c < sum; c++) { 938fbdc3dfeSToby Isaac /* going into the loop, total is the number of way to have a tuple of sum exactly c with length len - 1 - i */ 939fbdc3dfeSToby Isaac if (index < total) break; 940fbdc3dfeSToby Isaac index -= total; 941fbdc3dfeSToby Isaac total = (total * (len - 1 - i + c)) / (c + 1); 942fbdc3dfeSToby Isaac degtup[i]--; 943fbdc3dfeSToby Isaac } 944fbdc3dfeSToby Isaac sum -= degtup[i]; 945fbdc3dfeSToby Isaac } 946fbdc3dfeSToby Isaac PetscFunctionReturn(0); 947fbdc3dfeSToby Isaac } 948fbdc3dfeSToby Isaac 949fbdc3dfeSToby Isaac /*@ 950fbdc3dfeSToby Isaac PetscDTGradedOrderToIndex - convert a tuple into an index in a graded order, the inverse of PetscDTIndexToGradedOrder(). 951fbdc3dfeSToby Isaac 952fbdc3dfeSToby Isaac Input Parameters: 953fbdc3dfeSToby Isaac + len - the length of the degree tuple 954fbdc3dfeSToby Isaac - degtup - tuple with this length 955fbdc3dfeSToby Isaac 956fbdc3dfeSToby Isaac Output Parameter: 957fbdc3dfeSToby Isaac . index - index in graded order: >= 0 958fbdc3dfeSToby Isaac 959fbdc3dfeSToby Isaac Level: Beginner 960fbdc3dfeSToby Isaac 961fbdc3dfeSToby Isaac Note: for two tuples x and y with the same degree sum, partial degree sums over the final elements of the tuples 962fbdc3dfeSToby 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 963fbdc3dfeSToby Isaac last two elements is smaller for the former, so (2, 1, 1) < (1, 2, 1). 964fbdc3dfeSToby Isaac 965db781477SPatrick Sanan .seealso: `PetscDTIndexToGradedOrder()` 966fbdc3dfeSToby Isaac @*/ 967fbdc3dfeSToby Isaac PetscErrorCode PetscDTGradedOrderToIndex(PetscInt len, const PetscInt degtup[], PetscInt *index) 968fbdc3dfeSToby Isaac { 969fbdc3dfeSToby Isaac PetscInt i, idx, sum, total; 970fbdc3dfeSToby Isaac 971fbdc3dfeSToby Isaac PetscFunctionBeginHot; 97208401ef6SPierre Jolivet PetscCheck(len >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative"); 973fbdc3dfeSToby Isaac for (i = 0, sum = 0; i < len; i++) sum += degtup[i]; 974fbdc3dfeSToby Isaac idx = 0; 975fbdc3dfeSToby Isaac total = 1; 976fbdc3dfeSToby Isaac for (i = 0; i < sum; i++) { 977fbdc3dfeSToby Isaac idx += total; 978fbdc3dfeSToby Isaac total = (total * (len + i)) / (i + 1); 979fbdc3dfeSToby Isaac } 980fbdc3dfeSToby Isaac for (i = 0; i < len - 1; i++) { 981fbdc3dfeSToby Isaac PetscInt c; 982fbdc3dfeSToby Isaac 983fbdc3dfeSToby Isaac total = 1; 984fbdc3dfeSToby Isaac sum -= degtup[i]; 985fbdc3dfeSToby Isaac for (c = 0; c < sum; c++) { 986fbdc3dfeSToby Isaac idx += total; 987fbdc3dfeSToby Isaac total = (total * (len - 1 - i + c)) / (c + 1); 988fbdc3dfeSToby Isaac } 989fbdc3dfeSToby Isaac } 990fbdc3dfeSToby Isaac *index = idx; 991fbdc3dfeSToby Isaac PetscFunctionReturn(0); 992fbdc3dfeSToby Isaac } 993fbdc3dfeSToby Isaac 994e3aa2e09SToby Isaac static PetscBool PKDCite = PETSC_FALSE; 995e3aa2e09SToby Isaac const char PKDCitation[] = "@article{Kirby2010,\n" 996e3aa2e09SToby Isaac " title={Singularity-free evaluation of collapsed-coordinate orthogonal polynomials},\n" 997e3aa2e09SToby Isaac " author={Kirby, Robert C},\n" 998e3aa2e09SToby Isaac " journal={ACM Transactions on Mathematical Software (TOMS)},\n" 999e3aa2e09SToby Isaac " volume={37},\n" 1000e3aa2e09SToby Isaac " number={1},\n" 1001e3aa2e09SToby Isaac " pages={1--16},\n" 1002e3aa2e09SToby Isaac " year={2010},\n" 1003e3aa2e09SToby Isaac " publisher={ACM New York, NY, USA}\n}\n"; 1004e3aa2e09SToby Isaac 1005fbdc3dfeSToby Isaac /*@ 1006d8f25ad8SToby Isaac PetscDTPKDEvalJet - Evaluate the jet (function and derivatives) of the Proriol-Koornwinder-Dubiner (PKD) basis for 1007fbdc3dfeSToby Isaac the space of polynomials up to a given degree. The PKD basis is L2-orthonormal on the biunit simplex (which is used 1008fbdc3dfeSToby Isaac as the reference element for finite elements in PETSc), which makes it a stable basis to use for evaluating 1009fbdc3dfeSToby Isaac polynomials in that domain. 1010fbdc3dfeSToby Isaac 10114165533cSJose E. Roman Input Parameters: 1012fbdc3dfeSToby Isaac + dim - the number of variables in the multivariate polynomials 1013fbdc3dfeSToby Isaac . npoints - the number of points to evaluate the polynomials at 1014fbdc3dfeSToby Isaac . points - [npoints x dim] array of point coordinates 1015fbdc3dfeSToby 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. 1016fbdc3dfeSToby Isaac - k - the maximum order partial derivative to evaluate in the jet. There are (dim + k choose dim) partial derivatives 1017fbdc3dfeSToby Isaac in the jet. Choosing k = 0 means to evaluate just the function and no derivatives 1018fbdc3dfeSToby Isaac 10196aad120cSJose E. Roman Output Parameters: 1020fbdc3dfeSToby Isaac - p - an array containing the evaluations of the PKD polynomials' jets on the points. The size is ((dim + degree) 1021fbdc3dfeSToby Isaac choose dim) x ((dim + k) choose dim) x npoints, which also describes the order of the dimensions of this 1022fbdc3dfeSToby Isaac three-dimensional array: the first (slowest varying) dimension is basis function index; the second dimension is jet 1023fbdc3dfeSToby Isaac index; the third (fastest varying) dimension is the index of the evaluation point. 1024fbdc3dfeSToby Isaac 1025fbdc3dfeSToby Isaac Level: advanced 1026fbdc3dfeSToby Isaac 1027fbdc3dfeSToby Isaac Note: The ordering of the basis functions, and the ordering of the derivatives in the jet, both follow the graded 1028fbdc3dfeSToby Isaac ordering of PetscDTIndexToGradedOrder() and PetscDTGradedOrderToIndex(). For example, in 3D, the polynomial with 1029d8f25ad8SToby Isaac leading monomial x^2,y^0,z^1, which has degree tuple (2,0,1), which by PetscDTGradedOrderToIndex() has index 12 (it is the 13th basis function in the space); 1030fbdc3dfeSToby 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). 1031fbdc3dfeSToby Isaac 1032e3aa2e09SToby Isaac The implementation uses Kirby's singularity-free evaluation algorithm, https://doi.org/10.1145/1644001.1644006. 1033e3aa2e09SToby Isaac 1034db781477SPatrick Sanan .seealso: `PetscDTGradedOrderToIndex()`, `PetscDTIndexToGradedOrder()`, `PetscDTJacobiEvalJet()` 1035fbdc3dfeSToby Isaac @*/ 1036fbdc3dfeSToby Isaac PetscErrorCode PetscDTPKDEvalJet(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt k, PetscReal p[]) 1037fbdc3dfeSToby Isaac { 1038fbdc3dfeSToby Isaac PetscInt degidx, kidx, d, pt; 1039fbdc3dfeSToby Isaac PetscInt Nk, Ndeg; 1040fbdc3dfeSToby Isaac PetscInt *ktup, *degtup; 1041fbdc3dfeSToby Isaac PetscReal *scales, initscale, scaleexp; 1042fbdc3dfeSToby Isaac 1043fbdc3dfeSToby Isaac PetscFunctionBegin; 10449566063dSJacob Faibussowitsch PetscCall(PetscCitationsRegister(PKDCitation, &PKDCite)); 10459566063dSJacob Faibussowitsch PetscCall(PetscDTBinomialInt(dim + k, k, &Nk)); 10469566063dSJacob Faibussowitsch PetscCall(PetscDTBinomialInt(degree + dim, degree, &Ndeg)); 10479566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(dim, °tup, dim, &ktup)); 10489566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Ndeg, &scales)); 1049fbdc3dfeSToby Isaac initscale = 1.; 1050fbdc3dfeSToby Isaac if (dim > 1) { 10519566063dSJacob Faibussowitsch PetscCall(PetscDTBinomial(dim,2,&scaleexp)); 10522f613bf5SBarry Smith initscale = PetscPowReal(2.,scaleexp*0.5); 1053fbdc3dfeSToby Isaac } 1054fbdc3dfeSToby Isaac for (degidx = 0; degidx < Ndeg; degidx++) { 1055fbdc3dfeSToby Isaac PetscInt e, i; 1056fbdc3dfeSToby Isaac PetscInt m1idx = -1, m2idx = -1; 1057fbdc3dfeSToby Isaac PetscInt n; 1058fbdc3dfeSToby Isaac PetscInt degsum; 1059fbdc3dfeSToby Isaac PetscReal alpha; 1060fbdc3dfeSToby Isaac PetscReal cnm1, cnm1x, cnm2; 1061fbdc3dfeSToby Isaac PetscReal norm; 1062fbdc3dfeSToby Isaac 10639566063dSJacob Faibussowitsch PetscCall(PetscDTIndexToGradedOrder(dim, degidx, degtup)); 1064fbdc3dfeSToby Isaac for (d = dim - 1; d >= 0; d--) if (degtup[d]) break; 1065fbdc3dfeSToby Isaac if (d < 0) { /* constant is 1 everywhere, all derivatives are zero */ 1066fbdc3dfeSToby Isaac scales[degidx] = initscale; 1067fbdc3dfeSToby Isaac for (e = 0; e < dim; e++) { 10689566063dSJacob Faibussowitsch PetscCall(PetscDTJacobiNorm(e,0.,0,&norm)); 1069fbdc3dfeSToby Isaac scales[degidx] /= norm; 1070fbdc3dfeSToby Isaac } 1071fbdc3dfeSToby Isaac for (i = 0; i < npoints; i++) p[degidx * Nk * npoints + i] = 1.; 1072fbdc3dfeSToby Isaac for (i = 0; i < (Nk - 1) * npoints; i++) p[(degidx * Nk + 1) * npoints + i] = 0.; 1073fbdc3dfeSToby Isaac continue; 1074fbdc3dfeSToby Isaac } 1075fbdc3dfeSToby Isaac n = degtup[d]; 1076fbdc3dfeSToby Isaac degtup[d]--; 10779566063dSJacob Faibussowitsch PetscCall(PetscDTGradedOrderToIndex(dim, degtup, &m1idx)); 1078fbdc3dfeSToby Isaac if (degtup[d] > 0) { 1079fbdc3dfeSToby Isaac degtup[d]--; 10809566063dSJacob Faibussowitsch PetscCall(PetscDTGradedOrderToIndex(dim, degtup, &m2idx)); 1081fbdc3dfeSToby Isaac degtup[d]++; 1082fbdc3dfeSToby Isaac } 1083fbdc3dfeSToby Isaac degtup[d]++; 1084fbdc3dfeSToby Isaac for (e = 0, degsum = 0; e < d; e++) degsum += degtup[e]; 1085fbdc3dfeSToby Isaac alpha = 2 * degsum + d; 1086fbdc3dfeSToby Isaac PetscDTJacobiRecurrence_Internal(n,alpha,0.,cnm1,cnm1x,cnm2); 1087fbdc3dfeSToby Isaac 1088fbdc3dfeSToby Isaac scales[degidx] = initscale; 1089fbdc3dfeSToby Isaac for (e = 0, degsum = 0; e < dim; e++) { 1090fbdc3dfeSToby Isaac PetscInt f; 1091fbdc3dfeSToby Isaac PetscReal ealpha; 1092fbdc3dfeSToby Isaac PetscReal enorm; 1093fbdc3dfeSToby Isaac 1094fbdc3dfeSToby Isaac ealpha = 2 * degsum + e; 1095fbdc3dfeSToby Isaac for (f = 0; f < degsum; f++) scales[degidx] *= 2.; 10969566063dSJacob Faibussowitsch PetscCall(PetscDTJacobiNorm(ealpha,0.,degtup[e],&enorm)); 1097fbdc3dfeSToby Isaac scales[degidx] /= enorm; 1098fbdc3dfeSToby Isaac degsum += degtup[e]; 1099fbdc3dfeSToby Isaac } 1100fbdc3dfeSToby Isaac 1101fbdc3dfeSToby Isaac for (pt = 0; pt < npoints; pt++) { 1102fbdc3dfeSToby Isaac /* compute the multipliers */ 1103fbdc3dfeSToby Isaac PetscReal thetanm1, thetanm1x, thetanm2; 1104fbdc3dfeSToby Isaac 1105fbdc3dfeSToby Isaac thetanm1x = dim - (d+1) + 2.*points[pt * dim + d]; 1106fbdc3dfeSToby Isaac for (e = d+1; e < dim; e++) thetanm1x += points[pt * dim + e]; 1107fbdc3dfeSToby Isaac thetanm1x *= 0.5; 1108fbdc3dfeSToby Isaac thetanm1 = (2. - (dim-(d+1))); 1109fbdc3dfeSToby Isaac for (e = d+1; e < dim; e++) thetanm1 -= points[pt * dim + e]; 1110fbdc3dfeSToby Isaac thetanm1 *= 0.5; 1111fbdc3dfeSToby Isaac thetanm2 = thetanm1 * thetanm1; 1112fbdc3dfeSToby Isaac 1113fbdc3dfeSToby Isaac for (kidx = 0; kidx < Nk; kidx++) { 1114fbdc3dfeSToby Isaac PetscInt f; 1115fbdc3dfeSToby Isaac 11169566063dSJacob Faibussowitsch PetscCall(PetscDTIndexToGradedOrder(dim, kidx, ktup)); 1117fbdc3dfeSToby Isaac /* first sum in the same derivative terms */ 1118fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] = (cnm1 * thetanm1 + cnm1x * thetanm1x) * p[(m1idx * Nk + kidx) * npoints + pt]; 1119fbdc3dfeSToby Isaac if (m2idx >= 0) { 1120fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] -= cnm2 * thetanm2 * p[(m2idx * Nk + kidx) * npoints + pt]; 1121fbdc3dfeSToby Isaac } 1122fbdc3dfeSToby Isaac 1123fbdc3dfeSToby Isaac for (f = d; f < dim; f++) { 1124fbdc3dfeSToby Isaac PetscInt km1idx, mplty = ktup[f]; 1125fbdc3dfeSToby Isaac 1126fbdc3dfeSToby Isaac if (!mplty) continue; 1127fbdc3dfeSToby Isaac ktup[f]--; 11289566063dSJacob Faibussowitsch PetscCall(PetscDTGradedOrderToIndex(dim, ktup, &km1idx)); 1129fbdc3dfeSToby Isaac 1130fbdc3dfeSToby Isaac /* the derivative of cnm1x * thetanm1x wrt x variable f is 0.5 * cnm1x if f > d otherwise it is cnm1x */ 1131fbdc3dfeSToby Isaac /* the derivative of cnm1 * thetanm1 wrt x variable f is 0 if f == d, otherwise it is -0.5 * cnm1 */ 1132fbdc3dfeSToby Isaac /* the derivative of -cnm2 * thetanm2 wrt x variable f is 0 if f == d, otherwise it is cnm2 * thetanm1 */ 1133fbdc3dfeSToby Isaac if (f > d) { 1134fbdc3dfeSToby Isaac PetscInt f2; 1135fbdc3dfeSToby Isaac 1136fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] += mplty * 0.5 * (cnm1x - cnm1) * p[(m1idx * Nk + km1idx) * npoints + pt]; 1137fbdc3dfeSToby Isaac if (m2idx >= 0) { 1138fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] += mplty * cnm2 * thetanm1 * p[(m2idx * Nk + km1idx) * npoints + pt]; 1139fbdc3dfeSToby Isaac /* second derivatives of -cnm2 * thetanm2 wrt x variable f,f2 is like - 0.5 * cnm2 */ 1140fbdc3dfeSToby Isaac for (f2 = f; f2 < dim; f2++) { 1141fbdc3dfeSToby Isaac PetscInt km2idx, mplty2 = ktup[f2]; 1142fbdc3dfeSToby Isaac PetscInt factor; 1143fbdc3dfeSToby Isaac 1144fbdc3dfeSToby Isaac if (!mplty2) continue; 1145fbdc3dfeSToby Isaac ktup[f2]--; 11469566063dSJacob Faibussowitsch PetscCall(PetscDTGradedOrderToIndex(dim, ktup, &km2idx)); 1147fbdc3dfeSToby Isaac 1148fbdc3dfeSToby Isaac factor = mplty * mplty2; 1149fbdc3dfeSToby Isaac if (f == f2) factor /= 2; 1150fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] -= 0.5 * factor * cnm2 * p[(m2idx * Nk + km2idx) * npoints + pt]; 1151fbdc3dfeSToby Isaac ktup[f2]++; 1152fbdc3dfeSToby Isaac } 11533034baaeSToby Isaac } 1154fbdc3dfeSToby Isaac } else { 1155fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] += mplty * cnm1x * p[(m1idx * Nk + km1idx) * npoints + pt]; 1156fbdc3dfeSToby Isaac } 1157fbdc3dfeSToby Isaac ktup[f]++; 1158fbdc3dfeSToby Isaac } 1159fbdc3dfeSToby Isaac } 1160fbdc3dfeSToby Isaac } 1161fbdc3dfeSToby Isaac } 1162fbdc3dfeSToby Isaac for (degidx = 0; degidx < Ndeg; degidx++) { 1163fbdc3dfeSToby Isaac PetscReal scale = scales[degidx]; 1164fbdc3dfeSToby Isaac PetscInt i; 1165fbdc3dfeSToby Isaac 1166fbdc3dfeSToby Isaac for (i = 0; i < Nk * npoints; i++) p[degidx*Nk*npoints + i] *= scale; 1167fbdc3dfeSToby Isaac } 11689566063dSJacob Faibussowitsch PetscCall(PetscFree(scales)); 11699566063dSJacob Faibussowitsch PetscCall(PetscFree2(degtup, ktup)); 1170fbdc3dfeSToby Isaac PetscFunctionReturn(0); 1171fbdc3dfeSToby Isaac } 1172fbdc3dfeSToby Isaac 1173d8f25ad8SToby Isaac /*@ 1174d8f25ad8SToby Isaac PetscDTPTrimmedSize - The size of the trimmed polynomial space of k-forms with a given degree and form degree, 1175d8f25ad8SToby Isaac which can be evaluated in PetscDTPTrimmedEvalJet(). 1176d8f25ad8SToby Isaac 1177d8f25ad8SToby Isaac Input Parameters: 1178d8f25ad8SToby Isaac + dim - the number of variables in the multivariate polynomials 1179d8f25ad8SToby Isaac . degree - the degree (sum of degrees on the variables in a monomial) of the trimmed polynomial space. 1180d8f25ad8SToby Isaac - formDegree - the degree of the form 1181d8f25ad8SToby Isaac 11826aad120cSJose E. Roman Output Parameters: 1183d8f25ad8SToby Isaac - size - The number ((dim + degree) choose (dim + formDegree)) x ((degree + formDegree - 1) choose (formDegree)) 1184d8f25ad8SToby Isaac 1185d8f25ad8SToby Isaac Level: advanced 1186d8f25ad8SToby Isaac 1187db781477SPatrick Sanan .seealso: `PetscDTPTrimmedEvalJet()` 1188d8f25ad8SToby Isaac @*/ 1189d8f25ad8SToby Isaac PetscErrorCode PetscDTPTrimmedSize(PetscInt dim, PetscInt degree, PetscInt formDegree, PetscInt *size) 1190d8f25ad8SToby Isaac { 1191d8f25ad8SToby Isaac PetscInt Nrk, Nbpt; // number of trimmed polynomials 1192d8f25ad8SToby Isaac 1193d8f25ad8SToby Isaac PetscFunctionBegin; 1194d8f25ad8SToby Isaac formDegree = PetscAbsInt(formDegree); 11959566063dSJacob Faibussowitsch PetscCall(PetscDTBinomialInt(degree + dim, degree + formDegree, &Nbpt)); 11969566063dSJacob Faibussowitsch PetscCall(PetscDTBinomialInt(degree + formDegree - 1, formDegree, &Nrk)); 1197d8f25ad8SToby Isaac Nbpt *= Nrk; 1198d8f25ad8SToby Isaac *size = Nbpt; 1199d8f25ad8SToby Isaac PetscFunctionReturn(0); 1200d8f25ad8SToby Isaac } 1201d8f25ad8SToby Isaac 1202d8f25ad8SToby Isaac /* there was a reference implementation based on section 4.4 of Arnold, Falk & Winther (acta numerica, 2006), but it 1203d8f25ad8SToby Isaac * was inferior to this implementation */ 1204d8f25ad8SToby Isaac static PetscErrorCode PetscDTPTrimmedEvalJet_Internal(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt formDegree, PetscInt jetDegree, PetscReal p[]) 1205d8f25ad8SToby Isaac { 1206d8f25ad8SToby Isaac PetscInt formDegreeOrig = formDegree; 1207d8f25ad8SToby Isaac PetscBool formNegative = (formDegreeOrig < 0) ? PETSC_TRUE : PETSC_FALSE; 1208d8f25ad8SToby Isaac 1209d8f25ad8SToby Isaac PetscFunctionBegin; 1210d8f25ad8SToby Isaac formDegree = PetscAbsInt(formDegreeOrig); 1211d8f25ad8SToby Isaac if (formDegree == 0) { 12129566063dSJacob Faibussowitsch PetscCall(PetscDTPKDEvalJet(dim, npoints, points, degree, jetDegree, p)); 1213d8f25ad8SToby Isaac PetscFunctionReturn(0); 1214d8f25ad8SToby Isaac } 1215d8f25ad8SToby Isaac if (formDegree == dim) { 12169566063dSJacob Faibussowitsch PetscCall(PetscDTPKDEvalJet(dim, npoints, points, degree - 1, jetDegree, p)); 1217d8f25ad8SToby Isaac PetscFunctionReturn(0); 1218d8f25ad8SToby Isaac } 1219d8f25ad8SToby Isaac PetscInt Nbpt; 12209566063dSJacob Faibussowitsch PetscCall(PetscDTPTrimmedSize(dim, degree, formDegree, &Nbpt)); 1221d8f25ad8SToby Isaac PetscInt Nf; 12229566063dSJacob Faibussowitsch PetscCall(PetscDTBinomialInt(dim, formDegree, &Nf)); 1223d8f25ad8SToby Isaac PetscInt Nk; 12249566063dSJacob Faibussowitsch PetscCall(PetscDTBinomialInt(dim + jetDegree, dim, &Nk)); 12259566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(p, Nbpt * Nf * Nk * npoints)); 1226d8f25ad8SToby Isaac 1227d8f25ad8SToby Isaac PetscInt Nbpm1; // number of scalar polynomials up to degree - 1; 12289566063dSJacob Faibussowitsch PetscCall(PetscDTBinomialInt(dim + degree - 1, dim, &Nbpm1)); 1229d8f25ad8SToby Isaac PetscReal *p_scalar; 12309566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Nbpm1 * Nk * npoints, &p_scalar)); 12319566063dSJacob Faibussowitsch PetscCall(PetscDTPKDEvalJet(dim, npoints, points, degree - 1, jetDegree, p_scalar)); 1232d8f25ad8SToby Isaac PetscInt total = 0; 1233d8f25ad8SToby Isaac // First add the full polynomials up to degree - 1 into the basis: take the scalar 1234d8f25ad8SToby Isaac // and copy one for each form component 1235d8f25ad8SToby Isaac for (PetscInt i = 0; i < Nbpm1; i++) { 1236d8f25ad8SToby Isaac const PetscReal *src = &p_scalar[i * Nk * npoints]; 1237d8f25ad8SToby Isaac for (PetscInt f = 0; f < Nf; f++) { 1238d8f25ad8SToby Isaac PetscReal *dest = &p[(total++ * Nf + f) * Nk * npoints]; 12399566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(dest, src, Nk * npoints)); 1240d8f25ad8SToby Isaac } 1241d8f25ad8SToby Isaac } 1242d8f25ad8SToby Isaac PetscInt *form_atoms; 12439566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(formDegree + 1, &form_atoms)); 1244d8f25ad8SToby Isaac // construct the interior product pattern 1245d8f25ad8SToby Isaac PetscInt (*pattern)[3]; 1246d8f25ad8SToby Isaac PetscInt Nf1; // number of formDegree + 1 forms 12479566063dSJacob Faibussowitsch PetscCall(PetscDTBinomialInt(dim, formDegree + 1, &Nf1)); 1248d8f25ad8SToby Isaac PetscInt nnz = Nf1 * (formDegree+1); 12499566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Nf1 * (formDegree+1), &pattern)); 12509566063dSJacob Faibussowitsch PetscCall(PetscDTAltVInteriorPattern(dim, formDegree+1, pattern)); 1251d8f25ad8SToby Isaac PetscReal centroid = (1. - dim) / (dim + 1.); 1252d8f25ad8SToby Isaac PetscInt *deriv; 12539566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(dim, &deriv)); 1254d8f25ad8SToby Isaac for (PetscInt d = dim; d >= formDegree + 1; d--) { 1255d8f25ad8SToby Isaac PetscInt Nfd1; // number of formDegree + 1 forms in dimension d that include dx_0 1256d8f25ad8SToby Isaac // (equal to the number of formDegree forms in dimension d-1) 12579566063dSJacob Faibussowitsch PetscCall(PetscDTBinomialInt(d - 1, formDegree, &Nfd1)); 1258d8f25ad8SToby Isaac // The number of homogeneous (degree-1) scalar polynomials in d variables 1259d8f25ad8SToby Isaac PetscInt Nh; 12609566063dSJacob Faibussowitsch PetscCall(PetscDTBinomialInt(d - 1 + degree - 1, d - 1, &Nh)); 1261d8f25ad8SToby Isaac const PetscReal *h_scalar = &p_scalar[(Nbpm1 - Nh) * Nk * npoints]; 1262d8f25ad8SToby Isaac for (PetscInt b = 0; b < Nh; b++) { 1263d8f25ad8SToby Isaac const PetscReal *h_s = &h_scalar[b * Nk * npoints]; 1264d8f25ad8SToby Isaac for (PetscInt f = 0; f < Nfd1; f++) { 1265d8f25ad8SToby Isaac // construct all formDegree+1 forms that start with dx_(dim - d) /\ ... 1266d8f25ad8SToby Isaac form_atoms[0] = dim - d; 12679566063dSJacob Faibussowitsch PetscCall(PetscDTEnumSubset(d-1, formDegree, f, &form_atoms[1])); 1268d8f25ad8SToby Isaac for (PetscInt i = 0; i < formDegree; i++) { 1269d8f25ad8SToby Isaac form_atoms[1+i] += form_atoms[0] + 1; 1270d8f25ad8SToby Isaac } 1271d8f25ad8SToby Isaac PetscInt f_ind; // index of the resulting form 12729566063dSJacob Faibussowitsch PetscCall(PetscDTSubsetIndex(dim, formDegree + 1, form_atoms, &f_ind)); 1273d8f25ad8SToby Isaac PetscReal *p_f = &p[total++ * Nf * Nk * npoints]; 1274d8f25ad8SToby Isaac for (PetscInt nz = 0; nz < nnz; nz++) { 1275d8f25ad8SToby Isaac PetscInt i = pattern[nz][0]; // formDegree component 1276d8f25ad8SToby Isaac PetscInt j = pattern[nz][1]; // (formDegree + 1) component 1277d8f25ad8SToby Isaac PetscInt v = pattern[nz][2]; // coordinate component 1278d8f25ad8SToby Isaac PetscReal scale = v < 0 ? -1. : 1.; 1279d8f25ad8SToby Isaac 1280d8f25ad8SToby Isaac i = formNegative ? (Nf - 1 - i) : i; 1281d8f25ad8SToby Isaac scale = (formNegative && (i & 1)) ? -scale : scale; 1282d8f25ad8SToby Isaac v = v < 0 ? -(v + 1) : v; 1283d8f25ad8SToby Isaac if (j != f_ind) { 1284d8f25ad8SToby Isaac continue; 1285d8f25ad8SToby Isaac } 1286d8f25ad8SToby Isaac PetscReal *p_i = &p_f[i * Nk * npoints]; 1287d8f25ad8SToby Isaac for (PetscInt jet = 0; jet < Nk; jet++) { 1288d8f25ad8SToby Isaac const PetscReal *h_jet = &h_s[jet * npoints]; 1289d8f25ad8SToby Isaac PetscReal *p_jet = &p_i[jet * npoints]; 1290d8f25ad8SToby Isaac 1291d8f25ad8SToby Isaac for (PetscInt pt = 0; pt < npoints; pt++) { 1292d8f25ad8SToby Isaac p_jet[pt] += scale * h_jet[pt] * (points[pt * dim + v] - centroid); 1293d8f25ad8SToby Isaac } 12949566063dSJacob Faibussowitsch PetscCall(PetscDTIndexToGradedOrder(dim, jet, deriv)); 1295d8f25ad8SToby Isaac deriv[v]++; 1296d8f25ad8SToby Isaac PetscReal mult = deriv[v]; 1297d8f25ad8SToby Isaac PetscInt l; 12989566063dSJacob Faibussowitsch PetscCall(PetscDTGradedOrderToIndex(dim, deriv, &l)); 1299d8f25ad8SToby Isaac if (l >= Nk) { 1300d8f25ad8SToby Isaac continue; 1301d8f25ad8SToby Isaac } 1302d8f25ad8SToby Isaac p_jet = &p_i[l * npoints]; 1303d8f25ad8SToby Isaac for (PetscInt pt = 0; pt < npoints; pt++) { 1304d8f25ad8SToby Isaac p_jet[pt] += scale * mult * h_jet[pt]; 1305d8f25ad8SToby Isaac } 1306d8f25ad8SToby Isaac deriv[v]--; 1307d8f25ad8SToby Isaac } 1308d8f25ad8SToby Isaac } 1309d8f25ad8SToby Isaac } 1310d8f25ad8SToby Isaac } 1311d8f25ad8SToby Isaac } 131208401ef6SPierre Jolivet PetscCheck(total == Nbpt,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Incorrectly counted P trimmed polynomials"); 13139566063dSJacob Faibussowitsch PetscCall(PetscFree(deriv)); 13149566063dSJacob Faibussowitsch PetscCall(PetscFree(pattern)); 13159566063dSJacob Faibussowitsch PetscCall(PetscFree(form_atoms)); 13169566063dSJacob Faibussowitsch PetscCall(PetscFree(p_scalar)); 1317d8f25ad8SToby Isaac PetscFunctionReturn(0); 1318d8f25ad8SToby Isaac } 1319d8f25ad8SToby Isaac 1320d8f25ad8SToby Isaac /*@ 1321d8f25ad8SToby Isaac PetscDTPTrimmedEvalJet - Evaluate the jet (function and derivatives) of a basis of the trimmed polynomial k-forms up to 1322d8f25ad8SToby Isaac a given degree. 1323d8f25ad8SToby Isaac 1324d8f25ad8SToby Isaac Input Parameters: 1325d8f25ad8SToby Isaac + dim - the number of variables in the multivariate polynomials 1326d8f25ad8SToby Isaac . npoints - the number of points to evaluate the polynomials at 1327d8f25ad8SToby Isaac . points - [npoints x dim] array of point coordinates 1328d8f25ad8SToby Isaac . degree - the degree (sum of degrees on the variables in a monomial) of the trimmed polynomial space to evaluate. 1329d8f25ad8SToby Isaac There are ((dim + degree) choose (dim + formDegree)) x ((degree + formDegree - 1) choose (formDegree)) polynomials in this space. 1330d8f25ad8SToby Isaac (You can use PetscDTPTrimmedSize() to compute this size.) 1331d8f25ad8SToby Isaac . formDegree - the degree of the form 1332d8f25ad8SToby Isaac - jetDegree - the maximum order partial derivative to evaluate in the jet. There are ((dim + jetDegree) choose dim) partial derivatives 1333d8f25ad8SToby Isaac in the jet. Choosing jetDegree = 0 means to evaluate just the function and no derivatives 1334d8f25ad8SToby Isaac 13356aad120cSJose E. Roman Output Parameters: 1336d8f25ad8SToby Isaac - p - an array containing the evaluations of the PKD polynomials' jets on the points. The size is 1337d8f25ad8SToby Isaac PetscDTPTrimmedSize() x ((dim + formDegree) choose dim) x ((dim + k) choose dim) x npoints, 1338d8f25ad8SToby Isaac which also describes the order of the dimensions of this 1339d8f25ad8SToby Isaac four-dimensional array: 1340d8f25ad8SToby Isaac the first (slowest varying) dimension is basis function index; 1341d8f25ad8SToby Isaac the second dimension is component of the form; 1342d8f25ad8SToby Isaac the third dimension is jet index; 1343d8f25ad8SToby Isaac the fourth (fastest varying) dimension is the index of the evaluation point. 1344d8f25ad8SToby Isaac 1345d8f25ad8SToby Isaac Level: advanced 1346d8f25ad8SToby Isaac 1347d8f25ad8SToby Isaac Note: The ordering of the basis functions is not graded, so the basis functions are not nested by degree like PetscDTPKDEvalJet(). 1348d8f25ad8SToby Isaac The basis functions are not an L2-orthonormal basis on any particular domain. 1349d8f25ad8SToby Isaac 1350d8f25ad8SToby Isaac The implementation is based on the description of the trimmed polynomials up to degree r as 1351d8f25ad8SToby Isaac the direct sum of polynomials up to degree (r-1) and the Koszul differential applied to 1352d8f25ad8SToby Isaac homogeneous polynomials of degree (r-1). 1353d8f25ad8SToby Isaac 1354db781477SPatrick Sanan .seealso: `PetscDTPKDEvalJet()`, `PetscDTPTrimmedSize()` 1355d8f25ad8SToby Isaac @*/ 1356d8f25ad8SToby Isaac PetscErrorCode PetscDTPTrimmedEvalJet(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt formDegree, PetscInt jetDegree, PetscReal p[]) 1357d8f25ad8SToby Isaac { 1358d8f25ad8SToby Isaac PetscFunctionBegin; 13599566063dSJacob Faibussowitsch PetscCall(PetscDTPTrimmedEvalJet_Internal(dim, npoints, points, degree, formDegree, jetDegree, p)); 1360d8f25ad8SToby Isaac PetscFunctionReturn(0); 1361d8f25ad8SToby Isaac } 1362d8f25ad8SToby Isaac 1363e6a796c3SToby Isaac /* solve the symmetric tridiagonal eigenvalue system, writing the eigenvalues into eigs and the eigenvectors into V 1364e6a796c3SToby Isaac * with lds n; diag and subdiag are overwritten */ 1365e6a796c3SToby Isaac static PetscErrorCode PetscDTSymmetricTridiagonalEigensolve(PetscInt n, PetscReal diag[], PetscReal subdiag[], 1366e6a796c3SToby Isaac PetscReal eigs[], PetscScalar V[]) 1367e6a796c3SToby Isaac { 1368e6a796c3SToby Isaac char jobz = 'V'; /* eigenvalues and eigenvectors */ 1369e6a796c3SToby Isaac char range = 'A'; /* all eigenvalues will be found */ 1370e6a796c3SToby Isaac PetscReal VL = 0.; /* ignored because range is 'A' */ 1371e6a796c3SToby Isaac PetscReal VU = 0.; /* ignored because range is 'A' */ 1372e6a796c3SToby Isaac PetscBLASInt IL = 0; /* ignored because range is 'A' */ 1373e6a796c3SToby Isaac PetscBLASInt IU = 0; /* ignored because range is 'A' */ 1374e6a796c3SToby Isaac PetscReal abstol = 0.; /* unused */ 1375e6a796c3SToby Isaac PetscBLASInt bn, bm, ldz; /* bm will equal bn on exit */ 1376e6a796c3SToby Isaac PetscBLASInt *isuppz; 1377e6a796c3SToby Isaac PetscBLASInt lwork, liwork; 1378e6a796c3SToby Isaac PetscReal workquery; 1379e6a796c3SToby Isaac PetscBLASInt iworkquery; 1380e6a796c3SToby Isaac PetscBLASInt *iwork; 1381e6a796c3SToby Isaac PetscBLASInt info; 1382e6a796c3SToby Isaac PetscReal *work = NULL; 1383e6a796c3SToby Isaac 1384e6a796c3SToby Isaac PetscFunctionBegin; 1385e6a796c3SToby Isaac #if !defined(PETSCDTGAUSSIANQUADRATURE_EIG) 1386e6a796c3SToby Isaac SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "A LAPACK symmetric tridiagonal eigensolver could not be found"); 1387e6a796c3SToby Isaac #endif 13889566063dSJacob Faibussowitsch PetscCall(PetscBLASIntCast(n, &bn)); 13899566063dSJacob Faibussowitsch PetscCall(PetscBLASIntCast(n, &ldz)); 1390e6a796c3SToby Isaac #if !defined(PETSC_MISSING_LAPACK_STEGR) 13919566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(2 * n, &isuppz)); 1392e6a796c3SToby Isaac lwork = -1; 1393e6a796c3SToby Isaac liwork = -1; 1394792fecdfSBarry Smith PetscCallBLAS("LAPACKstegr",LAPACKstegr_(&jobz,&range,&bn,diag,subdiag,&VL,&VU,&IL,&IU,&abstol,&bm,eigs,V,&ldz,isuppz,&workquery,&lwork,&iworkquery,&liwork,&info)); 139528b400f6SJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEGR error"); 1396e6a796c3SToby Isaac lwork = (PetscBLASInt) workquery; 1397e6a796c3SToby Isaac liwork = (PetscBLASInt) iworkquery; 13989566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(lwork, &work, liwork, &iwork)); 13999566063dSJacob Faibussowitsch PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF)); 1400792fecdfSBarry Smith PetscCallBLAS("LAPACKstegr",LAPACKstegr_(&jobz,&range,&bn,diag,subdiag,&VL,&VU,&IL,&IU,&abstol,&bm,eigs,V,&ldz,isuppz,work,&lwork,iwork,&liwork,&info)); 14019566063dSJacob Faibussowitsch PetscCall(PetscFPTrapPop()); 140228b400f6SJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEGR error"); 14039566063dSJacob Faibussowitsch PetscCall(PetscFree2(work, iwork)); 14049566063dSJacob Faibussowitsch PetscCall(PetscFree(isuppz)); 1405e6a796c3SToby Isaac #elif !defined(PETSC_MISSING_LAPACK_STEQR) 1406e6a796c3SToby Isaac jobz = 'I'; /* Compute eigenvalues and eigenvectors of the 1407e6a796c3SToby Isaac tridiagonal matrix. Z is initialized to the identity 1408e6a796c3SToby Isaac matrix. */ 14099566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(PetscMax(1,2*n-2),&work)); 1410792fecdfSBarry Smith PetscCallBLAS("LAPACKsteqr",LAPACKsteqr_("I",&bn,diag,subdiag,V,&ldz,work,&info)); 14119566063dSJacob Faibussowitsch PetscCall(PetscFPTrapPop()); 141228b400f6SJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEQR error"); 14139566063dSJacob Faibussowitsch PetscCall(PetscFree(work)); 14149566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(eigs,diag,n)); 1415e6a796c3SToby Isaac #endif 1416e6a796c3SToby Isaac PetscFunctionReturn(0); 1417e6a796c3SToby Isaac } 1418e6a796c3SToby Isaac 1419e6a796c3SToby Isaac /* Formula for the weights at the endpoints (-1 and 1) of Gauss-Lobatto-Jacobi 1420e6a796c3SToby Isaac * quadrature rules on the interval [-1, 1] */ 1421e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussLobattoJacobiEndweights_Internal(PetscInt n, PetscReal alpha, PetscReal beta, PetscReal *leftw, PetscReal *rightw) 1422e6a796c3SToby Isaac { 1423e6a796c3SToby Isaac PetscReal twoab1; 1424e6a796c3SToby Isaac PetscInt m = n - 2; 1425e6a796c3SToby Isaac PetscReal a = alpha + 1.; 1426e6a796c3SToby Isaac PetscReal b = beta + 1.; 1427e6a796c3SToby Isaac PetscReal gra, grb; 1428e6a796c3SToby Isaac 1429e6a796c3SToby Isaac PetscFunctionBegin; 1430e6a796c3SToby Isaac twoab1 = PetscPowReal(2., a + b - 1.); 1431e6a796c3SToby Isaac #if defined(PETSC_HAVE_LGAMMA) 1432e6a796c3SToby Isaac grb = PetscExpReal(2. * PetscLGamma(b+1.) + PetscLGamma(m+1.) + PetscLGamma(m+a+1.) - 1433e6a796c3SToby Isaac (PetscLGamma(m+b+1) + PetscLGamma(m+a+b+1.))); 1434e6a796c3SToby Isaac gra = PetscExpReal(2. * PetscLGamma(a+1.) + PetscLGamma(m+1.) + PetscLGamma(m+b+1.) - 1435e6a796c3SToby Isaac (PetscLGamma(m+a+1) + PetscLGamma(m+a+b+1.))); 1436e6a796c3SToby Isaac #else 1437e6a796c3SToby Isaac { 1438e6a796c3SToby Isaac PetscInt alphai = (PetscInt) alpha; 1439e6a796c3SToby Isaac PetscInt betai = (PetscInt) beta; 1440e6a796c3SToby Isaac 1441e6a796c3SToby Isaac if ((PetscReal) alphai == alpha && (PetscReal) betai == beta) { 1442e6a796c3SToby Isaac PetscReal binom1, binom2; 1443e6a796c3SToby Isaac 14449566063dSJacob Faibussowitsch PetscCall(PetscDTBinomial(m+b, b, &binom1)); 14459566063dSJacob Faibussowitsch PetscCall(PetscDTBinomial(m+a+b, b, &binom2)); 1446e6a796c3SToby Isaac grb = 1./ (binom1 * binom2); 14479566063dSJacob Faibussowitsch PetscCall(PetscDTBinomial(m+a, a, &binom1)); 14489566063dSJacob Faibussowitsch PetscCall(PetscDTBinomial(m+a+b, a, &binom2)); 1449e6a796c3SToby Isaac gra = 1./ (binom1 * binom2); 1450e6a796c3SToby Isaac } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"lgamma() - math routine is unavailable."); 1451e6a796c3SToby Isaac } 1452e6a796c3SToby Isaac #endif 1453e6a796c3SToby Isaac *leftw = twoab1 * grb / b; 1454e6a796c3SToby Isaac *rightw = twoab1 * gra / a; 1455e6a796c3SToby Isaac PetscFunctionReturn(0); 1456e6a796c3SToby Isaac } 1457e6a796c3SToby Isaac 1458e6a796c3SToby Isaac /* Evaluates the nth jacobi polynomial with weight parameters a,b at a point x. 1459e6a796c3SToby Isaac Recurrence relations implemented from the pseudocode given in Karniadakis and Sherwin, Appendix B */ 14609fbee547SJacob Faibussowitsch static inline PetscErrorCode PetscDTComputeJacobi(PetscReal a, PetscReal b, PetscInt n, PetscReal x, PetscReal *P) 1461e6a796c3SToby Isaac { 146294e21283SToby Isaac PetscReal pn1, pn2; 146394e21283SToby Isaac PetscReal cnm1, cnm1x, cnm2; 1464e6a796c3SToby Isaac PetscInt k; 1465e6a796c3SToby Isaac 1466e6a796c3SToby Isaac PetscFunctionBegin; 1467e6a796c3SToby Isaac if (!n) {*P = 1.0; PetscFunctionReturn(0);} 146894e21283SToby Isaac PetscDTJacobiRecurrence_Internal(1,a,b,cnm1,cnm1x,cnm2); 146994e21283SToby Isaac pn2 = 1.; 147094e21283SToby Isaac pn1 = cnm1 + cnm1x*x; 147194e21283SToby Isaac if (n == 1) {*P = pn1; PetscFunctionReturn(0);} 1472e6a796c3SToby Isaac *P = 0.0; 1473e6a796c3SToby Isaac for (k = 2; k < n+1; ++k) { 147494e21283SToby Isaac PetscDTJacobiRecurrence_Internal(k,a,b,cnm1,cnm1x,cnm2); 1475e6a796c3SToby Isaac 147694e21283SToby Isaac *P = (cnm1 + cnm1x*x)*pn1 - cnm2*pn2; 1477e6a796c3SToby Isaac pn2 = pn1; 1478e6a796c3SToby Isaac pn1 = *P; 1479e6a796c3SToby Isaac } 1480e6a796c3SToby Isaac PetscFunctionReturn(0); 1481e6a796c3SToby Isaac } 1482e6a796c3SToby Isaac 1483e6a796c3SToby Isaac /* Evaluates the first derivative of P_{n}^{a,b} at a point x. */ 14849fbee547SJacob Faibussowitsch static inline PetscErrorCode PetscDTComputeJacobiDerivative(PetscReal a, PetscReal b, PetscInt n, PetscReal x, PetscInt k, PetscReal *P) 1485e6a796c3SToby Isaac { 1486e6a796c3SToby Isaac PetscReal nP; 1487e6a796c3SToby Isaac PetscInt i; 1488e6a796c3SToby Isaac 1489e6a796c3SToby Isaac PetscFunctionBegin; 149017a42bb7SSatish Balay *P = 0.0; 149117a42bb7SSatish Balay if (k > n) PetscFunctionReturn(0); 14929566063dSJacob Faibussowitsch PetscCall(PetscDTComputeJacobi(a+k, b+k, n-k, x, &nP)); 1493e6a796c3SToby Isaac for (i = 0; i < k; i++) nP *= (a + b + n + 1. + i) * 0.5; 1494e6a796c3SToby Isaac *P = nP; 1495e6a796c3SToby Isaac PetscFunctionReturn(0); 1496e6a796c3SToby Isaac } 1497e6a796c3SToby Isaac 1498e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussJacobiQuadrature_Newton_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscReal x[], PetscReal w[]) 1499e6a796c3SToby Isaac { 1500e6a796c3SToby Isaac PetscInt maxIter = 100; 150194e21283SToby Isaac PetscReal eps = PetscExpReal(0.75 * PetscLogReal(PETSC_MACHINE_EPSILON)); 1502200b5abcSJed Brown PetscReal a1, a6, gf; 1503e6a796c3SToby Isaac PetscInt k; 1504e6a796c3SToby Isaac 1505e6a796c3SToby Isaac PetscFunctionBegin; 1506e6a796c3SToby Isaac 1507e6a796c3SToby Isaac a1 = PetscPowReal(2.0, a+b+1); 150894e21283SToby Isaac #if defined(PETSC_HAVE_LGAMMA) 1509200b5abcSJed Brown { 1510200b5abcSJed Brown PetscReal a2, a3, a4, a5; 151194e21283SToby Isaac a2 = PetscLGamma(a + npoints + 1); 151294e21283SToby Isaac a3 = PetscLGamma(b + npoints + 1); 151394e21283SToby Isaac a4 = PetscLGamma(a + b + npoints + 1); 151494e21283SToby Isaac a5 = PetscLGamma(npoints + 1); 151594e21283SToby Isaac gf = PetscExpReal(a2 + a3 - (a4 + a5)); 1516200b5abcSJed Brown } 1517e6a796c3SToby Isaac #else 1518e6a796c3SToby Isaac { 1519e6a796c3SToby Isaac PetscInt ia, ib; 1520e6a796c3SToby Isaac 1521e6a796c3SToby Isaac ia = (PetscInt) a; 1522e6a796c3SToby Isaac ib = (PetscInt) b; 152394e21283SToby Isaac gf = 1.; 152494e21283SToby Isaac if (ia == a && ia >= 0) { /* compute ratio of rising factorals wrt a */ 152594e21283SToby Isaac for (k = 0; k < ia; k++) gf *= (npoints + 1. + k) / (npoints + b + 1. + k); 152694e21283SToby Isaac } else if (b == b && ib >= 0) { /* compute ratio of rising factorials wrt b */ 152794e21283SToby Isaac for (k = 0; k < ib; k++) gf *= (npoints + 1. + k) / (npoints + a + 1. + k); 152894e21283SToby Isaac } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"lgamma() - math routine is unavailable."); 1529e6a796c3SToby Isaac } 1530e6a796c3SToby Isaac #endif 1531e6a796c3SToby Isaac 153294e21283SToby Isaac a6 = a1 * gf; 1533e6a796c3SToby Isaac /* Computes the m roots of P_{m}^{a,b} on [-1,1] by Newton's method with Chebyshev points as initial guesses. 1534e6a796c3SToby Isaac Algorithm implemented from the pseudocode given by Karniadakis and Sherwin and Python in FIAT */ 1535e6a796c3SToby Isaac for (k = 0; k < npoints; ++k) { 153694e21283SToby Isaac PetscReal r = PetscCosReal(PETSC_PI * (1. - (4.*k + 3. + 2.*b) / (4.*npoints + 2.*(a + b + 1.)))), dP; 1537e6a796c3SToby Isaac PetscInt j; 1538e6a796c3SToby Isaac 1539e6a796c3SToby Isaac if (k > 0) r = 0.5 * (r + x[k-1]); 1540e6a796c3SToby Isaac for (j = 0; j < maxIter; ++j) { 1541e6a796c3SToby Isaac PetscReal s = 0.0, delta, f, fp; 1542e6a796c3SToby Isaac PetscInt i; 1543e6a796c3SToby Isaac 1544e6a796c3SToby Isaac for (i = 0; i < k; ++i) s = s + 1.0 / (r - x[i]); 15459566063dSJacob Faibussowitsch PetscCall(PetscDTComputeJacobi(a, b, npoints, r, &f)); 15469566063dSJacob Faibussowitsch PetscCall(PetscDTComputeJacobiDerivative(a, b, npoints, r, 1, &fp)); 1547e6a796c3SToby Isaac delta = f / (fp - f * s); 1548e6a796c3SToby Isaac r = r - delta; 1549e6a796c3SToby Isaac if (PetscAbsReal(delta) < eps) break; 1550e6a796c3SToby Isaac } 1551e6a796c3SToby Isaac x[k] = r; 15529566063dSJacob Faibussowitsch PetscCall(PetscDTComputeJacobiDerivative(a, b, npoints, x[k], 1, &dP)); 1553e6a796c3SToby Isaac w[k] = a6 / (1.0 - PetscSqr(x[k])) / PetscSqr(dP); 1554e6a796c3SToby Isaac } 1555e6a796c3SToby Isaac PetscFunctionReturn(0); 1556e6a796c3SToby Isaac } 1557e6a796c3SToby Isaac 155894e21283SToby Isaac /* Compute the diagonals of the Jacobi matrix used in Golub & Welsch algorithms for Gauss-Jacobi 1559e6a796c3SToby Isaac * quadrature weight calculations on [-1,1] for exponents (1. + x)^a (1.-x)^b */ 1560e6a796c3SToby Isaac static PetscErrorCode PetscDTJacobiMatrix_Internal(PetscInt nPoints, PetscReal a, PetscReal b, PetscReal *d, PetscReal *s) 1561e6a796c3SToby Isaac { 1562e6a796c3SToby Isaac PetscInt i; 1563e6a796c3SToby Isaac 1564e6a796c3SToby Isaac PetscFunctionBegin; 1565e6a796c3SToby Isaac for (i = 0; i < nPoints; i++) { 156694e21283SToby Isaac PetscReal A, B, C; 1567e6a796c3SToby Isaac 156894e21283SToby Isaac PetscDTJacobiRecurrence_Internal(i+1,a,b,A,B,C); 156994e21283SToby Isaac d[i] = -A / B; 157094e21283SToby Isaac if (i) s[i-1] *= C / B; 157194e21283SToby Isaac if (i < nPoints - 1) s[i] = 1. / B; 1572e6a796c3SToby Isaac } 1573e6a796c3SToby Isaac PetscFunctionReturn(0); 1574e6a796c3SToby Isaac } 1575e6a796c3SToby Isaac 1576e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussJacobiQuadrature_GolubWelsch_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscReal *x, PetscReal *w) 1577e6a796c3SToby Isaac { 1578e6a796c3SToby Isaac PetscReal mu0; 1579e6a796c3SToby Isaac PetscReal ga, gb, gab; 1580e6a796c3SToby Isaac PetscInt i; 1581e6a796c3SToby Isaac 1582e6a796c3SToby Isaac PetscFunctionBegin; 15839566063dSJacob Faibussowitsch PetscCall(PetscCitationsRegister(GolubWelschCitation, &GolubWelschCite)); 1584e6a796c3SToby Isaac 1585e6a796c3SToby Isaac #if defined(PETSC_HAVE_TGAMMA) 1586e6a796c3SToby Isaac ga = PetscTGamma(a + 1); 1587e6a796c3SToby Isaac gb = PetscTGamma(b + 1); 1588e6a796c3SToby Isaac gab = PetscTGamma(a + b + 2); 1589e6a796c3SToby Isaac #else 1590e6a796c3SToby Isaac { 1591e6a796c3SToby Isaac PetscInt ia, ib; 1592e6a796c3SToby Isaac 1593e6a796c3SToby Isaac ia = (PetscInt) a; 1594e6a796c3SToby Isaac ib = (PetscInt) b; 1595e6a796c3SToby Isaac if (ia == a && ib == b && ia + 1 > 0 && ib + 1 > 0 && ia + ib + 2 > 0) { /* All gamma(x) terms are (x-1)! terms */ 15969566063dSJacob Faibussowitsch PetscCall(PetscDTFactorial(ia, &ga)); 15979566063dSJacob Faibussowitsch PetscCall(PetscDTFactorial(ib, &gb)); 15989566063dSJacob Faibussowitsch PetscCall(PetscDTFactorial(ia + ib + 1, &gb)); 1599e6a796c3SToby Isaac } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"tgamma() - math routine is unavailable."); 1600e6a796c3SToby Isaac } 1601e6a796c3SToby Isaac #endif 1602e6a796c3SToby Isaac mu0 = PetscPowReal(2.,a + b + 1.) * ga * gb / gab; 1603e6a796c3SToby Isaac 1604e6a796c3SToby Isaac #if defined(PETSCDTGAUSSIANQUADRATURE_EIG) 1605e6a796c3SToby Isaac { 1606e6a796c3SToby Isaac PetscReal *diag, *subdiag; 1607e6a796c3SToby Isaac PetscScalar *V; 1608e6a796c3SToby Isaac 16099566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(npoints, &diag, npoints, &subdiag)); 16109566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(npoints*npoints, &V)); 16119566063dSJacob Faibussowitsch PetscCall(PetscDTJacobiMatrix_Internal(npoints, a, b, diag, subdiag)); 1612e6a796c3SToby Isaac for (i = 0; i < npoints - 1; i++) subdiag[i] = PetscSqrtReal(subdiag[i]); 16139566063dSJacob Faibussowitsch PetscCall(PetscDTSymmetricTridiagonalEigensolve(npoints, diag, subdiag, x, V)); 161494e21283SToby Isaac for (i = 0; i < npoints; i++) w[i] = PetscSqr(PetscRealPart(V[i * npoints])) * mu0; 16159566063dSJacob Faibussowitsch PetscCall(PetscFree(V)); 16169566063dSJacob Faibussowitsch PetscCall(PetscFree2(diag, subdiag)); 1617e6a796c3SToby Isaac } 1618e6a796c3SToby Isaac #else 1619e6a796c3SToby Isaac SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "A LAPACK symmetric tridiagonal eigensolver could not be found"); 1620e6a796c3SToby Isaac #endif 162194e21283SToby Isaac { /* As of March 2, 2020, The Sun Performance Library breaks the LAPACK contract for xstegr and xsteqr: the 162294e21283SToby Isaac eigenvalues are not guaranteed to be in ascending order. So we heave a passive aggressive sigh and check that 162394e21283SToby Isaac the eigenvalues are sorted */ 162494e21283SToby Isaac PetscBool sorted; 162594e21283SToby Isaac 16269566063dSJacob Faibussowitsch PetscCall(PetscSortedReal(npoints, x, &sorted)); 162794e21283SToby Isaac if (!sorted) { 162894e21283SToby Isaac PetscInt *order, i; 162994e21283SToby Isaac PetscReal *tmp; 163094e21283SToby Isaac 16319566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(npoints, &order, npoints, &tmp)); 163294e21283SToby Isaac for (i = 0; i < npoints; i++) order[i] = i; 16339566063dSJacob Faibussowitsch PetscCall(PetscSortRealWithPermutation(npoints, x, order)); 16349566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(tmp, x, npoints)); 163594e21283SToby Isaac for (i = 0; i < npoints; i++) x[i] = tmp[order[i]]; 16369566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(tmp, w, npoints)); 163794e21283SToby Isaac for (i = 0; i < npoints; i++) w[i] = tmp[order[i]]; 16389566063dSJacob Faibussowitsch PetscCall(PetscFree2(order, tmp)); 163994e21283SToby Isaac } 164094e21283SToby Isaac } 1641e6a796c3SToby Isaac PetscFunctionReturn(0); 1642e6a796c3SToby Isaac } 1643e6a796c3SToby Isaac 1644e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussJacobiQuadrature_Internal(PetscInt npoints,PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[], PetscBool newton) 1645e6a796c3SToby Isaac { 1646e6a796c3SToby Isaac PetscFunctionBegin; 164708401ef6SPierre Jolivet PetscCheck(npoints >= 1,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of points must be positive"); 1648e6a796c3SToby Isaac /* If asking for a 1D Lobatto point, just return the non-Lobatto 1D point */ 164908401ef6SPierre Jolivet PetscCheck(alpha > -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"alpha must be > -1."); 165008401ef6SPierre Jolivet PetscCheck(beta > -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"beta must be > -1."); 1651e6a796c3SToby Isaac 16521baa6e33SBarry Smith if (newton) PetscCall(PetscDTGaussJacobiQuadrature_Newton_Internal(npoints, alpha, beta, x, w)); 16531baa6e33SBarry Smith else PetscCall(PetscDTGaussJacobiQuadrature_GolubWelsch_Internal(npoints, alpha, beta, x, w)); 1654e6a796c3SToby Isaac if (alpha == beta) { /* symmetrize */ 1655e6a796c3SToby Isaac PetscInt i; 1656e6a796c3SToby Isaac for (i = 0; i < (npoints + 1) / 2; i++) { 1657e6a796c3SToby Isaac PetscInt j = npoints - 1 - i; 1658e6a796c3SToby Isaac PetscReal xi = x[i]; 1659e6a796c3SToby Isaac PetscReal xj = x[j]; 1660e6a796c3SToby Isaac PetscReal wi = w[i]; 1661e6a796c3SToby Isaac PetscReal wj = w[j]; 1662e6a796c3SToby Isaac 1663e6a796c3SToby Isaac x[i] = (xi - xj) / 2.; 1664e6a796c3SToby Isaac x[j] = (xj - xi) / 2.; 1665e6a796c3SToby Isaac w[i] = w[j] = (wi + wj) / 2.; 1666e6a796c3SToby Isaac } 1667e6a796c3SToby Isaac } 1668e6a796c3SToby Isaac PetscFunctionReturn(0); 1669e6a796c3SToby Isaac } 1670e6a796c3SToby Isaac 167194e21283SToby Isaac /*@ 167294e21283SToby Isaac PetscDTGaussJacobiQuadrature - quadrature for the interval [a, b] with the weight function 167394e21283SToby Isaac $(x-a)^\alpha (x-b)^\beta$. 167494e21283SToby Isaac 167594e21283SToby Isaac Not collective 167694e21283SToby Isaac 167794e21283SToby Isaac Input Parameters: 167894e21283SToby Isaac + npoints - the number of points in the quadrature rule 167994e21283SToby Isaac . a - the left endpoint of the interval 168094e21283SToby Isaac . b - the right endpoint of the interval 168194e21283SToby Isaac . alpha - the left exponent 168294e21283SToby Isaac - beta - the right exponent 168394e21283SToby Isaac 168494e21283SToby Isaac Output Parameters: 168594e21283SToby Isaac + x - array of length npoints, the locations of the quadrature points 168694e21283SToby Isaac - w - array of length npoints, the weights of the quadrature points 168794e21283SToby Isaac 168894e21283SToby Isaac Level: intermediate 168994e21283SToby Isaac 169094e21283SToby Isaac Note: this quadrature rule is exact for polynomials up to degree 2*npoints - 1. 169194e21283SToby Isaac @*/ 169294e21283SToby Isaac PetscErrorCode PetscDTGaussJacobiQuadrature(PetscInt npoints,PetscReal a, PetscReal b, PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[]) 1693e6a796c3SToby Isaac { 169494e21283SToby Isaac PetscInt i; 1695e6a796c3SToby Isaac 1696e6a796c3SToby Isaac PetscFunctionBegin; 16979566063dSJacob Faibussowitsch PetscCall(PetscDTGaussJacobiQuadrature_Internal(npoints, alpha, beta, x, w, PetscDTGaussQuadratureNewton_Internal)); 169894e21283SToby Isaac if (a != -1. || b != 1.) { /* shift */ 169994e21283SToby Isaac for (i = 0; i < npoints; i++) { 170094e21283SToby Isaac x[i] = (x[i] + 1.) * ((b - a) / 2.) + a; 170194e21283SToby Isaac w[i] *= (b - a) / 2.; 170294e21283SToby Isaac } 170394e21283SToby Isaac } 1704e6a796c3SToby Isaac PetscFunctionReturn(0); 1705e6a796c3SToby Isaac } 1706e6a796c3SToby Isaac 1707e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussLobattoJacobiQuadrature_Internal(PetscInt npoints,PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[], PetscBool newton) 1708e6a796c3SToby Isaac { 1709e6a796c3SToby Isaac PetscInt i; 1710e6a796c3SToby Isaac 1711e6a796c3SToby Isaac PetscFunctionBegin; 171208401ef6SPierre Jolivet PetscCheck(npoints >= 2,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of points must be positive"); 1713e6a796c3SToby Isaac /* If asking for a 1D Lobatto point, just return the non-Lobatto 1D point */ 171408401ef6SPierre Jolivet PetscCheck(alpha > -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"alpha must be > -1."); 171508401ef6SPierre Jolivet PetscCheck(beta > -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"beta must be > -1."); 1716e6a796c3SToby Isaac 1717e6a796c3SToby Isaac x[0] = -1.; 1718e6a796c3SToby Isaac x[npoints-1] = 1.; 171994e21283SToby Isaac if (npoints > 2) { 17209566063dSJacob Faibussowitsch PetscCall(PetscDTGaussJacobiQuadrature_Internal(npoints-2, alpha+1., beta+1., &x[1], &w[1], newton)); 172194e21283SToby Isaac } 1722e6a796c3SToby Isaac for (i = 1; i < npoints - 1; i++) { 1723e6a796c3SToby Isaac w[i] /= (1. - x[i]*x[i]); 1724e6a796c3SToby Isaac } 17259566063dSJacob Faibussowitsch PetscCall(PetscDTGaussLobattoJacobiEndweights_Internal(npoints, alpha, beta, &w[0], &w[npoints-1])); 1726e6a796c3SToby Isaac PetscFunctionReturn(0); 1727e6a796c3SToby Isaac } 1728e6a796c3SToby Isaac 172937045ce4SJed Brown /*@ 173094e21283SToby Isaac PetscDTGaussLobattoJacobiQuadrature - quadrature for the interval [a, b] with the weight function 173194e21283SToby Isaac $(x-a)^\alpha (x-b)^\beta$, with endpoints a and b included as quadrature points. 173294e21283SToby Isaac 173394e21283SToby Isaac Not collective 173494e21283SToby Isaac 173594e21283SToby Isaac Input Parameters: 173694e21283SToby Isaac + npoints - the number of points in the quadrature rule 173794e21283SToby Isaac . a - the left endpoint of the interval 173894e21283SToby Isaac . b - the right endpoint of the interval 173994e21283SToby Isaac . alpha - the left exponent 174094e21283SToby Isaac - beta - the right exponent 174194e21283SToby Isaac 174294e21283SToby Isaac Output Parameters: 174394e21283SToby Isaac + x - array of length npoints, the locations of the quadrature points 174494e21283SToby Isaac - w - array of length npoints, the weights of the quadrature points 174594e21283SToby Isaac 174694e21283SToby Isaac Level: intermediate 174794e21283SToby Isaac 174894e21283SToby Isaac Note: this quadrature rule is exact for polynomials up to degree 2*npoints - 3. 174994e21283SToby Isaac @*/ 175094e21283SToby Isaac PetscErrorCode PetscDTGaussLobattoJacobiQuadrature(PetscInt npoints,PetscReal a, PetscReal b, PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[]) 175194e21283SToby Isaac { 175294e21283SToby Isaac PetscInt i; 175394e21283SToby Isaac 175494e21283SToby Isaac PetscFunctionBegin; 17559566063dSJacob Faibussowitsch PetscCall(PetscDTGaussLobattoJacobiQuadrature_Internal(npoints, alpha, beta, x, w, PetscDTGaussQuadratureNewton_Internal)); 175694e21283SToby Isaac if (a != -1. || b != 1.) { /* shift */ 175794e21283SToby Isaac for (i = 0; i < npoints; i++) { 175894e21283SToby Isaac x[i] = (x[i] + 1.) * ((b - a) / 2.) + a; 175994e21283SToby Isaac w[i] *= (b - a) / 2.; 176094e21283SToby Isaac } 176194e21283SToby Isaac } 176294e21283SToby Isaac PetscFunctionReturn(0); 176394e21283SToby Isaac } 176494e21283SToby Isaac 176594e21283SToby Isaac /*@ 1766e6a796c3SToby Isaac PetscDTGaussQuadrature - create Gauss-Legendre quadrature 176737045ce4SJed Brown 176837045ce4SJed Brown Not Collective 176937045ce4SJed Brown 17704165533cSJose E. Roman Input Parameters: 177137045ce4SJed Brown + npoints - number of points 177237045ce4SJed Brown . a - left end of interval (often-1) 177337045ce4SJed Brown - b - right end of interval (often +1) 177437045ce4SJed Brown 17754165533cSJose E. Roman Output Parameters: 177637045ce4SJed Brown + x - quadrature points 177737045ce4SJed Brown - w - quadrature weights 177837045ce4SJed Brown 177937045ce4SJed Brown Level: intermediate 178037045ce4SJed Brown 178137045ce4SJed Brown References: 1782606c0280SSatish Balay . * - Golub and Welsch, Calculation of Quadrature Rules, Math. Comp. 23(106), 1969. 178337045ce4SJed Brown 1784db781477SPatrick Sanan .seealso: `PetscDTLegendreEval()` 178537045ce4SJed Brown @*/ 178637045ce4SJed Brown PetscErrorCode PetscDTGaussQuadrature(PetscInt npoints,PetscReal a,PetscReal b,PetscReal *x,PetscReal *w) 178737045ce4SJed Brown { 178837045ce4SJed Brown PetscInt i; 178937045ce4SJed Brown 179037045ce4SJed Brown PetscFunctionBegin; 17919566063dSJacob Faibussowitsch PetscCall(PetscDTGaussJacobiQuadrature_Internal(npoints, 0., 0., x, w, PetscDTGaussQuadratureNewton_Internal)); 179294e21283SToby Isaac if (a != -1. || b != 1.) { /* shift */ 179337045ce4SJed Brown for (i = 0; i < npoints; i++) { 1794e6a796c3SToby Isaac x[i] = (x[i] + 1.) * ((b - a) / 2.) + a; 1795e6a796c3SToby Isaac w[i] *= (b - a) / 2.; 179637045ce4SJed Brown } 179737045ce4SJed Brown } 179837045ce4SJed Brown PetscFunctionReturn(0); 179937045ce4SJed Brown } 1800194825f6SJed Brown 18018272889dSSatish Balay /*@C 18028272889dSSatish Balay PetscDTGaussLobattoLegendreQuadrature - creates a set of the locations and weights of the Gauss-Lobatto-Legendre 18038272889dSSatish Balay nodes of a given size on the domain [-1,1] 18048272889dSSatish Balay 18058272889dSSatish Balay Not Collective 18068272889dSSatish Balay 1807d8d19677SJose E. Roman Input Parameters: 18088272889dSSatish Balay + n - number of grid nodes 1809f2e8fe4dShannah_mairs - type - PETSCGAUSSLOBATTOLEGENDRE_VIA_LINEAR_ALGEBRA or PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON 18108272889dSSatish Balay 18114165533cSJose E. Roman Output Parameters: 18128272889dSSatish Balay + x - quadrature points 18138272889dSSatish Balay - w - quadrature weights 18148272889dSSatish Balay 18158272889dSSatish Balay Notes: 18168272889dSSatish Balay For n > 30 the Newton approach computes duplicate (incorrect) values for some nodes because the initial guess is apparently not 18178272889dSSatish Balay close enough to the desired solution 18188272889dSSatish Balay 18198272889dSSatish Balay These are useful for implementing spectral methods based on Gauss-Lobatto-Legendre (GLL) nodes 18208272889dSSatish Balay 1821a8d69d7bSBarry 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 18228272889dSSatish Balay 18238272889dSSatish Balay Level: intermediate 18248272889dSSatish Balay 1825db781477SPatrick Sanan .seealso: `PetscDTGaussQuadrature()` 18268272889dSSatish Balay 18278272889dSSatish Balay @*/ 1828916e780bShannah_mairs PetscErrorCode PetscDTGaussLobattoLegendreQuadrature(PetscInt npoints,PetscGaussLobattoLegendreCreateType type,PetscReal *x,PetscReal *w) 18298272889dSSatish Balay { 1830e6a796c3SToby Isaac PetscBool newton; 18318272889dSSatish Balay 18328272889dSSatish Balay PetscFunctionBegin; 183308401ef6SPierre Jolivet PetscCheck(npoints >= 2,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must provide at least 2 grid points per element"); 183494e21283SToby Isaac newton = (PetscBool) (type == PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON); 18359566063dSJacob Faibussowitsch PetscCall(PetscDTGaussLobattoJacobiQuadrature_Internal(npoints, 0., 0., x, w, newton)); 18368272889dSSatish Balay PetscFunctionReturn(0); 18378272889dSSatish Balay } 18388272889dSSatish Balay 1839744bafbcSMatthew G. Knepley /*@ 1840744bafbcSMatthew G. Knepley PetscDTGaussTensorQuadrature - creates a tensor-product Gauss quadrature 1841744bafbcSMatthew G. Knepley 1842744bafbcSMatthew G. Knepley Not Collective 1843744bafbcSMatthew G. Knepley 18444165533cSJose E. Roman Input Parameters: 1845744bafbcSMatthew G. Knepley + dim - The spatial dimension 1846a6b92713SMatthew G. Knepley . Nc - The number of components 1847744bafbcSMatthew G. Knepley . npoints - number of points in one dimension 1848744bafbcSMatthew G. Knepley . a - left end of interval (often-1) 1849744bafbcSMatthew G. Knepley - b - right end of interval (often +1) 1850744bafbcSMatthew G. Knepley 18514165533cSJose E. Roman Output Parameter: 1852744bafbcSMatthew G. Knepley . q - A PetscQuadrature object 1853744bafbcSMatthew G. Knepley 1854744bafbcSMatthew G. Knepley Level: intermediate 1855744bafbcSMatthew G. Knepley 1856db781477SPatrick Sanan .seealso: `PetscDTGaussQuadrature()`, `PetscDTLegendreEval()` 1857744bafbcSMatthew G. Knepley @*/ 1858a6b92713SMatthew G. Knepley PetscErrorCode PetscDTGaussTensorQuadrature(PetscInt dim, PetscInt Nc, PetscInt npoints, PetscReal a, PetscReal b, PetscQuadrature *q) 1859744bafbcSMatthew G. Knepley { 1860a6b92713SMatthew G. Knepley PetscInt totpoints = dim > 1 ? dim > 2 ? npoints*PetscSqr(npoints) : PetscSqr(npoints) : npoints, i, j, k, c; 1861744bafbcSMatthew G. Knepley PetscReal *x, *w, *xw, *ww; 1862744bafbcSMatthew G. Knepley 1863744bafbcSMatthew G. Knepley PetscFunctionBegin; 18649566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(totpoints*dim,&x)); 18659566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(totpoints*Nc,&w)); 1866744bafbcSMatthew G. Knepley /* Set up the Golub-Welsch system */ 1867744bafbcSMatthew G. Knepley switch (dim) { 1868744bafbcSMatthew G. Knepley case 0: 18699566063dSJacob Faibussowitsch PetscCall(PetscFree(x)); 18709566063dSJacob Faibussowitsch PetscCall(PetscFree(w)); 18719566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(1, &x)); 18729566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Nc, &w)); 1873744bafbcSMatthew G. Knepley x[0] = 0.0; 1874a6b92713SMatthew G. Knepley for (c = 0; c < Nc; ++c) w[c] = 1.0; 1875744bafbcSMatthew G. Knepley break; 1876744bafbcSMatthew G. Knepley case 1: 18779566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(npoints,&ww)); 18789566063dSJacob Faibussowitsch PetscCall(PetscDTGaussQuadrature(npoints, a, b, x, ww)); 1879a6b92713SMatthew G. Knepley for (i = 0; i < npoints; ++i) for (c = 0; c < Nc; ++c) w[i*Nc+c] = ww[i]; 18809566063dSJacob Faibussowitsch PetscCall(PetscFree(ww)); 1881744bafbcSMatthew G. Knepley break; 1882744bafbcSMatthew G. Knepley case 2: 18839566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(npoints,&xw,npoints,&ww)); 18849566063dSJacob Faibussowitsch PetscCall(PetscDTGaussQuadrature(npoints, a, b, xw, ww)); 1885744bafbcSMatthew G. Knepley for (i = 0; i < npoints; ++i) { 1886744bafbcSMatthew G. Knepley for (j = 0; j < npoints; ++j) { 1887744bafbcSMatthew G. Knepley x[(i*npoints+j)*dim+0] = xw[i]; 1888744bafbcSMatthew G. Knepley x[(i*npoints+j)*dim+1] = xw[j]; 1889a6b92713SMatthew G. Knepley for (c = 0; c < Nc; ++c) w[(i*npoints+j)*Nc+c] = ww[i] * ww[j]; 1890744bafbcSMatthew G. Knepley } 1891744bafbcSMatthew G. Knepley } 18929566063dSJacob Faibussowitsch PetscCall(PetscFree2(xw,ww)); 1893744bafbcSMatthew G. Knepley break; 1894744bafbcSMatthew G. Knepley case 3: 18959566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(npoints,&xw,npoints,&ww)); 18969566063dSJacob Faibussowitsch PetscCall(PetscDTGaussQuadrature(npoints, a, b, xw, ww)); 1897744bafbcSMatthew G. Knepley for (i = 0; i < npoints; ++i) { 1898744bafbcSMatthew G. Knepley for (j = 0; j < npoints; ++j) { 1899744bafbcSMatthew G. Knepley for (k = 0; k < npoints; ++k) { 1900744bafbcSMatthew G. Knepley x[((i*npoints+j)*npoints+k)*dim+0] = xw[i]; 1901744bafbcSMatthew G. Knepley x[((i*npoints+j)*npoints+k)*dim+1] = xw[j]; 1902744bafbcSMatthew G. Knepley x[((i*npoints+j)*npoints+k)*dim+2] = xw[k]; 1903a6b92713SMatthew G. Knepley for (c = 0; c < Nc; ++c) w[((i*npoints+j)*npoints+k)*Nc+c] = ww[i] * ww[j] * ww[k]; 1904744bafbcSMatthew G. Knepley } 1905744bafbcSMatthew G. Knepley } 1906744bafbcSMatthew G. Knepley } 19079566063dSJacob Faibussowitsch PetscCall(PetscFree2(xw,ww)); 1908744bafbcSMatthew G. Knepley break; 1909744bafbcSMatthew G. Knepley default: 191063a3b9bcSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct quadrature rule for dimension %" PetscInt_FMT, dim); 1911744bafbcSMatthew G. Knepley } 19129566063dSJacob Faibussowitsch PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, q)); 19139566063dSJacob Faibussowitsch PetscCall(PetscQuadratureSetOrder(*q, 2*npoints-1)); 19149566063dSJacob Faibussowitsch PetscCall(PetscQuadratureSetData(*q, dim, Nc, totpoints, x, w)); 19159566063dSJacob Faibussowitsch PetscCall(PetscObjectChangeTypeName((PetscObject)*q,"GaussTensor")); 1916744bafbcSMatthew G. Knepley PetscFunctionReturn(0); 1917744bafbcSMatthew G. Knepley } 1918744bafbcSMatthew G. Knepley 1919f5f57ec0SBarry Smith /*@ 1920e6a796c3SToby Isaac PetscDTStroudConicalQuadrature - create Stroud conical quadrature for a simplex 1921494e7359SMatthew G. Knepley 1922494e7359SMatthew G. Knepley Not Collective 1923494e7359SMatthew G. Knepley 19244165533cSJose E. Roman Input Parameters: 1925494e7359SMatthew G. Knepley + dim - The simplex dimension 1926a6b92713SMatthew G. Knepley . Nc - The number of components 1927dcce0ee2SMatthew G. Knepley . npoints - The number of points in one dimension 1928494e7359SMatthew G. Knepley . a - left end of interval (often-1) 1929494e7359SMatthew G. Knepley - b - right end of interval (often +1) 1930494e7359SMatthew G. Knepley 19314165533cSJose E. Roman Output Parameter: 1932552aa4f7SMatthew G. Knepley . q - A PetscQuadrature object 1933494e7359SMatthew G. Knepley 1934494e7359SMatthew G. Knepley Level: intermediate 1935494e7359SMatthew G. Knepley 1936494e7359SMatthew G. Knepley References: 1937606c0280SSatish Balay . * - Karniadakis and Sherwin. FIAT 1938494e7359SMatthew G. Knepley 1939e6a796c3SToby Isaac Note: For dim == 1, this is Gauss-Legendre quadrature 1940e6a796c3SToby Isaac 1941db781477SPatrick Sanan .seealso: `PetscDTGaussTensorQuadrature()`, `PetscDTGaussQuadrature()` 1942494e7359SMatthew G. Knepley @*/ 1943e6a796c3SToby Isaac PetscErrorCode PetscDTStroudConicalQuadrature(PetscInt dim, PetscInt Nc, PetscInt npoints, PetscReal a, PetscReal b, PetscQuadrature *q) 1944494e7359SMatthew G. Knepley { 1945fbdc3dfeSToby Isaac PetscInt totprev, totrem; 1946fbdc3dfeSToby Isaac PetscInt totpoints; 1947fbdc3dfeSToby Isaac PetscReal *p1, *w1; 1948fbdc3dfeSToby Isaac PetscReal *x, *w; 1949fbdc3dfeSToby Isaac PetscInt i, j, k, l, m, pt, c; 1950494e7359SMatthew G. Knepley 1951494e7359SMatthew G. Knepley PetscFunctionBegin; 195208401ef6SPierre Jolivet PetscCheck(!(a != -1.0) && !(b != 1.0),PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must use default internal right now"); 1953fbdc3dfeSToby Isaac totpoints = 1; 1954fbdc3dfeSToby Isaac for (i = 0, totpoints = 1; i < dim; i++) totpoints *= npoints; 19559566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(totpoints*dim, &x)); 19569566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(totpoints*Nc, &w)); 19579566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(npoints, &p1, npoints, &w1)); 1958fbdc3dfeSToby Isaac for (i = 0; i < totpoints*Nc; i++) w[i] = 1.; 1959fbdc3dfeSToby Isaac for (i = 0, totprev = 1, totrem = totpoints / npoints; i < dim; i++) { 1960fbdc3dfeSToby Isaac PetscReal mul; 1961fbdc3dfeSToby Isaac 1962fbdc3dfeSToby Isaac mul = PetscPowReal(2.,-i); 19639566063dSJacob Faibussowitsch PetscCall(PetscDTGaussJacobiQuadrature(npoints, -1., 1., i, 0.0, p1, w1)); 1964fbdc3dfeSToby Isaac for (pt = 0, l = 0; l < totprev; l++) { 1965fbdc3dfeSToby Isaac for (j = 0; j < npoints; j++) { 1966fbdc3dfeSToby Isaac for (m = 0; m < totrem; m++, pt++) { 1967fbdc3dfeSToby Isaac for (k = 0; k < i; k++) x[pt*dim+k] = (x[pt*dim+k]+1.)*(1.-p1[j])*0.5 - 1.; 1968fbdc3dfeSToby Isaac x[pt * dim + i] = p1[j]; 1969fbdc3dfeSToby Isaac for (c = 0; c < Nc; c++) w[pt*Nc + c] *= mul * w1[j]; 1970494e7359SMatthew G. Knepley } 1971494e7359SMatthew G. Knepley } 1972494e7359SMatthew G. Knepley } 1973fbdc3dfeSToby Isaac totprev *= npoints; 1974fbdc3dfeSToby Isaac totrem /= npoints; 1975494e7359SMatthew G. Knepley } 19769566063dSJacob Faibussowitsch PetscCall(PetscFree2(p1, w1)); 19779566063dSJacob Faibussowitsch PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, q)); 19789566063dSJacob Faibussowitsch PetscCall(PetscQuadratureSetOrder(*q, 2*npoints-1)); 19799566063dSJacob Faibussowitsch PetscCall(PetscQuadratureSetData(*q, dim, Nc, totpoints, x, w)); 19809566063dSJacob Faibussowitsch PetscCall(PetscObjectChangeTypeName((PetscObject)*q,"StroudConical")); 1981494e7359SMatthew G. Knepley PetscFunctionReturn(0); 1982494e7359SMatthew G. Knepley } 1983494e7359SMatthew G. Knepley 1984*d3c69ad0SToby Isaac static PetscBool MinSymTriQuadCite = PETSC_FALSE; 1985*d3c69ad0SToby Isaac const char MinSymTriQuadCitation[] = 1986*d3c69ad0SToby Isaac "@article{WitherdenVincent2015,\n" 1987*d3c69ad0SToby Isaac " title = {On the identification of symmetric quadrature rules for finite element methods},\n" 1988*d3c69ad0SToby Isaac " journal = {Computers & Mathematics with Applications},\n" 1989*d3c69ad0SToby Isaac " volume = {69},\n" 1990*d3c69ad0SToby Isaac " number = {10},\n" 1991*d3c69ad0SToby Isaac " pages = {1232-1241},\n" 1992*d3c69ad0SToby Isaac " year = {2015},\n" 1993*d3c69ad0SToby Isaac " issn = {0898-1221},\n" 1994*d3c69ad0SToby Isaac " doi = {10.1016/j.camwa.2015.03.017},\n" 1995*d3c69ad0SToby Isaac " url = {https://www.sciencedirect.com/science/article/pii/S0898122115001224},\n" 1996*d3c69ad0SToby Isaac " author = {F.D. Witherden and P.E. Vincent},\n" 1997*d3c69ad0SToby Isaac "}\n"; 1998*d3c69ad0SToby Isaac 1999*d3c69ad0SToby Isaac #include "petscdttriquadrules.h" 2000*d3c69ad0SToby Isaac 2001*d3c69ad0SToby Isaac static PetscBool MinSymTetQuadCite = PETSC_FALSE; 2002*d3c69ad0SToby Isaac const char MinSymTetQuadCitation[] = 2003*d3c69ad0SToby Isaac "@article{JaskowiecSukumar2021\n" 2004*d3c69ad0SToby Isaac " author = {Jaskowiec, Jan and Sukumar, N.},\n" 2005*d3c69ad0SToby Isaac " title = {High-order symmetric cubature rules for tetrahedra and pyramids},\n" 2006*d3c69ad0SToby Isaac " journal = {International Journal for Numerical Methods in Engineering},\n" 2007*d3c69ad0SToby Isaac " volume = {122},\n" 2008*d3c69ad0SToby Isaac " number = {1},\n" 2009*d3c69ad0SToby Isaac " pages = {148-171},\n" 2010*d3c69ad0SToby Isaac " doi = {10.1002/nme.6528},\n" 2011*d3c69ad0SToby Isaac " url = {https://onlinelibrary.wiley.com/doi/abs/10.1002/nme.6528},\n" 2012*d3c69ad0SToby Isaac " eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1002/nme.6528},\n" 2013*d3c69ad0SToby Isaac " year = {2021}\n" 2014*d3c69ad0SToby Isaac "}\n"; 2015*d3c69ad0SToby Isaac 2016*d3c69ad0SToby Isaac #include "petscdttetquadrules.h" 2017*d3c69ad0SToby Isaac 2018*d3c69ad0SToby Isaac // https://en.wikipedia.org/wiki/Partition_(number_theory) 2019*d3c69ad0SToby Isaac static PetscErrorCode PetscDTPartitionNumber(PetscInt n, PetscInt *p) 2020*d3c69ad0SToby Isaac { 2021*d3c69ad0SToby Isaac // sequence A000041 in the OEIS 2022*d3c69ad0SToby Isaac const PetscInt partition[] = {1, 1, 2, 3, 5, 7, 11, 15, 22, 30, 42, 56, 77, 101, 135, 176, 231, 297, 385, 490, 627, 792, 1002, 1255, 1575, 1958, 2436, 3010, 3718, 4565, 5604}; 2023*d3c69ad0SToby Isaac PetscInt tabulated_max = PETSC_STATIC_ARRAY_LENGTH(partition) - 1; 2024*d3c69ad0SToby Isaac 2025*d3c69ad0SToby Isaac PetscFunctionBegin; 2026*d3c69ad0SToby Isaac PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Partition number not defined for negative number %" PetscInt_FMT, n); 2027*d3c69ad0SToby Isaac // not implementing the pentagonal number recurrence, we don't need partition numbers for n that high 2028*d3c69ad0SToby Isaac PetscCheck(n <= tabulated_max, PETSC_COMM_SELF, PETSC_ERR_SUP, "Partition numbers only tabulated up to %" PetscInt_FMT", not computed for %" PetscInt_FMT, tabulated_max, n); 2029*d3c69ad0SToby Isaac *p = partition[n]; 2030*d3c69ad0SToby Isaac PetscFunctionReturn(0); 2031*d3c69ad0SToby Isaac } 2032*d3c69ad0SToby Isaac 2033*d3c69ad0SToby Isaac /*@ 2034*d3c69ad0SToby Isaac PetscDTSimplexQuadrature - Create a quadrature rule for a simplex that exactly integrates polynomials up to a given degree. 2035*d3c69ad0SToby Isaac 2036*d3c69ad0SToby Isaac Not Collective 2037*d3c69ad0SToby Isaac 2038*d3c69ad0SToby Isaac Input Parameters: 2039*d3c69ad0SToby Isaac + dim - The spatial dimension of the simplex (1 = segment, 2 = triangle, 3 = tetrahedron) 2040*d3c69ad0SToby Isaac . degree - The largest polynomial degree that is required to be integrated exactly 2041*d3c69ad0SToby Isaac - type - left end of interval (often-1) 2042*d3c69ad0SToby Isaac 2043*d3c69ad0SToby Isaac Output Parameter: 2044*d3c69ad0SToby Isaac . quad - A PetscQuadrature object for integration over the biunit simplex 2045*d3c69ad0SToby Isaac (defined by the bounds $x_i >= -1$ and $\sum_i x_i <= 2 - d$) that is exact for 2046*d3c69ad0SToby Isaac polynomials up to the given degree 2047*d3c69ad0SToby Isaac 2048*d3c69ad0SToby Isaac Level: intermediate 2049*d3c69ad0SToby Isaac 2050*d3c69ad0SToby Isaac .seealso: `PetscDTSimplexQuadratureType`, `PetscDTGaussQuadrature()`, `PetscDTStroudCononicalQuadrature()` 2051*d3c69ad0SToby Isaac @*/ 2052*d3c69ad0SToby Isaac PetscErrorCode PetscDTSimplexQuadrature(PetscInt dim, PetscInt degree, PetscDTSimplexQuadratureType type, PetscQuadrature *quad) 2053*d3c69ad0SToby Isaac { 2054*d3c69ad0SToby Isaac PetscDTSimplexQuadratureType orig_type = type; 2055*d3c69ad0SToby Isaac 2056*d3c69ad0SToby Isaac PetscFunctionBegin; 2057*d3c69ad0SToby Isaac PetscCheck(dim >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative dimension %" PetscInt_FMT, dim); 2058*d3c69ad0SToby Isaac PetscCheck(degree >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative degree %" PetscInt_FMT, degree); 2059*d3c69ad0SToby Isaac if (type == PETSCDTSIMPLEXQUAD_DEFAULT) { 2060*d3c69ad0SToby Isaac type = PETSCDTSIMPLEXQUAD_MINSYM; 2061*d3c69ad0SToby Isaac } 2062*d3c69ad0SToby Isaac if (type == PETSCDTSIMPLEXQUAD_CONIC || dim < 2) { 2063*d3c69ad0SToby Isaac PetscInt points_per_dim = (degree + 2) / 2; // ceil((degree + 1) / 2); 2064*d3c69ad0SToby Isaac PetscCall(PetscDTStroudConicalQuadrature(dim, 1, points_per_dim, -1, 1, quad)); 2065*d3c69ad0SToby Isaac } else { 2066*d3c69ad0SToby Isaac PetscInt n = dim + 1; 2067*d3c69ad0SToby Isaac PetscInt fact = 1; 2068*d3c69ad0SToby Isaac PetscInt *part, *perm; 2069*d3c69ad0SToby Isaac PetscInt p = 0; 2070*d3c69ad0SToby Isaac PetscInt max_degree; 2071*d3c69ad0SToby Isaac const PetscInt *nodes_per_type = NULL; 2072*d3c69ad0SToby Isaac const PetscInt *all_num_full_nodes = NULL; 2073*d3c69ad0SToby Isaac const PetscReal **weights_list = NULL; 2074*d3c69ad0SToby Isaac const PetscReal **compact_nodes_list = NULL; 2075*d3c69ad0SToby Isaac const char *citation = NULL; 2076*d3c69ad0SToby Isaac PetscBool *cited = NULL; 2077*d3c69ad0SToby Isaac 2078*d3c69ad0SToby Isaac switch (dim) { 2079*d3c69ad0SToby Isaac case 2: 2080*d3c69ad0SToby Isaac cited = &MinSymTriQuadCite; 2081*d3c69ad0SToby Isaac citation = MinSymTriQuadCitation; 2082*d3c69ad0SToby Isaac max_degree = PetscDTWVTriQuad_max_degree; 2083*d3c69ad0SToby Isaac nodes_per_type = PetscDTWVTriQuad_num_orbits; 2084*d3c69ad0SToby Isaac all_num_full_nodes = PetscDTWVTriQuad_num_nodes; 2085*d3c69ad0SToby Isaac weights_list = PetscDTWVTriQuad_weights; 2086*d3c69ad0SToby Isaac compact_nodes_list = PetscDTWVTriQuad_orbits; 2087*d3c69ad0SToby Isaac break; 2088*d3c69ad0SToby Isaac case 3: 2089*d3c69ad0SToby Isaac cited = &MinSymTetQuadCite; 2090*d3c69ad0SToby Isaac citation = MinSymTetQuadCitation; 2091*d3c69ad0SToby Isaac max_degree = PetscDTJSTetQuad_max_degree; 2092*d3c69ad0SToby Isaac nodes_per_type = PetscDTJSTetQuad_num_orbits; 2093*d3c69ad0SToby Isaac all_num_full_nodes = PetscDTJSTetQuad_num_nodes; 2094*d3c69ad0SToby Isaac weights_list = PetscDTJSTetQuad_weights; 2095*d3c69ad0SToby Isaac compact_nodes_list = PetscDTJSTetQuad_orbits; 2096*d3c69ad0SToby Isaac break; 2097*d3c69ad0SToby Isaac default: 2098*d3c69ad0SToby Isaac max_degree = - 1; 2099*d3c69ad0SToby Isaac break; 2100*d3c69ad0SToby Isaac } 2101*d3c69ad0SToby Isaac 2102*d3c69ad0SToby Isaac if (degree > max_degree) { 2103*d3c69ad0SToby Isaac if (orig_type == PETSCDTSIMPLEXQUAD_DEFAULT) { 2104*d3c69ad0SToby Isaac // fall back to conic 2105*d3c69ad0SToby Isaac PetscCall(PetscDTSimplexQuadrature(dim, degree, PETSCDTSIMPLEXQUAD_CONIC, quad)); 2106*d3c69ad0SToby Isaac PetscFunctionReturn(0); 2107*d3c69ad0SToby Isaac } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Minimal symmetric quadrature for dim %" PetscInt_FMT ", degree %" PetscInt_FMT " unsupported", dim, degree); 2108*d3c69ad0SToby Isaac } 2109*d3c69ad0SToby Isaac 2110*d3c69ad0SToby Isaac PetscCall(PetscCitationsRegister(citation, cited)); 2111*d3c69ad0SToby Isaac 2112*d3c69ad0SToby Isaac PetscCall(PetscDTPartitionNumber(n, &p)); 2113*d3c69ad0SToby Isaac for (PetscInt d = 2; d <= n; d++) fact *= d; 2114*d3c69ad0SToby Isaac 2115*d3c69ad0SToby Isaac PetscInt num_full_nodes = all_num_full_nodes[degree]; 2116*d3c69ad0SToby Isaac const PetscReal *all_compact_nodes = compact_nodes_list[degree]; 2117*d3c69ad0SToby Isaac const PetscReal *all_compact_weights = weights_list[degree]; 2118*d3c69ad0SToby Isaac nodes_per_type = &nodes_per_type[p * degree]; 2119*d3c69ad0SToby Isaac 2120*d3c69ad0SToby Isaac PetscReal *points; 2121*d3c69ad0SToby Isaac PetscReal *counts; 2122*d3c69ad0SToby Isaac PetscReal *weights; 2123*d3c69ad0SToby Isaac PetscReal *bary_to_biunit; // row-major transformation of barycentric coordinate to biunit 2124*d3c69ad0SToby Isaac PetscQuadrature q; 2125*d3c69ad0SToby Isaac 2126*d3c69ad0SToby Isaac // compute the transformation 2127*d3c69ad0SToby Isaac PetscCall(PetscMalloc1(n * dim, &bary_to_biunit)); 2128*d3c69ad0SToby Isaac for (PetscInt d = 0; d < dim; d++) { 2129*d3c69ad0SToby Isaac for (PetscInt b = 0; b < n; b++) { 2130*d3c69ad0SToby Isaac bary_to_biunit[d * n + b] = (d == b) ? 1.0 : -1.0; 2131*d3c69ad0SToby Isaac } 2132*d3c69ad0SToby Isaac } 2133*d3c69ad0SToby Isaac 2134*d3c69ad0SToby Isaac PetscCall(PetscMalloc3(n, &part, n, &perm, n, &counts)); 2135*d3c69ad0SToby Isaac PetscCall(PetscCalloc1(num_full_nodes * dim, &points)); 2136*d3c69ad0SToby Isaac PetscCall(PetscMalloc1(num_full_nodes, &weights)); 2137*d3c69ad0SToby Isaac 2138*d3c69ad0SToby Isaac // (0, 0, ...) is the first partition lexicographically 2139*d3c69ad0SToby Isaac PetscCall(PetscArrayzero(part, n)); 2140*d3c69ad0SToby Isaac PetscCall(PetscArrayzero(counts, n)); 2141*d3c69ad0SToby Isaac counts[0] = n; 2142*d3c69ad0SToby Isaac 2143*d3c69ad0SToby Isaac // for each partition 2144*d3c69ad0SToby Isaac for (PetscInt s = 0, node_offset = 0; s < p; s++) { 2145*d3c69ad0SToby Isaac PetscInt num_compact_coords = part[n-1] + 1; 2146*d3c69ad0SToby Isaac 2147*d3c69ad0SToby Isaac const PetscReal *compact_nodes = all_compact_nodes; 2148*d3c69ad0SToby Isaac const PetscReal *compact_weights = all_compact_weights; 2149*d3c69ad0SToby Isaac all_compact_nodes += num_compact_coords * nodes_per_type[s]; 2150*d3c69ad0SToby Isaac all_compact_weights += nodes_per_type[s]; 2151*d3c69ad0SToby Isaac 2152*d3c69ad0SToby Isaac // for every permutation of the vertices 2153*d3c69ad0SToby Isaac for (PetscInt f = 0; f < fact; f++) { 2154*d3c69ad0SToby Isaac PetscCall(PetscDTEnumPerm(n, f, perm, NULL)); 2155*d3c69ad0SToby Isaac 2156*d3c69ad0SToby Isaac // check if it is a valid permutation 2157*d3c69ad0SToby Isaac PetscInt digit; 2158*d3c69ad0SToby Isaac for (digit = 1; digit < n; digit++) { 2159*d3c69ad0SToby Isaac // skip permutations that would duplicate a node because it has a smaller symmetry group 2160*d3c69ad0SToby Isaac if (part[digit - 1] == part[digit] && perm[digit - 1] > perm[digit]) break; 2161*d3c69ad0SToby Isaac } 2162*d3c69ad0SToby Isaac if (digit < n) continue; 2163*d3c69ad0SToby Isaac 2164*d3c69ad0SToby Isaac // create full nodes from this permutation of the compact nodes 2165*d3c69ad0SToby Isaac PetscReal *full_nodes = &points[node_offset * dim]; 2166*d3c69ad0SToby Isaac PetscReal *full_weights = &weights[node_offset]; 2167*d3c69ad0SToby Isaac 2168*d3c69ad0SToby Isaac PetscCall(PetscArraycpy(full_weights, compact_weights, nodes_per_type[s])); 2169*d3c69ad0SToby Isaac for (PetscInt b = 0; b < n; b++) { 2170*d3c69ad0SToby Isaac for (PetscInt d = 0; d < dim; d++) { 2171*d3c69ad0SToby Isaac for (PetscInt node = 0; node < nodes_per_type[s]; node++) { 2172*d3c69ad0SToby Isaac full_nodes[node * dim + d] += bary_to_biunit[d * n + perm[b]] * compact_nodes[node * num_compact_coords + part[b]]; 2173*d3c69ad0SToby Isaac } 2174*d3c69ad0SToby Isaac } 2175*d3c69ad0SToby Isaac } 2176*d3c69ad0SToby Isaac node_offset += nodes_per_type[s]; 2177*d3c69ad0SToby Isaac } 2178*d3c69ad0SToby Isaac 2179*d3c69ad0SToby Isaac if (s < p - 1) { // Generate the next partition 2180*d3c69ad0SToby Isaac /* A partition is described by the number of coordinates that are in 2181*d3c69ad0SToby Isaac * each set of duplicates (counts) and redundantly by mapping each 2182*d3c69ad0SToby Isaac * index to its set of duplicates (part) 2183*d3c69ad0SToby Isaac * 2184*d3c69ad0SToby Isaac * Counts should always be in nonincreasing order 2185*d3c69ad0SToby Isaac * 2186*d3c69ad0SToby Isaac * We want to generate the partitions lexically by part, which means 2187*d3c69ad0SToby Isaac * finding the last index where count > 1 and reducing by 1. 2188*d3c69ad0SToby Isaac * 2189*d3c69ad0SToby Isaac * For the new counts beyond that index, we eagerly assign the remaining 2190*d3c69ad0SToby Isaac * capacity of the partition to smaller indices (ensures lexical ordering), 2191*d3c69ad0SToby Isaac * while respecting the nonincreasing invariant of the counts 2192*d3c69ad0SToby Isaac */ 2193*d3c69ad0SToby Isaac PetscInt last_digit = part[n-1]; 2194*d3c69ad0SToby Isaac PetscInt last_digit_with_extra = last_digit; 2195*d3c69ad0SToby Isaac while (counts[last_digit_with_extra] == 1) last_digit_with_extra--; 2196*d3c69ad0SToby Isaac PetscInt limit = --counts[last_digit_with_extra]; 2197*d3c69ad0SToby Isaac PetscInt total_to_distribute = last_digit - last_digit_with_extra + 1; 2198*d3c69ad0SToby Isaac for (PetscInt digit = last_digit_with_extra + 1; digit < n; digit++) { 2199*d3c69ad0SToby Isaac counts[digit] = PetscMin(limit, total_to_distribute); 2200*d3c69ad0SToby Isaac total_to_distribute -= PetscMin(limit, total_to_distribute); 2201*d3c69ad0SToby Isaac } 2202*d3c69ad0SToby Isaac for (PetscInt digit = 0, offset = 0; digit < n; digit++) { 2203*d3c69ad0SToby Isaac PetscInt count = counts[digit]; 2204*d3c69ad0SToby Isaac for (PetscInt c = 0; c < count; c++) { 2205*d3c69ad0SToby Isaac part[offset++] = digit; 2206*d3c69ad0SToby Isaac } 2207*d3c69ad0SToby Isaac } 2208*d3c69ad0SToby Isaac } 2209*d3c69ad0SToby Isaac } 2210*d3c69ad0SToby Isaac PetscCall(PetscFree3(part, perm, counts)); 2211*d3c69ad0SToby Isaac PetscCall(PetscFree(bary_to_biunit)); 2212*d3c69ad0SToby Isaac PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, &q)); 2213*d3c69ad0SToby Isaac PetscCall(PetscQuadratureSetData(q, dim, 1, num_full_nodes, points, weights)); 2214*d3c69ad0SToby Isaac *quad = q; 2215*d3c69ad0SToby Isaac } 2216*d3c69ad0SToby Isaac PetscFunctionReturn(0); 2217*d3c69ad0SToby Isaac } 2218*d3c69ad0SToby Isaac 2219f5f57ec0SBarry Smith /*@ 2220b3c0f97bSTom Klotz PetscDTTanhSinhTensorQuadrature - create tanh-sinh quadrature for a tensor product cell 2221b3c0f97bSTom Klotz 2222b3c0f97bSTom Klotz Not Collective 2223b3c0f97bSTom Klotz 22244165533cSJose E. Roman Input Parameters: 2225b3c0f97bSTom Klotz + dim - The cell dimension 2226b3c0f97bSTom Klotz . level - The number of points in one dimension, 2^l 2227b3c0f97bSTom Klotz . a - left end of interval (often-1) 2228b3c0f97bSTom Klotz - b - right end of interval (often +1) 2229b3c0f97bSTom Klotz 22304165533cSJose E. Roman Output Parameter: 2231b3c0f97bSTom Klotz . q - A PetscQuadrature object 2232b3c0f97bSTom Klotz 2233b3c0f97bSTom Klotz Level: intermediate 2234b3c0f97bSTom Klotz 2235db781477SPatrick Sanan .seealso: `PetscDTGaussTensorQuadrature()` 2236b3c0f97bSTom Klotz @*/ 2237b3c0f97bSTom Klotz PetscErrorCode PetscDTTanhSinhTensorQuadrature(PetscInt dim, PetscInt level, PetscReal a, PetscReal b, PetscQuadrature *q) 2238b3c0f97bSTom Klotz { 2239b3c0f97bSTom Klotz const PetscInt p = 16; /* Digits of precision in the evaluation */ 2240b3c0f97bSTom Klotz const PetscReal alpha = (b-a)/2.; /* Half-width of the integration interval */ 2241b3c0f97bSTom Klotz const PetscReal beta = (b+a)/2.; /* Center of the integration interval */ 2242b3c0f97bSTom Klotz const PetscReal h = PetscPowReal(2.0, -level); /* Step size, length between x_k */ 2243d84b4d08SMatthew G. Knepley PetscReal xk; /* Quadrature point x_k on reference domain [-1, 1] */ 2244b3c0f97bSTom Klotz PetscReal wk = 0.5*PETSC_PI; /* Quadrature weight at x_k */ 2245b3c0f97bSTom Klotz PetscReal *x, *w; 2246b3c0f97bSTom Klotz PetscInt K, k, npoints; 2247b3c0f97bSTom Klotz 2248b3c0f97bSTom Klotz PetscFunctionBegin; 224963a3b9bcSJacob Faibussowitsch PetscCheck(dim <= 1,PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not yet implemented", dim); 225028b400f6SJacob Faibussowitsch PetscCheck(level,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a number of significant digits"); 2251b3c0f97bSTom Klotz /* Find K such that the weights are < 32 digits of precision */ 2252b3c0f97bSTom Klotz for (K = 1; PetscAbsReal(PetscLog10Real(wk)) < 2*p; ++K) { 22539add2064SThomas Klotz wk = 0.5*h*PETSC_PI*PetscCoshReal(K*h)/PetscSqr(PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(K*h))); 2254b3c0f97bSTom Klotz } 22559566063dSJacob Faibussowitsch PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, q)); 22569566063dSJacob Faibussowitsch PetscCall(PetscQuadratureSetOrder(*q, 2*K+1)); 2257b3c0f97bSTom Klotz npoints = 2*K-1; 22589566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(npoints*dim, &x)); 22599566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(npoints, &w)); 2260b3c0f97bSTom Klotz /* Center term */ 2261b3c0f97bSTom Klotz x[0] = beta; 2262b3c0f97bSTom Klotz w[0] = 0.5*alpha*PETSC_PI; 2263b3c0f97bSTom Klotz for (k = 1; k < K; ++k) { 22649add2064SThomas Klotz wk = 0.5*alpha*h*PETSC_PI*PetscCoshReal(k*h)/PetscSqr(PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(k*h))); 22651118d4bcSLisandro Dalcin xk = PetscTanhReal(0.5*PETSC_PI*PetscSinhReal(k*h)); 2266b3c0f97bSTom Klotz x[2*k-1] = -alpha*xk+beta; 2267b3c0f97bSTom Klotz w[2*k-1] = wk; 2268b3c0f97bSTom Klotz x[2*k+0] = alpha*xk+beta; 2269b3c0f97bSTom Klotz w[2*k+0] = wk; 2270b3c0f97bSTom Klotz } 22719566063dSJacob Faibussowitsch PetscCall(PetscQuadratureSetData(*q, dim, 1, npoints, x, w)); 2272b3c0f97bSTom Klotz PetscFunctionReturn(0); 2273b3c0f97bSTom Klotz } 2274b3c0f97bSTom Klotz 2275d6685f55SMatthew G. Knepley PetscErrorCode PetscDTTanhSinhIntegrate(void (*func)(const PetscReal[], void *, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, void *ctx, PetscReal *sol) 2276b3c0f97bSTom Klotz { 2277b3c0f97bSTom Klotz const PetscInt p = 16; /* Digits of precision in the evaluation */ 2278b3c0f97bSTom Klotz const PetscReal alpha = (b-a)/2.; /* Half-width of the integration interval */ 2279b3c0f97bSTom Klotz const PetscReal beta = (b+a)/2.; /* Center of the integration interval */ 2280b3c0f97bSTom Klotz PetscReal h = 1.0; /* Step size, length between x_k */ 2281b3c0f97bSTom Klotz PetscInt l = 0; /* Level of refinement, h = 2^{-l} */ 2282b3c0f97bSTom Klotz PetscReal osum = 0.0; /* Integral on last level */ 2283b3c0f97bSTom Klotz PetscReal psum = 0.0; /* Integral on the level before the last level */ 2284b3c0f97bSTom Klotz PetscReal sum; /* Integral on current level */ 2285446c295cSMatthew G. Knepley PetscReal yk; /* Quadrature point 1 - x_k on reference domain [-1, 1] */ 2286b3c0f97bSTom Klotz PetscReal lx, rx; /* Quadrature points to the left and right of 0 on the real domain [a, b] */ 2287b3c0f97bSTom Klotz PetscReal wk; /* Quadrature weight at x_k */ 2288b3c0f97bSTom Klotz PetscReal lval, rval; /* Terms in the quadature sum to the left and right of 0 */ 2289b3c0f97bSTom Klotz PetscInt d; /* Digits of precision in the integral */ 2290b3c0f97bSTom Klotz 2291b3c0f97bSTom Klotz PetscFunctionBegin; 229208401ef6SPierre Jolivet PetscCheck(digits > 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a positive number of significant digits"); 2293b3c0f97bSTom Klotz /* Center term */ 2294d6685f55SMatthew G. Knepley func(&beta, ctx, &lval); 2295b3c0f97bSTom Klotz sum = 0.5*alpha*PETSC_PI*lval; 2296b3c0f97bSTom Klotz /* */ 2297b3c0f97bSTom Klotz do { 2298b3c0f97bSTom Klotz PetscReal lterm, rterm, maxTerm = 0.0, d1, d2, d3, d4; 2299b3c0f97bSTom Klotz PetscInt k = 1; 2300b3c0f97bSTom Klotz 2301b3c0f97bSTom Klotz ++l; 230263a3b9bcSJacob Faibussowitsch /* PetscPrintf(PETSC_COMM_SELF, "LEVEL %" PetscInt_FMT " sum: %15.15f\n", l, sum); */ 2303b3c0f97bSTom Klotz /* At each level of refinement, h --> h/2 and sum --> sum/2 */ 2304b3c0f97bSTom Klotz psum = osum; 2305b3c0f97bSTom Klotz osum = sum; 2306b3c0f97bSTom Klotz h *= 0.5; 2307b3c0f97bSTom Klotz sum *= 0.5; 2308b3c0f97bSTom Klotz do { 23099add2064SThomas Klotz wk = 0.5*h*PETSC_PI*PetscCoshReal(k*h)/PetscSqr(PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(k*h))); 2310446c295cSMatthew G. Knepley yk = 1.0/(PetscExpReal(0.5*PETSC_PI*PetscSinhReal(k*h)) * PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(k*h))); 2311446c295cSMatthew G. Knepley lx = -alpha*(1.0 - yk)+beta; 2312446c295cSMatthew G. Knepley rx = alpha*(1.0 - yk)+beta; 2313d6685f55SMatthew G. Knepley func(&lx, ctx, &lval); 2314d6685f55SMatthew G. Knepley func(&rx, ctx, &rval); 2315b3c0f97bSTom Klotz lterm = alpha*wk*lval; 2316b3c0f97bSTom Klotz maxTerm = PetscMax(PetscAbsReal(lterm), maxTerm); 2317b3c0f97bSTom Klotz sum += lterm; 2318b3c0f97bSTom Klotz rterm = alpha*wk*rval; 2319b3c0f97bSTom Klotz maxTerm = PetscMax(PetscAbsReal(rterm), maxTerm); 2320b3c0f97bSTom Klotz sum += rterm; 2321b3c0f97bSTom Klotz ++k; 2322b3c0f97bSTom Klotz /* Only need to evaluate every other point on refined levels */ 2323b3c0f97bSTom Klotz if (l != 1) ++k; 23249add2064SThomas Klotz } while (PetscAbsReal(PetscLog10Real(wk)) < p); /* Only need to evaluate sum until weights are < 32 digits of precision */ 2325b3c0f97bSTom Klotz 2326b3c0f97bSTom Klotz d1 = PetscLog10Real(PetscAbsReal(sum - osum)); 2327b3c0f97bSTom Klotz d2 = PetscLog10Real(PetscAbsReal(sum - psum)); 2328b3c0f97bSTom Klotz d3 = PetscLog10Real(maxTerm) - p; 232909d48545SBarry Smith if (PetscMax(PetscAbsReal(lterm), PetscAbsReal(rterm)) == 0.0) d4 = 0.0; 233009d48545SBarry Smith else d4 = PetscLog10Real(PetscMax(PetscAbsReal(lterm), PetscAbsReal(rterm))); 2331b3c0f97bSTom Klotz d = PetscAbsInt(PetscMin(0, PetscMax(PetscMax(PetscMax(PetscSqr(d1)/d2, 2*d1), d3), d4))); 23329add2064SThomas Klotz } while (d < digits && l < 12); 2333b3c0f97bSTom Klotz *sol = sum; 2334e510cb1fSThomas Klotz 2335b3c0f97bSTom Klotz PetscFunctionReturn(0); 2336b3c0f97bSTom Klotz } 2337b3c0f97bSTom Klotz 2338497880caSRichard Tran Mills #if defined(PETSC_HAVE_MPFR) 2339d6685f55SMatthew G. Knepley PetscErrorCode PetscDTTanhSinhIntegrateMPFR(void (*func)(const PetscReal[], void *, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, void *ctx, PetscReal *sol) 234029f144ccSMatthew G. Knepley { 2341e510cb1fSThomas Klotz const PetscInt safetyFactor = 2; /* Calculate abcissa until 2*p digits */ 234229f144ccSMatthew G. Knepley PetscInt l = 0; /* Level of refinement, h = 2^{-l} */ 234329f144ccSMatthew G. Knepley mpfr_t alpha; /* Half-width of the integration interval */ 234429f144ccSMatthew G. Knepley mpfr_t beta; /* Center of the integration interval */ 234529f144ccSMatthew G. Knepley mpfr_t h; /* Step size, length between x_k */ 234629f144ccSMatthew G. Knepley mpfr_t osum; /* Integral on last level */ 234729f144ccSMatthew G. Knepley mpfr_t psum; /* Integral on the level before the last level */ 234829f144ccSMatthew G. Knepley mpfr_t sum; /* Integral on current level */ 234929f144ccSMatthew G. Knepley mpfr_t yk; /* Quadrature point 1 - x_k on reference domain [-1, 1] */ 235029f144ccSMatthew G. Knepley mpfr_t lx, rx; /* Quadrature points to the left and right of 0 on the real domain [a, b] */ 235129f144ccSMatthew G. Knepley mpfr_t wk; /* Quadrature weight at x_k */ 23521fbc92bbSMatthew G. Knepley PetscReal lval, rval, rtmp; /* Terms in the quadature sum to the left and right of 0 */ 235329f144ccSMatthew G. Knepley PetscInt d; /* Digits of precision in the integral */ 235429f144ccSMatthew G. Knepley mpfr_t pi2, kh, msinh, mcosh, maxTerm, curTerm, tmp; 235529f144ccSMatthew G. Knepley 235629f144ccSMatthew G. Knepley PetscFunctionBegin; 235708401ef6SPierre Jolivet PetscCheck(digits > 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a positive number of significant digits"); 235829f144ccSMatthew G. Knepley /* Create high precision storage */ 2359c9f744b5SMatthew 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); 236029f144ccSMatthew G. Knepley /* Initialization */ 236129f144ccSMatthew G. Knepley mpfr_set_d(alpha, 0.5*(b-a), MPFR_RNDN); 236229f144ccSMatthew G. Knepley mpfr_set_d(beta, 0.5*(b+a), MPFR_RNDN); 236329f144ccSMatthew G. Knepley mpfr_set_d(osum, 0.0, MPFR_RNDN); 236429f144ccSMatthew G. Knepley mpfr_set_d(psum, 0.0, MPFR_RNDN); 236529f144ccSMatthew G. Knepley mpfr_set_d(h, 1.0, MPFR_RNDN); 236629f144ccSMatthew G. Knepley mpfr_const_pi(pi2, MPFR_RNDN); 236729f144ccSMatthew G. Knepley mpfr_mul_d(pi2, pi2, 0.5, MPFR_RNDN); 236829f144ccSMatthew G. Knepley /* Center term */ 23691fbc92bbSMatthew G. Knepley rtmp = 0.5*(b+a); 23701fbc92bbSMatthew G. Knepley func(&rtmp, ctx, &lval); 237129f144ccSMatthew G. Knepley mpfr_set(sum, pi2, MPFR_RNDN); 237229f144ccSMatthew G. Knepley mpfr_mul(sum, sum, alpha, MPFR_RNDN); 237329f144ccSMatthew G. Knepley mpfr_mul_d(sum, sum, lval, MPFR_RNDN); 237429f144ccSMatthew G. Knepley /* */ 237529f144ccSMatthew G. Knepley do { 237629f144ccSMatthew G. Knepley PetscReal d1, d2, d3, d4; 237729f144ccSMatthew G. Knepley PetscInt k = 1; 237829f144ccSMatthew G. Knepley 237929f144ccSMatthew G. Knepley ++l; 238029f144ccSMatthew G. Knepley mpfr_set_d(maxTerm, 0.0, MPFR_RNDN); 238163a3b9bcSJacob Faibussowitsch /* PetscPrintf(PETSC_COMM_SELF, "LEVEL %" PetscInt_FMT " sum: %15.15f\n", l, sum); */ 238229f144ccSMatthew G. Knepley /* At each level of refinement, h --> h/2 and sum --> sum/2 */ 238329f144ccSMatthew G. Knepley mpfr_set(psum, osum, MPFR_RNDN); 238429f144ccSMatthew G. Knepley mpfr_set(osum, sum, MPFR_RNDN); 238529f144ccSMatthew G. Knepley mpfr_mul_d(h, h, 0.5, MPFR_RNDN); 238629f144ccSMatthew G. Knepley mpfr_mul_d(sum, sum, 0.5, MPFR_RNDN); 238729f144ccSMatthew G. Knepley do { 238829f144ccSMatthew G. Knepley mpfr_set_si(kh, k, MPFR_RNDN); 238929f144ccSMatthew G. Knepley mpfr_mul(kh, kh, h, MPFR_RNDN); 239029f144ccSMatthew G. Knepley /* Weight */ 239129f144ccSMatthew G. Knepley mpfr_set(wk, h, MPFR_RNDN); 239229f144ccSMatthew G. Knepley mpfr_sinh_cosh(msinh, mcosh, kh, MPFR_RNDN); 239329f144ccSMatthew G. Knepley mpfr_mul(msinh, msinh, pi2, MPFR_RNDN); 239429f144ccSMatthew G. Knepley mpfr_mul(mcosh, mcosh, pi2, MPFR_RNDN); 239529f144ccSMatthew G. Knepley mpfr_cosh(tmp, msinh, MPFR_RNDN); 239629f144ccSMatthew G. Knepley mpfr_sqr(tmp, tmp, MPFR_RNDN); 239729f144ccSMatthew G. Knepley mpfr_mul(wk, wk, mcosh, MPFR_RNDN); 239829f144ccSMatthew G. Knepley mpfr_div(wk, wk, tmp, MPFR_RNDN); 239929f144ccSMatthew G. Knepley /* Abscissa */ 240029f144ccSMatthew G. Knepley mpfr_set_d(yk, 1.0, MPFR_RNDZ); 240129f144ccSMatthew G. Knepley mpfr_cosh(tmp, msinh, MPFR_RNDN); 240229f144ccSMatthew G. Knepley mpfr_div(yk, yk, tmp, MPFR_RNDZ); 240329f144ccSMatthew G. Knepley mpfr_exp(tmp, msinh, MPFR_RNDN); 240429f144ccSMatthew G. Knepley mpfr_div(yk, yk, tmp, MPFR_RNDZ); 240529f144ccSMatthew G. Knepley /* Quadrature points */ 240629f144ccSMatthew G. Knepley mpfr_sub_d(lx, yk, 1.0, MPFR_RNDZ); 240729f144ccSMatthew G. Knepley mpfr_mul(lx, lx, alpha, MPFR_RNDU); 240829f144ccSMatthew G. Knepley mpfr_add(lx, lx, beta, MPFR_RNDU); 240929f144ccSMatthew G. Knepley mpfr_d_sub(rx, 1.0, yk, MPFR_RNDZ); 241029f144ccSMatthew G. Knepley mpfr_mul(rx, rx, alpha, MPFR_RNDD); 241129f144ccSMatthew G. Knepley mpfr_add(rx, rx, beta, MPFR_RNDD); 241229f144ccSMatthew G. Knepley /* Evaluation */ 24131fbc92bbSMatthew G. Knepley rtmp = mpfr_get_d(lx, MPFR_RNDU); 24141fbc92bbSMatthew G. Knepley func(&rtmp, ctx, &lval); 24151fbc92bbSMatthew G. Knepley rtmp = mpfr_get_d(rx, MPFR_RNDD); 24161fbc92bbSMatthew G. Knepley func(&rtmp, ctx, &rval); 241729f144ccSMatthew G. Knepley /* Update */ 241829f144ccSMatthew G. Knepley mpfr_mul(tmp, wk, alpha, MPFR_RNDN); 241929f144ccSMatthew G. Knepley mpfr_mul_d(tmp, tmp, lval, MPFR_RNDN); 242029f144ccSMatthew G. Knepley mpfr_add(sum, sum, tmp, MPFR_RNDN); 242129f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 242229f144ccSMatthew G. Knepley mpfr_max(maxTerm, maxTerm, tmp, MPFR_RNDN); 242329f144ccSMatthew G. Knepley mpfr_set(curTerm, tmp, MPFR_RNDN); 242429f144ccSMatthew G. Knepley mpfr_mul(tmp, wk, alpha, MPFR_RNDN); 242529f144ccSMatthew G. Knepley mpfr_mul_d(tmp, tmp, rval, MPFR_RNDN); 242629f144ccSMatthew G. Knepley mpfr_add(sum, sum, tmp, MPFR_RNDN); 242729f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 242829f144ccSMatthew G. Knepley mpfr_max(maxTerm, maxTerm, tmp, MPFR_RNDN); 242929f144ccSMatthew G. Knepley mpfr_max(curTerm, curTerm, tmp, MPFR_RNDN); 243029f144ccSMatthew G. Knepley ++k; 243129f144ccSMatthew G. Knepley /* Only need to evaluate every other point on refined levels */ 243229f144ccSMatthew G. Knepley if (l != 1) ++k; 243329f144ccSMatthew G. Knepley mpfr_log10(tmp, wk, MPFR_RNDN); 243429f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 2435c9f744b5SMatthew G. Knepley } while (mpfr_get_d(tmp, MPFR_RNDN) < safetyFactor*digits); /* Only need to evaluate sum until weights are < 32 digits of precision */ 243629f144ccSMatthew G. Knepley mpfr_sub(tmp, sum, osum, MPFR_RNDN); 243729f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 243829f144ccSMatthew G. Knepley mpfr_log10(tmp, tmp, MPFR_RNDN); 243929f144ccSMatthew G. Knepley d1 = mpfr_get_d(tmp, MPFR_RNDN); 244029f144ccSMatthew G. Knepley mpfr_sub(tmp, sum, psum, MPFR_RNDN); 244129f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 244229f144ccSMatthew G. Knepley mpfr_log10(tmp, tmp, MPFR_RNDN); 244329f144ccSMatthew G. Knepley d2 = mpfr_get_d(tmp, MPFR_RNDN); 244429f144ccSMatthew G. Knepley mpfr_log10(tmp, maxTerm, MPFR_RNDN); 2445c9f744b5SMatthew G. Knepley d3 = mpfr_get_d(tmp, MPFR_RNDN) - digits; 244629f144ccSMatthew G. Knepley mpfr_log10(tmp, curTerm, MPFR_RNDN); 244729f144ccSMatthew G. Knepley d4 = mpfr_get_d(tmp, MPFR_RNDN); 244829f144ccSMatthew G. Knepley d = PetscAbsInt(PetscMin(0, PetscMax(PetscMax(PetscMax(PetscSqr(d1)/d2, 2*d1), d3), d4))); 2449b0649871SThomas Klotz } while (d < digits && l < 8); 245029f144ccSMatthew G. Knepley *sol = mpfr_get_d(sum, MPFR_RNDN); 245129f144ccSMatthew G. Knepley /* Cleanup */ 245229f144ccSMatthew G. Knepley mpfr_clears(alpha, beta, h, sum, osum, psum, yk, wk, lx, rx, tmp, maxTerm, curTerm, pi2, kh, msinh, mcosh, NULL); 245329f144ccSMatthew G. Knepley PetscFunctionReturn(0); 245429f144ccSMatthew G. Knepley } 2455d525116cSMatthew G. Knepley #else 2456fbfcfee5SBarry Smith 2457d6685f55SMatthew G. Knepley PetscErrorCode PetscDTTanhSinhIntegrateMPFR(void (*func)(const PetscReal[], void *, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, void *ctx, PetscReal *sol) 2458d525116cSMatthew G. Knepley { 2459d525116cSMatthew G. Knepley SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "This method will not work without MPFR. Reconfigure using --download-mpfr --download-gmp"); 2460d525116cSMatthew G. Knepley } 246129f144ccSMatthew G. Knepley #endif 246229f144ccSMatthew G. Knepley 24632df84da0SMatthew G. Knepley /*@ 24642df84da0SMatthew G. Knepley PetscDTTensorQuadratureCreate - create the tensor product quadrature from two lower-dimensional quadratures 24652df84da0SMatthew G. Knepley 24662df84da0SMatthew G. Knepley Not Collective 24672df84da0SMatthew G. Knepley 24682df84da0SMatthew G. Knepley Input Parameters: 24692df84da0SMatthew G. Knepley + q1 - The first quadrature 24702df84da0SMatthew G. Knepley - q2 - The second quadrature 24712df84da0SMatthew G. Knepley 24722df84da0SMatthew G. Knepley Output Parameter: 24732df84da0SMatthew G. Knepley . q - A PetscQuadrature object 24742df84da0SMatthew G. Knepley 24752df84da0SMatthew G. Knepley Level: intermediate 24762df84da0SMatthew G. Knepley 2477db781477SPatrick Sanan .seealso: `PetscDTGaussTensorQuadrature()` 24782df84da0SMatthew G. Knepley @*/ 24792df84da0SMatthew G. Knepley PetscErrorCode PetscDTTensorQuadratureCreate(PetscQuadrature q1, PetscQuadrature q2, PetscQuadrature *q) 24802df84da0SMatthew G. Knepley { 24812df84da0SMatthew G. Knepley const PetscReal *x1, *w1, *x2, *w2; 24822df84da0SMatthew G. Knepley PetscReal *x, *w; 24832df84da0SMatthew G. Knepley PetscInt dim1, Nc1, Np1, order1, qa, d1; 24842df84da0SMatthew G. Knepley PetscInt dim2, Nc2, Np2, order2, qb, d2; 24852df84da0SMatthew G. Knepley PetscInt dim, Nc, Np, order, qc, d; 24862df84da0SMatthew G. Knepley 24872df84da0SMatthew G. Knepley PetscFunctionBegin; 24882df84da0SMatthew G. Knepley PetscValidHeaderSpecific(q1, PETSCQUADRATURE_CLASSID, 1); 24892df84da0SMatthew G. Knepley PetscValidHeaderSpecific(q2, PETSCQUADRATURE_CLASSID, 2); 24902df84da0SMatthew G. Knepley PetscValidPointer(q, 3); 24919566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetOrder(q1, &order1)); 24929566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetOrder(q2, &order2)); 24932df84da0SMatthew G. Knepley PetscCheck(order1 == order2, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Order1 %" PetscInt_FMT " != %" PetscInt_FMT " Order2", order1, order2); 24949566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetData(q1, &dim1, &Nc1, &Np1, &x1, &w1)); 24959566063dSJacob Faibussowitsch PetscCall(PetscQuadratureGetData(q2, &dim2, &Nc2, &Np2, &x2, &w2)); 24962df84da0SMatthew G. Knepley PetscCheck(Nc1 == Nc2, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "NumComp1 %" PetscInt_FMT " != %" PetscInt_FMT " NumComp2", Nc1, Nc2); 24972df84da0SMatthew G. Knepley 24982df84da0SMatthew G. Knepley dim = dim1 + dim2; 24992df84da0SMatthew G. Knepley Nc = Nc1; 25002df84da0SMatthew G. Knepley Np = Np1 * Np2; 25012df84da0SMatthew G. Knepley order = order1; 25029566063dSJacob Faibussowitsch PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, q)); 25039566063dSJacob Faibussowitsch PetscCall(PetscQuadratureSetOrder(*q, order)); 25049566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Np*dim, &x)); 25059566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Np, &w)); 25062df84da0SMatthew G. Knepley for (qa = 0, qc = 0; qa < Np1; ++qa) { 25072df84da0SMatthew G. Knepley for (qb = 0; qb < Np2; ++qb, ++qc) { 25082df84da0SMatthew G. Knepley for (d1 = 0, d = 0; d1 < dim1; ++d1, ++d) { 25092df84da0SMatthew G. Knepley x[qc*dim+d] = x1[qa*dim1+d1]; 25102df84da0SMatthew G. Knepley } 25112df84da0SMatthew G. Knepley for (d2 = 0; d2 < dim2; ++d2, ++d) { 25122df84da0SMatthew G. Knepley x[qc*dim+d] = x2[qb*dim2+d2]; 25132df84da0SMatthew G. Knepley } 25142df84da0SMatthew G. Knepley w[qc] = w1[qa] * w2[qb]; 25152df84da0SMatthew G. Knepley } 25162df84da0SMatthew G. Knepley } 25179566063dSJacob Faibussowitsch PetscCall(PetscQuadratureSetData(*q, dim, Nc, Np, x, w)); 25182df84da0SMatthew G. Knepley PetscFunctionReturn(0); 25192df84da0SMatthew G. Knepley } 25202df84da0SMatthew G. Knepley 2521194825f6SJed Brown /* Overwrites A. Can only handle full-rank problems with m>=n 2522194825f6SJed Brown * A in column-major format 2523194825f6SJed Brown * Ainv in row-major format 2524194825f6SJed Brown * tau has length m 2525194825f6SJed Brown * worksize must be >= max(1,n) 2526194825f6SJed Brown */ 2527194825f6SJed Brown static PetscErrorCode PetscDTPseudoInverseQR(PetscInt m,PetscInt mstride,PetscInt n,PetscReal *A_in,PetscReal *Ainv_out,PetscScalar *tau,PetscInt worksize,PetscScalar *work) 2528194825f6SJed Brown { 2529194825f6SJed Brown PetscBLASInt M,N,K,lda,ldb,ldwork,info; 2530194825f6SJed Brown PetscScalar *A,*Ainv,*R,*Q,Alpha; 2531194825f6SJed Brown 2532194825f6SJed Brown PetscFunctionBegin; 2533194825f6SJed Brown #if defined(PETSC_USE_COMPLEX) 2534194825f6SJed Brown { 2535194825f6SJed Brown PetscInt i,j; 25369566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(m*n,&A,m*n,&Ainv)); 2537194825f6SJed Brown for (j=0; j<n; j++) { 2538194825f6SJed Brown for (i=0; i<m; i++) A[i+m*j] = A_in[i+mstride*j]; 2539194825f6SJed Brown } 2540194825f6SJed Brown mstride = m; 2541194825f6SJed Brown } 2542194825f6SJed Brown #else 2543194825f6SJed Brown A = A_in; 2544194825f6SJed Brown Ainv = Ainv_out; 2545194825f6SJed Brown #endif 2546194825f6SJed Brown 25479566063dSJacob Faibussowitsch PetscCall(PetscBLASIntCast(m,&M)); 25489566063dSJacob Faibussowitsch PetscCall(PetscBLASIntCast(n,&N)); 25499566063dSJacob Faibussowitsch PetscCall(PetscBLASIntCast(mstride,&lda)); 25509566063dSJacob Faibussowitsch PetscCall(PetscBLASIntCast(worksize,&ldwork)); 25519566063dSJacob Faibussowitsch PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF)); 2552792fecdfSBarry Smith PetscCallBLAS("LAPACKgeqrf",LAPACKgeqrf_(&M,&N,A,&lda,tau,work,&ldwork,&info)); 25539566063dSJacob Faibussowitsch PetscCall(PetscFPTrapPop()); 255428b400f6SJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_LIB,"xGEQRF error"); 2555194825f6SJed Brown R = A; /* Upper triangular part of A now contains R, the rest contains the elementary reflectors */ 2556194825f6SJed Brown 2557194825f6SJed Brown /* Extract an explicit representation of Q */ 2558194825f6SJed Brown Q = Ainv; 25599566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(Q,A,mstride*n)); 2560194825f6SJed Brown K = N; /* full rank */ 2561792fecdfSBarry Smith PetscCallBLAS("LAPACKorgqr",LAPACKorgqr_(&M,&N,&K,Q,&lda,tau,work,&ldwork,&info)); 256228b400f6SJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_LIB,"xORGQR/xUNGQR error"); 2563194825f6SJed Brown 2564194825f6SJed Brown /* Compute A^{-T} = (R^{-1} Q^T)^T = Q R^{-T} */ 2565194825f6SJed Brown Alpha = 1.0; 2566194825f6SJed Brown ldb = lda; 2567792fecdfSBarry Smith PetscCallBLAS("BLAStrsm",BLAStrsm_("Right","Upper","ConjugateTranspose","NotUnitTriangular",&M,&N,&Alpha,R,&lda,Q,&ldb)); 2568194825f6SJed Brown /* Ainv is Q, overwritten with inverse */ 2569194825f6SJed Brown 2570194825f6SJed Brown #if defined(PETSC_USE_COMPLEX) 2571194825f6SJed Brown { 2572194825f6SJed Brown PetscInt i; 2573194825f6SJed Brown for (i=0; i<m*n; i++) Ainv_out[i] = PetscRealPart(Ainv[i]); 25749566063dSJacob Faibussowitsch PetscCall(PetscFree2(A,Ainv)); 2575194825f6SJed Brown } 2576194825f6SJed Brown #endif 2577194825f6SJed Brown PetscFunctionReturn(0); 2578194825f6SJed Brown } 2579194825f6SJed Brown 2580194825f6SJed Brown /* Computes integral of L_p' over intervals {(x0,x1),(x1,x2),...} */ 2581194825f6SJed Brown static PetscErrorCode PetscDTLegendreIntegrate(PetscInt ninterval,const PetscReal *x,PetscInt ndegree,const PetscInt *degrees,PetscBool Transpose,PetscReal *B) 2582194825f6SJed Brown { 2583194825f6SJed Brown PetscReal *Bv; 2584194825f6SJed Brown PetscInt i,j; 2585194825f6SJed Brown 2586194825f6SJed Brown PetscFunctionBegin; 25879566063dSJacob Faibussowitsch PetscCall(PetscMalloc1((ninterval+1)*ndegree,&Bv)); 2588194825f6SJed Brown /* Point evaluation of L_p on all the source vertices */ 25899566063dSJacob Faibussowitsch PetscCall(PetscDTLegendreEval(ninterval+1,x,ndegree,degrees,Bv,NULL,NULL)); 2590194825f6SJed Brown /* Integral over each interval: \int_a^b L_p' = L_p(b)-L_p(a) */ 2591194825f6SJed Brown for (i=0; i<ninterval; i++) { 2592194825f6SJed Brown for (j=0; j<ndegree; j++) { 2593194825f6SJed Brown if (Transpose) B[i+ninterval*j] = Bv[(i+1)*ndegree+j] - Bv[i*ndegree+j]; 2594194825f6SJed Brown else B[i*ndegree+j] = Bv[(i+1)*ndegree+j] - Bv[i*ndegree+j]; 2595194825f6SJed Brown } 2596194825f6SJed Brown } 25979566063dSJacob Faibussowitsch PetscCall(PetscFree(Bv)); 2598194825f6SJed Brown PetscFunctionReturn(0); 2599194825f6SJed Brown } 2600194825f6SJed Brown 2601194825f6SJed Brown /*@ 2602194825f6SJed Brown PetscDTReconstructPoly - create matrix representing polynomial reconstruction using cell intervals and evaluation at target intervals 2603194825f6SJed Brown 2604194825f6SJed Brown Not Collective 2605194825f6SJed Brown 26064165533cSJose E. Roman Input Parameters: 2607194825f6SJed Brown + degree - degree of reconstruction polynomial 2608194825f6SJed Brown . nsource - number of source intervals 2609194825f6SJed Brown . sourcex - sorted coordinates of source cell boundaries (length nsource+1) 2610194825f6SJed Brown . ntarget - number of target intervals 2611194825f6SJed Brown - targetx - sorted coordinates of target cell boundaries (length ntarget+1) 2612194825f6SJed Brown 26134165533cSJose E. Roman Output Parameter: 2614194825f6SJed Brown . R - reconstruction matrix, utarget = sum_s R[t*nsource+s] * usource[s] 2615194825f6SJed Brown 2616194825f6SJed Brown Level: advanced 2617194825f6SJed Brown 2618db781477SPatrick Sanan .seealso: `PetscDTLegendreEval()` 2619194825f6SJed Brown @*/ 2620194825f6SJed Brown PetscErrorCode PetscDTReconstructPoly(PetscInt degree,PetscInt nsource,const PetscReal *sourcex,PetscInt ntarget,const PetscReal *targetx,PetscReal *R) 2621194825f6SJed Brown { 2622194825f6SJed Brown PetscInt i,j,k,*bdegrees,worksize; 2623194825f6SJed Brown PetscReal xmin,xmax,center,hscale,*sourcey,*targety,*Bsource,*Bsinv,*Btarget; 2624194825f6SJed Brown PetscScalar *tau,*work; 2625194825f6SJed Brown 2626194825f6SJed Brown PetscFunctionBegin; 2627194825f6SJed Brown PetscValidRealPointer(sourcex,3); 2628194825f6SJed Brown PetscValidRealPointer(targetx,5); 2629194825f6SJed Brown PetscValidRealPointer(R,6); 263063a3b9bcSJacob Faibussowitsch PetscCheck(degree < nsource,PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Reconstruction degree %" PetscInt_FMT " must be less than number of source intervals %" PetscInt_FMT,degree,nsource); 263176bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 2632194825f6SJed Brown for (i=0; i<nsource; i++) { 263363a3b9bcSJacob Faibussowitsch PetscCheck(sourcex[i] < sourcex[i+1],PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Source interval %" PetscInt_FMT " has negative orientation (%g,%g)",i,(double)sourcex[i],(double)sourcex[i+1]); 2634194825f6SJed Brown } 2635194825f6SJed Brown for (i=0; i<ntarget; i++) { 263663a3b9bcSJacob Faibussowitsch PetscCheck(targetx[i] < targetx[i+1],PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Target interval %" PetscInt_FMT " has negative orientation (%g,%g)",i,(double)targetx[i],(double)targetx[i+1]); 2637194825f6SJed Brown } 263876bd3646SJed Brown } 2639194825f6SJed Brown xmin = PetscMin(sourcex[0],targetx[0]); 2640194825f6SJed Brown xmax = PetscMax(sourcex[nsource],targetx[ntarget]); 2641194825f6SJed Brown center = (xmin + xmax)/2; 2642194825f6SJed Brown hscale = (xmax - xmin)/2; 2643194825f6SJed Brown worksize = nsource; 26449566063dSJacob Faibussowitsch PetscCall(PetscMalloc4(degree+1,&bdegrees,nsource+1,&sourcey,nsource*(degree+1),&Bsource,worksize,&work)); 26459566063dSJacob Faibussowitsch PetscCall(PetscMalloc4(nsource,&tau,nsource*(degree+1),&Bsinv,ntarget+1,&targety,ntarget*(degree+1),&Btarget)); 2646194825f6SJed Brown for (i=0; i<=nsource; i++) sourcey[i] = (sourcex[i]-center)/hscale; 2647194825f6SJed Brown for (i=0; i<=degree; i++) bdegrees[i] = i+1; 26489566063dSJacob Faibussowitsch PetscCall(PetscDTLegendreIntegrate(nsource,sourcey,degree+1,bdegrees,PETSC_TRUE,Bsource)); 26499566063dSJacob Faibussowitsch PetscCall(PetscDTPseudoInverseQR(nsource,nsource,degree+1,Bsource,Bsinv,tau,nsource,work)); 2650194825f6SJed Brown for (i=0; i<=ntarget; i++) targety[i] = (targetx[i]-center)/hscale; 26519566063dSJacob Faibussowitsch PetscCall(PetscDTLegendreIntegrate(ntarget,targety,degree+1,bdegrees,PETSC_FALSE,Btarget)); 2652194825f6SJed Brown for (i=0; i<ntarget; i++) { 2653194825f6SJed Brown PetscReal rowsum = 0; 2654194825f6SJed Brown for (j=0; j<nsource; j++) { 2655194825f6SJed Brown PetscReal sum = 0; 2656194825f6SJed Brown for (k=0; k<degree+1; k++) { 2657194825f6SJed Brown sum += Btarget[i*(degree+1)+k] * Bsinv[k*nsource+j]; 2658194825f6SJed Brown } 2659194825f6SJed Brown R[i*nsource+j] = sum; 2660194825f6SJed Brown rowsum += sum; 2661194825f6SJed Brown } 2662194825f6SJed Brown for (j=0; j<nsource; j++) R[i*nsource+j] /= rowsum; /* normalize each row */ 2663194825f6SJed Brown } 26649566063dSJacob Faibussowitsch PetscCall(PetscFree4(bdegrees,sourcey,Bsource,work)); 26659566063dSJacob Faibussowitsch PetscCall(PetscFree4(tau,Bsinv,targety,Btarget)); 2666194825f6SJed Brown PetscFunctionReturn(0); 2667194825f6SJed Brown } 2668916e780bShannah_mairs 2669916e780bShannah_mairs /*@C 2670916e780bShannah_mairs PetscGaussLobattoLegendreIntegrate - Compute the L2 integral of a function on the GLL points 2671916e780bShannah_mairs 2672916e780bShannah_mairs Not Collective 2673916e780bShannah_mairs 2674d8d19677SJose E. Roman Input Parameters: 2675916e780bShannah_mairs + n - the number of GLL nodes 2676916e780bShannah_mairs . nodes - the GLL nodes 2677916e780bShannah_mairs . weights - the GLL weights 2678f0fc11ceSJed Brown - f - the function values at the nodes 2679916e780bShannah_mairs 2680916e780bShannah_mairs Output Parameter: 2681916e780bShannah_mairs . in - the value of the integral 2682916e780bShannah_mairs 2683916e780bShannah_mairs Level: beginner 2684916e780bShannah_mairs 2685db781477SPatrick Sanan .seealso: `PetscDTGaussLobattoLegendreQuadrature()` 2686916e780bShannah_mairs 2687916e780bShannah_mairs @*/ 2688916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreIntegrate(PetscInt n,PetscReal *nodes,PetscReal *weights,const PetscReal *f,PetscReal *in) 2689916e780bShannah_mairs { 2690916e780bShannah_mairs PetscInt i; 2691916e780bShannah_mairs 2692916e780bShannah_mairs PetscFunctionBegin; 2693916e780bShannah_mairs *in = 0.; 2694916e780bShannah_mairs for (i=0; i<n; i++) { 2695916e780bShannah_mairs *in += f[i]*f[i]*weights[i]; 2696916e780bShannah_mairs } 2697916e780bShannah_mairs PetscFunctionReturn(0); 2698916e780bShannah_mairs } 2699916e780bShannah_mairs 2700916e780bShannah_mairs /*@C 2701916e780bShannah_mairs PetscGaussLobattoLegendreElementLaplacianCreate - computes the Laplacian for a single 1d GLL element 2702916e780bShannah_mairs 2703916e780bShannah_mairs Not Collective 2704916e780bShannah_mairs 2705d8d19677SJose E. Roman Input Parameters: 2706916e780bShannah_mairs + n - the number of GLL nodes 2707916e780bShannah_mairs . nodes - the GLL nodes 2708f0fc11ceSJed Brown - weights - the GLL weights 2709916e780bShannah_mairs 2710916e780bShannah_mairs Output Parameter: 2711916e780bShannah_mairs . A - the stiffness element 2712916e780bShannah_mairs 2713916e780bShannah_mairs Level: beginner 2714916e780bShannah_mairs 2715916e780bShannah_mairs Notes: 2716916e780bShannah_mairs Destroy this with PetscGaussLobattoLegendreElementLaplacianDestroy() 2717916e780bShannah_mairs 2718916e780bShannah_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) 2719916e780bShannah_mairs 2720db781477SPatrick Sanan .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementLaplacianDestroy()` 2721916e780bShannah_mairs 2722916e780bShannah_mairs @*/ 2723916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementLaplacianCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2724916e780bShannah_mairs { 2725916e780bShannah_mairs PetscReal **A; 2726916e780bShannah_mairs const PetscReal *gllnodes = nodes; 2727916e780bShannah_mairs const PetscInt p = n-1; 2728916e780bShannah_mairs PetscReal z0,z1,z2 = -1,x,Lpj,Lpr; 2729916e780bShannah_mairs PetscInt i,j,nn,r; 2730916e780bShannah_mairs 2731916e780bShannah_mairs PetscFunctionBegin; 27329566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n,&A)); 27339566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n*n,&A[0])); 2734916e780bShannah_mairs for (i=1; i<n; i++) A[i] = A[i-1]+n; 2735916e780bShannah_mairs 2736916e780bShannah_mairs for (j=1; j<p; j++) { 2737916e780bShannah_mairs x = gllnodes[j]; 2738916e780bShannah_mairs z0 = 1.; 2739916e780bShannah_mairs z1 = x; 2740916e780bShannah_mairs for (nn=1; nn<p; nn++) { 2741916e780bShannah_mairs z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.)); 2742916e780bShannah_mairs z0 = z1; 2743916e780bShannah_mairs z1 = z2; 2744916e780bShannah_mairs } 2745916e780bShannah_mairs Lpj=z2; 2746916e780bShannah_mairs for (r=1; r<p; r++) { 2747916e780bShannah_mairs if (r == j) { 2748916e780bShannah_mairs A[j][j]=2./(3.*(1.-gllnodes[j]*gllnodes[j])*Lpj*Lpj); 2749916e780bShannah_mairs } else { 2750916e780bShannah_mairs x = gllnodes[r]; 2751916e780bShannah_mairs z0 = 1.; 2752916e780bShannah_mairs z1 = x; 2753916e780bShannah_mairs for (nn=1; nn<p; nn++) { 2754916e780bShannah_mairs z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.)); 2755916e780bShannah_mairs z0 = z1; 2756916e780bShannah_mairs z1 = z2; 2757916e780bShannah_mairs } 2758916e780bShannah_mairs Lpr = z2; 2759916e780bShannah_mairs A[r][j] = 4./(((PetscReal)p)*(((PetscReal)p)+1.)*Lpj*Lpr*(gllnodes[j]-gllnodes[r])*(gllnodes[j]-gllnodes[r])); 2760916e780bShannah_mairs } 2761916e780bShannah_mairs } 2762916e780bShannah_mairs } 2763916e780bShannah_mairs for (j=1; j<p+1; j++) { 2764916e780bShannah_mairs x = gllnodes[j]; 2765916e780bShannah_mairs z0 = 1.; 2766916e780bShannah_mairs z1 = x; 2767916e780bShannah_mairs for (nn=1; nn<p; nn++) { 2768916e780bShannah_mairs z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.)); 2769916e780bShannah_mairs z0 = z1; 2770916e780bShannah_mairs z1 = z2; 2771916e780bShannah_mairs } 2772916e780bShannah_mairs Lpj = z2; 2773916e780bShannah_mairs A[j][0] = 4.*PetscPowRealInt(-1.,p)/(((PetscReal)p)*(((PetscReal)p)+1.)*Lpj*(1.+gllnodes[j])*(1.+gllnodes[j])); 2774916e780bShannah_mairs A[0][j] = A[j][0]; 2775916e780bShannah_mairs } 2776916e780bShannah_mairs for (j=0; j<p; j++) { 2777916e780bShannah_mairs x = gllnodes[j]; 2778916e780bShannah_mairs z0 = 1.; 2779916e780bShannah_mairs z1 = x; 2780916e780bShannah_mairs for (nn=1; nn<p; nn++) { 2781916e780bShannah_mairs z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.)); 2782916e780bShannah_mairs z0 = z1; 2783916e780bShannah_mairs z1 = z2; 2784916e780bShannah_mairs } 2785916e780bShannah_mairs Lpj=z2; 2786916e780bShannah_mairs 2787916e780bShannah_mairs A[p][j] = 4./(((PetscReal)p)*(((PetscReal)p)+1.)*Lpj*(1.-gllnodes[j])*(1.-gllnodes[j])); 2788916e780bShannah_mairs A[j][p] = A[p][j]; 2789916e780bShannah_mairs } 2790916e780bShannah_mairs A[0][0]=0.5+(((PetscReal)p)*(((PetscReal)p)+1.)-2.)/6.; 2791916e780bShannah_mairs A[p][p]=A[0][0]; 2792916e780bShannah_mairs *AA = A; 2793916e780bShannah_mairs PetscFunctionReturn(0); 2794916e780bShannah_mairs } 2795916e780bShannah_mairs 2796916e780bShannah_mairs /*@C 2797916e780bShannah_mairs PetscGaussLobattoLegendreElementLaplacianDestroy - frees the Laplacian for a single 1d GLL element 2798916e780bShannah_mairs 2799916e780bShannah_mairs Not Collective 2800916e780bShannah_mairs 2801d8d19677SJose E. Roman Input Parameters: 2802916e780bShannah_mairs + n - the number of GLL nodes 2803916e780bShannah_mairs . nodes - the GLL nodes 2804916e780bShannah_mairs . weights - the GLL weightss 2805916e780bShannah_mairs - A - the stiffness element 2806916e780bShannah_mairs 2807916e780bShannah_mairs Level: beginner 2808916e780bShannah_mairs 2809db781477SPatrick Sanan .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementLaplacianCreate()` 2810916e780bShannah_mairs 2811916e780bShannah_mairs @*/ 2812916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementLaplacianDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2813916e780bShannah_mairs { 2814916e780bShannah_mairs PetscFunctionBegin; 28159566063dSJacob Faibussowitsch PetscCall(PetscFree((*AA)[0])); 28169566063dSJacob Faibussowitsch PetscCall(PetscFree(*AA)); 2817916e780bShannah_mairs *AA = NULL; 2818916e780bShannah_mairs PetscFunctionReturn(0); 2819916e780bShannah_mairs } 2820916e780bShannah_mairs 2821916e780bShannah_mairs /*@C 2822916e780bShannah_mairs PetscGaussLobattoLegendreElementGradientCreate - computes the gradient for a single 1d GLL element 2823916e780bShannah_mairs 2824916e780bShannah_mairs Not Collective 2825916e780bShannah_mairs 2826916e780bShannah_mairs Input Parameter: 2827916e780bShannah_mairs + n - the number of GLL nodes 2828916e780bShannah_mairs . nodes - the GLL nodes 2829916e780bShannah_mairs . weights - the GLL weights 2830916e780bShannah_mairs 2831d8d19677SJose E. Roman Output Parameters: 2832916e780bShannah_mairs . AA - the stiffness element 2833916e780bShannah_mairs - AAT - the transpose of AA (pass in NULL if you do not need this array) 2834916e780bShannah_mairs 2835916e780bShannah_mairs Level: beginner 2836916e780bShannah_mairs 2837916e780bShannah_mairs Notes: 2838916e780bShannah_mairs Destroy this with PetscGaussLobattoLegendreElementGradientDestroy() 2839916e780bShannah_mairs 2840916e780bShannah_mairs You can access entries in these arrays with AA[i][j] but in memory it is stored in contiguous memory, row oriented 2841916e780bShannah_mairs 2842db781477SPatrick Sanan .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementLaplacianDestroy()` 2843916e780bShannah_mairs 2844916e780bShannah_mairs @*/ 2845916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementGradientCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA,PetscReal ***AAT) 2846916e780bShannah_mairs { 2847916e780bShannah_mairs PetscReal **A, **AT = NULL; 2848916e780bShannah_mairs const PetscReal *gllnodes = nodes; 2849916e780bShannah_mairs const PetscInt p = n-1; 2850e6a796c3SToby Isaac PetscReal Li, Lj,d0; 2851916e780bShannah_mairs PetscInt i,j; 2852916e780bShannah_mairs 2853916e780bShannah_mairs PetscFunctionBegin; 28549566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n,&A)); 28559566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n*n,&A[0])); 2856916e780bShannah_mairs for (i=1; i<n; i++) A[i] = A[i-1]+n; 2857916e780bShannah_mairs 2858916e780bShannah_mairs if (AAT) { 28599566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n,&AT)); 28609566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n*n,&AT[0])); 2861916e780bShannah_mairs for (i=1; i<n; i++) AT[i] = AT[i-1]+n; 2862916e780bShannah_mairs } 2863916e780bShannah_mairs 2864916e780bShannah_mairs if (n==1) {A[0][0] = 0.;} 2865916e780bShannah_mairs d0 = (PetscReal)p*((PetscReal)p+1.)/4.; 2866916e780bShannah_mairs for (i=0; i<n; i++) { 2867916e780bShannah_mairs for (j=0; j<n; j++) { 2868916e780bShannah_mairs A[i][j] = 0.; 28699566063dSJacob Faibussowitsch PetscCall(PetscDTComputeJacobi(0., 0., p, gllnodes[i], &Li)); 28709566063dSJacob Faibussowitsch PetscCall(PetscDTComputeJacobi(0., 0., p, gllnodes[j], &Lj)); 2871916e780bShannah_mairs if (i!=j) A[i][j] = Li/(Lj*(gllnodes[i]-gllnodes[j])); 2872916e780bShannah_mairs if ((j==i) && (i==0)) A[i][j] = -d0; 2873916e780bShannah_mairs if (j==i && i==p) A[i][j] = d0; 2874916e780bShannah_mairs if (AT) AT[j][i] = A[i][j]; 2875916e780bShannah_mairs } 2876916e780bShannah_mairs } 2877916e780bShannah_mairs if (AAT) *AAT = AT; 2878916e780bShannah_mairs *AA = A; 2879916e780bShannah_mairs PetscFunctionReturn(0); 2880916e780bShannah_mairs } 2881916e780bShannah_mairs 2882916e780bShannah_mairs /*@C 2883916e780bShannah_mairs PetscGaussLobattoLegendreElementGradientDestroy - frees the gradient for a single 1d GLL element obtained with PetscGaussLobattoLegendreElementGradientCreate() 2884916e780bShannah_mairs 2885916e780bShannah_mairs Not Collective 2886916e780bShannah_mairs 2887d8d19677SJose E. Roman Input Parameters: 2888916e780bShannah_mairs + n - the number of GLL nodes 2889916e780bShannah_mairs . nodes - the GLL nodes 2890916e780bShannah_mairs . weights - the GLL weights 2891916e780bShannah_mairs . AA - the stiffness element 2892916e780bShannah_mairs - AAT - the transpose of the element 2893916e780bShannah_mairs 2894916e780bShannah_mairs Level: beginner 2895916e780bShannah_mairs 2896db781477SPatrick Sanan .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementLaplacianCreate()`, `PetscGaussLobattoLegendreElementAdvectionCreate()` 2897916e780bShannah_mairs 2898916e780bShannah_mairs @*/ 2899916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementGradientDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA,PetscReal ***AAT) 2900916e780bShannah_mairs { 2901916e780bShannah_mairs PetscFunctionBegin; 29029566063dSJacob Faibussowitsch PetscCall(PetscFree((*AA)[0])); 29039566063dSJacob Faibussowitsch PetscCall(PetscFree(*AA)); 2904916e780bShannah_mairs *AA = NULL; 2905916e780bShannah_mairs if (*AAT) { 29069566063dSJacob Faibussowitsch PetscCall(PetscFree((*AAT)[0])); 29079566063dSJacob Faibussowitsch PetscCall(PetscFree(*AAT)); 2908916e780bShannah_mairs *AAT = NULL; 2909916e780bShannah_mairs } 2910916e780bShannah_mairs PetscFunctionReturn(0); 2911916e780bShannah_mairs } 2912916e780bShannah_mairs 2913916e780bShannah_mairs /*@C 2914916e780bShannah_mairs PetscGaussLobattoLegendreElementAdvectionCreate - computes the advection operator for a single 1d GLL element 2915916e780bShannah_mairs 2916916e780bShannah_mairs Not Collective 2917916e780bShannah_mairs 2918d8d19677SJose E. Roman Input Parameters: 2919916e780bShannah_mairs + n - the number of GLL nodes 2920916e780bShannah_mairs . nodes - the GLL nodes 2921f0fc11ceSJed Brown - weights - the GLL weightss 2922916e780bShannah_mairs 2923916e780bShannah_mairs Output Parameter: 2924916e780bShannah_mairs . AA - the stiffness element 2925916e780bShannah_mairs 2926916e780bShannah_mairs Level: beginner 2927916e780bShannah_mairs 2928916e780bShannah_mairs Notes: 2929916e780bShannah_mairs Destroy this with PetscGaussLobattoLegendreElementAdvectionDestroy() 2930916e780bShannah_mairs 2931916e780bShannah_mairs This is the same as the Gradient operator multiplied by the diagonal mass matrix 2932916e780bShannah_mairs 2933916e780bShannah_mairs You can access entries in this array with AA[i][j] but in memory it is stored in contiguous memory, row oriented 2934916e780bShannah_mairs 2935db781477SPatrick Sanan .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementLaplacianCreate()`, `PetscGaussLobattoLegendreElementAdvectionDestroy()` 2936916e780bShannah_mairs 2937916e780bShannah_mairs @*/ 2938916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementAdvectionCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2939916e780bShannah_mairs { 2940916e780bShannah_mairs PetscReal **D; 2941916e780bShannah_mairs const PetscReal *gllweights = weights; 2942916e780bShannah_mairs const PetscInt glln = n; 2943916e780bShannah_mairs PetscInt i,j; 2944916e780bShannah_mairs 2945916e780bShannah_mairs PetscFunctionBegin; 29469566063dSJacob Faibussowitsch PetscCall(PetscGaussLobattoLegendreElementGradientCreate(n,nodes,weights,&D,NULL)); 2947916e780bShannah_mairs for (i=0; i<glln; i++) { 2948916e780bShannah_mairs for (j=0; j<glln; j++) { 2949916e780bShannah_mairs D[i][j] = gllweights[i]*D[i][j]; 2950916e780bShannah_mairs } 2951916e780bShannah_mairs } 2952916e780bShannah_mairs *AA = D; 2953916e780bShannah_mairs PetscFunctionReturn(0); 2954916e780bShannah_mairs } 2955916e780bShannah_mairs 2956916e780bShannah_mairs /*@C 2957916e780bShannah_mairs PetscGaussLobattoLegendreElementAdvectionDestroy - frees the advection stiffness for a single 1d GLL element 2958916e780bShannah_mairs 2959916e780bShannah_mairs Not Collective 2960916e780bShannah_mairs 2961d8d19677SJose E. Roman Input Parameters: 2962916e780bShannah_mairs + n - the number of GLL nodes 2963916e780bShannah_mairs . nodes - the GLL nodes 2964916e780bShannah_mairs . weights - the GLL weights 2965916e780bShannah_mairs - A - advection 2966916e780bShannah_mairs 2967916e780bShannah_mairs Level: beginner 2968916e780bShannah_mairs 2969db781477SPatrick Sanan .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementAdvectionCreate()` 2970916e780bShannah_mairs 2971916e780bShannah_mairs @*/ 2972916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementAdvectionDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2973916e780bShannah_mairs { 2974916e780bShannah_mairs PetscFunctionBegin; 29759566063dSJacob Faibussowitsch PetscCall(PetscFree((*AA)[0])); 29769566063dSJacob Faibussowitsch PetscCall(PetscFree(*AA)); 2977916e780bShannah_mairs *AA = NULL; 2978916e780bShannah_mairs PetscFunctionReturn(0); 2979916e780bShannah_mairs } 2980916e780bShannah_mairs 2981916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementMassCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2982916e780bShannah_mairs { 2983916e780bShannah_mairs PetscReal **A; 2984916e780bShannah_mairs const PetscReal *gllweights = weights; 2985916e780bShannah_mairs const PetscInt glln = n; 2986916e780bShannah_mairs PetscInt i,j; 2987916e780bShannah_mairs 2988916e780bShannah_mairs PetscFunctionBegin; 29899566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(glln,&A)); 29909566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(glln*glln,&A[0])); 2991916e780bShannah_mairs for (i=1; i<glln; i++) A[i] = A[i-1]+glln; 2992916e780bShannah_mairs if (glln==1) {A[0][0] = 0.;} 2993916e780bShannah_mairs for (i=0; i<glln; i++) { 2994916e780bShannah_mairs for (j=0; j<glln; j++) { 2995916e780bShannah_mairs A[i][j] = 0.; 2996916e780bShannah_mairs if (j==i) A[i][j] = gllweights[i]; 2997916e780bShannah_mairs } 2998916e780bShannah_mairs } 2999916e780bShannah_mairs *AA = A; 3000916e780bShannah_mairs PetscFunctionReturn(0); 3001916e780bShannah_mairs } 3002916e780bShannah_mairs 3003916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementMassDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 3004916e780bShannah_mairs { 3005916e780bShannah_mairs PetscFunctionBegin; 30069566063dSJacob Faibussowitsch PetscCall(PetscFree((*AA)[0])); 30079566063dSJacob Faibussowitsch PetscCall(PetscFree(*AA)); 3008916e780bShannah_mairs *AA = NULL; 3009916e780bShannah_mairs PetscFunctionReturn(0); 3010916e780bShannah_mairs } 3011d4afb720SToby Isaac 3012d4afb720SToby Isaac /*@ 3013d4afb720SToby Isaac PetscDTIndexToBary - convert an index into a barycentric coordinate. 3014d4afb720SToby Isaac 3015d4afb720SToby Isaac Input Parameters: 3016d4afb720SToby 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) 3017d4afb720SToby Isaac . sum - the value that the sum of the barycentric coordinates (which will be non-negative integers) should sum to 3018d4afb720SToby Isaac - index - the index to convert: should be >= 0 and < Binomial(len - 1 + sum, sum) 3019d4afb720SToby Isaac 3020d4afb720SToby Isaac Output Parameter: 3021d4afb720SToby Isaac . coord - will be filled with the barycentric coordinate 3022d4afb720SToby Isaac 3023d4afb720SToby Isaac Level: beginner 3024d4afb720SToby Isaac 3025d4afb720SToby Isaac Note: the indices map to barycentric coordinates in lexicographic order, where the first index is the 3026d4afb720SToby Isaac least significant and the last index is the most significant. 3027d4afb720SToby Isaac 3028db781477SPatrick Sanan .seealso: `PetscDTBaryToIndex()` 3029d4afb720SToby Isaac @*/ 3030d4afb720SToby Isaac PetscErrorCode PetscDTIndexToBary(PetscInt len, PetscInt sum, PetscInt index, PetscInt coord[]) 3031d4afb720SToby Isaac { 3032d4afb720SToby Isaac PetscInt c, d, s, total, subtotal, nexttotal; 3033d4afb720SToby Isaac 3034d4afb720SToby Isaac PetscFunctionBeginHot; 303508401ef6SPierre Jolivet PetscCheck(len >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative"); 303608401ef6SPierre Jolivet PetscCheck(index >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index must be non-negative"); 3037d4afb720SToby Isaac if (!len) { 3038d4afb720SToby Isaac if (!sum && !index) PetscFunctionReturn(0); 3039d4afb720SToby Isaac SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid index or sum for length 0 barycentric coordinate"); 3040d4afb720SToby Isaac } 3041d4afb720SToby Isaac for (c = 1, total = 1; c <= len; c++) { 3042d4afb720SToby Isaac /* total is the number of ways to have a tuple of length c with sum */ 3043d4afb720SToby Isaac if (index < total) break; 3044d4afb720SToby Isaac total = (total * (sum + c)) / c; 3045d4afb720SToby Isaac } 304608401ef6SPierre Jolivet PetscCheck(c <= len,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index out of range"); 3047d4afb720SToby Isaac for (d = c; d < len; d++) coord[d] = 0; 3048d4afb720SToby Isaac for (s = 0, subtotal = 1, nexttotal = 1; c > 0;) { 3049d4afb720SToby Isaac /* subtotal is the number of ways to have a tuple of length c with sum s */ 3050d4afb720SToby Isaac /* nexttotal is the number of ways to have a tuple of length c-1 with sum s */ 3051d4afb720SToby Isaac if ((index + subtotal) >= total) { 3052d4afb720SToby Isaac coord[--c] = sum - s; 3053d4afb720SToby Isaac index -= (total - subtotal); 3054d4afb720SToby Isaac sum = s; 3055d4afb720SToby Isaac total = nexttotal; 3056d4afb720SToby Isaac subtotal = 1; 3057d4afb720SToby Isaac nexttotal = 1; 3058d4afb720SToby Isaac s = 0; 3059d4afb720SToby Isaac } else { 3060d4afb720SToby Isaac subtotal = (subtotal * (c + s)) / (s + 1); 3061d4afb720SToby Isaac nexttotal = (nexttotal * (c - 1 + s)) / (s + 1); 3062d4afb720SToby Isaac s++; 3063d4afb720SToby Isaac } 3064d4afb720SToby Isaac } 3065d4afb720SToby Isaac PetscFunctionReturn(0); 3066d4afb720SToby Isaac } 3067d4afb720SToby Isaac 3068d4afb720SToby Isaac /*@ 3069d4afb720SToby Isaac PetscDTBaryToIndex - convert a barycentric coordinate to an index 3070d4afb720SToby Isaac 3071d4afb720SToby Isaac Input Parameters: 3072d4afb720SToby 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) 3073d4afb720SToby Isaac . sum - the value that the sum of the barycentric coordinates (which will be non-negative integers) should sum to 3074d4afb720SToby Isaac - coord - a barycentric coordinate with the given length and sum 3075d4afb720SToby Isaac 3076d4afb720SToby Isaac Output Parameter: 3077d4afb720SToby Isaac . index - the unique index for the coordinate, >= 0 and < Binomial(len - 1 + sum, sum) 3078d4afb720SToby Isaac 3079d4afb720SToby Isaac Level: beginner 3080d4afb720SToby Isaac 3081d4afb720SToby Isaac Note: the indices map to barycentric coordinates in lexicographic order, where the first index is the 3082d4afb720SToby Isaac least significant and the last index is the most significant. 3083d4afb720SToby Isaac 3084db781477SPatrick Sanan .seealso: `PetscDTIndexToBary` 3085d4afb720SToby Isaac @*/ 3086d4afb720SToby Isaac PetscErrorCode PetscDTBaryToIndex(PetscInt len, PetscInt sum, const PetscInt coord[], PetscInt *index) 3087d4afb720SToby Isaac { 3088d4afb720SToby Isaac PetscInt c; 3089d4afb720SToby Isaac PetscInt i; 3090d4afb720SToby Isaac PetscInt total; 3091d4afb720SToby Isaac 3092d4afb720SToby Isaac PetscFunctionBeginHot; 309308401ef6SPierre Jolivet PetscCheck(len >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative"); 3094d4afb720SToby Isaac if (!len) { 3095d4afb720SToby Isaac if (!sum) { 3096d4afb720SToby Isaac *index = 0; 3097d4afb720SToby Isaac PetscFunctionReturn(0); 3098d4afb720SToby Isaac } 3099d4afb720SToby Isaac SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid index or sum for length 0 barycentric coordinate"); 3100d4afb720SToby Isaac } 3101d4afb720SToby Isaac for (c = 1, total = 1; c < len; c++) total = (total * (sum + c)) / c; 3102d4afb720SToby Isaac i = total - 1; 3103d4afb720SToby Isaac c = len - 1; 3104d4afb720SToby Isaac sum -= coord[c]; 3105d4afb720SToby Isaac while (sum > 0) { 3106d4afb720SToby Isaac PetscInt subtotal; 3107d4afb720SToby Isaac PetscInt s; 3108d4afb720SToby Isaac 3109d4afb720SToby Isaac for (s = 1, subtotal = 1; s < sum; s++) subtotal = (subtotal * (c + s)) / s; 3110d4afb720SToby Isaac i -= subtotal; 3111d4afb720SToby Isaac sum -= coord[--c]; 3112d4afb720SToby Isaac } 3113d4afb720SToby Isaac *index = i; 3114d4afb720SToby Isaac PetscFunctionReturn(0); 3115d4afb720SToby Isaac } 3116