xref: /honee/src/misc.c (revision 93ca29b6e094721a136ad10375037d18cdc7fb9d)
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(LoadFluidsBinaryVec(comm, 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 // Gather initial Q values in case of continuation of simulation
178 PetscErrorCode SetupICsFromBinary(MPI_Comm comm, AppCtx app_ctx, Vec Q) {
179   PetscViewer viewer;
180 
181   PetscFunctionBeginUser;
182   PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_file, FILE_MODE_READ, &viewer));
183   PetscCall(LoadFluidsBinaryVec(comm, viewer, Q, &app_ctx->cont_time, &app_ctx->cont_steps));
184   PetscCall(PetscViewerDestroy(&viewer));
185   PetscFunctionReturn(PETSC_SUCCESS);
186 }
187 
188 // Free a plain data context that was allocated using PETSc; returning libCEED error codes
189 int FreeContextPetsc(void *data) {
190   if (PetscFree(data)) return CeedError(NULL, CEED_ERROR_ACCESS, "PetscFree failed");
191   return CEED_ERROR_SUCCESS;
192 }
193 
194 // Return mass qfunction specification for number of components N
195 PetscErrorCode CreateMassQFunction(Ceed ceed, CeedInt N, CeedInt q_data_size, CeedQFunction *qf) {
196   PetscFunctionBeginUser;
197   switch (N) {
198     case 1:
199       PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, Mass_1, Mass_1_loc, qf));
200       break;
201     case 5:
202       PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, Mass_5, Mass_5_loc, qf));
203       break;
204     case 7:
205       PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, Mass_7, Mass_7_loc, qf));
206       break;
207     case 9:
208       PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, Mass_9, Mass_9_loc, qf));
209       break;
210     case 22:
211       PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, Mass_22, Mass_22_loc, qf));
212       break;
213     default:
214       SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "Could not find mass qfunction of size %d", N);
215   }
216 
217   PetscCallCeed(ceed, CeedQFunctionAddInput(*qf, "u", N, CEED_EVAL_INTERP));
218   PetscCallCeed(ceed, CeedQFunctionAddInput(*qf, "qdata", q_data_size, CEED_EVAL_NONE));
219   PetscCallCeed(ceed, CeedQFunctionAddOutput(*qf, "v", N, CEED_EVAL_INTERP));
220   PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(*qf, N));
221   PetscFunctionReturn(PETSC_SUCCESS);
222 }
223 
224 PetscErrorCode NodalProjectionDataDestroy(NodalProjectionData context) {
225   PetscFunctionBeginUser;
226   if (context == NULL) PetscFunctionReturn(PETSC_SUCCESS);
227 
228   PetscCall(DMDestroy(&context->dm));
229   PetscCall(KSPDestroy(&context->ksp));
230 
231   PetscCall(OperatorApplyContextDestroy(context->l2_rhs_ctx));
232 
233   PetscCall(PetscFree(context));
234   PetscFunctionReturn(PETSC_SUCCESS);
235 }
236 
237 // Print information about the given simulation run
238 PetscErrorCode PrintRunInfo(User user, Physics phys_ctx, ProblemData problem, TS ts) {
239   Ceed     ceed = user->ceed;
240   MPI_Comm comm = PetscObjectComm((PetscObject)ts);
241 
242   PetscFunctionBeginUser;
243   // Header and rank
244   char        host_name[PETSC_MAX_PATH_LEN];
245   PetscMPIInt rank, comm_size;
246   PetscCall(PetscGetHostName(host_name, sizeof host_name));
247   PetscCallMPI(MPI_Comm_rank(comm, &rank));
248   PetscCallMPI(MPI_Comm_size(comm, &comm_size));
249   PetscCall(PetscPrintf(comm,
250                         "\n-- Navier-Stokes solver - libCEED + PETSc --\n"
251                         "  MPI:\n"
252                         "    Host Name                          : %s\n"
253                         "    Total ranks                        : %d\n",
254                         host_name, comm_size));
255 
256   // Problem specific info
257   PetscCall(problem->print_info(user, problem, user->app_ctx));
258 
259   // libCEED
260   const char *used_resource;
261   CeedMemType mem_type_backend;
262   PetscCallCeed(ceed, CeedGetResource(user->ceed, &used_resource));
263   PetscCallCeed(ceed, CeedGetPreferredMemType(user->ceed, &mem_type_backend));
264   PetscCall(PetscPrintf(comm,
265                         "  libCEED:\n"
266                         "    libCEED Backend                    : %s\n"
267                         "    libCEED Backend MemType            : %s\n",
268                         used_resource, CeedMemTypes[mem_type_backend]));
269   // PETSc
270   VecType vec_type;
271   char    box_faces_str[PETSC_MAX_PATH_LEN] = "3,3,3";
272   if (problem->dim == 2) box_faces_str[3] = '\0';
273   PetscCall(PetscOptionsGetString(NULL, NULL, "-dm_plex_box_faces", box_faces_str, sizeof(box_faces_str), NULL));
274   PetscCall(DMGetVecType(user->dm, &vec_type));
275   PetscCall(PetscPrintf(comm,
276                         "  PETSc:\n"
277                         "    Box Faces                          : %s\n"
278                         "    DM VecType                         : %s\n"
279                         "    Time Stepping Scheme               : %s\n",
280                         box_faces_str, vec_type, phys_ctx->implicit ? "implicit" : "explicit"));
281   {
282     char           pmat_type_str[PETSC_MAX_PATH_LEN];
283     MatType        amat_type, pmat_type;
284     Mat            Amat, Pmat;
285     TSIJacobianFn *ijacob_function;
286 
287     PetscCall(TSGetIJacobian(ts, &Amat, &Pmat, &ijacob_function, NULL));
288     PetscCall(MatGetType(Amat, &amat_type));
289     PetscCall(MatGetType(Pmat, &pmat_type));
290 
291     PetscCall(PetscStrncpy(pmat_type_str, pmat_type, sizeof(pmat_type_str)));
292     if (!strcmp(pmat_type, MATCEED)) {
293       MatType pmat_coo_type;
294       char    pmat_coo_type_str[PETSC_MAX_PATH_LEN];
295 
296       PetscCall(MatCeedGetCOOMatType(Pmat, &pmat_coo_type));
297       PetscCall(PetscSNPrintf(pmat_coo_type_str, sizeof(pmat_coo_type_str), " (COO MatType: %s)", pmat_coo_type));
298       PetscCall(PetscStrlcat(pmat_type_str, pmat_coo_type_str, sizeof(pmat_type_str)));
299     }
300     if (ijacob_function) {
301       PetscCall(PetscPrintf(comm,
302                             "    IJacobian A MatType                : %s\n"
303                             "    IJacobian P MatType                : %s\n",
304                             amat_type, pmat_type_str));
305     }
306   }
307   if (user->app_ctx->cont_steps) {
308     PetscCall(PetscPrintf(comm,
309                           "  Continue:\n"
310                           "    Filename:                          : %s\n"
311                           "    Step:                              : %" PetscInt_FMT "\n"
312                           "    Time:                              : %g\n",
313                           user->app_ctx->cont_file, user->app_ctx->cont_steps, user->app_ctx->cont_time));
314   }
315   // Mesh
316   const PetscInt num_comp_q = 5;
317   PetscInt       glob_dofs, owned_dofs, local_dofs;
318   const CeedInt  num_P = user->app_ctx->degree + 1, num_Q = num_P + user->app_ctx->q_extra;
319   PetscCall(DMGetGlobalVectorInfo(user->dm, &owned_dofs, &glob_dofs, NULL));
320   PetscCall(DMGetLocalVectorInfo(user->dm, &local_dofs, NULL, NULL));
321   PetscCall(PetscPrintf(comm,
322                         "  Mesh:\n"
323                         "    Number of 1D Basis Nodes (P)       : %" CeedInt_FMT "\n"
324                         "    Number of 1D Quadrature Points (Q) : %" CeedInt_FMT "\n"
325                         "    Global DoFs                        : %" PetscInt_FMT "\n"
326                         "    DoFs per node                      : %" PetscInt_FMT "\n"
327                         "    Global %" PetscInt_FMT "-DoF nodes                 : %" PetscInt_FMT "\n",
328                         num_P, num_Q, glob_dofs, num_comp_q, num_comp_q, glob_dofs / num_comp_q));
329   // -- Get Partition Statistics
330   PetscCall(PetscPrintf(comm, "  Partition:                             (min,max,median,max/median)\n"));
331   {
332     PetscInt *gather_buffer = NULL;
333     PetscInt  part_owned_dofs[3], part_local_dofs[3], part_boundary_dofs[3], part_neighbors[3];
334     PetscInt  median_index = comm_size % 2 ? comm_size / 2 : comm_size / 2 - 1;
335     if (!rank) PetscCall(PetscMalloc1(comm_size, &gather_buffer));
336 
337     PetscCallMPI(MPI_Gather(&owned_dofs, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
338     if (!rank) {
339       PetscCall(PetscSortInt(comm_size, gather_buffer));
340       part_owned_dofs[0]             = gather_buffer[0];              // min
341       part_owned_dofs[1]             = gather_buffer[comm_size - 1];  // max
342       part_owned_dofs[2]             = gather_buffer[median_index];   // median
343       PetscReal part_owned_dof_ratio = (PetscReal)part_owned_dofs[1] / (PetscReal)part_owned_dofs[2];
344       PetscCall(PetscPrintf(
345           comm, "    Global Vector %" PetscInt_FMT "-DoF nodes          : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n", num_comp_q,
346           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));
347     }
348 
349     PetscCallMPI(MPI_Gather(&local_dofs, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
350     if (!rank) {
351       PetscCall(PetscSortInt(comm_size, gather_buffer));
352       part_local_dofs[0]             = gather_buffer[0];              // min
353       part_local_dofs[1]             = gather_buffer[comm_size - 1];  // max
354       part_local_dofs[2]             = gather_buffer[median_index];   // median
355       PetscReal part_local_dof_ratio = (PetscReal)part_local_dofs[1] / (PetscReal)part_local_dofs[2];
356       PetscCall(PetscPrintf(
357           comm, "    Local Vector %" PetscInt_FMT "-DoF nodes           : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n", num_comp_q,
358           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));
359     }
360 
361     if (comm_size != 1) {
362       PetscInt num_remote_roots_total = 0, num_remote_leaves_total = 0, num_ghost_interface_ranks = 0, num_owned_interface_ranks = 0;
363       {
364         PetscSF            sf;
365         PetscInt           nrranks, niranks;
366         const PetscInt    *roffset, *rmine, *rremote, *ioffset, *irootloc;
367         const PetscMPIInt *rranks, *iranks;
368         PetscCall(DMGetSectionSF(user->dm, &sf));
369         PetscCall(PetscSFGetRootRanks(sf, &nrranks, &rranks, &roffset, &rmine, &rremote));
370         PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, &ioffset, &irootloc));
371         for (PetscInt i = 0; i < nrranks; i++) {
372           if (rranks[i] == rank) continue;  // Ignore same-part global->local transfers
373           num_remote_roots_total += roffset[i + 1] - roffset[i];
374           num_ghost_interface_ranks++;
375         }
376         for (PetscInt i = 0; i < niranks; i++) {
377           if (iranks[i] == rank) continue;
378           num_remote_leaves_total += ioffset[i + 1] - ioffset[i];
379           num_owned_interface_ranks++;
380         }
381       }
382       PetscCallMPI(MPI_Gather(&num_remote_roots_total, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
383       if (!rank) {
384         PetscCall(PetscSortInt(comm_size, gather_buffer));
385         part_boundary_dofs[0]           = gather_buffer[0];              // min
386         part_boundary_dofs[1]           = gather_buffer[comm_size - 1];  // max
387         part_boundary_dofs[2]           = gather_buffer[median_index];   // median
388         PetscReal part_shared_dof_ratio = (PetscReal)part_boundary_dofs[1] / (PetscReal)part_boundary_dofs[2];
389         PetscCall(PetscPrintf(
390             comm, "    Ghost Interface %" PetscInt_FMT "-DoF nodes        : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
391             num_comp_q, part_boundary_dofs[0] / num_comp_q, part_boundary_dofs[1] / num_comp_q, part_boundary_dofs[2] / num_comp_q,
392             part_shared_dof_ratio));
393       }
394 
395       PetscCallMPI(MPI_Gather(&num_ghost_interface_ranks, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
396       if (!rank) {
397         PetscCall(PetscSortInt(comm_size, gather_buffer));
398         part_neighbors[0]              = gather_buffer[0];              // min
399         part_neighbors[1]              = gather_buffer[comm_size - 1];  // max
400         part_neighbors[2]              = gather_buffer[median_index];   // median
401         PetscReal part_neighbors_ratio = (PetscReal)part_neighbors[1] / (PetscReal)part_neighbors[2];
402         PetscCall(PetscPrintf(comm, "    Ghost Interface Ranks              : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
403                               part_neighbors[0], part_neighbors[1], part_neighbors[2], part_neighbors_ratio));
404       }
405 
406       PetscCallMPI(MPI_Gather(&num_remote_leaves_total, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
407       if (!rank) {
408         PetscCall(PetscSortInt(comm_size, gather_buffer));
409         part_boundary_dofs[0]           = gather_buffer[0];              // min
410         part_boundary_dofs[1]           = gather_buffer[comm_size - 1];  // max
411         part_boundary_dofs[2]           = gather_buffer[median_index];   // median
412         PetscReal part_shared_dof_ratio = (PetscReal)part_boundary_dofs[1] / (PetscReal)part_boundary_dofs[2];
413         PetscCall(PetscPrintf(
414             comm, "    Owned Interface %" PetscInt_FMT "-DoF nodes        : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
415             num_comp_q, part_boundary_dofs[0] / num_comp_q, part_boundary_dofs[1] / num_comp_q, part_boundary_dofs[2] / num_comp_q,
416             part_shared_dof_ratio));
417       }
418 
419       PetscCallMPI(MPI_Gather(&num_owned_interface_ranks, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
420       if (!rank) {
421         PetscCall(PetscSortInt(comm_size, gather_buffer));
422         part_neighbors[0]              = gather_buffer[0];              // min
423         part_neighbors[1]              = gather_buffer[comm_size - 1];  // max
424         part_neighbors[2]              = gather_buffer[median_index];   // median
425         PetscReal part_neighbors_ratio = (PetscReal)part_neighbors[1] / (PetscReal)part_neighbors[2];
426         PetscCall(PetscPrintf(comm, "    Owned Interface Ranks              : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
427                               part_neighbors[0], part_neighbors[1], part_neighbors[2], part_neighbors_ratio));
428       }
429     }
430 
431     if (!rank) PetscCall(PetscFree(gather_buffer));
432   }
433   PetscFunctionReturn(PETSC_SUCCESS);
434 }
435