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; 36*b4e9a8f8SJames 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 47*b4e9a8f8SJames Wright PetscCall(VecReadPetscToCeed(Q_loc, &q_mem_type, user->q_ceed)); 48*b4e9a8f8SJames Wright 49*b4e9a8f8SJames Wright // Inverse of the mass matrix 5004292f7dSJames Wright PetscCall(KSPSolve(user->mass_ksp, G, G)); 51*b4e9a8f8SJames Wright 52*b4e9a8f8SJames 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; 158a424bcd0SJames Wright Ceed ceed = user->ceed; 15991c97f41SJames Wright PetscBool J_is_matceed, J_is_mffd, J_pre_is_matceed, J_pre_is_mffd; 160f17d818dSJames Wright 161e334ad8fSJed Brown PetscFunctionBeginUser; 162d6bf345cSJed Brown PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd)); 16391c97f41SJames Wright PetscCall(PetscObjectTypeCompare((PetscObject)J, MATCEED, &J_is_matceed)); 16491c97f41SJames Wright PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATMFFD, &J_pre_is_mffd)); 16591c97f41SJames Wright PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATCEED, &J_pre_is_matceed)); 16691c97f41SJames Wright if (user->phys->ijacobian_time_shift_label) { 16791c97f41SJames Wright CeedOperator op_ijacobian; 16891c97f41SJames Wright 16991c97f41SJames Wright PetscCall(MatCeedGetCeedOperators(user->mat_ijacobian, &op_ijacobian, NULL)); 17091c97f41SJames Wright PetscCallCeed(ceed, CeedOperatorSetContextDouble(op_ijacobian, user->phys->ijacobian_time_shift_label, &shift)); 171e334ad8fSJed Brown } 17291c97f41SJames Wright 17391c97f41SJames Wright if (J_is_matceed || J_is_mffd) { 174d6bf345cSJed Brown PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 175d6bf345cSJed Brown PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 17691c97f41SJames Wright } else PetscCall(MatCeedAssembleCOO(user->mat_ijacobian, J)); 17791c97f41SJames Wright 17891c97f41SJames Wright if (J_pre_is_matceed && J != J_pre) { 17991c97f41SJames Wright PetscCall(MatAssemblyBegin(J_pre, MAT_FINAL_ASSEMBLY)); 18091c97f41SJames Wright PetscCall(MatAssemblyEnd(J_pre, MAT_FINAL_ASSEMBLY)); 18191c97f41SJames Wright } else if (!J_pre_is_matceed && !J_pre_is_mffd && J != J_pre) { 18291c97f41SJames Wright PetscCall(MatCeedAssembleCOO(user->mat_ijacobian, J_pre)); 183d6bf345cSJed Brown } 184ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 185e334ad8fSJed Brown } 186e334ad8fSJed Brown 1872b730f8bSJeremy L Thompson PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, PetscScalar time) { 18877841947SLeila Ghaffari Vec Q_loc; 18977841947SLeila Ghaffari char file_path[PETSC_MAX_PATH_LEN]; 19077841947SLeila Ghaffari PetscViewer viewer; 19177841947SLeila Ghaffari 192f17d818dSJames Wright PetscFunctionBeginUser; 19337cbb16aSJed Brown if (user->app_ctx->checkpoint_vtk) { 19477841947SLeila Ghaffari // Set up output 195f14660a4SJames Wright PetscCall(DMGetLocalVector(user->dm, &Q_loc)); 196f14660a4SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec")); 197f14660a4SJames Wright PetscCall(VecZeroEntries(Q_loc)); 198f14660a4SJames Wright PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc)); 19977841947SLeila Ghaffari 20077841947SLeila Ghaffari // Output 20137cbb16aSJed Brown PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 202f14660a4SJames Wright 2032b730f8bSJeremy L Thompson PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer)); 204f14660a4SJames Wright PetscCall(VecView(Q_loc, viewer)); 205f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 20677841947SLeila Ghaffari if (user->dm_viz) { 20777841947SLeila Ghaffari Vec Q_refined, Q_refined_loc; 20877841947SLeila Ghaffari char file_path_refined[PETSC_MAX_PATH_LEN]; 20977841947SLeila Ghaffari PetscViewer viewer_refined; 21077841947SLeila Ghaffari 211f14660a4SJames Wright PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined)); 212f14660a4SJames Wright PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc)); 213f14660a4SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined")); 214f14660a4SJames Wright 215f14660a4SJames Wright PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined)); 216f14660a4SJames Wright PetscCall(VecZeroEntries(Q_refined_loc)); 2172b730f8bSJeremy L Thompson PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc)); 218f14660a4SJames Wright 21937cbb16aSJed Brown PetscCall( 22037cbb16aSJed Brown PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 221f14660a4SJames Wright 2222b730f8bSJeremy L Thompson PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined)); 223f14660a4SJames Wright PetscCall(VecView(Q_refined_loc, viewer_refined)); 224f14660a4SJames Wright PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc)); 225f14660a4SJames Wright PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined)); 226f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer_refined)); 22777841947SLeila Ghaffari } 228f14660a4SJames Wright PetscCall(DMRestoreLocalVector(user->dm, &Q_loc)); 22937cbb16aSJed Brown } 23077841947SLeila Ghaffari 23177841947SLeila Ghaffari // Save data in a binary file for continuation of simulations 23269293791SJames Wright if (user->app_ctx->add_stepnum2bin) { 23337cbb16aSJed Brown PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", user->app_ctx->output_dir, step_no)); 23469293791SJames Wright } else { 2352b730f8bSJeremy L Thompson PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", user->app_ctx->output_dir)); 23669293791SJames Wright } 2372b730f8bSJeremy L Thompson PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, &viewer)); 238f14660a4SJames Wright 2390de6a49fSJames Wright PetscInt32 token = PetscDefined(USE_64BIT_INDICES) ? FLUIDS_FILE_TOKEN_64 : FLUIDS_FILE_TOKEN_32; 2400de6a49fSJames Wright PetscCall(PetscViewerBinaryWrite(viewer, &token, 1, PETSC_INT32)); 2414de8550aSJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &step_no, 1, PETSC_INT)); 2424de8550aSJed Brown time /= user->units->second; // Dimensionalize time back 2434de8550aSJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL)); 244f14660a4SJames Wright PetscCall(VecView(Q, viewer)); 245f14660a4SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 246ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 247f14660a4SJames Wright } 248f14660a4SJames Wright 249ca69d878SAdeleke O. Bankole // CSV Monitor 250ca69d878SAdeleke O. Bankole PetscErrorCode TSMonitor_WallForce(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 251ca69d878SAdeleke O. Bankole User user = ctx; 252ca69d878SAdeleke O. Bankole Vec G_loc; 253ca69d878SAdeleke O. Bankole PetscInt num_wall = user->app_ctx->wall_forces.num_wall, dim = 3; 254ca69d878SAdeleke O. Bankole const PetscInt *walls = user->app_ctx->wall_forces.walls; 255ca69d878SAdeleke O. Bankole PetscViewer viewer = user->app_ctx->wall_forces.viewer; 256ca69d878SAdeleke O. Bankole PetscViewerFormat format = user->app_ctx->wall_forces.viewer_format; 257ca69d878SAdeleke O. Bankole PetscScalar *reaction_force; 258ca69d878SAdeleke O. Bankole PetscBool iascii; 259ca69d878SAdeleke O. Bankole 260ca69d878SAdeleke O. Bankole PetscFunctionBeginUser; 261ee4ca9cbSJames Wright if (!viewer) PetscFunctionReturn(PETSC_SUCCESS); 262ca69d878SAdeleke O. Bankole PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 263ca69d878SAdeleke O. Bankole PetscCall(PetscMalloc1(num_wall * dim, &reaction_force)); 264ca69d878SAdeleke O. Bankole PetscCall(Surface_Forces_NS(user->dm, G_loc, num_wall, walls, reaction_force)); 265ca69d878SAdeleke O. Bankole PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 266ca69d878SAdeleke O. Bankole 267ca69d878SAdeleke O. Bankole PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 268ca69d878SAdeleke O. Bankole 269ca69d878SAdeleke O. Bankole if (iascii) { 270ca69d878SAdeleke O. Bankole if (format == PETSC_VIEWER_ASCII_CSV && !user->app_ctx->wall_forces.header_written) { 271ca69d878SAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "Step,Time,Wall,ForceX,ForceY,ForceZ\n")); 272ca69d878SAdeleke O. Bankole user->app_ctx->wall_forces.header_written = PETSC_TRUE; 273ca69d878SAdeleke O. Bankole } 274ca69d878SAdeleke O. Bankole for (PetscInt w = 0; w < num_wall; w++) { 275ca69d878SAdeleke O. Bankole PetscInt wall = walls[w]; 276ca69d878SAdeleke O. Bankole if (format == PETSC_VIEWER_ASCII_CSV) { 277ca69d878SAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ",%g,%" PetscInt_FMT ",%g,%g,%g\n", step_no, time, wall, 278ca69d878SAdeleke O. Bankole reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2])); 279ca69d878SAdeleke O. Bankole 280ca69d878SAdeleke O. Bankole } else { 281ca69d878SAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "Wall %" PetscInt_FMT " Forces: Force_x = %12g, Force_y = %12g, Force_z = %12g\n", wall, 282ca69d878SAdeleke O. Bankole reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2])); 283ca69d878SAdeleke O. Bankole } 284ca69d878SAdeleke O. Bankole } 285ca69d878SAdeleke O. Bankole } 286ca69d878SAdeleke O. Bankole PetscCall(PetscFree(reaction_force)); 287ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 288ca69d878SAdeleke O. Bankole } 289ca69d878SAdeleke O. Bankole 290f14660a4SJames Wright // User provided TS Monitor 2912b730f8bSJeremy L Thompson PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 292f14660a4SJames Wright User user = ctx; 293f14660a4SJames Wright 294f17d818dSJames Wright PetscFunctionBeginUser; 29537cbb16aSJed Brown // Print every 'checkpoint_interval' steps 296894de27cSJames Wright if (user->app_ctx->checkpoint_interval <= 0 || step_no % user->app_ctx->checkpoint_interval != 0 || 29749aac155SJeremy L Thompson (user->app_ctx->cont_steps == step_no && step_no != 0)) { 298ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 29949aac155SJeremy L Thompson } 300f14660a4SJames Wright 301f14660a4SJames Wright PetscCall(WriteOutput(user, Q, step_no, time)); 302ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 30377841947SLeila Ghaffari } 30477841947SLeila Ghaffari 30577841947SLeila Ghaffari // TS: Create, setup, and solve 3062b730f8bSJeremy L Thompson PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, Vec *Q, PetscScalar *f_time, TS *ts) { 30777841947SLeila Ghaffari MPI_Comm comm = user->comm; 30877841947SLeila Ghaffari TSAdapt adapt; 30977841947SLeila Ghaffari PetscScalar final_time; 31077841947SLeila Ghaffari 311f17d818dSJames Wright PetscFunctionBeginUser; 3122b730f8bSJeremy L Thompson PetscCall(TSCreate(comm, ts)); 3132b730f8bSJeremy L Thompson PetscCall(TSSetDM(*ts, dm)); 3141a7db67cSJames Wright PetscCall(TSSetApplicationContext(*ts, user)); 31577841947SLeila Ghaffari if (phys->implicit) { 3162b730f8bSJeremy L Thompson PetscCall(TSSetType(*ts, TSBDF)); 31777841947SLeila Ghaffari if (user->op_ifunction) { 3182b730f8bSJeremy L Thompson PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &user)); 31977841947SLeila Ghaffari } else { // Implicit integrators can fall back to using an RHSFunction 3202b730f8bSJeremy L Thompson PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 32177841947SLeila Ghaffari } 32291c97f41SJames Wright if (user->mat_ijacobian) { 3232b730f8bSJeremy L Thompson PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &user)); 324e334ad8fSJed Brown } 32577841947SLeila Ghaffari } else { 3269ad5e8e4SJames Wright PetscCheck(user->op_rhs_ctx, comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction"); 3272b730f8bSJeremy L Thompson PetscCall(TSSetType(*ts, TSRK)); 3282b730f8bSJeremy L Thompson PetscCall(TSRKSetType(*ts, TSRK5F)); 3292b730f8bSJeremy L Thompson PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 33077841947SLeila Ghaffari } 3312b730f8bSJeremy L Thompson PetscCall(TSSetMaxTime(*ts, 500. * user->units->second)); 3322b730f8bSJeremy L Thompson PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER)); 33351b00d91SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) PetscCall(TSSetErrorIfStepFails(*ts, PETSC_FALSE)); 3342b730f8bSJeremy L Thompson PetscCall(TSSetTimeStep(*ts, 1.e-2 * user->units->second)); 3352b730f8bSJeremy L Thompson PetscCall(TSGetAdapt(*ts, &adapt)); 3362b730f8bSJeremy L Thompson PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * user->units->second, 1.e2 * user->units->second)); 3372b730f8bSJeremy L Thompson PetscCall(TSSetFromOptions(*ts)); 33891c97f41SJames Wright if (user->mat_ijacobian) { 33991c97f41SJames Wright if (app_ctx->amat_type && !strcmp(app_ctx->amat_type, MATSHELL)) { 34091c97f41SJames Wright SNES snes; 34191c97f41SJames Wright KSP ksp; 3427f2a9303SJames Wright Mat Pmat, Amat; 34391c97f41SJames Wright 34491c97f41SJames Wright PetscCall(TSGetSNES(*ts, &snes)); 34591c97f41SJames Wright PetscCall(SNESGetKSP(snes, &ksp)); 3467f2a9303SJames Wright PetscCall(CreateSolveOperatorsFromMatCeed(ksp, user->mat_ijacobian, PETSC_FALSE, &Amat, &Pmat)); 34791c97f41SJames Wright PetscCall(TSSetIJacobian(*ts, user->mat_ijacobian, Pmat, NULL, NULL)); 3487f2a9303SJames Wright PetscCall(MatDestroy(&Amat)); 3497f2a9303SJames Wright PetscCall(MatDestroy(&Pmat)); 35091c97f41SJames Wright } 35191c97f41SJames Wright } 35215637395SJames Wright user->time_bc_set = -1.0; // require all BCs be updated 353d55646a4SJames Wright if (app_ctx->cont_steps) { // continue from previous timestep data 35477841947SLeila Ghaffari PetscInt count; 35577841947SLeila Ghaffari PetscViewer viewer; 3562b730f8bSJeremy L Thompson 3574de8550aSJed Brown if (app_ctx->cont_time <= 0) { // Legacy files did not include step number and time 3582b730f8bSJeremy L Thompson PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer)); 3594de8550aSJed Brown PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL)); 3602b730f8bSJeremy L Thompson PetscCall(PetscViewerDestroy(&viewer)); 3614de8550aSJed Brown PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP, 3624de8550aSJed Brown "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)"); 3634de8550aSJed Brown } 3644de8550aSJed Brown PetscCall(TSSetTime(*ts, app_ctx->cont_time * user->units->second)); 365d8e0aecdSJed Brown PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps)); 36677841947SLeila Ghaffari } 3678fb33541SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 3682b730f8bSJeremy L Thompson PetscCall(TSMonitorSet(*ts, TSMonitor_NS, user, NULL)); 3698fb33541SJames Wright } 370ca69d878SAdeleke O. Bankole if (app_ctx->wall_forces.viewer) { 371ca69d878SAdeleke O. Bankole PetscCall(TSMonitorSet(*ts, TSMonitor_WallForce, user, NULL)); 372ca69d878SAdeleke O. Bankole } 373b7d66439SJames Wright if (app_ctx->turb_spanstats_enable) { 374f5452247SJames Wright PetscCall(TSMonitorSet(*ts, TSMonitor_TurbulenceStatistics, user, NULL)); 375495b9769SJames Wright CeedScalar previous_time = app_ctx->cont_time * user->units->second; 376a424bcd0SJames Wright PetscCallCeed(user->ceed, 377a424bcd0SJames Wright CeedOperatorSetContextDouble(user->spanstats.op_stats_collect_ctx->op, user->spanstats.previous_time_label, &previous_time)); 378a175e481SJames Wright } 3794e9802d1SJames Wright if (app_ctx->diff_filter_monitor) PetscCall(TSMonitorSet(*ts, TSMonitor_DifferentialFilter, user, NULL)); 38077841947SLeila Ghaffari 38128134cfaSJames Wright if (app_ctx->sgs_train_enable) { 38228134cfaSJames Wright PetscCall(TSMonitorSet(*ts, TSMonitor_SGS_DD_Training, user, NULL)); 38328134cfaSJames Wright PetscCall(TSSetPostStep(*ts, TSPostStep_SGS_DD_Training)); 38428134cfaSJames Wright } 38577841947SLeila Ghaffari // Solve 386d8e0aecdSJed Brown PetscReal start_time; 387d8e0aecdSJed Brown PetscInt start_step; 3882b730f8bSJeremy L Thompson PetscCall(TSGetTime(*ts, &start_time)); 389d8e0aecdSJed Brown PetscCall(TSGetStepNumber(*ts, &start_step)); 3907b39487dSJeremy L Thompson 391711a423aSJed Brown PetscCall(PetscLogDefaultBegin()); // So we can use PetscLogStageGetPerfInfo without -log_view 3927b39487dSJeremy L Thompson PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve"); 3937b39487dSJeremy L Thompson PetscCall(TSSetTime(*ts, start_time)); 394d8e0aecdSJed Brown PetscCall(TSSetStepNumber(*ts, start_step)); 3957b39487dSJeremy L Thompson if (PetscPreLoadingOn) { 3967b39487dSJeremy L Thompson // LCOV_EXCL_START 3977b39487dSJeremy L Thompson SNES snes; 3987b39487dSJeremy L Thompson Vec Q_preload; 3997b39487dSJeremy L Thompson PetscReal rtol; 4007b39487dSJeremy L Thompson PetscCall(VecDuplicate(*Q, &Q_preload)); 4017b39487dSJeremy L Thompson PetscCall(VecCopy(*Q, Q_preload)); 4027b39487dSJeremy L Thompson PetscCall(TSGetSNES(*ts, &snes)); 4037b39487dSJeremy L Thompson PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL)); 4042b730f8bSJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 405fc4c3d69SJames Wright PetscCall(TSSetSolution(*ts, Q_preload)); 4067b39487dSJeremy L Thompson PetscCall(TSStep(*ts)); 4072b730f8bSJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 4087b39487dSJeremy L Thompson PetscCall(VecDestroy(&Q_preload)); 4097b39487dSJeremy L Thompson // LCOV_EXCL_STOP 4107b39487dSJeremy L Thompson } else { 4112b730f8bSJeremy L Thompson PetscCall(PetscBarrier((PetscObject)*ts)); 4122b730f8bSJeremy L Thompson PetscCall(TSSolve(*ts, *Q)); 4137b39487dSJeremy L Thompson } 4147b39487dSJeremy L Thompson PetscPreLoadEnd(); 4157b39487dSJeremy L Thompson 4167b39487dSJeremy L Thompson PetscCall(TSGetSolveTime(*ts, &final_time)); 41777841947SLeila Ghaffari *f_time = final_time; 4187b39487dSJeremy L Thompson 4198fb33541SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 420f14660a4SJames Wright PetscInt step_no; 421f14660a4SJames Wright PetscCall(TSGetStepNumber(*ts, &step_no)); 422a175e481SJames Wright if (user->app_ctx->checkpoint_interval > 0 || user->app_ctx->checkpoint_interval == -1) { 423f14660a4SJames Wright PetscCall(WriteOutput(user, *Q, step_no, final_time)); 424f14660a4SJames Wright } 425f14660a4SJames Wright 42675d1798cSJames Wright PetscLogStage stage_id; 427711a423aSJed Brown PetscEventPerfInfo stage_perf; 4287b39487dSJeremy L Thompson 4297b39487dSJeremy L Thompson PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id)); 430711a423aSJed Brown PetscCall(PetscLogStageGetPerfInfo(stage_id, &stage_perf)); 431711a423aSJed Brown PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_perf.time)); 43277841947SLeila Ghaffari } 433ee4ca9cbSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 43477841947SLeila Ghaffari } 435