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 1177841947SLeila Ghaffari #include "../navierstokes.h" 12*ca69d878SAdeleke O. Bankole #include "../qfunctions/newtonian_state.h" 1377841947SLeila Ghaffari 1477841947SLeila Ghaffari // Compute mass matrix for explicit scheme 152b730f8bSJeremy L Thompson PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm, CeedData ceed_data, Vec M) { 1677841947SLeila Ghaffari Vec M_loc; 1777841947SLeila Ghaffari CeedQFunction qf_mass; 1877841947SLeila Ghaffari CeedOperator op_mass; 1977841947SLeila Ghaffari CeedVector m_ceed, ones_vec; 2077841947SLeila Ghaffari CeedInt num_comp_q, q_data_size; 2177841947SLeila Ghaffari PetscFunctionBeginUser; 2277841947SLeila Ghaffari 2377841947SLeila Ghaffari // CEED Restriction 2477841947SLeila Ghaffari CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_q, &num_comp_q); 2577841947SLeila Ghaffari CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size); 2677841947SLeila Ghaffari CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &m_ceed, NULL); 2777841947SLeila Ghaffari CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &ones_vec, NULL); 2877841947SLeila Ghaffari CeedVectorSetValue(ones_vec, 1.0); 2977841947SLeila Ghaffari 3077841947SLeila Ghaffari // CEED QFunction 31ef080ff9SJames Wright PetscCall(CreateMassQFunction(ceed, num_comp_q, q_data_size, &qf_mass)); 3277841947SLeila Ghaffari 3377841947SLeila Ghaffari // CEED Operator 3477841947SLeila Ghaffari CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass); 35ef080ff9SJames Wright CeedOperatorSetField(op_mass, "u", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE); 362b730f8bSJeremy L Thompson CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data); 372b730f8bSJeremy L Thompson CeedOperatorSetField(op_mass, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE); 3877841947SLeila Ghaffari 3977841947SLeila Ghaffari // Place PETSc vector in CEED vector 4077841947SLeila Ghaffari CeedScalar *m; 4177841947SLeila Ghaffari PetscMemType m_mem_type; 422b730f8bSJeremy L Thompson PetscCall(DMGetLocalVector(dm, &M_loc)); 432b730f8bSJeremy L Thompson PetscCall(VecGetArrayAndMemType(M_loc, (PetscScalar **)&m, &m_mem_type)); 4477841947SLeila Ghaffari CeedVectorSetArray(m_ceed, MemTypeP2C(m_mem_type), CEED_USE_POINTER, m); 4577841947SLeila Ghaffari 4677841947SLeila Ghaffari // Apply CEED Operator 4777841947SLeila Ghaffari CeedOperatorApply(op_mass, ones_vec, m_ceed, CEED_REQUEST_IMMEDIATE); 4877841947SLeila Ghaffari 4977841947SLeila Ghaffari // Restore vectors 5077841947SLeila Ghaffari CeedVectorTakeArray(m_ceed, MemTypeP2C(m_mem_type), NULL); 512b730f8bSJeremy L Thompson PetscCall(VecRestoreArrayReadAndMemType(M_loc, (const PetscScalar **)&m)); 5277841947SLeila Ghaffari 5377841947SLeila Ghaffari // Local-to-Global 542b730f8bSJeremy L Thompson PetscCall(VecZeroEntries(M)); 552b730f8bSJeremy L Thompson PetscCall(DMLocalToGlobal(dm, M_loc, ADD_VALUES, M)); 562b730f8bSJeremy L Thompson PetscCall(DMRestoreLocalVector(dm, &M_loc)); 5777841947SLeila Ghaffari 5877841947SLeila Ghaffari // Invert diagonally lumped mass vector for RHS function 592b730f8bSJeremy L Thompson PetscCall(VecReciprocal(M)); 6077841947SLeila Ghaffari 6177841947SLeila Ghaffari // Cleanup 6277841947SLeila Ghaffari CeedVectorDestroy(&ones_vec); 6377841947SLeila Ghaffari CeedVectorDestroy(&m_ceed); 6477841947SLeila Ghaffari CeedQFunctionDestroy(&qf_mass); 6577841947SLeila Ghaffari CeedOperatorDestroy(&op_mass); 6677841947SLeila Ghaffari 6777841947SLeila Ghaffari PetscFunctionReturn(0); 6877841947SLeila Ghaffari } 6977841947SLeila Ghaffari 70a0b9a424SJames Wright // Insert Boundary values if it's a new time 71a0b9a424SJames Wright PetscErrorCode UpdateBoundaryValues(User user, Vec Q_loc, PetscReal t) { 72a0b9a424SJames Wright PetscFunctionBeginUser; 73a0b9a424SJames Wright if (user->time_bc_set != t) { 74a0b9a424SJames Wright PetscCall(DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL)); 75a0b9a424SJames Wright user->time_bc_set = t; 76a0b9a424SJames Wright } 77a0b9a424SJames Wright PetscFunctionReturn(0); 78a0b9a424SJames Wright } 79a0b9a424SJames Wright 80e42c1df6SJames Wright PetscErrorCode UpdateContextLabel(PetscScalar *previous_value, PetscScalar update_value, CeedOperator op, CeedContextFieldLabel label) { 81e42c1df6SJames Wright PetscFunctionBeginUser; 82e42c1df6SJames Wright 83e42c1df6SJames Wright if (*previous_value != update_value) { 84e42c1df6SJames Wright if (label) { 85e42c1df6SJames Wright CeedOperatorContextSetDouble(op, label, &update_value); 86e42c1df6SJames Wright } 87e42c1df6SJames Wright *previous_value = 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; 97e42c1df6SJames Wright PetscScalar *q, *g, dt; 985e82a6e1SJeremy L Thompson Vec Q_loc = user->Q_loc, G_loc; 9977841947SLeila Ghaffari PetscMemType q_mem_type, g_mem_type; 10077841947SLeila Ghaffari PetscFunctionBeginUser; 10177841947SLeila Ghaffari 1025e82a6e1SJeremy L Thompson // Get local vector 1032b730f8bSJeremy L Thompson PetscCall(DMGetLocalVector(user->dm, &G_loc)); 1045e82a6e1SJeremy L Thompson 1055e82a6e1SJeremy L Thompson // Update time dependent data 106a0b9a424SJames Wright PetscCall(UpdateBoundaryValues(user, Q_loc, t)); 107e42c1df6SJames Wright PetscCall(UpdateContextLabel(&user->time, t, user->op_rhs, user->phys->solution_time_label)); 1082b730f8bSJeremy L Thompson PetscCall(TSGetTimeStep(ts, &dt)); 109e42c1df6SJames Wright PetscCall(UpdateContextLabel(&user->dt, dt, user->op_rhs, user->phys->timestep_size_label)); 11077841947SLeila Ghaffari 11177841947SLeila Ghaffari // Global-to-local 1122b730f8bSJeremy L Thompson PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc)); 11377841947SLeila Ghaffari 11477841947SLeila Ghaffari // Place PETSc vectors in CEED vectors 1152b730f8bSJeremy L Thompson PetscCall(VecGetArrayReadAndMemType(Q_loc, (const PetscScalar **)&q, &q_mem_type)); 1162b730f8bSJeremy L Thompson PetscCall(VecGetArrayAndMemType(G_loc, &g, &g_mem_type)); 11777841947SLeila Ghaffari CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, q); 11877841947SLeila Ghaffari CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g); 11977841947SLeila Ghaffari 12077841947SLeila Ghaffari // Apply CEED operator 1212b730f8bSJeremy L Thompson CeedOperatorApply(user->op_rhs, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE); 12277841947SLeila Ghaffari 12377841947SLeila Ghaffari // Restore vectors 12477841947SLeila Ghaffari CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL); 12577841947SLeila Ghaffari CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL); 1262b730f8bSJeremy L Thompson PetscCall(VecRestoreArrayReadAndMemType(Q_loc, (const PetscScalar **)&q)); 1272b730f8bSJeremy L Thompson PetscCall(VecRestoreArrayAndMemType(G_loc, &g)); 12877841947SLeila Ghaffari 12977841947SLeila Ghaffari // Local-to-Global 1302b730f8bSJeremy L Thompson PetscCall(VecZeroEntries(G)); 1312b730f8bSJeremy L Thompson PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G)); 13277841947SLeila Ghaffari 13377841947SLeila Ghaffari // Inverse of the lumped mass matrix (M is Minv) 1342b730f8bSJeremy L Thompson PetscCall(VecPointwiseMult(G, G, user->M)); 13577841947SLeila Ghaffari 13677841947SLeila Ghaffari // Restore vectors 1372b730f8bSJeremy L Thompson PetscCall(DMRestoreLocalVector(user->dm, &G_loc)); 13877841947SLeila Ghaffari 13977841947SLeila Ghaffari PetscFunctionReturn(0); 14077841947SLeila Ghaffari } 14177841947SLeila Ghaffari 142*ca69d878SAdeleke O. Bankole // Surface forces function setup 143*ca69d878SAdeleke O. Bankole static PetscErrorCode Surface_Forces_NS(DM dm, Vec G_loc, PetscInt num_walls, const PetscInt walls[], PetscScalar *reaction_force) { 144*ca69d878SAdeleke O. Bankole DMLabel face_label; 145*ca69d878SAdeleke O. Bankole const PetscScalar *g; 146*ca69d878SAdeleke O. Bankole PetscInt dim = 3; 147*ca69d878SAdeleke O. Bankole MPI_Comm comm; 148*ca69d878SAdeleke O. Bankole 149*ca69d878SAdeleke O. Bankole PetscFunctionBeginUser; 150*ca69d878SAdeleke O. Bankole PetscCall(PetscArrayzero(reaction_force, num_walls * dim)); 151*ca69d878SAdeleke O. Bankole PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 152*ca69d878SAdeleke O. Bankole PetscCall(DMGetLabel(dm, "Face Sets", &face_label)); 153*ca69d878SAdeleke O. Bankole PetscCall(VecGetArrayRead(G_loc, &g)); 154*ca69d878SAdeleke O. Bankole for (PetscInt w = 0; w < num_walls; w++) { 155*ca69d878SAdeleke O. Bankole const PetscInt wall = walls[w]; 156*ca69d878SAdeleke O. Bankole IS wall_is; 157*ca69d878SAdeleke O. Bankole PetscCall(DMLabelGetStratumIS(face_label, wall, &wall_is)); 158*ca69d878SAdeleke O. Bankole if (wall_is) { // There exist such points on this process 159*ca69d878SAdeleke O. Bankole PetscInt num_points; 160*ca69d878SAdeleke O. Bankole const PetscInt *points; 161*ca69d878SAdeleke O. Bankole PetscCall(ISGetSize(wall_is, &num_points)); 162*ca69d878SAdeleke O. Bankole PetscCall(ISGetIndices(wall_is, &points)); 163*ca69d878SAdeleke O. Bankole for (PetscInt i = 0; i < num_points; i++) { 164*ca69d878SAdeleke O. Bankole const PetscInt p = points[i]; 165*ca69d878SAdeleke O. Bankole const StateConservative *r; 166*ca69d878SAdeleke O. Bankole PetscCall(DMPlexPointLocalRead(dm, p, g, &r)); 167*ca69d878SAdeleke O. Bankole if (!r) continue; 168*ca69d878SAdeleke O. Bankole for (PetscInt j = 0; j < 3; j++) { 169*ca69d878SAdeleke O. Bankole reaction_force[w * dim + j] -= r->momentum[j]; 170*ca69d878SAdeleke O. Bankole } 171*ca69d878SAdeleke O. Bankole } 172*ca69d878SAdeleke O. Bankole PetscCall(ISRestoreIndices(wall_is, &points)); 173*ca69d878SAdeleke O. Bankole } 174*ca69d878SAdeleke O. Bankole PetscCall(ISDestroy(&wall_is)); 175*ca69d878SAdeleke O. Bankole } 176*ca69d878SAdeleke O. Bankole PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, reaction_force, dim * num_walls, MPIU_SCALAR, MPI_SUM, comm)); 177*ca69d878SAdeleke O. Bankole // Restore Vectors 178*ca69d878SAdeleke O. Bankole PetscCall(VecRestoreArrayRead(G_loc, &g)); 179*ca69d878SAdeleke O. Bankole 180*ca69d878SAdeleke O. Bankole PetscFunctionReturn(0); 181*ca69d878SAdeleke O. Bankole } 182*ca69d878SAdeleke O. Bankole 18377841947SLeila Ghaffari // Implicit time-stepper function setup 1842b730f8bSJeremy L Thompson PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data) { 18577841947SLeila Ghaffari User user = *(User *)user_data; 18677841947SLeila Ghaffari const PetscScalar *q, *q_dot; 187e42c1df6SJames Wright PetscScalar *g, dt; 1885e82a6e1SJeremy L Thompson Vec Q_loc = user->Q_loc, Q_dot_loc = user->Q_dot_loc, G_loc; 18977841947SLeila Ghaffari PetscMemType q_mem_type, q_dot_mem_type, g_mem_type; 19077841947SLeila Ghaffari PetscFunctionBeginUser; 19177841947SLeila Ghaffari 1925e82a6e1SJeremy L Thompson // Get local vectors 193*ca69d878SAdeleke O. Bankole PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 1945e82a6e1SJeremy L Thompson 1955e82a6e1SJeremy L Thompson // Update time dependent data 196a0b9a424SJames Wright PetscCall(UpdateBoundaryValues(user, Q_loc, t)); 197e42c1df6SJames Wright PetscCall(UpdateContextLabel(&user->time, t, user->op_ifunction, user->phys->solution_time_label)); 1982b730f8bSJeremy L Thompson PetscCall(TSGetTimeStep(ts, &dt)); 199e42c1df6SJames Wright PetscCall(UpdateContextLabel(&user->dt, dt, user->op_ifunction, user->phys->timestep_size_label)); 20077841947SLeila Ghaffari 20177841947SLeila Ghaffari // Global-to-local 202878eb0e7SJames Wright PetscCall(DMGlobalToLocalBegin(user->dm, Q, INSERT_VALUES, Q_loc)); 203878eb0e7SJames Wright PetscCall(DMGlobalToLocalBegin(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc)); 204878eb0e7SJames Wright PetscCall(DMGlobalToLocalEnd(user->dm, Q, INSERT_VALUES, Q_loc)); 205878eb0e7SJames Wright PetscCall(DMGlobalToLocalEnd(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc)); 20677841947SLeila Ghaffari 20777841947SLeila Ghaffari // Place PETSc vectors in CEED vectors 2082b730f8bSJeremy L Thompson PetscCall(VecGetArrayReadAndMemType(Q_loc, &q, &q_mem_type)); 2092b730f8bSJeremy L Thompson PetscCall(VecGetArrayReadAndMemType(Q_dot_loc, &q_dot, &q_dot_mem_type)); 2102b730f8bSJeremy L Thompson PetscCall(VecGetArrayAndMemType(G_loc, &g, &g_mem_type)); 2112b730f8bSJeremy L Thompson CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, (PetscScalar *)q); 2122b730f8bSJeremy L Thompson CeedVectorSetArray(user->q_dot_ceed, MemTypeP2C(q_dot_mem_type), CEED_USE_POINTER, (PetscScalar *)q_dot); 21377841947SLeila Ghaffari CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g); 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 21977841947SLeila Ghaffari CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL); 22077841947SLeila Ghaffari CeedVectorTakeArray(user->q_dot_ceed, MemTypeP2C(q_dot_mem_type), NULL); 22177841947SLeila Ghaffari CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL); 2222b730f8bSJeremy L Thompson PetscCall(VecRestoreArrayReadAndMemType(Q_loc, &q)); 2232b730f8bSJeremy L Thompson PetscCall(VecRestoreArrayReadAndMemType(Q_dot_loc, &q_dot)); 2242b730f8bSJeremy L Thompson PetscCall(VecRestoreArrayAndMemType(G_loc, &g)); 22577841947SLeila Ghaffari 22677841947SLeila Ghaffari // Local-to-Global 2272b730f8bSJeremy L Thompson PetscCall(VecZeroEntries(G)); 2282b730f8bSJeremy L Thompson PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G)); 22977841947SLeila Ghaffari 23077841947SLeila Ghaffari // Restore vectors 231*ca69d878SAdeleke O. Bankole PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 23277841947SLeila Ghaffari 23377841947SLeila Ghaffari PetscFunctionReturn(0); 23477841947SLeila Ghaffari } 23577841947SLeila Ghaffari 236e334ad8fSJed Brown static PetscErrorCode MatMult_NS_IJacobian(Mat J, Vec Q, Vec G) { 237e334ad8fSJed Brown User user; 238e334ad8fSJed Brown const PetscScalar *q; 239e334ad8fSJed Brown PetscScalar *g; 240e334ad8fSJed Brown PetscMemType q_mem_type, g_mem_type; 241e334ad8fSJed Brown PetscFunctionBeginUser; 2422b730f8bSJeremy L Thompson PetscCall(MatShellGetContext(J, &user)); 2435e82a6e1SJeremy L Thompson Vec Q_loc = user->Q_dot_loc, // Note - Q_dot_loc has zero BCs 2445e82a6e1SJeremy L Thompson G_loc; 2455e82a6e1SJeremy L Thompson 246e334ad8fSJed Brown // Get local vectors 2472b730f8bSJeremy L Thompson PetscCall(DMGetLocalVector(user->dm, &G_loc)); 248e334ad8fSJed Brown 249e334ad8fSJed Brown // Global-to-local 2502b730f8bSJeremy L Thompson PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc)); 251e334ad8fSJed Brown 252e334ad8fSJed Brown // Place PETSc vectors in CEED vectors 2532b730f8bSJeremy L Thompson PetscCall(VecGetArrayReadAndMemType(Q_loc, &q, &q_mem_type)); 2542b730f8bSJeremy L Thompson PetscCall(VecGetArrayAndMemType(G_loc, &g, &g_mem_type)); 2552b730f8bSJeremy L Thompson CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, (PetscScalar *)q); 256e334ad8fSJed Brown CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g); 257e334ad8fSJed Brown 258e334ad8fSJed Brown // Apply CEED operator 2592b730f8bSJeremy L Thompson CeedOperatorApply(user->op_ijacobian, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE); 260e334ad8fSJed Brown 261e334ad8fSJed Brown // Restore vectors 262e334ad8fSJed Brown CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL); 263e334ad8fSJed Brown CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL); 2642b730f8bSJeremy L Thompson PetscCall(VecRestoreArrayReadAndMemType(Q_loc, &q)); 2652b730f8bSJeremy L Thompson PetscCall(VecRestoreArrayAndMemType(G_loc, &g)); 266e334ad8fSJed Brown 267e334ad8fSJed Brown // Local-to-Global 2682b730f8bSJeremy L Thompson PetscCall(VecZeroEntries(G)); 2692b730f8bSJeremy L Thompson PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G)); 270e334ad8fSJed Brown 271e334ad8fSJed Brown // Restore vectors 2722b730f8bSJeremy L Thompson PetscCall(DMRestoreLocalVector(user->dm, &G_loc)); 273e334ad8fSJed Brown PetscFunctionReturn(0); 274e334ad8fSJed Brown } 275e334ad8fSJed Brown 276e334ad8fSJed Brown PetscErrorCode MatGetDiagonal_NS_IJacobian(Mat A, Vec D) { 277e334ad8fSJed Brown User user; 278e334ad8fSJed Brown Vec D_loc; 279e334ad8fSJed Brown PetscScalar *d; 280e334ad8fSJed Brown PetscMemType mem_type; 281e334ad8fSJed Brown 282e334ad8fSJed Brown PetscFunctionBeginUser; 283e334ad8fSJed Brown MatShellGetContext(A, &user); 284e334ad8fSJed Brown PetscCall(DMGetLocalVector(user->dm, &D_loc)); 285e334ad8fSJed Brown PetscCall(VecGetArrayAndMemType(D_loc, &d, &mem_type)); 286e334ad8fSJed Brown CeedVectorSetArray(user->g_ceed, MemTypeP2C(mem_type), CEED_USE_POINTER, d); 2872b730f8bSJeremy L Thompson CeedOperatorLinearAssembleDiagonal(user->op_ijacobian, user->g_ceed, CEED_REQUEST_IMMEDIATE); 288e334ad8fSJed Brown CeedVectorTakeArray(user->g_ceed, MemTypeP2C(mem_type), NULL); 289e334ad8fSJed Brown PetscCall(VecRestoreArrayAndMemType(D_loc, &d)); 290e334ad8fSJed Brown PetscCall(VecZeroEntries(D)); 291e334ad8fSJed Brown PetscCall(DMLocalToGlobal(user->dm, D_loc, ADD_VALUES, D)); 292e334ad8fSJed Brown PetscCall(DMRestoreLocalVector(user->dm, &D_loc)); 293e334ad8fSJed Brown VecViewFromOptions(D, NULL, "-diag_vec_view"); 294e334ad8fSJed Brown PetscFunctionReturn(0); 295e334ad8fSJed Brown } 296e334ad8fSJed Brown 2972b730f8bSJeremy L Thompson static PetscErrorCode FormPreallocation(User user, PetscBool pbdiagonal, Mat J, CeedVector *coo_values) { 298544be873SJed Brown PetscCount ncoo; 299544be873SJed Brown PetscInt *rows, *cols; 300544be873SJed Brown 301544be873SJed Brown PetscFunctionBeginUser; 302544be873SJed Brown if (pbdiagonal) { 303544be873SJed Brown CeedSize l_size; 304544be873SJed Brown CeedOperatorGetActiveVectorLengths(user->op_ijacobian, &l_size, NULL); 305544be873SJed Brown ncoo = l_size * 5; 306544be873SJed Brown rows = malloc(ncoo * sizeof(rows[0])); 307544be873SJed Brown cols = malloc(ncoo * sizeof(cols[0])); 308544be873SJed Brown for (PetscCount n = 0; n < l_size / 5; n++) { 309544be873SJed Brown for (PetscInt i = 0; i < 5; i++) { 310544be873SJed Brown for (PetscInt j = 0; j < 5; j++) { 311544be873SJed Brown rows[(n * 5 + i) * 5 + j] = n * 5 + i; 312544be873SJed Brown cols[(n * 5 + i) * 5 + j] = n * 5 + j; 313544be873SJed Brown } 314544be873SJed Brown } 315544be873SJed Brown } 316544be873SJed Brown } else { 3172b730f8bSJeremy L Thompson PetscCall(CeedOperatorLinearAssembleSymbolic(user->op_ijacobian, &ncoo, &rows, &cols)); 318544be873SJed Brown } 319544be873SJed Brown PetscCall(MatSetPreallocationCOOLocal(J, ncoo, rows, cols)); 320544be873SJed Brown free(rows); 321544be873SJed Brown free(cols); 322544be873SJed Brown CeedVectorCreate(user->ceed, ncoo, coo_values); 323544be873SJed Brown PetscFunctionReturn(0); 324544be873SJed Brown } 325544be873SJed Brown 3262b730f8bSJeremy L Thompson static PetscErrorCode FormSetValues(User user, PetscBool pbdiagonal, Mat J, CeedVector coo_values) { 327544be873SJed Brown CeedMemType mem_type = CEED_MEM_HOST; 328544be873SJed Brown const PetscScalar *values; 329544be873SJed Brown MatType mat_type; 330544be873SJed Brown 331544be873SJed Brown PetscFunctionBeginUser; 332544be873SJed Brown PetscCall(MatGetType(J, &mat_type)); 3332b730f8bSJeremy L Thompson if (strstr(mat_type, "kokkos") || strstr(mat_type, "cusparse")) mem_type = CEED_MEM_DEVICE; 334544be873SJed Brown if (user->app_ctx->pmat_pbdiagonal) { 3352b730f8bSJeremy L Thompson CeedOperatorLinearAssemblePointBlockDiagonal(user->op_ijacobian, coo_values, CEED_REQUEST_IMMEDIATE); 336544be873SJed Brown } else { 337544be873SJed Brown CeedOperatorLinearAssemble(user->op_ijacobian, coo_values); 338544be873SJed Brown } 339544be873SJed Brown CeedVectorGetArrayRead(coo_values, mem_type, &values); 340544be873SJed Brown PetscCall(MatSetValuesCOO(J, values, INSERT_VALUES)); 341544be873SJed Brown CeedVectorRestoreArrayRead(coo_values, &values); 342544be873SJed Brown PetscFunctionReturn(0); 343544be873SJed Brown } 344544be873SJed Brown 3452b730f8bSJeremy L Thompson PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, PetscReal shift, Mat J, Mat J_pre, void *user_data) { 346e334ad8fSJed Brown User user = *(User *)user_data; 347d6bf345cSJed Brown PetscBool J_is_shell, J_is_mffd, J_pre_is_shell; 348e334ad8fSJed Brown PetscFunctionBeginUser; 3492b730f8bSJeremy L Thompson if (user->phys->ijacobian_time_shift_label) CeedOperatorContextSetDouble(user->op_ijacobian, user->phys->ijacobian_time_shift_label, &shift); 350d6bf345cSJed Brown PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd)); 351e334ad8fSJed Brown PetscCall(PetscObjectTypeCompare((PetscObject)J, MATSHELL, &J_is_shell)); 3522b730f8bSJeremy L Thompson PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATSHELL, &J_pre_is_shell)); 353e334ad8fSJed Brown if (!user->matrices_set_up) { 354e334ad8fSJed Brown if (J_is_shell) { 355e334ad8fSJed Brown PetscCall(MatShellSetContext(J, user)); 3562b730f8bSJeremy L Thompson PetscCall(MatShellSetOperation(J, MATOP_MULT, (void (*)(void))MatMult_NS_IJacobian)); 3572b730f8bSJeremy L Thompson PetscCall(MatShellSetOperation(J, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiagonal_NS_IJacobian)); 358e334ad8fSJed Brown PetscCall(MatSetUp(J)); 359e334ad8fSJed Brown } 360e334ad8fSJed Brown if (!J_pre_is_shell) { 3612b730f8bSJeremy L Thompson PetscCall(FormPreallocation(user, user->app_ctx->pmat_pbdiagonal, J_pre, &user->coo_values_pmat)); 362544be873SJed Brown } 363d6bf345cSJed Brown if (J != J_pre && !J_is_shell && !J_is_mffd) { 364544be873SJed Brown PetscCall(FormPreallocation(user, PETSC_FALSE, J, &user->coo_values_amat)); 365544be873SJed Brown } 366e334ad8fSJed Brown user->matrices_set_up = true; 367e334ad8fSJed Brown } 368e334ad8fSJed Brown if (!J_pre_is_shell) { 3692b730f8bSJeremy L Thompson PetscCall(FormSetValues(user, user->app_ctx->pmat_pbdiagonal, J_pre, user->coo_values_pmat)); 370e334ad8fSJed Brown } 371d6bf345cSJed Brown if (user->coo_values_amat) { 372d6bf345cSJed Brown PetscCall(FormSetValues(user, PETSC_FALSE, J, user->coo_values_amat)); 373d6bf345cSJed Brown } else if (J_is_mffd) { 374d6bf345cSJed Brown PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 375d6bf345cSJed Brown PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 376d6bf345cSJed Brown } 377e334ad8fSJed Brown PetscFunctionReturn(0); 378e334ad8fSJed Brown } 379e334ad8fSJed Brown 3802b730f8bSJeremy L Thompson PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, PetscScalar time) { 38177841947SLeila Ghaffari Vec Q_loc; 38277841947SLeila Ghaffari char file_path[PETSC_MAX_PATH_LEN]; 38377841947SLeila Ghaffari PetscViewer viewer; 38477841947SLeila Ghaffari PetscFunctionBeginUser; 38577841947SLeila Ghaffari 38637cbb16aSJed Brown if (user->app_ctx->checkpoint_vtk) { 38777841947SLeila Ghaffari // Set up output 388f14660a4SJames Wright PetscCall(DMGetLocalVector(user->dm, &Q_loc)); 389f14660a4SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec")); 390f14660a4SJames Wright PetscCall(VecZeroEntries(Q_loc)); 391f14660a4SJames Wright PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc)); 39277841947SLeila Ghaffari 39377841947SLeila Ghaffari // Output 39437cbb16aSJed Brown PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 395f14660a4SJames Wright 3962b730f8bSJeremy L Thompson PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer)); 397f14660a4SJames Wright PetscCall(VecView(Q_loc, viewer)); 398f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 39977841947SLeila Ghaffari if (user->dm_viz) { 40077841947SLeila Ghaffari Vec Q_refined, Q_refined_loc; 40177841947SLeila Ghaffari char file_path_refined[PETSC_MAX_PATH_LEN]; 40277841947SLeila Ghaffari PetscViewer viewer_refined; 40377841947SLeila Ghaffari 404f14660a4SJames Wright PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined)); 405f14660a4SJames Wright PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc)); 406f14660a4SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined")); 407f14660a4SJames Wright 408f14660a4SJames Wright PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined)); 409f14660a4SJames Wright PetscCall(VecZeroEntries(Q_refined_loc)); 4102b730f8bSJeremy L Thompson PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc)); 411f14660a4SJames Wright 41237cbb16aSJed Brown PetscCall( 41337cbb16aSJed Brown PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 414f14660a4SJames Wright 4152b730f8bSJeremy L Thompson PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined)); 416f14660a4SJames Wright PetscCall(VecView(Q_refined_loc, viewer_refined)); 417f14660a4SJames Wright PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc)); 418f14660a4SJames Wright PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined)); 419f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer_refined)); 42077841947SLeila Ghaffari } 421f14660a4SJames Wright PetscCall(DMRestoreLocalVector(user->dm, &Q_loc)); 42237cbb16aSJed Brown } 42377841947SLeila Ghaffari 42477841947SLeila Ghaffari // Save data in a binary file for continuation of simulations 42569293791SJames Wright if (user->app_ctx->add_stepnum2bin) { 42637cbb16aSJed Brown PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", user->app_ctx->output_dir, step_no)); 42769293791SJames Wright } else { 4282b730f8bSJeremy L Thompson PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", user->app_ctx->output_dir)); 42969293791SJames Wright } 4302b730f8bSJeremy L Thompson PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, &viewer)); 431f14660a4SJames Wright 4324de8550aSJed Brown PetscInt token = FLUIDS_FILE_TOKEN; 4334de8550aSJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &token, 1, PETSC_INT)); 4344de8550aSJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &step_no, 1, PETSC_INT)); 4354de8550aSJed Brown time /= user->units->second; // Dimensionalize time back 4364de8550aSJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL)); 437f14660a4SJames Wright PetscCall(VecView(Q, viewer)); 438f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 439f14660a4SJames Wright PetscFunctionReturn(0); 440f14660a4SJames Wright } 441f14660a4SJames Wright 442*ca69d878SAdeleke O. Bankole // CSV Monitor 443*ca69d878SAdeleke O. Bankole PetscErrorCode TSMonitor_WallForce(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 444*ca69d878SAdeleke O. Bankole User user = ctx; 445*ca69d878SAdeleke O. Bankole Vec G_loc; 446*ca69d878SAdeleke O. Bankole PetscInt num_wall = user->app_ctx->wall_forces.num_wall, dim = 3; 447*ca69d878SAdeleke O. Bankole const PetscInt *walls = user->app_ctx->wall_forces.walls; 448*ca69d878SAdeleke O. Bankole PetscViewer viewer = user->app_ctx->wall_forces.viewer; 449*ca69d878SAdeleke O. Bankole PetscViewerFormat format = user->app_ctx->wall_forces.viewer_format; 450*ca69d878SAdeleke O. Bankole PetscScalar *reaction_force; 451*ca69d878SAdeleke O. Bankole PetscBool iascii; 452*ca69d878SAdeleke O. Bankole 453*ca69d878SAdeleke O. Bankole PetscFunctionBeginUser; 454*ca69d878SAdeleke O. Bankole if (!viewer) PetscFunctionReturn(0); 455*ca69d878SAdeleke O. Bankole PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 456*ca69d878SAdeleke O. Bankole PetscCall(PetscMalloc1(num_wall * dim, &reaction_force)); 457*ca69d878SAdeleke O. Bankole PetscCall(Surface_Forces_NS(user->dm, G_loc, num_wall, walls, reaction_force)); 458*ca69d878SAdeleke O. Bankole PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 459*ca69d878SAdeleke O. Bankole 460*ca69d878SAdeleke O. Bankole PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 461*ca69d878SAdeleke O. Bankole 462*ca69d878SAdeleke O. Bankole if (iascii) { 463*ca69d878SAdeleke O. Bankole if (format == PETSC_VIEWER_ASCII_CSV && !user->app_ctx->wall_forces.header_written) { 464*ca69d878SAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "Step,Time,Wall,ForceX,ForceY,ForceZ\n")); 465*ca69d878SAdeleke O. Bankole user->app_ctx->wall_forces.header_written = PETSC_TRUE; 466*ca69d878SAdeleke O. Bankole } 467*ca69d878SAdeleke O. Bankole for (PetscInt w = 0; w < num_wall; w++) { 468*ca69d878SAdeleke O. Bankole PetscInt wall = walls[w]; 469*ca69d878SAdeleke O. Bankole if (format == PETSC_VIEWER_ASCII_CSV) { 470*ca69d878SAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ",%g,%" PetscInt_FMT ",%g,%g,%g\n", step_no, time, wall, 471*ca69d878SAdeleke O. Bankole reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2])); 472*ca69d878SAdeleke O. Bankole 473*ca69d878SAdeleke O. Bankole } else { 474*ca69d878SAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "Wall %" PetscInt_FMT " Forces: Force_x = %12g, Force_y = %12g, Force_z = %12g\n", wall, 475*ca69d878SAdeleke O. Bankole reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2])); 476*ca69d878SAdeleke O. Bankole } 477*ca69d878SAdeleke O. Bankole } 478*ca69d878SAdeleke O. Bankole } 479*ca69d878SAdeleke O. Bankole PetscCall(PetscFree(reaction_force)); 480*ca69d878SAdeleke O. Bankole PetscFunctionReturn(0); 481*ca69d878SAdeleke O. Bankole } 482*ca69d878SAdeleke O. Bankole 483f14660a4SJames Wright // User provided TS Monitor 4842b730f8bSJeremy L Thompson PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 485f14660a4SJames Wright User user = ctx; 486f14660a4SJames Wright PetscFunctionBeginUser; 487f14660a4SJames Wright 48837cbb16aSJed Brown // Print every 'checkpoint_interval' steps 489894de27cSJames Wright if (user->app_ctx->checkpoint_interval <= 0 || step_no % user->app_ctx->checkpoint_interval != 0 || 490894de27cSJames Wright (user->app_ctx->cont_steps == step_no && step_no != 0)) 491894de27cSJames Wright PetscFunctionReturn(0); 492f14660a4SJames Wright 493f14660a4SJames Wright PetscCall(WriteOutput(user, Q, step_no, time)); 49477841947SLeila Ghaffari 49577841947SLeila Ghaffari PetscFunctionReturn(0); 49677841947SLeila Ghaffari } 49777841947SLeila Ghaffari 49877841947SLeila Ghaffari // TS: Create, setup, and solve 4992b730f8bSJeremy L Thompson PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, Vec *Q, PetscScalar *f_time, TS *ts) { 50077841947SLeila Ghaffari MPI_Comm comm = user->comm; 50177841947SLeila Ghaffari TSAdapt adapt; 50277841947SLeila Ghaffari PetscScalar final_time; 50377841947SLeila Ghaffari PetscFunctionBeginUser; 50477841947SLeila Ghaffari 5052b730f8bSJeremy L Thompson PetscCall(TSCreate(comm, ts)); 5062b730f8bSJeremy L Thompson PetscCall(TSSetDM(*ts, dm)); 50777841947SLeila Ghaffari if (phys->implicit) { 5082b730f8bSJeremy L Thompson PetscCall(TSSetType(*ts, TSBDF)); 50977841947SLeila Ghaffari if (user->op_ifunction) { 5102b730f8bSJeremy L Thompson PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &user)); 51177841947SLeila Ghaffari } else { // Implicit integrators can fall back to using an RHSFunction 5122b730f8bSJeremy L Thompson PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 51377841947SLeila Ghaffari } 514e334ad8fSJed Brown if (user->op_ijacobian) { 5152b730f8bSJeremy L Thompson PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &user)); 516544be873SJed Brown if (app_ctx->amat_type) { 517544be873SJed Brown Mat Pmat, Amat; 5182b730f8bSJeremy L Thompson PetscCall(DMCreateMatrix(dm, &Pmat)); 5192b730f8bSJeremy L Thompson PetscCall(DMSetMatType(dm, app_ctx->amat_type)); 5202b730f8bSJeremy L Thompson PetscCall(DMCreateMatrix(dm, &Amat)); 5212b730f8bSJeremy L Thompson PetscCall(TSSetIJacobian(*ts, Amat, Pmat, NULL, NULL)); 5222b730f8bSJeremy L Thompson PetscCall(MatDestroy(&Amat)); 5232b730f8bSJeremy L Thompson PetscCall(MatDestroy(&Pmat)); 524544be873SJed Brown } 525e334ad8fSJed Brown } 52677841947SLeila Ghaffari } else { 5272b730f8bSJeremy L Thompson if (!user->op_rhs) SETERRQ(comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction"); 5282b730f8bSJeremy L Thompson PetscCall(TSSetType(*ts, TSRK)); 5292b730f8bSJeremy L Thompson PetscCall(TSRKSetType(*ts, TSRK5F)); 5302b730f8bSJeremy L Thompson PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 53177841947SLeila Ghaffari } 5322b730f8bSJeremy L Thompson PetscCall(TSSetMaxTime(*ts, 500. * user->units->second)); 5332b730f8bSJeremy L Thompson PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER)); 534cf7a0454SJed Brown PetscCall(TSSetErrorIfStepFails(*ts, PETSC_FALSE)); 5352b730f8bSJeremy L Thompson PetscCall(TSSetTimeStep(*ts, 1.e-2 * user->units->second)); 5368fb33541SJames Wright if (app_ctx->test_type != TESTTYPE_NONE) { 5372b730f8bSJeremy L Thompson PetscCall(TSSetMaxSteps(*ts, 10)); 5382b730f8bSJeremy L Thompson } 5392b730f8bSJeremy L Thompson PetscCall(TSGetAdapt(*ts, &adapt)); 5402b730f8bSJeremy L Thompson PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * user->units->second, 1.e2 * user->units->second)); 5412b730f8bSJeremy L Thompson PetscCall(TSSetFromOptions(*ts)); 542a0b9a424SJames Wright user->time = user->time_bc_set = -1.0; // require all BCs and ctx to be updated 5435e82a6e1SJeremy L Thompson user->dt = -1.0; 54477841947SLeila Ghaffari if (!app_ctx->cont_steps) { // print initial condition 5458fb33541SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 5462b730f8bSJeremy L Thompson PetscCall(TSMonitor_NS(*ts, 0, 0., *Q, user)); 54777841947SLeila Ghaffari } 54877841947SLeila Ghaffari } else { // continue from time of last output 54977841947SLeila Ghaffari PetscInt count; 55077841947SLeila Ghaffari PetscViewer viewer; 5512b730f8bSJeremy L Thompson 5524de8550aSJed Brown if (app_ctx->cont_time <= 0) { // Legacy files did not include step number and time 5532b730f8bSJeremy L Thompson PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer)); 5544de8550aSJed Brown PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL)); 5552b730f8bSJeremy L Thompson PetscCall(PetscViewerDestroy(&viewer)); 5564de8550aSJed Brown PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP, 5574de8550aSJed Brown "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)"); 5584de8550aSJed Brown } 5594de8550aSJed Brown PetscCall(TSSetTime(*ts, app_ctx->cont_time * user->units->second)); 560d8e0aecdSJed Brown PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps)); 56177841947SLeila Ghaffari } 5628fb33541SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 5632b730f8bSJeremy L Thompson PetscCall(TSMonitorSet(*ts, TSMonitor_NS, user, NULL)); 5648fb33541SJames Wright } 565*ca69d878SAdeleke O. Bankole if (app_ctx->wall_forces.viewer) { 566*ca69d878SAdeleke O. Bankole PetscCall(TSMonitorSet(*ts, TSMonitor_WallForce, user, NULL)); 567*ca69d878SAdeleke O. Bankole } 568b7d66439SJames Wright if (app_ctx->turb_spanstats_enable) { 569a175e481SJames Wright PetscCall(TSMonitorSet(*ts, TSMonitor_Statistics, user, NULL)); 570495b9769SJames Wright CeedScalar previous_time = app_ctx->cont_time * user->units->second; 571495b9769SJames Wright CeedOperatorContextSetDouble(user->spanstats.op_stats_collect, user->spanstats.previous_time_label, &previous_time); 572a175e481SJames Wright } 57377841947SLeila Ghaffari 57477841947SLeila Ghaffari // Solve 575d8e0aecdSJed Brown PetscReal start_time; 576d8e0aecdSJed Brown PetscInt start_step; 5772b730f8bSJeremy L Thompson PetscCall(TSGetTime(*ts, &start_time)); 578d8e0aecdSJed Brown PetscCall(TSGetStepNumber(*ts, &start_step)); 5797b39487dSJeremy L Thompson 5807b39487dSJeremy L Thompson PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve"); 5817b39487dSJeremy L Thompson PetscCall(TSSetTime(*ts, start_time)); 582d8e0aecdSJed Brown PetscCall(TSSetStepNumber(*ts, start_step)); 5837b39487dSJeremy L Thompson if (PetscPreLoadingOn) { 5847b39487dSJeremy L Thompson // LCOV_EXCL_START 5857b39487dSJeremy L Thompson SNES snes; 5867b39487dSJeremy L Thompson Vec Q_preload; 5877b39487dSJeremy L Thompson PetscReal rtol; 5887b39487dSJeremy L Thompson PetscCall(VecDuplicate(*Q, &Q_preload)); 5897b39487dSJeremy L Thompson PetscCall(VecCopy(*Q, Q_preload)); 5907b39487dSJeremy L Thompson PetscCall(TSGetSNES(*ts, &snes)); 5917b39487dSJeremy L Thompson PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL)); 5922b730f8bSJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 5937b39487dSJeremy L Thompson PetscCall(TSSetSolution(*ts, *Q)); 5947b39487dSJeremy L Thompson PetscCall(TSStep(*ts)); 5952b730f8bSJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 5967b39487dSJeremy L Thompson PetscCall(VecDestroy(&Q_preload)); 5977b39487dSJeremy L Thompson // LCOV_EXCL_STOP 5987b39487dSJeremy L Thompson } else { 5992b730f8bSJeremy L Thompson PetscCall(PetscBarrier((PetscObject)*ts)); 6002b730f8bSJeremy L Thompson PetscCall(TSSolve(*ts, *Q)); 6017b39487dSJeremy L Thompson } 6027b39487dSJeremy L Thompson PetscPreLoadEnd(); 6037b39487dSJeremy L Thompson 6047b39487dSJeremy L Thompson PetscCall(TSGetSolveTime(*ts, &final_time)); 60577841947SLeila Ghaffari *f_time = final_time; 6067b39487dSJeremy L Thompson 6078fb33541SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 608f14660a4SJames Wright PetscInt step_no; 609f14660a4SJames Wright PetscCall(TSGetStepNumber(*ts, &step_no)); 610a175e481SJames Wright if (user->app_ctx->checkpoint_interval > 0 || user->app_ctx->checkpoint_interval == -1) { 611f14660a4SJames Wright PetscCall(WriteOutput(user, *Q, step_no, final_time)); 612f14660a4SJames Wright } 613f14660a4SJames Wright 6147b39487dSJeremy L Thompson PetscLogEvent stage_id; 6157b39487dSJeremy L Thompson PetscStageLog stage_log; 6167b39487dSJeremy L Thompson 6177b39487dSJeremy L Thompson PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id)); 6187b39487dSJeremy L Thompson PetscCall(PetscLogGetStageLog(&stage_log)); 6192b730f8bSJeremy L Thompson PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_log->stageInfo[stage_id].perfInfo.time)); 62077841947SLeila Ghaffari } 62177841947SLeila Ghaffari PetscFunctionReturn(0); 62277841947SLeila Ghaffari } 623