125125139SJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors. 225125139SJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause 325125139SJames Wright 425125139SJames Wright #include "../qfunctions/monitor_totalkineticenergy.h" 525125139SJames Wright #include <navierstokes.h> 625125139SJames Wright #include <time.h> 725125139SJames Wright 825125139SJames Wright typedef struct { 925125139SJames Wright OperatorApplyContext op_monitor_ctx; 1025125139SJames Wright Vec values; 1125125139SJames Wright PetscScalar *sum_values; 1225125139SJames Wright PetscScalar volume; 1325125139SJames Wright PetscInt num_comps; 1425125139SJames Wright PetscBool is_header_written; 15*600dae7dSJames Wright } *MonitorTotalKE; 1625125139SJames Wright 17*600dae7dSJames Wright PetscErrorCode MonitorTotalKEDestroy(void **ctx) { 18*600dae7dSJames Wright MonitorTotalKE monitor_ctx = *(MonitorTotalKE *)ctx; 1925125139SJames Wright 2025125139SJames Wright PetscFunctionBeginUser; 2125125139SJames Wright PetscCall(OperatorApplyContextDestroy(monitor_ctx->op_monitor_ctx)); 2225125139SJames Wright PetscCall(VecDestroy(&monitor_ctx->values)); 2325125139SJames Wright PetscCall(PetscFree(monitor_ctx->sum_values)); 2425125139SJames Wright PetscCall(PetscFree(monitor_ctx)); 2525125139SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 2625125139SJames Wright } 2725125139SJames Wright 2825125139SJames Wright /** @brief Create CeedElemRestriction for collocated data in component-major order. 2925125139SJames Wright a. Sets the strides of the restriction to component-major order 3025125139SJames Wright Number of quadrature points is used from the CeedBasis, and number of elements is used from the CeedElemRestriction. 3125125139SJames Wright */ 3225125139SJames Wright static PetscErrorCode CreateElemRestrColloc_CompMajor(Ceed ceed, CeedInt num_comp, CeedBasis basis, CeedElemRestriction elem_restr_base, 3325125139SJames Wright CeedElemRestriction *elem_restr_collocated) { 3425125139SJames Wright CeedInt num_elem_qpts, loc_num_elem; 3525125139SJames Wright 3625125139SJames Wright PetscFunctionBeginUser; 3725125139SJames Wright PetscCallCeed(ceed, CeedBasisGetNumQuadraturePoints(basis, &num_elem_qpts)); 3825125139SJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumElements(elem_restr_base, &loc_num_elem)); 3925125139SJames Wright 4025125139SJames Wright const CeedInt strides[] = {num_comp, 1, num_elem_qpts * num_comp}; 4125125139SJames Wright PetscCallCeed(ceed, CeedElemRestrictionCreateStrided(ceed, loc_num_elem, num_elem_qpts, num_comp, num_comp * loc_num_elem * num_elem_qpts, strides, 4225125139SJames Wright elem_restr_collocated)); 4325125139SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 4425125139SJames Wright } 4525125139SJames Wright 4625125139SJames Wright PetscErrorCode SetupMontiorTotalKineticEnergy(TS ts, PetscViewerAndFormat *ctx) { 4725125139SJames Wright Honee honee; 4825125139SJames Wright Ceed ceed; 4925125139SJames Wright CeedQFunction qf_monitor; 5025125139SJames Wright CeedOperator op_monitor; 5125125139SJames Wright CeedElemRestriction elem_restr_qd, elem_restr_totalke, elem_restr_q; 5225125139SJames Wright CeedBasis basis_q; 5325125139SJames Wright CeedVector q_data; 5425125139SJames Wright CeedInt num_comp_q, q_data_size; 5525125139SJames Wright DMLabel domain_label = NULL; 5625125139SJames Wright PetscInt label_value = 0, num_comp_totalke = 5, dim; 57*600dae7dSJames Wright MonitorTotalKE monitor_ctx; 5825125139SJames Wright CeedQFunctionContext newt_qfctx; 5925125139SJames Wright PetscBool is_ascii; 6025125139SJames Wright 6125125139SJames Wright PetscFunctionBeginUser; 6225125139SJames Wright PetscCall(PetscObjectTypeCompare((PetscObject)ctx->viewer, PETSCVIEWERASCII, &is_ascii)); 6325125139SJames Wright PetscCheck(is_ascii, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Only supports ASCII viewers"); 6425125139SJames Wright PetscCall(TSGetApplicationContext(ts, &honee)); 6525125139SJames Wright PetscCall(DMGetDimension(honee->dm, &dim)); 6625125139SJames Wright ceed = honee->ceed; 6725125139SJames Wright 6825125139SJames Wright PetscCall(PetscNew(&monitor_ctx)); 6925125139SJames Wright PetscCall(HoneeCalculateDomainSize(honee, &monitor_ctx->volume)); 7025125139SJames Wright 7125125139SJames Wright PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(honee->elem_restr_q, &num_comp_q)); 7225125139SJames 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, 7325125139SJames Wright &q_data_size)); 7425125139SJames Wright 7525125139SJames Wright { // Get restriction and basis from the RHS function 7625125139SJames Wright CeedOperator *sub_ops; 7725125139SJames Wright CeedOperatorField op_field; 7825125139SJames Wright PetscInt sub_op_index = 0; // will be 0 for the volume op 7925125139SJames Wright 8025125139SJames Wright PetscCallCeed(ceed, CeedCompositeOperatorGetSubList(honee->op_rhs_ctx->op, &sub_ops)); 8125125139SJames Wright PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "q", &op_field)); 8225125139SJames Wright 8325125139SJames Wright PetscCallCeed(ceed, CeedOperatorFieldGetData(op_field, NULL, &elem_restr_q, &basis_q, NULL)); 8425125139SJames Wright PetscCallCeed(ceed, CeedOperatorGetContext(sub_ops[sub_op_index], &newt_qfctx)); 8525125139SJames Wright } 8625125139SJames Wright PetscCall(CreateElemRestrColloc_CompMajor(ceed, num_comp_totalke, basis_q, elem_restr_q, &elem_restr_totalke)); 8725125139SJames Wright 8825125139SJames Wright switch (honee->phys->state_var) { 8925125139SJames Wright case STATEVAR_PRIMITIVE: 9025125139SJames Wright PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, MonitorTotalKineticEnergy_Prim, MonitorTotalKineticEnergy_Prim_loc, &qf_monitor)); 9125125139SJames Wright break; 9225125139SJames Wright case STATEVAR_CONSERVATIVE: 9325125139SJames Wright PetscCallCeed(ceed, 9425125139SJames Wright CeedQFunctionCreateInterior(ceed, 1, MonitorTotalKineticEnergy_Conserv, MonitorTotalKineticEnergy_Conserv_loc, &qf_monitor)); 9525125139SJames Wright break; 9625125139SJames Wright case STATEVAR_ENTROPY: 9725125139SJames Wright PetscCallCeed(ceed, 9825125139SJames Wright CeedQFunctionCreateInterior(ceed, 1, MonitorTotalKineticEnergy_Entropy, MonitorTotalKineticEnergy_Entropy_loc, &qf_monitor)); 9925125139SJames Wright break; 10025125139SJames Wright } 10125125139SJames Wright 10225125139SJames Wright PetscCallCeed(ceed, CeedQFunctionSetContext(qf_monitor, newt_qfctx)); 10325125139SJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_monitor, "q", num_comp_q, CEED_EVAL_INTERP)); 10425125139SJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_monitor, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD)); 10525125139SJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_monitor, "q_data", q_data_size, CEED_EVAL_NONE)); 10625125139SJames Wright PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_monitor, "v", num_comp_totalke, CEED_EVAL_NONE)); 10725125139SJames Wright 10825125139SJames Wright PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_monitor, NULL, NULL, &op_monitor)); 10925125139SJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_monitor, "q", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 11025125139SJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_monitor, "Grad_q", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE)); 11125125139SJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_monitor, "q_data", elem_restr_qd, CEED_BASIS_NONE, q_data)); 11225125139SJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_monitor, "v", elem_restr_totalke, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE)); 11325125139SJames Wright 11425125139SJames Wright PetscCall(OperatorApplyContextCreate(honee->dm, NULL, ceed, op_monitor, NULL, NULL, NULL, NULL, &monitor_ctx->op_monitor_ctx)); 11525125139SJames Wright 11625125139SJames Wright PetscCall(CeedOperatorCreateLocalVecs(op_monitor, DMReturnVecType(honee->dm), PETSC_COMM_SELF, NULL, &monitor_ctx->values)); 11725125139SJames Wright PetscCall(VecSetBlockSize(monitor_ctx->values, num_comp_totalke)); 11825125139SJames Wright monitor_ctx->num_comps = num_comp_totalke; 11925125139SJames Wright PetscCall(PetscMalloc1(num_comp_totalke, &monitor_ctx->sum_values)); 12025125139SJames Wright 12125125139SJames Wright ctx->data = monitor_ctx; 122*600dae7dSJames Wright ctx->data_destroy = MonitorTotalKEDestroy; 12325125139SJames Wright 12425125139SJames Wright PetscCallCeed(ceed, CeedQFunctionContextDestroy(&newt_qfctx)); 12525125139SJames Wright PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd)); 12625125139SJames Wright PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_q)); 12725125139SJames Wright PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_totalke)); 12825125139SJames Wright PetscCallCeed(ceed, CeedBasisDestroy(&basis_q)); 12925125139SJames Wright PetscCallCeed(ceed, CeedVectorDestroy(&q_data)); 13025125139SJames Wright PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_monitor)); 13125125139SJames Wright PetscCallCeed(ceed, CeedOperatorDestroy(&op_monitor)); 13225125139SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 13325125139SJames Wright } 13425125139SJames Wright 13525125139SJames Wright PetscErrorCode TSMonitor_TotalKineticEnergy(TS ts, PetscInt step, PetscReal solution_time, Vec Q, PetscViewerAndFormat *ctx) { 136*600dae7dSJames Wright MonitorTotalKE monitor_ctx = (MonitorTotalKE)ctx->data; 13725125139SJames Wright Honee honee; 13825125139SJames Wright MPI_Comm comm; 13925125139SJames Wright PetscMPIInt rank; 14025125139SJames Wright TSConvergedReason reason; 14125125139SJames Wright static const char *field_names[] = {"TotalKineticEnergy", "StrainDissipation", "DivergenceDissipation", "VolumeExpansion", "MuVorticity2"}; 14225125139SJames Wright 14325125139SJames Wright PetscFunctionBeginUser; 14425125139SJames Wright PetscCall(TSGetConvergedReason(ts, &reason)); 14525125139SJames Wright if (!(step % ctx->view_interval == 0 || reason != TS_CONVERGED_ITERATING)) PetscFunctionReturn(PETSC_SUCCESS); 14625125139SJames Wright PetscCall(TSGetApplicationContext(ts, &honee)); 14725125139SJames Wright PetscCall(PetscObjectGetComm((PetscObject)ts, &comm)); 14825125139SJames Wright PetscCallMPI(MPI_Comm_rank(comm, &rank)); 14925125139SJames Wright 15025125139SJames Wright PetscCall(UpdateBoundaryValues(honee, honee->Q_loc, solution_time)); 15125125139SJames Wright PetscCall(DMGlobalToLocal(honee->dm, Q, INSERT_VALUES, honee->Q_loc)); 15225125139SJames Wright 15325125139SJames Wright PetscCall(ApplyCeedOperatorLocalToLocal(honee->Q_loc, monitor_ctx->values, monitor_ctx->op_monitor_ctx)); 15425125139SJames Wright 15525125139SJames Wright for (PetscInt i = 0; i < monitor_ctx->num_comps; i++) { 15625125139SJames Wright PetscCall(VecStrideSum(monitor_ctx->values, i, &monitor_ctx->sum_values[i])); 15725125139SJames Wright } 15825125139SJames Wright 15925125139SJames Wright if (rank == 0) PetscCallMPI(MPI_Reduce(MPI_IN_PLACE, monitor_ctx->sum_values, monitor_ctx->num_comps, MPIU_SCALAR, MPI_SUM, 0, comm)); 16025125139SJames Wright else PetscCallMPI(MPI_Reduce(monitor_ctx->sum_values, monitor_ctx->sum_values, monitor_ctx->num_comps, MPIU_SCALAR, MPI_SUM, 0, comm)); 16125125139SJames Wright 16225125139SJames Wright if (rank == 0) 16325125139SJames Wright for (PetscInt i = 0; i < monitor_ctx->num_comps; i++) monitor_ctx->sum_values[i] /= monitor_ctx->volume; 16425125139SJames Wright 16525125139SJames Wright if (ctx->format == PETSC_VIEWER_ASCII_CSV) { 16625125139SJames Wright if (!monitor_ctx->is_header_written) { 16725125139SJames Wright char buf[PETSC_MAX_PATH_LEN]; 16825125139SJames Wright const char *buf_const; 16925125139SJames Wright time_t t = time(NULL); 17025125139SJames Wright 17125125139SJames Wright PetscCall(PetscGetVersion(buf, sizeof(buf))); 17225125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# petsc_version: %s\n", buf)); 17325125139SJames Wright PetscCall(PetscGetPetscDir(&buf_const)); 17425125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# petsc_directory: %s\n", buf_const)); 17525125139SJames Wright PetscCall(PetscGetArchType(buf, sizeof(buf))); 17625125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# petsc_arch: %s\n", buf)); 17725125139SJames Wright if (strftime(buf, sizeof(buf), "%FT%T%z", localtime(&t)) == 0) SETERRQ(comm, PETSC_ERR_SYS, "strftime() call failed"); 17825125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# date: %s\n", buf)); 17925125139SJames Wright if (strftime(buf, sizeof(buf), "%Z", localtime(&t)) == 0) SETERRQ(comm, PETSC_ERR_SYS, "strftime() call failed"); 18025125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# date_timezone: %s\n", buf)); 18125125139SJames Wright PetscCall(PetscGetUserName(buf, sizeof(buf))); 18225125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# username: %s\n", buf)); 18325125139SJames Wright PetscCall(PetscGetHostName(buf, sizeof(buf))); 18425125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# hostname: %s\n", buf)); 18525125139SJames Wright PetscCall(PetscGetWorkingDirectory(buf, sizeof(buf))); 18625125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# working_directory: %s\n", buf)); 18725125139SJames Wright 18825125139SJames Wright PetscCall(PetscViewerFileGetName(ctx->viewer, &buf_const)); 18925125139SJames Wright PetscCall(PetscGetFullPath(buf_const, buf, sizeof(buf))); 19025125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "# original_file_path: %s\n", buf)); 19125125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "#\n")); 19225125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "step,time,")); 19325125139SJames Wright for (PetscInt i = 0; i < monitor_ctx->num_comps; i++) { 19425125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "%s", field_names[i])); 19525125139SJames Wright if (i < monitor_ctx->num_comps - 1) PetscCall(PetscViewerASCIIPrintf(ctx->viewer, ",")); 19625125139SJames Wright } 19725125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "\n")); 19825125139SJames Wright monitor_ctx->is_header_written = PETSC_TRUE; 19925125139SJames Wright } 20025125139SJames Wright 20125125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "%" PetscInt_FMT ",%0.17e,", step, solution_time)); 20225125139SJames Wright for (PetscInt i = 0; i < monitor_ctx->num_comps; i++) { 20325125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "%0.17e", monitor_ctx->sum_values[i])); 20425125139SJames Wright if (i < monitor_ctx->num_comps - 1) PetscCall(PetscViewerASCIIPrintf(ctx->viewer, ",")); 20525125139SJames Wright } 20625125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "\n")); 20725125139SJames Wright } else { 20825125139SJames Wright for (PetscInt i = 0; i < monitor_ctx->num_comps; i++) { 20925125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "%s: %0.12e ", field_names[i], monitor_ctx->sum_values[i])); 21025125139SJames Wright } 21125125139SJames Wright PetscScalar sum_rates = 0; 21225125139SJames Wright for (PetscInt i = 1; i < monitor_ctx->num_comps - 1; i++) sum_rates += monitor_ctx->sum_values[i]; 21325125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "SumOfRates: %0.12e ", sum_rates)); 21425125139SJames Wright PetscCall(PetscViewerASCIIPrintf(ctx->viewer, "\n")); 21525125139SJames Wright } 21625125139SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 21725125139SJames Wright } 218