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 "../qfunctions/newtonian.h" 12 13 #include <ceed.h> 14 #include <petscdm.h> 15 16 #include "../navierstokes.h" 17 #include "../qfunctions/setupgeo.h" 18 19 // For use with PetscOptionsEnum 20 static const char *const StateVariables[] = {"CONSERVATIVE", "PRIMITIVE", "StateVariable", "STATEVAR_", NULL}; 21 22 // Compute relative error |a - b|/|s| 23 static PetscErrorCode CheckPrimitiveWithTolerance(StatePrimitive sY, StatePrimitive aY, StatePrimitive bY, const char *name, PetscReal rtol_pressure, 24 PetscReal rtol_velocity, PetscReal rtol_temperature) { 25 StatePrimitive eY; // relative error 26 27 PetscFunctionBeginUser; 28 eY.pressure = (aY.pressure - bY.pressure) / sY.pressure; 29 PetscScalar u = sqrt(Square(sY.velocity[0]) + Square(sY.velocity[1]) + Square(sY.velocity[2])); 30 for (int j = 0; j < 3; j++) eY.velocity[j] = (aY.velocity[j] - bY.velocity[j]) / u; 31 eY.temperature = (aY.temperature - bY.temperature) / sY.temperature; 32 if (fabs(eY.pressure) > rtol_pressure) printf("%s: pressure error %g\n", name, eY.pressure); 33 for (int j = 0; j < 3; j++) { 34 if (fabs(eY.velocity[j]) > rtol_velocity) printf("%s: velocity[%d] error %g\n", name, j, eY.velocity[j]); 35 } 36 if (fabs(eY.temperature) > rtol_temperature) printf("%s: temperature error %g\n", name, eY.temperature); 37 PetscFunctionReturn(PETSC_SUCCESS); 38 } 39 40 static PetscErrorCode UnitTests_Newtonian(User user, NewtonianIdealGasContext gas) { 41 Units units = user->units; 42 const CeedScalar eps = 1e-6; 43 const CeedScalar x[3] = {.1, .2, .3}; 44 const CeedScalar kg = units->kilogram, m = units->meter, sec = units->second, Pascal = units->Pascal; 45 46 PetscFunctionBeginUser; 47 const CeedScalar rho = 1.2 * kg / (m * m * m), u = 40 * m / sec; 48 CeedScalar U[5] = {rho, rho * u, rho * u * 1.1, rho * u * 1.2, 250e3 * Pascal + .5 * rho * u * u}; 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++) dY.velocity[j] = (t.Y.velocity[j] - s.Y.velocity[j]) / eps; 61 dY.temperature = (t.Y.temperature - s.Y.temperature) / eps; 62 char buf[128]; 63 snprintf(buf, sizeof buf, "StateFromU_fwd i=%d", i); 64 PetscCall(CheckPrimitiveWithTolerance(dY, ds.Y, dY, buf, 5e-6, 1e-6, 1e-6)); 65 } 66 PetscFunctionReturn(PETSC_SUCCESS); 67 } 68 69 PetscErrorCode NS_NEWTONIAN_IG(ProblemData *problem, DM dm, void *ctx, SimpleBC bc) { 70 SetupContext setup_context; 71 User user = *(User *)ctx; 72 CeedInt degree = user->app_ctx->degree; 73 StabilizationType stab; 74 StateVariable state_var; 75 MPI_Comm comm = user->comm; 76 Ceed ceed = user->ceed; 77 PetscBool implicit; 78 PetscBool has_curr_time = PETSC_FALSE, unit_tests; 79 NewtonianIdealGasContext newtonian_ig_ctx; 80 CeedQFunctionContext newtonian_ig_context; 81 82 PetscFunctionBeginUser; 83 PetscCall(PetscCalloc1(1, &setup_context)); 84 PetscCall(PetscCalloc1(1, &newtonian_ig_ctx)); 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 = 11; 93 problem->setup_vol.qfunction = Setup; 94 problem->setup_vol.qfunction_loc = Setup_loc; 95 problem->setup_sur.qfunction = SetupBoundary; 96 problem->setup_sur.qfunction_loc = SetupBoundary_loc; 97 problem->non_zero_time = PETSC_FALSE; 98 problem->print_info = PRINT_NEWTONIAN; 99 100 // ------------------------------------------------------ 101 // Create the libCEED context 102 // ------------------------------------------------------ 103 CeedScalar cv = 717.; // J/(kg K) 104 CeedScalar cp = 1004.; // J/(kg K) 105 CeedScalar g[3] = {0, 0, 0}; // m/s^2 106 CeedScalar lambda = -2. / 3.; // - 107 CeedScalar mu = 1.8e-5; // Pa s, dynamic viscosity 108 CeedScalar k = 0.02638; // W/(m K) 109 CeedScalar c_tau = 0.5 / degree; // - 110 CeedScalar Ctau_t = 1.0; // - 111 CeedScalar Cv_func[3] = {36, 60, 128}; 112 CeedScalar Ctau_v = Cv_func[(CeedInt)Min(3, degree) - 1]; 113 CeedScalar Ctau_C = 0.25 / degree; 114 CeedScalar Ctau_M = 0.25 / degree; 115 CeedScalar Ctau_E = 0.125; 116 PetscReal domain_min[3], domain_max[3], domain_size[3]; 117 PetscCall(DMGetBoundingBox(dm, domain_min, domain_max)); 118 for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i]; 119 120 StatePrimitive reference = {.pressure = 1.01e5, .velocity = {0}, .temperature = 288.15}; 121 CeedScalar idl_decay_time = -1, idl_start = 0, idl_length = 0; 122 PetscBool idl_enable = PETSC_FALSE; 123 124 // ------------------------------------------------------ 125 // Create the PETSc context 126 // ------------------------------------------------------ 127 PetscScalar meter = 1; // 1 meter in scaled length units 128 PetscScalar kilogram = 1; // 1 kilogram in scaled mass units 129 PetscScalar second = 1; // 1 second in scaled time units 130 PetscScalar Kelvin = 1; // 1 Kelvin in scaled temperature units 131 PetscScalar W_per_m_K, Pascal, J_per_kg_K, m_per_squared_s; 132 133 // ------------------------------------------------------ 134 // Command line Options 135 // ------------------------------------------------------ 136 PetscBool given_option = PETSC_FALSE; 137 PetscOptionsBegin(comm, NULL, "Options for Newtonian Ideal Gas based problem", NULL); 138 // -- Conservative vs Primitive variables 139 PetscCall(PetscOptionsEnum("-state_var", "State variables used", NULL, StateVariables, (PetscEnum)(state_var = STATEVAR_CONSERVATIVE), 140 (PetscEnum *)&state_var, NULL)); 141 142 switch (state_var) { 143 case STATEVAR_CONSERVATIVE: 144 problem->ics.qfunction = ICsNewtonianIG_Conserv; 145 problem->ics.qfunction_loc = ICsNewtonianIG_Conserv_loc; 146 problem->apply_vol_rhs.qfunction = RHSFunction_Newtonian; 147 problem->apply_vol_rhs.qfunction_loc = RHSFunction_Newtonian_loc; 148 problem->apply_vol_ifunction.qfunction = IFunction_Newtonian_Conserv; 149 problem->apply_vol_ifunction.qfunction_loc = IFunction_Newtonian_Conserv_loc; 150 problem->apply_vol_ijacobian.qfunction = IJacobian_Newtonian_Conserv; 151 problem->apply_vol_ijacobian.qfunction_loc = IJacobian_Newtonian_Conserv_loc; 152 problem->apply_inflow.qfunction = BoundaryIntegral_Conserv; 153 problem->apply_inflow.qfunction_loc = BoundaryIntegral_Conserv_loc; 154 problem->apply_inflow_jacobian.qfunction = BoundaryIntegral_Jacobian_Conserv; 155 problem->apply_inflow_jacobian.qfunction_loc = BoundaryIntegral_Jacobian_Conserv_loc; 156 break; 157 158 case STATEVAR_PRIMITIVE: 159 problem->ics.qfunction = ICsNewtonianIG_Prim; 160 problem->ics.qfunction_loc = ICsNewtonianIG_Prim_loc; 161 problem->apply_vol_ifunction.qfunction = IFunction_Newtonian_Prim; 162 problem->apply_vol_ifunction.qfunction_loc = IFunction_Newtonian_Prim_loc; 163 problem->apply_vol_ijacobian.qfunction = IJacobian_Newtonian_Prim; 164 problem->apply_vol_ijacobian.qfunction_loc = IJacobian_Newtonian_Prim_loc; 165 problem->apply_inflow.qfunction = BoundaryIntegral_Prim; 166 problem->apply_inflow.qfunction_loc = BoundaryIntegral_Prim_loc; 167 problem->apply_inflow_jacobian.qfunction = BoundaryIntegral_Jacobian_Prim; 168 problem->apply_inflow_jacobian.qfunction_loc = BoundaryIntegral_Jacobian_Prim_loc; 169 break; 170 } 171 172 // -- Physics 173 PetscCall(PetscOptionsScalar("-cv", "Heat capacity at constant volume", NULL, cv, &cv, NULL)); 174 PetscCall(PetscOptionsScalar("-cp", "Heat capacity at constant pressure", NULL, cp, &cp, NULL)); 175 PetscCall(PetscOptionsScalar("-lambda", "Stokes hypothesis second viscosity coefficient", NULL, lambda, &lambda, NULL)); 176 PetscCall(PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient", NULL, mu, &mu, NULL)); 177 PetscCall(PetscOptionsScalar("-k", "Thermal conductivity", NULL, k, &k, NULL)); 178 179 PetscInt dim = problem->dim; 180 PetscCall(PetscOptionsDeprecated("-g", "-gravity", "libCEED 0.11.1", NULL)); 181 PetscCall(PetscOptionsRealArray("-g", "Gravitational acceleration vector", NULL, g, &dim, &given_option)); 182 dim = problem->dim; 183 PetscCall(PetscOptionsRealArray("-gravity", "Gravitational acceleration vector", NULL, g, &dim, &given_option)); 184 if (given_option) PetscCheck(dim == 3, comm, PETSC_ERR_ARG_SIZ, "Gravity vector must be size 3, %" PetscInt_FMT " values given", dim); 185 186 PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL)); 187 PetscCall(PetscOptionsScalar("-c_tau", "Stabilization constant", NULL, c_tau, &c_tau, NULL)); 188 PetscCall(PetscOptionsScalar("-Ctau_t", "Stabilization time constant", NULL, Ctau_t, &Ctau_t, NULL)); 189 PetscCall(PetscOptionsScalar("-Ctau_v", "Stabilization viscous constant", NULL, Ctau_v, &Ctau_v, NULL)); 190 PetscCall(PetscOptionsScalar("-Ctau_C", "Stabilization continuity constant", NULL, Ctau_C, &Ctau_C, NULL)); 191 PetscCall(PetscOptionsScalar("-Ctau_M", "Stabilization momentum constant", NULL, Ctau_M, &Ctau_M, NULL)); 192 PetscCall(PetscOptionsScalar("-Ctau_E", "Stabilization energy constant", NULL, Ctau_E, &Ctau_E, NULL)); 193 PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL)); 194 PetscCall(PetscOptionsBool("-newtonian_unit_tests", "Run Newtonian unit tests", NULL, unit_tests = PETSC_FALSE, &unit_tests, NULL)); 195 196 dim = 3; 197 PetscCall(PetscOptionsScalar("-reference_pressure", "Reference/initial pressure", NULL, reference.pressure, &reference.pressure, NULL)); 198 PetscCall(PetscOptionsScalarArray("-reference_velocity", "Reference/initial velocity", NULL, reference.velocity, &dim, NULL)); 199 PetscCall(PetscOptionsScalar("-reference_temperature", "Reference/initial temperature", NULL, reference.temperature, &reference.temperature, NULL)); 200 201 // -- Units 202 PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL)); 203 meter = fabs(meter); 204 PetscCall(PetscOptionsScalar("-units_kilogram", "1 kilogram in scaled mass units", NULL, kilogram, &kilogram, NULL)); 205 kilogram = fabs(kilogram); 206 PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL)); 207 second = fabs(second); 208 PetscCall(PetscOptionsScalar("-units_Kelvin", "1 Kelvin in scaled temperature units", NULL, Kelvin, &Kelvin, NULL)); 209 Kelvin = fabs(Kelvin); 210 211 // -- Warnings 212 if (stab == STAB_SUPG && !implicit) { 213 PetscCall(PetscPrintf(comm, "Warning! Use -stab supg only with -implicit\n")); 214 } 215 PetscCheck(!(state_var == STATEVAR_PRIMITIVE && !implicit), comm, PETSC_ERR_SUP, 216 "RHSFunction is not provided for primitive variables (use -state_var primitive only with -implicit)\n"); 217 218 PetscCall(PetscOptionsScalar("-idl_decay_time", "Characteristic timescale of the pressure deviance decay. The timestep is good starting point", 219 NULL, idl_decay_time, &idl_decay_time, &idl_enable)); 220 if (idl_enable && idl_decay_time == 0) SETERRQ(comm, PETSC_ERR_SUP, "idl_decay_time may not be equal to zero."); 221 else if (idl_decay_time < 0) idl_enable = PETSC_FALSE; 222 PetscCall(PetscOptionsScalar("-idl_start", "Start of IDL in the x direction", NULL, idl_start, &idl_start, NULL)); 223 PetscCall(PetscOptionsScalar("-idl_length", "Length of IDL in the positive x direction", NULL, idl_length, &idl_length, NULL)); 224 PetscOptionsEnd(); 225 226 // ------------------------------------------------------ 227 // Set up the PETSc context 228 // ------------------------------------------------------ 229 // -- Define derived units 230 Pascal = kilogram / (meter * PetscSqr(second)); 231 J_per_kg_K = PetscSqr(meter) / (PetscSqr(second) * Kelvin); 232 m_per_squared_s = meter / PetscSqr(second); 233 W_per_m_K = kilogram * meter / (pow(second, 3) * Kelvin); 234 235 user->units->meter = meter; 236 user->units->kilogram = kilogram; 237 user->units->second = second; 238 user->units->Kelvin = Kelvin; 239 user->units->Pascal = Pascal; 240 user->units->J_per_kg_K = J_per_kg_K; 241 user->units->m_per_squared_s = m_per_squared_s; 242 user->units->W_per_m_K = W_per_m_K; 243 244 // ------------------------------------------------------ 245 // Set up the libCEED context 246 // ------------------------------------------------------ 247 // -- Scale variables to desired units 248 cv *= J_per_kg_K; 249 cp *= J_per_kg_K; 250 mu *= Pascal * second; 251 k *= W_per_m_K; 252 for (PetscInt i = 0; i < 3; i++) domain_size[i] *= meter; 253 for (PetscInt i = 0; i < 3; i++) g[i] *= m_per_squared_s; 254 reference.pressure *= Pascal; 255 for (PetscInt i = 0; i < 3; i++) reference.velocity[i] *= meter / second; 256 reference.temperature *= Kelvin; 257 problem->dm_scale = meter; 258 259 // -- Solver Settings 260 user->phys->stab = stab; 261 user->phys->implicit = implicit; 262 user->phys->state_var = state_var; 263 user->phys->has_curr_time = has_curr_time; 264 265 // -- QFunction Context 266 newtonian_ig_ctx->lambda = lambda; 267 newtonian_ig_ctx->mu = mu; 268 newtonian_ig_ctx->k = k; 269 newtonian_ig_ctx->cv = cv; 270 newtonian_ig_ctx->cp = cp; 271 newtonian_ig_ctx->c_tau = c_tau; 272 newtonian_ig_ctx->Ctau_t = Ctau_t; 273 newtonian_ig_ctx->Ctau_v = Ctau_v; 274 newtonian_ig_ctx->Ctau_C = Ctau_C; 275 newtonian_ig_ctx->Ctau_M = Ctau_M; 276 newtonian_ig_ctx->Ctau_E = Ctau_E; 277 newtonian_ig_ctx->P0 = reference.pressure; 278 newtonian_ig_ctx->stabilization = stab; 279 newtonian_ig_ctx->P0 = reference.pressure; 280 newtonian_ig_ctx->is_implicit = implicit; 281 newtonian_ig_ctx->state_var = state_var; 282 newtonian_ig_ctx->idl_enable = idl_enable; 283 newtonian_ig_ctx->idl_amplitude = 1 / (idl_decay_time * second); 284 newtonian_ig_ctx->idl_start = idl_start * meter; 285 newtonian_ig_ctx->idl_length = idl_length * meter; 286 PetscCall(PetscArraycpy(newtonian_ig_ctx->g, g, 3)); 287 288 // -- Setup Context 289 setup_context->reference = reference; 290 setup_context->gas = *newtonian_ig_ctx; 291 setup_context->lx = domain_size[0]; 292 setup_context->ly = domain_size[1]; 293 setup_context->lz = domain_size[2]; 294 setup_context->time = 0; 295 296 if (bc->num_freestream > 0) PetscCall(FreestreamBCSetup(problem, dm, ctx, newtonian_ig_ctx, &reference)); 297 if (bc->num_outflow > 0) PetscCall(OutflowBCSetup(problem, dm, ctx, newtonian_ig_ctx, &reference)); 298 299 PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context)); 300 PetscCallCeed(ceed, 301 CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*setup_context), setup_context)); 302 PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(problem->ics.qfunction_context, CEED_MEM_HOST, FreeContextPetsc)); 303 PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(problem->ics.qfunction_context, "evaluation time", offsetof(struct SetupContext_, time), 1, 304 "Time of evaluation")); 305 306 PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &newtonian_ig_context)); 307 PetscCallCeed(ceed, 308 CeedQFunctionContextSetData(newtonian_ig_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*newtonian_ig_ctx), newtonian_ig_ctx)); 309 PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(newtonian_ig_context, CEED_MEM_HOST, FreeContextPetsc)); 310 PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(newtonian_ig_context, "timestep size", offsetof(struct NewtonianIdealGasContext_, dt), 1, 311 "Size of timestep, delta t")); 312 PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(newtonian_ig_context, "ijacobian time shift", 313 offsetof(struct NewtonianIdealGasContext_, ijacobian_time_shift), 1, 314 "Shift for mass matrix in IJacobian")); 315 PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(newtonian_ig_context, "solution time", offsetof(struct NewtonianIdealGasContext_, time), 1, 316 "Current solution time")); 317 318 problem->apply_vol_rhs.qfunction_context = newtonian_ig_context; 319 PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(newtonian_ig_context, &problem->apply_vol_ifunction.qfunction_context)); 320 PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(newtonian_ig_context, &problem->apply_vol_ijacobian.qfunction_context)); 321 PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(newtonian_ig_context, &problem->apply_inflow.qfunction_context)); 322 PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(newtonian_ig_context, &problem->apply_inflow_jacobian.qfunction_context)); 323 324 if (unit_tests) { 325 PetscCall(UnitTests_Newtonian(user, newtonian_ig_ctx)); 326 } 327 PetscFunctionReturn(PETSC_SUCCESS); 328 } 329 330 PetscErrorCode PRINT_NEWTONIAN(User user, ProblemData *problem, AppCtx app_ctx) { 331 MPI_Comm comm = user->comm; 332 Ceed ceed = user->ceed; 333 NewtonianIdealGasContext newtonian_ctx; 334 335 PetscFunctionBeginUser; 336 PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, CEED_MEM_HOST, &newtonian_ctx)); 337 PetscCall(PetscPrintf(comm, 338 " Problem:\n" 339 " Problem Name : %s\n" 340 " Stabilization : %s\n", 341 app_ctx->problem_name, StabilizationTypes[newtonian_ctx->stabilization])); 342 PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, &newtonian_ctx)); 343 PetscFunctionReturn(PETSC_SUCCESS); 344 } 345