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" 1277841947SLeila Ghaffari 1377841947SLeila Ghaffari // Compute mass matrix for explicit scheme 142b730f8bSJeremy L Thompson PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm, CeedData ceed_data, Vec M) { 1577841947SLeila Ghaffari Vec M_loc; 1677841947SLeila Ghaffari CeedQFunction qf_mass; 1777841947SLeila Ghaffari CeedOperator op_mass; 1877841947SLeila Ghaffari CeedVector m_ceed, ones_vec; 1977841947SLeila Ghaffari CeedInt num_comp_q, q_data_size; 2077841947SLeila Ghaffari PetscFunctionBeginUser; 2177841947SLeila Ghaffari 2277841947SLeila Ghaffari // CEED Restriction 2377841947SLeila Ghaffari CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_q, &num_comp_q); 2477841947SLeila Ghaffari CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size); 2577841947SLeila Ghaffari CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &m_ceed, NULL); 2677841947SLeila Ghaffari CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &ones_vec, NULL); 2777841947SLeila Ghaffari CeedVectorSetValue(ones_vec, 1.0); 2877841947SLeila Ghaffari 2977841947SLeila Ghaffari // CEED QFunction 30ef080ff9SJames Wright PetscCall(CreateMassQFunction(ceed, num_comp_q, q_data_size, &qf_mass)); 3177841947SLeila Ghaffari 3277841947SLeila Ghaffari // CEED Operator 3377841947SLeila Ghaffari CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass); 34ef080ff9SJames Wright CeedOperatorSetField(op_mass, "u", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE); 352b730f8bSJeremy L Thompson CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data); 362b730f8bSJeremy L Thompson CeedOperatorSetField(op_mass, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE); 3777841947SLeila Ghaffari 3877841947SLeila Ghaffari // Place PETSc vector in CEED vector 3977841947SLeila Ghaffari CeedScalar *m; 4077841947SLeila Ghaffari PetscMemType m_mem_type; 412b730f8bSJeremy L Thompson PetscCall(DMGetLocalVector(dm, &M_loc)); 422b730f8bSJeremy L Thompson PetscCall(VecGetArrayAndMemType(M_loc, (PetscScalar **)&m, &m_mem_type)); 4377841947SLeila Ghaffari CeedVectorSetArray(m_ceed, MemTypeP2C(m_mem_type), CEED_USE_POINTER, m); 4477841947SLeila Ghaffari 4577841947SLeila Ghaffari // Apply CEED Operator 4677841947SLeila Ghaffari CeedOperatorApply(op_mass, ones_vec, m_ceed, CEED_REQUEST_IMMEDIATE); 4777841947SLeila Ghaffari 4877841947SLeila Ghaffari // Restore vectors 4977841947SLeila Ghaffari CeedVectorTakeArray(m_ceed, MemTypeP2C(m_mem_type), NULL); 502b730f8bSJeremy L Thompson PetscCall(VecRestoreArrayReadAndMemType(M_loc, (const PetscScalar **)&m)); 5177841947SLeila Ghaffari 5277841947SLeila Ghaffari // Local-to-Global 532b730f8bSJeremy L Thompson PetscCall(VecZeroEntries(M)); 542b730f8bSJeremy L Thompson PetscCall(DMLocalToGlobal(dm, M_loc, ADD_VALUES, M)); 552b730f8bSJeremy L Thompson PetscCall(DMRestoreLocalVector(dm, &M_loc)); 5677841947SLeila Ghaffari 5777841947SLeila Ghaffari // Invert diagonally lumped mass vector for RHS function 582b730f8bSJeremy L Thompson PetscCall(VecReciprocal(M)); 5977841947SLeila Ghaffari 6077841947SLeila Ghaffari // Cleanup 6177841947SLeila Ghaffari CeedVectorDestroy(&ones_vec); 6277841947SLeila Ghaffari CeedVectorDestroy(&m_ceed); 6377841947SLeila Ghaffari CeedQFunctionDestroy(&qf_mass); 6477841947SLeila Ghaffari CeedOperatorDestroy(&op_mass); 6577841947SLeila Ghaffari 6677841947SLeila Ghaffari PetscFunctionReturn(0); 6777841947SLeila Ghaffari } 6877841947SLeila Ghaffari 69a0b9a424SJames Wright // Insert Boundary values if it's a new time 70a0b9a424SJames Wright PetscErrorCode UpdateBoundaryValues(User user, Vec Q_loc, PetscReal t) { 71a0b9a424SJames Wright PetscFunctionBeginUser; 72a0b9a424SJames Wright if (user->time_bc_set != t) { 73a0b9a424SJames Wright PetscCall(DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL)); 74a0b9a424SJames Wright user->time_bc_set = t; 75a0b9a424SJames Wright } 76a0b9a424SJames Wright PetscFunctionReturn(0); 77a0b9a424SJames Wright } 78a0b9a424SJames Wright 7977841947SLeila Ghaffari // RHS (Explicit time-stepper) function setup 8077841947SLeila Ghaffari // This is the RHS of the ODE, given as u_t = G(t,u) 8177841947SLeila Ghaffari // This function takes in a state vector Q and writes into G 8277841947SLeila Ghaffari PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data) { 8377841947SLeila Ghaffari User user = *(User *)user_data; 8477841947SLeila Ghaffari PetscScalar *q, *g; 855e82a6e1SJeremy L Thompson Vec Q_loc = user->Q_loc, G_loc; 8677841947SLeila Ghaffari PetscMemType q_mem_type, g_mem_type; 8777841947SLeila Ghaffari PetscFunctionBeginUser; 8877841947SLeila Ghaffari 895e82a6e1SJeremy L Thompson // Get local vector 902b730f8bSJeremy L Thompson PetscCall(DMGetLocalVector(user->dm, &G_loc)); 915e82a6e1SJeremy L Thompson 925e82a6e1SJeremy L Thompson // Update time dependent data 93a0b9a424SJames Wright PetscCall(UpdateBoundaryValues(user, Q_loc, t)); 945e82a6e1SJeremy L Thompson if (user->time != t) { 955e82a6e1SJeremy L Thompson if (user->phys->solution_time_label) { 9611436a05SJames Wright CeedOperatorContextSetDouble(user->op_rhs, user->phys->solution_time_label, &t); 975e82a6e1SJeremy L Thompson } 985e82a6e1SJeremy L Thompson user->time = t; 995e82a6e1SJeremy L Thompson } 10088626eedSJames Wright if (user->phys->timestep_size_label) { 10188626eedSJames Wright PetscScalar dt; 1022b730f8bSJeremy L Thompson PetscCall(TSGetTimeStep(ts, &dt)); 1035e82a6e1SJeremy L Thompson if (user->dt != dt) { 1042b730f8bSJeremy L Thompson CeedOperatorContextSetDouble(user->op_rhs, user->phys->timestep_size_label, &dt); 1055e82a6e1SJeremy L Thompson user->dt = dt; 1065e82a6e1SJeremy L Thompson } 10788626eedSJames Wright } 10877841947SLeila Ghaffari 10977841947SLeila Ghaffari // Global-to-local 1102b730f8bSJeremy L Thompson PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc)); 11177841947SLeila Ghaffari 11277841947SLeila Ghaffari // Place PETSc vectors in CEED vectors 1132b730f8bSJeremy L Thompson PetscCall(VecGetArrayReadAndMemType(Q_loc, (const PetscScalar **)&q, &q_mem_type)); 1142b730f8bSJeremy L Thompson PetscCall(VecGetArrayAndMemType(G_loc, &g, &g_mem_type)); 11577841947SLeila Ghaffari CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, q); 11677841947SLeila Ghaffari CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g); 11777841947SLeila Ghaffari 11877841947SLeila Ghaffari // Apply CEED operator 1192b730f8bSJeremy L Thompson CeedOperatorApply(user->op_rhs, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE); 12077841947SLeila Ghaffari 12177841947SLeila Ghaffari // Restore vectors 12277841947SLeila Ghaffari CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL); 12377841947SLeila Ghaffari CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL); 1242b730f8bSJeremy L Thompson PetscCall(VecRestoreArrayReadAndMemType(Q_loc, (const PetscScalar **)&q)); 1252b730f8bSJeremy L Thompson PetscCall(VecRestoreArrayAndMemType(G_loc, &g)); 12677841947SLeila Ghaffari 12777841947SLeila Ghaffari // Local-to-Global 1282b730f8bSJeremy L Thompson PetscCall(VecZeroEntries(G)); 1292b730f8bSJeremy L Thompson PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G)); 13077841947SLeila Ghaffari 13177841947SLeila Ghaffari // Inverse of the lumped mass matrix (M is Minv) 1322b730f8bSJeremy L Thompson PetscCall(VecPointwiseMult(G, G, user->M)); 13377841947SLeila Ghaffari 13477841947SLeila Ghaffari // Restore vectors 1352b730f8bSJeremy L Thompson PetscCall(DMRestoreLocalVector(user->dm, &G_loc)); 13677841947SLeila Ghaffari 13777841947SLeila Ghaffari PetscFunctionReturn(0); 13877841947SLeila Ghaffari } 13977841947SLeila Ghaffari 14077841947SLeila Ghaffari // Implicit time-stepper function setup 1412b730f8bSJeremy L Thompson PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data) { 14277841947SLeila Ghaffari User user = *(User *)user_data; 14377841947SLeila Ghaffari const PetscScalar *q, *q_dot; 14477841947SLeila Ghaffari PetscScalar *g; 1455e82a6e1SJeremy L Thompson Vec Q_loc = user->Q_loc, Q_dot_loc = user->Q_dot_loc, G_loc; 14677841947SLeila Ghaffari PetscMemType q_mem_type, q_dot_mem_type, g_mem_type; 14777841947SLeila Ghaffari PetscFunctionBeginUser; 14877841947SLeila Ghaffari 1495e82a6e1SJeremy L Thompson // Get local vectors 1502b730f8bSJeremy L Thompson PetscCall(DMGetLocalVector(user->dm, &G_loc)); 1515e82a6e1SJeremy L Thompson 1525e82a6e1SJeremy L Thompson // Update time dependent data 153a0b9a424SJames Wright PetscCall(UpdateBoundaryValues(user, Q_loc, t)); 1545e82a6e1SJeremy L Thompson if (user->time != t) { 1555e82a6e1SJeremy L Thompson if (user->phys->solution_time_label) { 1562b730f8bSJeremy L Thompson CeedOperatorContextSetDouble(user->op_ifunction, user->phys->solution_time_label, &t); 1575e82a6e1SJeremy L Thompson } 1585e82a6e1SJeremy L Thompson user->time = t; 1595e82a6e1SJeremy L Thompson } 16088626eedSJames Wright if (user->phys->timestep_size_label) { 16188626eedSJames Wright PetscScalar dt; 1622b730f8bSJeremy L Thompson PetscCall(TSGetTimeStep(ts, &dt)); 1635e82a6e1SJeremy L Thompson if (user->dt != dt) { 1642b730f8bSJeremy L Thompson CeedOperatorContextSetDouble(user->op_ifunction, user->phys->timestep_size_label, &dt); 1655e82a6e1SJeremy L Thompson user->dt = dt; 1665e82a6e1SJeremy L Thompson } 16788626eedSJames Wright } 16877841947SLeila Ghaffari 16977841947SLeila Ghaffari // Global-to-local 170878eb0e7SJames Wright PetscCall(DMGlobalToLocalBegin(user->dm, Q, INSERT_VALUES, Q_loc)); 171878eb0e7SJames Wright PetscCall(DMGlobalToLocalBegin(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc)); 172878eb0e7SJames Wright PetscCall(DMGlobalToLocalEnd(user->dm, Q, INSERT_VALUES, Q_loc)); 173878eb0e7SJames Wright PetscCall(DMGlobalToLocalEnd(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc)); 17477841947SLeila Ghaffari 17577841947SLeila Ghaffari // Place PETSc vectors in CEED vectors 1762b730f8bSJeremy L Thompson PetscCall(VecGetArrayReadAndMemType(Q_loc, &q, &q_mem_type)); 1772b730f8bSJeremy L Thompson PetscCall(VecGetArrayReadAndMemType(Q_dot_loc, &q_dot, &q_dot_mem_type)); 1782b730f8bSJeremy L Thompson PetscCall(VecGetArrayAndMemType(G_loc, &g, &g_mem_type)); 1792b730f8bSJeremy L Thompson CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, (PetscScalar *)q); 1802b730f8bSJeremy L Thompson CeedVectorSetArray(user->q_dot_ceed, MemTypeP2C(q_dot_mem_type), CEED_USE_POINTER, (PetscScalar *)q_dot); 18177841947SLeila Ghaffari CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g); 18277841947SLeila Ghaffari 18377841947SLeila Ghaffari // Apply CEED operator 1842b730f8bSJeremy L Thompson CeedOperatorApply(user->op_ifunction, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE); 18577841947SLeila Ghaffari 18677841947SLeila Ghaffari // Restore vectors 18777841947SLeila Ghaffari CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL); 18877841947SLeila Ghaffari CeedVectorTakeArray(user->q_dot_ceed, MemTypeP2C(q_dot_mem_type), NULL); 18977841947SLeila Ghaffari CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL); 1902b730f8bSJeremy L Thompson PetscCall(VecRestoreArrayReadAndMemType(Q_loc, &q)); 1912b730f8bSJeremy L Thompson PetscCall(VecRestoreArrayReadAndMemType(Q_dot_loc, &q_dot)); 1922b730f8bSJeremy L Thompson PetscCall(VecRestoreArrayAndMemType(G_loc, &g)); 19377841947SLeila Ghaffari 19477841947SLeila Ghaffari // Local-to-Global 1952b730f8bSJeremy L Thompson PetscCall(VecZeroEntries(G)); 1962b730f8bSJeremy L Thompson PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G)); 19777841947SLeila Ghaffari 19877841947SLeila Ghaffari // Restore vectors 1992b730f8bSJeremy L Thompson PetscCall(DMRestoreLocalVector(user->dm, &G_loc)); 20077841947SLeila Ghaffari 20177841947SLeila Ghaffari PetscFunctionReturn(0); 20277841947SLeila Ghaffari } 20377841947SLeila Ghaffari 204e334ad8fSJed Brown static PetscErrorCode MatMult_NS_IJacobian(Mat J, Vec Q, Vec G) { 205e334ad8fSJed Brown User user; 206e334ad8fSJed Brown const PetscScalar *q; 207e334ad8fSJed Brown PetscScalar *g; 208e334ad8fSJed Brown PetscMemType q_mem_type, g_mem_type; 209e334ad8fSJed Brown PetscFunctionBeginUser; 2102b730f8bSJeremy L Thompson PetscCall(MatShellGetContext(J, &user)); 2115e82a6e1SJeremy L Thompson Vec Q_loc = user->Q_dot_loc, // Note - Q_dot_loc has zero BCs 2125e82a6e1SJeremy L Thompson G_loc; 2135e82a6e1SJeremy L Thompson 214e334ad8fSJed Brown // Get local vectors 2152b730f8bSJeremy L Thompson PetscCall(DMGetLocalVector(user->dm, &G_loc)); 216e334ad8fSJed Brown 217e334ad8fSJed Brown // Global-to-local 2182b730f8bSJeremy L Thompson PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc)); 219e334ad8fSJed Brown 220e334ad8fSJed Brown // Place PETSc vectors in CEED vectors 2212b730f8bSJeremy L Thompson PetscCall(VecGetArrayReadAndMemType(Q_loc, &q, &q_mem_type)); 2222b730f8bSJeremy L Thompson PetscCall(VecGetArrayAndMemType(G_loc, &g, &g_mem_type)); 2232b730f8bSJeremy L Thompson CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, (PetscScalar *)q); 224e334ad8fSJed Brown CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g); 225e334ad8fSJed Brown 226e334ad8fSJed Brown // Apply CEED operator 2272b730f8bSJeremy L Thompson CeedOperatorApply(user->op_ijacobian, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE); 228e334ad8fSJed Brown 229e334ad8fSJed Brown // Restore vectors 230e334ad8fSJed Brown CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL); 231e334ad8fSJed Brown CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL); 2322b730f8bSJeremy L Thompson PetscCall(VecRestoreArrayReadAndMemType(Q_loc, &q)); 2332b730f8bSJeremy L Thompson PetscCall(VecRestoreArrayAndMemType(G_loc, &g)); 234e334ad8fSJed Brown 235e334ad8fSJed Brown // Local-to-Global 2362b730f8bSJeremy L Thompson PetscCall(VecZeroEntries(G)); 2372b730f8bSJeremy L Thompson PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G)); 238e334ad8fSJed Brown 239e334ad8fSJed Brown // Restore vectors 2402b730f8bSJeremy L Thompson PetscCall(DMRestoreLocalVector(user->dm, &G_loc)); 241e334ad8fSJed Brown PetscFunctionReturn(0); 242e334ad8fSJed Brown } 243e334ad8fSJed Brown 244e334ad8fSJed Brown PetscErrorCode MatGetDiagonal_NS_IJacobian(Mat A, Vec D) { 245e334ad8fSJed Brown User user; 246e334ad8fSJed Brown Vec D_loc; 247e334ad8fSJed Brown PetscScalar *d; 248e334ad8fSJed Brown PetscMemType mem_type; 249e334ad8fSJed Brown 250e334ad8fSJed Brown PetscFunctionBeginUser; 251e334ad8fSJed Brown MatShellGetContext(A, &user); 252e334ad8fSJed Brown PetscCall(DMGetLocalVector(user->dm, &D_loc)); 253e334ad8fSJed Brown PetscCall(VecGetArrayAndMemType(D_loc, &d, &mem_type)); 254e334ad8fSJed Brown CeedVectorSetArray(user->g_ceed, MemTypeP2C(mem_type), CEED_USE_POINTER, d); 2552b730f8bSJeremy L Thompson CeedOperatorLinearAssembleDiagonal(user->op_ijacobian, user->g_ceed, CEED_REQUEST_IMMEDIATE); 256e334ad8fSJed Brown CeedVectorTakeArray(user->g_ceed, MemTypeP2C(mem_type), NULL); 257e334ad8fSJed Brown PetscCall(VecRestoreArrayAndMemType(D_loc, &d)); 258e334ad8fSJed Brown PetscCall(VecZeroEntries(D)); 259e334ad8fSJed Brown PetscCall(DMLocalToGlobal(user->dm, D_loc, ADD_VALUES, D)); 260e334ad8fSJed Brown PetscCall(DMRestoreLocalVector(user->dm, &D_loc)); 261e334ad8fSJed Brown VecViewFromOptions(D, NULL, "-diag_vec_view"); 262e334ad8fSJed Brown PetscFunctionReturn(0); 263e334ad8fSJed Brown } 264e334ad8fSJed Brown 2652b730f8bSJeremy L Thompson static PetscErrorCode FormPreallocation(User user, PetscBool pbdiagonal, Mat J, CeedVector *coo_values) { 266544be873SJed Brown PetscCount ncoo; 267544be873SJed Brown PetscInt *rows, *cols; 268544be873SJed Brown 269544be873SJed Brown PetscFunctionBeginUser; 270544be873SJed Brown if (pbdiagonal) { 271544be873SJed Brown CeedSize l_size; 272544be873SJed Brown CeedOperatorGetActiveVectorLengths(user->op_ijacobian, &l_size, NULL); 273544be873SJed Brown ncoo = l_size * 5; 274544be873SJed Brown rows = malloc(ncoo * sizeof(rows[0])); 275544be873SJed Brown cols = malloc(ncoo * sizeof(cols[0])); 276544be873SJed Brown for (PetscCount n = 0; n < l_size / 5; n++) { 277544be873SJed Brown for (PetscInt i = 0; i < 5; i++) { 278544be873SJed Brown for (PetscInt j = 0; j < 5; j++) { 279544be873SJed Brown rows[(n * 5 + i) * 5 + j] = n * 5 + i; 280544be873SJed Brown cols[(n * 5 + i) * 5 + j] = n * 5 + j; 281544be873SJed Brown } 282544be873SJed Brown } 283544be873SJed Brown } 284544be873SJed Brown } else { 2852b730f8bSJeremy L Thompson PetscCall(CeedOperatorLinearAssembleSymbolic(user->op_ijacobian, &ncoo, &rows, &cols)); 286544be873SJed Brown } 287544be873SJed Brown PetscCall(MatSetPreallocationCOOLocal(J, ncoo, rows, cols)); 288544be873SJed Brown free(rows); 289544be873SJed Brown free(cols); 290544be873SJed Brown CeedVectorCreate(user->ceed, ncoo, coo_values); 291544be873SJed Brown PetscFunctionReturn(0); 292544be873SJed Brown } 293544be873SJed Brown 2942b730f8bSJeremy L Thompson static PetscErrorCode FormSetValues(User user, PetscBool pbdiagonal, Mat J, CeedVector coo_values) { 295544be873SJed Brown CeedMemType mem_type = CEED_MEM_HOST; 296544be873SJed Brown const PetscScalar *values; 297544be873SJed Brown MatType mat_type; 298544be873SJed Brown 299544be873SJed Brown PetscFunctionBeginUser; 300544be873SJed Brown PetscCall(MatGetType(J, &mat_type)); 3012b730f8bSJeremy L Thompson if (strstr(mat_type, "kokkos") || strstr(mat_type, "cusparse")) mem_type = CEED_MEM_DEVICE; 302544be873SJed Brown if (user->app_ctx->pmat_pbdiagonal) { 3032b730f8bSJeremy L Thompson CeedOperatorLinearAssemblePointBlockDiagonal(user->op_ijacobian, coo_values, CEED_REQUEST_IMMEDIATE); 304544be873SJed Brown } else { 305544be873SJed Brown CeedOperatorLinearAssemble(user->op_ijacobian, coo_values); 306544be873SJed Brown } 307544be873SJed Brown CeedVectorGetArrayRead(coo_values, mem_type, &values); 308544be873SJed Brown PetscCall(MatSetValuesCOO(J, values, INSERT_VALUES)); 309544be873SJed Brown CeedVectorRestoreArrayRead(coo_values, &values); 310544be873SJed Brown PetscFunctionReturn(0); 311544be873SJed Brown } 312544be873SJed Brown 3132b730f8bSJeremy L Thompson PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, PetscReal shift, Mat J, Mat J_pre, void *user_data) { 314e334ad8fSJed Brown User user = *(User *)user_data; 315d6bf345cSJed Brown PetscBool J_is_shell, J_is_mffd, J_pre_is_shell; 316e334ad8fSJed Brown PetscFunctionBeginUser; 3172b730f8bSJeremy L Thompson if (user->phys->ijacobian_time_shift_label) CeedOperatorContextSetDouble(user->op_ijacobian, user->phys->ijacobian_time_shift_label, &shift); 318d6bf345cSJed Brown PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd)); 319e334ad8fSJed Brown PetscCall(PetscObjectTypeCompare((PetscObject)J, MATSHELL, &J_is_shell)); 3202b730f8bSJeremy L Thompson PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATSHELL, &J_pre_is_shell)); 321e334ad8fSJed Brown if (!user->matrices_set_up) { 322e334ad8fSJed Brown if (J_is_shell) { 323e334ad8fSJed Brown PetscCall(MatShellSetContext(J, user)); 3242b730f8bSJeremy L Thompson PetscCall(MatShellSetOperation(J, MATOP_MULT, (void (*)(void))MatMult_NS_IJacobian)); 3252b730f8bSJeremy L Thompson PetscCall(MatShellSetOperation(J, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiagonal_NS_IJacobian)); 326e334ad8fSJed Brown PetscCall(MatSetUp(J)); 327e334ad8fSJed Brown } 328e334ad8fSJed Brown if (!J_pre_is_shell) { 3292b730f8bSJeremy L Thompson PetscCall(FormPreallocation(user, user->app_ctx->pmat_pbdiagonal, J_pre, &user->coo_values_pmat)); 330544be873SJed Brown } 331d6bf345cSJed Brown if (J != J_pre && !J_is_shell && !J_is_mffd) { 332544be873SJed Brown PetscCall(FormPreallocation(user, PETSC_FALSE, J, &user->coo_values_amat)); 333544be873SJed Brown } 334e334ad8fSJed Brown user->matrices_set_up = true; 335e334ad8fSJed Brown } 336e334ad8fSJed Brown if (!J_pre_is_shell) { 3372b730f8bSJeremy L Thompson PetscCall(FormSetValues(user, user->app_ctx->pmat_pbdiagonal, J_pre, user->coo_values_pmat)); 338e334ad8fSJed Brown } 339d6bf345cSJed Brown if (user->coo_values_amat) { 340d6bf345cSJed Brown PetscCall(FormSetValues(user, PETSC_FALSE, J, user->coo_values_amat)); 341d6bf345cSJed Brown } else if (J_is_mffd) { 342d6bf345cSJed Brown PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 343d6bf345cSJed Brown PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 344d6bf345cSJed Brown } 345e334ad8fSJed Brown PetscFunctionReturn(0); 346e334ad8fSJed Brown } 347e334ad8fSJed Brown 3482b730f8bSJeremy L Thompson PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, PetscScalar time) { 34977841947SLeila Ghaffari Vec Q_loc; 35077841947SLeila Ghaffari char file_path[PETSC_MAX_PATH_LEN]; 35177841947SLeila Ghaffari PetscViewer viewer; 35277841947SLeila Ghaffari PetscFunctionBeginUser; 35377841947SLeila Ghaffari 35437cbb16aSJed Brown if (user->app_ctx->checkpoint_vtk) { 35577841947SLeila Ghaffari // Set up output 356f14660a4SJames Wright PetscCall(DMGetLocalVector(user->dm, &Q_loc)); 357f14660a4SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec")); 358f14660a4SJames Wright PetscCall(VecZeroEntries(Q_loc)); 359f14660a4SJames Wright PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc)); 36077841947SLeila Ghaffari 36177841947SLeila Ghaffari // Output 36237cbb16aSJed Brown PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 363f14660a4SJames Wright 3642b730f8bSJeremy L Thompson PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer)); 365f14660a4SJames Wright PetscCall(VecView(Q_loc, viewer)); 366f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 36777841947SLeila Ghaffari if (user->dm_viz) { 36877841947SLeila Ghaffari Vec Q_refined, Q_refined_loc; 36977841947SLeila Ghaffari char file_path_refined[PETSC_MAX_PATH_LEN]; 37077841947SLeila Ghaffari PetscViewer viewer_refined; 37177841947SLeila Ghaffari 372f14660a4SJames Wright PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined)); 373f14660a4SJames Wright PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc)); 374f14660a4SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined")); 375f14660a4SJames Wright 376f14660a4SJames Wright PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined)); 377f14660a4SJames Wright PetscCall(VecZeroEntries(Q_refined_loc)); 3782b730f8bSJeremy L Thompson PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc)); 379f14660a4SJames Wright 38037cbb16aSJed Brown PetscCall( 38137cbb16aSJed Brown PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 382f14660a4SJames Wright 3832b730f8bSJeremy L Thompson PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined)); 384f14660a4SJames Wright PetscCall(VecView(Q_refined_loc, viewer_refined)); 385f14660a4SJames Wright PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc)); 386f14660a4SJames Wright PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined)); 387f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer_refined)); 38877841947SLeila Ghaffari } 389f14660a4SJames Wright PetscCall(DMRestoreLocalVector(user->dm, &Q_loc)); 39037cbb16aSJed Brown } 39177841947SLeila Ghaffari 39277841947SLeila Ghaffari // Save data in a binary file for continuation of simulations 39369293791SJames Wright if (user->app_ctx->add_stepnum2bin) { 39437cbb16aSJed Brown PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", user->app_ctx->output_dir, step_no)); 39569293791SJames Wright } else { 3962b730f8bSJeremy L Thompson PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", user->app_ctx->output_dir)); 39769293791SJames Wright } 3982b730f8bSJeremy L Thompson PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, &viewer)); 399f14660a4SJames Wright 4004de8550aSJed Brown PetscInt token = FLUIDS_FILE_TOKEN; 4014de8550aSJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &token, 1, PETSC_INT)); 4024de8550aSJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &step_no, 1, PETSC_INT)); 4034de8550aSJed Brown time /= user->units->second; // Dimensionalize time back 4044de8550aSJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL)); 405f14660a4SJames Wright PetscCall(VecView(Q, viewer)); 406f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 407f14660a4SJames Wright PetscFunctionReturn(0); 408f14660a4SJames Wright } 409f14660a4SJames Wright 410f14660a4SJames Wright // User provided TS Monitor 4112b730f8bSJeremy L Thompson PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 412f14660a4SJames Wright User user = ctx; 413f14660a4SJames Wright PetscFunctionBeginUser; 414f14660a4SJames Wright 41537cbb16aSJed Brown // Print every 'checkpoint_interval' steps 416894de27cSJames Wright if (user->app_ctx->checkpoint_interval <= 0 || step_no % user->app_ctx->checkpoint_interval != 0 || 417894de27cSJames Wright (user->app_ctx->cont_steps == step_no && step_no != 0)) 418894de27cSJames Wright PetscFunctionReturn(0); 419f14660a4SJames Wright 420f14660a4SJames Wright PetscCall(WriteOutput(user, Q, step_no, time)); 42177841947SLeila Ghaffari 42277841947SLeila Ghaffari PetscFunctionReturn(0); 42377841947SLeila Ghaffari } 42477841947SLeila Ghaffari 42577841947SLeila Ghaffari // TS: Create, setup, and solve 4262b730f8bSJeremy L Thompson PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, Vec *Q, PetscScalar *f_time, TS *ts) { 42777841947SLeila Ghaffari MPI_Comm comm = user->comm; 42877841947SLeila Ghaffari TSAdapt adapt; 42977841947SLeila Ghaffari PetscScalar final_time; 43077841947SLeila Ghaffari PetscFunctionBeginUser; 43177841947SLeila Ghaffari 4322b730f8bSJeremy L Thompson PetscCall(TSCreate(comm, ts)); 4332b730f8bSJeremy L Thompson PetscCall(TSSetDM(*ts, dm)); 43477841947SLeila Ghaffari if (phys->implicit) { 4352b730f8bSJeremy L Thompson PetscCall(TSSetType(*ts, TSBDF)); 43677841947SLeila Ghaffari if (user->op_ifunction) { 4372b730f8bSJeremy L Thompson PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &user)); 43877841947SLeila Ghaffari } else { // Implicit integrators can fall back to using an RHSFunction 4392b730f8bSJeremy L Thompson PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 44077841947SLeila Ghaffari } 441e334ad8fSJed Brown if (user->op_ijacobian) { 4422b730f8bSJeremy L Thompson PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &user)); 443544be873SJed Brown if (app_ctx->amat_type) { 444544be873SJed Brown Mat Pmat, Amat; 4452b730f8bSJeremy L Thompson PetscCall(DMCreateMatrix(dm, &Pmat)); 4462b730f8bSJeremy L Thompson PetscCall(DMSetMatType(dm, app_ctx->amat_type)); 4472b730f8bSJeremy L Thompson PetscCall(DMCreateMatrix(dm, &Amat)); 4482b730f8bSJeremy L Thompson PetscCall(TSSetIJacobian(*ts, Amat, Pmat, NULL, NULL)); 4492b730f8bSJeremy L Thompson PetscCall(MatDestroy(&Amat)); 4502b730f8bSJeremy L Thompson PetscCall(MatDestroy(&Pmat)); 451544be873SJed Brown } 452e334ad8fSJed Brown } 45377841947SLeila Ghaffari } else { 4542b730f8bSJeremy L Thompson if (!user->op_rhs) SETERRQ(comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction"); 4552b730f8bSJeremy L Thompson PetscCall(TSSetType(*ts, TSRK)); 4562b730f8bSJeremy L Thompson PetscCall(TSRKSetType(*ts, TSRK5F)); 4572b730f8bSJeremy L Thompson PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 45877841947SLeila Ghaffari } 4592b730f8bSJeremy L Thompson PetscCall(TSSetMaxTime(*ts, 500. * user->units->second)); 4602b730f8bSJeremy L Thompson PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER)); 461cf7a0454SJed Brown PetscCall(TSSetErrorIfStepFails(*ts, PETSC_FALSE)); 4622b730f8bSJeremy L Thompson PetscCall(TSSetTimeStep(*ts, 1.e-2 * user->units->second)); 4638fb33541SJames Wright if (app_ctx->test_type != TESTTYPE_NONE) { 4642b730f8bSJeremy L Thompson PetscCall(TSSetMaxSteps(*ts, 10)); 4652b730f8bSJeremy L Thompson } 4662b730f8bSJeremy L Thompson PetscCall(TSGetAdapt(*ts, &adapt)); 4672b730f8bSJeremy L Thompson PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * user->units->second, 1.e2 * user->units->second)); 4682b730f8bSJeremy L Thompson PetscCall(TSSetFromOptions(*ts)); 469a0b9a424SJames Wright user->time = user->time_bc_set = -1.0; // require all BCs and ctx to be updated 4705e82a6e1SJeremy L Thompson user->dt = -1.0; 47177841947SLeila Ghaffari if (!app_ctx->cont_steps) { // print initial condition 4728fb33541SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 4732b730f8bSJeremy L Thompson PetscCall(TSMonitor_NS(*ts, 0, 0., *Q, user)); 47477841947SLeila Ghaffari } 47577841947SLeila Ghaffari } else { // continue from time of last output 47677841947SLeila Ghaffari PetscInt count; 47777841947SLeila Ghaffari PetscViewer viewer; 4782b730f8bSJeremy L Thompson 4794de8550aSJed Brown if (app_ctx->cont_time <= 0) { // Legacy files did not include step number and time 4802b730f8bSJeremy L Thompson PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer)); 4814de8550aSJed Brown PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL)); 4822b730f8bSJeremy L Thompson PetscCall(PetscViewerDestroy(&viewer)); 4834de8550aSJed Brown PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP, 4844de8550aSJed Brown "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)"); 4854de8550aSJed Brown } 4864de8550aSJed Brown PetscCall(TSSetTime(*ts, app_ctx->cont_time * user->units->second)); 487d8e0aecdSJed Brown PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps)); 48877841947SLeila Ghaffari } 4898fb33541SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 4902b730f8bSJeremy L Thompson PetscCall(TSMonitorSet(*ts, TSMonitor_NS, user, NULL)); 4918fb33541SJames Wright } 492b7d66439SJames Wright if (app_ctx->turb_spanstats_enable) { 493a175e481SJames Wright PetscCall(TSMonitorSet(*ts, TSMonitor_Statistics, user, NULL)); 494*495b9769SJames Wright CeedScalar previous_time = app_ctx->cont_time * user->units->second; 495*495b9769SJames Wright CeedOperatorContextSetDouble(user->spanstats.op_stats_collect, user->spanstats.previous_time_label, &previous_time); 496a175e481SJames Wright } 49777841947SLeila Ghaffari 49877841947SLeila Ghaffari // Solve 499d8e0aecdSJed Brown PetscReal start_time; 500d8e0aecdSJed Brown PetscInt start_step; 5012b730f8bSJeremy L Thompson PetscCall(TSGetTime(*ts, &start_time)); 502d8e0aecdSJed Brown PetscCall(TSGetStepNumber(*ts, &start_step)); 5037b39487dSJeremy L Thompson 5047b39487dSJeremy L Thompson PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve"); 5057b39487dSJeremy L Thompson PetscCall(TSSetTime(*ts, start_time)); 506d8e0aecdSJed Brown PetscCall(TSSetStepNumber(*ts, start_step)); 5077b39487dSJeremy L Thompson if (PetscPreLoadingOn) { 5087b39487dSJeremy L Thompson // LCOV_EXCL_START 5097b39487dSJeremy L Thompson SNES snes; 5107b39487dSJeremy L Thompson Vec Q_preload; 5117b39487dSJeremy L Thompson PetscReal rtol; 5127b39487dSJeremy L Thompson PetscCall(VecDuplicate(*Q, &Q_preload)); 5137b39487dSJeremy L Thompson PetscCall(VecCopy(*Q, Q_preload)); 5147b39487dSJeremy L Thompson PetscCall(TSGetSNES(*ts, &snes)); 5157b39487dSJeremy L Thompson PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL)); 5162b730f8bSJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 5177b39487dSJeremy L Thompson PetscCall(TSSetSolution(*ts, *Q)); 5187b39487dSJeremy L Thompson PetscCall(TSStep(*ts)); 5192b730f8bSJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 5207b39487dSJeremy L Thompson PetscCall(VecDestroy(&Q_preload)); 5217b39487dSJeremy L Thompson // LCOV_EXCL_STOP 5227b39487dSJeremy L Thompson } else { 5232b730f8bSJeremy L Thompson PetscCall(PetscBarrier((PetscObject)*ts)); 5242b730f8bSJeremy L Thompson PetscCall(TSSolve(*ts, *Q)); 5257b39487dSJeremy L Thompson } 5267b39487dSJeremy L Thompson PetscPreLoadEnd(); 5277b39487dSJeremy L Thompson 5287b39487dSJeremy L Thompson PetscCall(TSGetSolveTime(*ts, &final_time)); 52977841947SLeila Ghaffari *f_time = final_time; 5307b39487dSJeremy L Thompson 5318fb33541SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 532f14660a4SJames Wright PetscInt step_no; 533f14660a4SJames Wright PetscCall(TSGetStepNumber(*ts, &step_no)); 534a175e481SJames Wright if (user->app_ctx->checkpoint_interval > 0 || user->app_ctx->checkpoint_interval == -1) { 535f14660a4SJames Wright PetscCall(WriteOutput(user, *Q, step_no, final_time)); 536f14660a4SJames Wright } 537f14660a4SJames Wright 5387b39487dSJeremy L Thompson PetscLogEvent stage_id; 5397b39487dSJeremy L Thompson PetscStageLog stage_log; 5407b39487dSJeremy L Thompson 5417b39487dSJeremy L Thompson PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id)); 5427b39487dSJeremy L Thompson PetscCall(PetscLogGetStageLog(&stage_log)); 5432b730f8bSJeremy L Thompson PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_log->stageInfo[stage_id].perfInfo.time)); 54477841947SLeila Ghaffari } 54577841947SLeila Ghaffari PetscFunctionReturn(0); 54677841947SLeila Ghaffari } 547