xref: /honee/problems/densitycurrent.c (revision 2b916ea7fa53b5c2584160b9274b1b14ca18ff4f)
1bb8a0c61SJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other
2bb8a0c61SJames Wright // CEED contributors. All Rights Reserved. See the top-level LICENSE and NOTICE
3bb8a0c61SJames Wright // files for details.
4a515125bSLeila Ghaffari //
5727da7e7SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
6a515125bSLeila Ghaffari //
7727da7e7SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
8a515125bSLeila Ghaffari 
9a515125bSLeila Ghaffari /// @file
10a515125bSLeila Ghaffari /// Utility functions for setting up DENSITY_CURRENT
11a515125bSLeila Ghaffari 
12a515125bSLeila Ghaffari #include "../qfunctions/densitycurrent.h"
13*2b916ea7SJeremy L Thompson 
14bb8a0c61SJames Wright #include "../navierstokes.h"
15a515125bSLeila Ghaffari 
16b7f03f12SJed Brown PetscErrorCode NS_DENSITY_CURRENT(ProblemData *problem, DM dm, void *ctx) {
17a515125bSLeila Ghaffari   MPI_Comm                 comm = PETSC_COMM_WORLD;
18cbe60e31SLeila Ghaffari   User                     user = *(User *)ctx;
19cbe60e31SLeila Ghaffari   DensityCurrentContext    dc_ctx;
20cbe60e31SLeila Ghaffari   CeedQFunctionContext     density_current_context;
21cbe60e31SLeila Ghaffari   NewtonianIdealGasContext newtonian_ig_ctx;
22a515125bSLeila Ghaffari 
23b7f03f12SJed Brown   PetscFunctionBeginUser;
24*2b916ea7SJeremy L Thompson   PetscCall(NS_NEWTONIAN_IG(problem, dm, ctx));
25*2b916ea7SJeremy L Thompson   PetscCall(PetscCalloc1(1, &dc_ctx));
26a515125bSLeila Ghaffari   // ------------------------------------------------------
27a515125bSLeila Ghaffari   //               SET UP DENSITY_CURRENT
28a515125bSLeila Ghaffari   // ------------------------------------------------------
29cbe60e31SLeila Ghaffari   CeedQFunctionContextDestroy(&problem->ics.qfunction_context);
309785fe93SJed Brown   problem->ics.qfunction     = ICsDC;
319785fe93SJed Brown   problem->ics.qfunction_loc = ICsDC_loc;
32a515125bSLeila Ghaffari 
33a515125bSLeila Ghaffari   // ------------------------------------------------------
34a515125bSLeila Ghaffari   //             Create the libCEED context
35a515125bSLeila Ghaffari   // ------------------------------------------------------
36bb8a0c61SJames Wright   CeedScalar theta0 = 300.;   // K
37bb8a0c61SJames Wright   CeedScalar thetaC = -15.;   // K
38bb8a0c61SJames Wright   CeedScalar P0     = 1.e5;   // Pa
39bb8a0c61SJames Wright   CeedScalar N      = 0.01;   // 1/s
40a515125bSLeila Ghaffari   CeedScalar rc     = 1000.;  // m (Radius of bubble)
41a515125bSLeila Ghaffari   PetscReal  center[3], dc_axis[3] = {0, 0, 0};
4205a512bdSLeila Ghaffari   PetscReal  domain_min[3], domain_max[3], domain_size[3];
43*2b916ea7SJeremy L Thompson   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
44*2b916ea7SJeremy L Thompson   for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i];
45a515125bSLeila Ghaffari 
46a515125bSLeila Ghaffari   // ------------------------------------------------------
47a515125bSLeila Ghaffari   //              Command line Options
48a515125bSLeila Ghaffari   // ------------------------------------------------------
491485969bSJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for DENSITY_CURRENT problem", NULL);
50*2b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-theta0", "Reference potential temperature", NULL, theta0, &theta0, NULL));
51*2b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-thetaC", "Perturbation of potential temperature", NULL, thetaC, &thetaC, NULL));
52*2b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-P0", "Atmospheric pressure", NULL, P0, &P0, NULL));
53*2b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-N", "Brunt-Vaisala frequency", NULL, N, &N, NULL));
54*2b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble", NULL, rc, &rc, NULL));
55*2b916ea7SJeremy L Thompson   for (PetscInt i = 0; i < 3; i++) center[i] = .5 * domain_size[i];
56a515125bSLeila Ghaffari   PetscInt n = problem->dim;
57*2b916ea7SJeremy L Thompson   PetscCall(PetscOptionsRealArray("-center", "Location of bubble center", NULL, center, &n, NULL));
58a515125bSLeila Ghaffari   n = problem->dim;
59*2b916ea7SJeremy L Thompson   PetscCall(PetscOptionsRealArray("-dc_axis",
60bb8a0c61SJames Wright                                   "Axis of density current cylindrical anomaly, "
61bb8a0c61SJames Wright                                   "or {0,0,0} for spherically symmetric",
62*2b916ea7SJeremy L Thompson                                   NULL, dc_axis, &n, NULL));
63a515125bSLeila Ghaffari   {
64*2b916ea7SJeremy L Thompson     PetscReal norm = PetscSqrtReal(PetscSqr(dc_axis[0]) + PetscSqr(dc_axis[1]) + PetscSqr(dc_axis[2]));
65a515125bSLeila Ghaffari     if (norm > 0) {
66*2b916ea7SJeremy L Thompson       for (PetscInt i = 0; i < 3; i++) dc_axis[i] /= norm;
67a515125bSLeila Ghaffari     }
68a515125bSLeila Ghaffari   }
69a515125bSLeila Ghaffari 
701485969bSJeremy L Thompson   PetscOptionsEnd();
71a515125bSLeila Ghaffari 
723a8779fbSJames Wright   PetscScalar meter  = user->units->meter;
73bb8a0c61SJames Wright   PetscScalar second = user->units->second;
74bb8a0c61SJames Wright   PetscScalar Kelvin = user->units->Kelvin;
75bb8a0c61SJames Wright   PetscScalar Pascal = user->units->Pascal;
76a515125bSLeila Ghaffari   rc                 = fabs(rc) * meter;
77bb8a0c61SJames Wright   theta0 *= Kelvin;
78bb8a0c61SJames Wright   thetaC *= Kelvin;
79bb8a0c61SJames Wright   P0 *= Pascal;
80bb8a0c61SJames Wright   N *= (1. / second);
81*2b916ea7SJeremy L Thompson   for (PetscInt i = 0; i < 3; i++) center[i] *= meter;
82a515125bSLeila Ghaffari 
83cbe60e31SLeila Ghaffari   dc_ctx->theta0     = theta0;
84cbe60e31SLeila Ghaffari   dc_ctx->thetaC     = thetaC;
85cbe60e31SLeila Ghaffari   dc_ctx->P0         = P0;
86cbe60e31SLeila Ghaffari   dc_ctx->N          = N;
87cbe60e31SLeila Ghaffari   dc_ctx->rc         = rc;
88cbe60e31SLeila Ghaffari   dc_ctx->center[0]  = center[0];
89cbe60e31SLeila Ghaffari   dc_ctx->center[1]  = center[1];
90cbe60e31SLeila Ghaffari   dc_ctx->center[2]  = center[2];
91cbe60e31SLeila Ghaffari   dc_ctx->dc_axis[0] = dc_axis[0];
92cbe60e31SLeila Ghaffari   dc_ctx->dc_axis[1] = dc_axis[1];
93cbe60e31SLeila Ghaffari   dc_ctx->dc_axis[2] = dc_axis[2];
94a515125bSLeila Ghaffari 
95*2b916ea7SJeremy L Thompson   CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, CEED_MEM_HOST, &newtonian_ig_ctx);
96cbe60e31SLeila Ghaffari   dc_ctx->newtonian_ctx = *newtonian_ig_ctx;
97*2b916ea7SJeremy L Thompson   CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, &newtonian_ig_ctx);
98cbe60e31SLeila Ghaffari   CeedQFunctionContextCreate(user->ceed, &density_current_context);
99*2b916ea7SJeremy L Thompson   CeedQFunctionContextSetData(density_current_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*dc_ctx), dc_ctx);
100*2b916ea7SJeremy L Thompson   CeedQFunctionContextSetDataDestroy(density_current_context, CEED_MEM_HOST, FreeContextPetsc);
101cbe60e31SLeila Ghaffari   problem->ics.qfunction_context = density_current_context;
102270bbb13SJeremy L Thompson 
103a515125bSLeila Ghaffari   PetscFunctionReturn(0);
104a515125bSLeila Ghaffari }
105