1 /* Discretization tools */ 2 3 #include <petscconf.h> 4 #if defined(PETSC_HAVE_MATHIMF_H) 5 #include <mathimf.h> /* this needs to be included before math.h */ 6 #endif 7 8 #include <petscdt.h> /*I "petscdt.h" I*/ 9 #include <petscblaslapack.h> 10 #include <petsc-private/petscimpl.h> 11 #include <petsc-private/dtimpl.h> 12 #include <petscviewer.h> 13 #include <petscdmplex.h> 14 #include <petscdmshell.h> 15 16 static PetscBool GaussCite = PETSC_FALSE; 17 const char GaussCitation[] = "@article{GolubWelsch1969,\n" 18 " author = {Golub and Welsch},\n" 19 " title = {Calculation of Quadrature Rules},\n" 20 " journal = {Math. Comp.},\n" 21 " volume = {23},\n" 22 " number = {106},\n" 23 " pages = {221--230},\n" 24 " year = {1969}\n}\n"; 25 26 #undef __FUNCT__ 27 #define __FUNCT__ "PetscQuadratureCreate" 28 PetscErrorCode PetscQuadratureCreate(MPI_Comm comm, PetscQuadrature *q) 29 { 30 PetscErrorCode ierr; 31 32 PetscFunctionBegin; 33 PetscValidPointer(q, 2); 34 ierr = DMInitializePackage();CHKERRQ(ierr); 35 ierr = PetscHeaderCreate(*q,_p_PetscQuadrature,int,PETSC_OBJECT_CLASSID,"PetscQuadrature","Quadrature","DT",comm,PetscQuadratureDestroy,PetscQuadratureView);CHKERRQ(ierr); 36 (*q)->dim = -1; 37 (*q)->numPoints = 0; 38 (*q)->points = NULL; 39 (*q)->weights = NULL; 40 PetscFunctionReturn(0); 41 } 42 43 #undef __FUNCT__ 44 #define __FUNCT__ "PetscQuadratureDestroy" 45 PetscErrorCode PetscQuadratureDestroy(PetscQuadrature *q) 46 { 47 PetscErrorCode ierr; 48 49 PetscFunctionBegin; 50 if (!*q) PetscFunctionReturn(0); 51 PetscValidHeaderSpecific((*q),PETSC_OBJECT_CLASSID,1); 52 if (--((PetscObject)(*q))->refct > 0) { 53 *q = NULL; 54 PetscFunctionReturn(0); 55 } 56 ierr = PetscFree((*q)->points);CHKERRQ(ierr); 57 ierr = PetscFree((*q)->weights);CHKERRQ(ierr); 58 ierr = PetscHeaderDestroy(q);CHKERRQ(ierr); 59 PetscFunctionReturn(0); 60 } 61 62 #undef __FUNCT__ 63 #define __FUNCT__ "PetscQuadratureGetData" 64 PetscErrorCode PetscQuadratureGetData(PetscQuadrature q, PetscInt *dim, PetscInt *npoints, const PetscReal *points[], const PetscReal *weights[]) 65 { 66 PetscFunctionBegin; 67 PetscValidHeaderSpecific(q, PETSC_OBJECT_CLASSID, 1); 68 if (dim) { 69 PetscValidPointer(dim, 2); 70 *dim = q->dim; 71 } 72 if (npoints) { 73 PetscValidPointer(npoints, 3); 74 *npoints = q->numPoints; 75 } 76 if (points) { 77 PetscValidPointer(points, 4); 78 *points = q->points; 79 } 80 if (weights) { 81 PetscValidPointer(weights, 5); 82 *weights = q->weights; 83 } 84 PetscFunctionReturn(0); 85 } 86 87 #undef __FUNCT__ 88 #define __FUNCT__ "PetscQuadratureSetData" 89 PetscErrorCode PetscQuadratureSetData(PetscQuadrature q, PetscInt dim, PetscInt npoints, const PetscReal points[], const PetscReal weights[]) 90 { 91 PetscFunctionBegin; 92 PetscValidHeaderSpecific(q, PETSC_OBJECT_CLASSID, 1); 93 if (dim >= 0) q->dim = dim; 94 if (npoints >= 0) q->numPoints = npoints; 95 if (points) { 96 PetscValidPointer(points, 4); 97 q->points = points; 98 } 99 if (weights) { 100 PetscValidPointer(weights, 5); 101 q->weights = weights; 102 } 103 PetscFunctionReturn(0); 104 } 105 106 #undef __FUNCT__ 107 #define __FUNCT__ "PetscQuadratureView" 108 PetscErrorCode PetscQuadratureView(PetscQuadrature quad, PetscViewer viewer) 109 { 110 PetscInt q, d; 111 PetscErrorCode ierr; 112 113 PetscFunctionBegin; 114 ierr = PetscViewerASCIIPrintf(viewer, "Quadrature on %d points\n (", quad->numPoints);CHKERRQ(ierr); 115 for (q = 0; q < quad->numPoints; ++q) { 116 for (d = 0; d < quad->dim; ++d) { 117 if (d) ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr); 118 ierr = PetscViewerASCIIPrintf(viewer, "%g\n", (double)quad->points[q*quad->dim+d]);CHKERRQ(ierr); 119 } 120 ierr = PetscViewerASCIIPrintf(viewer, ") %g\n", (double)quad->weights[q]);CHKERRQ(ierr); 121 } 122 PetscFunctionReturn(0); 123 } 124 125 #undef __FUNCT__ 126 #define __FUNCT__ "PetscDTLegendreEval" 127 /*@ 128 PetscDTLegendreEval - evaluate Legendre polynomial at points 129 130 Not Collective 131 132 Input Arguments: 133 + npoints - number of spatial points to evaluate at 134 . points - array of locations to evaluate at 135 . ndegree - number of basis degrees to evaluate 136 - degrees - sorted array of degrees to evaluate 137 138 Output Arguments: 139 + B - row-oriented basis evaluation matrix B[point*ndegree + degree] (dimension npoints*ndegrees, allocated by caller) (or NULL) 140 . D - row-oriented derivative evaluation matrix (or NULL) 141 - D2 - row-oriented second derivative evaluation matrix (or NULL) 142 143 Level: intermediate 144 145 .seealso: PetscDTGaussQuadrature() 146 @*/ 147 PetscErrorCode PetscDTLegendreEval(PetscInt npoints,const PetscReal *points,PetscInt ndegree,const PetscInt *degrees,PetscReal *B,PetscReal *D,PetscReal *D2) 148 { 149 PetscInt i,maxdegree; 150 151 PetscFunctionBegin; 152 if (!npoints || !ndegree) PetscFunctionReturn(0); 153 maxdegree = degrees[ndegree-1]; 154 for (i=0; i<npoints; i++) { 155 PetscReal pm1,pm2,pd1,pd2,pdd1,pdd2,x; 156 PetscInt j,k; 157 x = points[i]; 158 pm2 = 0; 159 pm1 = 1; 160 pd2 = 0; 161 pd1 = 0; 162 pdd2 = 0; 163 pdd1 = 0; 164 k = 0; 165 if (degrees[k] == 0) { 166 if (B) B[i*ndegree+k] = pm1; 167 if (D) D[i*ndegree+k] = pd1; 168 if (D2) D2[i*ndegree+k] = pdd1; 169 k++; 170 } 171 for (j=1; j<=maxdegree; j++,k++) { 172 PetscReal p,d,dd; 173 p = ((2*j-1)*x*pm1 - (j-1)*pm2)/j; 174 d = pd2 + (2*j-1)*pm1; 175 dd = pdd2 + (2*j-1)*pd1; 176 pm2 = pm1; 177 pm1 = p; 178 pd2 = pd1; 179 pd1 = d; 180 pdd2 = pdd1; 181 pdd1 = dd; 182 if (degrees[k] == j) { 183 if (B) B[i*ndegree+k] = p; 184 if (D) D[i*ndegree+k] = d; 185 if (D2) D2[i*ndegree+k] = dd; 186 } 187 } 188 } 189 PetscFunctionReturn(0); 190 } 191 192 #undef __FUNCT__ 193 #define __FUNCT__ "PetscDTGaussQuadrature" 194 /*@ 195 PetscDTGaussQuadrature - create Gauss quadrature 196 197 Not Collective 198 199 Input Arguments: 200 + npoints - number of points 201 . a - left end of interval (often-1) 202 - b - right end of interval (often +1) 203 204 Output Arguments: 205 + x - quadrature points 206 - w - quadrature weights 207 208 Level: intermediate 209 210 References: 211 Golub and Welsch, Calculation of Quadrature Rules, Math. Comp. 23(106), 221--230, 1969. 212 213 .seealso: PetscDTLegendreEval() 214 @*/ 215 PetscErrorCode PetscDTGaussQuadrature(PetscInt npoints,PetscReal a,PetscReal b,PetscReal *x,PetscReal *w) 216 { 217 PetscErrorCode ierr; 218 PetscInt i; 219 PetscReal *work; 220 PetscScalar *Z; 221 PetscBLASInt N,LDZ,info; 222 223 PetscFunctionBegin; 224 ierr = PetscCitationsRegister(GaussCitation, &GaussCite);CHKERRQ(ierr); 225 /* Set up the Golub-Welsch system */ 226 for (i=0; i<npoints; i++) { 227 x[i] = 0; /* diagonal is 0 */ 228 if (i) w[i-1] = 0.5 / PetscSqrtReal(1 - 1./PetscSqr(2*i)); 229 } 230 ierr = PetscMalloc2(npoints*npoints,&Z,PetscMax(1,2*npoints-2),&work);CHKERRQ(ierr); 231 ierr = PetscBLASIntCast(npoints,&N);CHKERRQ(ierr); 232 LDZ = N; 233 ierr = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr); 234 PetscStackCallBLAS("LAPACKsteqr",LAPACKsteqr_("I",&N,x,w,Z,&LDZ,work,&info)); 235 ierr = PetscFPTrapPop();CHKERRQ(ierr); 236 if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEQR error"); 237 238 for (i=0; i<(npoints+1)/2; i++) { 239 PetscReal y = 0.5 * (-x[i] + x[npoints-i-1]); /* enforces symmetry */ 240 x[i] = (a+b)/2 - y*(b-a)/2; 241 x[npoints-i-1] = (a+b)/2 + y*(b-a)/2; 242 243 w[i] = w[npoints-1-i] = (b-a)*PetscSqr(0.5*PetscAbsScalar(Z[i*npoints] + Z[(npoints-i-1)*npoints])); 244 } 245 ierr = PetscFree2(Z,work);CHKERRQ(ierr); 246 PetscFunctionReturn(0); 247 } 248 249 #undef __FUNCT__ 250 #define __FUNCT__ "PetscDTFactorial_Internal" 251 /* Evaluates the nth jacobi polynomial with weight parameters a,b at a point x. 252 Recurrence relations implemented from the pseudocode given in Karniadakis and Sherwin, Appendix B */ 253 PETSC_STATIC_INLINE PetscErrorCode PetscDTFactorial_Internal(PetscInt n, PetscReal *factorial) 254 { 255 PetscReal f = 1.0; 256 PetscInt i; 257 258 PetscFunctionBegin; 259 for (i = 1; i < n+1; ++i) f *= i; 260 *factorial = f; 261 PetscFunctionReturn(0); 262 } 263 264 #undef __FUNCT__ 265 #define __FUNCT__ "PetscDTComputeJacobi" 266 /* Evaluates the nth jacobi polynomial with weight parameters a,b at a point x. 267 Recurrence relations implemented from the pseudocode given in Karniadakis and Sherwin, Appendix B */ 268 PETSC_STATIC_INLINE PetscErrorCode PetscDTComputeJacobi(PetscReal a, PetscReal b, PetscInt n, PetscReal x, PetscReal *P) 269 { 270 PetscReal apb, pn1, pn2; 271 PetscInt k; 272 273 PetscFunctionBegin; 274 if (!n) {*P = 1.0; PetscFunctionReturn(0);} 275 if (n == 1) {*P = 0.5 * (a - b + (a + b + 2.0) * x); PetscFunctionReturn(0);} 276 apb = a + b; 277 pn2 = 1.0; 278 pn1 = 0.5 * (a - b + (apb + 2.0) * x); 279 *P = 0.0; 280 for (k = 2; k < n+1; ++k) { 281 PetscReal a1 = 2.0 * k * (k + apb) * (2.0*k + apb - 2.0); 282 PetscReal a2 = (2.0 * k + apb - 1.0) * (a*a - b*b); 283 PetscReal a3 = (2.0 * k + apb - 2.0) * (2.0 * k + apb - 1.0) * (2.0 * k + apb); 284 PetscReal a4 = 2.0 * (k + a - 1.0) * (k + b - 1.0) * (2.0 * k + apb); 285 286 a2 = a2 / a1; 287 a3 = a3 / a1; 288 a4 = a4 / a1; 289 *P = (a2 + a3 * x) * pn1 - a4 * pn2; 290 pn2 = pn1; 291 pn1 = *P; 292 } 293 PetscFunctionReturn(0); 294 } 295 296 #undef __FUNCT__ 297 #define __FUNCT__ "PetscDTComputeJacobiDerivative" 298 /* Evaluates the first derivative of P_{n}^{a,b} at a point x. */ 299 PETSC_STATIC_INLINE PetscErrorCode PetscDTComputeJacobiDerivative(PetscReal a, PetscReal b, PetscInt n, PetscReal x, PetscReal *P) 300 { 301 PetscReal nP; 302 PetscErrorCode ierr; 303 304 PetscFunctionBegin; 305 if (!n) {*P = 0.0; PetscFunctionReturn(0);} 306 ierr = PetscDTComputeJacobi(a+1, b+1, n-1, x, &nP);CHKERRQ(ierr); 307 *P = 0.5 * (a + b + n + 1) * nP; 308 PetscFunctionReturn(0); 309 } 310 311 #undef __FUNCT__ 312 #define __FUNCT__ "PetscDTMapSquareToTriangle_Internal" 313 /* Maps from [-1,1]^2 to the (-1,1) reference triangle */ 314 PETSC_STATIC_INLINE PetscErrorCode PetscDTMapSquareToTriangle_Internal(PetscReal x, PetscReal y, PetscReal *xi, PetscReal *eta) 315 { 316 PetscFunctionBegin; 317 *xi = 0.5 * (1.0 + x) * (1.0 - y) - 1.0; 318 *eta = y; 319 PetscFunctionReturn(0); 320 } 321 322 #undef __FUNCT__ 323 #define __FUNCT__ "PetscDTMapCubeToTetrahedron_Internal" 324 /* Maps from [-1,1]^2 to the (-1,1) reference triangle */ 325 PETSC_STATIC_INLINE PetscErrorCode PetscDTMapCubeToTetrahedron_Internal(PetscReal x, PetscReal y, PetscReal z, PetscReal *xi, PetscReal *eta, PetscReal *zeta) 326 { 327 PetscFunctionBegin; 328 *xi = 0.25 * (1.0 + x) * (1.0 - y) * (1.0 - z) - 1.0; 329 *eta = 0.5 * (1.0 + y) * (1.0 - z) - 1.0; 330 *zeta = z; 331 PetscFunctionReturn(0); 332 } 333 334 #undef __FUNCT__ 335 #define __FUNCT__ "PetscDTGaussJacobiQuadrature1D_Internal" 336 static PetscErrorCode PetscDTGaussJacobiQuadrature1D_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscReal *x, PetscReal *w) 337 { 338 PetscInt maxIter = 100; 339 PetscReal eps = 1.0e-8; 340 PetscReal a1, a2, a3, a4, a5, a6; 341 PetscInt k; 342 PetscErrorCode ierr; 343 344 PetscFunctionBegin; 345 346 a1 = PetscPowReal(2.0, a+b+1); 347 #if defined(PETSC_HAVE_TGAMMA) 348 a2 = PetscTGamma(a + npoints + 1); 349 a3 = PetscTGamma(b + npoints + 1); 350 a4 = PetscTGamma(a + b + npoints + 1); 351 #else 352 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"tgamma() - math routine is unavailable."); 353 #endif 354 355 ierr = PetscDTFactorial_Internal(npoints, &a5);CHKERRQ(ierr); 356 a6 = a1 * a2 * a3 / a4 / a5; 357 /* Computes the m roots of P_{m}^{a,b} on [-1,1] by Newton's method with Chebyshev points as initial guesses. 358 Algorithm implemented from the pseudocode given by Karniadakis and Sherwin and Python in FIAT */ 359 for (k = 0; k < npoints; ++k) { 360 PetscReal r = -PetscCosReal((2.0*k + 1.0) * PETSC_PI / (2.0 * npoints)), dP; 361 PetscInt j; 362 363 if (k > 0) r = 0.5 * (r + x[k-1]); 364 for (j = 0; j < maxIter; ++j) { 365 PetscReal s = 0.0, delta, f, fp; 366 PetscInt i; 367 368 for (i = 0; i < k; ++i) s = s + 1.0 / (r - x[i]); 369 ierr = PetscDTComputeJacobi(a, b, npoints, r, &f);CHKERRQ(ierr); 370 ierr = PetscDTComputeJacobiDerivative(a, b, npoints, r, &fp);CHKERRQ(ierr); 371 delta = f / (fp - f * s); 372 r = r - delta; 373 if (PetscAbs(delta) < eps) break; 374 } 375 x[k] = r; 376 ierr = PetscDTComputeJacobiDerivative(a, b, npoints, x[k], &dP);CHKERRQ(ierr); 377 w[k] = a6 / (1.0 - PetscSqr(x[k])) / PetscSqr(dP); 378 } 379 PetscFunctionReturn(0); 380 } 381 382 #undef __FUNCT__ 383 #define __FUNCT__ "PetscDTGaussJacobiQuadrature" 384 /*@C 385 PetscDTGaussJacobiQuadrature - create Gauss-Jacobi quadrature for a simplex 386 387 Not Collective 388 389 Input Arguments: 390 + dim - The simplex dimension 391 . order - The quadrature order 392 . a - left end of interval (often-1) 393 - b - right end of interval (often +1) 394 395 Output Arguments: 396 . q - A PetscQuadrature object 397 398 Level: intermediate 399 400 References: 401 Karniadakis and Sherwin. 402 FIAT 403 404 .seealso: PetscDTGaussQuadrature() 405 @*/ 406 PetscErrorCode PetscDTGaussJacobiQuadrature(PetscInt dim, PetscInt order, PetscReal a, PetscReal b, PetscQuadrature *q) 407 { 408 PetscInt npoints = dim > 1 ? dim > 2 ? order*PetscSqr(order) : PetscSqr(order) : order; 409 PetscReal *px, *wx, *py, *wy, *pz, *wz, *x, *w; 410 PetscInt i, j, k; 411 PetscErrorCode ierr; 412 413 PetscFunctionBegin; 414 if ((a != -1.0) || (b != 1.0)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must use default internal right now"); 415 ierr = PetscMalloc1(npoints*dim, &x);CHKERRQ(ierr); 416 ierr = PetscMalloc1(npoints, &w);CHKERRQ(ierr); 417 switch (dim) { 418 case 0: 419 ierr = PetscFree(x);CHKERRQ(ierr); 420 ierr = PetscFree(w);CHKERRQ(ierr); 421 ierr = PetscMalloc1(1, &x);CHKERRQ(ierr); 422 ierr = PetscMalloc1(1, &w);CHKERRQ(ierr); 423 x[0] = 0.0; 424 w[0] = 1.0; 425 break; 426 case 1: 427 ierr = PetscDTGaussJacobiQuadrature1D_Internal(order, 0.0, 0.0, x, w);CHKERRQ(ierr); 428 break; 429 case 2: 430 ierr = PetscMalloc4(order,&px,order,&wx,order,&py,order,&wy);CHKERRQ(ierr); 431 ierr = PetscDTGaussJacobiQuadrature1D_Internal(order, 0.0, 0.0, px, wx);CHKERRQ(ierr); 432 ierr = PetscDTGaussJacobiQuadrature1D_Internal(order, 1.0, 0.0, py, wy);CHKERRQ(ierr); 433 for (i = 0; i < order; ++i) { 434 for (j = 0; j < order; ++j) { 435 ierr = PetscDTMapSquareToTriangle_Internal(px[i], py[j], &x[(i*order+j)*2+0], &x[(i*order+j)*2+1]);CHKERRQ(ierr); 436 w[i*order+j] = 0.5 * wx[i] * wy[j]; 437 } 438 } 439 ierr = PetscFree4(px,wx,py,wy);CHKERRQ(ierr); 440 break; 441 case 3: 442 ierr = PetscMalloc6(order,&px,order,&wx,order,&py,order,&wy,order,&pz,order,&wz);CHKERRQ(ierr); 443 ierr = PetscDTGaussJacobiQuadrature1D_Internal(order, 0.0, 0.0, px, wx);CHKERRQ(ierr); 444 ierr = PetscDTGaussJacobiQuadrature1D_Internal(order, 1.0, 0.0, py, wy);CHKERRQ(ierr); 445 ierr = PetscDTGaussJacobiQuadrature1D_Internal(order, 2.0, 0.0, pz, wz);CHKERRQ(ierr); 446 for (i = 0; i < order; ++i) { 447 for (j = 0; j < order; ++j) { 448 for (k = 0; k < order; ++k) { 449 ierr = PetscDTMapCubeToTetrahedron_Internal(px[i], py[j], pz[k], &x[((i*order+j)*order+k)*3+0], &x[((i*order+j)*order+k)*3+1], &x[((i*order+j)*order+k)*3+2]);CHKERRQ(ierr); 450 w[(i*order+j)*order+k] = 0.125 * wx[i] * wy[j] * wz[k]; 451 } 452 } 453 } 454 ierr = PetscFree6(px,wx,py,wy,pz,wz);CHKERRQ(ierr); 455 break; 456 default: 457 SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct quadrature rule for dimension %d", dim); 458 } 459 ierr = PetscQuadratureCreate(PETSC_COMM_SELF, q);CHKERRQ(ierr); 460 ierr = PetscQuadratureSetData(*q, dim, npoints, x, w);CHKERRQ(ierr); 461 PetscFunctionReturn(0); 462 } 463 464 #undef __FUNCT__ 465 #define __FUNCT__ "PetscDTPseudoInverseQR" 466 /* Overwrites A. Can only handle full-rank problems with m>=n 467 * A in column-major format 468 * Ainv in row-major format 469 * tau has length m 470 * worksize must be >= max(1,n) 471 */ 472 static PetscErrorCode PetscDTPseudoInverseQR(PetscInt m,PetscInt mstride,PetscInt n,PetscReal *A_in,PetscReal *Ainv_out,PetscScalar *tau,PetscInt worksize,PetscScalar *work) 473 { 474 PetscErrorCode ierr; 475 PetscBLASInt M,N,K,lda,ldb,ldwork,info; 476 PetscScalar *A,*Ainv,*R,*Q,Alpha; 477 478 PetscFunctionBegin; 479 #if defined(PETSC_USE_COMPLEX) 480 { 481 PetscInt i,j; 482 ierr = PetscMalloc2(m*n,&A,m*n,&Ainv);CHKERRQ(ierr); 483 for (j=0; j<n; j++) { 484 for (i=0; i<m; i++) A[i+m*j] = A_in[i+mstride*j]; 485 } 486 mstride = m; 487 } 488 #else 489 A = A_in; 490 Ainv = Ainv_out; 491 #endif 492 493 ierr = PetscBLASIntCast(m,&M);CHKERRQ(ierr); 494 ierr = PetscBLASIntCast(n,&N);CHKERRQ(ierr); 495 ierr = PetscBLASIntCast(mstride,&lda);CHKERRQ(ierr); 496 ierr = PetscBLASIntCast(worksize,&ldwork);CHKERRQ(ierr); 497 ierr = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr); 498 PetscStackCallBLAS("LAPACKgeqrf",LAPACKgeqrf_(&M,&N,A,&lda,tau,work,&ldwork,&info)); 499 ierr = PetscFPTrapPop();CHKERRQ(ierr); 500 if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"xGEQRF error"); 501 R = A; /* Upper triangular part of A now contains R, the rest contains the elementary reflectors */ 502 503 /* Extract an explicit representation of Q */ 504 Q = Ainv; 505 ierr = PetscMemcpy(Q,A,mstride*n*sizeof(PetscScalar));CHKERRQ(ierr); 506 K = N; /* full rank */ 507 PetscStackCallBLAS("LAPACKungqr",LAPACKungqr_(&M,&N,&K,Q,&lda,tau,work,&ldwork,&info)); 508 if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"xORGQR/xUNGQR error"); 509 510 /* Compute A^{-T} = (R^{-1} Q^T)^T = Q R^{-T} */ 511 Alpha = 1.0; 512 ldb = lda; 513 PetscStackCallBLAS("BLAStrsm",BLAStrsm_("Right","Upper","ConjugateTranspose","NotUnitTriangular",&M,&N,&Alpha,R,&lda,Q,&ldb)); 514 /* Ainv is Q, overwritten with inverse */ 515 516 #if defined(PETSC_USE_COMPLEX) 517 { 518 PetscInt i; 519 for (i=0; i<m*n; i++) Ainv_out[i] = PetscRealPart(Ainv[i]); 520 ierr = PetscFree2(A,Ainv);CHKERRQ(ierr); 521 } 522 #endif 523 PetscFunctionReturn(0); 524 } 525 526 #undef __FUNCT__ 527 #define __FUNCT__ "PetscDTLegendreIntegrate" 528 /* Computes integral of L_p' over intervals {(x0,x1),(x1,x2),...} */ 529 static PetscErrorCode PetscDTLegendreIntegrate(PetscInt ninterval,const PetscReal *x,PetscInt ndegree,const PetscInt *degrees,PetscBool Transpose,PetscReal *B) 530 { 531 PetscErrorCode ierr; 532 PetscReal *Bv; 533 PetscInt i,j; 534 535 PetscFunctionBegin; 536 ierr = PetscMalloc1((ninterval+1)*ndegree,&Bv);CHKERRQ(ierr); 537 /* Point evaluation of L_p on all the source vertices */ 538 ierr = PetscDTLegendreEval(ninterval+1,x,ndegree,degrees,Bv,NULL,NULL);CHKERRQ(ierr); 539 /* Integral over each interval: \int_a^b L_p' = L_p(b)-L_p(a) */ 540 for (i=0; i<ninterval; i++) { 541 for (j=0; j<ndegree; j++) { 542 if (Transpose) B[i+ninterval*j] = Bv[(i+1)*ndegree+j] - Bv[i*ndegree+j]; 543 else B[i*ndegree+j] = Bv[(i+1)*ndegree+j] - Bv[i*ndegree+j]; 544 } 545 } 546 ierr = PetscFree(Bv);CHKERRQ(ierr); 547 PetscFunctionReturn(0); 548 } 549 550 #undef __FUNCT__ 551 #define __FUNCT__ "PetscDTReconstructPoly" 552 /*@ 553 PetscDTReconstructPoly - create matrix representing polynomial reconstruction using cell intervals and evaluation at target intervals 554 555 Not Collective 556 557 Input Arguments: 558 + degree - degree of reconstruction polynomial 559 . nsource - number of source intervals 560 . sourcex - sorted coordinates of source cell boundaries (length nsource+1) 561 . ntarget - number of target intervals 562 - targetx - sorted coordinates of target cell boundaries (length ntarget+1) 563 564 Output Arguments: 565 . R - reconstruction matrix, utarget = sum_s R[t*nsource+s] * usource[s] 566 567 Level: advanced 568 569 .seealso: PetscDTLegendreEval() 570 @*/ 571 PetscErrorCode PetscDTReconstructPoly(PetscInt degree,PetscInt nsource,const PetscReal *sourcex,PetscInt ntarget,const PetscReal *targetx,PetscReal *R) 572 { 573 PetscErrorCode ierr; 574 PetscInt i,j,k,*bdegrees,worksize; 575 PetscReal xmin,xmax,center,hscale,*sourcey,*targety,*Bsource,*Bsinv,*Btarget; 576 PetscScalar *tau,*work; 577 578 PetscFunctionBegin; 579 PetscValidRealPointer(sourcex,3); 580 PetscValidRealPointer(targetx,5); 581 PetscValidRealPointer(R,6); 582 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); 583 #if defined(PETSC_USE_DEBUG) 584 for (i=0; i<nsource; i++) { 585 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]); 586 } 587 for (i=0; i<ntarget; i++) { 588 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]); 589 } 590 #endif 591 xmin = PetscMin(sourcex[0],targetx[0]); 592 xmax = PetscMax(sourcex[nsource],targetx[ntarget]); 593 center = (xmin + xmax)/2; 594 hscale = (xmax - xmin)/2; 595 worksize = nsource; 596 ierr = PetscMalloc4(degree+1,&bdegrees,nsource+1,&sourcey,nsource*(degree+1),&Bsource,worksize,&work);CHKERRQ(ierr); 597 ierr = PetscMalloc4(nsource,&tau,nsource*(degree+1),&Bsinv,ntarget+1,&targety,ntarget*(degree+1),&Btarget);CHKERRQ(ierr); 598 for (i=0; i<=nsource; i++) sourcey[i] = (sourcex[i]-center)/hscale; 599 for (i=0; i<=degree; i++) bdegrees[i] = i+1; 600 ierr = PetscDTLegendreIntegrate(nsource,sourcey,degree+1,bdegrees,PETSC_TRUE,Bsource);CHKERRQ(ierr); 601 ierr = PetscDTPseudoInverseQR(nsource,nsource,degree+1,Bsource,Bsinv,tau,nsource,work);CHKERRQ(ierr); 602 for (i=0; i<=ntarget; i++) targety[i] = (targetx[i]-center)/hscale; 603 ierr = PetscDTLegendreIntegrate(ntarget,targety,degree+1,bdegrees,PETSC_FALSE,Btarget);CHKERRQ(ierr); 604 for (i=0; i<ntarget; i++) { 605 PetscReal rowsum = 0; 606 for (j=0; j<nsource; j++) { 607 PetscReal sum = 0; 608 for (k=0; k<degree+1; k++) { 609 sum += Btarget[i*(degree+1)+k] * Bsinv[k*nsource+j]; 610 } 611 R[i*nsource+j] = sum; 612 rowsum += sum; 613 } 614 for (j=0; j<nsource; j++) R[i*nsource+j] /= rowsum; /* normalize each row */ 615 } 616 ierr = PetscFree4(bdegrees,sourcey,Bsource,work);CHKERRQ(ierr); 617 ierr = PetscFree4(tau,Bsinv,targety,Btarget);CHKERRQ(ierr); 618 PetscFunctionReturn(0); 619 } 620