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