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)); 133*f27c2204SJames Wright for (PetscInt i = 0; i < problem->num_components; i++) { 134*f27c2204SJames Wright PetscCall(PetscFree(problem->component_names[i])); 135*f27c2204SJames Wright } 136*f27c2204SJames Wright PetscCall(PetscFree(problem->component_names)); 1376a9fb8efSJames Wright PetscCall(PetscFree(problem)); 138*f27c2204SJames Wright PetscCall(PetscFree(honee_->units)); 1396a9fb8efSJames Wright PetscCall(PetscFree(honee_->phys)); 1406a9fb8efSJames Wright PetscCall(PetscFree(app_ctx)); 141*f27c2204SJames Wright 1426a9fb8efSJames Wright PetscCall(PetscHeaderDestroy(&honee_)); 1436a9fb8efSJames Wright *honee = NULL; 1446a9fb8efSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 1456a9fb8efSJames Wright } 1460c70a8bcSJames Wright 1470c70a8bcSJames Wright /** 1480c70a8bcSJames Wright @brief Saves a pointer-to-data in a `Honee` object by key 1490c70a8bcSJames Wright 1500c70a8bcSJames Wright This also checks whether `key` has been used previously and will error out if it has. 1510c70a8bcSJames Wright 1520c70a8bcSJames Wright @param[in] honee `Honee` object to save pointer to 1530c70a8bcSJames Wright @param[in] key String used to identify the pointer 1540c70a8bcSJames Wright @param[in] container Pointer to the data 1550c70a8bcSJames Wright @param[in] container_destroy Function to destroy the struct after it is used 1560c70a8bcSJames Wright **/ 1570c70a8bcSJames Wright PetscErrorCode HoneeSetContainer(Honee honee, const char key[], void *container, PetscCtxDestroyFn *container_destroy) { 1580c70a8bcSJames Wright PetscFunctionBeginUser; 1590c70a8bcSJames Wright { // Verify that key is not already taken 1600c70a8bcSJames Wright void *test_data; 1610c70a8bcSJames Wright PetscCall(PetscObjectContainerQuery((PetscObject)honee, key, &test_data)); 1620c70a8bcSJames Wright PetscCheck(test_data == NULL, PetscObjectComm((PetscObject)honee), PETSC_ERR_SUP, "Cannot set container with key '%s'; key is already in use.", 1630c70a8bcSJames Wright key); 1640c70a8bcSJames Wright } 1650c70a8bcSJames Wright PetscCall(PetscObjectContainerCompose((PetscObject)honee, key, container, container_destroy)); 1660c70a8bcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 1670c70a8bcSJames Wright } 1680c70a8bcSJames Wright 1690c70a8bcSJames Wright /** 1700c70a8bcSJames Wright @brief Retrieve a pointer-to-data in a `Honee` object by key 1710c70a8bcSJames Wright 1720c70a8bcSJames Wright This will error out if `honee` does not have a container identified by `key`. 1730c70a8bcSJames Wright 1740c70a8bcSJames Wright @param[in] honee `Honee` object to retrieve pointer from 1750c70a8bcSJames Wright @param[in] key String used to identify the pointer 1760c70a8bcSJames Wright @param[out] container Pointer to the data 1770c70a8bcSJames Wright **/ 1780c70a8bcSJames Wright PetscErrorCode HoneeGetContainer(Honee honee, const char key[], void *container) { 1790c70a8bcSJames Wright PetscFunctionBeginUser; 1800c70a8bcSJames Wright PetscCall(PetscObjectContainerQuery((PetscObject)honee, key, container)); 1810c70a8bcSJames Wright PetscCheck(*(void **)container != NULL, PetscObjectComm((PetscObject)honee), PETSC_ERR_SUP, "Container with key '%s' not found.", key); 1820c70a8bcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 1830c70a8bcSJames Wright } 1840c70a8bcSJames Wright 1850c70a8bcSJames Wright /** 1860c70a8bcSJames Wright @brief Check if `Honee` object as pointer-to-data identified by key 1870c70a8bcSJames Wright 1880c70a8bcSJames Wright @param[in] honee `Honee` object to query for key 1890c70a8bcSJames Wright @param[in] key String to search for 1900c70a8bcSJames Wright @param[out] has_key Whether key has been set 1910c70a8bcSJames Wright **/ 1920c70a8bcSJames Wright PetscErrorCode HoneeHasContainer(Honee honee, const char key[], PetscBool *has_key) { 1930c70a8bcSJames Wright void *test_data; 1940c70a8bcSJames Wright 1950c70a8bcSJames Wright PetscFunctionBeginUser; 1960c70a8bcSJames Wright PetscCall(PetscObjectContainerQuery((PetscObject)honee, key, &test_data)); 1970c70a8bcSJames Wright *has_key = test_data == NULL ? PETSC_FALSE : PETSC_TRUE; 1980c70a8bcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 1990c70a8bcSJames Wright } 200