15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 377841947SLeila Ghaffari // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 577841947SLeila Ghaffari // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 777841947SLeila Ghaffari 877841947SLeila Ghaffari /// @file 977841947SLeila Ghaffari /// Time-stepping functions for Navier-Stokes example using PETSc 1077841947SLeila Ghaffari 1149aac155SJeremy L Thompson #include <ceed.h> 1249aac155SJeremy L Thompson #include <petscdmplex.h> 1349aac155SJeremy L Thompson #include <petscts.h> 1449aac155SJeremy L Thompson 1577841947SLeila Ghaffari #include "../navierstokes.h" 16ca69d878SAdeleke O. Bankole #include "../qfunctions/newtonian_state.h" 1777841947SLeila Ghaffari 18a0b9a424SJames Wright // Insert Boundary values if it's a new time 19a0b9a424SJames Wright PetscErrorCode UpdateBoundaryValues(User user, Vec Q_loc, PetscReal t) { 20a0b9a424SJames Wright PetscFunctionBeginUser; 21a0b9a424SJames Wright if (user->time_bc_set != t) { 22a0b9a424SJames Wright PetscCall(DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL)); 23a0b9a424SJames Wright user->time_bc_set = t; 24a0b9a424SJames Wright } 25ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 26a0b9a424SJames Wright } 27a0b9a424SJames Wright 2877841947SLeila Ghaffari // RHS (Explicit time-stepper) function setup 2977841947SLeila Ghaffari // This is the RHS of the ODE, given as u_t = G(t,u) 3077841947SLeila Ghaffari // This function takes in a state vector Q and writes into G 3177841947SLeila Ghaffari PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data) { 3277841947SLeila Ghaffari User user = *(User *)user_data; 337d4c6defSJames Wright Ceed ceed = user->ceed; 34c798d55aSJames Wright PetscScalar dt; 359ad5e8e4SJames Wright Vec Q_loc = user->Q_loc; 36b4e9a8f8SJames Wright PetscMemType q_mem_type; 3777841947SLeila Ghaffari 38f17d818dSJames Wright PetscFunctionBeginUser; 395e82a6e1SJeremy L Thompson // Update time dependent data 40a0b9a424SJames Wright PetscCall(UpdateBoundaryValues(user, Q_loc, t)); 417d4c6defSJames Wright if (user->phys->solution_time_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_rhs_ctx->op, user->phys->solution_time_label, &t)); 422b730f8bSJeremy L Thompson PetscCall(TSGetTimeStep(ts, &dt)); 437d4c6defSJames Wright if (user->phys->timestep_size_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_rhs_ctx->op, user->phys->timestep_size_label, &dt)); 4477841947SLeila Ghaffari 459ad5e8e4SJames Wright PetscCall(ApplyCeedOperatorGlobalToGlobal(Q, G, user->op_rhs_ctx)); 4677841947SLeila Ghaffari 47b4e9a8f8SJames Wright PetscCall(VecReadPetscToCeed(Q_loc, &q_mem_type, user->q_ceed)); 48b4e9a8f8SJames Wright 49b4e9a8f8SJames Wright // Inverse of the mass matrix 5004292f7dSJames Wright PetscCall(KSPSolve(user->mass_ksp, G, G)); 51b4e9a8f8SJames Wright 52b4e9a8f8SJames Wright PetscCall(VecReadCeedToPetsc(user->q_ceed, q_mem_type, Q_loc)); 53ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 5477841947SLeila Ghaffari } 5577841947SLeila Ghaffari 56ca69d878SAdeleke O. Bankole // Surface forces function setup 57ca69d878SAdeleke O. Bankole static PetscErrorCode Surface_Forces_NS(DM dm, Vec G_loc, PetscInt num_walls, const PetscInt walls[], PetscScalar *reaction_force) { 58ca69d878SAdeleke O. Bankole DMLabel face_label; 59ca69d878SAdeleke O. Bankole const PetscScalar *g; 60d6734f85SAdeleke O. Bankole PetscInt dof, dim = 3; 61ca69d878SAdeleke O. Bankole MPI_Comm comm; 62d6734f85SAdeleke O. Bankole PetscSection s; 63ca69d878SAdeleke O. Bankole 64ca69d878SAdeleke O. Bankole PetscFunctionBeginUser; 65ca69d878SAdeleke O. Bankole PetscCall(PetscArrayzero(reaction_force, num_walls * dim)); 66ca69d878SAdeleke O. Bankole PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 67ca69d878SAdeleke O. Bankole PetscCall(DMGetLabel(dm, "Face Sets", &face_label)); 68ca69d878SAdeleke O. Bankole PetscCall(VecGetArrayRead(G_loc, &g)); 69ca69d878SAdeleke O. Bankole for (PetscInt w = 0; w < num_walls; w++) { 70ca69d878SAdeleke O. Bankole const PetscInt wall = walls[w]; 71ca69d878SAdeleke O. Bankole IS wall_is; 72d6734f85SAdeleke O. Bankole PetscCall(DMGetLocalSection(dm, &s)); 73ca69d878SAdeleke O. Bankole PetscCall(DMLabelGetStratumIS(face_label, wall, &wall_is)); 74ca69d878SAdeleke O. Bankole if (wall_is) { // There exist such points on this process 75ca69d878SAdeleke O. Bankole PetscInt num_points; 76d6734f85SAdeleke O. Bankole PetscInt num_comp = 0; 77ca69d878SAdeleke O. Bankole const PetscInt *points; 78d6734f85SAdeleke O. Bankole PetscCall(PetscSectionGetFieldComponents(s, 0, &num_comp)); 79ca69d878SAdeleke O. Bankole PetscCall(ISGetSize(wall_is, &num_points)); 80ca69d878SAdeleke O. Bankole PetscCall(ISGetIndices(wall_is, &points)); 81ca69d878SAdeleke O. Bankole for (PetscInt i = 0; i < num_points; i++) { 82ca69d878SAdeleke O. Bankole const PetscInt p = points[i]; 83ca69d878SAdeleke O. Bankole const StateConservative *r; 84ca69d878SAdeleke O. Bankole PetscCall(DMPlexPointLocalRead(dm, p, g, &r)); 85d6734f85SAdeleke O. Bankole PetscCall(PetscSectionGetDof(s, p, &dof)); 86d6734f85SAdeleke O. Bankole for (PetscInt node = 0; node < dof / num_comp; node++) { 87ca69d878SAdeleke O. Bankole for (PetscInt j = 0; j < 3; j++) { 88d6734f85SAdeleke O. Bankole reaction_force[w * dim + j] -= r[node].momentum[j]; 89d6734f85SAdeleke O. Bankole } 90ca69d878SAdeleke O. Bankole } 91ca69d878SAdeleke O. Bankole } 92ca69d878SAdeleke O. Bankole PetscCall(ISRestoreIndices(wall_is, &points)); 93ca69d878SAdeleke O. Bankole } 94ca69d878SAdeleke O. Bankole PetscCall(ISDestroy(&wall_is)); 95ca69d878SAdeleke O. Bankole } 96ca69d878SAdeleke O. Bankole PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, reaction_force, dim * num_walls, MPIU_SCALAR, MPI_SUM, comm)); 97ca69d878SAdeleke O. Bankole // Restore Vectors 98ca69d878SAdeleke O. Bankole PetscCall(VecRestoreArrayRead(G_loc, &g)); 99ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 100ca69d878SAdeleke O. Bankole } 101ca69d878SAdeleke O. Bankole 10277841947SLeila Ghaffari // Implicit time-stepper function setup 1032b730f8bSJeremy L Thompson PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data) { 10477841947SLeila Ghaffari User user = *(User *)user_data; 1057d4c6defSJames Wright Ceed ceed = user->ceed; 106c798d55aSJames Wright PetscScalar dt; 1075e82a6e1SJeremy L Thompson Vec Q_loc = user->Q_loc, Q_dot_loc = user->Q_dot_loc, G_loc; 10877841947SLeila Ghaffari PetscMemType q_mem_type, q_dot_mem_type, g_mem_type; 10977841947SLeila Ghaffari 110f17d818dSJames Wright PetscFunctionBeginUser; 1115e82a6e1SJeremy L Thompson // Get local vectors 112ca69d878SAdeleke O. Bankole PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 1135e82a6e1SJeremy L Thompson 1145e82a6e1SJeremy L Thompson // Update time dependent data 115a0b9a424SJames Wright PetscCall(UpdateBoundaryValues(user, Q_loc, t)); 1167d4c6defSJames Wright if (user->phys->solution_time_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_ifunction, user->phys->solution_time_label, &t)); 1172b730f8bSJeremy L Thompson PetscCall(TSGetTimeStep(ts, &dt)); 1187d4c6defSJames Wright if (user->phys->timestep_size_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_ifunction, user->phys->timestep_size_label, &dt)); 11977841947SLeila Ghaffari 12077841947SLeila Ghaffari // Global-to-local 121878eb0e7SJames Wright PetscCall(DMGlobalToLocalBegin(user->dm, Q, INSERT_VALUES, Q_loc)); 122878eb0e7SJames Wright PetscCall(DMGlobalToLocalBegin(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc)); 123878eb0e7SJames Wright PetscCall(DMGlobalToLocalEnd(user->dm, Q, INSERT_VALUES, Q_loc)); 124878eb0e7SJames Wright PetscCall(DMGlobalToLocalEnd(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc)); 12577841947SLeila Ghaffari 12677841947SLeila Ghaffari // Place PETSc vectors in CEED vectors 127d0593705SJames Wright PetscCall(VecReadPetscToCeed(Q_loc, &q_mem_type, user->q_ceed)); 128d0593705SJames Wright PetscCall(VecReadPetscToCeed(Q_dot_loc, &q_dot_mem_type, user->q_dot_ceed)); 129d0593705SJames Wright PetscCall(VecPetscToCeed(G_loc, &g_mem_type, user->g_ceed)); 13077841947SLeila Ghaffari 13177841947SLeila Ghaffari // Apply CEED operator 13275d1798cSJames Wright PetscCall(PetscLogEventBegin(FLUIDS_CeedOperatorApply, Q, G, 0, 0)); 13375d1798cSJames Wright PetscCall(PetscLogGpuTimeBegin()); 134a424bcd0SJames Wright PetscCallCeed(user->ceed, CeedOperatorApply(user->op_ifunction, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE)); 13575d1798cSJames Wright PetscCall(PetscLogGpuTimeEnd()); 13675d1798cSJames Wright PetscCall(PetscLogEventEnd(FLUIDS_CeedOperatorApply, Q, G, 0, 0)); 13777841947SLeila Ghaffari 13877841947SLeila Ghaffari // Restore vectors 139d0593705SJames Wright PetscCall(VecReadCeedToPetsc(user->q_ceed, q_mem_type, Q_loc)); 140d0593705SJames Wright PetscCall(VecReadCeedToPetsc(user->q_dot_ceed, q_dot_mem_type, Q_dot_loc)); 141d0593705SJames Wright PetscCall(VecCeedToPetsc(user->g_ceed, g_mem_type, G_loc)); 14277841947SLeila Ghaffari 14319ffbc25SJames Wright if (user->app_ctx->sgs_model_type == SGS_MODEL_DATA_DRIVEN) { 1448f5ab23bSJames Wright PetscCall(SgsDDApplyIFunction(user, Q_loc, G_loc)); 14519ffbc25SJames Wright } 146be34b3b7SJames Wright 14777841947SLeila Ghaffari // Local-to-Global 1482b730f8bSJeremy L Thompson PetscCall(VecZeroEntries(G)); 1492b730f8bSJeremy L Thompson PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G)); 15077841947SLeila Ghaffari 15177841947SLeila Ghaffari // Restore vectors 152ca69d878SAdeleke O. Bankole PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 153ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 15477841947SLeila Ghaffari } 15577841947SLeila Ghaffari 1562b730f8bSJeremy L Thompson PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, PetscReal shift, Mat J, Mat J_pre, void *user_data) { 157e334ad8fSJed Brown User user = *(User *)user_data; 15891c97f41SJames Wright PetscBool J_is_matceed, J_is_mffd, J_pre_is_matceed, J_pre_is_mffd; 159f17d818dSJames Wright 160e334ad8fSJed Brown PetscFunctionBeginUser; 161d6bf345cSJed Brown PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd)); 16291c97f41SJames Wright PetscCall(PetscObjectTypeCompare((PetscObject)J, MATCEED, &J_is_matceed)); 16391c97f41SJames Wright PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATMFFD, &J_pre_is_mffd)); 16491c97f41SJames Wright PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATCEED, &J_pre_is_matceed)); 16591c97f41SJames Wright 166*f965f5c6SJames Wright PetscCall(MatCeedSetContextReal(user->mat_ijacobian, "ijacobian time shift", shift)); 16791c97f41SJames Wright 16891c97f41SJames Wright if (J_is_matceed || J_is_mffd) { 169d6bf345cSJed Brown PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 170d6bf345cSJed Brown PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 17191c97f41SJames Wright } else PetscCall(MatCeedAssembleCOO(user->mat_ijacobian, J)); 17291c97f41SJames Wright 17391c97f41SJames Wright if (J_pre_is_matceed && J != J_pre) { 17491c97f41SJames Wright PetscCall(MatAssemblyBegin(J_pre, MAT_FINAL_ASSEMBLY)); 17591c97f41SJames Wright PetscCall(MatAssemblyEnd(J_pre, MAT_FINAL_ASSEMBLY)); 17691c97f41SJames Wright } else if (!J_pre_is_matceed && !J_pre_is_mffd && J != J_pre) { 17791c97f41SJames Wright PetscCall(MatCeedAssembleCOO(user->mat_ijacobian, J_pre)); 178d6bf345cSJed Brown } 179ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 180e334ad8fSJed Brown } 181e334ad8fSJed Brown 1822b730f8bSJeremy L Thompson PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, PetscScalar time) { 18377841947SLeila Ghaffari Vec Q_loc; 18477841947SLeila Ghaffari char file_path[PETSC_MAX_PATH_LEN]; 18577841947SLeila Ghaffari PetscViewer viewer; 18677841947SLeila Ghaffari 187f17d818dSJames Wright PetscFunctionBeginUser; 18837cbb16aSJed Brown if (user->app_ctx->checkpoint_vtk) { 18977841947SLeila Ghaffari // Set up output 190f14660a4SJames Wright PetscCall(DMGetLocalVector(user->dm, &Q_loc)); 191f14660a4SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec")); 192f14660a4SJames Wright PetscCall(VecZeroEntries(Q_loc)); 193f14660a4SJames Wright PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc)); 19477841947SLeila Ghaffari 19577841947SLeila Ghaffari // Output 19637cbb16aSJed Brown PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 197f14660a4SJames Wright 1982b730f8bSJeremy L Thompson PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer)); 199f14660a4SJames Wright PetscCall(VecView(Q_loc, viewer)); 200f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 20177841947SLeila Ghaffari if (user->dm_viz) { 20277841947SLeila Ghaffari Vec Q_refined, Q_refined_loc; 20377841947SLeila Ghaffari char file_path_refined[PETSC_MAX_PATH_LEN]; 20477841947SLeila Ghaffari PetscViewer viewer_refined; 20577841947SLeila Ghaffari 206f14660a4SJames Wright PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined)); 207f14660a4SJames Wright PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc)); 208f14660a4SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined")); 209f14660a4SJames Wright 210f14660a4SJames Wright PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined)); 211f14660a4SJames Wright PetscCall(VecZeroEntries(Q_refined_loc)); 2122b730f8bSJeremy L Thompson PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc)); 213f14660a4SJames Wright 21437cbb16aSJed Brown PetscCall( 21537cbb16aSJed Brown PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 216f14660a4SJames Wright 2172b730f8bSJeremy L Thompson PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined)); 218f14660a4SJames Wright PetscCall(VecView(Q_refined_loc, viewer_refined)); 219f14660a4SJames Wright PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc)); 220f14660a4SJames Wright PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined)); 221f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer_refined)); 22277841947SLeila Ghaffari } 223f14660a4SJames Wright PetscCall(DMRestoreLocalVector(user->dm, &Q_loc)); 22437cbb16aSJed Brown } 22577841947SLeila Ghaffari 22677841947SLeila Ghaffari // Save data in a binary file for continuation of simulations 22769293791SJames Wright if (user->app_ctx->add_stepnum2bin) { 22837cbb16aSJed Brown PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", user->app_ctx->output_dir, step_no)); 22969293791SJames Wright } else { 2302b730f8bSJeremy L Thompson PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", user->app_ctx->output_dir)); 23169293791SJames Wright } 2322b730f8bSJeremy L Thompson PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, &viewer)); 233f14660a4SJames Wright 2340de6a49fSJames Wright PetscInt32 token = PetscDefined(USE_64BIT_INDICES) ? FLUIDS_FILE_TOKEN_64 : FLUIDS_FILE_TOKEN_32; 2350de6a49fSJames Wright PetscCall(PetscViewerBinaryWrite(viewer, &token, 1, PETSC_INT32)); 2364de8550aSJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &step_no, 1, PETSC_INT)); 2374de8550aSJed Brown time /= user->units->second; // Dimensionalize time back 2384de8550aSJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL)); 239f14660a4SJames Wright PetscCall(VecView(Q, viewer)); 240f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 241ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 242f14660a4SJames Wright } 243f14660a4SJames Wright 244ca69d878SAdeleke O. Bankole // CSV Monitor 245ca69d878SAdeleke O. Bankole PetscErrorCode TSMonitor_WallForce(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 246ca69d878SAdeleke O. Bankole User user = ctx; 247ca69d878SAdeleke O. Bankole Vec G_loc; 248ca69d878SAdeleke O. Bankole PetscInt num_wall = user->app_ctx->wall_forces.num_wall, dim = 3; 249ca69d878SAdeleke O. Bankole const PetscInt *walls = user->app_ctx->wall_forces.walls; 250ca69d878SAdeleke O. Bankole PetscViewer viewer = user->app_ctx->wall_forces.viewer; 251ca69d878SAdeleke O. Bankole PetscViewerFormat format = user->app_ctx->wall_forces.viewer_format; 252ca69d878SAdeleke O. Bankole PetscScalar *reaction_force; 253ca69d878SAdeleke O. Bankole PetscBool iascii; 254ca69d878SAdeleke O. Bankole 255ca69d878SAdeleke O. Bankole PetscFunctionBeginUser; 256ee4ca9cbSJames Wright if (!viewer) PetscFunctionReturn(PETSC_SUCCESS); 257ca69d878SAdeleke O. Bankole PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 258ca69d878SAdeleke O. Bankole PetscCall(PetscMalloc1(num_wall * dim, &reaction_force)); 259ca69d878SAdeleke O. Bankole PetscCall(Surface_Forces_NS(user->dm, G_loc, num_wall, walls, reaction_force)); 260ca69d878SAdeleke O. Bankole PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 261ca69d878SAdeleke O. Bankole 262ca69d878SAdeleke O. Bankole PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 263ca69d878SAdeleke O. Bankole 264ca69d878SAdeleke O. Bankole if (iascii) { 265ca69d878SAdeleke O. Bankole if (format == PETSC_VIEWER_ASCII_CSV && !user->app_ctx->wall_forces.header_written) { 266ca69d878SAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "Step,Time,Wall,ForceX,ForceY,ForceZ\n")); 267ca69d878SAdeleke O. Bankole user->app_ctx->wall_forces.header_written = PETSC_TRUE; 268ca69d878SAdeleke O. Bankole } 269ca69d878SAdeleke O. Bankole for (PetscInt w = 0; w < num_wall; w++) { 270ca69d878SAdeleke O. Bankole PetscInt wall = walls[w]; 271ca69d878SAdeleke O. Bankole if (format == PETSC_VIEWER_ASCII_CSV) { 272ca69d878SAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ",%g,%" PetscInt_FMT ",%g,%g,%g\n", step_no, time, wall, 273ca69d878SAdeleke O. Bankole reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2])); 274ca69d878SAdeleke O. Bankole 275ca69d878SAdeleke O. Bankole } else { 276ca69d878SAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "Wall %" PetscInt_FMT " Forces: Force_x = %12g, Force_y = %12g, Force_z = %12g\n", wall, 277ca69d878SAdeleke O. Bankole reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2])); 278ca69d878SAdeleke O. Bankole } 279ca69d878SAdeleke O. Bankole } 280ca69d878SAdeleke O. Bankole } 281ca69d878SAdeleke O. Bankole PetscCall(PetscFree(reaction_force)); 282ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 283ca69d878SAdeleke O. Bankole } 284ca69d878SAdeleke O. Bankole 285f14660a4SJames Wright // User provided TS Monitor 2862b730f8bSJeremy L Thompson PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 287f14660a4SJames Wright User user = ctx; 288f14660a4SJames Wright 289f17d818dSJames Wright PetscFunctionBeginUser; 29037cbb16aSJed Brown // Print every 'checkpoint_interval' steps 291894de27cSJames Wright if (user->app_ctx->checkpoint_interval <= 0 || step_no % user->app_ctx->checkpoint_interval != 0 || 29249aac155SJeremy L Thompson (user->app_ctx->cont_steps == step_no && step_no != 0)) { 293ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 29449aac155SJeremy L Thompson } 295f14660a4SJames Wright 296f14660a4SJames Wright PetscCall(WriteOutput(user, Q, step_no, time)); 297ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 29877841947SLeila Ghaffari } 29977841947SLeila Ghaffari 30077841947SLeila Ghaffari // TS: Create, setup, and solve 3012b730f8bSJeremy L Thompson PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, Vec *Q, PetscScalar *f_time, TS *ts) { 30277841947SLeila Ghaffari MPI_Comm comm = user->comm; 30377841947SLeila Ghaffari TSAdapt adapt; 30477841947SLeila Ghaffari PetscScalar final_time; 30577841947SLeila Ghaffari 306f17d818dSJames Wright PetscFunctionBeginUser; 3072b730f8bSJeremy L Thompson PetscCall(TSCreate(comm, ts)); 3082b730f8bSJeremy L Thompson PetscCall(TSSetDM(*ts, dm)); 3091a7db67cSJames Wright PetscCall(TSSetApplicationContext(*ts, user)); 31077841947SLeila Ghaffari if (phys->implicit) { 3112b730f8bSJeremy L Thompson PetscCall(TSSetType(*ts, TSBDF)); 31277841947SLeila Ghaffari if (user->op_ifunction) { 3132b730f8bSJeremy L Thompson PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &user)); 31477841947SLeila Ghaffari } else { // Implicit integrators can fall back to using an RHSFunction 3152b730f8bSJeremy L Thompson PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 31677841947SLeila Ghaffari } 31791c97f41SJames Wright if (user->mat_ijacobian) { 3182b730f8bSJeremy L Thompson PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &user)); 319e334ad8fSJed Brown } 32077841947SLeila Ghaffari } else { 3219ad5e8e4SJames Wright PetscCheck(user->op_rhs_ctx, comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction"); 3222b730f8bSJeremy L Thompson PetscCall(TSSetType(*ts, TSRK)); 3232b730f8bSJeremy L Thompson PetscCall(TSRKSetType(*ts, TSRK5F)); 3242b730f8bSJeremy L Thompson PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 32577841947SLeila Ghaffari } 3262b730f8bSJeremy L Thompson PetscCall(TSSetMaxTime(*ts, 500. * user->units->second)); 3272b730f8bSJeremy L Thompson PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER)); 32851b00d91SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) PetscCall(TSSetErrorIfStepFails(*ts, PETSC_FALSE)); 3292b730f8bSJeremy L Thompson PetscCall(TSSetTimeStep(*ts, 1.e-2 * user->units->second)); 3302b730f8bSJeremy L Thompson PetscCall(TSGetAdapt(*ts, &adapt)); 3312b730f8bSJeremy L Thompson PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * user->units->second, 1.e2 * user->units->second)); 3322b730f8bSJeremy L Thompson PetscCall(TSSetFromOptions(*ts)); 33391c97f41SJames Wright if (user->mat_ijacobian) { 33491c97f41SJames Wright if (app_ctx->amat_type && !strcmp(app_ctx->amat_type, MATSHELL)) { 33591c97f41SJames Wright SNES snes; 33691c97f41SJames Wright KSP ksp; 3377f2a9303SJames Wright Mat Pmat, Amat; 33891c97f41SJames Wright 33991c97f41SJames Wright PetscCall(TSGetSNES(*ts, &snes)); 34091c97f41SJames Wright PetscCall(SNESGetKSP(snes, &ksp)); 3417f2a9303SJames Wright PetscCall(CreateSolveOperatorsFromMatCeed(ksp, user->mat_ijacobian, PETSC_FALSE, &Amat, &Pmat)); 34291c97f41SJames Wright PetscCall(TSSetIJacobian(*ts, user->mat_ijacobian, Pmat, NULL, NULL)); 3437f2a9303SJames Wright PetscCall(MatDestroy(&Amat)); 3447f2a9303SJames Wright PetscCall(MatDestroy(&Pmat)); 34591c97f41SJames Wright } 34691c97f41SJames Wright } 34715637395SJames Wright user->time_bc_set = -1.0; // require all BCs be updated 348d55646a4SJames Wright if (app_ctx->cont_steps) { // continue from previous timestep data 34977841947SLeila Ghaffari PetscInt count; 35077841947SLeila Ghaffari PetscViewer viewer; 3512b730f8bSJeremy L Thompson 3524de8550aSJed Brown if (app_ctx->cont_time <= 0) { // Legacy files did not include step number and time 3532b730f8bSJeremy L Thompson PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer)); 3544de8550aSJed Brown PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL)); 3552b730f8bSJeremy L Thompson PetscCall(PetscViewerDestroy(&viewer)); 3564de8550aSJed Brown PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP, 3574de8550aSJed Brown "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)"); 3584de8550aSJed Brown } 3594de8550aSJed Brown PetscCall(TSSetTime(*ts, app_ctx->cont_time * user->units->second)); 360d8e0aecdSJed Brown PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps)); 36177841947SLeila Ghaffari } 3628fb33541SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 3632b730f8bSJeremy L Thompson PetscCall(TSMonitorSet(*ts, TSMonitor_NS, user, NULL)); 3648fb33541SJames Wright } 365ca69d878SAdeleke O. Bankole if (app_ctx->wall_forces.viewer) { 366ca69d878SAdeleke O. Bankole PetscCall(TSMonitorSet(*ts, TSMonitor_WallForce, user, NULL)); 367ca69d878SAdeleke O. Bankole } 368b7d66439SJames Wright if (app_ctx->turb_spanstats_enable) { 369f5452247SJames Wright PetscCall(TSMonitorSet(*ts, TSMonitor_TurbulenceStatistics, user, NULL)); 370495b9769SJames Wright CeedScalar previous_time = app_ctx->cont_time * user->units->second; 371a424bcd0SJames Wright PetscCallCeed(user->ceed, 372a424bcd0SJames Wright CeedOperatorSetContextDouble(user->spanstats.op_stats_collect_ctx->op, user->spanstats.previous_time_label, &previous_time)); 373a175e481SJames Wright } 3744e9802d1SJames Wright if (app_ctx->diff_filter_monitor) PetscCall(TSMonitorSet(*ts, TSMonitor_DifferentialFilter, user, NULL)); 37577841947SLeila Ghaffari 37628134cfaSJames Wright if (app_ctx->sgs_train_enable) { 37728134cfaSJames Wright PetscCall(TSMonitorSet(*ts, TSMonitor_SGS_DD_Training, user, NULL)); 37828134cfaSJames Wright PetscCall(TSSetPostStep(*ts, TSPostStep_SGS_DD_Training)); 37928134cfaSJames Wright } 38077841947SLeila Ghaffari // Solve 381d8e0aecdSJed Brown PetscReal start_time; 382d8e0aecdSJed Brown PetscInt start_step; 3832b730f8bSJeremy L Thompson PetscCall(TSGetTime(*ts, &start_time)); 384d8e0aecdSJed Brown PetscCall(TSGetStepNumber(*ts, &start_step)); 3857b39487dSJeremy L Thompson 386711a423aSJed Brown PetscCall(PetscLogDefaultBegin()); // So we can use PetscLogStageGetPerfInfo without -log_view 3877b39487dSJeremy L Thompson PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve"); 3887b39487dSJeremy L Thompson PetscCall(TSSetTime(*ts, start_time)); 389d8e0aecdSJed Brown PetscCall(TSSetStepNumber(*ts, start_step)); 3907b39487dSJeremy L Thompson if (PetscPreLoadingOn) { 3917b39487dSJeremy L Thompson // LCOV_EXCL_START 3927b39487dSJeremy L Thompson SNES snes; 3937b39487dSJeremy L Thompson Vec Q_preload; 3947b39487dSJeremy L Thompson PetscReal rtol; 3957b39487dSJeremy L Thompson PetscCall(VecDuplicate(*Q, &Q_preload)); 3967b39487dSJeremy L Thompson PetscCall(VecCopy(*Q, Q_preload)); 3977b39487dSJeremy L Thompson PetscCall(TSGetSNES(*ts, &snes)); 3987b39487dSJeremy L Thompson PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL)); 3992b730f8bSJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 400fc4c3d69SJames Wright PetscCall(TSSetSolution(*ts, Q_preload)); 4017b39487dSJeremy L Thompson PetscCall(TSStep(*ts)); 4022b730f8bSJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 4037b39487dSJeremy L Thompson PetscCall(VecDestroy(&Q_preload)); 4047b39487dSJeremy L Thompson // LCOV_EXCL_STOP 4057b39487dSJeremy L Thompson } else { 4062b730f8bSJeremy L Thompson PetscCall(PetscBarrier((PetscObject)*ts)); 4072b730f8bSJeremy L Thompson PetscCall(TSSolve(*ts, *Q)); 4087b39487dSJeremy L Thompson } 4097b39487dSJeremy L Thompson PetscPreLoadEnd(); 4107b39487dSJeremy L Thompson 4117b39487dSJeremy L Thompson PetscCall(TSGetSolveTime(*ts, &final_time)); 41277841947SLeila Ghaffari *f_time = final_time; 4137b39487dSJeremy L Thompson 4148fb33541SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 415f14660a4SJames Wright PetscInt step_no; 416f14660a4SJames Wright PetscCall(TSGetStepNumber(*ts, &step_no)); 417a175e481SJames Wright if (user->app_ctx->checkpoint_interval > 0 || user->app_ctx->checkpoint_interval == -1) { 418f14660a4SJames Wright PetscCall(WriteOutput(user, *Q, step_no, final_time)); 419f14660a4SJames Wright } 420f14660a4SJames Wright 42175d1798cSJames Wright PetscLogStage stage_id; 422711a423aSJed Brown PetscEventPerfInfo stage_perf; 4237b39487dSJeremy L Thompson 4247b39487dSJeremy L Thompson PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id)); 425711a423aSJed Brown PetscCall(PetscLogStageGetPerfInfo(stage_id, &stage_perf)); 426711a423aSJed Brown PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_perf.time)); 42777841947SLeila Ghaffari } 428ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 42977841947SLeila Ghaffari } 430