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 MPI_Comm comm = PETSC_COMM_WORLD; 78 PetscBool implicit; 79 PetscBool has_curr_time = PETSC_FALSE, 80 prim_var, 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 = PetscOptionsBool("-primitive", "Use primitive variables", 140 NULL, prim_var=PETSC_FALSE, &prim_var, NULL); CHKERRQ(ierr); 141 if (prim_var) { 142 problem->ics.qfunction = ICsNewtonianIG_Prim; 143 problem->ics.qfunction_loc = ICsNewtonianIG_Prim_loc; 144 problem->apply_vol_ifunction.qfunction = IFunction_Newtonian_Prim; 145 problem->apply_vol_ifunction.qfunction_loc = IFunction_Newtonian_Prim_loc; 146 problem->apply_vol_ijacobian.qfunction = IJacobian_Newtonian_Prim; 147 problem->apply_vol_ijacobian.qfunction_loc = IJacobian_Newtonian_Prim_loc; 148 } else { 149 problem->ics.qfunction = ICsNewtonianIG; 150 problem->ics.qfunction_loc = ICsNewtonianIG_loc; 151 problem->apply_vol_rhs.qfunction = RHSFunction_Newtonian; 152 problem->apply_vol_rhs.qfunction_loc = RHSFunction_Newtonian_loc; 153 problem->apply_vol_ifunction.qfunction = IFunction_Newtonian; 154 problem->apply_vol_ifunction.qfunction_loc = IFunction_Newtonian_loc; 155 problem->apply_vol_ijacobian.qfunction = IJacobian_Newtonian; 156 problem->apply_vol_ijacobian.qfunction_loc = IJacobian_Newtonian_loc; 157 problem->apply_inflow.qfunction = BoundaryIntegral; 158 problem->apply_inflow.qfunction_loc = BoundaryIntegral_loc; 159 problem->apply_inflow_jacobian.qfunction = BoundaryIntegral_Jacobian; 160 problem->apply_inflow_jacobian.qfunction_loc = BoundaryIntegral_Jacobian_loc; 161 problem->apply_outflow.qfunction = PressureOutflow; 162 problem->apply_outflow.qfunction_loc = PressureOutflow_loc; 163 problem->apply_outflow_jacobian.qfunction = PressureOutflow_Jacobian; 164 problem->apply_outflow_jacobian.qfunction_loc = PressureOutflow_Jacobian_loc; 165 } 166 167 // -- Physics 168 ierr = PetscOptionsScalar("-cv", "Heat capacity at constant volume", 169 NULL, cv, &cv, NULL); CHKERRQ(ierr); 170 ierr = PetscOptionsScalar("-cp", "Heat capacity at constant pressure", 171 NULL, cp, &cp, NULL); CHKERRQ(ierr); 172 ierr = PetscOptionsScalar("-lambda", 173 "Stokes hypothesis second viscosity coefficient", 174 NULL, lambda, &lambda, NULL); CHKERRQ(ierr); 175 ierr = PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient", 176 NULL, mu, &mu, NULL); CHKERRQ(ierr); 177 ierr = PetscOptionsScalar("-k", "Thermal conductivity", 178 NULL, k, &k, NULL); CHKERRQ(ierr); 179 180 PetscInt dim = problem->dim; 181 ierr = PetscOptionsRealArray("-g", "Gravitational acceleration", 182 NULL, g, &dim, NULL); CHKERRQ(ierr); 183 ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL, 184 StabilizationTypes, (PetscEnum)(stab = STAB_NONE), 185 (PetscEnum *)&stab, NULL); CHKERRQ(ierr); 186 ierr = PetscOptionsScalar("-c_tau", "Stabilization constant", 187 NULL, c_tau, &c_tau, NULL); CHKERRQ(ierr); 188 ierr = PetscOptionsScalar("-Ctau_t", "Stabilization time constant", 189 NULL, Ctau_t, &Ctau_t, NULL); CHKERRQ(ierr); 190 ierr = PetscOptionsScalar("-Ctau_v", "Stabilization viscous constant", 191 NULL, Ctau_v, &Ctau_v, NULL); CHKERRQ(ierr); 192 ierr = PetscOptionsScalar("-Ctau_C", "Stabilization continuity constant", 193 NULL, Ctau_C, &Ctau_C, NULL); CHKERRQ(ierr); 194 ierr = PetscOptionsScalar("-Ctau_M", "Stabilization momentum constant", 195 NULL, Ctau_M, &Ctau_M, NULL); CHKERRQ(ierr); 196 ierr = PetscOptionsScalar("-Ctau_E", "Stabilization energy constant", 197 NULL, Ctau_E, &Ctau_E, NULL); CHKERRQ(ierr); 198 ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", 199 NULL, implicit=PETSC_FALSE, &implicit, NULL); 200 CHKERRQ(ierr); 201 ierr = PetscOptionsBool("-newtonian_unit_tests", "Run Newtonian unit tests", 202 NULL, unit_tests=PETSC_FALSE, &unit_tests, NULL); 203 CHKERRQ(ierr); 204 205 // -- Units 206 ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units", 207 NULL, meter, &meter, NULL); CHKERRQ(ierr); 208 meter = fabs(meter); 209 ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units", 210 NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr); 211 kilogram = fabs(kilogram); 212 ierr = PetscOptionsScalar("-units_second","1 second in scaled time units", 213 NULL, second, &second, NULL); CHKERRQ(ierr); 214 second = fabs(second); 215 ierr = PetscOptionsScalar("-units_Kelvin", 216 "1 Kelvin in scaled temperature units", 217 NULL, Kelvin, &Kelvin, NULL); CHKERRQ(ierr); 218 Kelvin = fabs(Kelvin); 219 220 // -- Warnings 221 if (stab == STAB_SUPG && !implicit) { 222 ierr = PetscPrintf(comm, 223 "Warning! Use -stab supg only with -implicit\n"); 224 CHKERRQ(ierr); 225 } 226 if (prim_var && !implicit) { 227 SETERRQ(comm, PETSC_ERR_ARG_NULL, 228 "RHSFunction is not provided for primitive variables (use -primitive only with -implicit)\n"); 229 } 230 PetscOptionsEnd(); 231 232 // ------------------------------------------------------ 233 // Set up the PETSc context 234 // ------------------------------------------------------ 235 // -- Define derived units 236 Pascal = kilogram / (meter * PetscSqr(second)); 237 J_per_kg_K = PetscSqr(meter) / (PetscSqr(second) * Kelvin); 238 m_per_squared_s = meter / PetscSqr(second); 239 W_per_m_K = kilogram * meter / (pow(second,3) * Kelvin); 240 241 user->units->meter = meter; 242 user->units->kilogram = kilogram; 243 user->units->second = second; 244 user->units->Kelvin = Kelvin; 245 user->units->Pascal = Pascal; 246 user->units->J_per_kg_K = J_per_kg_K; 247 user->units->m_per_squared_s = m_per_squared_s; 248 user->units->W_per_m_K = W_per_m_K; 249 250 // ------------------------------------------------------ 251 // Set up the libCEED context 252 // ------------------------------------------------------ 253 // -- Scale variables to desired units 254 cv *= J_per_kg_K; 255 cp *= J_per_kg_K; 256 mu *= Pascal * second; 257 k *= W_per_m_K; 258 for (PetscInt i=0; i<3; i++) domain_size[i] *= meter; 259 for (PetscInt i=0; i<3; i++) g[i] *= m_per_squared_s; 260 problem->dm_scale = meter; 261 262 // -- Setup Context 263 setup_context->cv = cv; 264 setup_context->cp = cp; 265 setup_context->lx = domain_size[0]; 266 setup_context->ly = domain_size[1]; 267 setup_context->lz = domain_size[2]; 268 setup_context->time = 0; 269 ierr = PetscArraycpy(setup_context->g, g, 3); CHKERRQ(ierr); 270 271 // -- Solver Settings 272 user->phys->stab = stab; 273 user->phys->implicit = implicit; 274 user->phys->primitive = prim_var; 275 user->phys->has_curr_time = has_curr_time; 276 277 // -- QFunction Context 278 newtonian_ig_ctx->lambda = lambda; 279 newtonian_ig_ctx->mu = mu; 280 newtonian_ig_ctx->k = k; 281 newtonian_ig_ctx->cv = cv; 282 newtonian_ig_ctx->cp = cp; 283 newtonian_ig_ctx->c_tau = c_tau; 284 newtonian_ig_ctx->Ctau_t = Ctau_t; 285 newtonian_ig_ctx->Ctau_v = Ctau_v; 286 newtonian_ig_ctx->Ctau_C = Ctau_C; 287 newtonian_ig_ctx->Ctau_M = Ctau_M; 288 newtonian_ig_ctx->Ctau_E = Ctau_E; 289 newtonian_ig_ctx->stabilization = stab; 290 newtonian_ig_ctx->is_implicit = implicit; 291 newtonian_ig_ctx->is_primitive = prim_var; 292 ierr = PetscArraycpy(newtonian_ig_ctx->g, g, 3); CHKERRQ(ierr); 293 294 CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context); 295 CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST, 296 CEED_USE_POINTER, sizeof(*setup_context), setup_context); 297 CeedQFunctionContextSetDataDestroy(problem->ics.qfunction_context, 298 CEED_MEM_HOST, 299 FreeContextPetsc); 300 CeedQFunctionContextRegisterDouble(problem->ics.qfunction_context, 301 "evaluation time", 302 (char *)&setup_context->time - (char *)setup_context, 1, "Time of evaluation"); 303 304 CeedQFunctionContextCreate(user->ceed, &newtonian_ig_context); 305 CeedQFunctionContextSetData(newtonian_ig_context, CEED_MEM_HOST, 306 CEED_USE_POINTER, 307 sizeof(*newtonian_ig_ctx), newtonian_ig_ctx); 308 CeedQFunctionContextSetDataDestroy(newtonian_ig_context, CEED_MEM_HOST, 309 FreeContextPetsc); 310 CeedQFunctionContextRegisterDouble(newtonian_ig_context, "timestep size", 311 offsetof(struct NewtonianIdealGasContext_, dt), 1, "Size of timestep, delta t"); 312 CeedQFunctionContextRegisterDouble(newtonian_ig_context, "ijacobian time shift", 313 offsetof(struct NewtonianIdealGasContext_, ijacobian_time_shift), 1, 314 "Shift for mass matrix in IJacobian"); 315 problem->apply_vol_rhs.qfunction_context = newtonian_ig_context; 316 CeedQFunctionContextReferenceCopy(newtonian_ig_context, 317 &problem->apply_vol_ifunction.qfunction_context); 318 CeedQFunctionContextReferenceCopy(newtonian_ig_context, 319 &problem->apply_vol_ijacobian.qfunction_context); 320 CeedQFunctionContextReferenceCopy(newtonian_ig_context, 321 &problem->apply_inflow.qfunction_context); 322 CeedQFunctionContextReferenceCopy(newtonian_ig_context, 323 &problem->apply_inflow_jacobian.qfunction_context); 324 CeedQFunctionContextReferenceCopy(newtonian_ig_context, 325 &problem->apply_outflow.qfunction_context); 326 CeedQFunctionContextReferenceCopy(newtonian_ig_context, 327 &problem->apply_outflow_jacobian.qfunction_context); 328 329 if (unit_tests) { 330 PetscCall(UnitTests_Newtonian(user, newtonian_ig_ctx)); 331 } 332 PetscFunctionReturn(0); 333 } 334 335 PetscErrorCode PRINT_NEWTONIAN(ProblemData *problem, AppCtx app_ctx) { 336 337 MPI_Comm comm = PETSC_COMM_WORLD; 338 PetscErrorCode ierr; 339 NewtonianIdealGasContext newtonian_ctx; 340 341 PetscFunctionBeginUser; 342 CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, 343 CEED_MEM_HOST, &newtonian_ctx); 344 ierr = PetscPrintf(comm, 345 " Problem:\n" 346 " Problem Name : %s\n" 347 " Stabilization : %s\n", 348 app_ctx->problem_name, StabilizationTypes[newtonian_ctx->stabilization]); 349 CHKERRQ(ierr); 350 CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, 351 &newtonian_ctx); 352 PetscFunctionReturn(0); 353 } 354