xref: /libCEED/examples/fluids/src/misc.c (revision d429f2b73816aac9bc6a2102bb3b580b9b804026)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 /// @file
9 /// Miscellaneous utility functions
10 
11 #include <ceed.h>
12 #include <petscdm.h>
13 #include <petscsf.h>
14 #include <petscts.h>
15 
16 #include "../navierstokes.h"
17 #include "../qfunctions/mass.h"
18 
19 PetscErrorCode ICs_FixMultiplicity(DM dm, CeedData ceed_data, User user, Vec Q_loc, Vec Q, CeedScalar time) {
20   PetscFunctionBeginUser;
21 
22   // ---------------------------------------------------------------------------
23   // Update time for evaluation
24   // ---------------------------------------------------------------------------
25   if (user->phys->ics_time_label) CeedOperatorSetContextDouble(ceed_data->op_ics_ctx->op, user->phys->ics_time_label, &time);
26 
27   // ---------------------------------------------------------------------------
28   // ICs
29   // ---------------------------------------------------------------------------
30   // -- CEED Restriction
31   CeedVector q0_ceed;
32   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &q0_ceed, NULL);
33 
34   // -- Place PETSc vector in CEED vector
35   PetscCall(ApplyCeedOperatorLocalToGlobal(NULL, Q, ceed_data->op_ics_ctx));
36 
37   // ---------------------------------------------------------------------------
38   // Fix multiplicity for output of ICs
39   // ---------------------------------------------------------------------------
40   // -- CEED Restriction
41   CeedVector mult_vec;
42   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &mult_vec, NULL);
43 
44   // -- Place PETSc vector in CEED vector
45   PetscMemType m_mem_type;
46   Vec          multiplicity_loc;
47   PetscCall(DMGetLocalVector(dm, &multiplicity_loc));
48   PetscCall(VecP2C(multiplicity_loc, &m_mem_type, mult_vec));
49 
50   // -- Get multiplicity
51   CeedElemRestrictionGetMultiplicity(ceed_data->elem_restr_q, mult_vec);
52 
53   // -- Restore vectors
54   PetscCall(VecC2P(mult_vec, m_mem_type, multiplicity_loc));
55 
56   // -- Local-to-Global
57   Vec multiplicity;
58   PetscCall(DMGetGlobalVector(dm, &multiplicity));
59   PetscCall(VecZeroEntries(multiplicity));
60   PetscCall(DMLocalToGlobal(dm, multiplicity_loc, ADD_VALUES, multiplicity));
61 
62   // -- Fix multiplicity
63   PetscCall(VecPointwiseDivide(Q, Q, multiplicity));
64   PetscCall(VecPointwiseDivide(Q_loc, Q_loc, multiplicity_loc));
65 
66   // -- Restore vectors
67   PetscCall(DMRestoreLocalVector(dm, &multiplicity_loc));
68   PetscCall(DMRestoreGlobalVector(dm, &multiplicity));
69 
70   // Cleanup
71   CeedVectorDestroy(&mult_vec);
72   CeedVectorDestroy(&q0_ceed);
73 
74   PetscFunctionReturn(PETSC_SUCCESS);
75 }
76 
77 PetscErrorCode DMPlexInsertBoundaryValues_NS(DM dm, PetscBool insert_essential, Vec Q_loc, PetscReal time, Vec face_geom_FVM, Vec cell_geom_FVM,
78                                              Vec grad_FVM) {
79   Vec Qbc, boundary_mask;
80   PetscFunctionBegin;
81 
82   // Mask (zero) Strong BC entries
83   PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask));
84   PetscCall(VecPointwiseMult(Q_loc, Q_loc, boundary_mask));
85   PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask));
86 
87   PetscCall(DMGetNamedLocalVector(dm, "Qbc", &Qbc));
88   PetscCall(VecAXPY(Q_loc, 1., Qbc));
89   PetscCall(DMRestoreNamedLocalVector(dm, "Qbc", &Qbc));
90 
91   PetscFunctionReturn(PETSC_SUCCESS);
92 }
93 
94 // @brief Load vector from binary file, possibly with embedded solution time and step number
95 PetscErrorCode LoadFluidsBinaryVec(MPI_Comm comm, PetscViewer viewer, Vec Q, PetscReal *time, PetscInt *step_number) {
96   PetscInt   file_step_number;
97   PetscInt32 token;
98   PetscReal  file_time;
99   PetscFunctionBeginUser;
100 
101   // Attempt
102   PetscCall(PetscViewerBinaryRead(viewer, &token, 1, NULL, PETSC_INT32));
103   if (token == FLUIDS_FILE_TOKEN_32 || token == FLUIDS_FILE_TOKEN_64 ||
104       token == FLUIDS_FILE_TOKEN) {  // New style format; we're reading a file with step number and time in the header
105     PetscCall(PetscViewerBinaryRead(viewer, &file_step_number, 1, NULL, PETSC_INT));
106     PetscCall(PetscViewerBinaryRead(viewer, &file_time, 1, NULL, PETSC_REAL));
107     if (time) *time = file_time;
108     if (step_number) *step_number = file_step_number;
109   } else if (token == VEC_FILE_CLASSID) {  // Legacy format of just the vector, encoded as [VEC_FILE_CLASSID, length, ]
110     PetscInt length, N;
111     PetscCall(PetscViewerBinaryRead(viewer, &length, 1, NULL, PETSC_INT));
112     PetscCall(VecGetSize(Q, &N));
113     PetscCheck(length == N, comm, PETSC_ERR_ARG_INCOMP, "File Vec has length %" PetscInt_FMT " but DM has global Vec size %" PetscInt_FMT, length, N);
114     PetscCall(PetscViewerBinarySetSkipHeader(viewer, PETSC_TRUE));
115   } else SETERRQ(comm, PETSC_ERR_FILE_UNEXPECTED, "Not a fluids header token or a PETSc Vec in file");
116 
117   // Load Q from existent solution
118   PetscCall(VecLoad(Q, viewer));
119 
120   PetscFunctionReturn(PETSC_SUCCESS);
121 }
122 
123 // Compare reference solution values with current test run for CI
124 PetscErrorCode RegressionTests_NS(AppCtx app_ctx, Vec Q) {
125   Vec         Qref;
126   PetscViewer viewer;
127   PetscReal   error, Qrefnorm;
128   MPI_Comm    comm = PetscObjectComm((PetscObject)Q);
129   PetscFunctionBegin;
130 
131   // Read reference file
132   PetscCall(VecDuplicate(Q, &Qref));
133   PetscCall(PetscViewerBinaryOpen(comm, app_ctx->test_file_path, FILE_MODE_READ, &viewer));
134   PetscCall(LoadFluidsBinaryVec(comm, viewer, Qref, NULL, NULL));
135 
136   // Compute error with respect to reference solution
137   PetscCall(VecAXPY(Q, -1.0, Qref));
138   PetscCall(VecNorm(Qref, NORM_MAX, &Qrefnorm));
139   PetscCall(VecScale(Q, 1. / Qrefnorm));
140   PetscCall(VecNorm(Q, NORM_MAX, &error));
141 
142   // Check error
143   if (error > app_ctx->test_tol) {
144     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test failed with error norm %g\n", (double)error));
145   }
146 
147   // Cleanup
148   PetscCall(PetscViewerDestroy(&viewer));
149   PetscCall(VecDestroy(&Qref));
150 
151   PetscFunctionReturn(PETSC_SUCCESS);
152 }
153 
154 // Get error for problems with exact solutions
155 PetscErrorCode GetError_NS(CeedData ceed_data, DM dm, User user, Vec Q, PetscScalar final_time) {
156   PetscInt  loc_nodes;
157   Vec       Q_exact, Q_exact_loc;
158   PetscReal rel_error, norm_error, norm_exact;
159   PetscFunctionBegin;
160 
161   // Get exact solution at final time
162   PetscCall(DMCreateGlobalVector(dm, &Q_exact));
163   PetscCall(DMGetLocalVector(dm, &Q_exact_loc));
164   PetscCall(VecGetSize(Q_exact_loc, &loc_nodes));
165   PetscCall(ICs_FixMultiplicity(dm, ceed_data, user, Q_exact_loc, Q_exact, final_time));
166 
167   // Get |exact solution - obtained solution|
168   PetscCall(VecNorm(Q_exact, NORM_1, &norm_exact));
169   PetscCall(VecAXPY(Q, -1.0, Q_exact));
170   PetscCall(VecNorm(Q, NORM_1, &norm_error));
171 
172   // Compute relative error
173   rel_error = norm_error / norm_exact;
174 
175   // Output relative error
176   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Relative Error: %g\n", (double)rel_error));
177   // Cleanup
178   PetscCall(DMRestoreLocalVector(dm, &Q_exact_loc));
179   PetscCall(VecDestroy(&Q_exact));
180 
181   PetscFunctionReturn(PETSC_SUCCESS);
182 }
183 
184 // Post-processing
185 PetscErrorCode PostProcess_NS(TS ts, CeedData ceed_data, DM dm, ProblemData *problem, User user, Vec Q, PetscScalar final_time) {
186   PetscInt          steps;
187   TSConvergedReason reason;
188   PetscFunctionBegin;
189 
190   // Print relative error
191   if (problem->non_zero_time && user->app_ctx->test_type == TESTTYPE_NONE) {
192     PetscCall(GetError_NS(ceed_data, dm, user, Q, final_time));
193   }
194 
195   // Print final time and number of steps
196   PetscCall(TSGetStepNumber(ts, &steps));
197   PetscCall(TSGetConvergedReason(ts, &reason));
198   if (user->app_ctx->test_type == TESTTYPE_NONE) {
199     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time integrator %s on time step %" PetscInt_FMT " with final time %g\n", TSConvergedReasons[reason],
200                           steps, (double)final_time));
201   }
202 
203   // Output numerical values from command line
204   PetscCall(VecViewFromOptions(Q, NULL, "-vec_view"));
205 
206   // Compare reference solution values with current test run for CI
207   if (user->app_ctx->test_type == TESTTYPE_SOLVER) {
208     PetscCall(RegressionTests_NS(user->app_ctx, Q));
209   }
210   PetscFunctionReturn(PETSC_SUCCESS);
211 }
212 
213 const PetscInt32 FLUIDS_FILE_TOKEN    = 0xceedf00;  // for backwards compatibility
214 const PetscInt32 FLUIDS_FILE_TOKEN_32 = 0xceedf32;
215 const PetscInt32 FLUIDS_FILE_TOKEN_64 = 0xceedf64;
216 
217 // Gather initial Q values in case of continuation of simulation
218 PetscErrorCode SetupICsFromBinary(MPI_Comm comm, AppCtx app_ctx, Vec Q) {
219   PetscViewer viewer;
220 
221   PetscFunctionBegin;
222 
223   PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_file, FILE_MODE_READ, &viewer));
224   PetscCall(LoadFluidsBinaryVec(comm, viewer, Q, &app_ctx->cont_time, &app_ctx->cont_steps));
225   PetscCall(PetscViewerDestroy(&viewer));
226 
227   PetscFunctionReturn(PETSC_SUCCESS);
228 }
229 
230 // Record boundary values from initial condition
231 PetscErrorCode SetBCsFromICs_NS(DM dm, Vec Q, Vec Q_loc) {
232   Vec Qbc, boundary_mask;
233   PetscFunctionBegin;
234 
235   PetscCall(DMGetNamedLocalVector(dm, "Qbc", &Qbc));
236   PetscCall(VecCopy(Q_loc, Qbc));
237   PetscCall(VecZeroEntries(Q_loc));
238   PetscCall(DMGlobalToLocal(dm, Q, INSERT_VALUES, Q_loc));
239   PetscCall(VecAXPY(Qbc, -1., Q_loc));
240   PetscCall(DMRestoreNamedLocalVector(dm, "Qbc", &Qbc));
241   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_NS));
242 
243   PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask));
244   PetscCall(DMGetGlobalVector(dm, &Q));
245   PetscCall(VecZeroEntries(boundary_mask));
246   PetscCall(VecSet(Q, 1.0));
247   PetscCall(DMGlobalToLocal(dm, Q, INSERT_VALUES, boundary_mask));
248   PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask));
249 
250   PetscFunctionReturn(PETSC_SUCCESS);
251 }
252 
253 // Free a plain data context that was allocated using PETSc; returning libCEED error codes
254 int FreeContextPetsc(void *data) {
255   if (PetscFree(data)) return CeedError(NULL, CEED_ERROR_ACCESS, "PetscFree failed");
256   return CEED_ERROR_SUCCESS;
257 }
258 
259 // Return mass qfunction specification for number of components N
260 PetscErrorCode CreateMassQFunction(Ceed ceed, CeedInt N, CeedInt q_data_size, CeedQFunction *qf) {
261   PetscFunctionBeginUser;
262 
263   switch (N) {
264     case 1:
265       CeedQFunctionCreateInterior(ceed, 1, Mass_1, Mass_1_loc, qf);
266       break;
267     case 5:
268       CeedQFunctionCreateInterior(ceed, 1, Mass_5, Mass_5_loc, qf);
269       break;
270     case 7:
271       CeedQFunctionCreateInterior(ceed, 1, Mass_7, Mass_7_loc, qf);
272       break;
273     case 9:
274       CeedQFunctionCreateInterior(ceed, 1, Mass_9, Mass_9_loc, qf);
275       break;
276     case 22:
277       CeedQFunctionCreateInterior(ceed, 1, Mass_22, Mass_22_loc, qf);
278       break;
279     default:
280       SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "Could not find mass qfunction of size %d", N);
281   }
282 
283   CeedQFunctionAddInput(*qf, "u", N, CEED_EVAL_INTERP);
284   CeedQFunctionAddInput(*qf, "qdata", q_data_size, CEED_EVAL_NONE);
285   CeedQFunctionAddOutput(*qf, "v", N, CEED_EVAL_INTERP);
286   PetscFunctionReturn(PETSC_SUCCESS);
287 }
288 
289 PetscErrorCode NodalProjectionDataDestroy(NodalProjectionData context) {
290   PetscFunctionBeginUser;
291   if (context == NULL) PetscFunctionReturn(PETSC_SUCCESS);
292 
293   PetscCall(DMDestroy(&context->dm));
294   PetscCall(KSPDestroy(&context->ksp));
295 
296   PetscCall(OperatorApplyContextDestroy(context->l2_rhs_ctx));
297 
298   PetscCall(PetscFree(context));
299 
300   PetscFunctionReturn(PETSC_SUCCESS);
301 }
302 
303 /*
304  * @brief Open a PHASTA *.dat file, grabbing dimensions and file pointer
305  *
306  * This function opens the file specified by `path` using `PetscFOpen` and passes the file pointer in `fp`.
307  * It is not closed in this function, thus `fp` must be closed sometime after this function has been called (using `PetscFClose` for example).
308  *
309  * Assumes that the first line of the file has the number of rows and columns as the only two entries, separated by a single space.
310  *
311  * @param[in]  comm           MPI_Comm for the program
312  * @param[in]  path           Path to the file
313  * @param[in]  char_array_len Length of the character array that should contain each line
314  * @param[out] dims           Dimensions of the file, taken from the first line of the file
315  * @param[out] fp File        pointer to the opened file
316  */
317 PetscErrorCode PHASTADatFileOpen(const MPI_Comm comm, const char path[PETSC_MAX_PATH_LEN], const PetscInt char_array_len, PetscInt dims[2],
318                                  FILE **fp) {
319   int    ndims;
320   char   line[char_array_len];
321   char **array;
322 
323   PetscFunctionBeginUser;
324   PetscCall(PetscFOpen(comm, path, "r", fp));
325   PetscCall(PetscSynchronizedFGets(comm, *fp, char_array_len, line));
326   PetscCall(PetscStrToArray(line, ' ', &ndims, &array));
327   PetscCheck(ndims == 2, comm, PETSC_ERR_FILE_UNEXPECTED, "Found %d dimensions instead of 2 on the first line of %s", ndims, path);
328 
329   for (PetscInt i = 0; i < ndims; i++) dims[i] = atoi(array[i]);
330   PetscCall(PetscStrToArrayDestroy(ndims, array));
331 
332   PetscFunctionReturn(PETSC_SUCCESS);
333 }
334 
335 /*
336  * @brief Get the number of rows for the PHASTA file at path.
337  *
338  * Assumes that the first line of the file has the number of rows and columns as the only two entries, separated by a single space.
339  *
340  * @param[in]  comm  MPI_Comm for the program
341  * @param[in]  path  Path to the file
342  * @param[out] nrows Number of rows
343  */
344 PetscErrorCode PHASTADatFileGetNRows(const MPI_Comm comm, const char path[PETSC_MAX_PATH_LEN], PetscInt *nrows) {
345   const PetscInt char_array_len = 512;
346   PetscInt       dims[2];
347   FILE          *fp;
348 
349   PetscFunctionBeginUser;
350   PetscCall(PHASTADatFileOpen(comm, path, char_array_len, dims, &fp));
351   *nrows = dims[0];
352   PetscCall(PetscFClose(comm, fp));
353 
354   PetscFunctionReturn(PETSC_SUCCESS);
355 }
356 
357 PetscErrorCode PHASTADatFileReadToArrayReal(MPI_Comm comm, const char path[PETSC_MAX_PATH_LEN], PetscReal array[]) {
358   PetscInt       dims[2];
359   int            ndims;
360   FILE          *fp;
361   const PetscInt char_array_len = 512;
362   char           line[char_array_len];
363   char         **row_array;
364   PetscFunctionBeginUser;
365 
366   PetscCall(PHASTADatFileOpen(comm, path, char_array_len, dims, &fp));
367 
368   for (PetscInt i = 0; i < dims[0]; i++) {
369     PetscCall(PetscSynchronizedFGets(comm, fp, char_array_len, line));
370     PetscCall(PetscStrToArray(line, ' ', &ndims, &row_array));
371     PetscCheck(ndims == dims[1], comm, PETSC_ERR_FILE_UNEXPECTED,
372                "Line %" PetscInt_FMT " of %s does not contain enough columns (%d instead of %" PetscInt_FMT ")", i, path, ndims, dims[1]);
373 
374     for (PetscInt j = 0; j < dims[1]; j++) {
375       array[i * dims[1] + j] = (PetscReal)atof(row_array[j]);
376     }
377   }
378 
379   PetscCall(PetscFClose(comm, fp));
380 
381   PetscFunctionReturn(PETSC_SUCCESS);
382 }
383 
384 PetscLogEvent       FLUIDS_CeedOperatorApply;
385 PetscLogEvent       FLUIDS_CeedOperatorAssemble;
386 PetscLogEvent       FLUIDS_CeedOperatorAssembleDiagonal;
387 PetscLogEvent       FLUIDS_CeedOperatorAssemblePointBlockDiagonal;
388 static PetscClassId libCEED_classid;
389 
390 PetscErrorCode RegisterLogEvents() {
391   PetscFunctionBeginUser;
392   PetscCall(PetscClassIdRegister("libCEED", &libCEED_classid));
393   PetscCall(PetscLogEventRegister("CeedOpApply", libCEED_classid, &FLUIDS_CeedOperatorApply));
394   PetscCall(PetscLogEventRegister("CeedOpAsm", libCEED_classid, &FLUIDS_CeedOperatorAssemble));
395   PetscCall(PetscLogEventRegister("CeedOpAsmD", libCEED_classid, &FLUIDS_CeedOperatorAssembleDiagonal));
396   PetscCall(PetscLogEventRegister("CeedOpAsmPBD", libCEED_classid, &FLUIDS_CeedOperatorAssemblePointBlockDiagonal));
397   PetscFunctionReturn(PETSC_SUCCESS);
398 }
399 
400 /**
401   @brief Translate array of CeedInt to PetscInt.
402     If the types differ, `array_ceed` is freed with `free()` and `array_petsc` is allocated with `malloc()`.
403     Caller is responsible for freeing `array_petsc` with `free()`.
404 
405   @param[in]      num_entries  Number of array entries
406   @param[in,out]  array_ceed   Array of CeedInts
407   @param[out]     array_petsc  Array of PetscInts
408 **/
409 PetscErrorCode IntArrayC2P(PetscInt num_entries, CeedInt **array_ceed, PetscInt **array_petsc) {
410   CeedInt  int_c = 0;
411   PetscInt int_p = 0;
412 
413   PetscFunctionBeginUser;
414   if (sizeof(int_c) == sizeof(int_p)) {
415     *array_petsc = (PetscInt *)*array_ceed;
416   } else {
417     *array_petsc = malloc(num_entries * sizeof(PetscInt));
418     for (PetscInt i = 0; i < num_entries; i++) (*array_petsc)[i] = (*array_ceed)[i];
419     free(*array_ceed);
420   }
421   *array_ceed = NULL;
422 
423   PetscFunctionReturn(PETSC_SUCCESS);
424 }
425 
426 /**
427   @brief Translate array of PetscInt to CeedInt.
428     If the types differ, `array_petsc` is freed with `PetscFree()` and `array_ceed` is allocated with `PetscMalloc1()`.
429     Caller is responsible for freeing `array_ceed` with `PetscFree()`.
430 
431   @param[in]      num_entries  Number of array entries
432   @param[in,out]  array_petsc  Array of PetscInts
433   @param[out]     array_ceed   Array of CeedInts
434 **/
435 PetscErrorCode IntArrayP2C(PetscInt num_entries, PetscInt **array_petsc, CeedInt **array_ceed) {
436   CeedInt  int_c = 0;
437   PetscInt int_p = 0;
438 
439   PetscFunctionBeginUser;
440   if (sizeof(int_c) == sizeof(int_p)) {
441     *array_ceed = (CeedInt *)*array_petsc;
442   } else {
443     PetscCall(PetscMalloc1(num_entries, array_ceed));
444     for (PetscInt i = 0; i < num_entries; i++) (*array_ceed)[i] = (*array_petsc)[i];
445     PetscCall(PetscFree(*array_petsc));
446   }
447   *array_petsc = NULL;
448 
449   PetscFunctionReturn(PETSC_SUCCESS);
450 }
451 
452 // Print information about the given simulation run
453 PetscErrorCode PrintRunInfo(User user, Physics phys_ctx, ProblemData *problem, MPI_Comm comm) {
454   PetscFunctionBeginUser;
455   // Header and rank
456   char        host_name[PETSC_MAX_PATH_LEN];
457   PetscMPIInt rank, comm_size;
458   PetscCall(PetscGetHostName(host_name, sizeof host_name));
459   PetscCallMPI(MPI_Comm_rank(comm, &rank));
460   PetscCallMPI(MPI_Comm_size(comm, &comm_size));
461   PetscCall(PetscPrintf(comm,
462                         "\n-- Navier-Stokes solver - libCEED + PETSc --\n"
463                         "  MPI:\n"
464                         "    Host Name                          : %s\n"
465                         "    Total ranks                        : %d\n",
466                         host_name, comm_size));
467 
468   // Problem specific info
469   PetscCall(problem->print_info(problem, user->app_ctx));
470 
471   // libCEED
472   const char *used_resource;
473   CeedMemType mem_type_backend;
474   CeedGetResource(user->ceed, &used_resource);
475   CeedGetPreferredMemType(user->ceed, &mem_type_backend);
476   PetscCall(PetscPrintf(comm,
477                         "  libCEED:\n"
478                         "    libCEED Backend                    : %s\n"
479                         "    libCEED Backend MemType            : %s\n",
480                         used_resource, CeedMemTypes[mem_type_backend]));
481   // PETSc
482   char box_faces_str[PETSC_MAX_PATH_LEN] = "3,3,3";
483   if (problem->dim == 2) box_faces_str[3] = '\0';
484   PetscCall(PetscOptionsGetString(NULL, NULL, "-dm_plex_box_faces", box_faces_str, sizeof(box_faces_str), NULL));
485   MatType mat_type;
486   VecType vec_type;
487   PetscCall(DMGetMatType(user->dm, &mat_type));
488   PetscCall(DMGetVecType(user->dm, &vec_type));
489   PetscCall(PetscPrintf(comm,
490                         "  PETSc:\n"
491                         "    Box Faces                          : %s\n"
492                         "    DM MatType                         : %s\n"
493                         "    DM VecType                         : %s\n"
494                         "    Time Stepping Scheme               : %s\n",
495                         box_faces_str, mat_type, vec_type, phys_ctx->implicit ? "implicit" : "explicit"));
496   if (user->app_ctx->cont_steps) {
497     PetscCall(PetscPrintf(comm,
498                           "  Continue:\n"
499                           "    Filename:                          : %s\n"
500                           "    Step:                              : %" PetscInt_FMT "\n"
501                           "    Time:                              : %g\n",
502                           user->app_ctx->cont_file, user->app_ctx->cont_steps, user->app_ctx->cont_time));
503   }
504   // Mesh
505   const PetscInt num_comp_q = 5;
506   PetscInt       glob_dofs, owned_dofs, local_dofs;
507   const CeedInt  num_P = user->app_ctx->degree + 1, num_Q = num_P + user->app_ctx->q_extra;
508   // -- Get global size
509   PetscCall(DMGetGlobalVectorInfo(user->dm, &owned_dofs, &glob_dofs, NULL));
510   // -- Get local size
511   PetscCall(DMGetLocalVectorInfo(user->dm, &local_dofs, NULL, NULL));
512   PetscCall(PetscPrintf(comm,
513                         "  Mesh:\n"
514                         "    Number of 1D Basis Nodes (P)       : %" CeedInt_FMT "\n"
515                         "    Number of 1D Quadrature Points (Q) : %" CeedInt_FMT "\n"
516                         "    Global DoFs                        : %" PetscInt_FMT "\n"
517                         "    DoFs per node                      : %" PetscInt_FMT "\n"
518                         "    Global %" PetscInt_FMT "-DoF nodes                 : %" PetscInt_FMT "\n",
519                         num_P, num_Q, glob_dofs, num_comp_q, num_comp_q, glob_dofs / num_comp_q));
520   // -- Get Partition Statistics
521   PetscCall(PetscPrintf(comm, "  Partition:                             (min,max,median,max/median)\n"));
522   {
523     PetscInt *gather_buffer = NULL;
524     PetscInt  part_owned_dofs[3], part_local_dofs[3], part_boundary_dofs[3], part_neighbors[3];
525     PetscInt  median_index = comm_size % 2 ? comm_size / 2 : comm_size / 2 - 1;
526     if (!rank) PetscCall(PetscMalloc1(comm_size, &gather_buffer));
527 
528     PetscCallMPI(MPI_Gather(&owned_dofs, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
529     if (!rank) {
530       PetscCall(PetscSortInt(comm_size, gather_buffer));
531       part_owned_dofs[0]             = gather_buffer[0];              // min
532       part_owned_dofs[1]             = gather_buffer[comm_size - 1];  // max
533       part_owned_dofs[2]             = gather_buffer[median_index];   // median
534       PetscReal part_owned_dof_ratio = (PetscReal)part_owned_dofs[1] / (PetscReal)part_owned_dofs[2];
535       PetscCall(PetscPrintf(
536           comm, "    Global Vector %" PetscInt_FMT "-DoF nodes          : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n", num_comp_q,
537           part_owned_dofs[0] / num_comp_q, part_owned_dofs[1] / num_comp_q, part_owned_dofs[2] / num_comp_q, part_owned_dof_ratio));
538     }
539 
540     PetscCallMPI(MPI_Gather(&local_dofs, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
541     if (!rank) {
542       PetscCall(PetscSortInt(comm_size, gather_buffer));
543       part_local_dofs[0]             = gather_buffer[0];              // min
544       part_local_dofs[1]             = gather_buffer[comm_size - 1];  // max
545       part_local_dofs[2]             = gather_buffer[median_index];   // median
546       PetscReal part_local_dof_ratio = (PetscReal)part_local_dofs[1] / (PetscReal)part_local_dofs[2];
547       PetscCall(PetscPrintf(
548           comm, "    Local Vector %" PetscInt_FMT "-DoF nodes           : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n", num_comp_q,
549           part_local_dofs[0] / num_comp_q, part_local_dofs[1] / num_comp_q, part_local_dofs[2] / num_comp_q, part_local_dof_ratio));
550     }
551 
552     if (comm_size != 1) {
553       PetscInt num_remote_roots_total = 0, num_remote_leaves_total = 0, num_ghost_interface_ranks = 0, num_owned_interface_ranks = 0;
554       {
555         PetscSF            sf;
556         PetscInt           nrranks, niranks;
557         const PetscInt    *roffset, *rmine, *rremote, *ioffset, *irootloc;
558         const PetscMPIInt *rranks, *iranks;
559         PetscCall(DMGetSectionSF(user->dm, &sf));
560         PetscCall(PetscSFGetRootRanks(sf, &nrranks, &rranks, &roffset, &rmine, &rremote));
561         PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, &ioffset, &irootloc));
562         for (PetscInt i = 0; i < nrranks; i++) {
563           if (rranks[i] == rank) continue;  // Ignore same-part global->local transfers
564           num_remote_roots_total += roffset[i + 1] - roffset[i];
565           num_ghost_interface_ranks++;
566         }
567         for (PetscInt i = 0; i < niranks; i++) {
568           if (iranks[i] == rank) continue;
569           num_remote_leaves_total += ioffset[i + 1] - ioffset[i];
570           num_owned_interface_ranks++;
571         }
572       }
573       PetscCallMPI(MPI_Gather(&num_remote_roots_total, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
574       if (!rank) {
575         PetscCall(PetscSortInt(comm_size, gather_buffer));
576         part_boundary_dofs[0]           = gather_buffer[0];              // min
577         part_boundary_dofs[1]           = gather_buffer[comm_size - 1];  // max
578         part_boundary_dofs[2]           = gather_buffer[median_index];   // median
579         PetscReal part_shared_dof_ratio = (PetscReal)part_boundary_dofs[1] / (PetscReal)part_boundary_dofs[2];
580         PetscCall(PetscPrintf(
581             comm, "    Ghost Interface %" PetscInt_FMT "-DoF nodes        : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
582             num_comp_q, part_boundary_dofs[0] / num_comp_q, part_boundary_dofs[1] / num_comp_q, part_boundary_dofs[2] / num_comp_q,
583             part_shared_dof_ratio));
584       }
585 
586       PetscCallMPI(MPI_Gather(&num_ghost_interface_ranks, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
587       if (!rank) {
588         PetscCall(PetscSortInt(comm_size, gather_buffer));
589         part_neighbors[0]              = gather_buffer[0];              // min
590         part_neighbors[1]              = gather_buffer[comm_size - 1];  // max
591         part_neighbors[2]              = gather_buffer[median_index];   // median
592         PetscReal part_neighbors_ratio = (PetscReal)part_neighbors[1] / (PetscReal)part_neighbors[2];
593         PetscCall(PetscPrintf(comm, "    Ghost Interface Ranks              : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
594                               part_neighbors[0], part_neighbors[1], part_neighbors[2], part_neighbors_ratio));
595       }
596 
597       PetscCallMPI(MPI_Gather(&num_remote_leaves_total, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
598       if (!rank) {
599         PetscCall(PetscSortInt(comm_size, gather_buffer));
600         part_boundary_dofs[0]           = gather_buffer[0];              // min
601         part_boundary_dofs[1]           = gather_buffer[comm_size - 1];  // max
602         part_boundary_dofs[2]           = gather_buffer[median_index];   // median
603         PetscReal part_shared_dof_ratio = (PetscReal)part_boundary_dofs[1] / (PetscReal)part_boundary_dofs[2];
604         PetscCall(PetscPrintf(
605             comm, "    Owned Interface %" PetscInt_FMT "-DoF nodes        : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
606             num_comp_q, part_boundary_dofs[0] / num_comp_q, part_boundary_dofs[1] / num_comp_q, part_boundary_dofs[2] / num_comp_q,
607             part_shared_dof_ratio));
608       }
609 
610       PetscCallMPI(MPI_Gather(&num_owned_interface_ranks, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
611       if (!rank) {
612         PetscCall(PetscSortInt(comm_size, gather_buffer));
613         part_neighbors[0]              = gather_buffer[0];              // min
614         part_neighbors[1]              = gather_buffer[comm_size - 1];  // max
615         part_neighbors[2]              = gather_buffer[median_index];   // median
616         PetscReal part_neighbors_ratio = (PetscReal)part_neighbors[1] / (PetscReal)part_neighbors[2];
617         PetscCall(PetscPrintf(comm, "    Owned Interface Ranks              : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
618                               part_neighbors[0], part_neighbors[1], part_neighbors[2], part_neighbors_ratio));
619       }
620     }
621 
622     if (!rank) PetscCall(PetscFree(gather_buffer));
623   }
624 
625   PetscFunctionReturn(PETSC_SUCCESS);
626 }
627