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 ADVECTION 6 7 #include "../qfunctions/advection.h" 8 9 #include <ceed.h> 10 #include <petscdm.h> 11 12 #include <navierstokes.h> 13 14 // @brief Create CeedOperator for stabilized mass KSP for explicit timestepping 15 // 16 // Only used for SUPG stabilization 17 PetscErrorCode CreateKSPMassOperator_AdvectionStabilized(User user, CeedOperator *op_mass) { 18 Ceed ceed = user->ceed; 19 CeedInt num_comp_q, q_data_size; 20 CeedQFunction qf_mass = NULL; 21 CeedElemRestriction elem_restr_q, elem_restr_qd_i; 22 CeedBasis basis_q; 23 CeedVector q_data; 24 CeedQFunctionContext qfctx = NULL; 25 PetscInt dim; 26 27 PetscFunctionBeginUser; 28 PetscCall(DMGetDimension(user->dm, &dim)); 29 { // Get restriction and basis from the RHS function 30 CeedOperator *sub_ops; 31 CeedOperatorField field; 32 PetscInt sub_op_index = 0; // will be 0 for the volume op 33 34 PetscCallCeed(ceed, CeedCompositeOperatorGetSubList(user->op_rhs_ctx->op, &sub_ops)); 35 PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "q", &field)); 36 PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(field, &elem_restr_q)); 37 PetscCallCeed(ceed, CeedOperatorFieldGetBasis(field, &basis_q)); 38 39 PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "qdata", &field)); 40 PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(field, &elem_restr_qd_i)); 41 PetscCallCeed(ceed, CeedOperatorFieldGetVector(field, &q_data)); 42 43 PetscCallCeed(ceed, CeedOperatorGetContext(sub_ops[sub_op_index], &qfctx)); 44 } 45 46 PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_q, &num_comp_q)); 47 PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_qd_i, &q_data_size)); 48 49 switch (dim) { 50 case 2: 51 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, MassFunction_Advection2D, MassFunction_Advection2D_loc, &qf_mass)); 52 break; 53 case 3: 54 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, MassFunction_Advection, MassFunction_Advection_loc, &qf_mass)); 55 break; 56 } 57 58 PetscCallCeed(ceed, CeedQFunctionSetContext(qf_mass, qfctx)); 59 PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(qf_mass, 0)); 60 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_mass, "q_dot", 5, CEED_EVAL_INTERP)); 61 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_mass, "q", 5, CEED_EVAL_INTERP)); 62 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_mass, "qdata", q_data_size, CEED_EVAL_NONE)); 63 PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_mass, "v", 5, CEED_EVAL_INTERP)); 64 PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_mass, "Grad_v", 5 * dim, CEED_EVAL_GRAD)); 65 66 PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, op_mass)); 67 PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "q_dot", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 68 PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "q", elem_restr_q, basis_q, user->q_ceed)); 69 PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "qdata", elem_restr_qd_i, CEED_BASIS_NONE, q_data)); 70 PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "v", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 71 PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "Grad_v", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 72 73 PetscCallCeed(ceed, CeedQFunctionContextDestroy(&qfctx)); 74 PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass)); 75 PetscFunctionReturn(PETSC_SUCCESS); 76 } 77 78 /** 79 @brief Create RHS CeedOperator for direct projection of divergence of diffusive flux 80 81 @param[in] user `User` object 82 @param[in] ceed_data `CeedData` object 83 @param[in] diff_flux_proj `DivDiffFluxProjectionData` object 84 @param[out] op_rhs Operator to calculate the RHS of the L^2 projection 85 **/ 86 static PetscErrorCode DivDiffFluxProjectionCreateRHS_Direct_AdvDif(User user, CeedData ceed_data, DivDiffFluxProjectionData diff_flux_proj, 87 CeedOperator *op_rhs) { 88 Ceed ceed = user->ceed; 89 NodalProjectionData projection = diff_flux_proj->projection; 90 CeedInt num_comp_q; 91 PetscInt dim, label_value = 0; 92 DMLabel domain_label = NULL; 93 CeedQFunctionContext advection_qfctx = NULL; 94 95 PetscFunctionBeginUser; 96 // -- Get Pre-requisite things 97 PetscCall(DMGetDimension(projection->dm, &dim)); 98 PetscCallCeed(ceed, CeedBasisGetNumComponents(ceed_data->basis_q, &num_comp_q)); 99 100 { // Get advection-diffusion QF context 101 CeedOperator *sub_ops; 102 PetscInt sub_op_index = 0; // will be 0 for the volume op 103 104 if (user->op_ifunction) PetscCallCeed(ceed, CeedCompositeOperatorGetSubList(user->op_ifunction, &sub_ops)); 105 else PetscCallCeed(ceed, CeedCompositeOperatorGetSubList(user->op_rhs_ctx->op, &sub_ops)); 106 PetscCallCeed(ceed, CeedOperatorGetContext(sub_ops[sub_op_index], &advection_qfctx)); 107 } 108 PetscCallCeed(ceed, CeedCompositeOperatorCreate(ceed, op_rhs)); 109 { // Add the volume integral CeedOperator 110 CeedQFunction qf_rhs_volume = NULL; 111 CeedOperator op_rhs_volume; 112 CeedVector q_data; 113 CeedElemRestriction elem_restr_qd, elem_restr_diff_flux_volume = NULL; 114 CeedBasis basis_diff_flux = NULL; 115 CeedInt q_data_size; 116 117 PetscCall(DivDiffFluxProjectionGetOperatorFieldData(diff_flux_proj, &elem_restr_diff_flux_volume, &basis_diff_flux, NULL, NULL)); 118 PetscCall(QDataGet(ceed, projection->dm, domain_label, label_value, ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord, 119 &elem_restr_qd, &q_data, &q_data_size)); 120 switch (dim) { 121 case 2: 122 PetscCallCeed( 123 ceed, CeedQFunctionCreateInterior(ceed, 1, DivDiffusiveFluxVolumeRHS_AdvDif_2D, DivDiffusiveFluxVolumeRHS_AdvDif_2D_loc, &qf_rhs_volume)); 124 break; 125 case 3: 126 PetscCallCeed( 127 ceed, CeedQFunctionCreateInterior(ceed, 1, DivDiffusiveFluxVolumeRHS_AdvDif_3D, DivDiffusiveFluxVolumeRHS_AdvDif_3D_loc, &qf_rhs_volume)); 128 break; 129 } 130 PetscCheck(qf_rhs_volume, user->comm, PETSC_ERR_SUP, "%s not valid for DM of dimension %" PetscInt_FMT, __func__, dim); 131 132 PetscCallCeed(ceed, CeedQFunctionSetContext(qf_rhs_volume, advection_qfctx)); 133 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_volume, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD)); 134 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_volume, "qdata", q_data_size, CEED_EVAL_NONE)); 135 PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_rhs_volume, "diffusive flux RHS", projection->num_comp * dim, CEED_EVAL_GRAD)); 136 137 PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_rhs_volume, NULL, NULL, &op_rhs_volume)); 138 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_volume, "Grad_q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE)); 139 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_volume, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data)); 140 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_volume, "diffusive flux RHS", elem_restr_diff_flux_volume, basis_diff_flux, CEED_VECTOR_ACTIVE)); 141 142 PetscCallCeed(ceed, CeedCompositeOperatorAddSub(*op_rhs, op_rhs_volume)); 143 144 PetscCallCeed(ceed, CeedVectorDestroy(&q_data)); 145 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd)); 146 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_diff_flux_volume)); 147 PetscCallCeed(ceed, CeedBasisDestroy(&basis_diff_flux)); 148 PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs_volume)); 149 PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs_volume)); 150 } 151 152 { // Add the boundary integral CeedOperator 153 CeedQFunction qf_rhs_boundary; 154 DMLabel face_sets_label; 155 PetscInt num_face_set_values, *face_set_values; 156 CeedInt q_data_size; 157 158 // -- Build RHS operator 159 switch (dim) { 160 case 2: 161 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DivDiffusiveFluxBoundaryRHS_AdvDif_2D, DivDiffusiveFluxBoundaryRHS_AdvDif_2D_loc, 162 &qf_rhs_boundary)); 163 break; 164 case 3: 165 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DivDiffusiveFluxBoundaryRHS_AdvDif_3D, DivDiffusiveFluxBoundaryRHS_AdvDif_3D_loc, 166 &qf_rhs_boundary)); 167 break; 168 } 169 170 PetscCall(QDataBoundaryGradientGetNumComponents(user->dm, &q_data_size)); 171 PetscCallCeed(ceed, CeedQFunctionSetContext(qf_rhs_boundary, advection_qfctx)); 172 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_boundary, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD)); 173 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_boundary, "qdata", q_data_size, CEED_EVAL_NONE)); 174 PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_rhs_boundary, "diffusive flux RHS", projection->num_comp, CEED_EVAL_INTERP)); 175 176 PetscCall(DMGetLabel(projection->dm, "Face Sets", &face_sets_label)); 177 PetscCall(DMLabelCreateGlobalValueArray(projection->dm, face_sets_label, &num_face_set_values, &face_set_values)); 178 for (PetscInt f = 0; f < num_face_set_values; f++) { 179 DMLabel face_orientation_label; 180 PetscInt num_orientations_values, *orientation_values; 181 182 { 183 char *face_orientation_label_name; 184 185 PetscCall(DMPlexCreateFaceLabel(projection->dm, face_set_values[f], &face_orientation_label_name)); 186 PetscCall(DMGetLabel(projection->dm, face_orientation_label_name, &face_orientation_label)); 187 PetscCall(PetscFree(face_orientation_label_name)); 188 } 189 PetscCall(DMLabelCreateGlobalValueArray(projection->dm, face_orientation_label, &num_orientations_values, &orientation_values)); 190 for (PetscInt o = 0; o < num_orientations_values; o++) { 191 CeedOperator op_rhs_boundary; 192 CeedBasis basis_q, basis_diff_flux_boundary; 193 CeedElemRestriction elem_restr_qdata, elem_restr_q, elem_restr_diff_flux_boundary; 194 CeedVector q_data; 195 CeedInt q_data_size; 196 PetscInt orientation = orientation_values[o], dm_field_q = 0, height_cell = 0, height_face = 1; 197 198 PetscCall(DMPlexCeedElemRestrictionCreate(ceed, user->dm, face_orientation_label, orientation, height_cell, dm_field_q, &elem_restr_q)); 199 PetscCall(DMPlexCeedBasisCellToFaceCreate(ceed, user->dm, face_orientation_label, orientation, orientation, dm_field_q, &basis_q)); 200 PetscCall(DMPlexCeedElemRestrictionCreate(ceed, projection->dm, face_orientation_label, orientation, height_face, 0, 201 &elem_restr_diff_flux_boundary)); 202 PetscCall(CreateBasisFromPlex(ceed, projection->dm, face_orientation_label, orientation, height_face, 0, &basis_diff_flux_boundary)); 203 PetscCall(QDataBoundaryGradientGet(ceed, user->dm, face_orientation_label, orientation, ceed_data->x_coord, &elem_restr_qdata, &q_data, 204 &q_data_size)); 205 206 PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_rhs_boundary, NULL, NULL, &op_rhs_boundary)); 207 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_boundary, "Grad_q", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 208 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_boundary, "qdata", elem_restr_qdata, CEED_BASIS_NONE, q_data)); 209 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_boundary, "diffusive flux RHS", elem_restr_diff_flux_boundary, basis_diff_flux_boundary, 210 CEED_VECTOR_ACTIVE)); 211 212 PetscCallCeed(ceed, CeedCompositeOperatorAddSub(*op_rhs, op_rhs_boundary)); 213 214 PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs_boundary)); 215 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qdata)); 216 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_q)); 217 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_diff_flux_boundary)); 218 PetscCallCeed(ceed, CeedBasisDestroy(&basis_q)); 219 PetscCallCeed(ceed, CeedBasisDestroy(&basis_diff_flux_boundary)); 220 PetscCallCeed(ceed, CeedVectorDestroy(&q_data)); 221 } 222 PetscCall(PetscFree(orientation_values)); 223 } 224 PetscCall(PetscFree(face_set_values)); 225 PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs_boundary)); 226 } 227 228 PetscCallCeed(ceed, CeedQFunctionContextDestroy(&advection_qfctx)); 229 PetscFunctionReturn(PETSC_SUCCESS); 230 } 231 232 /** 233 @brief Create RHS CeedOperator for indirect projection of divergence of diffusive flux 234 235 @param[in] user `User` object 236 @param[in] ceed_data `CeedData` object 237 @param[in] diff_flux_proj `DivDiffFluxProjectionData` object 238 @param[out] op_rhs Operator to calculate the RHS of the L^2 projection 239 **/ 240 static PetscErrorCode DivDiffFluxProjectionCreateRHS_Indirect_AdvDif(User user, CeedData ceed_data, DivDiffFluxProjectionData diff_flux_proj, 241 CeedOperator *op_rhs) { 242 Ceed ceed = user->ceed; 243 NodalProjectionData projection = diff_flux_proj->projection; 244 CeedBasis basis_diff_flux; 245 CeedElemRestriction elem_restr_diff_flux, elem_restr_qd; 246 CeedVector q_data; 247 CeedInt num_comp_q, q_data_size; 248 PetscInt dim; 249 PetscInt label_value = 0, height = 0, dm_field = 0; 250 DMLabel domain_label = NULL; 251 CeedQFunction qf_rhs = NULL; 252 CeedQFunctionContext advection_qfctx = NULL; 253 254 PetscFunctionBeginUser; 255 PetscCall(DMGetDimension(projection->dm, &dim)); 256 PetscCallCeed(ceed, CeedBasisGetNumComponents(ceed_data->basis_q, &num_comp_q)); 257 258 { // Get advection-diffusion QF context 259 CeedOperator *sub_ops; 260 PetscInt sub_op_index = 0; // will be 0 for the volume op 261 262 if (user->op_ifunction) PetscCallCeed(ceed, CeedCompositeOperatorGetSubList(user->op_ifunction, &sub_ops)); 263 else PetscCallCeed(ceed, CeedCompositeOperatorGetSubList(user->op_rhs_ctx->op, &sub_ops)); 264 PetscCallCeed(ceed, CeedOperatorGetContext(sub_ops[sub_op_index], &advection_qfctx)); 265 } 266 PetscCall(DMPlexCeedElemRestrictionCreate(ceed, projection->dm, domain_label, label_value, height, dm_field, &elem_restr_diff_flux)); 267 PetscCall(CreateBasisFromPlex(ceed, projection->dm, domain_label, label_value, height, dm_field, &basis_diff_flux)); 268 PetscCall(QDataGet(ceed, projection->dm, domain_label, label_value, ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord, &elem_restr_qd, 269 &q_data, &q_data_size)); 270 271 switch (dim) { 272 case 2: 273 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DiffusiveFluxRHS_AdvDif_2D, DiffusiveFluxRHS_AdvDif_2D_loc, &qf_rhs)); 274 break; 275 case 3: 276 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DiffusiveFluxRHS_AdvDif_3D, DiffusiveFluxRHS_AdvDif_3D_loc, &qf_rhs)); 277 break; 278 } 279 PetscCheck(qf_rhs, user->comm, PETSC_ERR_SUP, "%s not valid for DM of dimension %" PetscInt_FMT, __func__, dim); 280 281 PetscCallCeed(ceed, CeedQFunctionSetContext(qf_rhs, advection_qfctx)); 282 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD)); 283 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs, "qdata", q_data_size, CEED_EVAL_NONE)); 284 PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_rhs, "F_diff RHS", projection->num_comp, CEED_EVAL_INTERP)); 285 286 PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_rhs, NULL, NULL, op_rhs)); 287 PetscCallCeed(ceed, CeedOperatorSetField(*op_rhs, "Grad_q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE)); 288 PetscCallCeed(ceed, CeedOperatorSetField(*op_rhs, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data)); 289 PetscCallCeed(ceed, CeedOperatorSetField(*op_rhs, "F_diff RHS", elem_restr_diff_flux, basis_diff_flux, CEED_VECTOR_ACTIVE)); 290 291 PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs)); 292 PetscCallCeed(ceed, CeedQFunctionContextDestroy(&advection_qfctx)); 293 PetscCallCeed(ceed, CeedBasisDestroy(&basis_diff_flux)); 294 PetscCallCeed(ceed, CeedVectorDestroy(&q_data)); 295 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd)); 296 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_diff_flux)); 297 PetscFunctionReturn(PETSC_SUCCESS); 298 } 299 300 PetscErrorCode NS_ADVECTION(ProblemData problem, DM dm, void *ctx, SimpleBC bc) { 301 AdvDifWindType wind_type; 302 AdvDifICType advectionic_type; 303 AdvDifBubbleContinuityType bubble_continuity_type = -1; 304 StabilizationType stab; 305 StabilizationTauType stab_tau; 306 SetupContextAdv setup_context; 307 User user = *(User *)ctx; 308 MPI_Comm comm = user->comm; 309 Ceed ceed = user->ceed; 310 PetscBool implicit; 311 AdvectionContext advection_ctx; 312 CeedQFunctionContext advection_qfctx; 313 PetscInt dim; 314 315 PetscFunctionBeginUser; 316 PetscCall(PetscCalloc1(1, &setup_context)); 317 PetscCall(PetscCalloc1(1, &advection_ctx)); 318 PetscCall(DMGetDimension(dm, &dim)); 319 320 // ------------------------------------------------------ 321 // SET UP ADVECTION 322 // ------------------------------------------------------ 323 problem->print_info = PRINT_ADVECTION; 324 problem->jac_data_size_vol = 0; 325 problem->jac_data_size_sur = 0; 326 switch (dim) { 327 case 2: 328 problem->ics.qf_func_ptr = ICsAdvection2d; 329 problem->ics.qf_loc = ICsAdvection2d_loc; 330 problem->apply_vol_rhs.qf_func_ptr = RHS_Advection2d; 331 problem->apply_vol_rhs.qf_loc = RHS_Advection2d_loc; 332 problem->apply_vol_ifunction.qf_func_ptr = IFunction_Advection2d; 333 problem->apply_vol_ifunction.qf_loc = IFunction_Advection2d_loc; 334 problem->apply_inflow.qf_func_ptr = Advection2d_InOutFlow; 335 problem->apply_inflow.qf_loc = Advection2d_InOutFlow_loc; 336 problem->compute_exact_solution_error = PETSC_TRUE; 337 break; 338 case 3: 339 problem->ics.qf_func_ptr = ICsAdvection; 340 problem->ics.qf_loc = ICsAdvection_loc; 341 problem->apply_vol_rhs.qf_func_ptr = RHS_Advection; 342 problem->apply_vol_rhs.qf_loc = RHS_Advection_loc; 343 problem->apply_vol_ifunction.qf_func_ptr = IFunction_Advection; 344 problem->apply_vol_ifunction.qf_loc = IFunction_Advection_loc; 345 problem->apply_inflow.qf_func_ptr = Advection_InOutFlow; 346 problem->apply_inflow.qf_loc = Advection_InOutFlow_loc; 347 problem->compute_exact_solution_error = PETSC_FALSE; 348 break; 349 } 350 351 PetscCall(DivDiffFluxProjectionCreate(user, 1, &user->diff_flux_proj)); 352 if (user->diff_flux_proj) { 353 DivDiffFluxProjectionData diff_flux_proj = user->diff_flux_proj; 354 NodalProjectionData projection = diff_flux_proj->projection; 355 356 diff_flux_proj->CreateRHSOperator_Direct = DivDiffFluxProjectionCreateRHS_Direct_AdvDif; 357 diff_flux_proj->CreateRHSOperator_Indirect = DivDiffFluxProjectionCreateRHS_Indirect_AdvDif; 358 359 switch (user->diff_flux_proj->method) { 360 case DIV_DIFF_FLUX_PROJ_DIRECT: { 361 PetscSection section; 362 363 PetscCall(DMGetLocalSection(projection->dm, §ion)); 364 PetscCall(PetscSectionSetFieldName(section, 0, "")); 365 PetscCall(PetscSectionSetComponentName(section, 0, 0, "DivDiffusiveFlux_Scalar")); 366 } break; 367 case DIV_DIFF_FLUX_PROJ_INDIRECT: { 368 PetscSection section; 369 370 PetscCall(DMGetLocalSection(projection->dm, §ion)); 371 PetscCall(PetscSectionSetFieldName(section, 0, "")); 372 PetscCall(PetscSectionSetComponentName(section, 0, 0, "DiffusiveFlux_ScalarX")); 373 PetscCall(PetscSectionSetComponentName(section, 0, 1, "DiffusiveFlux_ScalarY")); 374 if (dim >= 3) PetscCall(PetscSectionSetComponentName(section, 0, 2, "DiffusiveFlux_ScalarZ")); 375 } break; 376 case DIV_DIFF_FLUX_PROJ_NONE: 377 SETERRQ(PetscObjectComm((PetscObject)user->dm), PETSC_ERR_ARG_WRONG, "Should not reach here with div_diff_flux_projection_method %s", 378 DivDiffFluxProjectionMethods[user->app_ctx->divFdiffproj_method]); 379 break; 380 } 381 } 382 383 // ------------------------------------------------------ 384 // Create the libCEED context 385 // ------------------------------------------------------ 386 CeedScalar rc = 1000.; // m (Radius of bubble) 387 CeedScalar CtauS = 0.; // dimensionless 388 PetscBool strong_form = PETSC_FALSE; 389 CeedScalar E_wind = 1.e6; // J 390 CeedScalar Ctau_a = PetscPowScalarInt(user->app_ctx->degree, 2); 391 CeedScalar Ctau_d = PetscPowScalarInt(user->app_ctx->degree, 4); 392 CeedScalar Ctau_t = 0.; 393 PetscReal wind[3] = {1., 0, 0}; // m/s 394 CeedScalar diffusion_coeff = 0.; 395 CeedScalar wave_frequency = 2 * M_PI; 396 CeedScalar wave_phase = 0; 397 AdvDifWaveType wave_type = -1; 398 PetscReal domain_min[3], domain_max[3], domain_size[3] = {0.}; 399 PetscCall(DMGetBoundingBox(dm, domain_min, domain_max)); 400 for (PetscInt i = 0; i < dim; i++) domain_size[i] = domain_max[i] - domain_min[i]; 401 402 // ------------------------------------------------------ 403 // Create the PETSc context 404 // ------------------------------------------------------ 405 PetscScalar meter = 1e-2; // 1 meter in scaled length units 406 PetscScalar kilogram = 1e-6; // 1 kilogram in scaled mass units 407 PetscScalar second = 1e-2; // 1 second in scaled time units 408 PetscScalar Joule; 409 410 // ------------------------------------------------------ 411 // Command line Options 412 // ------------------------------------------------------ 413 PetscOptionsBegin(comm, NULL, "Options for ADVECTION problem", NULL); 414 // -- Physics 415 PetscCall(PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble", NULL, rc, &rc, NULL)); 416 PetscBool translation; 417 PetscCall(PetscOptionsEnum("-wind_type", "Wind type in Advection", NULL, AdvDifWindTypes, (PetscEnum)(wind_type = ADVDIF_WIND_ROTATION), 418 (PetscEnum *)&wind_type, &translation)); 419 PetscInt n = dim; 420 PetscBool user_wind; 421 PetscCall(PetscOptionsRealArray("-wind_translation", "Constant wind vector", NULL, wind, &n, &user_wind)); 422 PetscCall(PetscOptionsScalar("-diffusion_coeff", "Diffusion coefficient", NULL, diffusion_coeff, &diffusion_coeff, NULL)); 423 PetscCall(PetscOptionsScalar("-CtauS", "Scale coefficient for tau (nondimensional)", NULL, CtauS, &CtauS, NULL)); 424 PetscCall(PetscOptionsBool("-strong_form", "Strong (true) or weak/integrated by parts (false) advection residual", NULL, strong_form, &strong_form, 425 NULL)); 426 PetscCall(PetscOptionsScalar("-E_wind", "Total energy of inflow wind", NULL, E_wind, &E_wind, NULL)); 427 PetscCall(PetscOptionsEnum("-advection_ic_type", "Initial condition for Advection problem", NULL, AdvDifICTypes, 428 (PetscEnum)(advectionic_type = ADVDIF_IC_BUBBLE_SPHERE), (PetscEnum *)&advectionic_type, NULL)); 429 PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL)); 430 PetscCall(PetscOptionsEnum("-stab_tau", "Stabilization constant, tau", NULL, StabilizationTauTypes, (PetscEnum)(stab_tau = STAB_TAU_CTAU), 431 (PetscEnum *)&stab_tau, NULL)); 432 PetscCall(PetscOptionsScalar("-Ctau_t", "Stabilization time constant", NULL, Ctau_t, &Ctau_t, NULL)); 433 PetscCall(PetscOptionsScalar("-Ctau_a", "Coefficient for the stabilization, advection component", NULL, Ctau_a, &Ctau_a, NULL)); 434 PetscCall(PetscOptionsScalar("-Ctau_d", "Coefficient for the stabilization, diffusion component", NULL, Ctau_d, &Ctau_d, NULL)); 435 PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL)); 436 437 if (advectionic_type == ADVDIF_IC_WAVE) { 438 PetscCall(PetscOptionsEnum("-wave_type", "Type of wave", NULL, AdvDifWaveTypes, (PetscEnum)(wave_type = ADVDIF_WAVE_SINE), 439 (PetscEnum *)&wave_type, NULL)); 440 PetscCall(PetscOptionsScalar("-wave_frequency", "Frequency of sine wave", NULL, wave_frequency, &wave_frequency, NULL)); 441 PetscCall(PetscOptionsScalar("-wave_phase", "Length correction", NULL, wave_phase, &wave_phase, NULL)); 442 } 443 444 if (advectionic_type == ADVDIF_IC_BUBBLE_CYLINDER || advectionic_type == ADVDIF_IC_BUBBLE_SPHERE) { 445 bubble_continuity_type = dim == 3 ? ADVDIF_BUBBLE_CONTINUITY_SMOOTH : ADVDIF_BUBBLE_CONTINUITY_COSINE; 446 PetscCall(PetscOptionsEnum("-bubble_continuity", "Smooth, back_sharp, or thick", NULL, AdvDifBubbleContinuityTypes, 447 (PetscEnum)bubble_continuity_type, (PetscEnum *)&bubble_continuity_type, NULL)); 448 } 449 450 // -- Units 451 PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL)); 452 meter = fabs(meter); 453 PetscCall(PetscOptionsScalar("-units_kilogram", "1 kilogram in scaled mass units", NULL, kilogram, &kilogram, NULL)); 454 kilogram = fabs(kilogram); 455 PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL)); 456 second = fabs(second); 457 458 // -- Warnings 459 if (wind_type == ADVDIF_WIND_ROTATION && user_wind) { 460 PetscCall(PetscPrintf(comm, "Warning! Use -wind_translation only with -wind_type translation\n")); 461 } 462 if (wind_type == ADVDIF_WIND_TRANSLATION && advectionic_type == ADVDIF_IC_BUBBLE_CYLINDER && wind[2] != 0.) { 463 wind[2] = 0; 464 PetscCall( 465 PetscPrintf(comm, "Warning! Background wind in the z direction should be zero (-wind_translation x,x,0) with -advection_ic_type cylinder\n")); 466 } 467 if (stab == STAB_NONE && CtauS != 0) { 468 PetscCall(PetscPrintf(comm, "Warning! Use -CtauS only with -stab su or -stab supg\n")); 469 } 470 PetscOptionsEnd(); 471 472 if (stab == STAB_SUPG) problem->create_mass_operator = CreateKSPMassOperator_AdvectionStabilized; 473 474 // ------------------------------------------------------ 475 // Set up the PETSc context 476 // ------------------------------------------------------ 477 // -- Define derived units 478 Joule = kilogram * PetscSqr(meter) / PetscSqr(second); 479 480 user->units->meter = meter; 481 user->units->kilogram = kilogram; 482 user->units->second = second; 483 user->units->Joule = Joule; 484 485 // ------------------------------------------------------ 486 // Set up the libCEED context 487 // ------------------------------------------------------ 488 // -- Scale variables to desired units 489 E_wind *= Joule; 490 rc = fabs(rc) * meter; 491 for (PetscInt i = 0; i < dim; i++) { 492 wind[i] *= (meter / second); 493 domain_size[i] *= meter; 494 } 495 496 // -- Setup Context 497 setup_context->rc = rc; 498 setup_context->lx = domain_size[0]; 499 setup_context->ly = domain_size[1]; 500 setup_context->lz = dim == 3 ? domain_size[2] : 0.; 501 setup_context->wind[0] = wind[0]; 502 setup_context->wind[1] = wind[1]; 503 setup_context->wind[2] = dim == 3 ? wind[2] : 0.; 504 setup_context->wind_type = wind_type; 505 setup_context->initial_condition_type = advectionic_type; 506 setup_context->bubble_continuity_type = bubble_continuity_type; 507 setup_context->time = 0; 508 setup_context->wave_frequency = wave_frequency; 509 setup_context->wave_phase = wave_phase; 510 setup_context->wave_type = wave_type; 511 512 // -- QFunction Context 513 user->phys->implicit = implicit; 514 advection_ctx->CtauS = CtauS; 515 advection_ctx->E_wind = E_wind; 516 advection_ctx->implicit = implicit; 517 advection_ctx->strong_form = strong_form; 518 advection_ctx->stabilization = stab; 519 advection_ctx->stabilization_tau = stab_tau; 520 advection_ctx->Ctau_a = Ctau_a; 521 advection_ctx->Ctau_d = Ctau_d; 522 advection_ctx->Ctau_t = Ctau_t; 523 advection_ctx->diffusion_coeff = diffusion_coeff; 524 advection_ctx->divFdiff_method = user->app_ctx->divFdiffproj_method; 525 526 PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &problem->ics.qfctx)); 527 PetscCallCeed(ceed, CeedQFunctionContextSetData(problem->ics.qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*setup_context), setup_context)); 528 PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(problem->ics.qfctx, CEED_MEM_HOST, FreeContextPetsc)); 529 530 PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &advection_qfctx)); 531 PetscCallCeed(ceed, CeedQFunctionContextSetData(advection_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*advection_ctx), advection_ctx)); 532 PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(advection_qfctx, CEED_MEM_HOST, FreeContextPetsc)); 533 PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(advection_qfctx, "timestep size", offsetof(struct AdvectionContext_, dt), 1, 534 "Size of timestep, delta t")); 535 problem->apply_vol_rhs.qfctx = advection_qfctx; 536 PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(advection_qfctx, &problem->apply_vol_ifunction.qfctx)); 537 PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(advection_qfctx, &problem->apply_inflow.qfctx)); 538 PetscFunctionReturn(PETSC_SUCCESS); 539 } 540 541 PetscErrorCode PRINT_ADVECTION(User user, ProblemData problem, AppCtx app_ctx) { 542 MPI_Comm comm = user->comm; 543 Ceed ceed = user->ceed; 544 SetupContextAdv setup_ctx; 545 AdvectionContext advection_ctx; 546 PetscInt dim; 547 548 PetscFunctionBeginUser; 549 PetscCall(DMGetDimension(user->dm, &dim)); 550 PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->ics.qfctx, CEED_MEM_HOST, &setup_ctx)); 551 PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->apply_vol_rhs.qfctx, CEED_MEM_HOST, &advection_ctx)); 552 PetscCall(PetscPrintf(comm, 553 " Problem:\n" 554 " Problem Name : %s\n" 555 " Stabilization : %s\n" 556 " Stabilization Tau : %s\n" 557 " Wind Type : %s\n", 558 app_ctx->problem_name, StabilizationTypes[advection_ctx->stabilization], 559 StabilizationTauTypes[advection_ctx->stabilization_tau], AdvDifWindTypes[setup_ctx->wind_type])); 560 561 if (setup_ctx->wind_type == ADVDIF_WIND_TRANSLATION) { 562 CeedScalar *wind = setup_ctx->wind; 563 switch (dim) { 564 case 2: 565 PetscCall(PetscPrintf(comm, " Background Wind : %f,%f\n", wind[0], wind[1])); 566 break; 567 case 3: 568 PetscCall(PetscPrintf(comm, " Background Wind : %f,%f,%f\n", wind[0], wind[1], wind[2])); 569 break; 570 } 571 } 572 573 PetscCall(PetscPrintf(comm, " Initial Condition Type : %s\n", AdvDifICTypes[setup_ctx->initial_condition_type])); 574 switch (setup_ctx->initial_condition_type) { 575 case ADVDIF_IC_SKEW: 576 case ADVDIF_IC_COSINE_HILL: 577 break; 578 case ADVDIF_IC_BUBBLE_SPHERE: 579 case ADVDIF_IC_BUBBLE_CYLINDER: 580 PetscCall(PetscPrintf(comm, " Bubble Continuity : %s\n", AdvDifBubbleContinuityTypes[setup_ctx->bubble_continuity_type])); 581 break; 582 case ADVDIF_IC_WAVE: 583 PetscCall(PetscPrintf(comm, " Wave Type : %s\n", AdvDifWaveTypes[setup_ctx->wave_type])); 584 break; 585 } 586 587 PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->ics.qfctx, &setup_ctx)); 588 PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfctx, &advection_ctx)); 589 PetscFunctionReturn(PETSC_SUCCESS); 590 } 591