xref: /honee/problems/bc_outflow.c (revision 337840fc9f8b699b98947f09e30443f8c0c7f962)
1224fc8c8SJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2025, HONEE contributors.
2224fc8c8SJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3224fc8c8SJames Wright 
4224fc8c8SJames Wright /// @file
5224fc8c8SJames Wright /// Utility functions for setting up Freestream boundary condition
6224fc8c8SJames Wright 
7224fc8c8SJames Wright #include "../qfunctions/bc_outflow.h"
8224fc8c8SJames Wright 
9224fc8c8SJames Wright #include <ceed.h>
10224fc8c8SJames Wright #include <petscdm.h>
11224fc8c8SJames Wright 
12224fc8c8SJames Wright #include <navierstokes.h>
13224fc8c8SJames Wright #include "../qfunctions/newtonian_types.h"
14224fc8c8SJames Wright 
15224fc8c8SJames Wright static const char *const OutflowTypes[] = {"RIEMANN", "PRESSURE", "OutflowType", "OUTFLOW_", NULL};
16224fc8c8SJames Wright typedef enum {
17224fc8c8SJames Wright   OUTFLOW_RIEMANN,
18224fc8c8SJames Wright   OUTFLOW_PRESSURE,
19224fc8c8SJames Wright } OutflowType;
20224fc8c8SJames Wright 
21f978755dSJames Wright typedef struct {
22f978755dSJames Wright   OutflowType outflow_type;
23f978755dSJames Wright } *OutflowHoneeBCCtx;
24f978755dSJames Wright 
OutflowBCSetup_CreateIFunctionQF(BCDefinition bc_def,CeedQFunction * qf)25f978755dSJames Wright static PetscErrorCode OutflowBCSetup_CreateIFunctionQF(BCDefinition bc_def, CeedQFunction *qf) {
26f978755dSJames Wright   Honee         honee;
27f978755dSJames Wright   HoneeBCStruct honee_bc;
28f978755dSJames Wright 
29f978755dSJames Wright   PetscFunctionBeginUser;
30f978755dSJames Wright   PetscCall(BCDefinitionGetContext(bc_def, &honee_bc));
31f978755dSJames Wright   honee                              = honee_bc->honee;
32f978755dSJames Wright   OutflowHoneeBCCtx outflow_honee_bc = (OutflowHoneeBCCtx)honee_bc->ctx;
33f978755dSJames Wright 
34f978755dSJames Wright   switch (honee->phys->state_var) {
35f978755dSJames Wright     case STATEVAR_CONSERVATIVE:
36f978755dSJames Wright       switch (outflow_honee_bc->outflow_type) {
37f978755dSJames Wright         case OUTFLOW_RIEMANN:
38f978755dSJames Wright           PetscCall(HoneeBCCreateIFunctionQF(bc_def, RiemannOutflow_Conserv, RiemannOutflow_Conserv_loc, honee_bc->qfctx, qf));
39f978755dSJames Wright           break;
40f978755dSJames Wright         case OUTFLOW_PRESSURE:
41f978755dSJames Wright           PetscCall(HoneeBCCreateIFunctionQF(bc_def, PressureOutflow_Conserv, PressureOutflow_Conserv_loc, honee_bc->qfctx, qf));
42f978755dSJames Wright           break;
43f978755dSJames Wright       }
44f978755dSJames Wright       break;
45f978755dSJames Wright     case STATEVAR_PRIMITIVE:
46f978755dSJames Wright       switch (outflow_honee_bc->outflow_type) {
47f978755dSJames Wright         case OUTFLOW_RIEMANN:
48f978755dSJames Wright           PetscCall(HoneeBCCreateIFunctionQF(bc_def, RiemannOutflow_Prim, RiemannOutflow_Prim_loc, honee_bc->qfctx, qf));
49f978755dSJames Wright           break;
50f978755dSJames Wright         case OUTFLOW_PRESSURE:
51f978755dSJames Wright           PetscCall(HoneeBCCreateIFunctionQF(bc_def, PressureOutflow_Prim, PressureOutflow_Prim_loc, honee_bc->qfctx, qf));
52f978755dSJames Wright           break;
53f978755dSJames Wright       }
54f978755dSJames Wright       break;
55f978755dSJames Wright     case STATEVAR_ENTROPY:
56f978755dSJames Wright       switch (outflow_honee_bc->outflow_type) {
57f978755dSJames Wright         case OUTFLOW_RIEMANN:
58f978755dSJames Wright           PetscCall(HoneeBCCreateIFunctionQF(bc_def, RiemannOutflow_Entropy, RiemannOutflow_Entropy_loc, honee_bc->qfctx, qf));
59f978755dSJames Wright           break;
60f978755dSJames Wright         case OUTFLOW_PRESSURE:
61f978755dSJames Wright           PetscCall(HoneeBCCreateIFunctionQF(bc_def, PressureOutflow_Entropy, PressureOutflow_Entropy_loc, honee_bc->qfctx, qf));
62f978755dSJames Wright           break;
63f978755dSJames Wright       }
64f978755dSJames Wright       break;
65f978755dSJames Wright   }
66f978755dSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
67f978755dSJames Wright }
68f978755dSJames Wright 
OutflowBCSetup_CreateIJacobianQF(BCDefinition bc_def,CeedQFunction * qf)69f978755dSJames Wright static PetscErrorCode OutflowBCSetup_CreateIJacobianQF(BCDefinition bc_def, CeedQFunction *qf) {
70f978755dSJames Wright   Honee         honee;
71f978755dSJames Wright   HoneeBCStruct honee_bc;
72f978755dSJames Wright 
73f978755dSJames Wright   PetscFunctionBeginUser;
74f978755dSJames Wright   PetscCall(BCDefinitionGetContext(bc_def, &honee_bc));
75f978755dSJames Wright   honee                              = honee_bc->honee;
76f978755dSJames Wright   OutflowHoneeBCCtx outflow_honee_bc = (OutflowHoneeBCCtx)honee_bc->ctx;
77f978755dSJames Wright 
78f978755dSJames Wright   switch (honee->phys->state_var) {
79f978755dSJames Wright     case STATEVAR_CONSERVATIVE:
80f978755dSJames Wright       switch (outflow_honee_bc->outflow_type) {
81f978755dSJames Wright         case OUTFLOW_RIEMANN:
82f978755dSJames Wright           PetscCall(HoneeBCCreateIJacobianQF(bc_def, RiemannOutflow_Jacobian_Conserv, RiemannOutflow_Jacobian_Conserv_loc, honee_bc->qfctx, qf));
83f978755dSJames Wright           break;
84f978755dSJames Wright         case OUTFLOW_PRESSURE:
85f978755dSJames Wright           PetscCall(HoneeBCCreateIJacobianQF(bc_def, PressureOutflow_Jacobian_Conserv, PressureOutflow_Jacobian_Conserv_loc, honee_bc->qfctx, qf));
86f978755dSJames Wright           break;
87f978755dSJames Wright       }
88f978755dSJames Wright       break;
89f978755dSJames Wright     case STATEVAR_PRIMITIVE:
90f978755dSJames Wright       switch (outflow_honee_bc->outflow_type) {
91f978755dSJames Wright         case OUTFLOW_RIEMANN:
92f978755dSJames Wright           PetscCall(HoneeBCCreateIJacobianQF(bc_def, RiemannOutflow_Jacobian_Prim, RiemannOutflow_Jacobian_Prim_loc, honee_bc->qfctx, qf));
93f978755dSJames Wright           break;
94f978755dSJames Wright         case OUTFLOW_PRESSURE:
95f978755dSJames Wright           PetscCall(HoneeBCCreateIJacobianQF(bc_def, PressureOutflow_Jacobian_Prim, PressureOutflow_Jacobian_Prim_loc, honee_bc->qfctx, qf));
96f978755dSJames Wright           break;
97f978755dSJames Wright       }
98f978755dSJames Wright       break;
99f978755dSJames Wright     case STATEVAR_ENTROPY:
100f978755dSJames Wright       switch (outflow_honee_bc->outflow_type) {
101f978755dSJames Wright         case OUTFLOW_RIEMANN:
102f978755dSJames Wright           PetscCall(HoneeBCCreateIJacobianQF(bc_def, RiemannOutflow_Jacobian_Entropy, RiemannOutflow_Jacobian_Entropy_loc, honee_bc->qfctx, qf));
103f978755dSJames Wright           break;
104f978755dSJames Wright         case OUTFLOW_PRESSURE:
105f978755dSJames Wright           PetscCall(HoneeBCCreateIJacobianQF(bc_def, PressureOutflow_Jacobian_Entropy, PressureOutflow_Jacobian_Entropy_loc, honee_bc->qfctx, qf));
106f978755dSJames Wright           break;
107f978755dSJames Wright       }
108f978755dSJames Wright       break;
109f978755dSJames Wright   }
110f978755dSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
111f978755dSJames Wright }
112f978755dSJames Wright 
OutflowBCSetup(BCDefinition bc_def,ProblemData problem,DM dm,void * ctx,NewtonianIdealGasContext newtonian_ig_ctx,const StatePrimitive * reference)113f978755dSJames Wright PetscErrorCode OutflowBCSetup(BCDefinition bc_def, ProblemData problem, DM dm, void *ctx, NewtonianIdealGasContext newtonian_ig_ctx,
114f978755dSJames Wright                               const StatePrimitive *reference) {
115224fc8c8SJames Wright   Honee                honee = *(Honee *)ctx;
116224fc8c8SJames Wright   Ceed                 ceed  = honee->ceed;
117224fc8c8SJames Wright   OutflowContext       outflow_ctx;
118224fc8c8SJames Wright   OutflowType          outflow_type = OUTFLOW_RIEMANN;
119224fc8c8SJames Wright   CeedQFunctionContext outflow_qfctx;
120c9f37605SMohammed Amin   Units                units = honee->units;
121f978755dSJames Wright   HoneeBCStruct        honee_bc;
122f978755dSJames Wright   OutflowHoneeBCCtx    outflow_honee_bc;
123224fc8c8SJames Wright 
124224fc8c8SJames Wright   PetscFunctionBeginUser;
125c9f37605SMohammed Amin   CeedScalar pressure    = reference->pressure / units->Pascal;
126c9f37605SMohammed Amin   CeedScalar temperature = reference->temperature / units->Kelvin;
127224fc8c8SJames Wright   CeedScalar recirc = 1, softplus_velocity = 1e-2;
128224fc8c8SJames Wright   PetscOptionsBegin(honee->comm, NULL, "Options for Outflow boundary condition", NULL);
1299eadbee4SJames Wright   PetscCall(PetscOptionsEnum("-outflow_type", "Type of outflow condition", NULL, OutflowTypes, (PetscEnum)outflow_type, (PetscEnum *)&outflow_type,
1309eadbee4SJames Wright                              NULL));
131224fc8c8SJames Wright   PetscCall(PetscOptionsScalar("-outflow_pressure", "Pressure at outflow condition", NULL, pressure, &pressure, NULL));
132224fc8c8SJames Wright   if (outflow_type == OUTFLOW_RIEMANN) {
133224fc8c8SJames Wright     PetscCall(PetscOptionsScalar("-outflow_temperature", "Temperature at outflow condition", NULL, temperature, &temperature, NULL));
1349eadbee4SJames Wright     PetscCall(PetscOptionsReal("-outflow_recirc", "Fraction of recirculation to allow in exterior velocity state [0,1]", NULL, recirc, &recirc,
1359eadbee4SJames Wright                                NULL));
136224fc8c8SJames Wright     PetscCall(PetscOptionsReal("-outflow_softplus_velocity", "Characteristic velocity of softplus regularization", NULL, softplus_velocity,
137224fc8c8SJames Wright                                &softplus_velocity, NULL));
138224fc8c8SJames Wright   }
139224fc8c8SJames Wright   PetscOptionsEnd();
140c9f37605SMohammed Amin   pressure *= units->Pascal;
141c9f37605SMohammed Amin   temperature *= units->Kelvin;
142224fc8c8SJames Wright 
143f978755dSJames Wright   PetscCall(PetscNew(&outflow_ctx));
144f5dc303cSJames Wright   *outflow_ctx = (struct OutflowContext_){
145cde3d787SJames Wright       .newt_ctx          = *newtonian_ig_ctx,
146f5dc303cSJames Wright       .recirc            = recirc,
147f5dc303cSJames Wright       .softplus_velocity = softplus_velocity,
148f5dc303cSJames Wright       .pressure          = pressure,
149f5dc303cSJames Wright       .temperature       = temperature,
150f5dc303cSJames Wright   };
151224fc8c8SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextCreate(honee->ceed, &outflow_qfctx));
152224fc8c8SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetData(outflow_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*outflow_ctx), outflow_ctx));
153224fc8c8SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(outflow_qfctx, CEED_MEM_HOST, FreeContextPetsc));
154f978755dSJames Wright 
155f978755dSJames Wright   PetscCall(PetscNew(&outflow_honee_bc));
156f978755dSJames Wright   outflow_honee_bc->outflow_type = outflow_type;
157f5dc303cSJames Wright   PetscCall(PetscNew(&honee_bc));
158f5dc303cSJames Wright   *honee_bc = (struct HoneeBCStruct_){
159f5dc303cSJames Wright       .ctx                = outflow_honee_bc,
160f5dc303cSJames Wright       .DestroyCtx         = PetscCtxDestroyDefault,
161f5dc303cSJames Wright       .honee              = honee,
162f5dc303cSJames Wright       .num_comps_jac_data = honee->phys->implicit ? 11 : 0,
163f5dc303cSJames Wright       .qfctx              = outflow_qfctx,
164f5dc303cSJames Wright   };
165*26d401f3SJames Wright   PetscCall(BCDefinitionSetContext(bc_def, (PetscCtxDestroyFn *)HoneeBCDestroy, honee_bc));
166f978755dSJames Wright 
167f978755dSJames Wright   PetscCall(BCDefinitionSetIFunction(bc_def, OutflowBCSetup_CreateIFunctionQF, HoneeBCAddIFunctionOp));
168f978755dSJames Wright   PetscCall(BCDefinitionSetIJacobian(bc_def, OutflowBCSetup_CreateIJacobianQF, HoneeBCAddIJacobianOp));
169224fc8c8SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
170224fc8c8SJames Wright }
171