xref: /libCEED/examples/fluids/problems/channel.c (revision 9ba83ac0e4b1fca39d6fa6737a318a9f0cbc172d)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
288626eedSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
388626eedSJames Wright //
488626eedSJames Wright // SPDX-License-Identifier: BSD-2-Clause
588626eedSJames Wright //
688626eedSJames Wright // This file is part of CEED:  http://github.com/ceed
788626eedSJames Wright 
888626eedSJames Wright /// @file
988626eedSJames Wright /// Utility functions for setting up Channel flow
1088626eedSJames Wright 
1188626eedSJames Wright #include "../qfunctions/channel.h"
1288626eedSJames Wright 
1349aac155SJeremy L Thompson #include <ceed.h>
1449aac155SJeremy L Thompson #include <petscdm.h>
1549aac155SJeremy L Thompson 
162b730f8bSJeremy L Thompson #include "../navierstokes.h"
1788626eedSJames Wright 
18731c13d7SJames Wright PetscErrorCode NS_CHANNEL(ProblemData problem, DM dm, void *ctx, SimpleBC bc) {
1988626eedSJames Wright   User                     user = *(User *)ctx;
20a424bcd0SJames Wright   MPI_Comm                 comm = user->comm;
21a424bcd0SJames Wright   Ceed                     ceed = user->ceed;
22841e4c73SJed Brown   ChannelContext           channel_ctx;
23841e4c73SJed Brown   NewtonianIdealGasContext newtonian_ig_ctx;
24841e4c73SJed Brown   CeedQFunctionContext     channel_context;
25841e4c73SJed Brown 
2688626eedSJames Wright   PetscFunctionBeginUser;
2746de7363SJames Wright   PetscCall(NS_NEWTONIAN_IG(problem, dm, ctx, bc));
282b730f8bSJeremy L Thompson   PetscCall(PetscCalloc1(1, &channel_ctx));
2988626eedSJames Wright 
3088626eedSJames Wright   // ------------------------------------------------------
3188626eedSJames Wright   //               SET UP Channel
3288626eedSJames Wright   // ------------------------------------------------------
33a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->ics.qfunction_context));
3491e5af17SJed Brown   problem->ics.qfunction     = ICsChannel;
3591e5af17SJed Brown   problem->ics.qfunction_loc = ICsChannel_loc;
3697baf651SJames Wright   if (user->phys->state_var == STATEVAR_CONSERVATIVE) {
3791e5af17SJed Brown     problem->apply_inflow.qfunction      = Channel_Inflow;
3891e5af17SJed Brown     problem->apply_inflow.qfunction_loc  = Channel_Inflow_loc;
3991e5af17SJed Brown     problem->apply_outflow.qfunction     = Channel_Outflow;
4091e5af17SJed Brown     problem->apply_outflow.qfunction_loc = Channel_Outflow_loc;
41dc805cc4SLeila Ghaffari   }
4288626eedSJames Wright 
4388626eedSJames Wright   // -- Command Line Options
4488626eedSJames Wright   CeedScalar umax             = 10.;   // m/s
4588626eedSJames Wright   CeedScalar theta0           = 300.;  // K
4688626eedSJames Wright   CeedScalar P0               = 1.e5;  // Pa
47e71202f6SJed Brown   PetscReal  body_force_scale = 1.;
4888626eedSJames Wright   PetscOptionsBegin(comm, NULL, "Options for CHANNEL problem", NULL);
492b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-umax", "Centerline velocity of the Channel", NULL, umax, &umax, NULL));
502b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-theta0", "Wall temperature", NULL, theta0, &theta0, NULL));
512b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-P0", "Pressure at outflow", NULL, P0, &P0, NULL));
522b730f8bSJeremy L Thompson   PetscCall(PetscOptionsReal("-body_force_scale", "Multiplier for body force", NULL, body_force_scale = 1, &body_force_scale, NULL));
5388626eedSJames Wright   PetscOptionsEnd();
5488626eedSJames Wright 
5588626eedSJames Wright   PetscScalar meter  = user->units->meter;
5688626eedSJames Wright   PetscScalar second = user->units->second;
5788626eedSJames Wright   PetscScalar Kelvin = user->units->Kelvin;
5888626eedSJames Wright   PetscScalar Pascal = user->units->Pascal;
5988626eedSJames Wright 
6088626eedSJames Wright   theta0 *= Kelvin;
6188626eedSJames Wright   P0 *= Pascal;
6288626eedSJames Wright   umax *= meter / second;
6388626eedSJames Wright 
6488626eedSJames Wright   //-- Setup Problem information
6588626eedSJames Wright   CeedScalar H, center;
6688626eedSJames Wright   {
6788626eedSJames Wright     PetscReal domain_min[3], domain_max[3], domain_size[3];
682b730f8bSJeremy L Thompson     PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
69ba6664aeSJames Wright     for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i];
7088626eedSJames Wright 
7188626eedSJames Wright     H      = 0.5 * domain_size[1] * meter;
7288626eedSJames Wright     center = H + domain_min[1] * meter;
7388626eedSJames Wright   }
7488626eedSJames Wright 
75841e4c73SJed Brown   // Some properties depend on parameters from NewtonianIdealGas
76a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, CEED_MEM_HOST, &newtonian_ig_ctx));
77841e4c73SJed Brown 
78841e4c73SJed Brown   channel_ctx->center   = center;
79841e4c73SJed Brown   channel_ctx->H        = H;
80841e4c73SJed Brown   channel_ctx->theta0   = theta0;
81841e4c73SJed Brown   channel_ctx->P0       = P0;
82841e4c73SJed Brown   channel_ctx->umax     = umax;
83841e4c73SJed Brown   channel_ctx->implicit = user->phys->implicit;
84e71202f6SJed Brown   channel_ctx->B        = body_force_scale * 2 * umax * newtonian_ig_ctx->mu / (H * H);
8588626eedSJames Wright 
8688626eedSJames Wright   {
8788626eedSJames Wright     // Calculate Body force
882b730f8bSJeremy L Thompson     CeedScalar cv = newtonian_ig_ctx->cv, cp = newtonian_ig_ctx->cp;
8988626eedSJames Wright     CeedScalar Rd  = cp - cv;
9088626eedSJames Wright     CeedScalar rho = P0 / (Rd * theta0);
91841e4c73SJed Brown     CeedScalar g[] = {channel_ctx->B / rho, 0., 0.};
922b730f8bSJeremy L Thompson     PetscCall(PetscArraycpy(newtonian_ig_ctx->g, g, 3));
9388626eedSJames Wright   }
94841e4c73SJed Brown   channel_ctx->newtonian_ctx = *newtonian_ig_ctx;
95a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, &newtonian_ig_ctx));
9688626eedSJames Wright 
97a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &channel_context));
98a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetData(channel_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*channel_ctx), channel_ctx));
99a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(channel_context, CEED_MEM_HOST, FreeContextPetsc));
100841e4c73SJed Brown 
101841e4c73SJed Brown   problem->ics.qfunction_context = channel_context;
102a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(channel_context, &problem->apply_inflow.qfunction_context));
103a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(channel_context, &problem->apply_outflow.qfunction_context));
104ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
10588626eedSJames Wright }
106