xref: /honee/src/spanstats/cflpe.c (revision b30619f6337e4aeddfabbcf7d00859553914ef7b)
1*b30619f6SJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2*b30619f6SJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3*b30619f6SJames Wright /// @file
4*b30619f6SJames Wright /// Functions for setting up and performing spanwise-statistics collection of CFL and Peclet number
5*b30619f6SJames Wright 
6*b30619f6SJames Wright #include "../qfunctions/spanstats/cflpe.h"
7*b30619f6SJames Wright #include "../qfunctions/advection_types.h"
8*b30619f6SJames Wright 
9*b30619f6SJames Wright #include <ceed.h>
10*b30619f6SJames Wright #include <petscdmplex.h>
11*b30619f6SJames Wright #include <petscsf.h>
12*b30619f6SJames Wright #include <spanstats.h>
13*b30619f6SJames Wright 
14*b30619f6SJames Wright #include <navierstokes.h>
15*b30619f6SJames Wright #include <petsc_ops.h>
16*b30619f6SJames Wright 
17*b30619f6SJames Wright // @brief Create CeedOperator for statistics collection
18*b30619f6SJames Wright static PetscErrorCode CreateStatisticCollectionOperator(Honee honee, SpanStatsCtx spanstats, SpanStatsSetupData stats_data, ProblemData problem) {
19*b30619f6SJames Wright   Ceed                   ceed           = honee->ceed;
20*b30619f6SJames Wright   CeedInt                num_comp_stats = spanstats->num_comp_stats, num_comp_x, num_comp_q, q_data_size;
21*b30619f6SJames Wright   PetscInt               dim, label_value = 0;
22*b30619f6SJames Wright   CflPe_SpanStatsContext collect_ctx;
23*b30619f6SJames Wright   CeedQFunctionContext   collect_qfctx;
24*b30619f6SJames Wright   CeedQFunction          qf_stats_collect = NULL;
25*b30619f6SJames Wright   CeedOperator           op_stats_collect;
26*b30619f6SJames Wright   CeedVector             q_data;
27*b30619f6SJames Wright   CeedElemRestriction    elem_restr_qd;
28*b30619f6SJames Wright   DMLabel                domain_label = NULL;
29*b30619f6SJames Wright 
30*b30619f6SJames Wright   PetscFunctionBeginUser;
31*b30619f6SJames Wright   PetscCall(DMGetDimension(honee->dm, &dim));
32*b30619f6SJames Wright   num_comp_x = dim;
33*b30619f6SJames Wright   PetscCallCeed(ceed, CeedBasisGetNumComponents(honee->basis_q, &num_comp_q));
34*b30619f6SJames Wright 
35*b30619f6SJames Wright   // Create Operator for statistics collection
36*b30619f6SJames Wright   switch (dim) {
37*b30619f6SJames Wright     case 2:
38*b30619f6SJames Wright       switch (honee->phys->state_var) {
39*b30619f6SJames Wright         case STATEVAR_PRIMITIVE:
40*b30619f6SJames Wright           PetscCallCeed(ceed,
41*b30619f6SJames Wright                         CeedQFunctionCreateInterior(ceed, 1, ChildStatsCollection_2D_Prim, ChildStatsCollection_2D_Prim_loc, &qf_stats_collect));
42*b30619f6SJames Wright           break;
43*b30619f6SJames Wright         case STATEVAR_CONSERVATIVE:
44*b30619f6SJames Wright           PetscCallCeed(
45*b30619f6SJames Wright               ceed, CeedQFunctionCreateInterior(ceed, 1, ChildStatsCollection_2D_Conserv, ChildStatsCollection_2D_Conserv_loc, &qf_stats_collect));
46*b30619f6SJames Wright           break;
47*b30619f6SJames Wright         case STATEVAR_ENTROPY:
48*b30619f6SJames Wright           PetscCallCeed(
49*b30619f6SJames Wright               ceed, CeedQFunctionCreateInterior(ceed, 1, ChildStatsCollection_2D_Entropy, ChildStatsCollection_2D_Entropy_loc, &qf_stats_collect));
50*b30619f6SJames Wright           break;
51*b30619f6SJames Wright       }
52*b30619f6SJames Wright       break;
53*b30619f6SJames Wright     case 3:
54*b30619f6SJames Wright       switch (honee->phys->state_var) {
55*b30619f6SJames Wright         case STATEVAR_PRIMITIVE:
56*b30619f6SJames Wright           PetscCallCeed(ceed,
57*b30619f6SJames Wright                         CeedQFunctionCreateInterior(ceed, 1, ChildStatsCollection_3D_Prim, ChildStatsCollection_3D_Prim_loc, &qf_stats_collect));
58*b30619f6SJames Wright           break;
59*b30619f6SJames Wright         case STATEVAR_CONSERVATIVE:
60*b30619f6SJames Wright           PetscCallCeed(
61*b30619f6SJames Wright               ceed, CeedQFunctionCreateInterior(ceed, 1, ChildStatsCollection_3D_Conserv, ChildStatsCollection_3D_Conserv_loc, &qf_stats_collect));
62*b30619f6SJames Wright           break;
63*b30619f6SJames Wright         case STATEVAR_ENTROPY:
64*b30619f6SJames Wright           PetscCallCeed(
65*b30619f6SJames Wright               ceed, CeedQFunctionCreateInterior(ceed, 1, ChildStatsCollection_3D_Entropy, ChildStatsCollection_3D_Entropy_loc, &qf_stats_collect));
66*b30619f6SJames Wright           break;
67*b30619f6SJames Wright       }
68*b30619f6SJames Wright       break;
69*b30619f6SJames Wright   }
70*b30619f6SJames Wright   PetscCheck(qf_stats_collect, PetscObjectComm((PetscObject)honee->dm), PETSC_ERR_SUP,
71*b30619f6SJames Wright              "Could not create CFL/Pe spanstats collection QFunction for dim %" PetscInt_FMT " and state variable %s", dim,
72*b30619f6SJames Wright              StateVariables[honee->phys->state_var]);
73*b30619f6SJames Wright 
74*b30619f6SJames Wright   PetscCall(QDataGet(ceed, honee->dm, domain_label, label_value, honee->elem_restr_x, honee->basis_x, honee->x_coord, &elem_restr_qd, &q_data,
75*b30619f6SJames Wright                      &q_data_size));
76*b30619f6SJames Wright   {  // Setup Collection Context
77*b30619f6SJames Wright     PetscBool is_advection;
78*b30619f6SJames Wright 
79*b30619f6SJames Wright     PetscCall(PetscNew(&collect_ctx));
80*b30619f6SJames Wright     PetscCall(PetscStrcmp("advection", honee->app_ctx->problem_name, &is_advection));
81*b30619f6SJames Wright     if (is_advection) {
82*b30619f6SJames Wright       AdvectionContext                 advection_ctx;
83*b30619f6SJames Wright       struct NewtonianIdealGasContext_ gas_struct = {0};
84*b30619f6SJames Wright 
85*b30619f6SJames Wright       PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->apply_vol_rhs.qfctx, CEED_MEM_HOST, &advection_ctx));
86*b30619f6SJames Wright       collect_ctx->gas             = gas_struct;
87*b30619f6SJames Wright       collect_ctx->diffusion_coeff = advection_ctx->diffusion_coeff;
88*b30619f6SJames Wright       PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfctx, &advection_ctx));
89*b30619f6SJames Wright     } else {
90*b30619f6SJames Wright       NewtonianIdealGasContext newtonian_ig_ctx;
91*b30619f6SJames Wright 
92*b30619f6SJames Wright       PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->apply_vol_rhs.qfctx, CEED_MEM_HOST, &newtonian_ig_ctx));
93*b30619f6SJames Wright       collect_ctx->gas             = *newtonian_ig_ctx;
94*b30619f6SJames Wright       collect_ctx->diffusion_coeff = newtonian_ig_ctx->mu;
95*b30619f6SJames Wright       PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfctx, &newtonian_ig_ctx));
96*b30619f6SJames Wright     }
97*b30619f6SJames Wright 
98*b30619f6SJames Wright     PetscCallCeed(ceed, CeedQFunctionContextCreate(honee->ceed, &collect_qfctx));
99*b30619f6SJames Wright     PetscCallCeed(ceed, CeedQFunctionContextSetData(collect_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*collect_ctx), collect_ctx));
100*b30619f6SJames Wright     PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(collect_qfctx, CEED_MEM_HOST, FreeContextPetsc));
101*b30619f6SJames Wright 
102*b30619f6SJames Wright     PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(collect_qfctx, "solution time", offsetof(struct CflPe_SpanStatsContext_, solution_time), 1,
103*b30619f6SJames Wright                                                            "Current solution time"));
104*b30619f6SJames Wright     PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(collect_qfctx, "previous time", offsetof(struct CflPe_SpanStatsContext_, previous_time), 1,
105*b30619f6SJames Wright                                                            "Previous time statistics collection was done"));
106*b30619f6SJames Wright     PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(collect_qfctx, "timestep", offsetof(struct CflPe_SpanStatsContext_, timestep), 1,
107*b30619f6SJames Wright                                                            "Current timestep taken by TS"));
108*b30619f6SJames Wright   }
109*b30619f6SJames Wright 
110*b30619f6SJames Wright   PetscCallCeed(ceed, CeedQFunctionSetContext(qf_stats_collect, collect_qfctx));
111*b30619f6SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextDestroy(&collect_qfctx));
112*b30619f6SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_stats_collect, "q", num_comp_q, CEED_EVAL_INTERP));
113*b30619f6SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_stats_collect, "q_data", q_data_size, CEED_EVAL_NONE));
114*b30619f6SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_stats_collect, "x", num_comp_x, CEED_EVAL_INTERP));
115*b30619f6SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_stats_collect, "v", num_comp_stats, CEED_EVAL_NONE));
116*b30619f6SJames Wright 
117*b30619f6SJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_stats_collect, NULL, NULL, &op_stats_collect));
118*b30619f6SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_stats_collect, "q", honee->elem_restr_q, honee->basis_q, CEED_VECTOR_ACTIVE));
119*b30619f6SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_stats_collect, "q_data", elem_restr_qd, CEED_BASIS_NONE, q_data));
120*b30619f6SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_stats_collect, "x", honee->elem_restr_x, honee->basis_x, honee->x_coord));
121*b30619f6SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_stats_collect, "v", stats_data->elem_restr_child_colloc, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
122*b30619f6SJames Wright 
123*b30619f6SJames Wright   PetscCallCeed(ceed, CeedOperatorGetContextFieldLabel(op_stats_collect, "solution time", &spanstats->solution_time_label));
124*b30619f6SJames Wright   PetscCallCeed(ceed, CeedOperatorGetContextFieldLabel(op_stats_collect, "previous time", &spanstats->previous_time_label));
125*b30619f6SJames Wright   PetscCallCeed(ceed, CeedOperatorGetContextFieldLabel(op_stats_collect, "timestep", &spanstats->timestep_label));
126*b30619f6SJames Wright 
127*b30619f6SJames Wright   PetscCall(OperatorApplyContextCreate(honee->dm, spanstats->dm, honee->ceed, op_stats_collect, honee->q_ceed, NULL, NULL, NULL,
128*b30619f6SJames Wright                                        &spanstats->op_stats_collect_ctx));
129*b30619f6SJames Wright 
130*b30619f6SJames Wright   PetscCall(CeedOperatorCreateLocalVecs(op_stats_collect, DMReturnVecType(spanstats->dm), PETSC_COMM_SELF, NULL, &spanstats->Child_Stats_loc));
131*b30619f6SJames Wright   PetscCall(VecZeroEntries(spanstats->Child_Stats_loc));
132*b30619f6SJames Wright 
133*b30619f6SJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&q_data));
134*b30619f6SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd));
135*b30619f6SJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_stats_collect));
136*b30619f6SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_stats_collect));
137*b30619f6SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
138*b30619f6SJames Wright }
139*b30619f6SJames Wright 
140*b30619f6SJames Wright // @brief Setup for statistics collection of CFL and Peclet number
141*b30619f6SJames Wright PetscErrorCode SpanwiseStatisticsSetup_CflPe(TS ts, PetscViewerAndFormat *ctx) {
142*b30619f6SJames Wright   Honee              honee;
143*b30619f6SJames Wright   SpanStatsSetupData stats_setup_data;
144*b30619f6SJames Wright   SpanStatsCtx       spanstats;
145*b30619f6SJames Wright   const char        *prefix = "ts_monitor_spanstats_cflpe_";
146*b30619f6SJames Wright   PetscBool          is_ascii;
147*b30619f6SJames Wright 
148*b30619f6SJames Wright   PetscFunctionBeginUser;
149*b30619f6SJames Wright   PetscCall(TSGetApplicationContext(ts, &honee));
150*b30619f6SJames Wright   PetscCall(PetscObjectTypeCompare((PetscObject)ctx->viewer, PETSCVIEWERASCII, &is_ascii));
151*b30619f6SJames Wright   PetscCheck(!is_ascii || honee->app_ctx->test_type != TESTTYPE_NONE, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP,
152*b30619f6SJames Wright              "Turbulence spanwise statistics does not support ASCII viewers");
153*b30619f6SJames Wright 
154*b30619f6SJames Wright   PetscCall(SpanwiseStatisticsSetupInitialize(honee, honee->app_ctx->degree, 6, prefix, &stats_setup_data, &spanstats));
155*b30619f6SJames Wright 
156*b30619f6SJames Wright   {  // Create Section for data
157*b30619f6SJames Wright     PetscSection section;
158*b30619f6SJames Wright 
159*b30619f6SJames Wright     PetscCall(DMGetLocalSection(spanstats->dm, &section));
160*b30619f6SJames Wright     PetscCall(PetscSectionSetFieldName(section, 0, ""));
161*b30619f6SJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 0, "MeanCFL"));
162*b30619f6SJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 1, "MeanCFLSquared"));
163*b30619f6SJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 2, "MeanCFLCubed"));
164*b30619f6SJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 3, "MeanPe"));
165*b30619f6SJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 4, "MeanPeSquared"));
166*b30619f6SJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 5, "MeanPeCubed"));
167*b30619f6SJames Wright   }
168*b30619f6SJames Wright 
169*b30619f6SJames Wright   // Create CeedOperators for statistics collection
170*b30619f6SJames Wright   PetscCall(CreateStatisticCollectionOperator(honee, spanstats, stats_setup_data, honee->problem_data));
171*b30619f6SJames Wright 
172*b30619f6SJames Wright   ctx->data         = spanstats;
173*b30619f6SJames Wright   ctx->data_destroy = SpanStatsCtxDestroy;
174*b30619f6SJames Wright 
175*b30619f6SJames Wright   PetscCall(SpanwiseStatisticsSetupFinalize(ts, honee, spanstats, ctx, &stats_setup_data));
176*b30619f6SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
177*b30619f6SJames Wright }
178*b30619f6SJames Wright 
179*b30619f6SJames Wright // @brief TSMonitor for collection and processing of CFL and Peclet number statistics
180*b30619f6SJames Wright PetscErrorCode TSMonitor_SpanwiseStatisticsCflPe(TS ts, PetscInt steps, PetscReal solution_time, Vec Q, PetscViewerAndFormat *ctx) {
181*b30619f6SJames Wright   Honee             honee;
182*b30619f6SJames Wright   Vec               stats;
183*b30619f6SJames Wright   TSConvergedReason reason;
184*b30619f6SJames Wright   SpanStatsCtx      spanstats        = ctx->data;
185*b30619f6SJames Wright   PetscInt          collect_interval = spanstats->collect_interval, viewer_interval = ctx->view_interval;
186*b30619f6SJames Wright   PetscReal         timestep;
187*b30619f6SJames Wright 
188*b30619f6SJames Wright   PetscFunctionBeginUser;
189*b30619f6SJames Wright   PetscCall(TSGetApplicationContext(ts, &honee));
190*b30619f6SJames Wright   PetscCall(TSGetConvergedReason(ts, &reason));
191*b30619f6SJames Wright   // Do not collect or process on the first step of the run (ie. on the initial condition)
192*b30619f6SJames Wright   if (steps == honee->app_ctx->cont_steps) PetscFunctionReturn(PETSC_SUCCESS);
193*b30619f6SJames Wright 
194*b30619f6SJames Wright   PetscBool run_processing_and_viewer = (steps % viewer_interval == 0 && viewer_interval != -1) || reason != TS_CONVERGED_ITERATING;
195*b30619f6SJames Wright 
196*b30619f6SJames Wright   if (steps % collect_interval == 0 || run_processing_and_viewer) {
197*b30619f6SJames Wright     PetscCall(TSGetTimeStep(ts, &timestep));
198*b30619f6SJames Wright     PetscCallCeed(honee->ceed, CeedOperatorSetContextDouble(spanstats->op_stats_collect_ctx->op, spanstats->timestep_label, &timestep));
199*b30619f6SJames Wright     PetscCall(SpanwiseStatisticsCollect(honee, spanstats, solution_time, Q));
200*b30619f6SJames Wright 
201*b30619f6SJames Wright     if (run_processing_and_viewer) {
202*b30619f6SJames Wright       PetscCall(DMSetOutputSequenceNumber(spanstats->dm, steps, solution_time));
203*b30619f6SJames Wright       PetscCall(DMGetGlobalVector(spanstats->dm, &stats));
204*b30619f6SJames Wright       PetscCall(SpanwiseStatisticsProcess(honee, spanstats, stats));
205*b30619f6SJames Wright 
206*b30619f6SJames Wright       PetscCall(PetscViewerPushFormat(ctx->viewer, ctx->format));
207*b30619f6SJames Wright       PetscCall(VecView(stats, ctx->viewer));
208*b30619f6SJames Wright       PetscCall(PetscViewerPopFormat(ctx->viewer));
209*b30619f6SJames Wright       {
210*b30619f6SJames Wright         PetscInt    tab_level;
211*b30619f6SJames Wright         PetscViewer viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
212*b30619f6SJames Wright         CeedScalar  second = honee->units->second;
213*b30619f6SJames Wright         const char *filename;
214*b30619f6SJames Wright 
215*b30619f6SJames Wright         PetscCall(PetscViewerFileGetName(ctx->viewer, &filename););
216*b30619f6SJames Wright         PetscCall(PetscObjectGetTabLevel((PetscObject)ts, &tab_level));
217*b30619f6SJames Wright         PetscCall(PetscViewerASCIIAddTab(viewer, tab_level + 1));
218*b30619f6SJames Wright         if (filename) {
219*b30619f6SJames Wright           PetscCall(PetscViewerASCIIPrintf(
220*b30619f6SJames Wright               viewer,
221*b30619f6SJames Wright               "Spanwise turbulent statistics written to %s for time span (%0.12e,%0.12e] and step span [%" PetscInt_FMT ",%" PetscInt_FMT "]\n",
222*b30619f6SJames Wright               filename, spanstats->initial_solution_time / second, solution_time / second, spanstats->initial_solution_step, steps));
223*b30619f6SJames Wright         } else {
224*b30619f6SJames Wright           PetscCall(PetscViewerASCIIPrintf(
225*b30619f6SJames Wright               viewer, "Spanwise statistics (%s) file written for time span (%0.12e,%0.12e] and step span [%" PetscInt_FMT ",%" PetscInt_FMT "]\n",
226*b30619f6SJames Wright               spanstats->prefix, spanstats->initial_solution_time / second, solution_time / second, spanstats->initial_solution_step, steps));
227*b30619f6SJames Wright         }
228*b30619f6SJames Wright         PetscCall(PetscViewerASCIISubtractTab(viewer, tab_level + 1));
229*b30619f6SJames Wright       }
230*b30619f6SJames Wright 
231*b30619f6SJames Wright       PetscCall(DMRestoreGlobalVector(spanstats->dm, &stats));
232*b30619f6SJames Wright     }
233*b30619f6SJames Wright   }
234*b30619f6SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
235*b30619f6SJames Wright }
236