xref: /honee/src/honee.c (revision 0c70a8bcd2b7fcfffcc94798a5a3a808db05e6f3)
16a9fb8efSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
26a9fb8efSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
36a9fb8efSJames Wright 
46a9fb8efSJames Wright #include <navierstokes.h>
56a9fb8efSJames Wright 
66a9fb8efSJames Wright PetscClassId HONEE_CLASSID;
76a9fb8efSJames Wright 
86a9fb8efSJames Wright // @brief Initalize `HONEE` class.
96a9fb8efSJames Wright static PetscErrorCode HoneeInitClass_Private() {
106a9fb8efSJames Wright   static PetscBool registered = PETSC_FALSE;
116a9fb8efSJames Wright 
126a9fb8efSJames Wright   PetscFunctionBeginUser;
136a9fb8efSJames Wright   if (registered) PetscFunctionReturn(PETSC_SUCCESS);
146a9fb8efSJames Wright   PetscCall(PetscClassIdRegister("Honee", &HONEE_CLASSID));
156a9fb8efSJames Wright   registered = PETSC_TRUE;
166a9fb8efSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
176a9fb8efSJames Wright }
186a9fb8efSJames Wright 
196a9fb8efSJames Wright /**
206a9fb8efSJames Wright   @brief Initialize `Honee` object
216a9fb8efSJames Wright 
226a9fb8efSJames Wright   @param[in]  comm  `MPI_Comm` for the object
236a9fb8efSJames Wright   @param[out] honee The initialized `Honee` object
246a9fb8efSJames Wright **/
256a9fb8efSJames Wright PetscErrorCode HoneeInit(MPI_Comm comm, Honee *honee) {
266a9fb8efSJames Wright   Honee       honee_;
276a9fb8efSJames Wright   AppCtx      app_ctx;
286a9fb8efSJames Wright   ProblemData problem;
296a9fb8efSJames Wright   Physics     phys_ctx;
306a9fb8efSJames Wright   Units       units;
316a9fb8efSJames Wright 
326a9fb8efSJames Wright   PetscFunctionBeginUser;
336a9fb8efSJames Wright   PetscCall(HoneeInitClass_Private());
346a9fb8efSJames Wright   PetscCall(PetscHeaderCreate(honee_, HONEE_CLASSID, "Honee", "HONEE", "Honee", comm, HoneeDestroy, NULL));
356a9fb8efSJames Wright   PetscCall(PetscNew(&app_ctx));
366a9fb8efSJames Wright   PetscCall(PetscNew(&problem));
376a9fb8efSJames Wright   PetscCall(PetscNew(&phys_ctx));
386a9fb8efSJames Wright   PetscCall(PetscNew(&units));
396a9fb8efSJames Wright   honee_->app_ctx          = app_ctx;
406a9fb8efSJames Wright   honee_->units            = units;
416a9fb8efSJames Wright   honee_->phys             = phys_ctx;
426a9fb8efSJames Wright   honee_->problem_data     = problem;
436a9fb8efSJames Wright   problem->set_bc_from_ics = PETSC_TRUE;
446a9fb8efSJames Wright   honee_->comm             = PETSC_COMM_WORLD;
456a9fb8efSJames Wright   honee_->start_time       = time(NULL);
466a9fb8efSJames Wright 
476a9fb8efSJames Wright   PetscCall(RegisterLogEvents());
486a9fb8efSJames Wright 
496a9fb8efSJames Wright   *honee = honee_;
506a9fb8efSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
516a9fb8efSJames Wright }
526a9fb8efSJames Wright 
536a9fb8efSJames Wright /**
546a9fb8efSJames Wright   @brief Destroy `Honee` object
556a9fb8efSJames Wright 
566a9fb8efSJames Wright   @param[in] honee Object to be destroyed
576a9fb8efSJames Wright **/
586a9fb8efSJames Wright PetscErrorCode HoneeDestroy(Honee *honee) {
596a9fb8efSJames Wright   Honee honee_ = *honee;
606a9fb8efSJames Wright   Ceed  ceed;
616a9fb8efSJames Wright 
626a9fb8efSJames Wright   PetscFunctionBeginUser;
636a9fb8efSJames Wright   if (!honee_) PetscFunctionReturn(PETSC_SUCCESS);
646a9fb8efSJames Wright   PetscValidHeaderSpecific(honee_, HONEE_CLASSID, 1);
656a9fb8efSJames Wright 
666a9fb8efSJames Wright   ceed                = honee_->ceed;
676a9fb8efSJames Wright   MPI_Comm    comm    = honee_->comm;
686a9fb8efSJames Wright   AppCtx      app_ctx = honee_->app_ctx;
696a9fb8efSJames Wright   ProblemData problem = honee_->problem_data;
706a9fb8efSJames Wright 
716a9fb8efSJames Wright   PetscCall(QDataClearStoredData());
726a9fb8efSJames Wright   PetscCall(DivDiffFluxProjectionDataDestroy(honee_->diff_flux_proj));
736a9fb8efSJames Wright 
746a9fb8efSJames Wright   // -- Vectors
756a9fb8efSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&honee_->x_coord));
766a9fb8efSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&honee_->q_ceed));
776a9fb8efSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&honee_->q_dot_ceed));
786a9fb8efSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&honee_->g_ceed));
796a9fb8efSJames Wright 
806a9fb8efSJames Wright   // -- Bases
816a9fb8efSJames Wright   PetscCallCeed(ceed, CeedBasisDestroy(&honee_->basis_q));
826a9fb8efSJames Wright   PetscCallCeed(ceed, CeedBasisDestroy(&honee_->basis_x));
836a9fb8efSJames Wright 
846a9fb8efSJames Wright   // -- Restrictions
856a9fb8efSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&honee_->elem_restr_q));
866a9fb8efSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&honee_->elem_restr_x));
876a9fb8efSJames Wright 
886a9fb8efSJames Wright   // Destroy QFunction contexts after using
896a9fb8efSJames Wright   {
906a9fb8efSJames Wright     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->ics.qfctx));
916a9fb8efSJames Wright     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->apply_vol_rhs.qfctx));
926a9fb8efSJames Wright     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->apply_vol_ifunction.qfctx));
936a9fb8efSJames Wright     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->apply_vol_ijacobian.qfctx));
946a9fb8efSJames Wright   }
956a9fb8efSJames Wright 
966a9fb8efSJames Wright   // -- Operators
976a9fb8efSJames Wright   PetscCall(OperatorApplyContextDestroy(honee_->op_ics_ctx));
986a9fb8efSJames Wright   PetscCall(OperatorApplyContextDestroy(honee_->op_rhs_ctx));
996a9fb8efSJames Wright   PetscCall(OperatorApplyContextDestroy(honee_->op_strong_bc_ctx));
1006a9fb8efSJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&honee_->op_ifunction));
1016a9fb8efSJames Wright 
1026a9fb8efSJames Wright   // -- Ceed
1036a9fb8efSJames Wright   PetscCheck(CeedDestroy(&ceed) == CEED_ERROR_SUCCESS, comm, PETSC_ERR_LIB, "Destroying Ceed object failed");
1046a9fb8efSJames Wright 
1056a9fb8efSJames Wright   // ---------------------------------------------------------------------------
1066a9fb8efSJames Wright   // Clean up PETSc
1076a9fb8efSJames Wright   // ---------------------------------------------------------------------------
1086a9fb8efSJames Wright   // -- Vectors
1096a9fb8efSJames Wright   PetscCall(VecDestroy(&honee_->Q_loc));
1106a9fb8efSJames Wright   PetscCall(VecDestroy(&honee_->Q_dot_loc));
1116a9fb8efSJames Wright 
1126a9fb8efSJames Wright   PetscCall(KSPDestroy(&honee_->mass_ksp));
1136a9fb8efSJames Wright 
1146a9fb8efSJames Wright   // -- Matrices
1156a9fb8efSJames Wright   PetscCall(MatDestroy(&honee_->interp_viz));
1166a9fb8efSJames Wright   PetscCall(MatDestroy(&honee_->mat_ijacobian));
1176a9fb8efSJames Wright 
1186a9fb8efSJames Wright   // -- DM
1196a9fb8efSJames Wright   PetscCall(DMDestroy(&honee_->dm_viz));
1206a9fb8efSJames Wright 
1216a9fb8efSJames Wright   // -- Function list
1226a9fb8efSJames Wright   PetscCall(PetscFunctionListDestroy(&app_ctx->problems));
1236a9fb8efSJames Wright 
1246a9fb8efSJames Wright   PetscCall(PetscFree(app_ctx->amat_type));
1256a9fb8efSJames Wright   PetscCall(PetscFree(app_ctx->wall_forces.walls));
1266a9fb8efSJames Wright   PetscCall(PetscViewerDestroy(&app_ctx->wall_forces.viewer));
1276a9fb8efSJames Wright 
1286a9fb8efSJames Wright   // -- Structs
1296a9fb8efSJames Wright   for (PetscInt i = 0; i < problem->num_bc_defs; i++) {
1306a9fb8efSJames Wright     PetscCall(BCDefinitionDestroy(&problem->bc_defs[i]));
1316a9fb8efSJames Wright   }
1326a9fb8efSJames Wright   PetscCall(PetscFree(problem->bc_defs));
1336a9fb8efSJames Wright   PetscCall(PetscFree(honee_->units));
1346a9fb8efSJames Wright   PetscCall(PetscFree(problem));
1356a9fb8efSJames Wright   PetscCall(PetscFree(honee_->phys));
1366a9fb8efSJames Wright   PetscCall(PetscFree(app_ctx));
1376a9fb8efSJames Wright   PetscCall(PetscHeaderDestroy(&honee_));
1386a9fb8efSJames Wright   *honee = NULL;
1396a9fb8efSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
1406a9fb8efSJames Wright }
141*0c70a8bcSJames Wright 
142*0c70a8bcSJames Wright /**
143*0c70a8bcSJames Wright   @brief Saves a pointer-to-data in a `Honee` object by key
144*0c70a8bcSJames Wright 
145*0c70a8bcSJames Wright   This also checks whether `key` has been used previously and will error out if it has.
146*0c70a8bcSJames Wright 
147*0c70a8bcSJames Wright   @param[in] honee             `Honee` object to save pointer to
148*0c70a8bcSJames Wright   @param[in] key               String used to identify the pointer
149*0c70a8bcSJames Wright   @param[in] container         Pointer to the data
150*0c70a8bcSJames Wright   @param[in] container_destroy Function to destroy the struct after it is used
151*0c70a8bcSJames Wright **/
152*0c70a8bcSJames Wright PetscErrorCode HoneeSetContainer(Honee honee, const char key[], void *container, PetscCtxDestroyFn *container_destroy) {
153*0c70a8bcSJames Wright   PetscFunctionBeginUser;
154*0c70a8bcSJames Wright   {  // Verify that key is not already taken
155*0c70a8bcSJames Wright     void *test_data;
156*0c70a8bcSJames Wright     PetscCall(PetscObjectContainerQuery((PetscObject)honee, key, &test_data));
157*0c70a8bcSJames Wright     PetscCheck(test_data == NULL, PetscObjectComm((PetscObject)honee), PETSC_ERR_SUP, "Cannot set container with key '%s'; key is already in use.",
158*0c70a8bcSJames Wright                key);
159*0c70a8bcSJames Wright   }
160*0c70a8bcSJames Wright   PetscCall(PetscObjectContainerCompose((PetscObject)honee, key, container, container_destroy));
161*0c70a8bcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
162*0c70a8bcSJames Wright }
163*0c70a8bcSJames Wright 
164*0c70a8bcSJames Wright /**
165*0c70a8bcSJames Wright   @brief Retrieve a pointer-to-data in a `Honee` object by key
166*0c70a8bcSJames Wright 
167*0c70a8bcSJames Wright   This will error out if `honee` does not have a container identified by `key`.
168*0c70a8bcSJames Wright 
169*0c70a8bcSJames Wright   @param[in]  honee     `Honee` object to retrieve pointer from
170*0c70a8bcSJames Wright   @param[in]  key       String used to identify the pointer
171*0c70a8bcSJames Wright   @param[out] container Pointer to the data
172*0c70a8bcSJames Wright **/
173*0c70a8bcSJames Wright PetscErrorCode HoneeGetContainer(Honee honee, const char key[], void *container) {
174*0c70a8bcSJames Wright   PetscFunctionBeginUser;
175*0c70a8bcSJames Wright   PetscCall(PetscObjectContainerQuery((PetscObject)honee, key, container));
176*0c70a8bcSJames Wright   PetscCheck(*(void **)container != NULL, PetscObjectComm((PetscObject)honee), PETSC_ERR_SUP, "Container with key '%s' not found.", key);
177*0c70a8bcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
178*0c70a8bcSJames Wright }
179*0c70a8bcSJames Wright 
180*0c70a8bcSJames Wright /**
181*0c70a8bcSJames Wright   @brief Check if `Honee` object as pointer-to-data identified by key
182*0c70a8bcSJames Wright 
183*0c70a8bcSJames Wright   @param[in]  honee   `Honee` object to query for key
184*0c70a8bcSJames Wright   @param[in]  key     String to search for
185*0c70a8bcSJames Wright   @param[out] has_key Whether key has been set
186*0c70a8bcSJames Wright **/
187*0c70a8bcSJames Wright PetscErrorCode HoneeHasContainer(Honee honee, const char key[], PetscBool *has_key) {
188*0c70a8bcSJames Wright   void *test_data;
189*0c70a8bcSJames Wright 
190*0c70a8bcSJames Wright   PetscFunctionBeginUser;
191*0c70a8bcSJames Wright   PetscCall(PetscObjectContainerQuery((PetscObject)honee, key, &test_data));
192*0c70a8bcSJames Wright   *has_key = test_data == NULL ? PETSC_FALSE : PETSC_TRUE;
193*0c70a8bcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
194*0c70a8bcSJames Wright }
195