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