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 <petscts.h> 14 15 #include "../navierstokes.h" 16 #include "../qfunctions/mass.h" 17 18 PetscErrorCode ICs_FixMultiplicity(DM dm, CeedData ceed_data, User user, Vec Q_loc, Vec Q, CeedScalar time) { 19 PetscFunctionBeginUser; 20 21 // --------------------------------------------------------------------------- 22 // Update time for evaluation 23 // --------------------------------------------------------------------------- 24 if (user->phys->ics_time_label) CeedOperatorSetContextDouble(ceed_data->op_ics, user->phys->ics_time_label, &time); 25 26 // --------------------------------------------------------------------------- 27 // ICs 28 // --------------------------------------------------------------------------- 29 // -- CEED Restriction 30 CeedVector q0_ceed; 31 CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &q0_ceed, NULL); 32 33 // -- Place PETSc vector in CEED vector 34 PetscMemType q0_mem_type; 35 PetscCall(VecP2C(Q_loc, &q0_mem_type, q0_ceed)); 36 37 // -- Apply CEED Operator 38 CeedOperatorApply(ceed_data->op_ics, ceed_data->x_coord, q0_ceed, CEED_REQUEST_IMMEDIATE); 39 40 // -- Restore vectors 41 PetscCall(VecC2P(q0_ceed, q0_mem_type, Q_loc)); 42 43 // -- Local-to-Global 44 PetscCall(VecZeroEntries(Q)); 45 PetscCall(DMLocalToGlobal(dm, Q_loc, ADD_VALUES, Q)); 46 47 // --------------------------------------------------------------------------- 48 // Fix multiplicity for output of ICs 49 // --------------------------------------------------------------------------- 50 // -- CEED Restriction 51 CeedVector mult_vec; 52 CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &mult_vec, NULL); 53 54 // -- Place PETSc vector in CEED vector 55 PetscMemType m_mem_type; 56 Vec multiplicity_loc; 57 PetscCall(DMGetLocalVector(dm, &multiplicity_loc)); 58 PetscCall(VecP2C(multiplicity_loc, &m_mem_type, mult_vec)); 59 60 // -- Get multiplicity 61 CeedElemRestrictionGetMultiplicity(ceed_data->elem_restr_q, mult_vec); 62 63 // -- Restore vectors 64 PetscCall(VecC2P(mult_vec, m_mem_type, multiplicity_loc)); 65 66 // -- Local-to-Global 67 Vec multiplicity; 68 PetscCall(DMGetGlobalVector(dm, &multiplicity)); 69 PetscCall(VecZeroEntries(multiplicity)); 70 PetscCall(DMLocalToGlobal(dm, multiplicity_loc, ADD_VALUES, multiplicity)); 71 72 // -- Fix multiplicity 73 PetscCall(VecPointwiseDivide(Q, Q, multiplicity)); 74 PetscCall(VecPointwiseDivide(Q_loc, Q_loc, multiplicity_loc)); 75 76 // -- Restore vectors 77 PetscCall(DMRestoreLocalVector(dm, &multiplicity_loc)); 78 PetscCall(DMRestoreGlobalVector(dm, &multiplicity)); 79 80 // Cleanup 81 CeedVectorDestroy(&mult_vec); 82 CeedVectorDestroy(&q0_ceed); 83 84 PetscFunctionReturn(0); 85 } 86 87 PetscErrorCode DMPlexInsertBoundaryValues_NS(DM dm, PetscBool insert_essential, Vec Q_loc, PetscReal time, Vec face_geom_FVM, Vec cell_geom_FVM, 88 Vec grad_FVM) { 89 Vec Qbc, boundary_mask; 90 PetscFunctionBegin; 91 92 // Mask (zero) Dirichlet entries 93 PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask)); 94 PetscCall(VecPointwiseMult(Q_loc, Q_loc, boundary_mask)); 95 PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask)); 96 97 PetscCall(DMGetNamedLocalVector(dm, "Qbc", &Qbc)); 98 PetscCall(VecAXPY(Q_loc, 1., Qbc)); 99 PetscCall(DMRestoreNamedLocalVector(dm, "Qbc", &Qbc)); 100 101 PetscFunctionReturn(0); 102 } 103 104 // @brief Load vector from binary file, possibly with embedded solution time and step number 105 PetscErrorCode LoadFluidsBinaryVec(MPI_Comm comm, PetscViewer viewer, Vec Q, PetscReal *time, PetscInt *step_number) { 106 PetscInt token, file_step_number; 107 PetscReal file_time; 108 PetscFunctionBeginUser; 109 110 // Attempt 111 PetscCall(PetscViewerBinaryRead(viewer, &token, 1, NULL, PETSC_INT)); 112 if (token == FLUIDS_FILE_TOKEN) { // New style format; we're reading a file with step number and time in the header 113 PetscCall(PetscViewerBinaryRead(viewer, &file_step_number, 1, NULL, PETSC_INT)); 114 PetscCall(PetscViewerBinaryRead(viewer, &file_time, 1, NULL, PETSC_REAL)); 115 if (time) *time = file_time; 116 if (step_number) *step_number = file_step_number; 117 } else if (token == VEC_FILE_CLASSID) { // Legacy format of just the vector, encoded as [VEC_FILE_CLASSID, length, ] 118 PetscInt length, N; 119 PetscCall(PetscViewerBinaryRead(viewer, &length, 1, NULL, PETSC_INT)); 120 PetscCall(VecGetSize(Q, &N)); 121 PetscCheck(length == N, comm, PETSC_ERR_ARG_INCOMP, "File Vec has length %" PetscInt_FMT " but DM has global Vec size %" PetscInt_FMT, length, N); 122 PetscCall(PetscViewerBinarySetSkipHeader(viewer, PETSC_TRUE)); 123 } else SETERRQ(comm, PETSC_ERR_FILE_UNEXPECTED, "Not a fluids header token or a PETSc Vec in file"); 124 125 // Load Q from existent solution 126 PetscCall(VecLoad(Q, viewer)); 127 128 PetscFunctionReturn(0); 129 } 130 131 // Compare reference solution values with current test run for CI 132 PetscErrorCode RegressionTests_NS(AppCtx app_ctx, Vec Q) { 133 Vec Qref; 134 PetscViewer viewer; 135 PetscReal error, Qrefnorm; 136 MPI_Comm comm = PetscObjectComm((PetscObject)Q); 137 PetscFunctionBegin; 138 139 // Read reference file 140 PetscCall(VecDuplicate(Q, &Qref)); 141 PetscCall(PetscViewerBinaryOpen(comm, app_ctx->test_file_path, FILE_MODE_READ, &viewer)); 142 PetscCall(LoadFluidsBinaryVec(comm, viewer, Qref, NULL, NULL)); 143 144 // Compute error with respect to reference solution 145 PetscCall(VecAXPY(Q, -1.0, Qref)); 146 PetscCall(VecNorm(Qref, NORM_MAX, &Qrefnorm)); 147 PetscCall(VecScale(Q, 1. / Qrefnorm)); 148 PetscCall(VecNorm(Q, NORM_MAX, &error)); 149 150 // Check error 151 if (error > app_ctx->test_tol) { 152 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test failed with error norm %g\n", (double)error)); 153 } 154 155 // Cleanup 156 PetscCall(PetscViewerDestroy(&viewer)); 157 PetscCall(VecDestroy(&Qref)); 158 159 PetscFunctionReturn(0); 160 } 161 162 // Get error for problems with exact solutions 163 PetscErrorCode GetError_NS(CeedData ceed_data, DM dm, User user, Vec Q, PetscScalar final_time) { 164 PetscInt loc_nodes; 165 Vec Q_exact, Q_exact_loc; 166 PetscReal rel_error, norm_error, norm_exact; 167 PetscFunctionBegin; 168 169 // Get exact solution at final time 170 PetscCall(DMCreateGlobalVector(dm, &Q_exact)); 171 PetscCall(DMGetLocalVector(dm, &Q_exact_loc)); 172 PetscCall(VecGetSize(Q_exact_loc, &loc_nodes)); 173 PetscCall(ICs_FixMultiplicity(dm, ceed_data, user, Q_exact_loc, Q_exact, final_time)); 174 175 // Get |exact solution - obtained solution| 176 PetscCall(VecNorm(Q_exact, NORM_1, &norm_exact)); 177 PetscCall(VecAXPY(Q, -1.0, Q_exact)); 178 PetscCall(VecNorm(Q, NORM_1, &norm_error)); 179 180 // Compute relative error 181 rel_error = norm_error / norm_exact; 182 183 // Output relative error 184 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Relative Error: %g\n", (double)rel_error)); 185 // Cleanup 186 PetscCall(DMRestoreLocalVector(dm, &Q_exact_loc)); 187 PetscCall(VecDestroy(&Q_exact)); 188 189 PetscFunctionReturn(0); 190 } 191 192 // Post-processing 193 PetscErrorCode PostProcess_NS(TS ts, CeedData ceed_data, DM dm, ProblemData *problem, User user, Vec Q, PetscScalar final_time) { 194 PetscInt steps; 195 TSConvergedReason reason; 196 PetscFunctionBegin; 197 198 // Print relative error 199 if (problem->non_zero_time && user->app_ctx->test_type == TESTTYPE_NONE) { 200 PetscCall(GetError_NS(ceed_data, dm, user, Q, final_time)); 201 } 202 203 // Print final time and number of steps 204 PetscCall(TSGetStepNumber(ts, &steps)); 205 PetscCall(TSGetConvergedReason(ts, &reason)); 206 if (user->app_ctx->test_type == TESTTYPE_NONE) { 207 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time integrator %s on time step %" PetscInt_FMT " with final time %g\n", TSConvergedReasons[reason], 208 steps, (double)final_time)); 209 } 210 211 // Output numerical values from command line 212 PetscCall(VecViewFromOptions(Q, NULL, "-vec_view")); 213 214 // Compare reference solution values with current test run for CI 215 if (user->app_ctx->test_type == TESTTYPE_SOLVER) { 216 PetscCall(RegressionTests_NS(user->app_ctx, Q)); 217 } 218 219 PetscFunctionReturn(0); 220 } 221 222 const PetscInt FLUIDS_FILE_TOKEN = 0xceedf00; 223 224 // Gather initial Q values in case of continuation of simulation 225 PetscErrorCode SetupICsFromBinary(MPI_Comm comm, AppCtx app_ctx, Vec Q) { 226 PetscViewer viewer; 227 228 PetscFunctionBegin; 229 230 PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_file, FILE_MODE_READ, &viewer)); 231 PetscCall(LoadFluidsBinaryVec(comm, viewer, Q, &app_ctx->cont_time, &app_ctx->cont_steps)); 232 PetscCall(PetscViewerDestroy(&viewer)); 233 234 PetscFunctionReturn(0); 235 } 236 237 // Record boundary values from initial condition 238 PetscErrorCode SetBCsFromICs_NS(DM dm, Vec Q, Vec Q_loc) { 239 Vec Qbc, boundary_mask; 240 PetscFunctionBegin; 241 242 PetscCall(DMGetNamedLocalVector(dm, "Qbc", &Qbc)); 243 PetscCall(VecCopy(Q_loc, Qbc)); 244 PetscCall(VecZeroEntries(Q_loc)); 245 PetscCall(DMGlobalToLocal(dm, Q, INSERT_VALUES, Q_loc)); 246 PetscCall(VecAXPY(Qbc, -1., Q_loc)); 247 PetscCall(DMRestoreNamedLocalVector(dm, "Qbc", &Qbc)); 248 PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_NS)); 249 250 PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask)); 251 PetscCall(DMGetGlobalVector(dm, &Q)); 252 PetscCall(VecZeroEntries(boundary_mask)); 253 PetscCall(VecSet(Q, 1.0)); 254 PetscCall(DMGlobalToLocal(dm, Q, INSERT_VALUES, boundary_mask)); 255 PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask)); 256 257 PetscFunctionReturn(0); 258 } 259 260 // Free a plain data context that was allocated using PETSc; returning libCEED error codes 261 int FreeContextPetsc(void *data) { 262 if (PetscFree(data)) return CeedError(NULL, CEED_ERROR_ACCESS, "PetscFree failed"); 263 return CEED_ERROR_SUCCESS; 264 } 265 266 // Return mass qfunction specification for number of components N 267 PetscErrorCode CreateMassQFunction(Ceed ceed, CeedInt N, CeedInt q_data_size, CeedQFunction *qf) { 268 CeedQFunctionUser qfunction_ptr; 269 const char *qfunction_loc; 270 PetscFunctionBeginUser; 271 272 switch (N) { 273 case 1: 274 qfunction_ptr = Mass_1; 275 qfunction_loc = Mass_1_loc; 276 break; 277 case 5: 278 qfunction_ptr = Mass_5; 279 qfunction_loc = Mass_5_loc; 280 break; 281 case 7: 282 qfunction_ptr = Mass_7; 283 qfunction_loc = Mass_7_loc; 284 break; 285 case 9: 286 qfunction_ptr = Mass_9; 287 qfunction_loc = Mass_9_loc; 288 break; 289 case 22: 290 qfunction_ptr = Mass_22; 291 qfunction_loc = Mass_22_loc; 292 break; 293 default: 294 SETERRQ(PETSC_COMM_WORLD, -1, "Could not find mass qfunction of size %d", N); 295 } 296 CeedQFunctionCreateInterior(ceed, 1, qfunction_ptr, qfunction_loc, qf); 297 298 CeedQFunctionAddInput(*qf, "u", N, CEED_EVAL_INTERP); 299 CeedQFunctionAddInput(*qf, "qdata", q_data_size, CEED_EVAL_NONE); 300 CeedQFunctionAddOutput(*qf, "v", N, CEED_EVAL_INTERP); 301 PetscFunctionReturn(0); 302 } 303 304 /* @brief L^2 Projection of a source FEM function to a target FEM space 305 * 306 * To solve system using a lumped mass matrix, pass a KSP object with ksp_type=preonly, pc_type=jacobi, pc_jacobi_type=rowsum. 307 * 308 * @param[in] source_vec Global Vec of the source FEM function. NULL indicates using rhs_matop_ctx->X_loc 309 * @param[out] target_vec Global Vec of the target (result) FEM function. NULL indicates using rhs_matop_ctx->Y_loc 310 * @param[in] rhs_matop_ctx MatopApplyContext for performing the RHS evaluation 311 * @param[in] ksp KSP for solving the consistent projection problem 312 */ 313 PetscErrorCode ComputeL2Projection(Vec source_vec, Vec target_vec, OperatorApplyContext rhs_matop_ctx, KSP ksp) { 314 PetscFunctionBeginUser; 315 316 PetscCall(ApplyCeedOperatorGlobalToGlobal(source_vec, target_vec, rhs_matop_ctx)); 317 PetscCall(KSPSolve(ksp, target_vec, target_vec)); 318 319 PetscFunctionReturn(0); 320 } 321 322 PetscErrorCode NodalProjectionDataDestroy(NodalProjectionData context) { 323 PetscFunctionBeginUser; 324 if (context == NULL) PetscFunctionReturn(0); 325 326 PetscCall(DMDestroy(&context->dm)); 327 PetscCall(KSPDestroy(&context->ksp)); 328 329 PetscCall(OperatorApplyContextDestroy(context->l2_rhs_ctx)); 330 331 PetscCall(PetscFree(context)); 332 333 PetscFunctionReturn(0); 334 } 335 336 /* 337 * @brief Open a PHASTA *.dat file, grabbing dimensions and file pointer 338 * 339 * This function opens the file specified by `path` using `PetscFOpen` and passes the file pointer in `fp`. 340 * It is not closed in this function, thus `fp` must be closed sometime after this function has been called (using `PetscFClose` for example). 341 * 342 * 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. 343 * 344 * @param[in] comm MPI_Comm for the program 345 * @param[in] path Path to the file 346 * @param[in] char_array_len Length of the character array that should contain each line 347 * @param[out] dims Dimensions of the file, taken from the first line of the file 348 * @param[out] fp File pointer to the opened file 349 */ 350 PetscErrorCode PHASTADatFileOpen(const MPI_Comm comm, const char path[PETSC_MAX_PATH_LEN], const PetscInt char_array_len, PetscInt dims[2], 351 FILE **fp) { 352 PetscInt ndims; 353 char line[char_array_len]; 354 char **array; 355 356 PetscFunctionBeginUser; 357 PetscCall(PetscFOpen(comm, path, "r", fp)); 358 PetscCall(PetscSynchronizedFGets(comm, *fp, char_array_len, line)); 359 PetscCall(PetscStrToArray(line, ' ', &ndims, &array)); 360 PetscCheck(ndims == 2, comm, PETSC_ERR_FILE_UNEXPECTED, "Found %" PetscInt_FMT " dimensions instead of 2 on the first line of %s", ndims, path); 361 362 for (PetscInt i = 0; i < ndims; i++) dims[i] = atoi(array[i]); 363 PetscCall(PetscStrToArrayDestroy(ndims, array)); 364 365 PetscFunctionReturn(0); 366 } 367 368 /* 369 * @brief Get the number of rows for the PHASTA file at path. 370 * 371 * 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. 372 * 373 * @param[in] comm MPI_Comm for the program 374 * @param[in] path Path to the file 375 * @param[out] nrows Number of rows 376 */ 377 PetscErrorCode PHASTADatFileGetNRows(const MPI_Comm comm, const char path[PETSC_MAX_PATH_LEN], PetscInt *nrows) { 378 const PetscInt char_array_len = 512; 379 PetscInt dims[2]; 380 FILE *fp; 381 382 PetscFunctionBeginUser; 383 PetscCall(PHASTADatFileOpen(comm, path, char_array_len, dims, &fp)); 384 *nrows = dims[0]; 385 PetscCall(PetscFClose(comm, fp)); 386 387 PetscFunctionReturn(0); 388 } 389 390 PetscErrorCode PHASTADatFileReadToArrayReal(MPI_Comm comm, const char path[PETSC_MAX_PATH_LEN], PetscReal array[]) { 391 PetscInt ndims, dims[2]; 392 FILE *fp; 393 const PetscInt char_array_len = 512; 394 char line[char_array_len]; 395 char **row_array; 396 PetscFunctionBeginUser; 397 398 PetscCall(PHASTADatFileOpen(comm, path, char_array_len, dims, &fp)); 399 400 for (PetscInt i = 0; i < dims[0]; i++) { 401 PetscCall(PetscSynchronizedFGets(comm, fp, char_array_len, line)); 402 PetscCall(PetscStrToArray(line, ' ', &ndims, &row_array)); 403 PetscCheck(ndims == dims[1], comm, PETSC_ERR_FILE_UNEXPECTED, 404 "Line %" PetscInt_FMT " of %s does not contain enough columns (%" PetscInt_FMT " instead of %" PetscInt_FMT ")", i, path, ndims, 405 dims[1]); 406 407 for (PetscInt j = 0; j < dims[1]; j++) { 408 array[i * dims[1] + j] = (PetscReal)atof(row_array[j]); 409 } 410 } 411 412 PetscCall(PetscFClose(comm, fp)); 413 414 PetscFunctionReturn(0); 415 } 416