xref: /libCEED/examples/fluids/problems/newtonian.c (revision 65dd5cafde15489fff5d2ab607c335242f64f615)
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->apply_inflow.qfunction            = BoundaryIntegral;
106   problem->apply_inflow.qfunction_loc        = BoundaryIntegral_loc;
107   problem->bc                                = NULL;
108   problem->bc_ctx                            = setup_context;
109   problem->non_zero_time                     = PETSC_FALSE;
110   problem->print_info                        = PRINT_DENSITY_CURRENT;
111 
112   // ------------------------------------------------------
113   //             Create the libCEED context
114   // ------------------------------------------------------
115   CeedScalar cv     = 717.;          // J/(kg K)
116   CeedScalar cp     = 1004.;         // J/(kg K)
117   CeedScalar g[3]   = {0, 0, -9.81}; // m/s^2
118   CeedScalar lambda = -2./3.;        // -
119   CeedScalar mu     = 1.8e-5;        // Pa s, dynamic viscosity
120   CeedScalar k      = 0.02638;       // W/(m K)
121   CeedScalar c_tau  = 0.5;           // -
122   CeedScalar Ctau_t  = 1.0;          // -
123   CeedScalar Ctau_v  = 36.0;         // TODO make function of degree
124   CeedScalar Ctau_C  = 1.0;          // TODO make function of degree
125   CeedScalar Ctau_M  = 1.0;          // TODO make function of degree
126   CeedScalar Ctau_E  = 1.0;          // TODO make function of degree
127   PetscReal domain_min[3], domain_max[3], domain_size[3];
128   ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr);
129   for (PetscInt i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i];
130 
131   // ------------------------------------------------------
132   //             Create the PETSc context
133   // ------------------------------------------------------
134   PetscScalar meter    = 1;  // 1 meter in scaled length units
135   PetscScalar kilogram = 1;  // 1 kilogram in scaled mass units
136   PetscScalar second   = 1;  // 1 second in scaled time units
137   PetscScalar Kelvin   = 1;     // 1 Kelvin in scaled temperature units
138   PetscScalar W_per_m_K, Pascal, J_per_kg_K, m_per_squared_s;
139 
140   // ------------------------------------------------------
141   //              Command line Options
142   // ------------------------------------------------------
143   PetscOptionsBegin(comm, NULL, "Options for Newtonian Ideal Gas based problem",
144                     NULL);
145 
146   // -- Physics
147   ierr = PetscOptionsScalar("-cv", "Heat capacity at constant volume",
148                             NULL, cv, &cv, NULL); CHKERRQ(ierr);
149   ierr = PetscOptionsScalar("-cp", "Heat capacity at constant pressure",
150                             NULL, cp, &cp, NULL); CHKERRQ(ierr);
151   ierr = PetscOptionsScalar("-lambda",
152                             "Stokes hypothesis second viscosity coefficient",
153                             NULL, lambda, &lambda, NULL); CHKERRQ(ierr);
154   ierr = PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient",
155                             NULL, mu, &mu, NULL); CHKERRQ(ierr);
156   ierr = PetscOptionsScalar("-k", "Thermal conductivity",
157                             NULL, k, &k, NULL); CHKERRQ(ierr);
158 
159   PetscInt dim = problem->dim;
160   ierr = PetscOptionsRealArray("-g", "Gravitational acceleration",
161                                NULL, g, &dim, NULL); CHKERRQ(ierr);
162   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
163                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
164                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
165   ierr = PetscOptionsScalar("-c_tau", "Stabilization constant",
166                             NULL, c_tau, &c_tau, NULL); CHKERRQ(ierr);
167   ierr = PetscOptionsScalar("-Ctau_t", "Stabilization time constant",
168                             NULL, Ctau_t, &Ctau_t, NULL); CHKERRQ(ierr);
169   ierr = PetscOptionsScalar("-Ctau_v", "Stabilization viscous constant",
170                             NULL, Ctau_v, &Ctau_v, NULL); CHKERRQ(ierr);
171   ierr = PetscOptionsScalar("-Ctau_C", "Stabilization continuity constant",
172                             NULL, Ctau_C, &Ctau_C, NULL); CHKERRQ(ierr);
173   ierr = PetscOptionsScalar("-Ctau_M", "Stabilization momentum constant",
174                             NULL, Ctau_M, &Ctau_M, NULL); CHKERRQ(ierr);
175   ierr = PetscOptionsScalar("-Ctau_E", "Stabilization energy constant",
176                             NULL, Ctau_E, &Ctau_E, NULL); CHKERRQ(ierr);
177   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
178                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
179   CHKERRQ(ierr);
180   ierr = PetscOptionsBool("-newtonian_unit_tests", "Run Newtonian unit tests",
181                           NULL, unit_tests=PETSC_FALSE, &unit_tests, NULL);
182   CHKERRQ(ierr);
183 
184   // -- Units
185   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
186                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
187   meter = fabs(meter);
188   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
189                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
190   kilogram = fabs(kilogram);
191   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
192                             NULL, second, &second, NULL); CHKERRQ(ierr);
193   second = fabs(second);
194   ierr = PetscOptionsScalar("-units_Kelvin",
195                             "1 Kelvin in scaled temperature units",
196                             NULL, Kelvin, &Kelvin, NULL); CHKERRQ(ierr);
197   Kelvin = fabs(Kelvin);
198 
199   // -- Warnings
200   if (stab == STAB_SUPG && !implicit) {
201     ierr = PetscPrintf(comm,
202                        "Warning! Use -stab supg only with -implicit\n");
203     CHKERRQ(ierr);
204   }
205   PetscOptionsEnd();
206 
207   // ------------------------------------------------------
208   //           Set up the PETSc context
209   // ------------------------------------------------------
210   // -- Define derived units
211   Pascal          = kilogram / (meter * PetscSqr(second));
212   J_per_kg_K      =  PetscSqr(meter) / (PetscSqr(second) * Kelvin);
213   m_per_squared_s = meter / PetscSqr(second);
214   W_per_m_K       = kilogram * meter / (pow(second,3) * Kelvin);
215 
216   user->units->meter           = meter;
217   user->units->kilogram        = kilogram;
218   user->units->second          = second;
219   user->units->Kelvin          = Kelvin;
220   user->units->Pascal          = Pascal;
221   user->units->J_per_kg_K      = J_per_kg_K;
222   user->units->m_per_squared_s = m_per_squared_s;
223   user->units->W_per_m_K       = W_per_m_K;
224 
225   // ------------------------------------------------------
226   //           Set up the libCEED context
227   // ------------------------------------------------------
228   // -- Scale variables to desired units
229   cv     *= J_per_kg_K;
230   cp     *= J_per_kg_K;
231   mu     *= Pascal * second;
232   k      *= W_per_m_K;
233   for (PetscInt i=0; i<3; i++) domain_size[i] *= meter;
234   for (PetscInt i=0; i<3; i++) g[i]           *= m_per_squared_s;
235   problem->dm_scale = meter;
236 
237   // -- Setup Context
238   setup_context->cv         = cv;
239   setup_context->cp         = cp;
240   setup_context->lx         = domain_size[0];
241   setup_context->ly         = domain_size[1];
242   setup_context->lz         = domain_size[2];
243   setup_context->time       = 0;
244   ierr = PetscArraycpy(setup_context->g, g, 3); CHKERRQ(ierr);
245 
246   // -- Solver Settings
247   user->phys->stab          = stab;
248   user->phys->implicit      = implicit;
249   user->phys->has_curr_time = has_curr_time;
250 
251   // -- QFunction Context
252   newtonian_ig_ctx->lambda        = lambda;
253   newtonian_ig_ctx->mu            = mu;
254   newtonian_ig_ctx->k             = k;
255   newtonian_ig_ctx->cv            = cv;
256   newtonian_ig_ctx->cp            = cp;
257   newtonian_ig_ctx->c_tau         = c_tau;
258   newtonian_ig_ctx->Ctau_t        = Ctau_t;
259   newtonian_ig_ctx->Ctau_v        = Ctau_v;
260   newtonian_ig_ctx->Ctau_C        = Ctau_C;
261   newtonian_ig_ctx->Ctau_M        = Ctau_M;
262   newtonian_ig_ctx->Ctau_E        = Ctau_E;
263   newtonian_ig_ctx->stabilization = stab;
264   newtonian_ig_ctx->is_implicit   = implicit;
265   ierr = PetscArraycpy(newtonian_ig_ctx->g, g, 3); CHKERRQ(ierr);
266 
267   CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context);
268   CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST,
269                               CEED_USE_POINTER, sizeof(*setup_context), setup_context);
270   CeedQFunctionContextSetDataDestroy(problem->ics.qfunction_context,
271                                      CEED_MEM_HOST,
272                                      FreeContextPetsc);
273   CeedQFunctionContextRegisterDouble(problem->ics.qfunction_context,
274                                      "evaluation time",
275                                      (char *)&setup_context->time - (char *)setup_context, 1, "Time of evaluation");
276 
277   CeedQFunctionContextCreate(user->ceed, &newtonian_ig_context);
278   CeedQFunctionContextSetData(newtonian_ig_context, CEED_MEM_HOST,
279                               CEED_USE_POINTER,
280                               sizeof(*newtonian_ig_ctx), newtonian_ig_ctx);
281   CeedQFunctionContextSetDataDestroy(newtonian_ig_context, CEED_MEM_HOST,
282                                      FreeContextPetsc);
283   CeedQFunctionContextRegisterDouble(newtonian_ig_context, "timestep size",
284                                      offsetof(struct NewtonianIdealGasContext_, dt), 1, "Size of timestep, delta t");
285   CeedQFunctionContextRegisterDouble(newtonian_ig_context, "ijacobian time shift",
286                                      offsetof(struct NewtonianIdealGasContext_, ijacobian_time_shift), 1,
287                                      "Shift for mass matrix in IJacobian");
288   problem->apply_vol_rhs.qfunction_context = newtonian_ig_context;
289   CeedQFunctionContextReferenceCopy(newtonian_ig_context,
290                                     &problem->apply_vol_ifunction.qfunction_context);
291   CeedQFunctionContextReferenceCopy(newtonian_ig_context,
292                                     &problem->apply_vol_ijacobian.qfunction_context);
293   CeedQFunctionContextReferenceCopy(newtonian_ig_context,
294                                     &problem->apply_inflow.qfunction_context);
295 
296   if (unit_tests) {
297     PetscCall(UnitTests_Newtonian(user, newtonian_ig_ctx));
298   }
299   PetscFunctionReturn(0);
300 }
301 
302 PetscErrorCode PRINT_DENSITY_CURRENT(ProblemData *problem,
303                                      AppCtx app_ctx) {
304   MPI_Comm comm = PETSC_COMM_WORLD;
305   PetscErrorCode ierr;
306   NewtonianIdealGasContext newtonian_ctx;
307 
308   PetscFunctionBeginUser;
309   CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context,
310                               CEED_MEM_HOST, &newtonian_ctx);
311   ierr = PetscPrintf(comm,
312                      "  Problem:\n"
313                      "    Problem Name                       : %s\n"
314                      "    Stabilization                      : %s\n",
315                      app_ctx->problem_name, StabilizationTypes[newtonian_ctx->stabilization]);
316   CHKERRQ(ierr);
317   CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context,
318                                   &newtonian_ctx);
319   PetscFunctionReturn(0);
320 }
321