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