1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors. 2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause 3 4 #include <navierstokes.h> 5 #include "petscerror.h" 6 7 /** 8 @brief Add `BCDefinition` to a `PetscSegBuffer` 9 10 @param[in] bc_def `BCDefinition` to add 11 @param[in,out] bc_defs_seg `PetscSegBuffer` to add to 12 **/ 13 static PetscErrorCode AddBCDefinitionToSegBuffer(BCDefinition bc_def, PetscSegBuffer bc_defs_seg) { 14 BCDefinition *bc_def_ptr; 15 16 PetscFunctionBeginUser; 17 if (bc_def == NULL) PetscFunctionReturn(PETSC_SUCCESS); 18 PetscCall(PetscSegBufferGet(bc_defs_seg, 1, &bc_def_ptr)); 19 *bc_def_ptr = bc_def; 20 PetscFunctionReturn(PETSC_SUCCESS); 21 } 22 23 /** 24 @brief Create and setup `BCDefinition`s and `SimpleBC` from commandline options 25 26 @param[in] honee `Honee` 27 @param[in,out] problem `ProblemData` 28 @param[in] app_ctx `AppCtx` 29 @param[in,out] bc `SimpleBC` 30 **/ 31 PetscErrorCode BoundaryConditionSetUp(Honee honee, ProblemData problem, AppCtx app_ctx, SimpleBC bc) { 32 PetscSegBuffer bc_defs_seg; 33 PetscBool flg; 34 BCDefinition bc_def; 35 36 PetscFunctionBeginUser; 37 PetscCall(PetscSegBufferCreate(sizeof(BCDefinition), 4, &bc_defs_seg)); 38 39 PetscOptionsBegin(honee->comm, NULL, "Boundary Condition Options", NULL); 40 41 PetscCall(PetscOptionsBCDefinition("-bc_wall", "Face IDs to apply wall BC", NULL, "wall", &bc_def, NULL)); 42 PetscCall(AddBCDefinitionToSegBuffer(bc_def, bc_defs_seg)); 43 if (bc_def) { 44 PetscInt num_essential_comps = 16, essential_comps[16]; 45 46 PetscCall(PetscOptionsIntArray("-wall_comps", "An array of constrained component numbers", NULL, essential_comps, &num_essential_comps, &flg)); 47 PetscCall(BCDefinitionSetEssential(bc_def, num_essential_comps, essential_comps)); 48 49 app_ctx->wall_forces.num_wall = bc_def->num_label_values; 50 PetscCall(PetscMalloc1(bc_def->num_label_values, &app_ctx->wall_forces.walls)); 51 PetscCall(PetscArraycpy(app_ctx->wall_forces.walls, bc_def->label_values, bc_def->num_label_values)); 52 } 53 54 { // Symmetry Boundary Conditions 55 const char *deprecated[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"}; 56 const char *flags[3] = {"-bc_symmetry_x", "-bc_symmetry_y", "-bc_symmetry_z"}; 57 58 for (PetscInt j = 0; j < 3; j++) { 59 PetscCall(PetscOptionsDeprecated(deprecated[j], flags[j], "libCEED 0.12.0", 60 "Use -bc_symmetry_[x,y,z] for direct equivalency, or -bc_slip for weak, Riemann-based, direction-invariant " 61 "slip/no-penatration boundary conditions")); 62 PetscCall(PetscOptionsBCDefinition(flags[j], "Face IDs to apply symmetry BC", NULL, "symmetry", &bc_def, NULL)); 63 if (!bc_def) { 64 PetscCall(PetscOptionsBCDefinition(deprecated[j], "Face IDs to apply symmetry BC", NULL, "symmetry", &bc_def, NULL)); 65 } 66 PetscCall(AddBCDefinitionToSegBuffer(bc_def, bc_defs_seg)); 67 if (bc_def) { 68 PetscInt essential_comps[1] = {j + 1}; 69 70 PetscCall(BCDefinitionSetEssential(bc_def, 1, essential_comps)); 71 } 72 } 73 } 74 75 // Inflow BCs 76 bc->num_inflow = 16; 77 PetscCall(PetscOptionsIntArray("-bc_inflow", "Face IDs to apply inflow BC", NULL, bc->inflows, &bc->num_inflow, NULL)); 78 79 PetscCall(PetscOptionsBCDefinition("-bc_outflow", "Face IDs to apply outflow BC", NULL, "outflow", &bc_def, NULL)); 80 PetscCall(AddBCDefinitionToSegBuffer(bc_def, bc_defs_seg)); 81 82 PetscCall(PetscOptionsBCDefinition("-bc_freestream", "Face IDs to apply freestream BC", NULL, "freestream", &bc_def, NULL)); 83 PetscCall(AddBCDefinitionToSegBuffer(bc_def, bc_defs_seg)); 84 85 PetscCall(PetscOptionsBCDefinition("-bc_slip", "Face IDs to apply slip BC", NULL, "slip", &bc_def, NULL)); 86 PetscCall(AddBCDefinitionToSegBuffer(bc_def, bc_defs_seg)); 87 88 PetscOptionsEnd(); 89 90 PetscCall(PetscSegBufferGetSize(bc_defs_seg, &problem->num_bc_defs)); 91 PetscCall(PetscSegBufferExtractAlloc(bc_defs_seg, &problem->bc_defs)); 92 PetscCall(PetscSegBufferDestroy(&bc_defs_seg)); 93 94 //TODO: Verify that the BCDefinition don't have overlapping claims to boundary faces 95 96 PetscFunctionReturn(PETSC_SUCCESS); 97 } 98 99 /** 100 @brief Destroy `HoneeBCStruct` object 101 102 @param[in] ctx Pointer to `HoneeBCStruct` 103 **/ 104 PetscErrorCode HoneeBCDestroy(void **ctx) { 105 HoneeBCStruct honee_bc = *(HoneeBCStruct *)ctx; 106 Ceed ceed = honee_bc->honee->ceed; 107 108 PetscFunctionBeginUser; 109 if (honee_bc->qfctx) PetscCallCeed(ceed, CeedQFunctionContextDestroy(&honee_bc->qfctx)); 110 if (honee_bc->DestroyCtx) PetscCall((*(honee_bc->DestroyCtx))(&honee_bc->ctx)); 111 PetscCall(PetscFree(honee_bc)); 112 *ctx = NULL; 113 PetscFunctionReturn(PETSC_SUCCESS); 114 } 115 116 /** 117 @brief Create a QFunction matching the "standard" HONEE inputs for IFunctions 118 119 This assumes the `bc_def` context is a `HoneeBCStruct` and inputs/outputs of the IFunction QFunction are in the correct order. 120 121 @param[in] bc_def `BCDefinition` that hosts the IFunction 122 @param[in] qf_func_ptr `CeedQFunctionUser` for the IFunction 123 @param[in] qf_loc Absolute path to source of `CeedQFunctionUser` 124 @param[in] qfctx `CeedQFunctionContext` for the IFunction (also shared with the IJacobian, if applicable) 125 @param[out] qf_ifunc QFunction for the IFunction 126 **/ 127 PetscErrorCode HoneeBCCreateIFunctionQF(BCDefinition bc_def, CeedQFunctionUser qf_func_ptr, const char *qf_loc, CeedQFunctionContext qfctx, 128 CeedQFunction *qf_ifunc) { 129 Ceed ceed; 130 DM dm; 131 PetscInt dim, dim_sur, height = 1, num_comp_x, num_comp_q; 132 CeedInt q_data_size_sur, jac_data_size_sur; 133 HoneeBCStruct honee_bc; 134 135 PetscFunctionBeginUser; 136 PetscCall(BCDefinitionGetDM(bc_def, &dm)); 137 PetscCall(BCDefinitionGetContext(bc_def, &honee_bc)); 138 ceed = honee_bc->honee->ceed; 139 jac_data_size_sur = honee_bc->jac_data_size_sur; 140 141 PetscCall(DMGetDimension(dm, &dim)); 142 dim_sur = dim - height; 143 PetscCall(QDataBoundaryGetNumComponents(dm, &q_data_size_sur)); 144 PetscCall(DMGetCoordinateNumComps(dm, &num_comp_x)); 145 { 146 PetscSection section; 147 148 PetscCall(DMGetLocalSection(dm, §ion)); 149 PetscCall(PetscSectionGetFieldComponents(section, bc_def->dm_field, &num_comp_q)); 150 } 151 152 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, qf_func_ptr, qf_loc, qf_ifunc)); 153 PetscCallCeed(ceed, CeedQFunctionSetContext(*qf_ifunc, qfctx)); 154 PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(*qf_ifunc, 0)); 155 PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_ifunc, "q", num_comp_q, CEED_EVAL_INTERP)); 156 PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_ifunc, "Grad_q", num_comp_q * dim_sur, CEED_EVAL_GRAD)); 157 PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_ifunc, "surface qdata", q_data_size_sur, CEED_EVAL_NONE)); 158 PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_ifunc, "x", num_comp_x, CEED_EVAL_INTERP)); 159 PetscCallCeed(ceed, CeedQFunctionAddOutput(*qf_ifunc, "v", num_comp_q, CEED_EVAL_INTERP)); 160 if (jac_data_size_sur) PetscCallCeed(ceed, CeedQFunctionAddOutput(*qf_ifunc, "surface jacobian data", jac_data_size_sur, CEED_EVAL_NONE)); 161 PetscFunctionReturn(PETSC_SUCCESS); 162 } 163 164 /** 165 @brief Create a QFunction matching the "standard" HONEE inputs for IJacobian 166 167 This assumes the `bc_def` context is a `HoneeBCStruct` and inputs/outputs of the IJacobian QFunction are in the correct order. 168 169 @param[in] bc_def `BCDefinition` that hosts the IJacobian 170 @param[in] qf_func_ptr `CeedQFunctionUser` for the IJacobian 171 @param[in] qf_loc Absolute path to source of `CeedQFunctionUser` 172 @param[in] qfctx `CeedQFunctionContext` for the IJacobian (also shared with the IFunction) 173 @param[out] qf_ijac QFunction for the IJacobian 174 **/ 175 PetscErrorCode HoneeBCCreateIJacobianQF(BCDefinition bc_def, CeedQFunctionUser qf_func_ptr, const char *qf_loc, CeedQFunctionContext qfctx, 176 CeedQFunction *qf_ijac) { 177 Ceed ceed; 178 DM dm; 179 PetscInt dim, dim_sur, height = 1, num_comp_x, num_comp_q; 180 CeedInt q_data_size_sur, jac_data_size_sur; 181 HoneeBCStruct honee_bc; 182 183 PetscFunctionBeginUser; 184 PetscCall(BCDefinitionGetDM(bc_def, &dm)); 185 PetscCall(BCDefinitionGetContext(bc_def, &honee_bc)); 186 ceed = honee_bc->honee->ceed; 187 jac_data_size_sur = honee_bc->jac_data_size_sur; 188 189 PetscCall(DMGetDimension(dm, &dim)); 190 dim_sur = dim - height; 191 PetscCall(QDataBoundaryGetNumComponents(dm, &q_data_size_sur)); 192 PetscCall(DMGetCoordinateNumComps(dm, &num_comp_x)); 193 { 194 PetscSection section; 195 196 PetscCall(DMGetLocalSection(dm, §ion)); 197 PetscCall(PetscSectionGetFieldComponents(section, bc_def->dm_field, &num_comp_q)); 198 } 199 200 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, qf_func_ptr, qf_loc, qf_ijac)); 201 PetscCallCeed(ceed, CeedQFunctionSetContext(*qf_ijac, qfctx)); 202 PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(*qf_ijac, 0)); 203 PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_ijac, "dq", num_comp_q, CEED_EVAL_INTERP)); 204 PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_ijac, "Grad_dq", num_comp_q * dim_sur, CEED_EVAL_GRAD)); 205 PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_ijac, "surface qdata", q_data_size_sur, CEED_EVAL_NONE)); 206 PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_ijac, "x", num_comp_x, CEED_EVAL_INTERP)); 207 PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_ijac, "surface jacobian data", jac_data_size_sur, CEED_EVAL_NONE)); 208 PetscCallCeed(ceed, CeedQFunctionAddOutput(*qf_ijac, "v", num_comp_q, CEED_EVAL_INTERP)); 209 PetscFunctionReturn(PETSC_SUCCESS); 210 } 211 212 /** 213 @brief Setups and adds IFunction operator to given composite operator 214 215 @param[in] bc_def `BCDefinition` for the boundary condition IFunction 216 @param[in] domain_label `DMLabel` for the face orientation label 217 @param[in] label_value Orientation value 218 @param[in] qf_ifunc QFunction for the IFunction 219 @param[in,out] op_ifunc Composite operator to be added to 220 @param[out] sub_op_ifunc Non-composite operator that is added in this function. To be passed to `HoneeBCAddIJacobianOp()` 221 **/ 222 PetscErrorCode HoneeBCAddIFunctionOp(BCDefinition bc_def, DMLabel domain_label, PetscInt label_value, CeedQFunction qf_ifunc, CeedOperator op_ifunc, 223 CeedOperator *sub_op_ifunc) { 224 Honee honee; 225 Ceed ceed; 226 DM dm, dm_coord; 227 HoneeBCStruct honee_bc; 228 PetscInt dim, height = 1, num_comp_x, num_comp_q; 229 CeedInt q_data_size, jac_data_size; 230 CeedVector q_data, jac_data; 231 CeedOperator sub_op_ifunc_; 232 CeedElemRestriction elem_restr_qd, elem_restr_q, elem_restr_x, elem_restr_jac; 233 CeedBasis basis_x, basis_q; 234 235 PetscFunctionBeginUser; 236 PetscCall(BCDefinitionGetDM(bc_def, &dm)); 237 PetscCall(BCDefinitionGetContext(bc_def, &honee_bc)); 238 honee = honee_bc->honee; 239 ceed = honee_bc->honee->ceed; 240 jac_data_size = honee_bc->jac_data_size_sur; 241 242 PetscCall(DMGetDimension(dm, &dim)); 243 PetscCall(QDataBoundaryGetNumComponents(dm, &q_data_size)); 244 PetscCall(DMGetCoordinateNumComps(dm, &num_comp_x)); 245 246 { 247 PetscSection section; 248 249 PetscCall(DMGetLocalSection(dm, §ion)); 250 PetscCall(PetscSectionGetFieldComponents(section, bc_def->dm_field, &num_comp_q)); 251 } 252 253 PetscCall(DMGetCoordinateDM(dm, &dm_coord)); 254 PetscCall(CreateBasisFromPlex(ceed, dm, domain_label, label_value, height, bc_def->dm_field, &basis_q)); 255 PetscCall(CreateBasisFromPlex(ceed, dm_coord, domain_label, label_value, height, bc_def->dm_field, &basis_x)); 256 PetscCall(DMPlexCeedElemRestrictionCreate(ceed, dm, domain_label, label_value, height, bc_def->dm_field, &elem_restr_q)); 257 PetscCall(DMPlexCeedElemRestrictionCoordinateCreate(ceed, dm, domain_label, label_value, height, &elem_restr_x)); 258 if (jac_data_size > 0) { 259 PetscCall(DMPlexCeedElemRestrictionQDataCreate(ceed, dm, domain_label, label_value, height, jac_data_size, &elem_restr_jac)); 260 PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_jac, &jac_data, NULL)); 261 } 262 263 PetscCall(QDataBoundaryGet(ceed, dm, domain_label, label_value, elem_restr_x, basis_x, honee->x_coord, &elem_restr_qd, &q_data, &q_data_size)); 264 265 PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_ifunc, NULL, NULL, &sub_op_ifunc_)); 266 PetscCallCeed(ceed, CeedOperatorSetField(sub_op_ifunc_, "q", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 267 PetscCallCeed(ceed, CeedOperatorSetField(sub_op_ifunc_, "Grad_q", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 268 PetscCallCeed(ceed, CeedOperatorSetField(sub_op_ifunc_, "surface qdata", elem_restr_qd, CEED_BASIS_NONE, q_data)); 269 PetscCallCeed(ceed, CeedOperatorSetField(sub_op_ifunc_, "x", elem_restr_x, basis_x, honee->x_coord)); 270 PetscCallCeed(ceed, CeedOperatorSetField(sub_op_ifunc_, "v", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 271 if (jac_data_size > 0) PetscCallCeed(ceed, CeedOperatorSetField(sub_op_ifunc_, "surface jacobian data", elem_restr_jac, CEED_BASIS_NONE, jac_data)); 272 273 PetscCallCeed(ceed, CeedCompositeOperatorAddSub(op_ifunc, sub_op_ifunc_)); 274 *sub_op_ifunc = sub_op_ifunc_; 275 276 PetscCallCeed(ceed, CeedVectorDestroy(&q_data)); 277 PetscCallCeed(ceed, CeedBasisDestroy(&basis_q)); 278 PetscCallCeed(ceed, CeedBasisDestroy(&basis_x)); 279 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd)); 280 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_q)); 281 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_x)); 282 if (jac_data_size > 0) { 283 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_jac)); 284 PetscCallCeed(ceed, CeedVectorDestroy(&jac_data)); 285 } 286 PetscFunctionReturn(PETSC_SUCCESS); 287 } 288 289 /** 290 @brief Setups and adds IJacobian operator to given composite operator 291 292 The field data (element restriction, vector, etc) is read from `sub_op_ifunc` and used for the IJacobian. 293 294 @param[in] bc_def `BCDefinition` for the boundary condition IJacobian 295 @param[out] sub_op_ifunc IFunction operator corresponding to this IJacobian operator. 296 @param[in] domain_label `DMLabel` for the face orientation label 297 @param[in] label_value Orientation value 298 @param[in] qf_ijac QFunction for the IJacobian 299 @param[in,out] op_ijac Composite operator to be added to 300 **/ 301 PetscErrorCode HoneeBCAddIJacobianOp(BCDefinition bc_def, CeedOperator sub_op_ifunc, DMLabel domain_label, PetscInt label_value, 302 CeedQFunction qf_ijac, CeedOperator op_ijac) { 303 Honee honee; 304 Ceed ceed; 305 DM dm, dm_coord; 306 HoneeBCStruct honee_bc; 307 PetscInt dim, height = 1, num_comp_x, num_comp_q; 308 CeedInt q_data_size; 309 CeedVector q_data, jac_data; 310 CeedElemRestriction elem_restr_qd, elem_restr_q, elem_restr_x, elem_restr_jac; 311 CeedOperator sub_op_ijac; 312 CeedBasis basis_x, basis_q; 313 314 PetscFunctionBeginUser; 315 PetscCall(BCDefinitionGetContext(bc_def, &honee_bc)); 316 if (honee_bc->jac_data_size_sur == 0) PetscFunctionReturn(PETSC_SUCCESS); 317 PetscCall(BCDefinitionGetDM(bc_def, &dm)); 318 honee = honee_bc->honee; 319 ceed = honee_bc->honee->ceed; 320 321 PetscCall(DMGetDimension(dm, &dim)); 322 PetscCall(QDataBoundaryGetNumComponents(dm, &q_data_size)); 323 PetscCall(DMGetCoordinateNumComps(dm, &num_comp_x)); 324 325 { 326 PetscSection section; 327 328 PetscCall(DMGetLocalSection(dm, §ion)); 329 PetscCall(PetscSectionGetFieldComponents(section, bc_def->dm_field, &num_comp_q)); 330 } 331 332 PetscCall(DMGetCoordinateDM(dm, &dm_coord)); 333 PetscCall(CreateBasisFromPlex(ceed, dm, domain_label, label_value, height, bc_def->dm_field, &basis_q)); 334 PetscCall(CreateBasisFromPlex(ceed, dm_coord, domain_label, label_value, height, bc_def->dm_field, &basis_x)); 335 336 { // Get restriction and basis from the RHS function 337 CeedOperatorField op_field; 338 339 PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_op_ifunc, "q", &op_field)); 340 PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(op_field, &elem_restr_q)); 341 342 PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_op_ifunc, "x", &op_field)); 343 PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(op_field, &elem_restr_x)); 344 345 PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_op_ifunc, "surface jacobian data", &op_field)); 346 PetscCallCeed(ceed, CeedOperatorFieldGetData(op_field, NULL, &elem_restr_jac, NULL, &jac_data)); 347 } 348 349 PetscCall(QDataBoundaryGet(ceed, dm, domain_label, label_value, elem_restr_x, basis_x, honee->x_coord, &elem_restr_qd, &q_data, &q_data_size)); 350 351 PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_ijac, NULL, NULL, &sub_op_ijac)); 352 PetscCallCeed(ceed, CeedOperatorSetField(sub_op_ijac, "dq", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 353 PetscCallCeed(ceed, CeedOperatorSetField(sub_op_ijac, "Grad_dq", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 354 PetscCallCeed(ceed, CeedOperatorSetField(sub_op_ijac, "surface qdata", elem_restr_qd, CEED_BASIS_NONE, q_data)); 355 PetscCallCeed(ceed, CeedOperatorSetField(sub_op_ijac, "x", elem_restr_x, basis_x, honee->x_coord)); 356 PetscCallCeed(ceed, CeedOperatorSetField(sub_op_ijac, "surface jacobian data", elem_restr_jac, CEED_BASIS_NONE, jac_data)); 357 PetscCallCeed(ceed, CeedOperatorSetField(sub_op_ijac, "v", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 358 359 PetscCallCeed(ceed, CeedCompositeOperatorAddSub(op_ijac, sub_op_ijac)); 360 361 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_q)); 362 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_x)); 363 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_jac)); 364 PetscCallCeed(ceed, CeedVectorDestroy(&jac_data)); 365 PetscCallCeed(ceed, CeedVectorDestroy(&q_data)); 366 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd)); 367 PetscCallCeed(ceed, CeedBasisDestroy(&basis_q)); 368 PetscCallCeed(ceed, CeedBasisDestroy(&basis_x)); 369 PetscCallCeed(ceed, CeedOperatorDestroy(&sub_op_ijac)); 370 PetscFunctionReturn(PETSC_SUCCESS); 371 } 372