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 PetscErrorCode ierr; 5821454ff5SMatthew G. Knepley 5921454ff5SMatthew G. Knepley PetscFunctionBegin; 6021454ff5SMatthew G. Knepley PetscValidPointer(q, 2); 612cd22861SMatthew G. Knepley ierr = DMInitializePackage();CHKERRQ(ierr); 622cd22861SMatthew G. Knepley ierr = PetscHeaderCreate(*q,PETSCQUADRATURE_CLASSID,"PetscQuadrature","Quadrature","DT",comm,PetscQuadratureDestroy,PetscQuadratureView);CHKERRQ(ierr); 6321454ff5SMatthew G. Knepley (*q)->dim = -1; 64a6b92713SMatthew G. Knepley (*q)->Nc = 1; 65bcede257SMatthew G. Knepley (*q)->order = -1; 6621454ff5SMatthew G. Knepley (*q)->numPoints = 0; 6721454ff5SMatthew G. Knepley (*q)->points = NULL; 6821454ff5SMatthew G. Knepley (*q)->weights = NULL; 6921454ff5SMatthew G. Knepley PetscFunctionReturn(0); 7021454ff5SMatthew G. Knepley } 7121454ff5SMatthew G. Knepley 72c9638911SMatthew G. Knepley /*@ 73c9638911SMatthew G. Knepley PetscQuadratureDuplicate - Create a deep copy of the PetscQuadrature object 74c9638911SMatthew G. Knepley 75d083f849SBarry Smith Collective on q 76c9638911SMatthew G. Knepley 77c9638911SMatthew G. Knepley Input Parameter: 78c9638911SMatthew G. Knepley . q - The PetscQuadrature object 79c9638911SMatthew G. Knepley 80c9638911SMatthew G. Knepley Output Parameter: 81c9638911SMatthew G. Knepley . r - The new PetscQuadrature object 82c9638911SMatthew G. Knepley 83c9638911SMatthew G. Knepley Level: beginner 84c9638911SMatthew G. Knepley 85c9638911SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureDestroy(), PetscQuadratureGetData() 86c9638911SMatthew G. Knepley @*/ 87c9638911SMatthew G. Knepley PetscErrorCode PetscQuadratureDuplicate(PetscQuadrature q, PetscQuadrature *r) 88c9638911SMatthew G. Knepley { 89a6b92713SMatthew G. Knepley PetscInt order, dim, Nc, Nq; 90c9638911SMatthew G. Knepley const PetscReal *points, *weights; 91c9638911SMatthew G. Knepley PetscReal *p, *w; 92c9638911SMatthew G. Knepley PetscErrorCode ierr; 93c9638911SMatthew G. Knepley 94c9638911SMatthew G. Knepley PetscFunctionBegin; 95064a246eSJacob Faibussowitsch PetscValidPointer(q, 1); 96c9638911SMatthew G. Knepley ierr = PetscQuadratureCreate(PetscObjectComm((PetscObject) q), r);CHKERRQ(ierr); 97c9638911SMatthew G. Knepley ierr = PetscQuadratureGetOrder(q, &order);CHKERRQ(ierr); 98c9638911SMatthew G. Knepley ierr = PetscQuadratureSetOrder(*r, order);CHKERRQ(ierr); 99a6b92713SMatthew G. Knepley ierr = PetscQuadratureGetData(q, &dim, &Nc, &Nq, &points, &weights);CHKERRQ(ierr); 100c9638911SMatthew G. Knepley ierr = PetscMalloc1(Nq*dim, &p);CHKERRQ(ierr); 101f0a0bfafSMatthew G. Knepley ierr = PetscMalloc1(Nq*Nc, &w);CHKERRQ(ierr); 102580bdb30SBarry Smith ierr = PetscArraycpy(p, points, Nq*dim);CHKERRQ(ierr); 103580bdb30SBarry Smith ierr = PetscArraycpy(w, weights, Nc * Nq);CHKERRQ(ierr); 104a6b92713SMatthew G. Knepley ierr = PetscQuadratureSetData(*r, dim, Nc, Nq, p, w);CHKERRQ(ierr); 105c9638911SMatthew G. Knepley PetscFunctionReturn(0); 106c9638911SMatthew G. Knepley } 107c9638911SMatthew G. Knepley 10840d8ff71SMatthew G. Knepley /*@ 10940d8ff71SMatthew G. Knepley PetscQuadratureDestroy - Destroys a PetscQuadrature object 11040d8ff71SMatthew G. Knepley 111d083f849SBarry Smith Collective on q 11240d8ff71SMatthew G. Knepley 11340d8ff71SMatthew G. Knepley Input Parameter: 11440d8ff71SMatthew G. Knepley . q - The PetscQuadrature object 11540d8ff71SMatthew G. Knepley 11640d8ff71SMatthew G. Knepley Level: beginner 11740d8ff71SMatthew G. Knepley 11840d8ff71SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureGetData() 11940d8ff71SMatthew G. Knepley @*/ 120bfa639d9SMatthew G. Knepley PetscErrorCode PetscQuadratureDestroy(PetscQuadrature *q) 121bfa639d9SMatthew G. Knepley { 122bfa639d9SMatthew G. Knepley PetscErrorCode ierr; 123bfa639d9SMatthew G. Knepley 124bfa639d9SMatthew G. Knepley PetscFunctionBegin; 12521454ff5SMatthew G. Knepley if (!*q) PetscFunctionReturn(0); 1262cd22861SMatthew G. Knepley PetscValidHeaderSpecific((*q),PETSCQUADRATURE_CLASSID,1); 12721454ff5SMatthew G. Knepley if (--((PetscObject)(*q))->refct > 0) { 12821454ff5SMatthew G. Knepley *q = NULL; 12921454ff5SMatthew G. Knepley PetscFunctionReturn(0); 13021454ff5SMatthew G. Knepley } 13121454ff5SMatthew G. Knepley ierr = PetscFree((*q)->points);CHKERRQ(ierr); 13221454ff5SMatthew G. Knepley ierr = PetscFree((*q)->weights);CHKERRQ(ierr); 13321454ff5SMatthew G. Knepley ierr = PetscHeaderDestroy(q);CHKERRQ(ierr); 13421454ff5SMatthew G. Knepley PetscFunctionReturn(0); 13521454ff5SMatthew G. Knepley } 13621454ff5SMatthew G. Knepley 137bcede257SMatthew G. Knepley /*@ 138a6b92713SMatthew G. Knepley PetscQuadratureGetOrder - Return the order of the method 139bcede257SMatthew G. Knepley 140bcede257SMatthew G. Knepley Not collective 141bcede257SMatthew G. Knepley 142bcede257SMatthew G. Knepley Input Parameter: 143bcede257SMatthew G. Knepley . q - The PetscQuadrature object 144bcede257SMatthew G. Knepley 145bcede257SMatthew G. Knepley Output Parameter: 146bcede257SMatthew G. Knepley . order - The order of the quadrature, i.e. the highest degree polynomial that is exactly integrated 147bcede257SMatthew G. Knepley 148bcede257SMatthew G. Knepley Level: intermediate 149bcede257SMatthew G. Knepley 150bcede257SMatthew G. Knepley .seealso: PetscQuadratureSetOrder(), PetscQuadratureGetData(), PetscQuadratureSetData() 151bcede257SMatthew G. Knepley @*/ 152bcede257SMatthew G. Knepley PetscErrorCode PetscQuadratureGetOrder(PetscQuadrature q, PetscInt *order) 153bcede257SMatthew G. Knepley { 154bcede257SMatthew G. Knepley PetscFunctionBegin; 1552cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 156bcede257SMatthew G. Knepley PetscValidPointer(order, 2); 157bcede257SMatthew G. Knepley *order = q->order; 158bcede257SMatthew G. Knepley PetscFunctionReturn(0); 159bcede257SMatthew G. Knepley } 160bcede257SMatthew G. Knepley 161bcede257SMatthew G. Knepley /*@ 162a6b92713SMatthew G. Knepley PetscQuadratureSetOrder - Return the order of the method 163bcede257SMatthew G. Knepley 164bcede257SMatthew G. Knepley Not collective 165bcede257SMatthew G. Knepley 166bcede257SMatthew G. Knepley Input Parameters: 167bcede257SMatthew G. Knepley + q - The PetscQuadrature object 168bcede257SMatthew G. Knepley - order - The order of the quadrature, i.e. the highest degree polynomial that is exactly integrated 169bcede257SMatthew G. Knepley 170bcede257SMatthew G. Knepley Level: intermediate 171bcede257SMatthew G. Knepley 172bcede257SMatthew G. Knepley .seealso: PetscQuadratureGetOrder(), PetscQuadratureGetData(), PetscQuadratureSetData() 173bcede257SMatthew G. Knepley @*/ 174bcede257SMatthew G. Knepley PetscErrorCode PetscQuadratureSetOrder(PetscQuadrature q, PetscInt order) 175bcede257SMatthew G. Knepley { 176bcede257SMatthew G. Knepley PetscFunctionBegin; 1772cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 178bcede257SMatthew G. Knepley q->order = order; 179bcede257SMatthew G. Knepley PetscFunctionReturn(0); 180bcede257SMatthew G. Knepley } 181bcede257SMatthew G. Knepley 182a6b92713SMatthew G. Knepley /*@ 183a6b92713SMatthew G. Knepley PetscQuadratureGetNumComponents - Return the number of components for functions to be integrated 184a6b92713SMatthew G. Knepley 185a6b92713SMatthew G. Knepley Not collective 186a6b92713SMatthew G. Knepley 187a6b92713SMatthew G. Knepley Input Parameter: 188a6b92713SMatthew G. Knepley . q - The PetscQuadrature object 189a6b92713SMatthew G. Knepley 190a6b92713SMatthew G. Knepley Output Parameter: 191a6b92713SMatthew G. Knepley . Nc - The number of components 192a6b92713SMatthew G. Knepley 193a6b92713SMatthew G. Knepley Note: We are performing an integral int f(x) . w(x) dx, where both f and w (the weight) have Nc components. 194a6b92713SMatthew G. Knepley 195a6b92713SMatthew G. Knepley Level: intermediate 196a6b92713SMatthew G. Knepley 197a6b92713SMatthew G. Knepley .seealso: PetscQuadratureSetNumComponents(), PetscQuadratureGetData(), PetscQuadratureSetData() 198a6b92713SMatthew G. Knepley @*/ 199a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureGetNumComponents(PetscQuadrature q, PetscInt *Nc) 200a6b92713SMatthew G. Knepley { 201a6b92713SMatthew G. Knepley PetscFunctionBegin; 2022cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 203a6b92713SMatthew G. Knepley PetscValidPointer(Nc, 2); 204a6b92713SMatthew G. Knepley *Nc = q->Nc; 205a6b92713SMatthew G. Knepley PetscFunctionReturn(0); 206a6b92713SMatthew G. Knepley } 207a6b92713SMatthew G. Knepley 208a6b92713SMatthew G. Knepley /*@ 209a6b92713SMatthew G. Knepley PetscQuadratureSetNumComponents - Return the number of components for functions to be integrated 210a6b92713SMatthew G. Knepley 211a6b92713SMatthew G. Knepley Not collective 212a6b92713SMatthew G. Knepley 213a6b92713SMatthew G. Knepley Input Parameters: 214a6b92713SMatthew G. Knepley + q - The PetscQuadrature object 215a6b92713SMatthew G. Knepley - Nc - The number of components 216a6b92713SMatthew G. Knepley 217a6b92713SMatthew G. Knepley Note: We are performing an integral int f(x) . w(x) dx, where both f and w (the weight) have Nc components. 218a6b92713SMatthew G. Knepley 219a6b92713SMatthew G. Knepley Level: intermediate 220a6b92713SMatthew G. Knepley 221a6b92713SMatthew G. Knepley .seealso: PetscQuadratureGetNumComponents(), PetscQuadratureGetData(), PetscQuadratureSetData() 222a6b92713SMatthew G. Knepley @*/ 223a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureSetNumComponents(PetscQuadrature q, PetscInt Nc) 224a6b92713SMatthew G. Knepley { 225a6b92713SMatthew G. Knepley PetscFunctionBegin; 2262cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 227a6b92713SMatthew G. Knepley q->Nc = Nc; 228a6b92713SMatthew G. Knepley PetscFunctionReturn(0); 229a6b92713SMatthew G. Knepley } 230a6b92713SMatthew G. Knepley 23140d8ff71SMatthew G. Knepley /*@C 23240d8ff71SMatthew G. Knepley PetscQuadratureGetData - Returns the data defining the quadrature 23340d8ff71SMatthew G. Knepley 23440d8ff71SMatthew G. Knepley Not collective 23540d8ff71SMatthew G. Knepley 23640d8ff71SMatthew G. Knepley Input Parameter: 23740d8ff71SMatthew G. Knepley . q - The PetscQuadrature object 23840d8ff71SMatthew G. Knepley 23940d8ff71SMatthew G. Knepley Output Parameters: 24040d8ff71SMatthew G. Knepley + dim - The spatial dimension 241805e7170SToby Isaac . Nc - The number of components 24240d8ff71SMatthew G. Knepley . npoints - The number of quadrature points 24340d8ff71SMatthew G. Knepley . points - The coordinates of each quadrature point 24440d8ff71SMatthew G. Knepley - weights - The weight of each quadrature point 24540d8ff71SMatthew G. Knepley 24640d8ff71SMatthew G. Knepley Level: intermediate 24740d8ff71SMatthew G. Knepley 24895452b02SPatrick Sanan Fortran Notes: 24995452b02SPatrick Sanan From Fortran you must call PetscQuadratureRestoreData() when you are done with the data 2501fd49c25SBarry Smith 25140d8ff71SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureSetData() 25240d8ff71SMatthew G. Knepley @*/ 253a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureGetData(PetscQuadrature q, PetscInt *dim, PetscInt *Nc, PetscInt *npoints, const PetscReal *points[], const PetscReal *weights[]) 25421454ff5SMatthew G. Knepley { 25521454ff5SMatthew G. Knepley PetscFunctionBegin; 2562cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 25721454ff5SMatthew G. Knepley if (dim) { 25821454ff5SMatthew G. Knepley PetscValidPointer(dim, 2); 25921454ff5SMatthew G. Knepley *dim = q->dim; 26021454ff5SMatthew G. Knepley } 261a6b92713SMatthew G. Knepley if (Nc) { 262a6b92713SMatthew G. Knepley PetscValidPointer(Nc, 3); 263a6b92713SMatthew G. Knepley *Nc = q->Nc; 264a6b92713SMatthew G. Knepley } 26521454ff5SMatthew G. Knepley if (npoints) { 266a6b92713SMatthew G. Knepley PetscValidPointer(npoints, 4); 26721454ff5SMatthew G. Knepley *npoints = q->numPoints; 26821454ff5SMatthew G. Knepley } 26921454ff5SMatthew G. Knepley if (points) { 270a6b92713SMatthew G. Knepley PetscValidPointer(points, 5); 27121454ff5SMatthew G. Knepley *points = q->points; 27221454ff5SMatthew G. Knepley } 27321454ff5SMatthew G. Knepley if (weights) { 274a6b92713SMatthew G. Knepley PetscValidPointer(weights, 6); 27521454ff5SMatthew G. Knepley *weights = q->weights; 27621454ff5SMatthew G. Knepley } 27721454ff5SMatthew G. Knepley PetscFunctionReturn(0); 27821454ff5SMatthew G. Knepley } 27921454ff5SMatthew G. Knepley 280907761f8SToby Isaac static PetscErrorCode PetscDTJacobianInverse_Internal(PetscInt m, PetscInt n, const PetscReal J[], PetscReal Jinv[]) 281907761f8SToby Isaac { 282907761f8SToby Isaac PetscScalar *Js, *Jinvs; 283907761f8SToby Isaac PetscInt i, j, k; 284907761f8SToby Isaac PetscBLASInt bm, bn, info; 285907761f8SToby Isaac PetscErrorCode ierr; 286907761f8SToby Isaac 287907761f8SToby Isaac PetscFunctionBegin; 288d4afb720SToby Isaac if (!m || !n) PetscFunctionReturn(0); 289907761f8SToby Isaac ierr = PetscBLASIntCast(m, &bm);CHKERRQ(ierr); 290907761f8SToby Isaac ierr = PetscBLASIntCast(n, &bn);CHKERRQ(ierr); 291907761f8SToby Isaac #if defined(PETSC_USE_COMPLEX) 292907761f8SToby Isaac ierr = PetscMalloc2(m*n, &Js, m*n, &Jinvs);CHKERRQ(ierr); 29328222859SToby Isaac for (i = 0; i < m*n; i++) Js[i] = J[i]; 294907761f8SToby Isaac #else 295907761f8SToby Isaac Js = (PetscReal *) J; 296907761f8SToby Isaac Jinvs = Jinv; 297907761f8SToby Isaac #endif 298907761f8SToby Isaac if (m == n) { 299907761f8SToby Isaac PetscBLASInt *pivots; 300907761f8SToby Isaac PetscScalar *W; 301907761f8SToby Isaac 302907761f8SToby Isaac ierr = PetscMalloc2(m, &pivots, m, &W);CHKERRQ(ierr); 303907761f8SToby Isaac 304907761f8SToby Isaac ierr = PetscArraycpy(Jinvs, Js, m * m);CHKERRQ(ierr); 305907761f8SToby Isaac PetscStackCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bm, &bm, Jinvs, &bm, pivots, &info)); 3062c71b3e2SJacob Faibussowitsch PetscCheckFalse(info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetrf %D",(PetscInt)info); 307907761f8SToby Isaac PetscStackCallBLAS("LAPACKgetri", LAPACKgetri_(&bm, Jinvs, &bm, pivots, W, &bm, &info)); 3082c71b3e2SJacob Faibussowitsch PetscCheckFalse(info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetri %D",(PetscInt)info); 309907761f8SToby Isaac ierr = PetscFree2(pivots, W);CHKERRQ(ierr); 310907761f8SToby Isaac } else if (m < n) { 311907761f8SToby Isaac PetscScalar *JJT; 312907761f8SToby Isaac PetscBLASInt *pivots; 313907761f8SToby Isaac PetscScalar *W; 314907761f8SToby Isaac 315907761f8SToby Isaac ierr = PetscMalloc1(m*m, &JJT);CHKERRQ(ierr); 316907761f8SToby Isaac ierr = PetscMalloc2(m, &pivots, m, &W);CHKERRQ(ierr); 317907761f8SToby Isaac for (i = 0; i < m; i++) { 318907761f8SToby Isaac for (j = 0; j < m; j++) { 319907761f8SToby Isaac PetscScalar val = 0.; 320907761f8SToby Isaac 321907761f8SToby Isaac for (k = 0; k < n; k++) val += Js[i * n + k] * Js[j * n + k]; 322907761f8SToby Isaac JJT[i * m + j] = val; 323907761f8SToby Isaac } 324907761f8SToby Isaac } 325907761f8SToby Isaac 326907761f8SToby Isaac PetscStackCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bm, &bm, JJT, &bm, pivots, &info)); 3272c71b3e2SJacob Faibussowitsch PetscCheckFalse(info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetrf %D",(PetscInt)info); 328907761f8SToby Isaac PetscStackCallBLAS("LAPACKgetri", LAPACKgetri_(&bm, JJT, &bm, pivots, W, &bm, &info)); 3292c71b3e2SJacob Faibussowitsch PetscCheckFalse(info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetri %D",(PetscInt)info); 330907761f8SToby Isaac for (i = 0; i < n; i++) { 331907761f8SToby Isaac for (j = 0; j < m; j++) { 332907761f8SToby Isaac PetscScalar val = 0.; 333907761f8SToby Isaac 334907761f8SToby Isaac for (k = 0; k < m; k++) val += Js[k * n + i] * JJT[k * m + j]; 335907761f8SToby Isaac Jinvs[i * m + j] = val; 336907761f8SToby Isaac } 337907761f8SToby Isaac } 338907761f8SToby Isaac ierr = PetscFree2(pivots, W);CHKERRQ(ierr); 339907761f8SToby Isaac ierr = PetscFree(JJT);CHKERRQ(ierr); 340907761f8SToby Isaac } else { 341907761f8SToby Isaac PetscScalar *JTJ; 342907761f8SToby Isaac PetscBLASInt *pivots; 343907761f8SToby Isaac PetscScalar *W; 344907761f8SToby Isaac 345907761f8SToby Isaac ierr = PetscMalloc1(n*n, &JTJ);CHKERRQ(ierr); 346907761f8SToby Isaac ierr = PetscMalloc2(n, &pivots, n, &W);CHKERRQ(ierr); 347907761f8SToby Isaac for (i = 0; i < n; i++) { 348907761f8SToby Isaac for (j = 0; j < n; j++) { 349907761f8SToby Isaac PetscScalar val = 0.; 350907761f8SToby Isaac 351907761f8SToby Isaac for (k = 0; k < m; k++) val += Js[k * n + i] * Js[k * n + j]; 352907761f8SToby Isaac JTJ[i * n + j] = val; 353907761f8SToby Isaac } 354907761f8SToby Isaac } 355907761f8SToby Isaac 356d4afb720SToby Isaac PetscStackCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bn, &bn, JTJ, &bn, pivots, &info)); 3572c71b3e2SJacob Faibussowitsch PetscCheckFalse(info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetrf %D",(PetscInt)info); 358907761f8SToby Isaac PetscStackCallBLAS("LAPACKgetri", LAPACKgetri_(&bn, JTJ, &bn, pivots, W, &bn, &info)); 3592c71b3e2SJacob Faibussowitsch PetscCheckFalse(info,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetri %D",(PetscInt)info); 360907761f8SToby Isaac for (i = 0; i < n; i++) { 361907761f8SToby Isaac for (j = 0; j < m; j++) { 362907761f8SToby Isaac PetscScalar val = 0.; 363907761f8SToby Isaac 364907761f8SToby Isaac for (k = 0; k < n; k++) val += JTJ[i * n + k] * Js[j * n + k]; 365907761f8SToby Isaac Jinvs[i * m + j] = val; 366907761f8SToby Isaac } 367907761f8SToby Isaac } 368907761f8SToby Isaac ierr = PetscFree2(pivots, W);CHKERRQ(ierr); 369907761f8SToby Isaac ierr = PetscFree(JTJ);CHKERRQ(ierr); 370907761f8SToby Isaac } 371907761f8SToby Isaac #if defined(PETSC_USE_COMPLEX) 37228222859SToby Isaac for (i = 0; i < m*n; i++) Jinv[i] = PetscRealPart(Jinvs[i]); 373907761f8SToby Isaac ierr = PetscFree2(Js, Jinvs);CHKERRQ(ierr); 374907761f8SToby Isaac #endif 375907761f8SToby Isaac PetscFunctionReturn(0); 376907761f8SToby Isaac } 377907761f8SToby Isaac 378907761f8SToby Isaac /*@ 379907761f8SToby Isaac PetscQuadraturePushForward - Push forward a quadrature functional under an affine transformation. 380907761f8SToby Isaac 381907761f8SToby Isaac Collecive on PetscQuadrature 382907761f8SToby Isaac 3834165533cSJose E. Roman Input Parameters: 384907761f8SToby Isaac + q - the quadrature functional 385907761f8SToby Isaac . imageDim - the dimension of the image of the transformation 386907761f8SToby Isaac . origin - a point in the original space 387907761f8SToby Isaac . originImage - the image of the origin under the transformation 388907761f8SToby Isaac . J - the Jacobian of the image: an [imageDim x dim] matrix in row major order 38928222859SToby Isaac - formDegree - transform the quadrature weights as k-forms of this form degree (if the number of components is a multiple of (dim choose formDegree), it is assumed that they represent multiple k-forms) [see PetscDTAltVPullback() for interpretation of formDegree] 390907761f8SToby Isaac 3914165533cSJose E. Roman Output Parameters: 392907761f8SToby Isaac . Jinvstarq - a quadrature rule where each point is the image of a point in the original quadrature rule, and where the k-form weights have been pulled-back by the pseudoinverse of J to the k-form weights in the image space. 393907761f8SToby Isaac 394907761f8SToby Isaac Note: the new quadrature rule will have a different number of components if spaces have different dimensions. For example, pushing a 2-form forward from a two dimensional space to a three dimensional space changes the number of components from 1 to 3. 395907761f8SToby Isaac 3966c877ef6SSatish Balay Level: intermediate 3976c877ef6SSatish Balay 398907761f8SToby Isaac .seealso: PetscDTAltVPullback(), PetscDTAltVPullbackMatrix() 399907761f8SToby Isaac @*/ 40028222859SToby Isaac PetscErrorCode PetscQuadraturePushForward(PetscQuadrature q, PetscInt imageDim, const PetscReal origin[], const PetscReal originImage[], const PetscReal J[], PetscInt formDegree, PetscQuadrature *Jinvstarq) 401907761f8SToby Isaac { 402907761f8SToby Isaac PetscInt dim, Nc, imageNc, formSize, Ncopies, imageFormSize, Npoints, pt, i, j, c; 403907761f8SToby Isaac const PetscReal *points; 404907761f8SToby Isaac const PetscReal *weights; 405907761f8SToby Isaac PetscReal *imagePoints, *imageWeights; 406907761f8SToby Isaac PetscReal *Jinv; 407907761f8SToby Isaac PetscReal *Jinvstar; 408907761f8SToby Isaac PetscErrorCode ierr; 409907761f8SToby Isaac 410907761f8SToby Isaac PetscFunctionBegin; 411d4afb720SToby Isaac PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 4122c71b3e2SJacob Faibussowitsch PetscCheckFalse(imageDim < PetscAbsInt(formDegree),PetscObjectComm((PetscObject)q), PETSC_ERR_ARG_INCOMP, "Cannot represent a %D-form in %D dimensions", PetscAbsInt(formDegree), imageDim); 413907761f8SToby Isaac ierr = PetscQuadratureGetData(q, &dim, &Nc, &Npoints, &points, &weights);CHKERRQ(ierr); 41428222859SToby Isaac ierr = PetscDTBinomialInt(dim, PetscAbsInt(formDegree), &formSize);CHKERRQ(ierr); 4152c71b3e2SJacob Faibussowitsch PetscCheckFalse(Nc % formSize,PetscObjectComm((PetscObject)q), PETSC_ERR_ARG_INCOMP, "Number of components %D is not a multiple of formSize %D", Nc, formSize); 416907761f8SToby Isaac Ncopies = Nc / formSize; 41728222859SToby Isaac ierr = PetscDTBinomialInt(imageDim, PetscAbsInt(formDegree), &imageFormSize);CHKERRQ(ierr); 418907761f8SToby Isaac imageNc = Ncopies * imageFormSize; 419907761f8SToby Isaac ierr = PetscMalloc1(Npoints * imageDim, &imagePoints);CHKERRQ(ierr); 420907761f8SToby Isaac ierr = PetscMalloc1(Npoints * imageNc, &imageWeights);CHKERRQ(ierr); 421907761f8SToby Isaac ierr = PetscMalloc2(imageDim * dim, &Jinv, formSize * imageFormSize, &Jinvstar);CHKERRQ(ierr); 422d4afb720SToby Isaac ierr = PetscDTJacobianInverse_Internal(imageDim, dim, J, Jinv);CHKERRQ(ierr); 42328222859SToby Isaac ierr = PetscDTAltVPullbackMatrix(imageDim, dim, Jinv, formDegree, Jinvstar);CHKERRQ(ierr); 424907761f8SToby Isaac for (pt = 0; pt < Npoints; pt++) { 425907761f8SToby Isaac const PetscReal *point = &points[pt * dim]; 426907761f8SToby Isaac PetscReal *imagePoint = &imagePoints[pt * imageDim]; 427907761f8SToby Isaac 428907761f8SToby Isaac for (i = 0; i < imageDim; i++) { 429907761f8SToby Isaac PetscReal val = originImage[i]; 430907761f8SToby Isaac 431907761f8SToby Isaac for (j = 0; j < dim; j++) val += J[i * dim + j] * (point[j] - origin[j]); 432907761f8SToby Isaac imagePoint[i] = val; 433907761f8SToby Isaac } 434907761f8SToby Isaac for (c = 0; c < Ncopies; c++) { 435907761f8SToby Isaac const PetscReal *form = &weights[pt * Nc + c * formSize]; 436907761f8SToby Isaac PetscReal *imageForm = &imageWeights[pt * imageNc + c * imageFormSize]; 437907761f8SToby Isaac 438907761f8SToby Isaac for (i = 0; i < imageFormSize; i++) { 439907761f8SToby Isaac PetscReal val = 0.; 440907761f8SToby Isaac 441907761f8SToby Isaac for (j = 0; j < formSize; j++) val += Jinvstar[i * formSize + j] * form[j]; 442907761f8SToby Isaac imageForm[i] = val; 443907761f8SToby Isaac } 444907761f8SToby Isaac } 445907761f8SToby Isaac } 446907761f8SToby Isaac ierr = PetscQuadratureCreate(PetscObjectComm((PetscObject)q), Jinvstarq);CHKERRQ(ierr); 447907761f8SToby Isaac ierr = PetscQuadratureSetData(*Jinvstarq, imageDim, imageNc, Npoints, imagePoints, imageWeights);CHKERRQ(ierr); 448907761f8SToby Isaac ierr = PetscFree2(Jinv, Jinvstar);CHKERRQ(ierr); 449907761f8SToby Isaac PetscFunctionReturn(0); 450907761f8SToby Isaac } 451907761f8SToby Isaac 45240d8ff71SMatthew G. Knepley /*@C 45340d8ff71SMatthew G. Knepley PetscQuadratureSetData - Sets the data defining the quadrature 45440d8ff71SMatthew G. Knepley 45540d8ff71SMatthew G. Knepley Not collective 45640d8ff71SMatthew G. Knepley 45740d8ff71SMatthew G. Knepley Input Parameters: 45840d8ff71SMatthew G. Knepley + q - The PetscQuadrature object 45940d8ff71SMatthew G. Knepley . dim - The spatial dimension 460e2b35d93SBarry Smith . Nc - The number of components 46140d8ff71SMatthew G. Knepley . npoints - The number of quadrature points 46240d8ff71SMatthew G. Knepley . points - The coordinates of each quadrature point 46340d8ff71SMatthew G. Knepley - weights - The weight of each quadrature point 46440d8ff71SMatthew G. Knepley 465c99e0549SMatthew G. Knepley Note: This routine owns the references to points and weights, so they must be allocated using PetscMalloc() and the user should not free them. 466f2fd9e53SMatthew G. Knepley 46740d8ff71SMatthew G. Knepley Level: intermediate 46840d8ff71SMatthew G. Knepley 46940d8ff71SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureGetData() 47040d8ff71SMatthew G. Knepley @*/ 471a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureSetData(PetscQuadrature q, PetscInt dim, PetscInt Nc, PetscInt npoints, const PetscReal points[], const PetscReal weights[]) 47221454ff5SMatthew G. Knepley { 47321454ff5SMatthew G. Knepley PetscFunctionBegin; 4742cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 47521454ff5SMatthew G. Knepley if (dim >= 0) q->dim = dim; 476a6b92713SMatthew G. Knepley if (Nc >= 0) q->Nc = Nc; 47721454ff5SMatthew G. Knepley if (npoints >= 0) q->numPoints = npoints; 47821454ff5SMatthew G. Knepley if (points) { 479064a246eSJacob Faibussowitsch PetscValidPointer(points, 5); 48021454ff5SMatthew G. Knepley q->points = points; 48121454ff5SMatthew G. Knepley } 48221454ff5SMatthew G. Knepley if (weights) { 483064a246eSJacob Faibussowitsch PetscValidPointer(weights, 6); 48421454ff5SMatthew G. Knepley q->weights = weights; 48521454ff5SMatthew G. Knepley } 486f9fd7fdbSMatthew G. Knepley PetscFunctionReturn(0); 487f9fd7fdbSMatthew G. Knepley } 488f9fd7fdbSMatthew G. Knepley 489d9bac1caSLisandro Dalcin static PetscErrorCode PetscQuadratureView_Ascii(PetscQuadrature quad, PetscViewer v) 490d9bac1caSLisandro Dalcin { 491d9bac1caSLisandro Dalcin PetscInt q, d, c; 492d9bac1caSLisandro Dalcin PetscViewerFormat format; 493d9bac1caSLisandro Dalcin PetscErrorCode ierr; 494d9bac1caSLisandro Dalcin 495d9bac1caSLisandro Dalcin PetscFunctionBegin; 496c74b4a09SMatthew G. Knepley if (quad->Nc > 1) {ierr = PetscViewerASCIIPrintf(v, "Quadrature of order %D on %D points (dim %D) with %D components\n", quad->order, quad->numPoints, quad->dim, quad->Nc);CHKERRQ(ierr);} 497c74b4a09SMatthew G. Knepley else {ierr = PetscViewerASCIIPrintf(v, "Quadrature of order %D on %D points (dim %D)\n", quad->order, quad->numPoints, quad->dim);CHKERRQ(ierr);} 498d9bac1caSLisandro Dalcin ierr = PetscViewerGetFormat(v, &format);CHKERRQ(ierr); 499d9bac1caSLisandro Dalcin if (format != PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(0); 500d9bac1caSLisandro Dalcin for (q = 0; q < quad->numPoints; ++q) { 501c74b4a09SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(v, "p%D (", q);CHKERRQ(ierr); 502d9bac1caSLisandro Dalcin ierr = PetscViewerASCIIUseTabs(v, PETSC_FALSE);CHKERRQ(ierr); 503d9bac1caSLisandro Dalcin for (d = 0; d < quad->dim; ++d) { 504d9bac1caSLisandro Dalcin if (d) {ierr = PetscViewerASCIIPrintf(v, ", ");CHKERRQ(ierr);} 505d9bac1caSLisandro Dalcin ierr = PetscViewerASCIIPrintf(v, "%+g", (double)quad->points[q*quad->dim+d]);CHKERRQ(ierr); 506d9bac1caSLisandro Dalcin } 507d9bac1caSLisandro Dalcin ierr = PetscViewerASCIIPrintf(v, ") ");CHKERRQ(ierr); 508c74b4a09SMatthew G. Knepley if (quad->Nc > 1) {ierr = PetscViewerASCIIPrintf(v, "w%D (", q);CHKERRQ(ierr);} 509d9bac1caSLisandro Dalcin for (c = 0; c < quad->Nc; ++c) { 510d9bac1caSLisandro Dalcin if (c) {ierr = PetscViewerASCIIPrintf(v, ", ");CHKERRQ(ierr);} 511c74b4a09SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(v, "%+g", (double)quad->weights[q*quad->Nc+c]);CHKERRQ(ierr); 512d9bac1caSLisandro Dalcin } 513d9bac1caSLisandro Dalcin if (quad->Nc > 1) {ierr = PetscViewerASCIIPrintf(v, ")");CHKERRQ(ierr);} 514d9bac1caSLisandro Dalcin ierr = PetscViewerASCIIPrintf(v, "\n");CHKERRQ(ierr); 515d9bac1caSLisandro Dalcin ierr = PetscViewerASCIIUseTabs(v, PETSC_TRUE);CHKERRQ(ierr); 516d9bac1caSLisandro Dalcin } 517d9bac1caSLisandro Dalcin PetscFunctionReturn(0); 518d9bac1caSLisandro Dalcin } 519d9bac1caSLisandro Dalcin 52040d8ff71SMatthew G. Knepley /*@C 52140d8ff71SMatthew G. Knepley PetscQuadratureView - Views a PetscQuadrature object 52240d8ff71SMatthew G. Knepley 523d083f849SBarry Smith Collective on quad 52440d8ff71SMatthew G. Knepley 52540d8ff71SMatthew G. Knepley Input Parameters: 526d9bac1caSLisandro Dalcin + quad - The PetscQuadrature object 52740d8ff71SMatthew G. Knepley - viewer - The PetscViewer object 52840d8ff71SMatthew G. Knepley 52940d8ff71SMatthew G. Knepley Level: beginner 53040d8ff71SMatthew G. Knepley 53140d8ff71SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureGetData() 53240d8ff71SMatthew G. Knepley @*/ 533f9fd7fdbSMatthew G. Knepley PetscErrorCode PetscQuadratureView(PetscQuadrature quad, PetscViewer viewer) 534f9fd7fdbSMatthew G. Knepley { 535d9bac1caSLisandro Dalcin PetscBool iascii; 536f9fd7fdbSMatthew G. Knepley PetscErrorCode ierr; 537f9fd7fdbSMatthew G. Knepley 538f9fd7fdbSMatthew G. Knepley PetscFunctionBegin; 539d9bac1caSLisandro Dalcin PetscValidHeader(quad, 1); 540d9bac1caSLisandro Dalcin if (viewer) PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 541d9bac1caSLisandro Dalcin if (!viewer) {ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject) quad), &viewer);CHKERRQ(ierr);} 542d9bac1caSLisandro Dalcin ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 543d9bac1caSLisandro Dalcin ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 544d9bac1caSLisandro Dalcin if (iascii) {ierr = PetscQuadratureView_Ascii(quad, viewer);CHKERRQ(ierr);} 545d9bac1caSLisandro Dalcin ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 546bfa639d9SMatthew G. Knepley PetscFunctionReturn(0); 547bfa639d9SMatthew G. Knepley } 548bfa639d9SMatthew G. Knepley 54989710940SMatthew G. Knepley /*@C 55089710940SMatthew G. Knepley PetscQuadratureExpandComposite - Return a quadrature over the composite element, which has the original quadrature in each subelement 55189710940SMatthew G. Knepley 55289710940SMatthew G. Knepley Not collective 55389710940SMatthew G. Knepley 554d8d19677SJose E. Roman Input Parameters: 55589710940SMatthew G. Knepley + q - The original PetscQuadrature 55689710940SMatthew G. Knepley . numSubelements - The number of subelements the original element is divided into 55789710940SMatthew G. Knepley . v0 - An array of the initial points for each subelement 55889710940SMatthew G. Knepley - jac - An array of the Jacobian mappings from the reference to each subelement 55989710940SMatthew G. Knepley 56089710940SMatthew G. Knepley Output Parameters: 56189710940SMatthew G. Knepley . dim - The dimension 56289710940SMatthew G. Knepley 56389710940SMatthew G. Knepley Note: Together v0 and jac define an affine mapping from the original reference element to each subelement 56489710940SMatthew G. Knepley 565f5f57ec0SBarry Smith Not available from Fortran 566f5f57ec0SBarry Smith 56789710940SMatthew G. Knepley Level: intermediate 56889710940SMatthew G. Knepley 56989710940SMatthew G. Knepley .seealso: PetscFECreate(), PetscSpaceGetDimension(), PetscDualSpaceGetDimension() 57089710940SMatthew G. Knepley @*/ 57189710940SMatthew G. Knepley PetscErrorCode PetscQuadratureExpandComposite(PetscQuadrature q, PetscInt numSubelements, const PetscReal v0[], const PetscReal jac[], PetscQuadrature *qref) 57289710940SMatthew G. Knepley { 57389710940SMatthew G. Knepley const PetscReal *points, *weights; 57489710940SMatthew G. Knepley PetscReal *pointsRef, *weightsRef; 575a6b92713SMatthew G. Knepley PetscInt dim, Nc, order, npoints, npointsRef, c, p, cp, d, e; 57689710940SMatthew G. Knepley PetscErrorCode ierr; 57789710940SMatthew G. Knepley 57889710940SMatthew G. Knepley PetscFunctionBegin; 5792cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 58089710940SMatthew G. Knepley PetscValidPointer(v0, 3); 58189710940SMatthew G. Knepley PetscValidPointer(jac, 4); 58289710940SMatthew G. Knepley PetscValidPointer(qref, 5); 58389710940SMatthew G. Knepley ierr = PetscQuadratureCreate(PETSC_COMM_SELF, qref);CHKERRQ(ierr); 58489710940SMatthew G. Knepley ierr = PetscQuadratureGetOrder(q, &order);CHKERRQ(ierr); 585a6b92713SMatthew G. Knepley ierr = PetscQuadratureGetData(q, &dim, &Nc, &npoints, &points, &weights);CHKERRQ(ierr); 58689710940SMatthew G. Knepley npointsRef = npoints*numSubelements; 58789710940SMatthew G. Knepley ierr = PetscMalloc1(npointsRef*dim,&pointsRef);CHKERRQ(ierr); 588a6b92713SMatthew G. Knepley ierr = PetscMalloc1(npointsRef*Nc, &weightsRef);CHKERRQ(ierr); 58989710940SMatthew G. Knepley for (c = 0; c < numSubelements; ++c) { 59089710940SMatthew G. Knepley for (p = 0; p < npoints; ++p) { 59189710940SMatthew G. Knepley for (d = 0; d < dim; ++d) { 59289710940SMatthew G. Knepley pointsRef[(c*npoints + p)*dim+d] = v0[c*dim+d]; 59389710940SMatthew G. Knepley for (e = 0; e < dim; ++e) { 59489710940SMatthew G. Knepley pointsRef[(c*npoints + p)*dim+d] += jac[(c*dim + d)*dim+e]*(points[p*dim+e] + 1.0); 59589710940SMatthew G. Knepley } 59689710940SMatthew G. Knepley } 59789710940SMatthew G. Knepley /* Could also use detJ here */ 598a6b92713SMatthew G. Knepley for (cp = 0; cp < Nc; ++cp) weightsRef[(c*npoints+p)*Nc+cp] = weights[p*Nc+cp]/numSubelements; 59989710940SMatthew G. Knepley } 60089710940SMatthew G. Knepley } 60189710940SMatthew G. Knepley ierr = PetscQuadratureSetOrder(*qref, order);CHKERRQ(ierr); 602a6b92713SMatthew G. Knepley ierr = PetscQuadratureSetData(*qref, dim, Nc, npointsRef, pointsRef, weightsRef);CHKERRQ(ierr); 60389710940SMatthew G. Knepley PetscFunctionReturn(0); 60489710940SMatthew G. Knepley } 60589710940SMatthew G. Knepley 60694e21283SToby Isaac /* Compute the coefficients for the Jacobi polynomial recurrence, 60794e21283SToby Isaac * 60894e21283SToby Isaac * J^{a,b}_n(x) = (cnm1 + cnm1x * x) * J^{a,b}_{n-1}(x) - cnm2 * J^{a,b}_{n-2}(x). 60994e21283SToby Isaac */ 61094e21283SToby Isaac #define PetscDTJacobiRecurrence_Internal(n,a,b,cnm1,cnm1x,cnm2) \ 61194e21283SToby Isaac do { \ 61294e21283SToby Isaac PetscReal _a = (a); \ 61394e21283SToby Isaac PetscReal _b = (b); \ 61494e21283SToby Isaac PetscReal _n = (n); \ 61594e21283SToby Isaac if (n == 1) { \ 61694e21283SToby Isaac (cnm1) = (_a-_b) * 0.5; \ 61794e21283SToby Isaac (cnm1x) = (_a+_b+2.)*0.5; \ 61894e21283SToby Isaac (cnm2) = 0.; \ 61994e21283SToby Isaac } else { \ 62094e21283SToby Isaac PetscReal _2n = _n+_n; \ 62194e21283SToby Isaac PetscReal _d = (_2n*(_n+_a+_b)*(_2n+_a+_b-2)); \ 62294e21283SToby Isaac PetscReal _n1 = (_2n+_a+_b-1.)*(_a*_a-_b*_b); \ 62394e21283SToby Isaac PetscReal _n1x = (_2n+_a+_b-1.)*(_2n+_a+_b)*(_2n+_a+_b-2); \ 62494e21283SToby Isaac PetscReal _n2 = 2.*((_n+_a-1.)*(_n+_b-1.)*(_2n+_a+_b)); \ 62594e21283SToby Isaac (cnm1) = _n1 / _d; \ 62694e21283SToby Isaac (cnm1x) = _n1x / _d; \ 62794e21283SToby Isaac (cnm2) = _n2 / _d; \ 62894e21283SToby Isaac } \ 62994e21283SToby Isaac } while (0) 63094e21283SToby Isaac 631fbdc3dfeSToby Isaac /*@ 632fbdc3dfeSToby Isaac PetscDTJacobiNorm - Compute the weighted L2 norm of a Jacobi polynomial. 633fbdc3dfeSToby Isaac 634fbdc3dfeSToby Isaac $\| P^{\alpha,\beta}_n \|_{\alpha,\beta}^2 = \int_{-1}^1 (1 + x)^{\alpha} (1 - x)^{\beta} P^{\alpha,\beta}_n (x)^2 dx.$ 635fbdc3dfeSToby Isaac 6364165533cSJose E. Roman Input Parameters: 637fbdc3dfeSToby Isaac - alpha - the left exponent > -1 638fbdc3dfeSToby Isaac . beta - the right exponent > -1 639fbdc3dfeSToby Isaac + n - the polynomial degree 640fbdc3dfeSToby Isaac 6414165533cSJose E. Roman Output Parameter: 642fbdc3dfeSToby Isaac . norm - the weighted L2 norm 643fbdc3dfeSToby Isaac 644fbdc3dfeSToby Isaac Level: beginner 645fbdc3dfeSToby Isaac 646fbdc3dfeSToby Isaac .seealso: PetscDTJacobiEval() 647fbdc3dfeSToby Isaac @*/ 648fbdc3dfeSToby Isaac PetscErrorCode PetscDTJacobiNorm(PetscReal alpha, PetscReal beta, PetscInt n, PetscReal *norm) 649fbdc3dfeSToby Isaac { 650fbdc3dfeSToby Isaac PetscReal twoab1; 651fbdc3dfeSToby Isaac PetscReal gr; 652fbdc3dfeSToby Isaac 653fbdc3dfeSToby Isaac PetscFunctionBegin; 6542c71b3e2SJacob Faibussowitsch PetscCheckFalse(alpha <= -1.,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Exponent alpha %g <= -1. invalid", (double) alpha); 6552c71b3e2SJacob Faibussowitsch PetscCheckFalse(beta <= -1.,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Exponent beta %g <= -1. invalid", (double) beta); 6562c71b3e2SJacob Faibussowitsch PetscCheckFalse(n < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "n %D < 0 invalid", n); 657fbdc3dfeSToby Isaac twoab1 = PetscPowReal(2., alpha + beta + 1.); 658fbdc3dfeSToby Isaac #if defined(PETSC_HAVE_LGAMMA) 659fbdc3dfeSToby Isaac if (!n) { 660fbdc3dfeSToby Isaac gr = PetscExpReal(PetscLGamma(alpha+1.) + PetscLGamma(beta+1.) - PetscLGamma(alpha+beta+2.)); 661fbdc3dfeSToby Isaac } else { 662fbdc3dfeSToby Isaac gr = PetscExpReal(PetscLGamma(n+alpha+1.) + PetscLGamma(n+beta+1.) - (PetscLGamma(n+1.) + PetscLGamma(n+alpha+beta+1.))) / (n+n+alpha+beta+1.); 663fbdc3dfeSToby Isaac } 664fbdc3dfeSToby Isaac #else 665fbdc3dfeSToby Isaac { 666fbdc3dfeSToby Isaac PetscInt alphai = (PetscInt) alpha; 667fbdc3dfeSToby Isaac PetscInt betai = (PetscInt) beta; 668fbdc3dfeSToby Isaac PetscInt i; 669fbdc3dfeSToby Isaac 670fbdc3dfeSToby Isaac gr = n ? (1. / (n+n+alpha+beta+1.)) : 1.; 671fbdc3dfeSToby Isaac if ((PetscReal) alphai == alpha) { 672fbdc3dfeSToby Isaac if (!n) { 673fbdc3dfeSToby Isaac for (i = 0; i < alphai; i++) gr *= (i+1.) / (beta+i+1.); 674fbdc3dfeSToby Isaac gr /= (alpha+beta+1.); 675fbdc3dfeSToby Isaac } else { 676fbdc3dfeSToby Isaac for (i = 0; i < alphai; i++) gr *= (n+i+1.) / (n+beta+i+1.); 677fbdc3dfeSToby Isaac } 678fbdc3dfeSToby Isaac } else if ((PetscReal) betai == beta) { 679fbdc3dfeSToby Isaac if (!n) { 680fbdc3dfeSToby Isaac for (i = 0; i < betai; i++) gr *= (i+1.) / (alpha+i+2.); 681fbdc3dfeSToby Isaac gr /= (alpha+beta+1.); 682fbdc3dfeSToby Isaac } else { 683fbdc3dfeSToby Isaac for (i = 0; i < betai; i++) gr *= (n+i+1.) / (n+alpha+i+1.); 684fbdc3dfeSToby Isaac } 685fbdc3dfeSToby Isaac } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"lgamma() - math routine is unavailable."); 686fbdc3dfeSToby Isaac } 687fbdc3dfeSToby Isaac #endif 688fbdc3dfeSToby Isaac *norm = PetscSqrtReal(twoab1 * gr); 689fbdc3dfeSToby Isaac PetscFunctionReturn(0); 690fbdc3dfeSToby Isaac } 691fbdc3dfeSToby Isaac 69294e21283SToby Isaac static PetscErrorCode PetscDTJacobiEval_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscInt k, const PetscReal *points, PetscInt ndegree, const PetscInt *degrees, PetscReal *p) 69394e21283SToby Isaac { 69494e21283SToby Isaac PetscReal ak, bk; 69594e21283SToby Isaac PetscReal abk1; 69694e21283SToby Isaac PetscInt i,l,maxdegree; 69794e21283SToby Isaac 69894e21283SToby Isaac PetscFunctionBegin; 69994e21283SToby Isaac maxdegree = degrees[ndegree-1] - k; 70094e21283SToby Isaac ak = a + k; 70194e21283SToby Isaac bk = b + k; 70294e21283SToby Isaac abk1 = a + b + k + 1.; 70394e21283SToby Isaac if (maxdegree < 0) { 70494e21283SToby Isaac for (i = 0; i < npoints; i++) for (l = 0; l < ndegree; l++) p[i*ndegree+l] = 0.; 70594e21283SToby Isaac PetscFunctionReturn(0); 70694e21283SToby Isaac } 70794e21283SToby Isaac for (i=0; i<npoints; i++) { 70894e21283SToby Isaac PetscReal pm1,pm2,x; 70994e21283SToby Isaac PetscReal cnm1, cnm1x, cnm2; 71094e21283SToby Isaac PetscInt j,m; 71194e21283SToby Isaac 71294e21283SToby Isaac x = points[i]; 71394e21283SToby Isaac pm2 = 1.; 71494e21283SToby Isaac PetscDTJacobiRecurrence_Internal(1,ak,bk,cnm1,cnm1x,cnm2); 71594e21283SToby Isaac pm1 = (cnm1 + cnm1x*x); 71694e21283SToby Isaac l = 0; 71794e21283SToby Isaac while (l < ndegree && degrees[l] - k < 0) { 71894e21283SToby Isaac p[l++] = 0.; 71994e21283SToby Isaac } 72094e21283SToby Isaac while (l < ndegree && degrees[l] - k == 0) { 72194e21283SToby Isaac p[l] = pm2; 72294e21283SToby Isaac for (m = 0; m < k; m++) p[l] *= (abk1 + m) * 0.5; 72394e21283SToby Isaac l++; 72494e21283SToby Isaac } 72594e21283SToby Isaac while (l < ndegree && degrees[l] - k == 1) { 72694e21283SToby Isaac p[l] = pm1; 72794e21283SToby Isaac for (m = 0; m < k; m++) p[l] *= (abk1 + 1 + m) * 0.5; 72894e21283SToby Isaac l++; 72994e21283SToby Isaac } 73094e21283SToby Isaac for (j=2; j<=maxdegree; j++) { 73194e21283SToby Isaac PetscReal pp; 73294e21283SToby Isaac 73394e21283SToby Isaac PetscDTJacobiRecurrence_Internal(j,ak,bk,cnm1,cnm1x,cnm2); 73494e21283SToby Isaac pp = (cnm1 + cnm1x*x)*pm1 - cnm2*pm2; 73594e21283SToby Isaac pm2 = pm1; 73694e21283SToby Isaac pm1 = pp; 73794e21283SToby Isaac while (l < ndegree && degrees[l] - k == j) { 73894e21283SToby Isaac p[l] = pp; 73994e21283SToby Isaac for (m = 0; m < k; m++) p[l] *= (abk1 + j + m) * 0.5; 74094e21283SToby Isaac l++; 74194e21283SToby Isaac } 74294e21283SToby Isaac } 74394e21283SToby Isaac p += ndegree; 74494e21283SToby Isaac } 74594e21283SToby Isaac PetscFunctionReturn(0); 74694e21283SToby Isaac } 74794e21283SToby Isaac 74837045ce4SJed Brown /*@ 749fbdc3dfeSToby Isaac PetscDTJacobiEvalJet - Evaluate the jet (function and derivatives) of the Jacobi polynomials basis up to a given degree. The Jacobi polynomials with indices $\alpha$ and $\beta$ are orthogonal with respect to the weighted inner product $\langle f, g \rangle = \int_{-1}^1 (1+x)^{\alpha} (1-x)^{\beta) f(x) g(x) dx$. 750fbdc3dfeSToby Isaac 7514165533cSJose E. Roman Input Parameters: 752fbdc3dfeSToby Isaac + alpha - the left exponent of the weight 753fbdc3dfeSToby Isaac . beta - the right exponetn of the weight 754fbdc3dfeSToby Isaac . npoints - the number of points to evaluate the polynomials at 755fbdc3dfeSToby Isaac . points - [npoints] array of point coordinates 756fbdc3dfeSToby Isaac . degree - the maximm degree polynomial space to evaluate, (degree + 1) will be evaluated total. 757fbdc3dfeSToby Isaac - k - the maximum derivative to evaluate in the jet, (k + 1) will be evaluated total. 758fbdc3dfeSToby Isaac 759fbdc3dfeSToby Isaac Output Argments: 760fbdc3dfeSToby Isaac - p - an array containing the evaluations of the Jacobi polynomials's jets on the points. the size is (degree + 1) x 761fbdc3dfeSToby Isaac (k + 1) x npoints, which also describes the order of the dimensions of this three-dimensional array: the first 762fbdc3dfeSToby Isaac (slowest varying) dimension is polynomial degree; the second dimension is derivative order; the third (fastest 763fbdc3dfeSToby Isaac varying) dimension is the index of the evaluation point. 764fbdc3dfeSToby Isaac 765fbdc3dfeSToby Isaac Level: advanced 766fbdc3dfeSToby Isaac 767fbdc3dfeSToby Isaac .seealso: PetscDTJacobiEval(), PetscDTPKDEvalJet() 768fbdc3dfeSToby Isaac @*/ 769fbdc3dfeSToby Isaac PetscErrorCode PetscDTJacobiEvalJet(PetscReal alpha, PetscReal beta, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt k, PetscReal p[]) 770fbdc3dfeSToby Isaac { 771fbdc3dfeSToby Isaac PetscInt i, j, l; 772fbdc3dfeSToby Isaac PetscInt *degrees; 773fbdc3dfeSToby Isaac PetscReal *psingle; 774fbdc3dfeSToby Isaac PetscErrorCode ierr; 775fbdc3dfeSToby Isaac 776fbdc3dfeSToby Isaac PetscFunctionBegin; 777fbdc3dfeSToby Isaac if (degree == 0) { 778fbdc3dfeSToby Isaac PetscInt zero = 0; 779fbdc3dfeSToby Isaac 780fbdc3dfeSToby Isaac for (i = 0; i <= k; i++) { 781fbdc3dfeSToby Isaac ierr = PetscDTJacobiEval_Internal(npoints, alpha, beta, i, points, 1, &zero, &p[i*npoints]);CHKERRQ(ierr); 782fbdc3dfeSToby Isaac } 783fbdc3dfeSToby Isaac PetscFunctionReturn(0); 784fbdc3dfeSToby Isaac } 785fbdc3dfeSToby Isaac ierr = PetscMalloc1(degree + 1, °rees);CHKERRQ(ierr); 786fbdc3dfeSToby Isaac ierr = PetscMalloc1((degree + 1) * npoints, &psingle);CHKERRQ(ierr); 787fbdc3dfeSToby Isaac for (i = 0; i <= degree; i++) degrees[i] = i; 788fbdc3dfeSToby Isaac for (i = 0; i <= k; i++) { 789fbdc3dfeSToby Isaac ierr = PetscDTJacobiEval_Internal(npoints, alpha, beta, i, points, degree + 1, degrees, psingle);CHKERRQ(ierr); 790fbdc3dfeSToby Isaac for (j = 0; j <= degree; j++) { 791fbdc3dfeSToby Isaac for (l = 0; l < npoints; l++) { 792fbdc3dfeSToby Isaac p[(j * (k + 1) + i) * npoints + l] = psingle[l * (degree + 1) + j]; 793fbdc3dfeSToby Isaac } 794fbdc3dfeSToby Isaac } 795fbdc3dfeSToby Isaac } 796fbdc3dfeSToby Isaac ierr = PetscFree(psingle);CHKERRQ(ierr); 797fbdc3dfeSToby Isaac ierr = PetscFree(degrees);CHKERRQ(ierr); 798fbdc3dfeSToby Isaac PetscFunctionReturn(0); 799fbdc3dfeSToby Isaac } 800fbdc3dfeSToby Isaac 801fbdc3dfeSToby Isaac /*@ 80294e21283SToby Isaac PetscDTJacobiEval - evaluate Jacobi polynomials for the weight function $(1.+x)^{\alpha} (1.-x)^{\beta}$ 80394e21283SToby Isaac at points 80494e21283SToby Isaac 80594e21283SToby Isaac Not Collective 80694e21283SToby Isaac 8074165533cSJose E. Roman Input Parameters: 80894e21283SToby Isaac + npoints - number of spatial points to evaluate at 80994e21283SToby Isaac . alpha - the left exponent > -1 81094e21283SToby Isaac . beta - the right exponent > -1 81194e21283SToby Isaac . points - array of locations to evaluate at 81294e21283SToby Isaac . ndegree - number of basis degrees to evaluate 81394e21283SToby Isaac - degrees - sorted array of degrees to evaluate 81494e21283SToby Isaac 8154165533cSJose E. Roman Output Parameters: 81694e21283SToby Isaac + B - row-oriented basis evaluation matrix B[point*ndegree + degree] (dimension npoints*ndegrees, allocated by caller) (or NULL) 81794e21283SToby Isaac . D - row-oriented derivative evaluation matrix (or NULL) 81894e21283SToby Isaac - D2 - row-oriented second derivative evaluation matrix (or NULL) 81994e21283SToby Isaac 82094e21283SToby Isaac Level: intermediate 82194e21283SToby Isaac 82294e21283SToby Isaac .seealso: PetscDTGaussQuadrature() 82394e21283SToby Isaac @*/ 82494e21283SToby Isaac PetscErrorCode PetscDTJacobiEval(PetscInt npoints,PetscReal alpha, PetscReal beta, const PetscReal *points,PetscInt ndegree,const PetscInt *degrees,PetscReal *B,PetscReal *D,PetscReal *D2) 82594e21283SToby Isaac { 82694e21283SToby Isaac PetscErrorCode ierr; 82794e21283SToby Isaac 82894e21283SToby Isaac PetscFunctionBegin; 8292c71b3e2SJacob Faibussowitsch PetscCheckFalse(alpha <= -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"alpha must be > -1."); 8302c71b3e2SJacob Faibussowitsch PetscCheckFalse(beta <= -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"beta must be > -1."); 83194e21283SToby Isaac if (!npoints || !ndegree) PetscFunctionReturn(0); 83294e21283SToby Isaac if (B) {ierr = PetscDTJacobiEval_Internal(npoints, alpha, beta, 0, points, ndegree, degrees, B);CHKERRQ(ierr);} 83394e21283SToby Isaac if (D) {ierr = PetscDTJacobiEval_Internal(npoints, alpha, beta, 1, points, ndegree, degrees, D);CHKERRQ(ierr);} 83494e21283SToby Isaac if (D2) {ierr = PetscDTJacobiEval_Internal(npoints, alpha, beta, 2, points, ndegree, degrees, D2);CHKERRQ(ierr);} 83594e21283SToby Isaac PetscFunctionReturn(0); 83694e21283SToby Isaac } 83794e21283SToby Isaac 83894e21283SToby Isaac /*@ 83994e21283SToby Isaac PetscDTLegendreEval - evaluate Legendre polynomials at points 84037045ce4SJed Brown 84137045ce4SJed Brown Not Collective 84237045ce4SJed Brown 8434165533cSJose E. Roman Input Parameters: 84437045ce4SJed Brown + npoints - number of spatial points to evaluate at 84537045ce4SJed Brown . points - array of locations to evaluate at 84637045ce4SJed Brown . ndegree - number of basis degrees to evaluate 84737045ce4SJed Brown - degrees - sorted array of degrees to evaluate 84837045ce4SJed Brown 8494165533cSJose E. Roman Output Parameters: 8500298fd71SBarry Smith + B - row-oriented basis evaluation matrix B[point*ndegree + degree] (dimension npoints*ndegrees, allocated by caller) (or NULL) 8510298fd71SBarry Smith . D - row-oriented derivative evaluation matrix (or NULL) 8520298fd71SBarry Smith - D2 - row-oriented second derivative evaluation matrix (or NULL) 85337045ce4SJed Brown 85437045ce4SJed Brown Level: intermediate 85537045ce4SJed Brown 85637045ce4SJed Brown .seealso: PetscDTGaussQuadrature() 85737045ce4SJed Brown @*/ 85837045ce4SJed Brown PetscErrorCode PetscDTLegendreEval(PetscInt npoints,const PetscReal *points,PetscInt ndegree,const PetscInt *degrees,PetscReal *B,PetscReal *D,PetscReal *D2) 85937045ce4SJed Brown { 86094e21283SToby Isaac PetscErrorCode ierr; 86137045ce4SJed Brown 86237045ce4SJed Brown PetscFunctionBegin; 86394e21283SToby Isaac ierr = PetscDTJacobiEval(npoints, 0., 0., points, ndegree, degrees, B, D, D2);CHKERRQ(ierr); 86437045ce4SJed Brown PetscFunctionReturn(0); 86537045ce4SJed Brown } 86637045ce4SJed Brown 867fbdc3dfeSToby Isaac /*@ 868fbdc3dfeSToby Isaac PetscDTIndexToGradedOrder - convert an index into a tuple of monomial degrees in a graded order (that is, if the degree sum of tuple x is less than the degree sum of tuple y, then the index of x is smaller than the index of y) 869fbdc3dfeSToby Isaac 870fbdc3dfeSToby Isaac Input Parameters: 871fbdc3dfeSToby Isaac + len - the desired length of the degree tuple 872fbdc3dfeSToby Isaac - index - the index to convert: should be >= 0 873fbdc3dfeSToby Isaac 874fbdc3dfeSToby Isaac Output Parameter: 875fbdc3dfeSToby Isaac . degtup - will be filled with a tuple of degrees 876fbdc3dfeSToby Isaac 877fbdc3dfeSToby Isaac Level: beginner 878fbdc3dfeSToby Isaac 879fbdc3dfeSToby Isaac Note: for two tuples x and y with the same degree sum, partial degree sums over the final elements of the tuples 880fbdc3dfeSToby Isaac acts as a tiebreaker. For example, (2, 1, 1) and (1, 2, 1) have the same degree sum, but the degree sum over the 881fbdc3dfeSToby Isaac last two elements is smaller for the former, so (2, 1, 1) < (1, 2, 1). 882fbdc3dfeSToby Isaac 883fbdc3dfeSToby Isaac .seealso: PetscDTGradedOrderToIndex() 884fbdc3dfeSToby Isaac @*/ 885fbdc3dfeSToby Isaac PetscErrorCode PetscDTIndexToGradedOrder(PetscInt len, PetscInt index, PetscInt degtup[]) 886fbdc3dfeSToby Isaac { 887fbdc3dfeSToby Isaac PetscInt i, total; 888fbdc3dfeSToby Isaac PetscInt sum; 889fbdc3dfeSToby Isaac 890fbdc3dfeSToby Isaac PetscFunctionBeginHot; 8912c71b3e2SJacob Faibussowitsch PetscCheckFalse(len < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative"); 8922c71b3e2SJacob Faibussowitsch PetscCheckFalse(index < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index must be non-negative"); 893fbdc3dfeSToby Isaac total = 1; 894fbdc3dfeSToby Isaac sum = 0; 895fbdc3dfeSToby Isaac while (index >= total) { 896fbdc3dfeSToby Isaac index -= total; 897fbdc3dfeSToby Isaac total = (total * (len + sum)) / (sum + 1); 898fbdc3dfeSToby Isaac sum++; 899fbdc3dfeSToby Isaac } 900fbdc3dfeSToby Isaac for (i = 0; i < len; i++) { 901fbdc3dfeSToby Isaac PetscInt c; 902fbdc3dfeSToby Isaac 903fbdc3dfeSToby Isaac degtup[i] = sum; 904fbdc3dfeSToby Isaac for (c = 0, total = 1; c < sum; c++) { 905fbdc3dfeSToby Isaac /* going into the loop, total is the number of way to have a tuple of sum exactly c with length len - 1 - i */ 906fbdc3dfeSToby Isaac if (index < total) break; 907fbdc3dfeSToby Isaac index -= total; 908fbdc3dfeSToby Isaac total = (total * (len - 1 - i + c)) / (c + 1); 909fbdc3dfeSToby Isaac degtup[i]--; 910fbdc3dfeSToby Isaac } 911fbdc3dfeSToby Isaac sum -= degtup[i]; 912fbdc3dfeSToby Isaac } 913fbdc3dfeSToby Isaac PetscFunctionReturn(0); 914fbdc3dfeSToby Isaac } 915fbdc3dfeSToby Isaac 916fbdc3dfeSToby Isaac /*@ 917fbdc3dfeSToby Isaac PetscDTGradedOrderToIndex - convert a tuple into an index in a graded order, the inverse of PetscDTIndexToGradedOrder(). 918fbdc3dfeSToby Isaac 919fbdc3dfeSToby Isaac Input Parameters: 920fbdc3dfeSToby Isaac + len - the length of the degree tuple 921fbdc3dfeSToby Isaac - degtup - tuple with this length 922fbdc3dfeSToby Isaac 923fbdc3dfeSToby Isaac Output Parameter: 924fbdc3dfeSToby Isaac . index - index in graded order: >= 0 925fbdc3dfeSToby Isaac 926fbdc3dfeSToby Isaac Level: Beginner 927fbdc3dfeSToby Isaac 928fbdc3dfeSToby Isaac Note: for two tuples x and y with the same degree sum, partial degree sums over the final elements of the tuples 929fbdc3dfeSToby Isaac acts as a tiebreaker. For example, (2, 1, 1) and (1, 2, 1) have the same degree sum, but the degree sum over the 930fbdc3dfeSToby Isaac last two elements is smaller for the former, so (2, 1, 1) < (1, 2, 1). 931fbdc3dfeSToby Isaac 932fbdc3dfeSToby Isaac .seealso: PetscDTIndexToGradedOrder() 933fbdc3dfeSToby Isaac @*/ 934fbdc3dfeSToby Isaac PetscErrorCode PetscDTGradedOrderToIndex(PetscInt len, const PetscInt degtup[], PetscInt *index) 935fbdc3dfeSToby Isaac { 936fbdc3dfeSToby Isaac PetscInt i, idx, sum, total; 937fbdc3dfeSToby Isaac 938fbdc3dfeSToby Isaac PetscFunctionBeginHot; 9392c71b3e2SJacob Faibussowitsch PetscCheckFalse(len < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative"); 940fbdc3dfeSToby Isaac for (i = 0, sum = 0; i < len; i++) sum += degtup[i]; 941fbdc3dfeSToby Isaac idx = 0; 942fbdc3dfeSToby Isaac total = 1; 943fbdc3dfeSToby Isaac for (i = 0; i < sum; i++) { 944fbdc3dfeSToby Isaac idx += total; 945fbdc3dfeSToby Isaac total = (total * (len + i)) / (i + 1); 946fbdc3dfeSToby Isaac } 947fbdc3dfeSToby Isaac for (i = 0; i < len - 1; i++) { 948fbdc3dfeSToby Isaac PetscInt c; 949fbdc3dfeSToby Isaac 950fbdc3dfeSToby Isaac total = 1; 951fbdc3dfeSToby Isaac sum -= degtup[i]; 952fbdc3dfeSToby Isaac for (c = 0; c < sum; c++) { 953fbdc3dfeSToby Isaac idx += total; 954fbdc3dfeSToby Isaac total = (total * (len - 1 - i + c)) / (c + 1); 955fbdc3dfeSToby Isaac } 956fbdc3dfeSToby Isaac } 957fbdc3dfeSToby Isaac *index = idx; 958fbdc3dfeSToby Isaac PetscFunctionReturn(0); 959fbdc3dfeSToby Isaac } 960fbdc3dfeSToby Isaac 961e3aa2e09SToby Isaac static PetscBool PKDCite = PETSC_FALSE; 962e3aa2e09SToby Isaac const char PKDCitation[] = "@article{Kirby2010,\n" 963e3aa2e09SToby Isaac " title={Singularity-free evaluation of collapsed-coordinate orthogonal polynomials},\n" 964e3aa2e09SToby Isaac " author={Kirby, Robert C},\n" 965e3aa2e09SToby Isaac " journal={ACM Transactions on Mathematical Software (TOMS)},\n" 966e3aa2e09SToby Isaac " volume={37},\n" 967e3aa2e09SToby Isaac " number={1},\n" 968e3aa2e09SToby Isaac " pages={1--16},\n" 969e3aa2e09SToby Isaac " year={2010},\n" 970e3aa2e09SToby Isaac " publisher={ACM New York, NY, USA}\n}\n"; 971e3aa2e09SToby Isaac 972fbdc3dfeSToby Isaac /*@ 973d8f25ad8SToby Isaac PetscDTPKDEvalJet - Evaluate the jet (function and derivatives) of the Proriol-Koornwinder-Dubiner (PKD) basis for 974fbdc3dfeSToby Isaac the space of polynomials up to a given degree. The PKD basis is L2-orthonormal on the biunit simplex (which is used 975fbdc3dfeSToby Isaac as the reference element for finite elements in PETSc), which makes it a stable basis to use for evaluating 976fbdc3dfeSToby Isaac polynomials in that domain. 977fbdc3dfeSToby Isaac 9784165533cSJose E. Roman Input Parameters: 979fbdc3dfeSToby Isaac + dim - the number of variables in the multivariate polynomials 980fbdc3dfeSToby Isaac . npoints - the number of points to evaluate the polynomials at 981fbdc3dfeSToby Isaac . points - [npoints x dim] array of point coordinates 982fbdc3dfeSToby Isaac . degree - the degree (sum of degrees on the variables in a monomial) of the polynomial space to evaluate. There are ((dim + degree) choose dim) polynomials in this space. 983fbdc3dfeSToby Isaac - k - the maximum order partial derivative to evaluate in the jet. There are (dim + k choose dim) partial derivatives 984fbdc3dfeSToby Isaac in the jet. Choosing k = 0 means to evaluate just the function and no derivatives 985fbdc3dfeSToby Isaac 986fbdc3dfeSToby Isaac Output Argments: 987fbdc3dfeSToby Isaac - p - an array containing the evaluations of the PKD polynomials' jets on the points. The size is ((dim + degree) 988fbdc3dfeSToby Isaac choose dim) x ((dim + k) choose dim) x npoints, which also describes the order of the dimensions of this 989fbdc3dfeSToby Isaac three-dimensional array: the first (slowest varying) dimension is basis function index; the second dimension is jet 990fbdc3dfeSToby Isaac index; the third (fastest varying) dimension is the index of the evaluation point. 991fbdc3dfeSToby Isaac 992fbdc3dfeSToby Isaac Level: advanced 993fbdc3dfeSToby Isaac 994fbdc3dfeSToby Isaac Note: The ordering of the basis functions, and the ordering of the derivatives in the jet, both follow the graded 995fbdc3dfeSToby Isaac ordering of PetscDTIndexToGradedOrder() and PetscDTGradedOrderToIndex(). For example, in 3D, the polynomial with 996d8f25ad8SToby 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); 997fbdc3dfeSToby Isaac the partial derivative $\partial_x \partial_z$ has order tuple (1,0,1), appears at index 6 in the jet (it is the 7th partial derivative in the jet). 998fbdc3dfeSToby Isaac 999e3aa2e09SToby Isaac The implementation uses Kirby's singularity-free evaluation algorithm, https://doi.org/10.1145/1644001.1644006. 1000e3aa2e09SToby Isaac 1001fbdc3dfeSToby Isaac .seealso: PetscDTGradedOrderToIndex(), PetscDTIndexToGradedOrder(), PetscDTJacobiEvalJet() 1002fbdc3dfeSToby Isaac @*/ 1003fbdc3dfeSToby Isaac PetscErrorCode PetscDTPKDEvalJet(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt k, PetscReal p[]) 1004fbdc3dfeSToby Isaac { 1005fbdc3dfeSToby Isaac PetscInt degidx, kidx, d, pt; 1006fbdc3dfeSToby Isaac PetscInt Nk, Ndeg; 1007fbdc3dfeSToby Isaac PetscInt *ktup, *degtup; 1008fbdc3dfeSToby Isaac PetscReal *scales, initscale, scaleexp; 1009fbdc3dfeSToby Isaac PetscErrorCode ierr; 1010fbdc3dfeSToby Isaac 1011fbdc3dfeSToby Isaac PetscFunctionBegin; 1012e3aa2e09SToby Isaac ierr = PetscCitationsRegister(PKDCitation, &PKDCite);CHKERRQ(ierr); 1013fbdc3dfeSToby Isaac ierr = PetscDTBinomialInt(dim + k, k, &Nk);CHKERRQ(ierr); 1014fbdc3dfeSToby Isaac ierr = PetscDTBinomialInt(degree + dim, degree, &Ndeg);CHKERRQ(ierr); 1015fbdc3dfeSToby Isaac ierr = PetscMalloc2(dim, °tup, dim, &ktup);CHKERRQ(ierr); 1016fbdc3dfeSToby Isaac ierr = PetscMalloc1(Ndeg, &scales);CHKERRQ(ierr); 1017fbdc3dfeSToby Isaac initscale = 1.; 1018fbdc3dfeSToby Isaac if (dim > 1) { 1019fbdc3dfeSToby Isaac ierr = PetscDTBinomial(dim,2,&scaleexp);CHKERRQ(ierr); 10202f613bf5SBarry Smith initscale = PetscPowReal(2.,scaleexp*0.5); 1021fbdc3dfeSToby Isaac } 1022fbdc3dfeSToby Isaac for (degidx = 0; degidx < Ndeg; degidx++) { 1023fbdc3dfeSToby Isaac PetscInt e, i; 1024fbdc3dfeSToby Isaac PetscInt m1idx = -1, m2idx = -1; 1025fbdc3dfeSToby Isaac PetscInt n; 1026fbdc3dfeSToby Isaac PetscInt degsum; 1027fbdc3dfeSToby Isaac PetscReal alpha; 1028fbdc3dfeSToby Isaac PetscReal cnm1, cnm1x, cnm2; 1029fbdc3dfeSToby Isaac PetscReal norm; 1030fbdc3dfeSToby Isaac 1031fbdc3dfeSToby Isaac ierr = PetscDTIndexToGradedOrder(dim, degidx, degtup);CHKERRQ(ierr); 1032fbdc3dfeSToby Isaac for (d = dim - 1; d >= 0; d--) if (degtup[d]) break; 1033fbdc3dfeSToby Isaac if (d < 0) { /* constant is 1 everywhere, all derivatives are zero */ 1034fbdc3dfeSToby Isaac scales[degidx] = initscale; 1035fbdc3dfeSToby Isaac for (e = 0; e < dim; e++) { 1036fbdc3dfeSToby Isaac ierr = PetscDTJacobiNorm(e,0.,0,&norm);CHKERRQ(ierr); 1037fbdc3dfeSToby Isaac scales[degidx] /= norm; 1038fbdc3dfeSToby Isaac } 1039fbdc3dfeSToby Isaac for (i = 0; i < npoints; i++) p[degidx * Nk * npoints + i] = 1.; 1040fbdc3dfeSToby Isaac for (i = 0; i < (Nk - 1) * npoints; i++) p[(degidx * Nk + 1) * npoints + i] = 0.; 1041fbdc3dfeSToby Isaac continue; 1042fbdc3dfeSToby Isaac } 1043fbdc3dfeSToby Isaac n = degtup[d]; 1044fbdc3dfeSToby Isaac degtup[d]--; 1045fbdc3dfeSToby Isaac ierr = PetscDTGradedOrderToIndex(dim, degtup, &m1idx);CHKERRQ(ierr); 1046fbdc3dfeSToby Isaac if (degtup[d] > 0) { 1047fbdc3dfeSToby Isaac degtup[d]--; 1048fbdc3dfeSToby Isaac ierr = PetscDTGradedOrderToIndex(dim, degtup, &m2idx);CHKERRQ(ierr); 1049fbdc3dfeSToby Isaac degtup[d]++; 1050fbdc3dfeSToby Isaac } 1051fbdc3dfeSToby Isaac degtup[d]++; 1052fbdc3dfeSToby Isaac for (e = 0, degsum = 0; e < d; e++) degsum += degtup[e]; 1053fbdc3dfeSToby Isaac alpha = 2 * degsum + d; 1054fbdc3dfeSToby Isaac PetscDTJacobiRecurrence_Internal(n,alpha,0.,cnm1,cnm1x,cnm2); 1055fbdc3dfeSToby Isaac 1056fbdc3dfeSToby Isaac scales[degidx] = initscale; 1057fbdc3dfeSToby Isaac for (e = 0, degsum = 0; e < dim; e++) { 1058fbdc3dfeSToby Isaac PetscInt f; 1059fbdc3dfeSToby Isaac PetscReal ealpha; 1060fbdc3dfeSToby Isaac PetscReal enorm; 1061fbdc3dfeSToby Isaac 1062fbdc3dfeSToby Isaac ealpha = 2 * degsum + e; 1063fbdc3dfeSToby Isaac for (f = 0; f < degsum; f++) scales[degidx] *= 2.; 1064fbdc3dfeSToby Isaac ierr = PetscDTJacobiNorm(ealpha,0.,degtup[e],&enorm);CHKERRQ(ierr); 1065fbdc3dfeSToby Isaac scales[degidx] /= enorm; 1066fbdc3dfeSToby Isaac degsum += degtup[e]; 1067fbdc3dfeSToby Isaac } 1068fbdc3dfeSToby Isaac 1069fbdc3dfeSToby Isaac for (pt = 0; pt < npoints; pt++) { 1070fbdc3dfeSToby Isaac /* compute the multipliers */ 1071fbdc3dfeSToby Isaac PetscReal thetanm1, thetanm1x, thetanm2; 1072fbdc3dfeSToby Isaac 1073fbdc3dfeSToby Isaac thetanm1x = dim - (d+1) + 2.*points[pt * dim + d]; 1074fbdc3dfeSToby Isaac for (e = d+1; e < dim; e++) thetanm1x += points[pt * dim + e]; 1075fbdc3dfeSToby Isaac thetanm1x *= 0.5; 1076fbdc3dfeSToby Isaac thetanm1 = (2. - (dim-(d+1))); 1077fbdc3dfeSToby Isaac for (e = d+1; e < dim; e++) thetanm1 -= points[pt * dim + e]; 1078fbdc3dfeSToby Isaac thetanm1 *= 0.5; 1079fbdc3dfeSToby Isaac thetanm2 = thetanm1 * thetanm1; 1080fbdc3dfeSToby Isaac 1081fbdc3dfeSToby Isaac for (kidx = 0; kidx < Nk; kidx++) { 1082fbdc3dfeSToby Isaac PetscInt f; 1083fbdc3dfeSToby Isaac 1084fbdc3dfeSToby Isaac ierr = PetscDTIndexToGradedOrder(dim, kidx, ktup);CHKERRQ(ierr); 1085fbdc3dfeSToby Isaac /* first sum in the same derivative terms */ 1086fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] = (cnm1 * thetanm1 + cnm1x * thetanm1x) * p[(m1idx * Nk + kidx) * npoints + pt]; 1087fbdc3dfeSToby Isaac if (m2idx >= 0) { 1088fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] -= cnm2 * thetanm2 * p[(m2idx * Nk + kidx) * npoints + pt]; 1089fbdc3dfeSToby Isaac } 1090fbdc3dfeSToby Isaac 1091fbdc3dfeSToby Isaac for (f = d; f < dim; f++) { 1092fbdc3dfeSToby Isaac PetscInt km1idx, mplty = ktup[f]; 1093fbdc3dfeSToby Isaac 1094fbdc3dfeSToby Isaac if (!mplty) continue; 1095fbdc3dfeSToby Isaac ktup[f]--; 1096fbdc3dfeSToby Isaac ierr = PetscDTGradedOrderToIndex(dim, ktup, &km1idx);CHKERRQ(ierr); 1097fbdc3dfeSToby Isaac 1098fbdc3dfeSToby Isaac /* the derivative of cnm1x * thetanm1x wrt x variable f is 0.5 * cnm1x if f > d otherwise it is cnm1x */ 1099fbdc3dfeSToby Isaac /* the derivative of cnm1 * thetanm1 wrt x variable f is 0 if f == d, otherwise it is -0.5 * cnm1 */ 1100fbdc3dfeSToby Isaac /* the derivative of -cnm2 * thetanm2 wrt x variable f is 0 if f == d, otherwise it is cnm2 * thetanm1 */ 1101fbdc3dfeSToby Isaac if (f > d) { 1102fbdc3dfeSToby Isaac PetscInt f2; 1103fbdc3dfeSToby Isaac 1104fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] += mplty * 0.5 * (cnm1x - cnm1) * p[(m1idx * Nk + km1idx) * npoints + pt]; 1105fbdc3dfeSToby Isaac if (m2idx >= 0) { 1106fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] += mplty * cnm2 * thetanm1 * p[(m2idx * Nk + km1idx) * npoints + pt]; 1107fbdc3dfeSToby Isaac /* second derivatives of -cnm2 * thetanm2 wrt x variable f,f2 is like - 0.5 * cnm2 */ 1108fbdc3dfeSToby Isaac for (f2 = f; f2 < dim; f2++) { 1109fbdc3dfeSToby Isaac PetscInt km2idx, mplty2 = ktup[f2]; 1110fbdc3dfeSToby Isaac PetscInt factor; 1111fbdc3dfeSToby Isaac 1112fbdc3dfeSToby Isaac if (!mplty2) continue; 1113fbdc3dfeSToby Isaac ktup[f2]--; 1114fbdc3dfeSToby Isaac ierr = PetscDTGradedOrderToIndex(dim, ktup, &km2idx);CHKERRQ(ierr); 1115fbdc3dfeSToby Isaac 1116fbdc3dfeSToby Isaac factor = mplty * mplty2; 1117fbdc3dfeSToby Isaac if (f == f2) factor /= 2; 1118fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] -= 0.5 * factor * cnm2 * p[(m2idx * Nk + km2idx) * npoints + pt]; 1119fbdc3dfeSToby Isaac ktup[f2]++; 1120fbdc3dfeSToby Isaac } 11213034baaeSToby Isaac } 1122fbdc3dfeSToby Isaac } else { 1123fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] += mplty * cnm1x * p[(m1idx * Nk + km1idx) * npoints + pt]; 1124fbdc3dfeSToby Isaac } 1125fbdc3dfeSToby Isaac ktup[f]++; 1126fbdc3dfeSToby Isaac } 1127fbdc3dfeSToby Isaac } 1128fbdc3dfeSToby Isaac } 1129fbdc3dfeSToby Isaac } 1130fbdc3dfeSToby Isaac for (degidx = 0; degidx < Ndeg; degidx++) { 1131fbdc3dfeSToby Isaac PetscReal scale = scales[degidx]; 1132fbdc3dfeSToby Isaac PetscInt i; 1133fbdc3dfeSToby Isaac 1134fbdc3dfeSToby Isaac for (i = 0; i < Nk * npoints; i++) p[degidx*Nk*npoints + i] *= scale; 1135fbdc3dfeSToby Isaac } 1136fbdc3dfeSToby Isaac ierr = PetscFree(scales);CHKERRQ(ierr); 1137fbdc3dfeSToby Isaac ierr = PetscFree2(degtup, ktup);CHKERRQ(ierr); 1138fbdc3dfeSToby Isaac PetscFunctionReturn(0); 1139fbdc3dfeSToby Isaac } 1140fbdc3dfeSToby Isaac 1141d8f25ad8SToby Isaac /*@ 1142d8f25ad8SToby Isaac PetscDTPTrimmedSize - The size of the trimmed polynomial space of k-forms with a given degree and form degree, 1143d8f25ad8SToby Isaac which can be evaluated in PetscDTPTrimmedEvalJet(). 1144d8f25ad8SToby Isaac 1145d8f25ad8SToby Isaac Input Parameters: 1146d8f25ad8SToby Isaac + dim - the number of variables in the multivariate polynomials 1147d8f25ad8SToby Isaac . degree - the degree (sum of degrees on the variables in a monomial) of the trimmed polynomial space. 1148d8f25ad8SToby Isaac - formDegree - the degree of the form 1149d8f25ad8SToby Isaac 1150d8f25ad8SToby Isaac Output Argments: 1151d8f25ad8SToby Isaac - size - The number ((dim + degree) choose (dim + formDegree)) x ((degree + formDegree - 1) choose (formDegree)) 1152d8f25ad8SToby Isaac 1153d8f25ad8SToby Isaac Level: advanced 1154d8f25ad8SToby Isaac 1155d8f25ad8SToby Isaac .seealso: PetscDTPTrimmedEvalJet() 1156d8f25ad8SToby Isaac @*/ 1157d8f25ad8SToby Isaac PetscErrorCode PetscDTPTrimmedSize(PetscInt dim, PetscInt degree, PetscInt formDegree, PetscInt *size) 1158d8f25ad8SToby Isaac { 1159d8f25ad8SToby Isaac PetscInt Nrk, Nbpt; // number of trimmed polynomials 1160d8f25ad8SToby Isaac PetscErrorCode ierr; 1161d8f25ad8SToby Isaac 1162d8f25ad8SToby Isaac PetscFunctionBegin; 1163d8f25ad8SToby Isaac formDegree = PetscAbsInt(formDegree); 1164d8f25ad8SToby Isaac ierr = PetscDTBinomialInt(degree + dim, degree + formDegree, &Nbpt);CHKERRQ(ierr); 1165d8f25ad8SToby Isaac ierr = PetscDTBinomialInt(degree + formDegree - 1, formDegree, &Nrk);CHKERRQ(ierr); 1166d8f25ad8SToby Isaac Nbpt *= Nrk; 1167d8f25ad8SToby Isaac *size = Nbpt; 1168d8f25ad8SToby Isaac PetscFunctionReturn(0); 1169d8f25ad8SToby Isaac } 1170d8f25ad8SToby Isaac 1171d8f25ad8SToby Isaac /* there was a reference implementation based on section 4.4 of Arnold, Falk & Winther (acta numerica, 2006), but it 1172d8f25ad8SToby Isaac * was inferior to this implementation */ 1173d8f25ad8SToby Isaac static PetscErrorCode PetscDTPTrimmedEvalJet_Internal(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt formDegree, PetscInt jetDegree, PetscReal p[]) 1174d8f25ad8SToby Isaac { 1175d8f25ad8SToby Isaac PetscInt formDegreeOrig = formDegree; 1176d8f25ad8SToby Isaac PetscBool formNegative = (formDegreeOrig < 0) ? PETSC_TRUE : PETSC_FALSE; 1177d8f25ad8SToby Isaac PetscErrorCode ierr; 1178d8f25ad8SToby Isaac 1179d8f25ad8SToby Isaac PetscFunctionBegin; 1180d8f25ad8SToby Isaac formDegree = PetscAbsInt(formDegreeOrig); 1181d8f25ad8SToby Isaac if (formDegree == 0) { 1182d8f25ad8SToby Isaac ierr = PetscDTPKDEvalJet(dim, npoints, points, degree, jetDegree, p);CHKERRQ(ierr); 1183d8f25ad8SToby Isaac PetscFunctionReturn(0); 1184d8f25ad8SToby Isaac } 1185d8f25ad8SToby Isaac if (formDegree == dim) { 1186d8f25ad8SToby Isaac ierr = PetscDTPKDEvalJet(dim, npoints, points, degree - 1, jetDegree, p);CHKERRQ(ierr); 1187d8f25ad8SToby Isaac PetscFunctionReturn(0); 1188d8f25ad8SToby Isaac } 1189d8f25ad8SToby Isaac PetscInt Nbpt; 1190d8f25ad8SToby Isaac ierr = PetscDTPTrimmedSize(dim, degree, formDegree, &Nbpt);CHKERRQ(ierr); 1191d8f25ad8SToby Isaac PetscInt Nf; 1192d8f25ad8SToby Isaac ierr = PetscDTBinomialInt(dim, formDegree, &Nf);CHKERRQ(ierr); 1193d8f25ad8SToby Isaac PetscInt Nk; 1194d8f25ad8SToby Isaac ierr = PetscDTBinomialInt(dim + jetDegree, dim, &Nk);CHKERRQ(ierr); 1195d8f25ad8SToby Isaac ierr = PetscArrayzero(p, Nbpt * Nf * Nk * npoints);CHKERRQ(ierr); 1196d8f25ad8SToby Isaac 1197d8f25ad8SToby Isaac PetscInt Nbpm1; // number of scalar polynomials up to degree - 1; 1198d8f25ad8SToby Isaac ierr = PetscDTBinomialInt(dim + degree - 1, dim, &Nbpm1);CHKERRQ(ierr); 1199d8f25ad8SToby Isaac PetscReal *p_scalar; 1200d8f25ad8SToby Isaac ierr = PetscMalloc1(Nbpm1 * Nk * npoints, &p_scalar);CHKERRQ(ierr); 1201d8f25ad8SToby Isaac ierr = PetscDTPKDEvalJet(dim, npoints, points, degree - 1, jetDegree, p_scalar);CHKERRQ(ierr); 1202d8f25ad8SToby Isaac PetscInt total = 0; 1203d8f25ad8SToby Isaac // First add the full polynomials up to degree - 1 into the basis: take the scalar 1204d8f25ad8SToby Isaac // and copy one for each form component 1205d8f25ad8SToby Isaac for (PetscInt i = 0; i < Nbpm1; i++) { 1206d8f25ad8SToby Isaac const PetscReal *src = &p_scalar[i * Nk * npoints]; 1207d8f25ad8SToby Isaac for (PetscInt f = 0; f < Nf; f++) { 1208d8f25ad8SToby Isaac PetscReal *dest = &p[(total++ * Nf + f) * Nk * npoints]; 1209d8f25ad8SToby Isaac ierr = PetscArraycpy(dest, src, Nk * npoints);CHKERRQ(ierr); 1210d8f25ad8SToby Isaac } 1211d8f25ad8SToby Isaac } 1212d8f25ad8SToby Isaac PetscInt *form_atoms; 1213d8f25ad8SToby Isaac ierr = PetscMalloc1(formDegree + 1, &form_atoms);CHKERRQ(ierr); 1214d8f25ad8SToby Isaac // construct the interior product pattern 1215d8f25ad8SToby Isaac PetscInt (*pattern)[3]; 1216d8f25ad8SToby Isaac PetscInt Nf1; // number of formDegree + 1 forms 1217d8f25ad8SToby Isaac ierr = PetscDTBinomialInt(dim, formDegree + 1, &Nf1);CHKERRQ(ierr); 1218d8f25ad8SToby Isaac PetscInt nnz = Nf1 * (formDegree+1); 1219d8f25ad8SToby Isaac ierr = PetscMalloc1(Nf1 * (formDegree+1), &pattern);CHKERRQ(ierr); 1220d8f25ad8SToby Isaac ierr = PetscDTAltVInteriorPattern(dim, formDegree+1, pattern);CHKERRQ(ierr); 1221d8f25ad8SToby Isaac PetscReal centroid = (1. - dim) / (dim + 1.); 1222d8f25ad8SToby Isaac PetscInt *deriv; 1223d8f25ad8SToby Isaac ierr = PetscMalloc1(dim, &deriv);CHKERRQ(ierr); 1224d8f25ad8SToby Isaac for (PetscInt d = dim; d >= formDegree + 1; d--) { 1225d8f25ad8SToby Isaac PetscInt Nfd1; // number of formDegree + 1 forms in dimension d that include dx_0 1226d8f25ad8SToby Isaac // (equal to the number of formDegree forms in dimension d-1) 1227d8f25ad8SToby Isaac ierr = PetscDTBinomialInt(d - 1, formDegree, &Nfd1);CHKERRQ(ierr); 1228d8f25ad8SToby Isaac // The number of homogeneous (degree-1) scalar polynomials in d variables 1229d8f25ad8SToby Isaac PetscInt Nh; 1230d8f25ad8SToby Isaac ierr = PetscDTBinomialInt(d - 1 + degree - 1, d - 1, &Nh);CHKERRQ(ierr); 1231d8f25ad8SToby Isaac const PetscReal *h_scalar = &p_scalar[(Nbpm1 - Nh) * Nk * npoints]; 1232d8f25ad8SToby Isaac for (PetscInt b = 0; b < Nh; b++) { 1233d8f25ad8SToby Isaac const PetscReal *h_s = &h_scalar[b * Nk * npoints]; 1234d8f25ad8SToby Isaac for (PetscInt f = 0; f < Nfd1; f++) { 1235d8f25ad8SToby Isaac // construct all formDegree+1 forms that start with dx_(dim - d) /\ ... 1236d8f25ad8SToby Isaac form_atoms[0] = dim - d; 1237d8f25ad8SToby Isaac ierr = PetscDTEnumSubset(d-1, formDegree, f, &form_atoms[1]);CHKERRQ(ierr); 1238d8f25ad8SToby Isaac for (PetscInt i = 0; i < formDegree; i++) { 1239d8f25ad8SToby Isaac form_atoms[1+i] += form_atoms[0] + 1; 1240d8f25ad8SToby Isaac } 1241d8f25ad8SToby Isaac PetscInt f_ind; // index of the resulting form 1242d8f25ad8SToby Isaac ierr = PetscDTSubsetIndex(dim, formDegree + 1, form_atoms, &f_ind);CHKERRQ(ierr); 1243d8f25ad8SToby Isaac PetscReal *p_f = &p[total++ * Nf * Nk * npoints]; 1244d8f25ad8SToby Isaac for (PetscInt nz = 0; nz < nnz; nz++) { 1245d8f25ad8SToby Isaac PetscInt i = pattern[nz][0]; // formDegree component 1246d8f25ad8SToby Isaac PetscInt j = pattern[nz][1]; // (formDegree + 1) component 1247d8f25ad8SToby Isaac PetscInt v = pattern[nz][2]; // coordinate component 1248d8f25ad8SToby Isaac PetscReal scale = v < 0 ? -1. : 1.; 1249d8f25ad8SToby Isaac 1250d8f25ad8SToby Isaac i = formNegative ? (Nf - 1 - i) : i; 1251d8f25ad8SToby Isaac scale = (formNegative && (i & 1)) ? -scale : scale; 1252d8f25ad8SToby Isaac v = v < 0 ? -(v + 1) : v; 1253d8f25ad8SToby Isaac if (j != f_ind) { 1254d8f25ad8SToby Isaac continue; 1255d8f25ad8SToby Isaac } 1256d8f25ad8SToby Isaac PetscReal *p_i = &p_f[i * Nk * npoints]; 1257d8f25ad8SToby Isaac for (PetscInt jet = 0; jet < Nk; jet++) { 1258d8f25ad8SToby Isaac const PetscReal *h_jet = &h_s[jet * npoints]; 1259d8f25ad8SToby Isaac PetscReal *p_jet = &p_i[jet * npoints]; 1260d8f25ad8SToby Isaac 1261d8f25ad8SToby Isaac for (PetscInt pt = 0; pt < npoints; pt++) { 1262d8f25ad8SToby Isaac p_jet[pt] += scale * h_jet[pt] * (points[pt * dim + v] - centroid); 1263d8f25ad8SToby Isaac } 1264d8f25ad8SToby Isaac ierr = PetscDTIndexToGradedOrder(dim, jet, deriv);CHKERRQ(ierr); 1265d8f25ad8SToby Isaac deriv[v]++; 1266d8f25ad8SToby Isaac PetscReal mult = deriv[v]; 1267d8f25ad8SToby Isaac PetscInt l; 1268d8f25ad8SToby Isaac ierr = PetscDTGradedOrderToIndex(dim, deriv, &l);CHKERRQ(ierr); 1269d8f25ad8SToby Isaac if (l >= Nk) { 1270d8f25ad8SToby Isaac continue; 1271d8f25ad8SToby Isaac } 1272d8f25ad8SToby Isaac p_jet = &p_i[l * npoints]; 1273d8f25ad8SToby Isaac for (PetscInt pt = 0; pt < npoints; pt++) { 1274d8f25ad8SToby Isaac p_jet[pt] += scale * mult * h_jet[pt]; 1275d8f25ad8SToby Isaac } 1276d8f25ad8SToby Isaac deriv[v]--; 1277d8f25ad8SToby Isaac } 1278d8f25ad8SToby Isaac } 1279d8f25ad8SToby Isaac } 1280d8f25ad8SToby Isaac } 1281d8f25ad8SToby Isaac } 12822c71b3e2SJacob Faibussowitsch PetscCheckFalse(total != Nbpt,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Incorrectly counted P trimmed polynomials"); 1283d8f25ad8SToby Isaac ierr = PetscFree(deriv);CHKERRQ(ierr); 1284d8f25ad8SToby Isaac ierr = PetscFree(pattern);CHKERRQ(ierr); 1285d8f25ad8SToby Isaac ierr = PetscFree(form_atoms);CHKERRQ(ierr); 1286d8f25ad8SToby Isaac ierr = PetscFree(p_scalar);CHKERRQ(ierr); 1287d8f25ad8SToby Isaac PetscFunctionReturn(0); 1288d8f25ad8SToby Isaac } 1289d8f25ad8SToby Isaac 1290d8f25ad8SToby Isaac /*@ 1291d8f25ad8SToby Isaac PetscDTPTrimmedEvalJet - Evaluate the jet (function and derivatives) of a basis of the trimmed polynomial k-forms up to 1292d8f25ad8SToby Isaac a given degree. 1293d8f25ad8SToby Isaac 1294d8f25ad8SToby Isaac Input Parameters: 1295d8f25ad8SToby Isaac + dim - the number of variables in the multivariate polynomials 1296d8f25ad8SToby Isaac . npoints - the number of points to evaluate the polynomials at 1297d8f25ad8SToby Isaac . points - [npoints x dim] array of point coordinates 1298d8f25ad8SToby Isaac . degree - the degree (sum of degrees on the variables in a monomial) of the trimmed polynomial space to evaluate. 1299d8f25ad8SToby Isaac There are ((dim + degree) choose (dim + formDegree)) x ((degree + formDegree - 1) choose (formDegree)) polynomials in this space. 1300d8f25ad8SToby Isaac (You can use PetscDTPTrimmedSize() to compute this size.) 1301d8f25ad8SToby Isaac . formDegree - the degree of the form 1302d8f25ad8SToby Isaac - jetDegree - the maximum order partial derivative to evaluate in the jet. There are ((dim + jetDegree) choose dim) partial derivatives 1303d8f25ad8SToby Isaac in the jet. Choosing jetDegree = 0 means to evaluate just the function and no derivatives 1304d8f25ad8SToby Isaac 1305d8f25ad8SToby Isaac Output Argments: 1306d8f25ad8SToby Isaac - p - an array containing the evaluations of the PKD polynomials' jets on the points. The size is 1307d8f25ad8SToby Isaac PetscDTPTrimmedSize() x ((dim + formDegree) choose dim) x ((dim + k) choose dim) x npoints, 1308d8f25ad8SToby Isaac which also describes the order of the dimensions of this 1309d8f25ad8SToby Isaac four-dimensional array: 1310d8f25ad8SToby Isaac the first (slowest varying) dimension is basis function index; 1311d8f25ad8SToby Isaac the second dimension is component of the form; 1312d8f25ad8SToby Isaac the third dimension is jet index; 1313d8f25ad8SToby Isaac the fourth (fastest varying) dimension is the index of the evaluation point. 1314d8f25ad8SToby Isaac 1315d8f25ad8SToby Isaac Level: advanced 1316d8f25ad8SToby Isaac 1317d8f25ad8SToby Isaac Note: The ordering of the basis functions is not graded, so the basis functions are not nested by degree like PetscDTPKDEvalJet(). 1318d8f25ad8SToby Isaac The basis functions are not an L2-orthonormal basis on any particular domain. 1319d8f25ad8SToby Isaac 1320d8f25ad8SToby Isaac The implementation is based on the description of the trimmed polynomials up to degree r as 1321d8f25ad8SToby Isaac the direct sum of polynomials up to degree (r-1) and the Koszul differential applied to 1322d8f25ad8SToby Isaac homogeneous polynomials of degree (r-1). 1323d8f25ad8SToby Isaac 1324d8f25ad8SToby Isaac .seealso: PetscDTPKDEvalJet(), PetscDTPTrimmedSize() 1325d8f25ad8SToby Isaac @*/ 1326d8f25ad8SToby Isaac PetscErrorCode PetscDTPTrimmedEvalJet(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt formDegree, PetscInt jetDegree, PetscReal p[]) 1327d8f25ad8SToby Isaac { 1328d8f25ad8SToby Isaac PetscErrorCode ierr; 1329d8f25ad8SToby Isaac 1330d8f25ad8SToby Isaac PetscFunctionBegin; 1331d8f25ad8SToby Isaac ierr = PetscDTPTrimmedEvalJet_Internal(dim, npoints, points, degree, formDegree, jetDegree, p);CHKERRQ(ierr); 1332d8f25ad8SToby Isaac PetscFunctionReturn(0); 1333d8f25ad8SToby Isaac } 1334d8f25ad8SToby Isaac 1335e6a796c3SToby Isaac /* solve the symmetric tridiagonal eigenvalue system, writing the eigenvalues into eigs and the eigenvectors into V 1336e6a796c3SToby Isaac * with lds n; diag and subdiag are overwritten */ 1337e6a796c3SToby Isaac static PetscErrorCode PetscDTSymmetricTridiagonalEigensolve(PetscInt n, PetscReal diag[], PetscReal subdiag[], 1338e6a796c3SToby Isaac PetscReal eigs[], PetscScalar V[]) 1339e6a796c3SToby Isaac { 1340e6a796c3SToby Isaac char jobz = 'V'; /* eigenvalues and eigenvectors */ 1341e6a796c3SToby Isaac char range = 'A'; /* all eigenvalues will be found */ 1342e6a796c3SToby Isaac PetscReal VL = 0.; /* ignored because range is 'A' */ 1343e6a796c3SToby Isaac PetscReal VU = 0.; /* ignored because range is 'A' */ 1344e6a796c3SToby Isaac PetscBLASInt IL = 0; /* ignored because range is 'A' */ 1345e6a796c3SToby Isaac PetscBLASInt IU = 0; /* ignored because range is 'A' */ 1346e6a796c3SToby Isaac PetscReal abstol = 0.; /* unused */ 1347e6a796c3SToby Isaac PetscBLASInt bn, bm, ldz; /* bm will equal bn on exit */ 1348e6a796c3SToby Isaac PetscBLASInt *isuppz; 1349e6a796c3SToby Isaac PetscBLASInt lwork, liwork; 1350e6a796c3SToby Isaac PetscReal workquery; 1351e6a796c3SToby Isaac PetscBLASInt iworkquery; 1352e6a796c3SToby Isaac PetscBLASInt *iwork; 1353e6a796c3SToby Isaac PetscBLASInt info; 1354e6a796c3SToby Isaac PetscReal *work = NULL; 1355e6a796c3SToby Isaac PetscErrorCode ierr; 1356e6a796c3SToby Isaac 1357e6a796c3SToby Isaac PetscFunctionBegin; 1358e6a796c3SToby Isaac #if !defined(PETSCDTGAUSSIANQUADRATURE_EIG) 1359e6a796c3SToby Isaac SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "A LAPACK symmetric tridiagonal eigensolver could not be found"); 1360e6a796c3SToby Isaac #endif 1361e6a796c3SToby Isaac ierr = PetscBLASIntCast(n, &bn);CHKERRQ(ierr); 1362e6a796c3SToby Isaac ierr = PetscBLASIntCast(n, &ldz);CHKERRQ(ierr); 1363e6a796c3SToby Isaac #if !defined(PETSC_MISSING_LAPACK_STEGR) 1364e6a796c3SToby Isaac ierr = PetscMalloc1(2 * n, &isuppz);CHKERRQ(ierr); 1365e6a796c3SToby Isaac lwork = -1; 1366e6a796c3SToby Isaac liwork = -1; 1367e6a796c3SToby Isaac PetscStackCallBLAS("LAPACKstegr",LAPACKstegr_(&jobz,&range,&bn,diag,subdiag,&VL,&VU,&IL,&IU,&abstol,&bm,eigs,V,&ldz,isuppz,&workquery,&lwork,&iworkquery,&liwork,&info)); 13682c71b3e2SJacob Faibussowitsch PetscCheckFalse(info,PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEGR error"); 1369e6a796c3SToby Isaac lwork = (PetscBLASInt) workquery; 1370e6a796c3SToby Isaac liwork = (PetscBLASInt) iworkquery; 1371e6a796c3SToby Isaac ierr = PetscMalloc2(lwork, &work, liwork, &iwork);CHKERRQ(ierr); 1372e6a796c3SToby Isaac ierr = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr); 1373e6a796c3SToby Isaac PetscStackCallBLAS("LAPACKstegr",LAPACKstegr_(&jobz,&range,&bn,diag,subdiag,&VL,&VU,&IL,&IU,&abstol,&bm,eigs,V,&ldz,isuppz,work,&lwork,iwork,&liwork,&info)); 1374e6a796c3SToby Isaac ierr = PetscFPTrapPop();CHKERRQ(ierr); 13752c71b3e2SJacob Faibussowitsch PetscCheckFalse(info,PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEGR error"); 1376e6a796c3SToby Isaac ierr = PetscFree2(work, iwork);CHKERRQ(ierr); 1377e6a796c3SToby Isaac ierr = PetscFree(isuppz);CHKERRQ(ierr); 1378e6a796c3SToby Isaac #elif !defined(PETSC_MISSING_LAPACK_STEQR) 1379e6a796c3SToby Isaac jobz = 'I'; /* Compute eigenvalues and eigenvectors of the 1380e6a796c3SToby Isaac tridiagonal matrix. Z is initialized to the identity 1381e6a796c3SToby Isaac matrix. */ 1382e6a796c3SToby Isaac ierr = PetscMalloc1(PetscMax(1,2*n-2),&work);CHKERRQ(ierr); 1383e6a796c3SToby Isaac PetscStackCallBLAS("LAPACKsteqr",LAPACKsteqr_("I",&bn,diag,subdiag,V,&ldz,work,&info)); 1384e6a796c3SToby Isaac ierr = PetscFPTrapPop();CHKERRQ(ierr); 13852c71b3e2SJacob Faibussowitsch PetscCheckFalse(info,PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEQR error"); 1386e6a796c3SToby Isaac ierr = PetscFree(work);CHKERRQ(ierr); 1387e6a796c3SToby Isaac ierr = PetscArraycpy(eigs,diag,n);CHKERRQ(ierr); 1388e6a796c3SToby Isaac #endif 1389e6a796c3SToby Isaac PetscFunctionReturn(0); 1390e6a796c3SToby Isaac } 1391e6a796c3SToby Isaac 1392e6a796c3SToby Isaac /* Formula for the weights at the endpoints (-1 and 1) of Gauss-Lobatto-Jacobi 1393e6a796c3SToby Isaac * quadrature rules on the interval [-1, 1] */ 1394e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussLobattoJacobiEndweights_Internal(PetscInt n, PetscReal alpha, PetscReal beta, PetscReal *leftw, PetscReal *rightw) 1395e6a796c3SToby Isaac { 1396e6a796c3SToby Isaac PetscReal twoab1; 1397e6a796c3SToby Isaac PetscInt m = n - 2; 1398e6a796c3SToby Isaac PetscReal a = alpha + 1.; 1399e6a796c3SToby Isaac PetscReal b = beta + 1.; 1400e6a796c3SToby Isaac PetscReal gra, grb; 1401e6a796c3SToby Isaac 1402e6a796c3SToby Isaac PetscFunctionBegin; 1403e6a796c3SToby Isaac twoab1 = PetscPowReal(2., a + b - 1.); 1404e6a796c3SToby Isaac #if defined(PETSC_HAVE_LGAMMA) 1405e6a796c3SToby Isaac grb = PetscExpReal(2. * PetscLGamma(b+1.) + PetscLGamma(m+1.) + PetscLGamma(m+a+1.) - 1406e6a796c3SToby Isaac (PetscLGamma(m+b+1) + PetscLGamma(m+a+b+1.))); 1407e6a796c3SToby Isaac gra = PetscExpReal(2. * PetscLGamma(a+1.) + PetscLGamma(m+1.) + PetscLGamma(m+b+1.) - 1408e6a796c3SToby Isaac (PetscLGamma(m+a+1) + PetscLGamma(m+a+b+1.))); 1409e6a796c3SToby Isaac #else 1410e6a796c3SToby Isaac { 1411e6a796c3SToby Isaac PetscInt alphai = (PetscInt) alpha; 1412e6a796c3SToby Isaac PetscInt betai = (PetscInt) beta; 141394e21283SToby Isaac PetscErrorCode ierr; 1414e6a796c3SToby Isaac 1415e6a796c3SToby Isaac if ((PetscReal) alphai == alpha && (PetscReal) betai == beta) { 1416e6a796c3SToby Isaac PetscReal binom1, binom2; 1417e6a796c3SToby Isaac 1418e6a796c3SToby Isaac ierr = PetscDTBinomial(m+b, b, &binom1);CHKERRQ(ierr); 1419e6a796c3SToby Isaac ierr = PetscDTBinomial(m+a+b, b, &binom2);CHKERRQ(ierr); 1420e6a796c3SToby Isaac grb = 1./ (binom1 * binom2); 1421e6a796c3SToby Isaac ierr = PetscDTBinomial(m+a, a, &binom1);CHKERRQ(ierr); 1422e6a796c3SToby Isaac ierr = PetscDTBinomial(m+a+b, a, &binom2);CHKERRQ(ierr); 1423e6a796c3SToby Isaac gra = 1./ (binom1 * binom2); 1424e6a796c3SToby Isaac } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"lgamma() - math routine is unavailable."); 1425e6a796c3SToby Isaac } 1426e6a796c3SToby Isaac #endif 1427e6a796c3SToby Isaac *leftw = twoab1 * grb / b; 1428e6a796c3SToby Isaac *rightw = twoab1 * gra / a; 1429e6a796c3SToby Isaac PetscFunctionReturn(0); 1430e6a796c3SToby Isaac } 1431e6a796c3SToby Isaac 1432e6a796c3SToby Isaac /* Evaluates the nth jacobi polynomial with weight parameters a,b at a point x. 1433e6a796c3SToby Isaac Recurrence relations implemented from the pseudocode given in Karniadakis and Sherwin, Appendix B */ 14349fbee547SJacob Faibussowitsch static inline PetscErrorCode PetscDTComputeJacobi(PetscReal a, PetscReal b, PetscInt n, PetscReal x, PetscReal *P) 1435e6a796c3SToby Isaac { 143694e21283SToby Isaac PetscReal pn1, pn2; 143794e21283SToby Isaac PetscReal cnm1, cnm1x, cnm2; 1438e6a796c3SToby Isaac PetscInt k; 1439e6a796c3SToby Isaac 1440e6a796c3SToby Isaac PetscFunctionBegin; 1441e6a796c3SToby Isaac if (!n) {*P = 1.0; PetscFunctionReturn(0);} 144294e21283SToby Isaac PetscDTJacobiRecurrence_Internal(1,a,b,cnm1,cnm1x,cnm2); 144394e21283SToby Isaac pn2 = 1.; 144494e21283SToby Isaac pn1 = cnm1 + cnm1x*x; 144594e21283SToby Isaac if (n == 1) {*P = pn1; PetscFunctionReturn(0);} 1446e6a796c3SToby Isaac *P = 0.0; 1447e6a796c3SToby Isaac for (k = 2; k < n+1; ++k) { 144894e21283SToby Isaac PetscDTJacobiRecurrence_Internal(k,a,b,cnm1,cnm1x,cnm2); 1449e6a796c3SToby Isaac 145094e21283SToby Isaac *P = (cnm1 + cnm1x*x)*pn1 - cnm2*pn2; 1451e6a796c3SToby Isaac pn2 = pn1; 1452e6a796c3SToby Isaac pn1 = *P; 1453e6a796c3SToby Isaac } 1454e6a796c3SToby Isaac PetscFunctionReturn(0); 1455e6a796c3SToby Isaac } 1456e6a796c3SToby Isaac 1457e6a796c3SToby Isaac /* Evaluates the first derivative of P_{n}^{a,b} at a point x. */ 14589fbee547SJacob Faibussowitsch static inline PetscErrorCode PetscDTComputeJacobiDerivative(PetscReal a, PetscReal b, PetscInt n, PetscReal x, PetscInt k, PetscReal *P) 1459e6a796c3SToby Isaac { 1460e6a796c3SToby Isaac PetscReal nP; 1461e6a796c3SToby Isaac PetscInt i; 1462e6a796c3SToby Isaac PetscErrorCode ierr; 1463e6a796c3SToby Isaac 1464e6a796c3SToby Isaac PetscFunctionBegin; 146517a42bb7SSatish Balay *P = 0.0; 146617a42bb7SSatish Balay if (k > n) PetscFunctionReturn(0); 1467e6a796c3SToby Isaac ierr = PetscDTComputeJacobi(a+k, b+k, n-k, x, &nP);CHKERRQ(ierr); 1468e6a796c3SToby Isaac for (i = 0; i < k; i++) nP *= (a + b + n + 1. + i) * 0.5; 1469e6a796c3SToby Isaac *P = nP; 1470e6a796c3SToby Isaac PetscFunctionReturn(0); 1471e6a796c3SToby Isaac } 1472e6a796c3SToby Isaac 1473e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussJacobiQuadrature_Newton_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscReal x[], PetscReal w[]) 1474e6a796c3SToby Isaac { 1475e6a796c3SToby Isaac PetscInt maxIter = 100; 147694e21283SToby Isaac PetscReal eps = PetscExpReal(0.75 * PetscLogReal(PETSC_MACHINE_EPSILON)); 1477200b5abcSJed Brown PetscReal a1, a6, gf; 1478e6a796c3SToby Isaac PetscInt k; 1479e6a796c3SToby Isaac PetscErrorCode ierr; 1480e6a796c3SToby Isaac 1481e6a796c3SToby Isaac PetscFunctionBegin; 1482e6a796c3SToby Isaac 1483e6a796c3SToby Isaac a1 = PetscPowReal(2.0, a+b+1); 148494e21283SToby Isaac #if defined(PETSC_HAVE_LGAMMA) 1485200b5abcSJed Brown { 1486200b5abcSJed Brown PetscReal a2, a3, a4, a5; 148794e21283SToby Isaac a2 = PetscLGamma(a + npoints + 1); 148894e21283SToby Isaac a3 = PetscLGamma(b + npoints + 1); 148994e21283SToby Isaac a4 = PetscLGamma(a + b + npoints + 1); 149094e21283SToby Isaac a5 = PetscLGamma(npoints + 1); 149194e21283SToby Isaac gf = PetscExpReal(a2 + a3 - (a4 + a5)); 1492200b5abcSJed Brown } 1493e6a796c3SToby Isaac #else 1494e6a796c3SToby Isaac { 1495e6a796c3SToby Isaac PetscInt ia, ib; 1496e6a796c3SToby Isaac 1497e6a796c3SToby Isaac ia = (PetscInt) a; 1498e6a796c3SToby Isaac ib = (PetscInt) b; 149994e21283SToby Isaac gf = 1.; 150094e21283SToby Isaac if (ia == a && ia >= 0) { /* compute ratio of rising factorals wrt a */ 150194e21283SToby Isaac for (k = 0; k < ia; k++) gf *= (npoints + 1. + k) / (npoints + b + 1. + k); 150294e21283SToby Isaac } else if (b == b && ib >= 0) { /* compute ratio of rising factorials wrt b */ 150394e21283SToby Isaac for (k = 0; k < ib; k++) gf *= (npoints + 1. + k) / (npoints + a + 1. + k); 150494e21283SToby Isaac } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"lgamma() - math routine is unavailable."); 1505e6a796c3SToby Isaac } 1506e6a796c3SToby Isaac #endif 1507e6a796c3SToby Isaac 150894e21283SToby Isaac a6 = a1 * gf; 1509e6a796c3SToby Isaac /* Computes the m roots of P_{m}^{a,b} on [-1,1] by Newton's method with Chebyshev points as initial guesses. 1510e6a796c3SToby Isaac Algorithm implemented from the pseudocode given by Karniadakis and Sherwin and Python in FIAT */ 1511e6a796c3SToby Isaac for (k = 0; k < npoints; ++k) { 151294e21283SToby Isaac PetscReal r = PetscCosReal(PETSC_PI * (1. - (4.*k + 3. + 2.*b) / (4.*npoints + 2.*(a + b + 1.)))), dP; 1513e6a796c3SToby Isaac PetscInt j; 1514e6a796c3SToby Isaac 1515e6a796c3SToby Isaac if (k > 0) r = 0.5 * (r + x[k-1]); 1516e6a796c3SToby Isaac for (j = 0; j < maxIter; ++j) { 1517e6a796c3SToby Isaac PetscReal s = 0.0, delta, f, fp; 1518e6a796c3SToby Isaac PetscInt i; 1519e6a796c3SToby Isaac 1520e6a796c3SToby Isaac for (i = 0; i < k; ++i) s = s + 1.0 / (r - x[i]); 1521e6a796c3SToby Isaac ierr = PetscDTComputeJacobi(a, b, npoints, r, &f);CHKERRQ(ierr); 1522e6a796c3SToby Isaac ierr = PetscDTComputeJacobiDerivative(a, b, npoints, r, 1, &fp);CHKERRQ(ierr); 1523e6a796c3SToby Isaac delta = f / (fp - f * s); 1524e6a796c3SToby Isaac r = r - delta; 1525e6a796c3SToby Isaac if (PetscAbsReal(delta) < eps) break; 1526e6a796c3SToby Isaac } 1527e6a796c3SToby Isaac x[k] = r; 1528e6a796c3SToby Isaac ierr = PetscDTComputeJacobiDerivative(a, b, npoints, x[k], 1, &dP);CHKERRQ(ierr); 1529e6a796c3SToby Isaac w[k] = a6 / (1.0 - PetscSqr(x[k])) / PetscSqr(dP); 1530e6a796c3SToby Isaac } 1531e6a796c3SToby Isaac PetscFunctionReturn(0); 1532e6a796c3SToby Isaac } 1533e6a796c3SToby Isaac 153494e21283SToby Isaac /* Compute the diagonals of the Jacobi matrix used in Golub & Welsch algorithms for Gauss-Jacobi 1535e6a796c3SToby Isaac * quadrature weight calculations on [-1,1] for exponents (1. + x)^a (1.-x)^b */ 1536e6a796c3SToby Isaac static PetscErrorCode PetscDTJacobiMatrix_Internal(PetscInt nPoints, PetscReal a, PetscReal b, PetscReal *d, PetscReal *s) 1537e6a796c3SToby Isaac { 1538e6a796c3SToby Isaac PetscInt i; 1539e6a796c3SToby Isaac 1540e6a796c3SToby Isaac PetscFunctionBegin; 1541e6a796c3SToby Isaac for (i = 0; i < nPoints; i++) { 154294e21283SToby Isaac PetscReal A, B, C; 1543e6a796c3SToby Isaac 154494e21283SToby Isaac PetscDTJacobiRecurrence_Internal(i+1,a,b,A,B,C); 154594e21283SToby Isaac d[i] = -A / B; 154694e21283SToby Isaac if (i) s[i-1] *= C / B; 154794e21283SToby Isaac if (i < nPoints - 1) s[i] = 1. / B; 1548e6a796c3SToby Isaac } 1549e6a796c3SToby Isaac PetscFunctionReturn(0); 1550e6a796c3SToby Isaac } 1551e6a796c3SToby Isaac 1552e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussJacobiQuadrature_GolubWelsch_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscReal *x, PetscReal *w) 1553e6a796c3SToby Isaac { 1554e6a796c3SToby Isaac PetscReal mu0; 1555e6a796c3SToby Isaac PetscReal ga, gb, gab; 1556e6a796c3SToby Isaac PetscInt i; 1557e6a796c3SToby Isaac PetscErrorCode ierr; 1558e6a796c3SToby Isaac 1559e6a796c3SToby Isaac PetscFunctionBegin; 1560e6a796c3SToby Isaac ierr = PetscCitationsRegister(GolubWelschCitation, &GolubWelschCite);CHKERRQ(ierr); 1561e6a796c3SToby Isaac 1562e6a796c3SToby Isaac #if defined(PETSC_HAVE_TGAMMA) 1563e6a796c3SToby Isaac ga = PetscTGamma(a + 1); 1564e6a796c3SToby Isaac gb = PetscTGamma(b + 1); 1565e6a796c3SToby Isaac gab = PetscTGamma(a + b + 2); 1566e6a796c3SToby Isaac #else 1567e6a796c3SToby Isaac { 1568e6a796c3SToby Isaac PetscInt ia, ib; 1569e6a796c3SToby Isaac 1570e6a796c3SToby Isaac ia = (PetscInt) a; 1571e6a796c3SToby Isaac ib = (PetscInt) b; 1572e6a796c3SToby Isaac if (ia == a && ib == b && ia + 1 > 0 && ib + 1 > 0 && ia + ib + 2 > 0) { /* All gamma(x) terms are (x-1)! terms */ 1573e6a796c3SToby Isaac ierr = PetscDTFactorial(ia, &ga);CHKERRQ(ierr); 1574e6a796c3SToby Isaac ierr = PetscDTFactorial(ib, &gb);CHKERRQ(ierr); 1575e6a796c3SToby Isaac ierr = PetscDTFactorial(ia + ib + 1, &gb);CHKERRQ(ierr); 1576e6a796c3SToby Isaac } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"tgamma() - math routine is unavailable."); 1577e6a796c3SToby Isaac } 1578e6a796c3SToby Isaac #endif 1579e6a796c3SToby Isaac mu0 = PetscPowReal(2.,a + b + 1.) * ga * gb / gab; 1580e6a796c3SToby Isaac 1581e6a796c3SToby Isaac #if defined(PETSCDTGAUSSIANQUADRATURE_EIG) 1582e6a796c3SToby Isaac { 1583e6a796c3SToby Isaac PetscReal *diag, *subdiag; 1584e6a796c3SToby Isaac PetscScalar *V; 1585e6a796c3SToby Isaac 1586e6a796c3SToby Isaac ierr = PetscMalloc2(npoints, &diag, npoints, &subdiag);CHKERRQ(ierr); 1587e6a796c3SToby Isaac ierr = PetscMalloc1(npoints*npoints, &V);CHKERRQ(ierr); 1588e6a796c3SToby Isaac ierr = PetscDTJacobiMatrix_Internal(npoints, a, b, diag, subdiag);CHKERRQ(ierr); 1589e6a796c3SToby Isaac for (i = 0; i < npoints - 1; i++) subdiag[i] = PetscSqrtReal(subdiag[i]); 1590e6a796c3SToby Isaac ierr = PetscDTSymmetricTridiagonalEigensolve(npoints, diag, subdiag, x, V);CHKERRQ(ierr); 159194e21283SToby Isaac for (i = 0; i < npoints; i++) w[i] = PetscSqr(PetscRealPart(V[i * npoints])) * mu0; 1592e6a796c3SToby Isaac ierr = PetscFree(V);CHKERRQ(ierr); 1593e6a796c3SToby Isaac ierr = PetscFree2(diag, subdiag);CHKERRQ(ierr); 1594e6a796c3SToby Isaac } 1595e6a796c3SToby Isaac #else 1596e6a796c3SToby Isaac SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "A LAPACK symmetric tridiagonal eigensolver could not be found"); 1597e6a796c3SToby Isaac #endif 159894e21283SToby Isaac { /* As of March 2, 2020, The Sun Performance Library breaks the LAPACK contract for xstegr and xsteqr: the 159994e21283SToby Isaac eigenvalues are not guaranteed to be in ascending order. So we heave a passive aggressive sigh and check that 160094e21283SToby Isaac the eigenvalues are sorted */ 160194e21283SToby Isaac PetscBool sorted; 160294e21283SToby Isaac 160394e21283SToby Isaac ierr = PetscSortedReal(npoints, x, &sorted);CHKERRQ(ierr); 160494e21283SToby Isaac if (!sorted) { 160594e21283SToby Isaac PetscInt *order, i; 160694e21283SToby Isaac PetscReal *tmp; 160794e21283SToby Isaac 160894e21283SToby Isaac ierr = PetscMalloc2(npoints, &order, npoints, &tmp);CHKERRQ(ierr); 160994e21283SToby Isaac for (i = 0; i < npoints; i++) order[i] = i; 161094e21283SToby Isaac ierr = PetscSortRealWithPermutation(npoints, x, order);CHKERRQ(ierr); 161194e21283SToby Isaac ierr = PetscArraycpy(tmp, x, npoints);CHKERRQ(ierr); 161294e21283SToby Isaac for (i = 0; i < npoints; i++) x[i] = tmp[order[i]]; 161394e21283SToby Isaac ierr = PetscArraycpy(tmp, w, npoints);CHKERRQ(ierr); 161494e21283SToby Isaac for (i = 0; i < npoints; i++) w[i] = tmp[order[i]]; 161594e21283SToby Isaac ierr = PetscFree2(order, tmp);CHKERRQ(ierr); 161694e21283SToby Isaac } 161794e21283SToby Isaac } 1618e6a796c3SToby Isaac PetscFunctionReturn(0); 1619e6a796c3SToby Isaac } 1620e6a796c3SToby Isaac 1621e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussJacobiQuadrature_Internal(PetscInt npoints,PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[], PetscBool newton) 1622e6a796c3SToby Isaac { 1623e6a796c3SToby Isaac PetscErrorCode ierr; 1624e6a796c3SToby Isaac 1625e6a796c3SToby Isaac PetscFunctionBegin; 16262c71b3e2SJacob Faibussowitsch PetscCheckFalse(npoints < 1,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of points must be positive"); 1627e6a796c3SToby Isaac /* If asking for a 1D Lobatto point, just return the non-Lobatto 1D point */ 16282c71b3e2SJacob Faibussowitsch PetscCheckFalse(alpha <= -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"alpha must be > -1."); 16292c71b3e2SJacob Faibussowitsch PetscCheckFalse(beta <= -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"beta must be > -1."); 1630e6a796c3SToby Isaac 1631e6a796c3SToby Isaac if (newton) { 1632e6a796c3SToby Isaac ierr = PetscDTGaussJacobiQuadrature_Newton_Internal(npoints, alpha, beta, x, w);CHKERRQ(ierr); 1633e6a796c3SToby Isaac } else { 1634e6a796c3SToby Isaac ierr = PetscDTGaussJacobiQuadrature_GolubWelsch_Internal(npoints, alpha, beta, x, w);CHKERRQ(ierr); 1635e6a796c3SToby Isaac } 1636e6a796c3SToby Isaac if (alpha == beta) { /* symmetrize */ 1637e6a796c3SToby Isaac PetscInt i; 1638e6a796c3SToby Isaac for (i = 0; i < (npoints + 1) / 2; i++) { 1639e6a796c3SToby Isaac PetscInt j = npoints - 1 - i; 1640e6a796c3SToby Isaac PetscReal xi = x[i]; 1641e6a796c3SToby Isaac PetscReal xj = x[j]; 1642e6a796c3SToby Isaac PetscReal wi = w[i]; 1643e6a796c3SToby Isaac PetscReal wj = w[j]; 1644e6a796c3SToby Isaac 1645e6a796c3SToby Isaac x[i] = (xi - xj) / 2.; 1646e6a796c3SToby Isaac x[j] = (xj - xi) / 2.; 1647e6a796c3SToby Isaac w[i] = w[j] = (wi + wj) / 2.; 1648e6a796c3SToby Isaac } 1649e6a796c3SToby Isaac } 1650e6a796c3SToby Isaac PetscFunctionReturn(0); 1651e6a796c3SToby Isaac } 1652e6a796c3SToby Isaac 165394e21283SToby Isaac /*@ 165494e21283SToby Isaac PetscDTGaussJacobiQuadrature - quadrature for the interval [a, b] with the weight function 165594e21283SToby Isaac $(x-a)^\alpha (x-b)^\beta$. 165694e21283SToby Isaac 165794e21283SToby Isaac Not collective 165894e21283SToby Isaac 165994e21283SToby Isaac Input Parameters: 166094e21283SToby Isaac + npoints - the number of points in the quadrature rule 166194e21283SToby Isaac . a - the left endpoint of the interval 166294e21283SToby Isaac . b - the right endpoint of the interval 166394e21283SToby Isaac . alpha - the left exponent 166494e21283SToby Isaac - beta - the right exponent 166594e21283SToby Isaac 166694e21283SToby Isaac Output Parameters: 166794e21283SToby Isaac + x - array of length npoints, the locations of the quadrature points 166894e21283SToby Isaac - w - array of length npoints, the weights of the quadrature points 166994e21283SToby Isaac 167094e21283SToby Isaac Level: intermediate 167194e21283SToby Isaac 167294e21283SToby Isaac Note: this quadrature rule is exact for polynomials up to degree 2*npoints - 1. 167394e21283SToby Isaac @*/ 167494e21283SToby Isaac PetscErrorCode PetscDTGaussJacobiQuadrature(PetscInt npoints,PetscReal a, PetscReal b, PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[]) 1675e6a796c3SToby Isaac { 167694e21283SToby Isaac PetscInt i; 1677e6a796c3SToby Isaac PetscErrorCode ierr; 1678e6a796c3SToby Isaac 1679e6a796c3SToby Isaac PetscFunctionBegin; 168094e21283SToby Isaac ierr = PetscDTGaussJacobiQuadrature_Internal(npoints, alpha, beta, x, w, PetscDTGaussQuadratureNewton_Internal);CHKERRQ(ierr); 168194e21283SToby Isaac if (a != -1. || b != 1.) { /* shift */ 168294e21283SToby Isaac for (i = 0; i < npoints; i++) { 168394e21283SToby Isaac x[i] = (x[i] + 1.) * ((b - a) / 2.) + a; 168494e21283SToby Isaac w[i] *= (b - a) / 2.; 168594e21283SToby Isaac } 168694e21283SToby Isaac } 1687e6a796c3SToby Isaac PetscFunctionReturn(0); 1688e6a796c3SToby Isaac } 1689e6a796c3SToby Isaac 1690e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussLobattoJacobiQuadrature_Internal(PetscInt npoints,PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[], PetscBool newton) 1691e6a796c3SToby Isaac { 1692e6a796c3SToby Isaac PetscInt i; 1693e6a796c3SToby Isaac PetscErrorCode ierr; 1694e6a796c3SToby Isaac 1695e6a796c3SToby Isaac PetscFunctionBegin; 16962c71b3e2SJacob Faibussowitsch PetscCheckFalse(npoints < 2,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of points must be positive"); 1697e6a796c3SToby Isaac /* If asking for a 1D Lobatto point, just return the non-Lobatto 1D point */ 16982c71b3e2SJacob Faibussowitsch PetscCheckFalse(alpha <= -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"alpha must be > -1."); 16992c71b3e2SJacob Faibussowitsch PetscCheckFalse(beta <= -1.,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"beta must be > -1."); 1700e6a796c3SToby Isaac 1701e6a796c3SToby Isaac x[0] = -1.; 1702e6a796c3SToby Isaac x[npoints-1] = 1.; 170394e21283SToby Isaac if (npoints > 2) { 170494e21283SToby Isaac ierr = PetscDTGaussJacobiQuadrature_Internal(npoints-2, alpha+1., beta+1., &x[1], &w[1], newton);CHKERRQ(ierr); 170594e21283SToby Isaac } 1706e6a796c3SToby Isaac for (i = 1; i < npoints - 1; i++) { 1707e6a796c3SToby Isaac w[i] /= (1. - x[i]*x[i]); 1708e6a796c3SToby Isaac } 1709e6a796c3SToby Isaac ierr = PetscDTGaussLobattoJacobiEndweights_Internal(npoints, alpha, beta, &w[0], &w[npoints-1]);CHKERRQ(ierr); 1710e6a796c3SToby Isaac PetscFunctionReturn(0); 1711e6a796c3SToby Isaac } 1712e6a796c3SToby Isaac 171337045ce4SJed Brown /*@ 171494e21283SToby Isaac PetscDTGaussLobattoJacobiQuadrature - quadrature for the interval [a, b] with the weight function 171594e21283SToby Isaac $(x-a)^\alpha (x-b)^\beta$, with endpoints a and b included as quadrature points. 171694e21283SToby Isaac 171794e21283SToby Isaac Not collective 171894e21283SToby Isaac 171994e21283SToby Isaac Input Parameters: 172094e21283SToby Isaac + npoints - the number of points in the quadrature rule 172194e21283SToby Isaac . a - the left endpoint of the interval 172294e21283SToby Isaac . b - the right endpoint of the interval 172394e21283SToby Isaac . alpha - the left exponent 172494e21283SToby Isaac - beta - the right exponent 172594e21283SToby Isaac 172694e21283SToby Isaac Output Parameters: 172794e21283SToby Isaac + x - array of length npoints, the locations of the quadrature points 172894e21283SToby Isaac - w - array of length npoints, the weights of the quadrature points 172994e21283SToby Isaac 173094e21283SToby Isaac Level: intermediate 173194e21283SToby Isaac 173294e21283SToby Isaac Note: this quadrature rule is exact for polynomials up to degree 2*npoints - 3. 173394e21283SToby Isaac @*/ 173494e21283SToby Isaac PetscErrorCode PetscDTGaussLobattoJacobiQuadrature(PetscInt npoints,PetscReal a, PetscReal b, PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[]) 173594e21283SToby Isaac { 173694e21283SToby Isaac PetscInt i; 173794e21283SToby Isaac PetscErrorCode ierr; 173894e21283SToby Isaac 173994e21283SToby Isaac PetscFunctionBegin; 174094e21283SToby Isaac ierr = PetscDTGaussLobattoJacobiQuadrature_Internal(npoints, alpha, beta, x, w, PetscDTGaussQuadratureNewton_Internal);CHKERRQ(ierr); 174194e21283SToby Isaac if (a != -1. || b != 1.) { /* shift */ 174294e21283SToby Isaac for (i = 0; i < npoints; i++) { 174394e21283SToby Isaac x[i] = (x[i] + 1.) * ((b - a) / 2.) + a; 174494e21283SToby Isaac w[i] *= (b - a) / 2.; 174594e21283SToby Isaac } 174694e21283SToby Isaac } 174794e21283SToby Isaac PetscFunctionReturn(0); 174894e21283SToby Isaac } 174994e21283SToby Isaac 175094e21283SToby Isaac /*@ 1751e6a796c3SToby Isaac PetscDTGaussQuadrature - create Gauss-Legendre quadrature 175237045ce4SJed Brown 175337045ce4SJed Brown Not Collective 175437045ce4SJed Brown 17554165533cSJose E. Roman Input Parameters: 175637045ce4SJed Brown + npoints - number of points 175737045ce4SJed Brown . a - left end of interval (often-1) 175837045ce4SJed Brown - b - right end of interval (often +1) 175937045ce4SJed Brown 17604165533cSJose E. Roman Output Parameters: 176137045ce4SJed Brown + x - quadrature points 176237045ce4SJed Brown - w - quadrature weights 176337045ce4SJed Brown 176437045ce4SJed Brown Level: intermediate 176537045ce4SJed Brown 176637045ce4SJed Brown References: 176796a0c994SBarry Smith . 1. - Golub and Welsch, Calculation of Quadrature Rules, Math. Comp. 23(106), 1969. 176837045ce4SJed Brown 176937045ce4SJed Brown .seealso: PetscDTLegendreEval() 177037045ce4SJed Brown @*/ 177137045ce4SJed Brown PetscErrorCode PetscDTGaussQuadrature(PetscInt npoints,PetscReal a,PetscReal b,PetscReal *x,PetscReal *w) 177237045ce4SJed Brown { 177337045ce4SJed Brown PetscInt i; 1774e6a796c3SToby Isaac PetscErrorCode ierr; 177537045ce4SJed Brown 177637045ce4SJed Brown PetscFunctionBegin; 177794e21283SToby Isaac ierr = PetscDTGaussJacobiQuadrature_Internal(npoints, 0., 0., x, w, PetscDTGaussQuadratureNewton_Internal);CHKERRQ(ierr); 177894e21283SToby Isaac if (a != -1. || b != 1.) { /* shift */ 177937045ce4SJed Brown for (i = 0; i < npoints; i++) { 1780e6a796c3SToby Isaac x[i] = (x[i] + 1.) * ((b - a) / 2.) + a; 1781e6a796c3SToby Isaac w[i] *= (b - a) / 2.; 178237045ce4SJed Brown } 178337045ce4SJed Brown } 178437045ce4SJed Brown PetscFunctionReturn(0); 178537045ce4SJed Brown } 1786194825f6SJed Brown 17878272889dSSatish Balay /*@C 17888272889dSSatish Balay PetscDTGaussLobattoLegendreQuadrature - creates a set of the locations and weights of the Gauss-Lobatto-Legendre 17898272889dSSatish Balay nodes of a given size on the domain [-1,1] 17908272889dSSatish Balay 17918272889dSSatish Balay Not Collective 17928272889dSSatish Balay 1793d8d19677SJose E. Roman Input Parameters: 17948272889dSSatish Balay + n - number of grid nodes 1795f2e8fe4dShannah_mairs - type - PETSCGAUSSLOBATTOLEGENDRE_VIA_LINEAR_ALGEBRA or PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON 17968272889dSSatish Balay 17974165533cSJose E. Roman Output Parameters: 17988272889dSSatish Balay + x - quadrature points 17998272889dSSatish Balay - w - quadrature weights 18008272889dSSatish Balay 18018272889dSSatish Balay Notes: 18028272889dSSatish Balay For n > 30 the Newton approach computes duplicate (incorrect) values for some nodes because the initial guess is apparently not 18038272889dSSatish Balay close enough to the desired solution 18048272889dSSatish Balay 18058272889dSSatish Balay These are useful for implementing spectral methods based on Gauss-Lobatto-Legendre (GLL) nodes 18068272889dSSatish Balay 1807a8d69d7bSBarry 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 18088272889dSSatish Balay 18098272889dSSatish Balay Level: intermediate 18108272889dSSatish Balay 18118272889dSSatish Balay .seealso: PetscDTGaussQuadrature() 18128272889dSSatish Balay 18138272889dSSatish Balay @*/ 1814916e780bShannah_mairs PetscErrorCode PetscDTGaussLobattoLegendreQuadrature(PetscInt npoints,PetscGaussLobattoLegendreCreateType type,PetscReal *x,PetscReal *w) 18158272889dSSatish Balay { 1816e6a796c3SToby Isaac PetscBool newton; 18178272889dSSatish Balay PetscErrorCode ierr; 18188272889dSSatish Balay 18198272889dSSatish Balay PetscFunctionBegin; 18202c71b3e2SJacob Faibussowitsch PetscCheckFalse(npoints < 2,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must provide at least 2 grid points per element"); 182194e21283SToby Isaac newton = (PetscBool) (type == PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON); 1822e6a796c3SToby Isaac ierr = PetscDTGaussLobattoJacobiQuadrature_Internal(npoints, 0., 0., x, w, newton);CHKERRQ(ierr); 18238272889dSSatish Balay PetscFunctionReturn(0); 18248272889dSSatish Balay } 18258272889dSSatish Balay 1826744bafbcSMatthew G. Knepley /*@ 1827744bafbcSMatthew G. Knepley PetscDTGaussTensorQuadrature - creates a tensor-product Gauss quadrature 1828744bafbcSMatthew G. Knepley 1829744bafbcSMatthew G. Knepley Not Collective 1830744bafbcSMatthew G. Knepley 18314165533cSJose E. Roman Input Parameters: 1832744bafbcSMatthew G. Knepley + dim - The spatial dimension 1833a6b92713SMatthew G. Knepley . Nc - The number of components 1834744bafbcSMatthew G. Knepley . npoints - number of points in one dimension 1835744bafbcSMatthew G. Knepley . a - left end of interval (often-1) 1836744bafbcSMatthew G. Knepley - b - right end of interval (often +1) 1837744bafbcSMatthew G. Knepley 18384165533cSJose E. Roman Output Parameter: 1839744bafbcSMatthew G. Knepley . q - A PetscQuadrature object 1840744bafbcSMatthew G. Knepley 1841744bafbcSMatthew G. Knepley Level: intermediate 1842744bafbcSMatthew G. Knepley 1843744bafbcSMatthew G. Knepley .seealso: PetscDTGaussQuadrature(), PetscDTLegendreEval() 1844744bafbcSMatthew G. Knepley @*/ 1845a6b92713SMatthew G. Knepley PetscErrorCode PetscDTGaussTensorQuadrature(PetscInt dim, PetscInt Nc, PetscInt npoints, PetscReal a, PetscReal b, PetscQuadrature *q) 1846744bafbcSMatthew G. Knepley { 1847a6b92713SMatthew G. Knepley PetscInt totpoints = dim > 1 ? dim > 2 ? npoints*PetscSqr(npoints) : PetscSqr(npoints) : npoints, i, j, k, c; 1848744bafbcSMatthew G. Knepley PetscReal *x, *w, *xw, *ww; 1849744bafbcSMatthew G. Knepley PetscErrorCode ierr; 1850744bafbcSMatthew G. Knepley 1851744bafbcSMatthew G. Knepley PetscFunctionBegin; 1852744bafbcSMatthew G. Knepley ierr = PetscMalloc1(totpoints*dim,&x);CHKERRQ(ierr); 1853a6b92713SMatthew G. Knepley ierr = PetscMalloc1(totpoints*Nc,&w);CHKERRQ(ierr); 1854744bafbcSMatthew G. Knepley /* Set up the Golub-Welsch system */ 1855744bafbcSMatthew G. Knepley switch (dim) { 1856744bafbcSMatthew G. Knepley case 0: 1857744bafbcSMatthew G. Knepley ierr = PetscFree(x);CHKERRQ(ierr); 1858744bafbcSMatthew G. Knepley ierr = PetscFree(w);CHKERRQ(ierr); 1859744bafbcSMatthew G. Knepley ierr = PetscMalloc1(1, &x);CHKERRQ(ierr); 1860a6b92713SMatthew G. Knepley ierr = PetscMalloc1(Nc, &w);CHKERRQ(ierr); 1861744bafbcSMatthew G. Knepley x[0] = 0.0; 1862a6b92713SMatthew G. Knepley for (c = 0; c < Nc; ++c) w[c] = 1.0; 1863744bafbcSMatthew G. Knepley break; 1864744bafbcSMatthew G. Knepley case 1: 1865a6b92713SMatthew G. Knepley ierr = PetscMalloc1(npoints,&ww);CHKERRQ(ierr); 1866a6b92713SMatthew G. Knepley ierr = PetscDTGaussQuadrature(npoints, a, b, x, ww);CHKERRQ(ierr); 1867a6b92713SMatthew G. Knepley for (i = 0; i < npoints; ++i) for (c = 0; c < Nc; ++c) w[i*Nc+c] = ww[i]; 1868a6b92713SMatthew G. Knepley ierr = PetscFree(ww);CHKERRQ(ierr); 1869744bafbcSMatthew G. Knepley break; 1870744bafbcSMatthew G. Knepley case 2: 1871744bafbcSMatthew G. Knepley ierr = PetscMalloc2(npoints,&xw,npoints,&ww);CHKERRQ(ierr); 1872744bafbcSMatthew G. Knepley ierr = PetscDTGaussQuadrature(npoints, a, b, xw, ww);CHKERRQ(ierr); 1873744bafbcSMatthew G. Knepley for (i = 0; i < npoints; ++i) { 1874744bafbcSMatthew G. Knepley for (j = 0; j < npoints; ++j) { 1875744bafbcSMatthew G. Knepley x[(i*npoints+j)*dim+0] = xw[i]; 1876744bafbcSMatthew G. Knepley x[(i*npoints+j)*dim+1] = xw[j]; 1877a6b92713SMatthew G. Knepley for (c = 0; c < Nc; ++c) w[(i*npoints+j)*Nc+c] = ww[i] * ww[j]; 1878744bafbcSMatthew G. Knepley } 1879744bafbcSMatthew G. Knepley } 1880744bafbcSMatthew G. Knepley ierr = PetscFree2(xw,ww);CHKERRQ(ierr); 1881744bafbcSMatthew G. Knepley break; 1882744bafbcSMatthew G. Knepley case 3: 1883744bafbcSMatthew G. Knepley ierr = PetscMalloc2(npoints,&xw,npoints,&ww);CHKERRQ(ierr); 1884744bafbcSMatthew G. Knepley ierr = PetscDTGaussQuadrature(npoints, a, b, xw, ww);CHKERRQ(ierr); 1885744bafbcSMatthew G. Knepley for (i = 0; i < npoints; ++i) { 1886744bafbcSMatthew G. Knepley for (j = 0; j < npoints; ++j) { 1887744bafbcSMatthew G. Knepley for (k = 0; k < npoints; ++k) { 1888744bafbcSMatthew G. Knepley x[((i*npoints+j)*npoints+k)*dim+0] = xw[i]; 1889744bafbcSMatthew G. Knepley x[((i*npoints+j)*npoints+k)*dim+1] = xw[j]; 1890744bafbcSMatthew G. Knepley x[((i*npoints+j)*npoints+k)*dim+2] = xw[k]; 1891a6b92713SMatthew G. Knepley for (c = 0; c < Nc; ++c) w[((i*npoints+j)*npoints+k)*Nc+c] = ww[i] * ww[j] * ww[k]; 1892744bafbcSMatthew G. Knepley } 1893744bafbcSMatthew G. Knepley } 1894744bafbcSMatthew G. Knepley } 1895744bafbcSMatthew G. Knepley ierr = PetscFree2(xw,ww);CHKERRQ(ierr); 1896744bafbcSMatthew G. Knepley break; 1897744bafbcSMatthew G. Knepley default: 189898921bdaSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct quadrature rule for dimension %d", dim); 1899744bafbcSMatthew G. Knepley } 1900744bafbcSMatthew G. Knepley ierr = PetscQuadratureCreate(PETSC_COMM_SELF, q);CHKERRQ(ierr); 19012f5fb066SToby Isaac ierr = PetscQuadratureSetOrder(*q, 2*npoints-1);CHKERRQ(ierr); 1902a6b92713SMatthew G. Knepley ierr = PetscQuadratureSetData(*q, dim, Nc, totpoints, x, w);CHKERRQ(ierr); 1903d9bac1caSLisandro Dalcin ierr = PetscObjectChangeTypeName((PetscObject)*q,"GaussTensor");CHKERRQ(ierr); 1904744bafbcSMatthew G. Knepley PetscFunctionReturn(0); 1905744bafbcSMatthew G. Knepley } 1906744bafbcSMatthew G. Knepley 1907f5f57ec0SBarry Smith /*@ 1908e6a796c3SToby Isaac PetscDTStroudConicalQuadrature - create Stroud conical quadrature for a simplex 1909494e7359SMatthew G. Knepley 1910494e7359SMatthew G. Knepley Not Collective 1911494e7359SMatthew G. Knepley 19124165533cSJose E. Roman Input Parameters: 1913494e7359SMatthew G. Knepley + dim - The simplex dimension 1914a6b92713SMatthew G. Knepley . Nc - The number of components 1915dcce0ee2SMatthew G. Knepley . npoints - The number of points in one dimension 1916494e7359SMatthew G. Knepley . a - left end of interval (often-1) 1917494e7359SMatthew G. Knepley - b - right end of interval (often +1) 1918494e7359SMatthew G. Knepley 19194165533cSJose E. Roman Output Parameter: 1920552aa4f7SMatthew G. Knepley . q - A PetscQuadrature object 1921494e7359SMatthew G. Knepley 1922494e7359SMatthew G. Knepley Level: intermediate 1923494e7359SMatthew G. Knepley 1924494e7359SMatthew G. Knepley References: 192596a0c994SBarry Smith . 1. - Karniadakis and Sherwin. FIAT 1926494e7359SMatthew G. Knepley 1927e6a796c3SToby Isaac Note: For dim == 1, this is Gauss-Legendre quadrature 1928e6a796c3SToby Isaac 1929744bafbcSMatthew G. Knepley .seealso: PetscDTGaussTensorQuadrature(), PetscDTGaussQuadrature() 1930494e7359SMatthew G. Knepley @*/ 1931e6a796c3SToby Isaac PetscErrorCode PetscDTStroudConicalQuadrature(PetscInt dim, PetscInt Nc, PetscInt npoints, PetscReal a, PetscReal b, PetscQuadrature *q) 1932494e7359SMatthew G. Knepley { 1933fbdc3dfeSToby Isaac PetscInt totprev, totrem; 1934fbdc3dfeSToby Isaac PetscInt totpoints; 1935fbdc3dfeSToby Isaac PetscReal *p1, *w1; 1936fbdc3dfeSToby Isaac PetscReal *x, *w; 1937fbdc3dfeSToby Isaac PetscInt i, j, k, l, m, pt, c; 1938fbdc3dfeSToby Isaac PetscErrorCode ierr; 1939494e7359SMatthew G. Knepley 1940494e7359SMatthew G. Knepley PetscFunctionBegin; 19412c71b3e2SJacob Faibussowitsch PetscCheckFalse((a != -1.0) || (b != 1.0),PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must use default internal right now"); 1942fbdc3dfeSToby Isaac totpoints = 1; 1943fbdc3dfeSToby Isaac for (i = 0, totpoints = 1; i < dim; i++) totpoints *= npoints; 1944dcce0ee2SMatthew G. Knepley ierr = PetscMalloc1(totpoints*dim, &x);CHKERRQ(ierr); 1945dcce0ee2SMatthew G. Knepley ierr = PetscMalloc1(totpoints*Nc, &w);CHKERRQ(ierr); 1946fbdc3dfeSToby Isaac ierr = PetscMalloc2(npoints, &p1, npoints, &w1);CHKERRQ(ierr); 1947fbdc3dfeSToby Isaac for (i = 0; i < totpoints*Nc; i++) w[i] = 1.; 1948fbdc3dfeSToby Isaac for (i = 0, totprev = 1, totrem = totpoints / npoints; i < dim; i++) { 1949fbdc3dfeSToby Isaac PetscReal mul; 1950fbdc3dfeSToby Isaac 1951fbdc3dfeSToby Isaac mul = PetscPowReal(2.,-i); 1952fbdc3dfeSToby Isaac ierr = PetscDTGaussJacobiQuadrature(npoints, -1., 1., i, 0.0, p1, w1);CHKERRQ(ierr); 1953fbdc3dfeSToby Isaac for (pt = 0, l = 0; l < totprev; l++) { 1954fbdc3dfeSToby Isaac for (j = 0; j < npoints; j++) { 1955fbdc3dfeSToby Isaac for (m = 0; m < totrem; m++, pt++) { 1956fbdc3dfeSToby Isaac for (k = 0; k < i; k++) x[pt*dim+k] = (x[pt*dim+k]+1.)*(1.-p1[j])*0.5 - 1.; 1957fbdc3dfeSToby Isaac x[pt * dim + i] = p1[j]; 1958fbdc3dfeSToby Isaac for (c = 0; c < Nc; c++) w[pt*Nc + c] *= mul * w1[j]; 1959494e7359SMatthew G. Knepley } 1960494e7359SMatthew G. Knepley } 1961494e7359SMatthew G. Knepley } 1962fbdc3dfeSToby Isaac totprev *= npoints; 1963fbdc3dfeSToby Isaac totrem /= npoints; 1964494e7359SMatthew G. Knepley } 1965fbdc3dfeSToby Isaac ierr = PetscFree2(p1, w1);CHKERRQ(ierr); 196621454ff5SMatthew G. Knepley ierr = PetscQuadratureCreate(PETSC_COMM_SELF, q);CHKERRQ(ierr); 19672f5fb066SToby Isaac ierr = PetscQuadratureSetOrder(*q, 2*npoints-1);CHKERRQ(ierr); 1968dcce0ee2SMatthew G. Knepley ierr = PetscQuadratureSetData(*q, dim, Nc, totpoints, x, w);CHKERRQ(ierr); 1969fbdc3dfeSToby Isaac ierr = PetscObjectChangeTypeName((PetscObject)*q,"StroudConical");CHKERRQ(ierr); 1970494e7359SMatthew G. Knepley PetscFunctionReturn(0); 1971494e7359SMatthew G. Knepley } 1972494e7359SMatthew G. Knepley 1973f5f57ec0SBarry Smith /*@ 1974b3c0f97bSTom Klotz PetscDTTanhSinhTensorQuadrature - create tanh-sinh quadrature for a tensor product cell 1975b3c0f97bSTom Klotz 1976b3c0f97bSTom Klotz Not Collective 1977b3c0f97bSTom Klotz 19784165533cSJose E. Roman Input Parameters: 1979b3c0f97bSTom Klotz + dim - The cell dimension 1980b3c0f97bSTom Klotz . level - The number of points in one dimension, 2^l 1981b3c0f97bSTom Klotz . a - left end of interval (often-1) 1982b3c0f97bSTom Klotz - b - right end of interval (often +1) 1983b3c0f97bSTom Klotz 19844165533cSJose E. Roman Output Parameter: 1985b3c0f97bSTom Klotz . q - A PetscQuadrature object 1986b3c0f97bSTom Klotz 1987b3c0f97bSTom Klotz Level: intermediate 1988b3c0f97bSTom Klotz 1989b3c0f97bSTom Klotz .seealso: PetscDTGaussTensorQuadrature() 1990b3c0f97bSTom Klotz @*/ 1991b3c0f97bSTom Klotz PetscErrorCode PetscDTTanhSinhTensorQuadrature(PetscInt dim, PetscInt level, PetscReal a, PetscReal b, PetscQuadrature *q) 1992b3c0f97bSTom Klotz { 1993b3c0f97bSTom Klotz const PetscInt p = 16; /* Digits of precision in the evaluation */ 1994b3c0f97bSTom Klotz const PetscReal alpha = (b-a)/2.; /* Half-width of the integration interval */ 1995b3c0f97bSTom Klotz const PetscReal beta = (b+a)/2.; /* Center of the integration interval */ 1996b3c0f97bSTom Klotz const PetscReal h = PetscPowReal(2.0, -level); /* Step size, length between x_k */ 1997d84b4d08SMatthew G. Knepley PetscReal xk; /* Quadrature point x_k on reference domain [-1, 1] */ 1998b3c0f97bSTom Klotz PetscReal wk = 0.5*PETSC_PI; /* Quadrature weight at x_k */ 1999b3c0f97bSTom Klotz PetscReal *x, *w; 2000b3c0f97bSTom Klotz PetscInt K, k, npoints; 2001b3c0f97bSTom Klotz PetscErrorCode ierr; 2002b3c0f97bSTom Klotz 2003b3c0f97bSTom Klotz PetscFunctionBegin; 20042c71b3e2SJacob Faibussowitsch PetscCheckFalse(dim > 1,PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %d not yet implemented", dim); 20052c71b3e2SJacob Faibussowitsch PetscCheckFalse(!level,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a number of significant digits"); 2006b3c0f97bSTom Klotz /* Find K such that the weights are < 32 digits of precision */ 2007b3c0f97bSTom Klotz for (K = 1; PetscAbsReal(PetscLog10Real(wk)) < 2*p; ++K) { 20089add2064SThomas Klotz wk = 0.5*h*PETSC_PI*PetscCoshReal(K*h)/PetscSqr(PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(K*h))); 2009b3c0f97bSTom Klotz } 2010b3c0f97bSTom Klotz ierr = PetscQuadratureCreate(PETSC_COMM_SELF, q);CHKERRQ(ierr); 2011b3c0f97bSTom Klotz ierr = PetscQuadratureSetOrder(*q, 2*K+1);CHKERRQ(ierr); 2012b3c0f97bSTom Klotz npoints = 2*K-1; 2013b3c0f97bSTom Klotz ierr = PetscMalloc1(npoints*dim, &x);CHKERRQ(ierr); 2014b3c0f97bSTom Klotz ierr = PetscMalloc1(npoints, &w);CHKERRQ(ierr); 2015b3c0f97bSTom Klotz /* Center term */ 2016b3c0f97bSTom Klotz x[0] = beta; 2017b3c0f97bSTom Klotz w[0] = 0.5*alpha*PETSC_PI; 2018b3c0f97bSTom Klotz for (k = 1; k < K; ++k) { 20199add2064SThomas Klotz wk = 0.5*alpha*h*PETSC_PI*PetscCoshReal(k*h)/PetscSqr(PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(k*h))); 20201118d4bcSLisandro Dalcin xk = PetscTanhReal(0.5*PETSC_PI*PetscSinhReal(k*h)); 2021b3c0f97bSTom Klotz x[2*k-1] = -alpha*xk+beta; 2022b3c0f97bSTom Klotz w[2*k-1] = wk; 2023b3c0f97bSTom Klotz x[2*k+0] = alpha*xk+beta; 2024b3c0f97bSTom Klotz w[2*k+0] = wk; 2025b3c0f97bSTom Klotz } 2026a6b92713SMatthew G. Knepley ierr = PetscQuadratureSetData(*q, dim, 1, npoints, x, w);CHKERRQ(ierr); 2027b3c0f97bSTom Klotz PetscFunctionReturn(0); 2028b3c0f97bSTom Klotz } 2029b3c0f97bSTom Klotz 2030d6685f55SMatthew G. Knepley PetscErrorCode PetscDTTanhSinhIntegrate(void (*func)(const PetscReal[], void *, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, void *ctx, PetscReal *sol) 2031b3c0f97bSTom Klotz { 2032b3c0f97bSTom Klotz const PetscInt p = 16; /* Digits of precision in the evaluation */ 2033b3c0f97bSTom Klotz const PetscReal alpha = (b-a)/2.; /* Half-width of the integration interval */ 2034b3c0f97bSTom Klotz const PetscReal beta = (b+a)/2.; /* Center of the integration interval */ 2035b3c0f97bSTom Klotz PetscReal h = 1.0; /* Step size, length between x_k */ 2036b3c0f97bSTom Klotz PetscInt l = 0; /* Level of refinement, h = 2^{-l} */ 2037b3c0f97bSTom Klotz PetscReal osum = 0.0; /* Integral on last level */ 2038b3c0f97bSTom Klotz PetscReal psum = 0.0; /* Integral on the level before the last level */ 2039b3c0f97bSTom Klotz PetscReal sum; /* Integral on current level */ 2040446c295cSMatthew G. Knepley PetscReal yk; /* Quadrature point 1 - x_k on reference domain [-1, 1] */ 2041b3c0f97bSTom Klotz PetscReal lx, rx; /* Quadrature points to the left and right of 0 on the real domain [a, b] */ 2042b3c0f97bSTom Klotz PetscReal wk; /* Quadrature weight at x_k */ 2043b3c0f97bSTom Klotz PetscReal lval, rval; /* Terms in the quadature sum to the left and right of 0 */ 2044b3c0f97bSTom Klotz PetscInt d; /* Digits of precision in the integral */ 2045b3c0f97bSTom Klotz 2046b3c0f97bSTom Klotz PetscFunctionBegin; 20472c71b3e2SJacob Faibussowitsch PetscCheckFalse(digits <= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a positive number of significant digits"); 2048b3c0f97bSTom Klotz /* Center term */ 2049d6685f55SMatthew G. Knepley func(&beta, ctx, &lval); 2050b3c0f97bSTom Klotz sum = 0.5*alpha*PETSC_PI*lval; 2051b3c0f97bSTom Klotz /* */ 2052b3c0f97bSTom Klotz do { 2053b3c0f97bSTom Klotz PetscReal lterm, rterm, maxTerm = 0.0, d1, d2, d3, d4; 2054b3c0f97bSTom Klotz PetscInt k = 1; 2055b3c0f97bSTom Klotz 2056b3c0f97bSTom Klotz ++l; 2057b3c0f97bSTom Klotz /* PetscPrintf(PETSC_COMM_SELF, "LEVEL %D sum: %15.15f\n", l, sum); */ 2058b3c0f97bSTom Klotz /* At each level of refinement, h --> h/2 and sum --> sum/2 */ 2059b3c0f97bSTom Klotz psum = osum; 2060b3c0f97bSTom Klotz osum = sum; 2061b3c0f97bSTom Klotz h *= 0.5; 2062b3c0f97bSTom Klotz sum *= 0.5; 2063b3c0f97bSTom Klotz do { 20649add2064SThomas Klotz wk = 0.5*h*PETSC_PI*PetscCoshReal(k*h)/PetscSqr(PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(k*h))); 2065446c295cSMatthew G. Knepley yk = 1.0/(PetscExpReal(0.5*PETSC_PI*PetscSinhReal(k*h)) * PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(k*h))); 2066446c295cSMatthew G. Knepley lx = -alpha*(1.0 - yk)+beta; 2067446c295cSMatthew G. Knepley rx = alpha*(1.0 - yk)+beta; 2068d6685f55SMatthew G. Knepley func(&lx, ctx, &lval); 2069d6685f55SMatthew G. Knepley func(&rx, ctx, &rval); 2070b3c0f97bSTom Klotz lterm = alpha*wk*lval; 2071b3c0f97bSTom Klotz maxTerm = PetscMax(PetscAbsReal(lterm), maxTerm); 2072b3c0f97bSTom Klotz sum += lterm; 2073b3c0f97bSTom Klotz rterm = alpha*wk*rval; 2074b3c0f97bSTom Klotz maxTerm = PetscMax(PetscAbsReal(rterm), maxTerm); 2075b3c0f97bSTom Klotz sum += rterm; 2076b3c0f97bSTom Klotz ++k; 2077b3c0f97bSTom Klotz /* Only need to evaluate every other point on refined levels */ 2078b3c0f97bSTom Klotz if (l != 1) ++k; 20799add2064SThomas Klotz } while (PetscAbsReal(PetscLog10Real(wk)) < p); /* Only need to evaluate sum until weights are < 32 digits of precision */ 2080b3c0f97bSTom Klotz 2081b3c0f97bSTom Klotz d1 = PetscLog10Real(PetscAbsReal(sum - osum)); 2082b3c0f97bSTom Klotz d2 = PetscLog10Real(PetscAbsReal(sum - psum)); 2083b3c0f97bSTom Klotz d3 = PetscLog10Real(maxTerm) - p; 208409d48545SBarry Smith if (PetscMax(PetscAbsReal(lterm), PetscAbsReal(rterm)) == 0.0) d4 = 0.0; 208509d48545SBarry Smith else d4 = PetscLog10Real(PetscMax(PetscAbsReal(lterm), PetscAbsReal(rterm))); 2086b3c0f97bSTom Klotz d = PetscAbsInt(PetscMin(0, PetscMax(PetscMax(PetscMax(PetscSqr(d1)/d2, 2*d1), d3), d4))); 20879add2064SThomas Klotz } while (d < digits && l < 12); 2088b3c0f97bSTom Klotz *sol = sum; 2089e510cb1fSThomas Klotz 2090b3c0f97bSTom Klotz PetscFunctionReturn(0); 2091b3c0f97bSTom Klotz } 2092b3c0f97bSTom Klotz 2093497880caSRichard Tran Mills #if defined(PETSC_HAVE_MPFR) 2094d6685f55SMatthew G. Knepley PetscErrorCode PetscDTTanhSinhIntegrateMPFR(void (*func)(const PetscReal[], void *, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, void *ctx, PetscReal *sol) 209529f144ccSMatthew G. Knepley { 2096e510cb1fSThomas Klotz const PetscInt safetyFactor = 2; /* Calculate abcissa until 2*p digits */ 209729f144ccSMatthew G. Knepley PetscInt l = 0; /* Level of refinement, h = 2^{-l} */ 209829f144ccSMatthew G. Knepley mpfr_t alpha; /* Half-width of the integration interval */ 209929f144ccSMatthew G. Knepley mpfr_t beta; /* Center of the integration interval */ 210029f144ccSMatthew G. Knepley mpfr_t h; /* Step size, length between x_k */ 210129f144ccSMatthew G. Knepley mpfr_t osum; /* Integral on last level */ 210229f144ccSMatthew G. Knepley mpfr_t psum; /* Integral on the level before the last level */ 210329f144ccSMatthew G. Knepley mpfr_t sum; /* Integral on current level */ 210429f144ccSMatthew G. Knepley mpfr_t yk; /* Quadrature point 1 - x_k on reference domain [-1, 1] */ 210529f144ccSMatthew G. Knepley mpfr_t lx, rx; /* Quadrature points to the left and right of 0 on the real domain [a, b] */ 210629f144ccSMatthew G. Knepley mpfr_t wk; /* Quadrature weight at x_k */ 2107*1fbc92bbSMatthew G. Knepley PetscReal lval, rval, rtmp; /* Terms in the quadature sum to the left and right of 0 */ 210829f144ccSMatthew G. Knepley PetscInt d; /* Digits of precision in the integral */ 210929f144ccSMatthew G. Knepley mpfr_t pi2, kh, msinh, mcosh, maxTerm, curTerm, tmp; 211029f144ccSMatthew G. Knepley 211129f144ccSMatthew G. Knepley PetscFunctionBegin; 21122c71b3e2SJacob Faibussowitsch PetscCheckFalse(digits <= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a positive number of significant digits"); 211329f144ccSMatthew G. Knepley /* Create high precision storage */ 2114c9f744b5SMatthew 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); 211529f144ccSMatthew G. Knepley /* Initialization */ 211629f144ccSMatthew G. Knepley mpfr_set_d(alpha, 0.5*(b-a), MPFR_RNDN); 211729f144ccSMatthew G. Knepley mpfr_set_d(beta, 0.5*(b+a), MPFR_RNDN); 211829f144ccSMatthew G. Knepley mpfr_set_d(osum, 0.0, MPFR_RNDN); 211929f144ccSMatthew G. Knepley mpfr_set_d(psum, 0.0, MPFR_RNDN); 212029f144ccSMatthew G. Knepley mpfr_set_d(h, 1.0, MPFR_RNDN); 212129f144ccSMatthew G. Knepley mpfr_const_pi(pi2, MPFR_RNDN); 212229f144ccSMatthew G. Knepley mpfr_mul_d(pi2, pi2, 0.5, MPFR_RNDN); 212329f144ccSMatthew G. Knepley /* Center term */ 2124*1fbc92bbSMatthew G. Knepley rtmp = 0.5*(b+a); 2125*1fbc92bbSMatthew G. Knepley func(&rtmp, ctx, &lval); 212629f144ccSMatthew G. Knepley mpfr_set(sum, pi2, MPFR_RNDN); 212729f144ccSMatthew G. Knepley mpfr_mul(sum, sum, alpha, MPFR_RNDN); 212829f144ccSMatthew G. Knepley mpfr_mul_d(sum, sum, lval, MPFR_RNDN); 212929f144ccSMatthew G. Knepley /* */ 213029f144ccSMatthew G. Knepley do { 213129f144ccSMatthew G. Knepley PetscReal d1, d2, d3, d4; 213229f144ccSMatthew G. Knepley PetscInt k = 1; 213329f144ccSMatthew G. Knepley 213429f144ccSMatthew G. Knepley ++l; 213529f144ccSMatthew G. Knepley mpfr_set_d(maxTerm, 0.0, MPFR_RNDN); 213629f144ccSMatthew G. Knepley /* PetscPrintf(PETSC_COMM_SELF, "LEVEL %D sum: %15.15f\n", l, sum); */ 213729f144ccSMatthew G. Knepley /* At each level of refinement, h --> h/2 and sum --> sum/2 */ 213829f144ccSMatthew G. Knepley mpfr_set(psum, osum, MPFR_RNDN); 213929f144ccSMatthew G. Knepley mpfr_set(osum, sum, MPFR_RNDN); 214029f144ccSMatthew G. Knepley mpfr_mul_d(h, h, 0.5, MPFR_RNDN); 214129f144ccSMatthew G. Knepley mpfr_mul_d(sum, sum, 0.5, MPFR_RNDN); 214229f144ccSMatthew G. Knepley do { 214329f144ccSMatthew G. Knepley mpfr_set_si(kh, k, MPFR_RNDN); 214429f144ccSMatthew G. Knepley mpfr_mul(kh, kh, h, MPFR_RNDN); 214529f144ccSMatthew G. Knepley /* Weight */ 214629f144ccSMatthew G. Knepley mpfr_set(wk, h, MPFR_RNDN); 214729f144ccSMatthew G. Knepley mpfr_sinh_cosh(msinh, mcosh, kh, MPFR_RNDN); 214829f144ccSMatthew G. Knepley mpfr_mul(msinh, msinh, pi2, MPFR_RNDN); 214929f144ccSMatthew G. Knepley mpfr_mul(mcosh, mcosh, pi2, MPFR_RNDN); 215029f144ccSMatthew G. Knepley mpfr_cosh(tmp, msinh, MPFR_RNDN); 215129f144ccSMatthew G. Knepley mpfr_sqr(tmp, tmp, MPFR_RNDN); 215229f144ccSMatthew G. Knepley mpfr_mul(wk, wk, mcosh, MPFR_RNDN); 215329f144ccSMatthew G. Knepley mpfr_div(wk, wk, tmp, MPFR_RNDN); 215429f144ccSMatthew G. Knepley /* Abscissa */ 215529f144ccSMatthew G. Knepley mpfr_set_d(yk, 1.0, MPFR_RNDZ); 215629f144ccSMatthew G. Knepley mpfr_cosh(tmp, msinh, MPFR_RNDN); 215729f144ccSMatthew G. Knepley mpfr_div(yk, yk, tmp, MPFR_RNDZ); 215829f144ccSMatthew G. Knepley mpfr_exp(tmp, msinh, MPFR_RNDN); 215929f144ccSMatthew G. Knepley mpfr_div(yk, yk, tmp, MPFR_RNDZ); 216029f144ccSMatthew G. Knepley /* Quadrature points */ 216129f144ccSMatthew G. Knepley mpfr_sub_d(lx, yk, 1.0, MPFR_RNDZ); 216229f144ccSMatthew G. Knepley mpfr_mul(lx, lx, alpha, MPFR_RNDU); 216329f144ccSMatthew G. Knepley mpfr_add(lx, lx, beta, MPFR_RNDU); 216429f144ccSMatthew G. Knepley mpfr_d_sub(rx, 1.0, yk, MPFR_RNDZ); 216529f144ccSMatthew G. Knepley mpfr_mul(rx, rx, alpha, MPFR_RNDD); 216629f144ccSMatthew G. Knepley mpfr_add(rx, rx, beta, MPFR_RNDD); 216729f144ccSMatthew G. Knepley /* Evaluation */ 2168*1fbc92bbSMatthew G. Knepley rtmp = mpfr_get_d(lx, MPFR_RNDU); 2169*1fbc92bbSMatthew G. Knepley func(&rtmp, ctx, &lval); 2170*1fbc92bbSMatthew G. Knepley rtmp = mpfr_get_d(rx, MPFR_RNDD); 2171*1fbc92bbSMatthew G. Knepley func(&rtmp, ctx, &rval); 217229f144ccSMatthew G. Knepley /* Update */ 217329f144ccSMatthew G. Knepley mpfr_mul(tmp, wk, alpha, MPFR_RNDN); 217429f144ccSMatthew G. Knepley mpfr_mul_d(tmp, tmp, lval, MPFR_RNDN); 217529f144ccSMatthew G. Knepley mpfr_add(sum, sum, tmp, MPFR_RNDN); 217629f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 217729f144ccSMatthew G. Knepley mpfr_max(maxTerm, maxTerm, tmp, MPFR_RNDN); 217829f144ccSMatthew G. Knepley mpfr_set(curTerm, tmp, MPFR_RNDN); 217929f144ccSMatthew G. Knepley mpfr_mul(tmp, wk, alpha, MPFR_RNDN); 218029f144ccSMatthew G. Knepley mpfr_mul_d(tmp, tmp, rval, MPFR_RNDN); 218129f144ccSMatthew G. Knepley mpfr_add(sum, sum, tmp, MPFR_RNDN); 218229f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 218329f144ccSMatthew G. Knepley mpfr_max(maxTerm, maxTerm, tmp, MPFR_RNDN); 218429f144ccSMatthew G. Knepley mpfr_max(curTerm, curTerm, tmp, MPFR_RNDN); 218529f144ccSMatthew G. Knepley ++k; 218629f144ccSMatthew G. Knepley /* Only need to evaluate every other point on refined levels */ 218729f144ccSMatthew G. Knepley if (l != 1) ++k; 218829f144ccSMatthew G. Knepley mpfr_log10(tmp, wk, MPFR_RNDN); 218929f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 2190c9f744b5SMatthew G. Knepley } while (mpfr_get_d(tmp, MPFR_RNDN) < safetyFactor*digits); /* Only need to evaluate sum until weights are < 32 digits of precision */ 219129f144ccSMatthew G. Knepley mpfr_sub(tmp, sum, osum, MPFR_RNDN); 219229f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 219329f144ccSMatthew G. Knepley mpfr_log10(tmp, tmp, MPFR_RNDN); 219429f144ccSMatthew G. Knepley d1 = mpfr_get_d(tmp, MPFR_RNDN); 219529f144ccSMatthew G. Knepley mpfr_sub(tmp, sum, psum, MPFR_RNDN); 219629f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 219729f144ccSMatthew G. Knepley mpfr_log10(tmp, tmp, MPFR_RNDN); 219829f144ccSMatthew G. Knepley d2 = mpfr_get_d(tmp, MPFR_RNDN); 219929f144ccSMatthew G. Knepley mpfr_log10(tmp, maxTerm, MPFR_RNDN); 2200c9f744b5SMatthew G. Knepley d3 = mpfr_get_d(tmp, MPFR_RNDN) - digits; 220129f144ccSMatthew G. Knepley mpfr_log10(tmp, curTerm, MPFR_RNDN); 220229f144ccSMatthew G. Knepley d4 = mpfr_get_d(tmp, MPFR_RNDN); 220329f144ccSMatthew G. Knepley d = PetscAbsInt(PetscMin(0, PetscMax(PetscMax(PetscMax(PetscSqr(d1)/d2, 2*d1), d3), d4))); 2204b0649871SThomas Klotz } while (d < digits && l < 8); 220529f144ccSMatthew G. Knepley *sol = mpfr_get_d(sum, MPFR_RNDN); 220629f144ccSMatthew G. Knepley /* Cleanup */ 220729f144ccSMatthew G. Knepley mpfr_clears(alpha, beta, h, sum, osum, psum, yk, wk, lx, rx, tmp, maxTerm, curTerm, pi2, kh, msinh, mcosh, NULL); 220829f144ccSMatthew G. Knepley PetscFunctionReturn(0); 220929f144ccSMatthew G. Knepley } 2210d525116cSMatthew G. Knepley #else 2211fbfcfee5SBarry Smith 2212d6685f55SMatthew G. Knepley PetscErrorCode PetscDTTanhSinhIntegrateMPFR(void (*func)(const PetscReal[], void *, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, void *ctx, PetscReal *sol) 2213d525116cSMatthew G. Knepley { 2214d525116cSMatthew G. Knepley SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "This method will not work without MPFR. Reconfigure using --download-mpfr --download-gmp"); 2215d525116cSMatthew G. Knepley } 221629f144ccSMatthew G. Knepley #endif 221729f144ccSMatthew G. Knepley 22182df84da0SMatthew G. Knepley /*@ 22192df84da0SMatthew G. Knepley PetscDTTensorQuadratureCreate - create the tensor product quadrature from two lower-dimensional quadratures 22202df84da0SMatthew G. Knepley 22212df84da0SMatthew G. Knepley Not Collective 22222df84da0SMatthew G. Knepley 22232df84da0SMatthew G. Knepley Input Parameters: 22242df84da0SMatthew G. Knepley + q1 - The first quadrature 22252df84da0SMatthew G. Knepley - q2 - The second quadrature 22262df84da0SMatthew G. Knepley 22272df84da0SMatthew G. Knepley Output Parameter: 22282df84da0SMatthew G. Knepley . q - A PetscQuadrature object 22292df84da0SMatthew G. Knepley 22302df84da0SMatthew G. Knepley Level: intermediate 22312df84da0SMatthew G. Knepley 22322df84da0SMatthew G. Knepley .seealso: PetscDTGaussTensorQuadrature() 22332df84da0SMatthew G. Knepley @*/ 22342df84da0SMatthew G. Knepley PetscErrorCode PetscDTTensorQuadratureCreate(PetscQuadrature q1, PetscQuadrature q2, PetscQuadrature *q) 22352df84da0SMatthew G. Knepley { 22362df84da0SMatthew G. Knepley const PetscReal *x1, *w1, *x2, *w2; 22372df84da0SMatthew G. Knepley PetscReal *x, *w; 22382df84da0SMatthew G. Knepley PetscInt dim1, Nc1, Np1, order1, qa, d1; 22392df84da0SMatthew G. Knepley PetscInt dim2, Nc2, Np2, order2, qb, d2; 22402df84da0SMatthew G. Knepley PetscInt dim, Nc, Np, order, qc, d; 22412df84da0SMatthew G. Knepley PetscErrorCode ierr; 22422df84da0SMatthew G. Knepley 22432df84da0SMatthew G. Knepley PetscFunctionBegin; 22442df84da0SMatthew G. Knepley PetscValidHeaderSpecific(q1, PETSCQUADRATURE_CLASSID, 1); 22452df84da0SMatthew G. Knepley PetscValidHeaderSpecific(q2, PETSCQUADRATURE_CLASSID, 2); 22462df84da0SMatthew G. Knepley PetscValidPointer(q, 3); 22472df84da0SMatthew G. Knepley ierr = PetscQuadratureGetOrder(q1, &order1);CHKERRQ(ierr); 22482df84da0SMatthew G. Knepley ierr = PetscQuadratureGetOrder(q2, &order2);CHKERRQ(ierr); 22492df84da0SMatthew G. Knepley PetscCheck(order1 == order2, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Order1 %" PetscInt_FMT " != %" PetscInt_FMT " Order2", order1, order2); 22502df84da0SMatthew G. Knepley ierr = PetscQuadratureGetData(q1, &dim1, &Nc1, &Np1, &x1, &w1);CHKERRQ(ierr); 22512df84da0SMatthew G. Knepley ierr = PetscQuadratureGetData(q2, &dim2, &Nc2, &Np2, &x2, &w2);CHKERRQ(ierr); 22522df84da0SMatthew G. Knepley PetscCheck(Nc1 == Nc2, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "NumComp1 %" PetscInt_FMT " != %" PetscInt_FMT " NumComp2", Nc1, Nc2); 22532df84da0SMatthew G. Knepley 22542df84da0SMatthew G. Knepley dim = dim1 + dim2; 22552df84da0SMatthew G. Knepley Nc = Nc1; 22562df84da0SMatthew G. Knepley Np = Np1 * Np2; 22572df84da0SMatthew G. Knepley order = order1; 22582df84da0SMatthew G. Knepley ierr = PetscQuadratureCreate(PETSC_COMM_SELF, q);CHKERRQ(ierr); 22592df84da0SMatthew G. Knepley ierr = PetscQuadratureSetOrder(*q, order);CHKERRQ(ierr); 22602df84da0SMatthew G. Knepley ierr = PetscMalloc1(Np*dim, &x);CHKERRQ(ierr); 22612df84da0SMatthew G. Knepley ierr = PetscMalloc1(Np, &w);CHKERRQ(ierr); 22622df84da0SMatthew G. Knepley for (qa = 0, qc = 0; qa < Np1; ++qa) { 22632df84da0SMatthew G. Knepley for (qb = 0; qb < Np2; ++qb, ++qc) { 22642df84da0SMatthew G. Knepley for (d1 = 0, d = 0; d1 < dim1; ++d1, ++d) { 22652df84da0SMatthew G. Knepley x[qc*dim+d] = x1[qa*dim1+d1]; 22662df84da0SMatthew G. Knepley } 22672df84da0SMatthew G. Knepley for (d2 = 0; d2 < dim2; ++d2, ++d) { 22682df84da0SMatthew G. Knepley x[qc*dim+d] = x2[qb*dim2+d2]; 22692df84da0SMatthew G. Knepley } 22702df84da0SMatthew G. Knepley w[qc] = w1[qa] * w2[qb]; 22712df84da0SMatthew G. Knepley } 22722df84da0SMatthew G. Knepley } 22732df84da0SMatthew G. Knepley ierr = PetscQuadratureSetData(*q, dim, Nc, Np, x, w);CHKERRQ(ierr); 22742df84da0SMatthew G. Knepley PetscFunctionReturn(0); 22752df84da0SMatthew G. Knepley } 22762df84da0SMatthew G. Knepley 2277194825f6SJed Brown /* Overwrites A. Can only handle full-rank problems with m>=n 2278194825f6SJed Brown * A in column-major format 2279194825f6SJed Brown * Ainv in row-major format 2280194825f6SJed Brown * tau has length m 2281194825f6SJed Brown * worksize must be >= max(1,n) 2282194825f6SJed Brown */ 2283194825f6SJed Brown static PetscErrorCode PetscDTPseudoInverseQR(PetscInt m,PetscInt mstride,PetscInt n,PetscReal *A_in,PetscReal *Ainv_out,PetscScalar *tau,PetscInt worksize,PetscScalar *work) 2284194825f6SJed Brown { 2285194825f6SJed Brown PetscErrorCode ierr; 2286194825f6SJed Brown PetscBLASInt M,N,K,lda,ldb,ldwork,info; 2287194825f6SJed Brown PetscScalar *A,*Ainv,*R,*Q,Alpha; 2288194825f6SJed Brown 2289194825f6SJed Brown PetscFunctionBegin; 2290194825f6SJed Brown #if defined(PETSC_USE_COMPLEX) 2291194825f6SJed Brown { 2292194825f6SJed Brown PetscInt i,j; 2293dcca6d9dSJed Brown ierr = PetscMalloc2(m*n,&A,m*n,&Ainv);CHKERRQ(ierr); 2294194825f6SJed Brown for (j=0; j<n; j++) { 2295194825f6SJed Brown for (i=0; i<m; i++) A[i+m*j] = A_in[i+mstride*j]; 2296194825f6SJed Brown } 2297194825f6SJed Brown mstride = m; 2298194825f6SJed Brown } 2299194825f6SJed Brown #else 2300194825f6SJed Brown A = A_in; 2301194825f6SJed Brown Ainv = Ainv_out; 2302194825f6SJed Brown #endif 2303194825f6SJed Brown 2304194825f6SJed Brown ierr = PetscBLASIntCast(m,&M);CHKERRQ(ierr); 2305194825f6SJed Brown ierr = PetscBLASIntCast(n,&N);CHKERRQ(ierr); 2306194825f6SJed Brown ierr = PetscBLASIntCast(mstride,&lda);CHKERRQ(ierr); 2307194825f6SJed Brown ierr = PetscBLASIntCast(worksize,&ldwork);CHKERRQ(ierr); 2308194825f6SJed Brown ierr = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr); 2309001a771dSBarry Smith PetscStackCallBLAS("LAPACKgeqrf",LAPACKgeqrf_(&M,&N,A,&lda,tau,work,&ldwork,&info)); 2310194825f6SJed Brown ierr = PetscFPTrapPop();CHKERRQ(ierr); 23112c71b3e2SJacob Faibussowitsch PetscCheckFalse(info,PETSC_COMM_SELF,PETSC_ERR_LIB,"xGEQRF error"); 2312194825f6SJed Brown R = A; /* Upper triangular part of A now contains R, the rest contains the elementary reflectors */ 2313194825f6SJed Brown 2314194825f6SJed Brown /* Extract an explicit representation of Q */ 2315194825f6SJed Brown Q = Ainv; 2316580bdb30SBarry Smith ierr = PetscArraycpy(Q,A,mstride*n);CHKERRQ(ierr); 2317194825f6SJed Brown K = N; /* full rank */ 2318c964aadfSJose E. Roman PetscStackCallBLAS("LAPACKorgqr",LAPACKorgqr_(&M,&N,&K,Q,&lda,tau,work,&ldwork,&info)); 23192c71b3e2SJacob Faibussowitsch PetscCheckFalse(info,PETSC_COMM_SELF,PETSC_ERR_LIB,"xORGQR/xUNGQR error"); 2320194825f6SJed Brown 2321194825f6SJed Brown /* Compute A^{-T} = (R^{-1} Q^T)^T = Q R^{-T} */ 2322194825f6SJed Brown Alpha = 1.0; 2323194825f6SJed Brown ldb = lda; 2324001a771dSBarry Smith PetscStackCallBLAS("BLAStrsm",BLAStrsm_("Right","Upper","ConjugateTranspose","NotUnitTriangular",&M,&N,&Alpha,R,&lda,Q,&ldb)); 2325194825f6SJed Brown /* Ainv is Q, overwritten with inverse */ 2326194825f6SJed Brown 2327194825f6SJed Brown #if defined(PETSC_USE_COMPLEX) 2328194825f6SJed Brown { 2329194825f6SJed Brown PetscInt i; 2330194825f6SJed Brown for (i=0; i<m*n; i++) Ainv_out[i] = PetscRealPart(Ainv[i]); 2331194825f6SJed Brown ierr = PetscFree2(A,Ainv);CHKERRQ(ierr); 2332194825f6SJed Brown } 2333194825f6SJed Brown #endif 2334194825f6SJed Brown PetscFunctionReturn(0); 2335194825f6SJed Brown } 2336194825f6SJed Brown 2337194825f6SJed Brown /* Computes integral of L_p' over intervals {(x0,x1),(x1,x2),...} */ 2338194825f6SJed Brown static PetscErrorCode PetscDTLegendreIntegrate(PetscInt ninterval,const PetscReal *x,PetscInt ndegree,const PetscInt *degrees,PetscBool Transpose,PetscReal *B) 2339194825f6SJed Brown { 2340194825f6SJed Brown PetscErrorCode ierr; 2341194825f6SJed Brown PetscReal *Bv; 2342194825f6SJed Brown PetscInt i,j; 2343194825f6SJed Brown 2344194825f6SJed Brown PetscFunctionBegin; 2345785e854fSJed Brown ierr = PetscMalloc1((ninterval+1)*ndegree,&Bv);CHKERRQ(ierr); 2346194825f6SJed Brown /* Point evaluation of L_p on all the source vertices */ 2347194825f6SJed Brown ierr = PetscDTLegendreEval(ninterval+1,x,ndegree,degrees,Bv,NULL,NULL);CHKERRQ(ierr); 2348194825f6SJed Brown /* Integral over each interval: \int_a^b L_p' = L_p(b)-L_p(a) */ 2349194825f6SJed Brown for (i=0; i<ninterval; i++) { 2350194825f6SJed Brown for (j=0; j<ndegree; j++) { 2351194825f6SJed Brown if (Transpose) B[i+ninterval*j] = Bv[(i+1)*ndegree+j] - Bv[i*ndegree+j]; 2352194825f6SJed Brown else B[i*ndegree+j] = Bv[(i+1)*ndegree+j] - Bv[i*ndegree+j]; 2353194825f6SJed Brown } 2354194825f6SJed Brown } 2355194825f6SJed Brown ierr = PetscFree(Bv);CHKERRQ(ierr); 2356194825f6SJed Brown PetscFunctionReturn(0); 2357194825f6SJed Brown } 2358194825f6SJed Brown 2359194825f6SJed Brown /*@ 2360194825f6SJed Brown PetscDTReconstructPoly - create matrix representing polynomial reconstruction using cell intervals and evaluation at target intervals 2361194825f6SJed Brown 2362194825f6SJed Brown Not Collective 2363194825f6SJed Brown 23644165533cSJose E. Roman Input Parameters: 2365194825f6SJed Brown + degree - degree of reconstruction polynomial 2366194825f6SJed Brown . nsource - number of source intervals 2367194825f6SJed Brown . sourcex - sorted coordinates of source cell boundaries (length nsource+1) 2368194825f6SJed Brown . ntarget - number of target intervals 2369194825f6SJed Brown - targetx - sorted coordinates of target cell boundaries (length ntarget+1) 2370194825f6SJed Brown 23714165533cSJose E. Roman Output Parameter: 2372194825f6SJed Brown . R - reconstruction matrix, utarget = sum_s R[t*nsource+s] * usource[s] 2373194825f6SJed Brown 2374194825f6SJed Brown Level: advanced 2375194825f6SJed Brown 2376194825f6SJed Brown .seealso: PetscDTLegendreEval() 2377194825f6SJed Brown @*/ 2378194825f6SJed Brown PetscErrorCode PetscDTReconstructPoly(PetscInt degree,PetscInt nsource,const PetscReal *sourcex,PetscInt ntarget,const PetscReal *targetx,PetscReal *R) 2379194825f6SJed Brown { 2380194825f6SJed Brown PetscErrorCode ierr; 2381194825f6SJed Brown PetscInt i,j,k,*bdegrees,worksize; 2382194825f6SJed Brown PetscReal xmin,xmax,center,hscale,*sourcey,*targety,*Bsource,*Bsinv,*Btarget; 2383194825f6SJed Brown PetscScalar *tau,*work; 2384194825f6SJed Brown 2385194825f6SJed Brown PetscFunctionBegin; 2386194825f6SJed Brown PetscValidRealPointer(sourcex,3); 2387194825f6SJed Brown PetscValidRealPointer(targetx,5); 2388194825f6SJed Brown PetscValidRealPointer(R,6); 23892c71b3e2SJacob 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); 239076bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 2391194825f6SJed Brown for (i=0; i<nsource; i++) { 23922c71b3e2SJacob 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]); 2393194825f6SJed Brown } 2394194825f6SJed Brown for (i=0; i<ntarget; i++) { 23952c71b3e2SJacob 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]); 2396194825f6SJed Brown } 239776bd3646SJed Brown } 2398194825f6SJed Brown xmin = PetscMin(sourcex[0],targetx[0]); 2399194825f6SJed Brown xmax = PetscMax(sourcex[nsource],targetx[ntarget]); 2400194825f6SJed Brown center = (xmin + xmax)/2; 2401194825f6SJed Brown hscale = (xmax - xmin)/2; 2402194825f6SJed Brown worksize = nsource; 2403dcca6d9dSJed Brown ierr = PetscMalloc4(degree+1,&bdegrees,nsource+1,&sourcey,nsource*(degree+1),&Bsource,worksize,&work);CHKERRQ(ierr); 2404dcca6d9dSJed Brown ierr = PetscMalloc4(nsource,&tau,nsource*(degree+1),&Bsinv,ntarget+1,&targety,ntarget*(degree+1),&Btarget);CHKERRQ(ierr); 2405194825f6SJed Brown for (i=0; i<=nsource; i++) sourcey[i] = (sourcex[i]-center)/hscale; 2406194825f6SJed Brown for (i=0; i<=degree; i++) bdegrees[i] = i+1; 2407194825f6SJed Brown ierr = PetscDTLegendreIntegrate(nsource,sourcey,degree+1,bdegrees,PETSC_TRUE,Bsource);CHKERRQ(ierr); 2408194825f6SJed Brown ierr = PetscDTPseudoInverseQR(nsource,nsource,degree+1,Bsource,Bsinv,tau,nsource,work);CHKERRQ(ierr); 2409194825f6SJed Brown for (i=0; i<=ntarget; i++) targety[i] = (targetx[i]-center)/hscale; 2410194825f6SJed Brown ierr = PetscDTLegendreIntegrate(ntarget,targety,degree+1,bdegrees,PETSC_FALSE,Btarget);CHKERRQ(ierr); 2411194825f6SJed Brown for (i=0; i<ntarget; i++) { 2412194825f6SJed Brown PetscReal rowsum = 0; 2413194825f6SJed Brown for (j=0; j<nsource; j++) { 2414194825f6SJed Brown PetscReal sum = 0; 2415194825f6SJed Brown for (k=0; k<degree+1; k++) { 2416194825f6SJed Brown sum += Btarget[i*(degree+1)+k] * Bsinv[k*nsource+j]; 2417194825f6SJed Brown } 2418194825f6SJed Brown R[i*nsource+j] = sum; 2419194825f6SJed Brown rowsum += sum; 2420194825f6SJed Brown } 2421194825f6SJed Brown for (j=0; j<nsource; j++) R[i*nsource+j] /= rowsum; /* normalize each row */ 2422194825f6SJed Brown } 2423194825f6SJed Brown ierr = PetscFree4(bdegrees,sourcey,Bsource,work);CHKERRQ(ierr); 2424194825f6SJed Brown ierr = PetscFree4(tau,Bsinv,targety,Btarget);CHKERRQ(ierr); 2425194825f6SJed Brown PetscFunctionReturn(0); 2426194825f6SJed Brown } 2427916e780bShannah_mairs 2428916e780bShannah_mairs /*@C 2429916e780bShannah_mairs PetscGaussLobattoLegendreIntegrate - Compute the L2 integral of a function on the GLL points 2430916e780bShannah_mairs 2431916e780bShannah_mairs Not Collective 2432916e780bShannah_mairs 2433d8d19677SJose E. Roman Input Parameters: 2434916e780bShannah_mairs + n - the number of GLL nodes 2435916e780bShannah_mairs . nodes - the GLL nodes 2436916e780bShannah_mairs . weights - the GLL weights 2437f0fc11ceSJed Brown - f - the function values at the nodes 2438916e780bShannah_mairs 2439916e780bShannah_mairs Output Parameter: 2440916e780bShannah_mairs . in - the value of the integral 2441916e780bShannah_mairs 2442916e780bShannah_mairs Level: beginner 2443916e780bShannah_mairs 2444916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature() 2445916e780bShannah_mairs 2446916e780bShannah_mairs @*/ 2447916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreIntegrate(PetscInt n,PetscReal *nodes,PetscReal *weights,const PetscReal *f,PetscReal *in) 2448916e780bShannah_mairs { 2449916e780bShannah_mairs PetscInt i; 2450916e780bShannah_mairs 2451916e780bShannah_mairs PetscFunctionBegin; 2452916e780bShannah_mairs *in = 0.; 2453916e780bShannah_mairs for (i=0; i<n; i++) { 2454916e780bShannah_mairs *in += f[i]*f[i]*weights[i]; 2455916e780bShannah_mairs } 2456916e780bShannah_mairs PetscFunctionReturn(0); 2457916e780bShannah_mairs } 2458916e780bShannah_mairs 2459916e780bShannah_mairs /*@C 2460916e780bShannah_mairs PetscGaussLobattoLegendreElementLaplacianCreate - computes the Laplacian for a single 1d GLL element 2461916e780bShannah_mairs 2462916e780bShannah_mairs Not Collective 2463916e780bShannah_mairs 2464d8d19677SJose E. Roman Input Parameters: 2465916e780bShannah_mairs + n - the number of GLL nodes 2466916e780bShannah_mairs . nodes - the GLL nodes 2467f0fc11ceSJed Brown - weights - the GLL weights 2468916e780bShannah_mairs 2469916e780bShannah_mairs Output Parameter: 2470916e780bShannah_mairs . A - the stiffness element 2471916e780bShannah_mairs 2472916e780bShannah_mairs Level: beginner 2473916e780bShannah_mairs 2474916e780bShannah_mairs Notes: 2475916e780bShannah_mairs Destroy this with PetscGaussLobattoLegendreElementLaplacianDestroy() 2476916e780bShannah_mairs 2477916e780bShannah_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) 2478916e780bShannah_mairs 2479916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianDestroy() 2480916e780bShannah_mairs 2481916e780bShannah_mairs @*/ 2482916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementLaplacianCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2483916e780bShannah_mairs { 2484916e780bShannah_mairs PetscReal **A; 2485916e780bShannah_mairs PetscErrorCode ierr; 2486916e780bShannah_mairs const PetscReal *gllnodes = nodes; 2487916e780bShannah_mairs const PetscInt p = n-1; 2488916e780bShannah_mairs PetscReal z0,z1,z2 = -1,x,Lpj,Lpr; 2489916e780bShannah_mairs PetscInt i,j,nn,r; 2490916e780bShannah_mairs 2491916e780bShannah_mairs PetscFunctionBegin; 2492916e780bShannah_mairs ierr = PetscMalloc1(n,&A);CHKERRQ(ierr); 2493916e780bShannah_mairs ierr = PetscMalloc1(n*n,&A[0]);CHKERRQ(ierr); 2494916e780bShannah_mairs for (i=1; i<n; i++) A[i] = A[i-1]+n; 2495916e780bShannah_mairs 2496916e780bShannah_mairs for (j=1; j<p; j++) { 2497916e780bShannah_mairs x = gllnodes[j]; 2498916e780bShannah_mairs z0 = 1.; 2499916e780bShannah_mairs z1 = x; 2500916e780bShannah_mairs for (nn=1; nn<p; nn++) { 2501916e780bShannah_mairs z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.)); 2502916e780bShannah_mairs z0 = z1; 2503916e780bShannah_mairs z1 = z2; 2504916e780bShannah_mairs } 2505916e780bShannah_mairs Lpj=z2; 2506916e780bShannah_mairs for (r=1; r<p; r++) { 2507916e780bShannah_mairs if (r == j) { 2508916e780bShannah_mairs A[j][j]=2./(3.*(1.-gllnodes[j]*gllnodes[j])*Lpj*Lpj); 2509916e780bShannah_mairs } else { 2510916e780bShannah_mairs x = gllnodes[r]; 2511916e780bShannah_mairs z0 = 1.; 2512916e780bShannah_mairs z1 = x; 2513916e780bShannah_mairs for (nn=1; nn<p; nn++) { 2514916e780bShannah_mairs z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.)); 2515916e780bShannah_mairs z0 = z1; 2516916e780bShannah_mairs z1 = z2; 2517916e780bShannah_mairs } 2518916e780bShannah_mairs Lpr = z2; 2519916e780bShannah_mairs A[r][j] = 4./(((PetscReal)p)*(((PetscReal)p)+1.)*Lpj*Lpr*(gllnodes[j]-gllnodes[r])*(gllnodes[j]-gllnodes[r])); 2520916e780bShannah_mairs } 2521916e780bShannah_mairs } 2522916e780bShannah_mairs } 2523916e780bShannah_mairs for (j=1; j<p+1; j++) { 2524916e780bShannah_mairs x = gllnodes[j]; 2525916e780bShannah_mairs z0 = 1.; 2526916e780bShannah_mairs z1 = x; 2527916e780bShannah_mairs for (nn=1; nn<p; nn++) { 2528916e780bShannah_mairs z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.)); 2529916e780bShannah_mairs z0 = z1; 2530916e780bShannah_mairs z1 = z2; 2531916e780bShannah_mairs } 2532916e780bShannah_mairs Lpj = z2; 2533916e780bShannah_mairs A[j][0] = 4.*PetscPowRealInt(-1.,p)/(((PetscReal)p)*(((PetscReal)p)+1.)*Lpj*(1.+gllnodes[j])*(1.+gllnodes[j])); 2534916e780bShannah_mairs A[0][j] = A[j][0]; 2535916e780bShannah_mairs } 2536916e780bShannah_mairs for (j=0; j<p; j++) { 2537916e780bShannah_mairs x = gllnodes[j]; 2538916e780bShannah_mairs z0 = 1.; 2539916e780bShannah_mairs z1 = x; 2540916e780bShannah_mairs for (nn=1; nn<p; nn++) { 2541916e780bShannah_mairs z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.)); 2542916e780bShannah_mairs z0 = z1; 2543916e780bShannah_mairs z1 = z2; 2544916e780bShannah_mairs } 2545916e780bShannah_mairs Lpj=z2; 2546916e780bShannah_mairs 2547916e780bShannah_mairs A[p][j] = 4./(((PetscReal)p)*(((PetscReal)p)+1.)*Lpj*(1.-gllnodes[j])*(1.-gllnodes[j])); 2548916e780bShannah_mairs A[j][p] = A[p][j]; 2549916e780bShannah_mairs } 2550916e780bShannah_mairs A[0][0]=0.5+(((PetscReal)p)*(((PetscReal)p)+1.)-2.)/6.; 2551916e780bShannah_mairs A[p][p]=A[0][0]; 2552916e780bShannah_mairs *AA = A; 2553916e780bShannah_mairs PetscFunctionReturn(0); 2554916e780bShannah_mairs } 2555916e780bShannah_mairs 2556916e780bShannah_mairs /*@C 2557916e780bShannah_mairs PetscGaussLobattoLegendreElementLaplacianDestroy - frees the Laplacian for a single 1d GLL element 2558916e780bShannah_mairs 2559916e780bShannah_mairs Not Collective 2560916e780bShannah_mairs 2561d8d19677SJose E. Roman Input Parameters: 2562916e780bShannah_mairs + n - the number of GLL nodes 2563916e780bShannah_mairs . nodes - the GLL nodes 2564916e780bShannah_mairs . weights - the GLL weightss 2565916e780bShannah_mairs - A - the stiffness element 2566916e780bShannah_mairs 2567916e780bShannah_mairs Level: beginner 2568916e780bShannah_mairs 2569916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianCreate() 2570916e780bShannah_mairs 2571916e780bShannah_mairs @*/ 2572916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementLaplacianDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2573916e780bShannah_mairs { 2574916e780bShannah_mairs PetscErrorCode ierr; 2575916e780bShannah_mairs 2576916e780bShannah_mairs PetscFunctionBegin; 2577916e780bShannah_mairs ierr = PetscFree((*AA)[0]);CHKERRQ(ierr); 2578916e780bShannah_mairs ierr = PetscFree(*AA);CHKERRQ(ierr); 2579916e780bShannah_mairs *AA = NULL; 2580916e780bShannah_mairs PetscFunctionReturn(0); 2581916e780bShannah_mairs } 2582916e780bShannah_mairs 2583916e780bShannah_mairs /*@C 2584916e780bShannah_mairs PetscGaussLobattoLegendreElementGradientCreate - computes the gradient for a single 1d GLL element 2585916e780bShannah_mairs 2586916e780bShannah_mairs Not Collective 2587916e780bShannah_mairs 2588916e780bShannah_mairs Input Parameter: 2589916e780bShannah_mairs + n - the number of GLL nodes 2590916e780bShannah_mairs . nodes - the GLL nodes 2591916e780bShannah_mairs . weights - the GLL weights 2592916e780bShannah_mairs 2593d8d19677SJose E. Roman Output Parameters: 2594916e780bShannah_mairs . AA - the stiffness element 2595916e780bShannah_mairs - AAT - the transpose of AA (pass in NULL if you do not need this array) 2596916e780bShannah_mairs 2597916e780bShannah_mairs Level: beginner 2598916e780bShannah_mairs 2599916e780bShannah_mairs Notes: 2600916e780bShannah_mairs Destroy this with PetscGaussLobattoLegendreElementGradientDestroy() 2601916e780bShannah_mairs 2602916e780bShannah_mairs You can access entries in these arrays with AA[i][j] but in memory it is stored in contiguous memory, row oriented 2603916e780bShannah_mairs 2604916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianDestroy() 2605916e780bShannah_mairs 2606916e780bShannah_mairs @*/ 2607916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementGradientCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA,PetscReal ***AAT) 2608916e780bShannah_mairs { 2609916e780bShannah_mairs PetscReal **A, **AT = NULL; 2610916e780bShannah_mairs PetscErrorCode ierr; 2611916e780bShannah_mairs const PetscReal *gllnodes = nodes; 2612916e780bShannah_mairs const PetscInt p = n-1; 2613e6a796c3SToby Isaac PetscReal Li, Lj,d0; 2614916e780bShannah_mairs PetscInt i,j; 2615916e780bShannah_mairs 2616916e780bShannah_mairs PetscFunctionBegin; 2617916e780bShannah_mairs ierr = PetscMalloc1(n,&A);CHKERRQ(ierr); 2618916e780bShannah_mairs ierr = PetscMalloc1(n*n,&A[0]);CHKERRQ(ierr); 2619916e780bShannah_mairs for (i=1; i<n; i++) A[i] = A[i-1]+n; 2620916e780bShannah_mairs 2621916e780bShannah_mairs if (AAT) { 2622916e780bShannah_mairs ierr = PetscMalloc1(n,&AT);CHKERRQ(ierr); 2623916e780bShannah_mairs ierr = PetscMalloc1(n*n,&AT[0]);CHKERRQ(ierr); 2624916e780bShannah_mairs for (i=1; i<n; i++) AT[i] = AT[i-1]+n; 2625916e780bShannah_mairs } 2626916e780bShannah_mairs 2627916e780bShannah_mairs if (n==1) {A[0][0] = 0.;} 2628916e780bShannah_mairs d0 = (PetscReal)p*((PetscReal)p+1.)/4.; 2629916e780bShannah_mairs for (i=0; i<n; i++) { 2630916e780bShannah_mairs for (j=0; j<n; j++) { 2631916e780bShannah_mairs A[i][j] = 0.; 2632e6a796c3SToby Isaac ierr = PetscDTComputeJacobi(0., 0., p, gllnodes[i], &Li);CHKERRQ(ierr); 2633e6a796c3SToby Isaac ierr = PetscDTComputeJacobi(0., 0., p, gllnodes[j], &Lj);CHKERRQ(ierr); 2634916e780bShannah_mairs if (i!=j) A[i][j] = Li/(Lj*(gllnodes[i]-gllnodes[j])); 2635916e780bShannah_mairs if ((j==i) && (i==0)) A[i][j] = -d0; 2636916e780bShannah_mairs if (j==i && i==p) A[i][j] = d0; 2637916e780bShannah_mairs if (AT) AT[j][i] = A[i][j]; 2638916e780bShannah_mairs } 2639916e780bShannah_mairs } 2640916e780bShannah_mairs if (AAT) *AAT = AT; 2641916e780bShannah_mairs *AA = A; 2642916e780bShannah_mairs PetscFunctionReturn(0); 2643916e780bShannah_mairs } 2644916e780bShannah_mairs 2645916e780bShannah_mairs /*@C 2646916e780bShannah_mairs PetscGaussLobattoLegendreElementGradientDestroy - frees the gradient for a single 1d GLL element obtained with PetscGaussLobattoLegendreElementGradientCreate() 2647916e780bShannah_mairs 2648916e780bShannah_mairs Not Collective 2649916e780bShannah_mairs 2650d8d19677SJose E. Roman Input Parameters: 2651916e780bShannah_mairs + n - the number of GLL nodes 2652916e780bShannah_mairs . nodes - the GLL nodes 2653916e780bShannah_mairs . weights - the GLL weights 2654916e780bShannah_mairs . AA - the stiffness element 2655916e780bShannah_mairs - AAT - the transpose of the element 2656916e780bShannah_mairs 2657916e780bShannah_mairs Level: beginner 2658916e780bShannah_mairs 2659916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianCreate(), PetscGaussLobattoLegendreElementAdvectionCreate() 2660916e780bShannah_mairs 2661916e780bShannah_mairs @*/ 2662916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementGradientDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA,PetscReal ***AAT) 2663916e780bShannah_mairs { 2664916e780bShannah_mairs PetscErrorCode ierr; 2665916e780bShannah_mairs 2666916e780bShannah_mairs PetscFunctionBegin; 2667916e780bShannah_mairs ierr = PetscFree((*AA)[0]);CHKERRQ(ierr); 2668916e780bShannah_mairs ierr = PetscFree(*AA);CHKERRQ(ierr); 2669916e780bShannah_mairs *AA = NULL; 2670916e780bShannah_mairs if (*AAT) { 2671916e780bShannah_mairs ierr = PetscFree((*AAT)[0]);CHKERRQ(ierr); 2672916e780bShannah_mairs ierr = PetscFree(*AAT);CHKERRQ(ierr); 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 PetscErrorCode ierr; 2707916e780bShannah_mairs const PetscReal *gllweights = weights; 2708916e780bShannah_mairs const PetscInt glln = n; 2709916e780bShannah_mairs PetscInt i,j; 2710916e780bShannah_mairs 2711916e780bShannah_mairs PetscFunctionBegin; 2712916e780bShannah_mairs ierr = PetscGaussLobattoLegendreElementGradientCreate(n,nodes,weights,&D,NULL);CHKERRQ(ierr); 2713916e780bShannah_mairs for (i=0; i<glln; i++) { 2714916e780bShannah_mairs for (j=0; j<glln; j++) { 2715916e780bShannah_mairs D[i][j] = gllweights[i]*D[i][j]; 2716916e780bShannah_mairs } 2717916e780bShannah_mairs } 2718916e780bShannah_mairs *AA = D; 2719916e780bShannah_mairs PetscFunctionReturn(0); 2720916e780bShannah_mairs } 2721916e780bShannah_mairs 2722916e780bShannah_mairs /*@C 2723916e780bShannah_mairs PetscGaussLobattoLegendreElementAdvectionDestroy - frees the advection stiffness for a single 1d GLL element 2724916e780bShannah_mairs 2725916e780bShannah_mairs Not Collective 2726916e780bShannah_mairs 2727d8d19677SJose E. Roman Input Parameters: 2728916e780bShannah_mairs + n - the number of GLL nodes 2729916e780bShannah_mairs . nodes - the GLL nodes 2730916e780bShannah_mairs . weights - the GLL weights 2731916e780bShannah_mairs - A - advection 2732916e780bShannah_mairs 2733916e780bShannah_mairs Level: beginner 2734916e780bShannah_mairs 2735916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementAdvectionCreate() 2736916e780bShannah_mairs 2737916e780bShannah_mairs @*/ 2738916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementAdvectionDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2739916e780bShannah_mairs { 2740916e780bShannah_mairs PetscErrorCode ierr; 2741916e780bShannah_mairs 2742916e780bShannah_mairs PetscFunctionBegin; 2743916e780bShannah_mairs ierr = PetscFree((*AA)[0]);CHKERRQ(ierr); 2744916e780bShannah_mairs ierr = PetscFree(*AA);CHKERRQ(ierr); 2745916e780bShannah_mairs *AA = NULL; 2746916e780bShannah_mairs PetscFunctionReturn(0); 2747916e780bShannah_mairs } 2748916e780bShannah_mairs 2749916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementMassCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2750916e780bShannah_mairs { 2751916e780bShannah_mairs PetscReal **A; 2752916e780bShannah_mairs PetscErrorCode ierr; 2753916e780bShannah_mairs const PetscReal *gllweights = weights; 2754916e780bShannah_mairs const PetscInt glln = n; 2755916e780bShannah_mairs PetscInt i,j; 2756916e780bShannah_mairs 2757916e780bShannah_mairs PetscFunctionBegin; 2758916e780bShannah_mairs ierr = PetscMalloc1(glln,&A);CHKERRQ(ierr); 2759916e780bShannah_mairs ierr = PetscMalloc1(glln*glln,&A[0]);CHKERRQ(ierr); 2760916e780bShannah_mairs for (i=1; i<glln; i++) A[i] = A[i-1]+glln; 2761916e780bShannah_mairs if (glln==1) {A[0][0] = 0.;} 2762916e780bShannah_mairs for (i=0; i<glln; i++) { 2763916e780bShannah_mairs for (j=0; j<glln; j++) { 2764916e780bShannah_mairs A[i][j] = 0.; 2765916e780bShannah_mairs if (j==i) A[i][j] = gllweights[i]; 2766916e780bShannah_mairs } 2767916e780bShannah_mairs } 2768916e780bShannah_mairs *AA = A; 2769916e780bShannah_mairs PetscFunctionReturn(0); 2770916e780bShannah_mairs } 2771916e780bShannah_mairs 2772916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementMassDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2773916e780bShannah_mairs { 2774916e780bShannah_mairs PetscErrorCode ierr; 2775916e780bShannah_mairs 2776916e780bShannah_mairs PetscFunctionBegin; 2777916e780bShannah_mairs ierr = PetscFree((*AA)[0]);CHKERRQ(ierr); 2778916e780bShannah_mairs ierr = PetscFree(*AA);CHKERRQ(ierr); 2779916e780bShannah_mairs *AA = NULL; 2780916e780bShannah_mairs PetscFunctionReturn(0); 2781916e780bShannah_mairs } 2782d4afb720SToby Isaac 2783d4afb720SToby Isaac /*@ 2784d4afb720SToby Isaac PetscDTIndexToBary - convert an index into a barycentric coordinate. 2785d4afb720SToby Isaac 2786d4afb720SToby Isaac Input Parameters: 2787d4afb720SToby 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) 2788d4afb720SToby Isaac . sum - the value that the sum of the barycentric coordinates (which will be non-negative integers) should sum to 2789d4afb720SToby Isaac - index - the index to convert: should be >= 0 and < Binomial(len - 1 + sum, sum) 2790d4afb720SToby Isaac 2791d4afb720SToby Isaac Output Parameter: 2792d4afb720SToby Isaac . coord - will be filled with the barycentric coordinate 2793d4afb720SToby Isaac 2794d4afb720SToby Isaac Level: beginner 2795d4afb720SToby Isaac 2796d4afb720SToby Isaac Note: the indices map to barycentric coordinates in lexicographic order, where the first index is the 2797d4afb720SToby Isaac least significant and the last index is the most significant. 2798d4afb720SToby Isaac 2799fbdc3dfeSToby Isaac .seealso: PetscDTBaryToIndex() 2800d4afb720SToby Isaac @*/ 2801d4afb720SToby Isaac PetscErrorCode PetscDTIndexToBary(PetscInt len, PetscInt sum, PetscInt index, PetscInt coord[]) 2802d4afb720SToby Isaac { 2803d4afb720SToby Isaac PetscInt c, d, s, total, subtotal, nexttotal; 2804d4afb720SToby Isaac 2805d4afb720SToby Isaac PetscFunctionBeginHot; 28062c71b3e2SJacob Faibussowitsch PetscCheckFalse(len < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative"); 28072c71b3e2SJacob Faibussowitsch PetscCheckFalse(index < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index must be non-negative"); 2808d4afb720SToby Isaac if (!len) { 2809d4afb720SToby Isaac if (!sum && !index) PetscFunctionReturn(0); 2810d4afb720SToby Isaac SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid index or sum for length 0 barycentric coordinate"); 2811d4afb720SToby Isaac } 2812d4afb720SToby Isaac for (c = 1, total = 1; c <= len; c++) { 2813d4afb720SToby Isaac /* total is the number of ways to have a tuple of length c with sum */ 2814d4afb720SToby Isaac if (index < total) break; 2815d4afb720SToby Isaac total = (total * (sum + c)) / c; 2816d4afb720SToby Isaac } 28172c71b3e2SJacob Faibussowitsch PetscCheckFalse(c > len,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index out of range"); 2818d4afb720SToby Isaac for (d = c; d < len; d++) coord[d] = 0; 2819d4afb720SToby Isaac for (s = 0, subtotal = 1, nexttotal = 1; c > 0;) { 2820d4afb720SToby Isaac /* subtotal is the number of ways to have a tuple of length c with sum s */ 2821d4afb720SToby Isaac /* nexttotal is the number of ways to have a tuple of length c-1 with sum s */ 2822d4afb720SToby Isaac if ((index + subtotal) >= total) { 2823d4afb720SToby Isaac coord[--c] = sum - s; 2824d4afb720SToby Isaac index -= (total - subtotal); 2825d4afb720SToby Isaac sum = s; 2826d4afb720SToby Isaac total = nexttotal; 2827d4afb720SToby Isaac subtotal = 1; 2828d4afb720SToby Isaac nexttotal = 1; 2829d4afb720SToby Isaac s = 0; 2830d4afb720SToby Isaac } else { 2831d4afb720SToby Isaac subtotal = (subtotal * (c + s)) / (s + 1); 2832d4afb720SToby Isaac nexttotal = (nexttotal * (c - 1 + s)) / (s + 1); 2833d4afb720SToby Isaac s++; 2834d4afb720SToby Isaac } 2835d4afb720SToby Isaac } 2836d4afb720SToby Isaac PetscFunctionReturn(0); 2837d4afb720SToby Isaac } 2838d4afb720SToby Isaac 2839d4afb720SToby Isaac /*@ 2840d4afb720SToby Isaac PetscDTBaryToIndex - convert a barycentric coordinate to an index 2841d4afb720SToby Isaac 2842d4afb720SToby Isaac Input Parameters: 2843d4afb720SToby 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) 2844d4afb720SToby Isaac . sum - the value that the sum of the barycentric coordinates (which will be non-negative integers) should sum to 2845d4afb720SToby Isaac - coord - a barycentric coordinate with the given length and sum 2846d4afb720SToby Isaac 2847d4afb720SToby Isaac Output Parameter: 2848d4afb720SToby Isaac . index - the unique index for the coordinate, >= 0 and < Binomial(len - 1 + sum, sum) 2849d4afb720SToby Isaac 2850d4afb720SToby Isaac Level: beginner 2851d4afb720SToby Isaac 2852d4afb720SToby Isaac Note: the indices map to barycentric coordinates in lexicographic order, where the first index is the 2853d4afb720SToby Isaac least significant and the last index is the most significant. 2854d4afb720SToby Isaac 2855d4afb720SToby Isaac .seealso: PetscDTIndexToBary 2856d4afb720SToby Isaac @*/ 2857d4afb720SToby Isaac PetscErrorCode PetscDTBaryToIndex(PetscInt len, PetscInt sum, const PetscInt coord[], PetscInt *index) 2858d4afb720SToby Isaac { 2859d4afb720SToby Isaac PetscInt c; 2860d4afb720SToby Isaac PetscInt i; 2861d4afb720SToby Isaac PetscInt total; 2862d4afb720SToby Isaac 2863d4afb720SToby Isaac PetscFunctionBeginHot; 28642c71b3e2SJacob Faibussowitsch PetscCheckFalse(len < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative"); 2865d4afb720SToby Isaac if (!len) { 2866d4afb720SToby Isaac if (!sum) { 2867d4afb720SToby Isaac *index = 0; 2868d4afb720SToby Isaac PetscFunctionReturn(0); 2869d4afb720SToby Isaac } 2870d4afb720SToby Isaac SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid index or sum for length 0 barycentric coordinate"); 2871d4afb720SToby Isaac } 2872d4afb720SToby Isaac for (c = 1, total = 1; c < len; c++) total = (total * (sum + c)) / c; 2873d4afb720SToby Isaac i = total - 1; 2874d4afb720SToby Isaac c = len - 1; 2875d4afb720SToby Isaac sum -= coord[c]; 2876d4afb720SToby Isaac while (sum > 0) { 2877d4afb720SToby Isaac PetscInt subtotal; 2878d4afb720SToby Isaac PetscInt s; 2879d4afb720SToby Isaac 2880d4afb720SToby Isaac for (s = 1, subtotal = 1; s < sum; s++) subtotal = (subtotal * (c + s)) / s; 2881d4afb720SToby Isaac i -= subtotal; 2882d4afb720SToby Isaac sum -= coord[--c]; 2883d4afb720SToby Isaac } 2884d4afb720SToby Isaac *index = i; 2885d4afb720SToby Isaac PetscFunctionReturn(0); 2886d4afb720SToby Isaac } 2887