xref: /honee/src/bc_definition.c (revision 8a8cb6e06ce4728cc6d80ca92f8de31da49852e5)
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 
6 /**
7    @brief Create `BCDefinition`
8 
9    @param[in]  name             Name of the boundary condition
10    @param[in]  num_label_values Number of `DMLabel` values
11    @param[in]  label_values     Array of label values that define the boundaries controlled by the `BCDefinition`, size `num_label_values`
12    @param[out] bc_def           The new `BCDefinition`
13 **/
14 PetscErrorCode BCDefinitionCreate(const char *name, PetscInt num_label_values, PetscInt label_values[], BCDefinition *bc_def) {
15   PetscFunctionBeginUser;
16   PetscCall(PetscNew(bc_def));
17 
18   PetscCall(PetscStrallocpy(name, &(*bc_def)->name));
19   (*bc_def)->num_label_values = num_label_values;
20   PetscCall(PetscMalloc1(num_label_values, &(*bc_def)->label_values));
21   for (PetscInt i = 0; i < num_label_values; i++) (*bc_def)->label_values[i] = label_values[i];
22   PetscFunctionReturn(PETSC_SUCCESS);
23 }
24 
25 /**
26    @brief Get base information for `BCDefinition`
27 
28    @param[in]  bc_def           `BCDefinition` to get information from
29    @param[out] name             Name of the `BCDefinition`
30    @param[out] num_label_values Number of `DMLabel` values
31    @param[out] label_values     Array of label values that define the boundaries controlled by the `BCDefinition`, size `num_label_values`
32 **/
33 PetscErrorCode BCDefinitionGetInfo(BCDefinition bc_def, const char *name[], PetscInt *num_label_values, const PetscInt *label_values[]) {
34   PetscFunctionBeginUser;
35   if (name) *name = bc_def->name;
36   if (label_values) {
37     *num_label_values = bc_def->num_label_values;
38     *label_values     = bc_def->label_values;
39   }
40   PetscFunctionReturn(PETSC_SUCCESS);
41 }
42 
43 /**
44    @brief Destory a `BCDefinition` object
45 
46    @param[in,out] bc_def `BCDefinition` to be destroyed
47 **/
48 PetscErrorCode BCDefinitionDestroy(BCDefinition *bc_def) {
49   PetscFunctionBeginUser;
50   if ((*bc_def)->name) PetscCall(PetscFree((*bc_def)->name));
51   if ((*bc_def)->label_values) PetscCall(PetscFree((*bc_def)->label_values));
52   if ((*bc_def)->essential_comps) PetscCall(PetscFree((*bc_def)->essential_comps));
53   PetscCall(PetscFree(*bc_def));
54   *bc_def = NULL;
55   PetscFunctionReturn(PETSC_SUCCESS);
56 }
57 
58 /**
59    @brief Set `DM_BC_ESSENTIAL` boundary condition values
60 
61    @param[in,out] bc_def              `BCDefinition` to set values to
62    @param[in]     num_essential_comps Number of components to set
63    @param[in]     essential_comps     Array of components to set, size `num_essential_comps`
64 **/
65 PetscErrorCode BCDefinitionSetEssential(BCDefinition bc_def, PetscInt num_essential_comps, PetscInt essential_comps[]) {
66   PetscFunctionBeginUser;
67   bc_def->num_essential_comps = num_essential_comps;
68   PetscCall(PetscMalloc1(num_essential_comps, &bc_def->essential_comps));
69   PetscCall(PetscArraycpy(bc_def->essential_comps, essential_comps, num_essential_comps));
70   PetscFunctionReturn(PETSC_SUCCESS);
71 }
72 
73 /**
74    @brief Get `DM_BC_ESSENTIAL` boundary condition values
75 
76    @param[in]  bc_def              `BCDefinition` to set values to
77    @param[out] num_essential_comps Number of components to set
78    @param[out] essential_comps     Array of components to set, size `num_essential_comps`
79 **/
80 PetscErrorCode BCDefinitionGetEssential(BCDefinition bc_def, PetscInt *num_essential_comps, const PetscInt *essential_comps[]) {
81   PetscFunctionBeginUser;
82   *num_essential_comps = bc_def->num_essential_comps;
83   *essential_comps     = bc_def->essential_comps;
84   PetscFunctionReturn(PETSC_SUCCESS);
85 }
86 
87 #define LABEL_ARRAY_SIZE 256
88 
89 // @brief See `PetscOptionsBCDefinition`
90 PetscErrorCode PetscOptionsBCDefinition_Private(PetscOptionItems *PetscOptionsObject, const char opt[], const char text[], const char man[],
91                                                 const char name[], BCDefinition *bc_def, PetscBool *set) {
92   PetscInt num_label_values = LABEL_ARRAY_SIZE, label_values[LABEL_ARRAY_SIZE] = {0};
93 
94   PetscFunctionBeginUser;
95   PetscCall(PetscOptionsIntArray(opt, text, man, label_values, &num_label_values, set));
96   if (num_label_values > 0) {
97     PetscCall(BCDefinitionCreate(name, num_label_values, label_values, bc_def));
98   } else {
99     *bc_def = NULL;
100   }
101   PetscFunctionReturn(PETSC_SUCCESS);
102 }
103