1727da7e7SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2727da7e7SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3a515125bSLeila Ghaffari // 4727da7e7SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5a515125bSLeila Ghaffari // 6727da7e7SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7a515125bSLeila Ghaffari 8a515125bSLeila Ghaffari /// @file 9a515125bSLeila Ghaffari /// Time-stepping functions for Navier-Stokes example using PETSc 10a515125bSLeila Ghaffari 11a515125bSLeila Ghaffari #include "../navierstokes.h" 12a515125bSLeila Ghaffari #include "../qfunctions/mass.h" 13a515125bSLeila Ghaffari 14a515125bSLeila Ghaffari // Compute mass matrix for explicit scheme 15a515125bSLeila Ghaffari PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm, CeedData ceed_data, 16a515125bSLeila Ghaffari Vec M) { 17a515125bSLeila Ghaffari Vec M_loc; 18a515125bSLeila Ghaffari CeedQFunction qf_mass; 19a515125bSLeila Ghaffari CeedOperator op_mass; 20a515125bSLeila Ghaffari CeedVector m_ceed, ones_vec; 21a515125bSLeila Ghaffari CeedInt num_comp_q, q_data_size; 22a515125bSLeila Ghaffari PetscErrorCode ierr; 23a515125bSLeila Ghaffari PetscFunctionBeginUser; 24a515125bSLeila Ghaffari 25a515125bSLeila Ghaffari // CEED Restriction 26a515125bSLeila Ghaffari CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_q, &num_comp_q); 27a515125bSLeila Ghaffari CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size); 28a515125bSLeila Ghaffari CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &m_ceed, NULL); 29a515125bSLeila Ghaffari CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &ones_vec, NULL); 30a515125bSLeila Ghaffari CeedVectorSetValue(ones_vec, 1.0); 31a515125bSLeila Ghaffari 32a515125bSLeila Ghaffari // CEED QFunction 33a515125bSLeila Ghaffari CeedQFunctionCreateInterior(ceed, 1, Mass, Mass_loc, &qf_mass); 34a515125bSLeila Ghaffari CeedQFunctionAddInput(qf_mass, "q", num_comp_q, CEED_EVAL_INTERP); 353b1e209bSJeremy L Thompson CeedQFunctionAddInput(qf_mass, "qdata", q_data_size, CEED_EVAL_NONE); 36a515125bSLeila Ghaffari CeedQFunctionAddOutput(qf_mass, "v", num_comp_q, CEED_EVAL_INTERP); 37a515125bSLeila Ghaffari 38a515125bSLeila Ghaffari // CEED Operator 39a515125bSLeila Ghaffari CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass); 40a515125bSLeila Ghaffari CeedOperatorSetField(op_mass, "q", ceed_data->elem_restr_q, ceed_data->basis_q, 41a515125bSLeila Ghaffari CEED_VECTOR_ACTIVE); 423b1e209bSJeremy L Thompson CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, 43a515125bSLeila Ghaffari CEED_BASIS_COLLOCATED, ceed_data->q_data); 44a515125bSLeila Ghaffari CeedOperatorSetField(op_mass, "v", ceed_data->elem_restr_q, ceed_data->basis_q, 45a515125bSLeila Ghaffari CEED_VECTOR_ACTIVE); 46a515125bSLeila Ghaffari 47a515125bSLeila Ghaffari // Place PETSc vector in CEED vector 48a515125bSLeila Ghaffari CeedScalar *m; 49a515125bSLeila Ghaffari PetscMemType m_mem_type; 50a515125bSLeila Ghaffari ierr = DMGetLocalVector(dm, &M_loc); CHKERRQ(ierr); 51a515125bSLeila Ghaffari ierr = VecGetArrayAndMemType(M_loc, (PetscScalar **)&m, &m_mem_type); 52a515125bSLeila Ghaffari CHKERRQ(ierr); 53a515125bSLeila Ghaffari CeedVectorSetArray(m_ceed, MemTypeP2C(m_mem_type), CEED_USE_POINTER, m); 54a515125bSLeila Ghaffari 55a515125bSLeila Ghaffari // Apply CEED Operator 56a515125bSLeila Ghaffari CeedOperatorApply(op_mass, ones_vec, m_ceed, CEED_REQUEST_IMMEDIATE); 57a515125bSLeila Ghaffari 58a515125bSLeila Ghaffari // Restore vectors 59a515125bSLeila Ghaffari CeedVectorTakeArray(m_ceed, MemTypeP2C(m_mem_type), NULL); 60a515125bSLeila Ghaffari ierr = VecRestoreArrayReadAndMemType(M_loc, (const PetscScalar **)&m); 61a515125bSLeila Ghaffari CHKERRQ(ierr); 62a515125bSLeila Ghaffari 63a515125bSLeila Ghaffari // Local-to-Global 64a515125bSLeila Ghaffari ierr = VecZeroEntries(M); CHKERRQ(ierr); 65a515125bSLeila Ghaffari ierr = DMLocalToGlobal(dm, M_loc, ADD_VALUES, M); CHKERRQ(ierr); 66a515125bSLeila Ghaffari ierr = DMRestoreLocalVector(dm, &M_loc); CHKERRQ(ierr); 67a515125bSLeila Ghaffari 68a515125bSLeila Ghaffari // Invert diagonally lumped mass vector for RHS function 69a515125bSLeila Ghaffari ierr = VecReciprocal(M); CHKERRQ(ierr); 70a515125bSLeila Ghaffari 71a515125bSLeila Ghaffari // Cleanup 72a515125bSLeila Ghaffari CeedVectorDestroy(&ones_vec); 73a515125bSLeila Ghaffari CeedVectorDestroy(&m_ceed); 74a515125bSLeila Ghaffari CeedQFunctionDestroy(&qf_mass); 75a515125bSLeila Ghaffari CeedOperatorDestroy(&op_mass); 76a515125bSLeila Ghaffari 77a515125bSLeila Ghaffari PetscFunctionReturn(0); 78a515125bSLeila Ghaffari } 79a515125bSLeila Ghaffari 80a515125bSLeila Ghaffari // RHS (Explicit time-stepper) function setup 81a515125bSLeila Ghaffari // This is the RHS of the ODE, given as u_t = G(t,u) 82a515125bSLeila Ghaffari // This function takes in a state vector Q and writes into G 83a515125bSLeila Ghaffari PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data) { 84a515125bSLeila Ghaffari User user = *(User *)user_data; 85a515125bSLeila Ghaffari PetscScalar *q, *g; 86e2f84137SJeremy L Thompson Vec Q_loc = user->Q_loc, G_loc; 87a515125bSLeila Ghaffari PetscMemType q_mem_type, g_mem_type; 88a515125bSLeila Ghaffari PetscErrorCode ierr; 89a515125bSLeila Ghaffari PetscFunctionBeginUser; 90a515125bSLeila Ghaffari 91e2f84137SJeremy L Thompson // Get local vector 92e2f84137SJeremy L Thompson ierr = DMGetLocalVector(user->dm, &G_loc); CHKERRQ(ierr); 93e2f84137SJeremy L Thompson 94e2f84137SJeremy L Thompson // Update time dependent data 95e2f84137SJeremy L Thompson if (user->time != t) { 96e2f84137SJeremy L Thompson ierr = DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, 97e2f84137SJeremy L Thompson NULL, NULL, NULL); CHKERRQ(ierr); 98e2f84137SJeremy L Thompson if (user->phys->solution_time_label) { 9992ada588SJames Wright CeedOperatorContextSetDouble(user->op_rhs, user->phys->solution_time_label, &t); 100e2f84137SJeremy L Thompson } 101e2f84137SJeremy L Thompson user->time = t; 102e2f84137SJeremy L Thompson } 103bb8a0c61SJames Wright if (user->phys->timestep_size_label) { 104bb8a0c61SJames Wright PetscScalar dt; 105bb8a0c61SJames Wright ierr = TSGetTimeStep(ts, &dt); CHKERRQ(ierr); 106e2f84137SJeremy L Thompson if (user->dt != dt) { 107bb8a0c61SJames Wright CeedOperatorContextSetDouble(user->op_rhs, user->phys->timestep_size_label, 108bb8a0c61SJames Wright &dt); 109e2f84137SJeremy L Thompson user->dt = dt; 110e2f84137SJeremy L Thompson } 111bb8a0c61SJames Wright } 112a515125bSLeila Ghaffari 113a515125bSLeila Ghaffari // Global-to-local 114a515125bSLeila Ghaffari ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc); CHKERRQ(ierr); 115a515125bSLeila Ghaffari 116a515125bSLeila Ghaffari // Place PETSc vectors in CEED vectors 117a515125bSLeila Ghaffari ierr = VecGetArrayReadAndMemType(Q_loc, (const PetscScalar **)&q, &q_mem_type); 118a515125bSLeila Ghaffari CHKERRQ(ierr); 119a515125bSLeila Ghaffari ierr = VecGetArrayAndMemType(G_loc, &g, &g_mem_type); CHKERRQ(ierr); 120a515125bSLeila Ghaffari CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, q); 121a515125bSLeila Ghaffari CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g); 122a515125bSLeila Ghaffari 123a515125bSLeila Ghaffari // Apply CEED operator 124a515125bSLeila Ghaffari CeedOperatorApply(user->op_rhs, user->q_ceed, user->g_ceed, 125a515125bSLeila Ghaffari CEED_REQUEST_IMMEDIATE); 126a515125bSLeila Ghaffari 127a515125bSLeila Ghaffari // Restore vectors 128a515125bSLeila Ghaffari CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL); 129a515125bSLeila Ghaffari CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL); 130a515125bSLeila Ghaffari ierr = VecRestoreArrayReadAndMemType(Q_loc, (const PetscScalar **)&q); 131a515125bSLeila Ghaffari CHKERRQ(ierr); 132a515125bSLeila Ghaffari ierr = VecRestoreArrayAndMemType(G_loc, &g); CHKERRQ(ierr); 133a515125bSLeila Ghaffari 134a515125bSLeila Ghaffari // Local-to-Global 135a515125bSLeila Ghaffari ierr = VecZeroEntries(G); CHKERRQ(ierr); 136a515125bSLeila Ghaffari ierr = DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G); CHKERRQ(ierr); 137a515125bSLeila Ghaffari 138a515125bSLeila Ghaffari // Inverse of the lumped mass matrix (M is Minv) 139a515125bSLeila Ghaffari ierr = VecPointwiseMult(G, G, user->M); CHKERRQ(ierr); 140a515125bSLeila Ghaffari 141a515125bSLeila Ghaffari // Restore vectors 142a515125bSLeila Ghaffari ierr = DMRestoreLocalVector(user->dm, &G_loc); CHKERRQ(ierr); 143a515125bSLeila Ghaffari 144a515125bSLeila Ghaffari PetscFunctionReturn(0); 145a515125bSLeila Ghaffari } 146a515125bSLeila Ghaffari 147a515125bSLeila Ghaffari // Implicit time-stepper function setup 148a515125bSLeila Ghaffari PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, 149a515125bSLeila Ghaffari void *user_data) { 150a515125bSLeila Ghaffari User user = *(User *)user_data; 151a515125bSLeila Ghaffari const PetscScalar *q, *q_dot; 152a515125bSLeila Ghaffari PetscScalar *g; 153e2f84137SJeremy L Thompson Vec Q_loc = user->Q_loc, Q_dot_loc = user->Q_dot_loc, G_loc; 154a515125bSLeila Ghaffari PetscMemType q_mem_type, q_dot_mem_type, g_mem_type; 155a515125bSLeila Ghaffari PetscErrorCode ierr; 156a515125bSLeila Ghaffari PetscFunctionBeginUser; 157a515125bSLeila Ghaffari 158e2f84137SJeremy L Thompson // Get local vectors 159e2f84137SJeremy L Thompson ierr = DMGetLocalVector(user->dm, &G_loc); CHKERRQ(ierr); 160e2f84137SJeremy L Thompson 161e2f84137SJeremy L Thompson // Update time dependent data 162e2f84137SJeremy L Thompson if (user->time != t) { 163e2f84137SJeremy L Thompson ierr = DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, 164e2f84137SJeremy L Thompson NULL, NULL, NULL); CHKERRQ(ierr); 165e2f84137SJeremy L Thompson if (user->phys->solution_time_label) { 16692ada588SJames Wright CeedOperatorContextSetDouble(user->op_ifunction, 16792ada588SJames Wright user->phys->solution_time_label, &t); 168e2f84137SJeremy L Thompson } 169e2f84137SJeremy L Thompson user->time = t; 170e2f84137SJeremy L Thompson } 171bb8a0c61SJames Wright if (user->phys->timestep_size_label) { 172bb8a0c61SJames Wright PetscScalar dt; 173bb8a0c61SJames Wright ierr = TSGetTimeStep(ts, &dt); CHKERRQ(ierr); 174e2f84137SJeremy L Thompson if (user->dt != dt) { 175bb8a0c61SJames Wright CeedOperatorContextSetDouble(user->op_ifunction, 176bb8a0c61SJames Wright user->phys->timestep_size_label, &dt); 177e2f84137SJeremy L Thompson user->dt = dt; 178e2f84137SJeremy L Thompson } 179bb8a0c61SJames Wright } 180a515125bSLeila Ghaffari 181a515125bSLeila Ghaffari // Global-to-local 182a515125bSLeila Ghaffari ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc); CHKERRQ(ierr); 183a515125bSLeila Ghaffari ierr = DMGlobalToLocal(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc); 184a515125bSLeila Ghaffari CHKERRQ(ierr); 185a515125bSLeila Ghaffari 186a515125bSLeila Ghaffari // Place PETSc vectors in CEED vectors 187a515125bSLeila Ghaffari ierr = VecGetArrayReadAndMemType(Q_loc, &q, &q_mem_type); CHKERRQ(ierr); 188a515125bSLeila Ghaffari ierr = VecGetArrayReadAndMemType(Q_dot_loc, &q_dot, &q_dot_mem_type); 189a515125bSLeila Ghaffari CHKERRQ(ierr); 190a515125bSLeila Ghaffari ierr = VecGetArrayAndMemType(G_loc, &g, &g_mem_type); CHKERRQ(ierr); 191a515125bSLeila Ghaffari CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, 192a515125bSLeila Ghaffari (PetscScalar *)q); 193a515125bSLeila Ghaffari CeedVectorSetArray(user->q_dot_ceed, MemTypeP2C(q_dot_mem_type), 194139613f2SLeila Ghaffari CEED_USE_POINTER, (PetscScalar *)q_dot); 195a515125bSLeila Ghaffari CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g); 196a515125bSLeila Ghaffari 197a515125bSLeila Ghaffari // Apply CEED operator 198a515125bSLeila Ghaffari CeedOperatorApply(user->op_ifunction, user->q_ceed, user->g_ceed, 199a515125bSLeila Ghaffari CEED_REQUEST_IMMEDIATE); 200a515125bSLeila Ghaffari 201a515125bSLeila Ghaffari // Restore vectors 202a515125bSLeila Ghaffari CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL); 203a515125bSLeila Ghaffari CeedVectorTakeArray(user->q_dot_ceed, MemTypeP2C(q_dot_mem_type), NULL); 204a515125bSLeila Ghaffari CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL); 205a515125bSLeila Ghaffari ierr = VecRestoreArrayReadAndMemType(Q_loc, &q); CHKERRQ(ierr); 206a515125bSLeila Ghaffari ierr = VecRestoreArrayReadAndMemType(Q_dot_loc, &q_dot); CHKERRQ(ierr); 207a515125bSLeila Ghaffari ierr = VecRestoreArrayAndMemType(G_loc, &g); CHKERRQ(ierr); 208a515125bSLeila Ghaffari 209a515125bSLeila Ghaffari // Local-to-Global 210a515125bSLeila Ghaffari ierr = VecZeroEntries(G); CHKERRQ(ierr); 211a515125bSLeila Ghaffari ierr = DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G); CHKERRQ(ierr); 212a515125bSLeila Ghaffari 213a515125bSLeila Ghaffari // Restore vectors 214a515125bSLeila Ghaffari ierr = DMRestoreLocalVector(user->dm, &G_loc); CHKERRQ(ierr); 215a515125bSLeila Ghaffari 216a515125bSLeila Ghaffari PetscFunctionReturn(0); 217a515125bSLeila Ghaffari } 218a515125bSLeila Ghaffari 219f0b65372SJed Brown static PetscErrorCode MatMult_NS_IJacobian(Mat J, Vec Q, Vec G) { 220f0b65372SJed Brown User user; 221f0b65372SJed Brown const PetscScalar *q; 222f0b65372SJed Brown PetscScalar *g; 223f0b65372SJed Brown PetscMemType q_mem_type, g_mem_type; 224f0b65372SJed Brown PetscErrorCode ierr; 225f0b65372SJed Brown PetscFunctionBeginUser; 226e2f84137SJeremy L Thompson ierr = MatShellGetContext(J, &user); CHKERRQ(ierr); 227e2f84137SJeremy L Thompson Vec Q_loc = user->Q_dot_loc, // Note - Q_dot_loc has zero BCs 228e2f84137SJeremy L Thompson G_loc; 229e2f84137SJeremy L Thompson 230f0b65372SJed Brown // Get local vectors 231f0b65372SJed Brown ierr = DMGetLocalVector(user->dm, &G_loc); CHKERRQ(ierr); 232f0b65372SJed Brown 233f0b65372SJed Brown // Global-to-local 234f0b65372SJed Brown ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc); CHKERRQ(ierr); 235f0b65372SJed Brown 236f0b65372SJed Brown // Place PETSc vectors in CEED vectors 237f0b65372SJed Brown ierr = VecGetArrayReadAndMemType(Q_loc, &q, &q_mem_type); CHKERRQ(ierr); 238f0b65372SJed Brown ierr = VecGetArrayAndMemType(G_loc, &g, &g_mem_type); CHKERRQ(ierr); 239f0b65372SJed Brown CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, 240f0b65372SJed Brown (PetscScalar *)q); 241f0b65372SJed Brown CeedVectorSetArray(user->g_ceed, MemTypeP2C(g_mem_type), CEED_USE_POINTER, g); 242f0b65372SJed Brown 243f0b65372SJed Brown // Apply CEED operator 244f0b65372SJed Brown CeedOperatorApply(user->op_ijacobian, user->q_ceed, user->g_ceed, 245f0b65372SJed Brown CEED_REQUEST_IMMEDIATE); 246f0b65372SJed Brown 247f0b65372SJed Brown // Restore vectors 248f0b65372SJed Brown CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL); 249f0b65372SJed Brown CeedVectorTakeArray(user->g_ceed, MemTypeP2C(g_mem_type), NULL); 250f0b65372SJed Brown ierr = VecRestoreArrayReadAndMemType(Q_loc, &q); CHKERRQ(ierr); 251f0b65372SJed Brown ierr = VecRestoreArrayAndMemType(G_loc, &g); CHKERRQ(ierr); 252f0b65372SJed Brown 253f0b65372SJed Brown // Local-to-Global 254f0b65372SJed Brown ierr = VecZeroEntries(G); CHKERRQ(ierr); 255f0b65372SJed Brown ierr = DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G); CHKERRQ(ierr); 256f0b65372SJed Brown 257f0b65372SJed Brown // Restore vectors 258f0b65372SJed Brown ierr = DMRestoreLocalVector(user->dm, &G_loc); CHKERRQ(ierr); 259f0b65372SJed Brown PetscFunctionReturn(0); 260f0b65372SJed Brown } 261f0b65372SJed Brown 262f0b65372SJed Brown PetscErrorCode MatGetDiagonal_NS_IJacobian(Mat A, Vec D) { 263f0b65372SJed Brown User user; 264f0b65372SJed Brown Vec D_loc; 265f0b65372SJed Brown PetscScalar *d; 266f0b65372SJed Brown PetscMemType mem_type; 267f0b65372SJed Brown 268f0b65372SJed Brown PetscFunctionBeginUser; 269f0b65372SJed Brown MatShellGetContext(A, &user); 270f0b65372SJed Brown PetscCall(DMGetLocalVector(user->dm, &D_loc)); 271f0b65372SJed Brown PetscCall(VecGetArrayAndMemType(D_loc, &d, &mem_type)); 272f0b65372SJed Brown CeedVectorSetArray(user->g_ceed, MemTypeP2C(mem_type), CEED_USE_POINTER, d); 273f0b65372SJed Brown CeedOperatorLinearAssembleDiagonal(user->op_ijacobian, user->g_ceed, 274f0b65372SJed Brown CEED_REQUEST_IMMEDIATE); 275f0b65372SJed Brown CeedVectorTakeArray(user->g_ceed, MemTypeP2C(mem_type), NULL); 276f0b65372SJed Brown PetscCall(VecRestoreArrayAndMemType(D_loc, &d)); 277f0b65372SJed Brown PetscCall(VecZeroEntries(D)); 278f0b65372SJed Brown PetscCall(DMLocalToGlobal(user->dm, D_loc, ADD_VALUES, D)); 279f0b65372SJed Brown PetscCall(DMRestoreLocalVector(user->dm, &D_loc)); 280f0b65372SJed Brown VecViewFromOptions(D, NULL, "-diag_vec_view"); 281f0b65372SJed Brown PetscFunctionReturn(0); 282f0b65372SJed Brown } 283f0b65372SJed Brown 284b107fddaSJed Brown static PetscErrorCode FormPreallocation(User user, PetscBool pbdiagonal, Mat J, 285b107fddaSJed Brown CeedVector *coo_values) { 286b107fddaSJed Brown PetscCount ncoo; 287b107fddaSJed Brown PetscInt *rows, *cols; 288b107fddaSJed Brown 289b107fddaSJed Brown PetscFunctionBeginUser; 290b107fddaSJed Brown if (pbdiagonal) { 291b107fddaSJed Brown CeedSize l_size; 292b107fddaSJed Brown CeedOperatorGetActiveVectorLengths(user->op_ijacobian, &l_size, NULL); 293b107fddaSJed Brown ncoo = l_size * 5; 294b107fddaSJed Brown rows = malloc(ncoo*sizeof(rows[0])); 295b107fddaSJed Brown cols = malloc(ncoo*sizeof(cols[0])); 296b107fddaSJed Brown for (PetscCount n=0; n<l_size/5; n++) { 297b107fddaSJed Brown for (PetscInt i=0; i<5; i++) { 298b107fddaSJed Brown for (PetscInt j=0; j<5; j++) { 299b107fddaSJed Brown rows[(n*5+i)*5+j] = n * 5 + i; 300b107fddaSJed Brown cols[(n*5+i)*5+j] = n * 5 + j; 301b107fddaSJed Brown } 302b107fddaSJed Brown } 303b107fddaSJed Brown } 304b107fddaSJed Brown } else { 305b107fddaSJed Brown PetscCall(CeedOperatorLinearAssembleSymbolic(user->op_ijacobian, &ncoo, &rows, 306b107fddaSJed Brown &cols)); 307b107fddaSJed Brown } 308b107fddaSJed Brown PetscCall(MatSetPreallocationCOOLocal(J, ncoo, rows, cols)); 309b107fddaSJed Brown free(rows); 310b107fddaSJed Brown free(cols); 311b107fddaSJed Brown CeedVectorCreate(user->ceed, ncoo, coo_values); 312b107fddaSJed Brown PetscFunctionReturn(0); 313b107fddaSJed Brown } 314b107fddaSJed Brown 315b107fddaSJed Brown static PetscErrorCode FormSetValues(User user, PetscBool pbdiagonal, Mat J, 316b107fddaSJed Brown CeedVector coo_values) { 317b107fddaSJed Brown CeedMemType mem_type = CEED_MEM_HOST; 318b107fddaSJed Brown const PetscScalar *values; 319b107fddaSJed Brown MatType mat_type; 320b107fddaSJed Brown 321b107fddaSJed Brown PetscFunctionBeginUser; 322b107fddaSJed Brown PetscCall(MatGetType(J, &mat_type)); 323b107fddaSJed Brown if (strstr(mat_type, "kokkos") || strstr(mat_type, "cusparse")) 324b107fddaSJed Brown mem_type = CEED_MEM_DEVICE; 325b107fddaSJed Brown if (user->app_ctx->pmat_pbdiagonal) { 326b107fddaSJed Brown CeedOperatorLinearAssemblePointBlockDiagonal(user->op_ijacobian, 327b107fddaSJed Brown coo_values, CEED_REQUEST_IMMEDIATE); 328b107fddaSJed Brown } else { 329b107fddaSJed Brown CeedOperatorLinearAssemble(user->op_ijacobian, coo_values); 330b107fddaSJed Brown } 331b107fddaSJed Brown CeedVectorGetArrayRead(coo_values, mem_type, &values); 332b107fddaSJed Brown PetscCall(MatSetValuesCOO(J, values, INSERT_VALUES)); 333b107fddaSJed Brown CeedVectorRestoreArrayRead(coo_values, &values); 334b107fddaSJed Brown PetscFunctionReturn(0); 335b107fddaSJed Brown } 336b107fddaSJed Brown 337f0b65372SJed Brown PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, 338f0b65372SJed Brown PetscReal shift, Mat J, Mat J_pre, 339f0b65372SJed Brown void *user_data) { 340f0b65372SJed Brown User user = *(User *)user_data; 34104855949SJed Brown PetscBool J_is_shell, J_is_mffd, J_pre_is_shell; 342f0b65372SJed Brown PetscFunctionBeginUser; 343f0b65372SJed Brown if (user->phys->ijacobian_time_shift_label) 344f0b65372SJed Brown CeedOperatorContextSetDouble(user->op_ijacobian, 345f0b65372SJed Brown user->phys->ijacobian_time_shift_label, &shift); 34604855949SJed Brown PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd)); 347f0b65372SJed Brown PetscCall(PetscObjectTypeCompare((PetscObject)J, MATSHELL, &J_is_shell)); 348f0b65372SJed Brown PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATSHELL, 349f0b65372SJed Brown &J_pre_is_shell)); 350f0b65372SJed Brown if (!user->matrices_set_up) { 351f0b65372SJed Brown if (J_is_shell) { 352f0b65372SJed Brown PetscCall(MatShellSetContext(J, user)); 353f0b65372SJed Brown PetscCall(MatShellSetOperation(J, MATOP_MULT, 354f0b65372SJed Brown (void (*)(void))MatMult_NS_IJacobian)); 355f0b65372SJed Brown PetscCall(MatShellSetOperation(J, MATOP_GET_DIAGONAL, 356f0b65372SJed Brown (void (*)(void))MatGetDiagonal_NS_IJacobian)); 357f0b65372SJed Brown PetscCall(MatSetUp(J)); 358f0b65372SJed Brown } 359f0b65372SJed Brown if (!J_pre_is_shell) { 360b107fddaSJed Brown PetscCall(FormPreallocation(user,user->app_ctx->pmat_pbdiagonal,J_pre, 361b107fddaSJed Brown &user->coo_values_pmat)); 362b107fddaSJed Brown } 36304855949SJed Brown if (J != J_pre && !J_is_shell && !J_is_mffd) { 364b107fddaSJed Brown PetscCall(FormPreallocation(user,PETSC_FALSE,J, &user->coo_values_amat)); 365b107fddaSJed Brown } 366f0b65372SJed Brown user->matrices_set_up = true; 367f0b65372SJed Brown } 368f0b65372SJed Brown if (!J_pre_is_shell) { 369b107fddaSJed Brown PetscCall(FormSetValues(user, user->app_ctx->pmat_pbdiagonal, J_pre, 370b107fddaSJed Brown user->coo_values_pmat)); 371f0b65372SJed Brown } 37204855949SJed Brown if (user->coo_values_amat) { 37304855949SJed Brown PetscCall(FormSetValues(user, PETSC_FALSE, J, user->coo_values_amat)); 37404855949SJed Brown } else if (J_is_mffd) { 37504855949SJed Brown PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 37604855949SJed Brown PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 37704855949SJed Brown } 378f0b65372SJed Brown PetscFunctionReturn(0); 379f0b65372SJed Brown } 380f0b65372SJed Brown 3817538d537SJames Wright PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, 3827538d537SJames Wright PetscScalar time) { 383a515125bSLeila Ghaffari Vec Q_loc; 384a515125bSLeila Ghaffari char file_path[PETSC_MAX_PATH_LEN]; 385a515125bSLeila Ghaffari PetscViewer viewer; 386a515125bSLeila Ghaffari PetscFunctionBeginUser; 387a515125bSLeila Ghaffari 388a515125bSLeila Ghaffari // Set up output 3897538d537SJames Wright PetscCall(DMGetLocalVector(user->dm, &Q_loc)); 3907538d537SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec")); 3917538d537SJames Wright PetscCall(VecZeroEntries(Q_loc)); 3927538d537SJames Wright PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc)); 393a515125bSLeila Ghaffari 394a515125bSLeila Ghaffari // Output 3957538d537SJames Wright PetscCall(PetscSNPrintf(file_path, sizeof file_path, 396d940ca4eSJed Brown "%s/ns-%03" PetscInt_FMT ".vtu", 3977538d537SJames Wright user->app_ctx->output_dir, step_no + user->app_ctx->cont_steps)); 3987538d537SJames Wright 3997538d537SJames Wright PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, 4007538d537SJames Wright FILE_MODE_WRITE, &viewer)); 4017538d537SJames Wright PetscCall(VecView(Q_loc, viewer)); 4027538d537SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 403a515125bSLeila Ghaffari if (user->dm_viz) { 404a515125bSLeila Ghaffari Vec Q_refined, Q_refined_loc; 405a515125bSLeila Ghaffari char file_path_refined[PETSC_MAX_PATH_LEN]; 406a515125bSLeila Ghaffari PetscViewer viewer_refined; 407a515125bSLeila Ghaffari 4087538d537SJames Wright PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined)); 4097538d537SJames Wright PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc)); 4107538d537SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined")); 4117538d537SJames Wright 4127538d537SJames Wright PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined)); 4137538d537SJames Wright PetscCall(VecZeroEntries(Q_refined_loc)); 4147538d537SJames Wright PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, 4157538d537SJames Wright Q_refined_loc)); 4167538d537SJames Wright 4177538d537SJames Wright PetscCall(PetscSNPrintf(file_path_refined, sizeof file_path_refined, 418d940ca4eSJed Brown "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, 4197538d537SJames Wright step_no + user->app_ctx->cont_steps)); 4207538d537SJames Wright 4217538d537SJames Wright PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), 4227538d537SJames Wright file_path_refined, FILE_MODE_WRITE, &viewer_refined)); 4237538d537SJames Wright PetscCall(VecView(Q_refined_loc, viewer_refined)); 4247538d537SJames Wright PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc)); 4257538d537SJames Wright PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined)); 4267538d537SJames Wright PetscCall(PetscViewerDestroy(&viewer_refined)); 427a515125bSLeila Ghaffari } 4287538d537SJames Wright PetscCall(DMRestoreLocalVector(user->dm, &Q_loc)); 429a515125bSLeila Ghaffari 430a515125bSLeila Ghaffari // Save data in a binary file for continuation of simulations 431*91a36801SJames Wright if (user->app_ctx->add_stepnum2bin) { 432*91a36801SJames Wright PetscCall(PetscSNPrintf(file_path, sizeof file_path, 433*91a36801SJames Wright "%s/ns-solution-%" PetscInt_FMT ".bin", 434*91a36801SJames Wright user->app_ctx->output_dir, step_no + user->app_ctx->cont_steps)); 435*91a36801SJames Wright } else { 4367538d537SJames Wright PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", 4377538d537SJames Wright user->app_ctx->output_dir)); 438*91a36801SJames Wright } 4397538d537SJames Wright PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, 4407538d537SJames Wright &viewer)); 4417538d537SJames Wright 4427538d537SJames Wright PetscCall(VecView(Q, viewer)); 4437538d537SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 444a515125bSLeila Ghaffari 445a515125bSLeila Ghaffari // Save time stamp 446a515125bSLeila Ghaffari // Dimensionalize time back 447a515125bSLeila Ghaffari time /= user->units->second; 448*91a36801SJames Wright if (user->app_ctx->add_stepnum2bin) { 449*91a36801SJames Wright PetscCall(PetscSNPrintf(file_path, sizeof file_path, 450*91a36801SJames Wright "%s/ns-time-%" PetscInt_FMT ".bin", 451*91a36801SJames Wright user->app_ctx->output_dir, step_no + user->app_ctx->cont_steps)); 452*91a36801SJames Wright } else { 4537538d537SJames Wright PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-time.bin", 4547538d537SJames Wright user->app_ctx->output_dir)); 455*91a36801SJames Wright } 4567538d537SJames Wright PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, 4577538d537SJames Wright &viewer)); 4587538d537SJames Wright 459a515125bSLeila Ghaffari #if PETSC_VERSION_GE(3,13,0) 4607538d537SJames Wright PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL)); 461a515125bSLeila Ghaffari #else 4627538d537SJames Wright PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL, true)); 463a515125bSLeila Ghaffari #endif 4647538d537SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 4657538d537SJames Wright 4667538d537SJames Wright PetscFunctionReturn(0); 4677538d537SJames Wright } 4687538d537SJames Wright 4697538d537SJames Wright // User provided TS Monitor 4707538d537SJames Wright PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, 4717538d537SJames Wright Vec Q, void *ctx) { 4727538d537SJames Wright User user = ctx; 4737538d537SJames Wright PetscFunctionBeginUser; 4747538d537SJames Wright 4757538d537SJames Wright // Print every 'output_freq' steps 4767538d537SJames Wright if (user->app_ctx->output_freq <= 0 4777538d537SJames Wright || step_no % user->app_ctx->output_freq != 0) 4787538d537SJames Wright PetscFunctionReturn(0); 4797538d537SJames Wright 4807538d537SJames Wright PetscCall(WriteOutput(user, Q, step_no, time)); 481a515125bSLeila Ghaffari 482a515125bSLeila Ghaffari PetscFunctionReturn(0); 483a515125bSLeila Ghaffari } 484a515125bSLeila Ghaffari 485a515125bSLeila Ghaffari // TS: Create, setup, and solve 486a515125bSLeila Ghaffari PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, 487a515125bSLeila Ghaffari Vec *Q, PetscScalar *f_time, TS *ts) { 488a515125bSLeila Ghaffari MPI_Comm comm = user->comm; 489a515125bSLeila Ghaffari TSAdapt adapt; 490a515125bSLeila Ghaffari PetscScalar final_time; 491a515125bSLeila Ghaffari PetscErrorCode ierr; 492a515125bSLeila Ghaffari PetscFunctionBeginUser; 493a515125bSLeila Ghaffari 494a515125bSLeila Ghaffari ierr = TSCreate(comm, ts); CHKERRQ(ierr); 495a515125bSLeila Ghaffari ierr = TSSetDM(*ts, dm); CHKERRQ(ierr); 496a515125bSLeila Ghaffari if (phys->implicit) { 497a515125bSLeila Ghaffari ierr = TSSetType(*ts, TSBDF); CHKERRQ(ierr); 498a515125bSLeila Ghaffari if (user->op_ifunction) { 499a515125bSLeila Ghaffari ierr = TSSetIFunction(*ts, NULL, IFunction_NS, &user); CHKERRQ(ierr); 500a515125bSLeila Ghaffari } else { // Implicit integrators can fall back to using an RHSFunction 501a515125bSLeila Ghaffari ierr = TSSetRHSFunction(*ts, NULL, RHS_NS, &user); CHKERRQ(ierr); 502a515125bSLeila Ghaffari } 503f0b65372SJed Brown if (user->op_ijacobian) { 504f0b65372SJed Brown ierr = DMTSSetIJacobian(dm, FormIJacobian_NS, &user); CHKERRQ(ierr); 505b107fddaSJed Brown if (app_ctx->amat_type) { 506b107fddaSJed Brown Mat Pmat,Amat; 507b107fddaSJed Brown ierr = DMCreateMatrix(dm, &Pmat); CHKERRQ(ierr); 508b107fddaSJed Brown ierr = DMSetMatType(dm, app_ctx->amat_type); CHKERRQ(ierr); 509b107fddaSJed Brown ierr = DMCreateMatrix(dm, &Amat); CHKERRQ(ierr); 510b107fddaSJed Brown ierr = TSSetIJacobian(*ts, Amat, Pmat, NULL, NULL); CHKERRQ(ierr); 511b107fddaSJed Brown ierr = MatDestroy(&Amat); CHKERRQ(ierr); 512b107fddaSJed Brown ierr = MatDestroy(&Pmat); CHKERRQ(ierr); 513b107fddaSJed Brown } 514f0b65372SJed Brown } 515a515125bSLeila Ghaffari } else { 516a515125bSLeila Ghaffari if (!user->op_rhs) SETERRQ(comm, PETSC_ERR_ARG_NULL, 517a515125bSLeila Ghaffari "Problem does not provide RHSFunction"); 518a515125bSLeila Ghaffari ierr = TSSetType(*ts, TSRK); CHKERRQ(ierr); 519a515125bSLeila Ghaffari ierr = TSRKSetType(*ts, TSRK5F); CHKERRQ(ierr); 520a515125bSLeila Ghaffari ierr = TSSetRHSFunction(*ts, NULL, RHS_NS, &user); CHKERRQ(ierr); 521a515125bSLeila Ghaffari } 522a515125bSLeila Ghaffari ierr = TSSetMaxTime(*ts, 500. * user->units->second); CHKERRQ(ierr); 523a515125bSLeila Ghaffari ierr = TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER); CHKERRQ(ierr); 524a515125bSLeila Ghaffari ierr = TSSetTimeStep(*ts, 1.e-2 * user->units->second); CHKERRQ(ierr); 525a515125bSLeila Ghaffari if (app_ctx->test_mode) {ierr = TSSetMaxSteps(*ts, 10); CHKERRQ(ierr);} 526a515125bSLeila Ghaffari ierr = TSGetAdapt(*ts, &adapt); CHKERRQ(ierr); 527a515125bSLeila Ghaffari ierr = TSAdaptSetStepLimits(adapt, 528a515125bSLeila Ghaffari 1.e-12 * user->units->second, 529a515125bSLeila Ghaffari 1.e2 * user->units->second); CHKERRQ(ierr); 530a515125bSLeila Ghaffari ierr = TSSetFromOptions(*ts); CHKERRQ(ierr); 531e2f84137SJeremy L Thompson user->time = -1.0; // require all BCs and ctx to be updated 532e2f84137SJeremy L Thompson user->dt = -1.0; 533a515125bSLeila Ghaffari if (!app_ctx->cont_steps) { // print initial condition 534a515125bSLeila Ghaffari if (!app_ctx->test_mode) { 535a515125bSLeila Ghaffari ierr = TSMonitor_NS(*ts, 0, 0., *Q, user); CHKERRQ(ierr); 536a515125bSLeila Ghaffari } 537a515125bSLeila Ghaffari } else { // continue from time of last output 538a515125bSLeila Ghaffari PetscReal time; 539a515125bSLeila Ghaffari PetscInt count; 540a515125bSLeila Ghaffari PetscViewer viewer; 541*91a36801SJames Wright ierr = PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, 542*91a36801SJames Wright &viewer); 543a515125bSLeila Ghaffari CHKERRQ(ierr); 544a515125bSLeila Ghaffari ierr = PetscViewerBinaryRead(viewer, &time, 1, &count, PETSC_REAL); 545a515125bSLeila Ghaffari CHKERRQ(ierr); 546a515125bSLeila Ghaffari ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr); 547a515125bSLeila Ghaffari ierr = TSSetTime(*ts, time * user->units->second); CHKERRQ(ierr); 548a515125bSLeila Ghaffari } 549a515125bSLeila Ghaffari if (!app_ctx->test_mode) { 550a515125bSLeila Ghaffari ierr = TSMonitorSet(*ts, TSMonitor_NS, user, NULL); CHKERRQ(ierr); 551a515125bSLeila Ghaffari } 552a515125bSLeila Ghaffari 553a515125bSLeila Ghaffari // Solve 55491982731SJeremy L Thompson PetscScalar start_time; 55591982731SJeremy L Thompson ierr = TSGetTime(*ts, &start_time); CHKERRQ(ierr); 55691982731SJeremy L Thompson 55791982731SJeremy L Thompson PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve"); 55891982731SJeremy L Thompson PetscCall(TSSetTime(*ts, start_time)); 55991982731SJeremy L Thompson PetscCall(TSSetStepNumber(*ts, 0)); 56091982731SJeremy L Thompson if (PetscPreLoadingOn) { 56191982731SJeremy L Thompson // LCOV_EXCL_START 56291982731SJeremy L Thompson SNES snes; 56391982731SJeremy L Thompson Vec Q_preload; 56491982731SJeremy L Thompson PetscReal rtol; 56591982731SJeremy L Thompson PetscCall(VecDuplicate(*Q, &Q_preload)); 56691982731SJeremy L Thompson PetscCall(VecCopy(*Q, Q_preload)); 56791982731SJeremy L Thompson PetscCall(TSGetSNES(*ts, &snes)); 56891982731SJeremy L Thompson PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL)); 56991982731SJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, 57091982731SJeremy L Thompson PETSC_DEFAULT, PETSC_DEFAULT)); 57191982731SJeremy L Thompson PetscCall(TSSetSolution(*ts, *Q)); 57291982731SJeremy L Thompson PetscCall(TSStep(*ts)); 57391982731SJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, 57491982731SJeremy L Thompson PETSC_DEFAULT, PETSC_DEFAULT)); 57591982731SJeremy L Thompson PetscCall(VecDestroy(&Q_preload)); 57691982731SJeremy L Thompson // LCOV_EXCL_STOP 57791982731SJeremy L Thompson } else { 578a515125bSLeila Ghaffari ierr = PetscBarrier((PetscObject) *ts); CHKERRQ(ierr); 579a515125bSLeila Ghaffari ierr = TSSolve(*ts, *Q); CHKERRQ(ierr); 58091982731SJeremy L Thompson } 58191982731SJeremy L Thompson PetscPreLoadEnd(); 58291982731SJeremy L Thompson 58391982731SJeremy L Thompson PetscCall(TSGetSolveTime(*ts, &final_time)); 584a515125bSLeila Ghaffari *f_time = final_time; 58591982731SJeremy L Thompson 5867538d537SJames Wright 587a515125bSLeila Ghaffari if (!app_ctx->test_mode) { 5887538d537SJames Wright if (user->app_ctx->output_freq > 0 || user->app_ctx->output_freq == -1) { 5897538d537SJames Wright PetscInt step_no; 5907538d537SJames Wright PetscCall(TSGetStepNumber(*ts, &step_no)); 5917538d537SJames Wright PetscCall(WriteOutput(user, *Q, step_no, final_time)); 5927538d537SJames Wright } 5937538d537SJames Wright 59491982731SJeremy L Thompson PetscLogEvent stage_id; 59591982731SJeremy L Thompson PetscStageLog stage_log; 59691982731SJeremy L Thompson 59791982731SJeremy L Thompson PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id)); 59891982731SJeremy L Thompson PetscCall(PetscLogGetStageLog(&stage_log)); 59991982731SJeremy L Thompson PetscCall(PetscPrintf(PETSC_COMM_WORLD, 600a515125bSLeila Ghaffari "Time taken for solution (sec): %g\n", 60191982731SJeremy L Thompson stage_log->stageInfo[stage_id].perfInfo.time)); 602a515125bSLeila Ghaffari } 603a515125bSLeila Ghaffari PetscFunctionReturn(0); 604a515125bSLeila Ghaffari } 605