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 15d4afb720SToby Isaac const char *const PetscDTNodeTypes[] = {"gaussjacobi", "equispaced", "tanhsinh", "PETSCDTNODES_", 0}; 16d4afb720SToby Isaac 17e6a796c3SToby Isaac static PetscBool GolubWelschCite = PETSC_FALSE; 18e6a796c3SToby Isaac const char GolubWelschCitation[] = "@article{GolubWelsch1969,\n" 190bfcf5a5SMatthew G. Knepley " author = {Golub and Welsch},\n" 200bfcf5a5SMatthew G. Knepley " title = {Calculation of Quadrature Rules},\n" 210bfcf5a5SMatthew G. Knepley " journal = {Math. Comp.},\n" 220bfcf5a5SMatthew G. Knepley " volume = {23},\n" 230bfcf5a5SMatthew G. Knepley " number = {106},\n" 240bfcf5a5SMatthew G. Knepley " pages = {221--230},\n" 250bfcf5a5SMatthew G. Knepley " year = {1969}\n}\n"; 260bfcf5a5SMatthew G. Knepley 27c4762a1bSJed Brown /* Numerical tests in src/dm/dt/tests/ex1.c show that when computing the nodes and weights of Gauss-Jacobi 2894e21283SToby Isaac quadrature rules: 29e6a796c3SToby Isaac 3094e21283SToby Isaac - in double precision, Newton's method and Golub & Welsch both work for moderate degrees (< 100), 3194e21283SToby Isaac - in single precision, Newton's method starts producing incorrect roots around n = 15, but 3294e21283SToby Isaac the weights from Golub & Welsch become a problem before then: they produces errors 3394e21283SToby Isaac in computing the Jacobi-polynomial Gram matrix around n = 6. 3494e21283SToby Isaac 3594e21283SToby Isaac So we default to Newton's method (required fewer dependencies) */ 3694e21283SToby Isaac PetscBool PetscDTGaussQuadratureNewton_Internal = PETSC_TRUE; 372cd22861SMatthew G. Knepley 382cd22861SMatthew G. Knepley PetscClassId PETSCQUADRATURE_CLASSID = 0; 392cd22861SMatthew G. Knepley 4040d8ff71SMatthew G. Knepley /*@ 4140d8ff71SMatthew G. Knepley PetscQuadratureCreate - Create a PetscQuadrature object 4240d8ff71SMatthew G. Knepley 43d083f849SBarry Smith Collective 4440d8ff71SMatthew G. Knepley 4540d8ff71SMatthew G. Knepley Input Parameter: 4640d8ff71SMatthew G. Knepley . comm - The communicator for the PetscQuadrature object 4740d8ff71SMatthew G. Knepley 4840d8ff71SMatthew G. Knepley Output Parameter: 4940d8ff71SMatthew G. Knepley . q - The PetscQuadrature object 5040d8ff71SMatthew G. Knepley 5140d8ff71SMatthew G. Knepley Level: beginner 5240d8ff71SMatthew G. Knepley 5340d8ff71SMatthew G. Knepley .seealso: PetscQuadratureDestroy(), PetscQuadratureGetData() 5440d8ff71SMatthew G. Knepley @*/ 5521454ff5SMatthew G. Knepley PetscErrorCode PetscQuadratureCreate(MPI_Comm comm, PetscQuadrature *q) 5621454ff5SMatthew G. Knepley { 5721454ff5SMatthew G. Knepley PetscErrorCode ierr; 5821454ff5SMatthew G. Knepley 5921454ff5SMatthew G. Knepley PetscFunctionBegin; 6021454ff5SMatthew G. Knepley PetscValidPointer(q, 2); 612cd22861SMatthew G. Knepley ierr = DMInitializePackage();CHKERRQ(ierr); 622cd22861SMatthew G. Knepley ierr = PetscHeaderCreate(*q,PETSCQUADRATURE_CLASSID,"PetscQuadrature","Quadrature","DT",comm,PetscQuadratureDestroy,PetscQuadratureView);CHKERRQ(ierr); 6321454ff5SMatthew G. Knepley (*q)->dim = -1; 64a6b92713SMatthew G. Knepley (*q)->Nc = 1; 65bcede257SMatthew G. Knepley (*q)->order = -1; 6621454ff5SMatthew G. Knepley (*q)->numPoints = 0; 6721454ff5SMatthew G. Knepley (*q)->points = NULL; 6821454ff5SMatthew G. Knepley (*q)->weights = NULL; 6921454ff5SMatthew G. Knepley PetscFunctionReturn(0); 7021454ff5SMatthew G. Knepley } 7121454ff5SMatthew G. Knepley 72c9638911SMatthew G. Knepley /*@ 73c9638911SMatthew G. Knepley PetscQuadratureDuplicate - Create a deep copy of the PetscQuadrature object 74c9638911SMatthew G. Knepley 75d083f849SBarry Smith Collective on q 76c9638911SMatthew G. Knepley 77c9638911SMatthew G. Knepley Input Parameter: 78c9638911SMatthew G. Knepley . q - The PetscQuadrature object 79c9638911SMatthew G. Knepley 80c9638911SMatthew G. Knepley Output Parameter: 81c9638911SMatthew G. Knepley . r - The new PetscQuadrature object 82c9638911SMatthew G. Knepley 83c9638911SMatthew G. Knepley Level: beginner 84c9638911SMatthew G. Knepley 85c9638911SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureDestroy(), PetscQuadratureGetData() 86c9638911SMatthew G. Knepley @*/ 87c9638911SMatthew G. Knepley PetscErrorCode PetscQuadratureDuplicate(PetscQuadrature q, PetscQuadrature *r) 88c9638911SMatthew G. Knepley { 89a6b92713SMatthew G. Knepley PetscInt order, dim, Nc, Nq; 90c9638911SMatthew G. Knepley const PetscReal *points, *weights; 91c9638911SMatthew G. Knepley PetscReal *p, *w; 92c9638911SMatthew G. Knepley PetscErrorCode ierr; 93c9638911SMatthew G. Knepley 94c9638911SMatthew G. Knepley PetscFunctionBegin; 95c9638911SMatthew G. Knepley PetscValidPointer(q, 2); 96c9638911SMatthew G. Knepley ierr = PetscQuadratureCreate(PetscObjectComm((PetscObject) q), r);CHKERRQ(ierr); 97c9638911SMatthew G. Knepley ierr = PetscQuadratureGetOrder(q, &order);CHKERRQ(ierr); 98c9638911SMatthew G. Knepley ierr = PetscQuadratureSetOrder(*r, order);CHKERRQ(ierr); 99a6b92713SMatthew G. Knepley ierr = PetscQuadratureGetData(q, &dim, &Nc, &Nq, &points, &weights);CHKERRQ(ierr); 100c9638911SMatthew G. Knepley ierr = PetscMalloc1(Nq*dim, &p);CHKERRQ(ierr); 101f0a0bfafSMatthew G. Knepley ierr = PetscMalloc1(Nq*Nc, &w);CHKERRQ(ierr); 102580bdb30SBarry Smith ierr = PetscArraycpy(p, points, Nq*dim);CHKERRQ(ierr); 103580bdb30SBarry Smith ierr = PetscArraycpy(w, weights, Nc * Nq);CHKERRQ(ierr); 104a6b92713SMatthew G. Knepley ierr = PetscQuadratureSetData(*r, dim, Nc, Nq, p, w);CHKERRQ(ierr); 105c9638911SMatthew G. Knepley PetscFunctionReturn(0); 106c9638911SMatthew G. Knepley } 107c9638911SMatthew G. Knepley 10840d8ff71SMatthew G. Knepley /*@ 10940d8ff71SMatthew G. Knepley PetscQuadratureDestroy - Destroys a PetscQuadrature object 11040d8ff71SMatthew G. Knepley 111d083f849SBarry Smith Collective on q 11240d8ff71SMatthew G. Knepley 11340d8ff71SMatthew G. Knepley Input Parameter: 11440d8ff71SMatthew G. Knepley . q - The PetscQuadrature object 11540d8ff71SMatthew G. Knepley 11640d8ff71SMatthew G. Knepley Level: beginner 11740d8ff71SMatthew G. Knepley 11840d8ff71SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureGetData() 11940d8ff71SMatthew G. Knepley @*/ 120bfa639d9SMatthew G. Knepley PetscErrorCode PetscQuadratureDestroy(PetscQuadrature *q) 121bfa639d9SMatthew G. Knepley { 122bfa639d9SMatthew G. Knepley PetscErrorCode ierr; 123bfa639d9SMatthew G. Knepley 124bfa639d9SMatthew G. Knepley PetscFunctionBegin; 12521454ff5SMatthew G. Knepley if (!*q) PetscFunctionReturn(0); 1262cd22861SMatthew G. Knepley PetscValidHeaderSpecific((*q),PETSCQUADRATURE_CLASSID,1); 12721454ff5SMatthew G. Knepley if (--((PetscObject)(*q))->refct > 0) { 12821454ff5SMatthew G. Knepley *q = NULL; 12921454ff5SMatthew G. Knepley PetscFunctionReturn(0); 13021454ff5SMatthew G. Knepley } 13121454ff5SMatthew G. Knepley ierr = PetscFree((*q)->points);CHKERRQ(ierr); 13221454ff5SMatthew G. Knepley ierr = PetscFree((*q)->weights);CHKERRQ(ierr); 13321454ff5SMatthew G. Knepley ierr = PetscHeaderDestroy(q);CHKERRQ(ierr); 13421454ff5SMatthew G. Knepley PetscFunctionReturn(0); 13521454ff5SMatthew G. Knepley } 13621454ff5SMatthew G. Knepley 137bcede257SMatthew G. Knepley /*@ 138a6b92713SMatthew G. Knepley PetscQuadratureGetOrder - Return the order of the method 139bcede257SMatthew G. Knepley 140bcede257SMatthew G. Knepley Not collective 141bcede257SMatthew G. Knepley 142bcede257SMatthew G. Knepley Input Parameter: 143bcede257SMatthew G. Knepley . q - The PetscQuadrature object 144bcede257SMatthew G. Knepley 145bcede257SMatthew G. Knepley Output Parameter: 146bcede257SMatthew G. Knepley . order - The order of the quadrature, i.e. the highest degree polynomial that is exactly integrated 147bcede257SMatthew G. Knepley 148bcede257SMatthew G. Knepley Level: intermediate 149bcede257SMatthew G. Knepley 150bcede257SMatthew G. Knepley .seealso: PetscQuadratureSetOrder(), PetscQuadratureGetData(), PetscQuadratureSetData() 151bcede257SMatthew G. Knepley @*/ 152bcede257SMatthew G. Knepley PetscErrorCode PetscQuadratureGetOrder(PetscQuadrature q, PetscInt *order) 153bcede257SMatthew G. Knepley { 154bcede257SMatthew G. Knepley PetscFunctionBegin; 1552cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 156bcede257SMatthew G. Knepley PetscValidPointer(order, 2); 157bcede257SMatthew G. Knepley *order = q->order; 158bcede257SMatthew G. Knepley PetscFunctionReturn(0); 159bcede257SMatthew G. Knepley } 160bcede257SMatthew G. Knepley 161bcede257SMatthew G. Knepley /*@ 162a6b92713SMatthew G. Knepley PetscQuadratureSetOrder - Return the order of the method 163bcede257SMatthew G. Knepley 164bcede257SMatthew G. Knepley Not collective 165bcede257SMatthew G. Knepley 166bcede257SMatthew G. Knepley Input Parameters: 167bcede257SMatthew G. Knepley + q - The PetscQuadrature object 168bcede257SMatthew G. Knepley - order - The order of the quadrature, i.e. the highest degree polynomial that is exactly integrated 169bcede257SMatthew G. Knepley 170bcede257SMatthew G. Knepley Level: intermediate 171bcede257SMatthew G. Knepley 172bcede257SMatthew G. Knepley .seealso: PetscQuadratureGetOrder(), PetscQuadratureGetData(), PetscQuadratureSetData() 173bcede257SMatthew G. Knepley @*/ 174bcede257SMatthew G. Knepley PetscErrorCode PetscQuadratureSetOrder(PetscQuadrature q, PetscInt order) 175bcede257SMatthew G. Knepley { 176bcede257SMatthew G. Knepley PetscFunctionBegin; 1772cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 178bcede257SMatthew G. Knepley q->order = order; 179bcede257SMatthew G. Knepley PetscFunctionReturn(0); 180bcede257SMatthew G. Knepley } 181bcede257SMatthew G. Knepley 182a6b92713SMatthew G. Knepley /*@ 183a6b92713SMatthew G. Knepley PetscQuadratureGetNumComponents - Return the number of components for functions to be integrated 184a6b92713SMatthew G. Knepley 185a6b92713SMatthew G. Knepley Not collective 186a6b92713SMatthew G. Knepley 187a6b92713SMatthew G. Knepley Input Parameter: 188a6b92713SMatthew G. Knepley . q - The PetscQuadrature object 189a6b92713SMatthew G. Knepley 190a6b92713SMatthew G. Knepley Output Parameter: 191a6b92713SMatthew G. Knepley . Nc - The number of components 192a6b92713SMatthew G. Knepley 193a6b92713SMatthew G. Knepley Note: We are performing an integral int f(x) . w(x) dx, where both f and w (the weight) have Nc components. 194a6b92713SMatthew G. Knepley 195a6b92713SMatthew G. Knepley Level: intermediate 196a6b92713SMatthew G. Knepley 197a6b92713SMatthew G. Knepley .seealso: PetscQuadratureSetNumComponents(), PetscQuadratureGetData(), PetscQuadratureSetData() 198a6b92713SMatthew G. Knepley @*/ 199a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureGetNumComponents(PetscQuadrature q, PetscInt *Nc) 200a6b92713SMatthew G. Knepley { 201a6b92713SMatthew G. Knepley PetscFunctionBegin; 2022cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 203a6b92713SMatthew G. Knepley PetscValidPointer(Nc, 2); 204a6b92713SMatthew G. Knepley *Nc = q->Nc; 205a6b92713SMatthew G. Knepley PetscFunctionReturn(0); 206a6b92713SMatthew G. Knepley } 207a6b92713SMatthew G. Knepley 208a6b92713SMatthew G. Knepley /*@ 209a6b92713SMatthew G. Knepley PetscQuadratureSetNumComponents - Return the number of components for functions to be integrated 210a6b92713SMatthew G. Knepley 211a6b92713SMatthew G. Knepley Not collective 212a6b92713SMatthew G. Knepley 213a6b92713SMatthew G. Knepley Input Parameters: 214a6b92713SMatthew G. Knepley + q - The PetscQuadrature object 215a6b92713SMatthew G. Knepley - Nc - The number of components 216a6b92713SMatthew G. Knepley 217a6b92713SMatthew G. Knepley Note: We are performing an integral int f(x) . w(x) dx, where both f and w (the weight) have Nc components. 218a6b92713SMatthew G. Knepley 219a6b92713SMatthew G. Knepley Level: intermediate 220a6b92713SMatthew G. Knepley 221a6b92713SMatthew G. Knepley .seealso: PetscQuadratureGetNumComponents(), PetscQuadratureGetData(), PetscQuadratureSetData() 222a6b92713SMatthew G. Knepley @*/ 223a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureSetNumComponents(PetscQuadrature q, PetscInt Nc) 224a6b92713SMatthew G. Knepley { 225a6b92713SMatthew G. Knepley PetscFunctionBegin; 2262cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 227a6b92713SMatthew G. Knepley q->Nc = Nc; 228a6b92713SMatthew G. Knepley PetscFunctionReturn(0); 229a6b92713SMatthew G. Knepley } 230a6b92713SMatthew G. Knepley 23140d8ff71SMatthew G. Knepley /*@C 23240d8ff71SMatthew G. Knepley PetscQuadratureGetData - Returns the data defining the quadrature 23340d8ff71SMatthew G. Knepley 23440d8ff71SMatthew G. Knepley Not collective 23540d8ff71SMatthew G. Knepley 23640d8ff71SMatthew G. Knepley Input Parameter: 23740d8ff71SMatthew G. Knepley . q - The PetscQuadrature object 23840d8ff71SMatthew G. Knepley 23940d8ff71SMatthew G. Knepley Output Parameters: 24040d8ff71SMatthew G. Knepley + dim - The spatial dimension 241805e7170SToby Isaac . Nc - The number of components 24240d8ff71SMatthew G. Knepley . npoints - The number of quadrature points 24340d8ff71SMatthew G. Knepley . points - The coordinates of each quadrature point 24440d8ff71SMatthew G. Knepley - weights - The weight of each quadrature point 24540d8ff71SMatthew G. Knepley 24640d8ff71SMatthew G. Knepley Level: intermediate 24740d8ff71SMatthew G. Knepley 24895452b02SPatrick Sanan Fortran Notes: 24995452b02SPatrick Sanan From Fortran you must call PetscQuadratureRestoreData() when you are done with the data 2501fd49c25SBarry Smith 25140d8ff71SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureSetData() 25240d8ff71SMatthew G. Knepley @*/ 253a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureGetData(PetscQuadrature q, PetscInt *dim, PetscInt *Nc, PetscInt *npoints, const PetscReal *points[], const PetscReal *weights[]) 25421454ff5SMatthew G. Knepley { 25521454ff5SMatthew G. Knepley PetscFunctionBegin; 2562cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 25721454ff5SMatthew G. Knepley if (dim) { 25821454ff5SMatthew G. Knepley PetscValidPointer(dim, 2); 25921454ff5SMatthew G. Knepley *dim = q->dim; 26021454ff5SMatthew G. Knepley } 261a6b92713SMatthew G. Knepley if (Nc) { 262a6b92713SMatthew G. Knepley PetscValidPointer(Nc, 3); 263a6b92713SMatthew G. Knepley *Nc = q->Nc; 264a6b92713SMatthew G. Knepley } 26521454ff5SMatthew G. Knepley if (npoints) { 266a6b92713SMatthew G. Knepley PetscValidPointer(npoints, 4); 26721454ff5SMatthew G. Knepley *npoints = q->numPoints; 26821454ff5SMatthew G. Knepley } 26921454ff5SMatthew G. Knepley if (points) { 270a6b92713SMatthew G. Knepley PetscValidPointer(points, 5); 27121454ff5SMatthew G. Knepley *points = q->points; 27221454ff5SMatthew G. Knepley } 27321454ff5SMatthew G. Knepley if (weights) { 274a6b92713SMatthew G. Knepley PetscValidPointer(weights, 6); 27521454ff5SMatthew G. Knepley *weights = q->weights; 27621454ff5SMatthew G. Knepley } 27721454ff5SMatthew G. Knepley PetscFunctionReturn(0); 27821454ff5SMatthew G. Knepley } 27921454ff5SMatthew G. Knepley 280907761f8SToby Isaac static PetscErrorCode PetscDTJacobianInverse_Internal(PetscInt m, PetscInt n, const PetscReal J[], PetscReal Jinv[]) 281907761f8SToby Isaac { 282907761f8SToby Isaac PetscScalar *Js, *Jinvs; 283907761f8SToby Isaac PetscInt i, j, k; 284907761f8SToby Isaac PetscBLASInt bm, bn, info; 285907761f8SToby Isaac PetscErrorCode ierr; 286907761f8SToby Isaac 287907761f8SToby Isaac PetscFunctionBegin; 288d4afb720SToby Isaac if (!m || !n) PetscFunctionReturn(0); 289907761f8SToby Isaac ierr = PetscBLASIntCast(m, &bm);CHKERRQ(ierr); 290907761f8SToby Isaac ierr = PetscBLASIntCast(n, &bn);CHKERRQ(ierr); 291907761f8SToby Isaac #if defined(PETSC_USE_COMPLEX) 292907761f8SToby Isaac ierr = PetscMalloc2(m*n, &Js, m*n, &Jinvs);CHKERRQ(ierr); 29328222859SToby Isaac for (i = 0; i < m*n; i++) Js[i] = J[i]; 294907761f8SToby Isaac #else 295907761f8SToby Isaac Js = (PetscReal *) J; 296907761f8SToby Isaac Jinvs = Jinv; 297907761f8SToby Isaac #endif 298907761f8SToby Isaac if (m == n) { 299907761f8SToby Isaac PetscBLASInt *pivots; 300907761f8SToby Isaac PetscScalar *W; 301907761f8SToby Isaac 302907761f8SToby Isaac ierr = PetscMalloc2(m, &pivots, m, &W);CHKERRQ(ierr); 303907761f8SToby Isaac 304907761f8SToby Isaac ierr = PetscArraycpy(Jinvs, Js, m * m);CHKERRQ(ierr); 305907761f8SToby Isaac PetscStackCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bm, &bm, Jinvs, &bm, pivots, &info)); 306907761f8SToby Isaac if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetrf %D",(PetscInt)info); 307907761f8SToby Isaac PetscStackCallBLAS("LAPACKgetri", LAPACKgetri_(&bm, Jinvs, &bm, pivots, W, &bm, &info)); 308907761f8SToby Isaac if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetri %D",(PetscInt)info); 309907761f8SToby Isaac ierr = PetscFree2(pivots, W);CHKERRQ(ierr); 310907761f8SToby Isaac } else if (m < n) { 311907761f8SToby Isaac PetscScalar *JJT; 312907761f8SToby Isaac PetscBLASInt *pivots; 313907761f8SToby Isaac PetscScalar *W; 314907761f8SToby Isaac 315907761f8SToby Isaac ierr = PetscMalloc1(m*m, &JJT);CHKERRQ(ierr); 316907761f8SToby Isaac ierr = PetscMalloc2(m, &pivots, m, &W);CHKERRQ(ierr); 317907761f8SToby Isaac for (i = 0; i < m; i++) { 318907761f8SToby Isaac for (j = 0; j < m; j++) { 319907761f8SToby Isaac PetscScalar val = 0.; 320907761f8SToby Isaac 321907761f8SToby Isaac for (k = 0; k < n; k++) val += Js[i * n + k] * Js[j * n + k]; 322907761f8SToby Isaac JJT[i * m + j] = val; 323907761f8SToby Isaac } 324907761f8SToby Isaac } 325907761f8SToby Isaac 326907761f8SToby Isaac PetscStackCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bm, &bm, JJT, &bm, pivots, &info)); 327907761f8SToby Isaac if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetrf %D",(PetscInt)info); 328907761f8SToby Isaac PetscStackCallBLAS("LAPACKgetri", LAPACKgetri_(&bm, JJT, &bm, pivots, W, &bm, &info)); 329907761f8SToby Isaac if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetri %D",(PetscInt)info); 330907761f8SToby Isaac for (i = 0; i < n; i++) { 331907761f8SToby Isaac for (j = 0; j < m; j++) { 332907761f8SToby Isaac PetscScalar val = 0.; 333907761f8SToby Isaac 334907761f8SToby Isaac for (k = 0; k < m; k++) val += Js[k * n + i] * JJT[k * m + j]; 335907761f8SToby Isaac Jinvs[i * m + j] = val; 336907761f8SToby Isaac } 337907761f8SToby Isaac } 338907761f8SToby Isaac ierr = PetscFree2(pivots, W);CHKERRQ(ierr); 339907761f8SToby Isaac ierr = PetscFree(JJT);CHKERRQ(ierr); 340907761f8SToby Isaac } else { 341907761f8SToby Isaac PetscScalar *JTJ; 342907761f8SToby Isaac PetscBLASInt *pivots; 343907761f8SToby Isaac PetscScalar *W; 344907761f8SToby Isaac 345907761f8SToby Isaac ierr = PetscMalloc1(n*n, &JTJ);CHKERRQ(ierr); 346907761f8SToby Isaac ierr = PetscMalloc2(n, &pivots, n, &W);CHKERRQ(ierr); 347907761f8SToby Isaac for (i = 0; i < n; i++) { 348907761f8SToby Isaac for (j = 0; j < n; j++) { 349907761f8SToby Isaac PetscScalar val = 0.; 350907761f8SToby Isaac 351907761f8SToby Isaac for (k = 0; k < m; k++) val += Js[k * n + i] * Js[k * n + j]; 352907761f8SToby Isaac JTJ[i * n + j] = val; 353907761f8SToby Isaac } 354907761f8SToby Isaac } 355907761f8SToby Isaac 356d4afb720SToby Isaac PetscStackCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bn, &bn, JTJ, &bn, pivots, &info)); 357907761f8SToby Isaac if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetrf %D",(PetscInt)info); 358907761f8SToby Isaac PetscStackCallBLAS("LAPACKgetri", LAPACKgetri_(&bn, JTJ, &bn, pivots, W, &bn, &info)); 359907761f8SToby Isaac if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error returned from LAPACKgetri %D",(PetscInt)info); 360907761f8SToby Isaac for (i = 0; i < n; i++) { 361907761f8SToby Isaac for (j = 0; j < m; j++) { 362907761f8SToby Isaac PetscScalar val = 0.; 363907761f8SToby Isaac 364907761f8SToby Isaac for (k = 0; k < n; k++) val += JTJ[i * n + k] * Js[j * n + k]; 365907761f8SToby Isaac Jinvs[i * m + j] = val; 366907761f8SToby Isaac } 367907761f8SToby Isaac } 368907761f8SToby Isaac ierr = PetscFree2(pivots, W);CHKERRQ(ierr); 369907761f8SToby Isaac ierr = PetscFree(JTJ);CHKERRQ(ierr); 370907761f8SToby Isaac } 371907761f8SToby Isaac #if defined(PETSC_USE_COMPLEX) 37228222859SToby Isaac for (i = 0; i < m*n; i++) Jinv[i] = PetscRealPart(Jinvs[i]); 373907761f8SToby Isaac ierr = PetscFree2(Js, Jinvs);CHKERRQ(ierr); 374907761f8SToby Isaac #endif 375907761f8SToby Isaac PetscFunctionReturn(0); 376907761f8SToby Isaac } 377907761f8SToby Isaac 378907761f8SToby Isaac /*@ 379907761f8SToby Isaac PetscQuadraturePushForward - Push forward a quadrature functional under an affine transformation. 380907761f8SToby Isaac 381907761f8SToby Isaac Collecive on PetscQuadrature 382907761f8SToby Isaac 383907761f8SToby Isaac Input Arguments: 384907761f8SToby Isaac + q - the quadrature functional 385907761f8SToby Isaac . imageDim - the dimension of the image of the transformation 386907761f8SToby Isaac . origin - a point in the original space 387907761f8SToby Isaac . originImage - the image of the origin under the transformation 388907761f8SToby Isaac . J - the Jacobian of the image: an [imageDim x dim] matrix in row major order 38928222859SToby Isaac - formDegree - transform the quadrature weights as k-forms of this form degree (if the number of components is a multiple of (dim choose formDegree), it is assumed that they represent multiple k-forms) [see PetscDTAltVPullback() for interpretation of formDegree] 390907761f8SToby Isaac 391907761f8SToby Isaac Output Arguments: 392907761f8SToby Isaac . Jinvstarq - a quadrature rule where each point is the image of a point in the original quadrature rule, and where the k-form weights have been pulled-back by the pseudoinverse of J to the k-form weights in the image space. 393907761f8SToby Isaac 394907761f8SToby Isaac Note: the new quadrature rule will have a different number of components if spaces have different dimensions. For example, pushing a 2-form forward from a two dimensional space to a three dimensional space changes the number of components from 1 to 3. 395907761f8SToby Isaac 3966c877ef6SSatish Balay Level: intermediate 3976c877ef6SSatish Balay 398907761f8SToby Isaac .seealso: PetscDTAltVPullback(), PetscDTAltVPullbackMatrix() 399907761f8SToby Isaac @*/ 40028222859SToby Isaac PetscErrorCode PetscQuadraturePushForward(PetscQuadrature q, PetscInt imageDim, const PetscReal origin[], const PetscReal originImage[], const PetscReal J[], PetscInt formDegree, PetscQuadrature *Jinvstarq) 401907761f8SToby Isaac { 402907761f8SToby Isaac PetscInt dim, Nc, imageNc, formSize, Ncopies, imageFormSize, Npoints, pt, i, j, c; 403907761f8SToby Isaac const PetscReal *points; 404907761f8SToby Isaac const PetscReal *weights; 405907761f8SToby Isaac PetscReal *imagePoints, *imageWeights; 406907761f8SToby Isaac PetscReal *Jinv; 407907761f8SToby Isaac PetscReal *Jinvstar; 408907761f8SToby Isaac PetscErrorCode ierr; 409907761f8SToby Isaac 410907761f8SToby Isaac PetscFunctionBegin; 411d4afb720SToby Isaac PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 41228222859SToby Isaac if (imageDim < PetscAbsInt(formDegree)) SETERRQ2(PetscObjectComm((PetscObject)q), PETSC_ERR_ARG_INCOMP, "Cannot represent a %D-form in %D dimensions", PetscAbsInt(formDegree), imageDim); 413907761f8SToby Isaac ierr = PetscQuadratureGetData(q, &dim, &Nc, &Npoints, &points, &weights);CHKERRQ(ierr); 41428222859SToby Isaac ierr = PetscDTBinomialInt(dim, PetscAbsInt(formDegree), &formSize);CHKERRQ(ierr); 415907761f8SToby Isaac if (Nc % formSize) SETERRQ2(PetscObjectComm((PetscObject)q), PETSC_ERR_ARG_INCOMP, "Number of components %D is not a multiple of formSize %D\n", Nc, formSize); 416907761f8SToby Isaac Ncopies = Nc / formSize; 41728222859SToby Isaac ierr = PetscDTBinomialInt(imageDim, PetscAbsInt(formDegree), &imageFormSize);CHKERRQ(ierr); 418907761f8SToby Isaac imageNc = Ncopies * imageFormSize; 419907761f8SToby Isaac ierr = PetscMalloc1(Npoints * imageDim, &imagePoints);CHKERRQ(ierr); 420907761f8SToby Isaac ierr = PetscMalloc1(Npoints * imageNc, &imageWeights);CHKERRQ(ierr); 421907761f8SToby Isaac ierr = PetscMalloc2(imageDim * dim, &Jinv, formSize * imageFormSize, &Jinvstar);CHKERRQ(ierr); 422d4afb720SToby Isaac ierr = PetscDTJacobianInverse_Internal(imageDim, dim, J, Jinv);CHKERRQ(ierr); 42328222859SToby Isaac ierr = PetscDTAltVPullbackMatrix(imageDim, dim, Jinv, formDegree, Jinvstar);CHKERRQ(ierr); 424907761f8SToby Isaac for (pt = 0; pt < Npoints; pt++) { 425907761f8SToby Isaac const PetscReal *point = &points[pt * dim]; 426907761f8SToby Isaac PetscReal *imagePoint = &imagePoints[pt * imageDim]; 427907761f8SToby Isaac 428907761f8SToby Isaac for (i = 0; i < imageDim; i++) { 429907761f8SToby Isaac PetscReal val = originImage[i]; 430907761f8SToby Isaac 431907761f8SToby Isaac for (j = 0; j < dim; j++) val += J[i * dim + j] * (point[j] - origin[j]); 432907761f8SToby Isaac imagePoint[i] = val; 433907761f8SToby Isaac } 434907761f8SToby Isaac for (c = 0; c < Ncopies; c++) { 435907761f8SToby Isaac const PetscReal *form = &weights[pt * Nc + c * formSize]; 436907761f8SToby Isaac PetscReal *imageForm = &imageWeights[pt * imageNc + c * imageFormSize]; 437907761f8SToby Isaac 438907761f8SToby Isaac for (i = 0; i < imageFormSize; i++) { 439907761f8SToby Isaac PetscReal val = 0.; 440907761f8SToby Isaac 441907761f8SToby Isaac for (j = 0; j < formSize; j++) val += Jinvstar[i * formSize + j] * form[j]; 442907761f8SToby Isaac imageForm[i] = val; 443907761f8SToby Isaac } 444907761f8SToby Isaac } 445907761f8SToby Isaac } 446907761f8SToby Isaac ierr = PetscQuadratureCreate(PetscObjectComm((PetscObject)q), Jinvstarq);CHKERRQ(ierr); 447907761f8SToby Isaac ierr = PetscQuadratureSetData(*Jinvstarq, imageDim, imageNc, Npoints, imagePoints, imageWeights);CHKERRQ(ierr); 448907761f8SToby Isaac ierr = PetscFree2(Jinv, Jinvstar);CHKERRQ(ierr); 449907761f8SToby Isaac PetscFunctionReturn(0); 450907761f8SToby Isaac } 451907761f8SToby Isaac 45240d8ff71SMatthew G. Knepley /*@C 45340d8ff71SMatthew G. Knepley PetscQuadratureSetData - Sets the data defining the quadrature 45440d8ff71SMatthew G. Knepley 45540d8ff71SMatthew G. Knepley Not collective 45640d8ff71SMatthew G. Knepley 45740d8ff71SMatthew G. Knepley Input Parameters: 45840d8ff71SMatthew G. Knepley + q - The PetscQuadrature object 45940d8ff71SMatthew G. Knepley . dim - The spatial dimension 460e2b35d93SBarry Smith . Nc - The number of components 46140d8ff71SMatthew G. Knepley . npoints - The number of quadrature points 46240d8ff71SMatthew G. Knepley . points - The coordinates of each quadrature point 46340d8ff71SMatthew G. Knepley - weights - The weight of each quadrature point 46440d8ff71SMatthew G. Knepley 465c99e0549SMatthew G. Knepley Note: This routine owns the references to points and weights, so they must be allocated using PetscMalloc() and the user should not free them. 466f2fd9e53SMatthew G. Knepley 46740d8ff71SMatthew G. Knepley Level: intermediate 46840d8ff71SMatthew G. Knepley 46940d8ff71SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureGetData() 47040d8ff71SMatthew G. Knepley @*/ 471a6b92713SMatthew G. Knepley PetscErrorCode PetscQuadratureSetData(PetscQuadrature q, PetscInt dim, PetscInt Nc, PetscInt npoints, const PetscReal points[], const PetscReal weights[]) 47221454ff5SMatthew G. Knepley { 47321454ff5SMatthew G. Knepley PetscFunctionBegin; 4742cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 47521454ff5SMatthew G. Knepley if (dim >= 0) q->dim = dim; 476a6b92713SMatthew G. Knepley if (Nc >= 0) q->Nc = Nc; 47721454ff5SMatthew G. Knepley if (npoints >= 0) q->numPoints = npoints; 47821454ff5SMatthew G. Knepley if (points) { 47921454ff5SMatthew G. Knepley PetscValidPointer(points, 4); 48021454ff5SMatthew G. Knepley q->points = points; 48121454ff5SMatthew G. Knepley } 48221454ff5SMatthew G. Knepley if (weights) { 48321454ff5SMatthew G. Knepley PetscValidPointer(weights, 5); 48421454ff5SMatthew G. Knepley q->weights = weights; 48521454ff5SMatthew G. Knepley } 486f9fd7fdbSMatthew G. Knepley PetscFunctionReturn(0); 487f9fd7fdbSMatthew G. Knepley } 488f9fd7fdbSMatthew G. Knepley 489d9bac1caSLisandro Dalcin static PetscErrorCode PetscQuadratureView_Ascii(PetscQuadrature quad, PetscViewer v) 490d9bac1caSLisandro Dalcin { 491d9bac1caSLisandro Dalcin PetscInt q, d, c; 492d9bac1caSLisandro Dalcin PetscViewerFormat format; 493d9bac1caSLisandro Dalcin PetscErrorCode ierr; 494d9bac1caSLisandro Dalcin 495d9bac1caSLisandro Dalcin PetscFunctionBegin; 496c74b4a09SMatthew G. Knepley if (quad->Nc > 1) {ierr = PetscViewerASCIIPrintf(v, "Quadrature of order %D on %D points (dim %D) with %D components\n", quad->order, quad->numPoints, quad->dim, quad->Nc);CHKERRQ(ierr);} 497c74b4a09SMatthew G. Knepley else {ierr = PetscViewerASCIIPrintf(v, "Quadrature of order %D on %D points (dim %D)\n", quad->order, quad->numPoints, quad->dim);CHKERRQ(ierr);} 498d9bac1caSLisandro Dalcin ierr = PetscViewerGetFormat(v, &format);CHKERRQ(ierr); 499d9bac1caSLisandro Dalcin if (format != PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(0); 500d9bac1caSLisandro Dalcin for (q = 0; q < quad->numPoints; ++q) { 501c74b4a09SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(v, "p%D (", q);CHKERRQ(ierr); 502d9bac1caSLisandro Dalcin ierr = PetscViewerASCIIUseTabs(v, PETSC_FALSE);CHKERRQ(ierr); 503d9bac1caSLisandro Dalcin for (d = 0; d < quad->dim; ++d) { 504d9bac1caSLisandro Dalcin if (d) {ierr = PetscViewerASCIIPrintf(v, ", ");CHKERRQ(ierr);} 505d9bac1caSLisandro Dalcin ierr = PetscViewerASCIIPrintf(v, "%+g", (double)quad->points[q*quad->dim+d]);CHKERRQ(ierr); 506d9bac1caSLisandro Dalcin } 507d9bac1caSLisandro Dalcin ierr = PetscViewerASCIIPrintf(v, ") ");CHKERRQ(ierr); 508c74b4a09SMatthew G. Knepley if (quad->Nc > 1) {ierr = PetscViewerASCIIPrintf(v, "w%D (", q);CHKERRQ(ierr);} 509d9bac1caSLisandro Dalcin for (c = 0; c < quad->Nc; ++c) { 510d9bac1caSLisandro Dalcin if (c) {ierr = PetscViewerASCIIPrintf(v, ", ");CHKERRQ(ierr);} 511c74b4a09SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(v, "%+g", (double)quad->weights[q*quad->Nc+c]);CHKERRQ(ierr); 512d9bac1caSLisandro Dalcin } 513d9bac1caSLisandro Dalcin if (quad->Nc > 1) {ierr = PetscViewerASCIIPrintf(v, ")");CHKERRQ(ierr);} 514d9bac1caSLisandro Dalcin ierr = PetscViewerASCIIPrintf(v, "\n");CHKERRQ(ierr); 515d9bac1caSLisandro Dalcin ierr = PetscViewerASCIIUseTabs(v, PETSC_TRUE);CHKERRQ(ierr); 516d9bac1caSLisandro Dalcin } 517d9bac1caSLisandro Dalcin PetscFunctionReturn(0); 518d9bac1caSLisandro Dalcin } 519d9bac1caSLisandro Dalcin 52040d8ff71SMatthew G. Knepley /*@C 52140d8ff71SMatthew G. Knepley PetscQuadratureView - Views a PetscQuadrature object 52240d8ff71SMatthew G. Knepley 523d083f849SBarry Smith Collective on quad 52440d8ff71SMatthew G. Knepley 52540d8ff71SMatthew G. Knepley Input Parameters: 526d9bac1caSLisandro Dalcin + quad - The PetscQuadrature object 52740d8ff71SMatthew G. Knepley - viewer - The PetscViewer object 52840d8ff71SMatthew G. Knepley 52940d8ff71SMatthew G. Knepley Level: beginner 53040d8ff71SMatthew G. Knepley 53140d8ff71SMatthew G. Knepley .seealso: PetscQuadratureCreate(), PetscQuadratureGetData() 53240d8ff71SMatthew G. Knepley @*/ 533f9fd7fdbSMatthew G. Knepley PetscErrorCode PetscQuadratureView(PetscQuadrature quad, PetscViewer viewer) 534f9fd7fdbSMatthew G. Knepley { 535d9bac1caSLisandro Dalcin PetscBool iascii; 536f9fd7fdbSMatthew G. Knepley PetscErrorCode ierr; 537f9fd7fdbSMatthew G. Knepley 538f9fd7fdbSMatthew G. Knepley PetscFunctionBegin; 539d9bac1caSLisandro Dalcin PetscValidHeader(quad, 1); 540d9bac1caSLisandro Dalcin if (viewer) PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 541d9bac1caSLisandro Dalcin if (!viewer) {ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject) quad), &viewer);CHKERRQ(ierr);} 542d9bac1caSLisandro Dalcin ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 543d9bac1caSLisandro Dalcin ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 544d9bac1caSLisandro Dalcin if (iascii) {ierr = PetscQuadratureView_Ascii(quad, viewer);CHKERRQ(ierr);} 545d9bac1caSLisandro Dalcin ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 546bfa639d9SMatthew G. Knepley PetscFunctionReturn(0); 547bfa639d9SMatthew G. Knepley } 548bfa639d9SMatthew G. Knepley 54989710940SMatthew G. Knepley /*@C 55089710940SMatthew G. Knepley PetscQuadratureExpandComposite - Return a quadrature over the composite element, which has the original quadrature in each subelement 55189710940SMatthew G. Knepley 55289710940SMatthew G. Knepley Not collective 55389710940SMatthew G. Knepley 55489710940SMatthew G. Knepley Input Parameter: 55589710940SMatthew G. Knepley + q - The original PetscQuadrature 55689710940SMatthew G. Knepley . numSubelements - The number of subelements the original element is divided into 55789710940SMatthew G. Knepley . v0 - An array of the initial points for each subelement 55889710940SMatthew G. Knepley - jac - An array of the Jacobian mappings from the reference to each subelement 55989710940SMatthew G. Knepley 56089710940SMatthew G. Knepley Output Parameters: 56189710940SMatthew G. Knepley . dim - The dimension 56289710940SMatthew G. Knepley 56389710940SMatthew G. Knepley Note: Together v0 and jac define an affine mapping from the original reference element to each subelement 56489710940SMatthew G. Knepley 565f5f57ec0SBarry Smith Not available from Fortran 566f5f57ec0SBarry Smith 56789710940SMatthew G. Knepley Level: intermediate 56889710940SMatthew G. Knepley 56989710940SMatthew G. Knepley .seealso: PetscFECreate(), PetscSpaceGetDimension(), PetscDualSpaceGetDimension() 57089710940SMatthew G. Knepley @*/ 57189710940SMatthew G. Knepley PetscErrorCode PetscQuadratureExpandComposite(PetscQuadrature q, PetscInt numSubelements, const PetscReal v0[], const PetscReal jac[], PetscQuadrature *qref) 57289710940SMatthew G. Knepley { 57389710940SMatthew G. Knepley const PetscReal *points, *weights; 57489710940SMatthew G. Knepley PetscReal *pointsRef, *weightsRef; 575a6b92713SMatthew G. Knepley PetscInt dim, Nc, order, npoints, npointsRef, c, p, cp, d, e; 57689710940SMatthew G. Knepley PetscErrorCode ierr; 57789710940SMatthew G. Knepley 57889710940SMatthew G. Knepley PetscFunctionBegin; 5792cd22861SMatthew G. Knepley PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1); 58089710940SMatthew G. Knepley PetscValidPointer(v0, 3); 58189710940SMatthew G. Knepley PetscValidPointer(jac, 4); 58289710940SMatthew G. Knepley PetscValidPointer(qref, 5); 58389710940SMatthew G. Knepley ierr = PetscQuadratureCreate(PETSC_COMM_SELF, qref);CHKERRQ(ierr); 58489710940SMatthew G. Knepley ierr = PetscQuadratureGetOrder(q, &order);CHKERRQ(ierr); 585a6b92713SMatthew G. Knepley ierr = PetscQuadratureGetData(q, &dim, &Nc, &npoints, &points, &weights);CHKERRQ(ierr); 58689710940SMatthew G. Knepley npointsRef = npoints*numSubelements; 58789710940SMatthew G. Knepley ierr = PetscMalloc1(npointsRef*dim,&pointsRef);CHKERRQ(ierr); 588a6b92713SMatthew G. Knepley ierr = PetscMalloc1(npointsRef*Nc, &weightsRef);CHKERRQ(ierr); 58989710940SMatthew G. Knepley for (c = 0; c < numSubelements; ++c) { 59089710940SMatthew G. Knepley for (p = 0; p < npoints; ++p) { 59189710940SMatthew G. Knepley for (d = 0; d < dim; ++d) { 59289710940SMatthew G. Knepley pointsRef[(c*npoints + p)*dim+d] = v0[c*dim+d]; 59389710940SMatthew G. Knepley for (e = 0; e < dim; ++e) { 59489710940SMatthew G. Knepley pointsRef[(c*npoints + p)*dim+d] += jac[(c*dim + d)*dim+e]*(points[p*dim+e] + 1.0); 59589710940SMatthew G. Knepley } 59689710940SMatthew G. Knepley } 59789710940SMatthew G. Knepley /* Could also use detJ here */ 598a6b92713SMatthew G. Knepley for (cp = 0; cp < Nc; ++cp) weightsRef[(c*npoints+p)*Nc+cp] = weights[p*Nc+cp]/numSubelements; 59989710940SMatthew G. Knepley } 60089710940SMatthew G. Knepley } 60189710940SMatthew G. Knepley ierr = PetscQuadratureSetOrder(*qref, order);CHKERRQ(ierr); 602a6b92713SMatthew G. Knepley ierr = PetscQuadratureSetData(*qref, dim, Nc, npointsRef, pointsRef, weightsRef);CHKERRQ(ierr); 60389710940SMatthew G. Knepley PetscFunctionReturn(0); 60489710940SMatthew G. Knepley } 60589710940SMatthew G. Knepley 60694e21283SToby Isaac /* Compute the coefficients for the Jacobi polynomial recurrence, 60794e21283SToby Isaac * 60894e21283SToby Isaac * J^{a,b}_n(x) = (cnm1 + cnm1x * x) * J^{a,b}_{n-1}(x) - cnm2 * J^{a,b}_{n-2}(x). 60994e21283SToby Isaac */ 61094e21283SToby Isaac #define PetscDTJacobiRecurrence_Internal(n,a,b,cnm1,cnm1x,cnm2) \ 61194e21283SToby Isaac do { \ 61294e21283SToby Isaac PetscReal _a = (a); \ 61394e21283SToby Isaac PetscReal _b = (b); \ 61494e21283SToby Isaac PetscReal _n = (n); \ 61594e21283SToby Isaac if (n == 1) { \ 61694e21283SToby Isaac (cnm1) = (_a-_b) * 0.5; \ 61794e21283SToby Isaac (cnm1x) = (_a+_b+2.)*0.5; \ 61894e21283SToby Isaac (cnm2) = 0.; \ 61994e21283SToby Isaac } else { \ 62094e21283SToby Isaac PetscReal _2n = _n+_n; \ 62194e21283SToby Isaac PetscReal _d = (_2n*(_n+_a+_b)*(_2n+_a+_b-2)); \ 62294e21283SToby Isaac PetscReal _n1 = (_2n+_a+_b-1.)*(_a*_a-_b*_b); \ 62394e21283SToby Isaac PetscReal _n1x = (_2n+_a+_b-1.)*(_2n+_a+_b)*(_2n+_a+_b-2); \ 62494e21283SToby Isaac PetscReal _n2 = 2.*((_n+_a-1.)*(_n+_b-1.)*(_2n+_a+_b)); \ 62594e21283SToby Isaac (cnm1) = _n1 / _d; \ 62694e21283SToby Isaac (cnm1x) = _n1x / _d; \ 62794e21283SToby Isaac (cnm2) = _n2 / _d; \ 62894e21283SToby Isaac } \ 62994e21283SToby Isaac } while (0) 63094e21283SToby Isaac 631*fbdc3dfeSToby Isaac /*@ 632*fbdc3dfeSToby Isaac PetscDTJacobiNorm - Compute the weighted L2 norm of a Jacobi polynomial. 633*fbdc3dfeSToby Isaac 634*fbdc3dfeSToby Isaac $\| P^{\alpha,\beta}_n \|_{\alpha,\beta}^2 = \int_{-1}^1 (1 + x)^{\alpha} (1 - x)^{\beta} P^{\alpha,\beta}_n (x)^2 dx.$ 635*fbdc3dfeSToby Isaac 636*fbdc3dfeSToby Isaac Input Arguments: 637*fbdc3dfeSToby Isaac - alpha - the left exponent > -1 638*fbdc3dfeSToby Isaac . beta - the right exponent > -1 639*fbdc3dfeSToby Isaac + n - the polynomial degree 640*fbdc3dfeSToby Isaac 641*fbdc3dfeSToby Isaac Output Arguments: 642*fbdc3dfeSToby Isaac . norm - the weighted L2 norm 643*fbdc3dfeSToby Isaac 644*fbdc3dfeSToby Isaac Level: beginner 645*fbdc3dfeSToby Isaac 646*fbdc3dfeSToby Isaac .seealso: PetscDTJacobiEval() 647*fbdc3dfeSToby Isaac @*/ 648*fbdc3dfeSToby Isaac PetscErrorCode PetscDTJacobiNorm(PetscReal alpha, PetscReal beta, PetscInt n, PetscReal *norm) 649*fbdc3dfeSToby Isaac { 650*fbdc3dfeSToby Isaac PetscReal twoab1; 651*fbdc3dfeSToby Isaac PetscReal gr; 652*fbdc3dfeSToby Isaac 653*fbdc3dfeSToby Isaac PetscFunctionBegin; 654*fbdc3dfeSToby Isaac if (alpha <= -1.) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Exponent alpha %g <= -1. invalid\n", (double) alpha); 655*fbdc3dfeSToby Isaac if (beta <= -1.) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Exponent beta %g <= -1. invalid\n", (double) beta); 656*fbdc3dfeSToby Isaac if (n < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "n %D < 0 invalid\n", n); 657*fbdc3dfeSToby Isaac twoab1 = PetscPowReal(2., alpha + beta + 1.); 658*fbdc3dfeSToby Isaac #if defined(PETSC_HAVE_LGAMMA) 659*fbdc3dfeSToby Isaac if (!n) { 660*fbdc3dfeSToby Isaac gr = PetscExpReal(PetscLGamma(alpha+1.) + PetscLGamma(beta+1.) - PetscLGamma(alpha+beta+2.)); 661*fbdc3dfeSToby Isaac } else { 662*fbdc3dfeSToby Isaac gr = PetscExpReal(PetscLGamma(n+alpha+1.) + PetscLGamma(n+beta+1.) - (PetscLGamma(n+1.) + PetscLGamma(n+alpha+beta+1.))) / (n+n+alpha+beta+1.); 663*fbdc3dfeSToby Isaac } 664*fbdc3dfeSToby Isaac #else 665*fbdc3dfeSToby Isaac { 666*fbdc3dfeSToby Isaac PetscInt alphai = (PetscInt) alpha; 667*fbdc3dfeSToby Isaac PetscInt betai = (PetscInt) beta; 668*fbdc3dfeSToby Isaac PetscInt i; 669*fbdc3dfeSToby Isaac 670*fbdc3dfeSToby Isaac gr = n ? (1. / (n+n+alpha+beta+1.)) : 1.; 671*fbdc3dfeSToby Isaac if ((PetscReal) alphai == alpha) { 672*fbdc3dfeSToby Isaac if (!n) { 673*fbdc3dfeSToby Isaac for (i = 0; i < alphai; i++) gr *= (i+1.) / (beta+i+1.); 674*fbdc3dfeSToby Isaac gr /= (alpha+beta+1.); 675*fbdc3dfeSToby Isaac } else { 676*fbdc3dfeSToby Isaac for (i = 0; i < alphai; i++) gr *= (n+i+1.) / (n+beta+i+1.); 677*fbdc3dfeSToby Isaac } 678*fbdc3dfeSToby Isaac } else if ((PetscReal) betai == beta) { 679*fbdc3dfeSToby Isaac if (!n) { 680*fbdc3dfeSToby Isaac for (i = 0; i < betai; i++) gr *= (i+1.) / (alpha+i+2.); 681*fbdc3dfeSToby Isaac gr /= (alpha+beta+1.); 682*fbdc3dfeSToby Isaac } else { 683*fbdc3dfeSToby Isaac for (i = 0; i < betai; i++) gr *= (n+i+1.) / (n+alpha+i+1.); 684*fbdc3dfeSToby Isaac } 685*fbdc3dfeSToby Isaac } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"lgamma() - math routine is unavailable."); 686*fbdc3dfeSToby Isaac } 687*fbdc3dfeSToby Isaac #endif 688*fbdc3dfeSToby Isaac *norm = PetscSqrtReal(twoab1 * gr); 689*fbdc3dfeSToby Isaac PetscFunctionReturn(0); 690*fbdc3dfeSToby Isaac } 691*fbdc3dfeSToby 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 /*@ 749*fbdc3dfeSToby 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$. 750*fbdc3dfeSToby Isaac 751*fbdc3dfeSToby Isaac Input Arguments: 752*fbdc3dfeSToby Isaac + alpha - the left exponent of the weight 753*fbdc3dfeSToby Isaac . beta - the right exponetn of the weight 754*fbdc3dfeSToby Isaac . npoints - the number of points to evaluate the polynomials at 755*fbdc3dfeSToby Isaac . points - [npoints] array of point coordinates 756*fbdc3dfeSToby Isaac . degree - the maximm degree polynomial space to evaluate, (degree + 1) will be evaluated total. 757*fbdc3dfeSToby Isaac - k - the maximum derivative to evaluate in the jet, (k + 1) will be evaluated total. 758*fbdc3dfeSToby Isaac 759*fbdc3dfeSToby Isaac Output Argments: 760*fbdc3dfeSToby Isaac - p - an array containing the evaluations of the Jacobi polynomials's jets on the points. the size is (degree + 1) x 761*fbdc3dfeSToby Isaac (k + 1) x npoints, which also describes the order of the dimensions of this three-dimensional array: the first 762*fbdc3dfeSToby Isaac (slowest varying) dimension is polynomial degree; the second dimension is derivative order; the third (fastest 763*fbdc3dfeSToby Isaac varying) dimension is the index of the evaluation point. 764*fbdc3dfeSToby Isaac 765*fbdc3dfeSToby Isaac Level: advanced 766*fbdc3dfeSToby Isaac 767*fbdc3dfeSToby Isaac .seealso: PetscDTJacobiEval(), PetscDTPKDEvalJet() 768*fbdc3dfeSToby Isaac @*/ 769*fbdc3dfeSToby Isaac PetscErrorCode PetscDTJacobiEvalJet(PetscReal alpha, PetscReal beta, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt k, PetscReal p[]) 770*fbdc3dfeSToby Isaac { 771*fbdc3dfeSToby Isaac PetscInt i, j, l; 772*fbdc3dfeSToby Isaac PetscInt *degrees; 773*fbdc3dfeSToby Isaac PetscReal *psingle; 774*fbdc3dfeSToby Isaac PetscErrorCode ierr; 775*fbdc3dfeSToby Isaac 776*fbdc3dfeSToby Isaac PetscFunctionBegin; 777*fbdc3dfeSToby Isaac if (degree == 0) { 778*fbdc3dfeSToby Isaac PetscInt zero = 0; 779*fbdc3dfeSToby Isaac 780*fbdc3dfeSToby Isaac for (i = 0; i <= k; i++) { 781*fbdc3dfeSToby Isaac ierr = PetscDTJacobiEval_Internal(npoints, alpha, beta, i, points, 1, &zero, &p[i*npoints]);CHKERRQ(ierr); 782*fbdc3dfeSToby Isaac } 783*fbdc3dfeSToby Isaac PetscFunctionReturn(0); 784*fbdc3dfeSToby Isaac } 785*fbdc3dfeSToby Isaac ierr = PetscMalloc1(degree + 1, °rees);CHKERRQ(ierr); 786*fbdc3dfeSToby Isaac ierr = PetscMalloc1((degree + 1) * npoints, &psingle);CHKERRQ(ierr); 787*fbdc3dfeSToby Isaac for (i = 0; i <= degree; i++) degrees[i] = i; 788*fbdc3dfeSToby Isaac for (i = 0; i <= k; i++) { 789*fbdc3dfeSToby Isaac ierr = PetscDTJacobiEval_Internal(npoints, alpha, beta, i, points, degree + 1, degrees, psingle);CHKERRQ(ierr); 790*fbdc3dfeSToby Isaac for (j = 0; j <= degree; j++) { 791*fbdc3dfeSToby Isaac for (l = 0; l < npoints; l++) { 792*fbdc3dfeSToby Isaac p[(j * (k + 1) + i) * npoints + l] = psingle[l * (degree + 1) + j]; 793*fbdc3dfeSToby Isaac } 794*fbdc3dfeSToby Isaac } 795*fbdc3dfeSToby Isaac } 796*fbdc3dfeSToby Isaac ierr = PetscFree(psingle);CHKERRQ(ierr); 797*fbdc3dfeSToby Isaac ierr = PetscFree(degrees);CHKERRQ(ierr); 798*fbdc3dfeSToby Isaac PetscFunctionReturn(0); 799*fbdc3dfeSToby Isaac } 800*fbdc3dfeSToby Isaac 801*fbdc3dfeSToby Isaac /*@ 80294e21283SToby Isaac PetscDTJacobiEval - evaluate Jacobi polynomials for the weight function $(1.+x)^{\alpha} (1.-x)^{\beta}$ 80394e21283SToby Isaac at points 80494e21283SToby Isaac 80594e21283SToby Isaac Not Collective 80694e21283SToby Isaac 80794e21283SToby Isaac Input Arguments: 80894e21283SToby Isaac + npoints - number of spatial points to evaluate at 80994e21283SToby Isaac . alpha - the left exponent > -1 81094e21283SToby Isaac . beta - the right exponent > -1 81194e21283SToby Isaac . points - array of locations to evaluate at 81294e21283SToby Isaac . ndegree - number of basis degrees to evaluate 81394e21283SToby Isaac - degrees - sorted array of degrees to evaluate 81494e21283SToby Isaac 81594e21283SToby Isaac Output Arguments: 81694e21283SToby Isaac + B - row-oriented basis evaluation matrix B[point*ndegree + degree] (dimension npoints*ndegrees, allocated by caller) (or NULL) 81794e21283SToby Isaac . D - row-oriented derivative evaluation matrix (or NULL) 81894e21283SToby Isaac - D2 - row-oriented second derivative evaluation matrix (or NULL) 81994e21283SToby Isaac 82094e21283SToby Isaac Level: intermediate 82194e21283SToby Isaac 82294e21283SToby Isaac .seealso: PetscDTGaussQuadrature() 82394e21283SToby Isaac @*/ 82494e21283SToby Isaac PetscErrorCode PetscDTJacobiEval(PetscInt npoints,PetscReal alpha, PetscReal beta, const PetscReal *points,PetscInt ndegree,const PetscInt *degrees,PetscReal *B,PetscReal *D,PetscReal *D2) 82594e21283SToby Isaac { 82694e21283SToby Isaac PetscErrorCode ierr; 82794e21283SToby Isaac 82894e21283SToby Isaac PetscFunctionBegin; 82994e21283SToby Isaac if (alpha <= -1.) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"alpha must be > -1."); 83094e21283SToby Isaac if (beta <= -1.) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"beta must be > -1."); 83194e21283SToby Isaac if (!npoints || !ndegree) PetscFunctionReturn(0); 83294e21283SToby Isaac if (B) {ierr = PetscDTJacobiEval_Internal(npoints, alpha, beta, 0, points, ndegree, degrees, B);CHKERRQ(ierr);} 83394e21283SToby Isaac if (D) {ierr = PetscDTJacobiEval_Internal(npoints, alpha, beta, 1, points, ndegree, degrees, D);CHKERRQ(ierr);} 83494e21283SToby Isaac if (D2) {ierr = PetscDTJacobiEval_Internal(npoints, alpha, beta, 2, points, ndegree, degrees, D2);CHKERRQ(ierr);} 83594e21283SToby Isaac PetscFunctionReturn(0); 83694e21283SToby Isaac } 83794e21283SToby Isaac 83894e21283SToby Isaac /*@ 83994e21283SToby Isaac PetscDTLegendreEval - evaluate Legendre polynomials at points 84037045ce4SJed Brown 84137045ce4SJed Brown Not Collective 84237045ce4SJed Brown 84337045ce4SJed Brown Input Arguments: 84437045ce4SJed Brown + npoints - number of spatial points to evaluate at 84537045ce4SJed Brown . points - array of locations to evaluate at 84637045ce4SJed Brown . ndegree - number of basis degrees to evaluate 84737045ce4SJed Brown - degrees - sorted array of degrees to evaluate 84837045ce4SJed Brown 84937045ce4SJed Brown Output Arguments: 8500298fd71SBarry Smith + B - row-oriented basis evaluation matrix B[point*ndegree + degree] (dimension npoints*ndegrees, allocated by caller) (or NULL) 8510298fd71SBarry Smith . D - row-oriented derivative evaluation matrix (or NULL) 8520298fd71SBarry Smith - D2 - row-oriented second derivative evaluation matrix (or NULL) 85337045ce4SJed Brown 85437045ce4SJed Brown Level: intermediate 85537045ce4SJed Brown 85637045ce4SJed Brown .seealso: PetscDTGaussQuadrature() 85737045ce4SJed Brown @*/ 85837045ce4SJed Brown PetscErrorCode PetscDTLegendreEval(PetscInt npoints,const PetscReal *points,PetscInt ndegree,const PetscInt *degrees,PetscReal *B,PetscReal *D,PetscReal *D2) 85937045ce4SJed Brown { 86094e21283SToby Isaac PetscErrorCode ierr; 86137045ce4SJed Brown 86237045ce4SJed Brown PetscFunctionBegin; 86394e21283SToby Isaac ierr = PetscDTJacobiEval(npoints, 0., 0., points, ndegree, degrees, B, D, D2);CHKERRQ(ierr); 86437045ce4SJed Brown PetscFunctionReturn(0); 86537045ce4SJed Brown } 86637045ce4SJed Brown 867*fbdc3dfeSToby Isaac /*@ 868*fbdc3dfeSToby 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) 869*fbdc3dfeSToby Isaac 870*fbdc3dfeSToby Isaac Input Parameters: 871*fbdc3dfeSToby Isaac + len - the desired length of the degree tuple 872*fbdc3dfeSToby Isaac - index - the index to convert: should be >= 0 873*fbdc3dfeSToby Isaac 874*fbdc3dfeSToby Isaac Output Parameter: 875*fbdc3dfeSToby Isaac . degtup - will be filled with a tuple of degrees 876*fbdc3dfeSToby Isaac 877*fbdc3dfeSToby Isaac Level: beginner 878*fbdc3dfeSToby Isaac 879*fbdc3dfeSToby Isaac Note: for two tuples x and y with the same degree sum, partial degree sums over the final elements of the tuples 880*fbdc3dfeSToby 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 881*fbdc3dfeSToby Isaac last two elements is smaller for the former, so (2, 1, 1) < (1, 2, 1). 882*fbdc3dfeSToby Isaac 883*fbdc3dfeSToby Isaac .seealso: PetscDTGradedOrderToIndex() 884*fbdc3dfeSToby Isaac @*/ 885*fbdc3dfeSToby Isaac PetscErrorCode PetscDTIndexToGradedOrder(PetscInt len, PetscInt index, PetscInt degtup[]) 886*fbdc3dfeSToby Isaac { 887*fbdc3dfeSToby Isaac PetscInt i, total; 888*fbdc3dfeSToby Isaac PetscInt sum; 889*fbdc3dfeSToby Isaac 890*fbdc3dfeSToby Isaac PetscFunctionBeginHot; 891*fbdc3dfeSToby Isaac if (len < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative"); 892*fbdc3dfeSToby Isaac if (index < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index must be non-negative"); 893*fbdc3dfeSToby Isaac total = 1; 894*fbdc3dfeSToby Isaac sum = 0; 895*fbdc3dfeSToby Isaac while (index >= total) { 896*fbdc3dfeSToby Isaac index -= total; 897*fbdc3dfeSToby Isaac total = (total * (len + sum)) / (sum + 1); 898*fbdc3dfeSToby Isaac sum++; 899*fbdc3dfeSToby Isaac } 900*fbdc3dfeSToby Isaac for (i = 0; i < len; i++) { 901*fbdc3dfeSToby Isaac PetscInt c; 902*fbdc3dfeSToby Isaac 903*fbdc3dfeSToby Isaac degtup[i] = sum; 904*fbdc3dfeSToby Isaac for (c = 0, total = 1; c < sum; c++) { 905*fbdc3dfeSToby Isaac /* going into the loop, total is the number of way to have a tuple of sum exactly c with length len - 1 - i */ 906*fbdc3dfeSToby Isaac if (index < total) break; 907*fbdc3dfeSToby Isaac index -= total; 908*fbdc3dfeSToby Isaac total = (total * (len - 1 - i + c)) / (c + 1); 909*fbdc3dfeSToby Isaac degtup[i]--; 910*fbdc3dfeSToby Isaac } 911*fbdc3dfeSToby Isaac sum -= degtup[i]; 912*fbdc3dfeSToby Isaac } 913*fbdc3dfeSToby Isaac PetscFunctionReturn(0); 914*fbdc3dfeSToby Isaac } 915*fbdc3dfeSToby Isaac 916*fbdc3dfeSToby Isaac /*@ 917*fbdc3dfeSToby Isaac PetscDTGradedOrderToIndex - convert a tuple into an index in a graded order, the inverse of PetscDTIndexToGradedOrder(). 918*fbdc3dfeSToby Isaac 919*fbdc3dfeSToby Isaac Input Parameters: 920*fbdc3dfeSToby Isaac + len - the length of the degree tuple 921*fbdc3dfeSToby Isaac - degtup - tuple with this length 922*fbdc3dfeSToby Isaac 923*fbdc3dfeSToby Isaac Output Parameter: 924*fbdc3dfeSToby Isaac . index - index in graded order: >= 0 925*fbdc3dfeSToby Isaac 926*fbdc3dfeSToby Isaac Level: Beginner 927*fbdc3dfeSToby Isaac 928*fbdc3dfeSToby Isaac Note: for two tuples x and y with the same degree sum, partial degree sums over the final elements of the tuples 929*fbdc3dfeSToby 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 930*fbdc3dfeSToby Isaac last two elements is smaller for the former, so (2, 1, 1) < (1, 2, 1). 931*fbdc3dfeSToby Isaac 932*fbdc3dfeSToby Isaac .seealso: PetscDTIndexToGradedOrder() 933*fbdc3dfeSToby Isaac @*/ 934*fbdc3dfeSToby Isaac PetscErrorCode PetscDTGradedOrderToIndex(PetscInt len, const PetscInt degtup[], PetscInt *index) 935*fbdc3dfeSToby Isaac { 936*fbdc3dfeSToby Isaac PetscInt i, idx, sum, total; 937*fbdc3dfeSToby Isaac 938*fbdc3dfeSToby Isaac PetscFunctionBeginHot; 939*fbdc3dfeSToby Isaac if (len < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative"); 940*fbdc3dfeSToby Isaac for (i = 0, sum = 0; i < len; i++) sum += degtup[i]; 941*fbdc3dfeSToby Isaac idx = 0; 942*fbdc3dfeSToby Isaac total = 1; 943*fbdc3dfeSToby Isaac for (i = 0; i < sum; i++) { 944*fbdc3dfeSToby Isaac idx += total; 945*fbdc3dfeSToby Isaac total = (total * (len + i)) / (i + 1); 946*fbdc3dfeSToby Isaac } 947*fbdc3dfeSToby Isaac for (i = 0; i < len - 1; i++) { 948*fbdc3dfeSToby Isaac PetscInt c; 949*fbdc3dfeSToby Isaac 950*fbdc3dfeSToby Isaac total = 1; 951*fbdc3dfeSToby Isaac sum -= degtup[i]; 952*fbdc3dfeSToby Isaac for (c = 0; c < sum; c++) { 953*fbdc3dfeSToby Isaac idx += total; 954*fbdc3dfeSToby Isaac total = (total * (len - 1 - i + c)) / (c + 1); 955*fbdc3dfeSToby Isaac } 956*fbdc3dfeSToby Isaac } 957*fbdc3dfeSToby Isaac *index = idx; 958*fbdc3dfeSToby Isaac PetscFunctionReturn(0); 959*fbdc3dfeSToby Isaac } 960*fbdc3dfeSToby Isaac 961*fbdc3dfeSToby Isaac /*@ 962*fbdc3dfeSToby Isaac PetscDTPKDEvalJet - Evaluate the jet (function and derivatives) of the Prioriol-Koornwinder-Dubiner (PKD) basis for 963*fbdc3dfeSToby Isaac the space of polynomials up to a given degree. The PKD basis is L2-orthonormal on the biunit simplex (which is used 964*fbdc3dfeSToby Isaac as the reference element for finite elements in PETSc), which makes it a stable basis to use for evaluating 965*fbdc3dfeSToby Isaac polynomials in that domain. 966*fbdc3dfeSToby Isaac 967*fbdc3dfeSToby Isaac Input Arguments: 968*fbdc3dfeSToby Isaac + dim - the number of variables in the multivariate polynomials 969*fbdc3dfeSToby Isaac . npoints - the number of points to evaluate the polynomials at 970*fbdc3dfeSToby Isaac . points - [npoints x dim] array of point coordinates 971*fbdc3dfeSToby 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. 972*fbdc3dfeSToby Isaac - k - the maximum order partial derivative to evaluate in the jet. There are (dim + k choose dim) partial derivatives 973*fbdc3dfeSToby Isaac in the jet. Choosing k = 0 means to evaluate just the function and no derivatives 974*fbdc3dfeSToby Isaac 975*fbdc3dfeSToby Isaac Output Argments: 976*fbdc3dfeSToby Isaac - p - an array containing the evaluations of the PKD polynomials' jets on the points. The size is ((dim + degree) 977*fbdc3dfeSToby Isaac choose dim) x ((dim + k) choose dim) x npoints, which also describes the order of the dimensions of this 978*fbdc3dfeSToby Isaac three-dimensional array: the first (slowest varying) dimension is basis function index; the second dimension is jet 979*fbdc3dfeSToby Isaac index; the third (fastest varying) dimension is the index of the evaluation point. 980*fbdc3dfeSToby Isaac 981*fbdc3dfeSToby Isaac Level: advanced 982*fbdc3dfeSToby Isaac 983*fbdc3dfeSToby Isaac Note: The ordering of the basis functions, and the ordering of the derivatives in the jet, both follow the graded 984*fbdc3dfeSToby Isaac ordering of PetscDTIndexToGradedOrder() and PetscDTGradedOrderToIndex(). For example, in 3D, the polynomial with 985*fbdc3dfeSToby Isaac leading monomial x^3,y^1,z^2, which as degree tuple (2,0,1), which by PetscDTGradedOrderToIndex() has index 12 (it is the 13th basis function in the space); 986*fbdc3dfeSToby 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). 987*fbdc3dfeSToby Isaac 988*fbdc3dfeSToby Isaac .seealso: PetscDTGradedOrderToIndex(), PetscDTIndexToGradedOrder(), PetscDTJacobiEvalJet() 989*fbdc3dfeSToby Isaac @*/ 990*fbdc3dfeSToby Isaac PetscErrorCode PetscDTPKDEvalJet(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt k, PetscReal p[]) 991*fbdc3dfeSToby Isaac { 992*fbdc3dfeSToby Isaac PetscInt degidx, kidx, d, pt; 993*fbdc3dfeSToby Isaac PetscInt Nk, Ndeg; 994*fbdc3dfeSToby Isaac PetscInt *ktup, *degtup; 995*fbdc3dfeSToby Isaac PetscReal *scales, initscale, scaleexp; 996*fbdc3dfeSToby Isaac PetscErrorCode ierr; 997*fbdc3dfeSToby Isaac 998*fbdc3dfeSToby Isaac PetscFunctionBegin; 999*fbdc3dfeSToby Isaac ierr = PetscDTBinomialInt(dim + k, k, &Nk);CHKERRQ(ierr); 1000*fbdc3dfeSToby Isaac ierr = PetscDTBinomialInt(degree + dim, degree, &Ndeg);CHKERRQ(ierr); 1001*fbdc3dfeSToby Isaac ierr = PetscMalloc2(dim, °tup, dim, &ktup);CHKERRQ(ierr); 1002*fbdc3dfeSToby Isaac ierr = PetscMalloc1(Ndeg, &scales);CHKERRQ(ierr); 1003*fbdc3dfeSToby Isaac initscale = 1.; 1004*fbdc3dfeSToby Isaac if (dim > 1) { 1005*fbdc3dfeSToby Isaac ierr = PetscDTBinomial(dim,2,&scaleexp);CHKERRQ(ierr); 1006*fbdc3dfeSToby Isaac initscale = PetscPowReal(2.,scaleexp*0.5);CHKERRQ(ierr); 1007*fbdc3dfeSToby Isaac } 1008*fbdc3dfeSToby Isaac for (degidx = 0; degidx < Ndeg; degidx++) { 1009*fbdc3dfeSToby Isaac PetscInt e, i; 1010*fbdc3dfeSToby Isaac PetscInt m1idx = -1, m2idx = -1; 1011*fbdc3dfeSToby Isaac PetscInt n; 1012*fbdc3dfeSToby Isaac PetscInt degsum; 1013*fbdc3dfeSToby Isaac PetscReal alpha; 1014*fbdc3dfeSToby Isaac PetscReal cnm1, cnm1x, cnm2; 1015*fbdc3dfeSToby Isaac PetscReal norm; 1016*fbdc3dfeSToby Isaac 1017*fbdc3dfeSToby Isaac ierr = PetscDTIndexToGradedOrder(dim, degidx, degtup);CHKERRQ(ierr); 1018*fbdc3dfeSToby Isaac for (d = dim - 1; d >= 0; d--) if (degtup[d]) break; 1019*fbdc3dfeSToby Isaac if (d < 0) { /* constant is 1 everywhere, all derivatives are zero */ 1020*fbdc3dfeSToby Isaac scales[degidx] = initscale; 1021*fbdc3dfeSToby Isaac for (e = 0; e < dim; e++) { 1022*fbdc3dfeSToby Isaac ierr = PetscDTJacobiNorm(e,0.,0,&norm);CHKERRQ(ierr); 1023*fbdc3dfeSToby Isaac scales[degidx] /= norm; 1024*fbdc3dfeSToby Isaac } 1025*fbdc3dfeSToby Isaac for (i = 0; i < npoints; i++) p[degidx * Nk * npoints + i] = 1.; 1026*fbdc3dfeSToby Isaac for (i = 0; i < (Nk - 1) * npoints; i++) p[(degidx * Nk + 1) * npoints + i] = 0.; 1027*fbdc3dfeSToby Isaac continue; 1028*fbdc3dfeSToby Isaac } 1029*fbdc3dfeSToby Isaac n = degtup[d]; 1030*fbdc3dfeSToby Isaac degtup[d]--; 1031*fbdc3dfeSToby Isaac ierr = PetscDTGradedOrderToIndex(dim, degtup, &m1idx);CHKERRQ(ierr); 1032*fbdc3dfeSToby Isaac if (degtup[d] > 0) { 1033*fbdc3dfeSToby Isaac degtup[d]--; 1034*fbdc3dfeSToby Isaac ierr = PetscDTGradedOrderToIndex(dim, degtup, &m2idx);CHKERRQ(ierr); 1035*fbdc3dfeSToby Isaac degtup[d]++; 1036*fbdc3dfeSToby Isaac } 1037*fbdc3dfeSToby Isaac degtup[d]++; 1038*fbdc3dfeSToby Isaac for (e = 0, degsum = 0; e < d; e++) degsum += degtup[e]; 1039*fbdc3dfeSToby Isaac alpha = 2 * degsum + d; 1040*fbdc3dfeSToby Isaac PetscDTJacobiRecurrence_Internal(n,alpha,0.,cnm1,cnm1x,cnm2); 1041*fbdc3dfeSToby Isaac 1042*fbdc3dfeSToby Isaac 1043*fbdc3dfeSToby Isaac scales[degidx] = initscale; 1044*fbdc3dfeSToby Isaac for (e = 0, degsum = 0; e < dim; e++) { 1045*fbdc3dfeSToby Isaac PetscInt f; 1046*fbdc3dfeSToby Isaac PetscReal ealpha; 1047*fbdc3dfeSToby Isaac PetscReal enorm; 1048*fbdc3dfeSToby Isaac 1049*fbdc3dfeSToby Isaac ealpha = 2 * degsum + e; 1050*fbdc3dfeSToby Isaac for (f = 0; f < degsum; f++) scales[degidx] *= 2.; 1051*fbdc3dfeSToby Isaac ierr = PetscDTJacobiNorm(ealpha,0.,degtup[e],&enorm);CHKERRQ(ierr); 1052*fbdc3dfeSToby Isaac scales[degidx] /= enorm; 1053*fbdc3dfeSToby Isaac degsum += degtup[e]; 1054*fbdc3dfeSToby Isaac } 1055*fbdc3dfeSToby Isaac 1056*fbdc3dfeSToby Isaac for (pt = 0; pt < npoints; pt++) { 1057*fbdc3dfeSToby Isaac /* compute the multipliers */ 1058*fbdc3dfeSToby Isaac PetscReal thetanm1, thetanm1x, thetanm2; 1059*fbdc3dfeSToby Isaac 1060*fbdc3dfeSToby Isaac thetanm1x = dim - (d+1) + 2.*points[pt * dim + d]; 1061*fbdc3dfeSToby Isaac for (e = d+1; e < dim; e++) thetanm1x += points[pt * dim + e]; 1062*fbdc3dfeSToby Isaac thetanm1x *= 0.5; 1063*fbdc3dfeSToby Isaac thetanm1 = (2. - (dim-(d+1))); 1064*fbdc3dfeSToby Isaac for (e = d+1; e < dim; e++) thetanm1 -= points[pt * dim + e]; 1065*fbdc3dfeSToby Isaac thetanm1 *= 0.5; 1066*fbdc3dfeSToby Isaac thetanm2 = thetanm1 * thetanm1; 1067*fbdc3dfeSToby Isaac 1068*fbdc3dfeSToby Isaac for (kidx = 0; kidx < Nk; kidx++) { 1069*fbdc3dfeSToby Isaac PetscInt f; 1070*fbdc3dfeSToby Isaac 1071*fbdc3dfeSToby Isaac ierr = PetscDTIndexToGradedOrder(dim, kidx, ktup);CHKERRQ(ierr); 1072*fbdc3dfeSToby Isaac /* first sum in the same derivative terms */ 1073*fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] = (cnm1 * thetanm1 + cnm1x * thetanm1x) * p[(m1idx * Nk + kidx) * npoints + pt]; 1074*fbdc3dfeSToby Isaac if (m2idx >= 0) { 1075*fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] -= cnm2 * thetanm2 * p[(m2idx * Nk + kidx) * npoints + pt]; 1076*fbdc3dfeSToby Isaac } 1077*fbdc3dfeSToby Isaac 1078*fbdc3dfeSToby Isaac for (f = d; f < dim; f++) { 1079*fbdc3dfeSToby Isaac PetscInt km1idx, mplty = ktup[f]; 1080*fbdc3dfeSToby Isaac 1081*fbdc3dfeSToby Isaac if (!mplty) continue; 1082*fbdc3dfeSToby Isaac ktup[f]--; 1083*fbdc3dfeSToby Isaac ierr = PetscDTGradedOrderToIndex(dim, ktup, &km1idx);CHKERRQ(ierr); 1084*fbdc3dfeSToby Isaac 1085*fbdc3dfeSToby Isaac /* the derivative of cnm1x * thetanm1x wrt x variable f is 0.5 * cnm1x if f > d otherwise it is cnm1x */ 1086*fbdc3dfeSToby Isaac /* the derivative of cnm1 * thetanm1 wrt x variable f is 0 if f == d, otherwise it is -0.5 * cnm1 */ 1087*fbdc3dfeSToby Isaac /* the derivative of -cnm2 * thetanm2 wrt x variable f is 0 if f == d, otherwise it is cnm2 * thetanm1 */ 1088*fbdc3dfeSToby Isaac if (f > d) { 1089*fbdc3dfeSToby Isaac PetscInt f2; 1090*fbdc3dfeSToby Isaac 1091*fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] += mplty * 0.5 * (cnm1x - cnm1) * p[(m1idx * Nk + km1idx) * npoints + pt]; 1092*fbdc3dfeSToby Isaac if (m2idx >= 0) { 1093*fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] += mplty * cnm2 * thetanm1 * p[(m2idx * Nk + km1idx) * npoints + pt]; 1094*fbdc3dfeSToby Isaac } 1095*fbdc3dfeSToby Isaac /* second derivatives of -cnm2 * thetanm2 wrt x variable f,f2 is like - 0.5 * cnm2 */ 1096*fbdc3dfeSToby Isaac for (f2 = f; f2 < dim; f2++) { 1097*fbdc3dfeSToby Isaac PetscInt km2idx, mplty2 = ktup[f2]; 1098*fbdc3dfeSToby Isaac PetscInt factor; 1099*fbdc3dfeSToby Isaac 1100*fbdc3dfeSToby Isaac if (!mplty2) continue; 1101*fbdc3dfeSToby Isaac ktup[f2]--; 1102*fbdc3dfeSToby Isaac ierr = PetscDTGradedOrderToIndex(dim, ktup, &km2idx);CHKERRQ(ierr); 1103*fbdc3dfeSToby Isaac 1104*fbdc3dfeSToby Isaac factor = mplty * mplty2; 1105*fbdc3dfeSToby Isaac if (f == f2) factor /= 2; 1106*fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] -= 0.5 * factor * cnm2 * p[(m2idx * Nk + km2idx) * npoints + pt]; 1107*fbdc3dfeSToby Isaac ktup[f2]++; 1108*fbdc3dfeSToby Isaac } 1109*fbdc3dfeSToby Isaac } else { 1110*fbdc3dfeSToby Isaac p[(degidx * Nk + kidx) * npoints + pt] += mplty * cnm1x * p[(m1idx * Nk + km1idx) * npoints + pt]; 1111*fbdc3dfeSToby Isaac } 1112*fbdc3dfeSToby Isaac ktup[f]++; 1113*fbdc3dfeSToby Isaac } 1114*fbdc3dfeSToby Isaac } 1115*fbdc3dfeSToby Isaac } 1116*fbdc3dfeSToby Isaac } 1117*fbdc3dfeSToby Isaac for (degidx = 0; degidx < Ndeg; degidx++) { 1118*fbdc3dfeSToby Isaac PetscReal scale = scales[degidx]; 1119*fbdc3dfeSToby Isaac PetscInt i; 1120*fbdc3dfeSToby Isaac 1121*fbdc3dfeSToby Isaac for (i = 0; i < Nk * npoints; i++) p[degidx*Nk*npoints + i] *= scale; 1122*fbdc3dfeSToby Isaac } 1123*fbdc3dfeSToby Isaac ierr = PetscFree(scales);CHKERRQ(ierr); 1124*fbdc3dfeSToby Isaac ierr = PetscFree2(degtup, ktup);CHKERRQ(ierr); 1125*fbdc3dfeSToby Isaac PetscFunctionReturn(0); 1126*fbdc3dfeSToby Isaac } 1127*fbdc3dfeSToby Isaac 1128e6a796c3SToby Isaac /* solve the symmetric tridiagonal eigenvalue system, writing the eigenvalues into eigs and the eigenvectors into V 1129e6a796c3SToby Isaac * with lds n; diag and subdiag are overwritten */ 1130e6a796c3SToby Isaac static PetscErrorCode PetscDTSymmetricTridiagonalEigensolve(PetscInt n, PetscReal diag[], PetscReal subdiag[], 1131e6a796c3SToby Isaac PetscReal eigs[], PetscScalar V[]) 1132e6a796c3SToby Isaac { 1133e6a796c3SToby Isaac char jobz = 'V'; /* eigenvalues and eigenvectors */ 1134e6a796c3SToby Isaac char range = 'A'; /* all eigenvalues will be found */ 1135e6a796c3SToby Isaac PetscReal VL = 0.; /* ignored because range is 'A' */ 1136e6a796c3SToby Isaac PetscReal VU = 0.; /* ignored because range is 'A' */ 1137e6a796c3SToby Isaac PetscBLASInt IL = 0; /* ignored because range is 'A' */ 1138e6a796c3SToby Isaac PetscBLASInt IU = 0; /* ignored because range is 'A' */ 1139e6a796c3SToby Isaac PetscReal abstol = 0.; /* unused */ 1140e6a796c3SToby Isaac PetscBLASInt bn, bm, ldz; /* bm will equal bn on exit */ 1141e6a796c3SToby Isaac PetscBLASInt *isuppz; 1142e6a796c3SToby Isaac PetscBLASInt lwork, liwork; 1143e6a796c3SToby Isaac PetscReal workquery; 1144e6a796c3SToby Isaac PetscBLASInt iworkquery; 1145e6a796c3SToby Isaac PetscBLASInt *iwork; 1146e6a796c3SToby Isaac PetscBLASInt info; 1147e6a796c3SToby Isaac PetscReal *work = NULL; 1148e6a796c3SToby Isaac PetscErrorCode ierr; 1149e6a796c3SToby Isaac 1150e6a796c3SToby Isaac PetscFunctionBegin; 1151e6a796c3SToby Isaac #if !defined(PETSCDTGAUSSIANQUADRATURE_EIG) 1152e6a796c3SToby Isaac SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "A LAPACK symmetric tridiagonal eigensolver could not be found"); 1153e6a796c3SToby Isaac #endif 1154e6a796c3SToby Isaac ierr = PetscBLASIntCast(n, &bn);CHKERRQ(ierr); 1155e6a796c3SToby Isaac ierr = PetscBLASIntCast(n, &ldz);CHKERRQ(ierr); 1156e6a796c3SToby Isaac #if !defined(PETSC_MISSING_LAPACK_STEGR) 1157e6a796c3SToby Isaac ierr = PetscMalloc1(2 * n, &isuppz);CHKERRQ(ierr); 1158e6a796c3SToby Isaac lwork = -1; 1159e6a796c3SToby Isaac liwork = -1; 1160e6a796c3SToby Isaac PetscStackCallBLAS("LAPACKstegr",LAPACKstegr_(&jobz,&range,&bn,diag,subdiag,&VL,&VU,&IL,&IU,&abstol,&bm,eigs,V,&ldz,isuppz,&workquery,&lwork,&iworkquery,&liwork,&info)); 1161e6a796c3SToby Isaac if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEGR error"); 1162e6a796c3SToby Isaac lwork = (PetscBLASInt) workquery; 1163e6a796c3SToby Isaac liwork = (PetscBLASInt) iworkquery; 1164e6a796c3SToby Isaac ierr = PetscMalloc2(lwork, &work, liwork, &iwork);CHKERRQ(ierr); 1165e6a796c3SToby Isaac ierr = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr); 1166e6a796c3SToby Isaac PetscStackCallBLAS("LAPACKstegr",LAPACKstegr_(&jobz,&range,&bn,diag,subdiag,&VL,&VU,&IL,&IU,&abstol,&bm,eigs,V,&ldz,isuppz,work,&lwork,iwork,&liwork,&info)); 1167e6a796c3SToby Isaac ierr = PetscFPTrapPop();CHKERRQ(ierr); 1168e6a796c3SToby Isaac if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEGR error"); 1169e6a796c3SToby Isaac ierr = PetscFree2(work, iwork);CHKERRQ(ierr); 1170e6a796c3SToby Isaac ierr = PetscFree(isuppz);CHKERRQ(ierr); 1171e6a796c3SToby Isaac #elif !defined(PETSC_MISSING_LAPACK_STEQR) 1172e6a796c3SToby Isaac jobz = 'I'; /* Compute eigenvalues and eigenvectors of the 1173e6a796c3SToby Isaac tridiagonal matrix. Z is initialized to the identity 1174e6a796c3SToby Isaac matrix. */ 1175e6a796c3SToby Isaac ierr = PetscMalloc1(PetscMax(1,2*n-2),&work);CHKERRQ(ierr); 1176e6a796c3SToby Isaac PetscStackCallBLAS("LAPACKsteqr",LAPACKsteqr_("I",&bn,diag,subdiag,V,&ldz,work,&info)); 1177e6a796c3SToby Isaac ierr = PetscFPTrapPop();CHKERRQ(ierr); 1178e6a796c3SToby Isaac if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEQR error"); 1179e6a796c3SToby Isaac ierr = PetscFree(work);CHKERRQ(ierr); 1180e6a796c3SToby Isaac ierr = PetscArraycpy(eigs,diag,n);CHKERRQ(ierr); 1181e6a796c3SToby Isaac #endif 1182e6a796c3SToby Isaac PetscFunctionReturn(0); 1183e6a796c3SToby Isaac } 1184e6a796c3SToby Isaac 1185e6a796c3SToby Isaac /* Formula for the weights at the endpoints (-1 and 1) of Gauss-Lobatto-Jacobi 1186e6a796c3SToby Isaac * quadrature rules on the interval [-1, 1] */ 1187e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussLobattoJacobiEndweights_Internal(PetscInt n, PetscReal alpha, PetscReal beta, PetscReal *leftw, PetscReal *rightw) 1188e6a796c3SToby Isaac { 1189e6a796c3SToby Isaac PetscReal twoab1; 1190e6a796c3SToby Isaac PetscInt m = n - 2; 1191e6a796c3SToby Isaac PetscReal a = alpha + 1.; 1192e6a796c3SToby Isaac PetscReal b = beta + 1.; 1193e6a796c3SToby Isaac PetscReal gra, grb; 1194e6a796c3SToby Isaac 1195e6a796c3SToby Isaac PetscFunctionBegin; 1196e6a796c3SToby Isaac twoab1 = PetscPowReal(2., a + b - 1.); 1197e6a796c3SToby Isaac #if defined(PETSC_HAVE_LGAMMA) 1198e6a796c3SToby Isaac grb = PetscExpReal(2. * PetscLGamma(b+1.) + PetscLGamma(m+1.) + PetscLGamma(m+a+1.) - 1199e6a796c3SToby Isaac (PetscLGamma(m+b+1) + PetscLGamma(m+a+b+1.))); 1200e6a796c3SToby Isaac gra = PetscExpReal(2. * PetscLGamma(a+1.) + PetscLGamma(m+1.) + PetscLGamma(m+b+1.) - 1201e6a796c3SToby Isaac (PetscLGamma(m+a+1) + PetscLGamma(m+a+b+1.))); 1202e6a796c3SToby Isaac #else 1203e6a796c3SToby Isaac { 1204e6a796c3SToby Isaac PetscInt alphai = (PetscInt) alpha; 1205e6a796c3SToby Isaac PetscInt betai = (PetscInt) beta; 120694e21283SToby Isaac PetscErrorCode ierr; 1207e6a796c3SToby Isaac 1208e6a796c3SToby Isaac if ((PetscReal) alphai == alpha && (PetscReal) betai == beta) { 1209e6a796c3SToby Isaac PetscReal binom1, binom2; 1210e6a796c3SToby Isaac 1211e6a796c3SToby Isaac ierr = PetscDTBinomial(m+b, b, &binom1);CHKERRQ(ierr); 1212e6a796c3SToby Isaac ierr = PetscDTBinomial(m+a+b, b, &binom2);CHKERRQ(ierr); 1213e6a796c3SToby Isaac grb = 1./ (binom1 * binom2); 1214e6a796c3SToby Isaac ierr = PetscDTBinomial(m+a, a, &binom1);CHKERRQ(ierr); 1215e6a796c3SToby Isaac ierr = PetscDTBinomial(m+a+b, a, &binom2);CHKERRQ(ierr); 1216e6a796c3SToby Isaac gra = 1./ (binom1 * binom2); 1217e6a796c3SToby Isaac } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"lgamma() - math routine is unavailable."); 1218e6a796c3SToby Isaac } 1219e6a796c3SToby Isaac #endif 1220e6a796c3SToby Isaac *leftw = twoab1 * grb / b; 1221e6a796c3SToby Isaac *rightw = twoab1 * gra / a; 1222e6a796c3SToby Isaac PetscFunctionReturn(0); 1223e6a796c3SToby Isaac } 1224e6a796c3SToby Isaac 1225e6a796c3SToby Isaac /* Evaluates the nth jacobi polynomial with weight parameters a,b at a point x. 1226e6a796c3SToby Isaac Recurrence relations implemented from the pseudocode given in Karniadakis and Sherwin, Appendix B */ 1227e6a796c3SToby Isaac PETSC_STATIC_INLINE PetscErrorCode PetscDTComputeJacobi(PetscReal a, PetscReal b, PetscInt n, PetscReal x, PetscReal *P) 1228e6a796c3SToby Isaac { 122994e21283SToby Isaac PetscReal pn1, pn2; 123094e21283SToby Isaac PetscReal cnm1, cnm1x, cnm2; 1231e6a796c3SToby Isaac PetscInt k; 1232e6a796c3SToby Isaac 1233e6a796c3SToby Isaac PetscFunctionBegin; 1234e6a796c3SToby Isaac if (!n) {*P = 1.0; PetscFunctionReturn(0);} 123594e21283SToby Isaac PetscDTJacobiRecurrence_Internal(1,a,b,cnm1,cnm1x,cnm2); 123694e21283SToby Isaac pn2 = 1.; 123794e21283SToby Isaac pn1 = cnm1 + cnm1x*x; 123894e21283SToby Isaac if (n == 1) {*P = pn1; PetscFunctionReturn(0);} 1239e6a796c3SToby Isaac *P = 0.0; 1240e6a796c3SToby Isaac for (k = 2; k < n+1; ++k) { 124194e21283SToby Isaac PetscDTJacobiRecurrence_Internal(k,a,b,cnm1,cnm1x,cnm2); 1242e6a796c3SToby Isaac 124394e21283SToby Isaac *P = (cnm1 + cnm1x*x)*pn1 - cnm2*pn2; 1244e6a796c3SToby Isaac pn2 = pn1; 1245e6a796c3SToby Isaac pn1 = *P; 1246e6a796c3SToby Isaac } 1247e6a796c3SToby Isaac PetscFunctionReturn(0); 1248e6a796c3SToby Isaac } 1249e6a796c3SToby Isaac 1250e6a796c3SToby Isaac /* Evaluates the first derivative of P_{n}^{a,b} at a point x. */ 1251e6a796c3SToby Isaac PETSC_STATIC_INLINE PetscErrorCode PetscDTComputeJacobiDerivative(PetscReal a, PetscReal b, PetscInt n, PetscReal x, PetscInt k, PetscReal *P) 1252e6a796c3SToby Isaac { 1253e6a796c3SToby Isaac PetscReal nP; 1254e6a796c3SToby Isaac PetscInt i; 1255e6a796c3SToby Isaac PetscErrorCode ierr; 1256e6a796c3SToby Isaac 1257e6a796c3SToby Isaac PetscFunctionBegin; 1258e6a796c3SToby Isaac if (k > n) {*P = 0.0; PetscFunctionReturn(0);} 1259e6a796c3SToby Isaac ierr = PetscDTComputeJacobi(a+k, b+k, n-k, x, &nP);CHKERRQ(ierr); 1260e6a796c3SToby Isaac for (i = 0; i < k; i++) nP *= (a + b + n + 1. + i) * 0.5; 1261e6a796c3SToby Isaac *P = nP; 1262e6a796c3SToby Isaac PetscFunctionReturn(0); 1263e6a796c3SToby Isaac } 1264e6a796c3SToby Isaac 1265e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussJacobiQuadrature_Newton_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscReal x[], PetscReal w[]) 1266e6a796c3SToby Isaac { 1267e6a796c3SToby Isaac PetscInt maxIter = 100; 126894e21283SToby Isaac PetscReal eps = PetscExpReal(0.75 * PetscLogReal(PETSC_MACHINE_EPSILON)); 1269200b5abcSJed Brown PetscReal a1, a6, gf; 1270e6a796c3SToby Isaac PetscInt k; 1271e6a796c3SToby Isaac PetscErrorCode ierr; 1272e6a796c3SToby Isaac 1273e6a796c3SToby Isaac PetscFunctionBegin; 1274e6a796c3SToby Isaac 1275e6a796c3SToby Isaac a1 = PetscPowReal(2.0, a+b+1); 127694e21283SToby Isaac #if defined(PETSC_HAVE_LGAMMA) 1277200b5abcSJed Brown { 1278200b5abcSJed Brown PetscReal a2, a3, a4, a5; 127994e21283SToby Isaac a2 = PetscLGamma(a + npoints + 1); 128094e21283SToby Isaac a3 = PetscLGamma(b + npoints + 1); 128194e21283SToby Isaac a4 = PetscLGamma(a + b + npoints + 1); 128294e21283SToby Isaac a5 = PetscLGamma(npoints + 1); 128394e21283SToby Isaac gf = PetscExpReal(a2 + a3 - (a4 + a5)); 1284200b5abcSJed Brown } 1285e6a796c3SToby Isaac #else 1286e6a796c3SToby Isaac { 1287e6a796c3SToby Isaac PetscInt ia, ib; 1288e6a796c3SToby Isaac 1289e6a796c3SToby Isaac ia = (PetscInt) a; 1290e6a796c3SToby Isaac ib = (PetscInt) b; 129194e21283SToby Isaac gf = 1.; 129294e21283SToby Isaac if (ia == a && ia >= 0) { /* compute ratio of rising factorals wrt a */ 129394e21283SToby Isaac for (k = 0; k < ia; k++) gf *= (npoints + 1. + k) / (npoints + b + 1. + k); 129494e21283SToby Isaac } else if (b == b && ib >= 0) { /* compute ratio of rising factorials wrt b */ 129594e21283SToby Isaac for (k = 0; k < ib; k++) gf *= (npoints + 1. + k) / (npoints + a + 1. + k); 129694e21283SToby Isaac } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"lgamma() - math routine is unavailable."); 1297e6a796c3SToby Isaac } 1298e6a796c3SToby Isaac #endif 1299e6a796c3SToby Isaac 130094e21283SToby Isaac a6 = a1 * gf; 1301e6a796c3SToby Isaac /* Computes the m roots of P_{m}^{a,b} on [-1,1] by Newton's method with Chebyshev points as initial guesses. 1302e6a796c3SToby Isaac Algorithm implemented from the pseudocode given by Karniadakis and Sherwin and Python in FIAT */ 1303e6a796c3SToby Isaac for (k = 0; k < npoints; ++k) { 130494e21283SToby Isaac PetscReal r = PetscCosReal(PETSC_PI * (1. - (4.*k + 3. + 2.*b) / (4.*npoints + 2.*(a + b + 1.)))), dP; 1305e6a796c3SToby Isaac PetscInt j; 1306e6a796c3SToby Isaac 1307e6a796c3SToby Isaac if (k > 0) r = 0.5 * (r + x[k-1]); 1308e6a796c3SToby Isaac for (j = 0; j < maxIter; ++j) { 1309e6a796c3SToby Isaac PetscReal s = 0.0, delta, f, fp; 1310e6a796c3SToby Isaac PetscInt i; 1311e6a796c3SToby Isaac 1312e6a796c3SToby Isaac for (i = 0; i < k; ++i) s = s + 1.0 / (r - x[i]); 1313e6a796c3SToby Isaac ierr = PetscDTComputeJacobi(a, b, npoints, r, &f);CHKERRQ(ierr); 1314e6a796c3SToby Isaac ierr = PetscDTComputeJacobiDerivative(a, b, npoints, r, 1, &fp);CHKERRQ(ierr); 1315e6a796c3SToby Isaac delta = f / (fp - f * s); 1316e6a796c3SToby Isaac r = r - delta; 1317e6a796c3SToby Isaac if (PetscAbsReal(delta) < eps) break; 1318e6a796c3SToby Isaac } 1319e6a796c3SToby Isaac x[k] = r; 1320e6a796c3SToby Isaac ierr = PetscDTComputeJacobiDerivative(a, b, npoints, x[k], 1, &dP);CHKERRQ(ierr); 1321e6a796c3SToby Isaac w[k] = a6 / (1.0 - PetscSqr(x[k])) / PetscSqr(dP); 1322e6a796c3SToby Isaac } 1323e6a796c3SToby Isaac PetscFunctionReturn(0); 1324e6a796c3SToby Isaac } 1325e6a796c3SToby Isaac 132694e21283SToby Isaac /* Compute the diagonals of the Jacobi matrix used in Golub & Welsch algorithms for Gauss-Jacobi 1327e6a796c3SToby Isaac * quadrature weight calculations on [-1,1] for exponents (1. + x)^a (1.-x)^b */ 1328e6a796c3SToby Isaac static PetscErrorCode PetscDTJacobiMatrix_Internal(PetscInt nPoints, PetscReal a, PetscReal b, PetscReal *d, PetscReal *s) 1329e6a796c3SToby Isaac { 1330e6a796c3SToby Isaac PetscInt i; 1331e6a796c3SToby Isaac 1332e6a796c3SToby Isaac PetscFunctionBegin; 1333e6a796c3SToby Isaac for (i = 0; i < nPoints; i++) { 133494e21283SToby Isaac PetscReal A, B, C; 1335e6a796c3SToby Isaac 133694e21283SToby Isaac PetscDTJacobiRecurrence_Internal(i+1,a,b,A,B,C); 133794e21283SToby Isaac d[i] = -A / B; 133894e21283SToby Isaac if (i) s[i-1] *= C / B; 133994e21283SToby Isaac if (i < nPoints - 1) s[i] = 1. / B; 1340e6a796c3SToby Isaac } 1341e6a796c3SToby Isaac PetscFunctionReturn(0); 1342e6a796c3SToby Isaac } 1343e6a796c3SToby Isaac 1344e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussJacobiQuadrature_GolubWelsch_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscReal *x, PetscReal *w) 1345e6a796c3SToby Isaac { 1346e6a796c3SToby Isaac PetscReal mu0; 1347e6a796c3SToby Isaac PetscReal ga, gb, gab; 1348e6a796c3SToby Isaac PetscInt i; 1349e6a796c3SToby Isaac PetscErrorCode ierr; 1350e6a796c3SToby Isaac 1351e6a796c3SToby Isaac PetscFunctionBegin; 1352e6a796c3SToby Isaac ierr = PetscCitationsRegister(GolubWelschCitation, &GolubWelschCite);CHKERRQ(ierr); 1353e6a796c3SToby Isaac 1354e6a796c3SToby Isaac #if defined(PETSC_HAVE_TGAMMA) 1355e6a796c3SToby Isaac ga = PetscTGamma(a + 1); 1356e6a796c3SToby Isaac gb = PetscTGamma(b + 1); 1357e6a796c3SToby Isaac gab = PetscTGamma(a + b + 2); 1358e6a796c3SToby Isaac #else 1359e6a796c3SToby Isaac { 1360e6a796c3SToby Isaac PetscInt ia, ib; 1361e6a796c3SToby Isaac 1362e6a796c3SToby Isaac ia = (PetscInt) a; 1363e6a796c3SToby Isaac ib = (PetscInt) b; 1364e6a796c3SToby Isaac if (ia == a && ib == b && ia + 1 > 0 && ib + 1 > 0 && ia + ib + 2 > 0) { /* All gamma(x) terms are (x-1)! terms */ 1365e6a796c3SToby Isaac ierr = PetscDTFactorial(ia, &ga);CHKERRQ(ierr); 1366e6a796c3SToby Isaac ierr = PetscDTFactorial(ib, &gb);CHKERRQ(ierr); 1367e6a796c3SToby Isaac ierr = PetscDTFactorial(ia + ib + 1, &gb);CHKERRQ(ierr); 1368e6a796c3SToby Isaac } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"tgamma() - math routine is unavailable."); 1369e6a796c3SToby Isaac } 1370e6a796c3SToby Isaac #endif 1371e6a796c3SToby Isaac mu0 = PetscPowReal(2.,a + b + 1.) * ga * gb / gab; 1372e6a796c3SToby Isaac 1373e6a796c3SToby Isaac #if defined(PETSCDTGAUSSIANQUADRATURE_EIG) 1374e6a796c3SToby Isaac { 1375e6a796c3SToby Isaac PetscReal *diag, *subdiag; 1376e6a796c3SToby Isaac PetscScalar *V; 1377e6a796c3SToby Isaac 1378e6a796c3SToby Isaac ierr = PetscMalloc2(npoints, &diag, npoints, &subdiag);CHKERRQ(ierr); 1379e6a796c3SToby Isaac ierr = PetscMalloc1(npoints*npoints, &V);CHKERRQ(ierr); 1380e6a796c3SToby Isaac ierr = PetscDTJacobiMatrix_Internal(npoints, a, b, diag, subdiag);CHKERRQ(ierr); 1381e6a796c3SToby Isaac for (i = 0; i < npoints - 1; i++) subdiag[i] = PetscSqrtReal(subdiag[i]); 1382e6a796c3SToby Isaac ierr = PetscDTSymmetricTridiagonalEigensolve(npoints, diag, subdiag, x, V);CHKERRQ(ierr); 138394e21283SToby Isaac for (i = 0; i < npoints; i++) w[i] = PetscSqr(PetscRealPart(V[i * npoints])) * mu0; 1384e6a796c3SToby Isaac ierr = PetscFree(V);CHKERRQ(ierr); 1385e6a796c3SToby Isaac ierr = PetscFree2(diag, subdiag);CHKERRQ(ierr); 1386e6a796c3SToby Isaac } 1387e6a796c3SToby Isaac #else 1388e6a796c3SToby Isaac SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "A LAPACK symmetric tridiagonal eigensolver could not be found"); 1389e6a796c3SToby Isaac #endif 139094e21283SToby Isaac { /* As of March 2, 2020, The Sun Performance Library breaks the LAPACK contract for xstegr and xsteqr: the 139194e21283SToby Isaac eigenvalues are not guaranteed to be in ascending order. So we heave a passive aggressive sigh and check that 139294e21283SToby Isaac the eigenvalues are sorted */ 139394e21283SToby Isaac PetscBool sorted; 139494e21283SToby Isaac 139594e21283SToby Isaac ierr = PetscSortedReal(npoints, x, &sorted);CHKERRQ(ierr); 139694e21283SToby Isaac if (!sorted) { 139794e21283SToby Isaac PetscInt *order, i; 139894e21283SToby Isaac PetscReal *tmp; 139994e21283SToby Isaac 140094e21283SToby Isaac ierr = PetscMalloc2(npoints, &order, npoints, &tmp);CHKERRQ(ierr); 140194e21283SToby Isaac for (i = 0; i < npoints; i++) order[i] = i; 140294e21283SToby Isaac ierr = PetscSortRealWithPermutation(npoints, x, order);CHKERRQ(ierr); 140394e21283SToby Isaac ierr = PetscArraycpy(tmp, x, npoints);CHKERRQ(ierr); 140494e21283SToby Isaac for (i = 0; i < npoints; i++) x[i] = tmp[order[i]]; 140594e21283SToby Isaac ierr = PetscArraycpy(tmp, w, npoints);CHKERRQ(ierr); 140694e21283SToby Isaac for (i = 0; i < npoints; i++) w[i] = tmp[order[i]]; 140794e21283SToby Isaac ierr = PetscFree2(order, tmp);CHKERRQ(ierr); 140894e21283SToby Isaac } 140994e21283SToby Isaac } 1410e6a796c3SToby Isaac PetscFunctionReturn(0); 1411e6a796c3SToby Isaac } 1412e6a796c3SToby Isaac 1413e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussJacobiQuadrature_Internal(PetscInt npoints,PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[], PetscBool newton) 1414e6a796c3SToby Isaac { 1415e6a796c3SToby Isaac PetscErrorCode ierr; 1416e6a796c3SToby Isaac 1417e6a796c3SToby Isaac PetscFunctionBegin; 1418e6a796c3SToby Isaac if (npoints < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of points must be positive"); 1419e6a796c3SToby Isaac /* If asking for a 1D Lobatto point, just return the non-Lobatto 1D point */ 1420e6a796c3SToby Isaac if (alpha <= -1.) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"alpha must be > -1."); 1421e6a796c3SToby Isaac if (beta <= -1.) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"beta must be > -1."); 1422e6a796c3SToby Isaac 1423e6a796c3SToby Isaac if (newton) { 1424e6a796c3SToby Isaac ierr = PetscDTGaussJacobiQuadrature_Newton_Internal(npoints, alpha, beta, x, w);CHKERRQ(ierr); 1425e6a796c3SToby Isaac } else { 1426e6a796c3SToby Isaac ierr = PetscDTGaussJacobiQuadrature_GolubWelsch_Internal(npoints, alpha, beta, x, w);CHKERRQ(ierr); 1427e6a796c3SToby Isaac } 1428e6a796c3SToby Isaac if (alpha == beta) { /* symmetrize */ 1429e6a796c3SToby Isaac PetscInt i; 1430e6a796c3SToby Isaac for (i = 0; i < (npoints + 1) / 2; i++) { 1431e6a796c3SToby Isaac PetscInt j = npoints - 1 - i; 1432e6a796c3SToby Isaac PetscReal xi = x[i]; 1433e6a796c3SToby Isaac PetscReal xj = x[j]; 1434e6a796c3SToby Isaac PetscReal wi = w[i]; 1435e6a796c3SToby Isaac PetscReal wj = w[j]; 1436e6a796c3SToby Isaac 1437e6a796c3SToby Isaac x[i] = (xi - xj) / 2.; 1438e6a796c3SToby Isaac x[j] = (xj - xi) / 2.; 1439e6a796c3SToby Isaac w[i] = w[j] = (wi + wj) / 2.; 1440e6a796c3SToby Isaac } 1441e6a796c3SToby Isaac } 1442e6a796c3SToby Isaac PetscFunctionReturn(0); 1443e6a796c3SToby Isaac } 1444e6a796c3SToby Isaac 144594e21283SToby Isaac /*@ 144694e21283SToby Isaac PetscDTGaussJacobiQuadrature - quadrature for the interval [a, b] with the weight function 144794e21283SToby Isaac $(x-a)^\alpha (x-b)^\beta$. 144894e21283SToby Isaac 144994e21283SToby Isaac Not collective 145094e21283SToby Isaac 145194e21283SToby Isaac Input Parameters: 145294e21283SToby Isaac + npoints - the number of points in the quadrature rule 145394e21283SToby Isaac . a - the left endpoint of the interval 145494e21283SToby Isaac . b - the right endpoint of the interval 145594e21283SToby Isaac . alpha - the left exponent 145694e21283SToby Isaac - beta - the right exponent 145794e21283SToby Isaac 145894e21283SToby Isaac Output Parameters: 145994e21283SToby Isaac + x - array of length npoints, the locations of the quadrature points 146094e21283SToby Isaac - w - array of length npoints, the weights of the quadrature points 146194e21283SToby Isaac 146294e21283SToby Isaac Level: intermediate 146394e21283SToby Isaac 146494e21283SToby Isaac Note: this quadrature rule is exact for polynomials up to degree 2*npoints - 1. 146594e21283SToby Isaac @*/ 146694e21283SToby Isaac PetscErrorCode PetscDTGaussJacobiQuadrature(PetscInt npoints,PetscReal a, PetscReal b, PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[]) 1467e6a796c3SToby Isaac { 146894e21283SToby Isaac PetscInt i; 1469e6a796c3SToby Isaac PetscErrorCode ierr; 1470e6a796c3SToby Isaac 1471e6a796c3SToby Isaac PetscFunctionBegin; 147294e21283SToby Isaac ierr = PetscDTGaussJacobiQuadrature_Internal(npoints, alpha, beta, x, w, PetscDTGaussQuadratureNewton_Internal);CHKERRQ(ierr); 147394e21283SToby Isaac if (a != -1. || b != 1.) { /* shift */ 147494e21283SToby Isaac for (i = 0; i < npoints; i++) { 147594e21283SToby Isaac x[i] = (x[i] + 1.) * ((b - a) / 2.) + a; 147694e21283SToby Isaac w[i] *= (b - a) / 2.; 147794e21283SToby Isaac } 147894e21283SToby Isaac } 1479e6a796c3SToby Isaac PetscFunctionReturn(0); 1480e6a796c3SToby Isaac } 1481e6a796c3SToby Isaac 1482e6a796c3SToby Isaac static PetscErrorCode PetscDTGaussLobattoJacobiQuadrature_Internal(PetscInt npoints,PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[], PetscBool newton) 1483e6a796c3SToby Isaac { 1484e6a796c3SToby Isaac PetscInt i; 1485e6a796c3SToby Isaac PetscErrorCode ierr; 1486e6a796c3SToby Isaac 1487e6a796c3SToby Isaac PetscFunctionBegin; 1488e6a796c3SToby Isaac if (npoints < 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of points must be positive"); 1489e6a796c3SToby Isaac /* If asking for a 1D Lobatto point, just return the non-Lobatto 1D point */ 1490e6a796c3SToby Isaac if (alpha <= -1.) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"alpha must be > -1."); 1491e6a796c3SToby Isaac if (beta <= -1.) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"beta must be > -1."); 1492e6a796c3SToby Isaac 1493e6a796c3SToby Isaac x[0] = -1.; 1494e6a796c3SToby Isaac x[npoints-1] = 1.; 149594e21283SToby Isaac if (npoints > 2) { 149694e21283SToby Isaac ierr = PetscDTGaussJacobiQuadrature_Internal(npoints-2, alpha+1., beta+1., &x[1], &w[1], newton);CHKERRQ(ierr); 149794e21283SToby Isaac } 1498e6a796c3SToby Isaac for (i = 1; i < npoints - 1; i++) { 1499e6a796c3SToby Isaac w[i] /= (1. - x[i]*x[i]); 1500e6a796c3SToby Isaac } 1501e6a796c3SToby Isaac ierr = PetscDTGaussLobattoJacobiEndweights_Internal(npoints, alpha, beta, &w[0], &w[npoints-1]);CHKERRQ(ierr); 1502e6a796c3SToby Isaac PetscFunctionReturn(0); 1503e6a796c3SToby Isaac } 1504e6a796c3SToby Isaac 150537045ce4SJed Brown /*@ 150694e21283SToby Isaac PetscDTGaussLobattoJacobiQuadrature - quadrature for the interval [a, b] with the weight function 150794e21283SToby Isaac $(x-a)^\alpha (x-b)^\beta$, with endpoints a and b included as quadrature points. 150894e21283SToby Isaac 150994e21283SToby Isaac Not collective 151094e21283SToby Isaac 151194e21283SToby Isaac Input Parameters: 151294e21283SToby Isaac + npoints - the number of points in the quadrature rule 151394e21283SToby Isaac . a - the left endpoint of the interval 151494e21283SToby Isaac . b - the right endpoint of the interval 151594e21283SToby Isaac . alpha - the left exponent 151694e21283SToby Isaac - beta - the right exponent 151794e21283SToby Isaac 151894e21283SToby Isaac Output Parameters: 151994e21283SToby Isaac + x - array of length npoints, the locations of the quadrature points 152094e21283SToby Isaac - w - array of length npoints, the weights of the quadrature points 152194e21283SToby Isaac 152294e21283SToby Isaac Level: intermediate 152394e21283SToby Isaac 152494e21283SToby Isaac Note: this quadrature rule is exact for polynomials up to degree 2*npoints - 3. 152594e21283SToby Isaac @*/ 152694e21283SToby Isaac PetscErrorCode PetscDTGaussLobattoJacobiQuadrature(PetscInt npoints,PetscReal a, PetscReal b, PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[]) 152794e21283SToby Isaac { 152894e21283SToby Isaac PetscInt i; 152994e21283SToby Isaac PetscErrorCode ierr; 153094e21283SToby Isaac 153194e21283SToby Isaac PetscFunctionBegin; 153294e21283SToby Isaac ierr = PetscDTGaussLobattoJacobiQuadrature_Internal(npoints, alpha, beta, x, w, PetscDTGaussQuadratureNewton_Internal);CHKERRQ(ierr); 153394e21283SToby Isaac if (a != -1. || b != 1.) { /* shift */ 153494e21283SToby Isaac for (i = 0; i < npoints; i++) { 153594e21283SToby Isaac x[i] = (x[i] + 1.) * ((b - a) / 2.) + a; 153694e21283SToby Isaac w[i] *= (b - a) / 2.; 153794e21283SToby Isaac } 153894e21283SToby Isaac } 153994e21283SToby Isaac PetscFunctionReturn(0); 154094e21283SToby Isaac } 154194e21283SToby Isaac 154294e21283SToby Isaac /*@ 1543e6a796c3SToby Isaac PetscDTGaussQuadrature - create Gauss-Legendre quadrature 154437045ce4SJed Brown 154537045ce4SJed Brown Not Collective 154637045ce4SJed Brown 154737045ce4SJed Brown Input Arguments: 154837045ce4SJed Brown + npoints - number of points 154937045ce4SJed Brown . a - left end of interval (often-1) 155037045ce4SJed Brown - b - right end of interval (often +1) 155137045ce4SJed Brown 155237045ce4SJed Brown Output Arguments: 155337045ce4SJed Brown + x - quadrature points 155437045ce4SJed Brown - w - quadrature weights 155537045ce4SJed Brown 155637045ce4SJed Brown Level: intermediate 155737045ce4SJed Brown 155837045ce4SJed Brown References: 155996a0c994SBarry Smith . 1. - Golub and Welsch, Calculation of Quadrature Rules, Math. Comp. 23(106), 1969. 156037045ce4SJed Brown 156137045ce4SJed Brown .seealso: PetscDTLegendreEval() 156237045ce4SJed Brown @*/ 156337045ce4SJed Brown PetscErrorCode PetscDTGaussQuadrature(PetscInt npoints,PetscReal a,PetscReal b,PetscReal *x,PetscReal *w) 156437045ce4SJed Brown { 156537045ce4SJed Brown PetscInt i; 1566e6a796c3SToby Isaac PetscErrorCode ierr; 156737045ce4SJed Brown 156837045ce4SJed Brown PetscFunctionBegin; 156994e21283SToby Isaac ierr = PetscDTGaussJacobiQuadrature_Internal(npoints, 0., 0., x, w, PetscDTGaussQuadratureNewton_Internal);CHKERRQ(ierr); 157094e21283SToby Isaac if (a != -1. || b != 1.) { /* shift */ 157137045ce4SJed Brown for (i = 0; i < npoints; i++) { 1572e6a796c3SToby Isaac x[i] = (x[i] + 1.) * ((b - a) / 2.) + a; 1573e6a796c3SToby Isaac w[i] *= (b - a) / 2.; 157437045ce4SJed Brown } 157537045ce4SJed Brown } 157637045ce4SJed Brown PetscFunctionReturn(0); 157737045ce4SJed Brown } 1578194825f6SJed Brown 15798272889dSSatish Balay /*@C 15808272889dSSatish Balay PetscDTGaussLobattoLegendreQuadrature - creates a set of the locations and weights of the Gauss-Lobatto-Legendre 15818272889dSSatish Balay nodes of a given size on the domain [-1,1] 15828272889dSSatish Balay 15838272889dSSatish Balay Not Collective 15848272889dSSatish Balay 15858272889dSSatish Balay Input Parameter: 15868272889dSSatish Balay + n - number of grid nodes 1587f2e8fe4dShannah_mairs - type - PETSCGAUSSLOBATTOLEGENDRE_VIA_LINEAR_ALGEBRA or PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON 15888272889dSSatish Balay 15898272889dSSatish Balay Output Arguments: 15908272889dSSatish Balay + x - quadrature points 15918272889dSSatish Balay - w - quadrature weights 15928272889dSSatish Balay 15938272889dSSatish Balay Notes: 15948272889dSSatish Balay For n > 30 the Newton approach computes duplicate (incorrect) values for some nodes because the initial guess is apparently not 15958272889dSSatish Balay close enough to the desired solution 15968272889dSSatish Balay 15978272889dSSatish Balay These are useful for implementing spectral methods based on Gauss-Lobatto-Legendre (GLL) nodes 15988272889dSSatish Balay 1599a8d69d7bSBarry 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 16008272889dSSatish Balay 16018272889dSSatish Balay Level: intermediate 16028272889dSSatish Balay 16038272889dSSatish Balay .seealso: PetscDTGaussQuadrature() 16048272889dSSatish Balay 16058272889dSSatish Balay @*/ 1606916e780bShannah_mairs PetscErrorCode PetscDTGaussLobattoLegendreQuadrature(PetscInt npoints,PetscGaussLobattoLegendreCreateType type,PetscReal *x,PetscReal *w) 16078272889dSSatish Balay { 1608e6a796c3SToby Isaac PetscBool newton; 16098272889dSSatish Balay PetscErrorCode ierr; 16108272889dSSatish Balay 16118272889dSSatish Balay PetscFunctionBegin; 16128272889dSSatish Balay if (npoints < 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must provide at least 2 grid points per element"); 161394e21283SToby Isaac newton = (PetscBool) (type == PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON); 1614e6a796c3SToby Isaac ierr = PetscDTGaussLobattoJacobiQuadrature_Internal(npoints, 0., 0., x, w, newton);CHKERRQ(ierr); 16158272889dSSatish Balay PetscFunctionReturn(0); 16168272889dSSatish Balay } 16178272889dSSatish Balay 1618744bafbcSMatthew G. Knepley /*@ 1619744bafbcSMatthew G. Knepley PetscDTGaussTensorQuadrature - creates a tensor-product Gauss quadrature 1620744bafbcSMatthew G. Knepley 1621744bafbcSMatthew G. Knepley Not Collective 1622744bafbcSMatthew G. Knepley 1623744bafbcSMatthew G. Knepley Input Arguments: 1624744bafbcSMatthew G. Knepley + dim - The spatial dimension 1625a6b92713SMatthew G. Knepley . Nc - The number of components 1626744bafbcSMatthew G. Knepley . npoints - number of points in one dimension 1627744bafbcSMatthew G. Knepley . a - left end of interval (often-1) 1628744bafbcSMatthew G. Knepley - b - right end of interval (often +1) 1629744bafbcSMatthew G. Knepley 1630744bafbcSMatthew G. Knepley Output Argument: 1631744bafbcSMatthew G. Knepley . q - A PetscQuadrature object 1632744bafbcSMatthew G. Knepley 1633744bafbcSMatthew G. Knepley Level: intermediate 1634744bafbcSMatthew G. Knepley 1635744bafbcSMatthew G. Knepley .seealso: PetscDTGaussQuadrature(), PetscDTLegendreEval() 1636744bafbcSMatthew G. Knepley @*/ 1637a6b92713SMatthew G. Knepley PetscErrorCode PetscDTGaussTensorQuadrature(PetscInt dim, PetscInt Nc, PetscInt npoints, PetscReal a, PetscReal b, PetscQuadrature *q) 1638744bafbcSMatthew G. Knepley { 1639a6b92713SMatthew G. Knepley PetscInt totpoints = dim > 1 ? dim > 2 ? npoints*PetscSqr(npoints) : PetscSqr(npoints) : npoints, i, j, k, c; 1640744bafbcSMatthew G. Knepley PetscReal *x, *w, *xw, *ww; 1641744bafbcSMatthew G. Knepley PetscErrorCode ierr; 1642744bafbcSMatthew G. Knepley 1643744bafbcSMatthew G. Knepley PetscFunctionBegin; 1644744bafbcSMatthew G. Knepley ierr = PetscMalloc1(totpoints*dim,&x);CHKERRQ(ierr); 1645a6b92713SMatthew G. Knepley ierr = PetscMalloc1(totpoints*Nc,&w);CHKERRQ(ierr); 1646744bafbcSMatthew G. Knepley /* Set up the Golub-Welsch system */ 1647744bafbcSMatthew G. Knepley switch (dim) { 1648744bafbcSMatthew G. Knepley case 0: 1649744bafbcSMatthew G. Knepley ierr = PetscFree(x);CHKERRQ(ierr); 1650744bafbcSMatthew G. Knepley ierr = PetscFree(w);CHKERRQ(ierr); 1651744bafbcSMatthew G. Knepley ierr = PetscMalloc1(1, &x);CHKERRQ(ierr); 1652a6b92713SMatthew G. Knepley ierr = PetscMalloc1(Nc, &w);CHKERRQ(ierr); 1653744bafbcSMatthew G. Knepley x[0] = 0.0; 1654a6b92713SMatthew G. Knepley for (c = 0; c < Nc; ++c) w[c] = 1.0; 1655744bafbcSMatthew G. Knepley break; 1656744bafbcSMatthew G. Knepley case 1: 1657a6b92713SMatthew G. Knepley ierr = PetscMalloc1(npoints,&ww);CHKERRQ(ierr); 1658a6b92713SMatthew G. Knepley ierr = PetscDTGaussQuadrature(npoints, a, b, x, ww);CHKERRQ(ierr); 1659a6b92713SMatthew G. Knepley for (i = 0; i < npoints; ++i) for (c = 0; c < Nc; ++c) w[i*Nc+c] = ww[i]; 1660a6b92713SMatthew G. Knepley ierr = PetscFree(ww);CHKERRQ(ierr); 1661744bafbcSMatthew G. Knepley break; 1662744bafbcSMatthew G. Knepley case 2: 1663744bafbcSMatthew G. Knepley ierr = PetscMalloc2(npoints,&xw,npoints,&ww);CHKERRQ(ierr); 1664744bafbcSMatthew G. Knepley ierr = PetscDTGaussQuadrature(npoints, a, b, xw, ww);CHKERRQ(ierr); 1665744bafbcSMatthew G. Knepley for (i = 0; i < npoints; ++i) { 1666744bafbcSMatthew G. Knepley for (j = 0; j < npoints; ++j) { 1667744bafbcSMatthew G. Knepley x[(i*npoints+j)*dim+0] = xw[i]; 1668744bafbcSMatthew G. Knepley x[(i*npoints+j)*dim+1] = xw[j]; 1669a6b92713SMatthew G. Knepley for (c = 0; c < Nc; ++c) w[(i*npoints+j)*Nc+c] = ww[i] * ww[j]; 1670744bafbcSMatthew G. Knepley } 1671744bafbcSMatthew G. Knepley } 1672744bafbcSMatthew G. Knepley ierr = PetscFree2(xw,ww);CHKERRQ(ierr); 1673744bafbcSMatthew G. Knepley break; 1674744bafbcSMatthew G. Knepley case 3: 1675744bafbcSMatthew G. Knepley ierr = PetscMalloc2(npoints,&xw,npoints,&ww);CHKERRQ(ierr); 1676744bafbcSMatthew G. Knepley ierr = PetscDTGaussQuadrature(npoints, a, b, xw, ww);CHKERRQ(ierr); 1677744bafbcSMatthew G. Knepley for (i = 0; i < npoints; ++i) { 1678744bafbcSMatthew G. Knepley for (j = 0; j < npoints; ++j) { 1679744bafbcSMatthew G. Knepley for (k = 0; k < npoints; ++k) { 1680744bafbcSMatthew G. Knepley x[((i*npoints+j)*npoints+k)*dim+0] = xw[i]; 1681744bafbcSMatthew G. Knepley x[((i*npoints+j)*npoints+k)*dim+1] = xw[j]; 1682744bafbcSMatthew G. Knepley x[((i*npoints+j)*npoints+k)*dim+2] = xw[k]; 1683a6b92713SMatthew G. Knepley for (c = 0; c < Nc; ++c) w[((i*npoints+j)*npoints+k)*Nc+c] = ww[i] * ww[j] * ww[k]; 1684744bafbcSMatthew G. Knepley } 1685744bafbcSMatthew G. Knepley } 1686744bafbcSMatthew G. Knepley } 1687744bafbcSMatthew G. Knepley ierr = PetscFree2(xw,ww);CHKERRQ(ierr); 1688744bafbcSMatthew G. Knepley break; 1689744bafbcSMatthew G. Knepley default: 1690744bafbcSMatthew G. Knepley SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct quadrature rule for dimension %d", dim); 1691744bafbcSMatthew G. Knepley } 1692744bafbcSMatthew G. Knepley ierr = PetscQuadratureCreate(PETSC_COMM_SELF, q);CHKERRQ(ierr); 16932f5fb066SToby Isaac ierr = PetscQuadratureSetOrder(*q, 2*npoints-1);CHKERRQ(ierr); 1694a6b92713SMatthew G. Knepley ierr = PetscQuadratureSetData(*q, dim, Nc, totpoints, x, w);CHKERRQ(ierr); 1695d9bac1caSLisandro Dalcin ierr = PetscObjectChangeTypeName((PetscObject)*q,"GaussTensor");CHKERRQ(ierr); 1696744bafbcSMatthew G. Knepley PetscFunctionReturn(0); 1697744bafbcSMatthew G. Knepley } 1698744bafbcSMatthew G. Knepley 1699f5f57ec0SBarry Smith /*@ 1700e6a796c3SToby Isaac PetscDTStroudConicalQuadrature - create Stroud conical quadrature for a simplex 1701494e7359SMatthew G. Knepley 1702494e7359SMatthew G. Knepley Not Collective 1703494e7359SMatthew G. Knepley 1704494e7359SMatthew G. Knepley Input Arguments: 1705494e7359SMatthew G. Knepley + dim - The simplex dimension 1706a6b92713SMatthew G. Knepley . Nc - The number of components 1707dcce0ee2SMatthew G. Knepley . npoints - The number of points in one dimension 1708494e7359SMatthew G. Knepley . a - left end of interval (often-1) 1709494e7359SMatthew G. Knepley - b - right end of interval (often +1) 1710494e7359SMatthew G. Knepley 1711744bafbcSMatthew G. Knepley Output Argument: 1712552aa4f7SMatthew G. Knepley . q - A PetscQuadrature object 1713494e7359SMatthew G. Knepley 1714494e7359SMatthew G. Knepley Level: intermediate 1715494e7359SMatthew G. Knepley 1716494e7359SMatthew G. Knepley References: 171796a0c994SBarry Smith . 1. - Karniadakis and Sherwin. FIAT 1718494e7359SMatthew G. Knepley 1719e6a796c3SToby Isaac Note: For dim == 1, this is Gauss-Legendre quadrature 1720e6a796c3SToby Isaac 1721744bafbcSMatthew G. Knepley .seealso: PetscDTGaussTensorQuadrature(), PetscDTGaussQuadrature() 1722494e7359SMatthew G. Knepley @*/ 1723e6a796c3SToby Isaac PetscErrorCode PetscDTStroudConicalQuadrature(PetscInt dim, PetscInt Nc, PetscInt npoints, PetscReal a, PetscReal b, PetscQuadrature *q) 1724494e7359SMatthew G. Knepley { 1725*fbdc3dfeSToby Isaac PetscInt totprev, totrem; 1726*fbdc3dfeSToby Isaac PetscInt totpoints; 1727*fbdc3dfeSToby Isaac PetscReal *p1, *w1; 1728*fbdc3dfeSToby Isaac PetscReal *x, *w; 1729*fbdc3dfeSToby Isaac PetscInt i, j, k, l, m, pt, c; 1730*fbdc3dfeSToby Isaac PetscErrorCode ierr; 1731494e7359SMatthew G. Knepley 1732494e7359SMatthew G. Knepley PetscFunctionBegin; 1733494e7359SMatthew G. Knepley if ((a != -1.0) || (b != 1.0)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must use default internal right now"); 1734*fbdc3dfeSToby Isaac totpoints = 1; 1735*fbdc3dfeSToby Isaac for (i = 0, totpoints = 1; i < dim; i++) totpoints *= npoints; 1736dcce0ee2SMatthew G. Knepley ierr = PetscMalloc1(totpoints*dim, &x);CHKERRQ(ierr); 1737dcce0ee2SMatthew G. Knepley ierr = PetscMalloc1(totpoints*Nc, &w);CHKERRQ(ierr); 1738*fbdc3dfeSToby Isaac ierr = PetscMalloc2(npoints, &p1, npoints, &w1);CHKERRQ(ierr); 1739*fbdc3dfeSToby Isaac for (i = 0; i < totpoints*Nc; i++) w[i] = 1.; 1740*fbdc3dfeSToby Isaac for (i = 0, totprev = 1, totrem = totpoints / npoints; i < dim; i++) { 1741*fbdc3dfeSToby Isaac PetscReal mul; 1742*fbdc3dfeSToby Isaac 1743*fbdc3dfeSToby Isaac mul = PetscPowReal(2.,-i); 1744*fbdc3dfeSToby Isaac ierr = PetscDTGaussJacobiQuadrature(npoints, -1., 1., i, 0.0, p1, w1);CHKERRQ(ierr); 1745*fbdc3dfeSToby Isaac for (pt = 0, l = 0; l < totprev; l++) { 1746*fbdc3dfeSToby Isaac for (j = 0; j < npoints; j++) { 1747*fbdc3dfeSToby Isaac for (m = 0; m < totrem; m++, pt++) { 1748*fbdc3dfeSToby Isaac for (k = 0; k < i; k++) x[pt*dim+k] = (x[pt*dim+k]+1.)*(1.-p1[j])*0.5 - 1.; 1749*fbdc3dfeSToby Isaac x[pt * dim + i] = p1[j]; 1750*fbdc3dfeSToby Isaac for (c = 0; c < Nc; c++) w[pt*Nc + c] *= mul * w1[j]; 1751494e7359SMatthew G. Knepley } 1752494e7359SMatthew G. Knepley } 1753494e7359SMatthew G. Knepley } 1754*fbdc3dfeSToby Isaac totprev *= npoints; 1755*fbdc3dfeSToby Isaac totrem /= npoints; 1756494e7359SMatthew G. Knepley } 1757*fbdc3dfeSToby Isaac ierr = PetscFree2(p1, w1);CHKERRQ(ierr); 175821454ff5SMatthew G. Knepley ierr = PetscQuadratureCreate(PETSC_COMM_SELF, q);CHKERRQ(ierr); 17592f5fb066SToby Isaac ierr = PetscQuadratureSetOrder(*q, 2*npoints-1);CHKERRQ(ierr); 1760dcce0ee2SMatthew G. Knepley ierr = PetscQuadratureSetData(*q, dim, Nc, totpoints, x, w);CHKERRQ(ierr); 1761*fbdc3dfeSToby Isaac ierr = PetscObjectChangeTypeName((PetscObject)*q,"StroudConical");CHKERRQ(ierr); 1762494e7359SMatthew G. Knepley PetscFunctionReturn(0); 1763494e7359SMatthew G. Knepley } 1764494e7359SMatthew G. Knepley 1765f5f57ec0SBarry Smith /*@ 1766b3c0f97bSTom Klotz PetscDTTanhSinhTensorQuadrature - create tanh-sinh quadrature for a tensor product cell 1767b3c0f97bSTom Klotz 1768b3c0f97bSTom Klotz Not Collective 1769b3c0f97bSTom Klotz 1770b3c0f97bSTom Klotz Input Arguments: 1771b3c0f97bSTom Klotz + dim - The cell dimension 1772b3c0f97bSTom Klotz . level - The number of points in one dimension, 2^l 1773b3c0f97bSTom Klotz . a - left end of interval (often-1) 1774b3c0f97bSTom Klotz - b - right end of interval (often +1) 1775b3c0f97bSTom Klotz 1776b3c0f97bSTom Klotz Output Argument: 1777b3c0f97bSTom Klotz . q - A PetscQuadrature object 1778b3c0f97bSTom Klotz 1779b3c0f97bSTom Klotz Level: intermediate 1780b3c0f97bSTom Klotz 1781b3c0f97bSTom Klotz .seealso: PetscDTGaussTensorQuadrature() 1782b3c0f97bSTom Klotz @*/ 1783b3c0f97bSTom Klotz PetscErrorCode PetscDTTanhSinhTensorQuadrature(PetscInt dim, PetscInt level, PetscReal a, PetscReal b, PetscQuadrature *q) 1784b3c0f97bSTom Klotz { 1785b3c0f97bSTom Klotz const PetscInt p = 16; /* Digits of precision in the evaluation */ 1786b3c0f97bSTom Klotz const PetscReal alpha = (b-a)/2.; /* Half-width of the integration interval */ 1787b3c0f97bSTom Klotz const PetscReal beta = (b+a)/2.; /* Center of the integration interval */ 1788b3c0f97bSTom Klotz const PetscReal h = PetscPowReal(2.0, -level); /* Step size, length between x_k */ 1789d84b4d08SMatthew G. Knepley PetscReal xk; /* Quadrature point x_k on reference domain [-1, 1] */ 1790b3c0f97bSTom Klotz PetscReal wk = 0.5*PETSC_PI; /* Quadrature weight at x_k */ 1791b3c0f97bSTom Klotz PetscReal *x, *w; 1792b3c0f97bSTom Klotz PetscInt K, k, npoints; 1793b3c0f97bSTom Klotz PetscErrorCode ierr; 1794b3c0f97bSTom Klotz 1795b3c0f97bSTom Klotz PetscFunctionBegin; 1796b3c0f97bSTom Klotz if (dim > 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %d not yet implemented", dim); 1797b3c0f97bSTom Klotz if (!level) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a number of significant digits"); 1798b3c0f97bSTom Klotz /* Find K such that the weights are < 32 digits of precision */ 1799b3c0f97bSTom Klotz for (K = 1; PetscAbsReal(PetscLog10Real(wk)) < 2*p; ++K) { 18009add2064SThomas Klotz wk = 0.5*h*PETSC_PI*PetscCoshReal(K*h)/PetscSqr(PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(K*h))); 1801b3c0f97bSTom Klotz } 1802b3c0f97bSTom Klotz ierr = PetscQuadratureCreate(PETSC_COMM_SELF, q);CHKERRQ(ierr); 1803b3c0f97bSTom Klotz ierr = PetscQuadratureSetOrder(*q, 2*K+1);CHKERRQ(ierr); 1804b3c0f97bSTom Klotz npoints = 2*K-1; 1805b3c0f97bSTom Klotz ierr = PetscMalloc1(npoints*dim, &x);CHKERRQ(ierr); 1806b3c0f97bSTom Klotz ierr = PetscMalloc1(npoints, &w);CHKERRQ(ierr); 1807b3c0f97bSTom Klotz /* Center term */ 1808b3c0f97bSTom Klotz x[0] = beta; 1809b3c0f97bSTom Klotz w[0] = 0.5*alpha*PETSC_PI; 1810b3c0f97bSTom Klotz for (k = 1; k < K; ++k) { 18119add2064SThomas Klotz wk = 0.5*alpha*h*PETSC_PI*PetscCoshReal(k*h)/PetscSqr(PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(k*h))); 18121118d4bcSLisandro Dalcin xk = PetscTanhReal(0.5*PETSC_PI*PetscSinhReal(k*h)); 1813b3c0f97bSTom Klotz x[2*k-1] = -alpha*xk+beta; 1814b3c0f97bSTom Klotz w[2*k-1] = wk; 1815b3c0f97bSTom Klotz x[2*k+0] = alpha*xk+beta; 1816b3c0f97bSTom Klotz w[2*k+0] = wk; 1817b3c0f97bSTom Klotz } 1818a6b92713SMatthew G. Knepley ierr = PetscQuadratureSetData(*q, dim, 1, npoints, x, w);CHKERRQ(ierr); 1819b3c0f97bSTom Klotz PetscFunctionReturn(0); 1820b3c0f97bSTom Klotz } 1821b3c0f97bSTom Klotz 1822b3c0f97bSTom Klotz PetscErrorCode PetscDTTanhSinhIntegrate(void (*func)(PetscReal, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, PetscReal *sol) 1823b3c0f97bSTom Klotz { 1824b3c0f97bSTom Klotz const PetscInt p = 16; /* Digits of precision in the evaluation */ 1825b3c0f97bSTom Klotz const PetscReal alpha = (b-a)/2.; /* Half-width of the integration interval */ 1826b3c0f97bSTom Klotz const PetscReal beta = (b+a)/2.; /* Center of the integration interval */ 1827b3c0f97bSTom Klotz PetscReal h = 1.0; /* Step size, length between x_k */ 1828b3c0f97bSTom Klotz PetscInt l = 0; /* Level of refinement, h = 2^{-l} */ 1829b3c0f97bSTom Klotz PetscReal osum = 0.0; /* Integral on last level */ 1830b3c0f97bSTom Klotz PetscReal psum = 0.0; /* Integral on the level before the last level */ 1831b3c0f97bSTom Klotz PetscReal sum; /* Integral on current level */ 1832446c295cSMatthew G. Knepley PetscReal yk; /* Quadrature point 1 - x_k on reference domain [-1, 1] */ 1833b3c0f97bSTom Klotz PetscReal lx, rx; /* Quadrature points to the left and right of 0 on the real domain [a, b] */ 1834b3c0f97bSTom Klotz PetscReal wk; /* Quadrature weight at x_k */ 1835b3c0f97bSTom Klotz PetscReal lval, rval; /* Terms in the quadature sum to the left and right of 0 */ 1836b3c0f97bSTom Klotz PetscInt d; /* Digits of precision in the integral */ 1837b3c0f97bSTom Klotz 1838b3c0f97bSTom Klotz PetscFunctionBegin; 1839b3c0f97bSTom Klotz if (digits <= 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a positive number of significant digits"); 1840b3c0f97bSTom Klotz /* Center term */ 1841b3c0f97bSTom Klotz func(beta, &lval); 1842b3c0f97bSTom Klotz sum = 0.5*alpha*PETSC_PI*lval; 1843b3c0f97bSTom Klotz /* */ 1844b3c0f97bSTom Klotz do { 1845b3c0f97bSTom Klotz PetscReal lterm, rterm, maxTerm = 0.0, d1, d2, d3, d4; 1846b3c0f97bSTom Klotz PetscInt k = 1; 1847b3c0f97bSTom Klotz 1848b3c0f97bSTom Klotz ++l; 1849b3c0f97bSTom Klotz /* PetscPrintf(PETSC_COMM_SELF, "LEVEL %D sum: %15.15f\n", l, sum); */ 1850b3c0f97bSTom Klotz /* At each level of refinement, h --> h/2 and sum --> sum/2 */ 1851b3c0f97bSTom Klotz psum = osum; 1852b3c0f97bSTom Klotz osum = sum; 1853b3c0f97bSTom Klotz h *= 0.5; 1854b3c0f97bSTom Klotz sum *= 0.5; 1855b3c0f97bSTom Klotz do { 18569add2064SThomas Klotz wk = 0.5*h*PETSC_PI*PetscCoshReal(k*h)/PetscSqr(PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(k*h))); 1857446c295cSMatthew G. Knepley yk = 1.0/(PetscExpReal(0.5*PETSC_PI*PetscSinhReal(k*h)) * PetscCoshReal(0.5*PETSC_PI*PetscSinhReal(k*h))); 1858446c295cSMatthew G. Knepley lx = -alpha*(1.0 - yk)+beta; 1859446c295cSMatthew G. Knepley rx = alpha*(1.0 - yk)+beta; 1860b3c0f97bSTom Klotz func(lx, &lval); 1861b3c0f97bSTom Klotz func(rx, &rval); 1862b3c0f97bSTom Klotz lterm = alpha*wk*lval; 1863b3c0f97bSTom Klotz maxTerm = PetscMax(PetscAbsReal(lterm), maxTerm); 1864b3c0f97bSTom Klotz sum += lterm; 1865b3c0f97bSTom Klotz rterm = alpha*wk*rval; 1866b3c0f97bSTom Klotz maxTerm = PetscMax(PetscAbsReal(rterm), maxTerm); 1867b3c0f97bSTom Klotz sum += rterm; 1868b3c0f97bSTom Klotz ++k; 1869b3c0f97bSTom Klotz /* Only need to evaluate every other point on refined levels */ 1870b3c0f97bSTom Klotz if (l != 1) ++k; 18719add2064SThomas Klotz } while (PetscAbsReal(PetscLog10Real(wk)) < p); /* Only need to evaluate sum until weights are < 32 digits of precision */ 1872b3c0f97bSTom Klotz 1873b3c0f97bSTom Klotz d1 = PetscLog10Real(PetscAbsReal(sum - osum)); 1874b3c0f97bSTom Klotz d2 = PetscLog10Real(PetscAbsReal(sum - psum)); 1875b3c0f97bSTom Klotz d3 = PetscLog10Real(maxTerm) - p; 187609d48545SBarry Smith if (PetscMax(PetscAbsReal(lterm), PetscAbsReal(rterm)) == 0.0) d4 = 0.0; 187709d48545SBarry Smith else d4 = PetscLog10Real(PetscMax(PetscAbsReal(lterm), PetscAbsReal(rterm))); 1878b3c0f97bSTom Klotz d = PetscAbsInt(PetscMin(0, PetscMax(PetscMax(PetscMax(PetscSqr(d1)/d2, 2*d1), d3), d4))); 18799add2064SThomas Klotz } while (d < digits && l < 12); 1880b3c0f97bSTom Klotz *sol = sum; 1881e510cb1fSThomas Klotz 1882b3c0f97bSTom Klotz PetscFunctionReturn(0); 1883b3c0f97bSTom Klotz } 1884b3c0f97bSTom Klotz 1885497880caSRichard Tran Mills #if defined(PETSC_HAVE_MPFR) 188629f144ccSMatthew G. Knepley PetscErrorCode PetscDTTanhSinhIntegrateMPFR(void (*func)(PetscReal, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, PetscReal *sol) 188729f144ccSMatthew G. Knepley { 1888e510cb1fSThomas Klotz const PetscInt safetyFactor = 2; /* Calculate abcissa until 2*p digits */ 188929f144ccSMatthew G. Knepley PetscInt l = 0; /* Level of refinement, h = 2^{-l} */ 189029f144ccSMatthew G. Knepley mpfr_t alpha; /* Half-width of the integration interval */ 189129f144ccSMatthew G. Knepley mpfr_t beta; /* Center of the integration interval */ 189229f144ccSMatthew G. Knepley mpfr_t h; /* Step size, length between x_k */ 189329f144ccSMatthew G. Knepley mpfr_t osum; /* Integral on last level */ 189429f144ccSMatthew G. Knepley mpfr_t psum; /* Integral on the level before the last level */ 189529f144ccSMatthew G. Knepley mpfr_t sum; /* Integral on current level */ 189629f144ccSMatthew G. Knepley mpfr_t yk; /* Quadrature point 1 - x_k on reference domain [-1, 1] */ 189729f144ccSMatthew G. Knepley mpfr_t lx, rx; /* Quadrature points to the left and right of 0 on the real domain [a, b] */ 189829f144ccSMatthew G. Knepley mpfr_t wk; /* Quadrature weight at x_k */ 189929f144ccSMatthew G. Knepley PetscReal lval, rval; /* Terms in the quadature sum to the left and right of 0 */ 190029f144ccSMatthew G. Knepley PetscInt d; /* Digits of precision in the integral */ 190129f144ccSMatthew G. Knepley mpfr_t pi2, kh, msinh, mcosh, maxTerm, curTerm, tmp; 190229f144ccSMatthew G. Knepley 190329f144ccSMatthew G. Knepley PetscFunctionBegin; 190429f144ccSMatthew G. Knepley if (digits <= 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a positive number of significant digits"); 190529f144ccSMatthew G. Knepley /* Create high precision storage */ 1906c9f744b5SMatthew 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); 190729f144ccSMatthew G. Knepley /* Initialization */ 190829f144ccSMatthew G. Knepley mpfr_set_d(alpha, 0.5*(b-a), MPFR_RNDN); 190929f144ccSMatthew G. Knepley mpfr_set_d(beta, 0.5*(b+a), MPFR_RNDN); 191029f144ccSMatthew G. Knepley mpfr_set_d(osum, 0.0, MPFR_RNDN); 191129f144ccSMatthew G. Knepley mpfr_set_d(psum, 0.0, MPFR_RNDN); 191229f144ccSMatthew G. Knepley mpfr_set_d(h, 1.0, MPFR_RNDN); 191329f144ccSMatthew G. Knepley mpfr_const_pi(pi2, MPFR_RNDN); 191429f144ccSMatthew G. Knepley mpfr_mul_d(pi2, pi2, 0.5, MPFR_RNDN); 191529f144ccSMatthew G. Knepley /* Center term */ 191629f144ccSMatthew G. Knepley func(0.5*(b+a), &lval); 191729f144ccSMatthew G. Knepley mpfr_set(sum, pi2, MPFR_RNDN); 191829f144ccSMatthew G. Knepley mpfr_mul(sum, sum, alpha, MPFR_RNDN); 191929f144ccSMatthew G. Knepley mpfr_mul_d(sum, sum, lval, MPFR_RNDN); 192029f144ccSMatthew G. Knepley /* */ 192129f144ccSMatthew G. Knepley do { 192229f144ccSMatthew G. Knepley PetscReal d1, d2, d3, d4; 192329f144ccSMatthew G. Knepley PetscInt k = 1; 192429f144ccSMatthew G. Knepley 192529f144ccSMatthew G. Knepley ++l; 192629f144ccSMatthew G. Knepley mpfr_set_d(maxTerm, 0.0, MPFR_RNDN); 192729f144ccSMatthew G. Knepley /* PetscPrintf(PETSC_COMM_SELF, "LEVEL %D sum: %15.15f\n", l, sum); */ 192829f144ccSMatthew G. Knepley /* At each level of refinement, h --> h/2 and sum --> sum/2 */ 192929f144ccSMatthew G. Knepley mpfr_set(psum, osum, MPFR_RNDN); 193029f144ccSMatthew G. Knepley mpfr_set(osum, sum, MPFR_RNDN); 193129f144ccSMatthew G. Knepley mpfr_mul_d(h, h, 0.5, MPFR_RNDN); 193229f144ccSMatthew G. Knepley mpfr_mul_d(sum, sum, 0.5, MPFR_RNDN); 193329f144ccSMatthew G. Knepley do { 193429f144ccSMatthew G. Knepley mpfr_set_si(kh, k, MPFR_RNDN); 193529f144ccSMatthew G. Knepley mpfr_mul(kh, kh, h, MPFR_RNDN); 193629f144ccSMatthew G. Knepley /* Weight */ 193729f144ccSMatthew G. Knepley mpfr_set(wk, h, MPFR_RNDN); 193829f144ccSMatthew G. Knepley mpfr_sinh_cosh(msinh, mcosh, kh, MPFR_RNDN); 193929f144ccSMatthew G. Knepley mpfr_mul(msinh, msinh, pi2, MPFR_RNDN); 194029f144ccSMatthew G. Knepley mpfr_mul(mcosh, mcosh, pi2, MPFR_RNDN); 194129f144ccSMatthew G. Knepley mpfr_cosh(tmp, msinh, MPFR_RNDN); 194229f144ccSMatthew G. Knepley mpfr_sqr(tmp, tmp, MPFR_RNDN); 194329f144ccSMatthew G. Knepley mpfr_mul(wk, wk, mcosh, MPFR_RNDN); 194429f144ccSMatthew G. Knepley mpfr_div(wk, wk, tmp, MPFR_RNDN); 194529f144ccSMatthew G. Knepley /* Abscissa */ 194629f144ccSMatthew G. Knepley mpfr_set_d(yk, 1.0, MPFR_RNDZ); 194729f144ccSMatthew G. Knepley mpfr_cosh(tmp, msinh, MPFR_RNDN); 194829f144ccSMatthew G. Knepley mpfr_div(yk, yk, tmp, MPFR_RNDZ); 194929f144ccSMatthew G. Knepley mpfr_exp(tmp, msinh, MPFR_RNDN); 195029f144ccSMatthew G. Knepley mpfr_div(yk, yk, tmp, MPFR_RNDZ); 195129f144ccSMatthew G. Knepley /* Quadrature points */ 195229f144ccSMatthew G. Knepley mpfr_sub_d(lx, yk, 1.0, MPFR_RNDZ); 195329f144ccSMatthew G. Knepley mpfr_mul(lx, lx, alpha, MPFR_RNDU); 195429f144ccSMatthew G. Knepley mpfr_add(lx, lx, beta, MPFR_RNDU); 195529f144ccSMatthew G. Knepley mpfr_d_sub(rx, 1.0, yk, MPFR_RNDZ); 195629f144ccSMatthew G. Knepley mpfr_mul(rx, rx, alpha, MPFR_RNDD); 195729f144ccSMatthew G. Knepley mpfr_add(rx, rx, beta, MPFR_RNDD); 195829f144ccSMatthew G. Knepley /* Evaluation */ 195929f144ccSMatthew G. Knepley func(mpfr_get_d(lx, MPFR_RNDU), &lval); 196029f144ccSMatthew G. Knepley func(mpfr_get_d(rx, MPFR_RNDD), &rval); 196129f144ccSMatthew G. Knepley /* Update */ 196229f144ccSMatthew G. Knepley mpfr_mul(tmp, wk, alpha, MPFR_RNDN); 196329f144ccSMatthew G. Knepley mpfr_mul_d(tmp, tmp, lval, MPFR_RNDN); 196429f144ccSMatthew G. Knepley mpfr_add(sum, sum, tmp, MPFR_RNDN); 196529f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 196629f144ccSMatthew G. Knepley mpfr_max(maxTerm, maxTerm, tmp, MPFR_RNDN); 196729f144ccSMatthew G. Knepley mpfr_set(curTerm, tmp, MPFR_RNDN); 196829f144ccSMatthew G. Knepley mpfr_mul(tmp, wk, alpha, MPFR_RNDN); 196929f144ccSMatthew G. Knepley mpfr_mul_d(tmp, tmp, rval, MPFR_RNDN); 197029f144ccSMatthew G. Knepley mpfr_add(sum, sum, tmp, MPFR_RNDN); 197129f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 197229f144ccSMatthew G. Knepley mpfr_max(maxTerm, maxTerm, tmp, MPFR_RNDN); 197329f144ccSMatthew G. Knepley mpfr_max(curTerm, curTerm, tmp, MPFR_RNDN); 197429f144ccSMatthew G. Knepley ++k; 197529f144ccSMatthew G. Knepley /* Only need to evaluate every other point on refined levels */ 197629f144ccSMatthew G. Knepley if (l != 1) ++k; 197729f144ccSMatthew G. Knepley mpfr_log10(tmp, wk, MPFR_RNDN); 197829f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 1979c9f744b5SMatthew G. Knepley } while (mpfr_get_d(tmp, MPFR_RNDN) < safetyFactor*digits); /* Only need to evaluate sum until weights are < 32 digits of precision */ 198029f144ccSMatthew G. Knepley mpfr_sub(tmp, sum, osum, MPFR_RNDN); 198129f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 198229f144ccSMatthew G. Knepley mpfr_log10(tmp, tmp, MPFR_RNDN); 198329f144ccSMatthew G. Knepley d1 = mpfr_get_d(tmp, MPFR_RNDN); 198429f144ccSMatthew G. Knepley mpfr_sub(tmp, sum, psum, MPFR_RNDN); 198529f144ccSMatthew G. Knepley mpfr_abs(tmp, tmp, MPFR_RNDN); 198629f144ccSMatthew G. Knepley mpfr_log10(tmp, tmp, MPFR_RNDN); 198729f144ccSMatthew G. Knepley d2 = mpfr_get_d(tmp, MPFR_RNDN); 198829f144ccSMatthew G. Knepley mpfr_log10(tmp, maxTerm, MPFR_RNDN); 1989c9f744b5SMatthew G. Knepley d3 = mpfr_get_d(tmp, MPFR_RNDN) - digits; 199029f144ccSMatthew G. Knepley mpfr_log10(tmp, curTerm, MPFR_RNDN); 199129f144ccSMatthew G. Knepley d4 = mpfr_get_d(tmp, MPFR_RNDN); 199229f144ccSMatthew G. Knepley d = PetscAbsInt(PetscMin(0, PetscMax(PetscMax(PetscMax(PetscSqr(d1)/d2, 2*d1), d3), d4))); 1993b0649871SThomas Klotz } while (d < digits && l < 8); 199429f144ccSMatthew G. Knepley *sol = mpfr_get_d(sum, MPFR_RNDN); 199529f144ccSMatthew G. Knepley /* Cleanup */ 199629f144ccSMatthew G. Knepley mpfr_clears(alpha, beta, h, sum, osum, psum, yk, wk, lx, rx, tmp, maxTerm, curTerm, pi2, kh, msinh, mcosh, NULL); 199729f144ccSMatthew G. Knepley PetscFunctionReturn(0); 199829f144ccSMatthew G. Knepley } 1999d525116cSMatthew G. Knepley #else 2000fbfcfee5SBarry Smith 2001d525116cSMatthew G. Knepley PetscErrorCode PetscDTTanhSinhIntegrateMPFR(void (*func)(PetscReal, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, PetscReal *sol) 2002d525116cSMatthew G. Knepley { 2003d525116cSMatthew G. Knepley SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "This method will not work without MPFR. Reconfigure using --download-mpfr --download-gmp"); 2004d525116cSMatthew G. Knepley } 200529f144ccSMatthew G. Knepley #endif 200629f144ccSMatthew G. Knepley 2007194825f6SJed Brown /* Overwrites A. Can only handle full-rank problems with m>=n 2008194825f6SJed Brown * A in column-major format 2009194825f6SJed Brown * Ainv in row-major format 2010194825f6SJed Brown * tau has length m 2011194825f6SJed Brown * worksize must be >= max(1,n) 2012194825f6SJed Brown */ 2013194825f6SJed Brown static PetscErrorCode PetscDTPseudoInverseQR(PetscInt m,PetscInt mstride,PetscInt n,PetscReal *A_in,PetscReal *Ainv_out,PetscScalar *tau,PetscInt worksize,PetscScalar *work) 2014194825f6SJed Brown { 2015194825f6SJed Brown PetscErrorCode ierr; 2016194825f6SJed Brown PetscBLASInt M,N,K,lda,ldb,ldwork,info; 2017194825f6SJed Brown PetscScalar *A,*Ainv,*R,*Q,Alpha; 2018194825f6SJed Brown 2019194825f6SJed Brown PetscFunctionBegin; 2020194825f6SJed Brown #if defined(PETSC_USE_COMPLEX) 2021194825f6SJed Brown { 2022194825f6SJed Brown PetscInt i,j; 2023dcca6d9dSJed Brown ierr = PetscMalloc2(m*n,&A,m*n,&Ainv);CHKERRQ(ierr); 2024194825f6SJed Brown for (j=0; j<n; j++) { 2025194825f6SJed Brown for (i=0; i<m; i++) A[i+m*j] = A_in[i+mstride*j]; 2026194825f6SJed Brown } 2027194825f6SJed Brown mstride = m; 2028194825f6SJed Brown } 2029194825f6SJed Brown #else 2030194825f6SJed Brown A = A_in; 2031194825f6SJed Brown Ainv = Ainv_out; 2032194825f6SJed Brown #endif 2033194825f6SJed Brown 2034194825f6SJed Brown ierr = PetscBLASIntCast(m,&M);CHKERRQ(ierr); 2035194825f6SJed Brown ierr = PetscBLASIntCast(n,&N);CHKERRQ(ierr); 2036194825f6SJed Brown ierr = PetscBLASIntCast(mstride,&lda);CHKERRQ(ierr); 2037194825f6SJed Brown ierr = PetscBLASIntCast(worksize,&ldwork);CHKERRQ(ierr); 2038194825f6SJed Brown ierr = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr); 2039001a771dSBarry Smith PetscStackCallBLAS("LAPACKgeqrf",LAPACKgeqrf_(&M,&N,A,&lda,tau,work,&ldwork,&info)); 2040194825f6SJed Brown ierr = PetscFPTrapPop();CHKERRQ(ierr); 2041194825f6SJed Brown if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"xGEQRF error"); 2042194825f6SJed Brown R = A; /* Upper triangular part of A now contains R, the rest contains the elementary reflectors */ 2043194825f6SJed Brown 2044194825f6SJed Brown /* Extract an explicit representation of Q */ 2045194825f6SJed Brown Q = Ainv; 2046580bdb30SBarry Smith ierr = PetscArraycpy(Q,A,mstride*n);CHKERRQ(ierr); 2047194825f6SJed Brown K = N; /* full rank */ 2048c964aadfSJose E. Roman PetscStackCallBLAS("LAPACKorgqr",LAPACKorgqr_(&M,&N,&K,Q,&lda,tau,work,&ldwork,&info)); 2049194825f6SJed Brown if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"xORGQR/xUNGQR error"); 2050194825f6SJed Brown 2051194825f6SJed Brown /* Compute A^{-T} = (R^{-1} Q^T)^T = Q R^{-T} */ 2052194825f6SJed Brown Alpha = 1.0; 2053194825f6SJed Brown ldb = lda; 2054001a771dSBarry Smith PetscStackCallBLAS("BLAStrsm",BLAStrsm_("Right","Upper","ConjugateTranspose","NotUnitTriangular",&M,&N,&Alpha,R,&lda,Q,&ldb)); 2055194825f6SJed Brown /* Ainv is Q, overwritten with inverse */ 2056194825f6SJed Brown 2057194825f6SJed Brown #if defined(PETSC_USE_COMPLEX) 2058194825f6SJed Brown { 2059194825f6SJed Brown PetscInt i; 2060194825f6SJed Brown for (i=0; i<m*n; i++) Ainv_out[i] = PetscRealPart(Ainv[i]); 2061194825f6SJed Brown ierr = PetscFree2(A,Ainv);CHKERRQ(ierr); 2062194825f6SJed Brown } 2063194825f6SJed Brown #endif 2064194825f6SJed Brown PetscFunctionReturn(0); 2065194825f6SJed Brown } 2066194825f6SJed Brown 2067194825f6SJed Brown /* Computes integral of L_p' over intervals {(x0,x1),(x1,x2),...} */ 2068194825f6SJed Brown static PetscErrorCode PetscDTLegendreIntegrate(PetscInt ninterval,const PetscReal *x,PetscInt ndegree,const PetscInt *degrees,PetscBool Transpose,PetscReal *B) 2069194825f6SJed Brown { 2070194825f6SJed Brown PetscErrorCode ierr; 2071194825f6SJed Brown PetscReal *Bv; 2072194825f6SJed Brown PetscInt i,j; 2073194825f6SJed Brown 2074194825f6SJed Brown PetscFunctionBegin; 2075785e854fSJed Brown ierr = PetscMalloc1((ninterval+1)*ndegree,&Bv);CHKERRQ(ierr); 2076194825f6SJed Brown /* Point evaluation of L_p on all the source vertices */ 2077194825f6SJed Brown ierr = PetscDTLegendreEval(ninterval+1,x,ndegree,degrees,Bv,NULL,NULL);CHKERRQ(ierr); 2078194825f6SJed Brown /* Integral over each interval: \int_a^b L_p' = L_p(b)-L_p(a) */ 2079194825f6SJed Brown for (i=0; i<ninterval; i++) { 2080194825f6SJed Brown for (j=0; j<ndegree; j++) { 2081194825f6SJed Brown if (Transpose) B[i+ninterval*j] = Bv[(i+1)*ndegree+j] - Bv[i*ndegree+j]; 2082194825f6SJed Brown else B[i*ndegree+j] = Bv[(i+1)*ndegree+j] - Bv[i*ndegree+j]; 2083194825f6SJed Brown } 2084194825f6SJed Brown } 2085194825f6SJed Brown ierr = PetscFree(Bv);CHKERRQ(ierr); 2086194825f6SJed Brown PetscFunctionReturn(0); 2087194825f6SJed Brown } 2088194825f6SJed Brown 2089194825f6SJed Brown /*@ 2090194825f6SJed Brown PetscDTReconstructPoly - create matrix representing polynomial reconstruction using cell intervals and evaluation at target intervals 2091194825f6SJed Brown 2092194825f6SJed Brown Not Collective 2093194825f6SJed Brown 2094194825f6SJed Brown Input Arguments: 2095194825f6SJed Brown + degree - degree of reconstruction polynomial 2096194825f6SJed Brown . nsource - number of source intervals 2097194825f6SJed Brown . sourcex - sorted coordinates of source cell boundaries (length nsource+1) 2098194825f6SJed Brown . ntarget - number of target intervals 2099194825f6SJed Brown - targetx - sorted coordinates of target cell boundaries (length ntarget+1) 2100194825f6SJed Brown 2101194825f6SJed Brown Output Arguments: 2102194825f6SJed Brown . R - reconstruction matrix, utarget = sum_s R[t*nsource+s] * usource[s] 2103194825f6SJed Brown 2104194825f6SJed Brown Level: advanced 2105194825f6SJed Brown 2106194825f6SJed Brown .seealso: PetscDTLegendreEval() 2107194825f6SJed Brown @*/ 2108194825f6SJed Brown PetscErrorCode PetscDTReconstructPoly(PetscInt degree,PetscInt nsource,const PetscReal *sourcex,PetscInt ntarget,const PetscReal *targetx,PetscReal *R) 2109194825f6SJed Brown { 2110194825f6SJed Brown PetscErrorCode ierr; 2111194825f6SJed Brown PetscInt i,j,k,*bdegrees,worksize; 2112194825f6SJed Brown PetscReal xmin,xmax,center,hscale,*sourcey,*targety,*Bsource,*Bsinv,*Btarget; 2113194825f6SJed Brown PetscScalar *tau,*work; 2114194825f6SJed Brown 2115194825f6SJed Brown PetscFunctionBegin; 2116194825f6SJed Brown PetscValidRealPointer(sourcex,3); 2117194825f6SJed Brown PetscValidRealPointer(targetx,5); 2118194825f6SJed Brown PetscValidRealPointer(R,6); 2119194825f6SJed Brown if (degree >= nsource) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Reconstruction degree %D must be less than number of source intervals %D",degree,nsource); 2120194825f6SJed Brown #if defined(PETSC_USE_DEBUG) 2121194825f6SJed Brown for (i=0; i<nsource; i++) { 212257622a8eSBarry Smith if (sourcex[i] >= sourcex[i+1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Source interval %D has negative orientation (%g,%g)",i,(double)sourcex[i],(double)sourcex[i+1]); 2123194825f6SJed Brown } 2124194825f6SJed Brown for (i=0; i<ntarget; i++) { 212557622a8eSBarry Smith if (targetx[i] >= targetx[i+1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Target interval %D has negative orientation (%g,%g)",i,(double)targetx[i],(double)targetx[i+1]); 2126194825f6SJed Brown } 2127194825f6SJed Brown #endif 2128194825f6SJed Brown xmin = PetscMin(sourcex[0],targetx[0]); 2129194825f6SJed Brown xmax = PetscMax(sourcex[nsource],targetx[ntarget]); 2130194825f6SJed Brown center = (xmin + xmax)/2; 2131194825f6SJed Brown hscale = (xmax - xmin)/2; 2132194825f6SJed Brown worksize = nsource; 2133dcca6d9dSJed Brown ierr = PetscMalloc4(degree+1,&bdegrees,nsource+1,&sourcey,nsource*(degree+1),&Bsource,worksize,&work);CHKERRQ(ierr); 2134dcca6d9dSJed Brown ierr = PetscMalloc4(nsource,&tau,nsource*(degree+1),&Bsinv,ntarget+1,&targety,ntarget*(degree+1),&Btarget);CHKERRQ(ierr); 2135194825f6SJed Brown for (i=0; i<=nsource; i++) sourcey[i] = (sourcex[i]-center)/hscale; 2136194825f6SJed Brown for (i=0; i<=degree; i++) bdegrees[i] = i+1; 2137194825f6SJed Brown ierr = PetscDTLegendreIntegrate(nsource,sourcey,degree+1,bdegrees,PETSC_TRUE,Bsource);CHKERRQ(ierr); 2138194825f6SJed Brown ierr = PetscDTPseudoInverseQR(nsource,nsource,degree+1,Bsource,Bsinv,tau,nsource,work);CHKERRQ(ierr); 2139194825f6SJed Brown for (i=0; i<=ntarget; i++) targety[i] = (targetx[i]-center)/hscale; 2140194825f6SJed Brown ierr = PetscDTLegendreIntegrate(ntarget,targety,degree+1,bdegrees,PETSC_FALSE,Btarget);CHKERRQ(ierr); 2141194825f6SJed Brown for (i=0; i<ntarget; i++) { 2142194825f6SJed Brown PetscReal rowsum = 0; 2143194825f6SJed Brown for (j=0; j<nsource; j++) { 2144194825f6SJed Brown PetscReal sum = 0; 2145194825f6SJed Brown for (k=0; k<degree+1; k++) { 2146194825f6SJed Brown sum += Btarget[i*(degree+1)+k] * Bsinv[k*nsource+j]; 2147194825f6SJed Brown } 2148194825f6SJed Brown R[i*nsource+j] = sum; 2149194825f6SJed Brown rowsum += sum; 2150194825f6SJed Brown } 2151194825f6SJed Brown for (j=0; j<nsource; j++) R[i*nsource+j] /= rowsum; /* normalize each row */ 2152194825f6SJed Brown } 2153194825f6SJed Brown ierr = PetscFree4(bdegrees,sourcey,Bsource,work);CHKERRQ(ierr); 2154194825f6SJed Brown ierr = PetscFree4(tau,Bsinv,targety,Btarget);CHKERRQ(ierr); 2155194825f6SJed Brown PetscFunctionReturn(0); 2156194825f6SJed Brown } 2157916e780bShannah_mairs 2158916e780bShannah_mairs /*@C 2159916e780bShannah_mairs PetscGaussLobattoLegendreIntegrate - Compute the L2 integral of a function on the GLL points 2160916e780bShannah_mairs 2161916e780bShannah_mairs Not Collective 2162916e780bShannah_mairs 2163916e780bShannah_mairs Input Parameter: 2164916e780bShannah_mairs + n - the number of GLL nodes 2165916e780bShannah_mairs . nodes - the GLL nodes 2166916e780bShannah_mairs . weights - the GLL weights 2167f0fc11ceSJed Brown - f - the function values at the nodes 2168916e780bShannah_mairs 2169916e780bShannah_mairs Output Parameter: 2170916e780bShannah_mairs . in - the value of the integral 2171916e780bShannah_mairs 2172916e780bShannah_mairs Level: beginner 2173916e780bShannah_mairs 2174916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature() 2175916e780bShannah_mairs 2176916e780bShannah_mairs @*/ 2177916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreIntegrate(PetscInt n,PetscReal *nodes,PetscReal *weights,const PetscReal *f,PetscReal *in) 2178916e780bShannah_mairs { 2179916e780bShannah_mairs PetscInt i; 2180916e780bShannah_mairs 2181916e780bShannah_mairs PetscFunctionBegin; 2182916e780bShannah_mairs *in = 0.; 2183916e780bShannah_mairs for (i=0; i<n; i++) { 2184916e780bShannah_mairs *in += f[i]*f[i]*weights[i]; 2185916e780bShannah_mairs } 2186916e780bShannah_mairs PetscFunctionReturn(0); 2187916e780bShannah_mairs } 2188916e780bShannah_mairs 2189916e780bShannah_mairs /*@C 2190916e780bShannah_mairs PetscGaussLobattoLegendreElementLaplacianCreate - computes the Laplacian for a single 1d GLL element 2191916e780bShannah_mairs 2192916e780bShannah_mairs Not Collective 2193916e780bShannah_mairs 2194916e780bShannah_mairs Input Parameter: 2195916e780bShannah_mairs + n - the number of GLL nodes 2196916e780bShannah_mairs . nodes - the GLL nodes 2197f0fc11ceSJed Brown - weights - the GLL weights 2198916e780bShannah_mairs 2199916e780bShannah_mairs Output Parameter: 2200916e780bShannah_mairs . A - the stiffness element 2201916e780bShannah_mairs 2202916e780bShannah_mairs Level: beginner 2203916e780bShannah_mairs 2204916e780bShannah_mairs Notes: 2205916e780bShannah_mairs Destroy this with PetscGaussLobattoLegendreElementLaplacianDestroy() 2206916e780bShannah_mairs 2207916e780bShannah_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) 2208916e780bShannah_mairs 2209916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianDestroy() 2210916e780bShannah_mairs 2211916e780bShannah_mairs @*/ 2212916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementLaplacianCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2213916e780bShannah_mairs { 2214916e780bShannah_mairs PetscReal **A; 2215916e780bShannah_mairs PetscErrorCode ierr; 2216916e780bShannah_mairs const PetscReal *gllnodes = nodes; 2217916e780bShannah_mairs const PetscInt p = n-1; 2218916e780bShannah_mairs PetscReal z0,z1,z2 = -1,x,Lpj,Lpr; 2219916e780bShannah_mairs PetscInt i,j,nn,r; 2220916e780bShannah_mairs 2221916e780bShannah_mairs PetscFunctionBegin; 2222916e780bShannah_mairs ierr = PetscMalloc1(n,&A);CHKERRQ(ierr); 2223916e780bShannah_mairs ierr = PetscMalloc1(n*n,&A[0]);CHKERRQ(ierr); 2224916e780bShannah_mairs for (i=1; i<n; i++) A[i] = A[i-1]+n; 2225916e780bShannah_mairs 2226916e780bShannah_mairs for (j=1; j<p; j++) { 2227916e780bShannah_mairs x = gllnodes[j]; 2228916e780bShannah_mairs z0 = 1.; 2229916e780bShannah_mairs z1 = x; 2230916e780bShannah_mairs for (nn=1; nn<p; nn++) { 2231916e780bShannah_mairs z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.)); 2232916e780bShannah_mairs z0 = z1; 2233916e780bShannah_mairs z1 = z2; 2234916e780bShannah_mairs } 2235916e780bShannah_mairs Lpj=z2; 2236916e780bShannah_mairs for (r=1; r<p; r++) { 2237916e780bShannah_mairs if (r == j) { 2238916e780bShannah_mairs A[j][j]=2./(3.*(1.-gllnodes[j]*gllnodes[j])*Lpj*Lpj); 2239916e780bShannah_mairs } else { 2240916e780bShannah_mairs x = gllnodes[r]; 2241916e780bShannah_mairs z0 = 1.; 2242916e780bShannah_mairs z1 = x; 2243916e780bShannah_mairs for (nn=1; nn<p; nn++) { 2244916e780bShannah_mairs z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.)); 2245916e780bShannah_mairs z0 = z1; 2246916e780bShannah_mairs z1 = z2; 2247916e780bShannah_mairs } 2248916e780bShannah_mairs Lpr = z2; 2249916e780bShannah_mairs A[r][j] = 4./(((PetscReal)p)*(((PetscReal)p)+1.)*Lpj*Lpr*(gllnodes[j]-gllnodes[r])*(gllnodes[j]-gllnodes[r])); 2250916e780bShannah_mairs } 2251916e780bShannah_mairs } 2252916e780bShannah_mairs } 2253916e780bShannah_mairs for (j=1; j<p+1; j++) { 2254916e780bShannah_mairs x = gllnodes[j]; 2255916e780bShannah_mairs z0 = 1.; 2256916e780bShannah_mairs z1 = x; 2257916e780bShannah_mairs for (nn=1; nn<p; nn++) { 2258916e780bShannah_mairs z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.)); 2259916e780bShannah_mairs z0 = z1; 2260916e780bShannah_mairs z1 = z2; 2261916e780bShannah_mairs } 2262916e780bShannah_mairs Lpj = z2; 2263916e780bShannah_mairs A[j][0] = 4.*PetscPowRealInt(-1.,p)/(((PetscReal)p)*(((PetscReal)p)+1.)*Lpj*(1.+gllnodes[j])*(1.+gllnodes[j])); 2264916e780bShannah_mairs A[0][j] = A[j][0]; 2265916e780bShannah_mairs } 2266916e780bShannah_mairs for (j=0; j<p; j++) { 2267916e780bShannah_mairs x = gllnodes[j]; 2268916e780bShannah_mairs z0 = 1.; 2269916e780bShannah_mairs z1 = x; 2270916e780bShannah_mairs for (nn=1; nn<p; nn++) { 2271916e780bShannah_mairs z2 = x*z1*(2.*((PetscReal)nn)+1.)/(((PetscReal)nn)+1.)-z0*(((PetscReal)nn)/(((PetscReal)nn)+1.)); 2272916e780bShannah_mairs z0 = z1; 2273916e780bShannah_mairs z1 = z2; 2274916e780bShannah_mairs } 2275916e780bShannah_mairs Lpj=z2; 2276916e780bShannah_mairs 2277916e780bShannah_mairs A[p][j] = 4./(((PetscReal)p)*(((PetscReal)p)+1.)*Lpj*(1.-gllnodes[j])*(1.-gllnodes[j])); 2278916e780bShannah_mairs A[j][p] = A[p][j]; 2279916e780bShannah_mairs } 2280916e780bShannah_mairs A[0][0]=0.5+(((PetscReal)p)*(((PetscReal)p)+1.)-2.)/6.; 2281916e780bShannah_mairs A[p][p]=A[0][0]; 2282916e780bShannah_mairs *AA = A; 2283916e780bShannah_mairs PetscFunctionReturn(0); 2284916e780bShannah_mairs } 2285916e780bShannah_mairs 2286916e780bShannah_mairs /*@C 2287916e780bShannah_mairs PetscGaussLobattoLegendreElementLaplacianDestroy - frees the Laplacian for a single 1d GLL element 2288916e780bShannah_mairs 2289916e780bShannah_mairs Not Collective 2290916e780bShannah_mairs 2291916e780bShannah_mairs Input Parameter: 2292916e780bShannah_mairs + n - the number of GLL nodes 2293916e780bShannah_mairs . nodes - the GLL nodes 2294916e780bShannah_mairs . weights - the GLL weightss 2295916e780bShannah_mairs - A - the stiffness element 2296916e780bShannah_mairs 2297916e780bShannah_mairs Level: beginner 2298916e780bShannah_mairs 2299916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianCreate() 2300916e780bShannah_mairs 2301916e780bShannah_mairs @*/ 2302916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementLaplacianDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2303916e780bShannah_mairs { 2304916e780bShannah_mairs PetscErrorCode ierr; 2305916e780bShannah_mairs 2306916e780bShannah_mairs PetscFunctionBegin; 2307916e780bShannah_mairs ierr = PetscFree((*AA)[0]);CHKERRQ(ierr); 2308916e780bShannah_mairs ierr = PetscFree(*AA);CHKERRQ(ierr); 2309916e780bShannah_mairs *AA = NULL; 2310916e780bShannah_mairs PetscFunctionReturn(0); 2311916e780bShannah_mairs } 2312916e780bShannah_mairs 2313916e780bShannah_mairs /*@C 2314916e780bShannah_mairs PetscGaussLobattoLegendreElementGradientCreate - computes the gradient for a single 1d GLL element 2315916e780bShannah_mairs 2316916e780bShannah_mairs Not Collective 2317916e780bShannah_mairs 2318916e780bShannah_mairs Input Parameter: 2319916e780bShannah_mairs + n - the number of GLL nodes 2320916e780bShannah_mairs . nodes - the GLL nodes 2321916e780bShannah_mairs . weights - the GLL weights 2322916e780bShannah_mairs 2323916e780bShannah_mairs Output Parameter: 2324916e780bShannah_mairs . AA - the stiffness element 2325916e780bShannah_mairs - AAT - the transpose of AA (pass in NULL if you do not need this array) 2326916e780bShannah_mairs 2327916e780bShannah_mairs Level: beginner 2328916e780bShannah_mairs 2329916e780bShannah_mairs Notes: 2330916e780bShannah_mairs Destroy this with PetscGaussLobattoLegendreElementGradientDestroy() 2331916e780bShannah_mairs 2332916e780bShannah_mairs You can access entries in these arrays with AA[i][j] but in memory it is stored in contiguous memory, row oriented 2333916e780bShannah_mairs 2334916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianDestroy() 2335916e780bShannah_mairs 2336916e780bShannah_mairs @*/ 2337916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementGradientCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA,PetscReal ***AAT) 2338916e780bShannah_mairs { 2339916e780bShannah_mairs PetscReal **A, **AT = NULL; 2340916e780bShannah_mairs PetscErrorCode ierr; 2341916e780bShannah_mairs const PetscReal *gllnodes = nodes; 2342916e780bShannah_mairs const PetscInt p = n-1; 2343e6a796c3SToby Isaac PetscReal Li, Lj,d0; 2344916e780bShannah_mairs PetscInt i,j; 2345916e780bShannah_mairs 2346916e780bShannah_mairs PetscFunctionBegin; 2347916e780bShannah_mairs ierr = PetscMalloc1(n,&A);CHKERRQ(ierr); 2348916e780bShannah_mairs ierr = PetscMalloc1(n*n,&A[0]);CHKERRQ(ierr); 2349916e780bShannah_mairs for (i=1; i<n; i++) A[i] = A[i-1]+n; 2350916e780bShannah_mairs 2351916e780bShannah_mairs if (AAT) { 2352916e780bShannah_mairs ierr = PetscMalloc1(n,&AT);CHKERRQ(ierr); 2353916e780bShannah_mairs ierr = PetscMalloc1(n*n,&AT[0]);CHKERRQ(ierr); 2354916e780bShannah_mairs for (i=1; i<n; i++) AT[i] = AT[i-1]+n; 2355916e780bShannah_mairs } 2356916e780bShannah_mairs 2357916e780bShannah_mairs if (n==1) {A[0][0] = 0.;} 2358916e780bShannah_mairs d0 = (PetscReal)p*((PetscReal)p+1.)/4.; 2359916e780bShannah_mairs for (i=0; i<n; i++) { 2360916e780bShannah_mairs for (j=0; j<n; j++) { 2361916e780bShannah_mairs A[i][j] = 0.; 2362e6a796c3SToby Isaac ierr = PetscDTComputeJacobi(0., 0., p, gllnodes[i], &Li);CHKERRQ(ierr); 2363e6a796c3SToby Isaac ierr = PetscDTComputeJacobi(0., 0., p, gllnodes[j], &Lj);CHKERRQ(ierr); 2364916e780bShannah_mairs if (i!=j) A[i][j] = Li/(Lj*(gllnodes[i]-gllnodes[j])); 2365916e780bShannah_mairs if ((j==i) && (i==0)) A[i][j] = -d0; 2366916e780bShannah_mairs if (j==i && i==p) A[i][j] = d0; 2367916e780bShannah_mairs if (AT) AT[j][i] = A[i][j]; 2368916e780bShannah_mairs } 2369916e780bShannah_mairs } 2370916e780bShannah_mairs if (AAT) *AAT = AT; 2371916e780bShannah_mairs *AA = A; 2372916e780bShannah_mairs PetscFunctionReturn(0); 2373916e780bShannah_mairs } 2374916e780bShannah_mairs 2375916e780bShannah_mairs /*@C 2376916e780bShannah_mairs PetscGaussLobattoLegendreElementGradientDestroy - frees the gradient for a single 1d GLL element obtained with PetscGaussLobattoLegendreElementGradientCreate() 2377916e780bShannah_mairs 2378916e780bShannah_mairs Not Collective 2379916e780bShannah_mairs 2380916e780bShannah_mairs Input Parameter: 2381916e780bShannah_mairs + n - the number of GLL nodes 2382916e780bShannah_mairs . nodes - the GLL nodes 2383916e780bShannah_mairs . weights - the GLL weights 2384916e780bShannah_mairs . AA - the stiffness element 2385916e780bShannah_mairs - AAT - the transpose of the element 2386916e780bShannah_mairs 2387916e780bShannah_mairs Level: beginner 2388916e780bShannah_mairs 2389916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianCreate(), PetscGaussLobattoLegendreElementAdvectionCreate() 2390916e780bShannah_mairs 2391916e780bShannah_mairs @*/ 2392916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementGradientDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA,PetscReal ***AAT) 2393916e780bShannah_mairs { 2394916e780bShannah_mairs PetscErrorCode ierr; 2395916e780bShannah_mairs 2396916e780bShannah_mairs PetscFunctionBegin; 2397916e780bShannah_mairs ierr = PetscFree((*AA)[0]);CHKERRQ(ierr); 2398916e780bShannah_mairs ierr = PetscFree(*AA);CHKERRQ(ierr); 2399916e780bShannah_mairs *AA = NULL; 2400916e780bShannah_mairs if (*AAT) { 2401916e780bShannah_mairs ierr = PetscFree((*AAT)[0]);CHKERRQ(ierr); 2402916e780bShannah_mairs ierr = PetscFree(*AAT);CHKERRQ(ierr); 2403916e780bShannah_mairs *AAT = NULL; 2404916e780bShannah_mairs } 2405916e780bShannah_mairs PetscFunctionReturn(0); 2406916e780bShannah_mairs } 2407916e780bShannah_mairs 2408916e780bShannah_mairs /*@C 2409916e780bShannah_mairs PetscGaussLobattoLegendreElementAdvectionCreate - computes the advection operator for a single 1d GLL element 2410916e780bShannah_mairs 2411916e780bShannah_mairs Not Collective 2412916e780bShannah_mairs 2413916e780bShannah_mairs Input Parameter: 2414916e780bShannah_mairs + n - the number of GLL nodes 2415916e780bShannah_mairs . nodes - the GLL nodes 2416f0fc11ceSJed Brown - weights - the GLL weightss 2417916e780bShannah_mairs 2418916e780bShannah_mairs Output Parameter: 2419916e780bShannah_mairs . AA - the stiffness element 2420916e780bShannah_mairs 2421916e780bShannah_mairs Level: beginner 2422916e780bShannah_mairs 2423916e780bShannah_mairs Notes: 2424916e780bShannah_mairs Destroy this with PetscGaussLobattoLegendreElementAdvectionDestroy() 2425916e780bShannah_mairs 2426916e780bShannah_mairs This is the same as the Gradient operator multiplied by the diagonal mass matrix 2427916e780bShannah_mairs 2428916e780bShannah_mairs You can access entries in this array with AA[i][j] but in memory it is stored in contiguous memory, row oriented 2429916e780bShannah_mairs 2430916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementLaplacianCreate(), PetscGaussLobattoLegendreElementAdvectionDestroy() 2431916e780bShannah_mairs 2432916e780bShannah_mairs @*/ 2433916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementAdvectionCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2434916e780bShannah_mairs { 2435916e780bShannah_mairs PetscReal **D; 2436916e780bShannah_mairs PetscErrorCode ierr; 2437916e780bShannah_mairs const PetscReal *gllweights = weights; 2438916e780bShannah_mairs const PetscInt glln = n; 2439916e780bShannah_mairs PetscInt i,j; 2440916e780bShannah_mairs 2441916e780bShannah_mairs PetscFunctionBegin; 2442916e780bShannah_mairs ierr = PetscGaussLobattoLegendreElementGradientCreate(n,nodes,weights,&D,NULL);CHKERRQ(ierr); 2443916e780bShannah_mairs for (i=0; i<glln; i++){ 2444916e780bShannah_mairs for (j=0; j<glln; j++) { 2445916e780bShannah_mairs D[i][j] = gllweights[i]*D[i][j]; 2446916e780bShannah_mairs } 2447916e780bShannah_mairs } 2448916e780bShannah_mairs *AA = D; 2449916e780bShannah_mairs PetscFunctionReturn(0); 2450916e780bShannah_mairs } 2451916e780bShannah_mairs 2452916e780bShannah_mairs /*@C 2453916e780bShannah_mairs PetscGaussLobattoLegendreElementAdvectionDestroy - frees the advection stiffness for a single 1d GLL element 2454916e780bShannah_mairs 2455916e780bShannah_mairs Not Collective 2456916e780bShannah_mairs 2457916e780bShannah_mairs Input Parameter: 2458916e780bShannah_mairs + n - the number of GLL nodes 2459916e780bShannah_mairs . nodes - the GLL nodes 2460916e780bShannah_mairs . weights - the GLL weights 2461916e780bShannah_mairs - A - advection 2462916e780bShannah_mairs 2463916e780bShannah_mairs Level: beginner 2464916e780bShannah_mairs 2465916e780bShannah_mairs .seealso: PetscDTGaussLobattoLegendreQuadrature(), PetscGaussLobattoLegendreElementAdvectionCreate() 2466916e780bShannah_mairs 2467916e780bShannah_mairs @*/ 2468916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementAdvectionDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2469916e780bShannah_mairs { 2470916e780bShannah_mairs PetscErrorCode ierr; 2471916e780bShannah_mairs 2472916e780bShannah_mairs PetscFunctionBegin; 2473916e780bShannah_mairs ierr = PetscFree((*AA)[0]);CHKERRQ(ierr); 2474916e780bShannah_mairs ierr = PetscFree(*AA);CHKERRQ(ierr); 2475916e780bShannah_mairs *AA = NULL; 2476916e780bShannah_mairs PetscFunctionReturn(0); 2477916e780bShannah_mairs } 2478916e780bShannah_mairs 2479916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementMassCreate(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2480916e780bShannah_mairs { 2481916e780bShannah_mairs PetscReal **A; 2482916e780bShannah_mairs PetscErrorCode ierr; 2483916e780bShannah_mairs const PetscReal *gllweights = weights; 2484916e780bShannah_mairs const PetscInt glln = n; 2485916e780bShannah_mairs PetscInt i,j; 2486916e780bShannah_mairs 2487916e780bShannah_mairs PetscFunctionBegin; 2488916e780bShannah_mairs ierr = PetscMalloc1(glln,&A);CHKERRQ(ierr); 2489916e780bShannah_mairs ierr = PetscMalloc1(glln*glln,&A[0]);CHKERRQ(ierr); 2490916e780bShannah_mairs for (i=1; i<glln; i++) A[i] = A[i-1]+glln; 2491916e780bShannah_mairs if (glln==1) {A[0][0] = 0.;} 2492916e780bShannah_mairs for (i=0; i<glln; i++) { 2493916e780bShannah_mairs for (j=0; j<glln; j++) { 2494916e780bShannah_mairs A[i][j] = 0.; 2495916e780bShannah_mairs if (j==i) A[i][j] = gllweights[i]; 2496916e780bShannah_mairs } 2497916e780bShannah_mairs } 2498916e780bShannah_mairs *AA = A; 2499916e780bShannah_mairs PetscFunctionReturn(0); 2500916e780bShannah_mairs } 2501916e780bShannah_mairs 2502916e780bShannah_mairs PetscErrorCode PetscGaussLobattoLegendreElementMassDestroy(PetscInt n,PetscReal *nodes,PetscReal *weights,PetscReal ***AA) 2503916e780bShannah_mairs { 2504916e780bShannah_mairs PetscErrorCode ierr; 2505916e780bShannah_mairs 2506916e780bShannah_mairs PetscFunctionBegin; 2507916e780bShannah_mairs ierr = PetscFree((*AA)[0]);CHKERRQ(ierr); 2508916e780bShannah_mairs ierr = PetscFree(*AA);CHKERRQ(ierr); 2509916e780bShannah_mairs *AA = NULL; 2510916e780bShannah_mairs PetscFunctionReturn(0); 2511916e780bShannah_mairs } 2512d4afb720SToby Isaac 2513d4afb720SToby Isaac /*@ 2514d4afb720SToby Isaac PetscDTIndexToBary - convert an index into a barycentric coordinate. 2515d4afb720SToby Isaac 2516d4afb720SToby Isaac Input Parameters: 2517d4afb720SToby 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) 2518d4afb720SToby Isaac . sum - the value that the sum of the barycentric coordinates (which will be non-negative integers) should sum to 2519d4afb720SToby Isaac - index - the index to convert: should be >= 0 and < Binomial(len - 1 + sum, sum) 2520d4afb720SToby Isaac 2521d4afb720SToby Isaac Output Parameter: 2522d4afb720SToby Isaac . coord - will be filled with the barycentric coordinate 2523d4afb720SToby Isaac 2524d4afb720SToby Isaac Level: beginner 2525d4afb720SToby Isaac 2526d4afb720SToby Isaac Note: the indices map to barycentric coordinates in lexicographic order, where the first index is the 2527d4afb720SToby Isaac least significant and the last index is the most significant. 2528d4afb720SToby Isaac 2529*fbdc3dfeSToby Isaac .seealso: PetscDTBaryToIndex() 2530d4afb720SToby Isaac @*/ 2531d4afb720SToby Isaac PetscErrorCode PetscDTIndexToBary(PetscInt len, PetscInt sum, PetscInt index, PetscInt coord[]) 2532d4afb720SToby Isaac { 2533d4afb720SToby Isaac PetscInt c, d, s, total, subtotal, nexttotal; 2534d4afb720SToby Isaac 2535d4afb720SToby Isaac PetscFunctionBeginHot; 2536d4afb720SToby Isaac if (len < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative"); 2537d4afb720SToby Isaac if (index < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index must be non-negative"); 2538d4afb720SToby Isaac if (!len) { 2539d4afb720SToby Isaac if (!sum && !index) PetscFunctionReturn(0); 2540d4afb720SToby Isaac SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid index or sum for length 0 barycentric coordinate"); 2541d4afb720SToby Isaac } 2542d4afb720SToby Isaac for (c = 1, total = 1; c <= len; c++) { 2543d4afb720SToby Isaac /* total is the number of ways to have a tuple of length c with sum */ 2544d4afb720SToby Isaac if (index < total) break; 2545d4afb720SToby Isaac total = (total * (sum + c)) / c; 2546d4afb720SToby Isaac } 2547d4afb720SToby Isaac if (c > len) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index out of range"); 2548d4afb720SToby Isaac for (d = c; d < len; d++) coord[d] = 0; 2549d4afb720SToby Isaac for (s = 0, subtotal = 1, nexttotal = 1; c > 0;) { 2550d4afb720SToby Isaac /* subtotal is the number of ways to have a tuple of length c with sum s */ 2551d4afb720SToby Isaac /* nexttotal is the number of ways to have a tuple of length c-1 with sum s */ 2552d4afb720SToby Isaac if ((index + subtotal) >= total) { 2553d4afb720SToby Isaac coord[--c] = sum - s; 2554d4afb720SToby Isaac index -= (total - subtotal); 2555d4afb720SToby Isaac sum = s; 2556d4afb720SToby Isaac total = nexttotal; 2557d4afb720SToby Isaac subtotal = 1; 2558d4afb720SToby Isaac nexttotal = 1; 2559d4afb720SToby Isaac s = 0; 2560d4afb720SToby Isaac } else { 2561d4afb720SToby Isaac subtotal = (subtotal * (c + s)) / (s + 1); 2562d4afb720SToby Isaac nexttotal = (nexttotal * (c - 1 + s)) / (s + 1); 2563d4afb720SToby Isaac s++; 2564d4afb720SToby Isaac } 2565d4afb720SToby Isaac } 2566d4afb720SToby Isaac PetscFunctionReturn(0); 2567d4afb720SToby Isaac } 2568d4afb720SToby Isaac 2569d4afb720SToby Isaac /*@ 2570d4afb720SToby Isaac PetscDTBaryToIndex - convert a barycentric coordinate to an index 2571d4afb720SToby Isaac 2572d4afb720SToby Isaac Input Parameters: 2573d4afb720SToby 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) 2574d4afb720SToby Isaac . sum - the value that the sum of the barycentric coordinates (which will be non-negative integers) should sum to 2575d4afb720SToby Isaac - coord - a barycentric coordinate with the given length and sum 2576d4afb720SToby Isaac 2577d4afb720SToby Isaac Output Parameter: 2578d4afb720SToby Isaac . index - the unique index for the coordinate, >= 0 and < Binomial(len - 1 + sum, sum) 2579d4afb720SToby Isaac 2580d4afb720SToby Isaac Level: beginner 2581d4afb720SToby Isaac 2582d4afb720SToby Isaac Note: the indices map to barycentric coordinates in lexicographic order, where the first index is the 2583d4afb720SToby Isaac least significant and the last index is the most significant. 2584d4afb720SToby Isaac 2585d4afb720SToby Isaac .seealso: PetscDTIndexToBary 2586d4afb720SToby Isaac @*/ 2587d4afb720SToby Isaac PetscErrorCode PetscDTBaryToIndex(PetscInt len, PetscInt sum, const PetscInt coord[], PetscInt *index) 2588d4afb720SToby Isaac { 2589d4afb720SToby Isaac PetscInt c; 2590d4afb720SToby Isaac PetscInt i; 2591d4afb720SToby Isaac PetscInt total; 2592d4afb720SToby Isaac 2593d4afb720SToby Isaac PetscFunctionBeginHot; 2594d4afb720SToby Isaac if (len < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative"); 2595d4afb720SToby Isaac if (!len) { 2596d4afb720SToby Isaac if (!sum) { 2597d4afb720SToby Isaac *index = 0; 2598d4afb720SToby Isaac PetscFunctionReturn(0); 2599d4afb720SToby Isaac } 2600d4afb720SToby Isaac SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid index or sum for length 0 barycentric coordinate"); 2601d4afb720SToby Isaac } 2602d4afb720SToby Isaac for (c = 1, total = 1; c < len; c++) total = (total * (sum + c)) / c; 2603d4afb720SToby Isaac i = total - 1; 2604d4afb720SToby Isaac c = len - 1; 2605d4afb720SToby Isaac sum -= coord[c]; 2606d4afb720SToby Isaac while (sum > 0) { 2607d4afb720SToby Isaac PetscInt subtotal; 2608d4afb720SToby Isaac PetscInt s; 2609d4afb720SToby Isaac 2610d4afb720SToby Isaac for (s = 1, subtotal = 1; s < sum; s++) subtotal = (subtotal * (c + s)) / s; 2611d4afb720SToby Isaac i -= subtotal; 2612d4afb720SToby Isaac sum -= coord[--c]; 2613d4afb720SToby Isaac } 2614d4afb720SToby Isaac *index = i; 2615d4afb720SToby Isaac PetscFunctionReturn(0); 2616d4afb720SToby Isaac } 2617