xref: /honee/src/honee.c (revision 6a9fb8efc46f8e3b96a05aa8c306a200675fccd6)
1*6a9fb8efSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2*6a9fb8efSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3*6a9fb8efSJames Wright 
4*6a9fb8efSJames Wright #include <navierstokes.h>
5*6a9fb8efSJames Wright 
6*6a9fb8efSJames Wright PetscClassId HONEE_CLASSID;
7*6a9fb8efSJames Wright 
8*6a9fb8efSJames Wright // @brief Initalize `HONEE` class.
9*6a9fb8efSJames Wright static PetscErrorCode HoneeInitClass_Private() {
10*6a9fb8efSJames Wright   static PetscBool registered = PETSC_FALSE;
11*6a9fb8efSJames Wright 
12*6a9fb8efSJames Wright   PetscFunctionBeginUser;
13*6a9fb8efSJames Wright   if (registered) PetscFunctionReturn(PETSC_SUCCESS);
14*6a9fb8efSJames Wright   PetscCall(PetscClassIdRegister("Honee", &HONEE_CLASSID));
15*6a9fb8efSJames Wright   registered = PETSC_TRUE;
16*6a9fb8efSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
17*6a9fb8efSJames Wright }
18*6a9fb8efSJames Wright 
19*6a9fb8efSJames Wright /**
20*6a9fb8efSJames Wright   @brief Initialize `Honee` object
21*6a9fb8efSJames Wright 
22*6a9fb8efSJames Wright   @param[in]  comm  `MPI_Comm` for the object
23*6a9fb8efSJames Wright   @param[out] honee The initialized `Honee` object
24*6a9fb8efSJames Wright **/
25*6a9fb8efSJames Wright PetscErrorCode HoneeInit(MPI_Comm comm, Honee *honee) {
26*6a9fb8efSJames Wright   Honee       honee_;
27*6a9fb8efSJames Wright   AppCtx      app_ctx;
28*6a9fb8efSJames Wright   ProblemData problem;
29*6a9fb8efSJames Wright   Physics     phys_ctx;
30*6a9fb8efSJames Wright   Units       units;
31*6a9fb8efSJames Wright 
32*6a9fb8efSJames Wright   PetscFunctionBeginUser;
33*6a9fb8efSJames Wright   PetscCall(HoneeInitClass_Private());
34*6a9fb8efSJames Wright   PetscCall(PetscHeaderCreate(honee_, HONEE_CLASSID, "Honee", "HONEE", "Honee", comm, HoneeDestroy, NULL));
35*6a9fb8efSJames Wright   PetscCall(PetscNew(&app_ctx));
36*6a9fb8efSJames Wright   PetscCall(PetscNew(&problem));
37*6a9fb8efSJames Wright   PetscCall(PetscNew(&phys_ctx));
38*6a9fb8efSJames Wright   PetscCall(PetscNew(&units));
39*6a9fb8efSJames Wright   honee_->app_ctx          = app_ctx;
40*6a9fb8efSJames Wright   honee_->units            = units;
41*6a9fb8efSJames Wright   honee_->phys             = phys_ctx;
42*6a9fb8efSJames Wright   honee_->problem_data     = problem;
43*6a9fb8efSJames Wright   problem->set_bc_from_ics = PETSC_TRUE;
44*6a9fb8efSJames Wright   honee_->comm             = PETSC_COMM_WORLD;
45*6a9fb8efSJames Wright   honee_->start_time       = time(NULL);
46*6a9fb8efSJames Wright 
47*6a9fb8efSJames Wright   PetscCall(RegisterLogEvents());
48*6a9fb8efSJames Wright 
49*6a9fb8efSJames Wright   *honee = honee_;
50*6a9fb8efSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
51*6a9fb8efSJames Wright }
52*6a9fb8efSJames Wright 
53*6a9fb8efSJames Wright /**
54*6a9fb8efSJames Wright   @brief Destroy `Honee` object
55*6a9fb8efSJames Wright 
56*6a9fb8efSJames Wright   @param[in] honee Object to be destroyed
57*6a9fb8efSJames Wright **/
58*6a9fb8efSJames Wright PetscErrorCode HoneeDestroy(Honee *honee) {
59*6a9fb8efSJames Wright   Honee honee_ = *honee;
60*6a9fb8efSJames Wright   Ceed  ceed;
61*6a9fb8efSJames Wright 
62*6a9fb8efSJames Wright   PetscFunctionBeginUser;
63*6a9fb8efSJames Wright   if (!honee_) PetscFunctionReturn(PETSC_SUCCESS);
64*6a9fb8efSJames Wright   PetscValidHeaderSpecific(honee_, HONEE_CLASSID, 1);
65*6a9fb8efSJames Wright 
66*6a9fb8efSJames Wright   ceed                = honee_->ceed;
67*6a9fb8efSJames Wright   MPI_Comm    comm    = honee_->comm;
68*6a9fb8efSJames Wright   AppCtx      app_ctx = honee_->app_ctx;
69*6a9fb8efSJames Wright   ProblemData problem = honee_->problem_data;
70*6a9fb8efSJames Wright 
71*6a9fb8efSJames Wright   PetscCall(NodalProjectionDataDestroy(honee_->grad_velo_proj));
72*6a9fb8efSJames Wright   PetscCall(SgsDDDataDestroy(honee_->sgs_dd_data));
73*6a9fb8efSJames Wright   PetscCall(DifferentialFilterDataDestroy(honee_->diff_filter));
74*6a9fb8efSJames Wright   PetscCall(SGS_DD_TrainingDataDestroy(honee_->sgs_dd_train));
75*6a9fb8efSJames Wright   PetscCall(SmartSimDataDestroy(honee_->smartsim));
76*6a9fb8efSJames Wright   PetscCall(QDataClearStoredData());
77*6a9fb8efSJames Wright   PetscCall(DivDiffFluxProjectionDataDestroy(honee_->diff_flux_proj));
78*6a9fb8efSJames Wright 
79*6a9fb8efSJames Wright   // -- Vectors
80*6a9fb8efSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&honee_->x_coord));
81*6a9fb8efSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&honee_->q_ceed));
82*6a9fb8efSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&honee_->q_dot_ceed));
83*6a9fb8efSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&honee_->g_ceed));
84*6a9fb8efSJames Wright 
85*6a9fb8efSJames Wright   // -- Bases
86*6a9fb8efSJames Wright   PetscCallCeed(ceed, CeedBasisDestroy(&honee_->basis_q));
87*6a9fb8efSJames Wright   PetscCallCeed(ceed, CeedBasisDestroy(&honee_->basis_x));
88*6a9fb8efSJames Wright 
89*6a9fb8efSJames Wright   // -- Restrictions
90*6a9fb8efSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&honee_->elem_restr_q));
91*6a9fb8efSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&honee_->elem_restr_x));
92*6a9fb8efSJames Wright 
93*6a9fb8efSJames Wright   // Destroy QFunction contexts after using
94*6a9fb8efSJames Wright   {
95*6a9fb8efSJames Wright     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->ics.qfctx));
96*6a9fb8efSJames Wright     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->apply_vol_rhs.qfctx));
97*6a9fb8efSJames Wright     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->apply_vol_ifunction.qfctx));
98*6a9fb8efSJames Wright     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&problem->apply_vol_ijacobian.qfctx));
99*6a9fb8efSJames Wright   }
100*6a9fb8efSJames Wright 
101*6a9fb8efSJames Wright   // -- Operators
102*6a9fb8efSJames Wright   PetscCall(OperatorApplyContextDestroy(honee_->op_ics_ctx));
103*6a9fb8efSJames Wright   PetscCall(OperatorApplyContextDestroy(honee_->op_rhs_ctx));
104*6a9fb8efSJames Wright   PetscCall(OperatorApplyContextDestroy(honee_->op_strong_bc_ctx));
105*6a9fb8efSJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&honee_->op_ifunction));
106*6a9fb8efSJames Wright 
107*6a9fb8efSJames Wright   // -- Ceed
108*6a9fb8efSJames Wright   PetscCheck(CeedDestroy(&ceed) == CEED_ERROR_SUCCESS, comm, PETSC_ERR_LIB, "Destroying Ceed object failed");
109*6a9fb8efSJames Wright 
110*6a9fb8efSJames Wright   // ---------------------------------------------------------------------------
111*6a9fb8efSJames Wright   // Clean up PETSc
112*6a9fb8efSJames Wright   // ---------------------------------------------------------------------------
113*6a9fb8efSJames Wright   // -- Vectors
114*6a9fb8efSJames Wright   PetscCall(VecDestroy(&honee_->Q_loc));
115*6a9fb8efSJames Wright   PetscCall(VecDestroy(&honee_->Q_dot_loc));
116*6a9fb8efSJames Wright 
117*6a9fb8efSJames Wright   PetscCall(KSPDestroy(&honee_->mass_ksp));
118*6a9fb8efSJames Wright 
119*6a9fb8efSJames Wright   // -- Matrices
120*6a9fb8efSJames Wright   PetscCall(MatDestroy(&honee_->interp_viz));
121*6a9fb8efSJames Wright   PetscCall(MatDestroy(&honee_->mat_ijacobian));
122*6a9fb8efSJames Wright 
123*6a9fb8efSJames Wright   // -- DM
124*6a9fb8efSJames Wright   PetscCall(DMDestroy(&honee_->dm_viz));
125*6a9fb8efSJames Wright 
126*6a9fb8efSJames Wright   // -- Function list
127*6a9fb8efSJames Wright   PetscCall(PetscFunctionListDestroy(&app_ctx->problems));
128*6a9fb8efSJames Wright 
129*6a9fb8efSJames Wright   PetscCall(PetscFree(app_ctx->amat_type));
130*6a9fb8efSJames Wright   PetscCall(PetscFree(app_ctx->wall_forces.walls));
131*6a9fb8efSJames Wright   PetscCall(PetscViewerDestroy(&app_ctx->wall_forces.viewer));
132*6a9fb8efSJames Wright 
133*6a9fb8efSJames Wright   // -- Structs
134*6a9fb8efSJames Wright   for (PetscInt i = 0; i < problem->num_bc_defs; i++) {
135*6a9fb8efSJames Wright     PetscCall(BCDefinitionDestroy(&problem->bc_defs[i]));
136*6a9fb8efSJames Wright   }
137*6a9fb8efSJames Wright   PetscCall(PetscFree(problem->bc_defs));
138*6a9fb8efSJames Wright   PetscCall(PetscFree(honee_->units));
139*6a9fb8efSJames Wright   PetscCall(PetscFree(problem));
140*6a9fb8efSJames Wright   PetscCall(PetscFree(honee_->phys));
141*6a9fb8efSJames Wright   PetscCall(PetscFree(app_ctx));
142*6a9fb8efSJames Wright   PetscCall(PetscHeaderDestroy(&honee_));
143*6a9fb8efSJames Wright   *honee = NULL;
144*6a9fb8efSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
145*6a9fb8efSJames Wright }
146