13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 377841947SLeila Ghaffari // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 577841947SLeila Ghaffari // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 777841947SLeila Ghaffari 877841947SLeila Ghaffari /// @file 977841947SLeila Ghaffari /// Miscellaneous utility functions 1077841947SLeila Ghaffari 1149aac155SJeremy L Thompson #include <ceed.h> 1249aac155SJeremy L Thompson #include <petscdm.h> 1349aac155SJeremy L Thompson #include <petscts.h> 1449aac155SJeremy L Thompson 1577841947SLeila Ghaffari #include "../navierstokes.h" 16ef080ff9SJames Wright #include "../qfunctions/mass.h" 1777841947SLeila Ghaffari 182b730f8bSJeremy L Thompson PetscErrorCode ICs_FixMultiplicity(DM dm, CeedData ceed_data, User user, Vec Q_loc, Vec Q, CeedScalar time) { 1977841947SLeila Ghaffari PetscFunctionBeginUser; 2077841947SLeila Ghaffari 2177841947SLeila Ghaffari // --------------------------------------------------------------------------- 22a0add3c9SJed Brown // Update time for evaluation 2377841947SLeila Ghaffari // --------------------------------------------------------------------------- 245263e9c6SJames Wright if (user->phys->ics_time_label) CeedOperatorSetContextDouble(ceed_data->op_ics_ctx->op, user->phys->ics_time_label, &time); 2577841947SLeila Ghaffari 2677841947SLeila Ghaffari // --------------------------------------------------------------------------- 2777841947SLeila Ghaffari // ICs 2877841947SLeila Ghaffari // --------------------------------------------------------------------------- 2977841947SLeila Ghaffari // -- CEED Restriction 3077841947SLeila Ghaffari CeedVector q0_ceed; 3177841947SLeila Ghaffari CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &q0_ceed, NULL); 3277841947SLeila Ghaffari 3377841947SLeila Ghaffari // -- Place PETSc vector in CEED vector 345263e9c6SJames Wright PetscCall(ApplyCeedOperatorLocalToGlobal(NULL, Q, ceed_data->op_ics_ctx)); 3577841947SLeila Ghaffari 3677841947SLeila Ghaffari // --------------------------------------------------------------------------- 3777841947SLeila Ghaffari // Fix multiplicity for output of ICs 3877841947SLeila Ghaffari // --------------------------------------------------------------------------- 3977841947SLeila Ghaffari // -- CEED Restriction 4077841947SLeila Ghaffari CeedVector mult_vec; 4177841947SLeila Ghaffari CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &mult_vec, NULL); 4277841947SLeila Ghaffari 4377841947SLeila Ghaffari // -- Place PETSc vector in CEED vector 4477841947SLeila Ghaffari PetscMemType m_mem_type; 4577841947SLeila Ghaffari Vec multiplicity_loc; 462b730f8bSJeremy L Thompson PetscCall(DMGetLocalVector(dm, &multiplicity_loc)); 47c798d55aSJames Wright PetscCall(VecP2C(multiplicity_loc, &m_mem_type, mult_vec)); 4877841947SLeila Ghaffari 4977841947SLeila Ghaffari // -- Get multiplicity 5077841947SLeila Ghaffari CeedElemRestrictionGetMultiplicity(ceed_data->elem_restr_q, mult_vec); 5177841947SLeila Ghaffari 5277841947SLeila Ghaffari // -- Restore vectors 53c798d55aSJames Wright PetscCall(VecC2P(mult_vec, m_mem_type, multiplicity_loc)); 5477841947SLeila Ghaffari 5577841947SLeila Ghaffari // -- Local-to-Global 5677841947SLeila Ghaffari Vec multiplicity; 572b730f8bSJeremy L Thompson PetscCall(DMGetGlobalVector(dm, &multiplicity)); 582b730f8bSJeremy L Thompson PetscCall(VecZeroEntries(multiplicity)); 592b730f8bSJeremy L Thompson PetscCall(DMLocalToGlobal(dm, multiplicity_loc, ADD_VALUES, multiplicity)); 6077841947SLeila Ghaffari 6177841947SLeila Ghaffari // -- Fix multiplicity 622b730f8bSJeremy L Thompson PetscCall(VecPointwiseDivide(Q, Q, multiplicity)); 632b730f8bSJeremy L Thompson PetscCall(VecPointwiseDivide(Q_loc, Q_loc, multiplicity_loc)); 6477841947SLeila Ghaffari 6577841947SLeila Ghaffari // -- Restore vectors 662b730f8bSJeremy L Thompson PetscCall(DMRestoreLocalVector(dm, &multiplicity_loc)); 672b730f8bSJeremy L Thompson PetscCall(DMRestoreGlobalVector(dm, &multiplicity)); 6877841947SLeila Ghaffari 6977841947SLeila Ghaffari // Cleanup 7077841947SLeila Ghaffari CeedVectorDestroy(&mult_vec); 7177841947SLeila Ghaffari CeedVectorDestroy(&q0_ceed); 7277841947SLeila Ghaffari 7377841947SLeila Ghaffari PetscFunctionReturn(0); 7477841947SLeila Ghaffari } 7577841947SLeila Ghaffari 762b730f8bSJeremy L Thompson PetscErrorCode DMPlexInsertBoundaryValues_NS(DM dm, PetscBool insert_essential, Vec Q_loc, PetscReal time, Vec face_geom_FVM, Vec cell_geom_FVM, 772b730f8bSJeremy L Thompson Vec grad_FVM) { 785571c6fdSJames Wright Vec Qbc, boundary_mask; 7977841947SLeila Ghaffari PetscFunctionBegin; 8077841947SLeila Ghaffari 813722cd23SJames Wright // Mask (zero) Strong BC entries 825571c6fdSJames Wright PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask)); 835571c6fdSJames Wright PetscCall(VecPointwiseMult(Q_loc, Q_loc, boundary_mask)); 845571c6fdSJames Wright PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask)); 855571c6fdSJames Wright 862b730f8bSJeremy L Thompson PetscCall(DMGetNamedLocalVector(dm, "Qbc", &Qbc)); 872b730f8bSJeremy L Thompson PetscCall(VecAXPY(Q_loc, 1., Qbc)); 882b730f8bSJeremy L Thompson PetscCall(DMRestoreNamedLocalVector(dm, "Qbc", &Qbc)); 8977841947SLeila Ghaffari 9077841947SLeila Ghaffari PetscFunctionReturn(0); 9177841947SLeila Ghaffari } 9277841947SLeila Ghaffari 93530ad8c4SKenneth E. Jansen // @brief Load vector from binary file, possibly with embedded solution time and step number 94530ad8c4SKenneth E. Jansen PetscErrorCode LoadFluidsBinaryVec(MPI_Comm comm, PetscViewer viewer, Vec Q, PetscReal *time, PetscInt *step_number) { 95530ad8c4SKenneth E. Jansen PetscInt token, file_step_number; 96530ad8c4SKenneth E. Jansen PetscReal file_time; 97530ad8c4SKenneth E. Jansen PetscFunctionBeginUser; 98530ad8c4SKenneth E. Jansen 99530ad8c4SKenneth E. Jansen // Attempt 100530ad8c4SKenneth E. Jansen PetscCall(PetscViewerBinaryRead(viewer, &token, 1, NULL, PETSC_INT)); 101530ad8c4SKenneth E. Jansen if (token == FLUIDS_FILE_TOKEN) { // New style format; we're reading a file with step number and time in the header 102530ad8c4SKenneth E. Jansen PetscCall(PetscViewerBinaryRead(viewer, &file_step_number, 1, NULL, PETSC_INT)); 103530ad8c4SKenneth E. Jansen PetscCall(PetscViewerBinaryRead(viewer, &file_time, 1, NULL, PETSC_REAL)); 104530ad8c4SKenneth E. Jansen if (time) *time = file_time; 105530ad8c4SKenneth E. Jansen if (step_number) *step_number = file_step_number; 106530ad8c4SKenneth E. Jansen } else if (token == VEC_FILE_CLASSID) { // Legacy format of just the vector, encoded as [VEC_FILE_CLASSID, length, ] 107530ad8c4SKenneth E. Jansen PetscInt length, N; 108530ad8c4SKenneth E. Jansen PetscCall(PetscViewerBinaryRead(viewer, &length, 1, NULL, PETSC_INT)); 109530ad8c4SKenneth E. Jansen PetscCall(VecGetSize(Q, &N)); 110530ad8c4SKenneth 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); 111530ad8c4SKenneth E. Jansen PetscCall(PetscViewerBinarySetSkipHeader(viewer, PETSC_TRUE)); 112530ad8c4SKenneth E. Jansen } else SETERRQ(comm, PETSC_ERR_FILE_UNEXPECTED, "Not a fluids header token or a PETSc Vec in file"); 113530ad8c4SKenneth E. Jansen 114530ad8c4SKenneth E. Jansen // Load Q from existent solution 115530ad8c4SKenneth E. Jansen PetscCall(VecLoad(Q, viewer)); 116530ad8c4SKenneth E. Jansen 117530ad8c4SKenneth E. Jansen PetscFunctionReturn(0); 118530ad8c4SKenneth E. Jansen } 119530ad8c4SKenneth E. Jansen 12077841947SLeila Ghaffari // Compare reference solution values with current test run for CI 12177841947SLeila Ghaffari PetscErrorCode RegressionTests_NS(AppCtx app_ctx, Vec Q) { 12277841947SLeila Ghaffari Vec Qref; 12377841947SLeila Ghaffari PetscViewer viewer; 12477841947SLeila Ghaffari PetscReal error, Qrefnorm; 125530ad8c4SKenneth E. Jansen MPI_Comm comm = PetscObjectComm((PetscObject)Q); 12677841947SLeila Ghaffari PetscFunctionBegin; 12777841947SLeila Ghaffari 12877841947SLeila Ghaffari // Read reference file 1292b730f8bSJeremy L Thompson PetscCall(VecDuplicate(Q, &Qref)); 130530ad8c4SKenneth E. Jansen PetscCall(PetscViewerBinaryOpen(comm, app_ctx->test_file_path, FILE_MODE_READ, &viewer)); 131530ad8c4SKenneth E. Jansen PetscCall(LoadFluidsBinaryVec(comm, viewer, Qref, NULL, NULL)); 13277841947SLeila Ghaffari 13377841947SLeila Ghaffari // Compute error with respect to reference solution 1342b730f8bSJeremy L Thompson PetscCall(VecAXPY(Q, -1.0, Qref)); 1352b730f8bSJeremy L Thompson PetscCall(VecNorm(Qref, NORM_MAX, &Qrefnorm)); 1362b730f8bSJeremy L Thompson PetscCall(VecScale(Q, 1. / Qrefnorm)); 1372b730f8bSJeremy L Thompson PetscCall(VecNorm(Q, NORM_MAX, &error)); 13877841947SLeila Ghaffari 13977841947SLeila Ghaffari // Check error 14077841947SLeila Ghaffari if (error > app_ctx->test_tol) { 1412b730f8bSJeremy L Thompson PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test failed with error norm %g\n", (double)error)); 14277841947SLeila Ghaffari } 14377841947SLeila Ghaffari 14477841947SLeila Ghaffari // Cleanup 1452b730f8bSJeremy L Thompson PetscCall(PetscViewerDestroy(&viewer)); 1462b730f8bSJeremy L Thompson PetscCall(VecDestroy(&Qref)); 14777841947SLeila Ghaffari 14877841947SLeila Ghaffari PetscFunctionReturn(0); 14977841947SLeila Ghaffari } 15077841947SLeila Ghaffari 15177841947SLeila Ghaffari // Get error for problems with exact solutions 1522b730f8bSJeremy L Thompson PetscErrorCode GetError_NS(CeedData ceed_data, DM dm, User user, Vec Q, PetscScalar final_time) { 15377841947SLeila Ghaffari PetscInt loc_nodes; 15477841947SLeila Ghaffari Vec Q_exact, Q_exact_loc; 15577841947SLeila Ghaffari PetscReal rel_error, norm_error, norm_exact; 15677841947SLeila Ghaffari PetscFunctionBegin; 15777841947SLeila Ghaffari 15877841947SLeila Ghaffari // Get exact solution at final time 1592b730f8bSJeremy L Thompson PetscCall(DMCreateGlobalVector(dm, &Q_exact)); 1602b730f8bSJeremy L Thompson PetscCall(DMGetLocalVector(dm, &Q_exact_loc)); 1612b730f8bSJeremy L Thompson PetscCall(VecGetSize(Q_exact_loc, &loc_nodes)); 1622b730f8bSJeremy L Thompson PetscCall(ICs_FixMultiplicity(dm, ceed_data, user, Q_exact_loc, Q_exact, final_time)); 16377841947SLeila Ghaffari 16477841947SLeila Ghaffari // Get |exact solution - obtained solution| 1652b730f8bSJeremy L Thompson PetscCall(VecNorm(Q_exact, NORM_1, &norm_exact)); 1662b730f8bSJeremy L Thompson PetscCall(VecAXPY(Q, -1.0, Q_exact)); 1672b730f8bSJeremy L Thompson PetscCall(VecNorm(Q, NORM_1, &norm_error)); 16877841947SLeila Ghaffari 16977841947SLeila Ghaffari // Compute relative error 17077841947SLeila Ghaffari rel_error = norm_error / norm_exact; 17177841947SLeila Ghaffari 17277841947SLeila Ghaffari // Output relative error 1732b730f8bSJeremy L Thompson PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Relative Error: %g\n", (double)rel_error)); 17477841947SLeila Ghaffari // Cleanup 1752b730f8bSJeremy L Thompson PetscCall(DMRestoreLocalVector(dm, &Q_exact_loc)); 1762b730f8bSJeremy L Thompson PetscCall(VecDestroy(&Q_exact)); 17777841947SLeila Ghaffari 17877841947SLeila Ghaffari PetscFunctionReturn(0); 17977841947SLeila Ghaffari } 18077841947SLeila Ghaffari 18177841947SLeila Ghaffari // Post-processing 1822b730f8bSJeremy L Thompson PetscErrorCode PostProcess_NS(TS ts, CeedData ceed_data, DM dm, ProblemData *problem, User user, Vec Q, PetscScalar final_time) { 18377841947SLeila Ghaffari PetscInt steps; 184cf7a0454SJed Brown TSConvergedReason reason; 18577841947SLeila Ghaffari PetscFunctionBegin; 18677841947SLeila Ghaffari 18777841947SLeila Ghaffari // Print relative error 1888fb33541SJames Wright if (problem->non_zero_time && user->app_ctx->test_type == TESTTYPE_NONE) { 1892b730f8bSJeremy L Thompson PetscCall(GetError_NS(ceed_data, dm, user, Q, final_time)); 19077841947SLeila Ghaffari } 19177841947SLeila Ghaffari 19277841947SLeila Ghaffari // Print final time and number of steps 1932b730f8bSJeremy L Thompson PetscCall(TSGetStepNumber(ts, &steps)); 194cf7a0454SJed Brown PetscCall(TSGetConvergedReason(ts, &reason)); 1958fb33541SJames Wright if (user->app_ctx->test_type == TESTTYPE_NONE) { 196cf7a0454SJed Brown PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time integrator %s on time step %" PetscInt_FMT " with final time %g\n", TSConvergedReasons[reason], 197cf7a0454SJed Brown steps, (double)final_time)); 19877841947SLeila Ghaffari } 19977841947SLeila Ghaffari 20077841947SLeila Ghaffari // Output numerical values from command line 2012b730f8bSJeremy L Thompson PetscCall(VecViewFromOptions(Q, NULL, "-vec_view")); 20277841947SLeila Ghaffari 20377841947SLeila Ghaffari // Compare reference solution values with current test run for CI 2048fb33541SJames Wright if (user->app_ctx->test_type == TESTTYPE_SOLVER) { 2052b730f8bSJeremy L Thompson PetscCall(RegressionTests_NS(user->app_ctx, Q)); 20677841947SLeila Ghaffari } 20777841947SLeila Ghaffari 20877841947SLeila Ghaffari PetscFunctionReturn(0); 20977841947SLeila Ghaffari } 21077841947SLeila Ghaffari 2114de8550aSJed Brown const PetscInt FLUIDS_FILE_TOKEN = 0xceedf00; 2124de8550aSJed Brown 21377841947SLeila Ghaffari // Gather initial Q values in case of continuation of simulation 21477841947SLeila Ghaffari PetscErrorCode SetupICsFromBinary(MPI_Comm comm, AppCtx app_ctx, Vec Q) { 21577841947SLeila Ghaffari PetscViewer viewer; 2162b730f8bSJeremy L Thompson 21777841947SLeila Ghaffari PetscFunctionBegin; 21877841947SLeila Ghaffari 2192b730f8bSJeremy L Thompson PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_file, FILE_MODE_READ, &viewer)); 220530ad8c4SKenneth E. Jansen PetscCall(LoadFluidsBinaryVec(comm, viewer, Q, &app_ctx->cont_time, &app_ctx->cont_steps)); 2212b730f8bSJeremy L Thompson PetscCall(PetscViewerDestroy(&viewer)); 22277841947SLeila Ghaffari 22377841947SLeila Ghaffari PetscFunctionReturn(0); 22477841947SLeila Ghaffari } 22577841947SLeila Ghaffari 22677841947SLeila Ghaffari // Record boundary values from initial condition 22777841947SLeila Ghaffari PetscErrorCode SetBCsFromICs_NS(DM dm, Vec Q, Vec Q_loc) { 2285571c6fdSJames Wright Vec Qbc, boundary_mask; 22977841947SLeila Ghaffari PetscFunctionBegin; 23077841947SLeila Ghaffari 2312b730f8bSJeremy L Thompson PetscCall(DMGetNamedLocalVector(dm, "Qbc", &Qbc)); 2322b730f8bSJeremy L Thompson PetscCall(VecCopy(Q_loc, Qbc)); 2332b730f8bSJeremy L Thompson PetscCall(VecZeroEntries(Q_loc)); 2342b730f8bSJeremy L Thompson PetscCall(DMGlobalToLocal(dm, Q, INSERT_VALUES, Q_loc)); 2352b730f8bSJeremy L Thompson PetscCall(VecAXPY(Qbc, -1., Q_loc)); 2362b730f8bSJeremy L Thompson PetscCall(DMRestoreNamedLocalVector(dm, "Qbc", &Qbc)); 2372b730f8bSJeremy L Thompson PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_NS)); 23877841947SLeila Ghaffari 2395571c6fdSJames Wright PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask)); 2405571c6fdSJames Wright PetscCall(DMGetGlobalVector(dm, &Q)); 2415571c6fdSJames Wright PetscCall(VecZeroEntries(boundary_mask)); 2425571c6fdSJames Wright PetscCall(VecSet(Q, 1.0)); 2435571c6fdSJames Wright PetscCall(DMGlobalToLocal(dm, Q, INSERT_VALUES, boundary_mask)); 2445571c6fdSJames Wright PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask)); 2455571c6fdSJames Wright 24677841947SLeila Ghaffari PetscFunctionReturn(0); 24777841947SLeila Ghaffari } 248841e4c73SJed Brown 249841e4c73SJed Brown // Free a plain data context that was allocated using PETSc; returning libCEED error codes 250841e4c73SJed Brown int FreeContextPetsc(void *data) { 2512b730f8bSJeremy L Thompson if (PetscFree(data)) return CeedError(NULL, CEED_ERROR_ACCESS, "PetscFree failed"); 252841e4c73SJed Brown return CEED_ERROR_SUCCESS; 253841e4c73SJed Brown } 254ef080ff9SJames Wright 255ef080ff9SJames Wright // Return mass qfunction specification for number of components N 256ef080ff9SJames Wright PetscErrorCode CreateMassQFunction(Ceed ceed, CeedInt N, CeedInt q_data_size, CeedQFunction *qf) { 257ef080ff9SJames Wright PetscFunctionBeginUser; 258ef080ff9SJames Wright 259ef080ff9SJames Wright switch (N) { 260ef080ff9SJames Wright case 1: 26183ae4962SJames Wright CeedQFunctionCreateInterior(ceed, 1, Mass_1, Mass_1_loc, qf); 262ef080ff9SJames Wright break; 263ef080ff9SJames Wright case 5: 26483ae4962SJames Wright CeedQFunctionCreateInterior(ceed, 1, Mass_5, Mass_5_loc, qf); 265ef080ff9SJames Wright break; 266052409adSJames Wright case 7: 26783ae4962SJames Wright CeedQFunctionCreateInterior(ceed, 1, Mass_7, Mass_7_loc, qf); 268052409adSJames Wright break; 269ef080ff9SJames Wright case 9: 27083ae4962SJames Wright CeedQFunctionCreateInterior(ceed, 1, Mass_9, Mass_9_loc, qf); 271ef080ff9SJames Wright break; 272ef080ff9SJames Wright case 22: 27383ae4962SJames Wright CeedQFunctionCreateInterior(ceed, 1, Mass_22, Mass_22_loc, qf); 274ef080ff9SJames Wright break; 275ef080ff9SJames Wright default: 27683ae4962SJames Wright SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "Could not find mass qfunction of size %d", N); 277ef080ff9SJames Wright } 278ef080ff9SJames Wright 279ef080ff9SJames Wright CeedQFunctionAddInput(*qf, "u", N, CEED_EVAL_INTERP); 280ef080ff9SJames Wright CeedQFunctionAddInput(*qf, "qdata", q_data_size, CEED_EVAL_NONE); 281ef080ff9SJames Wright CeedQFunctionAddOutput(*qf, "v", N, CEED_EVAL_INTERP); 282ef080ff9SJames Wright PetscFunctionReturn(0); 283ef080ff9SJames Wright } 2840df12fefSJames Wright 2850df12fefSJames Wright /* @brief L^2 Projection of a source FEM function to a target FEM space 2860df12fefSJames Wright * 2870df12fefSJames Wright * To solve system using a lumped mass matrix, pass a KSP object with ksp_type=preonly, pc_type=jacobi, pc_jacobi_type=rowsum. 2880df12fefSJames Wright * 2890df12fefSJames Wright * @param[in] source_vec Global Vec of the source FEM function. NULL indicates using rhs_matop_ctx->X_loc 2900df12fefSJames Wright * @param[out] target_vec Global Vec of the target (result) FEM function. NULL indicates using rhs_matop_ctx->Y_loc 2910df12fefSJames Wright * @param[in] rhs_matop_ctx MatopApplyContext for performing the RHS evaluation 2920df12fefSJames Wright * @param[in] ksp KSP for solving the consistent projection problem 2930df12fefSJames Wright */ 2944021610dSJames Wright PetscErrorCode ComputeL2Projection(Vec source_vec, Vec target_vec, OperatorApplyContext rhs_matop_ctx, KSP ksp) { 2950df12fefSJames Wright PetscFunctionBeginUser; 2960df12fefSJames Wright 2974021610dSJames Wright PetscCall(ApplyCeedOperatorGlobalToGlobal(source_vec, target_vec, rhs_matop_ctx)); 2980df12fefSJames Wright PetscCall(KSPSolve(ksp, target_vec, target_vec)); 2990df12fefSJames Wright 3000df12fefSJames Wright PetscFunctionReturn(0); 3010df12fefSJames Wright } 3028b892a05SJames Wright 303999ff5c7SJames Wright PetscErrorCode NodalProjectionDataDestroy(NodalProjectionData context) { 304999ff5c7SJames Wright PetscFunctionBeginUser; 305999ff5c7SJames Wright if (context == NULL) PetscFunctionReturn(0); 306999ff5c7SJames Wright 307999ff5c7SJames Wright PetscCall(DMDestroy(&context->dm)); 308999ff5c7SJames Wright PetscCall(KSPDestroy(&context->ksp)); 309999ff5c7SJames Wright 310999ff5c7SJames Wright PetscCall(OperatorApplyContextDestroy(context->l2_rhs_ctx)); 311999ff5c7SJames Wright 312999ff5c7SJames Wright PetscCall(PetscFree(context)); 313999ff5c7SJames Wright 314999ff5c7SJames Wright PetscFunctionReturn(0); 315999ff5c7SJames Wright } 316999ff5c7SJames Wright 3178b892a05SJames Wright /* 3188b892a05SJames Wright * @brief Open a PHASTA *.dat file, grabbing dimensions and file pointer 3198b892a05SJames Wright * 3208b892a05SJames Wright * This function opens the file specified by `path` using `PetscFOpen` and passes the file pointer in `fp`. 3218b892a05SJames Wright * It is not closed in this function, thus `fp` must be closed sometime after this function has been called (using `PetscFClose` for example). 3228b892a05SJames Wright * 3238b892a05SJames 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. 3248b892a05SJames Wright * 3258b892a05SJames Wright * @param[in] comm MPI_Comm for the program 3268b892a05SJames Wright * @param[in] path Path to the file 3278b892a05SJames Wright * @param[in] char_array_len Length of the character array that should contain each line 3288b892a05SJames Wright * @param[out] dims Dimensions of the file, taken from the first line of the file 3298b892a05SJames Wright * @param[out] fp File pointer to the opened file 3308b892a05SJames Wright */ 3318b892a05SJames Wright PetscErrorCode PHASTADatFileOpen(const MPI_Comm comm, const char path[PETSC_MAX_PATH_LEN], const PetscInt char_array_len, PetscInt dims[2], 3328b892a05SJames Wright FILE **fp) { 3338b892a05SJames Wright PetscInt ndims; 3348b892a05SJames Wright char line[char_array_len]; 3358b892a05SJames Wright char **array; 3368b892a05SJames Wright 3378b892a05SJames Wright PetscFunctionBeginUser; 3388b892a05SJames Wright PetscCall(PetscFOpen(comm, path, "r", fp)); 3398b892a05SJames Wright PetscCall(PetscSynchronizedFGets(comm, *fp, char_array_len, line)); 3408b892a05SJames Wright PetscCall(PetscStrToArray(line, ' ', &ndims, &array)); 3410e654f56SJames Wright PetscCheck(ndims == 2, comm, PETSC_ERR_FILE_UNEXPECTED, "Found %" PetscInt_FMT " dimensions instead of 2 on the first line of %s", ndims, path); 3428b892a05SJames Wright 3438b892a05SJames Wright for (PetscInt i = 0; i < ndims; i++) dims[i] = atoi(array[i]); 3448b892a05SJames Wright PetscCall(PetscStrToArrayDestroy(ndims, array)); 3458b892a05SJames Wright 3468b892a05SJames Wright PetscFunctionReturn(0); 3478b892a05SJames Wright } 3488b892a05SJames Wright 3498b892a05SJames Wright /* 3508b892a05SJames Wright * @brief Get the number of rows for the PHASTA file at path. 3518b892a05SJames Wright * 3528b892a05SJames 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. 3538b892a05SJames Wright * 3548b892a05SJames Wright * @param[in] comm MPI_Comm for the program 3558b892a05SJames Wright * @param[in] path Path to the file 3568b892a05SJames Wright * @param[out] nrows Number of rows 3578b892a05SJames Wright */ 3588b892a05SJames Wright PetscErrorCode PHASTADatFileGetNRows(const MPI_Comm comm, const char path[PETSC_MAX_PATH_LEN], PetscInt *nrows) { 3598b892a05SJames Wright const PetscInt char_array_len = 512; 3608b892a05SJames Wright PetscInt dims[2]; 3618b892a05SJames Wright FILE *fp; 3628b892a05SJames Wright 3638b892a05SJames Wright PetscFunctionBeginUser; 3648b892a05SJames Wright PetscCall(PHASTADatFileOpen(comm, path, char_array_len, dims, &fp)); 3658b892a05SJames Wright *nrows = dims[0]; 3668b892a05SJames Wright PetscCall(PetscFClose(comm, fp)); 3678b892a05SJames Wright 3688b892a05SJames Wright PetscFunctionReturn(0); 3698b892a05SJames Wright } 3704cc9442bSJames Wright 3714cc9442bSJames Wright PetscErrorCode PHASTADatFileReadToArrayReal(MPI_Comm comm, const char path[PETSC_MAX_PATH_LEN], PetscReal array[]) { 3724cc9442bSJames Wright PetscInt ndims, dims[2]; 3734cc9442bSJames Wright FILE *fp; 3744cc9442bSJames Wright const PetscInt char_array_len = 512; 3754cc9442bSJames Wright char line[char_array_len]; 3764cc9442bSJames Wright char **row_array; 3774cc9442bSJames Wright PetscFunctionBeginUser; 3784cc9442bSJames Wright 3794cc9442bSJames Wright PetscCall(PHASTADatFileOpen(comm, path, char_array_len, dims, &fp)); 3804cc9442bSJames Wright 3814cc9442bSJames Wright for (PetscInt i = 0; i < dims[0]; i++) { 3824cc9442bSJames Wright PetscCall(PetscSynchronizedFGets(comm, fp, char_array_len, line)); 3834cc9442bSJames Wright PetscCall(PetscStrToArray(line, ' ', &ndims, &row_array)); 3840e654f56SJames Wright PetscCheck(ndims == dims[1], comm, PETSC_ERR_FILE_UNEXPECTED, 3850e654f56SJames Wright "Line %" PetscInt_FMT " of %s does not contain enough columns (%" PetscInt_FMT " instead of %" PetscInt_FMT ")", i, path, ndims, 3860e654f56SJames Wright dims[1]); 3874cc9442bSJames Wright 3884cc9442bSJames Wright for (PetscInt j = 0; j < dims[1]; j++) { 3894cc9442bSJames Wright array[i * dims[1] + j] = (PetscReal)atof(row_array[j]); 3904cc9442bSJames Wright } 3914cc9442bSJames Wright } 3924cc9442bSJames Wright 3934cc9442bSJames Wright PetscCall(PetscFClose(comm, fp)); 3944cc9442bSJames Wright 3954cc9442bSJames Wright PetscFunctionReturn(0); 3964cc9442bSJames Wright } 397*75d1798cSJames Wright 398*75d1798cSJames Wright PetscLogEvent FLUIDS_CeedOperatorApply; 399*75d1798cSJames Wright PetscLogEvent FLUIDS_CeedOperatorAssemble; 400*75d1798cSJames Wright PetscLogEvent FLUIDS_CeedOperatorAssembleDiagonal; 401*75d1798cSJames Wright PetscLogEvent FLUIDS_CeedOperatorAssemblePointBlockDiagonal; 402*75d1798cSJames Wright static PetscClassId libCEED_classid; 403*75d1798cSJames Wright 404*75d1798cSJames Wright PetscErrorCode RegisterLogEvents() { 405*75d1798cSJames Wright PetscFunctionBeginUser; 406*75d1798cSJames Wright PetscCall(PetscClassIdRegister("libCEED", &libCEED_classid)); 407*75d1798cSJames Wright PetscCall(PetscLogEventRegister("CeedOpApply", libCEED_classid, &FLUIDS_CeedOperatorApply)); 408*75d1798cSJames Wright PetscCall(PetscLogEventRegister("CeedOpAsm", libCEED_classid, &FLUIDS_CeedOperatorAssemble)); 409*75d1798cSJames Wright PetscCall(PetscLogEventRegister("CeedOpAsmD", libCEED_classid, &FLUIDS_CeedOperatorAssembleDiagonal)); 410*75d1798cSJames Wright PetscCall(PetscLogEventRegister("CeedOpAsmPBD", libCEED_classid, &FLUIDS_CeedOperatorAssemblePointBlockDiagonal)); 411*75d1798cSJames Wright PetscFunctionReturn(0); 412*75d1798cSJames Wright } 413