xref: /libCEED/examples/fluids/problems/newtonian.c (revision 6cccb8e4b3ec0b5d0d60888533d5b9b04933e739)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 /// @file
9 /// Utility functions for setting up problems using the Newtonian Qfunction
10 
11 #include "../navierstokes.h"
12 #include "../qfunctions/setupgeo.h"
13 #include "../qfunctions/newtonian.h"
14 
15 // Compute relative error |a - b|/|s|
16 static PetscErrorCode CheckPrimitiveWithTolerance(StatePrimitive sY,
17     StatePrimitive aY, StatePrimitive bY, const char *name, PetscReal rtol_pressure,
18     PetscReal rtol_velocity, PetscReal rtol_temperature) {
19 
20   PetscFunctionBeginUser;
21   StatePrimitive eY; // relative error
22   eY.pressure = (aY.pressure - bY.pressure) / sY.pressure;
23   PetscScalar u = sqrt(Square(sY.velocity[0]) + Square(sY.velocity[1]) + Square(
24                          sY.velocity[2]));
25   for (int j=0; j<3; j++) eY.velocity[j] = (aY.velocity[j] - bY.velocity[j]) / u;
26   eY.temperature = (aY.temperature - bY.temperature) / sY.temperature;
27   if (fabs(eY.pressure) > rtol_pressure)
28     printf("%s: pressure error %g\n", name, eY.pressure);
29   for (int j=0; j<3; j++)
30     if (fabs(eY.velocity[j]) > rtol_velocity)
31       printf("%s: velocity[%d] error %g\n", name, j, eY.velocity[j]);
32   if (fabs(eY.temperature) > rtol_temperature)
33     printf("%s: temperature error %g\n", name, eY.temperature);
34   PetscFunctionReturn(0);
35 }
36 
37 static PetscErrorCode UnitTests_Newtonian(User user,
38     NewtonianIdealGasContext gas) {
39 
40   Units units = user->units;
41   const CeedScalar eps = 1e-6;
42   const CeedScalar kg = units->kilogram, m = units->meter, sec = units->second,
43                    Pascal = units->Pascal;
44 
45   PetscFunctionBeginUser;
46   const CeedScalar rho = 1.2 * kg / (m*m*m), u = 40 * m/sec;
47   CeedScalar U[5] = {rho, rho*u, rho *u*1.1, rho *u*1.2, 250e3*Pascal + .5*rho *u*u};
48   const CeedScalar x[3] = {.1, .2, .3};
49   State s = StateFromU(gas, U, x);
50   for (int i=0; i<8; i++) {
51     CeedScalar dU[5] = {0}, dx[3] = {0};
52     if (i < 5) dU[i] = U[i];
53     else dx[i-5] = x[i-5];
54     State ds = StateFromU_fwd(gas, s, dU, x, dx);
55     for (int j=0; j<5; j++) dU[j] = (1 + eps * (i == j)) * U[j];
56     for (int j=0; j<3; j++) dx[j] = (1 + eps * (i == 5 + j)) * x[j];
57     State t = StateFromU(gas, dU, dx);
58     StatePrimitive dY;
59     dY.pressure = (t.Y.pressure - s.Y.pressure) / eps;
60     for (int j=0; j<3; j++)
61       dY.velocity[j] = (t.Y.velocity[j] - s.Y.velocity[j]) / eps;
62     dY.temperature = (t.Y.temperature - s.Y.temperature) / eps;
63     char buf[128];
64     snprintf(buf, sizeof buf, "StateFromU_fwd i=%d", i);
65     PetscCall(CheckPrimitiveWithTolerance(dY, ds.Y, dY, buf, 5e-6, 1e-6, 1e-6));
66   }
67   PetscFunctionReturn(0);
68 }
69 
70 PetscErrorCode NS_NEWTONIAN_IG(ProblemData *problem, DM dm, void *ctx) {
71 
72   SetupContext      setup_context;
73   User              user = *(User *)ctx;
74   StabilizationType stab;
75   MPI_Comm          comm = PETSC_COMM_WORLD;
76   PetscBool         implicit;
77   PetscBool         has_curr_time = PETSC_FALSE, unit_tests;
78   PetscInt          ierr;
79   NewtonianIdealGasContext newtonian_ig_ctx;
80   CeedQFunctionContext newtonian_ig_context;
81 
82   PetscFunctionBeginUser;
83   ierr = PetscCalloc1(1, &setup_context); CHKERRQ(ierr);
84   ierr = PetscCalloc1(1, &newtonian_ig_ctx); CHKERRQ(ierr);
85 
86   // ------------------------------------------------------
87   //           Setup Generic Newtonian IG Problem
88   // ------------------------------------------------------
89   problem->dim                               = 3;
90   problem->q_data_size_vol                   = 10;
91   problem->q_data_size_sur                   = 10;
92   problem->jac_data_size_sur                 = 5;
93   problem->setup_vol.qfunction               = Setup;
94   problem->setup_vol.qfunction_loc           = Setup_loc;
95   problem->ics.qfunction                     = ICsNewtonianIG;
96   problem->ics.qfunction_loc                 = ICsNewtonianIG_loc;
97   problem->setup_sur.qfunction               = SetupBoundary;
98   problem->setup_sur.qfunction_loc           = SetupBoundary_loc;
99   problem->apply_vol_rhs.qfunction           = RHSFunction_Newtonian;
100   problem->apply_vol_rhs.qfunction_loc       = RHSFunction_Newtonian_loc;
101   problem->apply_vol_ifunction.qfunction     = IFunction_Newtonian;
102   problem->apply_vol_ifunction.qfunction_loc = IFunction_Newtonian_loc;
103   problem->apply_vol_ijacobian.qfunction     = IJacobian_Newtonian;
104   problem->apply_vol_ijacobian.qfunction_loc = IJacobian_Newtonian_loc;
105   problem->bc                                = NULL;
106   problem->bc_ctx                            = setup_context;
107   problem->non_zero_time                     = PETSC_FALSE;
108   problem->print_info                        = PRINT_DENSITY_CURRENT;
109 
110   // ------------------------------------------------------
111   //             Create the libCEED context
112   // ------------------------------------------------------
113   CeedScalar cv     = 717.;          // J/(kg K)
114   CeedScalar cp     = 1004.;         // J/(kg K)
115   CeedScalar g[3]   = {0, 0, -9.81}; // m/s^2
116   CeedScalar lambda = -2./3.;        // -
117   CeedScalar mu     = 1.8e-5;        // Pa s, dynamic viscosity
118   CeedScalar k      = 0.02638;       // W/(m K)
119   CeedScalar c_tau  = 0.5;           // -
120   CeedScalar Ctau_t  = 1.0;          // -
121   CeedScalar Ctau_v  = 36.0;         // TODO make function of degree
122   CeedScalar Ctau_C  = 1.0;          // TODO make function of degree
123   CeedScalar Ctau_M  = 1.0;          // TODO make function of degree
124   CeedScalar Ctau_E  = 1.0;          // TODO make function of degree
125   PetscReal domain_min[3], domain_max[3], domain_size[3];
126   ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr);
127   for (PetscInt i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i];
128 
129   // ------------------------------------------------------
130   //             Create the PETSc context
131   // ------------------------------------------------------
132   PetscScalar meter    = 1;  // 1 meter in scaled length units
133   PetscScalar kilogram = 1;  // 1 kilogram in scaled mass units
134   PetscScalar second   = 1;  // 1 second in scaled time units
135   PetscScalar Kelvin   = 1;     // 1 Kelvin in scaled temperature units
136   PetscScalar W_per_m_K, Pascal, J_per_kg_K, m_per_squared_s;
137 
138   // ------------------------------------------------------
139   //              Command line Options
140   // ------------------------------------------------------
141   PetscOptionsBegin(comm, NULL, "Options for Newtonian Ideal Gas based problem",
142                     NULL);
143 
144   // -- Physics
145   ierr = PetscOptionsScalar("-cv", "Heat capacity at constant volume",
146                             NULL, cv, &cv, NULL); CHKERRQ(ierr);
147   ierr = PetscOptionsScalar("-cp", "Heat capacity at constant pressure",
148                             NULL, cp, &cp, NULL); CHKERRQ(ierr);
149   ierr = PetscOptionsScalar("-lambda",
150                             "Stokes hypothesis second viscosity coefficient",
151                             NULL, lambda, &lambda, NULL); CHKERRQ(ierr);
152   ierr = PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient",
153                             NULL, mu, &mu, NULL); CHKERRQ(ierr);
154   ierr = PetscOptionsScalar("-k", "Thermal conductivity",
155                             NULL, k, &k, NULL); CHKERRQ(ierr);
156 
157   PetscInt dim = problem->dim;
158   ierr = PetscOptionsRealArray("-g", "Gravitational acceleration",
159                                NULL, g, &dim, NULL); CHKERRQ(ierr);
160   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
161                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
162                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
163   ierr = PetscOptionsScalar("-c_tau", "Stabilization constant",
164                             NULL, c_tau, &c_tau, NULL); CHKERRQ(ierr);
165   ierr = PetscOptionsScalar("-Ctau_t", "Stabilization time constant",
166                             NULL, Ctau_t, &Ctau_t, NULL); CHKERRQ(ierr);
167   ierr = PetscOptionsScalar("-Ctau_v", "Stabilization viscous constant",
168                             NULL, Ctau_v, &Ctau_v, NULL); CHKERRQ(ierr);
169   ierr = PetscOptionsScalar("-Ctau_C", "Stabilization continuity constant",
170                             NULL, Ctau_C, &Ctau_C, NULL); CHKERRQ(ierr);
171   ierr = PetscOptionsScalar("-Ctau_M", "Stabilization momentum constant",
172                             NULL, Ctau_M, &Ctau_M, NULL); CHKERRQ(ierr);
173   ierr = PetscOptionsScalar("-Ctau_E", "Stabilization energy constant",
174                             NULL, Ctau_E, &Ctau_E, NULL); CHKERRQ(ierr);
175   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
176                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
177   CHKERRQ(ierr);
178   ierr = PetscOptionsBool("-newtonian_unit_tests", "Run Newtonian unit tests",
179                           NULL, unit_tests=PETSC_FALSE, &unit_tests, NULL);
180   CHKERRQ(ierr);
181 
182   // -- Units
183   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
184                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
185   meter = fabs(meter);
186   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
187                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
188   kilogram = fabs(kilogram);
189   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
190                             NULL, second, &second, NULL); CHKERRQ(ierr);
191   second = fabs(second);
192   ierr = PetscOptionsScalar("-units_Kelvin",
193                             "1 Kelvin in scaled temperature units",
194                             NULL, Kelvin, &Kelvin, NULL); CHKERRQ(ierr);
195   Kelvin = fabs(Kelvin);
196 
197   // -- Warnings
198   if (stab == STAB_SUPG && !implicit) {
199     ierr = PetscPrintf(comm,
200                        "Warning! Use -stab supg only with -implicit\n");
201     CHKERRQ(ierr);
202   }
203   PetscOptionsEnd();
204 
205   // ------------------------------------------------------
206   //           Set up the PETSc context
207   // ------------------------------------------------------
208   // -- Define derived units
209   Pascal          = kilogram / (meter * PetscSqr(second));
210   J_per_kg_K      =  PetscSqr(meter) / (PetscSqr(second) * Kelvin);
211   m_per_squared_s = meter / PetscSqr(second);
212   W_per_m_K       = kilogram * meter / (pow(second,3) * Kelvin);
213 
214   user->units->meter           = meter;
215   user->units->kilogram        = kilogram;
216   user->units->second          = second;
217   user->units->Kelvin          = Kelvin;
218   user->units->Pascal          = Pascal;
219   user->units->J_per_kg_K      = J_per_kg_K;
220   user->units->m_per_squared_s = m_per_squared_s;
221   user->units->W_per_m_K       = W_per_m_K;
222 
223   // ------------------------------------------------------
224   //           Set up the libCEED context
225   // ------------------------------------------------------
226   // -- Scale variables to desired units
227   cv     *= J_per_kg_K;
228   cp     *= J_per_kg_K;
229   mu     *= Pascal * second;
230   k      *= W_per_m_K;
231   for (PetscInt i=0; i<3; i++) domain_size[i] *= meter;
232   for (PetscInt i=0; i<3; i++) g[i]           *= m_per_squared_s;
233   problem->dm_scale = meter;
234 
235   // -- Setup Context
236   setup_context->cv         = cv;
237   setup_context->cp         = cp;
238   setup_context->lx         = domain_size[0];
239   setup_context->ly         = domain_size[1];
240   setup_context->lz         = domain_size[2];
241   setup_context->time       = 0;
242   ierr = PetscArraycpy(setup_context->g, g, 3); CHKERRQ(ierr);
243 
244   // -- Solver Settings
245   user->phys->stab          = stab;
246   user->phys->implicit      = implicit;
247   user->phys->has_curr_time = has_curr_time;
248 
249   // -- QFunction Context
250   newtonian_ig_ctx->lambda        = lambda;
251   newtonian_ig_ctx->mu            = mu;
252   newtonian_ig_ctx->k             = k;
253   newtonian_ig_ctx->cv            = cv;
254   newtonian_ig_ctx->cp            = cp;
255   newtonian_ig_ctx->c_tau         = c_tau;
256   newtonian_ig_ctx->Ctau_t        = Ctau_t;
257   newtonian_ig_ctx->Ctau_v        = Ctau_v;
258   newtonian_ig_ctx->Ctau_C        = Ctau_C;
259   newtonian_ig_ctx->Ctau_M        = Ctau_M;
260   newtonian_ig_ctx->Ctau_E        = Ctau_E;
261   newtonian_ig_ctx->stabilization = stab;
262   ierr = PetscArraycpy(newtonian_ig_ctx->g, g, 3); CHKERRQ(ierr);
263 
264   CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context);
265   CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST,
266                               CEED_USE_POINTER, sizeof(*setup_context), setup_context);
267   CeedQFunctionContextRegisterDouble(problem->ics.qfunction_context,
268                                      "evaluation time",
269                                      (char *)&setup_context->time - (char *)setup_context, 1, "Time of evaluation");
270 
271   CeedQFunctionContextCreate(user->ceed, &newtonian_ig_context);
272   CeedQFunctionContextSetData(newtonian_ig_context, CEED_MEM_HOST,
273                               CEED_USE_POINTER,
274                               sizeof(*newtonian_ig_ctx), newtonian_ig_ctx);
275   CeedQFunctionContextSetDataDestroy(newtonian_ig_context, CEED_MEM_HOST,
276                                      FreeContextPetsc);
277   CeedQFunctionContextRegisterDouble(newtonian_ig_context, "timestep size",
278                                      offsetof(struct NewtonianIdealGasContext_, dt), 1, "Size of timestep, delta t");
279   CeedQFunctionContextRegisterDouble(newtonian_ig_context, "ijacobian time shift",
280                                      offsetof(struct NewtonianIdealGasContext_, ijacobian_time_shift), 1,
281                                      "Shift for mass matrix in IJacobian");
282   problem->apply_vol_rhs.qfunction_context = newtonian_ig_context;
283   CeedQFunctionContextReferenceCopy(newtonian_ig_context,
284                                     &problem->apply_vol_ifunction.qfunction_context);
285   CeedQFunctionContextReferenceCopy(newtonian_ig_context,
286                                     &problem->apply_vol_ijacobian.qfunction_context);
287 
288   if (unit_tests) {
289     PetscCall(UnitTests_Newtonian(user, newtonian_ig_ctx));
290   }
291   PetscFunctionReturn(0);
292 }
293 
294 PetscErrorCode PRINT_DENSITY_CURRENT(ProblemData *problem,
295                                      AppCtx app_ctx) {
296   MPI_Comm comm = PETSC_COMM_WORLD;
297   PetscErrorCode ierr;
298   NewtonianIdealGasContext newtonian_ctx;
299 
300   PetscFunctionBeginUser;
301   CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context,
302                               CEED_MEM_HOST, &newtonian_ctx);
303   ierr = PetscPrintf(comm,
304                      "  Problem:\n"
305                      "    Problem Name                       : %s\n"
306                      "    Stabilization                      : %s\n",
307                      app_ctx->problem_name, StabilizationTypes[newtonian_ctx->stabilization]);
308   CHKERRQ(ierr);
309   CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context,
310                                   &newtonian_ctx);
311   PetscFunctionReturn(0);
312 }
313