xref: /honee/problems/densitycurrent.c (revision 255e1892bd5386028f31cac4bde39f1208dcc3b9)
1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3a515125bSLeila Ghaffari 
4a515125bSLeila Ghaffari /// @file
5a515125bSLeila Ghaffari /// Utility functions for setting up DENSITY_CURRENT
6a515125bSLeila Ghaffari 
7a515125bSLeila Ghaffari #include "../qfunctions/densitycurrent.h"
82b916ea7SJeremy L Thompson 
9e419654dSJeremy L Thompson #include <ceed.h>
10e419654dSJeremy L Thompson #include <petscdm.h>
11e419654dSJeremy L Thompson 
12149fb536SJames Wright #include <navierstokes.h>
13a515125bSLeila Ghaffari 
NS_DENSITY_CURRENT(ProblemData problem,DM dm,void * ctx)14d3c60affSJames Wright PetscErrorCode NS_DENSITY_CURRENT(ProblemData problem, DM dm, void *ctx) {
150c373b74SJames Wright   Honee                    honee = *(Honee *)ctx;
160c373b74SJames Wright   MPI_Comm                 comm  = honee->comm;
170c373b74SJames Wright   Ceed                     ceed  = honee->ceed;
18e07531f7SJames Wright   DensityCurrentContext    density_current_ctx;
19e07531f7SJames Wright   CeedQFunctionContext     density_current_qfctx;
20cbe60e31SLeila Ghaffari   NewtonianIdealGasContext newtonian_ig_ctx;
2166d54740SJames Wright   PetscInt                 dim;
22a515125bSLeila Ghaffari 
23b7f03f12SJed Brown   PetscFunctionBeginUser;
24d3c60affSJames Wright   PetscCall(NS_NEWTONIAN_IG(problem, dm, ctx));
252d898fa6SJames Wright   PetscCall(PetscNew(&density_current_ctx));
26a515125bSLeila Ghaffari 
27a515125bSLeila Ghaffari   // ------------------------------------------------------
28ea615d4cSJames Wright   //             Create the QFunction context
29a515125bSLeila Ghaffari   // ------------------------------------------------------
30bb8a0c61SJames Wright   CeedScalar theta0 = 300.;   // K
31bb8a0c61SJames Wright   CeedScalar thetaC = -15.;   // K
32bb8a0c61SJames Wright   CeedScalar P0     = 1.e5;   // Pa
33bb8a0c61SJames Wright   CeedScalar N      = 0.01;   // 1/s
34a515125bSLeila Ghaffari   CeedScalar rc     = 1000.;  // m (Radius of bubble)
35a515125bSLeila Ghaffari   PetscReal  center[3], dc_axis[3] = {0, 0, 0};
3605a512bdSLeila Ghaffari   PetscReal  domain_min[3], domain_max[3], domain_size[3];
372b916ea7SJeremy L Thompson   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
3866d54740SJames Wright   PetscCall(DMGetDimension(dm, &dim));
3966d54740SJames Wright   for (PetscInt i = 0; i < dim; i++) domain_size[i] = domain_max[i] - domain_min[i];
40a515125bSLeila Ghaffari 
41a515125bSLeila Ghaffari   // ------------------------------------------------------
42a515125bSLeila Ghaffari   //              Command line Options
43a515125bSLeila Ghaffari   // ------------------------------------------------------
441485969bSJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for DENSITY_CURRENT problem", NULL);
452b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-theta0", "Reference potential temperature", NULL, theta0, &theta0, NULL));
462b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-thetaC", "Perturbation of potential temperature", NULL, thetaC, &thetaC, NULL));
472b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-P0", "Atmospheric pressure", NULL, P0, &P0, NULL));
482b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-N", "Brunt-Vaisala frequency", NULL, N, &N, NULL));
492b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble", NULL, rc, &rc, NULL));
5066d54740SJames Wright   for (PetscInt i = 0; i < dim; i++) center[i] = .5 * domain_size[i];
5166d54740SJames Wright   PetscInt n = dim;
522b916ea7SJeremy L Thompson   PetscCall(PetscOptionsRealArray("-center", "Location of bubble center", NULL, center, &n, NULL));
5366d54740SJames Wright   n = dim;
542b916ea7SJeremy L Thompson   PetscCall(PetscOptionsRealArray("-dc_axis",
55bb8a0c61SJames Wright                                   "Axis of density current cylindrical anomaly, "
56bb8a0c61SJames Wright                                   "or {0,0,0} for spherically symmetric",
572b916ea7SJeremy L Thompson                                   NULL, dc_axis, &n, NULL));
58a515125bSLeila Ghaffari   {
592b916ea7SJeremy L Thompson     PetscReal norm = PetscSqrtReal(PetscSqr(dc_axis[0]) + PetscSqr(dc_axis[1]) + PetscSqr(dc_axis[2]));
60a515125bSLeila Ghaffari     if (norm > 0) {
6166d54740SJames Wright       for (PetscInt i = 0; i < dim; i++) dc_axis[i] /= norm;
62a515125bSLeila Ghaffari     }
63a515125bSLeila Ghaffari   }
64a515125bSLeila Ghaffari 
651485969bSJeremy L Thompson   PetscOptionsEnd();
66a515125bSLeila Ghaffari 
67c9f37605SMohammed Amin   Units units = honee->units;
68c9f37605SMohammed Amin 
69c9f37605SMohammed Amin   rc = fabs(rc) * units->meter;
70c9f37605SMohammed Amin   theta0 *= units->Kelvin;
71c9f37605SMohammed Amin   thetaC *= units->Kelvin;
72c9f37605SMohammed Amin   P0 *= units->Pascal;
73c9f37605SMohammed Amin   N *= (1. / units->second);
74c9f37605SMohammed Amin   for (PetscInt i = 0; i < dim; i++) center[i] *= units->meter;
75a515125bSLeila Ghaffari 
76e07531f7SJames Wright   density_current_ctx->theta0 = theta0;
77e07531f7SJames Wright   density_current_ctx->thetaC = thetaC;
78e07531f7SJames Wright   density_current_ctx->P0     = P0;
79e07531f7SJames Wright   density_current_ctx->N      = N;
80e07531f7SJames Wright   density_current_ctx->rc     = rc;
8166d54740SJames Wright   PetscCall(PetscArraycpy(density_current_ctx->center, center, 3));
8266d54740SJames Wright   PetscCall(PetscArraycpy(density_current_ctx->dc_axis, dc_axis, 3));
83a515125bSLeila Ghaffari 
84e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->apply_vol_rhs.qfctx, CEED_MEM_HOST, &newtonian_ig_ctx));
85cde3d787SJames Wright   density_current_ctx->newt_ctx = *newtonian_ig_ctx;
86e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfctx, &newtonian_ig_ctx));
870c373b74SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextCreate(honee->ceed, &density_current_qfctx));
88*9eadbee4SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetData(density_current_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*density_current_ctx),
89*9eadbee4SJames Wright                                                   density_current_ctx));
90e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(density_current_qfctx, CEED_MEM_HOST, FreeContextPetsc));
91f5dc303cSJames Wright 
92f5dc303cSJames Wright   // ------------------------------------------------------
93f5dc303cSJames Wright   //               SET UP DENSITY_CURRENT
94f5dc303cSJames Wright   // ------------------------------------------------------
95f5dc303cSJames Wright   PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->ics.qfctx));
96f5dc303cSJames Wright   problem->ics = (HoneeQFSpec){.qf_func_ptr = ICsDC, .qf_loc = ICsDC_loc, .qfctx = density_current_qfctx};
97d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
98a515125bSLeila Ghaffari }
99