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