1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors. 2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause 3bb8a0c61SJames Wright 4bb8a0c61SJames Wright /// @file 5bb8a0c61SJames Wright /// Utility functions for setting up Channel flow 6bb8a0c61SJames Wright 7bb8a0c61SJames Wright #include "../qfunctions/channel.h" 8bb8a0c61SJames Wright 9e419654dSJeremy L Thompson #include <ceed.h> 10e419654dSJeremy L Thompson #include <petscdm.h> 11e419654dSJeremy L Thompson 12149fb536SJames Wright #include <navierstokes.h> 13bb8a0c61SJames Wright 14b3b24828SJames Wright static PetscErrorCode DivDiffFluxVerifyMesh(DM dm); 15b3b24828SJames Wright 16f978755dSJames Wright static PetscErrorCode ChannelOutflowBCSetup_CreateIFunctionQF(BCDefinition bc_def, CeedQFunction *qf) { 17f978755dSJames Wright HoneeBCStruct honee_bc; 18f978755dSJames Wright 19f978755dSJames Wright PetscFunctionBeginUser; 20f978755dSJames Wright PetscCall(BCDefinitionGetContext(bc_def, &honee_bc)); 21f978755dSJames Wright PetscCheck(honee_bc->honee->phys->state_var == STATEVAR_CONSERVATIVE, PETSC_COMM_WORLD, PETSC_ERR_SUP, 22f978755dSJames Wright "Channel outflow only valid for Conservative variables, recieved %s", StateVariables[honee_bc->honee->phys->state_var]); 23f978755dSJames Wright PetscCall(HoneeBCCreateIFunctionQF(bc_def, Channel_Outflow, Channel_Outflow_loc, honee_bc->qfctx, qf)); 24f978755dSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 25f978755dSJames Wright } 26f978755dSJames Wright 27d6cac220SJames Wright static PetscErrorCode ChannelInflowBCSetup_CreateIFunctionQF(BCDefinition bc_def, CeedQFunction *qf) { 28d6cac220SJames Wright HoneeBCStruct honee_bc; 29d6cac220SJames Wright 30d6cac220SJames Wright PetscFunctionBeginUser; 31d6cac220SJames Wright PetscCall(BCDefinitionGetContext(bc_def, &honee_bc)); 32d6cac220SJames Wright PetscCheck(honee_bc->honee->phys->state_var == STATEVAR_CONSERVATIVE, PETSC_COMM_WORLD, PETSC_ERR_SUP, 33d6cac220SJames Wright "Channel inflow only valid for Conservative variables, recieved %s", StateVariables[honee_bc->honee->phys->state_var]); 34d6cac220SJames Wright PetscCall(HoneeBCCreateIFunctionQF(bc_def, Channel_Inflow, Channel_Inflow_loc, honee_bc->qfctx, qf)); 35d6cac220SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 36d6cac220SJames Wright } 37d6cac220SJames Wright 38d3c60affSJames Wright PetscErrorCode NS_CHANNEL(ProblemData problem, DM dm, void *ctx) { 390c373b74SJames Wright Honee honee = *(Honee *)ctx; 400c373b74SJames Wright MPI_Comm comm = honee->comm; 410c373b74SJames Wright Ceed ceed = honee->ceed; 4215a3537eSJed Brown ChannelContext channel_ctx; 4315a3537eSJed Brown NewtonianIdealGasContext newtonian_ig_ctx; 44e07531f7SJames Wright CeedQFunctionContext channel_qfctx; 45b3b24828SJames Wright PetscBool use_divdiff_verify_mesh = PETSC_FALSE; 4615a3537eSJed Brown 47bb8a0c61SJames Wright PetscFunctionBeginUser; 48d3c60affSJames Wright PetscCall(NS_NEWTONIAN_IG(problem, dm, ctx)); 49*2d898fa6SJames Wright PetscCall(PetscNew(&channel_ctx)); 50bb8a0c61SJames Wright 51b3b24828SJames Wright PetscCall(PetscOptionsGetBool(NULL, NULL, "-mesh_transform_channel_div_diff_projection_verify", &use_divdiff_verify_mesh, NULL)); 52b3b24828SJames Wright if (use_divdiff_verify_mesh) PetscCall(DivDiffFluxVerifyMesh(dm)); 53b3b24828SJames Wright 54bb8a0c61SJames Wright // ------------------------------------------------------ 55bb8a0c61SJames Wright // SET UP Channel 56bb8a0c61SJames Wright // ------------------------------------------------------ 57e07531f7SJames Wright PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->ics.qfctx)); 58e07531f7SJames Wright problem->ics.qf_func_ptr = ICsChannel; 59e07531f7SJames Wright problem->ics.qf_loc = ICsChannel_loc; 60bb8a0c61SJames Wright 61bb8a0c61SJames Wright // -- Command Line Options 62bb8a0c61SJames Wright CeedScalar umax = 10.; // m/s 63bb8a0c61SJames Wright CeedScalar theta0 = 300.; // K 64bb8a0c61SJames Wright CeedScalar P0 = 1.e5; // Pa 6510786903SJed Brown PetscReal body_force_scale = 1.; 66bb8a0c61SJames Wright PetscOptionsBegin(comm, NULL, "Options for CHANNEL problem", NULL); 672b916ea7SJeremy L Thompson PetscCall(PetscOptionsScalar("-umax", "Centerline velocity of the Channel", NULL, umax, &umax, NULL)); 682b916ea7SJeremy L Thompson PetscCall(PetscOptionsScalar("-theta0", "Wall temperature", NULL, theta0, &theta0, NULL)); 692b916ea7SJeremy L Thompson PetscCall(PetscOptionsScalar("-P0", "Pressure at outflow", NULL, P0, &P0, NULL)); 702b916ea7SJeremy L Thompson PetscCall(PetscOptionsReal("-body_force_scale", "Multiplier for body force", NULL, body_force_scale = 1, &body_force_scale, NULL)); 71bb8a0c61SJames Wright PetscOptionsEnd(); 72bb8a0c61SJames Wright 73c9f37605SMohammed Amin Units units = honee->units; 74bb8a0c61SJames Wright 75c9f37605SMohammed Amin theta0 *= units->Kelvin; 76c9f37605SMohammed Amin P0 *= units->Pascal; 77c9f37605SMohammed Amin umax *= units->meter / units->second; 78bb8a0c61SJames Wright 79bb8a0c61SJames Wright //-- Setup Problem information 80bb8a0c61SJames Wright CeedScalar H, center; 81bb8a0c61SJames Wright { 82bb8a0c61SJames Wright PetscReal domain_min[3], domain_max[3], domain_size[3]; 832b916ea7SJeremy L Thompson PetscCall(DMGetBoundingBox(dm, domain_min, domain_max)); 84493642f1SJames Wright for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i]; 85bb8a0c61SJames Wright 86c9f37605SMohammed Amin H = 0.5 * domain_size[1] * units->meter; 87c9f37605SMohammed Amin center = H + domain_min[1] * units->meter; 88bb8a0c61SJames Wright } 89bb8a0c61SJames Wright 9015a3537eSJed Brown // Some properties depend on parameters from NewtonianIdealGas 91e07531f7SJames Wright PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->apply_vol_rhs.qfctx, CEED_MEM_HOST, &newtonian_ig_ctx)); 9215a3537eSJed Brown 9315a3537eSJed Brown channel_ctx->center = center; 9415a3537eSJed Brown channel_ctx->H = H; 9515a3537eSJed Brown channel_ctx->theta0 = theta0; 9615a3537eSJed Brown channel_ctx->P0 = P0; 9715a3537eSJed Brown channel_ctx->umax = umax; 980c373b74SJames Wright channel_ctx->implicit = honee->phys->implicit; 9910786903SJed Brown channel_ctx->B = body_force_scale * 2 * umax * newtonian_ig_ctx->mu / (H * H); 100bb8a0c61SJames Wright 101bb8a0c61SJames Wright { 102bb8a0c61SJames Wright // Calculate Body force 1032b916ea7SJeremy L Thompson CeedScalar cv = newtonian_ig_ctx->cv, cp = newtonian_ig_ctx->cp; 104bb8a0c61SJames Wright CeedScalar Rd = cp - cv; 105bb8a0c61SJames Wright CeedScalar rho = P0 / (Rd * theta0); 10615a3537eSJed Brown CeedScalar g[] = {channel_ctx->B / rho, 0., 0.}; 1072b916ea7SJeremy L Thompson PetscCall(PetscArraycpy(newtonian_ig_ctx->g, g, 3)); 108bb8a0c61SJames Wright } 10915a3537eSJed Brown channel_ctx->newtonian_ctx = *newtonian_ig_ctx; 110e07531f7SJames Wright PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfctx, &newtonian_ig_ctx)); 111bb8a0c61SJames Wright 1120c373b74SJames Wright PetscCallCeed(ceed, CeedQFunctionContextCreate(honee->ceed, &channel_qfctx)); 113e07531f7SJames Wright PetscCallCeed(ceed, CeedQFunctionContextSetData(channel_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*channel_ctx), channel_ctx)); 114e07531f7SJames Wright PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(channel_qfctx, CEED_MEM_HOST, FreeContextPetsc)); 115e07531f7SJames Wright problem->ics.qfctx = channel_qfctx; 116f978755dSJames Wright 117f978755dSJames Wright for (PetscCount b = 0; b < problem->num_bc_defs; b++) { 118f978755dSJames Wright BCDefinition bc_def = problem->bc_defs[b]; 119f978755dSJames Wright const char *name; 120f978755dSJames Wright 121f978755dSJames Wright PetscCall(BCDefinitionGetInfo(bc_def, &name, NULL, NULL)); 122f978755dSJames Wright if (honee->phys->state_var == STATEVAR_CONSERVATIVE && !strcmp(name, "outflow")) { 123f978755dSJames Wright HoneeBCStruct honee_bc; 124f978755dSJames Wright 125f978755dSJames Wright PetscCall(PetscPrintf(comm, "WARNING! Channel flow with Inflow and Outflow is currently broken.\n")); 126f978755dSJames Wright PetscCall(PetscNew(&honee_bc)); 127f978755dSJames Wright PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(channel_qfctx, &honee_bc->qfctx)); 128f978755dSJames Wright honee_bc->honee = honee; 1291abc2837SJames Wright honee_bc->num_comps_jac_data = honee->phys->implicit ? 11 : 0; 130f978755dSJames Wright PetscCall(BCDefinitionSetContext(bc_def, HoneeBCDestroy, honee_bc)); 131f978755dSJames Wright 132f978755dSJames Wright PetscCall(BCDefinitionSetIFunction(bc_def, ChannelOutflowBCSetup_CreateIFunctionQF, HoneeBCAddIFunctionOp)); 133f978755dSJames Wright PetscCall(BCDefinitionSetIJacobian(bc_def, NULL, NULL)); 134d6cac220SJames Wright } else if (honee->phys->state_var == STATEVAR_CONSERVATIVE && !strcmp(name, "inflow")) { 135d6cac220SJames Wright HoneeBCStruct honee_bc; 136d6cac220SJames Wright 137d6cac220SJames Wright PetscCall(PetscPrintf(comm, "WARNING! Channel flow with Inflow and Outflow is currently broken.\n")); 138d6cac220SJames Wright PetscCall(PetscNew(&honee_bc)); 139d6cac220SJames Wright PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(channel_qfctx, &honee_bc->qfctx)); 140d6cac220SJames Wright honee_bc->honee = honee; 1411abc2837SJames Wright honee_bc->num_comps_jac_data = honee->phys->implicit ? 11 : 0; 142d6cac220SJames Wright PetscCall(BCDefinitionSetContext(bc_def, HoneeBCDestroy, honee_bc)); 143d6cac220SJames Wright 144d6cac220SJames Wright PetscCall(BCDefinitionSetIFunction(bc_def, ChannelInflowBCSetup_CreateIFunctionQF, HoneeBCAddIFunctionOp)); 145d6cac220SJames Wright PetscCall(BCDefinitionSetIJacobian(bc_def, NULL, NULL)); 146f978755dSJames Wright } 147f978755dSJames Wright } 148d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 149bb8a0c61SJames Wright } 150b3b24828SJames Wright 151b3b24828SJames Wright // This function transforms the mesh coordinates to mimic the mesh used in 152b3b24828SJames Wright // *A better consistency for low-order stabilized finite element methods* Jansen et. al. 1999 153b3b24828SJames Wright // which is used to verify the projection of divergence of diffusive flux. See !27 and !94 for more details. 154b3b24828SJames Wright static PetscErrorCode DivDiffFluxVerifyMesh(DM dm) { 155b3b24828SJames Wright PetscInt narr, ncoords, dim; 156b3b24828SJames Wright PetscReal domain_min[3], domain_max[3], domain_size[3]; 157b3b24828SJames Wright PetscScalar *arr_coords; 158b3b24828SJames Wright Vec vec_coords; 159b3b24828SJames Wright 160b3b24828SJames Wright PetscFunctionBeginUser; 161b3b24828SJames Wright PetscCall(DMGetDimension(dm, &dim)); 162b3b24828SJames Wright // Get domain boundary information 163b3b24828SJames Wright PetscCall(DMGetBoundingBox(dm, domain_min, domain_max)); 164b3b24828SJames Wright for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i]; 165b3b24828SJames Wright 166b3b24828SJames Wright // Get coords array from DM 167b3b24828SJames Wright PetscCall(DMGetCoordinatesLocal(dm, &vec_coords)); 168b3b24828SJames Wright PetscCall(VecGetLocalSize(vec_coords, &narr)); 169b3b24828SJames Wright PetscCall(VecGetArray(vec_coords, &arr_coords)); 170b3b24828SJames Wright 171b3b24828SJames Wright PetscScalar(*coords)[dim] = (PetscScalar(*)[dim])arr_coords; 172b3b24828SJames Wright ncoords = narr / dim; 173b3b24828SJames Wright 174b3b24828SJames Wright // Get mesh information 175b3b24828SJames Wright PetscInt nmax = 3, faces[3]; 176b3b24828SJames Wright PetscCall(PetscOptionsGetIntArray(NULL, NULL, "-dm_plex_box_faces", faces, &nmax, NULL)); 177b3b24828SJames Wright // Get element size of the box mesh, for indexing each node 178b3b24828SJames Wright const PetscReal dxbox = domain_size[0] / (faces[0]); 179b3b24828SJames Wright 180b3b24828SJames Wright for (PetscInt i = 0; i < ncoords; i++) { 181b3b24828SJames Wright PetscInt x_box_index = round(coords[i][0] / dxbox); 182b3b24828SJames Wright if (x_box_index % 2) { 183b3b24828SJames Wright coords[i][0] = (x_box_index - 1) * dxbox + 0.5 * dxbox; 184b3b24828SJames Wright } 185b3b24828SJames Wright } 186b3b24828SJames Wright 187b3b24828SJames Wright PetscCall(VecRestoreArray(vec_coords, &arr_coords)); 188b3b24828SJames Wright PetscCall(DMSetCoordinatesLocal(dm, vec_coords)); 189b3b24828SJames Wright PetscFunctionReturn(0); 190b3b24828SJames Wright } 191