1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors. 2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause 3 4 /// @file 5 /// Utility functions for setting up problems using the Newtonian Qfunction 6 7 #include "../qfunctions/newtonian.h" 8 9 #include <ceed.h> 10 #include <petscdm.h> 11 12 #include <navierstokes.h> 13 14 const char *const StateVariables[] = {"CONSERVATIVE", "PRIMITIVE", "ENTROPY", "StateVariable", "STATEVAR_", NULL}; 15 const char *const StabilizationTypes[] = {"NONE", "SU", "SUPG", "StabilizationType", "STAB_", NULL}; 16 17 static PetscErrorCode UnitTests_Newtonian(Honee honee, NewtonianIGProperties gas); 18 19 static PetscErrorCode PRINT_NEWTONIAN(Honee honee, ProblemData problem, AppCtx app_ctx) { 20 MPI_Comm comm = honee->comm; 21 Ceed ceed = honee->ceed; 22 NewtonianIdealGasContext newt_ctx; 23 24 PetscFunctionBeginUser; 25 PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->apply_vol_rhs.qfctx, CEED_MEM_HOST, &newt_ctx)); 26 PetscCall(PetscPrintf(comm, 27 " Problem:\n" 28 " Problem Name : %s\n" 29 " Stabilization : %s\n", 30 app_ctx->problem_name, StabilizationTypes[newt_ctx->stabilization])); 31 PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfctx, &newt_ctx)); 32 PetscFunctionReturn(PETSC_SUCCESS); 33 } 34 35 // @brief Create CeedOperator for stabilized mass KSP for explicit timestepping 36 // 37 // Only used for SUPG stabilization 38 PetscErrorCode CreateKSPMassOperator_NewtonianStabilized(Honee honee, CeedOperator *op_mass) { 39 Ceed ceed = honee->ceed; 40 CeedInt num_comp_q, q_data_size; 41 CeedQFunction qf_mass; 42 CeedElemRestriction elem_restr_q, elem_restr_qd; 43 CeedBasis basis_q; 44 CeedVector q_data; 45 CeedQFunctionContext qfctx = NULL; 46 PetscInt dim = 3; 47 48 PetscFunctionBeginUser; 49 { // Get restriction and basis from the RHS function 50 CeedOperator *sub_ops; 51 CeedOperatorField op_field; 52 PetscInt sub_op_index = 0; // will be 0 for the volume op 53 54 PetscCallCeed(ceed, CeedOperatorCompositeGetSubList(honee->op_rhs_ctx->op, &sub_ops)); 55 PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "q", &op_field)); 56 PetscCallCeed(ceed, CeedOperatorFieldGetData(op_field, NULL, &elem_restr_q, &basis_q, NULL)); 57 PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "qdata", &op_field)); 58 PetscCallCeed(ceed, CeedOperatorFieldGetData(op_field, NULL, &elem_restr_qd, NULL, &q_data)); 59 60 PetscCallCeed(ceed, CeedOperatorGetContext(sub_ops[sub_op_index], &qfctx)); 61 } 62 63 PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_q, &num_comp_q)); 64 PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_qd, &q_data_size)); 65 66 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, MassFunction_Newtonian_Conserv, MassFunction_Newtonian_Conserv_loc, &qf_mass)); 67 68 PetscCallCeed(ceed, CeedQFunctionSetContext(qf_mass, qfctx)); 69 PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(qf_mass, 0)); 70 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_mass, "q_dot", 5, CEED_EVAL_INTERP)); 71 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_mass, "q", 5, CEED_EVAL_INTERP)); 72 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_mass, "qdata", q_data_size, CEED_EVAL_NONE)); 73 PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_mass, "v", 5, CEED_EVAL_INTERP)); 74 PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_mass, "Grad_v", 5 * dim, CEED_EVAL_GRAD)); 75 76 PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, op_mass)); 77 PetscCallCeed(ceed, CeedOperatorSetName(*op_mass, "RHS Mass Operator, Newtonian Stabilized")); 78 PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "q_dot", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 79 PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "q", elem_restr_q, basis_q, honee->q_ceed)); 80 PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data)); 81 PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "v", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 82 PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "Grad_v", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 83 84 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_q)); 85 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd)); 86 PetscCallCeed(ceed, CeedVectorDestroy(&q_data)); 87 PetscCallCeed(ceed, CeedBasisDestroy(&basis_q)); 88 PetscCallCeed(ceed, CeedQFunctionContextDestroy(&qfctx)); 89 PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass)); 90 PetscFunctionReturn(PETSC_SUCCESS); 91 } 92 93 /** 94 @brief Create RHS CeedOperator for direct projection of divergence of diffusive flux 95 96 @param[in] honee `Honee` context 97 @param[in] diff_flux_proj `DivDiffFluxProjectionData` object 98 @param[out] op_rhs Operator to calculate the RHS of the L^2 projection 99 **/ 100 static PetscErrorCode DivDiffFluxProjectionCreateRHS_Direct_NS(Honee honee, DivDiffFluxProjectionData diff_flux_proj, CeedOperator *op_rhs) { 101 Ceed ceed = honee->ceed; 102 NodalProjectionData projection = diff_flux_proj->projection; 103 PetscInt dim, num_comp_q; 104 CeedQFunctionContext newtonian_qfctx = NULL; 105 CeedElemRestriction elem_restr_q; 106 CeedBasis basis_q; 107 108 PetscFunctionBeginUser; 109 // -- Get Pre-requisite things 110 PetscCall(DMGetDimension(projection->dm, &dim)); 111 PetscCall(DMGetFieldNumComps(honee->dm, 0, &num_comp_q)); 112 113 { // Get newtonian QF context 114 CeedOperator *sub_ops, main_op = honee->op_ifunction ? honee->op_ifunction : honee->op_rhs_ctx->op; 115 PetscInt sub_op_index = 0; // will be 0 for the volume op 116 117 PetscCallCeed(ceed, CeedOperatorCompositeGetSubList(main_op, &sub_ops)); 118 PetscCallCeed(ceed, CeedOperatorGetContext(sub_ops[sub_op_index], &newtonian_qfctx)); 119 } 120 PetscCallCeed(ceed, CeedOperatorCreateComposite(ceed, op_rhs)); 121 { // Add the volume integral CeedOperator 122 CeedQFunction qf_rhs_volume; 123 CeedOperator op_rhs_volume; 124 CeedVector q_data; 125 CeedElemRestriction elem_restr_qd, elem_restr_diff_flux_volume = NULL; 126 CeedBasis basis_diff_flux = NULL; 127 CeedInt q_data_size; 128 129 PetscCall(DivDiffFluxProjectionGetOperatorFieldData(diff_flux_proj, &elem_restr_diff_flux_volume, &basis_diff_flux, NULL, NULL)); 130 PetscCall(DMPlexCeedElemRestrictionCreate(ceed, honee->dm, DMLABEL_DEFAULT, DMLABEL_DEFAULT_VALUE, 0, 0, &elem_restr_q)); 131 PetscCall(DMPlexCeedBasisCreate(ceed, honee->dm, DMLABEL_DEFAULT, DMLABEL_DEFAULT_VALUE, 0, 0, &basis_q)); 132 PetscCall(QDataGet(ceed, projection->dm, DMLABEL_DEFAULT, DMLABEL_DEFAULT_VALUE, &elem_restr_qd, &q_data, &q_data_size)); 133 switch (honee->phys->state_var) { 134 case STATEVAR_PRIMITIVE: 135 PetscCallCeed(ceed, 136 CeedQFunctionCreateInterior(ceed, 1, DivDiffusiveFluxVolumeRHS_NS_Prim, DivDiffusiveFluxVolumeRHS_NS_Prim_loc, &qf_rhs_volume)); 137 break; 138 case STATEVAR_CONSERVATIVE: 139 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DivDiffusiveFluxVolumeRHS_NS_Conserv, DivDiffusiveFluxVolumeRHS_NS_Conserv_loc, 140 &qf_rhs_volume)); 141 break; 142 case STATEVAR_ENTROPY: 143 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DivDiffusiveFluxVolumeRHS_NS_Entropy, DivDiffusiveFluxVolumeRHS_NS_Entropy_loc, 144 &qf_rhs_volume)); 145 break; 146 } 147 148 PetscCallCeed(ceed, CeedQFunctionSetContext(qf_rhs_volume, newtonian_qfctx)); 149 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_volume, "q", num_comp_q, CEED_EVAL_INTERP)); 150 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_volume, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD)); 151 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_volume, "qdata", q_data_size, CEED_EVAL_NONE)); 152 PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_rhs_volume, "diffusive flux RHS", projection->num_comp * dim, CEED_EVAL_GRAD)); 153 154 PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_rhs_volume, NULL, NULL, &op_rhs_volume)); 155 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_volume, "q", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 156 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_volume, "Grad_q", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 157 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_volume, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data)); 158 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_volume, "diffusive flux RHS", elem_restr_diff_flux_volume, basis_diff_flux, CEED_VECTOR_ACTIVE)); 159 160 PetscCallCeed(ceed, CeedOperatorCompositeAddSub(*op_rhs, op_rhs_volume)); 161 162 PetscCallCeed(ceed, CeedVectorDestroy(&q_data)); 163 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd)); 164 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_diff_flux_volume)); 165 PetscCallCeed(ceed, CeedBasisDestroy(&basis_diff_flux)); 166 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_q)); 167 PetscCallCeed(ceed, CeedBasisDestroy(&basis_q)); 168 PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs_volume)); 169 PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs_volume)); 170 } 171 172 { // Add the boundary integral CeedOperator 173 CeedQFunction qf_rhs_boundary; 174 DMLabel face_sets_label; 175 PetscInt num_face_set_values, *face_set_values; 176 CeedInt q_data_size; 177 178 // -- Build RHS operator 179 switch (honee->phys->state_var) { 180 case STATEVAR_PRIMITIVE: 181 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DivDiffusiveFluxBoundaryRHS_NS_Prim, DivDiffusiveFluxBoundaryRHS_NS_Prim_loc, 182 &qf_rhs_boundary)); 183 break; 184 case STATEVAR_CONSERVATIVE: 185 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DivDiffusiveFluxBoundaryRHS_NS_Conserv, DivDiffusiveFluxBoundaryRHS_NS_Conserv_loc, 186 &qf_rhs_boundary)); 187 break; 188 case STATEVAR_ENTROPY: 189 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DivDiffusiveFluxBoundaryRHS_NS_Entropy, DivDiffusiveFluxBoundaryRHS_NS_Entropy_loc, 190 &qf_rhs_boundary)); 191 break; 192 } 193 194 PetscCall(QDataBoundaryGradientGetNumComponents(honee->dm, &q_data_size)); 195 PetscCallCeed(ceed, CeedQFunctionSetContext(qf_rhs_boundary, newtonian_qfctx)); 196 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_boundary, "q", num_comp_q, CEED_EVAL_INTERP)); 197 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_boundary, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD)); 198 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_boundary, "qdata", q_data_size, CEED_EVAL_NONE)); 199 PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_rhs_boundary, "diffusive flux RHS", projection->num_comp, CEED_EVAL_INTERP)); 200 201 PetscCall(DMGetLabel(projection->dm, "Face Sets", &face_sets_label)); 202 PetscCall(DMLabelCreateGlobalValueArray(projection->dm, face_sets_label, &num_face_set_values, &face_set_values)); 203 for (PetscInt f = 0; f < num_face_set_values; f++) { 204 DMLabel face_orientation_label; 205 PetscInt num_orientations_values, *orientation_values; 206 207 { 208 char *face_orientation_label_name; 209 210 PetscCall(DMPlexCreateFaceLabel(projection->dm, face_set_values[f], &face_orientation_label_name)); 211 PetscCall(DMGetLabel(projection->dm, face_orientation_label_name, &face_orientation_label)); 212 PetscCall(PetscFree(face_orientation_label_name)); 213 } 214 PetscCall(DMLabelCreateGlobalValueArray(projection->dm, face_orientation_label, &num_orientations_values, &orientation_values)); 215 for (PetscInt o = 0; o < num_orientations_values; o++) { 216 CeedOperator op_rhs_boundary; 217 CeedBasis basis_q, basis_diff_flux_boundary; 218 CeedElemRestriction elem_restr_qdata, elem_restr_q, elem_restr_diff_flux_boundary; 219 CeedVector q_data; 220 CeedInt q_data_size; 221 PetscInt orientation = orientation_values[o], dm_field_q = 0, height_cell = 0, height_face = 1; 222 223 PetscCall(DMPlexCeedElemRestrictionCreate(ceed, honee->dm, face_orientation_label, orientation, height_cell, dm_field_q, &elem_restr_q)); 224 PetscCall(DMPlexCeedBasisCellToFaceCreate(ceed, honee->dm, face_orientation_label, orientation, orientation, dm_field_q, &basis_q)); 225 PetscCall(DMPlexCeedElemRestrictionCreate(ceed, projection->dm, face_orientation_label, orientation, height_face, 0, 226 &elem_restr_diff_flux_boundary)); 227 PetscCall(DMPlexCeedBasisCreate(ceed, projection->dm, face_orientation_label, orientation, height_face, 0, &basis_diff_flux_boundary)); 228 PetscCall(QDataBoundaryGradientGet(ceed, honee->dm, face_orientation_label, orientation, &elem_restr_qdata, &q_data, &q_data_size)); 229 230 PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_rhs_boundary, NULL, NULL, &op_rhs_boundary)); 231 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_boundary, "q", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 232 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_boundary, "Grad_q", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 233 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_boundary, "qdata", elem_restr_qdata, CEED_BASIS_NONE, q_data)); 234 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_boundary, "diffusive flux RHS", elem_restr_diff_flux_boundary, basis_diff_flux_boundary, 235 CEED_VECTOR_ACTIVE)); 236 237 PetscCallCeed(ceed, CeedOperatorCompositeAddSub(*op_rhs, op_rhs_boundary)); 238 239 PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs_boundary)); 240 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qdata)); 241 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_q)); 242 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_diff_flux_boundary)); 243 PetscCallCeed(ceed, CeedBasisDestroy(&basis_q)); 244 PetscCallCeed(ceed, CeedBasisDestroy(&basis_diff_flux_boundary)); 245 PetscCallCeed(ceed, CeedVectorDestroy(&q_data)); 246 } 247 PetscCall(PetscFree(orientation_values)); 248 } 249 PetscCall(PetscFree(face_set_values)); 250 PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs_boundary)); 251 } 252 253 PetscCallCeed(ceed, CeedQFunctionContextDestroy(&newtonian_qfctx)); 254 PetscFunctionReturn(PETSC_SUCCESS); 255 } 256 257 /** 258 @brief Create RHS CeedOperator for indirect projection of divergence of diffusive flux 259 260 @param[in] honee `Honee` context 261 @param[in] diff_flux_proj `DivDiffFluxProjectionData` object 262 @param[out] op_rhs Operator to calculate the RHS of the L^2 projection 263 **/ 264 static PetscErrorCode DivDiffFluxProjectionCreateRHS_Indirect_NS(Honee honee, DivDiffFluxProjectionData diff_flux_proj, CeedOperator *op_rhs) { 265 Ceed ceed = honee->ceed; 266 NodalProjectionData projection = diff_flux_proj->projection; 267 CeedBasis basis_diff_flux, basis_q; 268 CeedElemRestriction elem_restr_diff_flux, elem_restr_qd, elem_restr_q; 269 CeedVector q_data; 270 CeedInt q_data_size; 271 PetscInt dim, num_comp_q; 272 PetscInt height = 0, dm_field = 0; 273 CeedQFunction qf_rhs; 274 CeedQFunctionContext newtonian_qfctx = NULL; 275 276 PetscFunctionBeginUser; 277 PetscCall(DMGetDimension(projection->dm, &dim)); 278 PetscCall(DMGetFieldNumComps(honee->dm, 0, &num_comp_q)); 279 280 { // Get newtonian QF context 281 CeedOperator *sub_ops, main_op = honee->op_ifunction ? honee->op_ifunction : honee->op_rhs_ctx->op; 282 PetscInt sub_op_index = 0; // will be 0 for the volume op 283 284 PetscCallCeed(ceed, CeedOperatorCompositeGetSubList(main_op, &sub_ops)); 285 PetscCallCeed(ceed, CeedOperatorGetContext(sub_ops[sub_op_index], &newtonian_qfctx)); 286 } 287 PetscCall(DMPlexCeedElemRestrictionCreate(ceed, honee->dm, DMLABEL_DEFAULT, DMLABEL_DEFAULT_VALUE, 0, 0, &elem_restr_q)); 288 PetscCall(DMPlexCeedBasisCreate(ceed, honee->dm, DMLABEL_DEFAULT, DMLABEL_DEFAULT_VALUE, 0, 0, &basis_q)); 289 PetscCall(DMPlexCeedElemRestrictionCreate(ceed, projection->dm, DMLABEL_DEFAULT, DMLABEL_DEFAULT_VALUE, height, dm_field, &elem_restr_diff_flux)); 290 PetscCall(DMPlexCeedBasisCreate(ceed, projection->dm, DMLABEL_DEFAULT, DMLABEL_DEFAULT_VALUE, height, dm_field, &basis_diff_flux)); 291 PetscCall(QDataGet(ceed, projection->dm, DMLABEL_DEFAULT, DMLABEL_DEFAULT_VALUE, &elem_restr_qd, &q_data, &q_data_size)); 292 293 switch (honee->phys->state_var) { 294 case STATEVAR_PRIMITIVE: 295 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DiffusiveFluxRHS_NS_Prim, DiffusiveFluxRHS_NS_Prim_loc, &qf_rhs)); 296 break; 297 case STATEVAR_CONSERVATIVE: 298 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DiffusiveFluxRHS_NS_Conserv, DiffusiveFluxRHS_NS_Conserv_loc, &qf_rhs)); 299 break; 300 case STATEVAR_ENTROPY: 301 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DiffusiveFluxRHS_NS_Entropy, DiffusiveFluxRHS_NS_Entropy_loc, &qf_rhs)); 302 break; 303 } 304 305 PetscCallCeed(ceed, CeedQFunctionSetContext(qf_rhs, newtonian_qfctx)); 306 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs, "q", num_comp_q, CEED_EVAL_INTERP)); 307 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD)); 308 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs, "qdata", q_data_size, CEED_EVAL_NONE)); 309 PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_rhs, "F_diff RHS", projection->num_comp, CEED_EVAL_INTERP)); 310 311 PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_rhs, NULL, NULL, op_rhs)); 312 PetscCallCeed(ceed, CeedOperatorSetField(*op_rhs, "q", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 313 PetscCallCeed(ceed, CeedOperatorSetField(*op_rhs, "Grad_q", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 314 PetscCallCeed(ceed, CeedOperatorSetField(*op_rhs, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data)); 315 PetscCallCeed(ceed, CeedOperatorSetField(*op_rhs, "F_diff RHS", elem_restr_diff_flux, basis_diff_flux, CEED_VECTOR_ACTIVE)); 316 317 PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs)); 318 PetscCallCeed(ceed, CeedQFunctionContextDestroy(&newtonian_qfctx)); 319 PetscCallCeed(ceed, CeedBasisDestroy(&basis_diff_flux)); 320 PetscCallCeed(ceed, CeedVectorDestroy(&q_data)); 321 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd)); 322 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_diff_flux)); 323 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_q)); 324 PetscCallCeed(ceed, CeedBasisDestroy(&basis_q)); 325 PetscFunctionReturn(PETSC_SUCCESS); 326 } 327 328 static PetscErrorCode BoundaryIntegralBCSetup_CreateIFunctionQF(BCDefinition bc_def, CeedQFunction *qf) { 329 HoneeBCStruct honee_bc; 330 331 PetscFunctionBeginUser; 332 PetscCall(BCDefinitionGetContext(bc_def, &honee_bc)); 333 Honee honee = honee_bc->honee; 334 335 switch (honee->phys->state_var) { 336 case STATEVAR_CONSERVATIVE: 337 PetscCall(HoneeBCCreateIFunctionQF(bc_def, BoundaryIntegral_Conserv, BoundaryIntegral_Conserv_loc, honee_bc->qfctx, qf)); 338 break; 339 case STATEVAR_PRIMITIVE: 340 PetscCall(HoneeBCCreateIFunctionQF(bc_def, BoundaryIntegral_Prim, BoundaryIntegral_Prim_loc, honee_bc->qfctx, qf)); 341 break; 342 case STATEVAR_ENTROPY: 343 PetscCall(HoneeBCCreateIFunctionQF(bc_def, BoundaryIntegral_Entropy, BoundaryIntegral_Entropy_loc, honee_bc->qfctx, qf)); 344 break; 345 } 346 PetscFunctionReturn(PETSC_SUCCESS); 347 } 348 349 static PetscErrorCode BoundaryIntegralBCSetup_CreateIJacobianQF(BCDefinition bc_def, CeedQFunction *qf) { 350 HoneeBCStruct honee_bc; 351 352 PetscFunctionBeginUser; 353 PetscCall(BCDefinitionGetContext(bc_def, &honee_bc)); 354 Honee honee = honee_bc->honee; 355 356 switch (honee->phys->state_var) { 357 case STATEVAR_CONSERVATIVE: 358 PetscCall(HoneeBCCreateIJacobianQF(bc_def, BoundaryIntegral_Jacobian_Conserv, BoundaryIntegral_Jacobian_Conserv_loc, honee_bc->qfctx, qf)); 359 break; 360 case STATEVAR_PRIMITIVE: 361 PetscCall(HoneeBCCreateIJacobianQF(bc_def, BoundaryIntegral_Jacobian_Prim, BoundaryIntegral_Jacobian_Prim_loc, honee_bc->qfctx, qf)); 362 break; 363 case STATEVAR_ENTROPY: 364 PetscCall(HoneeBCCreateIJacobianQF(bc_def, BoundaryIntegral_Jacobian_Entropy, BoundaryIntegral_Jacobian_Entropy_loc, honee_bc->qfctx, qf)); 365 break; 366 } 367 PetscFunctionReturn(PETSC_SUCCESS); 368 } 369 370 PetscErrorCode NS_NEWTONIAN_IG(ProblemData problem, DM dm, void *ctx) { 371 SetupContext setup_context; 372 Honee honee = *(Honee *)ctx; 373 CeedInt degree = honee->app_ctx->degree; 374 StabilizationType stab; 375 StateVariable state_var; 376 MPI_Comm comm = honee->comm; 377 Ceed ceed = honee->ceed; 378 PetscBool implicit; 379 PetscBool unit_tests; 380 NewtonianIdealGasContext newtonian_ig_ctx; 381 382 PetscFunctionBeginUser; 383 // Option Defaults 384 const CeedScalar Cv_func[3] = {36, 60, 128}; 385 StatePrimitive reference = {.pressure = 1.01e5, .velocity = {0}, .temperature = 288.15}; 386 CeedScalar idl_decay_time = -1; 387 PetscCall(PetscNew(&newtonian_ig_ctx)); 388 *newtonian_ig_ctx = (struct NewtonianIdealGasContext_){ 389 .gas = 390 { 391 .cv = 717., 392 .cp = 1004., 393 .lambda = -2. / 3., 394 .mu = 1.8e-5, 395 .k = 0.02638, 396 }, 397 .tau_coeffs = 398 { 399 .Ctau_t = 1.0, 400 .Ctau_v = Cv_func[(CeedInt)Min(3, degree) - 1], 401 .Ctau_C = 0.25 / degree, 402 .Ctau_M = 0.25 / degree, 403 .Ctau_E = 0.125, 404 }, 405 .g = {0, 0, 0}, // m/s^2 406 .idl_start = 0, 407 .idl_length = 0, 408 .idl_pressure = reference.pressure, 409 .idl_enable = PETSC_FALSE, 410 }; 411 412 PetscOptionsBegin(comm, NULL, "Options for Newtonian Ideal Gas based problem", NULL); 413 PetscCall(PetscOptionsEnum("-state_var", "State variables used", NULL, StateVariables, (PetscEnum)(state_var = STATEVAR_CONSERVATIVE), 414 (PetscEnum *)&state_var, NULL)); 415 416 // Newtonian fluid properties 417 PetscCall(PetscOptionsScalar("-cv", "Heat capacity at constant volume", NULL, newtonian_ig_ctx->gas.cv, &newtonian_ig_ctx->gas.cv, NULL)); 418 PetscCall(PetscOptionsScalar("-cp", "Heat capacity at constant pressure", NULL, newtonian_ig_ctx->gas.cp, &newtonian_ig_ctx->gas.cp, NULL)); 419 PetscCall(PetscOptionsScalar("-lambda", "Stokes hypothesis second viscosity coefficient", NULL, newtonian_ig_ctx->gas.lambda, 420 &newtonian_ig_ctx->gas.lambda, NULL)); 421 PetscCall(PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient", NULL, newtonian_ig_ctx->gas.mu, &newtonian_ig_ctx->gas.mu, NULL)); 422 PetscCall(PetscOptionsScalar("-k", "Thermal conductivity", NULL, newtonian_ig_ctx->gas.k, &newtonian_ig_ctx->gas.k, NULL)); 423 424 PetscInt dim = 3; 425 PetscBool given_option = PETSC_FALSE; 426 PetscCall(PetscOptionsDeprecated("-g", "-gravity", "libCEED 0.11.1", NULL)); 427 PetscCall(PetscOptionsRealArray("-gravity", "Gravitational acceleration vector", NULL, newtonian_ig_ctx->g, &dim, &given_option)); 428 if (given_option) PetscCheck(dim == 3, comm, PETSC_ERR_ARG_SIZ, "Gravity vector must be size 3, %" PetscInt_FMT " values given", dim); 429 430 // Stabilization parameters 431 PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL)); 432 PetscCall(PetscOptionsScalar("-Ctau_t", "Stabilization time constant", NULL, newtonian_ig_ctx->tau_coeffs.Ctau_t, 433 &newtonian_ig_ctx->tau_coeffs.Ctau_t, NULL)); 434 PetscCall(PetscOptionsScalar("-Ctau_v", "Stabilization viscous constant", NULL, newtonian_ig_ctx->tau_coeffs.Ctau_v, 435 &newtonian_ig_ctx->tau_coeffs.Ctau_v, NULL)); 436 PetscCall(PetscOptionsScalar("-Ctau_C", "Stabilization continuity constant", NULL, newtonian_ig_ctx->tau_coeffs.Ctau_C, 437 &newtonian_ig_ctx->tau_coeffs.Ctau_C, NULL)); 438 PetscCall(PetscOptionsScalar("-Ctau_M", "Stabilization momentum constant", NULL, newtonian_ig_ctx->tau_coeffs.Ctau_M, 439 &newtonian_ig_ctx->tau_coeffs.Ctau_M, NULL)); 440 PetscCall(PetscOptionsScalar("-Ctau_E", "Stabilization energy constant", NULL, newtonian_ig_ctx->tau_coeffs.Ctau_E, 441 &newtonian_ig_ctx->tau_coeffs.Ctau_E, NULL)); 442 443 dim = 3; 444 PetscCall(PetscOptionsScalar("-reference_pressure", "Reference/initial pressure", NULL, reference.pressure, &reference.pressure, NULL)); 445 PetscCall(PetscOptionsScalarArray("-reference_velocity", "Reference/initial velocity", NULL, reference.velocity, &dim, NULL)); 446 PetscCall(PetscOptionsScalar("-reference_temperature", "Reference/initial temperature", NULL, reference.temperature, &reference.temperature, NULL)); 447 448 PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL)); 449 PetscCall(PetscOptionsBool("-newtonian_unit_tests", "Run Newtonian unit tests", NULL, unit_tests = PETSC_FALSE, &unit_tests, NULL)); 450 PetscCheck(!(state_var == STATEVAR_PRIMITIVE && !implicit), comm, PETSC_ERR_SUP, 451 "RHSFunction is not provided for primitive variables (use -state_var primitive only with -implicit)\n"); 452 453 // IDL Settings 454 { 455 PetscBool idl_enable = (PetscBool)newtonian_ig_ctx->idl_enable; // Need PetscBool variable to read in from PetscOptionsScalar() 456 PetscCall(PetscOptionsScalar("-idl_decay_time", "Characteristic timescale of the pressure deviance decay. The timestep is good starting point", 457 NULL, idl_decay_time, &idl_decay_time, &idl_enable)); 458 PetscCheck(!(idl_enable && idl_decay_time == 0), comm, PETSC_ERR_SUP, "idl_decay_time may not be equal to zero."); 459 if (idl_decay_time < 0) idl_enable = PETSC_FALSE; 460 newtonian_ig_ctx->idl_enable = idl_enable; 461 462 PetscCall(PetscOptionsScalar("-idl_start", "Start of IDL in the x direction", NULL, newtonian_ig_ctx->idl_start, &newtonian_ig_ctx->idl_start, 463 NULL)); 464 PetscCall(PetscOptionsScalar("-idl_length", "Length of IDL in the positive x direction", NULL, newtonian_ig_ctx->idl_length, 465 &newtonian_ig_ctx->idl_length, NULL)); 466 newtonian_ig_ctx->idl_pressure = reference.pressure; 467 PetscCall(PetscOptionsScalar("-idl_pressure", "Pressure IDL uses as reference (default is `-reference_pressure`)", NULL, 468 newtonian_ig_ctx->idl_pressure, &newtonian_ig_ctx->idl_pressure, NULL)); 469 } 470 PetscOptionsEnd(); 471 472 // ------------------------------------------------------ 473 // Set up the QFunction context 474 // ------------------------------------------------------ 475 // -- Scale variables to desired units 476 Units units = honee->units; 477 newtonian_ig_ctx->gas.cv *= units->J_per_kg_K; 478 newtonian_ig_ctx->gas.cp *= units->J_per_kg_K; 479 newtonian_ig_ctx->gas.mu *= units->Pascal * units->second; 480 newtonian_ig_ctx->gas.k *= units->W_per_m_K; 481 for (PetscInt i = 0; i < 3; i++) newtonian_ig_ctx->g[i] *= units->m_per_squared_s; 482 reference.pressure *= units->Pascal; 483 for (PetscInt i = 0; i < 3; i++) reference.velocity[i] *= units->meter / units->second; 484 reference.temperature *= units->Kelvin; 485 486 PetscReal domain_min[3], domain_max[3], domain_size[3]; 487 PetscCall(DMGetBoundingBox(dm, domain_min, domain_max)); 488 for (PetscInt i = 0; i < 3; i++) domain_size[i] = (domain_max[i] - domain_min[i]); 489 490 // -- Solver Settings 491 honee->phys->implicit = implicit; 492 honee->phys->state_var = state_var; 493 494 // -- QFunction Context 495 newtonian_ig_ctx->stabilization = stab; 496 newtonian_ig_ctx->is_implicit = implicit; 497 newtonian_ig_ctx->state_var = state_var; 498 newtonian_ig_ctx->idl_amplitude = 1 / (idl_decay_time * units->second); 499 newtonian_ig_ctx->divFdiff_method = honee->app_ctx->divFdiffproj_method; 500 501 // -- Setup Context 502 PetscCall(PetscNew(&setup_context)); 503 *setup_context = (struct SetupContext_){ 504 .reference = reference, 505 .newt_ctx = *newtonian_ig_ctx, 506 .lx = domain_size[0], 507 .ly = domain_size[1], 508 .lz = domain_size[2], 509 .time = 0, 510 }; 511 512 CeedQFunctionContext ics_qfctx, newtonian_ig_qfctx; 513 514 PetscCallCeed(ceed, CeedQFunctionContextCreate(honee->ceed, &ics_qfctx)); 515 PetscCallCeed(ceed, CeedQFunctionContextSetData(ics_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*setup_context), setup_context)); 516 PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(ics_qfctx, CEED_MEM_HOST, FreeContextPetsc)); 517 PetscCallCeed(ceed, 518 CeedQFunctionContextRegisterDouble(ics_qfctx, "evaluation time", offsetof(struct SetupContext_, time), 1, "Time of evaluation")); 519 520 PetscCallCeed(ceed, CeedQFunctionContextCreate(honee->ceed, &newtonian_ig_qfctx)); 521 PetscCallCeed(ceed, CeedQFunctionContextSetData(newtonian_ig_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*newtonian_ig_ctx), newtonian_ig_ctx)); 522 PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(newtonian_ig_qfctx, CEED_MEM_HOST, FreeContextPetsc)); 523 PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(newtonian_ig_qfctx, "timestep size", offsetof(struct NewtonianIdealGasContext_, dt), 1, 524 "Size of timestep, delta t")); 525 PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(newtonian_ig_qfctx, "ijacobian time shift", 526 offsetof(struct NewtonianIdealGasContext_, ijacobian_time_shift), 1, 527 "Shift for mass matrix in IJacobian")); 528 PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(newtonian_ig_qfctx, "solution time", offsetof(struct NewtonianIdealGasContext_, time), 1, 529 "Current solution time")); 530 531 // Set problem information 532 problem->num_comps_jac_data = 14; 533 if (newtonian_ig_ctx->idl_enable) problem->num_comps_jac_data += 1; 534 problem->compute_exact_solution_error = PETSC_FALSE; 535 problem->print_info = PRINT_NEWTONIAN; 536 problem->num_components = 5; 537 PetscCall(PetscMalloc1(problem->num_components, &problem->component_names)); 538 static const char *const conserv_component_names[] = {"Density", "MomentumX", "MomentumY", "MomentumZ", "TotalEnergy"}; 539 static const char *const prim_component_names[] = {"Pressure", "VelocityX", "VelocityY", "VelocityZ", "Temperature"}; 540 static const char *const entropy_component_names[] = {"EntropyDensity", "EntropyMomentumX", "EntropyMomentumY", "EntropyMomentumZ", 541 "EntropyTotalEnergy"}; 542 543 switch (state_var) { 544 case STATEVAR_CONSERVATIVE: 545 problem->ics = (HoneeQFSpec){.qf_func_ptr = ICsNewtonianIG_Conserv, .qf_loc = ICsNewtonianIG_Conserv_loc}; 546 problem->apply_vol_rhs = (HoneeQFSpec){.qf_func_ptr = RHSFunction_Newtonian, .qf_loc = RHSFunction_Newtonian_loc}; 547 problem->apply_vol_ifunction = (HoneeQFSpec){.qf_func_ptr = IFunction_Newtonian_Conserv, .qf_loc = IFunction_Newtonian_Conserv_loc}; 548 problem->apply_vol_ijacobian = (HoneeQFSpec){.qf_func_ptr = IJacobian_Newtonian_Conserv, .qf_loc = IJacobian_Newtonian_Conserv_loc}; 549 for (PetscInt i = 0; i < 5; i++) PetscCall(PetscStrallocpy(conserv_component_names[i], &problem->component_names[i])); 550 break; 551 case STATEVAR_PRIMITIVE: 552 problem->ics = (HoneeQFSpec){.qf_func_ptr = ICsNewtonianIG_Prim, .qf_loc = ICsNewtonianIG_Prim_loc}; 553 problem->apply_vol_ifunction = (HoneeQFSpec){.qf_func_ptr = IFunction_Newtonian_Prim, .qf_loc = IFunction_Newtonian_Prim_loc}; 554 problem->apply_vol_ijacobian = (HoneeQFSpec){.qf_func_ptr = IJacobian_Newtonian_Prim, .qf_loc = IJacobian_Newtonian_Prim_loc}; 555 for (PetscInt i = 0; i < 5; i++) PetscCall(PetscStrallocpy(prim_component_names[i], &problem->component_names[i])); 556 break; 557 case STATEVAR_ENTROPY: 558 problem->ics = (HoneeQFSpec){.qf_func_ptr = ICsNewtonianIG_Entropy, .qf_loc = ICsNewtonianIG_Entropy_loc}; 559 problem->apply_vol_ifunction = (HoneeQFSpec){.qf_func_ptr = IFunction_Newtonian_Entropy, .qf_loc = IFunction_Newtonian_Entropy_loc}; 560 problem->apply_vol_ijacobian = (HoneeQFSpec){.qf_func_ptr = IJacobian_Newtonian_Entropy, .qf_loc = IJacobian_Newtonian_Entropy_loc}; 561 for (PetscInt i = 0; i < 5; i++) PetscCall(PetscStrallocpy(entropy_component_names[i], &problem->component_names[i])); 562 break; 563 } 564 // All QFunctions get the same QFunctionContext regardless of state variable 565 problem->ics.qfctx = ics_qfctx; 566 problem->apply_vol_rhs.qfctx = newtonian_ig_qfctx; 567 PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(newtonian_ig_qfctx, &problem->apply_vol_ifunction.qfctx)); 568 PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(newtonian_ig_qfctx, &problem->apply_vol_ijacobian.qfctx)); 569 570 if (stab == STAB_SUPG && !implicit) problem->create_mass_operator = CreateKSPMassOperator_NewtonianStabilized; 571 572 PetscCall(DivDiffFluxProjectionCreate(honee, honee->app_ctx->divFdiffproj_method, 4, &honee->diff_flux_proj)); 573 if (honee->diff_flux_proj) { 574 DivDiffFluxProjectionData diff_flux_proj = honee->diff_flux_proj; 575 NodalProjectionData projection = diff_flux_proj->projection; 576 PetscSection section; 577 578 diff_flux_proj->CreateRHSOperator_Direct = DivDiffFluxProjectionCreateRHS_Direct_NS; 579 diff_flux_proj->CreateRHSOperator_Indirect = DivDiffFluxProjectionCreateRHS_Indirect_NS; 580 PetscCall(DMGetLocalSection(projection->dm, §ion)); 581 switch (honee->diff_flux_proj->method) { 582 case DIV_DIFF_FLUX_PROJ_DIRECT: { 583 PetscCall(PetscSectionSetFieldName(section, 0, "")); 584 PetscCall(PetscSectionSetComponentName(section, 0, 0, "DivDiffusiveFlux_MomentumX")); 585 PetscCall(PetscSectionSetComponentName(section, 0, 1, "DivDiffusiveFlux_MomentumY")); 586 PetscCall(PetscSectionSetComponentName(section, 0, 2, "DivDiffusiveFlux_MomentumZ")); 587 PetscCall(PetscSectionSetComponentName(section, 0, 3, "DivDiffusiveFlux_Energy")); 588 } break; 589 case DIV_DIFF_FLUX_PROJ_INDIRECT: { 590 PetscCall(PetscSectionSetFieldName(section, 0, "")); 591 PetscCall(PetscSectionSetComponentName(section, 0, 0, "DiffusiveFlux_MomentumXX")); 592 PetscCall(PetscSectionSetComponentName(section, 0, 1, "DiffusiveFlux_MomentumXY")); 593 PetscCall(PetscSectionSetComponentName(section, 0, 2, "DiffusiveFlux_MomentumXZ")); 594 PetscCall(PetscSectionSetComponentName(section, 0, 3, "DiffusiveFlux_MomentumYX")); 595 PetscCall(PetscSectionSetComponentName(section, 0, 4, "DiffusiveFlux_MomentumYY")); 596 PetscCall(PetscSectionSetComponentName(section, 0, 5, "DiffusiveFlux_MomentumYZ")); 597 PetscCall(PetscSectionSetComponentName(section, 0, 6, "DiffusiveFlux_MomentumZX")); 598 PetscCall(PetscSectionSetComponentName(section, 0, 7, "DiffusiveFlux_MomentumZY")); 599 PetscCall(PetscSectionSetComponentName(section, 0, 8, "DiffusiveFlux_MomentumZZ")); 600 PetscCall(PetscSectionSetComponentName(section, 0, 9, "DiffusiveFlux_EnergyX")); 601 PetscCall(PetscSectionSetComponentName(section, 0, 10, "DiffusiveFlux_EnergyY")); 602 PetscCall(PetscSectionSetComponentName(section, 0, 11, "DiffusiveFlux_EnergyZ")); 603 } break; 604 case DIV_DIFF_FLUX_PROJ_NONE: 605 SETERRQ(PetscObjectComm((PetscObject)honee->dm), PETSC_ERR_ARG_WRONG, "Should not reach here with div_diff_flux_projection_method %s", 606 DivDiffFluxProjectionMethods[honee->app_ctx->divFdiffproj_method]); 607 break; 608 } 609 } 610 611 for (PetscCount b = 0; b < problem->num_bc_defs; b++) { 612 BCDefinition bc_def = problem->bc_defs[b]; 613 const char *name; 614 615 PetscCall(BCDefinitionGetInfo(bc_def, &name, NULL, NULL)); 616 if (!strcmp(name, "slip")) { 617 PetscCall(SlipBCSetup(bc_def, problem, dm, ctx, newtonian_ig_qfctx)); 618 } else if (!strcmp(name, "freestream")) { 619 PetscCall(FreestreamBCSetup(bc_def, problem, dm, ctx, newtonian_ig_ctx, &reference)); 620 } else if (!strcmp(name, "outflow")) { 621 PetscCall(OutflowBCSetup(bc_def, problem, dm, ctx, newtonian_ig_ctx, &reference)); 622 } else if (!strcmp(name, "inflow")) { 623 HoneeBCStruct honee_bc; 624 625 PetscCall(PetscNew(&honee_bc)); 626 PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(newtonian_ig_qfctx, &honee_bc->qfctx)); 627 honee_bc->honee = honee; 628 honee_bc->num_comps_jac_data = honee->phys->implicit ? 11 : 0; 629 PetscCall(BCDefinitionSetContext(bc_def, HoneeBCDestroy, honee_bc)); 630 631 PetscCall(BCDefinitionSetIFunction(bc_def, BoundaryIntegralBCSetup_CreateIFunctionQF, HoneeBCAddIFunctionOp)); 632 PetscCall(BCDefinitionSetIJacobian(bc_def, BoundaryIntegralBCSetup_CreateIJacobianQF, HoneeBCAddIJacobianOp)); 633 } 634 } 635 636 if (unit_tests) PetscCall(UnitTests_Newtonian(honee, newtonian_ig_ctx->gas)); 637 PetscFunctionReturn(PETSC_SUCCESS); 638 } 639 640 //------------------------------------ 641 // Unit test functions 642 //------------------------------------ 643 644 static PetscErrorCode CheckQWithTolerance(const CeedScalar Q_s[5], const CeedScalar Q_a[5], const CeedScalar Q_b[5], const char *name, 645 PetscReal rtol_0, PetscReal rtol_u, PetscReal rtol_4) { 646 CeedScalar relative_error[5]; // relative error 647 CeedScalar divisor_threshold = 10 * CEED_EPSILON; 648 649 PetscFunctionBeginUser; 650 relative_error[0] = (Q_a[0] - Q_b[0]) / (fabs(Q_s[0]) > divisor_threshold ? Q_s[0] : 1); 651 relative_error[4] = (Q_a[4] - Q_b[4]) / (fabs(Q_s[4]) > divisor_threshold ? Q_s[4] : 1); 652 653 CeedScalar u_magnitude = sqrt(Square(Q_s[1]) + Square(Q_s[2]) + Square(Q_s[3])); 654 CeedScalar u_divisor = u_magnitude > divisor_threshold ? u_magnitude : 1; 655 for (int i = 1; i < 4; i++) { 656 relative_error[i] = (Q_a[i] - Q_b[i]) / u_divisor; 657 } 658 659 if (fabs(relative_error[0]) >= rtol_0) { 660 printf("%s[0] error %g (expected %.10e, got %.10e)\n", name, relative_error[0], Q_s[0], Q_a[0]); 661 } 662 for (int i = 1; i < 4; i++) { 663 if (fabs(relative_error[i]) >= rtol_u) { 664 printf("%s[%d] error %g (expected %.10e, got %.10e)\n", name, i, relative_error[i], Q_s[i], Q_a[i]); 665 } 666 } 667 if (fabs(relative_error[4]) >= rtol_4) { 668 printf("%s[4] error %g (expected %.10e, got %.10e)\n", name, relative_error[4], Q_s[4], Q_a[4]); 669 } 670 PetscFunctionReturn(PETSC_SUCCESS); 671 } 672 673 // @brief Verify `StateFromQ` by converting A0 -> B0 -> A0_test, where A0 should equal A0_test 674 static PetscErrorCode TestState(StateVariable state_var_A, StateVariable state_var_B, NewtonianIGProperties gas, const CeedScalar A0[5], 675 CeedScalar rtol_0, CeedScalar rtol_u, CeedScalar rtol_4) { 676 CeedScalar B0[5], A0_test[5]; 677 char buf[128]; 678 const char *const StateVariables_Initial[] = {"U", "Y", "V"}; 679 680 PetscFunctionBeginUser; 681 const char *A_initial = StateVariables_Initial[state_var_A]; 682 const char *B_initial = StateVariables_Initial[state_var_B]; 683 684 State state_A0 = StateFromQ(gas, A0, state_var_A); 685 StateToQ(gas, state_A0, B0, state_var_B); 686 State state_B0 = StateFromQ(gas, B0, state_var_B); 687 StateToQ(gas, state_B0, A0_test, state_var_A); 688 689 snprintf(buf, sizeof buf, "%s->%s->%s: %s", A_initial, B_initial, A_initial, A_initial); 690 PetscCall(CheckQWithTolerance(A0, A0_test, A0, buf, rtol_0, rtol_u, rtol_4)); 691 PetscFunctionReturn(PETSC_SUCCESS); 692 } 693 694 // @brief Verify `StateFromQ_fwd` via a finite difference approximation 695 static PetscErrorCode TestState_fwd(StateVariable state_var_A, StateVariable state_var_B, NewtonianIGProperties gas, const CeedScalar A0[5], 696 CeedScalar rtol_0, CeedScalar rtol_u, CeedScalar rtol_4) { 697 CeedScalar eps = 4e-7; // Finite difference step 698 char buf[128]; 699 const char *const StateVariables_Initial[] = {"U", "Y", "V"}; 700 701 PetscFunctionBeginUser; 702 const char *A_initial = StateVariables_Initial[state_var_A]; 703 const char *B_initial = StateVariables_Initial[state_var_B]; 704 State state_0 = StateFromQ(gas, A0, state_var_A); 705 706 for (int i = 0; i < 5; i++) { 707 CeedScalar dB[5] = {0.}, dB_fd[5] = {0.}; 708 { // Calculate dB using State functions 709 CeedScalar dA[5] = {0}; 710 711 dA[i] = A0[i]; 712 State dstate_0 = StateFromQ_fwd(gas, state_0, dA, state_var_A); 713 StateToQ_fwd(gas, state_0, dstate_0, dB, state_var_B); 714 } 715 716 { // Calculate dB_fd via finite difference approximation 717 CeedScalar A1[5], B0[5], B1[5]; 718 719 for (int j = 0; j < 5; j++) A1[j] = (1 + eps * (i == j)) * A0[j]; 720 State state_1 = StateFromQ(gas, A1, state_var_A); 721 StateToQ(gas, state_0, B0, state_var_B); 722 StateToQ(gas, state_1, B1, state_var_B); 723 for (int j = 0; j < 5; j++) dB_fd[j] = (B1[j] - B0[j]) / eps; 724 } 725 726 snprintf(buf, sizeof buf, "d%s->d%s: StateFrom%s_fwd i=%d: d%s", A_initial, B_initial, A_initial, i, B_initial); 727 PetscCall(CheckQWithTolerance(dB_fd, dB, dB_fd, buf, rtol_0, rtol_u, rtol_4)); 728 } 729 PetscFunctionReturn(PETSC_SUCCESS); 730 } 731 732 // @brief Test the Newtonian State transformation functions, `StateFrom*` 733 static PetscErrorCode UnitTests_Newtonian(Honee honee, NewtonianIGProperties gas) { 734 Units units = honee->units; 735 const CeedScalar kg = units->kilogram, m = units->meter, sec = units->second, K = units->Kelvin; 736 CeedScalar rtol; 737 738 PetscFunctionBeginUser; 739 const CeedScalar T = 200 * K; 740 const CeedScalar rho = 1.2 * kg / Cube(m); 741 const CeedScalar P = (HeatCapacityRatio(gas) - 1) * rho * gas.cv * T; 742 const CeedScalar u_base = 40 * m / sec; 743 const CeedScalar u[3] = {u_base, u_base * 1.1, u_base * 1.2}; 744 const CeedScalar e_kinetic = 0.5 * Dot3(u, u); 745 const CeedScalar e_internal = gas.cv * T; 746 const CeedScalar e_total = e_kinetic + e_internal; 747 const CeedScalar gamma = HeatCapacityRatio(gas); 748 const CeedScalar entropy = log(P) - gamma * log(rho); 749 const CeedScalar rho_div_p = rho / P; 750 const CeedScalar Y0[5] = {P, u[0], u[1], u[2], T}; 751 const CeedScalar U0[5] = {rho, rho * u[0], rho * u[1], rho * u[2], rho * e_total}; 752 const CeedScalar V0[5] = {(gamma - entropy) / (gamma - 1) - rho_div_p * (e_kinetic), rho_div_p * u[0], rho_div_p * u[1], rho_div_p * u[2], 753 -rho_div_p}; 754 755 rtol = 20 * CEED_EPSILON; 756 PetscCall(TestState(STATEVAR_PRIMITIVE, STATEVAR_CONSERVATIVE, gas, Y0, rtol, rtol, rtol)); 757 PetscCall(TestState(STATEVAR_PRIMITIVE, STATEVAR_ENTROPY, gas, Y0, rtol, rtol, rtol)); 758 PetscCall(TestState(STATEVAR_CONSERVATIVE, STATEVAR_PRIMITIVE, gas, U0, rtol, rtol, rtol)); 759 PetscCall(TestState(STATEVAR_CONSERVATIVE, STATEVAR_ENTROPY, gas, U0, rtol, rtol, rtol)); 760 PetscCall(TestState(STATEVAR_ENTROPY, STATEVAR_CONSERVATIVE, gas, V0, rtol, rtol, rtol)); 761 PetscCall(TestState(STATEVAR_ENTROPY, STATEVAR_PRIMITIVE, gas, V0, rtol, rtol, rtol)); 762 763 rtol = 5e-6; 764 PetscCall(TestState_fwd(STATEVAR_PRIMITIVE, STATEVAR_CONSERVATIVE, gas, Y0, rtol, rtol, rtol)); 765 PetscCall(TestState_fwd(STATEVAR_PRIMITIVE, STATEVAR_ENTROPY, gas, Y0, rtol, rtol, rtol)); 766 PetscCall(TestState_fwd(STATEVAR_CONSERVATIVE, STATEVAR_PRIMITIVE, gas, U0, rtol, rtol, rtol)); 767 PetscCall(TestState_fwd(STATEVAR_CONSERVATIVE, STATEVAR_ENTROPY, gas, U0, 10 * rtol, rtol, rtol)); 768 PetscCall(TestState_fwd(STATEVAR_ENTROPY, STATEVAR_CONSERVATIVE, gas, V0, 5 * rtol, rtol, rtol)); 769 PetscCall(TestState_fwd(STATEVAR_ENTROPY, STATEVAR_PRIMITIVE, gas, V0, 5 * rtol, 5 * rtol, 5 * rtol)); 770 PetscFunctionReturn(PETSC_SUCCESS); 771 } 772