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 <bc_definition.h>
9
10 /**
11 @brief Create `BCDefinition`
12
13 @param[in] name Name of the boundary condition
14 @param[in] num_label_values Number of `DMLabel` values
15 @param[in] label_values Array of label values that define the boundaries controlled by the `BCDefinition`, size `num_label_values`
16 @param[out] bc_def The new `BCDefinition`
17 **/
BCDefinitionCreate(const char * name,PetscInt num_label_values,PetscInt label_values[],BCDefinition * bc_def)18 PetscErrorCode BCDefinitionCreate(const char *name, PetscInt num_label_values, PetscInt label_values[], BCDefinition *bc_def) {
19 PetscFunctionBeginUser;
20 PetscCall(PetscNew(bc_def));
21
22 PetscCall(PetscStrallocpy(name, &(*bc_def)->name));
23 (*bc_def)->num_label_values = num_label_values;
24 PetscCall(PetscMalloc1(num_label_values, &(*bc_def)->label_values));
25 for (PetscInt i = 0; i < num_label_values; i++) (*bc_def)->label_values[i] = label_values[i];
26 PetscFunctionReturn(PETSC_SUCCESS);
27 }
28
29 /**
30 @brief Get base information for `BCDefinition`
31
32 @param[in] bc_def `BCDefinition` to get information from
33 @param[out] name Name of the `BCDefinition`
34 @param[out] num_label_values Number of `DMLabel` values
35 @param[out] label_values Array of label values that define the boundaries controlled by the `BCDefinition`, size `num_label_values`
36 **/
BCDefinitionGetInfo(BCDefinition bc_def,const char * name[],PetscInt * num_label_values,const PetscInt * label_values[])37 PetscErrorCode BCDefinitionGetInfo(BCDefinition bc_def, const char *name[], PetscInt *num_label_values, const PetscInt *label_values[]) {
38 PetscFunctionBeginUser;
39 if (name) *name = bc_def->name;
40 if (label_values) {
41 *num_label_values = bc_def->num_label_values;
42 *label_values = bc_def->label_values;
43 }
44 PetscFunctionReturn(PETSC_SUCCESS);
45 }
46
47 /**
48 @brief Destory a `BCDefinition` object
49
50 @param[in,out] bc_def `BCDefinition` to be destroyed
51 **/
BCDefinitionDestroy(BCDefinition * bc_def)52 PetscErrorCode BCDefinitionDestroy(BCDefinition *bc_def) {
53 PetscFunctionBeginUser;
54 if ((*bc_def)->name) PetscCall(PetscFree((*bc_def)->name));
55 if ((*bc_def)->label_values) PetscCall(PetscFree((*bc_def)->label_values));
56 if ((*bc_def)->essential_comps) PetscCall(PetscFree((*bc_def)->essential_comps));
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 **/
BCDefinitionSetEssential(BCDefinition bc_def,PetscInt num_essential_comps,PetscInt essential_comps[])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 **/
BCDefinitionGetEssential(BCDefinition bc_def,PetscInt * num_essential_comps,const PetscInt * essential_comps[])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`
PetscOptionsBCDefinition_Private(PetscOptionItems PetscOptionsObject,const char opt[],const char text[],const char man[],const char name[],BCDefinition * bc_def,PetscBool * set)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