1dc936754SJeremy L Thompson // Copyright (c) 2017-2024, 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 11e419654dSJeremy L Thompson #include <ceed.h> 12e419654dSJeremy L Thompson #include <petscdmplex.h> 13e419654dSJeremy L Thompson #include <petscts.h> 14e419654dSJeremy L Thompson 15a515125bSLeila Ghaffari #include "../navierstokes.h" 16c5e9980aSAdeleke O. Bankole #include "../qfunctions/newtonian_state.h" 17a515125bSLeila Ghaffari 18c996854bSJames Wright // Insert Boundary values if it's a new time 19c996854bSJames Wright PetscErrorCode UpdateBoundaryValues(User user, Vec Q_loc, PetscReal t) { 20c996854bSJames Wright PetscFunctionBeginUser; 21c996854bSJames Wright if (user->time_bc_set != t) { 22c996854bSJames Wright PetscCall(DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Q_loc, t, NULL, NULL, NULL)); 23c996854bSJames Wright user->time_bc_set = t; 24c996854bSJames Wright } 25d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 26c996854bSJames Wright } 27c996854bSJames Wright 28a515125bSLeila Ghaffari // RHS (Explicit time-stepper) function setup 29a515125bSLeila Ghaffari // This is the RHS of the ODE, given as u_t = G(t,u) 30a515125bSLeila Ghaffari // This function takes in a state vector Q and writes into G 31a515125bSLeila Ghaffari PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *user_data) { 32a515125bSLeila Ghaffari User user = *(User *)user_data; 33701e5830SJames Wright Ceed ceed = user->ceed; 34fd969b44SJames Wright PetscScalar dt; 35da5fe0e4SJames Wright Vec Q_loc = user->Q_loc; 36a78efa86SJames Wright PetscMemType q_mem_type; 37a515125bSLeila Ghaffari 3806f41313SJames Wright PetscFunctionBeginUser; 39e2f84137SJeremy L Thompson // Update time dependent data 40c996854bSJames Wright PetscCall(UpdateBoundaryValues(user, Q_loc, t)); 41701e5830SJames Wright if (user->phys->solution_time_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_rhs_ctx->op, user->phys->solution_time_label, &t)); 422b916ea7SJeremy L Thompson PetscCall(TSGetTimeStep(ts, &dt)); 43701e5830SJames Wright if (user->phys->timestep_size_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_rhs_ctx->op, user->phys->timestep_size_label, &dt)); 44a515125bSLeila Ghaffari 45da5fe0e4SJames Wright PetscCall(ApplyCeedOperatorGlobalToGlobal(Q, G, user->op_rhs_ctx)); 46a515125bSLeila Ghaffari 47a78efa86SJames Wright PetscCall(VecReadPetscToCeed(Q_loc, &q_mem_type, user->q_ceed)); 48a78efa86SJames Wright 49a78efa86SJames Wright // Inverse of the mass matrix 50f8e2d240SJames Wright PetscCall(KSPSolve(user->mass_ksp, G, G)); 51a78efa86SJames Wright 52a78efa86SJames Wright PetscCall(VecReadCeedToPetsc(user->q_ceed, q_mem_type, Q_loc)); 53d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 54a515125bSLeila Ghaffari } 55a515125bSLeila Ghaffari 56c5e9980aSAdeleke O. Bankole // Surface forces function setup 57c5e9980aSAdeleke O. Bankole static PetscErrorCode Surface_Forces_NS(DM dm, Vec G_loc, PetscInt num_walls, const PetscInt walls[], PetscScalar *reaction_force) { 58c5e9980aSAdeleke O. Bankole DMLabel face_label; 59c5e9980aSAdeleke O. Bankole const PetscScalar *g; 602004e3acSAdeleke O. Bankole PetscInt dof, dim = 3; 61c5e9980aSAdeleke O. Bankole MPI_Comm comm; 622004e3acSAdeleke O. Bankole PetscSection s; 63c5e9980aSAdeleke O. Bankole 64c5e9980aSAdeleke O. Bankole PetscFunctionBeginUser; 65c5e9980aSAdeleke O. Bankole PetscCall(PetscArrayzero(reaction_force, num_walls * dim)); 66c5e9980aSAdeleke O. Bankole PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 67c5e9980aSAdeleke O. Bankole PetscCall(DMGetLabel(dm, "Face Sets", &face_label)); 68c5e9980aSAdeleke O. Bankole PetscCall(VecGetArrayRead(G_loc, &g)); 69c5e9980aSAdeleke O. Bankole for (PetscInt w = 0; w < num_walls; w++) { 70c5e9980aSAdeleke O. Bankole const PetscInt wall = walls[w]; 71c5e9980aSAdeleke O. Bankole IS wall_is; 722004e3acSAdeleke O. Bankole PetscCall(DMGetLocalSection(dm, &s)); 73c5e9980aSAdeleke O. Bankole PetscCall(DMLabelGetStratumIS(face_label, wall, &wall_is)); 74c5e9980aSAdeleke O. Bankole if (wall_is) { // There exist such points on this process 75c5e9980aSAdeleke O. Bankole PetscInt num_points; 762004e3acSAdeleke O. Bankole PetscInt num_comp = 0; 77c5e9980aSAdeleke O. Bankole const PetscInt *points; 782004e3acSAdeleke O. Bankole PetscCall(PetscSectionGetFieldComponents(s, 0, &num_comp)); 79c5e9980aSAdeleke O. Bankole PetscCall(ISGetSize(wall_is, &num_points)); 80c5e9980aSAdeleke O. Bankole PetscCall(ISGetIndices(wall_is, &points)); 81c5e9980aSAdeleke O. Bankole for (PetscInt i = 0; i < num_points; i++) { 82c5e9980aSAdeleke O. Bankole const PetscInt p = points[i]; 83c5e9980aSAdeleke O. Bankole const StateConservative *r; 84c5e9980aSAdeleke O. Bankole PetscCall(DMPlexPointLocalRead(dm, p, g, &r)); 852004e3acSAdeleke O. Bankole PetscCall(PetscSectionGetDof(s, p, &dof)); 862004e3acSAdeleke O. Bankole for (PetscInt node = 0; node < dof / num_comp; node++) { 87c5e9980aSAdeleke O. Bankole for (PetscInt j = 0; j < 3; j++) { 882004e3acSAdeleke O. Bankole reaction_force[w * dim + j] -= r[node].momentum[j]; 892004e3acSAdeleke O. Bankole } 90c5e9980aSAdeleke O. Bankole } 91c5e9980aSAdeleke O. Bankole } 92c5e9980aSAdeleke O. Bankole PetscCall(ISRestoreIndices(wall_is, &points)); 93c5e9980aSAdeleke O. Bankole } 94c5e9980aSAdeleke O. Bankole PetscCall(ISDestroy(&wall_is)); 95c5e9980aSAdeleke O. Bankole } 96c5e9980aSAdeleke O. Bankole PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, reaction_force, dim * num_walls, MPIU_SCALAR, MPI_SUM, comm)); 97c5e9980aSAdeleke O. Bankole // Restore Vectors 98c5e9980aSAdeleke O. Bankole PetscCall(VecRestoreArrayRead(G_loc, &g)); 99d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 100c5e9980aSAdeleke O. Bankole } 101c5e9980aSAdeleke O. Bankole 102a515125bSLeila Ghaffari // Implicit time-stepper function setup 1032b916ea7SJeremy L Thompson PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, Vec G, void *user_data) { 104a515125bSLeila Ghaffari User user = *(User *)user_data; 105701e5830SJames Wright Ceed ceed = user->ceed; 106fd969b44SJames Wright PetscScalar dt; 107e2f84137SJeremy L Thompson Vec Q_loc = user->Q_loc, Q_dot_loc = user->Q_dot_loc, G_loc; 108a515125bSLeila Ghaffari PetscMemType q_mem_type, q_dot_mem_type, g_mem_type; 109a515125bSLeila Ghaffari 11006f41313SJames Wright PetscFunctionBeginUser; 111e2f84137SJeremy L Thompson // Get local vectors 112c5e9980aSAdeleke O. Bankole PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 113e2f84137SJeremy L Thompson 114e2f84137SJeremy L Thompson // Update time dependent data 115c996854bSJames Wright PetscCall(UpdateBoundaryValues(user, Q_loc, t)); 116701e5830SJames Wright if (user->phys->solution_time_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_ifunction, user->phys->solution_time_label, &t)); 1172b916ea7SJeremy L Thompson PetscCall(TSGetTimeStep(ts, &dt)); 118701e5830SJames Wright if (user->phys->timestep_size_label) PetscCallCeed(ceed, CeedOperatorSetContextDouble(user->op_ifunction, user->phys->timestep_size_label, &dt)); 119a515125bSLeila Ghaffari 120a515125bSLeila Ghaffari // Global-to-local 12106108310SJames Wright PetscCall(DMGlobalToLocalBegin(user->dm, Q, INSERT_VALUES, Q_loc)); 12206108310SJames Wright PetscCall(DMGlobalToLocalBegin(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc)); 12306108310SJames Wright PetscCall(DMGlobalToLocalEnd(user->dm, Q, INSERT_VALUES, Q_loc)); 12406108310SJames Wright PetscCall(DMGlobalToLocalEnd(user->dm, Q_dot, INSERT_VALUES, Q_dot_loc)); 125a515125bSLeila Ghaffari 126a515125bSLeila Ghaffari // Place PETSc vectors in CEED vectors 127a7dac1d5SJames Wright PetscCall(VecReadPetscToCeed(Q_loc, &q_mem_type, user->q_ceed)); 128a7dac1d5SJames Wright PetscCall(VecReadPetscToCeed(Q_dot_loc, &q_dot_mem_type, user->q_dot_ceed)); 129a7dac1d5SJames Wright PetscCall(VecPetscToCeed(G_loc, &g_mem_type, user->g_ceed)); 130a515125bSLeila Ghaffari 131a515125bSLeila Ghaffari // Apply CEED operator 1327eedc94cSJames Wright PetscCall(PetscLogEventBegin(FLUIDS_CeedOperatorApply, Q, G, 0, 0)); 1337eedc94cSJames Wright PetscCall(PetscLogGpuTimeBegin()); 134b4c37c5cSJames Wright PetscCallCeed(user->ceed, CeedOperatorApply(user->op_ifunction, user->q_ceed, user->g_ceed, CEED_REQUEST_IMMEDIATE)); 1357eedc94cSJames Wright PetscCall(PetscLogGpuTimeEnd()); 1367eedc94cSJames Wright PetscCall(PetscLogEventEnd(FLUIDS_CeedOperatorApply, Q, G, 0, 0)); 137a515125bSLeila Ghaffari 138a515125bSLeila Ghaffari // Restore vectors 139a7dac1d5SJames Wright PetscCall(VecReadCeedToPetsc(user->q_ceed, q_mem_type, Q_loc)); 140a7dac1d5SJames Wright PetscCall(VecReadCeedToPetsc(user->q_dot_ceed, q_dot_mem_type, Q_dot_loc)); 141a7dac1d5SJames Wright PetscCall(VecCeedToPetsc(user->g_ceed, g_mem_type, G_loc)); 142a515125bSLeila Ghaffari 14301ab89c1SJames Wright if (user->app_ctx->sgs_model_type == SGS_MODEL_DATA_DRIVEN) { 144ad494f68SJames Wright PetscCall(SgsDDApplyIFunction(user, Q_loc, G_loc)); 14501ab89c1SJames Wright } 1469c678832SJames Wright 147a515125bSLeila Ghaffari // Local-to-Global 1482b916ea7SJeremy L Thompson PetscCall(VecZeroEntries(G)); 1492b916ea7SJeremy L Thompson PetscCall(DMLocalToGlobal(user->dm, G_loc, ADD_VALUES, G)); 150a515125bSLeila Ghaffari 151a515125bSLeila Ghaffari // Restore vectors 152c5e9980aSAdeleke O. Bankole PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 153d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 154a515125bSLeila Ghaffari } 155a515125bSLeila Ghaffari 1562b916ea7SJeremy L Thompson PetscErrorCode FormIJacobian_NS(TS ts, PetscReal t, Vec Q, Vec Q_dot, PetscReal shift, Mat J, Mat J_pre, void *user_data) { 157f0b65372SJed Brown User user = *(User *)user_data; 158ebfabadfSJames Wright PetscBool J_is_matceed, J_is_mffd, J_pre_is_matceed, J_pre_is_mffd; 15906f41313SJames Wright 160f0b65372SJed Brown PetscFunctionBeginUser; 16104855949SJed Brown PetscCall(PetscObjectTypeCompare((PetscObject)J, MATMFFD, &J_is_mffd)); 162ebfabadfSJames Wright PetscCall(PetscObjectTypeCompare((PetscObject)J, MATCEED, &J_is_matceed)); 163ebfabadfSJames Wright PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATMFFD, &J_pre_is_mffd)); 164ebfabadfSJames Wright PetscCall(PetscObjectTypeCompare((PetscObject)J_pre, MATCEED, &J_pre_is_matceed)); 165ebfabadfSJames Wright 16651bb547fSJames Wright PetscCall(MatCeedSetContextReal(user->mat_ijacobian, "ijacobian time shift", shift)); 167ebfabadfSJames Wright 168ebfabadfSJames Wright if (J_is_matceed || J_is_mffd) { 16904855949SJed Brown PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 17004855949SJed Brown PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 171ebfabadfSJames Wright } else PetscCall(MatCeedAssembleCOO(user->mat_ijacobian, J)); 172ebfabadfSJames Wright 173ebfabadfSJames Wright if (J_pre_is_matceed && J != J_pre) { 174ebfabadfSJames Wright PetscCall(MatAssemblyBegin(J_pre, MAT_FINAL_ASSEMBLY)); 175ebfabadfSJames Wright PetscCall(MatAssemblyEnd(J_pre, MAT_FINAL_ASSEMBLY)); 176ebfabadfSJames Wright } else if (!J_pre_is_matceed && !J_pre_is_mffd && J != J_pre) { 177ebfabadfSJames Wright PetscCall(MatCeedAssembleCOO(user->mat_ijacobian, J_pre)); 17804855949SJed Brown } 179d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 180f0b65372SJed Brown } 181f0b65372SJed Brown 1822b916ea7SJeremy L Thompson PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no, PetscScalar time) { 183a515125bSLeila Ghaffari Vec Q_loc; 184a515125bSLeila Ghaffari char file_path[PETSC_MAX_PATH_LEN]; 185a515125bSLeila Ghaffari PetscViewer viewer; 186a515125bSLeila Ghaffari 18706f41313SJames Wright PetscFunctionBeginUser; 188852e5969SJed Brown if (user->app_ctx->checkpoint_vtk) { 189a515125bSLeila Ghaffari // Set up output 1907538d537SJames Wright PetscCall(DMGetLocalVector(user->dm, &Q_loc)); 1917538d537SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_loc, "StateVec")); 1927538d537SJames Wright PetscCall(VecZeroEntries(Q_loc)); 1937538d537SJames Wright PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Q_loc)); 194a515125bSLeila Ghaffari 195a515125bSLeila Ghaffari // Output 196852e5969SJed Brown PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 1977538d537SJames Wright 1982b916ea7SJeremy L Thompson PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), file_path, FILE_MODE_WRITE, &viewer)); 1997538d537SJames Wright PetscCall(VecView(Q_loc, viewer)); 2007538d537SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 201a515125bSLeila Ghaffari if (user->dm_viz) { 202a515125bSLeila Ghaffari Vec Q_refined, Q_refined_loc; 203a515125bSLeila Ghaffari char file_path_refined[PETSC_MAX_PATH_LEN]; 204a515125bSLeila Ghaffari PetscViewer viewer_refined; 205a515125bSLeila Ghaffari 2067538d537SJames Wright PetscCall(DMGetGlobalVector(user->dm_viz, &Q_refined)); 2077538d537SJames Wright PetscCall(DMGetLocalVector(user->dm_viz, &Q_refined_loc)); 2087538d537SJames Wright PetscCall(PetscObjectSetName((PetscObject)Q_refined_loc, "Refined")); 2097538d537SJames Wright 2107538d537SJames Wright PetscCall(MatInterpolate(user->interp_viz, Q, Q_refined)); 2117538d537SJames Wright PetscCall(VecZeroEntries(Q_refined_loc)); 2122b916ea7SJeremy L Thompson PetscCall(DMGlobalToLocal(user->dm_viz, Q_refined, INSERT_VALUES, Q_refined_loc)); 2137538d537SJames Wright 214852e5969SJed Brown PetscCall( 215852e5969SJed Brown PetscSNPrintf(file_path_refined, sizeof file_path_refined, "%s/nsrefined-%03" PetscInt_FMT ".vtu", user->app_ctx->output_dir, step_no)); 2167538d537SJames Wright 2172b916ea7SJeremy L Thompson PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q_refined), file_path_refined, FILE_MODE_WRITE, &viewer_refined)); 2187538d537SJames Wright PetscCall(VecView(Q_refined_loc, viewer_refined)); 2197538d537SJames Wright PetscCall(DMRestoreLocalVector(user->dm_viz, &Q_refined_loc)); 2207538d537SJames Wright PetscCall(DMRestoreGlobalVector(user->dm_viz, &Q_refined)); 2217538d537SJames Wright PetscCall(PetscViewerDestroy(&viewer_refined)); 222a515125bSLeila Ghaffari } 2237538d537SJames Wright PetscCall(DMRestoreLocalVector(user->dm, &Q_loc)); 224852e5969SJed Brown } 225a515125bSLeila Ghaffari 226a515125bSLeila Ghaffari // Save data in a binary file for continuation of simulations 22791a36801SJames Wright if (user->app_ctx->add_stepnum2bin) { 228852e5969SJed Brown PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution-%" PetscInt_FMT ".bin", user->app_ctx->output_dir, step_no)); 22991a36801SJames Wright } else { 2302b916ea7SJeremy L Thompson PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin", user->app_ctx->output_dir)); 23191a36801SJames Wright } 2322b916ea7SJeremy L Thompson PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE, &viewer)); 2337538d537SJames Wright 234e1233009SJames Wright PetscInt32 token = PetscDefined(USE_64BIT_INDICES) ? FLUIDS_FILE_TOKEN_64 : FLUIDS_FILE_TOKEN_32; 235e1233009SJames Wright PetscCall(PetscViewerBinaryWrite(viewer, &token, 1, PETSC_INT32)); 2369293eaa1SJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &step_no, 1, PETSC_INT)); 2379293eaa1SJed Brown time /= user->units->second; // Dimensionalize time back 2389293eaa1SJed Brown PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL)); 2397538d537SJames Wright PetscCall(VecView(Q, viewer)); 2407538d537SJames Wright PetscCall(PetscViewerDestroy(&viewer)); 241d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 2427538d537SJames Wright } 2437538d537SJames Wright 244c5e9980aSAdeleke O. Bankole // CSV Monitor 245c5e9980aSAdeleke O. Bankole PetscErrorCode TSMonitor_WallForce(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 246c5e9980aSAdeleke O. Bankole User user = ctx; 247c5e9980aSAdeleke O. Bankole Vec G_loc; 248c5e9980aSAdeleke O. Bankole PetscInt num_wall = user->app_ctx->wall_forces.num_wall, dim = 3; 249c5e9980aSAdeleke O. Bankole const PetscInt *walls = user->app_ctx->wall_forces.walls; 250c5e9980aSAdeleke O. Bankole PetscViewer viewer = user->app_ctx->wall_forces.viewer; 251c5e9980aSAdeleke O. Bankole PetscViewerFormat format = user->app_ctx->wall_forces.viewer_format; 252c5e9980aSAdeleke O. Bankole PetscScalar *reaction_force; 253c5e9980aSAdeleke O. Bankole PetscBool iascii; 254c5e9980aSAdeleke O. Bankole 255c5e9980aSAdeleke O. Bankole PetscFunctionBeginUser; 256d949ddfcSJames Wright if (!viewer) PetscFunctionReturn(PETSC_SUCCESS); 257c5e9980aSAdeleke O. Bankole PetscCall(DMGetNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 258c5e9980aSAdeleke O. Bankole PetscCall(PetscMalloc1(num_wall * dim, &reaction_force)); 259c5e9980aSAdeleke O. Bankole PetscCall(Surface_Forces_NS(user->dm, G_loc, num_wall, walls, reaction_force)); 260c5e9980aSAdeleke O. Bankole PetscCall(DMRestoreNamedLocalVector(user->dm, "ResidualLocal", &G_loc)); 261c5e9980aSAdeleke O. Bankole 262c5e9980aSAdeleke O. Bankole PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 263c5e9980aSAdeleke O. Bankole 264c5e9980aSAdeleke O. Bankole if (iascii) { 265c5e9980aSAdeleke O. Bankole if (format == PETSC_VIEWER_ASCII_CSV && !user->app_ctx->wall_forces.header_written) { 266c5e9980aSAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "Step,Time,Wall,ForceX,ForceY,ForceZ\n")); 267c5e9980aSAdeleke O. Bankole user->app_ctx->wall_forces.header_written = PETSC_TRUE; 268c5e9980aSAdeleke O. Bankole } 269c5e9980aSAdeleke O. Bankole for (PetscInt w = 0; w < num_wall; w++) { 270c5e9980aSAdeleke O. Bankole PetscInt wall = walls[w]; 271c5e9980aSAdeleke O. Bankole if (format == PETSC_VIEWER_ASCII_CSV) { 272c5e9980aSAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ",%g,%" PetscInt_FMT ",%g,%g,%g\n", step_no, time, wall, 273c5e9980aSAdeleke O. Bankole reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2])); 274c5e9980aSAdeleke O. Bankole 275c5e9980aSAdeleke O. Bankole } else { 276c5e9980aSAdeleke O. Bankole PetscCall(PetscViewerASCIIPrintf(viewer, "Wall %" PetscInt_FMT " Forces: Force_x = %12g, Force_y = %12g, Force_z = %12g\n", wall, 277c5e9980aSAdeleke O. Bankole reaction_force[w * dim + 0], reaction_force[w * dim + 1], reaction_force[w * dim + 2])); 278c5e9980aSAdeleke O. Bankole } 279c5e9980aSAdeleke O. Bankole } 280c5e9980aSAdeleke O. Bankole } 281c5e9980aSAdeleke O. Bankole PetscCall(PetscFree(reaction_force)); 282d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 283c5e9980aSAdeleke O. Bankole } 284c5e9980aSAdeleke O. Bankole 2857538d537SJames Wright // User provided TS Monitor 2862b916ea7SJeremy L Thompson PetscErrorCode TSMonitor_NS(TS ts, PetscInt step_no, PetscReal time, Vec Q, void *ctx) { 2877538d537SJames Wright User user = ctx; 2887538d537SJames Wright 28906f41313SJames Wright PetscFunctionBeginUser; 290852e5969SJed Brown // Print every 'checkpoint_interval' steps 291c539088bSJames Wright if (user->app_ctx->checkpoint_interval <= 0 || step_no % user->app_ctx->checkpoint_interval != 0 || 292e419654dSJeremy L Thompson (user->app_ctx->cont_steps == step_no && step_no != 0)) { 293d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 294e419654dSJeremy L Thompson } 2957538d537SJames Wright 2967538d537SJames Wright PetscCall(WriteOutput(user, Q, step_no, time)); 297d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 298a515125bSLeila Ghaffari } 299a515125bSLeila Ghaffari 300a515125bSLeila Ghaffari // TS: Create, setup, and solve 301*e539bbbaSJames Wright PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys, ProblemData problem, Vec *Q, PetscScalar *f_time, TS *ts) { 302a515125bSLeila Ghaffari MPI_Comm comm = user->comm; 303a515125bSLeila Ghaffari TSAdapt adapt; 304a515125bSLeila Ghaffari PetscScalar final_time; 305a515125bSLeila Ghaffari 30606f41313SJames Wright PetscFunctionBeginUser; 3072b916ea7SJeremy L Thompson PetscCall(TSCreate(comm, ts)); 3082b916ea7SJeremy L Thompson PetscCall(TSSetDM(*ts, dm)); 309632a41e1SJames Wright PetscCall(TSSetApplicationContext(*ts, user)); 310a515125bSLeila Ghaffari if (phys->implicit) { 3112b916ea7SJeremy L Thompson PetscCall(TSSetType(*ts, TSBDF)); 312a515125bSLeila Ghaffari if (user->op_ifunction) { 3132b916ea7SJeremy L Thompson PetscCall(TSSetIFunction(*ts, NULL, IFunction_NS, &user)); 314a515125bSLeila Ghaffari } else { // Implicit integrators can fall back to using an RHSFunction 3152b916ea7SJeremy L Thompson PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 316a515125bSLeila Ghaffari } 317ebfabadfSJames Wright if (user->mat_ijacobian) { 3182b916ea7SJeremy L Thompson PetscCall(DMTSSetIJacobian(dm, FormIJacobian_NS, &user)); 319f0b65372SJed Brown } 320a515125bSLeila Ghaffari } else { 321da5fe0e4SJames Wright PetscCheck(user->op_rhs_ctx, comm, PETSC_ERR_ARG_NULL, "Problem does not provide RHSFunction"); 3222b916ea7SJeremy L Thompson PetscCall(TSSetType(*ts, TSRK)); 3232b916ea7SJeremy L Thompson PetscCall(TSRKSetType(*ts, TSRK5F)); 3242b916ea7SJeremy L Thompson PetscCall(TSSetRHSFunction(*ts, NULL, RHS_NS, &user)); 325a515125bSLeila Ghaffari } 3262b916ea7SJeremy L Thompson PetscCall(TSSetMaxTime(*ts, 500. * user->units->second)); 3272b916ea7SJeremy L Thompson PetscCall(TSSetExactFinalTime(*ts, TS_EXACTFINALTIME_STEPOVER)); 32822387d3aSJames Wright if (app_ctx->test_type == TESTTYPE_NONE) PetscCall(TSSetErrorIfStepFails(*ts, PETSC_FALSE)); 3292b916ea7SJeremy L Thompson PetscCall(TSSetTimeStep(*ts, 1.e-2 * user->units->second)); 3302b916ea7SJeremy L Thompson PetscCall(TSGetAdapt(*ts, &adapt)); 3312b916ea7SJeremy L Thompson PetscCall(TSAdaptSetStepLimits(adapt, 1.e-12 * user->units->second, 1.e2 * user->units->second)); 3322b916ea7SJeremy L Thompson PetscCall(TSSetFromOptions(*ts)); 333ebfabadfSJames Wright if (user->mat_ijacobian) { 334ebfabadfSJames Wright if (app_ctx->amat_type && !strcmp(app_ctx->amat_type, MATSHELL)) { 335ebfabadfSJames Wright SNES snes; 336ebfabadfSJames Wright KSP ksp; 3373170c09fSJames Wright Mat Pmat, Amat; 338ebfabadfSJames Wright 339ebfabadfSJames Wright PetscCall(TSGetSNES(*ts, &snes)); 340ebfabadfSJames Wright PetscCall(SNESGetKSP(snes, &ksp)); 3413170c09fSJames Wright PetscCall(CreateSolveOperatorsFromMatCeed(ksp, user->mat_ijacobian, PETSC_FALSE, &Amat, &Pmat)); 342ebfabadfSJames Wright PetscCall(TSSetIJacobian(*ts, user->mat_ijacobian, Pmat, NULL, NULL)); 3433170c09fSJames Wright PetscCall(MatDestroy(&Amat)); 3443170c09fSJames Wright PetscCall(MatDestroy(&Pmat)); 345ebfabadfSJames Wright } 346ebfabadfSJames Wright } 34791f639d2SJames Wright user->time_bc_set = -1.0; // require all BCs be updated 348c26b555cSJames Wright if (app_ctx->cont_steps) { // continue from previous timestep data 349a515125bSLeila Ghaffari PetscInt count; 350a515125bSLeila Ghaffari PetscViewer viewer; 3512b916ea7SJeremy L Thompson 3529293eaa1SJed Brown if (app_ctx->cont_time <= 0) { // Legacy files did not include step number and time 3532b916ea7SJeremy L Thompson PetscCall(PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ, &viewer)); 3549293eaa1SJed Brown PetscCall(PetscViewerBinaryRead(viewer, &app_ctx->cont_time, 1, &count, PETSC_REAL)); 3552b916ea7SJeremy L Thompson PetscCall(PetscViewerDestroy(&viewer)); 3569293eaa1SJed Brown PetscCheck(app_ctx->cont_steps != -1, comm, PETSC_ERR_ARG_INCOMP, 3579293eaa1SJed Brown "-continue step number not specified, but checkpoint file does not contain a step number (likely written by older code version)"); 3589293eaa1SJed Brown } 3599293eaa1SJed Brown PetscCall(TSSetTime(*ts, app_ctx->cont_time * user->units->second)); 36074a6f4ddSJed Brown PetscCall(TSSetStepNumber(*ts, app_ctx->cont_steps)); 361a515125bSLeila Ghaffari } 3620e1e9333SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 3632b916ea7SJeremy L Thompson PetscCall(TSMonitorSet(*ts, TSMonitor_NS, user, NULL)); 3640e1e9333SJames Wright } 365c5e9980aSAdeleke O. Bankole if (app_ctx->wall_forces.viewer) { 366c5e9980aSAdeleke O. Bankole PetscCall(TSMonitorSet(*ts, TSMonitor_WallForce, user, NULL)); 367c5e9980aSAdeleke O. Bankole } 368c931fa59SJames Wright if (app_ctx->turb_spanstats_enable) { 36991933550SJames Wright PetscCall(TSMonitorSet(*ts, TSMonitor_TurbulenceStatistics, user, NULL)); 370b8daee98SJames Wright CeedScalar previous_time = app_ctx->cont_time * user->units->second; 371b4c37c5cSJames Wright PetscCallCeed(user->ceed, 372b4c37c5cSJames Wright CeedOperatorSetContextDouble(user->spanstats.op_stats_collect_ctx->op, user->spanstats.previous_time_label, &previous_time)); 373b0488d1fSJames Wright } 37488b07121SJames Wright if (app_ctx->diff_filter_monitor) PetscCall(TSMonitorSet(*ts, TSMonitor_DifferentialFilter, user, NULL)); 375a515125bSLeila Ghaffari 3761c17f66aSJames Wright if (app_ctx->sgs_train_enable) { 3771c17f66aSJames Wright PetscCall(TSMonitorSet(*ts, TSMonitor_SGS_DD_Training, user, NULL)); 3781c17f66aSJames Wright PetscCall(TSSetPostStep(*ts, TSPostStep_SGS_DD_Training)); 3791c17f66aSJames Wright } 380*e539bbbaSJames Wright 381*e539bbbaSJames Wright if (app_ctx->test_type == TESTTYPE_NONE) PetscCall(PrintRunInfo(user, user->phys, problem, comm)); 382a515125bSLeila Ghaffari // Solve 38374a6f4ddSJed Brown PetscReal start_time; 38474a6f4ddSJed Brown PetscInt start_step; 3852b916ea7SJeremy L Thompson PetscCall(TSGetTime(*ts, &start_time)); 38674a6f4ddSJed Brown PetscCall(TSGetStepNumber(*ts, &start_step)); 38791982731SJeremy L Thompson 388df4304b5SJed Brown PetscCall(PetscLogDefaultBegin()); // So we can use PetscLogStageGetPerfInfo without -log_view 38991982731SJeremy L Thompson PetscPreLoadBegin(PETSC_FALSE, "Fluids Solve"); 39091982731SJeremy L Thompson PetscCall(TSSetTime(*ts, start_time)); 39174a6f4ddSJed Brown PetscCall(TSSetStepNumber(*ts, start_step)); 39291982731SJeremy L Thompson if (PetscPreLoadingOn) { 39391982731SJeremy L Thompson // LCOV_EXCL_START 39491982731SJeremy L Thompson SNES snes; 39591982731SJeremy L Thompson Vec Q_preload; 39691982731SJeremy L Thompson PetscReal rtol; 39791982731SJeremy L Thompson PetscCall(VecDuplicate(*Q, &Q_preload)); 39891982731SJeremy L Thompson PetscCall(VecCopy(*Q, Q_preload)); 39991982731SJeremy L Thompson PetscCall(TSGetSNES(*ts, &snes)); 40091982731SJeremy L Thompson PetscCall(SNESGetTolerances(snes, NULL, &rtol, NULL, NULL, NULL)); 4012b916ea7SJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, .99, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 40222c1b34eSJames Wright PetscCall(TSSetSolution(*ts, Q_preload)); 40391982731SJeremy L Thompson PetscCall(TSStep(*ts)); 4042b916ea7SJeremy L Thompson PetscCall(SNESSetTolerances(snes, PETSC_DEFAULT, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 40591982731SJeremy L Thompson PetscCall(VecDestroy(&Q_preload)); 40691982731SJeremy L Thompson // LCOV_EXCL_STOP 40791982731SJeremy L Thompson } else { 4082b916ea7SJeremy L Thompson PetscCall(PetscBarrier((PetscObject)*ts)); 4092b916ea7SJeremy L Thompson PetscCall(TSSolve(*ts, *Q)); 41091982731SJeremy L Thompson } 41191982731SJeremy L Thompson PetscPreLoadEnd(); 41291982731SJeremy L Thompson 41391982731SJeremy L Thompson PetscCall(TSGetSolveTime(*ts, &final_time)); 414a515125bSLeila Ghaffari *f_time = final_time; 41591982731SJeremy L Thompson 4160e1e9333SJames Wright if (app_ctx->test_type == TESTTYPE_NONE) { 4177538d537SJames Wright PetscInt step_no; 4187538d537SJames Wright PetscCall(TSGetStepNumber(*ts, &step_no)); 419b0488d1fSJames Wright if (user->app_ctx->checkpoint_interval > 0 || user->app_ctx->checkpoint_interval == -1) { 4207538d537SJames Wright PetscCall(WriteOutput(user, *Q, step_no, final_time)); 4217538d537SJames Wright } 4227538d537SJames Wright 4237eedc94cSJames Wright PetscLogStage stage_id; 424df4304b5SJed Brown PetscEventPerfInfo stage_perf; 42591982731SJeremy L Thompson 42691982731SJeremy L Thompson PetscCall(PetscLogStageGetId("Fluids Solve", &stage_id)); 427df4304b5SJed Brown PetscCall(PetscLogStageGetPerfInfo(stage_id, &stage_perf)); 428df4304b5SJed Brown PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Time taken for solution (sec): %g\n", stage_perf.time)); 429a515125bSLeila Ghaffari } 430d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 431a515125bSLeila Ghaffari } 432