Actual source code: fv.c
1: #include <petsc/private/petscfvimpl.h>
2: #include <petscdmplex.h>
3: #include <petscdmplextransform.h>
4: #include <petscds.h>
6: PetscClassId PETSCLIMITER_CLASSID = 0;
8: PetscFunctionList PetscLimiterList = NULL;
9: PetscBool PetscLimiterRegisterAllCalled = PETSC_FALSE;
11: PetscBool Limitercite = PETSC_FALSE;
12: const char LimiterCitation[] = "@article{BergerAftosmisMurman2005,\n"
13: " title = {Analysis of slope limiters on irregular grids},\n"
14: " journal = {AIAA paper},\n"
15: " author = {Marsha Berger and Michael J. Aftosmis and Scott M. Murman},\n"
16: " volume = {490},\n"
17: " year = {2005}\n}\n";
19: /*@
20: PetscLimiterRegister - Adds a new `PetscLimiter` implementation
22: Not Collective, No Fortran Support
24: Input Parameters:
25: + sname - The name of a new user-defined creation routine
26: - function - The creation routine
28: Example Usage:
29: .vb
30: PetscLimiterRegister("my_lim", MyPetscLimiterCreate);
31: .ve
33: Then, your `PetscLimiter` type can be chosen with the procedural interface via
34: .vb
35: PetscLimiterCreate(MPI_Comm, PetscLimiter *);
36: PetscLimiterSetType(PetscLimiter, "my_lim");
37: .ve
38: or at runtime via the option
39: .vb
40: -petsclimiter_type my_lim
41: .ve
43: Level: advanced
45: Note:
46: `PetscLimiterRegister()` may be called multiple times to add several user-defined PetscLimiters
48: .seealso: `PetscLimiter`, `PetscLimiterType`, `PetscLimiterRegisterAll()`
49: @*/
50: PetscErrorCode PetscLimiterRegister(const char sname[], PetscErrorCode (*function)(PetscLimiter))
51: {
52: PetscFunctionBegin;
53: PetscCall(PetscFunctionListAdd(&PetscLimiterList, sname, function));
54: PetscFunctionReturn(PETSC_SUCCESS);
55: }
57: /*@
58: PetscLimiterSetType - Builds a `PetscLimiter` for a given `PetscLimiterType`
60: Collective
62: Input Parameters:
63: + lim - The `PetscLimiter` object
64: - name - The kind of limiter
66: Options Database Key:
67: . -petsclimiter_type type - Sets the PetscLimiter type; use -help for a list of available types
69: Level: intermediate
71: .seealso: `PetscLimiter`, `PetscLimiterType`, `PetscLimiterGetType()`, `PetscLimiterCreate()`
72: @*/
73: PetscErrorCode PetscLimiterSetType(PetscLimiter lim, PetscLimiterType name)
74: {
75: PetscErrorCode (*r)(PetscLimiter);
76: PetscBool match;
78: PetscFunctionBegin;
80: PetscCall(PetscObjectTypeCompare((PetscObject)lim, name, &match));
81: if (match) PetscFunctionReturn(PETSC_SUCCESS);
83: PetscCall(PetscLimiterRegisterAll());
84: PetscCall(PetscFunctionListFind(PetscLimiterList, name, &r));
85: PetscCheck(r, PetscObjectComm((PetscObject)lim), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscLimiter type: %s", name);
87: PetscTryTypeMethod(lim, destroy);
88: lim->ops->destroy = NULL;
90: PetscCall((*r)(lim));
91: PetscCall(PetscObjectChangeTypeName((PetscObject)lim, name));
92: PetscFunctionReturn(PETSC_SUCCESS);
93: }
95: /*@
96: PetscLimiterGetType - Gets the `PetscLimiterType` name (as a string) from the `PetscLimiter`.
98: Not Collective
100: Input Parameter:
101: . lim - The `PetscLimiter`
103: Output Parameter:
104: . name - The `PetscLimiterType`
106: Level: intermediate
108: .seealso: `PetscLimiter`, `PetscLimiterType`, `PetscLimiterSetType()`, `PetscLimiterCreate()`
109: @*/
110: PetscErrorCode PetscLimiterGetType(PetscLimiter lim, PetscLimiterType *name)
111: {
112: PetscFunctionBegin;
114: PetscAssertPointer(name, 2);
115: PetscCall(PetscLimiterRegisterAll());
116: *name = ((PetscObject)lim)->type_name;
117: PetscFunctionReturn(PETSC_SUCCESS);
118: }
120: /*@
121: PetscLimiterViewFromOptions - View a `PetscLimiter` based on values in the options database
123: Collective
125: Input Parameters:
126: + A - the `PetscLimiter` object to view
127: . obj - optional object that provides the options prefix to use, pass `NULL` to use the options prefix of `A`
128: - name - command line option name
130: Options Database Key:
131: . -name viewer_specification - See `PetscOptionsCreateViewer()` for the values of `viewer_specification`
133: Level: intermediate
135: Note:
136: This checks the options database, creates the viewer on-the-fly, uses it and then destroys it. Hence it should not be called in heavily used routines,
137: rather `PetscOptionsCreateViewer()` should be used to construct the viewer once which can then be utilized in the heavily used routine.
139: .seealso: `PetscLimiter`, `PetscLimiterView()`, `PetscObjectViewFromOptions()`, `PetscLimiterCreate()`, `PetscOptionsCreateViewer()`
140: @*/
141: PetscErrorCode PetscLimiterViewFromOptions(PetscLimiter A, PetscObject obj, const char name[])
142: {
143: PetscFunctionBegin;
145: PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
146: PetscFunctionReturn(PETSC_SUCCESS);
147: }
149: /*@
150: PetscLimiterView - Views a `PetscLimiter`
152: Collective
154: Input Parameters:
155: + lim - the `PetscLimiter` object to view
156: - v - the viewer
158: Level: beginner
160: .seealso: `PetscLimiter`, `PetscViewer`, `PetscLimiterDestroy()`, `PetscLimiterViewFromOptions()`
161: @*/
162: PetscErrorCode PetscLimiterView(PetscLimiter lim, PetscViewer v)
163: {
164: PetscFunctionBegin;
166: if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)lim), &v));
167: PetscTryTypeMethod(lim, view, v);
168: PetscFunctionReturn(PETSC_SUCCESS);
169: }
171: /*@
172: PetscLimiterSetFromOptions - sets parameters in a `PetscLimiter` from the options database
174: Collective
176: Input Parameter:
177: . lim - the `PetscLimiter` object to set options for
179: Level: intermediate
181: .seealso: `PetscLimiter`, `PetscLimiterView()`
182: @*/
183: PetscErrorCode PetscLimiterSetFromOptions(PetscLimiter lim)
184: {
185: const char *defaultType;
186: char name[256];
187: PetscBool flg;
189: PetscFunctionBegin;
191: if (!((PetscObject)lim)->type_name) defaultType = PETSCLIMITERSIN;
192: else defaultType = ((PetscObject)lim)->type_name;
193: PetscCall(PetscLimiterRegisterAll());
195: PetscObjectOptionsBegin((PetscObject)lim);
196: PetscCall(PetscOptionsFList("-petsclimiter_type", "Finite volume slope limiter", "PetscLimiterSetType", PetscLimiterList, defaultType, name, sizeof(name), &flg));
197: if (flg) {
198: PetscCall(PetscLimiterSetType(lim, name));
199: } else if (!((PetscObject)lim)->type_name) {
200: PetscCall(PetscLimiterSetType(lim, defaultType));
201: }
202: PetscTryTypeMethod(lim, setfromoptions);
203: /* process any options handlers added with PetscObjectAddOptionsHandler() */
204: PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)lim, PetscOptionsObject));
205: PetscOptionsEnd();
206: PetscCall(PetscLimiterViewFromOptions(lim, NULL, "-petsclimiter_view"));
207: PetscFunctionReturn(PETSC_SUCCESS);
208: }
210: /*@
211: PetscLimiterSetUp - Construct data structures for the `PetscLimiter`
213: Collective
215: Input Parameter:
216: . lim - the `PetscLimiter` object to setup
218: Level: intermediate
220: .seealso: `PetscLimiter`, `PetscLimiterView()`, `PetscLimiterDestroy()`
221: @*/
222: PetscErrorCode PetscLimiterSetUp(PetscLimiter lim)
223: {
224: PetscFunctionBegin;
226: PetscTryTypeMethod(lim, setup);
227: PetscFunctionReturn(PETSC_SUCCESS);
228: }
230: /*@
231: PetscLimiterDestroy - Destroys a `PetscLimiter` object
233: Collective
235: Input Parameter:
236: . lim - the `PetscLimiter` object to destroy
238: Level: beginner
240: .seealso: `PetscLimiter`, `PetscLimiterView()`
241: @*/
242: PetscErrorCode PetscLimiterDestroy(PetscLimiter *lim)
243: {
244: PetscFunctionBegin;
245: if (!*lim) PetscFunctionReturn(PETSC_SUCCESS);
248: if (--((PetscObject)*lim)->refct > 0) {
249: *lim = NULL;
250: PetscFunctionReturn(PETSC_SUCCESS);
251: }
252: ((PetscObject)*lim)->refct = 0;
254: PetscTryTypeMethod(*lim, destroy);
255: PetscCall(PetscHeaderDestroy(lim));
256: PetscFunctionReturn(PETSC_SUCCESS);
257: }
259: /*@
260: PetscLimiterCreate - Creates an empty `PetscLimiter` object. The type can then be set with `PetscLimiterSetType()`.
262: Collective
264: Input Parameter:
265: . comm - The communicator for the `PetscLimiter` object
267: Output Parameter:
268: . lim - The `PetscLimiter` object
270: Level: beginner
272: .seealso: `PetscLimiter`, `PetscLimiterType`, `PetscLimiterSetType()`, `PETSCLIMITERSIN`
273: @*/
274: PetscErrorCode PetscLimiterCreate(MPI_Comm comm, PetscLimiter *lim)
275: {
276: PetscLimiter l;
278: PetscFunctionBegin;
279: PetscAssertPointer(lim, 2);
280: PetscCall(PetscCitationsRegister(LimiterCitation, &Limitercite));
281: PetscCall(PetscFVInitializePackage());
283: PetscCall(PetscHeaderCreate(l, PETSCLIMITER_CLASSID, "PetscLimiter", "Finite Volume Slope Limiter", "PetscLimiter", comm, PetscLimiterDestroy, PetscLimiterView));
285: *lim = l;
286: PetscFunctionReturn(PETSC_SUCCESS);
287: }
289: /*@
290: PetscLimiterLimit - Limit the flux
292: Input Parameters:
293: + lim - The `PetscLimiter`
294: - flim - The input field
296: Output Parameter:
297: . phi - The limited field
299: Level: beginner
301: Note:
302: Limiters given in symmetric form following Berger, Aftosmis, and Murman 2005
303: .vb
304: The classical flux-limited formulation is psi(r) where
306: r = (u[0] - u[-1]) / (u[1] - u[0])
308: The second order TVD region is bounded by
310: psi_minmod(r) = min(1,r) and psi_superbee(r) = min(2, 2r, max(1,r))
312: where all limiters are implicitly clipped to be non-negative. A more convenient slope-limited form is
314: phi(r) = psi(r) 2/(1+r)
316: in which the reconstructed interface values are
318: u(v) = u[0] + phi(r) (grad u)[0] v
320: where v is the vector from centroid to quadrature point. In these variables, the extremal limiters become
322: phi_minmod(r) = 2/(1+r) min(1, r) phi_superbee(r) = 2/(1+r) min(2, 2r, max(1,r))
324: For a nicer symmetric formulation, rewrite in terms of
326: f = (u[1] - u[0]) / (u[1] - u[-1])
328: where r(f) = (1-f)/f. Note that r(1-f) = f/(1-f) = 1/r(f) so the symmetry condition
330: phi(r) = phi(1/r)
332: becomes
334: w(f) = w(1-f).
336: The limiters below implement this final form w(f) = phi(r(f)) = 2f psi(r(f)). The extremal methods are
338: w_minmod(f) = 2 min(f, 1-f) w_superbee(f) = min(4 min(1-f, f), 2 max(1-f, f))
339: .ve
341: .seealso: `PetscLimiter`, `PetscLimiterType`, `PetscLimiterSetType()`, `PetscLimiterCreate()`
342: @*/
343: PetscErrorCode PetscLimiterLimit(PetscLimiter lim, PetscReal flim, PetscReal *phi)
344: {
345: PetscFunctionBegin;
347: PetscAssertPointer(phi, 3);
348: PetscUseTypeMethod(lim, limit, flim, phi);
349: PetscFunctionReturn(PETSC_SUCCESS);
350: }
352: static PetscErrorCode PetscLimiterDestroy_Sin(PetscLimiter lim)
353: {
354: PetscLimiter_Sin *l = (PetscLimiter_Sin *)lim->data;
356: PetscFunctionBegin;
357: PetscCall(PetscFree(l));
358: PetscFunctionReturn(PETSC_SUCCESS);
359: }
361: static PetscErrorCode PetscLimiterView_Sin_Ascii(PetscLimiter lim, PetscViewer viewer)
362: {
363: PetscViewerFormat format;
365: PetscFunctionBegin;
366: PetscCall(PetscViewerGetFormat(viewer, &format));
367: PetscCall(PetscViewerASCIIPrintf(viewer, "Sin Slope Limiter:\n"));
368: PetscFunctionReturn(PETSC_SUCCESS);
369: }
371: static PetscErrorCode PetscLimiterView_Sin(PetscLimiter lim, PetscViewer viewer)
372: {
373: PetscBool isascii;
375: PetscFunctionBegin;
378: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
379: if (isascii) PetscCall(PetscLimiterView_Sin_Ascii(lim, viewer));
380: PetscFunctionReturn(PETSC_SUCCESS);
381: }
383: static PetscErrorCode PetscLimiterLimit_Sin(PetscLimiter lim, PetscReal f, PetscReal *phi)
384: {
385: PetscFunctionBegin;
386: *phi = PetscSinReal(PETSC_PI * PetscMax(0, PetscMin(f, 1)));
387: PetscFunctionReturn(PETSC_SUCCESS);
388: }
390: static PetscErrorCode PetscLimiterInitialize_Sin(PetscLimiter lim)
391: {
392: PetscFunctionBegin;
393: lim->ops->view = PetscLimiterView_Sin;
394: lim->ops->destroy = PetscLimiterDestroy_Sin;
395: lim->ops->limit = PetscLimiterLimit_Sin;
396: PetscFunctionReturn(PETSC_SUCCESS);
397: }
399: /*MC
400: PETSCLIMITERSIN = "sin" - A `PetscLimiter` implementation
402: Level: intermediate
404: .seealso: `PetscLimiter`, `PetscLimiterType`, `PetscLimiterCreate()`, `PetscLimiterSetType()`
405: M*/
407: PETSC_EXTERN PetscErrorCode PetscLimiterCreate_Sin(PetscLimiter lim)
408: {
409: PetscLimiter_Sin *l;
411: PetscFunctionBegin;
413: PetscCall(PetscNew(&l));
414: lim->data = l;
416: PetscCall(PetscLimiterInitialize_Sin(lim));
417: PetscFunctionReturn(PETSC_SUCCESS);
418: }
420: static PetscErrorCode PetscLimiterDestroy_Zero(PetscLimiter lim)
421: {
422: PetscLimiter_Zero *l = (PetscLimiter_Zero *)lim->data;
424: PetscFunctionBegin;
425: PetscCall(PetscFree(l));
426: PetscFunctionReturn(PETSC_SUCCESS);
427: }
429: static PetscErrorCode PetscLimiterView_Zero_Ascii(PetscLimiter lim, PetscViewer viewer)
430: {
431: PetscViewerFormat format;
433: PetscFunctionBegin;
434: PetscCall(PetscViewerGetFormat(viewer, &format));
435: PetscCall(PetscViewerASCIIPrintf(viewer, "Zero Slope Limiter:\n"));
436: PetscFunctionReturn(PETSC_SUCCESS);
437: }
439: static PetscErrorCode PetscLimiterView_Zero(PetscLimiter lim, PetscViewer viewer)
440: {
441: PetscBool isascii;
443: PetscFunctionBegin;
446: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
447: if (isascii) PetscCall(PetscLimiterView_Zero_Ascii(lim, viewer));
448: PetscFunctionReturn(PETSC_SUCCESS);
449: }
451: static PetscErrorCode PetscLimiterLimit_Zero(PetscLimiter lim, PetscReal f, PetscReal *phi)
452: {
453: PetscFunctionBegin;
454: *phi = 0.0;
455: PetscFunctionReturn(PETSC_SUCCESS);
456: }
458: static PetscErrorCode PetscLimiterInitialize_Zero(PetscLimiter lim)
459: {
460: PetscFunctionBegin;
461: lim->ops->view = PetscLimiterView_Zero;
462: lim->ops->destroy = PetscLimiterDestroy_Zero;
463: lim->ops->limit = PetscLimiterLimit_Zero;
464: PetscFunctionReturn(PETSC_SUCCESS);
465: }
467: /*MC
468: PETSCLIMITERZERO = "zero" - A simple `PetscLimiter` implementation
470: Level: intermediate
472: .seealso: `PetscLimiter`, `PetscLimiterType`, `PetscLimiterCreate()`, `PetscLimiterSetType()`
473: M*/
475: PETSC_EXTERN PetscErrorCode PetscLimiterCreate_Zero(PetscLimiter lim)
476: {
477: PetscLimiter_Zero *l;
479: PetscFunctionBegin;
481: PetscCall(PetscNew(&l));
482: lim->data = l;
484: PetscCall(PetscLimiterInitialize_Zero(lim));
485: PetscFunctionReturn(PETSC_SUCCESS);
486: }
488: static PetscErrorCode PetscLimiterDestroy_None(PetscLimiter lim)
489: {
490: PetscLimiter_None *l = (PetscLimiter_None *)lim->data;
492: PetscFunctionBegin;
493: PetscCall(PetscFree(l));
494: PetscFunctionReturn(PETSC_SUCCESS);
495: }
497: static PetscErrorCode PetscLimiterView_None_Ascii(PetscLimiter lim, PetscViewer viewer)
498: {
499: PetscViewerFormat format;
501: PetscFunctionBegin;
502: PetscCall(PetscViewerGetFormat(viewer, &format));
503: PetscCall(PetscViewerASCIIPrintf(viewer, "None Slope Limiter:\n"));
504: PetscFunctionReturn(PETSC_SUCCESS);
505: }
507: static PetscErrorCode PetscLimiterView_None(PetscLimiter lim, PetscViewer viewer)
508: {
509: PetscBool isascii;
511: PetscFunctionBegin;
514: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
515: if (isascii) PetscCall(PetscLimiterView_None_Ascii(lim, viewer));
516: PetscFunctionReturn(PETSC_SUCCESS);
517: }
519: static PetscErrorCode PetscLimiterLimit_None(PetscLimiter lim, PetscReal f, PetscReal *phi)
520: {
521: PetscFunctionBegin;
522: *phi = 1.0;
523: PetscFunctionReturn(PETSC_SUCCESS);
524: }
526: static PetscErrorCode PetscLimiterInitialize_None(PetscLimiter lim)
527: {
528: PetscFunctionBegin;
529: lim->ops->view = PetscLimiterView_None;
530: lim->ops->destroy = PetscLimiterDestroy_None;
531: lim->ops->limit = PetscLimiterLimit_None;
532: PetscFunctionReturn(PETSC_SUCCESS);
533: }
535: /*MC
536: PETSCLIMITERNONE = "none" - A trivial `PetscLimiter` implementation
538: Level: intermediate
540: .seealso: `PetscLimiter`, `PetscLimiterType`, `PetscLimiterCreate()`, `PetscLimiterSetType()`
541: M*/
543: PETSC_EXTERN PetscErrorCode PetscLimiterCreate_None(PetscLimiter lim)
544: {
545: PetscLimiter_None *l;
547: PetscFunctionBegin;
549: PetscCall(PetscNew(&l));
550: lim->data = l;
552: PetscCall(PetscLimiterInitialize_None(lim));
553: PetscFunctionReturn(PETSC_SUCCESS);
554: }
556: static PetscErrorCode PetscLimiterDestroy_Minmod(PetscLimiter lim)
557: {
558: PetscLimiter_Minmod *l = (PetscLimiter_Minmod *)lim->data;
560: PetscFunctionBegin;
561: PetscCall(PetscFree(l));
562: PetscFunctionReturn(PETSC_SUCCESS);
563: }
565: static PetscErrorCode PetscLimiterView_Minmod_Ascii(PetscLimiter lim, PetscViewer viewer)
566: {
567: PetscViewerFormat format;
569: PetscFunctionBegin;
570: PetscCall(PetscViewerGetFormat(viewer, &format));
571: PetscCall(PetscViewerASCIIPrintf(viewer, "Minmod Slope Limiter:\n"));
572: PetscFunctionReturn(PETSC_SUCCESS);
573: }
575: static PetscErrorCode PetscLimiterView_Minmod(PetscLimiter lim, PetscViewer viewer)
576: {
577: PetscBool isascii;
579: PetscFunctionBegin;
582: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
583: if (isascii) PetscCall(PetscLimiterView_Minmod_Ascii(lim, viewer));
584: PetscFunctionReturn(PETSC_SUCCESS);
585: }
587: static PetscErrorCode PetscLimiterLimit_Minmod(PetscLimiter lim, PetscReal f, PetscReal *phi)
588: {
589: PetscFunctionBegin;
590: *phi = 2 * PetscMax(0, PetscMin(f, 1 - f));
591: PetscFunctionReturn(PETSC_SUCCESS);
592: }
594: static PetscErrorCode PetscLimiterInitialize_Minmod(PetscLimiter lim)
595: {
596: PetscFunctionBegin;
597: lim->ops->view = PetscLimiterView_Minmod;
598: lim->ops->destroy = PetscLimiterDestroy_Minmod;
599: lim->ops->limit = PetscLimiterLimit_Minmod;
600: PetscFunctionReturn(PETSC_SUCCESS);
601: }
603: /*MC
604: PETSCLIMITERMINMOD = "minmod" - A `PetscLimiter` implementation
606: Level: intermediate
608: .seealso: `PetscLimiter`, `PetscLimiterType`, `PetscLimiterCreate()`, `PetscLimiterSetType()`
609: M*/
611: PETSC_EXTERN PetscErrorCode PetscLimiterCreate_Minmod(PetscLimiter lim)
612: {
613: PetscLimiter_Minmod *l;
615: PetscFunctionBegin;
617: PetscCall(PetscNew(&l));
618: lim->data = l;
620: PetscCall(PetscLimiterInitialize_Minmod(lim));
621: PetscFunctionReturn(PETSC_SUCCESS);
622: }
624: static PetscErrorCode PetscLimiterDestroy_VanLeer(PetscLimiter lim)
625: {
626: PetscLimiter_VanLeer *l = (PetscLimiter_VanLeer *)lim->data;
628: PetscFunctionBegin;
629: PetscCall(PetscFree(l));
630: PetscFunctionReturn(PETSC_SUCCESS);
631: }
633: static PetscErrorCode PetscLimiterView_VanLeer_Ascii(PetscLimiter lim, PetscViewer viewer)
634: {
635: PetscViewerFormat format;
637: PetscFunctionBegin;
638: PetscCall(PetscViewerGetFormat(viewer, &format));
639: PetscCall(PetscViewerASCIIPrintf(viewer, "Van Leer Slope Limiter:\n"));
640: PetscFunctionReturn(PETSC_SUCCESS);
641: }
643: static PetscErrorCode PetscLimiterView_VanLeer(PetscLimiter lim, PetscViewer viewer)
644: {
645: PetscBool isascii;
647: PetscFunctionBegin;
650: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
651: if (isascii) PetscCall(PetscLimiterView_VanLeer_Ascii(lim, viewer));
652: PetscFunctionReturn(PETSC_SUCCESS);
653: }
655: static PetscErrorCode PetscLimiterLimit_VanLeer(PetscLimiter lim, PetscReal f, PetscReal *phi)
656: {
657: PetscFunctionBegin;
658: *phi = PetscMax(0, 4 * f * (1 - f));
659: PetscFunctionReturn(PETSC_SUCCESS);
660: }
662: static PetscErrorCode PetscLimiterInitialize_VanLeer(PetscLimiter lim)
663: {
664: PetscFunctionBegin;
665: lim->ops->view = PetscLimiterView_VanLeer;
666: lim->ops->destroy = PetscLimiterDestroy_VanLeer;
667: lim->ops->limit = PetscLimiterLimit_VanLeer;
668: PetscFunctionReturn(PETSC_SUCCESS);
669: }
671: /*MC
672: PETSCLIMITERVANLEER = "vanleer" - A `PetscLimiter` implementation
674: Level: intermediate
676: .seealso: `PetscLimiter`, `PetscLimiterType`, `PetscLimiterCreate()`, `PetscLimiterSetType()`
677: M*/
679: PETSC_EXTERN PetscErrorCode PetscLimiterCreate_VanLeer(PetscLimiter lim)
680: {
681: PetscLimiter_VanLeer *l;
683: PetscFunctionBegin;
685: PetscCall(PetscNew(&l));
686: lim->data = l;
688: PetscCall(PetscLimiterInitialize_VanLeer(lim));
689: PetscFunctionReturn(PETSC_SUCCESS);
690: }
692: static PetscErrorCode PetscLimiterDestroy_VanAlbada(PetscLimiter lim)
693: {
694: PetscLimiter_VanAlbada *l = (PetscLimiter_VanAlbada *)lim->data;
696: PetscFunctionBegin;
697: PetscCall(PetscFree(l));
698: PetscFunctionReturn(PETSC_SUCCESS);
699: }
701: static PetscErrorCode PetscLimiterView_VanAlbada_Ascii(PetscLimiter lim, PetscViewer viewer)
702: {
703: PetscViewerFormat format;
705: PetscFunctionBegin;
706: PetscCall(PetscViewerGetFormat(viewer, &format));
707: PetscCall(PetscViewerASCIIPrintf(viewer, "Van Albada Slope Limiter:\n"));
708: PetscFunctionReturn(PETSC_SUCCESS);
709: }
711: static PetscErrorCode PetscLimiterView_VanAlbada(PetscLimiter lim, PetscViewer viewer)
712: {
713: PetscBool isascii;
715: PetscFunctionBegin;
718: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
719: if (isascii) PetscCall(PetscLimiterView_VanAlbada_Ascii(lim, viewer));
720: PetscFunctionReturn(PETSC_SUCCESS);
721: }
723: static PetscErrorCode PetscLimiterLimit_VanAlbada(PetscLimiter lim, PetscReal f, PetscReal *phi)
724: {
725: PetscFunctionBegin;
726: *phi = PetscMax(0, 2 * f * (1 - f) / (PetscSqr(f) + PetscSqr(1 - f)));
727: PetscFunctionReturn(PETSC_SUCCESS);
728: }
730: static PetscErrorCode PetscLimiterInitialize_VanAlbada(PetscLimiter lim)
731: {
732: PetscFunctionBegin;
733: lim->ops->view = PetscLimiterView_VanAlbada;
734: lim->ops->destroy = PetscLimiterDestroy_VanAlbada;
735: lim->ops->limit = PetscLimiterLimit_VanAlbada;
736: PetscFunctionReturn(PETSC_SUCCESS);
737: }
739: /*MC
740: PETSCLIMITERVANALBADA = "vanalbada" - A PetscLimiter implementation
742: Level: intermediate
744: .seealso: `PetscLimiter`, `PetscLimiterType`, `PetscLimiterCreate()`, `PetscLimiterSetType()`
745: M*/
747: PETSC_EXTERN PetscErrorCode PetscLimiterCreate_VanAlbada(PetscLimiter lim)
748: {
749: PetscLimiter_VanAlbada *l;
751: PetscFunctionBegin;
753: PetscCall(PetscNew(&l));
754: lim->data = l;
756: PetscCall(PetscLimiterInitialize_VanAlbada(lim));
757: PetscFunctionReturn(PETSC_SUCCESS);
758: }
760: static PetscErrorCode PetscLimiterDestroy_Superbee(PetscLimiter lim)
761: {
762: PetscLimiter_Superbee *l = (PetscLimiter_Superbee *)lim->data;
764: PetscFunctionBegin;
765: PetscCall(PetscFree(l));
766: PetscFunctionReturn(PETSC_SUCCESS);
767: }
769: static PetscErrorCode PetscLimiterView_Superbee_Ascii(PetscLimiter lim, PetscViewer viewer)
770: {
771: PetscViewerFormat format;
773: PetscFunctionBegin;
774: PetscCall(PetscViewerGetFormat(viewer, &format));
775: PetscCall(PetscViewerASCIIPrintf(viewer, "Superbee Slope Limiter:\n"));
776: PetscFunctionReturn(PETSC_SUCCESS);
777: }
779: static PetscErrorCode PetscLimiterView_Superbee(PetscLimiter lim, PetscViewer viewer)
780: {
781: PetscBool isascii;
783: PetscFunctionBegin;
786: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
787: if (isascii) PetscCall(PetscLimiterView_Superbee_Ascii(lim, viewer));
788: PetscFunctionReturn(PETSC_SUCCESS);
789: }
791: static PetscErrorCode PetscLimiterLimit_Superbee(PetscLimiter lim, PetscReal f, PetscReal *phi)
792: {
793: PetscFunctionBegin;
794: *phi = PetscMax(0, PetscMin(4 * PetscMin(f, 1 - f), 2 * PetscMax(f, 1 - f)));
795: PetscFunctionReturn(PETSC_SUCCESS);
796: }
798: static PetscErrorCode PetscLimiterInitialize_Superbee(PetscLimiter lim)
799: {
800: PetscFunctionBegin;
801: lim->ops->view = PetscLimiterView_Superbee;
802: lim->ops->destroy = PetscLimiterDestroy_Superbee;
803: lim->ops->limit = PetscLimiterLimit_Superbee;
804: PetscFunctionReturn(PETSC_SUCCESS);
805: }
807: /*MC
808: PETSCLIMITERSUPERBEE = "superbee" - A `PetscLimiter` implementation
810: Level: intermediate
812: .seealso: `PetscLimiter`, `PetscLimiterType`, `PetscLimiterCreate()`, `PetscLimiterSetType()`
813: M*/
815: PETSC_EXTERN PetscErrorCode PetscLimiterCreate_Superbee(PetscLimiter lim)
816: {
817: PetscLimiter_Superbee *l;
819: PetscFunctionBegin;
821: PetscCall(PetscNew(&l));
822: lim->data = l;
824: PetscCall(PetscLimiterInitialize_Superbee(lim));
825: PetscFunctionReturn(PETSC_SUCCESS);
826: }
828: static PetscErrorCode PetscLimiterDestroy_MC(PetscLimiter lim)
829: {
830: PetscLimiter_MC *l = (PetscLimiter_MC *)lim->data;
832: PetscFunctionBegin;
833: PetscCall(PetscFree(l));
834: PetscFunctionReturn(PETSC_SUCCESS);
835: }
837: static PetscErrorCode PetscLimiterView_MC_Ascii(PetscLimiter lim, PetscViewer viewer)
838: {
839: PetscViewerFormat format;
841: PetscFunctionBegin;
842: PetscCall(PetscViewerGetFormat(viewer, &format));
843: PetscCall(PetscViewerASCIIPrintf(viewer, "MC Slope Limiter:\n"));
844: PetscFunctionReturn(PETSC_SUCCESS);
845: }
847: static PetscErrorCode PetscLimiterView_MC(PetscLimiter lim, PetscViewer viewer)
848: {
849: PetscBool isascii;
851: PetscFunctionBegin;
854: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
855: if (isascii) PetscCall(PetscLimiterView_MC_Ascii(lim, viewer));
856: PetscFunctionReturn(PETSC_SUCCESS);
857: }
859: /* aka Barth-Jespersen */
860: static PetscErrorCode PetscLimiterLimit_MC(PetscLimiter lim, PetscReal f, PetscReal *phi)
861: {
862: PetscFunctionBegin;
863: *phi = PetscMin(1, 4 * PetscMax(0, PetscMin(f, 1 - f)));
864: PetscFunctionReturn(PETSC_SUCCESS);
865: }
867: static PetscErrorCode PetscLimiterInitialize_MC(PetscLimiter lim)
868: {
869: PetscFunctionBegin;
870: lim->ops->view = PetscLimiterView_MC;
871: lim->ops->destroy = PetscLimiterDestroy_MC;
872: lim->ops->limit = PetscLimiterLimit_MC;
873: PetscFunctionReturn(PETSC_SUCCESS);
874: }
876: /*MC
877: PETSCLIMITERMC = "mc" - A `PetscLimiter` implementation
879: Level: intermediate
881: .seealso: `PetscLimiter`, `PetscLimiterType`, `PetscLimiterCreate()`, `PetscLimiterSetType()`
882: M*/
884: PETSC_EXTERN PetscErrorCode PetscLimiterCreate_MC(PetscLimiter lim)
885: {
886: PetscLimiter_MC *l;
888: PetscFunctionBegin;
890: PetscCall(PetscNew(&l));
891: lim->data = l;
893: PetscCall(PetscLimiterInitialize_MC(lim));
894: PetscFunctionReturn(PETSC_SUCCESS);
895: }
897: PetscClassId PETSCFV_CLASSID = 0;
899: PetscFunctionList PetscFVList = NULL;
900: PetscBool PetscFVRegisterAllCalled = PETSC_FALSE;
902: /*@
903: PetscFVRegister - Adds a new `PetscFV` implementation
905: Not Collective, No Fortran Support
907: Input Parameters:
908: + sname - The name of a new user-defined creation routine
909: - function - The creation routine itself
911: Example Usage:
912: .vb
913: PetscFVRegister("my_fv", MyPetscFVCreate);
914: .ve
916: Then, your PetscFV type can be chosen with the procedural interface via
917: .vb
918: PetscFVCreate(MPI_Comm, PetscFV *);
919: PetscFVSetType(PetscFV, "my_fv");
920: .ve
921: or at runtime via the option
922: .vb
923: -petscfv_type my_fv
924: .ve
926: Level: advanced
928: Note:
929: `PetscFVRegister()` may be called multiple times to add several user-defined PetscFVs
931: .seealso: `PetscFV`, `PetscFVType`, `PetscFVRegisterAll()`
932: @*/
933: PetscErrorCode PetscFVRegister(const char sname[], PetscErrorCode (*function)(PetscFV))
934: {
935: PetscFunctionBegin;
936: PetscCall(PetscFunctionListAdd(&PetscFVList, sname, function));
937: PetscFunctionReturn(PETSC_SUCCESS);
938: }
940: /*@
941: PetscFVSetType - Builds a particular `PetscFV`
943: Collective
945: Input Parameters:
946: + fvm - The `PetscFV` object
947: - name - The type of FVM space
949: Options Database Key:
950: . -petscfv_type type - Sets the `PetscFVType`; use -help for a list of available types
952: Level: intermediate
954: .seealso: `PetscFV`, `PetscFVType`, `PetscFVGetType()`, `PetscFVCreate()`
955: @*/
956: PetscErrorCode PetscFVSetType(PetscFV fvm, PetscFVType name)
957: {
958: PetscErrorCode (*r)(PetscFV);
959: PetscBool match;
961: PetscFunctionBegin;
963: PetscCall(PetscObjectTypeCompare((PetscObject)fvm, name, &match));
964: if (match) PetscFunctionReturn(PETSC_SUCCESS);
966: PetscCall(PetscFVRegisterAll());
967: PetscCall(PetscFunctionListFind(PetscFVList, name, &r));
968: PetscCheck(r, PetscObjectComm((PetscObject)fvm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscFV type: %s", name);
970: PetscTryTypeMethod(fvm, destroy);
971: fvm->ops->destroy = NULL;
973: PetscCall((*r)(fvm));
974: PetscCall(PetscObjectChangeTypeName((PetscObject)fvm, name));
975: PetscFunctionReturn(PETSC_SUCCESS);
976: }
978: /*@
979: PetscFVGetType - Gets the `PetscFVType` (as a string) from a `PetscFV`.
981: Not Collective
983: Input Parameter:
984: . fvm - The `PetscFV`
986: Output Parameter:
987: . name - The `PetscFVType` name
989: Level: intermediate
991: .seealso: `PetscFV`, `PetscFVType`, `PetscFVSetType()`, `PetscFVCreate()`
992: @*/
993: PetscErrorCode PetscFVGetType(PetscFV fvm, PetscFVType *name)
994: {
995: PetscFunctionBegin;
997: PetscAssertPointer(name, 2);
998: PetscCall(PetscFVRegisterAll());
999: *name = ((PetscObject)fvm)->type_name;
1000: PetscFunctionReturn(PETSC_SUCCESS);
1001: }
1003: /*@
1004: PetscFVViewFromOptions - View a `PetscFV` based on values in the options database
1006: Collective
1008: Input Parameters:
1009: + A - the `PetscFV` object
1010: . obj - optional object that provides the options prefix, pass `NULL` to use the options prefix of `A`
1011: - name - command line option name
1013: Options Database Key:
1014: . -name viewer_specification - See `PetscOptionsCreateViewer()` for the values of `viewer_specification`
1016: Level: intermediate
1018: Note:
1019: This checks the options database, creates the viewer on-the-fly, uses it and then destroys it. Hence it should not be called in heavily used routines,
1020: rather `PetscOptionsCreateViewer()` should be used to construct the viewer once which can then be utilized in the heavily used routine.
1022: .seealso: `PetscFV`, `PetscFVView()`, `PetscObjectViewFromOptions()`, `PetscFVCreate()`, `PetscOptionsCreateViewer()`
1023: @*/
1024: PetscErrorCode PetscFVViewFromOptions(PetscFV A, PetscObject obj, const char name[])
1025: {
1026: PetscFunctionBegin;
1028: PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
1029: PetscFunctionReturn(PETSC_SUCCESS);
1030: }
1032: /*@
1033: PetscFVView - Views a `PetscFV`
1035: Collective
1037: Input Parameters:
1038: + fvm - the `PetscFV` object to view
1039: - v - the viewer
1041: Level: beginner
1043: .seealso: `PetscFV`, `PetscViewer`, `PetscFVDestroy()`
1044: @*/
1045: PetscErrorCode PetscFVView(PetscFV fvm, PetscViewer v)
1046: {
1047: PetscFunctionBegin;
1049: if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)fvm), &v));
1050: PetscTryTypeMethod(fvm, view, v);
1051: PetscFunctionReturn(PETSC_SUCCESS);
1052: }
1054: /*@
1055: PetscFVSetFromOptions - sets parameters in a `PetscFV` from the options database
1057: Collective
1059: Input Parameter:
1060: . fvm - the `PetscFV` object to set options for
1062: Options Database Key:
1063: . -petscfv_compute_gradients (true|false) - Determines whether cell gradients are calculated
1065: Level: intermediate
1067: .seealso: `PetscFV`, `PetscFVView()`
1068: @*/
1069: PetscErrorCode PetscFVSetFromOptions(PetscFV fvm)
1070: {
1071: const char *defaultType;
1072: char name[256];
1073: PetscBool flg;
1075: PetscFunctionBegin;
1077: if (!((PetscObject)fvm)->type_name) defaultType = PETSCFVUPWIND;
1078: else defaultType = ((PetscObject)fvm)->type_name;
1079: PetscCall(PetscFVRegisterAll());
1081: PetscObjectOptionsBegin((PetscObject)fvm);
1082: PetscCall(PetscOptionsFList("-petscfv_type", "Finite volume discretization", "PetscFVSetType", PetscFVList, defaultType, name, sizeof(name), &flg));
1083: if (flg) PetscCall(PetscFVSetType(fvm, name));
1084: else if (!((PetscObject)fvm)->type_name) PetscCall(PetscFVSetType(fvm, defaultType));
1085: PetscCall(PetscOptionsBool("-petscfv_compute_gradients", "Compute cell gradients", "PetscFVSetComputeGradients", fvm->computeGradients, &fvm->computeGradients, NULL));
1086: PetscTryTypeMethod(fvm, setfromoptions);
1087: /* process any options handlers added with PetscObjectAddOptionsHandler() */
1088: PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)fvm, PetscOptionsObject));
1089: PetscCall(PetscLimiterSetFromOptions(fvm->limiter));
1090: PetscOptionsEnd();
1091: PetscCall(PetscFVViewFromOptions(fvm, NULL, "-petscfv_view"));
1092: PetscFunctionReturn(PETSC_SUCCESS);
1093: }
1095: /*@
1096: PetscFVSetUp - Setup the data structures for the `PetscFV` based on the `PetscFVType` provided by `PetscFVSetType()`
1098: Collective
1100: Input Parameter:
1101: . fvm - the `PetscFV` object to setup
1103: Level: intermediate
1105: .seealso: `PetscFV`, `PetscFVView()`, `PetscFVDestroy()`
1106: @*/
1107: PetscErrorCode PetscFVSetUp(PetscFV fvm)
1108: {
1109: PetscFunctionBegin;
1111: PetscCall(PetscLimiterSetUp(fvm->limiter));
1112: PetscTryTypeMethod(fvm, setup);
1113: PetscFunctionReturn(PETSC_SUCCESS);
1114: }
1116: /*@
1117: PetscFVDestroy - Destroys a `PetscFV` object
1119: Collective
1121: Input Parameter:
1122: . fvm - the `PetscFV` object to destroy
1124: Level: beginner
1126: .seealso: `PetscFV`, `PetscFVCreate()`, `PetscFVView()`
1127: @*/
1128: PetscErrorCode PetscFVDestroy(PetscFV *fvm)
1129: {
1130: PetscFunctionBegin;
1131: if (!*fvm) PetscFunctionReturn(PETSC_SUCCESS);
1134: if (--((PetscObject)*fvm)->refct > 0) {
1135: *fvm = NULL;
1136: PetscFunctionReturn(PETSC_SUCCESS);
1137: }
1138: ((PetscObject)*fvm)->refct = 0;
1140: for (PetscInt i = 0; i < (*fvm)->numComponents; i++) PetscCall(PetscFree((*fvm)->componentNames[i]));
1141: PetscCall(PetscFree((*fvm)->componentNames));
1142: PetscCall(PetscLimiterDestroy(&(*fvm)->limiter));
1143: PetscCall(PetscDualSpaceDestroy(&(*fvm)->dualSpace));
1144: PetscCall(PetscFree((*fvm)->fluxWork));
1145: PetscCall(PetscQuadratureDestroy(&(*fvm)->quadrature));
1146: PetscCall(PetscTabulationDestroy(&(*fvm)->T));
1148: PetscTryTypeMethod(*fvm, destroy);
1149: PetscCall(PetscHeaderDestroy(fvm));
1150: PetscFunctionReturn(PETSC_SUCCESS);
1151: }
1153: /*@
1154: PetscFVCreate - Creates an empty `PetscFV` object. The type can then be set with `PetscFVSetType()`.
1156: Collective
1158: Input Parameter:
1159: . comm - The communicator for the `PetscFV` object
1161: Output Parameter:
1162: . fvm - The `PetscFV` object
1164: Level: beginner
1166: .seealso: `PetscFVSetUp()`, `PetscFVSetType()`, `PETSCFVUPWIND`, `PetscFVDestroy()`
1167: @*/
1168: PetscErrorCode PetscFVCreate(MPI_Comm comm, PetscFV *fvm)
1169: {
1170: PetscFV f;
1172: PetscFunctionBegin;
1173: PetscAssertPointer(fvm, 2);
1174: PetscCall(PetscFVInitializePackage());
1176: PetscCall(PetscHeaderCreate(f, PETSCFV_CLASSID, "PetscFV", "Finite Volume", "PetscFV", comm, PetscFVDestroy, PetscFVView));
1177: PetscCall(PetscMemzero(f->ops, sizeof(struct _PetscFVOps)));
1178: PetscCall(PetscLimiterCreate(comm, &f->limiter));
1179: f->numComponents = 1;
1180: f->dim = 0;
1181: f->computeGradients = PETSC_FALSE;
1182: f->fluxWork = NULL;
1183: PetscCall(PetscCalloc1(f->numComponents, &f->componentNames));
1185: *fvm = f;
1186: PetscFunctionReturn(PETSC_SUCCESS);
1187: }
1189: /*@
1190: PetscFVSetLimiter - Set the `PetscLimiter` to the `PetscFV`
1192: Logically Collective
1194: Input Parameters:
1195: + fvm - the `PetscFV` object
1196: - lim - The `PetscLimiter`
1198: Level: intermediate
1200: .seealso: `PetscFV`, `PetscLimiter`, `PetscFVGetLimiter()`
1201: @*/
1202: PetscErrorCode PetscFVSetLimiter(PetscFV fvm, PetscLimiter lim)
1203: {
1204: PetscFunctionBegin;
1207: PetscCall(PetscLimiterDestroy(&fvm->limiter));
1208: PetscCall(PetscObjectReference((PetscObject)lim));
1209: fvm->limiter = lim;
1210: PetscFunctionReturn(PETSC_SUCCESS);
1211: }
1213: /*@
1214: PetscFVGetLimiter - Get the `PetscLimiter` object from the `PetscFV`
1216: Not Collective
1218: Input Parameter:
1219: . fvm - the `PetscFV` object
1221: Output Parameter:
1222: . lim - The `PetscLimiter`
1224: Level: intermediate
1226: .seealso: `PetscFV`, `PetscLimiter`, `PetscFVSetLimiter()`
1227: @*/
1228: PetscErrorCode PetscFVGetLimiter(PetscFV fvm, PetscLimiter *lim)
1229: {
1230: PetscFunctionBegin;
1232: PetscAssertPointer(lim, 2);
1233: *lim = fvm->limiter;
1234: PetscFunctionReturn(PETSC_SUCCESS);
1235: }
1237: /*@
1238: PetscFVSetNumComponents - Set the number of field components in a `PetscFV`
1240: Logically Collective
1242: Input Parameters:
1243: + fvm - the `PetscFV` object
1244: - comp - The number of components
1246: Level: intermediate
1248: .seealso: `PetscFV`, `PetscFVGetNumComponents()`
1249: @*/
1250: PetscErrorCode PetscFVSetNumComponents(PetscFV fvm, PetscInt comp)
1251: {
1252: PetscFunctionBegin;
1254: if (fvm->numComponents != comp) {
1255: for (PetscInt i = 0; i < fvm->numComponents; i++) PetscCall(PetscFree(fvm->componentNames[i]));
1256: PetscCall(PetscFree(fvm->componentNames));
1257: PetscCall(PetscCalloc1(comp, &fvm->componentNames));
1258: }
1259: fvm->numComponents = comp;
1260: PetscCall(PetscFree(fvm->fluxWork));
1261: PetscCall(PetscMalloc1(comp, &fvm->fluxWork));
1262: PetscFunctionReturn(PETSC_SUCCESS);
1263: }
1265: /*@
1266: PetscFVGetNumComponents - Get the number of field components in a `PetscFV`
1268: Not Collective
1270: Input Parameter:
1271: . fvm - the `PetscFV` object
1273: Output Parameter:
1274: . comp - The number of components
1276: Level: intermediate
1278: .seealso: `PetscFV`, `PetscFVSetNumComponents()`, `PetscFVSetComponentName()`
1279: @*/
1280: PetscErrorCode PetscFVGetNumComponents(PetscFV fvm, PetscInt *comp)
1281: {
1282: PetscFunctionBegin;
1284: PetscAssertPointer(comp, 2);
1285: *comp = fvm->numComponents;
1286: PetscFunctionReturn(PETSC_SUCCESS);
1287: }
1289: /*@
1290: PetscFVSetComponentName - Set the name of a component (used in output and viewing) in a `PetscFV`
1292: Logically Collective
1294: Input Parameters:
1295: + fvm - the `PetscFV` object
1296: . comp - the component number
1297: - name - the component name
1299: Level: intermediate
1301: .seealso: `PetscFV`, `PetscFVGetComponentName()`
1302: @*/
1303: PetscErrorCode PetscFVSetComponentName(PetscFV fvm, PetscInt comp, const char *name)
1304: {
1305: PetscFunctionBegin;
1306: PetscCall(PetscFree(fvm->componentNames[comp]));
1307: PetscCall(PetscStrallocpy(name, &fvm->componentNames[comp]));
1308: PetscFunctionReturn(PETSC_SUCCESS);
1309: }
1311: /*@
1312: PetscFVGetComponentName - Get the name of a component (used in output and viewing) in a `PetscFV`
1314: Logically Collective
1316: Input Parameters:
1317: + fvm - the `PetscFV` object
1318: - comp - the component number
1320: Output Parameter:
1321: . name - the component name
1323: Level: intermediate
1325: .seealso: `PetscFV`, `PetscFVSetComponentName()`
1326: @*/
1327: PetscErrorCode PetscFVGetComponentName(PetscFV fvm, PetscInt comp, const char *name[])
1328: {
1329: PetscFunctionBegin;
1330: *name = fvm->componentNames[comp];
1331: PetscFunctionReturn(PETSC_SUCCESS);
1332: }
1334: /*@
1335: PetscFVSetSpatialDimension - Set the spatial dimension of a `PetscFV`
1337: Logically Collective
1339: Input Parameters:
1340: + fvm - the `PetscFV` object
1341: - dim - The spatial dimension
1343: Level: intermediate
1345: .seealso: `PetscFV`, `PetscFVGetSpatialDimension()`
1346: @*/
1347: PetscErrorCode PetscFVSetSpatialDimension(PetscFV fvm, PetscInt dim)
1348: {
1349: PetscFunctionBegin;
1351: fvm->dim = dim;
1352: PetscFunctionReturn(PETSC_SUCCESS);
1353: }
1355: /*@
1356: PetscFVGetSpatialDimension - Get the spatial dimension of a `PetscFV`
1358: Not Collective
1360: Input Parameter:
1361: . fvm - the `PetscFV` object
1363: Output Parameter:
1364: . dim - The spatial dimension
1366: Level: intermediate
1368: .seealso: `PetscFV`, `PetscFVSetSpatialDimension()`
1369: @*/
1370: PetscErrorCode PetscFVGetSpatialDimension(PetscFV fvm, PetscInt *dim)
1371: {
1372: PetscFunctionBegin;
1374: PetscAssertPointer(dim, 2);
1375: *dim = fvm->dim;
1376: PetscFunctionReturn(PETSC_SUCCESS);
1377: }
1379: /*@
1380: PetscFVSetComputeGradients - Toggle computation of cell gradients on a `PetscFV`
1382: Logically Collective
1384: Input Parameters:
1385: + fvm - the `PetscFV` object
1386: - computeGradients - Flag to compute cell gradients
1388: Level: intermediate
1390: .seealso: `PetscFV`, `PetscFVGetComputeGradients()`
1391: @*/
1392: PetscErrorCode PetscFVSetComputeGradients(PetscFV fvm, PetscBool computeGradients)
1393: {
1394: PetscFunctionBegin;
1396: fvm->computeGradients = computeGradients;
1397: PetscFunctionReturn(PETSC_SUCCESS);
1398: }
1400: /*@
1401: PetscFVGetComputeGradients - Return flag for computation of cell gradients on a `PetscFV`
1403: Not Collective
1405: Input Parameter:
1406: . fvm - the `PetscFV` object
1408: Output Parameter:
1409: . computeGradients - Flag to compute cell gradients
1411: Level: intermediate
1413: .seealso: `PetscFV`, `PetscFVSetComputeGradients()`
1414: @*/
1415: PetscErrorCode PetscFVGetComputeGradients(PetscFV fvm, PetscBool *computeGradients)
1416: {
1417: PetscFunctionBegin;
1419: PetscAssertPointer(computeGradients, 2);
1420: *computeGradients = fvm->computeGradients;
1421: PetscFunctionReturn(PETSC_SUCCESS);
1422: }
1424: /*@
1425: PetscFVSetQuadrature - Set the `PetscQuadrature` object for a `PetscFV`
1427: Logically Collective
1429: Input Parameters:
1430: + fvm - the `PetscFV` object
1431: - q - The `PetscQuadrature`
1433: Level: intermediate
1435: .seealso: `PetscQuadrature`, `PetscFV`, `PetscFVGetQuadrature()`
1436: @*/
1437: PetscErrorCode PetscFVSetQuadrature(PetscFV fvm, PetscQuadrature q)
1438: {
1439: PetscFunctionBegin;
1441: PetscCall(PetscObjectReference((PetscObject)q));
1442: PetscCall(PetscQuadratureDestroy(&fvm->quadrature));
1443: fvm->quadrature = q;
1444: PetscFunctionReturn(PETSC_SUCCESS);
1445: }
1447: /*@
1448: PetscFVGetQuadrature - Get the `PetscQuadrature` from a `PetscFV`
1450: Not Collective
1452: Input Parameter:
1453: . fvm - the `PetscFV` object
1455: Output Parameter:
1456: . q - The `PetscQuadrature`
1458: Level: intermediate
1460: .seealso: `PetscQuadrature`, `PetscFV`, `PetscFVSetQuadrature()`
1461: @*/
1462: PetscErrorCode PetscFVGetQuadrature(PetscFV fvm, PetscQuadrature *q)
1463: {
1464: PetscFunctionBegin;
1466: PetscAssertPointer(q, 2);
1467: if (!fvm->quadrature) {
1468: /* Create default 1-point quadrature */
1469: PetscReal *points, *weights;
1471: PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, &fvm->quadrature));
1472: PetscCall(PetscCalloc1(fvm->dim, &points));
1473: PetscCall(PetscMalloc1(1, &weights));
1474: weights[0] = 1.0;
1475: PetscCall(PetscQuadratureSetData(fvm->quadrature, fvm->dim, 1, 1, points, weights));
1476: }
1477: *q = fvm->quadrature;
1478: PetscFunctionReturn(PETSC_SUCCESS);
1479: }
1481: /*@
1482: PetscFVCreateDualSpace - Creates a `PetscDualSpace` appropriate for the `PetscFV`
1484: Not Collective
1486: Input Parameters:
1487: + fvm - The `PetscFV` object
1488: - ct - The `DMPolytopeType` for the cell
1490: Level: intermediate
1492: .seealso: `PetscFVGetDualSpace()`, `PetscFVSetDualSpace()`, `PetscDualSpace`, `PetscFV`, `PetscFVCreate()`
1493: @*/
1494: PetscErrorCode PetscFVCreateDualSpace(PetscFV fvm, DMPolytopeType ct)
1495: {
1496: DM K;
1497: PetscInt dim, Nc;
1499: PetscFunctionBegin;
1500: PetscCall(PetscFVGetSpatialDimension(fvm, &dim));
1501: PetscCall(PetscFVGetNumComponents(fvm, &Nc));
1502: PetscCall(PetscDualSpaceCreate(PetscObjectComm((PetscObject)fvm), &fvm->dualSpace));
1503: PetscCall(PetscDualSpaceSetType(fvm->dualSpace, PETSCDUALSPACESIMPLE));
1504: PetscCall(DMPlexCreateReferenceCell(PETSC_COMM_SELF, ct, &K));
1505: PetscCall(PetscDualSpaceSetNumComponents(fvm->dualSpace, Nc));
1506: PetscCall(PetscDualSpaceSetDM(fvm->dualSpace, K));
1507: PetscCall(DMDestroy(&K));
1508: PetscCall(PetscDualSpaceSimpleSetDimension(fvm->dualSpace, Nc));
1509: // Should we be using PetscFVGetQuadrature() here?
1510: for (PetscInt c = 0; c < Nc; ++c) {
1511: PetscQuadrature qc;
1512: PetscReal *points, *weights;
1514: PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, &qc));
1515: PetscCall(PetscCalloc1(dim, &points));
1516: PetscCall(PetscCalloc1(Nc, &weights));
1517: weights[c] = 1.0;
1518: PetscCall(PetscQuadratureSetData(qc, dim, Nc, 1, points, weights));
1519: PetscCall(PetscDualSpaceSimpleSetFunctional(fvm->dualSpace, c, qc));
1520: PetscCall(PetscQuadratureDestroy(&qc));
1521: }
1522: PetscCall(PetscDualSpaceSetUp(fvm->dualSpace));
1523: PetscFunctionReturn(PETSC_SUCCESS);
1524: }
1526: /*@
1527: PetscFVGetDualSpace - Returns the `PetscDualSpace` used to define the inner product on a `PetscFV`
1529: Not Collective
1531: Input Parameter:
1532: . fvm - The `PetscFV` object
1534: Output Parameter:
1535: . sp - The `PetscDualSpace` object
1537: Level: intermediate
1539: Developer Notes:
1540: There is overlap between the methods of `PetscFE` and `PetscFV`, they should probably share a common parent class
1542: .seealso: `PetscFVSetDualSpace()`, `PetscFVCreateDualSpace()`, `PetscDualSpace`, `PetscFV`, `PetscFVCreate()`
1543: @*/
1544: PetscErrorCode PetscFVGetDualSpace(PetscFV fvm, PetscDualSpace *sp)
1545: {
1546: PetscFunctionBegin;
1548: PetscAssertPointer(sp, 2);
1549: if (!fvm->dualSpace) {
1550: PetscInt dim;
1552: PetscCall(PetscFVGetSpatialDimension(fvm, &dim));
1553: PetscCall(PetscFVCreateDualSpace(fvm, DMPolytopeTypeSimpleShape(dim, PETSC_FALSE)));
1554: }
1555: *sp = fvm->dualSpace;
1556: PetscFunctionReturn(PETSC_SUCCESS);
1557: }
1559: /*@
1560: PetscFVSetDualSpace - Sets the `PetscDualSpace` used to define the inner product
1562: Not Collective
1564: Input Parameters:
1565: + fvm - The `PetscFV` object
1566: - sp - The `PetscDualSpace` object
1568: Level: intermediate
1570: Note:
1571: A simple dual space is provided automatically, and the user typically will not need to override it.
1573: .seealso: `PetscFVGetDualSpace()`, `PetscFVCreateDualSpace()`, `PetscDualSpace`, `PetscFV`, `PetscFVCreate()`
1574: @*/
1575: PetscErrorCode PetscFVSetDualSpace(PetscFV fvm, PetscDualSpace sp)
1576: {
1577: PetscFunctionBegin;
1580: PetscCall(PetscDualSpaceDestroy(&fvm->dualSpace));
1581: fvm->dualSpace = sp;
1582: PetscCall(PetscObjectReference((PetscObject)fvm->dualSpace));
1583: PetscFunctionReturn(PETSC_SUCCESS);
1584: }
1586: /*@
1587: PetscFVGetCellTabulation - Returns the tabulation of the basis functions at the quadrature points
1589: Not Collective
1591: Input Parameter:
1592: . fvm - The `PetscFV` object
1594: Output Parameter:
1595: . T - The basis function values and derivatives at quadrature points
1597: Level: intermediate
1599: Note:
1600: .vb
1601: T->T[0] = B[(p*pdim + i)*Nc + c] is the value at point p for basis function i and component c
1602: T->T[1] = D[((p*pdim + i)*Nc + c)*dim + d] is the derivative value at point p for basis function i, component c, in direction d
1603: T->T[2] = H[(((p*pdim + i)*Nc + c)*dim + d)*dim + e] is the value at point p for basis function i, component c, in directions d and e
1604: .ve
1606: .seealso: `PetscFV`, `PetscTabulation`, `PetscFEGetCellTabulation()`, `PetscFVCreateTabulation()`, `PetscFVGetQuadrature()`, `PetscQuadratureGetData()`
1607: @*/
1608: PetscErrorCode PetscFVGetCellTabulation(PetscFV fvm, PetscTabulation *T)
1609: {
1610: PetscInt npoints;
1611: const PetscReal *points;
1613: PetscFunctionBegin;
1615: PetscAssertPointer(T, 2);
1616: PetscCall(PetscQuadratureGetData(fvm->quadrature, NULL, NULL, &npoints, &points, NULL));
1617: if (!fvm->T) PetscCall(PetscFVCreateTabulation(fvm, 1, npoints, points, 1, &fvm->T));
1618: *T = fvm->T;
1619: PetscFunctionReturn(PETSC_SUCCESS);
1620: }
1622: /*@
1623: PetscFVCreateTabulation - Tabulates the basis functions, and perhaps derivatives, at the points provided.
1625: Not Collective
1627: Input Parameters:
1628: + fvm - The `PetscFV` object
1629: . nrepl - The number of replicas
1630: . npoints - The number of tabulation points in a replica
1631: . points - The tabulation point coordinates
1632: - K - The order of derivative to tabulate
1634: Output Parameter:
1635: . T - The basis function values and derivative at tabulation points
1637: Level: intermediate
1639: Note:
1640: .vb
1641: T->T[0] = B[(p*pdim + i)*Nc + c] is the value at point p for basis function i and component c
1642: T->T[1] = D[((p*pdim + i)*Nc + c)*dim + d] is the derivative value at point p for basis function i, component c, in direction d
1643: T->T[2] = H[(((p*pdim + i)*Nc + c)*dim + d)*dim + e] is the value at point p for basis function i, component c, in directions d and e
1644: .ve
1646: .seealso: `PetscFV`, `PetscTabulation`, `PetscFECreateTabulation()`, `PetscTabulationDestroy()`, `PetscFEGetCellTabulation()`
1647: @*/
1648: PetscErrorCode PetscFVCreateTabulation(PetscFV fvm, PetscInt nrepl, PetscInt npoints, const PetscReal points[], PetscInt K, PetscTabulation *T)
1649: {
1650: PetscInt pdim; // Dimension of approximation space P
1651: PetscInt cdim; // Spatial dimension
1652: PetscInt Nc; // Field components
1653: PetscInt k, p, d, c, e;
1655: PetscFunctionBegin;
1656: if (!npoints || K < 0) {
1657: *T = NULL;
1658: PetscFunctionReturn(PETSC_SUCCESS);
1659: }
1661: PetscAssertPointer(points, 4);
1662: PetscAssertPointer(T, 6);
1663: PetscCall(PetscFVGetSpatialDimension(fvm, &cdim));
1664: PetscCall(PetscFVGetNumComponents(fvm, &Nc));
1665: pdim = Nc;
1666: PetscCall(PetscMalloc1(1, T));
1667: (*T)->K = !cdim ? 0 : K;
1668: (*T)->Nr = nrepl;
1669: (*T)->Np = npoints;
1670: (*T)->Nb = pdim;
1671: (*T)->Nc = Nc;
1672: (*T)->cdim = cdim;
1673: PetscCall(PetscMalloc1((*T)->K + 1, &(*T)->T));
1674: for (k = 0; k <= (*T)->K; ++k) PetscCall(PetscMalloc1(nrepl * npoints * pdim * Nc * PetscPowInt(cdim, k), &(*T)->T[k]));
1675: if (K >= 0) {
1676: for (p = 0; p < nrepl * npoints; ++p)
1677: for (d = 0; d < pdim; ++d)
1678: for (c = 0; c < Nc; ++c) (*T)->T[0][(p * pdim + d) * Nc + c] = 1.;
1679: }
1680: if (K >= 1) {
1681: for (p = 0; p < nrepl * npoints; ++p)
1682: for (d = 0; d < pdim; ++d)
1683: for (c = 0; c < Nc; ++c)
1684: for (e = 0; e < cdim; ++e) (*T)->T[1][((p * pdim + d) * Nc + c) * cdim + e] = 0.0;
1685: }
1686: if (K >= 2) {
1687: for (p = 0; p < nrepl * npoints; ++p)
1688: for (d = 0; d < pdim; ++d)
1689: for (c = 0; c < Nc; ++c)
1690: for (e = 0; e < cdim * cdim; ++e) (*T)->T[2][((p * pdim + d) * Nc + c) * cdim * cdim + e] = 0.0;
1691: }
1692: PetscFunctionReturn(PETSC_SUCCESS);
1693: }
1695: /*@
1696: PetscFVComputeGradient - Compute the gradient reconstruction matrix for a given cell
1698: Input Parameters:
1699: + fvm - The `PetscFV` object
1700: . numFaces - The number of cell faces which are not constrained
1701: - dx - The vector from the cell centroid to the neighboring cell centroid for each face
1703: Output Parameter:
1704: . grad - the gradient
1706: Level: advanced
1708: .seealso: `PetscFV`, `PetscFVCreate()`
1709: @*/
1710: PetscErrorCode PetscFVComputeGradient(PetscFV fvm, PetscInt numFaces, PetscScalar dx[], PetscScalar grad[])
1711: {
1712: PetscFunctionBegin;
1714: PetscTryTypeMethod(fvm, computegradient, numFaces, dx, grad);
1715: PetscFunctionReturn(PETSC_SUCCESS);
1716: }
1718: /*@
1719: PetscFVIntegrateRHSFunction - Produce the cell residual vector for a chunk of elements by quadrature integration
1721: Not Collective
1723: Input Parameters:
1724: + fvm - The `PetscFV` object for the field being integrated
1725: . prob - The `PetscDS` specifying the discretizations and continuum functions
1726: . field - The field being integrated
1727: . Nf - The number of faces in the chunk
1728: . fgeom - The face geometry for each face in the chunk
1729: . neighborVol - The volume for each pair of cells in the chunk
1730: . uL - The state from the cell on the left
1731: - uR - The state from the cell on the right
1733: Output Parameters:
1734: + fluxL - the left fluxes for each face
1735: - fluxR - the right fluxes for each face
1737: Level: developer
1739: .seealso: `PetscFV`, `PetscDS`, `PetscFVFaceGeom`, `PetscFVCreate()`
1740: @*/
1741: PetscErrorCode PetscFVIntegrateRHSFunction(PetscFV fvm, PetscDS prob, PetscInt field, PetscInt Nf, PetscFVFaceGeom *fgeom, PetscReal *neighborVol, PetscScalar uL[], PetscScalar uR[], PetscScalar fluxL[], PetscScalar fluxR[])
1742: {
1743: PetscFunctionBegin;
1745: PetscTryTypeMethod(fvm, integraterhsfunction, prob, field, Nf, fgeom, neighborVol, uL, uR, fluxL, fluxR);
1746: PetscFunctionReturn(PETSC_SUCCESS);
1747: }
1749: /*@
1750: PetscFVClone - Create a shallow copy of a `PetscFV` object that just references the internal objects.
1752: Input Parameter:
1753: . fv - The initial `PetscFV`
1755: Output Parameter:
1756: . fvNew - A clone of the `PetscFV`
1758: Level: advanced
1760: Notes:
1761: This is typically used to change the number of components.
1763: .seealso: `PetscFV`, `PetscFVType`, `PetscFVCreate()`, `PetscFVSetType()`
1764: @*/
1765: PetscErrorCode PetscFVClone(PetscFV fv, PetscFV *fvNew)
1766: {
1767: PetscDualSpace Q;
1768: DM K;
1769: PetscQuadrature q;
1770: PetscInt Nc, cdim;
1772: PetscFunctionBegin;
1773: PetscCall(PetscFVGetDualSpace(fv, &Q));
1774: PetscCall(PetscFVGetQuadrature(fv, &q));
1775: PetscCall(PetscDualSpaceGetDM(Q, &K));
1777: PetscCall(PetscFVCreate(PetscObjectComm((PetscObject)fv), fvNew));
1778: PetscCall(PetscFVSetDualSpace(*fvNew, Q));
1779: PetscCall(PetscFVGetNumComponents(fv, &Nc));
1780: PetscCall(PetscFVSetNumComponents(*fvNew, Nc));
1781: PetscCall(PetscFVGetSpatialDimension(fv, &cdim));
1782: PetscCall(PetscFVSetSpatialDimension(*fvNew, cdim));
1783: PetscCall(PetscFVSetQuadrature(*fvNew, q));
1784: PetscFunctionReturn(PETSC_SUCCESS);
1785: }
1787: /*@
1788: PetscFVRefine - Create a "refined" `PetscFV` object that refines the reference cell into
1789: smaller copies.
1791: Input Parameter:
1792: . fv - The initial `PetscFV`
1794: Output Parameter:
1795: . fvRef - The refined `PetscFV`
1797: Level: advanced
1799: Notes:
1800: This is typically used to generate a preconditioner for a high order method from a lower order method on a
1801: refined mesh having the same number of dofs (but more sparsity). It is also used to create an
1802: interpolation between regularly refined meshes.
1804: .seealso: `PetscFV`, `PetscFVType`, `PetscFVCreate()`, `PetscFVSetType()`
1805: @*/
1806: PetscErrorCode PetscFVRefine(PetscFV fv, PetscFV *fvRef)
1807: {
1808: PetscDualSpace Q, Qref;
1809: DM K, Kref;
1810: PetscQuadrature q, qref;
1811: DMPolytopeType ct;
1812: DMPlexTransform tr;
1813: PetscReal *v0;
1814: PetscReal *jac, *invjac;
1815: PetscInt numComp, numSubelements, s;
1817: PetscFunctionBegin;
1818: PetscCall(PetscFVGetDualSpace(fv, &Q));
1819: PetscCall(PetscFVGetQuadrature(fv, &q));
1820: PetscCall(PetscDualSpaceGetDM(Q, &K));
1821: /* Create dual space */
1822: PetscCall(PetscDualSpaceDuplicate(Q, &Qref));
1823: PetscCall(DMRefine(K, PetscObjectComm((PetscObject)fv), &Kref));
1824: PetscCall(PetscDualSpaceSetDM(Qref, Kref));
1825: PetscCall(DMDestroy(&Kref));
1826: PetscCall(PetscDualSpaceSetUp(Qref));
1827: /* Create volume */
1828: PetscCall(PetscFVCreate(PetscObjectComm((PetscObject)fv), fvRef));
1829: PetscCall(PetscFVSetDualSpace(*fvRef, Qref));
1830: PetscCall(PetscFVGetNumComponents(fv, &numComp));
1831: PetscCall(PetscFVSetNumComponents(*fvRef, numComp));
1832: PetscCall(PetscFVSetUp(*fvRef));
1833: /* Create quadrature */
1834: PetscCall(DMPlexGetCellType(K, 0, &ct));
1835: PetscCall(DMPlexTransformCreate(PETSC_COMM_SELF, &tr));
1836: PetscCall(DMPlexTransformSetType(tr, DMPLEXREFINEREGULAR));
1837: PetscCall(DMPlexRefineRegularGetAffineTransforms(tr, ct, &numSubelements, &v0, &jac, &invjac));
1838: PetscCall(PetscQuadratureExpandComposite(q, numSubelements, v0, jac, &qref));
1839: PetscCall(PetscDualSpaceSimpleSetDimension(Qref, numSubelements));
1840: for (s = 0; s < numSubelements; ++s) {
1841: PetscQuadrature qs;
1842: const PetscReal *points, *weights;
1843: PetscReal *p, *w;
1844: PetscInt dim, Nc, npoints, np;
1846: PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, &qs));
1847: PetscCall(PetscQuadratureGetData(q, &dim, &Nc, &npoints, &points, &weights));
1848: np = npoints / numSubelements;
1849: PetscCall(PetscMalloc1(np * dim, &p));
1850: PetscCall(PetscMalloc1(np * Nc, &w));
1851: PetscCall(PetscArraycpy(p, &points[s * np * dim], np * dim));
1852: PetscCall(PetscArraycpy(w, &weights[s * np * Nc], np * Nc));
1853: PetscCall(PetscQuadratureSetData(qs, dim, Nc, np, p, w));
1854: PetscCall(PetscDualSpaceSimpleSetFunctional(Qref, s, qs));
1855: PetscCall(PetscQuadratureDestroy(&qs));
1856: }
1857: PetscCall(PetscFVSetQuadrature(*fvRef, qref));
1858: PetscCall(DMPlexTransformDestroy(&tr));
1859: PetscCall(PetscQuadratureDestroy(&qref));
1860: PetscCall(PetscDualSpaceDestroy(&Qref));
1861: PetscFunctionReturn(PETSC_SUCCESS);
1862: }
1864: static PetscErrorCode PetscFVDestroy_Upwind(PetscFV fvm)
1865: {
1866: PetscFV_Upwind *b = (PetscFV_Upwind *)fvm->data;
1868: PetscFunctionBegin;
1869: PetscCall(PetscFree(b));
1870: PetscFunctionReturn(PETSC_SUCCESS);
1871: }
1873: static PetscErrorCode PetscFVView_Upwind_Ascii(PetscFV fv, PetscViewer viewer)
1874: {
1875: PetscInt Nc;
1876: PetscViewerFormat format;
1878: PetscFunctionBegin;
1879: PetscCall(PetscFVGetNumComponents(fv, &Nc));
1880: PetscCall(PetscViewerGetFormat(viewer, &format));
1881: PetscCall(PetscViewerASCIIPrintf(viewer, "Upwind Finite Volume:\n"));
1882: PetscCall(PetscViewerASCIIPrintf(viewer, " num components: %" PetscInt_FMT "\n", Nc));
1883: for (PetscInt c = 0; c < Nc; c++) {
1884: if (fv->componentNames[c]) PetscCall(PetscViewerASCIIPrintf(viewer, " component %" PetscInt_FMT ": %s\n", c, fv->componentNames[c]));
1885: }
1886: PetscFunctionReturn(PETSC_SUCCESS);
1887: }
1889: static PetscErrorCode PetscFVView_Upwind(PetscFV fv, PetscViewer viewer)
1890: {
1891: PetscBool isascii;
1893: PetscFunctionBegin;
1896: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1897: if (isascii) PetscCall(PetscFVView_Upwind_Ascii(fv, viewer));
1898: PetscFunctionReturn(PETSC_SUCCESS);
1899: }
1901: static PetscErrorCode PetscFVComputeGradient_Upwind(PetscFV fv, PetscInt numFaces, const PetscScalar dx[], PetscScalar grad[])
1902: {
1903: PetscInt dim;
1905: PetscFunctionBegin;
1906: PetscCall(PetscFVGetSpatialDimension(fv, &dim));
1907: for (PetscInt f = 0; f < numFaces; ++f) {
1908: for (PetscInt d = 0; d < dim; ++d) grad[f * dim + d] = 0.;
1909: }
1910: PetscFunctionReturn(PETSC_SUCCESS);
1911: }
1913: /*
1914: neighborVol[f*2+0] contains the left geom
1915: neighborVol[f*2+1] contains the right geom
1916: */
1917: static PetscErrorCode PetscFVIntegrateRHSFunction_Upwind(PetscFV fvm, PetscDS prob, PetscInt field, PetscInt Nf, PetscFVFaceGeom *fgeom, PetscReal *neighborVol, PetscScalar uL[], PetscScalar uR[], PetscScalar fluxL[], PetscScalar fluxR[])
1918: {
1919: void (*riemann)(PetscInt, PetscInt, const PetscReal[], const PetscReal[], const PetscScalar[], const PetscScalar[], PetscInt, const PetscScalar[], PetscScalar[], void *);
1920: void *rctx;
1921: PetscScalar *flux = fvm->fluxWork;
1922: const PetscScalar *constants;
1923: PetscInt dim, numConstants, pdim, totDim, Nc, off, f, d;
1925: PetscFunctionBegin;
1926: PetscCall(PetscDSGetTotalComponents(prob, &Nc));
1927: PetscCall(PetscDSGetTotalDimension(prob, &totDim));
1928: PetscCall(PetscDSGetFieldOffset(prob, field, &off));
1929: PetscCall(PetscDSGetRiemannSolver(prob, field, &riemann));
1930: PetscCall(PetscDSGetContext(prob, field, &rctx));
1931: PetscCall(PetscDSGetConstants(prob, &numConstants, &constants));
1932: PetscCall(PetscFVGetSpatialDimension(fvm, &dim));
1933: PetscCall(PetscFVGetNumComponents(fvm, &pdim));
1934: for (f = 0; f < Nf; ++f) {
1935: (*riemann)(dim, pdim, fgeom[f].centroid, fgeom[f].normal, &uL[f * Nc], &uR[f * Nc], numConstants, constants, flux, rctx);
1936: for (d = 0; d < pdim; ++d) {
1937: fluxL[f * totDim + off + d] = flux[d] / neighborVol[f * 2 + 0];
1938: fluxR[f * totDim + off + d] = flux[d] / neighborVol[f * 2 + 1];
1939: }
1940: }
1941: PetscFunctionReturn(PETSC_SUCCESS);
1942: }
1944: static PetscErrorCode PetscFVInitialize_Upwind(PetscFV fvm)
1945: {
1946: PetscFunctionBegin;
1947: fvm->ops->setfromoptions = NULL;
1948: fvm->ops->view = PetscFVView_Upwind;
1949: fvm->ops->destroy = PetscFVDestroy_Upwind;
1950: fvm->ops->computegradient = PetscFVComputeGradient_Upwind;
1951: fvm->ops->integraterhsfunction = PetscFVIntegrateRHSFunction_Upwind;
1952: PetscFunctionReturn(PETSC_SUCCESS);
1953: }
1955: /*MC
1956: PETSCFVUPWIND = "upwind" - A `PetscFV` implementation
1958: Level: intermediate
1960: .seealso: `PetscFV`, `PetscFVType`, `PetscFVCreate()`, `PetscFVSetType()`
1961: M*/
1963: PETSC_EXTERN PetscErrorCode PetscFVCreate_Upwind(PetscFV fvm)
1964: {
1965: PetscFV_Upwind *b;
1967: PetscFunctionBegin;
1969: PetscCall(PetscNew(&b));
1970: fvm->data = b;
1972: PetscCall(PetscFVInitialize_Upwind(fvm));
1973: PetscFunctionReturn(PETSC_SUCCESS);
1974: }
1976: #include <petscblaslapack.h>
1978: static PetscErrorCode PetscFVDestroy_LeastSquares(PetscFV fvm)
1979: {
1980: PetscFV_LeastSquares *ls = (PetscFV_LeastSquares *)fvm->data;
1982: PetscFunctionBegin;
1983: PetscCall(PetscObjectComposeFunction((PetscObject)fvm, "PetscFVLeastSquaresSetMaxFaces_C", NULL));
1984: PetscCall(PetscFree4(ls->B, ls->Binv, ls->tau, ls->work));
1985: PetscCall(PetscFree(ls));
1986: PetscFunctionReturn(PETSC_SUCCESS);
1987: }
1989: static PetscErrorCode PetscFVView_LeastSquares_Ascii(PetscFV fv, PetscViewer viewer)
1990: {
1991: PetscInt Nc;
1992: PetscViewerFormat format;
1994: PetscFunctionBegin;
1995: PetscCall(PetscFVGetNumComponents(fv, &Nc));
1996: PetscCall(PetscViewerGetFormat(viewer, &format));
1997: PetscCall(PetscViewerASCIIPrintf(viewer, "Finite Volume with Least Squares Reconstruction:\n"));
1998: PetscCall(PetscViewerASCIIPrintf(viewer, " num components: %" PetscInt_FMT "\n", Nc));
1999: for (PetscInt c = 0; c < Nc; c++) {
2000: if (fv->componentNames[c]) PetscCall(PetscViewerASCIIPrintf(viewer, " component %" PetscInt_FMT ": %s\n", c, fv->componentNames[c]));
2001: }
2002: PetscFunctionReturn(PETSC_SUCCESS);
2003: }
2005: static PetscErrorCode PetscFVView_LeastSquares(PetscFV fv, PetscViewer viewer)
2006: {
2007: PetscBool isascii;
2009: PetscFunctionBegin;
2012: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
2013: if (isascii) PetscCall(PetscFVView_LeastSquares_Ascii(fv, viewer));
2014: PetscFunctionReturn(PETSC_SUCCESS);
2015: }
2017: /* Overwrites A. Can only handle full-rank problems with m>=n */
2018: static PetscErrorCode PetscFVLeastSquaresPseudoInverse_Static(PetscInt m, PetscInt mstride, PetscInt n, PetscScalar *A, PetscScalar *Ainv, PetscScalar *tau, PetscInt worksize, PetscScalar *work)
2019: {
2020: PetscBool debug = PETSC_FALSE;
2021: PetscBLASInt M, N, K, lda, ldb, ldwork;
2022: PetscScalar *R, *Q, *Aback, Alpha;
2024: PetscFunctionBegin;
2025: if (debug) {
2026: PetscCall(PetscMalloc1(m * n, &Aback));
2027: PetscCall(PetscArraycpy(Aback, A, m * n));
2028: }
2030: PetscCall(PetscBLASIntCast(m, &M));
2031: PetscCall(PetscBLASIntCast(n, &N));
2032: PetscCall(PetscBLASIntCast(mstride, &lda));
2033: PetscCall(PetscBLASIntCast(worksize, &ldwork));
2034: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
2035: PetscCallLAPACKInfo("LAPACKgeqrf", LAPACKgeqrf_(&M, &N, A, &lda, tau, work, &ldwork, &info));
2036: PetscCall(PetscFPTrapPop());
2037: R = A; /* Upper triangular part of A now contains R, the rest contains the elementary reflectors */
2039: /* Extract an explicit representation of Q */
2040: Q = Ainv;
2041: PetscCall(PetscArraycpy(Q, A, mstride * n));
2042: K = N; /* full rank */
2043: PetscCallLAPACKInfo("LAPACKorgqr", LAPACKorgqr_(&M, &N, &K, Q, &lda, tau, work, &ldwork, &info));
2045: /* Compute A^{-T} = (R^{-1} Q^T)^T = Q R^{-T} */
2046: Alpha = 1.0;
2047: ldb = lda;
2048: BLAStrsm_("Right", "Upper", "ConjugateTranspose", "NotUnitTriangular", &M, &N, &Alpha, R, &lda, Q, &ldb);
2049: /* Ainv is Q, overwritten with inverse */
2051: if (debug) { /* Check that pseudo-inverse worked */
2052: PetscScalar Beta = 0.0;
2053: PetscBLASInt ldc;
2054: K = N;
2055: ldc = N;
2056: BLASgemm_("ConjugateTranspose", "Normal", &N, &K, &M, &Alpha, Ainv, &lda, Aback, &ldb, &Beta, work, &ldc);
2057: PetscCall(PetscScalarView(n * n, work, PETSC_VIEWER_STDOUT_SELF));
2058: PetscCall(PetscFree(Aback));
2059: }
2060: PetscFunctionReturn(PETSC_SUCCESS);
2061: }
2063: /* Overwrites A. Can handle degenerate problems and m<n. */
2064: static PetscErrorCode PetscFVLeastSquaresPseudoInverseSVD_Static(PetscInt m, PetscInt mstride, PetscInt n, PetscScalar *A, PetscScalar *Ainv, PetscScalar *tau, PetscInt worksize, PetscScalar *work)
2065: {
2066: PetscScalar *Brhs;
2067: PetscScalar *tmpwork;
2068: PetscReal rcond;
2069: #if PetscDefined(USE_COMPLEX)
2070: PetscInt rworkSize;
2071: PetscReal *rwork, *rtau;
2072: #endif
2073: PetscInt i, j, maxmn;
2074: PetscBLASInt M, N, lda, ldb, ldwork;
2075: PetscBLASInt nrhs, irank;
2077: PetscFunctionBegin;
2078: /* initialize to identity */
2079: tmpwork = work;
2080: Brhs = Ainv;
2081: maxmn = PetscMax(m, n);
2082: for (j = 0; j < maxmn; j++) {
2083: for (i = 0; i < maxmn; i++) Brhs[i + j * maxmn] = 1.0 * (i == j);
2084: }
2086: PetscCall(PetscBLASIntCast(m, &M));
2087: PetscCall(PetscBLASIntCast(n, &N));
2088: PetscCall(PetscBLASIntCast(mstride, &lda));
2089: PetscCall(PetscBLASIntCast(maxmn, &ldb));
2090: PetscCall(PetscBLASIntCast(worksize, &ldwork));
2091: rcond = -1;
2092: nrhs = M;
2093: #if PetscDefined(USE_COMPLEX)
2094: rworkSize = 5 * PetscMin(M, N);
2095: PetscCall(PetscMalloc1(rworkSize, &rwork));
2096: PetscCall(PetscMalloc1(PetscMin(M, N), &rtau));
2097: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
2098: PetscCallLAPACKInfo("LAPACKgelss", LAPACKgelss_(&M, &N, &nrhs, A, &lda, Brhs, &ldb, rtau, &rcond, &irank, tmpwork, &ldwork, rwork, &info));
2099: PetscCall(PetscFPTrapPop());
2100: PetscCall(PetscFree(rwork));
2101: for (i = 0; i < PetscMin(M, N); i++) tau[i] = rtau[i];
2102: PetscCall(PetscFree(rtau));
2103: #else
2104: nrhs = M;
2105: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
2106: PetscCallLAPACKInfo("LAPACKgelss", LAPACKgelss_(&M, &N, &nrhs, A, &lda, Brhs, &ldb, tau, &rcond, &irank, tmpwork, &ldwork, &info));
2107: PetscCall(PetscFPTrapPop());
2108: #endif
2109: /* The following check should be turned into a diagnostic as soon as someone wants to do this intentionally */
2110: PetscCheck(irank >= PetscMin(M, N), PETSC_COMM_SELF, PETSC_ERR_USER, "Rank deficient least squares fit, indicates an isolated cell with two collinear points");
2111: PetscFunctionReturn(PETSC_SUCCESS);
2112: }
2114: #if 0
2115: static PetscErrorCode PetscFVLeastSquaresDebugCell_Static(PetscFV fvm, PetscInt cell, DM dm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2116: {
2117: PetscReal grad[2] = {0, 0};
2118: const PetscInt *faces;
2119: PetscInt numFaces, f;
2121: PetscFunctionBegin;
2122: PetscCall(DMPlexGetConeSize(dm, cell, &numFaces));
2123: PetscCall(DMPlexGetCone(dm, cell, &faces));
2124: for (f = 0; f < numFaces; ++f) {
2125: const PetscInt *fcells;
2126: const CellGeom *cg1;
2127: const FaceGeom *fg;
2129: PetscCall(DMPlexGetSupport(dm, faces[f], &fcells));
2130: PetscCall(DMPlexPointLocalRead(dmFace, faces[f], fgeom, &fg));
2131: for (i = 0; i < 2; ++i) {
2132: PetscScalar du;
2134: if (fcells[i] == c) continue;
2135: PetscCall(DMPlexPointLocalRead(dmCell, fcells[i], cgeom, &cg1));
2136: du = cg1->centroid[0] + 3*cg1->centroid[1] - (cg->centroid[0] + 3*cg->centroid[1]);
2137: grad[0] += fg->grad[!i][0] * du;
2138: grad[1] += fg->grad[!i][1] * du;
2139: }
2140: }
2141: PetscCall(PetscPrintf(PETSC_COMM_SELF, "cell[%d] grad (%g, %g)\n", cell, grad[0], grad[1]));
2142: PetscFunctionReturn(PETSC_SUCCESS);
2143: }
2144: #endif
2146: /*
2147: PetscFVComputeGradient_LeastSquares - Compute the gradient reconstruction matrix for a given cell
2149: Input Parameters:
2150: + fvm - The `PetscFV` object
2151: . numFaces - The number of cell faces which are not constrained
2152: . dx - The vector from the cell centroid to the neighboring cell centroid for each face
2154: Level: developer
2156: .seealso: `PetscFV`, `PetscFVCreate()`
2157: */
2158: static PetscErrorCode PetscFVComputeGradient_LeastSquares(PetscFV fvm, PetscInt numFaces, const PetscScalar dx[], PetscScalar grad[])
2159: {
2160: PetscFV_LeastSquares *ls = (PetscFV_LeastSquares *)fvm->data;
2161: const PetscBool useSVD = PETSC_TRUE;
2162: const PetscInt maxFaces = ls->maxFaces;
2163: PetscInt dim, f, d;
2165: PetscFunctionBegin;
2166: if (numFaces > maxFaces) {
2167: PetscCheck(maxFaces >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Reconstruction has not been initialized, call PetscFVLeastSquaresSetMaxFaces()");
2168: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of input faces %" PetscInt_FMT " > %" PetscInt_FMT " maxfaces", numFaces, maxFaces);
2169: }
2170: PetscCall(PetscFVGetSpatialDimension(fvm, &dim));
2171: for (f = 0; f < numFaces; ++f) {
2172: for (d = 0; d < dim; ++d) ls->B[d * maxFaces + f] = dx[f * dim + d];
2173: }
2174: /* Overwrites B with garbage, returns Binv in row-major format */
2175: if (useSVD) {
2176: PetscInt maxmn = PetscMax(numFaces, dim);
2177: PetscCall(PetscFVLeastSquaresPseudoInverseSVD_Static(numFaces, maxFaces, dim, ls->B, ls->Binv, ls->tau, ls->workSize, ls->work));
2178: /* Binv shaped in column-major, coldim=maxmn.*/
2179: for (f = 0; f < numFaces; ++f) {
2180: for (d = 0; d < dim; ++d) grad[f * dim + d] = ls->Binv[d + maxmn * f];
2181: }
2182: } else {
2183: PetscCall(PetscFVLeastSquaresPseudoInverse_Static(numFaces, maxFaces, dim, ls->B, ls->Binv, ls->tau, ls->workSize, ls->work));
2184: /* Binv shaped in row-major, rowdim=maxFaces.*/
2185: for (f = 0; f < numFaces; ++f) {
2186: for (d = 0; d < dim; ++d) grad[f * dim + d] = ls->Binv[d * maxFaces + f];
2187: }
2188: }
2189: PetscFunctionReturn(PETSC_SUCCESS);
2190: }
2192: /*
2193: neighborVol[f*2+0] contains the left geom
2194: neighborVol[f*2+1] contains the right geom
2195: */
2196: static PetscErrorCode PetscFVIntegrateRHSFunction_LeastSquares(PetscFV fvm, PetscDS prob, PetscInt field, PetscInt Nf, PetscFVFaceGeom *fgeom, PetscReal *neighborVol, PetscScalar uL[], PetscScalar uR[], PetscScalar fluxL[], PetscScalar fluxR[])
2197: {
2198: void (*riemann)(PetscInt, PetscInt, const PetscReal[], const PetscReal[], const PetscScalar[], const PetscScalar[], PetscInt, const PetscScalar[], PetscScalar[], void *);
2199: void *rctx;
2200: PetscScalar *flux = fvm->fluxWork;
2201: const PetscScalar *constants;
2202: PetscInt dim, numConstants, pdim, Nc, totDim, off, f, d;
2204: PetscFunctionBegin;
2205: PetscCall(PetscDSGetTotalComponents(prob, &Nc));
2206: PetscCall(PetscDSGetTotalDimension(prob, &totDim));
2207: PetscCall(PetscDSGetFieldOffset(prob, field, &off));
2208: PetscCall(PetscDSGetRiemannSolver(prob, field, &riemann));
2209: PetscCall(PetscDSGetContext(prob, field, &rctx));
2210: PetscCall(PetscDSGetConstants(prob, &numConstants, &constants));
2211: PetscCall(PetscFVGetSpatialDimension(fvm, &dim));
2212: PetscCall(PetscFVGetNumComponents(fvm, &pdim));
2213: for (f = 0; f < Nf; ++f) {
2214: (*riemann)(dim, pdim, fgeom[f].centroid, fgeom[f].normal, &uL[f * Nc], &uR[f * Nc], numConstants, constants, flux, rctx);
2215: for (d = 0; d < pdim; ++d) {
2216: fluxL[f * totDim + off + d] = flux[d] / neighborVol[f * 2 + 0];
2217: fluxR[f * totDim + off + d] = flux[d] / neighborVol[f * 2 + 1];
2218: }
2219: }
2220: PetscFunctionReturn(PETSC_SUCCESS);
2221: }
2223: static PetscErrorCode PetscFVLeastSquaresSetMaxFaces_LS(PetscFV fvm, PetscInt maxFaces)
2224: {
2225: PetscFV_LeastSquares *ls = (PetscFV_LeastSquares *)fvm->data;
2226: PetscInt dim, m, n, nrhs, minmn, maxmn;
2228: PetscFunctionBegin;
2230: PetscCall(PetscFVGetSpatialDimension(fvm, &dim));
2231: PetscCall(PetscFree4(ls->B, ls->Binv, ls->tau, ls->work));
2232: ls->maxFaces = maxFaces;
2233: m = ls->maxFaces;
2234: n = dim;
2235: nrhs = ls->maxFaces;
2236: minmn = PetscMin(m, n);
2237: maxmn = PetscMax(m, n);
2238: ls->workSize = 3 * minmn + PetscMax(2 * minmn, PetscMax(maxmn, nrhs)); /* required by LAPACK */
2239: PetscCall(PetscMalloc4(m * n, &ls->B, maxmn * maxmn, &ls->Binv, minmn, &ls->tau, ls->workSize, &ls->work));
2240: PetscFunctionReturn(PETSC_SUCCESS);
2241: }
2243: static PetscErrorCode PetscFVInitialize_LeastSquares(PetscFV fvm)
2244: {
2245: PetscFunctionBegin;
2246: fvm->ops->setfromoptions = NULL;
2247: fvm->ops->view = PetscFVView_LeastSquares;
2248: fvm->ops->destroy = PetscFVDestroy_LeastSquares;
2249: fvm->ops->computegradient = PetscFVComputeGradient_LeastSquares;
2250: fvm->ops->integraterhsfunction = PetscFVIntegrateRHSFunction_LeastSquares;
2251: PetscFunctionReturn(PETSC_SUCCESS);
2252: }
2254: /*MC
2255: PETSCFVLEASTSQUARES = "leastsquares" - A `PetscFV` implementation
2257: Level: intermediate
2259: .seealso: `PetscFV`, `PetscFVType`, `PetscFVCreate()`, `PetscFVSetType()`
2260: M*/
2262: PETSC_EXTERN PetscErrorCode PetscFVCreate_LeastSquares(PetscFV fvm)
2263: {
2264: PetscFV_LeastSquares *ls;
2266: PetscFunctionBegin;
2268: PetscCall(PetscNew(&ls));
2269: fvm->data = ls;
2271: ls->maxFaces = -1;
2272: ls->workSize = -1;
2273: ls->B = NULL;
2274: ls->Binv = NULL;
2275: ls->tau = NULL;
2276: ls->work = NULL;
2278: PetscCall(PetscFVSetComputeGradients(fvm, PETSC_TRUE));
2279: PetscCall(PetscFVInitialize_LeastSquares(fvm));
2280: PetscCall(PetscObjectComposeFunction((PetscObject)fvm, "PetscFVLeastSquaresSetMaxFaces_C", PetscFVLeastSquaresSetMaxFaces_LS));
2281: PetscFunctionReturn(PETSC_SUCCESS);
2282: }
2284: /*@
2285: PetscFVLeastSquaresSetMaxFaces - Set the maximum number of cell faces for gradient reconstruction
2287: Not Collective
2289: Input Parameters:
2290: + fvm - The `PetscFV` object
2291: - maxFaces - The maximum number of cell faces
2293: Level: intermediate
2295: .seealso: `PetscFV`, `PetscFVCreate()`, `PETSCFVLEASTSQUARES`, `PetscFVComputeGradient()`
2296: @*/
2297: PetscErrorCode PetscFVLeastSquaresSetMaxFaces(PetscFV fvm, PetscInt maxFaces)
2298: {
2299: PetscFunctionBegin;
2301: PetscTryMethod(fvm, "PetscFVLeastSquaresSetMaxFaces_C", (PetscFV, PetscInt), (fvm, maxFaces));
2302: PetscFunctionReturn(PETSC_SUCCESS);
2303: }