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 15ea78f98cSLisandro Dalcin const char *const PetscDTNodeTypes[] = {"gaussjacobi", "equispaced", "tanhsinh", "PETSCDTNODES_", NULL}; 16d4afb720SToby Isaac 17e6a796c3SToby Isaac static PetscBool GolubWelschCite = PETSC_FALSE; 18e6a796c3SToby Isaac const char GolubWelschCitation[] = "@article{GolubWelsch1969,\n" 190bfcf5a5SMatthew G. Knepley " author = {Golub and Welsch},\n" 200bfcf5a5SMatthew G. Knepley " title = {Calculation of Quadrature Rules},\n" 210bfcf5a5SMatthew G. Knepley " journal = {Math. Comp.},\n" 220bfcf5a5SMatthew G. Knepley " volume = {23},\n" 230bfcf5a5SMatthew G. Knepley " number = {106},\n" 240bfcf5a5SMatthew G. Knepley " pages = {221--230},\n" 250bfcf5a5SMatthew G. Knepley " year = {1969}\n}\n"; 260bfcf5a5SMatthew G. Knepley 27c4762a1bSJed Brown /* Numerical tests in src/dm/dt/tests/ex1.c show that when computing the nodes and weights of Gauss-Jacobi 2894e21283SToby Isaac quadrature rules: 29e6a796c3SToby Isaac 3094e21283SToby Isaac - in double precision, Newton's method and Golub & Welsch both work for moderate degrees (< 100), 3194e21283SToby Isaac - in single precision, Newton's method starts producing incorrect roots around n = 15, but 3294e21283SToby Isaac the weights from Golub & Welsch become a problem before then: they produces errors 3394e21283SToby Isaac in computing the Jacobi-polynomial Gram matrix around n = 6. 3494e21283SToby Isaac 3594e21283SToby Isaac So we default to Newton's method (required fewer dependencies) */ 3694e21283SToby Isaac PetscBool PetscDTGaussQuadratureNewton_Internal = PETSC_TRUE; 372cd22861SMatthew G. Knepley 382cd22861SMatthew G. Knepley PetscClassId PETSCQUADRATURE_CLASSID = 0; 392cd22861SMatthew G. Knepley 4040d8ff71SMatthew G. Knepley /*@ 4140d8ff71SMatthew G. Knepley PetscQuadratureCreate - Create a PetscQuadrature object 4240d8ff71SMatthew G. Knepley 43d083f849SBarry Smith Collective 4440d8ff71SMatthew G. Knepley 4540d8ff71SMatthew G. Knepley Input Parameter: 4640d8ff71SMatthew G. Knepley . comm - The communicator for the PetscQuadrature object 4740d8ff71SMatthew G. Knepley 4840d8ff71SMatthew G. Knepley Output Parameter: 4940d8ff71SMatthew G. Knepley . q - The PetscQuadrature object 5040d8ff71SMatthew G. Knepley 5140d8ff71SMatthew G. Knepley Level: beginner 5240d8ff71SMatthew G. Knepley 5340d8ff71SMatthew G. Knepley .seealso: PetscQuadratureDestroy(), PetscQuadratureGetData() 5440d8ff71SMatthew G. Knepley @*/ 5521454ff5SMatthew G. Knepley PetscErrorCode PetscQuadratureCreate(MPI_Comm comm, PetscQuadrature *q) 5621454ff5SMatthew G. Knepley { 5721454ff5SMatthew G. Knepley PetscFunctionBegin; 5821454ff5SMatthew G. Knepley PetscValidPointer(q, 2); 595f80ce2aSJacob Faibussowitsch CHKERRQ(DMInitializePackage()); 605f80ce2aSJacob Faibussowitsch CHKERRQ(PetscHeaderCreate(*q,PETSCQUADRATURE_CLASSID,"PetscQuadrature","Quadrature","DT",comm,PetscQuadratureDestroy,PetscQuadratureView)); 6121454ff5SMatthew G. Knepley (*q)->dim = -1; 62a6b92713SMatthew G. Knepley (*q)->Nc = 1; 63bcede257SMatthew G. Knepley (*q)->order = -1; 6421454ff5SMatthew G. Knepley (*q)->numPoints = 0; 6521454ff5SMatthew G. Knepley (*q)->points = NULL; 6621454ff5SMatthew G. Knepley (*q)->weights = NULL; 6721454ff5SMatthew G. Knepley PetscFunctionReturn(0); 6821454ff5SMatthew G. Knepley } 6921454ff5SMatthew G. Knepley 70c9638911SMatthew G. Knepley /*@ 71c9638911SMatthew G. Knepley PetscQuadratureDuplicate - Create a deep copy of the PetscQuadrature object 72c9638911SMatthew G. Knepley 73d083f849SBarry Smith Collective on q 74c9638911SMatthew G. Knepley 75c9638911SMatthew G. Knepley Input Parameter: 76c9638911SMatthew G. Knepley . q - The PetscQuadrature object 77c9638911SMatthew G. Knepley 78c9638911SMatthew G. Knepley Output Parameter: 79c9638911SMatthew G. Knepley . r - The new PetscQuadrature object 80c9638911SMatthew G. Knepley 81c9638911SMatthew G. Knepley Level: beginner 82c9638911SMatthew G. Knepley 83c9638911SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureDestroy(), PetscQuadratureGetData() 84c9638911SMatthew G. Knepley @*/ 85c9638911SMatthew G. Knepley PetscErrorCode PetscQuadratureDuplicate(PetscQuadrature q, PetscQuadrature *r) 86c9638911SMatthew G. Knepley { 87a6b92713SMatthew G. Knepley PetscInt order, dim, Nc, Nq; 88c9638911SMatthew G. Knepley const PetscReal *points, *weights; 89c9638911SMatthew G. Knepley PetscReal *p, *w; 90c9638911SMatthew G. Knepley 91c9638911SMatthew G. Knepley PetscFunctionBegin; 92064a246eSJacob Faibussowitsch PetscValidPointer(q, 1); 935f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureCreate(PetscObjectComm((PetscObject) q), r)); 945f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureGetOrder(q, &order)); 955f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureSetOrder(*r, order)); 965f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureGetData(q, &dim, &Nc, &Nq, &points, &weights)); 975f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(Nq*dim, &p)); 985f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(Nq*Nc, &w)); 995f80ce2aSJacob Faibussowitsch CHKERRQ(PetscArraycpy(p, points, Nq*dim)); 1005f80ce2aSJacob Faibussowitsch CHKERRQ(PetscArraycpy(w, weights, Nc * Nq)); 1015f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureSetData(*r, dim, Nc, Nq, p, w)); 102c9638911SMatthew G. Knepley PetscFunctionReturn(0); 103c9638911SMatthew G. Knepley } 104c9638911SMatthew G. Knepley 10540d8ff71SMatthew G. Knepley /*@ 10640d8ff71SMatthew G. Knepley PetscQuadratureDestroy - Destroys a PetscQuadrature object 10740d8ff71SMatthew G. Knepley 108d083f849SBarry Smith Collective on q 10940d8ff71SMatthew G. Knepley 11040d8ff71SMatthew G. Knepley Input Parameter: 11140d8ff71SMatthew G. Knepley . q - The PetscQuadrature object 11240d8ff71SMatthew G. Knepley 11340d8ff71SMatthew G. Knepley Level: beginner 11440d8ff71SMatthew G. Knepley 11540d8ff71SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureGetData() 11640d8ff71SMatthew G. Knepley @*/ 117bfa639d9SMatthew G. Knepley PetscErrorCode PetscQuadratureDestroy(PetscQuadrature *q) 118bfa639d9SMatthew G. Knepley { 119bfa639d9SMatthew G. Knepley PetscFunctionBegin; 12021454ff5SMatthew G. Knepley if (!*q) PetscFunctionReturn(0); 1212cd22861SMatthew G. Knepley PetscValidHeaderSpecific((*q),PETSCQUADRATURE_CLASSID,1); 12221454ff5SMatthew G. Knepley if (--((PetscObject)(*q))->refct > 0) { 12321454ff5SMatthew G. Knepley *q = NULL; 12421454ff5SMatthew G. Knepley PetscFunctionReturn(0); 12521454ff5SMatthew G. Knepley } 1265f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree((*q)->points)); 1275f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree((*q)->weights)); 1285f80ce2aSJacob Faibussowitsch CHKERRQ(PetscHeaderDestroy(q)); 12921454ff5SMatthew G. Knepley PetscFunctionReturn(0); 13021454ff5SMatthew G. Knepley } 13121454ff5SMatthew G. Knepley 132bcede257SMatthew G. Knepley /*@ 133a6b92713SMatthew G. Knepley PetscQuadratureGetOrder - Return the order of the method 134bcede257SMatthew G. Knepley 135bcede257SMatthew G. Knepley Not collective 136bcede257SMatthew G. Knepley 137bcede257SMatthew G. Knepley Input Parameter: 138bcede257SMatthew G. Knepley . q - The PetscQuadrature object 139bcede257SMatthew G. Knepley 140bcede257SMatthew G. Knepley Output Parameter: 141bcede257SMatthew G. Knepley . order - The order of the quadrature, i.e. the highest degree polynomial that is exactly integrated 142bcede257SMatthew G. Knepley 143bcede257SMatthew G. Knepley Level: intermediate 144bcede257SMatthew G. Knepley 145bcede257SMatthew G. Knepley .seealso: PetscQuadratureSetOrder(), PetscQuadratureGetData(), PetscQuadratureSetData() 146bcede257SMatthew G. Knepley @*/ 147bcede257SMatthew G. Knepley PetscErrorCode PetscQuadratureGetOrder(PetscQuadrature q, PetscInt *order) 148bcede257SMatthew G. Knepley { 149bcede257SMatthew G. Knepley PetscFunctionBegin; 1502cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 151bcede257SMatthew G. Knepley PetscValidPointer(order, 2); 152bcede257SMatthew G. Knepley *order = q->order; 153bcede257SMatthew G. Knepley PetscFunctionReturn(0); 154bcede257SMatthew G. Knepley } 155bcede257SMatthew G. Knepley 156bcede257SMatthew G. Knepley /*@ 157a6b92713SMatthew G. Knepley PetscQuadratureSetOrder - Return the order of the method 158bcede257SMatthew G. Knepley 159bcede257SMatthew G. Knepley Not collective 160bcede257SMatthew G. Knepley 161bcede257SMatthew G. Knepley Input Parameters: 162bcede257SMatthew G. Knepley + q - The PetscQuadrature object 163bcede257SMatthew G. Knepley - order - The order of the quadrature, i.e. the highest degree polynomial that is exactly integrated 164bcede257SMatthew G. Knepley 165bcede257SMatthew G. Knepley Level: intermediate 166bcede257SMatthew G. Knepley 167bcede257SMatthew G. Knepley .seealso: PetscQuadratureGetOrder(), PetscQuadratureGetData(), PetscQuadratureSetData() 168bcede257SMatthew G. Knepley @*/ 169bcede257SMatthew G. Knepley PetscErrorCode PetscQuadratureSetOrder(PetscQuadrature q, PetscInt order) 170bcede257SMatthew G. Knepley { 171bcede257SMatthew G. Knepley PetscFunctionBegin; 1722cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 173bcede257SMatthew G. Knepley q->order = order; 174bcede257SMatthew G. Knepley PetscFunctionReturn(0); 175bcede257SMatthew G. Knepley } 176bcede257SMatthew G. Knepley 177a6b92713SMatthew G. Knepley /*@ 178a6b92713SMatthew G. Knepley PetscQuadratureGetNumComponents - Return the number of components for functions to be integrated 179a6b92713SMatthew G. Knepley 180a6b92713SMatthew G. Knepley Not collective 181a6b92713SMatthew G. Knepley 182a6b92713SMatthew G. Knepley Input Parameter: 183a6b92713SMatthew G. Knepley . q - The PetscQuadrature object 184a6b92713SMatthew G. Knepley 185a6b92713SMatthew G. Knepley Output Parameter: 186a6b92713SMatthew G. Knepley . Nc - The number of components 187a6b92713SMatthew G. Knepley 188a6b92713SMatthew G. Knepley Note: We are performing an integral int f(x) . w(x) dx, where both f and w (the weight) have Nc components. 189a6b92713SMatthew G. Knepley 190a6b92713SMatthew G. Knepley Level: intermediate 191a6b92713SMatthew G. Knepley 192a6b92713SMatthew G. Knepley .seealso: PetscQuadratureSetNumComponents(), PetscQuadratureGetData(), PetscQuadratureSetData() 193a6b92713SMatthew G. Knepley @*/ 194a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureGetNumComponents(PetscQuadrature q, PetscInt *Nc) 195a6b92713SMatthew G. Knepley { 196a6b92713SMatthew G. Knepley PetscFunctionBegin; 1972cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 198a6b92713SMatthew G. Knepley PetscValidPointer(Nc, 2); 199a6b92713SMatthew G. Knepley *Nc = q->Nc; 200a6b92713SMatthew G. Knepley PetscFunctionReturn(0); 201a6b92713SMatthew G. Knepley } 202a6b92713SMatthew G. Knepley 203a6b92713SMatthew G. Knepley /*@ 204a6b92713SMatthew G. Knepley PetscQuadratureSetNumComponents - Return the number of components for functions to be integrated 205a6b92713SMatthew G. Knepley 206a6b92713SMatthew G. Knepley Not collective 207a6b92713SMatthew G. Knepley 208a6b92713SMatthew G. Knepley Input Parameters: 209a6b92713SMatthew G. Knepley + q - The PetscQuadrature object 210a6b92713SMatthew G. Knepley - Nc - The number of components 211a6b92713SMatthew G. Knepley 212a6b92713SMatthew G. Knepley Note: We are performing an integral int f(x) . w(x) dx, where both f and w (the weight) have Nc components. 213a6b92713SMatthew G. Knepley 214a6b92713SMatthew G. Knepley Level: intermediate 215a6b92713SMatthew G. Knepley 216a6b92713SMatthew G. Knepley .seealso: PetscQuadratureGetNumComponents(), PetscQuadratureGetData(), PetscQuadratureSetData() 217a6b92713SMatthew G. Knepley @*/ 218a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureSetNumComponents(PetscQuadrature q, PetscInt Nc) 219a6b92713SMatthew G. Knepley { 220a6b92713SMatthew G. Knepley PetscFunctionBegin; 2212cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 222a6b92713SMatthew G. Knepley q->Nc = Nc; 223a6b92713SMatthew G. Knepley PetscFunctionReturn(0); 224a6b92713SMatthew G. Knepley } 225a6b92713SMatthew G. Knepley 22640d8ff71SMatthew G. Knepley /*@C 22740d8ff71SMatthew G. Knepley PetscQuadratureGetData - Returns the data defining the quadrature 22840d8ff71SMatthew G. Knepley 22940d8ff71SMatthew G. Knepley Not collective 23040d8ff71SMatthew G. Knepley 23140d8ff71SMatthew G. Knepley Input Parameter: 23240d8ff71SMatthew G. Knepley . q - The PetscQuadrature object 23340d8ff71SMatthew G. Knepley 23440d8ff71SMatthew G. Knepley Output Parameters: 23540d8ff71SMatthew G. Knepley + dim - The spatial dimension 236805e7170SToby Isaac . Nc - The number of components 23740d8ff71SMatthew G. Knepley . npoints - The number of quadrature points 23840d8ff71SMatthew G. Knepley . points - The coordinates of each quadrature point 23940d8ff71SMatthew G. Knepley - weights - The weight of each quadrature point 24040d8ff71SMatthew G. Knepley 24140d8ff71SMatthew G. Knepley Level: intermediate 24240d8ff71SMatthew G. Knepley 24395452b02SPatrick Sanan Fortran Notes: 24495452b02SPatrick Sanan From Fortran you must call PetscQuadratureRestoreData() when you are done with the data 2451fd49c25SBarry Smith 24640d8ff71SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureSetData() 24740d8ff71SMatthew G. Knepley @*/ 248a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureGetData(PetscQuadrature q, PetscInt *dim, PetscInt *Nc, PetscInt *npoints, const PetscReal *points[], const PetscReal *weights[]) 24921454ff5SMatthew G. Knepley { 25021454ff5SMatthew G. Knepley PetscFunctionBegin; 2512cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 25221454ff5SMatthew G. Knepley if (dim) { 25321454ff5SMatthew G. Knepley PetscValidPointer(dim, 2); 25421454ff5SMatthew G. Knepley *dim = q->dim; 25521454ff5SMatthew G. Knepley } 256a6b92713SMatthew G. Knepley if (Nc) { 257a6b92713SMatthew G. Knepley PetscValidPointer(Nc, 3); 258a6b92713SMatthew G. Knepley *Nc = q->Nc; 259a6b92713SMatthew G. Knepley } 26021454ff5SMatthew G. Knepley if (npoints) { 261a6b92713SMatthew G. Knepley PetscValidPointer(npoints, 4); 26221454ff5SMatthew G. Knepley *npoints = q->numPoints; 26321454ff5SMatthew G. Knepley } 26421454ff5SMatthew G. Knepley if (points) { 265a6b92713SMatthew G. Knepley PetscValidPointer(points, 5); 26621454ff5SMatthew G. Knepley *points = q->points; 26721454ff5SMatthew G. Knepley } 26821454ff5SMatthew G. Knepley if (weights) { 269a6b92713SMatthew G. Knepley PetscValidPointer(weights, 6); 27021454ff5SMatthew G. Knepley *weights = q->weights; 27121454ff5SMatthew G. Knepley } 27221454ff5SMatthew G. Knepley PetscFunctionReturn(0); 27321454ff5SMatthew G. Knepley } 27421454ff5SMatthew G. Knepley 2754f9ab2b4SJed Brown /*@ 2764f9ab2b4SJed Brown PetscQuadratureEqual - determine whether two quadratures are equivalent 2774f9ab2b4SJed Brown 2784f9ab2b4SJed Brown Input Parameters: 2794f9ab2b4SJed Brown + A - A PetscQuadrature object 2804f9ab2b4SJed Brown - B - Another PetscQuadrature object 2814f9ab2b4SJed Brown 2824f9ab2b4SJed Brown Output Parameters: 2834f9ab2b4SJed Brown . equal - PETSC_TRUE if the quadratures are the same 2844f9ab2b4SJed Brown 2854f9ab2b4SJed Brown Level: intermediate 2864f9ab2b4SJed Brown 2874f9ab2b4SJed Brown .seealso: PetscQuadratureCreate() 2884f9ab2b4SJed Brown @*/ 2894f9ab2b4SJed Brown PetscErrorCode PetscQuadratureEqual(PetscQuadrature A, PetscQuadrature B, PetscBool *equal) 2904f9ab2b4SJed Brown { 2914f9ab2b4SJed Brown PetscFunctionBegin; 2924f9ab2b4SJed Brown PetscValidHeaderSpecific(A, PETSCQUADRATURE_CLASSID, 1); 2934f9ab2b4SJed Brown PetscValidHeaderSpecific(B, PETSCQUADRATURE_CLASSID, 2); 2944f9ab2b4SJed Brown PetscValidBoolPointer(equal, 3); 2954f9ab2b4SJed Brown *equal = PETSC_FALSE; 2964f9ab2b4SJed Brown if (A->dim != B->dim || A->Nc != B->Nc || A->order != B->order || A->numPoints != B->numPoints) { 2974f9ab2b4SJed Brown PetscFunctionReturn(0); 2984f9ab2b4SJed Brown } 2994f9ab2b4SJed Brown for (PetscInt i=0; i<A->numPoints*A->dim; i++) { 3004f9ab2b4SJed Brown if (PetscAbsReal(A->points[i] - B->points[i]) > PETSC_SMALL) { 3014f9ab2b4SJed Brown PetscFunctionReturn(0); 3024f9ab2b4SJed Brown } 3034f9ab2b4SJed Brown } 3044f9ab2b4SJed Brown if (!A->weights && !B->weights) { 3054f9ab2b4SJed Brown *equal = PETSC_TRUE; 3064f9ab2b4SJed Brown PetscFunctionReturn(0); 3074f9ab2b4SJed Brown } 3084f9ab2b4SJed Brown if (A->weights && B->weights) { 3094f9ab2b4SJed Brown for (PetscInt i=0; i<A->numPoints; i++) { 3104f9ab2b4SJed Brown if (PetscAbsReal(A->weights[i] - B->weights[i]) > PETSC_SMALL) { 3114f9ab2b4SJed Brown PetscFunctionReturn(0); 3124f9ab2b4SJed Brown } 3134f9ab2b4SJed Brown } 3144f9ab2b4SJed Brown *equal = PETSC_TRUE; 3154f9ab2b4SJed Brown } 3164f9ab2b4SJed Brown PetscFunctionReturn(0); 3174f9ab2b4SJed Brown } 3184f9ab2b4SJed Brown 319907761f8SToby Isaac static PetscErrorCode PetscDTJacobianInverse_Internal(PetscInt m, PetscInt n, const PetscReal J[], PetscReal Jinv[]) 320907761f8SToby Isaac { 321907761f8SToby Isaac PetscScalar *Js, *Jinvs; 322907761f8SToby Isaac PetscInt i, j, k; 323907761f8SToby Isaac PetscBLASInt bm, bn, info; 324907761f8SToby Isaac 325907761f8SToby Isaac PetscFunctionBegin; 326d4afb720SToby Isaac if (!m || !n) PetscFunctionReturn(0); 3275f80ce2aSJacob Faibussowitsch CHKERRQ(PetscBLASIntCast(m, &bm)); 3285f80ce2aSJacob Faibussowitsch CHKERRQ(PetscBLASIntCast(n, &bn)); 329907761f8SToby Isaac #if defined(PETSC_USE_COMPLEX) 3305f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc2(m*n, &Js, m*n, &Jinvs)); 33128222859SToby Isaac for (i = 0; i < m*n; i++) Js[i] = J[i]; 332907761f8SToby Isaac #else 333907761f8SToby Isaac Js = (PetscReal *) J; 334907761f8SToby Isaac Jinvs = Jinv; 335907761f8SToby Isaac #endif 336907761f8SToby Isaac if (m == n) { 337907761f8SToby Isaac PetscBLASInt *pivots; 338907761f8SToby Isaac PetscScalar *W; 339907761f8SToby Isaac 3405f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc2(m, &pivots, m, &W)); 341907761f8SToby Isaac 3425f80ce2aSJacob Faibussowitsch CHKERRQ(PetscArraycpy(Jinvs, Js, m * m)); 343907761f8SToby Isaac PetscStackCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bm, &bm, Jinvs, &bm, pivots, &info)); 344*28b400f6SJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetrf %D",(PetscInt)info); 345907761f8SToby Isaac PetscStackCallBLAS("LAPACKgetri", LAPACKgetri_(&bm, Jinvs, &bm, pivots, W, &bm, &info)); 346*28b400f6SJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetri %D",(PetscInt)info); 3475f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree2(pivots, W)); 348907761f8SToby Isaac } else if (m < n) { 349907761f8SToby Isaac PetscScalar *JJT; 350907761f8SToby Isaac PetscBLASInt *pivots; 351907761f8SToby Isaac PetscScalar *W; 352907761f8SToby Isaac 3535f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(m*m, &JJT)); 3545f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc2(m, &pivots, m, &W)); 355907761f8SToby Isaac for (i = 0; i < m; i++) { 356907761f8SToby Isaac for (j = 0; j < m; j++) { 357907761f8SToby Isaac PetscScalar val = 0.; 358907761f8SToby Isaac 359907761f8SToby Isaac for (k = 0; k < n; k++) val += Js[i * n + k] * Js[j * n + k]; 360907761f8SToby Isaac JJT[i * m + j] = val; 361907761f8SToby Isaac } 362907761f8SToby Isaac } 363907761f8SToby Isaac 364907761f8SToby Isaac PetscStackCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bm, &bm, JJT, &bm, pivots, &info)); 365*28b400f6SJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetrf %D",(PetscInt)info); 366907761f8SToby Isaac PetscStackCallBLAS("LAPACKgetri", LAPACKgetri_(&bm, JJT, &bm, pivots, W, &bm, &info)); 367*28b400f6SJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetri %D",(PetscInt)info); 368907761f8SToby Isaac for (i = 0; i < n; i++) { 369907761f8SToby Isaac for (j = 0; j < m; j++) { 370907761f8SToby Isaac PetscScalar val = 0.; 371907761f8SToby Isaac 372907761f8SToby Isaac for (k = 0; k < m; k++) val += Js[k * n + i] * JJT[k * m + j]; 373907761f8SToby Isaac Jinvs[i * m + j] = val; 374907761f8SToby Isaac } 375907761f8SToby Isaac } 3765f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree2(pivots, W)); 3775f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(JJT)); 378907761f8SToby Isaac } else { 379907761f8SToby Isaac PetscScalar *JTJ; 380907761f8SToby Isaac PetscBLASInt *pivots; 381907761f8SToby Isaac PetscScalar *W; 382907761f8SToby Isaac 3835f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(n*n, &JTJ)); 3845f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc2(n, &pivots, n, &W)); 385907761f8SToby Isaac for (i = 0; i < n; i++) { 386907761f8SToby Isaac for (j = 0; j < n; j++) { 387907761f8SToby Isaac PetscScalar val = 0.; 388907761f8SToby Isaac 389907761f8SToby Isaac for (k = 0; k < m; k++) val += Js[k * n + i] * Js[k * n + j]; 390907761f8SToby Isaac JTJ[i * n + j] = val; 391907761f8SToby Isaac } 392907761f8SToby Isaac } 393907761f8SToby Isaac 394d4afb720SToby Isaac PetscStackCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bn, &bn, JTJ, &bn, pivots, &info)); 395*28b400f6SJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetrf %D",(PetscInt)info); 396907761f8SToby Isaac PetscStackCallBLAS("LAPACKgetri", LAPACKgetri_(&bn, JTJ, &bn, pivots, W, &bn, &info)); 397*28b400f6SJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetri %D",(PetscInt)info); 398907761f8SToby Isaac for (i = 0; i < n; i++) { 399907761f8SToby Isaac for (j = 0; j < m; j++) { 400907761f8SToby Isaac PetscScalar val = 0.; 401907761f8SToby Isaac 402907761f8SToby Isaac for (k = 0; k < n; k++) val += JTJ[i * n + k] * Js[j * n + k]; 403907761f8SToby Isaac Jinvs[i * m + j] = val; 404907761f8SToby Isaac } 405907761f8SToby Isaac } 4065f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree2(pivots, W)); 4075f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(JTJ)); 408907761f8SToby Isaac } 409907761f8SToby Isaac #if defined(PETSC_USE_COMPLEX) 41028222859SToby Isaac for (i = 0; i < m*n; i++) Jinv[i] = PetscRealPart(Jinvs[i]); 4115f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree2(Js, Jinvs)); 412907761f8SToby Isaac #endif 413907761f8SToby Isaac PetscFunctionReturn(0); 414907761f8SToby Isaac } 415907761f8SToby Isaac 416907761f8SToby Isaac /*@ 417907761f8SToby Isaac PetscQuadraturePushForward - Push forward a quadrature functional under an affine transformation. 418907761f8SToby Isaac 419907761f8SToby Isaac Collecive on PetscQuadrature 420907761f8SToby Isaac 4214165533cSJose E. Roman Input Parameters: 422907761f8SToby Isaac + q - the quadrature functional 423907761f8SToby Isaac . imageDim - the dimension of the image of the transformation 424907761f8SToby Isaac . origin - a point in the original space 425907761f8SToby Isaac . originImage - the image of the origin under the transformation 426907761f8SToby Isaac . J - the Jacobian of the image: an [imageDim x dim] matrix in row major order 42728222859SToby 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] 428907761f8SToby Isaac 4294165533cSJose E. Roman Output Parameters: 430907761f8SToby 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. 431907761f8SToby Isaac 432907761f8SToby 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. 433907761f8SToby Isaac 4346c877ef6SSatish Balay Level: intermediate 4356c877ef6SSatish Balay 436907761f8SToby Isaac .seealso: PetscDTAltVPullback(), PetscDTAltVPullbackMatrix() 437907761f8SToby Isaac @*/ 43828222859SToby Isaac PetscErrorCode PetscQuadraturePushForward(PetscQuadrature q, PetscInt imageDim, const PetscReal origin[], const PetscReal originImage[], const PetscReal J[], PetscInt formDegree, PetscQuadrature *Jinvstarq) 439907761f8SToby Isaac { 440907761f8SToby Isaac PetscInt dim, Nc, imageNc, formSize, Ncopies, imageFormSize, Npoints, pt, i, j, c; 441907761f8SToby Isaac const PetscReal *points; 442907761f8SToby Isaac const PetscReal *weights; 443907761f8SToby Isaac PetscReal *imagePoints, *imageWeights; 444907761f8SToby Isaac PetscReal *Jinv; 445907761f8SToby Isaac PetscReal *Jinvstar; 446907761f8SToby Isaac 447907761f8SToby Isaac PetscFunctionBegin; 448d4afb720SToby Isaac PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 4492c71b3e2SJacob Faibussowitsch PetscCheckFalse(imageDim < PetscAbsInt(formDegree),PetscObjectComm((PetscObject)q), PETSC_ERR_ARG_INCOMP, "Cannot represent a %D-form in %D dimensions", PetscAbsInt(formDegree), imageDim); 4505f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureGetData(q, &dim, &Nc, &Npoints, &points, &weights)); 4515f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomialInt(dim, PetscAbsInt(formDegree), &formSize)); 4522c71b3e2SJacob Faibussowitsch PetscCheckFalse(Nc % formSize,PetscObjectComm((PetscObject)q), PETSC_ERR_ARG_INCOMP, "Number of components %D is not a multiple of formSize %D", Nc, formSize); 453907761f8SToby Isaac Ncopies = Nc / formSize; 4545f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomialInt(imageDim, PetscAbsInt(formDegree), &imageFormSize)); 455907761f8SToby Isaac imageNc = Ncopies * imageFormSize; 4565f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(Npoints * imageDim, &imagePoints)); 4575f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(Npoints * imageNc, &imageWeights)); 4585f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc2(imageDim * dim, &Jinv, formSize * imageFormSize, &Jinvstar)); 4595f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTJacobianInverse_Internal(imageDim, dim, J, Jinv)); 4605f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTAltVPullbackMatrix(imageDim, dim, Jinv, formDegree, Jinvstar)); 461907761f8SToby Isaac for (pt = 0; pt < Npoints; pt++) { 462907761f8SToby Isaac const PetscReal *point = &points[pt * dim]; 463907761f8SToby Isaac PetscReal *imagePoint = &imagePoints[pt * imageDim]; 464907761f8SToby Isaac 465907761f8SToby Isaac for (i = 0; i < imageDim; i++) { 466907761f8SToby Isaac PetscReal val = originImage[i]; 467907761f8SToby Isaac 468907761f8SToby Isaac for (j = 0; j < dim; j++) val += J[i * dim + j] * (point[j] - origin[j]); 469907761f8SToby Isaac imagePoint[i] = val; 470907761f8SToby Isaac } 471907761f8SToby Isaac for (c = 0; c < Ncopies; c++) { 472907761f8SToby Isaac const PetscReal *form = &weights[pt * Nc + c * formSize]; 473907761f8SToby Isaac PetscReal *imageForm = &imageWeights[pt * imageNc + c * imageFormSize]; 474907761f8SToby Isaac 475907761f8SToby Isaac for (i = 0; i < imageFormSize; i++) { 476907761f8SToby Isaac PetscReal val = 0.; 477907761f8SToby Isaac 478907761f8SToby Isaac for (j = 0; j < formSize; j++) val += Jinvstar[i * formSize + j] * form[j]; 479907761f8SToby Isaac imageForm[i] = val; 480907761f8SToby Isaac } 481907761f8SToby Isaac } 482907761f8SToby Isaac } 4835f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureCreate(PetscObjectComm((PetscObject)q), Jinvstarq)); 4845f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureSetData(*Jinvstarq, imageDim, imageNc, Npoints, imagePoints, imageWeights)); 4855f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree2(Jinv, Jinvstar)); 486907761f8SToby Isaac PetscFunctionReturn(0); 487907761f8SToby Isaac } 488907761f8SToby Isaac 48940d8ff71SMatthew G. Knepley /*@C 49040d8ff71SMatthew G. Knepley PetscQuadratureSetData - Sets the data defining the quadrature 49140d8ff71SMatthew G. Knepley 49240d8ff71SMatthew G. Knepley Not collective 49340d8ff71SMatthew G. Knepley 49440d8ff71SMatthew G. Knepley Input Parameters: 49540d8ff71SMatthew G. Knepley + q - The PetscQuadrature object 49640d8ff71SMatthew G. Knepley . dim - The spatial dimension 497e2b35d93SBarry Smith . Nc - The number of components 49840d8ff71SMatthew G. Knepley . npoints - The number of quadrature points 49940d8ff71SMatthew G. Knepley . points - The coordinates of each quadrature point 50040d8ff71SMatthew G. Knepley - weights - The weight of each quadrature point 50140d8ff71SMatthew G. Knepley 502c99e0549SMatthew 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. 503f2fd9e53SMatthew G. Knepley 50440d8ff71SMatthew G. Knepley Level: intermediate 50540d8ff71SMatthew G. Knepley 50640d8ff71SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureGetData() 50740d8ff71SMatthew G. Knepley @*/ 508a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureSetData(PetscQuadrature q, PetscInt dim, PetscInt Nc, PetscInt npoints, const PetscReal points[], const PetscReal weights[]) 50921454ff5SMatthew G. Knepley { 51021454ff5SMatthew G. Knepley PetscFunctionBegin; 5112cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 51221454ff5SMatthew G. Knepley if (dim >= 0) q->dim = dim; 513a6b92713SMatthew G. Knepley if (Nc >= 0) q->Nc = Nc; 51421454ff5SMatthew G. Knepley if (npoints >= 0) q->numPoints = npoints; 51521454ff5SMatthew G. Knepley if (points) { 516064a246eSJacob Faibussowitsch PetscValidPointer(points, 5); 51721454ff5SMatthew G. Knepley q->points = points; 51821454ff5SMatthew G. Knepley } 51921454ff5SMatthew G. Knepley if (weights) { 520064a246eSJacob Faibussowitsch PetscValidPointer(weights, 6); 52121454ff5SMatthew G. Knepley q->weights = weights; 52221454ff5SMatthew G. Knepley } 523f9fd7fdbSMatthew G. Knepley PetscFunctionReturn(0); 524f9fd7fdbSMatthew G. Knepley } 525f9fd7fdbSMatthew G. Knepley 526d9bac1caSLisandro Dalcin static PetscErrorCode PetscQuadratureView_Ascii(PetscQuadrature quad, PetscViewer v) 527d9bac1caSLisandro Dalcin { 528d9bac1caSLisandro Dalcin PetscInt q, d, c; 529d9bac1caSLisandro Dalcin PetscViewerFormat format; 530d9bac1caSLisandro Dalcin 531d9bac1caSLisandro Dalcin PetscFunctionBegin; 5325f80ce2aSJacob Faibussowitsch if (quad->Nc > 1) CHKERRQ(PetscViewerASCIIPrintf(v, "Quadrature of order %D on %D points (dim %D) with %D components\n", quad->order, quad->numPoints, quad->dim, quad->Nc)); 5335f80ce2aSJacob Faibussowitsch else CHKERRQ(PetscViewerASCIIPrintf(v, "Quadrature of order %D on %D points (dim %D)\n", quad->order, quad->numPoints, quad->dim)); 5345f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerGetFormat(v, &format)); 535d9bac1caSLisandro Dalcin if (format != PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(0); 536d9bac1caSLisandro Dalcin for (q = 0; q < quad->numPoints; ++q) { 5375f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIPrintf(v, "p%D (", q)); 5385f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIUseTabs(v, PETSC_FALSE)); 539d9bac1caSLisandro Dalcin for (d = 0; d < quad->dim; ++d) { 5405f80ce2aSJacob Faibussowitsch if (d) CHKERRQ(PetscViewerASCIIPrintf(v, ", ")); 5415f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIPrintf(v, "%+g", (double)quad->points[q*quad->dim+d])); 542d9bac1caSLisandro Dalcin } 5435f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIPrintf(v, ") ")); 5445f80ce2aSJacob Faibussowitsch if (quad->Nc > 1) CHKERRQ(PetscViewerASCIIPrintf(v, "w%D (", q)); 545d9bac1caSLisandro Dalcin for (c = 0; c < quad->Nc; ++c) { 5465f80ce2aSJacob Faibussowitsch if (c) CHKERRQ(PetscViewerASCIIPrintf(v, ", ")); 5475f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIPrintf(v, "%+g", (double)quad->weights[q*quad->Nc+c])); 548d9bac1caSLisandro Dalcin } 5495f80ce2aSJacob Faibussowitsch if (quad->Nc > 1) CHKERRQ(PetscViewerASCIIPrintf(v, ")")); 5505f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIPrintf(v, "\n")); 5515f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIUseTabs(v, PETSC_TRUE)); 552d9bac1caSLisandro Dalcin } 553d9bac1caSLisandro Dalcin PetscFunctionReturn(0); 554d9bac1caSLisandro Dalcin } 555d9bac1caSLisandro Dalcin 55640d8ff71SMatthew G. Knepley /*@C 55740d8ff71SMatthew G. Knepley PetscQuadratureView - Views a PetscQuadrature object 55840d8ff71SMatthew G. Knepley 559d083f849SBarry Smith Collective on quad 56040d8ff71SMatthew G. Knepley 56140d8ff71SMatthew G. Knepley Input Parameters: 562d9bac1caSLisandro Dalcin + quad - The PetscQuadrature object 56340d8ff71SMatthew G. Knepley - viewer - The PetscViewer object 56440d8ff71SMatthew G. Knepley 56540d8ff71SMatthew G. Knepley Level: beginner 56640d8ff71SMatthew G. Knepley 56740d8ff71SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureGetData() 56840d8ff71SMatthew G. Knepley @*/ 569f9fd7fdbSMatthew G. Knepley PetscErrorCode PetscQuadratureView(PetscQuadrature quad, PetscViewer viewer) 570f9fd7fdbSMatthew G. Knepley { 571d9bac1caSLisandro Dalcin PetscBool iascii; 572f9fd7fdbSMatthew G. Knepley 573f9fd7fdbSMatthew G. Knepley PetscFunctionBegin; 574d9bac1caSLisandro Dalcin PetscValidHeader(quad, 1); 575d9bac1caSLisandro Dalcin if (viewer) PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 5765f80ce2aSJacob Faibussowitsch if (!viewer) CHKERRQ(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject) quad), &viewer)); 5775f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii)); 5785f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIPushTab(viewer)); 5795f80ce2aSJacob Faibussowitsch if (iascii) CHKERRQ(PetscQuadratureView_Ascii(quad, viewer)); 5805f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIPopTab(viewer)); 581bfa639d9SMatthew G. Knepley PetscFunctionReturn(0); 582bfa639d9SMatthew G. Knepley } 583bfa639d9SMatthew G. Knepley 58489710940SMatthew G. Knepley /*@C 58589710940SMatthew G. Knepley PetscQuadratureExpandComposite - Return a quadrature over the composite element, which has the original quadrature in each subelement 58689710940SMatthew G. Knepley 58789710940SMatthew G. Knepley Not collective 58889710940SMatthew G. Knepley 589d8d19677SJose E. Roman Input Parameters: 59089710940SMatthew G. Knepley + q - The original PetscQuadrature 59189710940SMatthew G. Knepley . numSubelements - The number of subelements the original element is divided into 59289710940SMatthew G. Knepley . v0 - An array of the initial points for each subelement 59389710940SMatthew G. Knepley - jac - An array of the Jacobian mappings from the reference to each subelement 59489710940SMatthew G. Knepley 59589710940SMatthew G. Knepley Output Parameters: 59689710940SMatthew G. Knepley . dim - The dimension 59789710940SMatthew G. Knepley 59889710940SMatthew G. Knepley Note: Together v0 and jac define an affine mapping from the original reference element to each subelement 59989710940SMatthew G. Knepley 600f5f57ec0SBarry Smith Not available from Fortran 601f5f57ec0SBarry Smith 60289710940SMatthew G. Knepley Level: intermediate 60389710940SMatthew G. Knepley 60489710940SMatthew G. Knepley .seealso: PetscFECreate(), PetscSpaceGetDimension(), PetscDualSpaceGetDimension() 60589710940SMatthew G. Knepley @*/ 60689710940SMatthew G. Knepley PetscErrorCode PetscQuadratureExpandComposite(PetscQuadrature q, PetscInt numSubelements, const PetscReal v0[], const PetscReal jac[], PetscQuadrature *qref) 60789710940SMatthew G. Knepley { 60889710940SMatthew G. Knepley const PetscReal *points, *weights; 60989710940SMatthew G. Knepley PetscReal *pointsRef, *weightsRef; 610a6b92713SMatthew G. Knepley PetscInt dim, Nc, order, npoints, npointsRef, c, p, cp, d, e; 61189710940SMatthew G. Knepley 61289710940SMatthew G. Knepley PetscFunctionBegin; 6132cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 61489710940SMatthew G. Knepley PetscValidPointer(v0, 3); 61589710940SMatthew G. Knepley PetscValidPointer(jac, 4); 61689710940SMatthew G. Knepley PetscValidPointer(qref, 5); 6175f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureCreate(PETSC_COMM_SELF, qref)); 6185f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureGetOrder(q, &order)); 6195f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureGetData(q, &dim, &Nc, &npoints, &points, &weights)); 62089710940SMatthew G. Knepley npointsRef = npoints*numSubelements; 6215f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(npointsRef*dim,&pointsRef)); 6225f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(npointsRef*Nc, &weightsRef)); 62389710940SMatthew G. Knepley for (c = 0; c < numSubelements; ++c) { 62489710940SMatthew G. Knepley for (p = 0; p < npoints; ++p) { 62589710940SMatthew G. Knepley for (d = 0; d < dim; ++d) { 62689710940SMatthew G. Knepley pointsRef[(c*npoints + p)*dim+d] = v0[c*dim+d]; 62789710940SMatthew G. Knepley for (e = 0; e < dim; ++e) { 62889710940SMatthew G. Knepley pointsRef[(c*npoints + p)*dim+d] += jac[(c*dim + d)*dim+e]*(points[p*dim+e] + 1.0); 62989710940SMatthew G. Knepley } 63089710940SMatthew G. Knepley } 63189710940SMatthew G. Knepley /* Could also use detJ here */ 632a6b92713SMatthew G. Knepley for (cp = 0; cp < Nc; ++cp) weightsRef[(c*npoints+p)*Nc+cp] = weights[p*Nc+cp]/numSubelements; 63389710940SMatthew G. Knepley } 63489710940SMatthew G. Knepley } 6355f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureSetOrder(*qref, order)); 6365f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureSetData(*qref, dim, Nc, npointsRef, pointsRef, weightsRef)); 63789710940SMatthew G. Knepley PetscFunctionReturn(0); 63889710940SMatthew G. Knepley } 63989710940SMatthew G. Knepley 64094e21283SToby Isaac /* Compute the coefficients for the Jacobi polynomial recurrence, 64194e21283SToby Isaac * 64294e21283SToby Isaac * J^{a,b}_n(x) = (cnm1 + cnm1x * x) * J^{a,b}_{n-1}(x) - cnm2 * J^{a,b}_{n-2}(x). 64394e21283SToby Isaac */ 64494e21283SToby Isaac #define PetscDTJacobiRecurrence_Internal(n,a,b,cnm1,cnm1x,cnm2) \ 64594e21283SToby Isaac do { \ 64694e21283SToby Isaac PetscReal _a = (a); \ 64794e21283SToby Isaac PetscReal _b = (b); \ 64894e21283SToby Isaac PetscReal _n = (n); \ 64994e21283SToby Isaac if (n == 1) { \ 65094e21283SToby Isaac (cnm1) = (_a-_b) * 0.5; \ 65194e21283SToby Isaac (cnm1x) = (_a+_b+2.)*0.5; \ 65294e21283SToby Isaac (cnm2) = 0.; \ 65394e21283SToby Isaac } else { \ 65494e21283SToby Isaac PetscReal _2n = _n+_n; \ 65594e21283SToby Isaac PetscReal _d = (_2n*(_n+_a+_b)*(_2n+_a+_b-2)); \ 65694e21283SToby Isaac PetscReal _n1 = (_2n+_a+_b-1.)*(_a*_a-_b*_b); \ 65794e21283SToby Isaac PetscReal _n1x = (_2n+_a+_b-1.)*(_2n+_a+_b)*(_2n+_a+_b-2); \ 65894e21283SToby Isaac PetscReal _n2 = 2.*((_n+_a-1.)*(_n+_b-1.)*(_2n+_a+_b)); \ 65994e21283SToby Isaac (cnm1) = _n1 / _d; \ 66094e21283SToby Isaac (cnm1x) = _n1x / _d; \ 66194e21283SToby Isaac (cnm2) = _n2 / _d; \ 66294e21283SToby Isaac } \ 66394e21283SToby Isaac } while (0) 66494e21283SToby Isaac 665fbdc3dfeSToby Isaac /*@ 666fbdc3dfeSToby Isaac PetscDTJacobiNorm - Compute the weighted L2 norm of a Jacobi polynomial. 667fbdc3dfeSToby Isaac 668fbdc3dfeSToby Isaac $\| P^{\alpha,\beta}_n \|_{\alpha,\beta}^2 = \int_{-1}^1 (1 + x)^{\alpha} (1 - x)^{\beta} P^{\alpha,\beta}_n (x)^2 dx.$ 669fbdc3dfeSToby Isaac 6704165533cSJose E. Roman Input Parameters: 671fbdc3dfeSToby Isaac - alpha - the left exponent > -1 672fbdc3dfeSToby Isaac . beta - the right exponent > -1 673fbdc3dfeSToby Isaac + n - the polynomial degree 674fbdc3dfeSToby Isaac 6754165533cSJose E. Roman Output Parameter: 676fbdc3dfeSToby Isaac . norm - the weighted L2 norm 677fbdc3dfeSToby Isaac 678fbdc3dfeSToby Isaac Level: beginner 679fbdc3dfeSToby Isaac 680fbdc3dfeSToby Isaac .seealso: PetscDTJacobiEval() 681fbdc3dfeSToby Isaac @*/ 682fbdc3dfeSToby Isaac PetscErrorCode PetscDTJacobiNorm(PetscReal alpha, PetscReal beta, PetscInt n, PetscReal *norm) 683fbdc3dfeSToby Isaac { 684fbdc3dfeSToby Isaac PetscReal twoab1; 685fbdc3dfeSToby Isaac PetscReal gr; 686fbdc3dfeSToby Isaac 687fbdc3dfeSToby Isaac PetscFunctionBegin; 6882c71b3e2SJacob Faibussowitsch PetscCheckFalse(alpha <= -1.,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Exponent alpha %g <= -1. invalid", (double) alpha); 6892c71b3e2SJacob Faibussowitsch PetscCheckFalse(beta <= -1.,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Exponent beta %g <= -1. invalid", (double) beta); 6902c71b3e2SJacob Faibussowitsch PetscCheckFalse(n < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "n %D < 0 invalid", n); 691fbdc3dfeSToby Isaac twoab1 = PetscPowReal(2., alpha + beta + 1.); 692fbdc3dfeSToby Isaac #if defined(PETSC_HAVE_LGAMMA) 693fbdc3dfeSToby Isaac if (!n) { 694fbdc3dfeSToby Isaac gr = PetscExpReal(PetscLGamma(alpha+1.) + PetscLGamma(beta+1.) - PetscLGamma(alpha+beta+2.)); 695fbdc3dfeSToby Isaac } else { 696fbdc3dfeSToby Isaac gr = PetscExpReal(PetscLGamma(n+alpha+1.) + PetscLGamma(n+beta+1.) - (PetscLGamma(n+1.) + PetscLGamma(n+alpha+beta+1.))) / (n+n+alpha+beta+1.); 697fbdc3dfeSToby Isaac } 698fbdc3dfeSToby Isaac #else 699fbdc3dfeSToby Isaac { 700fbdc3dfeSToby Isaac PetscInt alphai = (PetscInt) alpha; 701fbdc3dfeSToby Isaac PetscInt betai = (PetscInt) beta; 702fbdc3dfeSToby Isaac PetscInt i; 703fbdc3dfeSToby Isaac 704fbdc3dfeSToby Isaac gr = n ? (1. / (n+n+alpha+beta+1.)) : 1.; 705fbdc3dfeSToby Isaac if ((PetscReal) alphai == alpha) { 706fbdc3dfeSToby Isaac if (!n) { 707fbdc3dfeSToby Isaac for (i = 0; i < alphai; i++) gr *= (i+1.) / (beta+i+1.); 708fbdc3dfeSToby Isaac gr /= (alpha+beta+1.); 709fbdc3dfeSToby Isaac } else { 710fbdc3dfeSToby Isaac for (i = 0; i < alphai; i++) gr *= (n+i+1.) / (n+beta+i+1.); 711fbdc3dfeSToby Isaac } 712fbdc3dfeSToby Isaac } else if ((PetscReal) betai == beta) { 713fbdc3dfeSToby Isaac if (!n) { 714fbdc3dfeSToby Isaac for (i = 0; i < betai; i++) gr *= (i+1.) / (alpha+i+2.); 715fbdc3dfeSToby Isaac gr /= (alpha+beta+1.); 716fbdc3dfeSToby Isaac } else { 717fbdc3dfeSToby Isaac for (i = 0; i < betai; i++) gr *= (n+i+1.) / (n+alpha+i+1.); 718fbdc3dfeSToby Isaac } 719fbdc3dfeSToby Isaac } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"lgamma() - math routine is unavailable."); 720fbdc3dfeSToby Isaac } 721fbdc3dfeSToby Isaac #endif 722fbdc3dfeSToby Isaac *norm = PetscSqrtReal(twoab1 * gr); 723fbdc3dfeSToby Isaac PetscFunctionReturn(0); 724fbdc3dfeSToby Isaac } 725fbdc3dfeSToby Isaac 72694e21283SToby Isaac static PetscErrorCode PetscDTJacobiEval_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscInt k, const PetscReal *points, PetscInt ndegree, const PetscInt *degrees, PetscReal *p) 72794e21283SToby Isaac { 72894e21283SToby Isaac PetscReal ak, bk; 72994e21283SToby Isaac PetscReal abk1; 73094e21283SToby Isaac PetscInt i,l,maxdegree; 73194e21283SToby Isaac 73294e21283SToby Isaac PetscFunctionBegin; 73394e21283SToby Isaac maxdegree = degrees[ndegree-1] - k; 73494e21283SToby Isaac ak = a + k; 73594e21283SToby Isaac bk = b + k; 73694e21283SToby Isaac abk1 = a + b + k + 1.; 73794e21283SToby Isaac if (maxdegree < 0) { 73894e21283SToby Isaac for (i = 0; i < npoints; i++) for (l = 0; l < ndegree; l++) p[i*ndegree+l] = 0.; 73994e21283SToby Isaac PetscFunctionReturn(0); 74094e21283SToby Isaac } 74194e21283SToby Isaac for (i=0; i<npoints; i++) { 74294e21283SToby Isaac PetscReal pm1,pm2,x; 74394e21283SToby Isaac PetscReal cnm1, cnm1x, cnm2; 74494e21283SToby Isaac PetscInt j,m; 74594e21283SToby Isaac 74694e21283SToby Isaac x = points[i]; 74794e21283SToby Isaac pm2 = 1.; 74894e21283SToby Isaac PetscDTJacobiRecurrence_Internal(1,ak,bk,cnm1,cnm1x,cnm2); 74994e21283SToby Isaac pm1 = (cnm1 + cnm1x*x); 75094e21283SToby Isaac l = 0; 75194e21283SToby Isaac while (l < ndegree && degrees[l] - k < 0) { 75294e21283SToby Isaac p[l++] = 0.; 75394e21283SToby Isaac } 75494e21283SToby Isaac while (l < ndegree && degrees[l] - k == 0) { 75594e21283SToby Isaac p[l] = pm2; 75694e21283SToby Isaac for (m = 0; m < k; m++) p[l] *= (abk1 + m) * 0.5; 75794e21283SToby Isaac l++; 75894e21283SToby Isaac } 75994e21283SToby Isaac while (l < ndegree && degrees[l] - k == 1) { 76094e21283SToby Isaac p[l] = pm1; 76194e21283SToby Isaac for (m = 0; m < k; m++) p[l] *= (abk1 + 1 + m) * 0.5; 76294e21283SToby Isaac l++; 76394e21283SToby Isaac } 76494e21283SToby Isaac for (j=2; j<=maxdegree; j++) { 76594e21283SToby Isaac PetscReal pp; 76694e21283SToby Isaac 76794e21283SToby Isaac PetscDTJacobiRecurrence_Internal(j,ak,bk,cnm1,cnm1x,cnm2); 76894e21283SToby Isaac pp = (cnm1 + cnm1x*x)*pm1 - cnm2*pm2; 76994e21283SToby Isaac pm2 = pm1; 77094e21283SToby Isaac pm1 = pp; 77194e21283SToby Isaac while (l < ndegree && degrees[l] - k == j) { 77294e21283SToby Isaac p[l] = pp; 77394e21283SToby Isaac for (m = 0; m < k; m++) p[l] *= (abk1 + j + m) * 0.5; 77494e21283SToby Isaac l++; 77594e21283SToby Isaac } 77694e21283SToby Isaac } 77794e21283SToby Isaac p += ndegree; 77894e21283SToby Isaac } 77994e21283SToby Isaac PetscFunctionReturn(0); 78094e21283SToby Isaac } 78194e21283SToby Isaac 78237045ce4SJed Brown /*@ 783fbdc3dfeSToby 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$. 784fbdc3dfeSToby Isaac 7854165533cSJose E. Roman Input Parameters: 786fbdc3dfeSToby Isaac + alpha - the left exponent of the weight 787fbdc3dfeSToby Isaac . beta - the right exponetn of the weight 788fbdc3dfeSToby Isaac . npoints - the number of points to evaluate the polynomials at 789fbdc3dfeSToby Isaac . points - [npoints] array of point coordinates 790fbdc3dfeSToby Isaac . degree - the maximm degree polynomial space to evaluate, (degree + 1) will be evaluated total. 791fbdc3dfeSToby Isaac - k - the maximum derivative to evaluate in the jet, (k + 1) will be evaluated total. 792fbdc3dfeSToby Isaac 793fbdc3dfeSToby Isaac Output Argments: 794fbdc3dfeSToby Isaac - p - an array containing the evaluations of the Jacobi polynomials's jets on the points. the size is (degree + 1) x 795fbdc3dfeSToby Isaac (k + 1) x npoints, which also describes the order of the dimensions of this three-dimensional array: the first 796fbdc3dfeSToby Isaac (slowest varying) dimension is polynomial degree; the second dimension is derivative order; the third (fastest 797fbdc3dfeSToby Isaac varying) dimension is the index of the evaluation point. 798fbdc3dfeSToby Isaac 799fbdc3dfeSToby Isaac Level: advanced 800fbdc3dfeSToby Isaac 801fbdc3dfeSToby Isaac .seealso: PetscDTJacobiEval(), PetscDTPKDEvalJet() 802fbdc3dfeSToby Isaac @*/ 803fbdc3dfeSToby Isaac PetscErrorCode PetscDTJacobiEvalJet(PetscReal alpha, PetscReal beta, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt k, PetscReal p[]) 804fbdc3dfeSToby Isaac { 805fbdc3dfeSToby Isaac PetscInt i, j, l; 806fbdc3dfeSToby Isaac PetscInt *degrees; 807fbdc3dfeSToby Isaac PetscReal *psingle; 808fbdc3dfeSToby Isaac 809fbdc3dfeSToby Isaac PetscFunctionBegin; 810fbdc3dfeSToby Isaac if (degree == 0) { 811fbdc3dfeSToby Isaac PetscInt zero = 0; 812fbdc3dfeSToby Isaac 813fbdc3dfeSToby Isaac for (i = 0; i <= k; i++) { 8145f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTJacobiEval_Internal(npoints, alpha, beta, i, points, 1, &zero, &p[i*npoints])); 815fbdc3dfeSToby Isaac } 816fbdc3dfeSToby Isaac PetscFunctionReturn(0); 817fbdc3dfeSToby Isaac } 8185f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(degree + 1, °rees)); 8195f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1((degree + 1) * npoints, &psingle)); 820fbdc3dfeSToby Isaac for (i = 0; i <= degree; i++) degrees[i] = i; 821fbdc3dfeSToby Isaac for (i = 0; i <= k; i++) { 8225f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTJacobiEval_Internal(npoints, alpha, beta, i, points, degree + 1, degrees, psingle)); 823fbdc3dfeSToby Isaac for (j = 0; j <= degree; j++) { 824fbdc3dfeSToby Isaac for (l = 0; l < npoints; l++) { 825fbdc3dfeSToby Isaac p[(j * (k + 1) + i) * npoints + l] = psingle[l * (degree + 1) + j]; 826fbdc3dfeSToby Isaac } 827fbdc3dfeSToby Isaac } 828fbdc3dfeSToby Isaac } 8295f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(psingle)); 8305f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(degrees)); 831fbdc3dfeSToby Isaac PetscFunctionReturn(0); 832fbdc3dfeSToby Isaac } 833fbdc3dfeSToby Isaac 834fbdc3dfeSToby Isaac /*@ 83594e21283SToby Isaac PetscDTJacobiEval - evaluate Jacobi polynomials for the weight function $(1.+x)^{\alpha} (1.-x)^{\beta}$ 83694e21283SToby Isaac at points 83794e21283SToby Isaac 83894e21283SToby Isaac Not Collective 83994e21283SToby Isaac 8404165533cSJose E. Roman Input Parameters: 84194e21283SToby Isaac + npoints - number of spatial points to evaluate at 84294e21283SToby Isaac . alpha - the left exponent > -1 84394e21283SToby Isaac . beta - the right exponent > -1 84494e21283SToby Isaac . points - array of locations to evaluate at 84594e21283SToby Isaac . ndegree - number of basis degrees to evaluate 84694e21283SToby Isaac - degrees - sorted array of degrees to evaluate 84794e21283SToby Isaac 8484165533cSJose E. Roman Output Parameters: 84994e21283SToby Isaac + B - row-oriented basis evaluation matrix B[point*ndegree + degree] (dimension npoints*ndegrees, allocated by caller) (or NULL) 85094e21283SToby Isaac . D - row-oriented derivative evaluation matrix (or NULL) 85194e21283SToby Isaac - D2 - row-oriented second derivative evaluation matrix (or NULL) 85294e21283SToby Isaac 85394e21283SToby Isaac Level: intermediate 85494e21283SToby Isaac 85594e21283SToby Isaac .seealso: PetscDTGaussQuadrature() 85694e21283SToby Isaac @*/ 85794e21283SToby Isaac PetscErrorCode PetscDTJacobiEval(PetscInt npoints,PetscReal alpha, PetscReal beta, const PetscReal *points,PetscInt ndegree,const PetscInt *degrees,PetscReal *B,PetscReal *D,PetscReal *D2) 85894e21283SToby Isaac { 85994e21283SToby Isaac PetscFunctionBegin; 8602c71b3e2SJacob Faibussowitsch PetscCheckFalse(alpha <= -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"alpha must be > -1."); 8612c71b3e2SJacob Faibussowitsch PetscCheckFalse(beta <= -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"beta must be > -1."); 86294e21283SToby Isaac if (!npoints || !ndegree) PetscFunctionReturn(0); 8635f80ce2aSJacob Faibussowitsch if (B) CHKERRQ(PetscDTJacobiEval_Internal(npoints, alpha, beta, 0, points, ndegree, degrees, B)); 8645f80ce2aSJacob Faibussowitsch if (D) CHKERRQ(PetscDTJacobiEval_Internal(npoints, alpha, beta, 1, points, ndegree, degrees, D)); 8655f80ce2aSJacob Faibussowitsch if (D2) CHKERRQ(PetscDTJacobiEval_Internal(npoints, alpha, beta, 2, points, ndegree, degrees, D2)); 86694e21283SToby Isaac PetscFunctionReturn(0); 86794e21283SToby Isaac } 86894e21283SToby Isaac 86994e21283SToby Isaac /*@ 87094e21283SToby Isaac PetscDTLegendreEval - evaluate Legendre polynomials at points 87137045ce4SJed Brown 87237045ce4SJed Brown Not Collective 87337045ce4SJed Brown 8744165533cSJose E. Roman Input Parameters: 87537045ce4SJed Brown + npoints - number of spatial points to evaluate at 87637045ce4SJed Brown . points - array of locations to evaluate at 87737045ce4SJed Brown . ndegree - number of basis degrees to evaluate 87837045ce4SJed Brown - degrees - sorted array of degrees to evaluate 87937045ce4SJed Brown 8804165533cSJose E. Roman Output Parameters: 8810298fd71SBarry Smith + B - row-oriented basis evaluation matrix B[point*ndegree + degree] (dimension npoints*ndegrees, allocated by caller) (or NULL) 8820298fd71SBarry Smith . D - row-oriented derivative evaluation matrix (or NULL) 8830298fd71SBarry Smith - D2 - row-oriented second derivative evaluation matrix (or NULL) 88437045ce4SJed Brown 88537045ce4SJed Brown Level: intermediate 88637045ce4SJed Brown 88737045ce4SJed Brown .seealso: PetscDTGaussQuadrature() 88837045ce4SJed Brown @*/ 88937045ce4SJed Brown PetscErrorCode PetscDTLegendreEval(PetscInt npoints,const PetscReal *points,PetscInt ndegree,const PetscInt *degrees,PetscReal *B,PetscReal *D,PetscReal *D2) 89037045ce4SJed Brown { 89137045ce4SJed Brown PetscFunctionBegin; 8925f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTJacobiEval(npoints, 0., 0., points, ndegree, degrees, B, D, D2)); 89337045ce4SJed Brown PetscFunctionReturn(0); 89437045ce4SJed Brown } 89537045ce4SJed Brown 896fbdc3dfeSToby Isaac /*@ 897fbdc3dfeSToby 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) 898fbdc3dfeSToby Isaac 899fbdc3dfeSToby Isaac Input Parameters: 900fbdc3dfeSToby Isaac + len - the desired length of the degree tuple 901fbdc3dfeSToby Isaac - index - the index to convert: should be >= 0 902fbdc3dfeSToby Isaac 903fbdc3dfeSToby Isaac Output Parameter: 904fbdc3dfeSToby Isaac . degtup - will be filled with a tuple of degrees 905fbdc3dfeSToby Isaac 906fbdc3dfeSToby Isaac Level: beginner 907fbdc3dfeSToby Isaac 908fbdc3dfeSToby Isaac Note: for two tuples x and y with the same degree sum, partial degree sums over the final elements of the tuples 909fbdc3dfeSToby 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 910fbdc3dfeSToby Isaac last two elements is smaller for the former, so (2, 1, 1) < (1, 2, 1). 911fbdc3dfeSToby Isaac 912fbdc3dfeSToby Isaac .seealso: PetscDTGradedOrderToIndex() 913fbdc3dfeSToby Isaac @*/ 914fbdc3dfeSToby Isaac PetscErrorCode PetscDTIndexToGradedOrder(PetscInt len, PetscInt index, PetscInt degtup[]) 915fbdc3dfeSToby Isaac { 916fbdc3dfeSToby Isaac PetscInt i, total; 917fbdc3dfeSToby Isaac PetscInt sum; 918fbdc3dfeSToby Isaac 919fbdc3dfeSToby Isaac PetscFunctionBeginHot; 9202c71b3e2SJacob Faibussowitsch PetscCheckFalse(len < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative"); 9212c71b3e2SJacob Faibussowitsch PetscCheckFalse(index < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index must be non-negative"); 922fbdc3dfeSToby Isaac total = 1; 923fbdc3dfeSToby Isaac sum = 0; 924fbdc3dfeSToby Isaac while (index >= total) { 925fbdc3dfeSToby Isaac index -= total; 926fbdc3dfeSToby Isaac total = (total * (len + sum)) / (sum + 1); 927fbdc3dfeSToby Isaac sum++; 928fbdc3dfeSToby Isaac } 929fbdc3dfeSToby Isaac for (i = 0; i < len; i++) { 930fbdc3dfeSToby Isaac PetscInt c; 931fbdc3dfeSToby Isaac 932fbdc3dfeSToby Isaac degtup[i] = sum; 933fbdc3dfeSToby Isaac for (c = 0, total = 1; c < sum; c++) { 934fbdc3dfeSToby Isaac /* going into the loop, total is the number of way to have a tuple of sum exactly c with length len - 1 - i */ 935fbdc3dfeSToby Isaac if (index < total) break; 936fbdc3dfeSToby Isaac index -= total; 937fbdc3dfeSToby Isaac total = (total * (len - 1 - i + c)) / (c + 1); 938fbdc3dfeSToby Isaac degtup[i]--; 939fbdc3dfeSToby Isaac } 940fbdc3dfeSToby Isaac sum -= degtup[i]; 941fbdc3dfeSToby Isaac } 942fbdc3dfeSToby Isaac PetscFunctionReturn(0); 943fbdc3dfeSToby Isaac } 944fbdc3dfeSToby Isaac 945fbdc3dfeSToby Isaac /*@ 946fbdc3dfeSToby Isaac PetscDTGradedOrderToIndex - convert a tuple into an index in a graded order, the inverse of PetscDTIndexToGradedOrder(). 947fbdc3dfeSToby Isaac 948fbdc3dfeSToby Isaac Input Parameters: 949fbdc3dfeSToby Isaac + len - the length of the degree tuple 950fbdc3dfeSToby Isaac - degtup - tuple with this length 951fbdc3dfeSToby Isaac 952fbdc3dfeSToby Isaac Output Parameter: 953fbdc3dfeSToby Isaac . index - index in graded order: >= 0 954fbdc3dfeSToby Isaac 955fbdc3dfeSToby Isaac Level: Beginner 956fbdc3dfeSToby Isaac 957fbdc3dfeSToby Isaac Note: for two tuples x and y with the same degree sum, partial degree sums over the final elements of the tuples 958fbdc3dfeSToby 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 959fbdc3dfeSToby Isaac last two elements is smaller for the former, so (2, 1, 1) < (1, 2, 1). 960fbdc3dfeSToby Isaac 961fbdc3dfeSToby Isaac .seealso: PetscDTIndexToGradedOrder() 962fbdc3dfeSToby Isaac @*/ 963fbdc3dfeSToby Isaac PetscErrorCode PetscDTGradedOrderToIndex(PetscInt len, const PetscInt degtup[], PetscInt *index) 964fbdc3dfeSToby Isaac { 965fbdc3dfeSToby Isaac PetscInt i, idx, sum, total; 966fbdc3dfeSToby Isaac 967fbdc3dfeSToby Isaac PetscFunctionBeginHot; 9682c71b3e2SJacob Faibussowitsch PetscCheckFalse(len < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative"); 969fbdc3dfeSToby Isaac for (i = 0, sum = 0; i < len; i++) sum += degtup[i]; 970fbdc3dfeSToby Isaac idx = 0; 971fbdc3dfeSToby Isaac total = 1; 972fbdc3dfeSToby Isaac for (i = 0; i < sum; i++) { 973fbdc3dfeSToby Isaac idx += total; 974fbdc3dfeSToby Isaac total = (total * (len + i)) / (i + 1); 975fbdc3dfeSToby Isaac } 976fbdc3dfeSToby Isaac for (i = 0; i < len - 1; i++) { 977fbdc3dfeSToby Isaac PetscInt c; 978fbdc3dfeSToby Isaac 979fbdc3dfeSToby Isaac total = 1; 980fbdc3dfeSToby Isaac sum -= degtup[i]; 981fbdc3dfeSToby Isaac for (c = 0; c < sum; c++) { 982fbdc3dfeSToby Isaac idx += total; 983fbdc3dfeSToby Isaac total = (total * (len - 1 - i + c)) / (c + 1); 984fbdc3dfeSToby Isaac } 985fbdc3dfeSToby Isaac } 986fbdc3dfeSToby Isaac *index = idx; 987fbdc3dfeSToby Isaac PetscFunctionReturn(0); 988fbdc3dfeSToby Isaac } 989fbdc3dfeSToby Isaac 990e3aa2e09SToby Isaac static PetscBool PKDCite = PETSC_FALSE; 991e3aa2e09SToby Isaac const char PKDCitation[] = "@article{Kirby2010,\n" 992e3aa2e09SToby Isaac " title={Singularity-free evaluation of collapsed-coordinate orthogonal polynomials},\n" 993e3aa2e09SToby Isaac " author={Kirby, Robert C},\n" 994e3aa2e09SToby Isaac " journal={ACM Transactions on Mathematical Software (TOMS)},\n" 995e3aa2e09SToby Isaac " volume={37},\n" 996e3aa2e09SToby Isaac " number={1},\n" 997e3aa2e09SToby Isaac " pages={1--16},\n" 998e3aa2e09SToby Isaac " year={2010},\n" 999e3aa2e09SToby Isaac " publisher={ACM New York, NY, USA}\n}\n"; 1000e3aa2e09SToby Isaac 1001fbdc3dfeSToby Isaac /*@ 1002d8f25ad8SToby Isaac PetscDTPKDEvalJet - Evaluate the jet (function and derivatives) of the Proriol-Koornwinder-Dubiner (PKD) basis for 1003fbdc3dfeSToby Isaac the space of polynomials up to a given degree. The PKD basis is L2-orthonormal on the biunit simplex (which is used 1004fbdc3dfeSToby Isaac as the reference element for finite elements in PETSc), which makes it a stable basis to use for evaluating 1005fbdc3dfeSToby Isaac polynomials in that domain. 1006fbdc3dfeSToby Isaac 10074165533cSJose E. Roman Input Parameters: 1008fbdc3dfeSToby Isaac + dim - the number of variables in the multivariate polynomials 1009fbdc3dfeSToby Isaac . npoints - the number of points to evaluate the polynomials at 1010fbdc3dfeSToby Isaac . points - [npoints x dim] array of point coordinates 1011fbdc3dfeSToby 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. 1012fbdc3dfeSToby Isaac - k - the maximum order partial derivative to evaluate in the jet. There are (dim + k choose dim) partial derivatives 1013fbdc3dfeSToby Isaac in the jet. Choosing k = 0 means to evaluate just the function and no derivatives 1014fbdc3dfeSToby Isaac 1015fbdc3dfeSToby Isaac Output Argments: 1016fbdc3dfeSToby Isaac - p - an array containing the evaluations of the PKD polynomials' jets on the points. The size is ((dim + degree) 1017fbdc3dfeSToby Isaac choose dim) x ((dim + k) choose dim) x npoints, which also describes the order of the dimensions of this 1018fbdc3dfeSToby Isaac three-dimensional array: the first (slowest varying) dimension is basis function index; the second dimension is jet 1019fbdc3dfeSToby Isaac index; the third (fastest varying) dimension is the index of the evaluation point. 1020fbdc3dfeSToby Isaac 1021fbdc3dfeSToby Isaac Level: advanced 1022fbdc3dfeSToby Isaac 1023fbdc3dfeSToby Isaac Note: The ordering of the basis functions, and the ordering of the derivatives in the jet, both follow the graded 1024fbdc3dfeSToby Isaac ordering of PetscDTIndexToGradedOrder() and PetscDTGradedOrderToIndex(). For example, in 3D, the polynomial with 1025d8f25ad8SToby 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); 1026fbdc3dfeSToby 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). 1027fbdc3dfeSToby Isaac 1028e3aa2e09SToby Isaac The implementation uses Kirby's singularity-free evaluation algorithm, https://doi.org/10.1145/1644001.1644006. 1029e3aa2e09SToby Isaac 1030fbdc3dfeSToby Isaac .seealso: PetscDTGradedOrderToIndex(), PetscDTIndexToGradedOrder(), PetscDTJacobiEvalJet() 1031fbdc3dfeSToby Isaac @*/ 1032fbdc3dfeSToby Isaac PetscErrorCode PetscDTPKDEvalJet(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt k, PetscReal p[]) 1033fbdc3dfeSToby Isaac { 1034fbdc3dfeSToby Isaac PetscInt degidx, kidx, d, pt; 1035fbdc3dfeSToby Isaac PetscInt Nk, Ndeg; 1036fbdc3dfeSToby Isaac PetscInt *ktup, *degtup; 1037fbdc3dfeSToby Isaac PetscReal *scales, initscale, scaleexp; 1038fbdc3dfeSToby Isaac 1039fbdc3dfeSToby Isaac PetscFunctionBegin; 10405f80ce2aSJacob Faibussowitsch CHKERRQ(PetscCitationsRegister(PKDCitation, &PKDCite)); 10415f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomialInt(dim + k, k, &Nk)); 10425f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomialInt(degree + dim, degree, &Ndeg)); 10435f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc2(dim, °tup, dim, &ktup)); 10445f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(Ndeg, &scales)); 1045fbdc3dfeSToby Isaac initscale = 1.; 1046fbdc3dfeSToby Isaac if (dim > 1) { 10475f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomial(dim,2,&scaleexp)); 10482f613bf5SBarry Smith initscale = PetscPowReal(2.,scaleexp*0.5); 1049fbdc3dfeSToby Isaac } 1050fbdc3dfeSToby Isaac for (degidx = 0; degidx < Ndeg; degidx++) { 1051fbdc3dfeSToby Isaac PetscInt e, i; 1052fbdc3dfeSToby Isaac PetscInt m1idx = -1, m2idx = -1; 1053fbdc3dfeSToby Isaac PetscInt n; 1054fbdc3dfeSToby Isaac PetscInt degsum; 1055fbdc3dfeSToby Isaac PetscReal alpha; 1056fbdc3dfeSToby Isaac PetscReal cnm1, cnm1x, cnm2; 1057fbdc3dfeSToby Isaac PetscReal norm; 1058fbdc3dfeSToby Isaac 10595f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTIndexToGradedOrder(dim, degidx, degtup)); 1060fbdc3dfeSToby Isaac for (d = dim - 1; d >= 0; d--) if (degtup[d]) break; 1061fbdc3dfeSToby Isaac if (d < 0) { /* constant is 1 everywhere, all derivatives are zero */ 1062fbdc3dfeSToby Isaac scales[degidx] = initscale; 1063fbdc3dfeSToby Isaac for (e = 0; e < dim; e++) { 10645f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTJacobiNorm(e,0.,0,&norm)); 1065fbdc3dfeSToby Isaac scales[degidx] /= norm; 1066fbdc3dfeSToby Isaac } 1067fbdc3dfeSToby Isaac for (i = 0; i < npoints; i++) p[degidx * Nk * npoints + i] = 1.; 1068fbdc3dfeSToby Isaac for (i = 0; i < (Nk - 1) * npoints; i++) p[(degidx * Nk + 1) * npoints + i] = 0.; 1069fbdc3dfeSToby Isaac continue; 1070fbdc3dfeSToby Isaac } 1071fbdc3dfeSToby Isaac n = degtup[d]; 1072fbdc3dfeSToby Isaac degtup[d]--; 10735f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTGradedOrderToIndex(dim, degtup, &m1idx)); 1074fbdc3dfeSToby Isaac if (degtup[d] > 0) { 1075fbdc3dfeSToby Isaac degtup[d]--; 10765f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTGradedOrderToIndex(dim, degtup, &m2idx)); 1077fbdc3dfeSToby Isaac degtup[d]++; 1078fbdc3dfeSToby Isaac } 1079fbdc3dfeSToby Isaac degtup[d]++; 1080fbdc3dfeSToby Isaac for (e = 0, degsum = 0; e < d; e++) degsum += degtup[e]; 1081fbdc3dfeSToby Isaac alpha = 2 * degsum + d; 1082fbdc3dfeSToby Isaac PetscDTJacobiRecurrence_Internal(n,alpha,0.,cnm1,cnm1x,cnm2); 1083fbdc3dfeSToby Isaac 1084fbdc3dfeSToby Isaac scales[degidx] = initscale; 1085fbdc3dfeSToby Isaac for (e = 0, degsum = 0; e < dim; e++) { 1086fbdc3dfeSToby Isaac PetscInt f; 1087fbdc3dfeSToby Isaac PetscReal ealpha; 1088fbdc3dfeSToby Isaac PetscReal enorm; 1089fbdc3dfeSToby Isaac 1090fbdc3dfeSToby Isaac ealpha = 2 * degsum + e; 1091fbdc3dfeSToby Isaac for (f = 0; f < degsum; f++) scales[degidx] *= 2.; 10925f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTJacobiNorm(ealpha,0.,degtup[e],&enorm)); 1093fbdc3dfeSToby Isaac scales[degidx] /= enorm; 1094fbdc3dfeSToby Isaac degsum += degtup[e]; 1095fbdc3dfeSToby Isaac } 1096fbdc3dfeSToby Isaac 1097fbdc3dfeSToby Isaac for (pt = 0; pt < npoints; pt++) { 1098fbdc3dfeSToby Isaac /* compute the multipliers */ 1099fbdc3dfeSToby Isaac PetscReal thetanm1, thetanm1x, thetanm2; 1100fbdc3dfeSToby Isaac 1101fbdc3dfeSToby Isaac thetanm1x = dim - (d+1) + 2.*points[pt * dim + d]; 1102fbdc3dfeSToby Isaac for (e = d+1; e < dim; e++) thetanm1x += points[pt * dim + e]; 1103fbdc3dfeSToby Isaac thetanm1x *= 0.5; 1104fbdc3dfeSToby Isaac thetanm1 = (2. - (dim-(d+1))); 1105fbdc3dfeSToby Isaac for (e = d+1; e < dim; e++) thetanm1 -= points[pt * dim + e]; 1106fbdc3dfeSToby Isaac thetanm1 *= 0.5; 1107fbdc3dfeSToby Isaac thetanm2 = thetanm1 * thetanm1; 1108fbdc3dfeSToby Isaac 1109fbdc3dfeSToby Isaac for (kidx = 0; kidx < Nk; kidx++) { 1110fbdc3dfeSToby Isaac PetscInt f; 1111fbdc3dfeSToby Isaac 11125f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTIndexToGradedOrder(dim, kidx, ktup)); 1113fbdc3dfeSToby Isaac /* first sum in the same derivative terms */ 1114fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] = (cnm1 * thetanm1 + cnm1x * thetanm1x) * p[(m1idx * Nk + kidx) * npoints + pt]; 1115fbdc3dfeSToby Isaac if (m2idx >= 0) { 1116fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] -= cnm2 * thetanm2 * p[(m2idx * Nk + kidx) * npoints + pt]; 1117fbdc3dfeSToby Isaac } 1118fbdc3dfeSToby Isaac 1119fbdc3dfeSToby Isaac for (f = d; f < dim; f++) { 1120fbdc3dfeSToby Isaac PetscInt km1idx, mplty = ktup[f]; 1121fbdc3dfeSToby Isaac 1122fbdc3dfeSToby Isaac if (!mplty) continue; 1123fbdc3dfeSToby Isaac ktup[f]--; 11245f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTGradedOrderToIndex(dim, ktup, &km1idx)); 1125fbdc3dfeSToby Isaac 1126fbdc3dfeSToby Isaac /* the derivative of cnm1x * thetanm1x wrt x variable f is 0.5 * cnm1x if f > d otherwise it is cnm1x */ 1127fbdc3dfeSToby Isaac /* the derivative of cnm1 * thetanm1 wrt x variable f is 0 if f == d, otherwise it is -0.5 * cnm1 */ 1128fbdc3dfeSToby Isaac /* the derivative of -cnm2 * thetanm2 wrt x variable f is 0 if f == d, otherwise it is cnm2 * thetanm1 */ 1129fbdc3dfeSToby Isaac if (f > d) { 1130fbdc3dfeSToby Isaac PetscInt f2; 1131fbdc3dfeSToby Isaac 1132fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] += mplty * 0.5 * (cnm1x - cnm1) * p[(m1idx * Nk + km1idx) * npoints + pt]; 1133fbdc3dfeSToby Isaac if (m2idx >= 0) { 1134fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] += mplty * cnm2 * thetanm1 * p[(m2idx * Nk + km1idx) * npoints + pt]; 1135fbdc3dfeSToby Isaac /* second derivatives of -cnm2 * thetanm2 wrt x variable f,f2 is like - 0.5 * cnm2 */ 1136fbdc3dfeSToby Isaac for (f2 = f; f2 < dim; f2++) { 1137fbdc3dfeSToby Isaac PetscInt km2idx, mplty2 = ktup[f2]; 1138fbdc3dfeSToby Isaac PetscInt factor; 1139fbdc3dfeSToby Isaac 1140fbdc3dfeSToby Isaac if (!mplty2) continue; 1141fbdc3dfeSToby Isaac ktup[f2]--; 11425f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTGradedOrderToIndex(dim, ktup, &km2idx)); 1143fbdc3dfeSToby Isaac 1144fbdc3dfeSToby Isaac factor = mplty * mplty2; 1145fbdc3dfeSToby Isaac if (f == f2) factor /= 2; 1146fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] -= 0.5 * factor * cnm2 * p[(m2idx * Nk + km2idx) * npoints + pt]; 1147fbdc3dfeSToby Isaac ktup[f2]++; 1148fbdc3dfeSToby Isaac } 11493034baaeSToby Isaac } 1150fbdc3dfeSToby Isaac } else { 1151fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] += mplty * cnm1x * p[(m1idx * Nk + km1idx) * npoints + pt]; 1152fbdc3dfeSToby Isaac } 1153fbdc3dfeSToby Isaac ktup[f]++; 1154fbdc3dfeSToby Isaac } 1155fbdc3dfeSToby Isaac } 1156fbdc3dfeSToby Isaac } 1157fbdc3dfeSToby Isaac } 1158fbdc3dfeSToby Isaac for (degidx = 0; degidx < Ndeg; degidx++) { 1159fbdc3dfeSToby Isaac PetscReal scale = scales[degidx]; 1160fbdc3dfeSToby Isaac PetscInt i; 1161fbdc3dfeSToby Isaac 1162fbdc3dfeSToby Isaac for (i = 0; i < Nk * npoints; i++) p[degidx*Nk*npoints + i] *= scale; 1163fbdc3dfeSToby Isaac } 11645f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(scales)); 11655f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree2(degtup, ktup)); 1166fbdc3dfeSToby Isaac PetscFunctionReturn(0); 1167fbdc3dfeSToby Isaac } 1168fbdc3dfeSToby Isaac 1169d8f25ad8SToby Isaac /*@ 1170d8f25ad8SToby Isaac PetscDTPTrimmedSize - The size of the trimmed polynomial space of k-forms with a given degree and form degree, 1171d8f25ad8SToby Isaac which can be evaluated in PetscDTPTrimmedEvalJet(). 1172d8f25ad8SToby Isaac 1173d8f25ad8SToby Isaac Input Parameters: 1174d8f25ad8SToby Isaac + dim - the number of variables in the multivariate polynomials 1175d8f25ad8SToby Isaac . degree - the degree (sum of degrees on the variables in a monomial) of the trimmed polynomial space. 1176d8f25ad8SToby Isaac - formDegree - the degree of the form 1177d8f25ad8SToby Isaac 1178d8f25ad8SToby Isaac Output Argments: 1179d8f25ad8SToby Isaac - size - The number ((dim + degree) choose (dim + formDegree)) x ((degree + formDegree - 1) choose (formDegree)) 1180d8f25ad8SToby Isaac 1181d8f25ad8SToby Isaac Level: advanced 1182d8f25ad8SToby Isaac 1183d8f25ad8SToby Isaac .seealso: PetscDTPTrimmedEvalJet() 1184d8f25ad8SToby Isaac @*/ 1185d8f25ad8SToby Isaac PetscErrorCode PetscDTPTrimmedSize(PetscInt dim, PetscInt degree, PetscInt formDegree, PetscInt *size) 1186d8f25ad8SToby Isaac { 1187d8f25ad8SToby Isaac PetscInt Nrk, Nbpt; // number of trimmed polynomials 1188d8f25ad8SToby Isaac 1189d8f25ad8SToby Isaac PetscFunctionBegin; 1190d8f25ad8SToby Isaac formDegree = PetscAbsInt(formDegree); 11915f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomialInt(degree + dim, degree + formDegree, &Nbpt)); 11925f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomialInt(degree + formDegree - 1, formDegree, &Nrk)); 1193d8f25ad8SToby Isaac Nbpt *= Nrk; 1194d8f25ad8SToby Isaac *size = Nbpt; 1195d8f25ad8SToby Isaac PetscFunctionReturn(0); 1196d8f25ad8SToby Isaac } 1197d8f25ad8SToby Isaac 1198d8f25ad8SToby Isaac /* there was a reference implementation based on section 4.4 of Arnold, Falk & Winther (acta numerica, 2006), but it 1199d8f25ad8SToby Isaac * was inferior to this implementation */ 1200d8f25ad8SToby Isaac static PetscErrorCode PetscDTPTrimmedEvalJet_Internal(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt formDegree, PetscInt jetDegree, PetscReal p[]) 1201d8f25ad8SToby Isaac { 1202d8f25ad8SToby Isaac PetscInt formDegreeOrig = formDegree; 1203d8f25ad8SToby Isaac PetscBool formNegative = (formDegreeOrig < 0) ? PETSC_TRUE : PETSC_FALSE; 1204d8f25ad8SToby Isaac 1205d8f25ad8SToby Isaac PetscFunctionBegin; 1206d8f25ad8SToby Isaac formDegree = PetscAbsInt(formDegreeOrig); 1207d8f25ad8SToby Isaac if (formDegree == 0) { 12085f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTPKDEvalJet(dim, npoints, points, degree, jetDegree, p)); 1209d8f25ad8SToby Isaac PetscFunctionReturn(0); 1210d8f25ad8SToby Isaac } 1211d8f25ad8SToby Isaac if (formDegree == dim) { 12125f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTPKDEvalJet(dim, npoints, points, degree - 1, jetDegree, p)); 1213d8f25ad8SToby Isaac PetscFunctionReturn(0); 1214d8f25ad8SToby Isaac } 1215d8f25ad8SToby Isaac PetscInt Nbpt; 12165f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTPTrimmedSize(dim, degree, formDegree, &Nbpt)); 1217d8f25ad8SToby Isaac PetscInt Nf; 12185f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomialInt(dim, formDegree, &Nf)); 1219d8f25ad8SToby Isaac PetscInt Nk; 12205f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomialInt(dim + jetDegree, dim, &Nk)); 12215f80ce2aSJacob Faibussowitsch CHKERRQ(PetscArrayzero(p, Nbpt * Nf * Nk * npoints)); 1222d8f25ad8SToby Isaac 1223d8f25ad8SToby Isaac PetscInt Nbpm1; // number of scalar polynomials up to degree - 1; 12245f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomialInt(dim + degree - 1, dim, &Nbpm1)); 1225d8f25ad8SToby Isaac PetscReal *p_scalar; 12265f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(Nbpm1 * Nk * npoints, &p_scalar)); 12275f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTPKDEvalJet(dim, npoints, points, degree - 1, jetDegree, p_scalar)); 1228d8f25ad8SToby Isaac PetscInt total = 0; 1229d8f25ad8SToby Isaac // First add the full polynomials up to degree - 1 into the basis: take the scalar 1230d8f25ad8SToby Isaac // and copy one for each form component 1231d8f25ad8SToby Isaac for (PetscInt i = 0; i < Nbpm1; i++) { 1232d8f25ad8SToby Isaac const PetscReal *src = &p_scalar[i * Nk * npoints]; 1233d8f25ad8SToby Isaac for (PetscInt f = 0; f < Nf; f++) { 1234d8f25ad8SToby Isaac PetscReal *dest = &p[(total++ * Nf + f) * Nk * npoints]; 12355f80ce2aSJacob Faibussowitsch CHKERRQ(PetscArraycpy(dest, src, Nk * npoints)); 1236d8f25ad8SToby Isaac } 1237d8f25ad8SToby Isaac } 1238d8f25ad8SToby Isaac PetscInt *form_atoms; 12395f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(formDegree + 1, &form_atoms)); 1240d8f25ad8SToby Isaac // construct the interior product pattern 1241d8f25ad8SToby Isaac PetscInt (*pattern)[3]; 1242d8f25ad8SToby Isaac PetscInt Nf1; // number of formDegree + 1 forms 12435f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomialInt(dim, formDegree + 1, &Nf1)); 1244d8f25ad8SToby Isaac PetscInt nnz = Nf1 * (formDegree+1); 12455f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(Nf1 * (formDegree+1), &pattern)); 12465f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTAltVInteriorPattern(dim, formDegree+1, pattern)); 1247d8f25ad8SToby Isaac PetscReal centroid = (1. - dim) / (dim + 1.); 1248d8f25ad8SToby Isaac PetscInt *deriv; 12495f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(dim, &deriv)); 1250d8f25ad8SToby Isaac for (PetscInt d = dim; d >= formDegree + 1; d--) { 1251d8f25ad8SToby Isaac PetscInt Nfd1; // number of formDegree + 1 forms in dimension d that include dx_0 1252d8f25ad8SToby Isaac // (equal to the number of formDegree forms in dimension d-1) 12535f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomialInt(d - 1, formDegree, &Nfd1)); 1254d8f25ad8SToby Isaac // The number of homogeneous (degree-1) scalar polynomials in d variables 1255d8f25ad8SToby Isaac PetscInt Nh; 12565f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomialInt(d - 1 + degree - 1, d - 1, &Nh)); 1257d8f25ad8SToby Isaac const PetscReal *h_scalar = &p_scalar[(Nbpm1 - Nh) * Nk * npoints]; 1258d8f25ad8SToby Isaac for (PetscInt b = 0; b < Nh; b++) { 1259d8f25ad8SToby Isaac const PetscReal *h_s = &h_scalar[b * Nk * npoints]; 1260d8f25ad8SToby Isaac for (PetscInt f = 0; f < Nfd1; f++) { 1261d8f25ad8SToby Isaac // construct all formDegree+1 forms that start with dx_(dim - d) /\ ... 1262d8f25ad8SToby Isaac form_atoms[0] = dim - d; 12635f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTEnumSubset(d-1, formDegree, f, &form_atoms[1])); 1264d8f25ad8SToby Isaac for (PetscInt i = 0; i < formDegree; i++) { 1265d8f25ad8SToby Isaac form_atoms[1+i] += form_atoms[0] + 1; 1266d8f25ad8SToby Isaac } 1267d8f25ad8SToby Isaac PetscInt f_ind; // index of the resulting form 12685f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTSubsetIndex(dim, formDegree + 1, form_atoms, &f_ind)); 1269d8f25ad8SToby Isaac PetscReal *p_f = &p[total++ * Nf * Nk * npoints]; 1270d8f25ad8SToby Isaac for (PetscInt nz = 0; nz < nnz; nz++) { 1271d8f25ad8SToby Isaac PetscInt i = pattern[nz][0]; // formDegree component 1272d8f25ad8SToby Isaac PetscInt j = pattern[nz][1]; // (formDegree + 1) component 1273d8f25ad8SToby Isaac PetscInt v = pattern[nz][2]; // coordinate component 1274d8f25ad8SToby Isaac PetscReal scale = v < 0 ? -1. : 1.; 1275d8f25ad8SToby Isaac 1276d8f25ad8SToby Isaac i = formNegative ? (Nf - 1 - i) : i; 1277d8f25ad8SToby Isaac scale = (formNegative && (i & 1)) ? -scale : scale; 1278d8f25ad8SToby Isaac v = v < 0 ? -(v + 1) : v; 1279d8f25ad8SToby Isaac if (j != f_ind) { 1280d8f25ad8SToby Isaac continue; 1281d8f25ad8SToby Isaac } 1282d8f25ad8SToby Isaac PetscReal *p_i = &p_f[i * Nk * npoints]; 1283d8f25ad8SToby Isaac for (PetscInt jet = 0; jet < Nk; jet++) { 1284d8f25ad8SToby Isaac const PetscReal *h_jet = &h_s[jet * npoints]; 1285d8f25ad8SToby Isaac PetscReal *p_jet = &p_i[jet * npoints]; 1286d8f25ad8SToby Isaac 1287d8f25ad8SToby Isaac for (PetscInt pt = 0; pt < npoints; pt++) { 1288d8f25ad8SToby Isaac p_jet[pt] += scale * h_jet[pt] * (points[pt * dim + v] - centroid); 1289d8f25ad8SToby Isaac } 12905f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTIndexToGradedOrder(dim, jet, deriv)); 1291d8f25ad8SToby Isaac deriv[v]++; 1292d8f25ad8SToby Isaac PetscReal mult = deriv[v]; 1293d8f25ad8SToby Isaac PetscInt l; 12945f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTGradedOrderToIndex(dim, deriv, &l)); 1295d8f25ad8SToby Isaac if (l >= Nk) { 1296d8f25ad8SToby Isaac continue; 1297d8f25ad8SToby Isaac } 1298d8f25ad8SToby Isaac p_jet = &p_i[l * npoints]; 1299d8f25ad8SToby Isaac for (PetscInt pt = 0; pt < npoints; pt++) { 1300d8f25ad8SToby Isaac p_jet[pt] += scale * mult * h_jet[pt]; 1301d8f25ad8SToby Isaac } 1302d8f25ad8SToby Isaac deriv[v]--; 1303d8f25ad8SToby Isaac } 1304d8f25ad8SToby Isaac } 1305d8f25ad8SToby Isaac } 1306d8f25ad8SToby Isaac } 1307d8f25ad8SToby Isaac } 13082c71b3e2SJacob Faibussowitsch PetscCheckFalse(total != Nbpt,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Incorrectly counted P trimmed polynomials"); 13095f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(deriv)); 13105f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(pattern)); 13115f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(form_atoms)); 13125f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(p_scalar)); 1313d8f25ad8SToby Isaac PetscFunctionReturn(0); 1314d8f25ad8SToby Isaac } 1315d8f25ad8SToby Isaac 1316d8f25ad8SToby Isaac /*@ 1317d8f25ad8SToby Isaac PetscDTPTrimmedEvalJet - Evaluate the jet (function and derivatives) of a basis of the trimmed polynomial k-forms up to 1318d8f25ad8SToby Isaac a given degree. 1319d8f25ad8SToby Isaac 1320d8f25ad8SToby Isaac Input Parameters: 1321d8f25ad8SToby Isaac + dim - the number of variables in the multivariate polynomials 1322d8f25ad8SToby Isaac . npoints - the number of points to evaluate the polynomials at 1323d8f25ad8SToby Isaac . points - [npoints x dim] array of point coordinates 1324d8f25ad8SToby Isaac . degree - the degree (sum of degrees on the variables in a monomial) of the trimmed polynomial space to evaluate. 1325d8f25ad8SToby Isaac There are ((dim + degree) choose (dim + formDegree)) x ((degree + formDegree - 1) choose (formDegree)) polynomials in this space. 1326d8f25ad8SToby Isaac (You can use PetscDTPTrimmedSize() to compute this size.) 1327d8f25ad8SToby Isaac . formDegree - the degree of the form 1328d8f25ad8SToby Isaac - jetDegree - the maximum order partial derivative to evaluate in the jet. There are ((dim + jetDegree) choose dim) partial derivatives 1329d8f25ad8SToby Isaac in the jet. Choosing jetDegree = 0 means to evaluate just the function and no derivatives 1330d8f25ad8SToby Isaac 1331d8f25ad8SToby Isaac Output Argments: 1332d8f25ad8SToby Isaac - p - an array containing the evaluations of the PKD polynomials' jets on the points. The size is 1333d8f25ad8SToby Isaac PetscDTPTrimmedSize() x ((dim + formDegree) choose dim) x ((dim + k) choose dim) x npoints, 1334d8f25ad8SToby Isaac which also describes the order of the dimensions of this 1335d8f25ad8SToby Isaac four-dimensional array: 1336d8f25ad8SToby Isaac the first (slowest varying) dimension is basis function index; 1337d8f25ad8SToby Isaac the second dimension is component of the form; 1338d8f25ad8SToby Isaac the third dimension is jet index; 1339d8f25ad8SToby Isaac the fourth (fastest varying) dimension is the index of the evaluation point. 1340d8f25ad8SToby Isaac 1341d8f25ad8SToby Isaac Level: advanced 1342d8f25ad8SToby Isaac 1343d8f25ad8SToby Isaac Note: The ordering of the basis functions is not graded, so the basis functions are not nested by degree like PetscDTPKDEvalJet(). 1344d8f25ad8SToby Isaac The basis functions are not an L2-orthonormal basis on any particular domain. 1345d8f25ad8SToby Isaac 1346d8f25ad8SToby Isaac The implementation is based on the description of the trimmed polynomials up to degree r as 1347d8f25ad8SToby Isaac the direct sum of polynomials up to degree (r-1) and the Koszul differential applied to 1348d8f25ad8SToby Isaac homogeneous polynomials of degree (r-1). 1349d8f25ad8SToby Isaac 1350d8f25ad8SToby Isaac .seealso: PetscDTPKDEvalJet(), PetscDTPTrimmedSize() 1351d8f25ad8SToby Isaac @*/ 1352d8f25ad8SToby Isaac PetscErrorCode PetscDTPTrimmedEvalJet(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt formDegree, PetscInt jetDegree, PetscReal p[]) 1353d8f25ad8SToby Isaac { 1354d8f25ad8SToby Isaac PetscFunctionBegin; 13555f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTPTrimmedEvalJet_Internal(dim, npoints, points, degree, formDegree, jetDegree, p)); 1356d8f25ad8SToby Isaac PetscFunctionReturn(0); 1357d8f25ad8SToby Isaac } 1358d8f25ad8SToby Isaac 1359e6a796c3SToby Isaac /* solve the symmetric tridiagonal eigenvalue system, writing the eigenvalues into eigs and the eigenvectors into V 1360e6a796c3SToby Isaac * with lds n; diag and subdiag are overwritten */ 1361e6a796c3SToby Isaac static PetscErrorCode PetscDTSymmetricTridiagonalEigensolve(PetscInt n, PetscReal diag[], PetscReal subdiag[], 1362e6a796c3SToby Isaac PetscReal eigs[], PetscScalar V[]) 1363e6a796c3SToby Isaac { 1364e6a796c3SToby Isaac char jobz = 'V'; /* eigenvalues and eigenvectors */ 1365e6a796c3SToby Isaac char range = 'A'; /* all eigenvalues will be found */ 1366e6a796c3SToby Isaac PetscReal VL = 0.; /* ignored because range is 'A' */ 1367e6a796c3SToby Isaac PetscReal VU = 0.; /* ignored because range is 'A' */ 1368e6a796c3SToby Isaac PetscBLASInt IL = 0; /* ignored because range is 'A' */ 1369e6a796c3SToby Isaac PetscBLASInt IU = 0; /* ignored because range is 'A' */ 1370e6a796c3SToby Isaac PetscReal abstol = 0.; /* unused */ 1371e6a796c3SToby Isaac PetscBLASInt bn, bm, ldz; /* bm will equal bn on exit */ 1372e6a796c3SToby Isaac PetscBLASInt *isuppz; 1373e6a796c3SToby Isaac PetscBLASInt lwork, liwork; 1374e6a796c3SToby Isaac PetscReal workquery; 1375e6a796c3SToby Isaac PetscBLASInt iworkquery; 1376e6a796c3SToby Isaac PetscBLASInt *iwork; 1377e6a796c3SToby Isaac PetscBLASInt info; 1378e6a796c3SToby Isaac PetscReal *work = NULL; 1379e6a796c3SToby Isaac 1380e6a796c3SToby Isaac PetscFunctionBegin; 1381e6a796c3SToby Isaac #if !defined(PETSCDTGAUSSIANQUADRATURE_EIG) 1382e6a796c3SToby Isaac SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "A LAPACK symmetric tridiagonal eigensolver could not be found"); 1383e6a796c3SToby Isaac #endif 13845f80ce2aSJacob Faibussowitsch CHKERRQ(PetscBLASIntCast(n, &bn)); 13855f80ce2aSJacob Faibussowitsch CHKERRQ(PetscBLASIntCast(n, &ldz)); 1386e6a796c3SToby Isaac #if !defined(PETSC_MISSING_LAPACK_STEGR) 13875f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(2 * n, &isuppz)); 1388e6a796c3SToby Isaac lwork = -1; 1389e6a796c3SToby Isaac liwork = -1; 1390e6a796c3SToby Isaac PetscStackCallBLAS("LAPACKstegr",LAPACKstegr_(&jobz,&range,&bn,diag,subdiag,&VL,&VU,&IL,&IU,&abstol,&bm,eigs,V,&ldz,isuppz,&workquery,&lwork,&iworkquery,&liwork,&info)); 1391*28b400f6SJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEGR error"); 1392e6a796c3SToby Isaac lwork = (PetscBLASInt) workquery; 1393e6a796c3SToby Isaac liwork = (PetscBLASInt) iworkquery; 13945f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc2(lwork, &work, liwork, &iwork)); 13955f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFPTrapPush(PETSC_FP_TRAP_OFF)); 1396e6a796c3SToby Isaac PetscStackCallBLAS("LAPACKstegr",LAPACKstegr_(&jobz,&range,&bn,diag,subdiag,&VL,&VU,&IL,&IU,&abstol,&bm,eigs,V,&ldz,isuppz,work,&lwork,iwork,&liwork,&info)); 13975f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFPTrapPop()); 1398*28b400f6SJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEGR error"); 13995f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree2(work, iwork)); 14005f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(isuppz)); 1401e6a796c3SToby Isaac #elif !defined(PETSC_MISSING_LAPACK_STEQR) 1402e6a796c3SToby Isaac jobz = 'I'; /* Compute eigenvalues and eigenvectors of the 1403e6a796c3SToby Isaac tridiagonal matrix. Z is initialized to the identity 1404e6a796c3SToby Isaac matrix. */ 14055f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(PetscMax(1,2*n-2),&work)); 1406e6a796c3SToby Isaac PetscStackCallBLAS("LAPACKsteqr",LAPACKsteqr_("I",&bn,diag,subdiag,V,&ldz,work,&info)); 14075f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFPTrapPop()); 1408*28b400f6SJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEQR error"); 14095f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(work)); 14105f80ce2aSJacob Faibussowitsch CHKERRQ(PetscArraycpy(eigs,diag,n)); 1411e6a796c3SToby Isaac #endif 1412e6a796c3SToby Isaac PetscFunctionReturn(0); 1413e6a796c3SToby Isaac } 1414e6a796c3SToby Isaac 1415e6a796c3SToby Isaac /* Formula for the weights at the endpoints (-1 and 1) of Gauss-Lobatto-Jacobi 1416e6a796c3SToby Isaac * quadrature rules on the interval [-1, 1] */ 1417e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussLobattoJacobiEndweights_Internal(PetscInt n, PetscReal alpha, PetscReal beta, PetscReal *leftw, PetscReal *rightw) 1418e6a796c3SToby Isaac { 1419e6a796c3SToby Isaac PetscReal twoab1; 1420e6a796c3SToby Isaac PetscInt m = n - 2; 1421e6a796c3SToby Isaac PetscReal a = alpha + 1.; 1422e6a796c3SToby Isaac PetscReal b = beta + 1.; 1423e6a796c3SToby Isaac PetscReal gra, grb; 1424e6a796c3SToby Isaac 1425e6a796c3SToby Isaac PetscFunctionBegin; 1426e6a796c3SToby Isaac twoab1 = PetscPowReal(2., a + b - 1.); 1427e6a796c3SToby Isaac #if defined(PETSC_HAVE_LGAMMA) 1428e6a796c3SToby Isaac grb = PetscExpReal(2. * PetscLGamma(b+1.) + PetscLGamma(m+1.) + PetscLGamma(m+a+1.) - 1429e6a796c3SToby Isaac (PetscLGamma(m+b+1) + PetscLGamma(m+a+b+1.))); 1430e6a796c3SToby Isaac gra = PetscExpReal(2. * PetscLGamma(a+1.) + PetscLGamma(m+1.) + PetscLGamma(m+b+1.) - 1431e6a796c3SToby Isaac (PetscLGamma(m+a+1) + PetscLGamma(m+a+b+1.))); 1432e6a796c3SToby Isaac #else 1433e6a796c3SToby Isaac { 1434e6a796c3SToby Isaac PetscInt alphai = (PetscInt) alpha; 1435e6a796c3SToby Isaac PetscInt betai = (PetscInt) beta; 143694e21283SToby Isaac PetscErrorCode ierr; 1437e6a796c3SToby Isaac 1438e6a796c3SToby Isaac if ((PetscReal) alphai == alpha && (PetscReal) betai == beta) { 1439e6a796c3SToby Isaac PetscReal binom1, binom2; 1440e6a796c3SToby Isaac 14415f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomial(m+b, b, &binom1)); 14425f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomial(m+a+b, b, &binom2)); 1443e6a796c3SToby Isaac grb = 1./ (binom1 * binom2); 14445f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomial(m+a, a, &binom1)); 14455f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTBinomial(m+a+b, a, &binom2)); 1446e6a796c3SToby Isaac gra = 1./ (binom1 * binom2); 1447e6a796c3SToby Isaac } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"lgamma() - math routine is unavailable."); 1448e6a796c3SToby Isaac } 1449e6a796c3SToby Isaac #endif 1450e6a796c3SToby Isaac *leftw = twoab1 * grb / b; 1451e6a796c3SToby Isaac *rightw = twoab1 * gra / a; 1452e6a796c3SToby Isaac PetscFunctionReturn(0); 1453e6a796c3SToby Isaac } 1454e6a796c3SToby Isaac 1455e6a796c3SToby Isaac /* Evaluates the nth jacobi polynomial with weight parameters a,b at a point x. 1456e6a796c3SToby Isaac Recurrence relations implemented from the pseudocode given in Karniadakis and Sherwin, Appendix B */ 14579fbee547SJacob Faibussowitsch static inline PetscErrorCode PetscDTComputeJacobi(PetscReal a, PetscReal b, PetscInt n, PetscReal x, PetscReal *P) 1458e6a796c3SToby Isaac { 145994e21283SToby Isaac PetscReal pn1, pn2; 146094e21283SToby Isaac PetscReal cnm1, cnm1x, cnm2; 1461e6a796c3SToby Isaac PetscInt k; 1462e6a796c3SToby Isaac 1463e6a796c3SToby Isaac PetscFunctionBegin; 1464e6a796c3SToby Isaac if (!n) {*P = 1.0; PetscFunctionReturn(0);} 146594e21283SToby Isaac PetscDTJacobiRecurrence_Internal(1,a,b,cnm1,cnm1x,cnm2); 146694e21283SToby Isaac pn2 = 1.; 146794e21283SToby Isaac pn1 = cnm1 + cnm1x*x; 146894e21283SToby Isaac if (n == 1) {*P = pn1; PetscFunctionReturn(0);} 1469e6a796c3SToby Isaac *P = 0.0; 1470e6a796c3SToby Isaac for (k = 2; k < n+1; ++k) { 147194e21283SToby Isaac PetscDTJacobiRecurrence_Internal(k,a,b,cnm1,cnm1x,cnm2); 1472e6a796c3SToby Isaac 147394e21283SToby Isaac *P = (cnm1 + cnm1x*x)*pn1 - cnm2*pn2; 1474e6a796c3SToby Isaac pn2 = pn1; 1475e6a796c3SToby Isaac pn1 = *P; 1476e6a796c3SToby Isaac } 1477e6a796c3SToby Isaac PetscFunctionReturn(0); 1478e6a796c3SToby Isaac } 1479e6a796c3SToby Isaac 1480e6a796c3SToby Isaac /* Evaluates the first derivative of P_{n}^{a,b} at a point x. */ 14819fbee547SJacob Faibussowitsch static inline PetscErrorCode PetscDTComputeJacobiDerivative(PetscReal a, PetscReal b, PetscInt n, PetscReal x, PetscInt k, PetscReal *P) 1482e6a796c3SToby Isaac { 1483e6a796c3SToby Isaac PetscReal nP; 1484e6a796c3SToby Isaac PetscInt i; 1485e6a796c3SToby Isaac 1486e6a796c3SToby Isaac PetscFunctionBegin; 148717a42bb7SSatish Balay *P = 0.0; 148817a42bb7SSatish Balay if (k > n) PetscFunctionReturn(0); 14895f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTComputeJacobi(a+k, b+k, n-k, x, &nP)); 1490e6a796c3SToby Isaac for (i = 0; i < k; i++) nP *= (a + b + n + 1. + i) * 0.5; 1491e6a796c3SToby Isaac *P = nP; 1492e6a796c3SToby Isaac PetscFunctionReturn(0); 1493e6a796c3SToby Isaac } 1494e6a796c3SToby Isaac 1495e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussJacobiQuadrature_Newton_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscReal x[], PetscReal w[]) 1496e6a796c3SToby Isaac { 1497e6a796c3SToby Isaac PetscInt maxIter = 100; 149894e21283SToby Isaac PetscReal eps = PetscExpReal(0.75 * PetscLogReal(PETSC_MACHINE_EPSILON)); 1499200b5abcSJed Brown PetscReal a1, a6, gf; 1500e6a796c3SToby Isaac PetscInt k; 1501e6a796c3SToby Isaac 1502e6a796c3SToby Isaac PetscFunctionBegin; 1503e6a796c3SToby Isaac 1504e6a796c3SToby Isaac a1 = PetscPowReal(2.0, a+b+1); 150594e21283SToby Isaac #if defined(PETSC_HAVE_LGAMMA) 1506200b5abcSJed Brown { 1507200b5abcSJed Brown PetscReal a2, a3, a4, a5; 150894e21283SToby Isaac a2 = PetscLGamma(a + npoints + 1); 150994e21283SToby Isaac a3 = PetscLGamma(b + npoints + 1); 151094e21283SToby Isaac a4 = PetscLGamma(a + b + npoints + 1); 151194e21283SToby Isaac a5 = PetscLGamma(npoints + 1); 151294e21283SToby Isaac gf = PetscExpReal(a2 + a3 - (a4 + a5)); 1513200b5abcSJed Brown } 1514e6a796c3SToby Isaac #else 1515e6a796c3SToby Isaac { 1516e6a796c3SToby Isaac PetscInt ia, ib; 1517e6a796c3SToby Isaac 1518e6a796c3SToby Isaac ia = (PetscInt) a; 1519e6a796c3SToby Isaac ib = (PetscInt) b; 152094e21283SToby Isaac gf = 1.; 152194e21283SToby Isaac if (ia == a && ia >= 0) { /* compute ratio of rising factorals wrt a */ 152294e21283SToby Isaac for (k = 0; k < ia; k++) gf *= (npoints + 1. + k) / (npoints + b + 1. + k); 152394e21283SToby Isaac } else if (b == b && ib >= 0) { /* compute ratio of rising factorials wrt b */ 152494e21283SToby Isaac for (k = 0; k < ib; k++) gf *= (npoints + 1. + k) / (npoints + a + 1. + k); 152594e21283SToby Isaac } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"lgamma() - math routine is unavailable."); 1526e6a796c3SToby Isaac } 1527e6a796c3SToby Isaac #endif 1528e6a796c3SToby Isaac 152994e21283SToby Isaac a6 = a1 * gf; 1530e6a796c3SToby Isaac /* Computes the m roots of P_{m}^{a,b} on [-1,1] by Newton's method with Chebyshev points as initial guesses. 1531e6a796c3SToby Isaac Algorithm implemented from the pseudocode given by Karniadakis and Sherwin and Python in FIAT */ 1532e6a796c3SToby Isaac for (k = 0; k < npoints; ++k) { 153394e21283SToby Isaac PetscReal r = PetscCosReal(PETSC_PI * (1. - (4.*k + 3. + 2.*b) / (4.*npoints + 2.*(a + b + 1.)))), dP; 1534e6a796c3SToby Isaac PetscInt j; 1535e6a796c3SToby Isaac 1536e6a796c3SToby Isaac if (k > 0) r = 0.5 * (r + x[k-1]); 1537e6a796c3SToby Isaac for (j = 0; j < maxIter; ++j) { 1538e6a796c3SToby Isaac PetscReal s = 0.0, delta, f, fp; 1539e6a796c3SToby Isaac PetscInt i; 1540e6a796c3SToby Isaac 1541e6a796c3SToby Isaac for (i = 0; i < k; ++i) s = s + 1.0 / (r - x[i]); 15425f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTComputeJacobi(a, b, npoints, r, &f)); 15435f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTComputeJacobiDerivative(a, b, npoints, r, 1, &fp)); 1544e6a796c3SToby Isaac delta = f / (fp - f * s); 1545e6a796c3SToby Isaac r = r - delta; 1546e6a796c3SToby Isaac if (PetscAbsReal(delta) < eps) break; 1547e6a796c3SToby Isaac } 1548e6a796c3SToby Isaac x[k] = r; 15495f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTComputeJacobiDerivative(a, b, npoints, x[k], 1, &dP)); 1550e6a796c3SToby Isaac w[k] = a6 / (1.0 - PetscSqr(x[k])) / PetscSqr(dP); 1551e6a796c3SToby Isaac } 1552e6a796c3SToby Isaac PetscFunctionReturn(0); 1553e6a796c3SToby Isaac } 1554e6a796c3SToby Isaac 155594e21283SToby Isaac /* Compute the diagonals of the Jacobi matrix used in Golub & Welsch algorithms for Gauss-Jacobi 1556e6a796c3SToby Isaac * quadrature weight calculations on [-1,1] for exponents (1. + x)^a (1.-x)^b */ 1557e6a796c3SToby Isaac static PetscErrorCode PetscDTJacobiMatrix_Internal(PetscInt nPoints, PetscReal a, PetscReal b, PetscReal *d, PetscReal *s) 1558e6a796c3SToby Isaac { 1559e6a796c3SToby Isaac PetscInt i; 1560e6a796c3SToby Isaac 1561e6a796c3SToby Isaac PetscFunctionBegin; 1562e6a796c3SToby Isaac for (i = 0; i < nPoints; i++) { 156394e21283SToby Isaac PetscReal A, B, C; 1564e6a796c3SToby Isaac 156594e21283SToby Isaac PetscDTJacobiRecurrence_Internal(i+1,a,b,A,B,C); 156694e21283SToby Isaac d[i] = -A / B; 156794e21283SToby Isaac if (i) s[i-1] *= C / B; 156894e21283SToby Isaac if (i < nPoints - 1) s[i] = 1. / B; 1569e6a796c3SToby Isaac } 1570e6a796c3SToby Isaac PetscFunctionReturn(0); 1571e6a796c3SToby Isaac } 1572e6a796c3SToby Isaac 1573e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussJacobiQuadrature_GolubWelsch_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscReal *x, PetscReal *w) 1574e6a796c3SToby Isaac { 1575e6a796c3SToby Isaac PetscReal mu0; 1576e6a796c3SToby Isaac PetscReal ga, gb, gab; 1577e6a796c3SToby Isaac PetscInt i; 1578e6a796c3SToby Isaac 1579e6a796c3SToby Isaac PetscFunctionBegin; 15805f80ce2aSJacob Faibussowitsch CHKERRQ(PetscCitationsRegister(GolubWelschCitation, &GolubWelschCite)); 1581e6a796c3SToby Isaac 1582e6a796c3SToby Isaac #if defined(PETSC_HAVE_TGAMMA) 1583e6a796c3SToby Isaac ga = PetscTGamma(a + 1); 1584e6a796c3SToby Isaac gb = PetscTGamma(b + 1); 1585e6a796c3SToby Isaac gab = PetscTGamma(a + b + 2); 1586e6a796c3SToby Isaac #else 1587e6a796c3SToby Isaac { 1588e6a796c3SToby Isaac PetscInt ia, ib; 1589e6a796c3SToby Isaac 1590e6a796c3SToby Isaac ia = (PetscInt) a; 1591e6a796c3SToby Isaac ib = (PetscInt) b; 1592e6a796c3SToby Isaac if (ia == a && ib == b && ia + 1 > 0 && ib + 1 > 0 && ia + ib + 2 > 0) { /* All gamma(x) terms are (x-1)! terms */ 15935f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTFactorial(ia, &ga)); 15945f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTFactorial(ib, &gb)); 15955f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTFactorial(ia + ib + 1, &gb)); 1596e6a796c3SToby Isaac } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"tgamma() - math routine is unavailable."); 1597e6a796c3SToby Isaac } 1598e6a796c3SToby Isaac #endif 1599e6a796c3SToby Isaac mu0 = PetscPowReal(2.,a + b + 1.) * ga * gb / gab; 1600e6a796c3SToby Isaac 1601e6a796c3SToby Isaac #if defined(PETSCDTGAUSSIANQUADRATURE_EIG) 1602e6a796c3SToby Isaac { 1603e6a796c3SToby Isaac PetscReal *diag, *subdiag; 1604e6a796c3SToby Isaac PetscScalar *V; 1605e6a796c3SToby Isaac 16065f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc2(npoints, &diag, npoints, &subdiag)); 16075f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(npoints*npoints, &V)); 16085f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTJacobiMatrix_Internal(npoints, a, b, diag, subdiag)); 1609e6a796c3SToby Isaac for (i = 0; i < npoints - 1; i++) subdiag[i] = PetscSqrtReal(subdiag[i]); 16105f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTSymmetricTridiagonalEigensolve(npoints, diag, subdiag, x, V)); 161194e21283SToby Isaac for (i = 0; i < npoints; i++) w[i] = PetscSqr(PetscRealPart(V[i * npoints])) * mu0; 16125f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(V)); 16135f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree2(diag, subdiag)); 1614e6a796c3SToby Isaac } 1615e6a796c3SToby Isaac #else 1616e6a796c3SToby Isaac SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "A LAPACK symmetric tridiagonal eigensolver could not be found"); 1617e6a796c3SToby Isaac #endif 161894e21283SToby Isaac { /* As of March 2, 2020, The Sun Performance Library breaks the LAPACK contract for xstegr and xsteqr: the 161994e21283SToby Isaac eigenvalues are not guaranteed to be in ascending order. So we heave a passive aggressive sigh and check that 162094e21283SToby Isaac the eigenvalues are sorted */ 162194e21283SToby Isaac PetscBool sorted; 162294e21283SToby Isaac 16235f80ce2aSJacob Faibussowitsch CHKERRQ(PetscSortedReal(npoints, x, &sorted)); 162494e21283SToby Isaac if (!sorted) { 162594e21283SToby Isaac PetscInt *order, i; 162694e21283SToby Isaac PetscReal *tmp; 162794e21283SToby Isaac 16285f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc2(npoints, &order, npoints, &tmp)); 162994e21283SToby Isaac for (i = 0; i < npoints; i++) order[i] = i; 16305f80ce2aSJacob Faibussowitsch CHKERRQ(PetscSortRealWithPermutation(npoints, x, order)); 16315f80ce2aSJacob Faibussowitsch CHKERRQ(PetscArraycpy(tmp, x, npoints)); 163294e21283SToby Isaac for (i = 0; i < npoints; i++) x[i] = tmp[order[i]]; 16335f80ce2aSJacob Faibussowitsch CHKERRQ(PetscArraycpy(tmp, w, npoints)); 163494e21283SToby Isaac for (i = 0; i < npoints; i++) w[i] = tmp[order[i]]; 16355f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree2(order, tmp)); 163694e21283SToby Isaac } 163794e21283SToby Isaac } 1638e6a796c3SToby Isaac PetscFunctionReturn(0); 1639e6a796c3SToby Isaac } 1640e6a796c3SToby Isaac 1641e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussJacobiQuadrature_Internal(PetscInt npoints,PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[], PetscBool newton) 1642e6a796c3SToby Isaac { 1643e6a796c3SToby Isaac PetscFunctionBegin; 16442c71b3e2SJacob Faibussowitsch PetscCheckFalse(npoints < 1,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of points must be positive"); 1645e6a796c3SToby Isaac /* If asking for a 1D Lobatto point, just return the non-Lobatto 1D point */ 16462c71b3e2SJacob Faibussowitsch PetscCheckFalse(alpha <= -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"alpha must be > -1."); 16472c71b3e2SJacob Faibussowitsch PetscCheckFalse(beta <= -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"beta must be > -1."); 1648e6a796c3SToby Isaac 1649e6a796c3SToby Isaac if (newton) { 16505f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTGaussJacobiQuadrature_Newton_Internal(npoints, alpha, beta, x, w)); 1651e6a796c3SToby Isaac } else { 16525f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTGaussJacobiQuadrature_GolubWelsch_Internal(npoints, alpha, beta, x, w)); 1653e6a796c3SToby Isaac } 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; 16975f80ce2aSJacob Faibussowitsch CHKERRQ(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; 17122c71b3e2SJacob Faibussowitsch PetscCheckFalse(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 */ 17142c71b3e2SJacob Faibussowitsch PetscCheckFalse(alpha <= -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"alpha must be > -1."); 17152c71b3e2SJacob Faibussowitsch PetscCheckFalse(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) { 17205f80ce2aSJacob Faibussowitsch CHKERRQ(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 } 17255f80ce2aSJacob Faibussowitsch CHKERRQ(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; 17555f80ce2aSJacob Faibussowitsch CHKERRQ(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 178437045ce4SJed Brown .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; 17915f80ce2aSJacob Faibussowitsch CHKERRQ(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 18258272889dSSatish Balay .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; 18332c71b3e2SJacob Faibussowitsch PetscCheckFalse(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); 18355f80ce2aSJacob Faibussowitsch CHKERRQ(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 1856744bafbcSMatthew G. Knepley .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; 18645f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(totpoints*dim,&x)); 18655f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(totpoints*Nc,&w)); 1866744bafbcSMatthew G. Knepley /* Set up the Golub-Welsch system */ 1867744bafbcSMatthew G. Knepley switch (dim) { 1868744bafbcSMatthew G. Knepley case 0: 18695f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(x)); 18705f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(w)); 18715f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(1, &x)); 18725f80ce2aSJacob Faibussowitsch CHKERRQ(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: 18775f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(npoints,&ww)); 18785f80ce2aSJacob Faibussowitsch CHKERRQ(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]; 18805f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(ww)); 1881744bafbcSMatthew G. Knepley break; 1882744bafbcSMatthew G. Knepley case 2: 18835f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc2(npoints,&xw,npoints,&ww)); 18845f80ce2aSJacob Faibussowitsch CHKERRQ(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 } 18925f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree2(xw,ww)); 1893744bafbcSMatthew G. Knepley break; 1894744bafbcSMatthew G. Knepley case 3: 18955f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc2(npoints,&xw,npoints,&ww)); 18965f80ce2aSJacob Faibussowitsch CHKERRQ(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 } 19075f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree2(xw,ww)); 1908744bafbcSMatthew G. Knepley break; 1909744bafbcSMatthew G. Knepley default: 191098921bdaSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct quadrature rule for dimension %d", dim); 1911744bafbcSMatthew G. Knepley } 19125f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureCreate(PETSC_COMM_SELF, q)); 19135f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureSetOrder(*q, 2*npoints-1)); 19145f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureSetData(*q, dim, Nc, totpoints, x, w)); 19155f80ce2aSJacob Faibussowitsch CHKERRQ(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 1941744bafbcSMatthew G. Knepley .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; 19522c71b3e2SJacob Faibussowitsch PetscCheckFalse((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; 19555f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(totpoints*dim, &x)); 19565f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(totpoints*Nc, &w)); 19575f80ce2aSJacob Faibussowitsch CHKERRQ(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); 19635f80ce2aSJacob Faibussowitsch CHKERRQ(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 } 19765f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree2(p1, w1)); 19775f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureCreate(PETSC_COMM_SELF, q)); 19785f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureSetOrder(*q, 2*npoints-1)); 19795f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureSetData(*q, dim, Nc, totpoints, x, w)); 19805f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectChangeTypeName((PetscObject)*q,"StroudConical")); 1981494e7359SMatthew G. Knepley PetscFunctionReturn(0); 1982494e7359SMatthew G. Knepley } 1983494e7359SMatthew G. Knepley 1984f5f57ec0SBarry Smith /*@ 1985b3c0f97bSTom Klotz PetscDTTanhSinhTensorQuadrature - create tanh-sinh quadrature for a tensor product cell 1986b3c0f97bSTom Klotz 1987b3c0f97bSTom Klotz Not Collective 1988b3c0f97bSTom Klotz 19894165533cSJose E. Roman Input Parameters: 1990b3c0f97bSTom Klotz + dim - The cell dimension 1991b3c0f97bSTom Klotz . level - The number of points in one dimension, 2^l 1992b3c0f97bSTom Klotz . a - left end of interval (often-1) 1993b3c0f97bSTom Klotz - b - right end of interval (often +1) 1994b3c0f97bSTom Klotz 19954165533cSJose E. Roman Output Parameter: 1996b3c0f97bSTom Klotz . q - A PetscQuadrature object 1997b3c0f97bSTom Klotz 1998b3c0f97bSTom Klotz Level: intermediate 1999b3c0f97bSTom Klotz 2000b3c0f97bSTom Klotz .seealso: PetscDTGaussTensorQuadrature() 2001b3c0f97bSTom Klotz @*/ 2002b3c0f97bSTom Klotz PetscErrorCode PetscDTTanhSinhTensorQuadrature(PetscInt dim, PetscInt level, PetscReal a, PetscReal b, PetscQuadrature *q) 2003b3c0f97bSTom Klotz { 2004b3c0f97bSTom Klotz const PetscInt p = 16; /* Digits of precision in the evaluation */ 2005b3c0f97bSTom Klotz const PetscReal alpha = (b-a)/2.; /* Half-width of the integration interval */ 2006b3c0f97bSTom Klotz const PetscReal beta = (b+a)/2.; /* Center of the integration interval */ 2007b3c0f97bSTom Klotz const PetscReal h = PetscPowReal(2.0, -level); /* Step size, length between x_k */ 2008d84b4d08SMatthew G. Knepley PetscReal xk; /* Quadrature point x_k on reference domain [-1, 1] */ 2009b3c0f97bSTom Klotz PetscReal wk = 0.5*PETSC_PI; /* Quadrature weight at x_k */ 2010b3c0f97bSTom Klotz PetscReal *x, *w; 2011b3c0f97bSTom Klotz PetscInt K, k, npoints; 2012b3c0f97bSTom Klotz 2013b3c0f97bSTom Klotz PetscFunctionBegin; 20142c71b3e2SJacob Faibussowitsch PetscCheckFalse(dim > 1,PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %d not yet implemented", dim); 2015*28b400f6SJacob Faibussowitsch PetscCheck(level,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a number of significant digits"); 2016b3c0f97bSTom Klotz /* Find K such that the weights are < 32 digits of precision */ 2017b3c0f97bSTom Klotz for (K = 1; PetscAbsReal(PetscLog10Real(wk)) < 2*p; ++K) { 20189add2064SThomas Klotz wk = 0.5*h*PETSC_PI*PetscCoshReal(K*h)/PetscSqr(PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(K*h))); 2019b3c0f97bSTom Klotz } 20205f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureCreate(PETSC_COMM_SELF, q)); 20215f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureSetOrder(*q, 2*K+1)); 2022b3c0f97bSTom Klotz npoints = 2*K-1; 20235f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(npoints*dim, &x)); 20245f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(npoints, &w)); 2025b3c0f97bSTom Klotz /* Center term */ 2026b3c0f97bSTom Klotz x[0] = beta; 2027b3c0f97bSTom Klotz w[0] = 0.5*alpha*PETSC_PI; 2028b3c0f97bSTom Klotz for (k = 1; k < K; ++k) { 20299add2064SThomas Klotz wk = 0.5*alpha*h*PETSC_PI*PetscCoshReal(k*h)/PetscSqr(PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(k*h))); 20301118d4bcSLisandro Dalcin xk = PetscTanhReal(0.5*PETSC_PI*PetscSinhReal(k*h)); 2031b3c0f97bSTom Klotz x[2*k-1] = -alpha*xk+beta; 2032b3c0f97bSTom Klotz w[2*k-1] = wk; 2033b3c0f97bSTom Klotz x[2*k+0] = alpha*xk+beta; 2034b3c0f97bSTom Klotz w[2*k+0] = wk; 2035b3c0f97bSTom Klotz } 20365f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureSetData(*q, dim, 1, npoints, x, w)); 2037b3c0f97bSTom Klotz PetscFunctionReturn(0); 2038b3c0f97bSTom Klotz } 2039b3c0f97bSTom Klotz 2040d6685f55SMatthew G. Knepley PetscErrorCode PetscDTTanhSinhIntegrate(void (*func)(const PetscReal[], void *, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, void *ctx, PetscReal *sol) 2041b3c0f97bSTom Klotz { 2042b3c0f97bSTom Klotz const PetscInt p = 16; /* Digits of precision in the evaluation */ 2043b3c0f97bSTom Klotz const PetscReal alpha = (b-a)/2.; /* Half-width of the integration interval */ 2044b3c0f97bSTom Klotz const PetscReal beta = (b+a)/2.; /* Center of the integration interval */ 2045b3c0f97bSTom Klotz PetscReal h = 1.0; /* Step size, length between x_k */ 2046b3c0f97bSTom Klotz PetscInt l = 0; /* Level of refinement, h = 2^{-l} */ 2047b3c0f97bSTom Klotz PetscReal osum = 0.0; /* Integral on last level */ 2048b3c0f97bSTom Klotz PetscReal psum = 0.0; /* Integral on the level before the last level */ 2049b3c0f97bSTom Klotz PetscReal sum; /* Integral on current level */ 2050446c295cSMatthew G. Knepley PetscReal yk; /* Quadrature point 1 - x_k on reference domain [-1, 1] */ 2051b3c0f97bSTom Klotz PetscReal lx, rx; /* Quadrature points to the left and right of 0 on the real domain [a, b] */ 2052b3c0f97bSTom Klotz PetscReal wk; /* Quadrature weight at x_k */ 2053b3c0f97bSTom Klotz PetscReal lval, rval; /* Terms in the quadature sum to the left and right of 0 */ 2054b3c0f97bSTom Klotz PetscInt d; /* Digits of precision in the integral */ 2055b3c0f97bSTom Klotz 2056b3c0f97bSTom Klotz PetscFunctionBegin; 20572c71b3e2SJacob Faibussowitsch PetscCheckFalse(digits <= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a positive number of significant digits"); 2058b3c0f97bSTom Klotz /* Center term */ 2059d6685f55SMatthew G. Knepley func(&beta, ctx, &lval); 2060b3c0f97bSTom Klotz sum = 0.5*alpha*PETSC_PI*lval; 2061b3c0f97bSTom Klotz /* */ 2062b3c0f97bSTom Klotz do { 2063b3c0f97bSTom Klotz PetscReal lterm, rterm, maxTerm = 0.0, d1, d2, d3, d4; 2064b3c0f97bSTom Klotz PetscInt k = 1; 2065b3c0f97bSTom Klotz 2066b3c0f97bSTom Klotz ++l; 2067b3c0f97bSTom Klotz /* PetscPrintf(PETSC_COMM_SELF, "LEVEL %D sum: %15.15f\n", l, sum); */ 2068b3c0f97bSTom Klotz /* At each level of refinement, h --> h/2 and sum --> sum/2 */ 2069b3c0f97bSTom Klotz psum = osum; 2070b3c0f97bSTom Klotz osum = sum; 2071b3c0f97bSTom Klotz h *= 0.5; 2072b3c0f97bSTom Klotz sum *= 0.5; 2073b3c0f97bSTom Klotz do { 20749add2064SThomas Klotz wk = 0.5*h*PETSC_PI*PetscCoshReal(k*h)/PetscSqr(PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(k*h))); 2075446c295cSMatthew G. Knepley yk = 1.0/(PetscExpReal(0.5*PETSC_PI*PetscSinhReal(k*h)) * PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(k*h))); 2076446c295cSMatthew G. Knepley lx = -alpha*(1.0 - yk)+beta; 2077446c295cSMatthew G. Knepley rx = alpha*(1.0 - yk)+beta; 2078d6685f55SMatthew G. Knepley func(&lx, ctx, &lval); 2079d6685f55SMatthew G. Knepley func(&rx, ctx, &rval); 2080b3c0f97bSTom Klotz lterm = alpha*wk*lval; 2081b3c0f97bSTom Klotz maxTerm = PetscMax(PetscAbsReal(lterm), maxTerm); 2082b3c0f97bSTom Klotz sum += lterm; 2083b3c0f97bSTom Klotz rterm = alpha*wk*rval; 2084b3c0f97bSTom Klotz maxTerm = PetscMax(PetscAbsReal(rterm), maxTerm); 2085b3c0f97bSTom Klotz sum += rterm; 2086b3c0f97bSTom Klotz ++k; 2087b3c0f97bSTom Klotz /* Only need to evaluate every other point on refined levels */ 2088b3c0f97bSTom Klotz if (l != 1) ++k; 20899add2064SThomas Klotz } while (PetscAbsReal(PetscLog10Real(wk)) < p); /* Only need to evaluate sum until weights are < 32 digits of precision */ 2090b3c0f97bSTom Klotz 2091b3c0f97bSTom Klotz d1 = PetscLog10Real(PetscAbsReal(sum - osum)); 2092b3c0f97bSTom Klotz d2 = PetscLog10Real(PetscAbsReal(sum - psum)); 2093b3c0f97bSTom Klotz d3 = PetscLog10Real(maxTerm) - p; 209409d48545SBarry Smith if (PetscMax(PetscAbsReal(lterm), PetscAbsReal(rterm)) == 0.0) d4 = 0.0; 209509d48545SBarry Smith else d4 = PetscLog10Real(PetscMax(PetscAbsReal(lterm), PetscAbsReal(rterm))); 2096b3c0f97bSTom Klotz d = PetscAbsInt(PetscMin(0, PetscMax(PetscMax(PetscMax(PetscSqr(d1)/d2, 2*d1), d3), d4))); 20979add2064SThomas Klotz } while (d < digits && l < 12); 2098b3c0f97bSTom Klotz *sol = sum; 2099e510cb1fSThomas Klotz 2100b3c0f97bSTom Klotz PetscFunctionReturn(0); 2101b3c0f97bSTom Klotz } 2102b3c0f97bSTom Klotz 2103497880caSRichard Tran Mills #if defined(PETSC_HAVE_MPFR) 2104d6685f55SMatthew G. Knepley PetscErrorCode PetscDTTanhSinhIntegrateMPFR(void (*func)(const PetscReal[], void *, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, void *ctx, PetscReal *sol) 210529f144ccSMatthew G. Knepley { 2106e510cb1fSThomas Klotz const PetscInt safetyFactor = 2; /* Calculate abcissa until 2*p digits */ 210729f144ccSMatthew G. Knepley PetscInt l = 0; /* Level of refinement, h = 2^{-l} */ 210829f144ccSMatthew G. Knepley mpfr_t alpha; /* Half-width of the integration interval */ 210929f144ccSMatthew G. Knepley mpfr_t beta; /* Center of the integration interval */ 211029f144ccSMatthew G. Knepley mpfr_t h; /* Step size, length between x_k */ 211129f144ccSMatthew G. Knepley mpfr_t osum; /* Integral on last level */ 211229f144ccSMatthew G. Knepley mpfr_t psum; /* Integral on the level before the last level */ 211329f144ccSMatthew G. Knepley mpfr_t sum; /* Integral on current level */ 211429f144ccSMatthew G. Knepley mpfr_t yk; /* Quadrature point 1 - x_k on reference domain [-1, 1] */ 211529f144ccSMatthew G. Knepley mpfr_t lx, rx; /* Quadrature points to the left and right of 0 on the real domain [a, b] */ 211629f144ccSMatthew G. Knepley mpfr_t wk; /* Quadrature weight at x_k */ 21171fbc92bbSMatthew G. Knepley PetscReal lval, rval, rtmp; /* Terms in the quadature sum to the left and right of 0 */ 211829f144ccSMatthew G. Knepley PetscInt d; /* Digits of precision in the integral */ 211929f144ccSMatthew G. Knepley mpfr_t pi2, kh, msinh, mcosh, maxTerm, curTerm, tmp; 212029f144ccSMatthew G. Knepley 212129f144ccSMatthew G. Knepley PetscFunctionBegin; 21222c71b3e2SJacob Faibussowitsch PetscCheckFalse(digits <= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a positive number of significant digits"); 212329f144ccSMatthew G. Knepley /* Create high precision storage */ 2124c9f744b5SMatthew 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); 212529f144ccSMatthew G. Knepley /* Initialization */ 212629f144ccSMatthew G. Knepley mpfr_set_d(alpha, 0.5*(b-a), MPFR_RNDN); 212729f144ccSMatthew G. Knepley mpfr_set_d(beta, 0.5*(b+a), MPFR_RNDN); 212829f144ccSMatthew G. Knepley mpfr_set_d(osum, 0.0, MPFR_RNDN); 212929f144ccSMatthew G. Knepley mpfr_set_d(psum, 0.0, MPFR_RNDN); 213029f144ccSMatthew G. Knepley mpfr_set_d(h, 1.0, MPFR_RNDN); 213129f144ccSMatthew G. Knepley mpfr_const_pi(pi2, MPFR_RNDN); 213229f144ccSMatthew G. Knepley mpfr_mul_d(pi2, pi2, 0.5, MPFR_RNDN); 213329f144ccSMatthew G. Knepley /* Center term */ 21341fbc92bbSMatthew G. Knepley rtmp = 0.5*(b+a); 21351fbc92bbSMatthew G. Knepley func(&rtmp, ctx, &lval); 213629f144ccSMatthew G. Knepley mpfr_set(sum, pi2, MPFR_RNDN); 213729f144ccSMatthew G. Knepley mpfr_mul(sum, sum, alpha, MPFR_RNDN); 213829f144ccSMatthew G. Knepley mpfr_mul_d(sum, sum, lval, MPFR_RNDN); 213929f144ccSMatthew G. Knepley /* */ 214029f144ccSMatthew G. Knepley do { 214129f144ccSMatthew G. Knepley PetscReal d1, d2, d3, d4; 214229f144ccSMatthew G. Knepley PetscInt k = 1; 214329f144ccSMatthew G. Knepley 214429f144ccSMatthew G. Knepley ++l; 214529f144ccSMatthew G. Knepley mpfr_set_d(maxTerm, 0.0, MPFR_RNDN); 214629f144ccSMatthew G. Knepley /* PetscPrintf(PETSC_COMM_SELF, "LEVEL %D sum: %15.15f\n", l, sum); */ 214729f144ccSMatthew G. Knepley /* At each level of refinement, h --> h/2 and sum --> sum/2 */ 214829f144ccSMatthew G. Knepley mpfr_set(psum, osum, MPFR_RNDN); 214929f144ccSMatthew G. Knepley mpfr_set(osum, sum, MPFR_RNDN); 215029f144ccSMatthew G. Knepley mpfr_mul_d(h, h, 0.5, MPFR_RNDN); 215129f144ccSMatthew G. Knepley mpfr_mul_d(sum, sum, 0.5, MPFR_RNDN); 215229f144ccSMatthew G. Knepley do { 215329f144ccSMatthew G. Knepley mpfr_set_si(kh, k, MPFR_RNDN); 215429f144ccSMatthew G. Knepley mpfr_mul(kh, kh, h, MPFR_RNDN); 215529f144ccSMatthew G. Knepley /* Weight */ 215629f144ccSMatthew G. Knepley mpfr_set(wk, h, MPFR_RNDN); 215729f144ccSMatthew G. Knepley mpfr_sinh_cosh(msinh, mcosh, kh, MPFR_RNDN); 215829f144ccSMatthew G. Knepley mpfr_mul(msinh, msinh, pi2, MPFR_RNDN); 215929f144ccSMatthew G. Knepley mpfr_mul(mcosh, mcosh, pi2, MPFR_RNDN); 216029f144ccSMatthew G. Knepley mpfr_cosh(tmp, msinh, MPFR_RNDN); 216129f144ccSMatthew G. Knepley mpfr_sqr(tmp, tmp, MPFR_RNDN); 216229f144ccSMatthew G. Knepley mpfr_mul(wk, wk, mcosh, MPFR_RNDN); 216329f144ccSMatthew G. Knepley mpfr_div(wk, wk, tmp, MPFR_RNDN); 216429f144ccSMatthew G. Knepley /* Abscissa */ 216529f144ccSMatthew G. Knepley mpfr_set_d(yk, 1.0, MPFR_RNDZ); 216629f144ccSMatthew G. Knepley mpfr_cosh(tmp, msinh, MPFR_RNDN); 216729f144ccSMatthew G. Knepley mpfr_div(yk, yk, tmp, MPFR_RNDZ); 216829f144ccSMatthew G. Knepley mpfr_exp(tmp, msinh, MPFR_RNDN); 216929f144ccSMatthew G. Knepley mpfr_div(yk, yk, tmp, MPFR_RNDZ); 217029f144ccSMatthew G. Knepley /* Quadrature points */ 217129f144ccSMatthew G. Knepley mpfr_sub_d(lx, yk, 1.0, MPFR_RNDZ); 217229f144ccSMatthew G. Knepley mpfr_mul(lx, lx, alpha, MPFR_RNDU); 217329f144ccSMatthew G. Knepley mpfr_add(lx, lx, beta, MPFR_RNDU); 217429f144ccSMatthew G. Knepley mpfr_d_sub(rx, 1.0, yk, MPFR_RNDZ); 217529f144ccSMatthew G. Knepley mpfr_mul(rx, rx, alpha, MPFR_RNDD); 217629f144ccSMatthew G. Knepley mpfr_add(rx, rx, beta, MPFR_RNDD); 217729f144ccSMatthew G. Knepley /* Evaluation */ 21781fbc92bbSMatthew G. Knepley rtmp = mpfr_get_d(lx, MPFR_RNDU); 21791fbc92bbSMatthew G. Knepley func(&rtmp, ctx, &lval); 21801fbc92bbSMatthew G. Knepley rtmp = mpfr_get_d(rx, MPFR_RNDD); 21811fbc92bbSMatthew G. Knepley func(&rtmp, ctx, &rval); 218229f144ccSMatthew G. Knepley /* Update */ 218329f144ccSMatthew G. Knepley mpfr_mul(tmp, wk, alpha, MPFR_RNDN); 218429f144ccSMatthew G. Knepley mpfr_mul_d(tmp, tmp, lval, MPFR_RNDN); 218529f144ccSMatthew G. Knepley mpfr_add(sum, sum, tmp, MPFR_RNDN); 218629f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 218729f144ccSMatthew G. Knepley mpfr_max(maxTerm, maxTerm, tmp, MPFR_RNDN); 218829f144ccSMatthew G. Knepley mpfr_set(curTerm, tmp, MPFR_RNDN); 218929f144ccSMatthew G. Knepley mpfr_mul(tmp, wk, alpha, MPFR_RNDN); 219029f144ccSMatthew G. Knepley mpfr_mul_d(tmp, tmp, rval, MPFR_RNDN); 219129f144ccSMatthew G. Knepley mpfr_add(sum, sum, tmp, MPFR_RNDN); 219229f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 219329f144ccSMatthew G. Knepley mpfr_max(maxTerm, maxTerm, tmp, MPFR_RNDN); 219429f144ccSMatthew G. Knepley mpfr_max(curTerm, curTerm, tmp, MPFR_RNDN); 219529f144ccSMatthew G. Knepley ++k; 219629f144ccSMatthew G. Knepley /* Only need to evaluate every other point on refined levels */ 219729f144ccSMatthew G. Knepley if (l != 1) ++k; 219829f144ccSMatthew G. Knepley mpfr_log10(tmp, wk, MPFR_RNDN); 219929f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 2200c9f744b5SMatthew G. Knepley } while (mpfr_get_d(tmp, MPFR_RNDN) < safetyFactor*digits); /* Only need to evaluate sum until weights are < 32 digits of precision */ 220129f144ccSMatthew G. Knepley mpfr_sub(tmp, sum, osum, MPFR_RNDN); 220229f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 220329f144ccSMatthew G. Knepley mpfr_log10(tmp, tmp, MPFR_RNDN); 220429f144ccSMatthew G. Knepley d1 = mpfr_get_d(tmp, MPFR_RNDN); 220529f144ccSMatthew G. Knepley mpfr_sub(tmp, sum, psum, MPFR_RNDN); 220629f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 220729f144ccSMatthew G. Knepley mpfr_log10(tmp, tmp, MPFR_RNDN); 220829f144ccSMatthew G. Knepley d2 = mpfr_get_d(tmp, MPFR_RNDN); 220929f144ccSMatthew G. Knepley mpfr_log10(tmp, maxTerm, MPFR_RNDN); 2210c9f744b5SMatthew G. Knepley d3 = mpfr_get_d(tmp, MPFR_RNDN) - digits; 221129f144ccSMatthew G. Knepley mpfr_log10(tmp, curTerm, MPFR_RNDN); 221229f144ccSMatthew G. Knepley d4 = mpfr_get_d(tmp, MPFR_RNDN); 221329f144ccSMatthew G. Knepley d = PetscAbsInt(PetscMin(0, PetscMax(PetscMax(PetscMax(PetscSqr(d1)/d2, 2*d1), d3), d4))); 2214b0649871SThomas Klotz } while (d < digits && l < 8); 221529f144ccSMatthew G. Knepley *sol = mpfr_get_d(sum, MPFR_RNDN); 221629f144ccSMatthew G. Knepley /* Cleanup */ 221729f144ccSMatthew G. Knepley mpfr_clears(alpha, beta, h, sum, osum, psum, yk, wk, lx, rx, tmp, maxTerm, curTerm, pi2, kh, msinh, mcosh, NULL); 221829f144ccSMatthew G. Knepley PetscFunctionReturn(0); 221929f144ccSMatthew G. Knepley } 2220d525116cSMatthew G. Knepley #else 2221fbfcfee5SBarry Smith 2222d6685f55SMatthew G. Knepley PetscErrorCode PetscDTTanhSinhIntegrateMPFR(void (*func)(const PetscReal[], void *, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, void *ctx, PetscReal *sol) 2223d525116cSMatthew G. Knepley { 2224d525116cSMatthew G. Knepley SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "This method will not work without MPFR. Reconfigure using --download-mpfr --download-gmp"); 2225d525116cSMatthew G. Knepley } 222629f144ccSMatthew G. Knepley #endif 222729f144ccSMatthew G. Knepley 22282df84da0SMatthew G. Knepley /*@ 22292df84da0SMatthew G. Knepley PetscDTTensorQuadratureCreate - create the tensor product quadrature from two lower-dimensional quadratures 22302df84da0SMatthew G. Knepley 22312df84da0SMatthew G. Knepley Not Collective 22322df84da0SMatthew G. Knepley 22332df84da0SMatthew G. Knepley Input Parameters: 22342df84da0SMatthew G. Knepley + q1 - The first quadrature 22352df84da0SMatthew G. Knepley - q2 - The second quadrature 22362df84da0SMatthew G. Knepley 22372df84da0SMatthew G. Knepley Output Parameter: 22382df84da0SMatthew G. Knepley . q - A PetscQuadrature object 22392df84da0SMatthew G. Knepley 22402df84da0SMatthew G. Knepley Level: intermediate 22412df84da0SMatthew G. Knepley 22422df84da0SMatthew G. Knepley .seealso: PetscDTGaussTensorQuadrature() 22432df84da0SMatthew G. Knepley @*/ 22442df84da0SMatthew G. Knepley PetscErrorCode PetscDTTensorQuadratureCreate(PetscQuadrature q1, PetscQuadrature q2, PetscQuadrature *q) 22452df84da0SMatthew G. Knepley { 22462df84da0SMatthew G. Knepley const PetscReal *x1, *w1, *x2, *w2; 22472df84da0SMatthew G. Knepley PetscReal *x, *w; 22482df84da0SMatthew G. Knepley PetscInt dim1, Nc1, Np1, order1, qa, d1; 22492df84da0SMatthew G. Knepley PetscInt dim2, Nc2, Np2, order2, qb, d2; 22502df84da0SMatthew G. Knepley PetscInt dim, Nc, Np, order, qc, d; 22512df84da0SMatthew G. Knepley 22522df84da0SMatthew G. Knepley PetscFunctionBegin; 22532df84da0SMatthew G. Knepley PetscValidHeaderSpecific(q1, PETSCQUADRATURE_CLASSID, 1); 22542df84da0SMatthew G. Knepley PetscValidHeaderSpecific(q2, PETSCQUADRATURE_CLASSID, 2); 22552df84da0SMatthew G. Knepley PetscValidPointer(q, 3); 22565f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureGetOrder(q1, &order1)); 22575f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureGetOrder(q2, &order2)); 22582df84da0SMatthew G. Knepley PetscCheck(order1 == order2, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Order1 %" PetscInt_FMT " != %" PetscInt_FMT " Order2", order1, order2); 22595f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureGetData(q1, &dim1, &Nc1, &Np1, &x1, &w1)); 22605f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureGetData(q2, &dim2, &Nc2, &Np2, &x2, &w2)); 22612df84da0SMatthew G. Knepley PetscCheck(Nc1 == Nc2, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "NumComp1 %" PetscInt_FMT " != %" PetscInt_FMT " NumComp2", Nc1, Nc2); 22622df84da0SMatthew G. Knepley 22632df84da0SMatthew G. Knepley dim = dim1 + dim2; 22642df84da0SMatthew G. Knepley Nc = Nc1; 22652df84da0SMatthew G. Knepley Np = Np1 * Np2; 22662df84da0SMatthew G. Knepley order = order1; 22675f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureCreate(PETSC_COMM_SELF, q)); 22685f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureSetOrder(*q, order)); 22695f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(Np*dim, &x)); 22705f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(Np, &w)); 22712df84da0SMatthew G. Knepley for (qa = 0, qc = 0; qa < Np1; ++qa) { 22722df84da0SMatthew G. Knepley for (qb = 0; qb < Np2; ++qb, ++qc) { 22732df84da0SMatthew G. Knepley for (d1 = 0, d = 0; d1 < dim1; ++d1, ++d) { 22742df84da0SMatthew G. Knepley x[qc*dim+d] = x1[qa*dim1+d1]; 22752df84da0SMatthew G. Knepley } 22762df84da0SMatthew G. Knepley for (d2 = 0; d2 < dim2; ++d2, ++d) { 22772df84da0SMatthew G. Knepley x[qc*dim+d] = x2[qb*dim2+d2]; 22782df84da0SMatthew G. Knepley } 22792df84da0SMatthew G. Knepley w[qc] = w1[qa] * w2[qb]; 22802df84da0SMatthew G. Knepley } 22812df84da0SMatthew G. Knepley } 22825f80ce2aSJacob Faibussowitsch CHKERRQ(PetscQuadratureSetData(*q, dim, Nc, Np, x, w)); 22832df84da0SMatthew G. Knepley PetscFunctionReturn(0); 22842df84da0SMatthew G. Knepley } 22852df84da0SMatthew G. Knepley 2286194825f6SJed Brown /* Overwrites A. Can only handle full-rank problems with m>=n 2287194825f6SJed Brown * A in column-major format 2288194825f6SJed Brown * Ainv in row-major format 2289194825f6SJed Brown * tau has length m 2290194825f6SJed Brown * worksize must be >= max(1,n) 2291194825f6SJed Brown */ 2292194825f6SJed Brown static PetscErrorCode PetscDTPseudoInverseQR(PetscInt m,PetscInt mstride,PetscInt n,PetscReal *A_in,PetscReal *Ainv_out,PetscScalar *tau,PetscInt worksize,PetscScalar *work) 2293194825f6SJed Brown { 2294194825f6SJed Brown PetscBLASInt M,N,K,lda,ldb,ldwork,info; 2295194825f6SJed Brown PetscScalar *A,*Ainv,*R,*Q,Alpha; 2296194825f6SJed Brown 2297194825f6SJed Brown PetscFunctionBegin; 2298194825f6SJed Brown #if defined(PETSC_USE_COMPLEX) 2299194825f6SJed Brown { 2300194825f6SJed Brown PetscInt i,j; 23015f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc2(m*n,&A,m*n,&Ainv)); 2302194825f6SJed Brown for (j=0; j<n; j++) { 2303194825f6SJed Brown for (i=0; i<m; i++) A[i+m*j] = A_in[i+mstride*j]; 2304194825f6SJed Brown } 2305194825f6SJed Brown mstride = m; 2306194825f6SJed Brown } 2307194825f6SJed Brown #else 2308194825f6SJed Brown A = A_in; 2309194825f6SJed Brown Ainv = Ainv_out; 2310194825f6SJed Brown #endif 2311194825f6SJed Brown 23125f80ce2aSJacob Faibussowitsch CHKERRQ(PetscBLASIntCast(m,&M)); 23135f80ce2aSJacob Faibussowitsch CHKERRQ(PetscBLASIntCast(n,&N)); 23145f80ce2aSJacob Faibussowitsch CHKERRQ(PetscBLASIntCast(mstride,&lda)); 23155f80ce2aSJacob Faibussowitsch CHKERRQ(PetscBLASIntCast(worksize,&ldwork)); 23165f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFPTrapPush(PETSC_FP_TRAP_OFF)); 2317001a771dSBarry Smith PetscStackCallBLAS("LAPACKgeqrf",LAPACKgeqrf_(&M,&N,A,&lda,tau,work,&ldwork,&info)); 23185f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFPTrapPop()); 2319*28b400f6SJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_LIB,"xGEQRF error"); 2320194825f6SJed Brown R = A; /* Upper triangular part of A now contains R, the rest contains the elementary reflectors */ 2321194825f6SJed Brown 2322194825f6SJed Brown /* Extract an explicit representation of Q */ 2323194825f6SJed Brown Q = Ainv; 23245f80ce2aSJacob Faibussowitsch CHKERRQ(PetscArraycpy(Q,A,mstride*n)); 2325194825f6SJed Brown K = N; /* full rank */ 2326c964aadfSJose E. Roman PetscStackCallBLAS("LAPACKorgqr",LAPACKorgqr_(&M,&N,&K,Q,&lda,tau,work,&ldwork,&info)); 2327*28b400f6SJacob Faibussowitsch PetscCheck(!info,PETSC_COMM_SELF,PETSC_ERR_LIB,"xORGQR/xUNGQR error"); 2328194825f6SJed Brown 2329194825f6SJed Brown /* Compute A^{-T} = (R^{-1} Q^T)^T = Q R^{-T} */ 2330194825f6SJed Brown Alpha = 1.0; 2331194825f6SJed Brown ldb = lda; 2332001a771dSBarry Smith PetscStackCallBLAS("BLAStrsm",BLAStrsm_("Right","Upper","ConjugateTranspose","NotUnitTriangular",&M,&N,&Alpha,R,&lda,Q,&ldb)); 2333194825f6SJed Brown /* Ainv is Q, overwritten with inverse */ 2334194825f6SJed Brown 2335194825f6SJed Brown #if defined(PETSC_USE_COMPLEX) 2336194825f6SJed Brown { 2337194825f6SJed Brown PetscInt i; 2338194825f6SJed Brown for (i=0; i<m*n; i++) Ainv_out[i] = PetscRealPart(Ainv[i]); 23395f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree2(A,Ainv)); 2340194825f6SJed Brown } 2341194825f6SJed Brown #endif 2342194825f6SJed Brown PetscFunctionReturn(0); 2343194825f6SJed Brown } 2344194825f6SJed Brown 2345194825f6SJed Brown /* Computes integral of L_p' over intervals {(x0,x1),(x1,x2),...} */ 2346194825f6SJed Brown static PetscErrorCode PetscDTLegendreIntegrate(PetscInt ninterval,const PetscReal *x,PetscInt ndegree,const PetscInt *degrees,PetscBool Transpose,PetscReal *B) 2347194825f6SJed Brown { 2348194825f6SJed Brown PetscReal *Bv; 2349194825f6SJed Brown PetscInt i,j; 2350194825f6SJed Brown 2351194825f6SJed Brown PetscFunctionBegin; 23525f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1((ninterval+1)*ndegree,&Bv)); 2353194825f6SJed Brown /* Point evaluation of L_p on all the source vertices */ 23545f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTLegendreEval(ninterval+1,x,ndegree,degrees,Bv,NULL,NULL)); 2355194825f6SJed Brown /* Integral over each interval: \int_a^b L_p' = L_p(b)-L_p(a) */ 2356194825f6SJed Brown for (i=0; i<ninterval; i++) { 2357194825f6SJed Brown for (j=0; j<ndegree; j++) { 2358194825f6SJed Brown if (Transpose) B[i+ninterval*j] = Bv[(i+1)*ndegree+j] - Bv[i*ndegree+j]; 2359194825f6SJed Brown else B[i*ndegree+j] = Bv[(i+1)*ndegree+j] - Bv[i*ndegree+j]; 2360194825f6SJed Brown } 2361194825f6SJed Brown } 23625f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(Bv)); 2363194825f6SJed Brown PetscFunctionReturn(0); 2364194825f6SJed Brown } 2365194825f6SJed Brown 2366194825f6SJed Brown /*@ 2367194825f6SJed Brown PetscDTReconstructPoly - create matrix representing polynomial reconstruction using cell intervals and evaluation at target intervals 2368194825f6SJed Brown 2369194825f6SJed Brown Not Collective 2370194825f6SJed Brown 23714165533cSJose E. Roman Input Parameters: 2372194825f6SJed Brown + degree - degree of reconstruction polynomial 2373194825f6SJed Brown . nsource - number of source intervals 2374194825f6SJed Brown . sourcex - sorted coordinates of source cell boundaries (length nsource+1) 2375194825f6SJed Brown . ntarget - number of target intervals 2376194825f6SJed Brown - targetx - sorted coordinates of target cell boundaries (length ntarget+1) 2377194825f6SJed Brown 23784165533cSJose E. Roman Output Parameter: 2379194825f6SJed Brown . R - reconstruction matrix, utarget = sum_s R[t*nsource+s] * usource[s] 2380194825f6SJed Brown 2381194825f6SJed Brown Level: advanced 2382194825f6SJed Brown 2383194825f6SJed Brown .seealso: PetscDTLegendreEval() 2384194825f6SJed Brown @*/ 2385194825f6SJed Brown PetscErrorCode PetscDTReconstructPoly(PetscInt degree,PetscInt nsource,const PetscReal *sourcex,PetscInt ntarget,const PetscReal *targetx,PetscReal *R) 2386194825f6SJed Brown { 2387194825f6SJed Brown PetscInt i,j,k,*bdegrees,worksize; 2388194825f6SJed Brown PetscReal xmin,xmax,center,hscale,*sourcey,*targety,*Bsource,*Bsinv,*Btarget; 2389194825f6SJed Brown PetscScalar *tau,*work; 2390194825f6SJed Brown 2391194825f6SJed Brown PetscFunctionBegin; 2392194825f6SJed Brown PetscValidRealPointer(sourcex,3); 2393194825f6SJed Brown PetscValidRealPointer(targetx,5); 2394194825f6SJed Brown PetscValidRealPointer(R,6); 23952c71b3e2SJacob Faibussowitsch PetscCheckFalse(degree >= nsource,PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Reconstruction degree %D must be less than number of source intervals %D",degree,nsource); 239676bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 2397194825f6SJed Brown for (i=0; i<nsource; i++) { 23982c71b3e2SJacob Faibussowitsch PetscCheckFalse(sourcex[i] >= sourcex[i+1],PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Source interval %D has negative orientation (%g,%g)",i,(double)sourcex[i],(double)sourcex[i+1]); 2399194825f6SJed Brown } 2400194825f6SJed Brown for (i=0; i<ntarget; i++) { 24012c71b3e2SJacob Faibussowitsch PetscCheckFalse(targetx[i] >= targetx[i+1],PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Target interval %D has negative orientation (%g,%g)",i,(double)targetx[i],(double)targetx[i+1]); 2402194825f6SJed Brown } 240376bd3646SJed Brown } 2404194825f6SJed Brown xmin = PetscMin(sourcex[0],targetx[0]); 2405194825f6SJed Brown xmax = PetscMax(sourcex[nsource],targetx[ntarget]); 2406194825f6SJed Brown center = (xmin + xmax)/2; 2407194825f6SJed Brown hscale = (xmax - xmin)/2; 2408194825f6SJed Brown worksize = nsource; 24095f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc4(degree+1,&bdegrees,nsource+1,&sourcey,nsource*(degree+1),&Bsource,worksize,&work)); 24105f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc4(nsource,&tau,nsource*(degree+1),&Bsinv,ntarget+1,&targety,ntarget*(degree+1),&Btarget)); 2411194825f6SJed Brown for (i=0; i<=nsource; i++) sourcey[i] = (sourcex[i]-center)/hscale; 2412194825f6SJed Brown for (i=0; i<=degree; i++) bdegrees[i] = i+1; 24135f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTLegendreIntegrate(nsource,sourcey,degree+1,bdegrees,PETSC_TRUE,Bsource)); 24145f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTPseudoInverseQR(nsource,nsource,degree+1,Bsource,Bsinv,tau,nsource,work)); 2415194825f6SJed Brown for (i=0; i<=ntarget; i++) targety[i] = (targetx[i]-center)/hscale; 24165f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTLegendreIntegrate(ntarget,targety,degree+1,bdegrees,PETSC_FALSE,Btarget)); 2417194825f6SJed Brown for (i=0; i<ntarget; i++) { 2418194825f6SJed Brown PetscReal rowsum = 0; 2419194825f6SJed Brown for (j=0; j<nsource; j++) { 2420194825f6SJed Brown PetscReal sum = 0; 2421194825f6SJed Brown for (k=0; k<degree+1; k++) { 2422194825f6SJed Brown sum += Btarget[i*(degree+1)+k] * Bsinv[k*nsource+j]; 2423194825f6SJed Brown } 2424194825f6SJed Brown R[i*nsource+j] = sum; 2425194825f6SJed Brown rowsum += sum; 2426194825f6SJed Brown } 2427194825f6SJed Brown for (j=0; j<nsource; j++) R[i*nsource+j] /= rowsum; /* normalize each row */ 2428194825f6SJed Brown } 24295f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree4(bdegrees,sourcey,Bsource,work)); 24305f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree4(tau,Bsinv,targety,Btarget)); 2431194825f6SJed Brown PetscFunctionReturn(0); 2432194825f6SJed Brown } 2433916e780bShannah_mairs 2434916e780bShannah_mairs /*@C 2435916e780bShannah_mairs PetscGaussLobattoLegendreIntegrate - Compute the L2 integral of a function on the GLL points 2436916e780bShannah_mairs 2437916e780bShannah_mairs Not Collective 2438916e780bShannah_mairs 2439d8d19677SJose E. Roman Input Parameters: 2440916e780bShannah_mairs + n - the number of GLL nodes 2441916e780bShannah_mairs . nodes - the GLL nodes 2442916e780bShannah_mairs . weights - the GLL weights 2443f0fc11ceSJed Brown - f - the function values at the nodes 2444916e780bShannah_mairs 2445916e780bShannah_mairs Output Parameter: 2446916e780bShannah_mairs . in - the value of the integral 2447916e780bShannah_mairs 2448916e780bShannah_mairs Level: beginner 2449916e780bShannah_mairs 2450916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature() 2451916e780bShannah_mairs 2452916e780bShannah_mairs @*/ 2453916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreIntegrate(PetscInt n,PetscReal *nodes,PetscReal *weights,const PetscReal *f,PetscReal *in) 2454916e780bShannah_mairs { 2455916e780bShannah_mairs PetscInt i; 2456916e780bShannah_mairs 2457916e780bShannah_mairs PetscFunctionBegin; 2458916e780bShannah_mairs *in = 0.; 2459916e780bShannah_mairs for (i=0; i<n; i++) { 2460916e780bShannah_mairs *in += f[i]*f[i]*weights[i]; 2461916e780bShannah_mairs } 2462916e780bShannah_mairs PetscFunctionReturn(0); 2463916e780bShannah_mairs } 2464916e780bShannah_mairs 2465916e780bShannah_mairs /*@C 2466916e780bShannah_mairs PetscGaussLobattoLegendreElementLaplacianCreate - computes the Laplacian for a single 1d GLL element 2467916e780bShannah_mairs 2468916e780bShannah_mairs Not Collective 2469916e780bShannah_mairs 2470d8d19677SJose E. Roman Input Parameters: 2471916e780bShannah_mairs + n - the number of GLL nodes 2472916e780bShannah_mairs . nodes - the GLL nodes 2473f0fc11ceSJed Brown - weights - the GLL weights 2474916e780bShannah_mairs 2475916e780bShannah_mairs Output Parameter: 2476916e780bShannah_mairs . A - the stiffness element 2477916e780bShannah_mairs 2478916e780bShannah_mairs Level: beginner 2479916e780bShannah_mairs 2480916e780bShannah_mairs Notes: 2481916e780bShannah_mairs Destroy this with PetscGaussLobattoLegendreElementLaplacianDestroy() 2482916e780bShannah_mairs 2483916e780bShannah_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) 2484916e780bShannah_mairs 2485916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianDestroy() 2486916e780bShannah_mairs 2487916e780bShannah_mairs @*/ 2488916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementLaplacianCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2489916e780bShannah_mairs { 2490916e780bShannah_mairs PetscReal **A; 2491916e780bShannah_mairs const PetscReal *gllnodes = nodes; 2492916e780bShannah_mairs const PetscInt p = n-1; 2493916e780bShannah_mairs PetscReal z0,z1,z2 = -1,x,Lpj,Lpr; 2494916e780bShannah_mairs PetscInt i,j,nn,r; 2495916e780bShannah_mairs 2496916e780bShannah_mairs PetscFunctionBegin; 24975f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(n,&A)); 24985f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(n*n,&A[0])); 2499916e780bShannah_mairs for (i=1; i<n; i++) A[i] = A[i-1]+n; 2500916e780bShannah_mairs 2501916e780bShannah_mairs for (j=1; j<p; j++) { 2502916e780bShannah_mairs x = gllnodes[j]; 2503916e780bShannah_mairs z0 = 1.; 2504916e780bShannah_mairs z1 = x; 2505916e780bShannah_mairs for (nn=1; nn<p; nn++) { 2506916e780bShannah_mairs z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.)); 2507916e780bShannah_mairs z0 = z1; 2508916e780bShannah_mairs z1 = z2; 2509916e780bShannah_mairs } 2510916e780bShannah_mairs Lpj=z2; 2511916e780bShannah_mairs for (r=1; r<p; r++) { 2512916e780bShannah_mairs if (r == j) { 2513916e780bShannah_mairs A[j][j]=2./(3.*(1.-gllnodes[j]*gllnodes[j])*Lpj*Lpj); 2514916e780bShannah_mairs } else { 2515916e780bShannah_mairs x = gllnodes[r]; 2516916e780bShannah_mairs z0 = 1.; 2517916e780bShannah_mairs z1 = x; 2518916e780bShannah_mairs for (nn=1; nn<p; nn++) { 2519916e780bShannah_mairs z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.)); 2520916e780bShannah_mairs z0 = z1; 2521916e780bShannah_mairs z1 = z2; 2522916e780bShannah_mairs } 2523916e780bShannah_mairs Lpr = z2; 2524916e780bShannah_mairs A[r][j] = 4./(((PetscReal)p)*(((PetscReal)p)+1.)*Lpj*Lpr*(gllnodes[j]-gllnodes[r])*(gllnodes[j]-gllnodes[r])); 2525916e780bShannah_mairs } 2526916e780bShannah_mairs } 2527916e780bShannah_mairs } 2528916e780bShannah_mairs for (j=1; j<p+1; j++) { 2529916e780bShannah_mairs x = gllnodes[j]; 2530916e780bShannah_mairs z0 = 1.; 2531916e780bShannah_mairs z1 = x; 2532916e780bShannah_mairs for (nn=1; nn<p; nn++) { 2533916e780bShannah_mairs z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.)); 2534916e780bShannah_mairs z0 = z1; 2535916e780bShannah_mairs z1 = z2; 2536916e780bShannah_mairs } 2537916e780bShannah_mairs Lpj = z2; 2538916e780bShannah_mairs A[j][0] = 4.*PetscPowRealInt(-1.,p)/(((PetscReal)p)*(((PetscReal)p)+1.)*Lpj*(1.+gllnodes[j])*(1.+gllnodes[j])); 2539916e780bShannah_mairs A[0][j] = A[j][0]; 2540916e780bShannah_mairs } 2541916e780bShannah_mairs for (j=0; j<p; j++) { 2542916e780bShannah_mairs x = gllnodes[j]; 2543916e780bShannah_mairs z0 = 1.; 2544916e780bShannah_mairs z1 = x; 2545916e780bShannah_mairs for (nn=1; nn<p; nn++) { 2546916e780bShannah_mairs z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.)); 2547916e780bShannah_mairs z0 = z1; 2548916e780bShannah_mairs z1 = z2; 2549916e780bShannah_mairs } 2550916e780bShannah_mairs Lpj=z2; 2551916e780bShannah_mairs 2552916e780bShannah_mairs A[p][j] = 4./(((PetscReal)p)*(((PetscReal)p)+1.)*Lpj*(1.-gllnodes[j])*(1.-gllnodes[j])); 2553916e780bShannah_mairs A[j][p] = A[p][j]; 2554916e780bShannah_mairs } 2555916e780bShannah_mairs A[0][0]=0.5+(((PetscReal)p)*(((PetscReal)p)+1.)-2.)/6.; 2556916e780bShannah_mairs A[p][p]=A[0][0]; 2557916e780bShannah_mairs *AA = A; 2558916e780bShannah_mairs PetscFunctionReturn(0); 2559916e780bShannah_mairs } 2560916e780bShannah_mairs 2561916e780bShannah_mairs /*@C 2562916e780bShannah_mairs PetscGaussLobattoLegendreElementLaplacianDestroy - frees the Laplacian for a single 1d GLL element 2563916e780bShannah_mairs 2564916e780bShannah_mairs Not Collective 2565916e780bShannah_mairs 2566d8d19677SJose E. Roman Input Parameters: 2567916e780bShannah_mairs + n - the number of GLL nodes 2568916e780bShannah_mairs . nodes - the GLL nodes 2569916e780bShannah_mairs . weights - the GLL weightss 2570916e780bShannah_mairs - A - the stiffness element 2571916e780bShannah_mairs 2572916e780bShannah_mairs Level: beginner 2573916e780bShannah_mairs 2574916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianCreate() 2575916e780bShannah_mairs 2576916e780bShannah_mairs @*/ 2577916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementLaplacianDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2578916e780bShannah_mairs { 2579916e780bShannah_mairs PetscFunctionBegin; 25805f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree((*AA)[0])); 25815f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(*AA)); 2582916e780bShannah_mairs *AA = NULL; 2583916e780bShannah_mairs PetscFunctionReturn(0); 2584916e780bShannah_mairs } 2585916e780bShannah_mairs 2586916e780bShannah_mairs /*@C 2587916e780bShannah_mairs PetscGaussLobattoLegendreElementGradientCreate - computes the gradient for a single 1d GLL element 2588916e780bShannah_mairs 2589916e780bShannah_mairs Not Collective 2590916e780bShannah_mairs 2591916e780bShannah_mairs Input Parameter: 2592916e780bShannah_mairs + n - the number of GLL nodes 2593916e780bShannah_mairs . nodes - the GLL nodes 2594916e780bShannah_mairs . weights - the GLL weights 2595916e780bShannah_mairs 2596d8d19677SJose E. Roman Output Parameters: 2597916e780bShannah_mairs . AA - the stiffness element 2598916e780bShannah_mairs - AAT - the transpose of AA (pass in NULL if you do not need this array) 2599916e780bShannah_mairs 2600916e780bShannah_mairs Level: beginner 2601916e780bShannah_mairs 2602916e780bShannah_mairs Notes: 2603916e780bShannah_mairs Destroy this with PetscGaussLobattoLegendreElementGradientDestroy() 2604916e780bShannah_mairs 2605916e780bShannah_mairs You can access entries in these arrays with AA[i][j] but in memory it is stored in contiguous memory, row oriented 2606916e780bShannah_mairs 2607916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianDestroy() 2608916e780bShannah_mairs 2609916e780bShannah_mairs @*/ 2610916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementGradientCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA,PetscReal ***AAT) 2611916e780bShannah_mairs { 2612916e780bShannah_mairs PetscReal **A, **AT = NULL; 2613916e780bShannah_mairs const PetscReal *gllnodes = nodes; 2614916e780bShannah_mairs const PetscInt p = n-1; 2615e6a796c3SToby Isaac PetscReal Li, Lj,d0; 2616916e780bShannah_mairs PetscInt i,j; 2617916e780bShannah_mairs 2618916e780bShannah_mairs PetscFunctionBegin; 26195f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(n,&A)); 26205f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(n*n,&A[0])); 2621916e780bShannah_mairs for (i=1; i<n; i++) A[i] = A[i-1]+n; 2622916e780bShannah_mairs 2623916e780bShannah_mairs if (AAT) { 26245f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(n,&AT)); 26255f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(n*n,&AT[0])); 2626916e780bShannah_mairs for (i=1; i<n; i++) AT[i] = AT[i-1]+n; 2627916e780bShannah_mairs } 2628916e780bShannah_mairs 2629916e780bShannah_mairs if (n==1) {A[0][0] = 0.;} 2630916e780bShannah_mairs d0 = (PetscReal)p*((PetscReal)p+1.)/4.; 2631916e780bShannah_mairs for (i=0; i<n; i++) { 2632916e780bShannah_mairs for (j=0; j<n; j++) { 2633916e780bShannah_mairs A[i][j] = 0.; 26345f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTComputeJacobi(0., 0., p, gllnodes[i], &Li)); 26355f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDTComputeJacobi(0., 0., p, gllnodes[j], &Lj)); 2636916e780bShannah_mairs if (i!=j) A[i][j] = Li/(Lj*(gllnodes[i]-gllnodes[j])); 2637916e780bShannah_mairs if ((j==i) && (i==0)) A[i][j] = -d0; 2638916e780bShannah_mairs if (j==i && i==p) A[i][j] = d0; 2639916e780bShannah_mairs if (AT) AT[j][i] = A[i][j]; 2640916e780bShannah_mairs } 2641916e780bShannah_mairs } 2642916e780bShannah_mairs if (AAT) *AAT = AT; 2643916e780bShannah_mairs *AA = A; 2644916e780bShannah_mairs PetscFunctionReturn(0); 2645916e780bShannah_mairs } 2646916e780bShannah_mairs 2647916e780bShannah_mairs /*@C 2648916e780bShannah_mairs PetscGaussLobattoLegendreElementGradientDestroy - frees the gradient for a single 1d GLL element obtained with PetscGaussLobattoLegendreElementGradientCreate() 2649916e780bShannah_mairs 2650916e780bShannah_mairs Not Collective 2651916e780bShannah_mairs 2652d8d19677SJose E. Roman Input Parameters: 2653916e780bShannah_mairs + n - the number of GLL nodes 2654916e780bShannah_mairs . nodes - the GLL nodes 2655916e780bShannah_mairs . weights - the GLL weights 2656916e780bShannah_mairs . AA - the stiffness element 2657916e780bShannah_mairs - AAT - the transpose of the element 2658916e780bShannah_mairs 2659916e780bShannah_mairs Level: beginner 2660916e780bShannah_mairs 2661916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianCreate(), PetscGaussLobattoLegendreElementAdvectionCreate() 2662916e780bShannah_mairs 2663916e780bShannah_mairs @*/ 2664916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementGradientDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA,PetscReal ***AAT) 2665916e780bShannah_mairs { 2666916e780bShannah_mairs PetscFunctionBegin; 26675f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree((*AA)[0])); 26685f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(*AA)); 2669916e780bShannah_mairs *AA = NULL; 2670916e780bShannah_mairs if (*AAT) { 26715f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree((*AAT)[0])); 26725f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(*AAT)); 2673916e780bShannah_mairs *AAT = NULL; 2674916e780bShannah_mairs } 2675916e780bShannah_mairs PetscFunctionReturn(0); 2676916e780bShannah_mairs } 2677916e780bShannah_mairs 2678916e780bShannah_mairs /*@C 2679916e780bShannah_mairs PetscGaussLobattoLegendreElementAdvectionCreate - computes the advection operator for a single 1d GLL element 2680916e780bShannah_mairs 2681916e780bShannah_mairs Not Collective 2682916e780bShannah_mairs 2683d8d19677SJose E. Roman Input Parameters: 2684916e780bShannah_mairs + n - the number of GLL nodes 2685916e780bShannah_mairs . nodes - the GLL nodes 2686f0fc11ceSJed Brown - weights - the GLL weightss 2687916e780bShannah_mairs 2688916e780bShannah_mairs Output Parameter: 2689916e780bShannah_mairs . AA - the stiffness element 2690916e780bShannah_mairs 2691916e780bShannah_mairs Level: beginner 2692916e780bShannah_mairs 2693916e780bShannah_mairs Notes: 2694916e780bShannah_mairs Destroy this with PetscGaussLobattoLegendreElementAdvectionDestroy() 2695916e780bShannah_mairs 2696916e780bShannah_mairs This is the same as the Gradient operator multiplied by the diagonal mass matrix 2697916e780bShannah_mairs 2698916e780bShannah_mairs You can access entries in this array with AA[i][j] but in memory it is stored in contiguous memory, row oriented 2699916e780bShannah_mairs 2700916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianCreate(), PetscGaussLobattoLegendreElementAdvectionDestroy() 2701916e780bShannah_mairs 2702916e780bShannah_mairs @*/ 2703916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementAdvectionCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2704916e780bShannah_mairs { 2705916e780bShannah_mairs PetscReal **D; 2706916e780bShannah_mairs const PetscReal *gllweights = weights; 2707916e780bShannah_mairs const PetscInt glln = n; 2708916e780bShannah_mairs PetscInt i,j; 2709916e780bShannah_mairs 2710916e780bShannah_mairs PetscFunctionBegin; 27115f80ce2aSJacob Faibussowitsch CHKERRQ(PetscGaussLobattoLegendreElementGradientCreate(n,nodes,weights,&D,NULL)); 2712916e780bShannah_mairs for (i=0; i<glln; i++) { 2713916e780bShannah_mairs for (j=0; j<glln; j++) { 2714916e780bShannah_mairs D[i][j] = gllweights[i]*D[i][j]; 2715916e780bShannah_mairs } 2716916e780bShannah_mairs } 2717916e780bShannah_mairs *AA = D; 2718916e780bShannah_mairs PetscFunctionReturn(0); 2719916e780bShannah_mairs } 2720916e780bShannah_mairs 2721916e780bShannah_mairs /*@C 2722916e780bShannah_mairs PetscGaussLobattoLegendreElementAdvectionDestroy - frees the advection stiffness for a single 1d GLL element 2723916e780bShannah_mairs 2724916e780bShannah_mairs Not Collective 2725916e780bShannah_mairs 2726d8d19677SJose E. Roman Input Parameters: 2727916e780bShannah_mairs + n - the number of GLL nodes 2728916e780bShannah_mairs . nodes - the GLL nodes 2729916e780bShannah_mairs . weights - the GLL weights 2730916e780bShannah_mairs - A - advection 2731916e780bShannah_mairs 2732916e780bShannah_mairs Level: beginner 2733916e780bShannah_mairs 2734916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementAdvectionCreate() 2735916e780bShannah_mairs 2736916e780bShannah_mairs @*/ 2737916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementAdvectionDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2738916e780bShannah_mairs { 2739916e780bShannah_mairs PetscFunctionBegin; 27405f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree((*AA)[0])); 27415f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(*AA)); 2742916e780bShannah_mairs *AA = NULL; 2743916e780bShannah_mairs PetscFunctionReturn(0); 2744916e780bShannah_mairs } 2745916e780bShannah_mairs 2746916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementMassCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2747916e780bShannah_mairs { 2748916e780bShannah_mairs PetscReal **A; 2749916e780bShannah_mairs const PetscReal *gllweights = weights; 2750916e780bShannah_mairs const PetscInt glln = n; 2751916e780bShannah_mairs PetscInt i,j; 2752916e780bShannah_mairs 2753916e780bShannah_mairs PetscFunctionBegin; 27545f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(glln,&A)); 27555f80ce2aSJacob Faibussowitsch CHKERRQ(PetscMalloc1(glln*glln,&A[0])); 2756916e780bShannah_mairs for (i=1; i<glln; i++) A[i] = A[i-1]+glln; 2757916e780bShannah_mairs if (glln==1) {A[0][0] = 0.;} 2758916e780bShannah_mairs for (i=0; i<glln; i++) { 2759916e780bShannah_mairs for (j=0; j<glln; j++) { 2760916e780bShannah_mairs A[i][j] = 0.; 2761916e780bShannah_mairs if (j==i) A[i][j] = gllweights[i]; 2762916e780bShannah_mairs } 2763916e780bShannah_mairs } 2764916e780bShannah_mairs *AA = A; 2765916e780bShannah_mairs PetscFunctionReturn(0); 2766916e780bShannah_mairs } 2767916e780bShannah_mairs 2768916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementMassDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2769916e780bShannah_mairs { 2770916e780bShannah_mairs PetscFunctionBegin; 27715f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree((*AA)[0])); 27725f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(*AA)); 2773916e780bShannah_mairs *AA = NULL; 2774916e780bShannah_mairs PetscFunctionReturn(0); 2775916e780bShannah_mairs } 2776d4afb720SToby Isaac 2777d4afb720SToby Isaac /*@ 2778d4afb720SToby Isaac PetscDTIndexToBary - convert an index into a barycentric coordinate. 2779d4afb720SToby Isaac 2780d4afb720SToby Isaac Input Parameters: 2781d4afb720SToby 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) 2782d4afb720SToby Isaac . sum - the value that the sum of the barycentric coordinates (which will be non-negative integers) should sum to 2783d4afb720SToby Isaac - index - the index to convert: should be >= 0 and < Binomial(len - 1 + sum, sum) 2784d4afb720SToby Isaac 2785d4afb720SToby Isaac Output Parameter: 2786d4afb720SToby Isaac . coord - will be filled with the barycentric coordinate 2787d4afb720SToby Isaac 2788d4afb720SToby Isaac Level: beginner 2789d4afb720SToby Isaac 2790d4afb720SToby Isaac Note: the indices map to barycentric coordinates in lexicographic order, where the first index is the 2791d4afb720SToby Isaac least significant and the last index is the most significant. 2792d4afb720SToby Isaac 2793fbdc3dfeSToby Isaac .seealso: PetscDTBaryToIndex() 2794d4afb720SToby Isaac @*/ 2795d4afb720SToby Isaac PetscErrorCode PetscDTIndexToBary(PetscInt len, PetscInt sum, PetscInt index, PetscInt coord[]) 2796d4afb720SToby Isaac { 2797d4afb720SToby Isaac PetscInt c, d, s, total, subtotal, nexttotal; 2798d4afb720SToby Isaac 2799d4afb720SToby Isaac PetscFunctionBeginHot; 28002c71b3e2SJacob Faibussowitsch PetscCheckFalse(len < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative"); 28012c71b3e2SJacob Faibussowitsch PetscCheckFalse(index < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index must be non-negative"); 2802d4afb720SToby Isaac if (!len) { 2803d4afb720SToby Isaac if (!sum && !index) PetscFunctionReturn(0); 2804d4afb720SToby Isaac SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid index or sum for length 0 barycentric coordinate"); 2805d4afb720SToby Isaac } 2806d4afb720SToby Isaac for (c = 1, total = 1; c <= len; c++) { 2807d4afb720SToby Isaac /* total is the number of ways to have a tuple of length c with sum */ 2808d4afb720SToby Isaac if (index < total) break; 2809d4afb720SToby Isaac total = (total * (sum + c)) / c; 2810d4afb720SToby Isaac } 28112c71b3e2SJacob Faibussowitsch PetscCheckFalse(c > len,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index out of range"); 2812d4afb720SToby Isaac for (d = c; d < len; d++) coord[d] = 0; 2813d4afb720SToby Isaac for (s = 0, subtotal = 1, nexttotal = 1; c > 0;) { 2814d4afb720SToby Isaac /* subtotal is the number of ways to have a tuple of length c with sum s */ 2815d4afb720SToby Isaac /* nexttotal is the number of ways to have a tuple of length c-1 with sum s */ 2816d4afb720SToby Isaac if ((index + subtotal) >= total) { 2817d4afb720SToby Isaac coord[--c] = sum - s; 2818d4afb720SToby Isaac index -= (total - subtotal); 2819d4afb720SToby Isaac sum = s; 2820d4afb720SToby Isaac total = nexttotal; 2821d4afb720SToby Isaac subtotal = 1; 2822d4afb720SToby Isaac nexttotal = 1; 2823d4afb720SToby Isaac s = 0; 2824d4afb720SToby Isaac } else { 2825d4afb720SToby Isaac subtotal = (subtotal * (c + s)) / (s + 1); 2826d4afb720SToby Isaac nexttotal = (nexttotal * (c - 1 + s)) / (s + 1); 2827d4afb720SToby Isaac s++; 2828d4afb720SToby Isaac } 2829d4afb720SToby Isaac } 2830d4afb720SToby Isaac PetscFunctionReturn(0); 2831d4afb720SToby Isaac } 2832d4afb720SToby Isaac 2833d4afb720SToby Isaac /*@ 2834d4afb720SToby Isaac PetscDTBaryToIndex - convert a barycentric coordinate to an index 2835d4afb720SToby Isaac 2836d4afb720SToby Isaac Input Parameters: 2837d4afb720SToby 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) 2838d4afb720SToby Isaac . sum - the value that the sum of the barycentric coordinates (which will be non-negative integers) should sum to 2839d4afb720SToby Isaac - coord - a barycentric coordinate with the given length and sum 2840d4afb720SToby Isaac 2841d4afb720SToby Isaac Output Parameter: 2842d4afb720SToby Isaac . index - the unique index for the coordinate, >= 0 and < Binomial(len - 1 + sum, sum) 2843d4afb720SToby Isaac 2844d4afb720SToby Isaac Level: beginner 2845d4afb720SToby Isaac 2846d4afb720SToby Isaac Note: the indices map to barycentric coordinates in lexicographic order, where the first index is the 2847d4afb720SToby Isaac least significant and the last index is the most significant. 2848d4afb720SToby Isaac 2849d4afb720SToby Isaac .seealso: PetscDTIndexToBary 2850d4afb720SToby Isaac @*/ 2851d4afb720SToby Isaac PetscErrorCode PetscDTBaryToIndex(PetscInt len, PetscInt sum, const PetscInt coord[], PetscInt *index) 2852d4afb720SToby Isaac { 2853d4afb720SToby Isaac PetscInt c; 2854d4afb720SToby Isaac PetscInt i; 2855d4afb720SToby Isaac PetscInt total; 2856d4afb720SToby Isaac 2857d4afb720SToby Isaac PetscFunctionBeginHot; 28582c71b3e2SJacob Faibussowitsch PetscCheckFalse(len < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative"); 2859d4afb720SToby Isaac if (!len) { 2860d4afb720SToby Isaac if (!sum) { 2861d4afb720SToby Isaac *index = 0; 2862d4afb720SToby Isaac PetscFunctionReturn(0); 2863d4afb720SToby Isaac } 2864d4afb720SToby Isaac SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid index or sum for length 0 barycentric coordinate"); 2865d4afb720SToby Isaac } 2866d4afb720SToby Isaac for (c = 1, total = 1; c < len; c++) total = (total * (sum + c)) / c; 2867d4afb720SToby Isaac i = total - 1; 2868d4afb720SToby Isaac c = len - 1; 2869d4afb720SToby Isaac sum -= coord[c]; 2870d4afb720SToby Isaac while (sum > 0) { 2871d4afb720SToby Isaac PetscInt subtotal; 2872d4afb720SToby Isaac PetscInt s; 2873d4afb720SToby Isaac 2874d4afb720SToby Isaac for (s = 1, subtotal = 1; s < sum; s++) subtotal = (subtotal * (c + s)) / s; 2875d4afb720SToby Isaac i -= subtotal; 2876d4afb720SToby Isaac sum -= coord[--c]; 2877d4afb720SToby Isaac } 2878d4afb720SToby Isaac *index = i; 2879d4afb720SToby Isaac PetscFunctionReturn(0); 2880d4afb720SToby Isaac } 2881