1727da7e7SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2727da7e7SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3a515125bSLeila Ghaffari // 4727da7e7SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5a515125bSLeila Ghaffari // 6727da7e7SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7a515125bSLeila Ghaffari 8a515125bSLeila Ghaffari /// @file 9a515125bSLeila Ghaffari /// Miscellaneous utility functions 10a515125bSLeila Ghaffari 11a515125bSLeila Ghaffari #include "../navierstokes.h" 129f59f36eSJames Wright #include "../qfunctions/mass.h" 13a515125bSLeila Ghaffari 142b916ea7SJeremy L Thompson PetscErrorCode ICs_FixMultiplicity(DM dm, CeedData ceed_data, User user, Vec Q_loc, Vec Q, CeedScalar time) { 15a515125bSLeila Ghaffari PetscFunctionBeginUser; 16a515125bSLeila Ghaffari 17a515125bSLeila Ghaffari // --------------------------------------------------------------------------- 18b7f03f12SJed Brown // Update time for evaluation 19a515125bSLeila Ghaffari // --------------------------------------------------------------------------- 202b916ea7SJeremy L Thompson if (user->phys->ics_time_label) CeedOperatorContextSetDouble(ceed_data->op_ics, user->phys->ics_time_label, &time); 21a515125bSLeila Ghaffari 22a515125bSLeila Ghaffari // --------------------------------------------------------------------------- 23a515125bSLeila Ghaffari // ICs 24a515125bSLeila Ghaffari // --------------------------------------------------------------------------- 25a515125bSLeila Ghaffari // -- CEED Restriction 26a515125bSLeila Ghaffari CeedVector q0_ceed; 27a515125bSLeila Ghaffari CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &q0_ceed, NULL); 28a515125bSLeila Ghaffari 29a515125bSLeila Ghaffari // -- Place PETSc vector in CEED vector 30a515125bSLeila Ghaffari PetscMemType q0_mem_type; 31*fd969b44SJames Wright PetscCall(VecP2C(Q_loc, &q0_mem_type, q0_ceed)); 32a515125bSLeila Ghaffari 33a515125bSLeila Ghaffari // -- Apply CEED Operator 342b916ea7SJeremy L Thompson CeedOperatorApply(ceed_data->op_ics, ceed_data->x_coord, q0_ceed, CEED_REQUEST_IMMEDIATE); 35a515125bSLeila Ghaffari 36a515125bSLeila Ghaffari // -- Restore vectors 37*fd969b44SJames Wright PetscCall(VecC2P(q0_ceed, q0_mem_type, Q_loc)); 38a515125bSLeila Ghaffari 39a515125bSLeila Ghaffari // -- Local-to-Global 402b916ea7SJeremy L Thompson PetscCall(VecZeroEntries(Q)); 412b916ea7SJeremy L Thompson PetscCall(DMLocalToGlobal(dm, Q_loc, ADD_VALUES, Q)); 42a515125bSLeila Ghaffari 43a515125bSLeila Ghaffari // --------------------------------------------------------------------------- 44a515125bSLeila Ghaffari // Fix multiplicity for output of ICs 45a515125bSLeila Ghaffari // --------------------------------------------------------------------------- 46a515125bSLeila Ghaffari // -- CEED Restriction 47a515125bSLeila Ghaffari CeedVector mult_vec; 48a515125bSLeila Ghaffari CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &mult_vec, NULL); 49a515125bSLeila Ghaffari 50a515125bSLeila Ghaffari // -- Place PETSc vector in CEED vector 51a515125bSLeila Ghaffari PetscMemType m_mem_type; 52a515125bSLeila Ghaffari Vec multiplicity_loc; 532b916ea7SJeremy L Thompson PetscCall(DMGetLocalVector(dm, &multiplicity_loc)); 54*fd969b44SJames Wright PetscCall(VecP2C(multiplicity_loc, &m_mem_type, mult_vec)); 55a515125bSLeila Ghaffari 56a515125bSLeila Ghaffari // -- Get multiplicity 57a515125bSLeila Ghaffari CeedElemRestrictionGetMultiplicity(ceed_data->elem_restr_q, mult_vec); 58a515125bSLeila Ghaffari 59a515125bSLeila Ghaffari // -- Restore vectors 60*fd969b44SJames Wright PetscCall(VecC2P(mult_vec, m_mem_type, multiplicity_loc)); 61a515125bSLeila Ghaffari 62a515125bSLeila Ghaffari // -- Local-to-Global 63a515125bSLeila Ghaffari Vec multiplicity; 642b916ea7SJeremy L Thompson PetscCall(DMGetGlobalVector(dm, &multiplicity)); 652b916ea7SJeremy L Thompson PetscCall(VecZeroEntries(multiplicity)); 662b916ea7SJeremy L Thompson PetscCall(DMLocalToGlobal(dm, multiplicity_loc, ADD_VALUES, multiplicity)); 67a515125bSLeila Ghaffari 68a515125bSLeila Ghaffari // -- Fix multiplicity 692b916ea7SJeremy L Thompson PetscCall(VecPointwiseDivide(Q, Q, multiplicity)); 702b916ea7SJeremy L Thompson PetscCall(VecPointwiseDivide(Q_loc, Q_loc, multiplicity_loc)); 71a515125bSLeila Ghaffari 72a515125bSLeila Ghaffari // -- Restore vectors 732b916ea7SJeremy L Thompson PetscCall(DMRestoreLocalVector(dm, &multiplicity_loc)); 742b916ea7SJeremy L Thompson PetscCall(DMRestoreGlobalVector(dm, &multiplicity)); 75a515125bSLeila Ghaffari 76a515125bSLeila Ghaffari // Cleanup 77a515125bSLeila Ghaffari CeedVectorDestroy(&mult_vec); 78a515125bSLeila Ghaffari CeedVectorDestroy(&q0_ceed); 79a515125bSLeila Ghaffari 80a515125bSLeila Ghaffari PetscFunctionReturn(0); 81a515125bSLeila Ghaffari } 82a515125bSLeila Ghaffari 832b916ea7SJeremy L Thompson PetscErrorCode DMPlexInsertBoundaryValues_NS(DM dm, PetscBool insert_essential, Vec Q_loc, PetscReal time, Vec face_geom_FVM, Vec cell_geom_FVM, 842b916ea7SJeremy L Thompson Vec grad_FVM) { 859d437337SJames Wright Vec Qbc, boundary_mask; 86a515125bSLeila Ghaffari PetscFunctionBegin; 87a515125bSLeila Ghaffari 889d437337SJames Wright // Mask (zero) Dirichlet entries 899d437337SJames Wright PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask)); 909d437337SJames Wright PetscCall(VecPointwiseMult(Q_loc, Q_loc, boundary_mask)); 919d437337SJames Wright PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask)); 929d437337SJames Wright 932b916ea7SJeremy L Thompson PetscCall(DMGetNamedLocalVector(dm, "Qbc", &Qbc)); 942b916ea7SJeremy L Thompson PetscCall(VecAXPY(Q_loc, 1., Qbc)); 952b916ea7SJeremy L Thompson PetscCall(DMRestoreNamedLocalVector(dm, "Qbc", &Qbc)); 96a515125bSLeila Ghaffari 97a515125bSLeila Ghaffari PetscFunctionReturn(0); 98a515125bSLeila Ghaffari } 99a515125bSLeila Ghaffari 100a515125bSLeila Ghaffari // Compare reference solution values with current test run for CI 101a515125bSLeila Ghaffari PetscErrorCode RegressionTests_NS(AppCtx app_ctx, Vec Q) { 102a515125bSLeila Ghaffari Vec Qref; 103a515125bSLeila Ghaffari PetscViewer viewer; 104a515125bSLeila Ghaffari PetscReal error, Qrefnorm; 105a515125bSLeila Ghaffari PetscFunctionBegin; 106a515125bSLeila Ghaffari 107a515125bSLeila Ghaffari // Read reference file 1082b916ea7SJeremy L Thompson PetscCall(VecDuplicate(Q, &Qref)); 109c931fa59SJames Wright PetscCall(PetscViewerBinaryOpen(PetscObjectComm((PetscObject)Q), app_ctx->test_file_path, FILE_MODE_READ, &viewer)); 1102b916ea7SJeremy L Thompson PetscCall(VecLoad(Qref, viewer)); 111a515125bSLeila Ghaffari 112a515125bSLeila Ghaffari // Compute error with respect to reference solution 1132b916ea7SJeremy L Thompson PetscCall(VecAXPY(Q, -1.0, Qref)); 1142b916ea7SJeremy L Thompson PetscCall(VecNorm(Qref, NORM_MAX, &Qrefnorm)); 1152b916ea7SJeremy L Thompson PetscCall(VecScale(Q, 1. / Qrefnorm)); 1162b916ea7SJeremy L Thompson PetscCall(VecNorm(Q, NORM_MAX, &error)); 117a515125bSLeila Ghaffari 118a515125bSLeila Ghaffari // Check error 119a515125bSLeila Ghaffari if (error > app_ctx->test_tol) { 1202b916ea7SJeremy L Thompson PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test failed with error norm %g\n", (double)error)); 121a515125bSLeila Ghaffari } 122a515125bSLeila Ghaffari 123a515125bSLeila Ghaffari // Cleanup 1242b916ea7SJeremy L Thompson PetscCall(PetscViewerDestroy(&viewer)); 1252b916ea7SJeremy L Thompson PetscCall(VecDestroy(&Qref)); 126a515125bSLeila Ghaffari 127a515125bSLeila Ghaffari PetscFunctionReturn(0); 128a515125bSLeila Ghaffari } 129a515125bSLeila Ghaffari 130a515125bSLeila Ghaffari // Get error for problems with exact solutions 1312b916ea7SJeremy L Thompson PetscErrorCode GetError_NS(CeedData ceed_data, DM dm, User user, Vec Q, PetscScalar final_time) { 132a515125bSLeila Ghaffari PetscInt loc_nodes; 133a515125bSLeila Ghaffari Vec Q_exact, Q_exact_loc; 134a515125bSLeila Ghaffari PetscReal rel_error, norm_error, norm_exact; 135a515125bSLeila Ghaffari PetscFunctionBegin; 136a515125bSLeila Ghaffari 137a515125bSLeila Ghaffari // Get exact solution at final time 1382b916ea7SJeremy L Thompson PetscCall(DMCreateGlobalVector(dm, &Q_exact)); 1392b916ea7SJeremy L Thompson PetscCall(DMGetLocalVector(dm, &Q_exact_loc)); 1402b916ea7SJeremy L Thompson PetscCall(VecGetSize(Q_exact_loc, &loc_nodes)); 1412b916ea7SJeremy L Thompson PetscCall(ICs_FixMultiplicity(dm, ceed_data, user, Q_exact_loc, Q_exact, final_time)); 142a515125bSLeila Ghaffari 143a515125bSLeila Ghaffari // Get |exact solution - obtained solution| 1442b916ea7SJeremy L Thompson PetscCall(VecNorm(Q_exact, NORM_1, &norm_exact)); 1452b916ea7SJeremy L Thompson PetscCall(VecAXPY(Q, -1.0, Q_exact)); 1462b916ea7SJeremy L Thompson PetscCall(VecNorm(Q, NORM_1, &norm_error)); 147a515125bSLeila Ghaffari 148a515125bSLeila Ghaffari // Compute relative error 149a515125bSLeila Ghaffari rel_error = norm_error / norm_exact; 150a515125bSLeila Ghaffari 151a515125bSLeila Ghaffari // Output relative error 1522b916ea7SJeremy L Thompson PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Relative Error: %g\n", (double)rel_error)); 153a515125bSLeila Ghaffari // Cleanup 1542b916ea7SJeremy L Thompson PetscCall(DMRestoreLocalVector(dm, &Q_exact_loc)); 1552b916ea7SJeremy L Thompson PetscCall(VecDestroy(&Q_exact)); 156a515125bSLeila Ghaffari 157a515125bSLeila Ghaffari PetscFunctionReturn(0); 158a515125bSLeila Ghaffari } 159a515125bSLeila Ghaffari 160a515125bSLeila Ghaffari // Post-processing 1612b916ea7SJeremy L Thompson PetscErrorCode PostProcess_NS(TS ts, CeedData ceed_data, DM dm, ProblemData *problem, User user, Vec Q, PetscScalar final_time) { 162a515125bSLeila Ghaffari PetscInt steps; 163f0784ed3SJed Brown TSConvergedReason reason; 164a515125bSLeila Ghaffari PetscFunctionBegin; 165a515125bSLeila Ghaffari 166a515125bSLeila Ghaffari // Print relative error 1670e1e9333SJames Wright if (problem->non_zero_time && user->app_ctx->test_type == TESTTYPE_NONE) { 1682b916ea7SJeremy L Thompson PetscCall(GetError_NS(ceed_data, dm, user, Q, final_time)); 169a515125bSLeila Ghaffari } 170a515125bSLeila Ghaffari 171a515125bSLeila Ghaffari // Print final time and number of steps 1722b916ea7SJeremy L Thompson PetscCall(TSGetStepNumber(ts, &steps)); 173f0784ed3SJed Brown PetscCall(TSGetConvergedReason(ts, &reason)); 1740e1e9333SJames Wright if (user->app_ctx->test_type == TESTTYPE_NONE) { 175f0784ed3SJed Brown PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time integrator %s on time step %" PetscInt_FMT " with final time %g\n", TSConvergedReasons[reason], 176f0784ed3SJed Brown steps, (double)final_time)); 177a515125bSLeila Ghaffari } 178a515125bSLeila Ghaffari 179a515125bSLeila Ghaffari // Output numerical values from command line 1802b916ea7SJeremy L Thompson PetscCall(VecViewFromOptions(Q, NULL, "-vec_view")); 181a515125bSLeila Ghaffari 182a515125bSLeila Ghaffari // Compare reference solution values with current test run for CI 1830e1e9333SJames Wright if (user->app_ctx->test_type == TESTTYPE_SOLVER) { 1842b916ea7SJeremy L Thompson PetscCall(RegressionTests_NS(user->app_ctx, Q)); 185a515125bSLeila Ghaffari } 186a515125bSLeila Ghaffari 187a515125bSLeila Ghaffari PetscFunctionReturn(0); 188a515125bSLeila Ghaffari } 189a515125bSLeila Ghaffari 1909293eaa1SJed Brown const PetscInt FLUIDS_FILE_TOKEN = 0xceedf00; 1919293eaa1SJed Brown 192a515125bSLeila Ghaffari // Gather initial Q values in case of continuation of simulation 193a515125bSLeila Ghaffari PetscErrorCode SetupICsFromBinary(MPI_Comm comm, AppCtx app_ctx, Vec Q) { 194a515125bSLeila Ghaffari PetscViewer viewer; 1959293eaa1SJed Brown PetscInt token, step_number; 1969293eaa1SJed Brown PetscReal time; 1972b916ea7SJeremy L Thompson 198a515125bSLeila Ghaffari PetscFunctionBegin; 199a515125bSLeila Ghaffari 200a515125bSLeila Ghaffari // Read input 2012b916ea7SJeremy L Thompson PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_file, FILE_MODE_READ, &viewer)); 202a515125bSLeila Ghaffari 2039293eaa1SJed Brown // Attempt 2049293eaa1SJed Brown PetscCall(PetscViewerBinaryRead(viewer, &token, 1, NULL, PETSC_INT)); 2059293eaa1SJed Brown if (token == FLUIDS_FILE_TOKEN) { // New style format; we're reading a file with step number and time in the header 2069293eaa1SJed Brown PetscCall(PetscViewerBinaryRead(viewer, &step_number, 1, NULL, PETSC_INT)); 2079293eaa1SJed Brown PetscCall(PetscViewerBinaryRead(viewer, &time, 1, NULL, PETSC_REAL)); 2089293eaa1SJed Brown app_ctx->cont_steps = step_number; 2099293eaa1SJed Brown app_ctx->cont_time = time; 2109293eaa1SJed Brown } else if (token == VEC_FILE_CLASSID) { // Legacy format of just the vector, encoded as [VEC_FILE_CLASSID, length, ] 2119293eaa1SJed Brown PetscInt length, N; 2129293eaa1SJed Brown PetscCall(PetscViewerBinaryRead(viewer, &length, 1, NULL, PETSC_INT)); 2139293eaa1SJed Brown PetscCall(VecGetSize(Q, &N)); 2149293eaa1SJed Brown PetscCheck(length == N, comm, PETSC_ERR_ARG_INCOMP, "File Vec has length %" PetscInt_FMT " but DM has global Vec size %" PetscInt_FMT, length, N); 2159293eaa1SJed Brown PetscCall(PetscViewerBinarySetSkipHeader(viewer, PETSC_TRUE)); 2169293eaa1SJed Brown } else SETERRQ(comm, PETSC_ERR_FILE_UNEXPECTED, "Not a fluids header token or a PETSc Vec in file"); 2179293eaa1SJed Brown 218a515125bSLeila Ghaffari // Load Q from existent solution 2192b916ea7SJeremy L Thompson PetscCall(VecLoad(Q, viewer)); 220a515125bSLeila Ghaffari 221a515125bSLeila Ghaffari // Cleanup 2222b916ea7SJeremy L Thompson PetscCall(PetscViewerDestroy(&viewer)); 223a515125bSLeila Ghaffari 224a515125bSLeila Ghaffari PetscFunctionReturn(0); 225a515125bSLeila Ghaffari } 226a515125bSLeila Ghaffari 227a515125bSLeila Ghaffari // Record boundary values from initial condition 228a515125bSLeila Ghaffari PetscErrorCode SetBCsFromICs_NS(DM dm, Vec Q, Vec Q_loc) { 2299d437337SJames Wright Vec Qbc, boundary_mask; 230a515125bSLeila Ghaffari PetscFunctionBegin; 231a515125bSLeila Ghaffari 2322b916ea7SJeremy L Thompson PetscCall(DMGetNamedLocalVector(dm, "Qbc", &Qbc)); 2332b916ea7SJeremy L Thompson PetscCall(VecCopy(Q_loc, Qbc)); 2342b916ea7SJeremy L Thompson PetscCall(VecZeroEntries(Q_loc)); 2352b916ea7SJeremy L Thompson PetscCall(DMGlobalToLocal(dm, Q, INSERT_VALUES, Q_loc)); 2362b916ea7SJeremy L Thompson PetscCall(VecAXPY(Qbc, -1., Q_loc)); 2372b916ea7SJeremy L Thompson PetscCall(DMRestoreNamedLocalVector(dm, "Qbc", &Qbc)); 2382b916ea7SJeremy L Thompson PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_NS)); 239a515125bSLeila Ghaffari 2409d437337SJames Wright PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask)); 2419d437337SJames Wright PetscCall(DMGetGlobalVector(dm, &Q)); 2429d437337SJames Wright PetscCall(VecZeroEntries(boundary_mask)); 2439d437337SJames Wright PetscCall(VecSet(Q, 1.0)); 2449d437337SJames Wright PetscCall(DMGlobalToLocal(dm, Q, INSERT_VALUES, boundary_mask)); 2459d437337SJames Wright PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask)); 2469d437337SJames Wright 247a515125bSLeila Ghaffari PetscFunctionReturn(0); 248a515125bSLeila Ghaffari } 24915a3537eSJed Brown 25015a3537eSJed Brown // Free a plain data context that was allocated using PETSc; returning libCEED error codes 25115a3537eSJed Brown int FreeContextPetsc(void *data) { 2522b916ea7SJeremy L Thompson if (PetscFree(data)) return CeedError(NULL, CEED_ERROR_ACCESS, "PetscFree failed"); 25315a3537eSJed Brown return CEED_ERROR_SUCCESS; 25415a3537eSJed Brown } 2559f59f36eSJames Wright 2569f59f36eSJames Wright // Return mass qfunction specification for number of components N 2579f59f36eSJames Wright PetscErrorCode CreateMassQFunction(Ceed ceed, CeedInt N, CeedInt q_data_size, CeedQFunction *qf) { 2589f59f36eSJames Wright CeedQFunctionUser qfunction_ptr; 2599f59f36eSJames Wright const char *qfunction_loc; 2609f59f36eSJames Wright PetscFunctionBeginUser; 2619f59f36eSJames Wright 2629f59f36eSJames Wright switch (N) { 2639f59f36eSJames Wright case 1: 2649f59f36eSJames Wright qfunction_ptr = Mass_1; 2659f59f36eSJames Wright qfunction_loc = Mass_1_loc; 2669f59f36eSJames Wright break; 2679f59f36eSJames Wright case 5: 2689f59f36eSJames Wright qfunction_ptr = Mass_5; 2699f59f36eSJames Wright qfunction_loc = Mass_5_loc; 2709f59f36eSJames Wright break; 2719f59f36eSJames Wright case 9: 2729f59f36eSJames Wright qfunction_ptr = Mass_9; 2739f59f36eSJames Wright qfunction_loc = Mass_9_loc; 2749f59f36eSJames Wright break; 2759f59f36eSJames Wright case 22: 2769f59f36eSJames Wright qfunction_ptr = Mass_22; 2779f59f36eSJames Wright qfunction_loc = Mass_22_loc; 2789f59f36eSJames Wright break; 2799f59f36eSJames Wright default: 2809f59f36eSJames Wright SETERRQ(PETSC_COMM_WORLD, -1, "Could not find mass qfunction of size %d", N); 2819f59f36eSJames Wright } 2829f59f36eSJames Wright CeedQFunctionCreateInterior(ceed, 1, qfunction_ptr, qfunction_loc, qf); 2839f59f36eSJames Wright 2849f59f36eSJames Wright CeedQFunctionAddInput(*qf, "u", N, CEED_EVAL_INTERP); 2859f59f36eSJames Wright CeedQFunctionAddInput(*qf, "qdata", q_data_size, CEED_EVAL_NONE); 2869f59f36eSJames Wright CeedQFunctionAddOutput(*qf, "v", N, CEED_EVAL_INTERP); 2879f59f36eSJames Wright PetscFunctionReturn(0); 2889f59f36eSJames Wright } 289