xref: /honee/src/misc.c (revision 0edc72b3d4deacb8e64d91ba6eb4a10b9d32f68c)
1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3 
4 /// @file
5 /// Miscellaneous utility functions
6 
7 #include <ceed.h>
8 #include <petscdm.h>
9 #include <petscsf.h>
10 #include <petscts.h>
11 
12 #include <navierstokes.h>
13 #include "../qfunctions/mass.h"
14 
15 PetscErrorCode ICs_FixMultiplicity(DM dm, CeedData ceed_data, User user, Vec Q_loc, Vec Q, CeedScalar time) {
16   Ceed         ceed = user->ceed;
17   CeedVector   mult_vec;
18   PetscMemType m_mem_type;
19   Vec          Multiplicity, Multiplicity_loc;
20 
21   PetscFunctionBeginUser;
22   if (user->phys->ics_time_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(ceed_data->op_ics_ctx->op, user->phys->ics_time_label, &time));
23   PetscCall(ApplyCeedOperatorLocalToGlobal(NULL, Q, ceed_data->op_ics_ctx));
24 
25   PetscCallCeed(ceed, CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &mult_vec, NULL));
26 
27   // -- Get multiplicity
28   PetscCall(DMGetLocalVector(dm, &Multiplicity_loc));
29   PetscCall(VecPetscToCeed(Multiplicity_loc, &m_mem_type, mult_vec));
30   PetscCallCeed(ceed, CeedElemRestrictionGetMultiplicity(ceed_data->elem_restr_q, mult_vec));
31   PetscCall(VecCeedToPetsc(mult_vec, m_mem_type, Multiplicity_loc));
32 
33   PetscCall(DMGetGlobalVector(dm, &Multiplicity));
34   PetscCall(VecZeroEntries(Multiplicity));
35   PetscCall(DMLocalToGlobal(dm, Multiplicity_loc, ADD_VALUES, Multiplicity));
36 
37   // -- Fix multiplicity
38   PetscCall(VecPointwiseDivide(Q, Q, Multiplicity));
39   PetscCall(VecPointwiseDivide(Q_loc, Q_loc, Multiplicity_loc));
40 
41   PetscCall(DMRestoreLocalVector(dm, &Multiplicity_loc));
42   PetscCall(DMRestoreGlobalVector(dm, &Multiplicity));
43   PetscCallCeed(ceed, CeedVectorDestroy(&mult_vec));
44   PetscFunctionReturn(PETSC_SUCCESS);
45 }
46 
47 // Record boundary values from initial condition
48 PetscErrorCode SetBCsFromICs(DM dm, Vec Q, Vec Q_loc) {
49   PetscFunctionBeginUser;
50   {  // Capture initial condition values in Qbc
51     Vec Qbc;
52 
53     PetscCall(DMGetNamedLocalVector(dm, "Qbc", &Qbc));
54     PetscCall(VecCopy(Q_loc, Qbc));
55     PetscCall(VecZeroEntries(Q_loc));
56     PetscCall(DMGlobalToLocal(dm, Q, INSERT_VALUES, Q_loc));
57     PetscCall(VecAXPY(Qbc, -1., Q_loc));
58     PetscCall(DMRestoreNamedLocalVector(dm, "Qbc", &Qbc));
59   }
60   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_FromICs));
61 
62   {  // Set boundary mask to zero out essential BCs
63     Vec boundary_mask, ones;
64 
65     PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask));
66     PetscCall(DMGetGlobalVector(dm, &ones));
67     PetscCall(VecZeroEntries(boundary_mask));
68     PetscCall(VecSet(ones, 1.0));
69     PetscCall(DMGlobalToLocal(dm, ones, INSERT_VALUES, boundary_mask));
70     PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask));
71     PetscCall(DMRestoreGlobalVector(dm, &ones));
72   }
73   PetscFunctionReturn(PETSC_SUCCESS);
74 }
75 
76 PetscErrorCode DMPlexInsertBoundaryValues_FromICs(DM dm, PetscBool insert_essential, Vec Q_loc, PetscReal time, Vec face_geom_FVM, Vec cell_geom_FVM,
77                                                   Vec grad_FVM) {
78   Vec Qbc, boundary_mask;
79 
80   PetscFunctionBeginUser;
81   // Mask (zero) Strong BC entries
82   PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask));
83   PetscCall(VecPointwiseMult(Q_loc, Q_loc, boundary_mask));
84   PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask));
85 
86   PetscCall(DMGetNamedLocalVector(dm, "Qbc", &Qbc));
87   PetscCall(VecAXPY(Q_loc, 1., Qbc));
88   PetscCall(DMRestoreNamedLocalVector(dm, "Qbc", &Qbc));
89   PetscFunctionReturn(PETSC_SUCCESS);
90 }
91 
92 // Compare reference solution values with current test run for CI
93 PetscErrorCode RegressionTest(AppCtx app_ctx, Vec Q) {
94   Vec         Qref;
95   PetscViewer viewer;
96   PetscReal   error, Qrefnorm;
97   MPI_Comm    comm = PetscObjectComm((PetscObject)Q);
98 
99   PetscFunctionBeginUser;
100   // Read reference file
101   PetscCall(VecDuplicate(Q, &Qref));
102   PetscCheck(strcmp(app_ctx->test_file_path, "") != 0, comm, PETSC_ERR_FILE_READ, "File for regression test not given");
103   PetscCall(PetscViewerBinaryOpen(comm, app_ctx->test_file_path, FILE_MODE_READ, &viewer));
104   PetscCall(HoneeLoadBinaryVec(viewer, Qref, NULL, NULL));
105 
106   // Compute error with respect to reference solution
107   PetscCall(VecAXPY(Q, -1.0, Qref));
108   PetscCall(VecNorm(Qref, NORM_MAX, &Qrefnorm));
109   PetscCall(VecScale(Q, 1. / Qrefnorm));
110   PetscCall(VecNorm(Q, NORM_MAX, &error));
111 
112   // Check error
113   if (error > app_ctx->test_tol) {
114     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test failed with error norm %g\n", (double)error));
115   }
116 
117   // Cleanup
118   PetscCall(PetscViewerDestroy(&viewer));
119   PetscCall(VecDestroy(&Qref));
120   PetscFunctionReturn(PETSC_SUCCESS);
121 }
122 
123 // Get error for problems with exact solutions
124 PetscErrorCode PrintError(CeedData ceed_data, DM dm, User user, Vec Q, PetscScalar final_time) {
125   PetscInt  loc_nodes;
126   Vec       Q_exact, Q_exact_loc;
127   PetscReal rel_error, norm_error, norm_exact;
128 
129   PetscFunctionBeginUser;
130   // Get exact solution at final time
131   PetscCall(DMGetGlobalVector(dm, &Q_exact));
132   PetscCall(DMGetLocalVector(dm, &Q_exact_loc));
133   PetscCall(VecGetSize(Q_exact_loc, &loc_nodes));
134   PetscCall(ICs_FixMultiplicity(dm, ceed_data, user, Q_exact_loc, Q_exact, final_time));
135 
136   // Get |exact solution - obtained solution|
137   PetscCall(VecNorm(Q_exact, NORM_1, &norm_exact));
138   PetscCall(VecAXPY(Q, -1.0, Q_exact));
139   PetscCall(VecNorm(Q, NORM_1, &norm_error));
140 
141   rel_error = norm_error / norm_exact;
142   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Relative Error: %g\n", (double)rel_error));
143   PetscCall(DMRestoreLocalVector(dm, &Q_exact_loc));
144   PetscCall(DMRestoreGlobalVector(dm, &Q_exact));
145   PetscFunctionReturn(PETSC_SUCCESS);
146 }
147 
148 // Post-processing
149 PetscErrorCode PostProcess(TS ts, CeedData ceed_data, DM dm, ProblemData problem, User user, Vec Q, PetscScalar final_time) {
150   PetscInt          steps;
151   TSConvergedReason reason;
152 
153   PetscFunctionBeginUser;
154   // Print relative error
155   if (problem->compute_exact_solution_error && user->app_ctx->test_type == TESTTYPE_NONE) {
156     PetscCall(PrintError(ceed_data, dm, user, Q, final_time));
157   }
158 
159   // Print final time and number of steps
160   PetscCall(TSGetStepNumber(ts, &steps));
161   PetscCall(TSGetConvergedReason(ts, &reason));
162   if (user->app_ctx->test_type == TESTTYPE_NONE) {
163     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time integrator %s on time step %" PetscInt_FMT " with final time %g\n", TSConvergedReasons[reason],
164                           steps, (double)final_time));
165   }
166 
167   // Output numerical values from command line
168   PetscCall(VecViewFromOptions(Q, NULL, "-vec_view"));
169 
170   // Compare reference solution values with current test run for CI
171   if (user->app_ctx->test_type == TESTTYPE_SOLVER) {
172     PetscCall(RegressionTest(user->app_ctx, Q));
173   }
174   PetscFunctionReturn(PETSC_SUCCESS);
175 }
176 
177 // Free a plain data context that was allocated using PETSc; returning libCEED error codes
178 int FreeContextPetsc(void *data) {
179   if (PetscFree(data)) return CeedError(NULL, CEED_ERROR_ACCESS, "PetscFree failed");
180   return CEED_ERROR_SUCCESS;
181 }
182 
183 // @brief Return mass qfunction specification for number of components N
184 PetscErrorCode CreateMassQFunction(Ceed ceed, CeedInt N, CeedInt q_data_size, CeedQFunction *qf) {
185   PetscFunctionBeginUser;
186   switch (N) {
187     case 1:
188       PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, Mass_1, Mass_1_loc, qf));
189       break;
190     case 5:
191       PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, Mass_5, Mass_5_loc, qf));
192       break;
193     case 7:
194       PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, Mass_7, Mass_7_loc, qf));
195       break;
196     case 9:
197       PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, Mass_9, Mass_9_loc, qf));
198       break;
199     case 22:
200       PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, Mass_22, Mass_22_loc, qf));
201       break;
202     default:
203       SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "Could not find mass qfunction of size %d", N);
204   }
205 
206   PetscCallCeed(ceed, CeedQFunctionAddInput(*qf, "u", N, CEED_EVAL_INTERP));
207   PetscCallCeed(ceed, CeedQFunctionAddInput(*qf, "qdata", q_data_size, CEED_EVAL_NONE));
208   PetscCallCeed(ceed, CeedQFunctionAddOutput(*qf, "v", N, CEED_EVAL_INTERP));
209   PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(*qf, N));
210   PetscFunctionReturn(PETSC_SUCCESS);
211 }
212 
213 PetscErrorCode NodalProjectionDataDestroy(NodalProjectionData context) {
214   PetscFunctionBeginUser;
215   if (context == NULL) PetscFunctionReturn(PETSC_SUCCESS);
216 
217   PetscCall(DMDestroy(&context->dm));
218   PetscCall(KSPDestroy(&context->ksp));
219 
220   PetscCall(OperatorApplyContextDestroy(context->l2_rhs_ctx));
221 
222   PetscCall(PetscFree(context));
223   PetscFunctionReturn(PETSC_SUCCESS);
224 }
225 
226 /**
227    @brief Sets the value of an option if it is not already set
228 
229    @param[in,out] options `PetscOptions` database, or `NULL` for default global database
230    @param[in]     name    Name of the option (with `-` prepended to it)
231    @param[in]     value   Value to set the option to
232 **/
233 PetscErrorCode HoneeOptionsSetValueDefault(PetscOptions options, const char name[], const char value[]) {
234   PetscBool has_option;
235 
236   PetscFunctionBeginUser;
237   PetscCall(PetscOptionsHasName(options, NULL, name, &has_option));
238   if (!has_option) PetscCall(PetscOptionsSetValue(options, name, value));
239   PetscFunctionReturn(PETSC_SUCCESS);
240 }
241 
242 // Print information about the given simulation run
243 PetscErrorCode PrintRunInfo(User user, Physics phys_ctx, ProblemData problem, TS ts) {
244   Ceed     ceed = user->ceed;
245   MPI_Comm comm = PetscObjectComm((PetscObject)ts);
246 
247   PetscFunctionBeginUser;
248   // Header and rank
249   char        host_name[PETSC_MAX_PATH_LEN];
250   PetscMPIInt rank, comm_size;
251   PetscCall(PetscGetHostName(host_name, sizeof host_name));
252   PetscCallMPI(MPI_Comm_rank(comm, &rank));
253   PetscCallMPI(MPI_Comm_size(comm, &comm_size));
254   PetscCall(PetscPrintf(comm,
255                         "\n-- Navier-Stokes solver - libCEED + PETSc --\n"
256                         "  MPI:\n"
257                         "    Host Name                          : %s\n"
258                         "    Total ranks                        : %d\n",
259                         host_name, comm_size));
260 
261   // Problem specific info
262   PetscCall(problem->print_info(user, problem, user->app_ctx));
263 
264   // libCEED
265   const char *used_resource;
266   CeedMemType mem_type_backend;
267   PetscCallCeed(ceed, CeedGetResource(user->ceed, &used_resource));
268   PetscCallCeed(ceed, CeedGetPreferredMemType(user->ceed, &mem_type_backend));
269   PetscCall(PetscPrintf(comm,
270                         "  libCEED:\n"
271                         "    libCEED Backend                    : %s\n"
272                         "    libCEED Backend MemType            : %s\n",
273                         used_resource, CeedMemTypes[mem_type_backend]));
274   // PETSc
275   {
276     VecType  vec_type;
277     char     box_faces_str[PETSC_MAX_PATH_LEN] = "3,3,3";
278     PetscInt dim;
279 
280     PetscCall(DMGetDimension(user->dm, &dim));
281     if (dim == 2) box_faces_str[3] = '\0';
282     PetscCall(PetscOptionsGetString(NULL, NULL, "-dm_plex_box_faces", box_faces_str, sizeof(box_faces_str), NULL));
283     PetscCall(DMGetVecType(user->dm, &vec_type));
284     PetscCall(PetscPrintf(comm,
285                           "  PETSc:\n"
286                           "    Box Faces                          : %s\n"
287                           "    DM VecType                         : %s\n"
288                           "    Time Stepping Scheme               : %s\n",
289                           box_faces_str, vec_type, phys_ctx->implicit ? "implicit" : "explicit"));
290   }
291   {
292     char           pmat_type_str[PETSC_MAX_PATH_LEN];
293     MatType        amat_type, pmat_type;
294     Mat            Amat, Pmat;
295     TSIJacobianFn *ijacob_function;
296 
297     PetscCall(TSGetIJacobian(ts, &Amat, &Pmat, &ijacob_function, NULL));
298     PetscCall(MatGetType(Amat, &amat_type));
299     PetscCall(MatGetType(Pmat, &pmat_type));
300 
301     PetscCall(PetscStrncpy(pmat_type_str, pmat_type, sizeof(pmat_type_str)));
302     if (!strcmp(pmat_type, MATCEED)) {
303       MatType pmat_coo_type;
304       char    pmat_coo_type_str[PETSC_MAX_PATH_LEN];
305 
306       PetscCall(MatCeedGetCOOMatType(Pmat, &pmat_coo_type));
307       PetscCall(PetscSNPrintf(pmat_coo_type_str, sizeof(pmat_coo_type_str), " (COO MatType: %s)", pmat_coo_type));
308       PetscCall(PetscStrlcat(pmat_type_str, pmat_coo_type_str, sizeof(pmat_type_str)));
309     }
310     if (ijacob_function) {
311       PetscCall(PetscPrintf(comm,
312                             "    IJacobian A MatType                : %s\n"
313                             "    IJacobian P MatType                : %s\n",
314                             amat_type, pmat_type_str));
315     }
316   }
317   if (user->app_ctx->cont_steps) {
318     PetscCall(PetscPrintf(comm,
319                           "  Continue:\n"
320                           "    Filename:                          : %s\n"
321                           "    Step:                              : %" PetscInt_FMT "\n"
322                           "    Time:                              : %g\n",
323                           user->app_ctx->cont_file, user->app_ctx->cont_steps, user->app_ctx->cont_time));
324   }
325   // Mesh
326   const PetscInt num_comp_q = 5;
327   PetscInt       glob_dofs, owned_dofs, local_dofs;
328   const CeedInt  num_P = user->app_ctx->degree + 1, num_Q = num_P + user->app_ctx->q_extra;
329   PetscCall(DMGetGlobalVectorInfo(user->dm, &owned_dofs, &glob_dofs, NULL));
330   PetscCall(DMGetLocalVectorInfo(user->dm, &local_dofs, NULL, NULL));
331   PetscCall(PetscPrintf(comm,
332                         "  Mesh:\n"
333                         "    Number of 1D Basis Nodes (P)       : %" CeedInt_FMT "\n"
334                         "    Number of 1D Quadrature Points (Q) : %" CeedInt_FMT "\n"
335                         "    Global DoFs                        : %" PetscInt_FMT "\n"
336                         "    DoFs per node                      : %" PetscInt_FMT "\n"
337                         "    Global %" PetscInt_FMT "-DoF nodes                 : %" PetscInt_FMT "\n",
338                         num_P, num_Q, glob_dofs, num_comp_q, num_comp_q, glob_dofs / num_comp_q));
339   // -- Get Partition Statistics
340   PetscCall(PetscPrintf(comm, "  Partition:                             (min,max,median,max/median)\n"));
341   {
342     PetscInt *gather_buffer = NULL;
343     PetscInt  part_owned_dofs[3], part_local_dofs[3], part_boundary_dofs[3], part_neighbors[3];
344     PetscInt  median_index = comm_size % 2 ? comm_size / 2 : comm_size / 2 - 1;
345     if (!rank) PetscCall(PetscMalloc1(comm_size, &gather_buffer));
346 
347     PetscCallMPI(MPI_Gather(&owned_dofs, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
348     if (!rank) {
349       PetscCall(PetscSortInt(comm_size, gather_buffer));
350       part_owned_dofs[0]             = gather_buffer[0];              // min
351       part_owned_dofs[1]             = gather_buffer[comm_size - 1];  // max
352       part_owned_dofs[2]             = gather_buffer[median_index];   // median
353       PetscReal part_owned_dof_ratio = (PetscReal)part_owned_dofs[1] / (PetscReal)part_owned_dofs[2];
354       PetscCall(PetscPrintf(
355           comm, "    Global Vector %" PetscInt_FMT "-DoF nodes          : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n", num_comp_q,
356           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));
357     }
358 
359     PetscCallMPI(MPI_Gather(&local_dofs, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
360     if (!rank) {
361       PetscCall(PetscSortInt(comm_size, gather_buffer));
362       part_local_dofs[0]             = gather_buffer[0];              // min
363       part_local_dofs[1]             = gather_buffer[comm_size - 1];  // max
364       part_local_dofs[2]             = gather_buffer[median_index];   // median
365       PetscReal part_local_dof_ratio = (PetscReal)part_local_dofs[1] / (PetscReal)part_local_dofs[2];
366       PetscCall(PetscPrintf(
367           comm, "    Local Vector %" PetscInt_FMT "-DoF nodes           : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n", num_comp_q,
368           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));
369     }
370 
371     if (comm_size != 1) {
372       PetscInt num_remote_roots_total = 0, num_remote_leaves_total = 0, num_ghost_interface_ranks = 0, num_owned_interface_ranks = 0;
373       {
374         PetscSF            sf;
375         PetscInt           nrranks, niranks;
376         const PetscInt    *roffset, *rmine, *rremote, *ioffset, *irootloc;
377         const PetscMPIInt *rranks, *iranks;
378         PetscCall(DMGetSectionSF(user->dm, &sf));
379         PetscCall(PetscSFGetRootRanks(sf, &nrranks, &rranks, &roffset, &rmine, &rremote));
380         PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, &ioffset, &irootloc));
381         for (PetscInt i = 0; i < nrranks; i++) {
382           if (rranks[i] == rank) continue;  // Ignore same-part global->local transfers
383           num_remote_roots_total += roffset[i + 1] - roffset[i];
384           num_ghost_interface_ranks++;
385         }
386         for (PetscInt i = 0; i < niranks; i++) {
387           if (iranks[i] == rank) continue;
388           num_remote_leaves_total += ioffset[i + 1] - ioffset[i];
389           num_owned_interface_ranks++;
390         }
391       }
392       PetscCallMPI(MPI_Gather(&num_remote_roots_total, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
393       if (!rank) {
394         PetscCall(PetscSortInt(comm_size, gather_buffer));
395         part_boundary_dofs[0]           = gather_buffer[0];              // min
396         part_boundary_dofs[1]           = gather_buffer[comm_size - 1];  // max
397         part_boundary_dofs[2]           = gather_buffer[median_index];   // median
398         PetscReal part_shared_dof_ratio = (PetscReal)part_boundary_dofs[1] / (PetscReal)part_boundary_dofs[2];
399         PetscCall(PetscPrintf(
400             comm, "    Ghost Interface %" PetscInt_FMT "-DoF nodes        : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
401             num_comp_q, part_boundary_dofs[0] / num_comp_q, part_boundary_dofs[1] / num_comp_q, part_boundary_dofs[2] / num_comp_q,
402             part_shared_dof_ratio));
403       }
404 
405       PetscCallMPI(MPI_Gather(&num_ghost_interface_ranks, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
406       if (!rank) {
407         PetscCall(PetscSortInt(comm_size, gather_buffer));
408         part_neighbors[0]              = gather_buffer[0];              // min
409         part_neighbors[1]              = gather_buffer[comm_size - 1];  // max
410         part_neighbors[2]              = gather_buffer[median_index];   // median
411         PetscReal part_neighbors_ratio = (PetscReal)part_neighbors[1] / (PetscReal)part_neighbors[2];
412         PetscCall(PetscPrintf(comm, "    Ghost Interface Ranks              : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
413                               part_neighbors[0], part_neighbors[1], part_neighbors[2], part_neighbors_ratio));
414       }
415 
416       PetscCallMPI(MPI_Gather(&num_remote_leaves_total, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
417       if (!rank) {
418         PetscCall(PetscSortInt(comm_size, gather_buffer));
419         part_boundary_dofs[0]           = gather_buffer[0];              // min
420         part_boundary_dofs[1]           = gather_buffer[comm_size - 1];  // max
421         part_boundary_dofs[2]           = gather_buffer[median_index];   // median
422         PetscReal part_shared_dof_ratio = (PetscReal)part_boundary_dofs[1] / (PetscReal)part_boundary_dofs[2];
423         PetscCall(PetscPrintf(
424             comm, "    Owned Interface %" PetscInt_FMT "-DoF nodes        : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
425             num_comp_q, part_boundary_dofs[0] / num_comp_q, part_boundary_dofs[1] / num_comp_q, part_boundary_dofs[2] / num_comp_q,
426             part_shared_dof_ratio));
427       }
428 
429       PetscCallMPI(MPI_Gather(&num_owned_interface_ranks, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
430       if (!rank) {
431         PetscCall(PetscSortInt(comm_size, gather_buffer));
432         part_neighbors[0]              = gather_buffer[0];              // min
433         part_neighbors[1]              = gather_buffer[comm_size - 1];  // max
434         part_neighbors[2]              = gather_buffer[median_index];   // median
435         PetscReal part_neighbors_ratio = (PetscReal)part_neighbors[1] / (PetscReal)part_neighbors[2];
436         PetscCall(PetscPrintf(comm, "    Owned Interface Ranks              : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
437                               part_neighbors[0], part_neighbors[1], part_neighbors[2], part_neighbors_ratio));
438       }
439     }
440 
441     if (!rank) PetscCall(PetscFree(gather_buffer));
442   }
443   PetscFunctionReturn(PETSC_SUCCESS);
444 }
445