1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors. 2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause 3 /// @file 4 /// Functions for setting up and projecting the velocity gradient 5 6 #include "../qfunctions/velocity_gradient_projection.h" 7 8 #include <petscdmplex.h> 9 10 #include <navierstokes.h> 11 12 PetscErrorCode VelocityGradientProjectionCreateDM(NodalProjectionData grad_velo_proj, User user, PetscInt degree) { 13 PetscSection section; 14 15 PetscFunctionBeginUser; 16 grad_velo_proj->num_comp = 9; // 9 velocity gradient 17 18 PetscCall(DMClone(user->dm, &grad_velo_proj->dm)); 19 PetscCall(DMSetMatrixPreallocateSkip(grad_velo_proj->dm, PETSC_TRUE)); 20 PetscCall(PetscObjectSetName((PetscObject)grad_velo_proj->dm, "Velocity Gradient Projection")); 21 22 PetscCall( 23 DMSetupByOrder_FEM(PETSC_TRUE, PETSC_TRUE, user->app_ctx->degree, 1, user->app_ctx->q_extra, 1, &grad_velo_proj->num_comp, grad_velo_proj->dm)); 24 25 PetscCall(DMGetLocalSection(grad_velo_proj->dm, §ion)); 26 PetscCall(PetscSectionSetFieldName(section, 0, "")); 27 PetscCall(PetscSectionSetComponentName(section, 0, 0, "VelocityGradientXX")); 28 PetscCall(PetscSectionSetComponentName(section, 0, 1, "VelocityGradientXY")); 29 PetscCall(PetscSectionSetComponentName(section, 0, 2, "VelocityGradientXZ")); 30 PetscCall(PetscSectionSetComponentName(section, 0, 3, "VelocityGradientYX")); 31 PetscCall(PetscSectionSetComponentName(section, 0, 4, "VelocityGradientYY")); 32 PetscCall(PetscSectionSetComponentName(section, 0, 5, "VelocityGradientYZ")); 33 PetscCall(PetscSectionSetComponentName(section, 0, 6, "VelocityGradientZX")); 34 PetscCall(PetscSectionSetComponentName(section, 0, 7, "VelocityGradientZY")); 35 PetscCall(PetscSectionSetComponentName(section, 0, 8, "VelocityGradientZZ")); 36 PetscFunctionReturn(PETSC_SUCCESS); 37 }; 38 39 PetscErrorCode VelocityGradientProjectionSetup(Ceed ceed, User user, CeedData ceed_data, ProblemData problem, StateVariable state_var_input, 40 CeedElemRestriction elem_restr_input, CeedBasis basis_input, NodalProjectionData *pgrad_velo_proj) { 41 NodalProjectionData grad_velo_proj; 42 CeedBasis basis_grad_velo; 43 CeedElemRestriction elem_restr_grad_velo, elem_restr_qd; 44 CeedVector q_data; 45 PetscInt dim, label_value = 0, height = 0, dm_field = 0; 46 CeedInt num_comp_x, num_comp_input, q_data_size; 47 DMLabel domain_label = NULL; 48 49 PetscFunctionBeginUser; 50 PetscCall(PetscNew(&grad_velo_proj)); 51 PetscCall(VelocityGradientProjectionCreateDM(grad_velo_proj, user, user->app_ctx->degree)); 52 53 // -- Get Pre-requisite things 54 PetscCall(DMGetDimension(grad_velo_proj->dm, &dim)); 55 PetscCallCeed(ceed, CeedBasisGetNumComponents(ceed_data->basis_x, &num_comp_x)); 56 PetscCallCeed(ceed, CeedBasisGetNumComponents(ceed_data->basis_q, &num_comp_input)); 57 PetscCall(DMPlexCeedElemRestrictionCreate(ceed, grad_velo_proj->dm, domain_label, label_value, height, dm_field, &elem_restr_grad_velo)); 58 PetscCall(CreateBasisFromPlex(ceed, grad_velo_proj->dm, domain_label, label_value, height, dm_field, &basis_grad_velo)); 59 PetscCall(QDataGet(ceed, grad_velo_proj->dm, domain_label, label_value, ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord, 60 &elem_restr_qd, &q_data, &q_data_size)); 61 62 { // -- Build RHS operator 63 CeedOperator op_rhs_assemble; 64 CeedQFunction qf_rhs_assemble; 65 66 switch (state_var_input) { 67 case STATEVAR_PRIMITIVE: 68 PetscCallCeed( 69 ceed, CeedQFunctionCreateInterior(ceed, 1, VelocityGradientProjectionRHS_Prim, VelocityGradientProjectionRHS_Prim_loc, &qf_rhs_assemble)); 70 break; 71 case STATEVAR_CONSERVATIVE: 72 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, VelocityGradientProjectionRHS_Conserv, VelocityGradientProjectionRHS_Conserv_loc, 73 &qf_rhs_assemble)); 74 break; 75 case STATEVAR_ENTROPY: 76 PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, VelocityGradientProjectionRHS_Entropy, VelocityGradientProjectionRHS_Entropy_loc, 77 &qf_rhs_assemble)); 78 break; 79 } 80 81 PetscCallCeed(ceed, CeedQFunctionSetContext(qf_rhs_assemble, problem->apply_vol_ifunction.qfctx)); 82 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_assemble, "q", num_comp_input, CEED_EVAL_INTERP)); 83 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_assemble, "Grad_q", num_comp_input * dim, CEED_EVAL_GRAD)); 84 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_assemble, "qdata", q_data_size, CEED_EVAL_NONE)); 85 PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_assemble, "x", num_comp_x, CEED_EVAL_INTERP)); 86 PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_rhs_assemble, "velocity gradient", grad_velo_proj->num_comp, CEED_EVAL_INTERP)); 87 88 PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_rhs_assemble, NULL, NULL, &op_rhs_assemble)); 89 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_assemble, "q", elem_restr_input, basis_input, CEED_VECTOR_ACTIVE)); 90 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_assemble, "Grad_q", elem_restr_input, basis_input, CEED_VECTOR_ACTIVE)); 91 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_assemble, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data)); 92 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_assemble, "x", ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord)); 93 PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_assemble, "velocity gradient", elem_restr_grad_velo, basis_grad_velo, CEED_VECTOR_ACTIVE)); 94 95 PetscCall(OperatorApplyContextCreate(user->dm, grad_velo_proj->dm, ceed, op_rhs_assemble, NULL, NULL, NULL, NULL, &grad_velo_proj->l2_rhs_ctx)); 96 97 PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs_assemble)); 98 PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs_assemble)); 99 } 100 101 { // -- Build Mass operator 102 CeedOperator op_mass; 103 CeedQFunction qf_mass; 104 Mat mat_mass; 105 106 PetscCall(CreateMassQFunction(ceed, grad_velo_proj->num_comp, q_data_size, &qf_mass)); 107 PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass)); 108 PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "u", elem_restr_grad_velo, basis_grad_velo, CEED_VECTOR_ACTIVE)); 109 PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data)); 110 PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "v", elem_restr_grad_velo, basis_grad_velo, CEED_VECTOR_ACTIVE)); 111 112 PetscCall(MatCreateCeed(grad_velo_proj->dm, grad_velo_proj->dm, op_mass, NULL, &mat_mass)); 113 114 // Setup KSP for L^2 projection with lumped mass operator 115 PetscCall(KSPCreate(PetscObjectComm((PetscObject)grad_velo_proj->dm), &grad_velo_proj->ksp)); 116 PetscCall(KSPSetOptionsPrefix(grad_velo_proj->ksp, "velocity_gradient_projection_")); 117 { 118 PC pc; 119 PetscCall(KSPGetPC(grad_velo_proj->ksp, &pc)); 120 PetscCall(PCSetType(pc, PCJACOBI)); 121 PetscCall(PCJacobiSetType(pc, PC_JACOBI_ROWSUM)); 122 PetscCall(KSPSetType(grad_velo_proj->ksp, KSPPREONLY)); 123 } 124 PetscCall(KSPSetFromOptions_WithMatCeed(grad_velo_proj->ksp, mat_mass)); 125 126 PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass)); 127 PetscCallCeed(ceed, CeedOperatorDestroy(&op_mass)); 128 PetscCall(MatDestroy(&mat_mass)); 129 } 130 131 *pgrad_velo_proj = grad_velo_proj; 132 133 PetscCallCeed(ceed, CeedBasisDestroy(&basis_grad_velo)); 134 PetscCallCeed(ceed, CeedVectorDestroy(&q_data)); 135 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd)); 136 PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_grad_velo)); 137 PetscFunctionReturn(PETSC_SUCCESS); 138 } 139 140 PetscErrorCode VelocityGradientProjectionApply(NodalProjectionData grad_velo_proj, Vec Q_loc, Vec VelocityGradient) { 141 OperatorApplyContext l2_rhs_ctx = grad_velo_proj->l2_rhs_ctx; 142 143 PetscFunctionBeginUser; 144 PetscCall(PetscLogEventBegin(FLUIDS_VelocityGradientProjection, Q_loc, VelocityGradient, 0, 0)); 145 PetscCall(ApplyCeedOperatorLocalToGlobal(Q_loc, VelocityGradient, l2_rhs_ctx)); 146 147 PetscCall(KSPSolve(grad_velo_proj->ksp, VelocityGradient, VelocityGradient)); 148 PetscCall(PetscLogEventEnd(FLUIDS_VelocityGradientProjection, Q_loc, VelocityGradient, 0, 0)); 149 PetscFunctionReturn(PETSC_SUCCESS); 150 } 151