1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3 // 4 // SPDX-License-Identifier: BSD-2-Clause 5 // 6 // This file is part of CEED: http://github.com/ceed 7 /// @file 8 /// Functions for setting up and performing statistics collection 9 10 #include "../qfunctions/turb_spanstats.h" 11 12 #include <petscsf.h> 13 14 #include "../include/matops.h" 15 #include "../navierstokes.h" 16 #include "ceed/ceed.h" 17 #include "petscerror.h" 18 #include "petsclog.h" 19 #include "petscmat.h" 20 #include "petscsys.h" 21 #include "petscvec.h" 22 #include "petscviewer.h" 23 24 PetscErrorCode CreateStatsDM(User user, ProblemData *problem, PetscInt degree, SimpleBC bc) { 25 user->spanstats.num_comp_stats = TURB_NUM_COMPONENTS; 26 PetscReal domain_min[3], domain_max[3]; 27 PetscFE fe; 28 PetscSection section; 29 PetscLogStage stage_stats_setup; 30 MPI_Comm comm = PetscObjectComm((PetscObject)user->dm); 31 PetscFunctionBeginUser; 32 33 PetscCall(PetscLogStageGetId("Stats Setup", &stage_stats_setup)); 34 if (stage_stats_setup == -1) PetscCall(PetscLogStageRegister("Stats Setup", &stage_stats_setup)); 35 PetscCall(PetscLogStagePush(stage_stats_setup)); 36 37 // Get spanwise length 38 PetscCall(DMGetBoundingBox(user->dm, domain_min, domain_max)); 39 user->spanstats.span_width = domain_max[2] - domain_min[1]; 40 41 { // Get DM from surface 42 DM parent_distributed_dm; 43 PetscSF isoperiodicface; 44 DMLabel label; 45 PetscMPIInt size; 46 47 PetscCall(DMPlexGetIsoperiodicFaceSF(user->dm, &isoperiodicface)); 48 49 if (isoperiodicface) { 50 PetscSF inv_isoperiodicface; 51 PetscInt nleaves; 52 const PetscInt *ilocal; 53 54 PetscCall(PetscSFCreateInverseSF(isoperiodicface, &inv_isoperiodicface)); 55 PetscCall(PetscSFGetGraph(inv_isoperiodicface, NULL, &nleaves, &ilocal, NULL)); 56 PetscCall(DMCreateLabel(user->dm, "Periodic Face")); 57 PetscCall(DMGetLabel(user->dm, "Periodic Face", &label)); 58 for (PetscInt i = 0; i < nleaves; i++) { 59 PetscCall(DMLabelSetValue(label, ilocal[i], 1)); 60 } 61 } else { 62 PetscCall(DMGetLabel(user->dm, "Face Sets", &label)); 63 } 64 65 PetscCall(DMPlexLabelComplete(user->dm, label)); 66 PetscCall(DMPlexFilter(user->dm, label, 1, &user->spanstats.dm)); 67 PetscCall(DMProjectCoordinates(user->spanstats.dm, NULL)); // Ensure that a coordinate FE exists 68 69 PetscCall(DMPlexDistribute(user->spanstats.dm, 0, NULL, &parent_distributed_dm)); 70 PetscCallMPI(MPI_Comm_size(comm, &size)); 71 if (parent_distributed_dm) { 72 PetscCall(DMDestroy(&user->spanstats.dm)); 73 user->spanstats.dm = parent_distributed_dm; 74 } else if (size > 1) { 75 PetscCall(PetscPrintf(comm, "WARNING: Turbulent spanwise statistics: parent DM could not be distributed accross %d ranks.\n", size)); 76 } 77 } 78 79 PetscCall(PetscObjectSetName((PetscObject)user->spanstats.dm, "Spanwise_Stats")); 80 PetscCall(DMSetOptionsPrefix(user->spanstats.dm, "turbulence_spanstats_")); 81 PetscCall(DMSetFromOptions(user->spanstats.dm)); 82 PetscCall(DMViewFromOptions(user->spanstats.dm, NULL, "-dm_view")); // -spanstats_dm_view 83 84 // Create FE space for parent DM 85 PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, problem->dim - 1, user->spanstats.num_comp_stats, PETSC_FALSE, degree, PETSC_DECIDE, &fe)); 86 PetscCall(PetscObjectSetName((PetscObject)fe, "stats")); 87 PetscCall(DMAddField(user->spanstats.dm, NULL, (PetscObject)fe)); 88 PetscCall(DMCreateDS(user->spanstats.dm)); 89 PetscCall(DMPlexSetClosurePermutationTensor(user->spanstats.dm, PETSC_DETERMINE, NULL)); 90 91 // Create Section for data 92 PetscCall(DMGetLocalSection(user->spanstats.dm, §ion)); 93 PetscCall(PetscSectionSetFieldName(section, 0, "")); 94 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_DENSITY, "MeanDensity")); 95 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_PRESSURE, "MeanPressure")); 96 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_PRESSURE_SQUARED, "MeanPressureSquared")); 97 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_PRESSURE_VELOCITY_X, "MeanPressureVelocityX")); 98 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_PRESSURE_VELOCITY_Y, "MeanPressureVelocityY")); 99 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_PRESSURE_VELOCITY_Z, "MeanPressureVelocityZ")); 100 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_DENSITY_TEMPERATURE, "MeanDensityTemperature")); 101 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_DENSITY_TEMPERATURE_FLUX_X, "MeanDensityTemperatureFluxX")); 102 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_DENSITY_TEMPERATURE_FLUX_Y, "MeanDensityTemperatureFluxY")); 103 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_DENSITY_TEMPERATURE_FLUX_Z, "MeanDensityTemperatureFluxZ")); 104 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_MOMENTUM_X, "MeanMomentumX")); 105 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_MOMENTUM_Y, "MeanMomentumY")); 106 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_MOMENTUM_Z, "MeanMomentumZ")); 107 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_MOMENTUMFLUX_XX, "MeanMomentumFluxXX")); 108 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_MOMENTUMFLUX_YY, "MeanMomentumFluxYY")); 109 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_MOMENTUMFLUX_ZZ, "MeanMomentumFluxZZ")); 110 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_MOMENTUMFLUX_YZ, "MeanMomentumFluxYZ")); 111 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_MOMENTUMFLUX_XZ, "MeanMomentumFluxXZ")); 112 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_MOMENTUMFLUX_XY, "MeanMomentumFluxXY")); 113 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_VELOCITY_X, "MeanVelocityX")); 114 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_VELOCITY_Y, "MeanVelocityY")); 115 PetscCall(PetscSectionSetComponentName(section, 0, TURB_MEAN_VELOCITY_Z, "MeanVelocityZ")); 116 117 // Cleanup 118 PetscCall(PetscFEDestroy(&fe)); 119 120 PetscCall(PetscLogStagePop()); 121 PetscFunctionReturn(0); 122 } 123 124 // Create CeedElemRestriction for collocated data based on associated CeedBasis and CeedElemRestriction 125 // Number of quadrature points is used from the CeedBasis, and number of elements is used from the CeedElemRestriction 126 PetscErrorCode CreateElemRestrColloc(Ceed ceed, CeedInt num_comp, CeedBasis basis, CeedElemRestriction elem_restr_base, 127 CeedElemRestriction *elem_restr_collocated, CeedVector *l_vec, CeedVector *e_vec) { 128 CeedInt num_elem_qpts, loc_num_elem; 129 PetscFunctionBeginUser; 130 131 CeedBasisGetNumQuadraturePoints(basis, &num_elem_qpts); 132 CeedElemRestrictionGetNumElements(elem_restr_base, &loc_num_elem); 133 134 const CeedInt strides[] = {num_comp, 1, num_elem_qpts * num_comp}; 135 CeedElemRestrictionCreateStrided(ceed, loc_num_elem, num_elem_qpts, num_comp, num_comp * loc_num_elem * num_elem_qpts, strides, 136 elem_restr_collocated); 137 CeedElemRestrictionCreateVector(*elem_restr_collocated, l_vec, e_vec); 138 PetscFunctionReturn(0); 139 } 140 141 // Get coordinates of quadrature points 142 PetscErrorCode GetQuadratureCoords(Ceed ceed, DM dm, CeedElemRestriction elem_restr_x, CeedBasis basis_x, CeedVector x_coords, CeedVector *qx_coords, 143 PetscInt *total_nqpnts) { 144 CeedQFunction qf_quad_coords; 145 CeedOperator op_quad_coords; 146 PetscInt num_comp_x, loc_num_elem, num_elem_qpts; 147 CeedElemRestriction elem_restr_qx; 148 PetscFunctionBeginUser; 149 150 // Create Element Restriction and CeedVector for quadrature coordinates 151 CeedBasisGetNumQuadraturePoints(basis_x, &num_elem_qpts); 152 CeedElemRestrictionGetNumElements(elem_restr_x, &loc_num_elem); 153 CeedElemRestrictionGetNumComponents(elem_restr_x, &num_comp_x); 154 *total_nqpnts = num_elem_qpts * loc_num_elem; 155 PetscCall(CreateElemRestrColloc(ceed, num_comp_x, basis_x, elem_restr_x, &elem_restr_qx, qx_coords, NULL)); 156 157 // Create QFunction 158 CeedQFunctionCreateIdentity(ceed, num_comp_x, CEED_EVAL_INTERP, CEED_EVAL_NONE, &qf_quad_coords); 159 160 // Create Operator 161 CeedOperatorCreate(ceed, qf_quad_coords, NULL, NULL, &op_quad_coords); 162 CeedOperatorSetField(op_quad_coords, "input", elem_restr_x, basis_x, CEED_VECTOR_ACTIVE); 163 CeedOperatorSetField(op_quad_coords, "output", elem_restr_qx, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE); 164 165 CeedOperatorApply(op_quad_coords, x_coords, *qx_coords, CEED_REQUEST_IMMEDIATE); 166 167 CeedQFunctionDestroy(&qf_quad_coords); 168 CeedOperatorDestroy(&op_quad_coords); 169 PetscFunctionReturn(0); 170 } 171 172 // Create PetscSF for child-to-parent communication 173 PetscErrorCode CreateStatsSF(Ceed ceed, CeedData ceed_data, DM parentdm, DM childdm, PetscSF statssf) { 174 PetscInt child_num_qpnts, parent_num_qpnts, num_comp_x; 175 CeedVector child_qx_coords, parent_qx_coords; 176 PetscReal *child_coords, *parent_coords; 177 PetscFunctionBeginUser; 178 179 // Assume that child and parent have the same number of components 180 CeedBasisGetNumComponents(ceed_data->basis_x, &num_comp_x); 181 const PetscInt num_comp_sf = num_comp_x - 1; // Number of coord components used in the creation of the SF 182 183 // Get quad_coords for child DM 184 PetscCall(GetQuadratureCoords(ceed, childdm, ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord, &child_qx_coords, &child_num_qpnts)); 185 186 // Get quad_coords for parent DM 187 PetscCall(GetQuadratureCoords(ceed, parentdm, ceed_data->spanstats.elem_restr_parent_x, ceed_data->spanstats.basis_x, ceed_data->spanstats.x_coord, 188 &parent_qx_coords, &parent_num_qpnts)); 189 190 // Remove z component of coordinates for matching 191 { 192 const PetscReal *child_quad_coords, *parent_quad_coords; 193 194 CeedVectorGetArrayRead(child_qx_coords, CEED_MEM_HOST, &child_quad_coords); 195 CeedVectorGetArrayRead(parent_qx_coords, CEED_MEM_HOST, &parent_quad_coords); 196 197 PetscCall(PetscMalloc2(child_num_qpnts * 2, &child_coords, parent_num_qpnts * 2, &parent_coords)); 198 for (int i = 0; i < child_num_qpnts; i++) { 199 child_coords[0 + i * num_comp_sf] = child_quad_coords[0 + i * num_comp_x]; 200 child_coords[1 + i * num_comp_sf] = child_quad_coords[1 + i * num_comp_x]; 201 } 202 for (int i = 0; i < parent_num_qpnts; i++) { 203 parent_coords[0 + i * num_comp_sf] = parent_quad_coords[0 + i * num_comp_x]; 204 parent_coords[1 + i * num_comp_sf] = parent_quad_coords[1 + i * num_comp_x]; 205 } 206 CeedVectorRestoreArrayRead(child_qx_coords, &child_quad_coords); 207 CeedVectorRestoreArrayRead(parent_qx_coords, &parent_quad_coords); 208 } 209 210 PetscCall(PetscSFSetGraphFromCoordinates(statssf, parent_num_qpnts, child_num_qpnts, num_comp_sf, 1e-12, parent_coords, child_coords)); 211 212 PetscCall(PetscSFViewFromOptions(statssf, NULL, "-spanstats_sf_view")); 213 214 PetscCall(PetscFree2(child_coords, parent_coords)); 215 CeedVectorDestroy(&child_qx_coords); 216 CeedVectorDestroy(&parent_qx_coords); 217 PetscFunctionReturn(0); 218 } 219 220 // Compute mass matrix for statistics projection 221 PetscErrorCode SetupL2ProjectionStats(Ceed ceed, User user, CeedData ceed_data) { 222 CeedQFunction qf_mass; 223 CeedOperator op_mass; 224 CeedInt num_comp_q, q_data_size; 225 MPI_Comm comm = PetscObjectComm((PetscObject)user->spanstats.dm); 226 PetscFunctionBeginUser; 227 228 // CEED Restriction 229 CeedElemRestrictionGetNumComponents(ceed_data->spanstats.elem_restr_parent_stats, &num_comp_q); 230 CeedElemRestrictionGetNumComponents(ceed_data->spanstats.elem_restr_parent_qd, &q_data_size); 231 232 // Create Mass CeedOperator 233 PetscCall(CreateMassQFunction(ceed, num_comp_q, q_data_size, &qf_mass)); 234 CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass); 235 CeedOperatorSetField(op_mass, "q", ceed_data->spanstats.elem_restr_parent_stats, ceed_data->spanstats.basis_stats, CEED_VECTOR_ACTIVE); 236 CeedOperatorSetField(op_mass, "qdata", ceed_data->spanstats.elem_restr_parent_qd, CEED_BASIS_COLLOCATED, ceed_data->spanstats.q_data); 237 CeedOperatorSetField(op_mass, "v", ceed_data->spanstats.elem_restr_parent_stats, ceed_data->spanstats.basis_stats, CEED_VECTOR_ACTIVE); 238 239 { // Setup KSP for L^2 projection 240 MatopApplyContext M_ctx; 241 PetscInt l_size, g_size; 242 Mat mat_mass; 243 VecType vec_type; 244 KSP ksp; 245 Vec ones, M_inv; 246 CeedVector x_ceed, y_ceed; 247 248 PetscCall(DMCreateGlobalVector(user->spanstats.dm, &M_inv)); 249 PetscCall(VecGetLocalSize(M_inv, &l_size)); 250 PetscCall(VecGetSize(M_inv, &g_size)); 251 PetscCall(VecGetType(M_inv, &vec_type)); 252 253 CeedElemRestrictionCreateVector(ceed_data->spanstats.elem_restr_parent_stats, &x_ceed, NULL); 254 CeedElemRestrictionCreateVector(ceed_data->spanstats.elem_restr_parent_stats, &y_ceed, NULL); 255 PetscCall(MatopApplyContextCreate(user->spanstats.dm, user->ceed, op_mass, x_ceed, y_ceed, NULL, &M_ctx)); 256 CeedVectorDestroy(&x_ceed); 257 CeedVectorDestroy(&y_ceed); 258 259 PetscCall(MatCreateShell(comm, l_size, l_size, g_size, g_size, M_ctx, &mat_mass)); 260 PetscCall(MatShellSetContextDestroy(mat_mass, (PetscErrorCode(*)(void *))MatopApplyContextDestroy)); 261 PetscCall(MatShellSetOperation(mat_mass, MATOP_MULT, (void (*)(void))MatMult_Ceed)); 262 PetscCall(MatShellSetOperation(mat_mass, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiag_Ceed)); 263 PetscCall(MatShellSetVecType(mat_mass, vec_type)); 264 265 // Create lumped mass matrix inverse 266 PetscCall(DMGetGlobalVector(user->spanstats.dm, &ones)); 267 PetscCall(VecZeroEntries(M_inv)); 268 PetscCall(VecSet(ones, 1)); 269 PetscCall(MatMult(mat_mass, ones, M_inv)); 270 PetscCall(VecReciprocal(M_inv)); 271 user->spanstats.M_inv = M_inv; 272 PetscCall(DMRestoreGlobalVector(user->spanstats.dm, &ones)); 273 274 PetscCall(KSPCreate(comm, &ksp)); 275 PetscCall(KSPSetOptionsPrefix(ksp, "turbulence_spanstats_")); 276 { 277 PC pc; 278 PetscCall(KSPGetPC(ksp, &pc)); 279 PetscCall(PCSetType(pc, PCJACOBI)); 280 PetscCall(PCJacobiSetType(pc, PC_JACOBI_DIAGONAL)); 281 PetscCall(KSPSetType(ksp, KSPCG)); 282 PetscCall(KSPSetNormType(ksp, KSP_NORM_NATURAL)); 283 PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 284 } 285 PetscCall(KSPSetOperators(ksp, mat_mass, mat_mass)); 286 PetscCall(KSPSetFromOptions(ksp)); 287 user->spanstats.ksp = ksp; 288 } 289 290 // Cleanup 291 CeedQFunctionDestroy(&qf_mass); 292 CeedOperatorDestroy(&op_mass); 293 PetscFunctionReturn(0); 294 } 295 296 // Create CeedOperators and KSP for the statistics collection and processing 297 PetscErrorCode CreateStatisticsOperators(Ceed ceed, User user, CeedData ceed_data, ProblemData *problem) { 298 CeedInt num_comp_stats = user->spanstats.num_comp_stats, num_comp_x = problem->dim, num_comp_q; 299 CeedOperator op_setup_sur; 300 Turbulence_SpanStatsContext collect_ctx; 301 NewtonianIdealGasContext newtonian_ig_ctx; 302 CeedQFunctionContext collect_context; 303 PetscFunctionBeginUser; 304 CeedBasisGetNumComponents(ceed_data->basis_q, &num_comp_q); 305 306 // Create Operator for statistics collection 307 switch (user->phys->state_var) { 308 case STATEVAR_PRIMITIVE: 309 CeedQFunctionCreateInterior(ceed, 1, ChildStatsCollection_Prim, ChildStatsCollection_Prim_loc, &ceed_data->spanstats.qf_stats_collect); 310 break; 311 case STATEVAR_CONSERVATIVE: 312 CeedQFunctionCreateInterior(ceed, 1, ChildStatsCollection_Conserv, ChildStatsCollection_Conserv_loc, &ceed_data->spanstats.qf_stats_collect); 313 break; 314 } 315 316 if (user->spanstats.do_mms_test) { 317 CeedQFunctionDestroy(&ceed_data->spanstats.qf_stats_collect); 318 CeedQFunctionCreateInterior(ceed, 1, ChildStatsCollectionMMSTest, ChildStatsCollectionMMSTest_loc, &ceed_data->spanstats.qf_stats_collect); 319 } 320 321 // Setup Collection Context 322 { 323 PetscCall(PetscNew(&collect_ctx)); 324 CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, CEED_MEM_HOST, &newtonian_ig_ctx); 325 collect_ctx->gas = *newtonian_ig_ctx; 326 327 CeedQFunctionContextCreate(user->ceed, &collect_context); 328 CeedQFunctionContextSetData(collect_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*collect_ctx), collect_ctx); 329 CeedQFunctionContextSetDataDestroy(collect_context, CEED_MEM_HOST, FreeContextPetsc); 330 331 CeedQFunctionContextRegisterDouble(collect_context, "solution time", offsetof(struct Turbulence_SpanStatsContext_, solution_time), 1, 332 "Current solution time"); 333 CeedQFunctionContextRegisterDouble(collect_context, "previous time", offsetof(struct Turbulence_SpanStatsContext_, previous_time), 1, 334 "Previous time statistics collection was done"); 335 336 CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, &newtonian_ig_ctx); 337 } 338 339 CeedQFunctionSetContext(ceed_data->spanstats.qf_stats_collect, collect_context); 340 CeedQFunctionContextDestroy(&collect_context); 341 CeedQFunctionAddInput(ceed_data->spanstats.qf_stats_collect, "q", num_comp_q, CEED_EVAL_INTERP); 342 CeedQFunctionAddInput(ceed_data->spanstats.qf_stats_collect, "q_data", problem->q_data_size_vol, CEED_EVAL_NONE); 343 CeedQFunctionAddInput(ceed_data->spanstats.qf_stats_collect, "x", num_comp_x, CEED_EVAL_INTERP); 344 CeedQFunctionAddOutput(ceed_data->spanstats.qf_stats_collect, "v", num_comp_stats, CEED_EVAL_NONE); 345 346 CeedOperatorCreate(ceed, ceed_data->spanstats.qf_stats_collect, NULL, NULL, &user->spanstats.op_stats_collect); 347 CeedOperatorSetField(user->spanstats.op_stats_collect, "q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE); 348 CeedOperatorSetField(user->spanstats.op_stats_collect, "q_data", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data); 349 CeedOperatorSetField(user->spanstats.op_stats_collect, "x", ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord); 350 CeedOperatorSetField(user->spanstats.op_stats_collect, "v", ceed_data->spanstats.elem_restr_child_colloc, CEED_BASIS_COLLOCATED, 351 CEED_VECTOR_ACTIVE); 352 353 CeedOperatorContextGetFieldLabel(user->spanstats.op_stats_collect, "solution time", &user->spanstats.solution_time_label); 354 CeedOperatorContextGetFieldLabel(user->spanstats.op_stats_collect, "previous time", &user->spanstats.previous_time_label); 355 356 // Create Operator for L^2 projection of statistics 357 // Simply take collocated parent data (with quadrature weight already applied) and multiply by weight function. 358 // Therefore, an Identity QF is sufficient 359 CeedQFunctionCreateIdentity(ceed, num_comp_stats, CEED_EVAL_NONE, CEED_EVAL_INTERP, &ceed_data->spanstats.qf_stats_proj); 360 361 CeedOperatorCreate(ceed, ceed_data->spanstats.qf_stats_proj, NULL, NULL, &user->spanstats.op_stats_proj); 362 CeedOperatorSetField(user->spanstats.op_stats_proj, "input", ceed_data->spanstats.elem_restr_parent_colloc, CEED_BASIS_COLLOCATED, 363 CEED_VECTOR_ACTIVE); 364 CeedOperatorSetField(user->spanstats.op_stats_proj, "output", ceed_data->spanstats.elem_restr_parent_stats, ceed_data->spanstats.basis_stats, 365 CEED_VECTOR_ACTIVE); 366 367 // Get q_data for lumped mass matrix formation 368 CeedOperatorCreate(ceed, ceed_data->qf_setup_sur, NULL, NULL, &op_setup_sur); 369 CeedOperatorSetField(op_setup_sur, "dx", ceed_data->spanstats.elem_restr_parent_x, ceed_data->spanstats.basis_x, CEED_VECTOR_ACTIVE); 370 CeedOperatorSetField(op_setup_sur, "weight", CEED_ELEMRESTRICTION_NONE, ceed_data->spanstats.basis_x, CEED_VECTOR_NONE); 371 CeedOperatorSetField(op_setup_sur, "surface qdata", ceed_data->spanstats.elem_restr_parent_qd, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE); 372 CeedOperatorApply(op_setup_sur, ceed_data->spanstats.x_coord, ceed_data->spanstats.q_data, CEED_REQUEST_IMMEDIATE); 373 374 CeedOperatorDestroy(&op_setup_sur); 375 PetscFunctionReturn(0); 376 } 377 378 // Creates operator for calculating error of method of manufactured solution (MMS) test 379 PetscErrorCode SetupMMSErrorChecking(Ceed ceed, User user, CeedData ceed_data) { 380 CeedInt num_comp_stats = user->spanstats.num_comp_stats, num_comp_x; 381 CeedQFunction qf_error; 382 CeedOperator op_error; 383 CeedInt q_data_size; 384 CeedVector x_ceed, y_ceed; 385 PetscFunctionBeginUser; 386 387 CeedElemRestrictionGetNumComponents(ceed_data->spanstats.elem_restr_parent_qd, &q_data_size); 388 CeedBasisGetNumComponents(ceed_data->spanstats.basis_x, &num_comp_x); 389 390 CeedQFunctionCreateInterior(ceed, 1, ChildStatsCollectionMMSTest_Error, ChildStatsCollectionMMSTest_Error_loc, &qf_error); 391 CeedQFunctionAddInput(qf_error, "q", num_comp_stats, CEED_EVAL_INTERP); 392 CeedQFunctionAddInput(qf_error, "qdata", q_data_size, CEED_EVAL_NONE); 393 CeedQFunctionAddInput(qf_error, "x", num_comp_x, CEED_EVAL_INTERP); 394 CeedQFunctionAddOutput(qf_error, "v", num_comp_stats, CEED_EVAL_INTERP); 395 396 CeedOperatorCreate(ceed, qf_error, NULL, NULL, &op_error); 397 CeedOperatorSetField(op_error, "q", ceed_data->spanstats.elem_restr_parent_stats, ceed_data->spanstats.basis_stats, CEED_VECTOR_ACTIVE); 398 CeedOperatorSetField(op_error, "qdata", ceed_data->spanstats.elem_restr_parent_qd, CEED_BASIS_COLLOCATED, ceed_data->spanstats.q_data); 399 CeedOperatorSetField(op_error, "x", ceed_data->spanstats.elem_restr_parent_x, ceed_data->spanstats.basis_x, ceed_data->spanstats.x_coord); 400 CeedOperatorSetField(op_error, "v", ceed_data->spanstats.elem_restr_parent_stats, ceed_data->spanstats.basis_stats, CEED_VECTOR_ACTIVE); 401 402 CeedElemRestrictionCreateVector(ceed_data->spanstats.elem_restr_parent_stats, &x_ceed, NULL); 403 CeedElemRestrictionCreateVector(ceed_data->spanstats.elem_restr_parent_stats, &y_ceed, NULL); 404 PetscCall(MatopApplyContextCreate(user->spanstats.dm, user->ceed, op_error, x_ceed, y_ceed, NULL, &user->spanstats.mms_error_ctx)); 405 406 CeedOperatorDestroy(&op_error); 407 CeedVectorDestroy(&x_ceed); 408 CeedVectorDestroy(&y_ceed); 409 PetscFunctionReturn(0); 410 } 411 412 // Setup for statistics collection 413 PetscErrorCode SetupStatsCollection(Ceed ceed, User user, CeedData ceed_data, ProblemData *problem) { 414 DM dm = user->spanstats.dm; 415 MPI_Comm comm = PetscObjectComm((PetscObject)dm); 416 CeedInt dim, P, Q, num_comp_x; 417 Vec X_loc; 418 PetscMemType X_loc_memtype; 419 const PetscScalar *X_loc_array; 420 PetscLogStage stage_stats_setup; 421 PetscFunctionBeginUser; 422 423 PetscCall(PetscLogStageGetId("Stats Setup", &stage_stats_setup)); 424 if (stage_stats_setup == -1) PetscCall(PetscLogStageRegister("Stats Setup", &stage_stats_setup)); 425 PetscCall(PetscLogStagePush(stage_stats_setup)); 426 427 PetscCall(DMGetDimension(dm, &dim)); 428 CeedBasisGetNumQuadraturePoints1D(ceed_data->basis_q, &Q); 429 CeedBasisGetNumNodes1D(ceed_data->basis_q, &P); 430 431 PetscCall(GetRestrictionForDomain(ceed, dm, 0, 0, 0, Q, problem->q_data_size_sur, &ceed_data->spanstats.elem_restr_parent_stats, 432 &ceed_data->spanstats.elem_restr_parent_x, &ceed_data->spanstats.elem_restr_parent_qd)); 433 CeedElemRestrictionGetNumComponents(ceed_data->spanstats.elem_restr_parent_x, &num_comp_x); 434 CeedElemRestrictionCreateVector(ceed_data->spanstats.elem_restr_parent_x, &ceed_data->spanstats.x_coord, NULL); 435 CeedElemRestrictionCreateVector(ceed_data->spanstats.elem_restr_parent_stats, &user->spanstats.rhs_ceed, NULL); 436 CeedElemRestrictionCreateVector(ceed_data->spanstats.elem_restr_parent_qd, &ceed_data->spanstats.q_data, NULL); 437 438 CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp_x, 2, Q, CEED_GAUSS, &ceed_data->spanstats.basis_x); 439 CeedBasisCreateTensorH1Lagrange(ceed, dim, user->spanstats.num_comp_stats, P, Q, CEED_GAUSS, &ceed_data->spanstats.basis_stats); 440 441 PetscCall(CreateElemRestrColloc(ceed, user->spanstats.num_comp_stats, ceed_data->spanstats.basis_stats, 442 ceed_data->spanstats.elem_restr_parent_stats, &ceed_data->spanstats.elem_restr_parent_colloc, 443 &user->spanstats.parent_stats, NULL)); 444 PetscCall(CreateElemRestrColloc(ceed, user->spanstats.num_comp_stats, ceed_data->basis_q, ceed_data->elem_restr_q, 445 &ceed_data->spanstats.elem_restr_child_colloc, &user->spanstats.child_stats, NULL)); 446 CeedVectorSetValue(user->spanstats.child_stats, 0); 447 448 { // -- Copy DM coordinates into CeedVector 449 DM cdm; 450 PetscCall(DMGetCellCoordinateDM(dm, &cdm)); 451 if (cdm) { 452 PetscCall(DMGetCellCoordinatesLocal(dm, &X_loc)); 453 } else { 454 PetscCall(DMGetCoordinatesLocal(dm, &X_loc)); 455 } 456 } 457 PetscCall(VecScale(X_loc, problem->dm_scale)); 458 PetscCall(VecGetArrayReadAndMemType(X_loc, &X_loc_array, &X_loc_memtype)); 459 CeedVectorSetArray(ceed_data->spanstats.x_coord, MemTypeP2C(X_loc_memtype), CEED_COPY_VALUES, (PetscScalar *)X_loc_array); 460 PetscCall(VecRestoreArrayRead(X_loc, &X_loc_array)); 461 462 // Create SF for communicating child data back their respective parents 463 PetscCall(PetscSFCreate(comm, &user->spanstats.sf)); 464 PetscCall(CreateStatsSF(ceed, ceed_data, user->dm, user->spanstats.dm, user->spanstats.sf)); 465 466 // Create CeedOperators for statistics collection 467 PetscCall(CreateStatisticsOperators(ceed, user, ceed_data, problem)); 468 469 // Setup KSP and Mat for L^2 projection of statistics 470 PetscCall(SetupL2ProjectionStats(ceed, user, ceed_data)); 471 472 PetscCall(PetscOptionsGetBool(NULL, NULL, "-ts_monitor_turbulence_spanstats_mms", &user->spanstats.do_mms_test, NULL)); 473 if (user->spanstats.do_mms_test) { 474 PetscCall(SetupMMSErrorChecking(ceed, user, ceed_data)); 475 } 476 477 { // Setup stats viewer with prefix 478 PetscViewerType viewer_type; 479 PetscCall(PetscViewerGetType(user->app_ctx->turb_spanstats_viewer, &viewer_type)); 480 PetscCall(PetscOptionsSetValue(NULL, "-ts_monitor_turbulence_spanstats_viewer_type", viewer_type)); 481 482 PetscCall(PetscViewerSetOptionsPrefix(user->app_ctx->turb_spanstats_viewer, "ts_monitor_turbulence_spanstats_")); 483 PetscCall(PetscViewerSetFromOptions(user->app_ctx->turb_spanstats_viewer)); 484 } 485 486 PetscCall(PetscLogStagePop()); 487 PetscFunctionReturn(0); 488 } 489 490 // Collect statistics based on the solution Q 491 PetscErrorCode CollectStatistics(User user, PetscScalar solution_time, Vec Q) { 492 PetscMemType q_mem_type; 493 const PetscScalar *q_arr; 494 PetscFunctionBeginUser; 495 496 PetscLogStage stage_stats_collect; 497 PetscCall(PetscLogStageGetId("Stats Collect", &stage_stats_collect)); 498 if (stage_stats_collect == -1) PetscCall(PetscLogStageRegister("Stats Collect", &stage_stats_collect)); 499 PetscCall(PetscLogStagePush(stage_stats_collect)); 500 501 PetscCall(UpdateBoundaryValues(user, user->Q_loc, solution_time)); 502 CeedOperatorContextSetDouble(user->spanstats.op_stats_collect, user->spanstats.solution_time_label, &solution_time); 503 PetscCall(DMGlobalToLocal(user->dm, Q, INSERT_VALUES, user->Q_loc)); 504 PetscCall(VecGetArrayReadAndMemType(user->Q_loc, &q_arr, &q_mem_type)); 505 CeedVectorSetArray(user->q_ceed, MemTypeP2C(q_mem_type), CEED_USE_POINTER, (PetscScalar *)q_arr); 506 507 CeedOperatorApplyAdd(user->spanstats.op_stats_collect, user->q_ceed, user->spanstats.child_stats, CEED_REQUEST_IMMEDIATE); 508 509 CeedVectorTakeArray(user->q_ceed, MemTypeP2C(q_mem_type), NULL); 510 PetscCall(VecRestoreArrayReadAndMemType(user->Q_loc, &q_arr)); 511 512 CeedOperatorContextSetDouble(user->spanstats.op_stats_collect, user->spanstats.previous_time_label, &solution_time); 513 514 PetscCall(PetscLogStagePop()); 515 PetscFunctionReturn(0); 516 } 517 518 // Process the child statistics into parent statistics and project them onto stats 519 PetscErrorCode ProcessStatistics(User user, Vec stats) { 520 Span_Stats user_stats = user->spanstats; 521 const PetscScalar *child_stats; 522 PetscScalar *parent_stats; 523 MPI_Datatype unit; 524 Vec rhs_loc, rhs; 525 PetscMemType rhs_mem_type; 526 CeedScalar *rhs_arr; 527 CeedMemType ceed_mem_type; 528 PetscFunctionBeginUser; 529 530 PetscLogStage stage_stats_process; 531 PetscCall(PetscLogStageGetId("Stats Process", &stage_stats_process)); 532 if (stage_stats_process == -1) PetscCall(PetscLogStageRegister("Stats Process", &stage_stats_process)); 533 PetscCall(PetscLogStagePush(stage_stats_process)); 534 535 CeedGetPreferredMemType(user->ceed, &ceed_mem_type); 536 CeedVectorSetValue(user_stats.parent_stats, 0); 537 538 CeedVectorGetArrayRead(user_stats.child_stats, ceed_mem_type, &child_stats); 539 CeedVectorGetArray(user_stats.parent_stats, ceed_mem_type, &parent_stats); 540 541 if (user_stats.num_comp_stats == 1) unit = MPIU_REAL; 542 else { 543 PetscCallMPI(MPI_Type_contiguous(user_stats.num_comp_stats, MPIU_REAL, &unit)); 544 PetscCallMPI(MPI_Type_commit(&unit)); 545 } 546 547 PetscCall(PetscSFReduceBegin(user_stats.sf, unit, child_stats, parent_stats, MPI_SUM)); 548 PetscCall(PetscSFReduceEnd(user_stats.sf, unit, child_stats, parent_stats, MPI_SUM)); 549 550 CeedVectorRestoreArrayRead(user_stats.child_stats, &child_stats); 551 CeedVectorRestoreArray(user_stats.parent_stats, &parent_stats); 552 PetscCallMPI(MPI_Type_free(&unit)); 553 554 PetscReal solution_time; 555 PetscCall(DMGetOutputSequenceNumber(user_stats.dm, NULL, &solution_time)); 556 PetscReal summing_duration = solution_time - user->app_ctx->cont_time; 557 CeedVectorScale(user_stats.parent_stats, 1 / (summing_duration * user_stats.span_width)); 558 559 // L^2 projection with the parent_data 560 PetscCall(DMGetLocalVector(user_stats.dm, &rhs_loc)); 561 PetscCall(VecZeroEntries(rhs_loc)); 562 PetscCall(VecGetArrayWriteAndMemType(rhs_loc, &rhs_arr, &rhs_mem_type)); 563 CeedVectorSetArray(user_stats.rhs_ceed, MemTypeP2C(rhs_mem_type), CEED_USE_POINTER, (PetscScalar *)rhs_arr); 564 565 CeedOperatorApply(user_stats.op_stats_proj, user_stats.parent_stats, user_stats.rhs_ceed, CEED_REQUEST_IMMEDIATE); 566 567 CeedVectorTakeArray(user_stats.rhs_ceed, MemTypeP2C(rhs_mem_type), &rhs_arr); 568 PetscCall(VecRestoreArrayAndMemType(rhs_loc, &rhs_arr)); 569 570 PetscCall(DMGetGlobalVector(user_stats.dm, &rhs)); 571 PetscCall(VecZeroEntries(rhs)); 572 PetscCall(DMLocalToGlobal(user_stats.dm, rhs_loc, ADD_VALUES, rhs)); 573 PetscCall(DMRestoreLocalVector(user_stats.dm, &rhs_loc)); 574 575 PetscCall(KSPSolve(user_stats.ksp, rhs, stats)); 576 577 PetscCall(DMRestoreGlobalVector(user_stats.dm, &rhs)); 578 PetscCall(PetscLogStagePop()); 579 PetscFunctionReturn(0); 580 } 581 582 // TSMonitor for the statistics collection and processing 583 PetscErrorCode TSMonitor_Statistics(TS ts, PetscInt steps, PetscReal solution_time, Vec Q, void *ctx) { 584 User user = (User)ctx; 585 Vec stats; 586 TSConvergedReason reason; 587 PetscInt collect_interval = user->app_ctx->turb_spanstats_collect_interval, viewer_interval = user->app_ctx->turb_spanstats_viewer_interval; 588 PetscFunctionBeginUser; 589 PetscCall(TSGetConvergedReason(ts, &reason)); 590 // Do not collect or process on the first step of the run (ie. on the initial condition) 591 if (steps == user->app_ctx->cont_steps && reason == TS_CONVERGED_ITERATING) PetscFunctionReturn(0); 592 593 PetscBool run_processing_and_viewer = (steps % viewer_interval == 0 && viewer_interval != -1) || reason != TS_CONVERGED_ITERATING; 594 595 if (steps % collect_interval == 0 || run_processing_and_viewer) { 596 PetscCall(CollectStatistics(user, solution_time, Q)); 597 598 if (run_processing_and_viewer) { 599 PetscCall(DMSetOutputSequenceNumber(user->spanstats.dm, steps, solution_time)); 600 PetscCall(DMGetGlobalVector(user->spanstats.dm, &stats)); 601 PetscCall(ProcessStatistics(user, stats)); 602 if (user->app_ctx->test_type == TESTTYPE_NONE) { 603 PetscCall(PetscViewerPushFormat(user->app_ctx->turb_spanstats_viewer, user->app_ctx->turb_spanstats_viewer_format)); 604 PetscCall(VecView(stats, user->app_ctx->turb_spanstats_viewer)); 605 PetscCall(PetscViewerPopFormat(user->app_ctx->turb_spanstats_viewer)); 606 } 607 if (user->app_ctx->test_type == TESTTYPE_TURB_SPANSTATS && reason != TS_CONVERGED_ITERATING) { 608 PetscCall(RegressionTests_NS(user->app_ctx, stats)); 609 } 610 if (user->spanstats.do_mms_test && reason != TS_CONVERGED_ITERATING) { 611 Vec error; 612 PetscCall(VecDuplicate(stats, &error)); 613 PetscCall(ApplyLocal_Ceed(stats, error, user->spanstats.mms_error_ctx)); 614 PetscScalar error_sq = 0; 615 PetscCall(VecSum(error, &error_sq)); 616 PetscScalar l2_error = sqrt(error_sq); 617 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "l2 error: %.5e\n", l2_error)); 618 } 619 PetscCall(DMRestoreGlobalVector(user->spanstats.dm, &stats)); 620 } 621 } 622 PetscFunctionReturn(0); 623 } 624 625 PetscErrorCode DestroyStats(User user, CeedData ceed_data) { 626 PetscFunctionBeginUser; 627 628 // -- CeedVectors 629 CeedVectorDestroy(&user->spanstats.child_stats); 630 CeedVectorDestroy(&user->spanstats.parent_stats); 631 CeedVectorDestroy(&user->spanstats.rhs_ceed); 632 CeedVectorDestroy(&user->spanstats.x_ceed); 633 CeedVectorDestroy(&user->spanstats.y_ceed); 634 CeedVectorDestroy(&ceed_data->spanstats.x_coord); 635 CeedVectorDestroy(&ceed_data->spanstats.q_data); 636 637 // -- QFunctions 638 CeedQFunctionDestroy(&ceed_data->spanstats.qf_stats_collect); 639 CeedQFunctionDestroy(&ceed_data->spanstats.qf_stats_proj); 640 641 // -- CeedBasis 642 CeedBasisDestroy(&ceed_data->spanstats.basis_stats); 643 CeedBasisDestroy(&ceed_data->spanstats.basis_x); 644 645 // -- CeedElemRestriction 646 CeedElemRestrictionDestroy(&ceed_data->spanstats.elem_restr_parent_stats); 647 CeedElemRestrictionDestroy(&ceed_data->spanstats.elem_restr_parent_qd); 648 CeedElemRestrictionDestroy(&ceed_data->spanstats.elem_restr_parent_x); 649 CeedElemRestrictionDestroy(&ceed_data->spanstats.elem_restr_parent_colloc); 650 CeedElemRestrictionDestroy(&ceed_data->spanstats.elem_restr_child_colloc); 651 652 // -- CeedOperators 653 CeedOperatorDestroy(&user->spanstats.op_stats_collect); 654 CeedOperatorDestroy(&user->spanstats.op_stats_proj); 655 PetscCall(MatopApplyContextDestroy(user->spanstats.mms_error_ctx)); 656 657 // -- Vec 658 PetscCall(VecDestroy(&user->spanstats.M_inv)); 659 660 // -- KSP 661 PetscCall(KSPDestroy(&user->spanstats.ksp)); 662 663 // -- SF 664 PetscCall(PetscSFDestroy(&user->spanstats.sf)); 665 666 // -- DM 667 PetscCall(DMDestroy(&user->spanstats.dm)); 668 669 PetscFunctionReturn(0); 670 } 671