xref: /honee/src/honee.c (revision ca10df36c62de063b40b5cdaf686b7aaa41d81d6)
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 
286a9fb8efSJames Wright   PetscFunctionBeginUser;
296a9fb8efSJames Wright   PetscCall(HoneeInitClass_Private());
306a9fb8efSJames Wright   PetscCall(PetscHeaderCreate(honee_, HONEE_CLASSID, "Honee", "HONEE", "Honee", comm, HoneeDestroy, NULL));
31*ca10df36SJames Wright   PetscCall(PetscCalloc4(1, &honee_->app_ctx, 1, &honee_->problem_data, 1, &honee_->phys, 1, &honee_->units));
32*ca10df36SJames Wright   honee_->problem_data->set_bc_from_ics = PETSC_TRUE;
336a9fb8efSJames Wright   honee_->comm                          = PETSC_COMM_WORLD;
346a9fb8efSJames Wright   honee_->start_time                    = time(NULL);
356a9fb8efSJames Wright   PetscCall(RegisterLogEvents());
366a9fb8efSJames Wright   *honee = honee_;
376a9fb8efSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
386a9fb8efSJames Wright }
396a9fb8efSJames Wright 
406a9fb8efSJames Wright /**
416a9fb8efSJames Wright   @brief Destroy `Honee` object
426a9fb8efSJames Wright 
436a9fb8efSJames Wright   @param[in] honee Object to be destroyed
446a9fb8efSJames Wright **/
456a9fb8efSJames Wright PetscErrorCode HoneeDestroy(Honee *honee) {
466a9fb8efSJames Wright   Honee honee_ = *honee;
476a9fb8efSJames Wright   Ceed  ceed;
486a9fb8efSJames Wright 
496a9fb8efSJames Wright   PetscFunctionBeginUser;
506a9fb8efSJames Wright   if (!honee_) PetscFunctionReturn(PETSC_SUCCESS);
516a9fb8efSJames Wright   PetscValidHeaderSpecific(honee_, HONEE_CLASSID, 1);
526a9fb8efSJames Wright 
536a9fb8efSJames Wright   ceed                = honee_->ceed;
546a9fb8efSJames Wright   MPI_Comm    comm    = honee_->comm;
556a9fb8efSJames Wright   AppCtx      app_ctx = honee_->app_ctx;
566a9fb8efSJames Wright   ProblemData problem = honee_->problem_data;
576a9fb8efSJames Wright 
586a9fb8efSJames Wright   PetscCall(QDataClearStoredData());
596a9fb8efSJames Wright   PetscCall(DivDiffFluxProjectionDataDestroy(honee_->diff_flux_proj));
606a9fb8efSJames Wright 
616a9fb8efSJames Wright   // -- Vectors
626a9fb8efSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&honee_->x_coord));
636a9fb8efSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&honee_->q_ceed));
646a9fb8efSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&honee_->q_dot_ceed));
656a9fb8efSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&honee_->g_ceed));
666a9fb8efSJames Wright 
676a9fb8efSJames Wright   // -- Bases
686a9fb8efSJames Wright   PetscCallCeed(ceed, CeedBasisDestroy(&honee_->basis_q));
696a9fb8efSJames Wright   PetscCallCeed(ceed, CeedBasisDestroy(&honee_->basis_x));
706a9fb8efSJames Wright 
716a9fb8efSJames Wright   // -- Restrictions
726a9fb8efSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&honee_->elem_restr_q));
736a9fb8efSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&honee_->elem_restr_x));
746a9fb8efSJames Wright 
756a9fb8efSJames Wright   // Destroy QFunction contexts after using
766a9fb8efSJames Wright   {
776a9fb8efSJames Wright     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->ics.qfctx));
786a9fb8efSJames Wright     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->apply_vol_rhs.qfctx));
796a9fb8efSJames Wright     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->apply_vol_ifunction.qfctx));
806a9fb8efSJames Wright     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->apply_vol_ijacobian.qfctx));
816a9fb8efSJames Wright   }
826a9fb8efSJames Wright 
836a9fb8efSJames Wright   // -- Operators
846a9fb8efSJames Wright   PetscCall(OperatorApplyContextDestroy(honee_->op_ics_ctx));
856a9fb8efSJames Wright   PetscCall(OperatorApplyContextDestroy(honee_->op_rhs_ctx));
866a9fb8efSJames Wright   PetscCall(OperatorApplyContextDestroy(honee_->op_strong_bc_ctx));
876a9fb8efSJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&honee_->op_ifunction));
886a9fb8efSJames Wright 
896a9fb8efSJames Wright   // -- Ceed
906a9fb8efSJames Wright   PetscCheck(CeedDestroy(&ceed) == CEED_ERROR_SUCCESS, comm, PETSC_ERR_LIB, "Destroying Ceed object failed");
916a9fb8efSJames Wright 
926a9fb8efSJames Wright   // ---------------------------------------------------------------------------
936a9fb8efSJames Wright   // Clean up PETSc
946a9fb8efSJames Wright   // ---------------------------------------------------------------------------
956a9fb8efSJames Wright   // -- Vectors
966a9fb8efSJames Wright   PetscCall(VecDestroy(&honee_->Q_loc));
976a9fb8efSJames Wright   PetscCall(VecDestroy(&honee_->Q_dot_loc));
986a9fb8efSJames Wright 
996a9fb8efSJames Wright   PetscCall(KSPDestroy(&honee_->mass_ksp));
1006a9fb8efSJames Wright 
1016a9fb8efSJames Wright   // -- Matrices
1026a9fb8efSJames Wright   PetscCall(MatDestroy(&honee_->interp_viz));
1036a9fb8efSJames Wright   PetscCall(MatDestroy(&honee_->mat_ijacobian));
1046a9fb8efSJames Wright 
1056a9fb8efSJames Wright   // -- DM
1066a9fb8efSJames Wright   PetscCall(DMDestroy(&honee_->dm_viz));
1076a9fb8efSJames Wright 
1086a9fb8efSJames Wright   // -- Function list
1096a9fb8efSJames Wright   PetscCall(PetscFunctionListDestroy(&app_ctx->problems));
1106a9fb8efSJames Wright 
1116a9fb8efSJames Wright   PetscCall(PetscFree(app_ctx->amat_type));
1126a9fb8efSJames Wright   PetscCall(PetscFree(app_ctx->wall_forces.walls));
1136a9fb8efSJames Wright   PetscCall(PetscViewerDestroy(&app_ctx->wall_forces.viewer));
1146a9fb8efSJames Wright 
1156a9fb8efSJames Wright   // -- Structs
1166a9fb8efSJames Wright   for (PetscInt i = 0; i < problem->num_bc_defs; i++) {
1176a9fb8efSJames Wright     PetscCall(BCDefinitionDestroy(&problem->bc_defs[i]));
1186a9fb8efSJames Wright   }
1196a9fb8efSJames Wright   PetscCall(PetscFree(problem->bc_defs));
120f27c2204SJames Wright   for (PetscInt i = 0; i < problem->num_components; i++) {
121f27c2204SJames Wright     PetscCall(PetscFree(problem->component_names[i]));
122f27c2204SJames Wright   }
123f27c2204SJames Wright   PetscCall(PetscFree(problem->component_names));
124f27c2204SJames Wright 
125*ca10df36SJames Wright   PetscCall(PetscFree4(honee_->app_ctx, honee_->problem_data, honee_->phys, honee_->units));
1266a9fb8efSJames Wright   PetscCall(PetscHeaderDestroy(&honee_));
1276a9fb8efSJames Wright   *honee = NULL;
1286a9fb8efSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
1296a9fb8efSJames Wright }
1300c70a8bcSJames Wright 
1310c70a8bcSJames Wright /**
1320c70a8bcSJames Wright   @brief Saves a pointer-to-data in a `Honee` object by key
1330c70a8bcSJames Wright 
1340c70a8bcSJames Wright   This also checks whether `key` has been used previously and will error out if it has.
1350c70a8bcSJames Wright 
1360c70a8bcSJames Wright   @param[in] honee             `Honee` object to save pointer to
1370c70a8bcSJames Wright   @param[in] key               String used to identify the pointer
1380c70a8bcSJames Wright   @param[in] container         Pointer to the data
1390c70a8bcSJames Wright   @param[in] container_destroy Function to destroy the struct after it is used
1400c70a8bcSJames Wright **/
1410c70a8bcSJames Wright PetscErrorCode HoneeSetContainer(Honee honee, const char key[], void *container, PetscCtxDestroyFn *container_destroy) {
1420c70a8bcSJames Wright   PetscFunctionBeginUser;
1430c70a8bcSJames Wright   {  // Verify that key is not already taken
1440c70a8bcSJames Wright     void *test_data;
1450c70a8bcSJames Wright     PetscCall(PetscObjectContainerQuery((PetscObject)honee, key, &test_data));
1460c70a8bcSJames Wright     PetscCheck(test_data == NULL, PetscObjectComm((PetscObject)honee), PETSC_ERR_SUP, "Cannot set container with key '%s'; key is already in use.",
1470c70a8bcSJames Wright                key);
1480c70a8bcSJames Wright   }
1490c70a8bcSJames Wright   PetscCall(PetscObjectContainerCompose((PetscObject)honee, key, container, container_destroy));
1500c70a8bcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
1510c70a8bcSJames Wright }
1520c70a8bcSJames Wright 
1530c70a8bcSJames Wright /**
1540c70a8bcSJames Wright   @brief Retrieve a pointer-to-data in a `Honee` object by key
1550c70a8bcSJames Wright 
1560c70a8bcSJames Wright   This will error out if `honee` does not have a container identified by `key`.
1570c70a8bcSJames Wright 
1580c70a8bcSJames Wright   @param[in]  honee     `Honee` object to retrieve pointer from
1590c70a8bcSJames Wright   @param[in]  key       String used to identify the pointer
1600c70a8bcSJames Wright   @param[out] container Pointer to the data
1610c70a8bcSJames Wright **/
1620c70a8bcSJames Wright PetscErrorCode HoneeGetContainer(Honee honee, const char key[], void *container) {
1630c70a8bcSJames Wright   PetscFunctionBeginUser;
1640c70a8bcSJames Wright   PetscCall(PetscObjectContainerQuery((PetscObject)honee, key, container));
1650c70a8bcSJames Wright   PetscCheck(*(void **)container != NULL, PetscObjectComm((PetscObject)honee), PETSC_ERR_SUP, "Container with key '%s' not found.", key);
1660c70a8bcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
1670c70a8bcSJames Wright }
1680c70a8bcSJames Wright 
1690c70a8bcSJames Wright /**
1700c70a8bcSJames Wright   @brief Check if `Honee` object as pointer-to-data identified by key
1710c70a8bcSJames Wright 
1720c70a8bcSJames Wright   @param[in]  honee   `Honee` object to query for key
1730c70a8bcSJames Wright   @param[in]  key     String to search for
1740c70a8bcSJames Wright   @param[out] has_key Whether key has been set
1750c70a8bcSJames Wright **/
1760c70a8bcSJames Wright PetscErrorCode HoneeHasContainer(Honee honee, const char key[], PetscBool *has_key) {
1770c70a8bcSJames Wright   void *test_data;
1780c70a8bcSJames Wright 
1790c70a8bcSJames Wright   PetscFunctionBeginUser;
1800c70a8bcSJames Wright   PetscCall(PetscObjectContainerQuery((PetscObject)honee, key, &test_data));
1810c70a8bcSJames Wright   *has_key = test_data == NULL ? PETSC_FALSE : PETSC_TRUE;
1820c70a8bcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
1830c70a8bcSJames Wright }
184