xref: /honee/src/honee.c (revision 00359db47665a79ecb0241f6ccbf886b649022df)
1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3 
4 #include <navierstokes.h>
5 
6 PetscClassId HONEE_CLASSID;
7 
8 // @brief Initalize `HONEE` class.
HoneeInitClass_Private()9 static PetscErrorCode HoneeInitClass_Private() {
10   static PetscBool registered = PETSC_FALSE;
11 
12   PetscFunctionBeginUser;
13   if (registered) PetscFunctionReturn(PETSC_SUCCESS);
14   PetscCall(PetscClassIdRegister("Honee", &HONEE_CLASSID));
15   registered = PETSC_TRUE;
16   PetscFunctionReturn(PETSC_SUCCESS);
17 }
18 
19 /**
20   @brief Initialize `Honee` object
21 
22   @param[in]  comm  `MPI_Comm` for the object
23   @param[out] honee The initialized `Honee` object
24 **/
HoneeInit(MPI_Comm comm,Honee * honee)25 PetscErrorCode HoneeInit(MPI_Comm comm, Honee *honee) {
26   Honee honee_;
27 
28   PetscFunctionBeginUser;
29   PetscCall(HoneeInitClass_Private());
30   PetscCall(PetscHeaderCreate(honee_, HONEE_CLASSID, "Honee", "HONEE", "Honee", comm, HoneeDestroy, NULL));
31   PetscCall(PetscCalloc4(1, &honee_->app_ctx, 1, &honee_->problem_data, 1, &honee_->phys, 1, &honee_->units));
32   honee_->problem_data->set_bc_from_ics = PETSC_TRUE;
33   honee_->comm                          = PETSC_COMM_WORLD;
34   honee_->start_time                    = time(NULL);
35   PetscCall(RegisterLogEvents());
36   *honee = honee_;
37   PetscFunctionReturn(PETSC_SUCCESS);
38 }
39 
40 /**
41   @brief Destroy `Honee` object
42 
43   @param[in] honee Object to be destroyed
44 **/
HoneeDestroy(Honee * honee)45 PetscErrorCode HoneeDestroy(Honee *honee) {
46   Honee honee_ = *honee;
47   Ceed  ceed;
48 
49   PetscFunctionBeginUser;
50   if (!honee_) PetscFunctionReturn(PETSC_SUCCESS);
51   PetscValidHeaderSpecific(honee_, HONEE_CLASSID, 1);
52 
53   ceed                = honee_->ceed;
54   MPI_Comm    comm    = honee_->comm;
55   AppCtx      app_ctx = honee_->app_ctx;
56   ProblemData problem = honee_->problem_data;
57 
58   PetscCall(QDataClearStoredData());
59   PetscCall(DivDiffFluxProjectionDataDestroy(honee_->diff_flux_proj));
60 
61   // -- Vectors
62   PetscCallCeed(ceed, CeedVectorDestroy(&honee_->q_ceed));
63   PetscCallCeed(ceed, CeedVectorDestroy(&honee_->q_dot_ceed));
64   PetscCallCeed(ceed, CeedVectorDestroy(&honee_->g_ceed));
65 
66   // Destroy QFunction contexts after using
67   {
68     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->ics.qfctx));
69     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->apply_vol_rhs.qfctx));
70     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->apply_vol_ifunction.qfctx));
71     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->apply_vol_ijacobian.qfctx));
72   }
73 
74   // -- Operators
75   PetscCall(OperatorApplyContextDestroy(honee_->op_ics_ctx));
76   PetscCall(OperatorApplyContextDestroy(honee_->op_rhs_ctx));
77   PetscCall(OperatorApplyContextDestroy(honee_->op_strong_bc_ctx));
78   PetscCallCeed(ceed, CeedOperatorDestroy(&honee_->op_ifunction));
79 
80   // -- Ceed
81   PetscCheck(CeedDestroy(&ceed) == CEED_ERROR_SUCCESS, comm, PETSC_ERR_LIB, "Destroying Ceed object failed");
82 
83   // ---------------------------------------------------------------------------
84   // Clean up PETSc
85   // ---------------------------------------------------------------------------
86   // -- Vectors
87   PetscCall(VecDestroy(&honee_->Q_loc));
88   PetscCall(VecDestroy(&honee_->Q_dot_loc));
89 
90   PetscCall(KSPDestroy(&honee_->mass_ksp));
91 
92   // -- Matrices
93   PetscCall(MatDestroy(&honee_->interp_viz));
94   PetscCall(MatDestroy(&honee_->mat_ijacobian));
95 
96   // -- DM
97   PetscCall(DMDestroy(&honee_->dm_viz));
98 
99   // -- Function list
100   PetscCall(PetscFunctionListDestroy(&app_ctx->problems));
101 
102   PetscCall(PetscFree(app_ctx->amat_type));
103   PetscCall(PetscFree(app_ctx->wall_forces.walls));
104   PetscCall(PetscViewerDestroy(&app_ctx->wall_forces.viewer));
105 
106   // -- Structs
107   for (PetscInt i = 0; i < problem->num_bc_defs; i++) {
108     PetscCall(BCDefinitionDestroy(&problem->bc_defs[i]));
109   }
110   PetscCall(PetscFree(problem->bc_defs));
111   for (PetscInt i = 0; i < problem->num_components; i++) {
112     PetscCall(PetscFree(problem->component_names[i]));
113   }
114   PetscCall(PetscFree(problem->component_names));
115 
116   PetscCall(PetscFree4(honee_->app_ctx, honee_->problem_data, honee_->phys, honee_->units));
117   PetscCall(PetscHeaderDestroy(&honee_));
118   *honee = NULL;
119   PetscFunctionReturn(PETSC_SUCCESS);
120 }
121 
122 /**
123   @brief Saves a pointer-to-data in a `Honee` object by key
124 
125   This also checks whether `key` has been used previously and will error out if it has.
126 
127   @param[in] honee             `Honee` object to save pointer to
128   @param[in] key               String used to identify the pointer
129   @param[in] container         Pointer to the data
130   @param[in] container_destroy Function to destroy the struct after it is used
131 **/
HoneeSetContainer(Honee honee,const char key[],void * container,PetscCtxDestroyFn * container_destroy)132 PetscErrorCode HoneeSetContainer(Honee honee, const char key[], void *container, PetscCtxDestroyFn *container_destroy) {
133   PetscFunctionBeginUser;
134   {  // Verify that key is not already taken
135     void *test_data;
136     PetscCall(PetscObjectContainerQuery((PetscObject)honee, key, &test_data));
137     PetscCheck(test_data == NULL, PetscObjectComm((PetscObject)honee), PETSC_ERR_SUP, "Cannot set container with key '%s'; key is already in use.",
138                key);
139   }
140   PetscCall(PetscObjectContainerCompose((PetscObject)honee, key, container, container_destroy));
141   PetscFunctionReturn(PETSC_SUCCESS);
142 }
143 
144 /**
145   @brief Retrieve a pointer-to-data in a `Honee` object by key
146 
147   This will error out if `honee` does not have a container identified by `key`.
148 
149   @param[in]  honee     `Honee` object to retrieve pointer from
150   @param[in]  key       String used to identify the pointer
151   @param[out] container Pointer to the data
152 **/
HoneeGetContainer(Honee honee,const char key[],void * container)153 PetscErrorCode HoneeGetContainer(Honee honee, const char key[], void *container) {
154   PetscFunctionBeginUser;
155   PetscCall(PetscObjectContainerQuery((PetscObject)honee, key, container));
156   PetscCheck(*(void **)container != NULL, PetscObjectComm((PetscObject)honee), PETSC_ERR_SUP, "Container with key '%s' not found.", key);
157   PetscFunctionReturn(PETSC_SUCCESS);
158 }
159 
160 /**
161   @brief Check if `Honee` object as pointer-to-data identified by key
162 
163   @param[in]  honee   `Honee` object to query for key
164   @param[in]  key     String to search for
165   @param[out] has_key Whether key has been set
166 **/
HoneeHasContainer(Honee honee,const char key[],PetscBool * has_key)167 PetscErrorCode HoneeHasContainer(Honee honee, const char key[], PetscBool *has_key) {
168   void *test_data;
169 
170   PetscFunctionBeginUser;
171   PetscCall(PetscObjectContainerQuery((PetscObject)honee, key, &test_data));
172   *has_key = test_data == NULL ? PETSC_FALSE : PETSC_TRUE;
173   PetscFunctionReturn(PETSC_SUCCESS);
174 }
175 
176 const DMLabel  DMLABEL_DEFAULT       = NULL;
177 const PetscInt DMLABEL_DEFAULT_VALUE = 0;
178