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 1877841947SLeila Ghaffari // Compute mass matrix for explicit scheme 192b730f8bSJeremy L Thompson PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm, CeedData ceed_data, Vec M) { 2077841947SLeila Ghaffari CeedQFunction qf_mass; 2177841947SLeila Ghaffari CeedOperator op_mass; 22*3221f4d3SJames Wright OperatorApplyContext op_mass_ctx; 23*3221f4d3SJames Wright Vec Ones_loc; 2477841947SLeila Ghaffari CeedInt num_comp_q, q_data_size; 2577841947SLeila Ghaffari PetscFunctionBeginUser; 2677841947SLeila Ghaffari 2777841947SLeila Ghaffari // CEED Restriction 2877841947SLeila Ghaffari CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_q, &num_comp_q); 2977841947SLeila Ghaffari CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size); 3077841947SLeila Ghaffari 3177841947SLeila Ghaffari // CEED QFunction 32ef080ff9SJames Wright PetscCall(CreateMassQFunction(ceed, num_comp_q, q_data_size, &qf_mass)); 3377841947SLeila Ghaffari 3477841947SLeila Ghaffari // CEED Operator 3577841947SLeila Ghaffari CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass); 36ef080ff9SJames Wright CeedOperatorSetField(op_mass, "u", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE); 372b730f8bSJeremy L Thompson CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data); 382b730f8bSJeremy L Thompson CeedOperatorSetField(op_mass, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE); 3977841947SLeila Ghaffari 40*3221f4d3SJames Wright PetscCall(OperatorApplyContextCreate(NULL, dm, ceed, op_mass, NULL, NULL, NULL, NULL, &op_mass_ctx)); 4177841947SLeila Ghaffari 42*3221f4d3SJames Wright PetscCall(DMGetLocalVector(dm, &Ones_loc)); 43*3221f4d3SJames Wright PetscCall(VecSet(Ones_loc, 1)); 44*3221f4d3SJames Wright PetscCall(ApplyCeedOperatorLocalToGlobal(Ones_loc, M, op_mass_ctx)); 4577841947SLeila Ghaffari 4677841947SLeila Ghaffari // Invert diagonally lumped mass vector for RHS function 472b730f8bSJeremy L Thompson PetscCall(VecReciprocal(M)); 4877841947SLeila Ghaffari 4977841947SLeila Ghaffari // Cleanup 50*3221f4d3SJames Wright PetscCall(OperatorApplyContextDestroy(op_mass_ctx)); 51*3221f4d3SJames Wright PetscCall(DMRestoreLocalVector(dm, &Ones_loc)); 5277841947SLeila Ghaffari CeedQFunctionDestroy(&qf_mass); 5377841947SLeila Ghaffari CeedOperatorDestroy(&op_mass); 5477841947SLeila Ghaffari 5577841947SLeila Ghaffari PetscFunctionReturn(0); 5677841947SLeila Ghaffari } 5777841947SLeila Ghaffari 58a0b9a424SJames Wright // Insert Boundary values if it's a new time 59a0b9a424SJames Wright PetscErrorCode UpdateBoundaryValues(User user, Vec Q_loc, PetscReal t) { 60a0b9a424SJames Wright PetscFunctionBeginUser; 61a0b9a424SJames Wright if (user->time_bc_set != t) { 62a0b9a424SJames Wright PetscCall(DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL)); 63a0b9a424SJames Wright user->time_bc_set = t; 64a0b9a424SJames Wright } 65a0b9a424SJames Wright PetscFunctionReturn(0); 66a0b9a424SJames Wright } 67a0b9a424SJames Wright 6815637395SJames Wright // @brief Update the context label value to new value if necessary. 6915637395SJames Wright // @note This only supports labels with scalar label values (ie. not arrays) 7015637395SJames Wright PetscErrorCode UpdateContextLabel(MPI_Comm comm, PetscScalar update_value, CeedOperator op, CeedContextFieldLabel label) { 7115637395SJames Wright PetscScalar label_value; 72e42c1df6SJames Wright 7315637395SJames Wright PetscFunctionBeginUser; 7415637395SJames Wright PetscCheck(label, comm, PETSC_ERR_ARG_BADPTR, "Label should be non-NULL"); 7515637395SJames Wright 7615637395SJames Wright { 7715637395SJames Wright size_t num_elements; 7815637395SJames Wright const PetscScalar *label_values; 7915637395SJames Wright CeedOperatorGetContextDoubleRead(op, label, &num_elements, &label_values); 8015637395SJames Wright PetscCheck(num_elements == 1, comm, PETSC_ERR_SUP, "%s does not support labels with more than 1 value. Label has %zu values", __func__, 8115637395SJames Wright num_elements); 8215637395SJames Wright label_value = *label_values; 8315637395SJames Wright CeedOperatorRestoreContextDoubleRead(op, label, &label_values); 84e42c1df6SJames Wright } 8515637395SJames Wright 8615637395SJames Wright if (label_value != update_value) { 8715637395SJames Wright CeedOperatorSetContextDouble(op, label, &update_value); 88e42c1df6SJames Wright } 89e42c1df6SJames Wright PetscFunctionReturn(0); 90e42c1df6SJames Wright } 91e42c1df6SJames Wright 9277841947SLeila Ghaffari // RHS (Explicit time-stepper) function setup 9377841947SLeila Ghaffari // This is the RHS of the ODE, given as u_t = G(t,u) 9477841947SLeila Ghaffari // This function takes in a state vector Q and writes into G 9577841947SLeila Ghaffari PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data) { 9677841947SLeila Ghaffari User user = *(User *)user_data; 9715637395SJames Wright MPI_Comm comm = PetscObjectComm((PetscObject)ts); 98c798d55aSJames Wright PetscScalar dt; 995e82a6e1SJeremy L Thompson Vec Q_loc = user->Q_loc, G_loc; 10077841947SLeila Ghaffari PetscMemType q_mem_type, g_mem_type; 10177841947SLeila Ghaffari PetscFunctionBeginUser; 10277841947SLeila Ghaffari 1035e82a6e1SJeremy L Thompson // Get local vector 1042b730f8bSJeremy L Thompson PetscCall(DMGetLocalVector(user->dm, &G_loc)); 1055e82a6e1SJeremy L Thompson 1065e82a6e1SJeremy L Thompson // Update time dependent data 107a0b9a424SJames Wright PetscCall(UpdateBoundaryValues(user, Q_loc, t)); 10815637395SJames Wright if (user->phys->solution_time_label) PetscCall(UpdateContextLabel(comm, t, user->op_rhs, user->phys->solution_time_label)); 1092b730f8bSJeremy L Thompson PetscCall(TSGetTimeStep(ts, &dt)); 11015637395SJames Wright if (user->phys->timestep_size_label) PetscCall(UpdateContextLabel(comm, dt, user->op_rhs, user->phys->timestep_size_label)); 11177841947SLeila Ghaffari 11277841947SLeila Ghaffari // Global-to-local 1132b730f8bSJeremy L Thompson PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc)); 11477841947SLeila Ghaffari 11577841947SLeila Ghaffari // Place PETSc vectors in CEED vectors 116c798d55aSJames Wright PetscCall(VecReadP2C(Q_loc, &q_mem_type, user->q_ceed)); 117c798d55aSJames Wright PetscCall(VecP2C(G_loc, &g_mem_type, user->g_ceed)); 11877841947SLeila Ghaffari 11977841947SLeila Ghaffari // Apply CEED operator 1202b730f8bSJeremy L Thompson CeedOperatorApply(user->op_rhs, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE); 12177841947SLeila Ghaffari 12277841947SLeila Ghaffari // Restore vectors 123c798d55aSJames Wright PetscCall(VecReadC2P(user->q_ceed, q_mem_type, Q_loc)); 124c798d55aSJames Wright PetscCall(VecC2P(user->g_ceed, g_mem_type, G_loc)); 12577841947SLeila Ghaffari 12677841947SLeila Ghaffari // Local-to-Global 1272b730f8bSJeremy L Thompson PetscCall(VecZeroEntries(G)); 1282b730f8bSJeremy L Thompson PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G)); 12977841947SLeila Ghaffari 13077841947SLeila Ghaffari // Inverse of the lumped mass matrix (M is Minv) 1312b730f8bSJeremy L Thompson PetscCall(VecPointwiseMult(G, G, user->M)); 13277841947SLeila Ghaffari 13377841947SLeila Ghaffari // Restore vectors 1342b730f8bSJeremy L Thompson PetscCall(DMRestoreLocalVector(user->dm, &G_loc)); 13577841947SLeila Ghaffari 13677841947SLeila Ghaffari PetscFunctionReturn(0); 13777841947SLeila Ghaffari } 13877841947SLeila Ghaffari 139ca69d878SAdeleke O. Bankole // Surface forces function setup 140ca69d878SAdeleke O. Bankole static PetscErrorCode Surface_Forces_NS(DM dm, Vec G_loc, PetscInt num_walls, const PetscInt walls[], PetscScalar *reaction_force) { 141ca69d878SAdeleke O. Bankole DMLabel face_label; 142ca69d878SAdeleke O. Bankole const PetscScalar *g; 143d6734f85SAdeleke O. Bankole PetscInt dof, dim = 3; 144ca69d878SAdeleke O. Bankole MPI_Comm comm; 145d6734f85SAdeleke O. Bankole PetscSection s; 146ca69d878SAdeleke O. Bankole 147ca69d878SAdeleke O. Bankole PetscFunctionBeginUser; 148ca69d878SAdeleke O. Bankole PetscCall(PetscArrayzero(reaction_force, num_walls * dim)); 149ca69d878SAdeleke O. Bankole PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 150ca69d878SAdeleke O. Bankole PetscCall(DMGetLabel(dm, "Face Sets", &face_label)); 151ca69d878SAdeleke O. Bankole PetscCall(VecGetArrayRead(G_loc, &g)); 152ca69d878SAdeleke O. Bankole for (PetscInt w = 0; w < num_walls; w++) { 153ca69d878SAdeleke O. Bankole const PetscInt wall = walls[w]; 154ca69d878SAdeleke O. Bankole IS wall_is; 155d6734f85SAdeleke O. Bankole PetscCall(DMGetLocalSection(dm, &s)); 156ca69d878SAdeleke O. Bankole PetscCall(DMLabelGetStratumIS(face_label, wall, &wall_is)); 157ca69d878SAdeleke O. Bankole if (wall_is) { // There exist such points on this process 158ca69d878SAdeleke O. Bankole PetscInt num_points; 159d6734f85SAdeleke O. Bankole PetscInt num_comp = 0; 160ca69d878SAdeleke O. Bankole const PetscInt *points; 161d6734f85SAdeleke O. Bankole PetscCall(PetscSectionGetFieldComponents(s, 0, &num_comp)); 162ca69d878SAdeleke O. Bankole PetscCall(ISGetSize(wall_is, &num_points)); 163ca69d878SAdeleke O. Bankole PetscCall(ISGetIndices(wall_is, &points)); 164ca69d878SAdeleke O. Bankole for (PetscInt i = 0; i < num_points; i++) { 165ca69d878SAdeleke O. Bankole const PetscInt p = points[i]; 166ca69d878SAdeleke O. Bankole const StateConservative *r; 167ca69d878SAdeleke O. Bankole PetscCall(DMPlexPointLocalRead(dm, p, g, &r)); 168d6734f85SAdeleke O. Bankole PetscCall(PetscSectionGetDof(s, p, &dof)); 169d6734f85SAdeleke O. Bankole for (PetscInt node = 0; node < dof / num_comp; node++) { 170ca69d878SAdeleke O. Bankole for (PetscInt j = 0; j < 3; j++) { 171d6734f85SAdeleke O. Bankole reaction_force[w * dim + j] -= r[node].momentum[j]; 172d6734f85SAdeleke O. Bankole } 173ca69d878SAdeleke O. Bankole } 174ca69d878SAdeleke O. Bankole } 175ca69d878SAdeleke O. Bankole PetscCall(ISRestoreIndices(wall_is, &points)); 176ca69d878SAdeleke O. Bankole } 177ca69d878SAdeleke O. Bankole PetscCall(ISDestroy(&wall_is)); 178ca69d878SAdeleke O. Bankole } 179ca69d878SAdeleke O. Bankole PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, reaction_force, dim * num_walls, MPIU_SCALAR, MPI_SUM, comm)); 180ca69d878SAdeleke O. Bankole // Restore Vectors 181ca69d878SAdeleke O. Bankole PetscCall(VecRestoreArrayRead(G_loc, &g)); 182ca69d878SAdeleke O. Bankole 183ca69d878SAdeleke O. Bankole PetscFunctionReturn(0); 184ca69d878SAdeleke O. Bankole } 185ca69d878SAdeleke O. Bankole 18677841947SLeila Ghaffari // Implicit time-stepper function setup 1872b730f8bSJeremy L Thompson PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data) { 18877841947SLeila Ghaffari User user = *(User *)user_data; 18915637395SJames Wright MPI_Comm comm = PetscObjectComm((PetscObject)ts); 190c798d55aSJames Wright PetscScalar dt; 1915e82a6e1SJeremy L Thompson Vec Q_loc = user->Q_loc, Q_dot_loc = user->Q_dot_loc, G_loc; 19277841947SLeila Ghaffari PetscMemType q_mem_type, q_dot_mem_type, g_mem_type; 19377841947SLeila Ghaffari PetscFunctionBeginUser; 19477841947SLeila Ghaffari 1955e82a6e1SJeremy L Thompson // Get local vectors 196ca69d878SAdeleke O. Bankole PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 1975e82a6e1SJeremy L Thompson 1985e82a6e1SJeremy L Thompson // Update time dependent data 199a0b9a424SJames Wright PetscCall(UpdateBoundaryValues(user, Q_loc, t)); 20015637395SJames Wright if (user->phys->solution_time_label) PetscCall(UpdateContextLabel(comm, t, user->op_ifunction, user->phys->solution_time_label)); 2012b730f8bSJeremy L Thompson PetscCall(TSGetTimeStep(ts, &dt)); 20215637395SJames Wright if (user->phys->timestep_size_label) PetscCall(UpdateContextLabel(comm, dt, user->op_ifunction, user->phys->timestep_size_label)); 20377841947SLeila Ghaffari 20477841947SLeila Ghaffari // Global-to-local 205878eb0e7SJames Wright PetscCall(DMGlobalToLocalBegin(user->dm, Q, INSERT_VALUES, Q_loc)); 206878eb0e7SJames Wright PetscCall(DMGlobalToLocalBegin(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc)); 207878eb0e7SJames Wright PetscCall(DMGlobalToLocalEnd(user->dm, Q, INSERT_VALUES, Q_loc)); 208878eb0e7SJames Wright PetscCall(DMGlobalToLocalEnd(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc)); 20977841947SLeila Ghaffari 21077841947SLeila Ghaffari // Place PETSc vectors in CEED vectors 211c798d55aSJames Wright PetscCall(VecReadP2C(Q_loc, &q_mem_type, user->q_ceed)); 212c798d55aSJames Wright PetscCall(VecReadP2C(Q_dot_loc, &q_dot_mem_type, user->q_dot_ceed)); 213c798d55aSJames Wright PetscCall(VecP2C(G_loc, &g_mem_type, user->g_ceed)); 21477841947SLeila Ghaffari 21577841947SLeila Ghaffari // Apply CEED operator 2162b730f8bSJeremy L Thompson CeedOperatorApply(user->op_ifunction, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE); 21777841947SLeila Ghaffari 21877841947SLeila Ghaffari // Restore vectors 219c798d55aSJames Wright PetscCall(VecReadC2P(user->q_ceed, q_mem_type, Q_loc)); 220c798d55aSJames Wright PetscCall(VecReadC2P(user->q_dot_ceed, q_dot_mem_type, Q_dot_loc)); 221c798d55aSJames Wright PetscCall(VecC2P(user->g_ceed, g_mem_type, G_loc)); 22277841947SLeila Ghaffari 22319ffbc25SJames Wright if (user->app_ctx->sgs_model_type == SGS_MODEL_DATA_DRIVEN) { 22419ffbc25SJames Wright PetscCall(SGS_DD_ModelApplyIFunction(user, Q_loc, G_loc)); 22519ffbc25SJames Wright } 226be34b3b7SJames Wright 22777841947SLeila Ghaffari // Local-to-Global 2282b730f8bSJeremy L Thompson PetscCall(VecZeroEntries(G)); 2292b730f8bSJeremy L Thompson PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G)); 23077841947SLeila Ghaffari 23177841947SLeila Ghaffari // Restore vectors 232ca69d878SAdeleke O. Bankole PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 23377841947SLeila Ghaffari 23477841947SLeila Ghaffari PetscFunctionReturn(0); 23577841947SLeila Ghaffari } 23677841947SLeila Ghaffari 2372b730f8bSJeremy L Thompson static PetscErrorCode FormPreallocation(User user, PetscBool pbdiagonal, Mat J, CeedVector *coo_values) { 238544be873SJed Brown PetscCount ncoo; 239544be873SJed Brown PetscInt *rows, *cols; 240544be873SJed Brown 241544be873SJed Brown PetscFunctionBeginUser; 242544be873SJed Brown if (pbdiagonal) { 243544be873SJed Brown CeedSize l_size; 244544be873SJed Brown CeedOperatorGetActiveVectorLengths(user->op_ijacobian, &l_size, NULL); 245544be873SJed Brown ncoo = l_size * 5; 246544be873SJed Brown rows = malloc(ncoo * sizeof(rows[0])); 247544be873SJed Brown cols = malloc(ncoo * sizeof(cols[0])); 248544be873SJed Brown for (PetscCount n = 0; n < l_size / 5; n++) { 249544be873SJed Brown for (PetscInt i = 0; i < 5; i++) { 250544be873SJed Brown for (PetscInt j = 0; j < 5; j++) { 251544be873SJed Brown rows[(n * 5 + i) * 5 + j] = n * 5 + i; 252544be873SJed Brown cols[(n * 5 + i) * 5 + j] = n * 5 + j; 253544be873SJed Brown } 254544be873SJed Brown } 255544be873SJed Brown } 256544be873SJed Brown } else { 2572b730f8bSJeremy L Thompson PetscCall(CeedOperatorLinearAssembleSymbolic(user->op_ijacobian, &ncoo, &rows, &cols)); 258544be873SJed Brown } 259544be873SJed Brown PetscCall(MatSetPreallocationCOOLocal(J, ncoo, rows, cols)); 260544be873SJed Brown free(rows); 261544be873SJed Brown free(cols); 262544be873SJed Brown CeedVectorCreate(user->ceed, ncoo, coo_values); 263544be873SJed Brown PetscFunctionReturn(0); 264544be873SJed Brown } 265544be873SJed Brown 2662b730f8bSJeremy L Thompson static PetscErrorCode FormSetValues(User user, PetscBool pbdiagonal, Mat J, CeedVector coo_values) { 267544be873SJed Brown CeedMemType mem_type = CEED_MEM_HOST; 268544be873SJed Brown const PetscScalar *values; 269544be873SJed Brown MatType mat_type; 270544be873SJed Brown 271544be873SJed Brown PetscFunctionBeginUser; 272544be873SJed Brown PetscCall(MatGetType(J, &mat_type)); 2732b730f8bSJeremy L Thompson if (strstr(mat_type, "kokkos") || strstr(mat_type, "cusparse")) mem_type = CEED_MEM_DEVICE; 274544be873SJed Brown if (user->app_ctx->pmat_pbdiagonal) { 2752b730f8bSJeremy L Thompson CeedOperatorLinearAssemblePointBlockDiagonal(user->op_ijacobian, coo_values, CEED_REQUEST_IMMEDIATE); 276544be873SJed Brown } else { 277544be873SJed Brown CeedOperatorLinearAssemble(user->op_ijacobian, coo_values); 278544be873SJed Brown } 279544be873SJed Brown CeedVectorGetArrayRead(coo_values, mem_type, &values); 280544be873SJed Brown PetscCall(MatSetValuesCOO(J, values, INSERT_VALUES)); 281544be873SJed Brown CeedVectorRestoreArrayRead(coo_values, &values); 282544be873SJed Brown PetscFunctionReturn(0); 283544be873SJed Brown } 284544be873SJed Brown 2852b730f8bSJeremy L Thompson PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, PetscReal shift, Mat J, Mat J_pre, void *user_data) { 286e334ad8fSJed Brown User user = *(User *)user_data; 287d6bf345cSJed Brown PetscBool J_is_shell, J_is_mffd, J_pre_is_shell; 288e334ad8fSJed Brown PetscFunctionBeginUser; 28917b0d5c6SJeremy L Thompson if (user->phys->ijacobian_time_shift_label) CeedOperatorSetContextDouble(user->op_ijacobian, user->phys->ijacobian_time_shift_label, &shift); 290d6bf345cSJed Brown PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd)); 291e334ad8fSJed Brown PetscCall(PetscObjectTypeCompare((PetscObject)J, MATSHELL, &J_is_shell)); 2922b730f8bSJeremy L Thompson PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATSHELL, &J_pre_is_shell)); 293e334ad8fSJed Brown if (!user->matrices_set_up) { 294e334ad8fSJed Brown if (J_is_shell) { 295b07979f9SJames Wright OperatorApplyContext op_ijacobian_ctx; 296b07979f9SJames Wright OperatorApplyContextCreate(user->dm, user->dm, user->ceed, user->op_ijacobian, user->q_ceed, user->g_ceed, user->Q_dot_loc, NULL, 297b07979f9SJames Wright &op_ijacobian_ctx); 298b07979f9SJames Wright PetscCall(MatShellSetContext(J, op_ijacobian_ctx)); 299b07979f9SJames Wright PetscCall(MatShellSetContextDestroy(J, (PetscErrorCode(*)(void *))OperatorApplyContextDestroy)); 300b07979f9SJames Wright PetscCall(MatShellSetOperation(J, MATOP_MULT, (void (*)(void))MatMult_Ceed)); 301b07979f9SJames Wright PetscCall(MatShellSetOperation(J, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiag_Ceed)); 302e334ad8fSJed Brown PetscCall(MatSetUp(J)); 303e334ad8fSJed Brown } 304e334ad8fSJed Brown if (!J_pre_is_shell) { 3052b730f8bSJeremy L Thompson PetscCall(FormPreallocation(user, user->app_ctx->pmat_pbdiagonal, J_pre, &user->coo_values_pmat)); 306544be873SJed Brown } 307d6bf345cSJed Brown if (J != J_pre && !J_is_shell && !J_is_mffd) { 308544be873SJed Brown PetscCall(FormPreallocation(user, PETSC_FALSE, J, &user->coo_values_amat)); 309544be873SJed Brown } 310e334ad8fSJed Brown user->matrices_set_up = true; 311e334ad8fSJed Brown } 312e334ad8fSJed Brown if (!J_pre_is_shell) { 3132b730f8bSJeremy L Thompson PetscCall(FormSetValues(user, user->app_ctx->pmat_pbdiagonal, J_pre, user->coo_values_pmat)); 314e334ad8fSJed Brown } 315d6bf345cSJed Brown if (user->coo_values_amat) { 316d6bf345cSJed Brown PetscCall(FormSetValues(user, PETSC_FALSE, J, user->coo_values_amat)); 317d6bf345cSJed Brown } else if (J_is_mffd) { 318d6bf345cSJed Brown PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 319d6bf345cSJed Brown PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 320d6bf345cSJed Brown } 321e334ad8fSJed Brown PetscFunctionReturn(0); 322e334ad8fSJed Brown } 323e334ad8fSJed Brown 3242b730f8bSJeremy L Thompson PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, PetscScalar time) { 32577841947SLeila Ghaffari Vec Q_loc; 32677841947SLeila Ghaffari char file_path[PETSC_MAX_PATH_LEN]; 32777841947SLeila Ghaffari PetscViewer viewer; 32877841947SLeila Ghaffari PetscFunctionBeginUser; 32977841947SLeila Ghaffari 33037cbb16aSJed Brown if (user->app_ctx->checkpoint_vtk) { 33177841947SLeila Ghaffari // Set up output 332f14660a4SJames Wright PetscCall(DMGetLocalVector(user->dm, &Q_loc)); 333f14660a4SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec")); 334f14660a4SJames Wright PetscCall(VecZeroEntries(Q_loc)); 335f14660a4SJames Wright PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc)); 33677841947SLeila Ghaffari 33777841947SLeila Ghaffari // Output 33837cbb16aSJed Brown PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 339f14660a4SJames Wright 3402b730f8bSJeremy L Thompson PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer)); 341f14660a4SJames Wright PetscCall(VecView(Q_loc, viewer)); 342f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 34377841947SLeila Ghaffari if (user->dm_viz) { 34477841947SLeila Ghaffari Vec Q_refined, Q_refined_loc; 34577841947SLeila Ghaffari char file_path_refined[PETSC_MAX_PATH_LEN]; 34677841947SLeila Ghaffari PetscViewer viewer_refined; 34777841947SLeila Ghaffari 348f14660a4SJames Wright PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined)); 349f14660a4SJames Wright PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc)); 350f14660a4SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined")); 351f14660a4SJames Wright 352f14660a4SJames Wright PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined)); 353f14660a4SJames Wright PetscCall(VecZeroEntries(Q_refined_loc)); 3542b730f8bSJeremy L Thompson PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc)); 355f14660a4SJames Wright 35637cbb16aSJed Brown PetscCall( 35737cbb16aSJed Brown PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 358f14660a4SJames Wright 3592b730f8bSJeremy L Thompson PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined)); 360f14660a4SJames Wright PetscCall(VecView(Q_refined_loc, viewer_refined)); 361f14660a4SJames Wright PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc)); 362f14660a4SJames Wright PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined)); 363f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer_refined)); 36477841947SLeila Ghaffari } 365f14660a4SJames Wright PetscCall(DMRestoreLocalVector(user->dm, &Q_loc)); 36637cbb16aSJed Brown } 36777841947SLeila Ghaffari 36877841947SLeila Ghaffari // Save data in a binary file for continuation of simulations 36969293791SJames Wright if (user->app_ctx->add_stepnum2bin) { 37037cbb16aSJed Brown PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", user->app_ctx->output_dir, step_no)); 37169293791SJames Wright } else { 3722b730f8bSJeremy L Thompson PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", user->app_ctx->output_dir)); 37369293791SJames Wright } 3742b730f8bSJeremy L Thompson PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, &viewer)); 375f14660a4SJames Wright 3764de8550aSJed Brown PetscInt token = FLUIDS_FILE_TOKEN; 3774de8550aSJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &token, 1, PETSC_INT)); 3784de8550aSJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &step_no, 1, PETSC_INT)); 3794de8550aSJed Brown time /= user->units->second; // Dimensionalize time back 3804de8550aSJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL)); 381f14660a4SJames Wright PetscCall(VecView(Q, viewer)); 382f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 383f14660a4SJames Wright PetscFunctionReturn(0); 384f14660a4SJames Wright } 385f14660a4SJames Wright 386ca69d878SAdeleke O. Bankole // CSV Monitor 387ca69d878SAdeleke O. Bankole PetscErrorCode TSMonitor_WallForce(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 388ca69d878SAdeleke O. Bankole User user = ctx; 389ca69d878SAdeleke O. Bankole Vec G_loc; 390ca69d878SAdeleke O. Bankole PetscInt num_wall = user->app_ctx->wall_forces.num_wall, dim = 3; 391ca69d878SAdeleke O. Bankole const PetscInt *walls = user->app_ctx->wall_forces.walls; 392ca69d878SAdeleke O. Bankole PetscViewer viewer = user->app_ctx->wall_forces.viewer; 393ca69d878SAdeleke O. Bankole PetscViewerFormat format = user->app_ctx->wall_forces.viewer_format; 394ca69d878SAdeleke O. Bankole PetscScalar *reaction_force; 395ca69d878SAdeleke O. Bankole PetscBool iascii; 396ca69d878SAdeleke O. Bankole 397ca69d878SAdeleke O. Bankole PetscFunctionBeginUser; 398ca69d878SAdeleke O. Bankole if (!viewer) PetscFunctionReturn(0); 399ca69d878SAdeleke O. Bankole PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 400ca69d878SAdeleke O. Bankole PetscCall(PetscMalloc1(num_wall * dim, &reaction_force)); 401ca69d878SAdeleke O. Bankole PetscCall(Surface_Forces_NS(user->dm, G_loc, num_wall, walls, reaction_force)); 402ca69d878SAdeleke O. Bankole PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 403ca69d878SAdeleke O. Bankole 404ca69d878SAdeleke O. Bankole PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 405ca69d878SAdeleke O. Bankole 406ca69d878SAdeleke O. Bankole if (iascii) { 407ca69d878SAdeleke O. Bankole if (format == PETSC_VIEWER_ASCII_CSV && !user->app_ctx->wall_forces.header_written) { 408ca69d878SAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "Step,Time,Wall,ForceX,ForceY,ForceZ\n")); 409ca69d878SAdeleke O. Bankole user->app_ctx->wall_forces.header_written = PETSC_TRUE; 410ca69d878SAdeleke O. Bankole } 411ca69d878SAdeleke O. Bankole for (PetscInt w = 0; w < num_wall; w++) { 412ca69d878SAdeleke O. Bankole PetscInt wall = walls[w]; 413ca69d878SAdeleke O. Bankole if (format == PETSC_VIEWER_ASCII_CSV) { 414ca69d878SAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ",%g,%" PetscInt_FMT ",%g,%g,%g\n", step_no, time, wall, 415ca69d878SAdeleke O. Bankole reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2])); 416ca69d878SAdeleke O. Bankole 417ca69d878SAdeleke O. Bankole } else { 418ca69d878SAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "Wall %" PetscInt_FMT " Forces: Force_x = %12g, Force_y = %12g, Force_z = %12g\n", wall, 419ca69d878SAdeleke O. Bankole reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2])); 420ca69d878SAdeleke O. Bankole } 421ca69d878SAdeleke O. Bankole } 422ca69d878SAdeleke O. Bankole } 423ca69d878SAdeleke O. Bankole PetscCall(PetscFree(reaction_force)); 424ca69d878SAdeleke O. Bankole PetscFunctionReturn(0); 425ca69d878SAdeleke O. Bankole } 426ca69d878SAdeleke O. Bankole 427f14660a4SJames Wright // User provided TS Monitor 4282b730f8bSJeremy L Thompson PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 429f14660a4SJames Wright User user = ctx; 430f14660a4SJames Wright PetscFunctionBeginUser; 431f14660a4SJames Wright 43237cbb16aSJed Brown // Print every 'checkpoint_interval' steps 433894de27cSJames Wright if (user->app_ctx->checkpoint_interval <= 0 || step_no % user->app_ctx->checkpoint_interval != 0 || 43449aac155SJeremy L Thompson (user->app_ctx->cont_steps == step_no && step_no != 0)) { 435894de27cSJames Wright PetscFunctionReturn(0); 43649aac155SJeremy L Thompson } 437f14660a4SJames Wright 438f14660a4SJames Wright PetscCall(WriteOutput(user, Q, step_no, time)); 43977841947SLeila Ghaffari 44077841947SLeila Ghaffari PetscFunctionReturn(0); 44177841947SLeila Ghaffari } 44277841947SLeila Ghaffari 44377841947SLeila Ghaffari // TS: Create, setup, and solve 4442b730f8bSJeremy L Thompson PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, Vec *Q, PetscScalar *f_time, TS *ts) { 44577841947SLeila Ghaffari MPI_Comm comm = user->comm; 44677841947SLeila Ghaffari TSAdapt adapt; 44777841947SLeila Ghaffari PetscScalar final_time; 44877841947SLeila Ghaffari PetscFunctionBeginUser; 44977841947SLeila Ghaffari 4502b730f8bSJeremy L Thompson PetscCall(TSCreate(comm, ts)); 4512b730f8bSJeremy L Thompson PetscCall(TSSetDM(*ts, dm)); 45277841947SLeila Ghaffari if (phys->implicit) { 4532b730f8bSJeremy L Thompson PetscCall(TSSetType(*ts, TSBDF)); 45477841947SLeila Ghaffari if (user->op_ifunction) { 4552b730f8bSJeremy L Thompson PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &user)); 45677841947SLeila Ghaffari } else { // Implicit integrators can fall back to using an RHSFunction 4572b730f8bSJeremy L Thompson PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 45877841947SLeila Ghaffari } 459e334ad8fSJed Brown if (user->op_ijacobian) { 4602b730f8bSJeremy L Thompson PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &user)); 461544be873SJed Brown if (app_ctx->amat_type) { 462544be873SJed Brown Mat Pmat, Amat; 4632b730f8bSJeremy L Thompson PetscCall(DMCreateMatrix(dm, &Pmat)); 4642b730f8bSJeremy L Thompson PetscCall(DMSetMatType(dm, app_ctx->amat_type)); 4652b730f8bSJeremy L Thompson PetscCall(DMCreateMatrix(dm, &Amat)); 4662b730f8bSJeremy L Thompson PetscCall(TSSetIJacobian(*ts, Amat, Pmat, NULL, NULL)); 4672b730f8bSJeremy L Thompson PetscCall(MatDestroy(&Amat)); 4682b730f8bSJeremy L Thompson PetscCall(MatDestroy(&Pmat)); 469544be873SJed Brown } 470e334ad8fSJed Brown } 47177841947SLeila Ghaffari } else { 4720e654f56SJames Wright PetscCheck(user->op_rhs, comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction"); 4732b730f8bSJeremy L Thompson PetscCall(TSSetType(*ts, TSRK)); 4742b730f8bSJeremy L Thompson PetscCall(TSRKSetType(*ts, TSRK5F)); 4752b730f8bSJeremy L Thompson PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 47677841947SLeila Ghaffari } 4772b730f8bSJeremy L Thompson PetscCall(TSSetMaxTime(*ts, 500. * user->units->second)); 4782b730f8bSJeremy L Thompson PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER)); 47951b00d91SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) PetscCall(TSSetErrorIfStepFails(*ts, PETSC_FALSE)); 4802b730f8bSJeremy L Thompson PetscCall(TSSetTimeStep(*ts, 1.e-2 * user->units->second)); 4818fb33541SJames Wright if (app_ctx->test_type != TESTTYPE_NONE) { 4822b730f8bSJeremy L Thompson PetscCall(TSSetMaxSteps(*ts, 10)); 4832b730f8bSJeremy L Thompson } 4842b730f8bSJeremy L Thompson PetscCall(TSGetAdapt(*ts, &adapt)); 4852b730f8bSJeremy L Thompson PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * user->units->second, 1.e2 * user->units->second)); 4862b730f8bSJeremy L Thompson PetscCall(TSSetFromOptions(*ts)); 48715637395SJames Wright user->time_bc_set = -1.0; // require all BCs be updated 48877841947SLeila Ghaffari if (!app_ctx->cont_steps) { // print initial condition 4898fb33541SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 4902b730f8bSJeremy L Thompson PetscCall(TSMonitor_NS(*ts, 0, 0., *Q, user)); 49177841947SLeila Ghaffari } 49277841947SLeila Ghaffari } else { // continue from time of last output 49377841947SLeila Ghaffari PetscInt count; 49477841947SLeila Ghaffari PetscViewer viewer; 4952b730f8bSJeremy L Thompson 4964de8550aSJed Brown if (app_ctx->cont_time <= 0) { // Legacy files did not include step number and time 4972b730f8bSJeremy L Thompson PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer)); 4984de8550aSJed Brown PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL)); 4992b730f8bSJeremy L Thompson PetscCall(PetscViewerDestroy(&viewer)); 5004de8550aSJed Brown PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP, 5014de8550aSJed Brown "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)"); 5024de8550aSJed Brown } 5034de8550aSJed Brown PetscCall(TSSetTime(*ts, app_ctx->cont_time * user->units->second)); 504d8e0aecdSJed Brown PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps)); 50577841947SLeila Ghaffari } 5068fb33541SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 5072b730f8bSJeremy L Thompson PetscCall(TSMonitorSet(*ts, TSMonitor_NS, user, NULL)); 5088fb33541SJames Wright } 509ca69d878SAdeleke O. Bankole if (app_ctx->wall_forces.viewer) { 510ca69d878SAdeleke O. Bankole PetscCall(TSMonitorSet(*ts, TSMonitor_WallForce, user, NULL)); 511ca69d878SAdeleke O. Bankole } 512b7d66439SJames Wright if (app_ctx->turb_spanstats_enable) { 513a175e481SJames Wright PetscCall(TSMonitorSet(*ts, TSMonitor_Statistics, user, NULL)); 514495b9769SJames Wright CeedScalar previous_time = app_ctx->cont_time * user->units->second; 515ed331efdSJames Wright CeedOperatorSetContextDouble(user->spanstats.op_stats_collect_ctx->op, user->spanstats.previous_time_label, &previous_time); 516a175e481SJames Wright } 51777841947SLeila Ghaffari 51877841947SLeila Ghaffari // Solve 519d8e0aecdSJed Brown PetscReal start_time; 520d8e0aecdSJed Brown PetscInt start_step; 5212b730f8bSJeremy L Thompson PetscCall(TSGetTime(*ts, &start_time)); 522d8e0aecdSJed Brown PetscCall(TSGetStepNumber(*ts, &start_step)); 5237b39487dSJeremy L Thompson 5247b39487dSJeremy L Thompson PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve"); 5257b39487dSJeremy L Thompson PetscCall(TSSetTime(*ts, start_time)); 526d8e0aecdSJed Brown PetscCall(TSSetStepNumber(*ts, start_step)); 5277b39487dSJeremy L Thompson if (PetscPreLoadingOn) { 5287b39487dSJeremy L Thompson // LCOV_EXCL_START 5297b39487dSJeremy L Thompson SNES snes; 5307b39487dSJeremy L Thompson Vec Q_preload; 5317b39487dSJeremy L Thompson PetscReal rtol; 5327b39487dSJeremy L Thompson PetscCall(VecDuplicate(*Q, &Q_preload)); 5337b39487dSJeremy L Thompson PetscCall(VecCopy(*Q, Q_preload)); 5347b39487dSJeremy L Thompson PetscCall(TSGetSNES(*ts, &snes)); 5357b39487dSJeremy L Thompson PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL)); 5362b730f8bSJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 5377b39487dSJeremy L Thompson PetscCall(TSSetSolution(*ts, *Q)); 5387b39487dSJeremy L Thompson PetscCall(TSStep(*ts)); 5392b730f8bSJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 5407b39487dSJeremy L Thompson PetscCall(VecDestroy(&Q_preload)); 5417b39487dSJeremy L Thompson // LCOV_EXCL_STOP 5427b39487dSJeremy L Thompson } else { 5432b730f8bSJeremy L Thompson PetscCall(PetscBarrier((PetscObject)*ts)); 5442b730f8bSJeremy L Thompson PetscCall(TSSolve(*ts, *Q)); 5457b39487dSJeremy L Thompson } 5467b39487dSJeremy L Thompson PetscPreLoadEnd(); 5477b39487dSJeremy L Thompson 5487b39487dSJeremy L Thompson PetscCall(TSGetSolveTime(*ts, &final_time)); 54977841947SLeila Ghaffari *f_time = final_time; 5507b39487dSJeremy L Thompson 5518fb33541SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 552f14660a4SJames Wright PetscInt step_no; 553f14660a4SJames Wright PetscCall(TSGetStepNumber(*ts, &step_no)); 554a175e481SJames Wright if (user->app_ctx->checkpoint_interval > 0 || user->app_ctx->checkpoint_interval == -1) { 555f14660a4SJames Wright PetscCall(WriteOutput(user, *Q, step_no, final_time)); 556f14660a4SJames Wright } 557f14660a4SJames Wright 5587b39487dSJeremy L Thompson PetscLogEvent stage_id; 5597b39487dSJeremy L Thompson PetscStageLog stage_log; 5607b39487dSJeremy L Thompson 5617b39487dSJeremy L Thompson PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id)); 5627b39487dSJeremy L Thompson PetscCall(PetscLogGetStageLog(&stage_log)); 5632b730f8bSJeremy L Thompson PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_log->stageInfo[stage_id].perfInfo.time)); 56477841947SLeila Ghaffari } 56577841947SLeila Ghaffari PetscFunctionReturn(0); 56677841947SLeila Ghaffari } 567