1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3 // 4 // SPDX-License-Identifier: BSD-2-Clause 5 // 6 // This file is part of CEED: http://github.com/ceed 7 8 /// @file 9 /// Time-stepping functions for Navier-Stokes example using PETSc 10 11 #include <ceed.h> 12 #include <petscdmplex.h> 13 #include <petscts.h> 14 15 #include "../navierstokes.h" 16 #include "../qfunctions/newtonian_state.h" 17 18 // Compute mass matrix for explicit scheme 19 PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm, CeedData ceed_data, Vec M) { 20 Vec M_loc; 21 CeedQFunction qf_mass; 22 CeedOperator op_mass; 23 CeedVector m_ceed, ones_vec; 24 CeedInt num_comp_q, q_data_size; 25 PetscFunctionBeginUser; 26 27 // CEED Restriction 28 CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_q, &num_comp_q); 29 CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size); 30 CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &m_ceed, NULL); 31 CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &ones_vec, NULL); 32 CeedVectorSetValue(ones_vec, 1.0); 33 34 // CEED QFunction 35 PetscCall(CreateMassQFunction(ceed, num_comp_q, q_data_size, &qf_mass)); 36 37 // CEED Operator 38 CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass); 39 CeedOperatorSetField(op_mass, "u", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE); 40 CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data); 41 CeedOperatorSetField(op_mass, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE); 42 43 // Place PETSc vector in CEED vector 44 PetscMemType m_mem_type; 45 PetscCall(DMGetLocalVector(dm, &M_loc)); 46 PetscCall(VecP2C(M_loc, &m_mem_type, m_ceed)); 47 48 // Apply CEED Operator 49 CeedOperatorApply(op_mass, ones_vec, m_ceed, CEED_REQUEST_IMMEDIATE); 50 51 // Restore vectors 52 PetscCall(VecC2P(m_ceed, m_mem_type, M_loc)); 53 54 // Local-to-Global 55 PetscCall(VecZeroEntries(M)); 56 PetscCall(DMLocalToGlobal(dm, M_loc, ADD_VALUES, M)); 57 PetscCall(DMRestoreLocalVector(dm, &M_loc)); 58 59 // Invert diagonally lumped mass vector for RHS function 60 PetscCall(VecReciprocal(M)); 61 62 // Cleanup 63 CeedVectorDestroy(&ones_vec); 64 CeedVectorDestroy(&m_ceed); 65 CeedQFunctionDestroy(&qf_mass); 66 CeedOperatorDestroy(&op_mass); 67 68 PetscFunctionReturn(0); 69 } 70 71 // Insert Boundary values if it's a new time 72 PetscErrorCode UpdateBoundaryValues(User user, Vec Q_loc, PetscReal t) { 73 PetscFunctionBeginUser; 74 if (user->time_bc_set != t) { 75 PetscCall(DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL)); 76 user->time_bc_set = t; 77 } 78 PetscFunctionReturn(0); 79 } 80 81 // @brief Update the context label value to new value if necessary. 82 // @note This only supports labels with scalar label values (ie. not arrays) 83 PetscErrorCode UpdateContextLabel(MPI_Comm comm, PetscScalar update_value, CeedOperator op, CeedContextFieldLabel label) { 84 PetscScalar label_value; 85 86 PetscFunctionBeginUser; 87 PetscCheck(label, comm, PETSC_ERR_ARG_BADPTR, "Label should be non-NULL"); 88 89 { 90 size_t num_elements; 91 const PetscScalar *label_values; 92 CeedOperatorGetContextDoubleRead(op, label, &num_elements, &label_values); 93 PetscCheck(num_elements == 1, comm, PETSC_ERR_SUP, "%s does not support labels with more than 1 value. Label has %zu values", __func__, 94 num_elements); 95 label_value = *label_values; 96 CeedOperatorRestoreContextDoubleRead(op, label, &label_values); 97 } 98 99 if (label_value != update_value) { 100 CeedOperatorSetContextDouble(op, label, &update_value); 101 } 102 PetscFunctionReturn(0); 103 } 104 105 // RHS (Explicit time-stepper) function setup 106 // This is the RHS of the ODE, given as u_t = G(t,u) 107 // This function takes in a state vector Q and writes into G 108 PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data) { 109 User user = *(User *)user_data; 110 MPI_Comm comm = PetscObjectComm((PetscObject)ts); 111 PetscScalar dt; 112 Vec Q_loc = user->Q_loc, G_loc; 113 PetscMemType q_mem_type, g_mem_type; 114 PetscFunctionBeginUser; 115 116 // Get local vector 117 PetscCall(DMGetLocalVector(user->dm, &G_loc)); 118 119 // Update time dependent data 120 PetscCall(UpdateBoundaryValues(user, Q_loc, t)); 121 if (user->phys->solution_time_label) PetscCall(UpdateContextLabel(comm, t, user->op_rhs, user->phys->solution_time_label)); 122 PetscCall(TSGetTimeStep(ts, &dt)); 123 if (user->phys->timestep_size_label) PetscCall(UpdateContextLabel(comm, dt, user->op_rhs, user->phys->timestep_size_label)); 124 125 // Global-to-local 126 PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc)); 127 128 // Place PETSc vectors in CEED vectors 129 PetscCall(VecReadP2C(Q_loc, &q_mem_type, user->q_ceed)); 130 PetscCall(VecP2C(G_loc, &g_mem_type, user->g_ceed)); 131 132 // Apply CEED operator 133 CeedOperatorApply(user->op_rhs, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE); 134 135 // Restore vectors 136 PetscCall(VecReadC2P(user->q_ceed, q_mem_type, Q_loc)); 137 PetscCall(VecC2P(user->g_ceed, g_mem_type, G_loc)); 138 139 // Local-to-Global 140 PetscCall(VecZeroEntries(G)); 141 PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G)); 142 143 // Inverse of the lumped mass matrix (M is Minv) 144 PetscCall(VecPointwiseMult(G, G, user->M)); 145 146 // Restore vectors 147 PetscCall(DMRestoreLocalVector(user->dm, &G_loc)); 148 149 PetscFunctionReturn(0); 150 } 151 152 // Surface forces function setup 153 static PetscErrorCode Surface_Forces_NS(DM dm, Vec G_loc, PetscInt num_walls, const PetscInt walls[], PetscScalar *reaction_force) { 154 DMLabel face_label; 155 const PetscScalar *g; 156 PetscInt dof, dim = 3; 157 MPI_Comm comm; 158 PetscSection s; 159 160 PetscFunctionBeginUser; 161 PetscCall(PetscArrayzero(reaction_force, num_walls * dim)); 162 PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 163 PetscCall(DMGetLabel(dm, "Face Sets", &face_label)); 164 PetscCall(VecGetArrayRead(G_loc, &g)); 165 for (PetscInt w = 0; w < num_walls; w++) { 166 const PetscInt wall = walls[w]; 167 IS wall_is; 168 PetscCall(DMGetLocalSection(dm, &s)); 169 PetscCall(DMLabelGetStratumIS(face_label, wall, &wall_is)); 170 if (wall_is) { // There exist such points on this process 171 PetscInt num_points; 172 PetscInt num_comp = 0; 173 const PetscInt *points; 174 PetscCall(PetscSectionGetFieldComponents(s, 0, &num_comp)); 175 PetscCall(ISGetSize(wall_is, &num_points)); 176 PetscCall(ISGetIndices(wall_is, &points)); 177 for (PetscInt i = 0; i < num_points; i++) { 178 const PetscInt p = points[i]; 179 const StateConservative *r; 180 PetscCall(DMPlexPointLocalRead(dm, p, g, &r)); 181 PetscCall(PetscSectionGetDof(s, p, &dof)); 182 for (PetscInt node = 0; node < dof / num_comp; node++) { 183 for (PetscInt j = 0; j < 3; j++) { 184 reaction_force[w * dim + j] -= r[node].momentum[j]; 185 } 186 } 187 } 188 PetscCall(ISRestoreIndices(wall_is, &points)); 189 } 190 PetscCall(ISDestroy(&wall_is)); 191 } 192 PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, reaction_force, dim * num_walls, MPIU_SCALAR, MPI_SUM, comm)); 193 // Restore Vectors 194 PetscCall(VecRestoreArrayRead(G_loc, &g)); 195 196 PetscFunctionReturn(0); 197 } 198 199 // Implicit time-stepper function setup 200 PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data) { 201 User user = *(User *)user_data; 202 MPI_Comm comm = PetscObjectComm((PetscObject)ts); 203 PetscScalar dt; 204 Vec Q_loc = user->Q_loc, Q_dot_loc = user->Q_dot_loc, G_loc; 205 PetscMemType q_mem_type, q_dot_mem_type, g_mem_type; 206 PetscFunctionBeginUser; 207 208 // Get local vectors 209 PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 210 211 // Update time dependent data 212 PetscCall(UpdateBoundaryValues(user, Q_loc, t)); 213 if (user->phys->solution_time_label) PetscCall(UpdateContextLabel(comm, t, user->op_ifunction, user->phys->solution_time_label)); 214 PetscCall(TSGetTimeStep(ts, &dt)); 215 if (user->phys->timestep_size_label) PetscCall(UpdateContextLabel(comm, dt, user->op_ifunction, user->phys->timestep_size_label)); 216 217 // Global-to-local 218 PetscCall(DMGlobalToLocalBegin(user->dm, Q, INSERT_VALUES, Q_loc)); 219 PetscCall(DMGlobalToLocalBegin(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc)); 220 PetscCall(DMGlobalToLocalEnd(user->dm, Q, INSERT_VALUES, Q_loc)); 221 PetscCall(DMGlobalToLocalEnd(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc)); 222 223 // Place PETSc vectors in CEED vectors 224 PetscCall(VecReadP2C(Q_loc, &q_mem_type, user->q_ceed)); 225 PetscCall(VecReadP2C(Q_dot_loc, &q_dot_mem_type, user->q_dot_ceed)); 226 PetscCall(VecP2C(G_loc, &g_mem_type, user->g_ceed)); 227 228 // Apply CEED operator 229 CeedOperatorApply(user->op_ifunction, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE); 230 231 // Restore vectors 232 PetscCall(VecReadC2P(user->q_ceed, q_mem_type, Q_loc)); 233 PetscCall(VecReadC2P(user->q_dot_ceed, q_dot_mem_type, Q_dot_loc)); 234 PetscCall(VecC2P(user->g_ceed, g_mem_type, G_loc)); 235 236 if (user->app_ctx->sgs_model_type == SGS_MODEL_DATA_DRIVEN) { 237 PetscCall(SGS_DD_ModelApplyIFunction(user, Q_loc, G_loc)); 238 } 239 240 // Local-to-Global 241 PetscCall(VecZeroEntries(G)); 242 PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G)); 243 244 // Restore vectors 245 PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 246 247 PetscFunctionReturn(0); 248 } 249 250 static PetscErrorCode FormPreallocation(User user, PetscBool pbdiagonal, Mat J, CeedVector *coo_values) { 251 PetscCount ncoo; 252 PetscInt *rows, *cols; 253 254 PetscFunctionBeginUser; 255 if (pbdiagonal) { 256 CeedSize l_size; 257 CeedOperatorGetActiveVectorLengths(user->op_ijacobian, &l_size, NULL); 258 ncoo = l_size * 5; 259 rows = malloc(ncoo * sizeof(rows[0])); 260 cols = malloc(ncoo * sizeof(cols[0])); 261 for (PetscCount n = 0; n < l_size / 5; n++) { 262 for (PetscInt i = 0; i < 5; i++) { 263 for (PetscInt j = 0; j < 5; j++) { 264 rows[(n * 5 + i) * 5 + j] = n * 5 + i; 265 cols[(n * 5 + i) * 5 + j] = n * 5 + j; 266 } 267 } 268 } 269 } else { 270 PetscCall(CeedOperatorLinearAssembleSymbolic(user->op_ijacobian, &ncoo, &rows, &cols)); 271 } 272 PetscCall(MatSetPreallocationCOOLocal(J, ncoo, rows, cols)); 273 free(rows); 274 free(cols); 275 CeedVectorCreate(user->ceed, ncoo, coo_values); 276 PetscFunctionReturn(0); 277 } 278 279 static PetscErrorCode FormSetValues(User user, PetscBool pbdiagonal, Mat J, CeedVector coo_values) { 280 CeedMemType mem_type = CEED_MEM_HOST; 281 const PetscScalar *values; 282 MatType mat_type; 283 284 PetscFunctionBeginUser; 285 PetscCall(MatGetType(J, &mat_type)); 286 if (strstr(mat_type, "kokkos") || strstr(mat_type, "cusparse")) mem_type = CEED_MEM_DEVICE; 287 if (user->app_ctx->pmat_pbdiagonal) { 288 CeedOperatorLinearAssemblePointBlockDiagonal(user->op_ijacobian, coo_values, CEED_REQUEST_IMMEDIATE); 289 } else { 290 CeedOperatorLinearAssemble(user->op_ijacobian, coo_values); 291 } 292 CeedVectorGetArrayRead(coo_values, mem_type, &values); 293 PetscCall(MatSetValuesCOO(J, values, INSERT_VALUES)); 294 CeedVectorRestoreArrayRead(coo_values, &values); 295 PetscFunctionReturn(0); 296 } 297 298 PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, PetscReal shift, Mat J, Mat J_pre, void *user_data) { 299 User user = *(User *)user_data; 300 PetscBool J_is_shell, J_is_mffd, J_pre_is_shell; 301 PetscFunctionBeginUser; 302 if (user->phys->ijacobian_time_shift_label) CeedOperatorSetContextDouble(user->op_ijacobian, user->phys->ijacobian_time_shift_label, &shift); 303 PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd)); 304 PetscCall(PetscObjectTypeCompare((PetscObject)J, MATSHELL, &J_is_shell)); 305 PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATSHELL, &J_pre_is_shell)); 306 if (!user->matrices_set_up) { 307 if (J_is_shell) { 308 OperatorApplyContext op_ijacobian_ctx; 309 OperatorApplyContextCreate(user->dm, user->dm, user->ceed, user->op_ijacobian, user->q_ceed, user->g_ceed, user->Q_dot_loc, NULL, 310 &op_ijacobian_ctx); 311 PetscCall(MatShellSetContext(J, op_ijacobian_ctx)); 312 PetscCall(MatShellSetContextDestroy(J, (PetscErrorCode(*)(void *))OperatorApplyContextDestroy)); 313 PetscCall(MatShellSetOperation(J, MATOP_MULT, (void (*)(void))MatMult_Ceed)); 314 PetscCall(MatShellSetOperation(J, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiag_Ceed)); 315 PetscCall(MatSetUp(J)); 316 } 317 if (!J_pre_is_shell) { 318 PetscCall(FormPreallocation(user, user->app_ctx->pmat_pbdiagonal, J_pre, &user->coo_values_pmat)); 319 } 320 if (J != J_pre && !J_is_shell && !J_is_mffd) { 321 PetscCall(FormPreallocation(user, PETSC_FALSE, J, &user->coo_values_amat)); 322 } 323 user->matrices_set_up = true; 324 } 325 if (!J_pre_is_shell) { 326 PetscCall(FormSetValues(user, user->app_ctx->pmat_pbdiagonal, J_pre, user->coo_values_pmat)); 327 } 328 if (user->coo_values_amat) { 329 PetscCall(FormSetValues(user, PETSC_FALSE, J, user->coo_values_amat)); 330 } else if (J_is_mffd) { 331 PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 332 PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 333 } 334 PetscFunctionReturn(0); 335 } 336 337 PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, PetscScalar time) { 338 Vec Q_loc; 339 char file_path[PETSC_MAX_PATH_LEN]; 340 PetscViewer viewer; 341 PetscFunctionBeginUser; 342 343 if (user->app_ctx->checkpoint_vtk) { 344 // Set up output 345 PetscCall(DMGetLocalVector(user->dm, &Q_loc)); 346 PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec")); 347 PetscCall(VecZeroEntries(Q_loc)); 348 PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc)); 349 350 // Output 351 PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 352 353 PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer)); 354 PetscCall(VecView(Q_loc, viewer)); 355 PetscCall(PetscViewerDestroy(&viewer)); 356 if (user->dm_viz) { 357 Vec Q_refined, Q_refined_loc; 358 char file_path_refined[PETSC_MAX_PATH_LEN]; 359 PetscViewer viewer_refined; 360 361 PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined)); 362 PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc)); 363 PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined")); 364 365 PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined)); 366 PetscCall(VecZeroEntries(Q_refined_loc)); 367 PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc)); 368 369 PetscCall( 370 PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 371 372 PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined)); 373 PetscCall(VecView(Q_refined_loc, viewer_refined)); 374 PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc)); 375 PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined)); 376 PetscCall(PetscViewerDestroy(&viewer_refined)); 377 } 378 PetscCall(DMRestoreLocalVector(user->dm, &Q_loc)); 379 } 380 381 // Save data in a binary file for continuation of simulations 382 if (user->app_ctx->add_stepnum2bin) { 383 PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", user->app_ctx->output_dir, step_no)); 384 } else { 385 PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", user->app_ctx->output_dir)); 386 } 387 PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, &viewer)); 388 389 PetscInt token = FLUIDS_FILE_TOKEN; 390 PetscCall(PetscViewerBinaryWrite(viewer, &token, 1, PETSC_INT)); 391 PetscCall(PetscViewerBinaryWrite(viewer, &step_no, 1, PETSC_INT)); 392 time /= user->units->second; // Dimensionalize time back 393 PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL)); 394 PetscCall(VecView(Q, viewer)); 395 PetscCall(PetscViewerDestroy(&viewer)); 396 PetscFunctionReturn(0); 397 } 398 399 // CSV Monitor 400 PetscErrorCode TSMonitor_WallForce(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 401 User user = ctx; 402 Vec G_loc; 403 PetscInt num_wall = user->app_ctx->wall_forces.num_wall, dim = 3; 404 const PetscInt *walls = user->app_ctx->wall_forces.walls; 405 PetscViewer viewer = user->app_ctx->wall_forces.viewer; 406 PetscViewerFormat format = user->app_ctx->wall_forces.viewer_format; 407 PetscScalar *reaction_force; 408 PetscBool iascii; 409 410 PetscFunctionBeginUser; 411 if (!viewer) PetscFunctionReturn(0); 412 PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 413 PetscCall(PetscMalloc1(num_wall * dim, &reaction_force)); 414 PetscCall(Surface_Forces_NS(user->dm, G_loc, num_wall, walls, reaction_force)); 415 PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 416 417 PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 418 419 if (iascii) { 420 if (format == PETSC_VIEWER_ASCII_CSV && !user->app_ctx->wall_forces.header_written) { 421 PetscCall(PetscViewerASCIIPrintf(viewer, "Step,Time,Wall,ForceX,ForceY,ForceZ\n")); 422 user->app_ctx->wall_forces.header_written = PETSC_TRUE; 423 } 424 for (PetscInt w = 0; w < num_wall; w++) { 425 PetscInt wall = walls[w]; 426 if (format == PETSC_VIEWER_ASCII_CSV) { 427 PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ",%g,%" PetscInt_FMT ",%g,%g,%g\n", step_no, time, wall, 428 reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2])); 429 430 } else { 431 PetscCall(PetscViewerASCIIPrintf(viewer, "Wall %" PetscInt_FMT " Forces: Force_x = %12g, Force_y = %12g, Force_z = %12g\n", wall, 432 reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2])); 433 } 434 } 435 } 436 PetscCall(PetscFree(reaction_force)); 437 PetscFunctionReturn(0); 438 } 439 440 // User provided TS Monitor 441 PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 442 User user = ctx; 443 PetscFunctionBeginUser; 444 445 // Print every 'checkpoint_interval' steps 446 if (user->app_ctx->checkpoint_interval <= 0 || step_no % user->app_ctx->checkpoint_interval != 0 || 447 (user->app_ctx->cont_steps == step_no && step_no != 0)) { 448 PetscFunctionReturn(0); 449 } 450 451 PetscCall(WriteOutput(user, Q, step_no, time)); 452 453 PetscFunctionReturn(0); 454 } 455 456 // TS: Create, setup, and solve 457 PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, Vec *Q, PetscScalar *f_time, TS *ts) { 458 MPI_Comm comm = user->comm; 459 TSAdapt adapt; 460 PetscScalar final_time; 461 PetscFunctionBeginUser; 462 463 PetscCall(TSCreate(comm, ts)); 464 PetscCall(TSSetDM(*ts, dm)); 465 if (phys->implicit) { 466 PetscCall(TSSetType(*ts, TSBDF)); 467 if (user->op_ifunction) { 468 PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &user)); 469 } else { // Implicit integrators can fall back to using an RHSFunction 470 PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 471 } 472 if (user->op_ijacobian) { 473 PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &user)); 474 if (app_ctx->amat_type) { 475 Mat Pmat, Amat; 476 PetscCall(DMCreateMatrix(dm, &Pmat)); 477 PetscCall(DMSetMatType(dm, app_ctx->amat_type)); 478 PetscCall(DMCreateMatrix(dm, &Amat)); 479 PetscCall(TSSetIJacobian(*ts, Amat, Pmat, NULL, NULL)); 480 PetscCall(MatDestroy(&Amat)); 481 PetscCall(MatDestroy(&Pmat)); 482 } 483 } 484 } else { 485 PetscCheck(user->op_rhs, comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction"); 486 PetscCall(TSSetType(*ts, TSRK)); 487 PetscCall(TSRKSetType(*ts, TSRK5F)); 488 PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 489 } 490 PetscCall(TSSetMaxTime(*ts, 500. * user->units->second)); 491 PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER)); 492 if (app_ctx->test_type == TESTTYPE_NONE) PetscCall(TSSetErrorIfStepFails(*ts, PETSC_FALSE)); 493 PetscCall(TSSetTimeStep(*ts, 1.e-2 * user->units->second)); 494 if (app_ctx->test_type != TESTTYPE_NONE) { 495 PetscCall(TSSetMaxSteps(*ts, 10)); 496 } 497 PetscCall(TSGetAdapt(*ts, &adapt)); 498 PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * user->units->second, 1.e2 * user->units->second)); 499 PetscCall(TSSetFromOptions(*ts)); 500 user->time_bc_set = -1.0; // require all BCs be updated 501 if (!app_ctx->cont_steps) { // print initial condition 502 if (app_ctx->test_type == TESTTYPE_NONE) { 503 PetscCall(TSMonitor_NS(*ts, 0, 0., *Q, user)); 504 } 505 } else { // continue from time of last output 506 PetscInt count; 507 PetscViewer viewer; 508 509 if (app_ctx->cont_time <= 0) { // Legacy files did not include step number and time 510 PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer)); 511 PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL)); 512 PetscCall(PetscViewerDestroy(&viewer)); 513 PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP, 514 "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)"); 515 } 516 PetscCall(TSSetTime(*ts, app_ctx->cont_time * user->units->second)); 517 PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps)); 518 } 519 if (app_ctx->test_type == TESTTYPE_NONE) { 520 PetscCall(TSMonitorSet(*ts, TSMonitor_NS, user, NULL)); 521 } 522 if (app_ctx->wall_forces.viewer) { 523 PetscCall(TSMonitorSet(*ts, TSMonitor_WallForce, user, NULL)); 524 } 525 if (app_ctx->turb_spanstats_enable) { 526 PetscCall(TSMonitorSet(*ts, TSMonitor_Statistics, user, NULL)); 527 CeedScalar previous_time = app_ctx->cont_time * user->units->second; 528 CeedOperatorSetContextDouble(user->spanstats.op_stats_collect_ctx->op, user->spanstats.previous_time_label, &previous_time); 529 } 530 531 // Solve 532 PetscReal start_time; 533 PetscInt start_step; 534 PetscCall(TSGetTime(*ts, &start_time)); 535 PetscCall(TSGetStepNumber(*ts, &start_step)); 536 537 PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve"); 538 PetscCall(TSSetTime(*ts, start_time)); 539 PetscCall(TSSetStepNumber(*ts, start_step)); 540 if (PetscPreLoadingOn) { 541 // LCOV_EXCL_START 542 SNES snes; 543 Vec Q_preload; 544 PetscReal rtol; 545 PetscCall(VecDuplicate(*Q, &Q_preload)); 546 PetscCall(VecCopy(*Q, Q_preload)); 547 PetscCall(TSGetSNES(*ts, &snes)); 548 PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL)); 549 PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 550 PetscCall(TSSetSolution(*ts, *Q)); 551 PetscCall(TSStep(*ts)); 552 PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 553 PetscCall(VecDestroy(&Q_preload)); 554 // LCOV_EXCL_STOP 555 } else { 556 PetscCall(PetscBarrier((PetscObject)*ts)); 557 PetscCall(TSSolve(*ts, *Q)); 558 } 559 PetscPreLoadEnd(); 560 561 PetscCall(TSGetSolveTime(*ts, &final_time)); 562 *f_time = final_time; 563 564 if (app_ctx->test_type == TESTTYPE_NONE) { 565 PetscInt step_no; 566 PetscCall(TSGetStepNumber(*ts, &step_no)); 567 if (user->app_ctx->checkpoint_interval > 0 || user->app_ctx->checkpoint_interval == -1) { 568 PetscCall(WriteOutput(user, *Q, step_no, final_time)); 569 } 570 571 PetscLogEvent stage_id; 572 PetscStageLog stage_log; 573 574 PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id)); 575 PetscCall(PetscLogGetStageLog(&stage_log)); 576 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_log->stageInfo[stage_id].perfInfo.time)); 577 } 578 PetscFunctionReturn(0); 579 } 580