xref: /libCEED/examples/fluids/problems/densitycurrent.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
188626eedSJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other
288626eedSJames Wright // CEED contributors. All Rights Reserved. See the top-level LICENSE and NOTICE
388626eedSJames Wright // files for details.
477841947SLeila Ghaffari //
53d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
677841947SLeila Ghaffari //
73d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
877841947SLeila Ghaffari 
977841947SLeila Ghaffari /// @file
1077841947SLeila Ghaffari /// Utility functions for setting up DENSITY_CURRENT
1177841947SLeila Ghaffari 
1277841947SLeila Ghaffari #include "../qfunctions/densitycurrent.h"
13*2b730f8bSJeremy L Thompson 
1488626eedSJames Wright #include "../navierstokes.h"
1577841947SLeila Ghaffari 
16a0add3c9SJed Brown PetscErrorCode NS_DENSITY_CURRENT(ProblemData *problem, DM dm, void *ctx) {
1777841947SLeila Ghaffari   MPI_Comm                 comm = PETSC_COMM_WORLD;
18dc805cc4SLeila Ghaffari   User                     user = *(User *)ctx;
19dc805cc4SLeila Ghaffari   DensityCurrentContext    dc_ctx;
20dc805cc4SLeila Ghaffari   CeedQFunctionContext     density_current_context;
21dc805cc4SLeila Ghaffari   NewtonianIdealGasContext newtonian_ig_ctx;
2277841947SLeila Ghaffari 
23a0add3c9SJed Brown   PetscFunctionBeginUser;
24*2b730f8bSJeremy L Thompson   PetscCall(NS_NEWTONIAN_IG(problem, dm, ctx));
25*2b730f8bSJeremy L Thompson   PetscCall(PetscCalloc1(1, &dc_ctx));
2677841947SLeila Ghaffari   // ------------------------------------------------------
2777841947SLeila Ghaffari   //               SET UP DENSITY_CURRENT
2877841947SLeila Ghaffari   // ------------------------------------------------------
29dc805cc4SLeila Ghaffari   CeedQFunctionContextDestroy(&problem->ics.qfunction_context);
3091e5af17SJed Brown   problem->ics.qfunction     = ICsDC;
3191e5af17SJed Brown   problem->ics.qfunction_loc = ICsDC_loc;
3277841947SLeila Ghaffari 
3377841947SLeila Ghaffari   // ------------------------------------------------------
3477841947SLeila Ghaffari   //             Create the libCEED context
3577841947SLeila Ghaffari   // ------------------------------------------------------
3688626eedSJames Wright   CeedScalar theta0 = 300.;   // K
3788626eedSJames Wright   CeedScalar thetaC = -15.;   // K
3888626eedSJames Wright   CeedScalar P0     = 1.e5;   // Pa
3988626eedSJames Wright   CeedScalar N      = 0.01;   // 1/s
4077841947SLeila Ghaffari   CeedScalar rc     = 1000.;  // m (Radius of bubble)
4177841947SLeila Ghaffari   PetscReal  center[3], dc_axis[3] = {0, 0, 0};
421864f1c2SLeila Ghaffari   PetscReal  domain_min[3], domain_max[3], domain_size[3];
43*2b730f8bSJeremy L Thompson   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
44*2b730f8bSJeremy L Thompson   for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i];
4577841947SLeila Ghaffari 
4677841947SLeila Ghaffari   // ------------------------------------------------------
4777841947SLeila Ghaffari   //              Command line Options
4877841947SLeila Ghaffari   // ------------------------------------------------------
4967490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for DENSITY_CURRENT problem", NULL);
50*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-theta0", "Reference potential temperature", NULL, theta0, &theta0, NULL));
51*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-thetaC", "Perturbation of potential temperature", NULL, thetaC, &thetaC, NULL));
52*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-P0", "Atmospheric pressure", NULL, P0, &P0, NULL));
53*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-N", "Brunt-Vaisala frequency", NULL, N, &N, NULL));
54*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble", NULL, rc, &rc, NULL));
55*2b730f8bSJeremy L Thompson   for (PetscInt i = 0; i < 3; i++) center[i] = .5 * domain_size[i];
5677841947SLeila Ghaffari   PetscInt n = problem->dim;
57*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsRealArray("-center", "Location of bubble center", NULL, center, &n, NULL));
5877841947SLeila Ghaffari   n = problem->dim;
59*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsRealArray("-dc_axis",
6088626eedSJames Wright                                   "Axis of density current cylindrical anomaly, "
6188626eedSJames Wright                                   "or {0,0,0} for spherically symmetric",
62*2b730f8bSJeremy L Thompson                                   NULL, dc_axis, &n, NULL));
6377841947SLeila Ghaffari   {
64*2b730f8bSJeremy L Thompson     PetscReal norm = PetscSqrtReal(PetscSqr(dc_axis[0]) + PetscSqr(dc_axis[1]) + PetscSqr(dc_axis[2]));
6577841947SLeila Ghaffari     if (norm > 0) {
66*2b730f8bSJeremy L Thompson       for (PetscInt i = 0; i < 3; i++) dc_axis[i] /= norm;
6777841947SLeila Ghaffari     }
6877841947SLeila Ghaffari   }
6977841947SLeila Ghaffari 
7067490bc6SJeremy L Thompson   PetscOptionsEnd();
7177841947SLeila Ghaffari 
7288b783a1SJames Wright   PetscScalar meter  = user->units->meter;
7388626eedSJames Wright   PetscScalar second = user->units->second;
7488626eedSJames Wright   PetscScalar Kelvin = user->units->Kelvin;
7588626eedSJames Wright   PetscScalar Pascal = user->units->Pascal;
7677841947SLeila Ghaffari   rc                 = fabs(rc) * meter;
7788626eedSJames Wright   theta0 *= Kelvin;
7888626eedSJames Wright   thetaC *= Kelvin;
7988626eedSJames Wright   P0 *= Pascal;
8088626eedSJames Wright   N *= (1. / second);
81*2b730f8bSJeremy L Thompson   for (PetscInt i = 0; i < 3; i++) center[i] *= meter;
8277841947SLeila Ghaffari 
83dc805cc4SLeila Ghaffari   dc_ctx->theta0     = theta0;
84dc805cc4SLeila Ghaffari   dc_ctx->thetaC     = thetaC;
85dc805cc4SLeila Ghaffari   dc_ctx->P0         = P0;
86dc805cc4SLeila Ghaffari   dc_ctx->N          = N;
87dc805cc4SLeila Ghaffari   dc_ctx->rc         = rc;
88dc805cc4SLeila Ghaffari   dc_ctx->center[0]  = center[0];
89dc805cc4SLeila Ghaffari   dc_ctx->center[1]  = center[1];
90dc805cc4SLeila Ghaffari   dc_ctx->center[2]  = center[2];
91dc805cc4SLeila Ghaffari   dc_ctx->dc_axis[0] = dc_axis[0];
92dc805cc4SLeila Ghaffari   dc_ctx->dc_axis[1] = dc_axis[1];
93dc805cc4SLeila Ghaffari   dc_ctx->dc_axis[2] = dc_axis[2];
9477841947SLeila Ghaffari 
95*2b730f8bSJeremy L Thompson   CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, CEED_MEM_HOST, &newtonian_ig_ctx);
96dc805cc4SLeila Ghaffari   dc_ctx->newtonian_ctx = *newtonian_ig_ctx;
97*2b730f8bSJeremy L Thompson   CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, &newtonian_ig_ctx);
98dc805cc4SLeila Ghaffari   CeedQFunctionContextCreate(user->ceed, &density_current_context);
99*2b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(density_current_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*dc_ctx), dc_ctx);
100*2b730f8bSJeremy L Thompson   CeedQFunctionContextSetDataDestroy(density_current_context, CEED_MEM_HOST, FreeContextPetsc);
101dc805cc4SLeila Ghaffari   problem->ics.qfunction_context = density_current_context;
10217ce10faSJeremy L Thompson 
10377841947SLeila Ghaffari   PetscFunctionReturn(0);
10477841947SLeila Ghaffari }
105