xref: /honee/src/misc.c (revision 45abf96e51f89d2dff93c92b966bf3c4bdaad3f7)
1727da7e7SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2727da7e7SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3a515125bSLeila Ghaffari //
4727da7e7SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5a515125bSLeila Ghaffari //
6727da7e7SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7a515125bSLeila Ghaffari 
8a515125bSLeila Ghaffari /// @file
9a515125bSLeila Ghaffari /// Miscellaneous utility functions
10a515125bSLeila Ghaffari 
11e419654dSJeremy L Thompson #include <ceed.h>
12e419654dSJeremy L Thompson #include <petscdm.h>
13926a6279SJames Wright #include <petscsf.h>
14e419654dSJeremy L Thompson #include <petscts.h>
15e419654dSJeremy L Thompson 
16a515125bSLeila Ghaffari #include "../navierstokes.h"
179f59f36eSJames Wright #include "../qfunctions/mass.h"
18a515125bSLeila Ghaffari 
192b916ea7SJeremy L Thompson PetscErrorCode ICs_FixMultiplicity(DM dm, CeedData ceed_data, User user, Vec Q_loc, Vec Q, CeedScalar time) {
20a515125bSLeila Ghaffari   PetscFunctionBeginUser;
21a515125bSLeila Ghaffari 
22a515125bSLeila Ghaffari   // ---------------------------------------------------------------------------
23b7f03f12SJed Brown   // Update time for evaluation
24a515125bSLeila Ghaffari   // ---------------------------------------------------------------------------
258f18bb8bSJames Wright   if (user->phys->ics_time_label) CeedOperatorSetContextDouble(ceed_data->op_ics_ctx->op, user->phys->ics_time_label, &time);
26a515125bSLeila Ghaffari 
27a515125bSLeila Ghaffari   // ---------------------------------------------------------------------------
28a515125bSLeila Ghaffari   // ICs
29a515125bSLeila Ghaffari   // ---------------------------------------------------------------------------
30a515125bSLeila Ghaffari   // -- CEED Restriction
31a515125bSLeila Ghaffari   CeedVector q0_ceed;
32a515125bSLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &q0_ceed, NULL);
33a515125bSLeila Ghaffari 
34a515125bSLeila Ghaffari   // -- Place PETSc vector in CEED vector
358f18bb8bSJames Wright   PetscCall(ApplyCeedOperatorLocalToGlobal(NULL, Q, ceed_data->op_ics_ctx));
36a515125bSLeila Ghaffari 
37a515125bSLeila Ghaffari   // ---------------------------------------------------------------------------
38a515125bSLeila Ghaffari   // Fix multiplicity for output of ICs
39a515125bSLeila Ghaffari   // ---------------------------------------------------------------------------
40a515125bSLeila Ghaffari   // -- CEED Restriction
41a515125bSLeila Ghaffari   CeedVector mult_vec;
42a515125bSLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &mult_vec, NULL);
43a515125bSLeila Ghaffari 
44a515125bSLeila Ghaffari   // -- Place PETSc vector in CEED vector
45a515125bSLeila Ghaffari   PetscMemType m_mem_type;
46a515125bSLeila Ghaffari   Vec          multiplicity_loc;
472b916ea7SJeremy L Thompson   PetscCall(DMGetLocalVector(dm, &multiplicity_loc));
48fd969b44SJames Wright   PetscCall(VecP2C(multiplicity_loc, &m_mem_type, mult_vec));
49a515125bSLeila Ghaffari 
50a515125bSLeila Ghaffari   // -- Get multiplicity
51a515125bSLeila Ghaffari   CeedElemRestrictionGetMultiplicity(ceed_data->elem_restr_q, mult_vec);
52a515125bSLeila Ghaffari 
53a515125bSLeila Ghaffari   // -- Restore vectors
54fd969b44SJames Wright   PetscCall(VecC2P(mult_vec, m_mem_type, multiplicity_loc));
55a515125bSLeila Ghaffari 
56a515125bSLeila Ghaffari   // -- Local-to-Global
57a515125bSLeila Ghaffari   Vec multiplicity;
582b916ea7SJeremy L Thompson   PetscCall(DMGetGlobalVector(dm, &multiplicity));
592b916ea7SJeremy L Thompson   PetscCall(VecZeroEntries(multiplicity));
602b916ea7SJeremy L Thompson   PetscCall(DMLocalToGlobal(dm, multiplicity_loc, ADD_VALUES, multiplicity));
61a515125bSLeila Ghaffari 
62a515125bSLeila Ghaffari   // -- Fix multiplicity
632b916ea7SJeremy L Thompson   PetscCall(VecPointwiseDivide(Q, Q, multiplicity));
642b916ea7SJeremy L Thompson   PetscCall(VecPointwiseDivide(Q_loc, Q_loc, multiplicity_loc));
65a515125bSLeila Ghaffari 
66a515125bSLeila Ghaffari   // -- Restore vectors
672b916ea7SJeremy L Thompson   PetscCall(DMRestoreLocalVector(dm, &multiplicity_loc));
682b916ea7SJeremy L Thompson   PetscCall(DMRestoreGlobalVector(dm, &multiplicity));
69a515125bSLeila Ghaffari 
70a515125bSLeila Ghaffari   // Cleanup
71a515125bSLeila Ghaffari   CeedVectorDestroy(&mult_vec);
72a515125bSLeila Ghaffari   CeedVectorDestroy(&q0_ceed);
73a515125bSLeila Ghaffari 
74d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
75a515125bSLeila Ghaffari }
76a515125bSLeila Ghaffari 
772b916ea7SJeremy L Thompson PetscErrorCode DMPlexInsertBoundaryValues_NS(DM dm, PetscBool insert_essential, Vec Q_loc, PetscReal time, Vec face_geom_FVM, Vec cell_geom_FVM,
782b916ea7SJeremy L Thompson                                              Vec grad_FVM) {
799d437337SJames Wright   Vec Qbc, boundary_mask;
80a515125bSLeila Ghaffari   PetscFunctionBegin;
81a515125bSLeila Ghaffari 
822eb7bf1fSJames Wright   // Mask (zero) Strong BC entries
839d437337SJames Wright   PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask));
849d437337SJames Wright   PetscCall(VecPointwiseMult(Q_loc, Q_loc, boundary_mask));
859d437337SJames Wright   PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask));
869d437337SJames Wright 
872b916ea7SJeremy L Thompson   PetscCall(DMGetNamedLocalVector(dm, "Qbc", &Qbc));
882b916ea7SJeremy L Thompson   PetscCall(VecAXPY(Q_loc, 1., Qbc));
892b916ea7SJeremy L Thompson   PetscCall(DMRestoreNamedLocalVector(dm, "Qbc", &Qbc));
90a515125bSLeila Ghaffari 
91d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
92a515125bSLeila Ghaffari }
93a515125bSLeila Ghaffari 
94e7754af5SKenneth E. Jansen // @brief Load vector from binary file, possibly with embedded solution time and step number
95e7754af5SKenneth E. Jansen PetscErrorCode LoadFluidsBinaryVec(MPI_Comm comm, PetscViewer viewer, Vec Q, PetscReal *time, PetscInt *step_number) {
96e1233009SJames Wright   PetscInt   file_step_number;
97e1233009SJames Wright   PetscInt32 token;
98e7754af5SKenneth E. Jansen   PetscReal  file_time;
99e7754af5SKenneth E. Jansen   PetscFunctionBeginUser;
100e7754af5SKenneth E. Jansen 
101e7754af5SKenneth E. Jansen   // Attempt
102e1233009SJames Wright   PetscCall(PetscViewerBinaryRead(viewer, &token, 1, NULL, PETSC_INT32));
103e1233009SJames Wright   if (token == FLUIDS_FILE_TOKEN_32 || token == FLUIDS_FILE_TOKEN_64 ||
104e1233009SJames Wright       token == FLUIDS_FILE_TOKEN) {  // New style format; we're reading a file with step number and time in the header
105e7754af5SKenneth E. Jansen     PetscCall(PetscViewerBinaryRead(viewer, &file_step_number, 1, NULL, PETSC_INT));
106e7754af5SKenneth E. Jansen     PetscCall(PetscViewerBinaryRead(viewer, &file_time, 1, NULL, PETSC_REAL));
107e7754af5SKenneth E. Jansen     if (time) *time = file_time;
108e7754af5SKenneth E. Jansen     if (step_number) *step_number = file_step_number;
109e7754af5SKenneth E. Jansen   } else if (token == VEC_FILE_CLASSID) {  // Legacy format of just the vector, encoded as [VEC_FILE_CLASSID, length, ]
110e7754af5SKenneth E. Jansen     PetscInt length, N;
111e7754af5SKenneth E. Jansen     PetscCall(PetscViewerBinaryRead(viewer, &length, 1, NULL, PETSC_INT));
112e7754af5SKenneth E. Jansen     PetscCall(VecGetSize(Q, &N));
113e7754af5SKenneth E. Jansen     PetscCheck(length == N, comm, PETSC_ERR_ARG_INCOMP, "File Vec has length %" PetscInt_FMT " but DM has global Vec size %" PetscInt_FMT, length, N);
114e7754af5SKenneth E. Jansen     PetscCall(PetscViewerBinarySetSkipHeader(viewer, PETSC_TRUE));
115e7754af5SKenneth E. Jansen   } else SETERRQ(comm, PETSC_ERR_FILE_UNEXPECTED, "Not a fluids header token or a PETSc Vec in file");
116e7754af5SKenneth E. Jansen 
117e7754af5SKenneth E. Jansen   // Load Q from existent solution
118e7754af5SKenneth E. Jansen   PetscCall(VecLoad(Q, viewer));
119e7754af5SKenneth E. Jansen 
120d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
121e7754af5SKenneth E. Jansen }
122e7754af5SKenneth E. Jansen 
123a515125bSLeila Ghaffari // Compare reference solution values with current test run for CI
124a515125bSLeila Ghaffari PetscErrorCode RegressionTests_NS(AppCtx app_ctx, Vec Q) {
125a515125bSLeila Ghaffari   Vec         Qref;
126a515125bSLeila Ghaffari   PetscViewer viewer;
127a515125bSLeila Ghaffari   PetscReal   error, Qrefnorm;
128e7754af5SKenneth E. Jansen   MPI_Comm    comm = PetscObjectComm((PetscObject)Q);
129a515125bSLeila Ghaffari   PetscFunctionBegin;
130a515125bSLeila Ghaffari 
131a515125bSLeila Ghaffari   // Read reference file
1322b916ea7SJeremy L Thompson   PetscCall(VecDuplicate(Q, &Qref));
133e7754af5SKenneth E. Jansen   PetscCall(PetscViewerBinaryOpen(comm, app_ctx->test_file_path, FILE_MODE_READ, &viewer));
134e7754af5SKenneth E. Jansen   PetscCall(LoadFluidsBinaryVec(comm, viewer, Qref, NULL, NULL));
135a515125bSLeila Ghaffari 
136a515125bSLeila Ghaffari   // Compute error with respect to reference solution
1372b916ea7SJeremy L Thompson   PetscCall(VecAXPY(Q, -1.0, Qref));
1382b916ea7SJeremy L Thompson   PetscCall(VecNorm(Qref, NORM_MAX, &Qrefnorm));
1392b916ea7SJeremy L Thompson   PetscCall(VecScale(Q, 1. / Qrefnorm));
1402b916ea7SJeremy L Thompson   PetscCall(VecNorm(Q, NORM_MAX, &error));
141a515125bSLeila Ghaffari 
142a515125bSLeila Ghaffari   // Check error
143a515125bSLeila Ghaffari   if (error > app_ctx->test_tol) {
1442b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test failed with error norm %g\n", (double)error));
145a515125bSLeila Ghaffari   }
146a515125bSLeila Ghaffari 
147a515125bSLeila Ghaffari   // Cleanup
1482b916ea7SJeremy L Thompson   PetscCall(PetscViewerDestroy(&viewer));
1492b916ea7SJeremy L Thompson   PetscCall(VecDestroy(&Qref));
150a515125bSLeila Ghaffari 
151d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
152a515125bSLeila Ghaffari }
153a515125bSLeila Ghaffari 
154a515125bSLeila Ghaffari // Get error for problems with exact solutions
1552b916ea7SJeremy L Thompson PetscErrorCode GetError_NS(CeedData ceed_data, DM dm, User user, Vec Q, PetscScalar final_time) {
156a515125bSLeila Ghaffari   PetscInt  loc_nodes;
157a515125bSLeila Ghaffari   Vec       Q_exact, Q_exact_loc;
158a515125bSLeila Ghaffari   PetscReal rel_error, norm_error, norm_exact;
159a515125bSLeila Ghaffari   PetscFunctionBegin;
160a515125bSLeila Ghaffari 
161a515125bSLeila Ghaffari   // Get exact solution at final time
1622b916ea7SJeremy L Thompson   PetscCall(DMCreateGlobalVector(dm, &Q_exact));
1632b916ea7SJeremy L Thompson   PetscCall(DMGetLocalVector(dm, &Q_exact_loc));
1642b916ea7SJeremy L Thompson   PetscCall(VecGetSize(Q_exact_loc, &loc_nodes));
1652b916ea7SJeremy L Thompson   PetscCall(ICs_FixMultiplicity(dm, ceed_data, user, Q_exact_loc, Q_exact, final_time));
166a515125bSLeila Ghaffari 
167a515125bSLeila Ghaffari   // Get |exact solution - obtained solution|
1682b916ea7SJeremy L Thompson   PetscCall(VecNorm(Q_exact, NORM_1, &norm_exact));
1692b916ea7SJeremy L Thompson   PetscCall(VecAXPY(Q, -1.0, Q_exact));
1702b916ea7SJeremy L Thompson   PetscCall(VecNorm(Q, NORM_1, &norm_error));
171a515125bSLeila Ghaffari 
172a515125bSLeila Ghaffari   // Compute relative error
173a515125bSLeila Ghaffari   rel_error = norm_error / norm_exact;
174a515125bSLeila Ghaffari 
175a515125bSLeila Ghaffari   // Output relative error
1762b916ea7SJeremy L Thompson   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Relative Error: %g\n", (double)rel_error));
177a515125bSLeila Ghaffari   // Cleanup
1782b916ea7SJeremy L Thompson   PetscCall(DMRestoreLocalVector(dm, &Q_exact_loc));
1792b916ea7SJeremy L Thompson   PetscCall(VecDestroy(&Q_exact));
180a515125bSLeila Ghaffari 
181d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
182a515125bSLeila Ghaffari }
183a515125bSLeila Ghaffari 
184a515125bSLeila Ghaffari // Post-processing
1852b916ea7SJeremy L Thompson PetscErrorCode PostProcess_NS(TS ts, CeedData ceed_data, DM dm, ProblemData *problem, User user, Vec Q, PetscScalar final_time) {
186a515125bSLeila Ghaffari   PetscInt          steps;
187f0784ed3SJed Brown   TSConvergedReason reason;
188a515125bSLeila Ghaffari   PetscFunctionBegin;
189a515125bSLeila Ghaffari 
190a515125bSLeila Ghaffari   // Print relative error
1910e1e9333SJames Wright   if (problem->non_zero_time && user->app_ctx->test_type == TESTTYPE_NONE) {
1922b916ea7SJeremy L Thompson     PetscCall(GetError_NS(ceed_data, dm, user, Q, final_time));
193a515125bSLeila Ghaffari   }
194a515125bSLeila Ghaffari 
195a515125bSLeila Ghaffari   // Print final time and number of steps
1962b916ea7SJeremy L Thompson   PetscCall(TSGetStepNumber(ts, &steps));
197f0784ed3SJed Brown   PetscCall(TSGetConvergedReason(ts, &reason));
1980e1e9333SJames Wright   if (user->app_ctx->test_type == TESTTYPE_NONE) {
199f0784ed3SJed Brown     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time integrator %s on time step %" PetscInt_FMT " with final time %g\n", TSConvergedReasons[reason],
200f0784ed3SJed Brown                           steps, (double)final_time));
201a515125bSLeila Ghaffari   }
202a515125bSLeila Ghaffari 
203a515125bSLeila Ghaffari   // Output numerical values from command line
2042b916ea7SJeremy L Thompson   PetscCall(VecViewFromOptions(Q, NULL, "-vec_view"));
205a515125bSLeila Ghaffari 
206a515125bSLeila Ghaffari   // Compare reference solution values with current test run for CI
2070e1e9333SJames Wright   if (user->app_ctx->test_type == TESTTYPE_SOLVER) {
2082b916ea7SJeremy L Thompson     PetscCall(RegressionTests_NS(user->app_ctx, Q));
209a515125bSLeila Ghaffari   }
210d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
211a515125bSLeila Ghaffari }
212a515125bSLeila Ghaffari 
213e1233009SJames Wright const PetscInt32 FLUIDS_FILE_TOKEN    = 0xceedf00;  // for backwards compatibility
214e1233009SJames Wright const PetscInt32 FLUIDS_FILE_TOKEN_32 = 0xceedf32;
215e1233009SJames Wright const PetscInt32 FLUIDS_FILE_TOKEN_64 = 0xceedf64;
2169293eaa1SJed Brown 
217a515125bSLeila Ghaffari // Gather initial Q values in case of continuation of simulation
218a515125bSLeila Ghaffari PetscErrorCode SetupICsFromBinary(MPI_Comm comm, AppCtx app_ctx, Vec Q) {
219a515125bSLeila Ghaffari   PetscViewer viewer;
2202b916ea7SJeremy L Thompson 
221a515125bSLeila Ghaffari   PetscFunctionBegin;
222a515125bSLeila Ghaffari 
2232b916ea7SJeremy L Thompson   PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_file, FILE_MODE_READ, &viewer));
224e7754af5SKenneth E. Jansen   PetscCall(LoadFluidsBinaryVec(comm, viewer, Q, &app_ctx->cont_time, &app_ctx->cont_steps));
2252b916ea7SJeremy L Thompson   PetscCall(PetscViewerDestroy(&viewer));
226a515125bSLeila Ghaffari 
227d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
228a515125bSLeila Ghaffari }
229a515125bSLeila Ghaffari 
230a515125bSLeila Ghaffari // Record boundary values from initial condition
231a515125bSLeila Ghaffari PetscErrorCode SetBCsFromICs_NS(DM dm, Vec Q, Vec Q_loc) {
2329d437337SJames Wright   Vec Qbc, boundary_mask;
233a515125bSLeila Ghaffari   PetscFunctionBegin;
234a515125bSLeila Ghaffari 
2352b916ea7SJeremy L Thompson   PetscCall(DMGetNamedLocalVector(dm, "Qbc", &Qbc));
2362b916ea7SJeremy L Thompson   PetscCall(VecCopy(Q_loc, Qbc));
2372b916ea7SJeremy L Thompson   PetscCall(VecZeroEntries(Q_loc));
2382b916ea7SJeremy L Thompson   PetscCall(DMGlobalToLocal(dm, Q, INSERT_VALUES, Q_loc));
2392b916ea7SJeremy L Thompson   PetscCall(VecAXPY(Qbc, -1., Q_loc));
2402b916ea7SJeremy L Thompson   PetscCall(DMRestoreNamedLocalVector(dm, "Qbc", &Qbc));
2412b916ea7SJeremy L Thompson   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_NS));
242a515125bSLeila Ghaffari 
2439d437337SJames Wright   PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask));
2449d437337SJames Wright   PetscCall(DMGetGlobalVector(dm, &Q));
2459d437337SJames Wright   PetscCall(VecZeroEntries(boundary_mask));
2469d437337SJames Wright   PetscCall(VecSet(Q, 1.0));
2479d437337SJames Wright   PetscCall(DMGlobalToLocal(dm, Q, INSERT_VALUES, boundary_mask));
2489d437337SJames Wright   PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask));
2499d437337SJames Wright 
250d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
251a515125bSLeila Ghaffari }
25215a3537eSJed Brown 
25315a3537eSJed Brown // Free a plain data context that was allocated using PETSc; returning libCEED error codes
25415a3537eSJed Brown int FreeContextPetsc(void *data) {
2552b916ea7SJeremy L Thompson   if (PetscFree(data)) return CeedError(NULL, CEED_ERROR_ACCESS, "PetscFree failed");
25615a3537eSJed Brown   return CEED_ERROR_SUCCESS;
25715a3537eSJed Brown }
2589f59f36eSJames Wright 
2599f59f36eSJames Wright // Return mass qfunction specification for number of components N
2609f59f36eSJames Wright PetscErrorCode CreateMassQFunction(Ceed ceed, CeedInt N, CeedInt q_data_size, CeedQFunction *qf) {
2619f59f36eSJames Wright   PetscFunctionBeginUser;
2629f59f36eSJames Wright 
2639f59f36eSJames Wright   switch (N) {
2649f59f36eSJames Wright     case 1:
2656f539f71SJames Wright       CeedQFunctionCreateInterior(ceed, 1, Mass_1, Mass_1_loc, qf);
2669f59f36eSJames Wright       break;
2679f59f36eSJames Wright     case 5:
2686f539f71SJames Wright       CeedQFunctionCreateInterior(ceed, 1, Mass_5, Mass_5_loc, qf);
2699f59f36eSJames Wright       break;
270c38c977aSJames Wright     case 7:
2716f539f71SJames Wright       CeedQFunctionCreateInterior(ceed, 1, Mass_7, Mass_7_loc, qf);
272c38c977aSJames Wright       break;
2739f59f36eSJames Wright     case 9:
2746f539f71SJames Wright       CeedQFunctionCreateInterior(ceed, 1, Mass_9, Mass_9_loc, qf);
2759f59f36eSJames Wright       break;
2769f59f36eSJames Wright     case 22:
2776f539f71SJames Wright       CeedQFunctionCreateInterior(ceed, 1, Mass_22, Mass_22_loc, qf);
2789f59f36eSJames Wright       break;
2799f59f36eSJames Wright     default:
2806f539f71SJames Wright       SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "Could not find mass qfunction of size %d", N);
2819f59f36eSJames Wright   }
2829f59f36eSJames Wright 
2839f59f36eSJames Wright   CeedQFunctionAddInput(*qf, "u", N, CEED_EVAL_INTERP);
2849f59f36eSJames Wright   CeedQFunctionAddInput(*qf, "qdata", q_data_size, CEED_EVAL_NONE);
2859f59f36eSJames Wright   CeedQFunctionAddOutput(*qf, "v", N, CEED_EVAL_INTERP);
286d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
2879f59f36eSJames Wright }
288e5e81594SJames Wright 
289e5e81594SJames Wright /* @brief L^2 Projection of a source FEM function to a target FEM space
290e5e81594SJames Wright  *
291e5e81594SJames Wright  * To solve system using a lumped mass matrix, pass a KSP object with ksp_type=preonly, pc_type=jacobi, pc_jacobi_type=rowsum.
292e5e81594SJames Wright  *
293e5e81594SJames Wright  * @param[in]  source_vec    Global Vec of the source FEM function. NULL indicates using rhs_matop_ctx->X_loc
294e5e81594SJames Wright  * @param[out] target_vec    Global Vec of the target (result) FEM function. NULL indicates using rhs_matop_ctx->Y_loc
295e5e81594SJames Wright  * @param[in]  rhs_matop_ctx MatopApplyContext for performing the RHS evaluation
296e5e81594SJames Wright  * @param[in]  ksp           KSP for solving the consistent projection problem
297e5e81594SJames Wright  */
298f7325489SJames Wright PetscErrorCode ComputeL2Projection(Vec source_vec, Vec target_vec, OperatorApplyContext rhs_matop_ctx, KSP ksp) {
299e5e81594SJames Wright   PetscFunctionBeginUser;
300e5e81594SJames Wright 
301f7325489SJames Wright   PetscCall(ApplyCeedOperatorGlobalToGlobal(source_vec, target_vec, rhs_matop_ctx));
302e5e81594SJames Wright   PetscCall(KSPSolve(ksp, target_vec, target_vec));
303e5e81594SJames Wright 
304d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
305e5e81594SJames Wright }
306676080b4SJames Wright 
307457a5831SJames Wright PetscErrorCode NodalProjectionDataDestroy(NodalProjectionData context) {
308457a5831SJames Wright   PetscFunctionBeginUser;
309d949ddfcSJames Wright   if (context == NULL) PetscFunctionReturn(PETSC_SUCCESS);
310457a5831SJames Wright 
311457a5831SJames Wright   PetscCall(DMDestroy(&context->dm));
312457a5831SJames Wright   PetscCall(KSPDestroy(&context->ksp));
313457a5831SJames Wright 
314457a5831SJames Wright   PetscCall(OperatorApplyContextDestroy(context->l2_rhs_ctx));
315457a5831SJames Wright 
316457a5831SJames Wright   PetscCall(PetscFree(context));
317457a5831SJames Wright 
318d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
319457a5831SJames Wright }
320457a5831SJames Wright 
321676080b4SJames Wright /*
322676080b4SJames Wright  * @brief Open a PHASTA *.dat file, grabbing dimensions and file pointer
323676080b4SJames Wright  *
324676080b4SJames Wright  * This function opens the file specified by `path` using `PetscFOpen` and passes the file pointer in `fp`.
325676080b4SJames Wright  * It is not closed in this function, thus `fp` must be closed sometime after this function has been called (using `PetscFClose` for example).
326676080b4SJames Wright  *
327676080b4SJames Wright  * 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.
328676080b4SJames Wright  *
329676080b4SJames Wright  * @param[in]  comm           MPI_Comm for the program
330676080b4SJames Wright  * @param[in]  path           Path to the file
331676080b4SJames Wright  * @param[in]  char_array_len Length of the character array that should contain each line
332676080b4SJames Wright  * @param[out] dims           Dimensions of the file, taken from the first line of the file
333676080b4SJames Wright  * @param[out] fp File        pointer to the opened file
334676080b4SJames Wright  */
335676080b4SJames Wright PetscErrorCode PHASTADatFileOpen(const MPI_Comm comm, const char path[PETSC_MAX_PATH_LEN], const PetscInt char_array_len, PetscInt dims[2],
336676080b4SJames Wright                                  FILE **fp) {
337defe8520SJames Wright   int    ndims;
338676080b4SJames Wright   char   line[char_array_len];
339676080b4SJames Wright   char **array;
340676080b4SJames Wright 
341676080b4SJames Wright   PetscFunctionBeginUser;
342676080b4SJames Wright   PetscCall(PetscFOpen(comm, path, "r", fp));
343676080b4SJames Wright   PetscCall(PetscSynchronizedFGets(comm, *fp, char_array_len, line));
344676080b4SJames Wright   PetscCall(PetscStrToArray(line, ' ', &ndims, &array));
345defe8520SJames Wright   PetscCheck(ndims == 2, comm, PETSC_ERR_FILE_UNEXPECTED, "Found %d dimensions instead of 2 on the first line of %s", ndims, path);
346676080b4SJames Wright 
347676080b4SJames Wright   for (PetscInt i = 0; i < ndims; i++) dims[i] = atoi(array[i]);
348676080b4SJames Wright   PetscCall(PetscStrToArrayDestroy(ndims, array));
349676080b4SJames Wright 
350d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
351676080b4SJames Wright }
352676080b4SJames Wright 
353676080b4SJames Wright /*
354676080b4SJames Wright  * @brief Get the number of rows for the PHASTA file at path.
355676080b4SJames Wright  *
356676080b4SJames Wright  * 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.
357676080b4SJames Wright  *
358676080b4SJames Wright  * @param[in]  comm  MPI_Comm for the program
359676080b4SJames Wright  * @param[in]  path  Path to the file
360676080b4SJames Wright  * @param[out] nrows Number of rows
361676080b4SJames Wright  */
362676080b4SJames Wright PetscErrorCode PHASTADatFileGetNRows(const MPI_Comm comm, const char path[PETSC_MAX_PATH_LEN], PetscInt *nrows) {
363676080b4SJames Wright   const PetscInt char_array_len = 512;
364676080b4SJames Wright   PetscInt       dims[2];
365676080b4SJames Wright   FILE          *fp;
366676080b4SJames Wright 
367676080b4SJames Wright   PetscFunctionBeginUser;
368676080b4SJames Wright   PetscCall(PHASTADatFileOpen(comm, path, char_array_len, dims, &fp));
369676080b4SJames Wright   *nrows = dims[0];
370676080b4SJames Wright   PetscCall(PetscFClose(comm, fp));
371676080b4SJames Wright 
372d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
373676080b4SJames Wright }
37462b7942eSJames Wright 
37562b7942eSJames Wright PetscErrorCode PHASTADatFileReadToArrayReal(MPI_Comm comm, const char path[PETSC_MAX_PATH_LEN], PetscReal array[]) {
376defe8520SJames Wright   PetscInt       dims[2];
377defe8520SJames Wright   int            ndims;
37862b7942eSJames Wright   FILE          *fp;
37962b7942eSJames Wright   const PetscInt char_array_len = 512;
38062b7942eSJames Wright   char           line[char_array_len];
38162b7942eSJames Wright   char         **row_array;
38262b7942eSJames Wright   PetscFunctionBeginUser;
38362b7942eSJames Wright 
38462b7942eSJames Wright   PetscCall(PHASTADatFileOpen(comm, path, char_array_len, dims, &fp));
38562b7942eSJames Wright 
38662b7942eSJames Wright   for (PetscInt i = 0; i < dims[0]; i++) {
38762b7942eSJames Wright     PetscCall(PetscSynchronizedFGets(comm, fp, char_array_len, line));
38862b7942eSJames Wright     PetscCall(PetscStrToArray(line, ' ', &ndims, &row_array));
3895d28dccaSJames Wright     PetscCheck(ndims == dims[1], comm, PETSC_ERR_FILE_UNEXPECTED,
390defe8520SJames Wright                "Line %" PetscInt_FMT " of %s does not contain enough columns (%d instead of %" PetscInt_FMT ")", i, path, ndims, dims[1]);
39162b7942eSJames Wright 
39262b7942eSJames Wright     for (PetscInt j = 0; j < dims[1]; j++) {
39362b7942eSJames Wright       array[i * dims[1] + j] = (PetscReal)atof(row_array[j]);
39462b7942eSJames Wright     }
39562b7942eSJames Wright   }
39662b7942eSJames Wright 
39762b7942eSJames Wright   PetscCall(PetscFClose(comm, fp));
39862b7942eSJames Wright 
399d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
40062b7942eSJames Wright }
4017eedc94cSJames Wright 
4027eedc94cSJames Wright PetscLogEvent       FLUIDS_CeedOperatorApply;
4037eedc94cSJames Wright PetscLogEvent       FLUIDS_CeedOperatorAssemble;
4047eedc94cSJames Wright PetscLogEvent       FLUIDS_CeedOperatorAssembleDiagonal;
4057eedc94cSJames Wright PetscLogEvent       FLUIDS_CeedOperatorAssemblePointBlockDiagonal;
4067eedc94cSJames Wright static PetscClassId libCEED_classid;
4077eedc94cSJames Wright 
4087eedc94cSJames Wright PetscErrorCode RegisterLogEvents() {
4097eedc94cSJames Wright   PetscFunctionBeginUser;
4107eedc94cSJames Wright   PetscCall(PetscClassIdRegister("libCEED", &libCEED_classid));
4117eedc94cSJames Wright   PetscCall(PetscLogEventRegister("CeedOpApply", libCEED_classid, &FLUIDS_CeedOperatorApply));
4127eedc94cSJames Wright   PetscCall(PetscLogEventRegister("CeedOpAsm", libCEED_classid, &FLUIDS_CeedOperatorAssemble));
4137eedc94cSJames Wright   PetscCall(PetscLogEventRegister("CeedOpAsmD", libCEED_classid, &FLUIDS_CeedOperatorAssembleDiagonal));
4147eedc94cSJames Wright   PetscCall(PetscLogEventRegister("CeedOpAsmPBD", libCEED_classid, &FLUIDS_CeedOperatorAssemblePointBlockDiagonal));
415d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
4167eedc94cSJames Wright }
417defe8520SJames Wright 
418defe8520SJames Wright /**
419defe8520SJames Wright   @brief Translate array of CeedInt to PetscInt.
420defe8520SJames Wright     If the types differ, `array_ceed` is freed with `free()` and `array_petsc` is allocated with `malloc()`.
421defe8520SJames Wright     Caller is responsible for freeing `array_petsc` with `free()`.
422defe8520SJames Wright 
423defe8520SJames Wright   @param[in]      num_entries  Number of array entries
424defe8520SJames Wright   @param[in,out]  array_ceed   Array of CeedInts
425defe8520SJames Wright   @param[out]     array_petsc  Array of PetscInts
426defe8520SJames Wright **/
427defe8520SJames Wright PetscErrorCode IntArrayC2P(PetscInt num_entries, CeedInt **array_ceed, PetscInt **array_petsc) {
428defe8520SJames Wright   CeedInt  int_c = 0;
429defe8520SJames Wright   PetscInt int_p = 0;
430defe8520SJames Wright 
431defe8520SJames Wright   PetscFunctionBeginUser;
432defe8520SJames Wright   if (sizeof(int_c) == sizeof(int_p)) {
433defe8520SJames Wright     *array_petsc = (PetscInt *)*array_ceed;
434defe8520SJames Wright   } else {
435defe8520SJames Wright     *array_petsc = malloc(num_entries * sizeof(PetscInt));
436defe8520SJames Wright     for (PetscInt i = 0; i < num_entries; i++) (*array_petsc)[i] = (*array_ceed)[i];
437defe8520SJames Wright     free(*array_ceed);
438defe8520SJames Wright   }
439defe8520SJames Wright   *array_ceed = NULL;
440defe8520SJames Wright 
441defe8520SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
442defe8520SJames Wright }
443defe8520SJames Wright 
444defe8520SJames Wright /**
445defe8520SJames Wright   @brief Translate array of PetscInt to CeedInt.
446defe8520SJames Wright     If the types differ, `array_petsc` is freed with `PetscFree()` and `array_ceed` is allocated with `PetscMalloc1()`.
447defe8520SJames Wright     Caller is responsible for freeing `array_ceed` with `PetscFree()`.
448defe8520SJames Wright 
449defe8520SJames Wright   @param[in]      num_entries  Number of array entries
450defe8520SJames Wright   @param[in,out]  array_petsc  Array of PetscInts
451defe8520SJames Wright   @param[out]     array_ceed   Array of CeedInts
452defe8520SJames Wright **/
453defe8520SJames Wright PetscErrorCode IntArrayP2C(PetscInt num_entries, PetscInt **array_petsc, CeedInt **array_ceed) {
454defe8520SJames Wright   CeedInt  int_c = 0;
455defe8520SJames Wright   PetscInt int_p = 0;
456defe8520SJames Wright 
457defe8520SJames Wright   PetscFunctionBeginUser;
458defe8520SJames Wright   if (sizeof(int_c) == sizeof(int_p)) {
459defe8520SJames Wright     *array_ceed = (CeedInt *)*array_petsc;
460defe8520SJames Wright   } else {
461defe8520SJames Wright     PetscCall(PetscMalloc1(num_entries, array_ceed));
462defe8520SJames Wright     for (PetscInt i = 0; i < num_entries; i++) (*array_ceed)[i] = (*array_petsc)[i];
463defe8520SJames Wright     PetscCall(PetscFree(*array_petsc));
464defe8520SJames Wright   }
465defe8520SJames Wright   *array_petsc = NULL;
466defe8520SJames Wright 
467defe8520SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
468defe8520SJames Wright }
469926a6279SJames Wright 
470926a6279SJames Wright // Print information about the given simulation run
471926a6279SJames Wright PetscErrorCode PrintRunInfo(User user, Physics phys_ctx, ProblemData *problem, MPI_Comm comm) {
472926a6279SJames Wright   PetscFunctionBeginUser;
473926a6279SJames Wright   // Header and rank
474926a6279SJames Wright   char        host_name[PETSC_MAX_PATH_LEN];
475926a6279SJames Wright   PetscMPIInt rank, comm_size;
476926a6279SJames Wright   PetscCall(PetscGetHostName(host_name, sizeof host_name));
477926a6279SJames Wright   PetscCallMPI(MPI_Comm_rank(comm, &rank));
478926a6279SJames Wright   PetscCallMPI(MPI_Comm_size(comm, &comm_size));
479926a6279SJames Wright   PetscCall(PetscPrintf(comm,
480926a6279SJames Wright                         "\n-- Navier-Stokes solver - libCEED + PETSc --\n"
481926a6279SJames Wright                         "  MPI:\n"
482926a6279SJames Wright                         "    Host Name                          : %s\n"
483926a6279SJames Wright                         "    Total ranks                        : %d\n",
484926a6279SJames Wright                         host_name, comm_size));
485926a6279SJames Wright 
486926a6279SJames Wright   // Problem specific info
487926a6279SJames Wright   PetscCall(problem->print_info(problem, user->app_ctx));
488926a6279SJames Wright 
489926a6279SJames Wright   // libCEED
490926a6279SJames Wright   const char *used_resource;
491926a6279SJames Wright   CeedMemType mem_type_backend;
492926a6279SJames Wright   CeedGetResource(user->ceed, &used_resource);
493926a6279SJames Wright   CeedGetPreferredMemType(user->ceed, &mem_type_backend);
494926a6279SJames Wright   PetscCall(PetscPrintf(comm,
495926a6279SJames Wright                         "  libCEED:\n"
496926a6279SJames Wright                         "    libCEED Backend                    : %s\n"
497926a6279SJames Wright                         "    libCEED Backend MemType            : %s\n",
498926a6279SJames Wright                         used_resource, CeedMemTypes[mem_type_backend]));
499926a6279SJames Wright   // PETSc
500926a6279SJames Wright   char box_faces_str[PETSC_MAX_PATH_LEN] = "3,3,3";
501926a6279SJames Wright   if (problem->dim == 2) box_faces_str[3] = '\0';
502926a6279SJames Wright   PetscCall(PetscOptionsGetString(NULL, NULL, "-dm_plex_box_faces", box_faces_str, sizeof(box_faces_str), NULL));
503926a6279SJames Wright   MatType mat_type;
504926a6279SJames Wright   VecType vec_type;
505926a6279SJames Wright   PetscCall(DMGetMatType(user->dm, &mat_type));
506926a6279SJames Wright   PetscCall(DMGetVecType(user->dm, &vec_type));
507926a6279SJames Wright   PetscCall(PetscPrintf(comm,
508926a6279SJames Wright                         "  PETSc:\n"
509926a6279SJames Wright                         "    Box Faces                          : %s\n"
510926a6279SJames Wright                         "    DM MatType                         : %s\n"
511926a6279SJames Wright                         "    DM VecType                         : %s\n"
512926a6279SJames Wright                         "    Time Stepping Scheme               : %s\n",
513926a6279SJames Wright                         box_faces_str, mat_type, vec_type, phys_ctx->implicit ? "implicit" : "explicit"));
514926a6279SJames Wright   if (user->app_ctx->cont_steps) {
515926a6279SJames Wright     PetscCall(PetscPrintf(comm,
516926a6279SJames Wright                           "  Continue:\n"
517926a6279SJames Wright                           "    Filename:                          : %s\n"
518926a6279SJames Wright                           "    Step:                              : %" PetscInt_FMT "\n"
519926a6279SJames Wright                           "    Time:                              : %g\n",
520926a6279SJames Wright                           user->app_ctx->cont_file, user->app_ctx->cont_steps, user->app_ctx->cont_time));
521926a6279SJames Wright   }
522926a6279SJames Wright   // Mesh
523926a6279SJames Wright   const PetscInt num_comp_q = 5;
524926a6279SJames Wright   PetscInt       glob_dofs, owned_dofs, local_dofs;
525926a6279SJames Wright   const CeedInt  num_P = user->app_ctx->degree + 1, num_Q = num_P + user->app_ctx->q_extra;
526926a6279SJames Wright   // -- Get global size
527926a6279SJames Wright   PetscCall(DMGetGlobalVectorInfo(user->dm, &owned_dofs, &glob_dofs, NULL));
528926a6279SJames Wright   // -- Get local size
529926a6279SJames Wright   PetscCall(DMGetLocalVectorInfo(user->dm, &local_dofs, NULL, NULL));
530926a6279SJames Wright   PetscCall(PetscPrintf(comm,
531926a6279SJames Wright                         "  Mesh:\n"
532926a6279SJames Wright                         "    Number of 1D Basis Nodes (P)       : %" CeedInt_FMT "\n"
533926a6279SJames Wright                         "    Number of 1D Quadrature Points (Q) : %" CeedInt_FMT "\n"
534926a6279SJames Wright                         "    Global DoFs                        : %" PetscInt_FMT "\n"
535926a6279SJames Wright                         "    DoFs per node                      : %" PetscInt_FMT "\n"
536dfeb939dSJames Wright                         "    Global %" PetscInt_FMT "-DoF nodes                 : %" PetscInt_FMT "\n",
537dfeb939dSJames Wright                         num_P, num_Q, glob_dofs, num_comp_q, num_comp_q, glob_dofs / num_comp_q));
538926a6279SJames Wright   // -- Get Partition Statistics
539926a6279SJames Wright   PetscCall(PetscPrintf(comm, "  Partition:                             (min,max,median,max/median)\n"));
540926a6279SJames Wright   {
541926a6279SJames Wright     PetscInt *gather_buffer = NULL;
542dfeb939dSJames Wright     PetscInt  part_owned_dofs[3], part_local_dofs[3], part_boundary_dofs[3], part_neighbors[3];
543926a6279SJames Wright     PetscInt  median_index = comm_size % 2 ? comm_size / 2 : comm_size / 2 - 1;
544926a6279SJames Wright     if (!rank) PetscCall(PetscMalloc1(comm_size, &gather_buffer));
545926a6279SJames Wright 
546dfeb939dSJames Wright     PetscCallMPI(MPI_Gather(&owned_dofs, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
547926a6279SJames Wright     if (!rank) {
548926a6279SJames Wright       PetscCall(PetscSortInt(comm_size, gather_buffer));
549926a6279SJames Wright       part_owned_dofs[0]             = gather_buffer[0];              // min
550926a6279SJames Wright       part_owned_dofs[1]             = gather_buffer[comm_size - 1];  // max
551926a6279SJames Wright       part_owned_dofs[2]             = gather_buffer[median_index];   // median
552926a6279SJames Wright       PetscReal part_owned_dof_ratio = (PetscReal)part_owned_dofs[1] / (PetscReal)part_owned_dofs[2];
553dfeb939dSJames Wright       PetscCall(PetscPrintf(
554dfeb939dSJames Wright           comm, "    Global Vector %" PetscInt_FMT "-DoF nodes          : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n", num_comp_q,
555926a6279SJames Wright           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));
556926a6279SJames Wright     }
557926a6279SJames Wright 
558dfeb939dSJames Wright     PetscCallMPI(MPI_Gather(&local_dofs, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
559926a6279SJames Wright     if (!rank) {
560926a6279SJames Wright       PetscCall(PetscSortInt(comm_size, gather_buffer));
561926a6279SJames Wright       part_local_dofs[0]             = gather_buffer[0];              // min
562926a6279SJames Wright       part_local_dofs[1]             = gather_buffer[comm_size - 1];  // max
563926a6279SJames Wright       part_local_dofs[2]             = gather_buffer[median_index];   // median
564926a6279SJames Wright       PetscReal part_local_dof_ratio = (PetscReal)part_local_dofs[1] / (PetscReal)part_local_dofs[2];
565dfeb939dSJames Wright       PetscCall(PetscPrintf(
566dfeb939dSJames Wright           comm, "    Local Vector %" PetscInt_FMT "-DoF nodes           : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n", num_comp_q,
567926a6279SJames Wright           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));
568926a6279SJames Wright     }
569926a6279SJames Wright 
570*45abf96eSJames Wright     if (comm_size != 1) {
571dfeb939dSJames Wright       PetscInt num_remote_roots_total = 0, num_remote_leaves_total = 0, num_ghost_interface_ranks = 0, num_owned_interface_ranks = 0;
572926a6279SJames Wright       {
573926a6279SJames Wright         PetscSF            sf;
574dfeb939dSJames Wright         PetscInt           nrranks, niranks;
575dfeb939dSJames Wright         const PetscInt    *roffset, *rmine, *rremote, *ioffset, *irootloc;
576dfeb939dSJames Wright         const PetscMPIInt *rranks, *iranks;
577926a6279SJames Wright         PetscCall(DMGetSectionSF(user->dm, &sf));
578926a6279SJames Wright         PetscCall(PetscSFGetRootRanks(sf, &nrranks, &rranks, &roffset, &rmine, &rremote));
579dfeb939dSJames Wright         PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, &ioffset, &irootloc));
580926a6279SJames Wright         for (PetscInt i = 0; i < nrranks; i++) {
581926a6279SJames Wright           if (rranks[i] == rank) continue;  // Ignore same-part global->local transfers
582926a6279SJames Wright           num_remote_roots_total += roffset[i + 1] - roffset[i];
583dfeb939dSJames Wright           num_ghost_interface_ranks++;
584dfeb939dSJames Wright         }
585dfeb939dSJames Wright         for (PetscInt i = 0; i < niranks; i++) {
586dfeb939dSJames Wright           if (iranks[i] == rank) continue;
587dfeb939dSJames Wright           num_remote_leaves_total += ioffset[i + 1] - ioffset[i];
588dfeb939dSJames Wright           num_owned_interface_ranks++;
589926a6279SJames Wright         }
590926a6279SJames Wright       }
591dfeb939dSJames Wright       PetscCallMPI(MPI_Gather(&num_remote_roots_total, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
592926a6279SJames Wright       if (!rank) {
593926a6279SJames Wright         PetscCall(PetscSortInt(comm_size, gather_buffer));
594dfeb939dSJames Wright         part_boundary_dofs[0]           = gather_buffer[0];              // min
595dfeb939dSJames Wright         part_boundary_dofs[1]           = gather_buffer[comm_size - 1];  // max
596dfeb939dSJames Wright         part_boundary_dofs[2]           = gather_buffer[median_index];   // median
597dfeb939dSJames Wright         PetscReal part_shared_dof_ratio = (PetscReal)part_boundary_dofs[1] / (PetscReal)part_boundary_dofs[2];
598dfeb939dSJames Wright         PetscCall(PetscPrintf(
599*45abf96eSJames Wright             comm, "    Ghost Interface %" PetscInt_FMT "-DoF nodes        : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
600*45abf96eSJames Wright             num_comp_q, part_boundary_dofs[0] / num_comp_q, part_boundary_dofs[1] / num_comp_q, part_boundary_dofs[2] / num_comp_q,
601*45abf96eSJames Wright             part_shared_dof_ratio));
602dfeb939dSJames Wright       }
603dfeb939dSJames Wright 
604dfeb939dSJames Wright       PetscCallMPI(MPI_Gather(&num_ghost_interface_ranks, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
605dfeb939dSJames Wright       if (!rank) {
606dfeb939dSJames Wright         PetscCall(PetscSortInt(comm_size, gather_buffer));
607dfeb939dSJames Wright         part_neighbors[0]              = gather_buffer[0];              // min
608dfeb939dSJames Wright         part_neighbors[1]              = gather_buffer[comm_size - 1];  // max
609dfeb939dSJames Wright         part_neighbors[2]              = gather_buffer[median_index];   // median
610dfeb939dSJames Wright         PetscReal part_neighbors_ratio = (PetscReal)part_neighbors[1] / (PetscReal)part_neighbors[2];
611dfeb939dSJames Wright         PetscCall(PetscPrintf(comm, "    Ghost Interface Ranks              : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
612dfeb939dSJames Wright                               part_neighbors[0], part_neighbors[1], part_neighbors[2], part_neighbors_ratio));
613dfeb939dSJames Wright       }
614dfeb939dSJames Wright 
615dfeb939dSJames Wright       PetscCallMPI(MPI_Gather(&num_remote_leaves_total, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
616dfeb939dSJames Wright       if (!rank) {
617dfeb939dSJames Wright         PetscCall(PetscSortInt(comm_size, gather_buffer));
618dfeb939dSJames Wright         part_boundary_dofs[0]           = gather_buffer[0];              // min
619dfeb939dSJames Wright         part_boundary_dofs[1]           = gather_buffer[comm_size - 1];  // max
620dfeb939dSJames Wright         part_boundary_dofs[2]           = gather_buffer[median_index];   // median
621dfeb939dSJames Wright         PetscReal part_shared_dof_ratio = (PetscReal)part_boundary_dofs[1] / (PetscReal)part_boundary_dofs[2];
622dfeb939dSJames Wright         PetscCall(PetscPrintf(
623*45abf96eSJames Wright             comm, "    Owned Interface %" PetscInt_FMT "-DoF nodes        : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
624*45abf96eSJames Wright             num_comp_q, part_boundary_dofs[0] / num_comp_q, part_boundary_dofs[1] / num_comp_q, part_boundary_dofs[2] / num_comp_q,
625*45abf96eSJames Wright             part_shared_dof_ratio));
626dfeb939dSJames Wright       }
627dfeb939dSJames Wright 
628dfeb939dSJames Wright       PetscCallMPI(MPI_Gather(&num_owned_interface_ranks, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
629dfeb939dSJames Wright       if (!rank) {
630dfeb939dSJames Wright         PetscCall(PetscSortInt(comm_size, gather_buffer));
631dfeb939dSJames Wright         part_neighbors[0]              = gather_buffer[0];              // min
632dfeb939dSJames Wright         part_neighbors[1]              = gather_buffer[comm_size - 1];  // max
633dfeb939dSJames Wright         part_neighbors[2]              = gather_buffer[median_index];   // median
634dfeb939dSJames Wright         PetscReal part_neighbors_ratio = (PetscReal)part_neighbors[1] / (PetscReal)part_neighbors[2];
635dfeb939dSJames Wright         PetscCall(PetscPrintf(comm, "    Owned Interface Ranks              : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
636dfeb939dSJames Wright                               part_neighbors[0], part_neighbors[1], part_neighbors[2], part_neighbors_ratio));
637926a6279SJames Wright       }
638*45abf96eSJames Wright     }
639926a6279SJames Wright 
640926a6279SJames Wright     if (!rank) PetscCall(PetscFree(gather_buffer));
641926a6279SJames Wright   }
642926a6279SJames Wright 
643926a6279SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
644926a6279SJames Wright }
645