xref: /honee/src/bc_definition.c (revision 2e678684359fe84b9aa1ee98600bc5e49a7b4181)
1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3 
4 #include <bc_definition.h>
5 #include <petsc/private/petscimpl.h>
6 
7 /**
8    @brief Create `BCDefinition`
9 
10    @param[in]  name             Name of the boundary condition
11    @param[in]  num_label_values Number of `DMLabel` values
12    @param[in]  label_values     Array of label values that define the boundaries controlled by the `BCDefinition`, size `num_label_values`
13    @param[out] bc_def           The new `BCDefinition`
14 **/
15 PetscErrorCode BCDefinitionCreate(const char *name, PetscInt num_label_values, PetscInt label_values[], BCDefinition *bc_def) {
16   PetscFunctionBeginUser;
17   PetscCall(PetscNew(bc_def));
18 
19   PetscCall(PetscStrallocpy(name, &(*bc_def)->name));
20   (*bc_def)->num_label_values = num_label_values;
21   PetscCall(PetscMalloc1(num_label_values, &(*bc_def)->label_values));
22   for (PetscInt i = 0; i < num_label_values; i++) (*bc_def)->label_values[i] = label_values[i];
23   PetscFunctionReturn(PETSC_SUCCESS);
24 }
25 
26 /**
27    @brief Get base information for `BCDefinition`
28 
29    @param[in]  bc_def           `BCDefinition` to get information from
30    @param[out] name             Name of the `BCDefinition`
31    @param[out] num_label_values Number of `DMLabel` values
32    @param[out] label_values     Array of label values that define the boundaries controlled by the `BCDefinition`, size `num_label_values`
33 **/
34 PetscErrorCode BCDefinitionGetInfo(BCDefinition bc_def, const char *name[], PetscInt *num_label_values, const PetscInt *label_values[]) {
35   PetscFunctionBeginUser;
36   if (name) *name = bc_def->name;
37   if (label_values) {
38     *num_label_values = bc_def->num_label_values;
39     *label_values     = bc_def->label_values;
40   }
41   PetscFunctionReturn(PETSC_SUCCESS);
42 }
43 
44 /**
45    @brief Destory a `BCDefinition` object
46 
47    @param[in,out] bc_def `BCDefinition` to be destroyed
48 **/
49 PetscErrorCode BCDefinitionDestroy(BCDefinition *bc_def) {
50   BCDefinition bc_def_ = *bc_def;
51   PetscFunctionBeginUser;
52   if (bc_def_->name) PetscCall(PetscFree(bc_def_->name));
53   if (bc_def_->label_values) PetscCall(PetscFree(bc_def_->label_values));
54   if (bc_def_->essential_comps) PetscCall(PetscFree(bc_def_->essential_comps));
55   if (bc_def_->dm) PetscCall(DMDestroy(&bc_def_->dm));
56   if (bc_def_->DestroyCtx) PetscCall((*(bc_def_->DestroyCtx))(&bc_def_->ctx));
57   PetscCall(PetscFree(bc_def_));
58   *bc_def = NULL;
59   PetscFunctionReturn(PETSC_SUCCESS);
60 }
61 
62 /**
63    @brief Set `DM_BC_ESSENTIAL` boundary condition values
64 
65    @param[in,out] bc_def              `BCDefinition` to set values to
66    @param[in]     num_essential_comps Number of components to set
67    @param[in]     essential_comps     Array of components to set, size `num_essential_comps`
68 **/
69 PetscErrorCode BCDefinitionSetEssential(BCDefinition bc_def, PetscInt num_essential_comps, PetscInt essential_comps[]) {
70   PetscFunctionBeginUser;
71   bc_def->num_essential_comps = num_essential_comps;
72   PetscCall(PetscMalloc1(num_essential_comps, &bc_def->essential_comps));
73   PetscCall(PetscArraycpy(bc_def->essential_comps, essential_comps, num_essential_comps));
74   PetscFunctionReturn(PETSC_SUCCESS);
75 }
76 
77 /**
78    @brief Get `DM_BC_ESSENTIAL` boundary condition values
79 
80    @param[in]  bc_def              `BCDefinition` to set values to
81    @param[out] num_essential_comps Number of components to set
82    @param[out] essential_comps     Array of components to set, size `num_essential_comps`
83 **/
84 PetscErrorCode BCDefinitionGetEssential(BCDefinition bc_def, PetscInt *num_essential_comps, const PetscInt *essential_comps[]) {
85   PetscFunctionBeginUser;
86   *num_essential_comps = bc_def->num_essential_comps;
87   *essential_comps     = bc_def->essential_comps;
88   PetscFunctionReturn(PETSC_SUCCESS);
89 }
90 
91 #define LABEL_ARRAY_SIZE 256
92 
93 // @brief See `PetscOptionsBCDefinition`
94 PetscErrorCode PetscOptionsBCDefinition_Private(PetscOptionItems PetscOptionsObject, const char opt[], const char text[], const char man[],
95                                                 const char name[], BCDefinition *bc_def, PetscBool *set) {
96   PetscInt num_label_values = LABEL_ARRAY_SIZE, label_values[LABEL_ARRAY_SIZE] = {0};
97 
98   PetscFunctionBeginUser;
99   PetscCall(PetscOptionsIntArray(opt, text, man, label_values, &num_label_values, set));
100   if (num_label_values > 0) {
101     PetscCall(BCDefinitionCreate(name, num_label_values, label_values, bc_def));
102   } else {
103     *bc_def = NULL;
104   }
105   PetscFunctionReturn(PETSC_SUCCESS);
106 }
107 
108 /**
109   @brief Set `DM` for BCDefinition
110 
111   @param[in,out] bc_def `BCDefinition` to add `dm` to
112   @param[in]     dm     `DM` to assign to BCDefinition, or `NULL` to remove `DM`
113 **/
114 PetscErrorCode BCDefinitionSetDM(BCDefinition bc_def, DM dm) {
115   PetscFunctionBeginUser;
116   if (bc_def->dm) PetscCall(DMDestroy(&bc_def->dm));
117   if (dm) {
118     PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
119     PetscCall(PetscObjectReference((PetscObject)dm));
120     bc_def->dm = dm;
121   }
122   PetscFunctionReturn(PETSC_SUCCESS);
123 }
124 
125 /**
126   @brief Get `DM` assigned to BCDefinition
127 
128   @param[in]  bc_def `BCDefinition` to get `dm` from
129   @param[out] dm     `DM` assigned to BCDefinition
130 **/
131 PetscErrorCode BCDefinitionGetDM(BCDefinition bc_def, DM *dm) {
132   PetscFunctionBeginUser;
133   PetscAssertPointer(dm, 2);
134   *dm = bc_def->dm;
135   PetscFunctionReturn(PETSC_SUCCESS);
136 }
137 
138 /**
139   @brief Set custom context struct for use in BCDefinition
140 
141   @param[in,out] bc_def      `BCDefinition` to add `ctx` to
142   @param[in]     destroy_ctx Optional function pointer that destroys the user context on `BCDefinitionDestroy()`
143   @param[in]     ctx         Pointer to context struct
144 **/
145 PetscErrorCode BCDefinitionSetContext(BCDefinition bc_def, PetscCtxDestroyFn *destroy_ctx, void *ctx) {
146   PetscFunctionBeginUser;
147   if (bc_def->DestroyCtx) PetscCall((*(bc_def->DestroyCtx))(&bc_def->ctx));
148   bc_def->ctx        = ctx;
149   bc_def->DestroyCtx = destroy_ctx;
150   PetscFunctionReturn(PETSC_SUCCESS);
151 }
152 
153 /**
154   @brief Set custom context struct for use in BCDefinition
155 
156   @param[in]  bc_def `BCDefinition` to get `ctx` from
157   @param[out] ctx    Pointer to context struct
158 **/
159 PetscErrorCode BCDefinitionGetContext(BCDefinition bc_def, void *ctx) {
160   PetscFunctionBeginUser;
161   PetscAssertPointer(ctx, 2);
162   *(void **)ctx = bc_def->ctx;
163   PetscFunctionReturn(PETSC_SUCCESS);
164 }
165