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, 43 m = units->meter, 44 sec = units->second, 45 Pascal = units->Pascal; 46 PetscFunctionBeginUser; 47 const CeedScalar rho = 1.2 * kg / (m*m*m), 48 u = 40 * m/sec; 49 CeedScalar U[5] = {rho, rho*u, rho *u*1.1, rho *u*1.2, 250e3*Pascal + .5*rho *u*u}; 50 const CeedScalar x[3] = {.1, .2, .3}; 51 State s = StateFromU(gas, U, x); 52 for (int i=0; i<8; i++) { 53 CeedScalar dU[5] = {0}, dx[3] = {0}; 54 if (i < 5) dU[i] = U[i]; 55 else dx[i-5] = x[i-5]; 56 State ds = StateFromU_fwd(gas, s, dU, x, dx); 57 for (int j=0; j<5; j++) dU[j] = (1 + eps * (i == j)) * U[j]; 58 for (int j=0; j<3; j++) dx[j] = (1 + eps * (i == 5 + j)) * x[j]; 59 State t = StateFromU(gas, dU, dx); 60 StatePrimitive dY; 61 dY.pressure = (t.Y.pressure - s.Y.pressure) / eps; 62 for (int j=0; j<3; j++) 63 dY.velocity[j] = (t.Y.velocity[j] - s.Y.velocity[j]) / eps; 64 dY.temperature = (t.Y.temperature - s.Y.temperature) / eps; 65 char buf[128]; 66 snprintf(buf, sizeof buf, "StateFromU_fwd i=%d", i); 67 PetscCall(CheckPrimitiveWithTolerance(dY, ds.Y, dY, buf, 5e-6, 1e-6, 1e-6)); 68 } 69 PetscFunctionReturn(0); 70 } 71 72 PetscErrorCode NS_NEWTONIAN_IG(ProblemData *problem, DM dm, void *ctx) { 73 74 SetupContext setup_context; 75 User user = *(User *)ctx; 76 StabilizationType stab; 77 StateVariable state_var; 78 MPI_Comm comm = PETSC_COMM_WORLD; 79 PetscBool implicit; 80 PetscBool has_curr_time = PETSC_FALSE, unit_tests; 81 PetscInt ierr; 82 NewtonianIdealGasContext newtonian_ig_ctx; 83 CeedQFunctionContext newtonian_ig_context; 84 85 PetscFunctionBeginUser; 86 ierr = PetscCalloc1(1, &setup_context); CHKERRQ(ierr); 87 ierr = PetscCalloc1(1, &newtonian_ig_ctx); CHKERRQ(ierr); 88 89 // ------------------------------------------------------ 90 // Setup Generic Newtonian IG Problem 91 // ------------------------------------------------------ 92 problem->dim = 3; 93 problem->q_data_size_vol = 10; 94 problem->q_data_size_sur = 10; 95 problem->jac_data_size_sur = 11; 96 problem->setup_vol.qfunction = Setup; 97 problem->setup_vol.qfunction_loc = Setup_loc; 98 problem->setup_sur.qfunction = SetupBoundary; 99 problem->setup_sur.qfunction_loc = SetupBoundary_loc; 100 problem->bc = NULL; 101 problem->bc_ctx = setup_context; 102 problem->non_zero_time = PETSC_FALSE; 103 problem->print_info = PRINT_NEWTONIAN; 104 105 // ------------------------------------------------------ 106 // Create the libCEED context 107 // ------------------------------------------------------ 108 CeedScalar cv = 717.; // J/(kg K) 109 CeedScalar cp = 1004.; // J/(kg K) 110 CeedScalar g[3] = {0, 0, -9.81}; // m/s^2 111 CeedScalar lambda = -2./3.; // - 112 CeedScalar mu = 1.8e-5; // Pa s, dynamic viscosity 113 CeedScalar k = 0.02638; // W/(m K) 114 CeedScalar c_tau = 0.5; // - 115 CeedScalar Ctau_t = 1.0; // - 116 CeedScalar Ctau_v = 36.0; // TODO make function of degree 117 CeedScalar Ctau_C = 1.0; // TODO make function of degree 118 CeedScalar Ctau_M = 1.0; // TODO make function of degree 119 CeedScalar Ctau_E = 1.0; // TODO make function of degree 120 PetscReal domain_min[3], domain_max[3], domain_size[3]; 121 ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr); 122 for (PetscInt i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i]; 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 PetscOptionsBegin(comm, NULL, "Options for Newtonian Ideal Gas based problem", 137 NULL); 138 // -- Conservative vs Primitive variables 139 ierr = PetscOptionsEnum("-state_var", "State variables used", NULL, 140 StateVariables, (PetscEnum)(state_var = STATEVAR_CONSERVATIVE), 141 (PetscEnum *)&state_var, NULL); CHKERRQ(ierr); 142 143 // *INDENT-OFF* 144 switch (state_var) { 145 case STATEVAR_CONSERVATIVE: 146 problem->ics.qfunction = ICsNewtonianIG; 147 problem->ics.qfunction_loc = ICsNewtonianIG_loc; 148 problem->apply_vol_rhs.qfunction = RHSFunction_Newtonian; 149 problem->apply_vol_rhs.qfunction_loc = RHSFunction_Newtonian_loc; 150 problem->apply_vol_ifunction.qfunction = IFunction_Newtonian_Conserv; 151 problem->apply_vol_ifunction.qfunction_loc = IFunction_Newtonian_Conserv_loc; 152 problem->apply_vol_ijacobian.qfunction = IJacobian_Newtonian_Conserv; 153 problem->apply_vol_ijacobian.qfunction_loc = IJacobian_Newtonian_Conserv_loc; 154 problem->apply_inflow.qfunction = BoundaryIntegral_Conserv; 155 problem->apply_inflow.qfunction_loc = BoundaryIntegral_Conserv_loc; 156 problem->apply_inflow_jacobian.qfunction = BoundaryIntegral_Jacobian_Conserv; 157 problem->apply_inflow_jacobian.qfunction_loc = BoundaryIntegral_Jacobian_Conserv_loc; 158 problem->apply_outflow.qfunction = PressureOutflow_Conserv; 159 problem->apply_outflow.qfunction_loc = PressureOutflow_Conserv_loc; 160 problem->apply_outflow_jacobian.qfunction = PressureOutflow_Jacobian_Conserv; 161 problem->apply_outflow_jacobian.qfunction_loc = PressureOutflow_Jacobian_Conserv_loc; 162 break; 163 164 case STATEVAR_PRIMITIVE: 165 problem->ics.qfunction = ICsNewtonianIG_Prim; 166 problem->ics.qfunction_loc = ICsNewtonianIG_Prim_loc; 167 problem->apply_vol_ifunction.qfunction = IFunction_Newtonian_Prim; 168 problem->apply_vol_ifunction.qfunction_loc = IFunction_Newtonian_Prim_loc; 169 problem->apply_vol_ijacobian.qfunction = IJacobian_Newtonian_Prim; 170 problem->apply_vol_ijacobian.qfunction_loc = IJacobian_Newtonian_Prim_loc; 171 problem->apply_inflow.qfunction = BoundaryIntegral_Prim; 172 problem->apply_inflow.qfunction_loc = BoundaryIntegral_Prim_loc; 173 problem->apply_inflow_jacobian.qfunction = BoundaryIntegral_Jacobian_Prim; 174 problem->apply_inflow_jacobian.qfunction_loc = BoundaryIntegral_Jacobian_Prim_loc; 175 problem->apply_outflow.qfunction = PressureOutflow_Prim; 176 problem->apply_outflow.qfunction_loc = PressureOutflow_Prim_loc; 177 problem->apply_outflow_jacobian.qfunction = PressureOutflow_Jacobian_Prim; 178 problem->apply_outflow_jacobian.qfunction_loc = PressureOutflow_Jacobian_Prim_loc; 179 break; 180 } 181 // *INDENT-ON* 182 183 // -- Physics 184 ierr = PetscOptionsScalar("-cv", "Heat capacity at constant volume", 185 NULL, cv, &cv, NULL); CHKERRQ(ierr); 186 ierr = PetscOptionsScalar("-cp", "Heat capacity at constant pressure", 187 NULL, cp, &cp, NULL); CHKERRQ(ierr); 188 ierr = PetscOptionsScalar("-lambda", 189 "Stokes hypothesis second viscosity coefficient", 190 NULL, lambda, &lambda, NULL); CHKERRQ(ierr); 191 ierr = PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient", 192 NULL, mu, &mu, NULL); CHKERRQ(ierr); 193 ierr = PetscOptionsScalar("-k", "Thermal conductivity", 194 NULL, k, &k, NULL); CHKERRQ(ierr); 195 196 PetscInt dim = problem->dim; 197 ierr = PetscOptionsRealArray("-g", "Gravitational acceleration", 198 NULL, g, &dim, NULL); CHKERRQ(ierr); 199 ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL, 200 StabilizationTypes, (PetscEnum)(stab = STAB_NONE), 201 (PetscEnum *)&stab, NULL); CHKERRQ(ierr); 202 ierr = PetscOptionsScalar("-c_tau", "Stabilization constant", 203 NULL, c_tau, &c_tau, NULL); CHKERRQ(ierr); 204 ierr = PetscOptionsScalar("-Ctau_t", "Stabilization time constant", 205 NULL, Ctau_t, &Ctau_t, NULL); CHKERRQ(ierr); 206 ierr = PetscOptionsScalar("-Ctau_v", "Stabilization viscous constant", 207 NULL, Ctau_v, &Ctau_v, NULL); CHKERRQ(ierr); 208 ierr = PetscOptionsScalar("-Ctau_C", "Stabilization continuity constant", 209 NULL, Ctau_C, &Ctau_C, NULL); CHKERRQ(ierr); 210 ierr = PetscOptionsScalar("-Ctau_M", "Stabilization momentum constant", 211 NULL, Ctau_M, &Ctau_M, NULL); CHKERRQ(ierr); 212 ierr = PetscOptionsScalar("-Ctau_E", "Stabilization energy constant", 213 NULL, Ctau_E, &Ctau_E, NULL); CHKERRQ(ierr); 214 ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", 215 NULL, implicit=PETSC_FALSE, &implicit, NULL); 216 CHKERRQ(ierr); 217 ierr = PetscOptionsBool("-newtonian_unit_tests", "Run Newtonian unit tests", 218 NULL, unit_tests=PETSC_FALSE, &unit_tests, NULL); 219 CHKERRQ(ierr); 220 221 // -- Units 222 ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units", 223 NULL, meter, &meter, NULL); CHKERRQ(ierr); 224 meter = fabs(meter); 225 ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units", 226 NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr); 227 kilogram = fabs(kilogram); 228 ierr = PetscOptionsScalar("-units_second","1 second in scaled time units", 229 NULL, second, &second, NULL); CHKERRQ(ierr); 230 second = fabs(second); 231 ierr = PetscOptionsScalar("-units_Kelvin", 232 "1 Kelvin in scaled temperature units", 233 NULL, Kelvin, &Kelvin, NULL); CHKERRQ(ierr); 234 Kelvin = fabs(Kelvin); 235 236 // -- Warnings 237 if (stab == STAB_SUPG && !implicit) { 238 ierr = PetscPrintf(comm, 239 "Warning! Use -stab supg only with -implicit\n"); 240 CHKERRQ(ierr); 241 } 242 if (state_var==STATEVAR_PRIMITIVE && !implicit) { 243 SETERRQ(comm, PETSC_ERR_ARG_NULL, 244 "RHSFunction is not provided for primitive variables (use -state_var primitive only with -implicit)\n"); 245 } 246 PetscOptionsEnd(); 247 248 // ------------------------------------------------------ 249 // Set up the PETSc context 250 // ------------------------------------------------------ 251 // -- Define derived units 252 Pascal = kilogram / (meter * PetscSqr(second)); 253 J_per_kg_K = PetscSqr(meter) / (PetscSqr(second) * Kelvin); 254 m_per_squared_s = meter / PetscSqr(second); 255 W_per_m_K = kilogram * meter / (pow(second,3) * Kelvin); 256 257 user->units->meter = meter; 258 user->units->kilogram = kilogram; 259 user->units->second = second; 260 user->units->Kelvin = Kelvin; 261 user->units->Pascal = Pascal; 262 user->units->J_per_kg_K = J_per_kg_K; 263 user->units->m_per_squared_s = m_per_squared_s; 264 user->units->W_per_m_K = W_per_m_K; 265 266 // ------------------------------------------------------ 267 // Set up the libCEED context 268 // ------------------------------------------------------ 269 // -- Scale variables to desired units 270 cv *= J_per_kg_K; 271 cp *= J_per_kg_K; 272 mu *= Pascal * second; 273 k *= W_per_m_K; 274 for (PetscInt i=0; i<3; i++) domain_size[i] *= meter; 275 for (PetscInt i=0; i<3; i++) g[i] *= m_per_squared_s; 276 problem->dm_scale = meter; 277 278 // -- Setup Context 279 setup_context->cv = cv; 280 setup_context->cp = cp; 281 setup_context->lx = domain_size[0]; 282 setup_context->ly = domain_size[1]; 283 setup_context->lz = domain_size[2]; 284 setup_context->time = 0; 285 ierr = PetscArraycpy(setup_context->g, g, 3); CHKERRQ(ierr); 286 287 // -- Solver Settings 288 user->phys->stab = stab; 289 user->phys->implicit = implicit; 290 user->phys->state_var = state_var; 291 user->phys->has_curr_time = has_curr_time; 292 293 // -- QFunction Context 294 newtonian_ig_ctx->lambda = lambda; 295 newtonian_ig_ctx->mu = mu; 296 newtonian_ig_ctx->k = k; 297 newtonian_ig_ctx->cv = cv; 298 newtonian_ig_ctx->cp = cp; 299 newtonian_ig_ctx->c_tau = c_tau; 300 newtonian_ig_ctx->Ctau_t = Ctau_t; 301 newtonian_ig_ctx->Ctau_v = Ctau_v; 302 newtonian_ig_ctx->Ctau_C = Ctau_C; 303 newtonian_ig_ctx->Ctau_M = Ctau_M; 304 newtonian_ig_ctx->Ctau_E = Ctau_E; 305 newtonian_ig_ctx->stabilization = stab; 306 newtonian_ig_ctx->is_implicit = implicit; 307 newtonian_ig_ctx->state_var = state_var; 308 ierr = PetscArraycpy(newtonian_ig_ctx->g, g, 3); CHKERRQ(ierr); 309 310 CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context); 311 CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST, 312 CEED_USE_POINTER, sizeof(*setup_context), setup_context); 313 CeedQFunctionContextSetDataDestroy(problem->ics.qfunction_context, 314 CEED_MEM_HOST, 315 FreeContextPetsc); 316 CeedQFunctionContextRegisterDouble(problem->ics.qfunction_context, 317 "evaluation time", 318 (char *)&setup_context->time - (char *)setup_context, 1, "Time of evaluation"); 319 320 CeedQFunctionContextCreate(user->ceed, &newtonian_ig_context); 321 CeedQFunctionContextSetData(newtonian_ig_context, CEED_MEM_HOST, 322 CEED_USE_POINTER, 323 sizeof(*newtonian_ig_ctx), newtonian_ig_ctx); 324 CeedQFunctionContextSetDataDestroy(newtonian_ig_context, CEED_MEM_HOST, 325 FreeContextPetsc); 326 CeedQFunctionContextRegisterDouble(newtonian_ig_context, "timestep size", 327 offsetof(struct NewtonianIdealGasContext_, dt), 1, "Size of timestep, delta t"); 328 CeedQFunctionContextRegisterDouble(newtonian_ig_context, "ijacobian time shift", 329 offsetof(struct NewtonianIdealGasContext_, ijacobian_time_shift), 1, 330 "Shift for mass matrix in IJacobian"); 331 problem->apply_vol_rhs.qfunction_context = newtonian_ig_context; 332 CeedQFunctionContextReferenceCopy(newtonian_ig_context, 333 &problem->apply_vol_ifunction.qfunction_context); 334 CeedQFunctionContextReferenceCopy(newtonian_ig_context, 335 &problem->apply_vol_ijacobian.qfunction_context); 336 CeedQFunctionContextReferenceCopy(newtonian_ig_context, 337 &problem->apply_inflow.qfunction_context); 338 CeedQFunctionContextReferenceCopy(newtonian_ig_context, 339 &problem->apply_inflow_jacobian.qfunction_context); 340 CeedQFunctionContextReferenceCopy(newtonian_ig_context, 341 &problem->apply_outflow.qfunction_context); 342 CeedQFunctionContextReferenceCopy(newtonian_ig_context, 343 &problem->apply_outflow_jacobian.qfunction_context); 344 345 if (unit_tests) { 346 PetscCall(UnitTests_Newtonian(user, newtonian_ig_ctx)); 347 } 348 PetscFunctionReturn(0); 349 } 350 351 PetscErrorCode PRINT_NEWTONIAN(ProblemData *problem, AppCtx app_ctx) { 352 353 MPI_Comm comm = PETSC_COMM_WORLD; 354 PetscErrorCode ierr; 355 NewtonianIdealGasContext newtonian_ctx; 356 357 PetscFunctionBeginUser; 358 CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, 359 CEED_MEM_HOST, &newtonian_ctx); 360 ierr = PetscPrintf(comm, 361 " Problem:\n" 362 " Problem Name : %s\n" 363 " Stabilization : %s\n", 364 app_ctx->problem_name, StabilizationTypes[newtonian_ctx->stabilization]); 365 CHKERRQ(ierr); 366 CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, 367 &newtonian_ctx); 368 PetscFunctionReturn(0); 369 } 370