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.
HoneeInitClass_Private()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 **/
HoneeInit(MPI_Comm comm,Honee * honee)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));
31ca10df36SJames Wright PetscCall(PetscCalloc4(1, &honee_->app_ctx, 1, &honee_->problem_data, 1, &honee_->phys, 1, &honee_->units));
32ca10df36SJames 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 **/
HoneeDestroy(Honee * honee)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_->q_ceed));
636a9fb8efSJames Wright PetscCallCeed(ceed, CeedVectorDestroy(&honee_->q_dot_ceed));
646a9fb8efSJames Wright PetscCallCeed(ceed, CeedVectorDestroy(&honee_->g_ceed));
656a9fb8efSJames Wright
666a9fb8efSJames Wright // Destroy QFunction contexts after using
676a9fb8efSJames Wright {
686a9fb8efSJames Wright PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->ics.qfctx));
696a9fb8efSJames Wright PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->apply_vol_rhs.qfctx));
706a9fb8efSJames Wright PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->apply_vol_ifunction.qfctx));
716a9fb8efSJames Wright PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->apply_vol_ijacobian.qfctx));
726a9fb8efSJames Wright }
736a9fb8efSJames Wright
746a9fb8efSJames Wright // -- Operators
756a9fb8efSJames Wright PetscCall(OperatorApplyContextDestroy(honee_->op_ics_ctx));
766a9fb8efSJames Wright PetscCall(OperatorApplyContextDestroy(honee_->op_rhs_ctx));
776a9fb8efSJames Wright PetscCall(OperatorApplyContextDestroy(honee_->op_strong_bc_ctx));
786a9fb8efSJames Wright PetscCallCeed(ceed, CeedOperatorDestroy(&honee_->op_ifunction));
796a9fb8efSJames Wright
806a9fb8efSJames Wright // -- Ceed
816a9fb8efSJames Wright PetscCheck(CeedDestroy(&ceed) == CEED_ERROR_SUCCESS, comm, PETSC_ERR_LIB, "Destroying Ceed object failed");
826a9fb8efSJames Wright
836a9fb8efSJames Wright // ---------------------------------------------------------------------------
846a9fb8efSJames Wright // Clean up PETSc
856a9fb8efSJames Wright // ---------------------------------------------------------------------------
866a9fb8efSJames Wright // -- Vectors
876a9fb8efSJames Wright PetscCall(VecDestroy(&honee_->Q_loc));
886a9fb8efSJames Wright PetscCall(VecDestroy(&honee_->Q_dot_loc));
896a9fb8efSJames Wright
906a9fb8efSJames Wright PetscCall(KSPDestroy(&honee_->mass_ksp));
916a9fb8efSJames Wright
926a9fb8efSJames Wright // -- Matrices
936a9fb8efSJames Wright PetscCall(MatDestroy(&honee_->interp_viz));
946a9fb8efSJames Wright PetscCall(MatDestroy(&honee_->mat_ijacobian));
956a9fb8efSJames Wright
966a9fb8efSJames Wright // -- DM
976a9fb8efSJames Wright PetscCall(DMDestroy(&honee_->dm_viz));
986a9fb8efSJames Wright
996a9fb8efSJames Wright // -- Function list
1006a9fb8efSJames Wright PetscCall(PetscFunctionListDestroy(&app_ctx->problems));
1016a9fb8efSJames Wright
1026a9fb8efSJames Wright PetscCall(PetscFree(app_ctx->amat_type));
1036a9fb8efSJames Wright PetscCall(PetscFree(app_ctx->wall_forces.walls));
1046a9fb8efSJames Wright PetscCall(PetscViewerDestroy(&app_ctx->wall_forces.viewer));
1056a9fb8efSJames Wright
1066a9fb8efSJames Wright // -- Structs
1076a9fb8efSJames Wright for (PetscInt i = 0; i < problem->num_bc_defs; i++) {
1086a9fb8efSJames Wright PetscCall(BCDefinitionDestroy(&problem->bc_defs[i]));
1096a9fb8efSJames Wright }
1106a9fb8efSJames Wright PetscCall(PetscFree(problem->bc_defs));
111f27c2204SJames Wright for (PetscInt i = 0; i < problem->num_components; i++) {
112f27c2204SJames Wright PetscCall(PetscFree(problem->component_names[i]));
113f27c2204SJames Wright }
114f27c2204SJames Wright PetscCall(PetscFree(problem->component_names));
115f27c2204SJames Wright
116ca10df36SJames Wright PetscCall(PetscFree4(honee_->app_ctx, honee_->problem_data, honee_->phys, honee_->units));
1176a9fb8efSJames Wright PetscCall(PetscHeaderDestroy(&honee_));
1186a9fb8efSJames Wright *honee = NULL;
1196a9fb8efSJames Wright PetscFunctionReturn(PETSC_SUCCESS);
1206a9fb8efSJames Wright }
1210c70a8bcSJames Wright
1220c70a8bcSJames Wright /**
1230c70a8bcSJames Wright @brief Saves a pointer-to-data in a `Honee` object by key
1240c70a8bcSJames Wright
1250c70a8bcSJames Wright This also checks whether `key` has been used previously and will error out if it has.
1260c70a8bcSJames Wright
1270c70a8bcSJames Wright @param[in] honee `Honee` object to save pointer to
1280c70a8bcSJames Wright @param[in] key String used to identify the pointer
1290c70a8bcSJames Wright @param[in] container Pointer to the data
1300c70a8bcSJames Wright @param[in] container_destroy Function to destroy the struct after it is used
1310c70a8bcSJames Wright **/
HoneeSetContainer(Honee honee,const char key[],void * container,PetscCtxDestroyFn * container_destroy)1320c70a8bcSJames Wright PetscErrorCode HoneeSetContainer(Honee honee, const char key[], void *container, PetscCtxDestroyFn *container_destroy) {
1330c70a8bcSJames Wright PetscFunctionBeginUser;
1340c70a8bcSJames Wright { // Verify that key is not already taken
1350c70a8bcSJames Wright void *test_data;
1360c70a8bcSJames Wright PetscCall(PetscObjectContainerQuery((PetscObject)honee, key, &test_data));
1370c70a8bcSJames Wright PetscCheck(test_data == NULL, PetscObjectComm((PetscObject)honee), PETSC_ERR_SUP, "Cannot set container with key '%s'; key is already in use.",
1380c70a8bcSJames Wright key);
1390c70a8bcSJames Wright }
1400c70a8bcSJames Wright PetscCall(PetscObjectContainerCompose((PetscObject)honee, key, container, container_destroy));
1410c70a8bcSJames Wright PetscFunctionReturn(PETSC_SUCCESS);
1420c70a8bcSJames Wright }
1430c70a8bcSJames Wright
1440c70a8bcSJames Wright /**
1450c70a8bcSJames Wright @brief Retrieve a pointer-to-data in a `Honee` object by key
1460c70a8bcSJames Wright
1470c70a8bcSJames Wright This will error out if `honee` does not have a container identified by `key`.
1480c70a8bcSJames Wright
1490c70a8bcSJames Wright @param[in] honee `Honee` object to retrieve pointer from
1500c70a8bcSJames Wright @param[in] key String used to identify the pointer
1510c70a8bcSJames Wright @param[out] container Pointer to the data
1520c70a8bcSJames Wright **/
HoneeGetContainer(Honee honee,const char key[],void * container)1530c70a8bcSJames Wright PetscErrorCode HoneeGetContainer(Honee honee, const char key[], void *container) {
1540c70a8bcSJames Wright PetscFunctionBeginUser;
1550c70a8bcSJames Wright PetscCall(PetscObjectContainerQuery((PetscObject)honee, key, container));
1560c70a8bcSJames Wright PetscCheck(*(void **)container != NULL, PetscObjectComm((PetscObject)honee), PETSC_ERR_SUP, "Container with key '%s' not found.", key);
1570c70a8bcSJames Wright PetscFunctionReturn(PETSC_SUCCESS);
1580c70a8bcSJames Wright }
1590c70a8bcSJames Wright
1600c70a8bcSJames Wright /**
1610c70a8bcSJames Wright @brief Check if `Honee` object as pointer-to-data identified by key
1620c70a8bcSJames Wright
1630c70a8bcSJames Wright @param[in] honee `Honee` object to query for key
1640c70a8bcSJames Wright @param[in] key String to search for
1650c70a8bcSJames Wright @param[out] has_key Whether key has been set
1660c70a8bcSJames Wright **/
HoneeHasContainer(Honee honee,const char key[],PetscBool * has_key)1670c70a8bcSJames Wright PetscErrorCode HoneeHasContainer(Honee honee, const char key[], PetscBool *has_key) {
1680c70a8bcSJames Wright void *test_data;
1690c70a8bcSJames Wright
1700c70a8bcSJames Wright PetscFunctionBeginUser;
1710c70a8bcSJames Wright PetscCall(PetscObjectContainerQuery((PetscObject)honee, key, &test_data));
1720c70a8bcSJames Wright *has_key = test_data == NULL ? PETSC_FALSE : PETSC_TRUE;
1730c70a8bcSJames Wright PetscFunctionReturn(PETSC_SUCCESS);
1740c70a8bcSJames Wright }
175*e3db12f8SJames Wright
176*e3db12f8SJames Wright const DMLabel DMLABEL_DEFAULT = NULL;
177*e3db12f8SJames Wright const PetscInt DMLABEL_DEFAULT_VALUE = 0;
178