xref: /honee/src/misc.c (revision 25125139fdc4964835f5c559770e5027599868dc)
1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3a515125bSLeila Ghaffari 
4a515125bSLeila Ghaffari /// @file
5a515125bSLeila Ghaffari /// Miscellaneous utility functions
6a515125bSLeila Ghaffari 
7e419654dSJeremy L Thompson #include <ceed.h>
8e419654dSJeremy L Thompson #include <petscdm.h>
9926a6279SJames Wright #include <petscsf.h>
10e419654dSJeremy L Thompson #include <petscts.h>
11e419654dSJeremy L Thompson 
12149fb536SJames Wright #include <navierstokes.h>
13a515125bSLeila Ghaffari 
14e3663b90SJames Wright PetscErrorCode ICs_FixMultiplicity(DM dm, Honee honee, Vec Q_loc, Vec Q, CeedScalar time) {
150c373b74SJames Wright   Ceed         ceed = honee->ceed;
16b2948607SJames Wright   CeedVector   mult_vec;
17b2948607SJames Wright   PetscMemType m_mem_type;
18b2948607SJames Wright   Vec          Multiplicity, Multiplicity_loc;
19b2948607SJames Wright 
20a515125bSLeila Ghaffari   PetscFunctionBeginUser;
21e3663b90SJames Wright   if (honee->phys->ics_time_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(honee->op_ics_ctx->op, honee->phys->ics_time_label, &time));
22e3663b90SJames Wright   PetscCall(ApplyCeedOperatorLocalToGlobal(NULL, Q, honee->op_ics_ctx));
23a515125bSLeila Ghaffari 
24e3663b90SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionCreateVector(honee->elem_restr_q, &mult_vec, NULL));
25a515125bSLeila Ghaffari 
26a515125bSLeila Ghaffari   // -- Get multiplicity
27b2948607SJames Wright   PetscCall(DMGetLocalVector(dm, &Multiplicity_loc));
28a7dac1d5SJames Wright   PetscCall(VecPetscToCeed(Multiplicity_loc, &m_mem_type, mult_vec));
29e3663b90SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetMultiplicity(honee->elem_restr_q, mult_vec));
30a7dac1d5SJames Wright   PetscCall(VecCeedToPetsc(mult_vec, m_mem_type, Multiplicity_loc));
31a515125bSLeila Ghaffari 
32b2948607SJames Wright   PetscCall(DMGetGlobalVector(dm, &Multiplicity));
33b2948607SJames Wright   PetscCall(VecZeroEntries(Multiplicity));
34b2948607SJames Wright   PetscCall(DMLocalToGlobal(dm, Multiplicity_loc, ADD_VALUES, Multiplicity));
35a515125bSLeila Ghaffari 
36a515125bSLeila Ghaffari   // -- Fix multiplicity
37b2948607SJames Wright   PetscCall(VecPointwiseDivide(Q, Q, Multiplicity));
38b2948607SJames Wright   PetscCall(VecPointwiseDivide(Q_loc, Q_loc, Multiplicity_loc));
39a515125bSLeila Ghaffari 
40b2948607SJames Wright   PetscCall(DMRestoreLocalVector(dm, &Multiplicity_loc));
41b2948607SJames Wright   PetscCall(DMRestoreGlobalVector(dm, &Multiplicity));
42b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&mult_vec));
43d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
44a515125bSLeila Ghaffari }
45a515125bSLeila Ghaffari 
46c56e8d5bSJames Wright // Record boundary values from initial condition
47c56e8d5bSJames Wright PetscErrorCode SetBCsFromICs(DM dm, Vec Q, Vec Q_loc) {
48c56e8d5bSJames Wright   PetscFunctionBeginUser;
49313f2f1eSJames Wright   {  // Capture initial condition values in Qbc
50313f2f1eSJames Wright     Vec Qbc;
51313f2f1eSJames Wright 
52c56e8d5bSJames Wright     PetscCall(DMGetNamedLocalVector(dm, "Qbc", &Qbc));
53c56e8d5bSJames Wright     PetscCall(VecCopy(Q_loc, Qbc));
54c56e8d5bSJames Wright     PetscCall(VecZeroEntries(Q_loc));
55c56e8d5bSJames Wright     PetscCall(DMGlobalToLocal(dm, Q, INSERT_VALUES, Q_loc));
56c56e8d5bSJames Wright     PetscCall(VecAXPY(Qbc, -1., Q_loc));
57c56e8d5bSJames Wright     PetscCall(DMRestoreNamedLocalVector(dm, "Qbc", &Qbc));
58313f2f1eSJames Wright   }
59c56e8d5bSJames Wright   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_FromICs));
60c56e8d5bSJames Wright 
61313f2f1eSJames Wright   {  // Set boundary mask to zero out essential BCs
62313f2f1eSJames Wright     Vec boundary_mask, ones;
63313f2f1eSJames Wright 
64c56e8d5bSJames Wright     PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask));
65313f2f1eSJames Wright     PetscCall(DMGetGlobalVector(dm, &ones));
66c56e8d5bSJames Wright     PetscCall(VecZeroEntries(boundary_mask));
67313f2f1eSJames Wright     PetscCall(VecSet(ones, 1.0));
68313f2f1eSJames Wright     PetscCall(DMGlobalToLocal(dm, ones, INSERT_VALUES, boundary_mask));
69c56e8d5bSJames Wright     PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask));
70313f2f1eSJames Wright     PetscCall(DMRestoreGlobalVector(dm, &ones));
71313f2f1eSJames Wright   }
72c56e8d5bSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
73c56e8d5bSJames Wright }
74c56e8d5bSJames Wright 
75c56e8d5bSJames Wright PetscErrorCode DMPlexInsertBoundaryValues_FromICs(DM dm, PetscBool insert_essential, Vec Q_loc, PetscReal time, Vec face_geom_FVM, Vec cell_geom_FVM,
762b916ea7SJeremy L Thompson                                                   Vec grad_FVM) {
779d437337SJames Wright   Vec Qbc, boundary_mask;
78a515125bSLeila Ghaffari 
7906f41313SJames Wright   PetscFunctionBeginUser;
802eb7bf1fSJames Wright   // Mask (zero) Strong BC entries
819d437337SJames Wright   PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask));
829d437337SJames Wright   PetscCall(VecPointwiseMult(Q_loc, Q_loc, boundary_mask));
839d437337SJames Wright   PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask));
849d437337SJames Wright 
852b916ea7SJeremy L Thompson   PetscCall(DMGetNamedLocalVector(dm, "Qbc", &Qbc));
862b916ea7SJeremy L Thompson   PetscCall(VecAXPY(Q_loc, 1., Qbc));
872b916ea7SJeremy L Thompson   PetscCall(DMRestoreNamedLocalVector(dm, "Qbc", &Qbc));
88d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
89a515125bSLeila Ghaffari }
90a515125bSLeila Ghaffari 
91a515125bSLeila Ghaffari // Compare reference solution values with current test run for CI
92c56e8d5bSJames Wright PetscErrorCode RegressionTest(AppCtx app_ctx, Vec Q) {
93a515125bSLeila Ghaffari   Vec         Qref;
94a515125bSLeila Ghaffari   PetscViewer viewer;
95a515125bSLeila Ghaffari   PetscReal   error, Qrefnorm;
96e7754af5SKenneth E. Jansen   MPI_Comm    comm = PetscObjectComm((PetscObject)Q);
97a515125bSLeila Ghaffari 
9806f41313SJames Wright   PetscFunctionBeginUser;
99a515125bSLeila Ghaffari   // Read reference file
1002b916ea7SJeremy L Thompson   PetscCall(VecDuplicate(Q, &Qref));
1014c07ec22SJames Wright   PetscCheck(strcmp(app_ctx->test_file_path, "") != 0, comm, PETSC_ERR_FILE_READ, "File for regression test not given");
102e7754af5SKenneth E. Jansen   PetscCall(PetscViewerBinaryOpen(comm, app_ctx->test_file_path, FILE_MODE_READ, &viewer));
103360b37c9SJames Wright   PetscCall(HoneeLoadBinaryVec(viewer, Qref, NULL, NULL));
104a515125bSLeila Ghaffari 
105a515125bSLeila Ghaffari   // Compute error with respect to reference solution
1062b916ea7SJeremy L Thompson   PetscCall(VecAXPY(Q, -1.0, Qref));
1072b916ea7SJeremy L Thompson   PetscCall(VecNorm(Qref, NORM_MAX, &Qrefnorm));
1082b916ea7SJeremy L Thompson   PetscCall(VecScale(Q, 1. / Qrefnorm));
1092b916ea7SJeremy L Thompson   PetscCall(VecNorm(Q, NORM_MAX, &error));
110a515125bSLeila Ghaffari 
111a515125bSLeila Ghaffari   // Check error
112a515125bSLeila Ghaffari   if (error > app_ctx->test_tol) {
1132b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test failed with error norm %g\n", (double)error));
114a515125bSLeila Ghaffari   }
115a515125bSLeila Ghaffari 
116a515125bSLeila Ghaffari   // Cleanup
1172b916ea7SJeremy L Thompson   PetscCall(PetscViewerDestroy(&viewer));
1182b916ea7SJeremy L Thompson   PetscCall(VecDestroy(&Qref));
119d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
120a515125bSLeila Ghaffari }
121a515125bSLeila Ghaffari 
122a515125bSLeila Ghaffari // Get error for problems with exact solutions
123e3663b90SJames Wright PetscErrorCode PrintError(DM dm, Honee honee, Vec Q, PetscScalar final_time) {
124a515125bSLeila Ghaffari   PetscInt  loc_nodes;
125a515125bSLeila Ghaffari   Vec       Q_exact, Q_exact_loc;
126a515125bSLeila Ghaffari   PetscReal rel_error, norm_error, norm_exact;
127a515125bSLeila Ghaffari 
12806f41313SJames Wright   PetscFunctionBeginUser;
129a515125bSLeila Ghaffari   // Get exact solution at final time
130b2948607SJames Wright   PetscCall(DMGetGlobalVector(dm, &Q_exact));
1312b916ea7SJeremy L Thompson   PetscCall(DMGetLocalVector(dm, &Q_exact_loc));
1322b916ea7SJeremy L Thompson   PetscCall(VecGetSize(Q_exact_loc, &loc_nodes));
133e3663b90SJames Wright   PetscCall(ICs_FixMultiplicity(dm, honee, Q_exact_loc, Q_exact, final_time));
134a515125bSLeila Ghaffari 
135a515125bSLeila Ghaffari   // Get |exact solution - obtained solution|
1362b916ea7SJeremy L Thompson   PetscCall(VecNorm(Q_exact, NORM_1, &norm_exact));
1372b916ea7SJeremy L Thompson   PetscCall(VecAXPY(Q, -1.0, Q_exact));
1382b916ea7SJeremy L Thompson   PetscCall(VecNorm(Q, NORM_1, &norm_error));
139a515125bSLeila Ghaffari 
140a515125bSLeila Ghaffari   rel_error = norm_error / norm_exact;
1412b916ea7SJeremy L Thompson   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Relative Error: %g\n", (double)rel_error));
1422b916ea7SJeremy L Thompson   PetscCall(DMRestoreLocalVector(dm, &Q_exact_loc));
143b2948607SJames Wright   PetscCall(DMRestoreGlobalVector(dm, &Q_exact));
144d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
145a515125bSLeila Ghaffari }
146a515125bSLeila Ghaffari 
147a515125bSLeila Ghaffari // Post-processing
148e3663b90SJames Wright PetscErrorCode PostProcess(TS ts, DM dm, ProblemData problem, Honee honee, Vec Q, PetscScalar final_time) {
149a515125bSLeila Ghaffari   PetscInt          steps;
150f0784ed3SJed Brown   TSConvergedReason reason;
151a515125bSLeila Ghaffari 
15206f41313SJames Wright   PetscFunctionBeginUser;
153a515125bSLeila Ghaffari   // Print relative error
1540c373b74SJames Wright   if (problem->compute_exact_solution_error && honee->app_ctx->test_type == TESTTYPE_NONE) {
155e3663b90SJames Wright     PetscCall(PrintError(dm, honee, Q, final_time));
156a515125bSLeila Ghaffari   }
157a515125bSLeila Ghaffari 
158a515125bSLeila Ghaffari   // Print final time and number of steps
1592b916ea7SJeremy L Thompson   PetscCall(TSGetStepNumber(ts, &steps));
160f0784ed3SJed Brown   PetscCall(TSGetConvergedReason(ts, &reason));
1610c373b74SJames Wright   if (honee->app_ctx->test_type == TESTTYPE_NONE) {
162f0784ed3SJed Brown     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time integrator %s on time step %" PetscInt_FMT " with final time %g\n", TSConvergedReasons[reason],
163f0784ed3SJed Brown                           steps, (double)final_time));
164a515125bSLeila Ghaffari   }
165a515125bSLeila Ghaffari 
166a515125bSLeila Ghaffari   // Output numerical values from command line
1672b916ea7SJeremy L Thompson   PetscCall(VecViewFromOptions(Q, NULL, "-vec_view"));
168a515125bSLeila Ghaffari 
169a515125bSLeila Ghaffari   // Compare reference solution values with current test run for CI
1700c373b74SJames Wright   if (honee->app_ctx->test_type == TESTTYPE_SOLVER) {
1710c373b74SJames Wright     PetscCall(RegressionTest(honee->app_ctx, Q));
172a515125bSLeila Ghaffari   }
173d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
174a515125bSLeila Ghaffari }
175a515125bSLeila Ghaffari 
17615a3537eSJed Brown // Free a plain data context that was allocated using PETSc; returning libCEED error codes
17715a3537eSJed Brown int FreeContextPetsc(void *data) {
1782b916ea7SJeremy L Thompson   if (PetscFree(data)) return CeedError(NULL, CEED_ERROR_ACCESS, "PetscFree failed");
17915a3537eSJed Brown   return CEED_ERROR_SUCCESS;
18015a3537eSJed Brown }
1819f59f36eSJames Wright 
182457a5831SJames Wright PetscErrorCode NodalProjectionDataDestroy(NodalProjectionData context) {
183457a5831SJames Wright   PetscFunctionBeginUser;
184d949ddfcSJames Wright   if (context == NULL) PetscFunctionReturn(PETSC_SUCCESS);
185457a5831SJames Wright   PetscCall(DMDestroy(&context->dm));
186457a5831SJames Wright   PetscCall(KSPDestroy(&context->ksp));
187457a5831SJames Wright   PetscCall(OperatorApplyContextDestroy(context->l2_rhs_ctx));
188457a5831SJames Wright   PetscCall(PetscFree(context));
189d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
190457a5831SJames Wright }
191457a5831SJames Wright 
19259572072SJames Wright /**
19359572072SJames Wright    @brief Sets the value of an option if it is not already set
19459572072SJames Wright 
19559572072SJames Wright    @param[in,out] options `PetscOptions` database, or `NULL` for default global database
19659572072SJames Wright    @param[in]     name    Name of the option (with `-` prepended to it)
19759572072SJames Wright    @param[in]     value   Value to set the option to
19859572072SJames Wright **/
19959572072SJames Wright PetscErrorCode HoneeOptionsSetValueDefault(PetscOptions options, const char name[], const char value[]) {
20059572072SJames Wright   PetscBool has_option;
20159572072SJames Wright 
20259572072SJames Wright   PetscFunctionBeginUser;
20359572072SJames Wright   PetscCall(PetscOptionsHasName(options, NULL, name, &has_option));
20459572072SJames Wright   if (!has_option) PetscCall(PetscOptionsSetValue(options, name, value));
20559572072SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
20659572072SJames Wright }
20759572072SJames Wright 
208*25125139SJames Wright PetscErrorCode HoneeCalculateDomainSize(Honee honee, PetscScalar *volume) {
209*25125139SJames Wright   DM                   dm   = honee->dm, dm_coord;
210*25125139SJames Wright   Ceed                 ceed = honee->ceed;
211*25125139SJames Wright   CeedQFunction        qf_mass;
212*25125139SJames Wright   CeedOperator         op_mass;
213*25125139SJames Wright   OperatorApplyContext op_mass_ctx;
214*25125139SJames Wright   CeedElemRestriction  elem_restr_qd;
215*25125139SJames Wright   CeedVector           qdata;
216*25125139SJames Wright   CeedInt              q_data_size, num_comps_x;
217*25125139SJames Wright   DMLabel              domain_label = 0;
218*25125139SJames Wright   PetscInt             label_value  = 0;
219*25125139SJames Wright   Vec                  u, v;
220*25125139SJames Wright 
221*25125139SJames Wright   PetscFunctionBeginUser;
222*25125139SJames Wright   PetscCall(DMGetCoordinateDM(dm, &dm_coord));
223*25125139SJames Wright   PetscCall(QDataGet(ceed, dm, domain_label, label_value, honee->elem_restr_x, honee->basis_x, honee->x_coord, &elem_restr_qd, &qdata, &q_data_size));
224*25125139SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(honee->elem_restr_x, &num_comps_x));
225*25125139SJames Wright 
226*25125139SJames Wright   PetscCall(HoneeMassQFunctionCreate(ceed, num_comps_x, q_data_size, &qf_mass));
227*25125139SJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass));
228*25125139SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "u", honee->elem_restr_x, honee->basis_x, CEED_VECTOR_ACTIVE));
229*25125139SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "qdata", elem_restr_qd, CEED_BASIS_NONE, qdata));
230*25125139SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "v", honee->elem_restr_x, honee->basis_x, CEED_VECTOR_ACTIVE));
231*25125139SJames Wright 
232*25125139SJames Wright   PetscCall(OperatorApplyContextCreate(NULL, dm_coord, ceed, op_mass, NULL, NULL, NULL, NULL, &op_mass_ctx));
233*25125139SJames Wright   PetscCall(CeedOperatorCreateLocalVecs(op_mass, DMReturnVecType(dm), PETSC_COMM_SELF, &u, NULL));
234*25125139SJames Wright   PetscCall(DMCreateGlobalVector(dm_coord, &v));
235*25125139SJames Wright   PetscCall(VecSet(u, 1.));
236*25125139SJames Wright   PetscCall(ApplyCeedOperatorLocalToGlobal(u, v, op_mass_ctx));
237*25125139SJames Wright   PetscCall(VecSum(v, volume));
238*25125139SJames Wright   *volume /= num_comps_x;  // Correct for number of components != 1
239*25125139SJames Wright 
240*25125139SJames Wright   PetscCall(VecDestroy(&u));
241*25125139SJames Wright   PetscCall(VecDestroy(&v));
242*25125139SJames Wright   PetscCall(OperatorApplyContextDestroy(op_mass_ctx));
243*25125139SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd));
244*25125139SJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&qdata));
245*25125139SJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass));
246*25125139SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_mass));
247*25125139SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
248*25125139SJames Wright }
249*25125139SJames Wright 
250926a6279SJames Wright // Print information about the given simulation run
2510c373b74SJames Wright PetscErrorCode PrintRunInfo(Honee honee, Physics phys_ctx, ProblemData problem, TS ts) {
2520c373b74SJames Wright   Ceed     ceed = honee->ceed;
2533678fae3SJames Wright   MPI_Comm comm = PetscObjectComm((PetscObject)ts);
2543678fae3SJames Wright 
255926a6279SJames Wright   PetscFunctionBeginUser;
256926a6279SJames Wright   // Header and rank
257926a6279SJames Wright   char        host_name[PETSC_MAX_PATH_LEN];
258926a6279SJames Wright   PetscMPIInt rank, comm_size;
259926a6279SJames Wright   PetscCall(PetscGetHostName(host_name, sizeof host_name));
260926a6279SJames Wright   PetscCallMPI(MPI_Comm_rank(comm, &rank));
261926a6279SJames Wright   PetscCallMPI(MPI_Comm_size(comm, &comm_size));
262926a6279SJames Wright   PetscCall(PetscPrintf(comm,
26315747f0fSJames Wright                         "\n-- HONEE - High-Order Navier-stokes Equation Evaluator --\n"
264926a6279SJames Wright                         "  MPI:\n"
265926a6279SJames Wright                         "    Host Name                          : %s\n"
266926a6279SJames Wright                         "    Total ranks                        : %d\n",
267926a6279SJames Wright                         host_name, comm_size));
268926a6279SJames Wright 
269926a6279SJames Wright   // Problem specific info
2700c373b74SJames Wright   PetscCall(problem->print_info(honee, problem, honee->app_ctx));
271926a6279SJames Wright 
272926a6279SJames Wright   // libCEED
273926a6279SJames Wright   const char *used_resource;
274926a6279SJames Wright   CeedMemType mem_type_backend;
2750c373b74SJames Wright   PetscCallCeed(ceed, CeedGetResource(honee->ceed, &used_resource));
2760c373b74SJames Wright   PetscCallCeed(ceed, CeedGetPreferredMemType(honee->ceed, &mem_type_backend));
277926a6279SJames Wright   PetscCall(PetscPrintf(comm,
278926a6279SJames Wright                         "  libCEED:\n"
279926a6279SJames Wright                         "    libCEED Backend                    : %s\n"
280926a6279SJames Wright                         "    libCEED Backend MemType            : %s\n",
281926a6279SJames Wright                         used_resource, CeedMemTypes[mem_type_backend]));
282926a6279SJames Wright   // PETSc
28366d54740SJames Wright   {
2843678fae3SJames Wright     VecType  vec_type;
285926a6279SJames Wright     char     box_faces_str[PETSC_MAX_PATH_LEN] = "3,3,3";
28666d54740SJames Wright     PetscInt dim;
28766d54740SJames Wright 
2880c373b74SJames Wright     PetscCall(DMGetDimension(honee->dm, &dim));
28966d54740SJames Wright     if (dim == 2) box_faces_str[3] = '\0';
290926a6279SJames Wright     PetscCall(PetscOptionsGetString(NULL, NULL, "-dm_plex_box_faces", box_faces_str, sizeof(box_faces_str), NULL));
2910c373b74SJames Wright     PetscCall(DMGetVecType(honee->dm, &vec_type));
292926a6279SJames Wright     PetscCall(PetscPrintf(comm,
293926a6279SJames Wright                           "  PETSc:\n"
294926a6279SJames Wright                           "    Box Faces                          : %s\n"
295926a6279SJames Wright                           "    DM VecType                         : %s\n"
296926a6279SJames Wright                           "    Time Stepping Scheme               : %s\n",
2973678fae3SJames Wright                           box_faces_str, vec_type, phys_ctx->implicit ? "implicit" : "explicit"));
29866d54740SJames Wright   }
2993678fae3SJames Wright   {
3003678fae3SJames Wright     char           pmat_type_str[PETSC_MAX_PATH_LEN];
3013678fae3SJames Wright     MatType        amat_type, pmat_type;
3023678fae3SJames Wright     Mat            Amat, Pmat;
3033678fae3SJames Wright     TSIJacobianFn *ijacob_function;
3043678fae3SJames Wright 
3053678fae3SJames Wright     PetscCall(TSGetIJacobian(ts, &Amat, &Pmat, &ijacob_function, NULL));
3063678fae3SJames Wright     PetscCall(MatGetType(Amat, &amat_type));
3073678fae3SJames Wright     PetscCall(MatGetType(Pmat, &pmat_type));
3083678fae3SJames Wright 
3093678fae3SJames Wright     PetscCall(PetscStrncpy(pmat_type_str, pmat_type, sizeof(pmat_type_str)));
3103678fae3SJames Wright     if (!strcmp(pmat_type, MATCEED)) {
3113678fae3SJames Wright       MatType pmat_coo_type;
3123678fae3SJames Wright       char    pmat_coo_type_str[PETSC_MAX_PATH_LEN];
3133678fae3SJames Wright 
3143678fae3SJames Wright       PetscCall(MatCeedGetCOOMatType(Pmat, &pmat_coo_type));
3153678fae3SJames Wright       PetscCall(PetscSNPrintf(pmat_coo_type_str, sizeof(pmat_coo_type_str), " (COO MatType: %s)", pmat_coo_type));
3163678fae3SJames Wright       PetscCall(PetscStrlcat(pmat_type_str, pmat_coo_type_str, sizeof(pmat_type_str)));
3173678fae3SJames Wright     }
3183678fae3SJames Wright     if (ijacob_function) {
3193678fae3SJames Wright       PetscCall(PetscPrintf(comm,
3203678fae3SJames Wright                             "    IJacobian A MatType                : %s\n"
3213678fae3SJames Wright                             "    IJacobian P MatType                : %s\n",
3223678fae3SJames Wright                             amat_type, pmat_type_str));
3233678fae3SJames Wright     }
3243678fae3SJames Wright   }
325481d14cbSJames Wright   if (honee->app_ctx->use_continue_file) {
326926a6279SJames Wright     PetscCall(PetscPrintf(comm,
327926a6279SJames Wright                           "  Continue:\n"
328926a6279SJames Wright                           "    Filename:                          : %s\n"
329926a6279SJames Wright                           "    Step:                              : %" PetscInt_FMT "\n"
330926a6279SJames Wright                           "    Time:                              : %g\n",
3310c373b74SJames Wright                           honee->app_ctx->cont_file, honee->app_ctx->cont_steps, honee->app_ctx->cont_time));
332926a6279SJames Wright   }
333926a6279SJames Wright   // Mesh
334926a6279SJames Wright   const PetscInt num_comp_q = 5;
335926a6279SJames Wright   PetscInt       glob_dofs, owned_dofs, local_dofs;
3360c373b74SJames Wright   const CeedInt  num_P = honee->app_ctx->degree + 1, num_Q = num_P + honee->app_ctx->q_extra;
3370c373b74SJames Wright   PetscCall(DMGetGlobalVectorInfo(honee->dm, &owned_dofs, &glob_dofs, NULL));
3380c373b74SJames Wright   PetscCall(DMGetLocalVectorInfo(honee->dm, &local_dofs, NULL, NULL));
339926a6279SJames Wright   PetscCall(PetscPrintf(comm,
340926a6279SJames Wright                         "  Mesh:\n"
341926a6279SJames Wright                         "    Number of 1D Basis Nodes (P)       : %" CeedInt_FMT "\n"
342926a6279SJames Wright                         "    Number of 1D Quadrature Points (Q) : %" CeedInt_FMT "\n"
343926a6279SJames Wright                         "    Global DoFs                        : %" PetscInt_FMT "\n"
344926a6279SJames Wright                         "    DoFs per node                      : %" PetscInt_FMT "\n"
345dfeb939dSJames Wright                         "    Global %" PetscInt_FMT "-DoF nodes                 : %" PetscInt_FMT "\n",
346dfeb939dSJames Wright                         num_P, num_Q, glob_dofs, num_comp_q, num_comp_q, glob_dofs / num_comp_q));
347926a6279SJames Wright   // -- Get Partition Statistics
348926a6279SJames Wright   PetscCall(PetscPrintf(comm, "  Partition:                             (min,max,median,max/median)\n"));
349926a6279SJames Wright   {
350926a6279SJames Wright     PetscInt *gather_buffer = NULL;
351dfeb939dSJames Wright     PetscInt  part_owned_dofs[3], part_local_dofs[3], part_boundary_dofs[3], part_neighbors[3];
352926a6279SJames Wright     PetscInt  median_index = comm_size % 2 ? comm_size / 2 : comm_size / 2 - 1;
353926a6279SJames Wright     if (!rank) PetscCall(PetscMalloc1(comm_size, &gather_buffer));
354926a6279SJames Wright 
355dfeb939dSJames Wright     PetscCallMPI(MPI_Gather(&owned_dofs, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
356926a6279SJames Wright     if (!rank) {
357926a6279SJames Wright       PetscCall(PetscSortInt(comm_size, gather_buffer));
358926a6279SJames Wright       part_owned_dofs[0]             = gather_buffer[0];              // min
359926a6279SJames Wright       part_owned_dofs[1]             = gather_buffer[comm_size - 1];  // max
360926a6279SJames Wright       part_owned_dofs[2]             = gather_buffer[median_index];   // median
361926a6279SJames Wright       PetscReal part_owned_dof_ratio = (PetscReal)part_owned_dofs[1] / (PetscReal)part_owned_dofs[2];
362dfeb939dSJames Wright       PetscCall(PetscPrintf(
363dfeb939dSJames Wright           comm, "    Global Vector %" PetscInt_FMT "-DoF nodes          : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n", num_comp_q,
364926a6279SJames 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));
365926a6279SJames Wright     }
366926a6279SJames Wright 
367dfeb939dSJames Wright     PetscCallMPI(MPI_Gather(&local_dofs, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
368926a6279SJames Wright     if (!rank) {
369926a6279SJames Wright       PetscCall(PetscSortInt(comm_size, gather_buffer));
370926a6279SJames Wright       part_local_dofs[0]             = gather_buffer[0];              // min
371926a6279SJames Wright       part_local_dofs[1]             = gather_buffer[comm_size - 1];  // max
372926a6279SJames Wright       part_local_dofs[2]             = gather_buffer[median_index];   // median
373926a6279SJames Wright       PetscReal part_local_dof_ratio = (PetscReal)part_local_dofs[1] / (PetscReal)part_local_dofs[2];
374dfeb939dSJames Wright       PetscCall(PetscPrintf(
375dfeb939dSJames Wright           comm, "    Local Vector %" PetscInt_FMT "-DoF nodes           : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n", num_comp_q,
376926a6279SJames 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));
377926a6279SJames Wright     }
378926a6279SJames Wright 
37945abf96eSJames Wright     if (comm_size != 1) {
380dfeb939dSJames Wright       PetscInt num_remote_roots_total = 0, num_remote_leaves_total = 0, num_ghost_interface_ranks = 0, num_owned_interface_ranks = 0;
381926a6279SJames Wright       {
382926a6279SJames Wright         PetscSF            sf;
3833bbbcc04SJames Wright         PetscMPIInt        nrranks, niranks;
384dfeb939dSJames Wright         const PetscInt    *roffset, *rmine, *rremote, *ioffset, *irootloc;
385dfeb939dSJames Wright         const PetscMPIInt *rranks, *iranks;
3860c373b74SJames Wright         PetscCall(DMGetSectionSF(honee->dm, &sf));
387926a6279SJames Wright         PetscCall(PetscSFGetRootRanks(sf, &nrranks, &rranks, &roffset, &rmine, &rremote));
388dfeb939dSJames Wright         PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, &ioffset, &irootloc));
389926a6279SJames Wright         for (PetscInt i = 0; i < nrranks; i++) {
390926a6279SJames Wright           if (rranks[i] == rank) continue;  // Ignore same-part global->local transfers
391926a6279SJames Wright           num_remote_roots_total += roffset[i + 1] - roffset[i];
392dfeb939dSJames Wright           num_ghost_interface_ranks++;
393dfeb939dSJames Wright         }
394dfeb939dSJames Wright         for (PetscInt i = 0; i < niranks; i++) {
395dfeb939dSJames Wright           if (iranks[i] == rank) continue;
396dfeb939dSJames Wright           num_remote_leaves_total += ioffset[i + 1] - ioffset[i];
397dfeb939dSJames Wright           num_owned_interface_ranks++;
398926a6279SJames Wright         }
399926a6279SJames Wright       }
400dfeb939dSJames Wright       PetscCallMPI(MPI_Gather(&num_remote_roots_total, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
401926a6279SJames Wright       if (!rank) {
402926a6279SJames Wright         PetscCall(PetscSortInt(comm_size, gather_buffer));
403dfeb939dSJames Wright         part_boundary_dofs[0]           = gather_buffer[0];              // min
404dfeb939dSJames Wright         part_boundary_dofs[1]           = gather_buffer[comm_size - 1];  // max
405dfeb939dSJames Wright         part_boundary_dofs[2]           = gather_buffer[median_index];   // median
406dfeb939dSJames Wright         PetscReal part_shared_dof_ratio = (PetscReal)part_boundary_dofs[1] / (PetscReal)part_boundary_dofs[2];
407dfeb939dSJames Wright         PetscCall(PetscPrintf(
40845abf96eSJames Wright             comm, "    Ghost Interface %" PetscInt_FMT "-DoF nodes        : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
40945abf96eSJames 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,
41045abf96eSJames Wright             part_shared_dof_ratio));
411dfeb939dSJames Wright       }
412dfeb939dSJames Wright 
413dfeb939dSJames Wright       PetscCallMPI(MPI_Gather(&num_ghost_interface_ranks, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
414dfeb939dSJames Wright       if (!rank) {
415dfeb939dSJames Wright         PetscCall(PetscSortInt(comm_size, gather_buffer));
416dfeb939dSJames Wright         part_neighbors[0]              = gather_buffer[0];              // min
417dfeb939dSJames Wright         part_neighbors[1]              = gather_buffer[comm_size - 1];  // max
418dfeb939dSJames Wright         part_neighbors[2]              = gather_buffer[median_index];   // median
419dfeb939dSJames Wright         PetscReal part_neighbors_ratio = (PetscReal)part_neighbors[1] / (PetscReal)part_neighbors[2];
420dfeb939dSJames Wright         PetscCall(PetscPrintf(comm, "    Ghost Interface Ranks              : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
421dfeb939dSJames Wright                               part_neighbors[0], part_neighbors[1], part_neighbors[2], part_neighbors_ratio));
422dfeb939dSJames Wright       }
423dfeb939dSJames Wright 
424dfeb939dSJames Wright       PetscCallMPI(MPI_Gather(&num_remote_leaves_total, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
425dfeb939dSJames Wright       if (!rank) {
426dfeb939dSJames Wright         PetscCall(PetscSortInt(comm_size, gather_buffer));
427dfeb939dSJames Wright         part_boundary_dofs[0]           = gather_buffer[0];              // min
428dfeb939dSJames Wright         part_boundary_dofs[1]           = gather_buffer[comm_size - 1];  // max
429dfeb939dSJames Wright         part_boundary_dofs[2]           = gather_buffer[median_index];   // median
430dfeb939dSJames Wright         PetscReal part_shared_dof_ratio = (PetscReal)part_boundary_dofs[1] / (PetscReal)part_boundary_dofs[2];
431dfeb939dSJames Wright         PetscCall(PetscPrintf(
43245abf96eSJames Wright             comm, "    Owned Interface %" PetscInt_FMT "-DoF nodes        : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
43345abf96eSJames 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,
43445abf96eSJames Wright             part_shared_dof_ratio));
435dfeb939dSJames Wright       }
436dfeb939dSJames Wright 
437dfeb939dSJames Wright       PetscCallMPI(MPI_Gather(&num_owned_interface_ranks, 1, MPIU_INT, gather_buffer, 1, MPIU_INT, 0, comm));
438dfeb939dSJames Wright       if (!rank) {
439dfeb939dSJames Wright         PetscCall(PetscSortInt(comm_size, gather_buffer));
440dfeb939dSJames Wright         part_neighbors[0]              = gather_buffer[0];              // min
441dfeb939dSJames Wright         part_neighbors[1]              = gather_buffer[comm_size - 1];  // max
442dfeb939dSJames Wright         part_neighbors[2]              = gather_buffer[median_index];   // median
443dfeb939dSJames Wright         PetscReal part_neighbors_ratio = (PetscReal)part_neighbors[1] / (PetscReal)part_neighbors[2];
444dfeb939dSJames Wright         PetscCall(PetscPrintf(comm, "    Owned Interface Ranks              : %" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ", %f\n",
445dfeb939dSJames Wright                               part_neighbors[0], part_neighbors[1], part_neighbors[2], part_neighbors_ratio));
446926a6279SJames Wright       }
44745abf96eSJames Wright     }
448926a6279SJames Wright 
449926a6279SJames Wright     if (!rank) PetscCall(PetscFree(gather_buffer));
450926a6279SJames Wright   }
451926a6279SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
452926a6279SJames Wright }
453