xref: /honee/src/bc_definition.c (revision 09240e0a7b1ed022a0b5d6f639eacef49a47bbbd)
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   PetscFunctionBeginUser;
51   if ((*bc_def)->name) PetscCall(PetscFree((*bc_def)->name));
52   if ((*bc_def)->label_values) PetscCall(PetscFree((*bc_def)->label_values));
53   if ((*bc_def)->essential_comps) PetscCall(PetscFree((*bc_def)->essential_comps));
54   if ((*bc_def)->dm) PetscCall(DMDestroy(&(*bc_def)->dm));
55   PetscCall(PetscFree(*bc_def));
56   *bc_def = NULL;
57   PetscFunctionReturn(PETSC_SUCCESS);
58 }
59 
60 /**
61    @brief Set `DM_BC_ESSENTIAL` boundary condition values
62 
63    @param[in,out] bc_def              `BCDefinition` to set values to
64    @param[in]     num_essential_comps Number of components to set
65    @param[in]     essential_comps     Array of components to set, size `num_essential_comps`
66 **/
67 PetscErrorCode BCDefinitionSetEssential(BCDefinition bc_def, PetscInt num_essential_comps, PetscInt essential_comps[]) {
68   PetscFunctionBeginUser;
69   bc_def->num_essential_comps = num_essential_comps;
70   PetscCall(PetscMalloc1(num_essential_comps, &bc_def->essential_comps));
71   PetscCall(PetscArraycpy(bc_def->essential_comps, essential_comps, num_essential_comps));
72   PetscFunctionReturn(PETSC_SUCCESS);
73 }
74 
75 /**
76    @brief Get `DM_BC_ESSENTIAL` boundary condition values
77 
78    @param[in]  bc_def              `BCDefinition` to set values to
79    @param[out] num_essential_comps Number of components to set
80    @param[out] essential_comps     Array of components to set, size `num_essential_comps`
81 **/
82 PetscErrorCode BCDefinitionGetEssential(BCDefinition bc_def, PetscInt *num_essential_comps, const PetscInt *essential_comps[]) {
83   PetscFunctionBeginUser;
84   *num_essential_comps = bc_def->num_essential_comps;
85   *essential_comps     = bc_def->essential_comps;
86   PetscFunctionReturn(PETSC_SUCCESS);
87 }
88 
89 #define LABEL_ARRAY_SIZE 256
90 
91 // @brief See `PetscOptionsBCDefinition`
92 PetscErrorCode PetscOptionsBCDefinition_Private(PetscOptionItems PetscOptionsObject, const char opt[], const char text[], const char man[],
93                                                 const char name[], BCDefinition *bc_def, PetscBool *set) {
94   PetscInt num_label_values = LABEL_ARRAY_SIZE, label_values[LABEL_ARRAY_SIZE] = {0};
95 
96   PetscFunctionBeginUser;
97   PetscCall(PetscOptionsIntArray(opt, text, man, label_values, &num_label_values, set));
98   if (num_label_values > 0) {
99     PetscCall(BCDefinitionCreate(name, num_label_values, label_values, bc_def));
100   } else {
101     *bc_def = NULL;
102   }
103   PetscFunctionReturn(PETSC_SUCCESS);
104 }
105 
106 /**
107   @brief Set `DM` for BCDefinition
108 
109   @param[in,out] bc_def `BCDefinition` to add `dm` to
110   @param[in]     dm     `DM` to assign to BCDefinition, or `NULL` to remove `DM`
111 **/
112 PetscErrorCode BCDefinitionSetDM(BCDefinition bc_def, DM dm) {
113   PetscFunctionBeginUser;
114   if (bc_def->dm) PetscCall(DMDestroy(&bc_def->dm));
115   if (dm) {
116     PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
117     PetscCall(PetscObjectReference((PetscObject)dm));
118     bc_def->dm = dm;
119   }
120   PetscFunctionReturn(PETSC_SUCCESS);
121 }
122 
123 /**
124   @brief Get `DM` assigned to BCDefinition
125 
126   @param[in]  bc_def `BCDefinition` to get `dm` from
127   @param[out] dm     `DM` assigned to BCDefinition
128 **/
129 PetscErrorCode BCDefinitionGetDM(BCDefinition bc_def, DM *dm) {
130   PetscFunctionBeginUser;
131   PetscAssertPointer(dm, 2);
132   *dm = bc_def->dm;
133   PetscFunctionReturn(PETSC_SUCCESS);
134 }
135