xref: /libCEED/examples/fluids/src/boundary_condition.c (revision d15030167e504f21b69a912dc34420b169f681b5)
1 // Copyright (c) 2017-2026, 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 #include "../navierstokes.h"
9 
10 /**
11    @brief Add `BCDefinition` to a `PetscSegBuffer`
12 
13    @param[in]     bc_def      `BCDefinition` to add
14    @param[in,out] bc_defs_seg `PetscSegBuffer` to add to
15 **/
16 static PetscErrorCode AddBCDefinitionToSegBuffer(BCDefinition bc_def, PetscSegBuffer bc_defs_seg) {
17   BCDefinition *bc_def_ptr;
18 
19   PetscFunctionBeginUser;
20   if (bc_def == NULL) PetscFunctionReturn(PETSC_SUCCESS);
21   PetscCall(PetscSegBufferGet(bc_defs_seg, 1, &bc_def_ptr));
22   *bc_def_ptr = bc_def;
23   PetscFunctionReturn(PETSC_SUCCESS);
24 }
25 
26 /**
27    @brief Create and setup `BCDefinition`s and `SimpleBC` from commandline options
28 
29    @param[in]     user    `User`
30    @param[in,out] problem `ProblemData`
31    @param[in]     app_ctx `AppCtx`
32    @param[in,out] bc      `SimpleBC`
33 **/
34 PetscErrorCode BoundaryConditionSetUp(User user, ProblemData problem, AppCtx app_ctx, SimpleBC bc) {
35   PetscSegBuffer bc_defs_seg;
36   PetscBool      flg;
37   BCDefinition   bc_def;
38 
39   PetscFunctionBeginUser;
40   PetscCall(PetscSegBufferCreate(sizeof(BCDefinition), 4, &bc_defs_seg));
41 
42   PetscOptionsBegin(user->comm, NULL, "Boundary Condition Options", NULL);
43 
44   PetscCall(PetscOptionsBCDefinition("-bc_wall", "Face IDs to apply wall BC", NULL, "wall", &bc_def, NULL));
45   PetscCall(AddBCDefinitionToSegBuffer(bc_def, bc_defs_seg));
46   if (bc_def) {
47     PetscInt num_essential_comps = 16, essential_comps[16];
48 
49     PetscCall(PetscOptionsIntArray("-wall_comps", "An array of constrained component numbers", NULL, essential_comps, &num_essential_comps, &flg));
50     PetscCall(BCDefinitionSetEssential(bc_def, num_essential_comps, essential_comps));
51 
52     app_ctx->wall_forces.num_wall = bc_def->num_label_values;
53     PetscCall(PetscMalloc1(bc_def->num_label_values, &app_ctx->wall_forces.walls));
54     PetscCall(PetscArraycpy(app_ctx->wall_forces.walls, bc_def->label_values, bc_def->num_label_values));
55   }
56 
57   {  // Symmetry Boundary Conditions
58     const char *deprecated[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"};
59     const char *flags[3]      = {"-bc_symmetry_x", "-bc_symmetry_y", "-bc_symmetry_z"};
60 
61     for (PetscInt j = 0; j < 3; j++) {
62       PetscCall(PetscOptionsDeprecated(deprecated[j], flags[j], "libCEED 0.12.0",
63                                        "Use -bc_symmetry_[x,y,z] for direct equivalency, or -bc_slip for weak, Riemann-based, direction-invariant "
64                                        "slip/no-penatration boundary conditions"));
65       PetscCall(PetscOptionsBCDefinition(flags[j], "Face IDs to apply symmetry BC", NULL, "symmetry", &bc_def, NULL));
66       if (!bc_def) {
67         PetscCall(PetscOptionsBCDefinition(deprecated[j], "Face IDs to apply symmetry BC", NULL, "symmetry", &bc_def, NULL));
68       }
69       PetscCall(AddBCDefinitionToSegBuffer(bc_def, bc_defs_seg));
70       if (bc_def) {
71         PetscInt essential_comps[1] = {j + 1};
72 
73         PetscCall(BCDefinitionSetEssential(bc_def, 1, essential_comps));
74       }
75     }
76   }
77 
78   // Inflow BCs
79   bc->num_inflow = 16;
80   PetscCall(PetscOptionsIntArray("-bc_inflow", "Face IDs to apply inflow BC", NULL, bc->inflows, &bc->num_inflow, NULL));
81   // Outflow BCs
82   bc->num_outflow = 16;
83   PetscCall(PetscOptionsIntArray("-bc_outflow", "Face IDs to apply outflow BC", NULL, bc->outflows, &bc->num_outflow, NULL));
84   // Freestream BCs
85   bc->num_freestream = 16;
86   PetscCall(PetscOptionsIntArray("-bc_freestream", "Face IDs to apply freestream BC", NULL, bc->freestreams, &bc->num_freestream, NULL));
87 
88   bc->num_slip = 16;
89   PetscCall(PetscOptionsIntArray("-bc_slip", "Face IDs to apply slip BC", NULL, bc->slips, &bc->num_slip, NULL));
90 
91   PetscOptionsEnd();
92 
93   PetscCall(PetscSegBufferGetSize(bc_defs_seg, &problem->num_bc_defs));
94   PetscCall(PetscSegBufferExtractAlloc(bc_defs_seg, &problem->bc_defs));
95   PetscCall(PetscSegBufferDestroy(&bc_defs_seg));
96 
97   //TODO: Verify that the BCDefinition don't have overlapping claims to boundary faces
98 
99   PetscFunctionReturn(PETSC_SUCCESS);
100 }
101