xref: /honee/src/monitor_cfl.c (revision e3db12f8fd36c2c636163113c7ff57d59e00b06c) !
1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3 
4 #include "../qfunctions/monitor_cfl.h"
5 #include <navierstokes.h>
6 
7 typedef struct {
8   OperatorApplyContext op_monitor_ctx;
9   Vec                  values;
10   PetscInt             num_comps;
11   PetscInt             tab_level;
12   PetscBool            is_header_written;
13 } *MonitorCflCtx;
14 
15 static PetscErrorCode MonitorCflCtxDestroy(MonitorCflCtx *monitor_ctx) {
16   MonitorCflCtx monitor_ctx_ = *monitor_ctx;
17 
18   PetscFunctionBeginUser;
19   PetscCall(OperatorApplyContextDestroy(monitor_ctx_->op_monitor_ctx));
20   PetscCall(VecDestroy(&monitor_ctx_->values));
21   PetscCall(PetscFree(monitor_ctx_));
22   *monitor_ctx = NULL;
23   PetscFunctionReturn(PETSC_SUCCESS);
24 }
25 
26 PetscErrorCode SetupMontiorCfl(TS ts, PetscViewerAndFormat *ctx) {
27   Honee                honee;
28   Ceed                 ceed;
29   CeedQFunction        qf_monitor = NULL;
30   CeedOperator         op_monitor;
31   CeedElemRestriction  elem_restr_qd, elem_restr_cfl, elem_restr_q;
32   CeedBasis            basis_q;
33   CeedVector           q_data;
34   CeedInt              num_comp_q, q_data_size;
35   PetscInt             num_comp_cfl = 1, dim, tab_level;
36   MonitorCflCtx        monitor_ctx;
37   CeedQFunctionContext newt_qfctx;
38   PetscBool            is_ascii;
39 
40   PetscFunctionBeginUser;
41   PetscCall(PetscObjectTypeCompare((PetscObject)ctx->viewer, PETSCVIEWERASCII, &is_ascii));
42   PetscCheck(is_ascii, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Only supports ASCII viewers");
43   PetscCall(TSGetApplicationContext(ts, &honee));
44   PetscCall(DMGetDimension(honee->dm, &dim));
45   ceed = honee->ceed;
46 
47   PetscCall(PetscNew(&monitor_ctx));
48 
49   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(honee->elem_restr_q, &num_comp_q));
50   PetscCall(QDataGet(ceed, honee->dm, DMLABEL_DEFAULT, DMLABEL_DEFAULT_VALUE, honee->elem_restr_x, honee->basis_x, honee->x_coord, &elem_restr_qd,
51                      &q_data, &q_data_size));
52   PetscCall(DMPlexCeedElemRestrictionQDataCreate(ceed, honee->dm, DMLABEL_DEFAULT, DMLABEL_DEFAULT_VALUE, 0, num_comp_cfl, &elem_restr_cfl));
53 
54   {  // Get restriction and basis from the RHS function
55     CeedOperator     *sub_ops, main_op = honee->op_ifunction ? honee->op_ifunction : honee->op_rhs_ctx->op;
56     CeedOperatorField op_field;
57     PetscInt          sub_op_index = 0;  // will be 0 for the volume op
58 
59     PetscCallCeed(ceed, CeedOperatorCompositeGetSubList(main_op, &sub_ops));
60     PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "q", &op_field));
61 
62     PetscCallCeed(ceed, CeedOperatorFieldGetData(op_field, NULL, &elem_restr_q, &basis_q, NULL));
63     PetscCallCeed(ceed, CeedOperatorGetContext(sub_ops[sub_op_index], &newt_qfctx));
64   }
65 
66   switch (dim) {
67     case 2:
68       switch (honee->phys->state_var) {
69         case STATEVAR_PRIMITIVE:
70           PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, MonitorCFL_2D_Prim, MonitorCFL_2D_Prim_loc, &qf_monitor));
71           break;
72         case STATEVAR_CONSERVATIVE:
73           PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, MonitorCFL_2D_Conserv, MonitorCFL_2D_Conserv_loc, &qf_monitor));
74           break;
75         case STATEVAR_ENTROPY:
76           PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, MonitorCFL_2D_Entropy, MonitorCFL_2D_Entropy_loc, &qf_monitor));
77           break;
78       }
79       break;
80     case 3:
81       switch (honee->phys->state_var) {
82         case STATEVAR_PRIMITIVE:
83           PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, MonitorCFL_3D_Prim, MonitorCFL_3D_Prim_loc, &qf_monitor));
84           break;
85         case STATEVAR_CONSERVATIVE:
86           PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, MonitorCFL_3D_Conserv, MonitorCFL_3D_Conserv_loc, &qf_monitor));
87           break;
88         case STATEVAR_ENTROPY:
89           PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, MonitorCFL_3D_Entropy, MonitorCFL_3D_Entropy_loc, &qf_monitor));
90           break;
91       }
92       break;
93   }
94   PetscCheck(qf_monitor, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP,
95              "Could not create CFL monitor QFunction for dim %" PetscInt_FMT " and state variable %s", dim, StateVariables[honee->phys->state_var]);
96 
97   PetscBool is_advection;
98   PetscCall(PetscStrcmp("advection", honee->app_ctx->problem_name, &is_advection));
99   if (is_advection) {
100     NewtonianIdealGasContext newt_ctx;
101 
102     PetscCall(PetscNew(&newt_ctx));
103     PetscCallCeed(ceed, CeedQFunctionContextDestroy(&newt_qfctx));
104     PetscCallCeed(ceed, CeedQFunctionContextCreate(ceed, &newt_qfctx));
105     PetscCallCeed(ceed, CeedQFunctionContextSetData(newt_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*newt_ctx), newt_ctx));
106     PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(newt_qfctx, CEED_MEM_HOST, FreeContextPetsc));
107   }
108 
109   PetscCallCeed(ceed, CeedQFunctionSetContext(qf_monitor, newt_qfctx));
110   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_monitor, "q", num_comp_q, CEED_EVAL_INTERP));
111   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_monitor, "q_data", q_data_size, CEED_EVAL_NONE));
112   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_monitor, "v", num_comp_cfl, CEED_EVAL_NONE));
113 
114   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_monitor, NULL, NULL, &op_monitor));
115   PetscCallCeed(ceed, CeedOperatorSetField(op_monitor, "q", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE));
116   PetscCallCeed(ceed, CeedOperatorSetField(op_monitor, "q_data", elem_restr_qd, CEED_BASIS_NONE, q_data));
117   PetscCallCeed(ceed, CeedOperatorSetField(op_monitor, "v", elem_restr_cfl, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
118 
119   PetscCall(OperatorApplyContextCreate(honee->dm, NULL, ceed, op_monitor, NULL, NULL, NULL, NULL, &monitor_ctx->op_monitor_ctx));
120 
121   PetscCall(CeedOperatorCreateLocalVecs(op_monitor, DMReturnVecType(honee->dm), PETSC_COMM_SELF, NULL, &monitor_ctx->values));
122   PetscCall(VecSetBlockSize(monitor_ctx->values, num_comp_cfl));
123   monitor_ctx->num_comps = num_comp_cfl;
124   PetscCall(PetscObjectGetTabLevel((PetscObject)ts, &tab_level));
125   monitor_ctx->tab_level = tab_level + 1;
126 
127   ctx->data         = monitor_ctx;
128   ctx->data_destroy = (PetscCtxDestroyFn *)MonitorCflCtxDestroy;
129 
130   PetscCallCeed(ceed, CeedQFunctionContextDestroy(&newt_qfctx));
131   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd));
132   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_q));
133   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_cfl));
134   PetscCallCeed(ceed, CeedBasisDestroy(&basis_q));
135   PetscCallCeed(ceed, CeedVectorDestroy(&q_data));
136   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_monitor));
137   PetscCallCeed(ceed, CeedOperatorDestroy(&op_monitor));
138   PetscFunctionReturn(PETSC_SUCCESS);
139 }
140 
141 PetscErrorCode TSMonitor_Cfl(TS ts, PetscInt step, PetscReal solution_time, Vec Q, PetscViewerAndFormat *ctx) {
142   MonitorCflCtx     monitor_ctx = (MonitorCflCtx)ctx->data;
143   Honee             honee;
144   MPI_Comm          comm;
145   TSConvergedReason reason;
146   PetscReal         part_minmax[2], global_minmax[2], dt;
147 
148   PetscFunctionBeginUser;
149   PetscCall(TSGetConvergedReason(ts, &reason));
150   if (!(step % ctx->view_interval == 0 || reason != TS_CONVERGED_ITERATING)) PetscFunctionReturn(PETSC_SUCCESS);
151   PetscCall(TSGetApplicationContext(ts, &honee));
152   PetscCall(PetscObjectGetComm((PetscObject)ts, &comm));
153 
154   PetscCall(UpdateBoundaryValues(honee, honee->Q_loc, solution_time));
155   PetscCall(DMGlobalToLocal(honee->dm, Q, INSERT_VALUES, honee->Q_loc));
156 
157   PetscCall(ApplyCeedOperatorLocalToLocal(honee->Q_loc, monitor_ctx->values, monitor_ctx->op_monitor_ctx));
158 
159   PetscCall(VecMin(monitor_ctx->values, NULL, &part_minmax[0]));
160   PetscCall(VecMax(monitor_ctx->values, NULL, &part_minmax[1]));
161   // Need to get global min/max since `values` is only the local partition
162   PetscCall(PetscGlobalMinMaxReal(comm, part_minmax, global_minmax));
163   PetscCall(TSGetTimeStep(ts, &dt));
164   global_minmax[0] *= dt;
165   global_minmax[1] *= dt;
166 
167   if (ctx->format == PETSC_VIEWER_ASCII_CSV) {
168     if (!monitor_ctx->is_header_written) {
169       char        buf[PETSC_MAX_PATH_LEN];
170       const char *buf_const;
171       time_t      t = time(NULL);
172 
173       PetscCall(PetscGetVersion(buf, sizeof(buf)));
174       PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# petsc_version: %s\n", buf));
175       PetscCall(PetscGetPetscDir(&buf_const));
176       PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# petsc_directory: %s\n", buf_const));
177       PetscCall(PetscGetArchType(buf, sizeof(buf)));
178       PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# petsc_arch: %s\n", buf));
179       PetscCheck(strftime(buf, sizeof(buf), "%FT%T%z", localtime(&t)), comm, PETSC_ERR_SYS, "strftime() call failed");
180       PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# date: %s\n", buf));
181       PetscCheck(strftime(buf, sizeof(buf), "%Z", localtime(&t)), comm, PETSC_ERR_SYS, "strftime() call failed");
182       PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# date_timezone: %s\n", buf));
183       PetscCall(PetscGetUserName(buf, sizeof(buf)));
184       PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# username: %s\n", buf));
185       PetscCall(PetscGetHostName(buf, sizeof(buf)));
186       PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# hostname: %s\n", buf));
187       PetscCall(PetscGetWorkingDirectory(buf, sizeof(buf)));
188       PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# working_directory: %s\n", buf));
189 
190       PetscCall(PetscViewerFileGetName(ctx->viewer, &buf_const));
191       PetscCall(PetscGetFullPath(buf_const, buf, sizeof(buf)));
192       PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# original_file_path: %s\n", buf));
193       PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "#\n"));
194       PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "step,time,mincfl,maxcfl\n"));
195       monitor_ctx->is_header_written = PETSC_TRUE;
196     }
197 
198     PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "%" PetscInt_FMT ",%0.17e,", step, solution_time));
199     PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "%0.17e,%0.17e", global_minmax[0], global_minmax[1]));
200     PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "\n"));
201   } else {
202     PetscCall(PetscViewerASCIIAddTab(ctx->viewer, monitor_ctx->tab_level));
203     PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "Min, Max CFL: %0.5g, %0.5g\n", global_minmax[0], global_minmax[1]));
204     PetscCall(PetscViewerASCIISubtractTab(ctx->viewer, monitor_ctx->tab_level));
205   }
206   PetscFunctionReturn(PETSC_SUCCESS);
207 }
208