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 /// Time-stepping functions for Navier-Stokes example using PETSc 10a515125bSLeila Ghaffari 11e419654dSJeremy L Thompson #include <ceed.h> 12e419654dSJeremy L Thompson #include <petscdmplex.h> 13e419654dSJeremy L Thompson #include <petscts.h> 14e419654dSJeremy L Thompson 15a515125bSLeila Ghaffari #include "../navierstokes.h" 16c5e9980aSAdeleke O. Bankole #include "../qfunctions/newtonian_state.h" 17a515125bSLeila Ghaffari 18*f8e2d240SJames Wright // @brief Create KSP to solve the inverse mass operator for explicit time stepping schemes 19*f8e2d240SJames Wright PetscErrorCode CreateKspMassOperator(User user, CeedData ceed_data) { 20*f8e2d240SJames Wright Ceed ceed = user->ceed; 21*f8e2d240SJames Wright DM dm = user->dm; 22a515125bSLeila Ghaffari CeedQFunction qf_mass; 23a515125bSLeila Ghaffari CeedOperator op_mass; 24*f8e2d240SJames Wright OperatorApplyContext mass_matop_ctx; 25a515125bSLeila Ghaffari CeedInt num_comp_q, q_data_size; 26a515125bSLeila Ghaffari 2706f41313SJames Wright PetscFunctionBeginUser; 28b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_q, &num_comp_q)); 29b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size)); 30a515125bSLeila Ghaffari 319f59f36eSJames Wright PetscCall(CreateMassQFunction(ceed, num_comp_q, q_data_size, &qf_mass)); 32b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass)); 33b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "u", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE)); 3458e1cbfdSJeremy L Thompson PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_NONE, ceed_data->q_data)); 35b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE)); 36a515125bSLeila Ghaffari 37*f8e2d240SJames Wright { // -- Setup KSP for mass operator 38*f8e2d240SJames Wright Mat mat_mass; 39*f8e2d240SJames Wright Vec Ones_loc; 40*f8e2d240SJames Wright MPI_Comm comm = PetscObjectComm((PetscObject)dm); 41a515125bSLeila Ghaffari 42*f8e2d240SJames Wright PetscCall(DMCreateLocalVector(dm, &Ones_loc)); 430143e3daSJames Wright PetscCall(VecSet(Ones_loc, 1)); 44*f8e2d240SJames Wright PetscCall(OperatorApplyContextCreate(dm, dm, ceed, op_mass, NULL, NULL, Ones_loc, NULL, &mass_matop_ctx)); 45*f8e2d240SJames Wright PetscCall(CreateMatShell_Ceed(mass_matop_ctx, &mat_mass)); 46a515125bSLeila Ghaffari 47*f8e2d240SJames Wright PetscCall(KSPCreate(comm, &user->mass_ksp)); 48*f8e2d240SJames Wright PetscCall(KSPSetOptionsPrefix(user->mass_ksp, "mass_")); 49*f8e2d240SJames Wright { // lumped by default 50*f8e2d240SJames Wright PC pc; 51*f8e2d240SJames Wright PetscCall(KSPGetPC(user->mass_ksp, &pc)); 52*f8e2d240SJames Wright PetscCall(PCSetType(pc, PCJACOBI)); 53*f8e2d240SJames Wright PetscCall(PCJacobiSetType(pc, PC_JACOBI_ROWSUM)); 54*f8e2d240SJames Wright PetscCall(KSPSetType(user->mass_ksp, KSPPREONLY)); 55*f8e2d240SJames Wright } 56*f8e2d240SJames Wright PetscCall(KSPSetOperators(user->mass_ksp, mat_mass, mat_mass)); 57*f8e2d240SJames Wright PetscCall(KSPSetFromOptions(user->mass_ksp)); 58*f8e2d240SJames Wright PetscCall(VecDestroy(&Ones_loc)); 59*f8e2d240SJames Wright } 60a515125bSLeila Ghaffari 61a515125bSLeila Ghaffari // Cleanup 62b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass)); 63b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorDestroy(&op_mass)); 64d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 65a515125bSLeila Ghaffari } 66a515125bSLeila Ghaffari 67c996854bSJames Wright // Insert Boundary values if it's a new time 68c996854bSJames Wright PetscErrorCode UpdateBoundaryValues(User user, Vec Q_loc, PetscReal t) { 69c996854bSJames Wright PetscFunctionBeginUser; 70c996854bSJames Wright if (user->time_bc_set != t) { 71c996854bSJames Wright PetscCall(DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL)); 72c996854bSJames Wright user->time_bc_set = t; 73c996854bSJames Wright } 74d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 75c996854bSJames Wright } 76c996854bSJames Wright 77a515125bSLeila Ghaffari // RHS (Explicit time-stepper) function setup 78a515125bSLeila Ghaffari // This is the RHS of the ODE, given as u_t = G(t,u) 79a515125bSLeila Ghaffari // This function takes in a state vector Q and writes into G 80a515125bSLeila Ghaffari PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data) { 81a515125bSLeila Ghaffari User user = *(User *)user_data; 82701e5830SJames Wright Ceed ceed = user->ceed; 83fd969b44SJames Wright PetscScalar dt; 84da5fe0e4SJames Wright Vec Q_loc = user->Q_loc; 85a515125bSLeila Ghaffari 8606f41313SJames Wright PetscFunctionBeginUser; 87e2f84137SJeremy L Thompson // Update time dependent data 88c996854bSJames Wright PetscCall(UpdateBoundaryValues(user, Q_loc, t)); 89701e5830SJames Wright if (user->phys->solution_time_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_rhs_ctx->op, user->phys->solution_time_label, &t)); 902b916ea7SJeremy L Thompson PetscCall(TSGetTimeStep(ts, &dt)); 91701e5830SJames Wright if (user->phys->timestep_size_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_rhs_ctx->op, user->phys->timestep_size_label, &dt)); 92a515125bSLeila Ghaffari 93da5fe0e4SJames Wright PetscCall(ApplyCeedOperatorGlobalToGlobal(Q, G, user->op_rhs_ctx)); 94a515125bSLeila Ghaffari 95f3fcf8f4SJames Wright // Inverse of the lumped mass matrix 96*f8e2d240SJames Wright PetscCall(KSPSolve(user->mass_ksp, G, G)); 97d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 98a515125bSLeila Ghaffari } 99a515125bSLeila Ghaffari 100c5e9980aSAdeleke O. Bankole // Surface forces function setup 101c5e9980aSAdeleke O. Bankole static PetscErrorCode Surface_Forces_NS(DM dm, Vec G_loc, PetscInt num_walls, const PetscInt walls[], PetscScalar *reaction_force) { 102c5e9980aSAdeleke O. Bankole DMLabel face_label; 103c5e9980aSAdeleke O. Bankole const PetscScalar *g; 1042004e3acSAdeleke O. Bankole PetscInt dof, dim = 3; 105c5e9980aSAdeleke O. Bankole MPI_Comm comm; 1062004e3acSAdeleke O. Bankole PetscSection s; 107c5e9980aSAdeleke O. Bankole 108c5e9980aSAdeleke O. Bankole PetscFunctionBeginUser; 109c5e9980aSAdeleke O. Bankole PetscCall(PetscArrayzero(reaction_force, num_walls * dim)); 110c5e9980aSAdeleke O. Bankole PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 111c5e9980aSAdeleke O. Bankole PetscCall(DMGetLabel(dm, "Face Sets", &face_label)); 112c5e9980aSAdeleke O. Bankole PetscCall(VecGetArrayRead(G_loc, &g)); 113c5e9980aSAdeleke O. Bankole for (PetscInt w = 0; w < num_walls; w++) { 114c5e9980aSAdeleke O. Bankole const PetscInt wall = walls[w]; 115c5e9980aSAdeleke O. Bankole IS wall_is; 1162004e3acSAdeleke O. Bankole PetscCall(DMGetLocalSection(dm, &s)); 117c5e9980aSAdeleke O. Bankole PetscCall(DMLabelGetStratumIS(face_label, wall, &wall_is)); 118c5e9980aSAdeleke O. Bankole if (wall_is) { // There exist such points on this process 119c5e9980aSAdeleke O. Bankole PetscInt num_points; 1202004e3acSAdeleke O. Bankole PetscInt num_comp = 0; 121c5e9980aSAdeleke O. Bankole const PetscInt *points; 1222004e3acSAdeleke O. Bankole PetscCall(PetscSectionGetFieldComponents(s, 0, &num_comp)); 123c5e9980aSAdeleke O. Bankole PetscCall(ISGetSize(wall_is, &num_points)); 124c5e9980aSAdeleke O. Bankole PetscCall(ISGetIndices(wall_is, &points)); 125c5e9980aSAdeleke O. Bankole for (PetscInt i = 0; i < num_points; i++) { 126c5e9980aSAdeleke O. Bankole const PetscInt p = points[i]; 127c5e9980aSAdeleke O. Bankole const StateConservative *r; 128c5e9980aSAdeleke O. Bankole PetscCall(DMPlexPointLocalRead(dm, p, g, &r)); 1292004e3acSAdeleke O. Bankole PetscCall(PetscSectionGetDof(s, p, &dof)); 1302004e3acSAdeleke O. Bankole for (PetscInt node = 0; node < dof / num_comp; node++) { 131c5e9980aSAdeleke O. Bankole for (PetscInt j = 0; j < 3; j++) { 1322004e3acSAdeleke O. Bankole reaction_force[w * dim + j] -= r[node].momentum[j]; 1332004e3acSAdeleke O. Bankole } 134c5e9980aSAdeleke O. Bankole } 135c5e9980aSAdeleke O. Bankole } 136c5e9980aSAdeleke O. Bankole PetscCall(ISRestoreIndices(wall_is, &points)); 137c5e9980aSAdeleke O. Bankole } 138c5e9980aSAdeleke O. Bankole PetscCall(ISDestroy(&wall_is)); 139c5e9980aSAdeleke O. Bankole } 140c5e9980aSAdeleke O. Bankole PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, reaction_force, dim * num_walls, MPIU_SCALAR, MPI_SUM, comm)); 141c5e9980aSAdeleke O. Bankole // Restore Vectors 142c5e9980aSAdeleke O. Bankole PetscCall(VecRestoreArrayRead(G_loc, &g)); 143d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 144c5e9980aSAdeleke O. Bankole } 145c5e9980aSAdeleke O. Bankole 146a515125bSLeila Ghaffari // Implicit time-stepper function setup 1472b916ea7SJeremy L Thompson PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data) { 148a515125bSLeila Ghaffari User user = *(User *)user_data; 149701e5830SJames Wright Ceed ceed = user->ceed; 150fd969b44SJames Wright PetscScalar dt; 151e2f84137SJeremy L Thompson Vec Q_loc = user->Q_loc, Q_dot_loc = user->Q_dot_loc, G_loc; 152a515125bSLeila Ghaffari PetscMemType q_mem_type, q_dot_mem_type, g_mem_type; 153a515125bSLeila Ghaffari 15406f41313SJames Wright PetscFunctionBeginUser; 155e2f84137SJeremy L Thompson // Get local vectors 156c5e9980aSAdeleke O. Bankole PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 157e2f84137SJeremy L Thompson 158e2f84137SJeremy L Thompson // Update time dependent data 159c996854bSJames Wright PetscCall(UpdateBoundaryValues(user, Q_loc, t)); 160701e5830SJames Wright if (user->phys->solution_time_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_ifunction, user->phys->solution_time_label, &t)); 1612b916ea7SJeremy L Thompson PetscCall(TSGetTimeStep(ts, &dt)); 162701e5830SJames Wright if (user->phys->timestep_size_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_ifunction, user->phys->timestep_size_label, &dt)); 163a515125bSLeila Ghaffari 164a515125bSLeila Ghaffari // Global-to-local 16506108310SJames Wright PetscCall(DMGlobalToLocalBegin(user->dm, Q, INSERT_VALUES, Q_loc)); 16606108310SJames Wright PetscCall(DMGlobalToLocalBegin(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc)); 16706108310SJames Wright PetscCall(DMGlobalToLocalEnd(user->dm, Q, INSERT_VALUES, Q_loc)); 16806108310SJames Wright PetscCall(DMGlobalToLocalEnd(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc)); 169a515125bSLeila Ghaffari 170a515125bSLeila Ghaffari // Place PETSc vectors in CEED vectors 171fd969b44SJames Wright PetscCall(VecReadP2C(Q_loc, &q_mem_type, user->q_ceed)); 172fd969b44SJames Wright PetscCall(VecReadP2C(Q_dot_loc, &q_dot_mem_type, user->q_dot_ceed)); 173fd969b44SJames Wright PetscCall(VecP2C(G_loc, &g_mem_type, user->g_ceed)); 174a515125bSLeila Ghaffari 175a515125bSLeila Ghaffari // Apply CEED operator 1767eedc94cSJames Wright PetscCall(PetscLogEventBegin(FLUIDS_CeedOperatorApply, Q, G, 0, 0)); 1777eedc94cSJames Wright PetscCall(PetscLogGpuTimeBegin()); 178b4c37c5cSJames Wright PetscCallCeed(user->ceed, CeedOperatorApply(user->op_ifunction, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE)); 1797eedc94cSJames Wright PetscCall(PetscLogGpuTimeEnd()); 1807eedc94cSJames Wright PetscCall(PetscLogEventEnd(FLUIDS_CeedOperatorApply, Q, G, 0, 0)); 181a515125bSLeila Ghaffari 182a515125bSLeila Ghaffari // Restore vectors 183fd969b44SJames Wright PetscCall(VecReadC2P(user->q_ceed, q_mem_type, Q_loc)); 184fd969b44SJames Wright PetscCall(VecReadC2P(user->q_dot_ceed, q_dot_mem_type, Q_dot_loc)); 185fd969b44SJames Wright PetscCall(VecC2P(user->g_ceed, g_mem_type, G_loc)); 186a515125bSLeila Ghaffari 18701ab89c1SJames Wright if (user->app_ctx->sgs_model_type == SGS_MODEL_DATA_DRIVEN) { 18842454adaSJames Wright PetscCall(SgsDDModelApplyIFunction(user, Q_loc, G_loc)); 18901ab89c1SJames Wright } 1909c678832SJames Wright 191a515125bSLeila Ghaffari // Local-to-Global 1922b916ea7SJeremy L Thompson PetscCall(VecZeroEntries(G)); 1932b916ea7SJeremy L Thompson PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G)); 194a515125bSLeila Ghaffari 195a515125bSLeila Ghaffari // Restore vectors 196c5e9980aSAdeleke O. Bankole PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 197d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 198a515125bSLeila Ghaffari } 199a515125bSLeila Ghaffari 2002b916ea7SJeremy L Thompson static PetscErrorCode FormPreallocation(User user, PetscBool pbdiagonal, Mat J, CeedVector *coo_values) { 201b107fddaSJed Brown PetscCount ncoo; 202defe8520SJames Wright PetscInt *rows_petsc, *cols_petsc; 20371c848e3SJames Wright CeedInt *rows_ceed, *cols_ceed; 204b107fddaSJed Brown 205b107fddaSJed Brown PetscFunctionBeginUser; 206b107fddaSJed Brown if (pbdiagonal) { 20771c848e3SJames Wright PetscCallCeed(user->ceed, CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(user->op_ijacobian, &ncoo, &rows_ceed, &cols_ceed)); 208b107fddaSJed Brown } else { 209b4c37c5cSJames Wright PetscCallCeed(user->ceed, CeedOperatorLinearAssembleSymbolic(user->op_ijacobian, &ncoo, &rows_ceed, &cols_ceed)); 21071c848e3SJames Wright } 211defe8520SJames Wright PetscCall(IntArrayC2P(ncoo, &rows_ceed, &rows_petsc)); 212defe8520SJames Wright PetscCall(IntArrayC2P(ncoo, &cols_ceed, &cols_petsc)); 213defe8520SJames Wright PetscCall(MatSetPreallocationCOOLocal(J, ncoo, rows_petsc, cols_petsc)); 214defe8520SJames Wright free(rows_petsc); 215defe8520SJames Wright free(cols_petsc); 216b4c37c5cSJames Wright PetscCallCeed(user->ceed, CeedVectorCreate(user->ceed, ncoo, coo_values)); 217d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 218b107fddaSJed Brown } 219b107fddaSJed Brown 2202b916ea7SJeremy L Thompson static PetscErrorCode FormSetValues(User user, PetscBool pbdiagonal, Mat J, CeedVector coo_values) { 221b107fddaSJed Brown CeedMemType mem_type = CEED_MEM_HOST; 222b107fddaSJed Brown const PetscScalar *values; 223b107fddaSJed Brown MatType mat_type; 224b107fddaSJed Brown 225b107fddaSJed Brown PetscFunctionBeginUser; 226b107fddaSJed Brown PetscCall(MatGetType(J, &mat_type)); 2272b916ea7SJeremy L Thompson if (strstr(mat_type, "kokkos") || strstr(mat_type, "cusparse")) mem_type = CEED_MEM_DEVICE; 228cb315d14SJames Wright if (pbdiagonal) { 2297eedc94cSJames Wright PetscCall(PetscLogEventBegin(FLUIDS_CeedOperatorAssemblePointBlockDiagonal, J, 0, 0, 0)); 2307eedc94cSJames Wright PetscCall(PetscLogGpuTimeBegin()); 231b4c37c5cSJames Wright PetscCallCeed(user->ceed, CeedOperatorLinearAssemblePointBlockDiagonal(user->op_ijacobian, coo_values, CEED_REQUEST_IMMEDIATE)); 2327eedc94cSJames Wright PetscCall(PetscLogGpuTimeEnd()); 2337eedc94cSJames Wright PetscCall(PetscLogEventEnd(FLUIDS_CeedOperatorAssemblePointBlockDiagonal, J, 0, 0, 0)); 234b107fddaSJed Brown } else { 2357eedc94cSJames Wright PetscCall(PetscLogEventBegin(FLUIDS_CeedOperatorAssemble, J, 0, 0, 0)); 2367eedc94cSJames Wright PetscCall(PetscLogGpuTimeBegin()); 237b4c37c5cSJames Wright PetscCallCeed(user->ceed, CeedOperatorLinearAssemble(user->op_ijacobian, coo_values)); 2387eedc94cSJames Wright PetscCall(PetscLogGpuTimeEnd()); 2397eedc94cSJames Wright PetscCall(PetscLogEventEnd(FLUIDS_CeedOperatorAssemble, J, 0, 0, 0)); 240b107fddaSJed Brown } 241b4c37c5cSJames Wright PetscCallCeed(user->ceed, CeedVectorGetArrayRead(coo_values, mem_type, &values)); 242b107fddaSJed Brown PetscCall(MatSetValuesCOO(J, values, INSERT_VALUES)); 243b4c37c5cSJames Wright PetscCallCeed(user->ceed, CeedVectorRestoreArrayRead(coo_values, &values)); 244d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 245b107fddaSJed Brown } 246b107fddaSJed Brown 2472b916ea7SJeremy L Thompson PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, PetscReal shift, Mat J, Mat J_pre, void *user_data) { 248f0b65372SJed Brown User user = *(User *)user_data; 249b4c37c5cSJames Wright Ceed ceed = user->ceed; 25004855949SJed Brown PetscBool J_is_shell, J_is_mffd, J_pre_is_shell; 25106f41313SJames Wright 252f0b65372SJed Brown PetscFunctionBeginUser; 253b4c37c5cSJames Wright if (user->phys->ijacobian_time_shift_label) 254b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_ijacobian, user->phys->ijacobian_time_shift_label, &shift)); 25504855949SJed Brown PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd)); 256f0b65372SJed Brown PetscCall(PetscObjectTypeCompare((PetscObject)J, MATSHELL, &J_is_shell)); 2572b916ea7SJeremy L Thompson PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATSHELL, &J_pre_is_shell)); 258f0b65372SJed Brown if (!user->matrices_set_up) { 259f0b65372SJed Brown if (J_is_shell) { 260f9028c3cSJames Wright OperatorApplyContext op_ijacobian_ctx; 261f9028c3cSJames Wright OperatorApplyContextCreate(user->dm, user->dm, user->ceed, user->op_ijacobian, user->q_ceed, user->g_ceed, user->Q_dot_loc, NULL, 262f9028c3cSJames Wright &op_ijacobian_ctx); 263f9028c3cSJames Wright PetscCall(MatShellSetContext(J, op_ijacobian_ctx)); 264f9028c3cSJames Wright PetscCall(MatShellSetContextDestroy(J, (PetscErrorCode(*)(void *))OperatorApplyContextDestroy)); 265f9028c3cSJames Wright PetscCall(MatShellSetOperation(J, MATOP_MULT, (void (*)(void))MatMult_Ceed)); 266f9028c3cSJames Wright PetscCall(MatShellSetOperation(J, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiag_Ceed)); 267f0b65372SJed Brown PetscCall(MatSetUp(J)); 268f0b65372SJed Brown } 269f0b65372SJed Brown if (!J_pre_is_shell) { 2702b916ea7SJeremy L Thompson PetscCall(FormPreallocation(user, user->app_ctx->pmat_pbdiagonal, J_pre, &user->coo_values_pmat)); 271b107fddaSJed Brown } 27204855949SJed Brown if (J != J_pre && !J_is_shell && !J_is_mffd) { 273b107fddaSJed Brown PetscCall(FormPreallocation(user, PETSC_FALSE, J, &user->coo_values_amat)); 274b107fddaSJed Brown } 275f0b65372SJed Brown user->matrices_set_up = true; 276f0b65372SJed Brown } 277f0b65372SJed Brown if (!J_pre_is_shell) { 2782b916ea7SJeremy L Thompson PetscCall(FormSetValues(user, user->app_ctx->pmat_pbdiagonal, J_pre, user->coo_values_pmat)); 279f0b65372SJed Brown } 28004855949SJed Brown if (user->coo_values_amat) { 28104855949SJed Brown PetscCall(FormSetValues(user, PETSC_FALSE, J, user->coo_values_amat)); 28204855949SJed Brown } else if (J_is_mffd) { 28304855949SJed Brown PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 28404855949SJed Brown PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 28504855949SJed Brown } 286d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 287f0b65372SJed Brown } 288f0b65372SJed Brown 2892b916ea7SJeremy L Thompson PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, PetscScalar time) { 290a515125bSLeila Ghaffari Vec Q_loc; 291a515125bSLeila Ghaffari char file_path[PETSC_MAX_PATH_LEN]; 292a515125bSLeila Ghaffari PetscViewer viewer; 293a515125bSLeila Ghaffari 29406f41313SJames Wright PetscFunctionBeginUser; 295852e5969SJed Brown if (user->app_ctx->checkpoint_vtk) { 296a515125bSLeila Ghaffari // Set up output 2977538d537SJames Wright PetscCall(DMGetLocalVector(user->dm, &Q_loc)); 2987538d537SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec")); 2997538d537SJames Wright PetscCall(VecZeroEntries(Q_loc)); 3007538d537SJames Wright PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc)); 301a515125bSLeila Ghaffari 302a515125bSLeila Ghaffari // Output 303852e5969SJed Brown PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 3047538d537SJames Wright 3052b916ea7SJeremy L Thompson PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer)); 3067538d537SJames Wright PetscCall(VecView(Q_loc, viewer)); 3077538d537SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 308a515125bSLeila Ghaffari if (user->dm_viz) { 309a515125bSLeila Ghaffari Vec Q_refined, Q_refined_loc; 310a515125bSLeila Ghaffari char file_path_refined[PETSC_MAX_PATH_LEN]; 311a515125bSLeila Ghaffari PetscViewer viewer_refined; 312a515125bSLeila Ghaffari 3137538d537SJames Wright PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined)); 3147538d537SJames Wright PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc)); 3157538d537SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined")); 3167538d537SJames Wright 3177538d537SJames Wright PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined)); 3187538d537SJames Wright PetscCall(VecZeroEntries(Q_refined_loc)); 3192b916ea7SJeremy L Thompson PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc)); 3207538d537SJames Wright 321852e5969SJed Brown PetscCall( 322852e5969SJed Brown PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 3237538d537SJames Wright 3242b916ea7SJeremy L Thompson PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined)); 3257538d537SJames Wright PetscCall(VecView(Q_refined_loc, viewer_refined)); 3267538d537SJames Wright PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc)); 3277538d537SJames Wright PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined)); 3287538d537SJames Wright PetscCall(PetscViewerDestroy(&viewer_refined)); 329a515125bSLeila Ghaffari } 3307538d537SJames Wright PetscCall(DMRestoreLocalVector(user->dm, &Q_loc)); 331852e5969SJed Brown } 332a515125bSLeila Ghaffari 333a515125bSLeila Ghaffari // Save data in a binary file for continuation of simulations 33491a36801SJames Wright if (user->app_ctx->add_stepnum2bin) { 335852e5969SJed Brown PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", user->app_ctx->output_dir, step_no)); 33691a36801SJames Wright } else { 3372b916ea7SJeremy L Thompson PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", user->app_ctx->output_dir)); 33891a36801SJames Wright } 3392b916ea7SJeremy L Thompson PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, &viewer)); 3407538d537SJames Wright 341e1233009SJames Wright PetscInt32 token = PetscDefined(USE_64BIT_INDICES) ? FLUIDS_FILE_TOKEN_64 : FLUIDS_FILE_TOKEN_32; 342e1233009SJames Wright PetscCall(PetscViewerBinaryWrite(viewer, &token, 1, PETSC_INT32)); 3439293eaa1SJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &step_no, 1, PETSC_INT)); 3449293eaa1SJed Brown time /= user->units->second; // Dimensionalize time back 3459293eaa1SJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL)); 3467538d537SJames Wright PetscCall(VecView(Q, viewer)); 3477538d537SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 348d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 3497538d537SJames Wright } 3507538d537SJames Wright 351c5e9980aSAdeleke O. Bankole // CSV Monitor 352c5e9980aSAdeleke O. Bankole PetscErrorCode TSMonitor_WallForce(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 353c5e9980aSAdeleke O. Bankole User user = ctx; 354c5e9980aSAdeleke O. Bankole Vec G_loc; 355c5e9980aSAdeleke O. Bankole PetscInt num_wall = user->app_ctx->wall_forces.num_wall, dim = 3; 356c5e9980aSAdeleke O. Bankole const PetscInt *walls = user->app_ctx->wall_forces.walls; 357c5e9980aSAdeleke O. Bankole PetscViewer viewer = user->app_ctx->wall_forces.viewer; 358c5e9980aSAdeleke O. Bankole PetscViewerFormat format = user->app_ctx->wall_forces.viewer_format; 359c5e9980aSAdeleke O. Bankole PetscScalar *reaction_force; 360c5e9980aSAdeleke O. Bankole PetscBool iascii; 361c5e9980aSAdeleke O. Bankole 362c5e9980aSAdeleke O. Bankole PetscFunctionBeginUser; 363d949ddfcSJames Wright if (!viewer) PetscFunctionReturn(PETSC_SUCCESS); 364c5e9980aSAdeleke O. Bankole PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 365c5e9980aSAdeleke O. Bankole PetscCall(PetscMalloc1(num_wall * dim, &reaction_force)); 366c5e9980aSAdeleke O. Bankole PetscCall(Surface_Forces_NS(user->dm, G_loc, num_wall, walls, reaction_force)); 367c5e9980aSAdeleke O. Bankole PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 368c5e9980aSAdeleke O. Bankole 369c5e9980aSAdeleke O. Bankole PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 370c5e9980aSAdeleke O. Bankole 371c5e9980aSAdeleke O. Bankole if (iascii) { 372c5e9980aSAdeleke O. Bankole if (format == PETSC_VIEWER_ASCII_CSV && !user->app_ctx->wall_forces.header_written) { 373c5e9980aSAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "Step,Time,Wall,ForceX,ForceY,ForceZ\n")); 374c5e9980aSAdeleke O. Bankole user->app_ctx->wall_forces.header_written = PETSC_TRUE; 375c5e9980aSAdeleke O. Bankole } 376c5e9980aSAdeleke O. Bankole for (PetscInt w = 0; w < num_wall; w++) { 377c5e9980aSAdeleke O. Bankole PetscInt wall = walls[w]; 378c5e9980aSAdeleke O. Bankole if (format == PETSC_VIEWER_ASCII_CSV) { 379c5e9980aSAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ",%g,%" PetscInt_FMT ",%g,%g,%g\n", step_no, time, wall, 380c5e9980aSAdeleke O. Bankole reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2])); 381c5e9980aSAdeleke O. Bankole 382c5e9980aSAdeleke O. Bankole } else { 383c5e9980aSAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "Wall %" PetscInt_FMT " Forces: Force_x = %12g, Force_y = %12g, Force_z = %12g\n", wall, 384c5e9980aSAdeleke O. Bankole reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2])); 385c5e9980aSAdeleke O. Bankole } 386c5e9980aSAdeleke O. Bankole } 387c5e9980aSAdeleke O. Bankole } 388c5e9980aSAdeleke O. Bankole PetscCall(PetscFree(reaction_force)); 389d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 390c5e9980aSAdeleke O. Bankole } 391c5e9980aSAdeleke O. Bankole 3927538d537SJames Wright // User provided TS Monitor 3932b916ea7SJeremy L Thompson PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 3947538d537SJames Wright User user = ctx; 3957538d537SJames Wright 39606f41313SJames Wright PetscFunctionBeginUser; 397852e5969SJed Brown // Print every 'checkpoint_interval' steps 398c539088bSJames Wright if (user->app_ctx->checkpoint_interval <= 0 || step_no % user->app_ctx->checkpoint_interval != 0 || 399e419654dSJeremy L Thompson (user->app_ctx->cont_steps == step_no && step_no != 0)) { 400d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 401e419654dSJeremy L Thompson } 4027538d537SJames Wright 4037538d537SJames Wright PetscCall(WriteOutput(user, Q, step_no, time)); 404d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 405a515125bSLeila Ghaffari } 406a515125bSLeila Ghaffari 407a515125bSLeila Ghaffari // TS: Create, setup, and solve 4082b916ea7SJeremy L Thompson PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, Vec *Q, PetscScalar *f_time, TS *ts) { 409a515125bSLeila Ghaffari MPI_Comm comm = user->comm; 410a515125bSLeila Ghaffari TSAdapt adapt; 411a515125bSLeila Ghaffari PetscScalar final_time; 412a515125bSLeila Ghaffari 41306f41313SJames Wright PetscFunctionBeginUser; 4142b916ea7SJeremy L Thompson PetscCall(TSCreate(comm, ts)); 4152b916ea7SJeremy L Thompson PetscCall(TSSetDM(*ts, dm)); 416632a41e1SJames Wright PetscCall(TSSetApplicationContext(*ts, user)); 417a515125bSLeila Ghaffari if (phys->implicit) { 4182b916ea7SJeremy L Thompson PetscCall(TSSetType(*ts, TSBDF)); 419a515125bSLeila Ghaffari if (user->op_ifunction) { 4202b916ea7SJeremy L Thompson PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &user)); 421a515125bSLeila Ghaffari } else { // Implicit integrators can fall back to using an RHSFunction 4222b916ea7SJeremy L Thompson PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 423a515125bSLeila Ghaffari } 424f0b65372SJed Brown if (user->op_ijacobian) { 4252b916ea7SJeremy L Thompson PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &user)); 426b107fddaSJed Brown if (app_ctx->amat_type) { 427b107fddaSJed Brown Mat Pmat, Amat; 4282b916ea7SJeremy L Thompson PetscCall(DMCreateMatrix(dm, &Pmat)); 4292b916ea7SJeremy L Thompson PetscCall(DMSetMatType(dm, app_ctx->amat_type)); 4302b916ea7SJeremy L Thompson PetscCall(DMCreateMatrix(dm, &Amat)); 4312b916ea7SJeremy L Thompson PetscCall(TSSetIJacobian(*ts, Amat, Pmat, NULL, NULL)); 4322b916ea7SJeremy L Thompson PetscCall(MatDestroy(&Amat)); 4332b916ea7SJeremy L Thompson PetscCall(MatDestroy(&Pmat)); 434b107fddaSJed Brown } 435f0b65372SJed Brown } 436a515125bSLeila Ghaffari } else { 437da5fe0e4SJames Wright PetscCheck(user->op_rhs_ctx, comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction"); 4382b916ea7SJeremy L Thompson PetscCall(TSSetType(*ts, TSRK)); 4392b916ea7SJeremy L Thompson PetscCall(TSRKSetType(*ts, TSRK5F)); 4402b916ea7SJeremy L Thompson PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 441a515125bSLeila Ghaffari } 4422b916ea7SJeremy L Thompson PetscCall(TSSetMaxTime(*ts, 500. * user->units->second)); 4432b916ea7SJeremy L Thompson PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER)); 44422387d3aSJames Wright if (app_ctx->test_type == TESTTYPE_NONE) PetscCall(TSSetErrorIfStepFails(*ts, PETSC_FALSE)); 4452b916ea7SJeremy L Thompson PetscCall(TSSetTimeStep(*ts, 1.e-2 * user->units->second)); 4462b916ea7SJeremy L Thompson PetscCall(TSGetAdapt(*ts, &adapt)); 4472b916ea7SJeremy L Thompson PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * user->units->second, 1.e2 * user->units->second)); 4482b916ea7SJeremy L Thompson PetscCall(TSSetFromOptions(*ts)); 44991f639d2SJames Wright user->time_bc_set = -1.0; // require all BCs be updated 450c26b555cSJames Wright if (app_ctx->cont_steps) { // continue from previous timestep data 451a515125bSLeila Ghaffari PetscInt count; 452a515125bSLeila Ghaffari PetscViewer viewer; 4532b916ea7SJeremy L Thompson 4549293eaa1SJed Brown if (app_ctx->cont_time <= 0) { // Legacy files did not include step number and time 4552b916ea7SJeremy L Thompson PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer)); 4569293eaa1SJed Brown PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL)); 4572b916ea7SJeremy L Thompson PetscCall(PetscViewerDestroy(&viewer)); 4589293eaa1SJed Brown PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP, 4599293eaa1SJed Brown "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)"); 4609293eaa1SJed Brown } 4619293eaa1SJed Brown PetscCall(TSSetTime(*ts, app_ctx->cont_time * user->units->second)); 46274a6f4ddSJed Brown PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps)); 463a515125bSLeila Ghaffari } 4640e1e9333SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 4652b916ea7SJeremy L Thompson PetscCall(TSMonitorSet(*ts, TSMonitor_NS, user, NULL)); 4660e1e9333SJames Wright } 467c5e9980aSAdeleke O. Bankole if (app_ctx->wall_forces.viewer) { 468c5e9980aSAdeleke O. Bankole PetscCall(TSMonitorSet(*ts, TSMonitor_WallForce, user, NULL)); 469c5e9980aSAdeleke O. Bankole } 470c931fa59SJames Wright if (app_ctx->turb_spanstats_enable) { 47191933550SJames Wright PetscCall(TSMonitorSet(*ts, TSMonitor_TurbulenceStatistics, user, NULL)); 472b8daee98SJames Wright CeedScalar previous_time = app_ctx->cont_time * user->units->second; 473b4c37c5cSJames Wright PetscCallCeed(user->ceed, 474b4c37c5cSJames Wright CeedOperatorSetContextDouble(user->spanstats.op_stats_collect_ctx->op, user->spanstats.previous_time_label, &previous_time)); 475b0488d1fSJames Wright } 47688b07121SJames Wright if (app_ctx->diff_filter_monitor) PetscCall(TSMonitorSet(*ts, TSMonitor_DifferentialFilter, user, NULL)); 477a515125bSLeila Ghaffari 4781c17f66aSJames Wright if (app_ctx->sgs_train_enable) { 4791c17f66aSJames Wright PetscCall(TSMonitorSet(*ts, TSMonitor_SGS_DD_Training, user, NULL)); 4801c17f66aSJames Wright PetscCall(TSSetPostStep(*ts, TSPostStep_SGS_DD_Training)); 4811c17f66aSJames Wright } 482a515125bSLeila Ghaffari // Solve 48374a6f4ddSJed Brown PetscReal start_time; 48474a6f4ddSJed Brown PetscInt start_step; 4852b916ea7SJeremy L Thompson PetscCall(TSGetTime(*ts, &start_time)); 48674a6f4ddSJed Brown PetscCall(TSGetStepNumber(*ts, &start_step)); 48791982731SJeremy L Thompson 488df4304b5SJed Brown PetscCall(PetscLogDefaultBegin()); // So we can use PetscLogStageGetPerfInfo without -log_view 48991982731SJeremy L Thompson PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve"); 49091982731SJeremy L Thompson PetscCall(TSSetTime(*ts, start_time)); 49174a6f4ddSJed Brown PetscCall(TSSetStepNumber(*ts, start_step)); 49291982731SJeremy L Thompson if (PetscPreLoadingOn) { 49391982731SJeremy L Thompson // LCOV_EXCL_START 49491982731SJeremy L Thompson SNES snes; 49591982731SJeremy L Thompson Vec Q_preload; 49691982731SJeremy L Thompson PetscReal rtol; 49791982731SJeremy L Thompson PetscCall(VecDuplicate(*Q, &Q_preload)); 49891982731SJeremy L Thompson PetscCall(VecCopy(*Q, Q_preload)); 49991982731SJeremy L Thompson PetscCall(TSGetSNES(*ts, &snes)); 50091982731SJeremy L Thompson PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL)); 5012b916ea7SJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 50222c1b34eSJames Wright PetscCall(TSSetSolution(*ts, Q_preload)); 50391982731SJeremy L Thompson PetscCall(TSStep(*ts)); 5042b916ea7SJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 50591982731SJeremy L Thompson PetscCall(VecDestroy(&Q_preload)); 50691982731SJeremy L Thompson // LCOV_EXCL_STOP 50791982731SJeremy L Thompson } else { 5082b916ea7SJeremy L Thompson PetscCall(PetscBarrier((PetscObject)*ts)); 5092b916ea7SJeremy L Thompson PetscCall(TSSolve(*ts, *Q)); 51091982731SJeremy L Thompson } 51191982731SJeremy L Thompson PetscPreLoadEnd(); 51291982731SJeremy L Thompson 51391982731SJeremy L Thompson PetscCall(TSGetSolveTime(*ts, &final_time)); 514a515125bSLeila Ghaffari *f_time = final_time; 51591982731SJeremy L Thompson 5160e1e9333SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 5177538d537SJames Wright PetscInt step_no; 5187538d537SJames Wright PetscCall(TSGetStepNumber(*ts, &step_no)); 519b0488d1fSJames Wright if (user->app_ctx->checkpoint_interval > 0 || user->app_ctx->checkpoint_interval == -1) { 5207538d537SJames Wright PetscCall(WriteOutput(user, *Q, step_no, final_time)); 5217538d537SJames Wright } 5227538d537SJames Wright 5237eedc94cSJames Wright PetscLogStage stage_id; 524df4304b5SJed Brown PetscEventPerfInfo stage_perf; 52591982731SJeremy L Thompson 52691982731SJeremy L Thompson PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id)); 527df4304b5SJed Brown PetscCall(PetscLogStageGetPerfInfo(stage_id, &stage_perf)); 528df4304b5SJed Brown PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_perf.time)); 529a515125bSLeila Ghaffari } 530d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 531a515125bSLeila Ghaffari } 532