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 /// Time-stepping functions for Navier-Stokes example using PETSc 1077841947SLeila Ghaffari 1149aac155SJeremy L Thompson #include <ceed.h> 1249aac155SJeremy L Thompson #include <petscdmplex.h> 1349aac155SJeremy L Thompson #include <petscts.h> 1449aac155SJeremy L Thompson 1577841947SLeila Ghaffari #include "../navierstokes.h" 16ca69d878SAdeleke O. Bankole #include "../qfunctions/newtonian_state.h" 1777841947SLeila Ghaffari 1804292f7dSJames Wright // @brief Create KSP to solve the inverse mass operator for explicit time stepping schemes 19*dfcf44b0SJames Wright PetscErrorCode CreateKSPMassOperator(User user, CeedData ceed_data) { 2004292f7dSJames Wright Ceed ceed = user->ceed; 2104292f7dSJames Wright DM dm = user->dm; 2277841947SLeila Ghaffari CeedQFunction qf_mass; 2377841947SLeila Ghaffari CeedOperator op_mass; 2404292f7dSJames Wright OperatorApplyContext mass_matop_ctx; 2577841947SLeila Ghaffari CeedInt num_comp_q, q_data_size; 2677841947SLeila Ghaffari 27f17d818dSJames Wright PetscFunctionBeginUser; 28a424bcd0SJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_q, &num_comp_q)); 29a424bcd0SJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size)); 3077841947SLeila Ghaffari 31ef080ff9SJames Wright PetscCall(CreateMassQFunction(ceed, num_comp_q, q_data_size, &qf_mass)); 32a424bcd0SJames Wright PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass)); 33a424bcd0SJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "u", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE)); 34356036faSJeremy L Thompson PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_NONE, ceed_data->q_data)); 35a424bcd0SJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE)); 3677841947SLeila Ghaffari 3704292f7dSJames Wright { // -- Setup KSP for mass operator 3804292f7dSJames Wright Mat mat_mass; 390f2fa9b4SJames Wright Vec Zeros_loc; 4004292f7dSJames Wright MPI_Comm comm = PetscObjectComm((PetscObject)dm); 4177841947SLeila Ghaffari 420f2fa9b4SJames Wright PetscCall(DMCreateLocalVector(dm, &Zeros_loc)); 430f2fa9b4SJames Wright PetscCall(VecZeroEntries(Zeros_loc)); 440f2fa9b4SJames Wright PetscCall(OperatorApplyContextCreate(dm, dm, ceed, op_mass, NULL, NULL, Zeros_loc, NULL, &mass_matop_ctx)); 4504292f7dSJames Wright PetscCall(CreateMatShell_Ceed(mass_matop_ctx, &mat_mass)); 4677841947SLeila Ghaffari 4704292f7dSJames Wright PetscCall(KSPCreate(comm, &user->mass_ksp)); 4804292f7dSJames Wright PetscCall(KSPSetOptionsPrefix(user->mass_ksp, "mass_")); 4904292f7dSJames Wright { // lumped by default 5004292f7dSJames Wright PC pc; 5104292f7dSJames Wright PetscCall(KSPGetPC(user->mass_ksp, &pc)); 5204292f7dSJames Wright PetscCall(PCSetType(pc, PCJACOBI)); 5304292f7dSJames Wright PetscCall(PCJacobiSetType(pc, PC_JACOBI_ROWSUM)); 5404292f7dSJames Wright PetscCall(KSPSetType(user->mass_ksp, KSPPREONLY)); 5504292f7dSJames Wright } 5604292f7dSJames Wright PetscCall(KSPSetOperators(user->mass_ksp, mat_mass, mat_mass)); 5704292f7dSJames Wright PetscCall(KSPSetFromOptions(user->mass_ksp)); 580f2fa9b4SJames Wright PetscCall(VecDestroy(&Zeros_loc)); 590f2fa9b4SJames Wright PetscCall(MatDestroy(&mat_mass)); 6004292f7dSJames Wright } 6177841947SLeila Ghaffari 6277841947SLeila Ghaffari // Cleanup 63a424bcd0SJames Wright PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass)); 64a424bcd0SJames Wright PetscCallCeed(ceed, CeedOperatorDestroy(&op_mass)); 65ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 6677841947SLeila Ghaffari } 6777841947SLeila Ghaffari 68a0b9a424SJames Wright // Insert Boundary values if it's a new time 69a0b9a424SJames Wright PetscErrorCode UpdateBoundaryValues(User user, Vec Q_loc, PetscReal t) { 70a0b9a424SJames Wright PetscFunctionBeginUser; 71a0b9a424SJames Wright if (user->time_bc_set != t) { 72a0b9a424SJames Wright PetscCall(DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL)); 73a0b9a424SJames Wright user->time_bc_set = t; 74a0b9a424SJames Wright } 75ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 76a0b9a424SJames Wright } 77a0b9a424SJames Wright 7877841947SLeila Ghaffari // RHS (Explicit time-stepper) function setup 7977841947SLeila Ghaffari // This is the RHS of the ODE, given as u_t = G(t,u) 8077841947SLeila Ghaffari // This function takes in a state vector Q and writes into G 8177841947SLeila Ghaffari PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data) { 8277841947SLeila Ghaffari User user = *(User *)user_data; 837d4c6defSJames Wright Ceed ceed = user->ceed; 84c798d55aSJames Wright PetscScalar dt; 859ad5e8e4SJames Wright Vec Q_loc = user->Q_loc; 8677841947SLeila Ghaffari 87f17d818dSJames Wright PetscFunctionBeginUser; 885e82a6e1SJeremy L Thompson // Update time dependent data 89a0b9a424SJames Wright PetscCall(UpdateBoundaryValues(user, Q_loc, t)); 907d4c6defSJames Wright if (user->phys->solution_time_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_rhs_ctx->op, user->phys->solution_time_label, &t)); 912b730f8bSJeremy L Thompson PetscCall(TSGetTimeStep(ts, &dt)); 927d4c6defSJames Wright if (user->phys->timestep_size_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_rhs_ctx->op, user->phys->timestep_size_label, &dt)); 9377841947SLeila Ghaffari 949ad5e8e4SJames Wright PetscCall(ApplyCeedOperatorGlobalToGlobal(Q, G, user->op_rhs_ctx)); 9577841947SLeila Ghaffari 96d6e67e47SJames Wright // Inverse of the lumped mass matrix 9704292f7dSJames Wright PetscCall(KSPSolve(user->mass_ksp, G, G)); 98ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 9977841947SLeila Ghaffari } 10077841947SLeila Ghaffari 101ca69d878SAdeleke O. Bankole // Surface forces function setup 102ca69d878SAdeleke O. Bankole static PetscErrorCode Surface_Forces_NS(DM dm, Vec G_loc, PetscInt num_walls, const PetscInt walls[], PetscScalar *reaction_force) { 103ca69d878SAdeleke O. Bankole DMLabel face_label; 104ca69d878SAdeleke O. Bankole const PetscScalar *g; 105d6734f85SAdeleke O. Bankole PetscInt dof, dim = 3; 106ca69d878SAdeleke O. Bankole MPI_Comm comm; 107d6734f85SAdeleke O. Bankole PetscSection s; 108ca69d878SAdeleke O. Bankole 109ca69d878SAdeleke O. Bankole PetscFunctionBeginUser; 110ca69d878SAdeleke O. Bankole PetscCall(PetscArrayzero(reaction_force, num_walls * dim)); 111ca69d878SAdeleke O. Bankole PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 112ca69d878SAdeleke O. Bankole PetscCall(DMGetLabel(dm, "Face Sets", &face_label)); 113ca69d878SAdeleke O. Bankole PetscCall(VecGetArrayRead(G_loc, &g)); 114ca69d878SAdeleke O. Bankole for (PetscInt w = 0; w < num_walls; w++) { 115ca69d878SAdeleke O. Bankole const PetscInt wall = walls[w]; 116ca69d878SAdeleke O. Bankole IS wall_is; 117d6734f85SAdeleke O. Bankole PetscCall(DMGetLocalSection(dm, &s)); 118ca69d878SAdeleke O. Bankole PetscCall(DMLabelGetStratumIS(face_label, wall, &wall_is)); 119ca69d878SAdeleke O. Bankole if (wall_is) { // There exist such points on this process 120ca69d878SAdeleke O. Bankole PetscInt num_points; 121d6734f85SAdeleke O. Bankole PetscInt num_comp = 0; 122ca69d878SAdeleke O. Bankole const PetscInt *points; 123d6734f85SAdeleke O. Bankole PetscCall(PetscSectionGetFieldComponents(s, 0, &num_comp)); 124ca69d878SAdeleke O. Bankole PetscCall(ISGetSize(wall_is, &num_points)); 125ca69d878SAdeleke O. Bankole PetscCall(ISGetIndices(wall_is, &points)); 126ca69d878SAdeleke O. Bankole for (PetscInt i = 0; i < num_points; i++) { 127ca69d878SAdeleke O. Bankole const PetscInt p = points[i]; 128ca69d878SAdeleke O. Bankole const StateConservative *r; 129ca69d878SAdeleke O. Bankole PetscCall(DMPlexPointLocalRead(dm, p, g, &r)); 130d6734f85SAdeleke O. Bankole PetscCall(PetscSectionGetDof(s, p, &dof)); 131d6734f85SAdeleke O. Bankole for (PetscInt node = 0; node < dof / num_comp; node++) { 132ca69d878SAdeleke O. Bankole for (PetscInt j = 0; j < 3; j++) { 133d6734f85SAdeleke O. Bankole reaction_force[w * dim + j] -= r[node].momentum[j]; 134d6734f85SAdeleke O. Bankole } 135ca69d878SAdeleke O. Bankole } 136ca69d878SAdeleke O. Bankole } 137ca69d878SAdeleke O. Bankole PetscCall(ISRestoreIndices(wall_is, &points)); 138ca69d878SAdeleke O. Bankole } 139ca69d878SAdeleke O. Bankole PetscCall(ISDestroy(&wall_is)); 140ca69d878SAdeleke O. Bankole } 141ca69d878SAdeleke O. Bankole PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, reaction_force, dim * num_walls, MPIU_SCALAR, MPI_SUM, comm)); 142ca69d878SAdeleke O. Bankole // Restore Vectors 143ca69d878SAdeleke O. Bankole PetscCall(VecRestoreArrayRead(G_loc, &g)); 144ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 145ca69d878SAdeleke O. Bankole } 146ca69d878SAdeleke O. Bankole 14777841947SLeila Ghaffari // Implicit time-stepper function setup 1482b730f8bSJeremy L Thompson PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data) { 14977841947SLeila Ghaffari User user = *(User *)user_data; 1507d4c6defSJames Wright Ceed ceed = user->ceed; 151c798d55aSJames Wright PetscScalar dt; 1525e82a6e1SJeremy L Thompson Vec Q_loc = user->Q_loc, Q_dot_loc = user->Q_dot_loc, G_loc; 15377841947SLeila Ghaffari PetscMemType q_mem_type, q_dot_mem_type, g_mem_type; 15477841947SLeila Ghaffari 155f17d818dSJames Wright PetscFunctionBeginUser; 1565e82a6e1SJeremy L Thompson // Get local vectors 157ca69d878SAdeleke O. Bankole PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 1585e82a6e1SJeremy L Thompson 1595e82a6e1SJeremy L Thompson // Update time dependent data 160a0b9a424SJames Wright PetscCall(UpdateBoundaryValues(user, Q_loc, t)); 1617d4c6defSJames Wright if (user->phys->solution_time_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_ifunction, user->phys->solution_time_label, &t)); 1622b730f8bSJeremy L Thompson PetscCall(TSGetTimeStep(ts, &dt)); 1637d4c6defSJames Wright if (user->phys->timestep_size_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_ifunction, user->phys->timestep_size_label, &dt)); 16477841947SLeila Ghaffari 16577841947SLeila Ghaffari // Global-to-local 166878eb0e7SJames Wright PetscCall(DMGlobalToLocalBegin(user->dm, Q, INSERT_VALUES, Q_loc)); 167878eb0e7SJames Wright PetscCall(DMGlobalToLocalBegin(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc)); 168878eb0e7SJames Wright PetscCall(DMGlobalToLocalEnd(user->dm, Q, INSERT_VALUES, Q_loc)); 169878eb0e7SJames Wright PetscCall(DMGlobalToLocalEnd(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc)); 17077841947SLeila Ghaffari 17177841947SLeila Ghaffari // Place PETSc vectors in CEED vectors 172c798d55aSJames Wright PetscCall(VecReadP2C(Q_loc, &q_mem_type, user->q_ceed)); 173c798d55aSJames Wright PetscCall(VecReadP2C(Q_dot_loc, &q_dot_mem_type, user->q_dot_ceed)); 174c798d55aSJames Wright PetscCall(VecP2C(G_loc, &g_mem_type, user->g_ceed)); 17577841947SLeila Ghaffari 17677841947SLeila Ghaffari // Apply CEED operator 17775d1798cSJames Wright PetscCall(PetscLogEventBegin(FLUIDS_CeedOperatorApply, Q, G, 0, 0)); 17875d1798cSJames Wright PetscCall(PetscLogGpuTimeBegin()); 179a424bcd0SJames Wright PetscCallCeed(user->ceed, CeedOperatorApply(user->op_ifunction, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE)); 18075d1798cSJames Wright PetscCall(PetscLogGpuTimeEnd()); 18175d1798cSJames Wright PetscCall(PetscLogEventEnd(FLUIDS_CeedOperatorApply, Q, G, 0, 0)); 18277841947SLeila Ghaffari 18377841947SLeila Ghaffari // Restore vectors 184c798d55aSJames Wright PetscCall(VecReadC2P(user->q_ceed, q_mem_type, Q_loc)); 185c798d55aSJames Wright PetscCall(VecReadC2P(user->q_dot_ceed, q_dot_mem_type, Q_dot_loc)); 186c798d55aSJames Wright PetscCall(VecC2P(user->g_ceed, g_mem_type, G_loc)); 18777841947SLeila Ghaffari 18819ffbc25SJames Wright if (user->app_ctx->sgs_model_type == SGS_MODEL_DATA_DRIVEN) { 189cbef7084SJames Wright PetscCall(SgsDDModelApplyIFunction(user, Q_loc, G_loc)); 19019ffbc25SJames Wright } 191be34b3b7SJames Wright 19277841947SLeila Ghaffari // Local-to-Global 1932b730f8bSJeremy L Thompson PetscCall(VecZeroEntries(G)); 1942b730f8bSJeremy L Thompson PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G)); 19577841947SLeila Ghaffari 19677841947SLeila Ghaffari // Restore vectors 197ca69d878SAdeleke O. Bankole PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 198ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 19977841947SLeila Ghaffari } 20077841947SLeila Ghaffari 2012b730f8bSJeremy L Thompson static PetscErrorCode FormPreallocation(User user, PetscBool pbdiagonal, Mat J, CeedVector *coo_values) { 202544be873SJed Brown PetscCount ncoo; 203457e73b2SJames Wright PetscInt *rows_petsc, *cols_petsc; 20401f0e615SJames Wright CeedInt *rows_ceed, *cols_ceed; 205544be873SJed Brown 206544be873SJed Brown PetscFunctionBeginUser; 207544be873SJed Brown if (pbdiagonal) { 20801f0e615SJames Wright PetscCallCeed(user->ceed, CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(user->op_ijacobian, &ncoo, &rows_ceed, &cols_ceed)); 209544be873SJed Brown } else { 210a424bcd0SJames Wright PetscCallCeed(user->ceed, CeedOperatorLinearAssembleSymbolic(user->op_ijacobian, &ncoo, &rows_ceed, &cols_ceed)); 21101f0e615SJames Wright } 212457e73b2SJames Wright PetscCall(IntArrayC2P(ncoo, &rows_ceed, &rows_petsc)); 213457e73b2SJames Wright PetscCall(IntArrayC2P(ncoo, &cols_ceed, &cols_petsc)); 214457e73b2SJames Wright PetscCall(MatSetPreallocationCOOLocal(J, ncoo, rows_petsc, cols_petsc)); 215457e73b2SJames Wright free(rows_petsc); 216457e73b2SJames Wright free(cols_petsc); 217a424bcd0SJames Wright PetscCallCeed(user->ceed, CeedVectorCreate(user->ceed, ncoo, coo_values)); 218ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 219544be873SJed Brown } 220544be873SJed Brown 2212b730f8bSJeremy L Thompson static PetscErrorCode FormSetValues(User user, PetscBool pbdiagonal, Mat J, CeedVector coo_values) { 222544be873SJed Brown CeedMemType mem_type = CEED_MEM_HOST; 223544be873SJed Brown const PetscScalar *values; 224544be873SJed Brown MatType mat_type; 225544be873SJed Brown 226544be873SJed Brown PetscFunctionBeginUser; 227544be873SJed Brown PetscCall(MatGetType(J, &mat_type)); 2282b730f8bSJeremy L Thompson if (strstr(mat_type, "kokkos") || strstr(mat_type, "cusparse")) mem_type = CEED_MEM_DEVICE; 229fbb546ddSJames Wright if (pbdiagonal) { 23075d1798cSJames Wright PetscCall(PetscLogEventBegin(FLUIDS_CeedOperatorAssemblePointBlockDiagonal, J, 0, 0, 0)); 23175d1798cSJames Wright PetscCall(PetscLogGpuTimeBegin()); 232a424bcd0SJames Wright PetscCallCeed(user->ceed, CeedOperatorLinearAssemblePointBlockDiagonal(user->op_ijacobian, coo_values, CEED_REQUEST_IMMEDIATE)); 23375d1798cSJames Wright PetscCall(PetscLogGpuTimeEnd()); 23475d1798cSJames Wright PetscCall(PetscLogEventEnd(FLUIDS_CeedOperatorAssemblePointBlockDiagonal, J, 0, 0, 0)); 235544be873SJed Brown } else { 23675d1798cSJames Wright PetscCall(PetscLogEventBegin(FLUIDS_CeedOperatorAssemble, J, 0, 0, 0)); 23775d1798cSJames Wright PetscCall(PetscLogGpuTimeBegin()); 238a424bcd0SJames Wright PetscCallCeed(user->ceed, CeedOperatorLinearAssemble(user->op_ijacobian, coo_values)); 23975d1798cSJames Wright PetscCall(PetscLogGpuTimeEnd()); 24075d1798cSJames Wright PetscCall(PetscLogEventEnd(FLUIDS_CeedOperatorAssemble, J, 0, 0, 0)); 241544be873SJed Brown } 242a424bcd0SJames Wright PetscCallCeed(user->ceed, CeedVectorGetArrayRead(coo_values, mem_type, &values)); 243544be873SJed Brown PetscCall(MatSetValuesCOO(J, values, INSERT_VALUES)); 244a424bcd0SJames Wright PetscCallCeed(user->ceed, CeedVectorRestoreArrayRead(coo_values, &values)); 245ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 246544be873SJed Brown } 247544be873SJed Brown 2482b730f8bSJeremy L Thompson PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, PetscReal shift, Mat J, Mat J_pre, void *user_data) { 249e334ad8fSJed Brown User user = *(User *)user_data; 250a424bcd0SJames Wright Ceed ceed = user->ceed; 251d6bf345cSJed Brown PetscBool J_is_shell, J_is_mffd, J_pre_is_shell; 252f17d818dSJames Wright 253e334ad8fSJed Brown PetscFunctionBeginUser; 254a424bcd0SJames Wright if (user->phys->ijacobian_time_shift_label) 255a424bcd0SJames Wright PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_ijacobian, user->phys->ijacobian_time_shift_label, &shift)); 256d6bf345cSJed Brown PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd)); 257e334ad8fSJed Brown PetscCall(PetscObjectTypeCompare((PetscObject)J, MATSHELL, &J_is_shell)); 2582b730f8bSJeremy L Thompson PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATSHELL, &J_pre_is_shell)); 259e334ad8fSJed Brown if (!user->matrices_set_up) { 260e334ad8fSJed Brown if (J_is_shell) { 261b07979f9SJames Wright OperatorApplyContext op_ijacobian_ctx; 262b07979f9SJames Wright OperatorApplyContextCreate(user->dm, user->dm, user->ceed, user->op_ijacobian, user->q_ceed, user->g_ceed, user->Q_dot_loc, NULL, 263b07979f9SJames Wright &op_ijacobian_ctx); 264b07979f9SJames Wright PetscCall(MatShellSetContext(J, op_ijacobian_ctx)); 265b07979f9SJames Wright PetscCall(MatShellSetContextDestroy(J, (PetscErrorCode(*)(void *))OperatorApplyContextDestroy)); 266b07979f9SJames Wright PetscCall(MatShellSetOperation(J, MATOP_MULT, (void (*)(void))MatMult_Ceed)); 267b07979f9SJames Wright PetscCall(MatShellSetOperation(J, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiag_Ceed)); 268e334ad8fSJed Brown PetscCall(MatSetUp(J)); 269e334ad8fSJed Brown } 270e334ad8fSJed Brown if (!J_pre_is_shell) { 2712b730f8bSJeremy L Thompson PetscCall(FormPreallocation(user, user->app_ctx->pmat_pbdiagonal, J_pre, &user->coo_values_pmat)); 272544be873SJed Brown } 273d6bf345cSJed Brown if (J != J_pre && !J_is_shell && !J_is_mffd) { 274544be873SJed Brown PetscCall(FormPreallocation(user, PETSC_FALSE, J, &user->coo_values_amat)); 275544be873SJed Brown } 276e334ad8fSJed Brown user->matrices_set_up = true; 277e334ad8fSJed Brown } 278e334ad8fSJed Brown if (!J_pre_is_shell) { 2792b730f8bSJeremy L Thompson PetscCall(FormSetValues(user, user->app_ctx->pmat_pbdiagonal, J_pre, user->coo_values_pmat)); 280e334ad8fSJed Brown } 281d6bf345cSJed Brown if (user->coo_values_amat) { 282d6bf345cSJed Brown PetscCall(FormSetValues(user, PETSC_FALSE, J, user->coo_values_amat)); 283d6bf345cSJed Brown } else if (J_is_mffd) { 284d6bf345cSJed Brown PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 285d6bf345cSJed Brown PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 286d6bf345cSJed Brown } 287ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 288e334ad8fSJed Brown } 289e334ad8fSJed Brown 2902b730f8bSJeremy L Thompson PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, PetscScalar time) { 29177841947SLeila Ghaffari Vec Q_loc; 29277841947SLeila Ghaffari char file_path[PETSC_MAX_PATH_LEN]; 29377841947SLeila Ghaffari PetscViewer viewer; 29477841947SLeila Ghaffari 295f17d818dSJames Wright PetscFunctionBeginUser; 29637cbb16aSJed Brown if (user->app_ctx->checkpoint_vtk) { 29777841947SLeila Ghaffari // Set up output 298f14660a4SJames Wright PetscCall(DMGetLocalVector(user->dm, &Q_loc)); 299f14660a4SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec")); 300f14660a4SJames Wright PetscCall(VecZeroEntries(Q_loc)); 301f14660a4SJames Wright PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc)); 30277841947SLeila Ghaffari 30377841947SLeila Ghaffari // Output 30437cbb16aSJed Brown PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 305f14660a4SJames Wright 3062b730f8bSJeremy L Thompson PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer)); 307f14660a4SJames Wright PetscCall(VecView(Q_loc, viewer)); 308f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 30977841947SLeila Ghaffari if (user->dm_viz) { 31077841947SLeila Ghaffari Vec Q_refined, Q_refined_loc; 31177841947SLeila Ghaffari char file_path_refined[PETSC_MAX_PATH_LEN]; 31277841947SLeila Ghaffari PetscViewer viewer_refined; 31377841947SLeila Ghaffari 314f14660a4SJames Wright PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined)); 315f14660a4SJames Wright PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc)); 316f14660a4SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined")); 317f14660a4SJames Wright 318f14660a4SJames Wright PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined)); 319f14660a4SJames Wright PetscCall(VecZeroEntries(Q_refined_loc)); 3202b730f8bSJeremy L Thompson PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc)); 321f14660a4SJames Wright 32237cbb16aSJed Brown PetscCall( 32337cbb16aSJed Brown PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 324f14660a4SJames Wright 3252b730f8bSJeremy L Thompson PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined)); 326f14660a4SJames Wright PetscCall(VecView(Q_refined_loc, viewer_refined)); 327f14660a4SJames Wright PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc)); 328f14660a4SJames Wright PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined)); 329f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer_refined)); 33077841947SLeila Ghaffari } 331f14660a4SJames Wright PetscCall(DMRestoreLocalVector(user->dm, &Q_loc)); 33237cbb16aSJed Brown } 33377841947SLeila Ghaffari 33477841947SLeila Ghaffari // Save data in a binary file for continuation of simulations 33569293791SJames Wright if (user->app_ctx->add_stepnum2bin) { 33637cbb16aSJed Brown PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", user->app_ctx->output_dir, step_no)); 33769293791SJames Wright } else { 3382b730f8bSJeremy L Thompson PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", user->app_ctx->output_dir)); 33969293791SJames Wright } 3402b730f8bSJeremy L Thompson PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, &viewer)); 341f14660a4SJames Wright 3420de6a49fSJames Wright PetscInt32 token = PetscDefined(USE_64BIT_INDICES) ? FLUIDS_FILE_TOKEN_64 : FLUIDS_FILE_TOKEN_32; 3430de6a49fSJames Wright PetscCall(PetscViewerBinaryWrite(viewer, &token, 1, PETSC_INT32)); 3444de8550aSJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &step_no, 1, PETSC_INT)); 3454de8550aSJed Brown time /= user->units->second; // Dimensionalize time back 3464de8550aSJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL)); 347f14660a4SJames Wright PetscCall(VecView(Q, viewer)); 348f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 349ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 350f14660a4SJames Wright } 351f14660a4SJames Wright 352ca69d878SAdeleke O. Bankole // CSV Monitor 353ca69d878SAdeleke O. Bankole PetscErrorCode TSMonitor_WallForce(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 354ca69d878SAdeleke O. Bankole User user = ctx; 355ca69d878SAdeleke O. Bankole Vec G_loc; 356ca69d878SAdeleke O. Bankole PetscInt num_wall = user->app_ctx->wall_forces.num_wall, dim = 3; 357ca69d878SAdeleke O. Bankole const PetscInt *walls = user->app_ctx->wall_forces.walls; 358ca69d878SAdeleke O. Bankole PetscViewer viewer = user->app_ctx->wall_forces.viewer; 359ca69d878SAdeleke O. Bankole PetscViewerFormat format = user->app_ctx->wall_forces.viewer_format; 360ca69d878SAdeleke O. Bankole PetscScalar *reaction_force; 361ca69d878SAdeleke O. Bankole PetscBool iascii; 362ca69d878SAdeleke O. Bankole 363ca69d878SAdeleke O. Bankole PetscFunctionBeginUser; 364ee4ca9cbSJames Wright if (!viewer) PetscFunctionReturn(PETSC_SUCCESS); 365ca69d878SAdeleke O. Bankole PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 366ca69d878SAdeleke O. Bankole PetscCall(PetscMalloc1(num_wall * dim, &reaction_force)); 367ca69d878SAdeleke O. Bankole PetscCall(Surface_Forces_NS(user->dm, G_loc, num_wall, walls, reaction_force)); 368ca69d878SAdeleke O. Bankole PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 369ca69d878SAdeleke O. Bankole 370ca69d878SAdeleke O. Bankole PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 371ca69d878SAdeleke O. Bankole 372ca69d878SAdeleke O. Bankole if (iascii) { 373ca69d878SAdeleke O. Bankole if (format == PETSC_VIEWER_ASCII_CSV && !user->app_ctx->wall_forces.header_written) { 374ca69d878SAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "Step,Time,Wall,ForceX,ForceY,ForceZ\n")); 375ca69d878SAdeleke O. Bankole user->app_ctx->wall_forces.header_written = PETSC_TRUE; 376ca69d878SAdeleke O. Bankole } 377ca69d878SAdeleke O. Bankole for (PetscInt w = 0; w < num_wall; w++) { 378ca69d878SAdeleke O. Bankole PetscInt wall = walls[w]; 379ca69d878SAdeleke O. Bankole if (format == PETSC_VIEWER_ASCII_CSV) { 380ca69d878SAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ",%g,%" PetscInt_FMT ",%g,%g,%g\n", step_no, time, wall, 381ca69d878SAdeleke O. Bankole reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2])); 382ca69d878SAdeleke O. Bankole 383ca69d878SAdeleke O. Bankole } else { 384ca69d878SAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "Wall %" PetscInt_FMT " Forces: Force_x = %12g, Force_y = %12g, Force_z = %12g\n", wall, 385ca69d878SAdeleke O. Bankole reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2])); 386ca69d878SAdeleke O. Bankole } 387ca69d878SAdeleke O. Bankole } 388ca69d878SAdeleke O. Bankole } 389ca69d878SAdeleke O. Bankole PetscCall(PetscFree(reaction_force)); 390ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 391ca69d878SAdeleke O. Bankole } 392ca69d878SAdeleke O. Bankole 393f14660a4SJames Wright // User provided TS Monitor 3942b730f8bSJeremy L Thompson PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 395f14660a4SJames Wright User user = ctx; 396f14660a4SJames Wright 397f17d818dSJames Wright PetscFunctionBeginUser; 39837cbb16aSJed Brown // Print every 'checkpoint_interval' steps 399894de27cSJames Wright if (user->app_ctx->checkpoint_interval <= 0 || step_no % user->app_ctx->checkpoint_interval != 0 || 40049aac155SJeremy L Thompson (user->app_ctx->cont_steps == step_no && step_no != 0)) { 401ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 40249aac155SJeremy L Thompson } 403f14660a4SJames Wright 404f14660a4SJames Wright PetscCall(WriteOutput(user, Q, step_no, time)); 405ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 40677841947SLeila Ghaffari } 40777841947SLeila Ghaffari 40877841947SLeila Ghaffari // TS: Create, setup, and solve 4092b730f8bSJeremy L Thompson PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, Vec *Q, PetscScalar *f_time, TS *ts) { 41077841947SLeila Ghaffari MPI_Comm comm = user->comm; 41177841947SLeila Ghaffari TSAdapt adapt; 41277841947SLeila Ghaffari PetscScalar final_time; 41377841947SLeila Ghaffari 414f17d818dSJames Wright PetscFunctionBeginUser; 4152b730f8bSJeremy L Thompson PetscCall(TSCreate(comm, ts)); 4162b730f8bSJeremy L Thompson PetscCall(TSSetDM(*ts, dm)); 4171a7db67cSJames Wright PetscCall(TSSetApplicationContext(*ts, user)); 41877841947SLeila Ghaffari if (phys->implicit) { 4192b730f8bSJeremy L Thompson PetscCall(TSSetType(*ts, TSBDF)); 42077841947SLeila Ghaffari if (user->op_ifunction) { 4212b730f8bSJeremy L Thompson PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &user)); 42277841947SLeila Ghaffari } else { // Implicit integrators can fall back to using an RHSFunction 4232b730f8bSJeremy L Thompson PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 42477841947SLeila Ghaffari } 425e334ad8fSJed Brown if (user->op_ijacobian) { 4262b730f8bSJeremy L Thompson PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &user)); 427544be873SJed Brown if (app_ctx->amat_type) { 428544be873SJed Brown Mat Pmat, Amat; 4292b730f8bSJeremy L Thompson PetscCall(DMCreateMatrix(dm, &Pmat)); 4302b730f8bSJeremy L Thompson PetscCall(DMSetMatType(dm, app_ctx->amat_type)); 4312b730f8bSJeremy L Thompson PetscCall(DMCreateMatrix(dm, &Amat)); 4322b730f8bSJeremy L Thompson PetscCall(TSSetIJacobian(*ts, Amat, Pmat, NULL, NULL)); 4332b730f8bSJeremy L Thompson PetscCall(MatDestroy(&Amat)); 4342b730f8bSJeremy L Thompson PetscCall(MatDestroy(&Pmat)); 435544be873SJed Brown } 436e334ad8fSJed Brown } 43777841947SLeila Ghaffari } else { 4389ad5e8e4SJames Wright PetscCheck(user->op_rhs_ctx, comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction"); 4392b730f8bSJeremy L Thompson PetscCall(TSSetType(*ts, TSRK)); 4402b730f8bSJeremy L Thompson PetscCall(TSRKSetType(*ts, TSRK5F)); 4412b730f8bSJeremy L Thompson PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 44277841947SLeila Ghaffari } 4432b730f8bSJeremy L Thompson PetscCall(TSSetMaxTime(*ts, 500. * user->units->second)); 4442b730f8bSJeremy L Thompson PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER)); 44551b00d91SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) PetscCall(TSSetErrorIfStepFails(*ts, PETSC_FALSE)); 4462b730f8bSJeremy L Thompson PetscCall(TSSetTimeStep(*ts, 1.e-2 * user->units->second)); 4472b730f8bSJeremy L Thompson PetscCall(TSGetAdapt(*ts, &adapt)); 4482b730f8bSJeremy L Thompson PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * user->units->second, 1.e2 * user->units->second)); 4492b730f8bSJeremy L Thompson PetscCall(TSSetFromOptions(*ts)); 45015637395SJames Wright user->time_bc_set = -1.0; // require all BCs be updated 451d55646a4SJames Wright if (app_ctx->cont_steps) { // continue from previous timestep data 45277841947SLeila Ghaffari PetscInt count; 45377841947SLeila Ghaffari PetscViewer viewer; 4542b730f8bSJeremy L Thompson 4554de8550aSJed Brown if (app_ctx->cont_time <= 0) { // Legacy files did not include step number and time 4562b730f8bSJeremy L Thompson PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer)); 4574de8550aSJed Brown PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL)); 4582b730f8bSJeremy L Thompson PetscCall(PetscViewerDestroy(&viewer)); 4594de8550aSJed Brown PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP, 4604de8550aSJed Brown "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)"); 4614de8550aSJed Brown } 4624de8550aSJed Brown PetscCall(TSSetTime(*ts, app_ctx->cont_time * user->units->second)); 463d8e0aecdSJed Brown PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps)); 46477841947SLeila Ghaffari } 4658fb33541SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 4662b730f8bSJeremy L Thompson PetscCall(TSMonitorSet(*ts, TSMonitor_NS, user, NULL)); 4678fb33541SJames Wright } 468ca69d878SAdeleke O. Bankole if (app_ctx->wall_forces.viewer) { 469ca69d878SAdeleke O. Bankole PetscCall(TSMonitorSet(*ts, TSMonitor_WallForce, user, NULL)); 470ca69d878SAdeleke O. Bankole } 471b7d66439SJames Wright if (app_ctx->turb_spanstats_enable) { 472f5452247SJames Wright PetscCall(TSMonitorSet(*ts, TSMonitor_TurbulenceStatistics, user, NULL)); 473495b9769SJames Wright CeedScalar previous_time = app_ctx->cont_time * user->units->second; 474a424bcd0SJames Wright PetscCallCeed(user->ceed, 475a424bcd0SJames Wright CeedOperatorSetContextDouble(user->spanstats.op_stats_collect_ctx->op, user->spanstats.previous_time_label, &previous_time)); 476a175e481SJames Wright } 4774e9802d1SJames Wright if (app_ctx->diff_filter_monitor) PetscCall(TSMonitorSet(*ts, TSMonitor_DifferentialFilter, user, NULL)); 47877841947SLeila Ghaffari 47928134cfaSJames Wright if (app_ctx->sgs_train_enable) { 48028134cfaSJames Wright PetscCall(TSMonitorSet(*ts, TSMonitor_SGS_DD_Training, user, NULL)); 48128134cfaSJames Wright PetscCall(TSSetPostStep(*ts, TSPostStep_SGS_DD_Training)); 48228134cfaSJames Wright } 48377841947SLeila Ghaffari // Solve 484d8e0aecdSJed Brown PetscReal start_time; 485d8e0aecdSJed Brown PetscInt start_step; 4862b730f8bSJeremy L Thompson PetscCall(TSGetTime(*ts, &start_time)); 487d8e0aecdSJed Brown PetscCall(TSGetStepNumber(*ts, &start_step)); 4887b39487dSJeremy L Thompson 489711a423aSJed Brown PetscCall(PetscLogDefaultBegin()); // So we can use PetscLogStageGetPerfInfo without -log_view 4907b39487dSJeremy L Thompson PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve"); 4917b39487dSJeremy L Thompson PetscCall(TSSetTime(*ts, start_time)); 492d8e0aecdSJed Brown PetscCall(TSSetStepNumber(*ts, start_step)); 4937b39487dSJeremy L Thompson if (PetscPreLoadingOn) { 4947b39487dSJeremy L Thompson // LCOV_EXCL_START 4957b39487dSJeremy L Thompson SNES snes; 4967b39487dSJeremy L Thompson Vec Q_preload; 4977b39487dSJeremy L Thompson PetscReal rtol; 4987b39487dSJeremy L Thompson PetscCall(VecDuplicate(*Q, &Q_preload)); 4997b39487dSJeremy L Thompson PetscCall(VecCopy(*Q, Q_preload)); 5007b39487dSJeremy L Thompson PetscCall(TSGetSNES(*ts, &snes)); 5017b39487dSJeremy L Thompson PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL)); 5022b730f8bSJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 503fc4c3d69SJames Wright PetscCall(TSSetSolution(*ts, Q_preload)); 5047b39487dSJeremy L Thompson PetscCall(TSStep(*ts)); 5052b730f8bSJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 5067b39487dSJeremy L Thompson PetscCall(VecDestroy(&Q_preload)); 5077b39487dSJeremy L Thompson // LCOV_EXCL_STOP 5087b39487dSJeremy L Thompson } else { 5092b730f8bSJeremy L Thompson PetscCall(PetscBarrier((PetscObject)*ts)); 5102b730f8bSJeremy L Thompson PetscCall(TSSolve(*ts, *Q)); 5117b39487dSJeremy L Thompson } 5127b39487dSJeremy L Thompson PetscPreLoadEnd(); 5137b39487dSJeremy L Thompson 5147b39487dSJeremy L Thompson PetscCall(TSGetSolveTime(*ts, &final_time)); 51577841947SLeila Ghaffari *f_time = final_time; 5167b39487dSJeremy L Thompson 5178fb33541SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 518f14660a4SJames Wright PetscInt step_no; 519f14660a4SJames Wright PetscCall(TSGetStepNumber(*ts, &step_no)); 520a175e481SJames Wright if (user->app_ctx->checkpoint_interval > 0 || user->app_ctx->checkpoint_interval == -1) { 521f14660a4SJames Wright PetscCall(WriteOutput(user, *Q, step_no, final_time)); 522f14660a4SJames Wright } 523f14660a4SJames Wright 52475d1798cSJames Wright PetscLogStage stage_id; 525711a423aSJed Brown PetscEventPerfInfo stage_perf; 5267b39487dSJeremy L Thompson 5277b39487dSJeremy L Thompson PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id)); 528711a423aSJed Brown PetscCall(PetscLogStageGetPerfInfo(stage_id, &stage_perf)); 529711a423aSJed Brown PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_perf.time)); 53077841947SLeila Ghaffari } 531ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 53277841947SLeila Ghaffari } 533