xref: /libCEED/examples/fluids/problems/densitycurrent.c (revision 91e5af17cc38f7d4d16af29119518d78e5298d3f)
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"
1388626eedSJames Wright #include "../navierstokes.h"
1477841947SLeila Ghaffari 
151864f1c2SLeila Ghaffari PetscErrorCode NS_DENSITY_CURRENT(ProblemData *problem, DM dm, void *setup_ctx,
1677841947SLeila Ghaffari                                   void *ctx) {
1788b783a1SJames Wright 
1888b783a1SJames Wright   PetscInt ierr;
1988626eedSJames Wright   ierr = NS_NEWTONIAN_IG(problem, dm, setup_ctx, ctx);
2088626eedSJames Wright   CHKERRQ(ierr);
2177841947SLeila Ghaffari   SetupContext setup_context = *(SetupContext *)setup_ctx;
2277841947SLeila Ghaffari   User user = *(User *)ctx;
2377841947SLeila Ghaffari   MPI_Comm comm = PETSC_COMM_WORLD;
2477841947SLeila Ghaffari   PetscFunctionBeginUser;
2577841947SLeila Ghaffari 
2677841947SLeila Ghaffari   // ------------------------------------------------------
2777841947SLeila Ghaffari   //               SET UP DENSITY_CURRENT
2877841947SLeila Ghaffari   // ------------------------------------------------------
29*91e5af17SJed Brown   problem->ics.qfunction = ICsDC;
30*91e5af17SJed Brown   problem->ics.qfunction_loc = ICsDC_loc;
3177841947SLeila Ghaffari   problem->bc = Exact_DC;
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];
4388626eedSJames Wright   ierr = DMGetBoundingBox(dm, domain_min, domain_max);
4488626eedSJames Wright   CHKERRQ(ierr);
4588626eedSJames Wright   for (int i = 0; i < 3; i++)
4688626eedSJames Wright     domain_size[i] = domain_max[i] - domain_min[i];
4777841947SLeila Ghaffari 
4877841947SLeila Ghaffari   // ------------------------------------------------------
4977841947SLeila Ghaffari   //              Command line Options
5077841947SLeila Ghaffari   // ------------------------------------------------------
5167490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for DENSITY_CURRENT problem", NULL);
5288626eedSJames Wright   ierr = PetscOptionsScalar("-theta0", "Reference potential temperature", NULL,
5388626eedSJames Wright                             theta0, &theta0, NULL);
5488626eedSJames Wright   CHKERRQ(ierr);
5588626eedSJames Wright   ierr = PetscOptionsScalar("-thetaC", "Perturbation of potential temperature",
5688626eedSJames Wright                             NULL, thetaC, &thetaC, NULL);
5788626eedSJames Wright   CHKERRQ(ierr);
5888626eedSJames Wright   ierr = PetscOptionsScalar("-P0", "Atmospheric pressure", NULL, P0, &P0, NULL);
5988626eedSJames Wright   CHKERRQ(ierr);
6088626eedSJames Wright   ierr = PetscOptionsScalar("-N", "Brunt-Vaisala frequency", NULL, N, &N, NULL);
6188626eedSJames Wright   CHKERRQ(ierr);
6277841947SLeila Ghaffari   ierr = PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble",
6388626eedSJames Wright                             NULL, rc, &rc, NULL);
6488626eedSJames Wright   CHKERRQ(ierr);
6588626eedSJames Wright   for (int i = 0; i < 3; i++)
6688626eedSJames Wright     center[i] = .5 * domain_size[i];
6777841947SLeila Ghaffari   PetscInt n = problem->dim;
6888626eedSJames Wright   ierr = PetscOptionsRealArray("-center", "Location of bubble center", NULL,
6988626eedSJames Wright                                center, &n, NULL);
7088626eedSJames Wright   CHKERRQ(ierr);
7177841947SLeila Ghaffari   n = problem->dim;
7277841947SLeila Ghaffari   ierr = PetscOptionsRealArray("-dc_axis",
7388626eedSJames Wright                                "Axis of density current cylindrical anomaly, "
7488626eedSJames Wright                                "or {0,0,0} for spherically symmetric",
7588626eedSJames Wright                                NULL, dc_axis, &n, NULL);
7688626eedSJames Wright   CHKERRQ(ierr);
7777841947SLeila Ghaffari   {
7877841947SLeila Ghaffari     PetscReal norm = PetscSqrtReal(PetscSqr(dc_axis[0]) + PetscSqr(dc_axis[1]) +
7977841947SLeila Ghaffari                                    PetscSqr(dc_axis[2]));
8077841947SLeila Ghaffari     if (norm > 0) {
8188626eedSJames Wright       for (int i = 0; i < 3; i++)
8288626eedSJames Wright         dc_axis[i] /= norm;
8377841947SLeila Ghaffari     }
8477841947SLeila Ghaffari   }
8577841947SLeila Ghaffari 
8667490bc6SJeremy L Thompson   PetscOptionsEnd();
8777841947SLeila Ghaffari 
8888b783a1SJames Wright   PetscScalar meter           = user->units->meter;
8988626eedSJames Wright   PetscScalar second          = user->units->second;
9088626eedSJames Wright   PetscScalar Kelvin          = user->units->Kelvin;
9188626eedSJames Wright   PetscScalar Pascal          = user->units->Pascal;
9277841947SLeila Ghaffari   rc = fabs(rc) * meter;
9388626eedSJames Wright   theta0 *= Kelvin;
9488626eedSJames Wright   thetaC *= Kelvin;
9588626eedSJames Wright   P0 *= Pascal;
9688626eedSJames Wright   N *= (1. / second);
9788626eedSJames Wright   for (int i = 0; i < 3; i++)
9888626eedSJames Wright     center[i] *= meter;
9977841947SLeila Ghaffari 
10088626eedSJames Wright   setup_context->theta0 = theta0;
10188626eedSJames Wright   setup_context->thetaC = thetaC;
10288626eedSJames Wright   setup_context->P0 = P0;
10388626eedSJames Wright   setup_context->N = N;
10477841947SLeila Ghaffari   setup_context->rc = rc;
10577841947SLeila Ghaffari   setup_context->center[0] = center[0];
10677841947SLeila Ghaffari   setup_context->center[1] = center[1];
10777841947SLeila Ghaffari   setup_context->center[2] = center[2];
10877841947SLeila Ghaffari   setup_context->dc_axis[0] = dc_axis[0];
10977841947SLeila Ghaffari   setup_context->dc_axis[1] = dc_axis[1];
11077841947SLeila Ghaffari   setup_context->dc_axis[2] = dc_axis[2];
11177841947SLeila Ghaffari 
11277841947SLeila Ghaffari   PetscFunctionReturn(0);
11377841947SLeila Ghaffari }
11477841947SLeila Ghaffari 
115d0b732dbSLeila Ghaffari PetscErrorCode SetupContext_DENSITY_CURRENT(Ceed ceed, CeedData ceed_data,
1162fe7aee7SLeila Ghaffari     AppCtx app_ctx, SetupContext setup_ctx, Physics phys) {
117d0b732dbSLeila Ghaffari   PetscFunctionBeginUser;
11888626eedSJames Wright   PetscInt ierr =
11988626eedSJames Wright     SetupContext_NEWTONIAN_IG(ceed, ceed_data, app_ctx, setup_ctx, phys);
12088b783a1SJames Wright   CHKERRQ(ierr);
12177841947SLeila Ghaffari   PetscFunctionReturn(0);
12277841947SLeila Ghaffari }
12377841947SLeila Ghaffari 
12477841947SLeila Ghaffari PetscErrorCode PRINT_DENSITY_CURRENT(Physics phys, SetupContext setup_ctx,
12577841947SLeila Ghaffari                                      AppCtx app_ctx) {
12677841947SLeila Ghaffari   MPI_Comm comm = PETSC_COMM_WORLD;
12777841947SLeila Ghaffari   PetscErrorCode ierr;
12877841947SLeila Ghaffari   PetscFunctionBeginUser;
12977841947SLeila Ghaffari 
13077841947SLeila Ghaffari   ierr = PetscPrintf(comm,
13177841947SLeila Ghaffari                      "  Problem:\n"
13277841947SLeila Ghaffari                      "    Problem Name                       : %s\n"
13377841947SLeila Ghaffari                      "    Stabilization                      : %s\n",
13477841947SLeila Ghaffari                      app_ctx->problem_name, StabilizationTypes[phys->stab]);
13577841947SLeila Ghaffari   CHKERRQ(ierr);
13677841947SLeila Ghaffari 
13777841947SLeila Ghaffari   PetscFunctionReturn(0);
13877841947SLeila Ghaffari }
139