1 // Copyright (c) 2017-2023, 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 projecting the velocity gradient 9 10 #include "../qfunctions/velocity_gradient_projection.h" 11 12 #include <petscdmplex.h> 13 14 #include "../navierstokes.h" 15 16 PetscErrorCode VelocityGradientProjectionCreateDM(NodalProjectionData grad_velo_proj, User user, PetscInt degree) { 17 PetscFE fe; 18 PetscSection section; 19 PetscInt dim; 20 21 PetscFunctionBeginUser; 22 grad_velo_proj->num_comp = 9; // 9 velocity gradient 23 24 PetscCall(DMClone(user->dm, &grad_velo_proj->dm)); 25 PetscCall(DMGetDimension(grad_velo_proj->dm, &dim)); 26 PetscCall(PetscObjectSetName((PetscObject)grad_velo_proj->dm, "Velocity Gradient Projection")); 27 28 PetscInt q_order = user->app_ctx->degree + user->app_ctx->q_extra; 29 PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, grad_velo_proj->num_comp, PETSC_FALSE, degree, q_order, &fe)); 30 PetscCall(PetscObjectSetName((PetscObject)fe, "Velocity Gradient Projection")); 31 PetscCall(DMAddField(grad_velo_proj->dm, NULL, (PetscObject)fe)); 32 PetscCall(DMCreateDS(grad_velo_proj->dm)); 33 PetscCall(DMPlexSetClosurePermutationTensor(grad_velo_proj->dm, PETSC_DETERMINE, NULL)); 34 35 PetscCall(DMGetLocalSection(grad_velo_proj->dm, §ion)); 36 PetscCall(PetscSectionSetFieldName(section, 0, "")); 37 PetscCall(PetscSectionSetComponentName(section, 0, 0, "VelocityGradientXX")); 38 PetscCall(PetscSectionSetComponentName(section, 0, 1, "VelocityGradientXY")); 39 PetscCall(PetscSectionSetComponentName(section, 0, 2, "VelocityGradientXZ")); 40 PetscCall(PetscSectionSetComponentName(section, 0, 3, "VelocityGradientYX")); 41 PetscCall(PetscSectionSetComponentName(section, 0, 4, "VelocityGradientYY")); 42 PetscCall(PetscSectionSetComponentName(section, 0, 5, "VelocityGradientYZ")); 43 PetscCall(PetscSectionSetComponentName(section, 0, 6, "VelocityGradientZX")); 44 PetscCall(PetscSectionSetComponentName(section, 0, 7, "VelocityGradientZY")); 45 PetscCall(PetscSectionSetComponentName(section, 0, 8, "VelocityGradientZZ")); 46 47 PetscCall(PetscFEDestroy(&fe)); 48 PetscFunctionReturn(PETSC_SUCCESS); 49 }; 50 51 PetscErrorCode VelocityGradientProjectionSetup(Ceed ceed, User user, CeedData ceed_data, ProblemData *problem) { 52 OperatorApplyContext mass_matop_ctx; 53 CeedOperator op_rhs_assemble, op_mass; 54 CeedQFunction qf_rhs_assemble, qf_mass; 55 CeedBasis basis_grad_velo; 56 CeedElemRestriction elem_restr_grad_velo; 57 PetscInt dim; 58 CeedInt num_comp_x, num_comp_q, q_data_size; 59 60 PetscFunctionBeginUser; 61 PetscCall(PetscNew(&user->grad_velo_proj)); 62 NodalProjectionData grad_velo_proj = user->grad_velo_proj; 63 64 PetscCall(VelocityGradientProjectionCreateDM(grad_velo_proj, user, user->app_ctx->degree)); 65 66 // -- Get Pre-requisite things 67 PetscCall(DMGetDimension(grad_velo_proj->dm, &dim)); 68 CeedBasisGetNumComponents(ceed_data->basis_x, &num_comp_x); 69 CeedBasisGetNumComponents(ceed_data->basis_q, &num_comp_q); 70 CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size); 71 PetscCall(GetRestrictionForDomain(ceed, grad_velo_proj->dm, 0, 0, 0, 0, -1, 0, &elem_restr_grad_velo, NULL, NULL)); 72 73 PetscCall(CreateBasisFromPlex(ceed, grad_velo_proj->dm, 0, 0, 0, 0, &basis_grad_velo)); 74 75 // -- Build RHS operator 76 switch (user->phys->state_var) { 77 case STATEVAR_PRIMITIVE: 78 CeedQFunctionCreateInterior(ceed, 1, VelocityGradientProjectionRHS_Prim, VelocityGradientProjectionRHS_Prim_loc, &qf_rhs_assemble); 79 break; 80 case STATEVAR_CONSERVATIVE: 81 CeedQFunctionCreateInterior(ceed, 1, VelocityGradientProjectionRHS_Conserv, VelocityGradientProjectionRHS_Conserv_loc, &qf_rhs_assemble); 82 break; 83 default: 84 SETERRQ(PetscObjectComm((PetscObject)user->dm), PETSC_ERR_SUP, "No velocity gradient projection QFunction for chosen state variable"); 85 } 86 87 CeedQFunctionSetContext(qf_rhs_assemble, problem->apply_vol_ifunction.qfunction_context); 88 CeedQFunctionAddInput(qf_rhs_assemble, "q", num_comp_q, CEED_EVAL_INTERP); 89 CeedQFunctionAddInput(qf_rhs_assemble, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD); 90 CeedQFunctionAddInput(qf_rhs_assemble, "qdata", q_data_size, CEED_EVAL_NONE); 91 CeedQFunctionAddInput(qf_rhs_assemble, "x", num_comp_x, CEED_EVAL_INTERP); 92 CeedQFunctionAddOutput(qf_rhs_assemble, "velocity gradient", grad_velo_proj->num_comp, CEED_EVAL_INTERP); 93 94 CeedOperatorCreate(ceed, qf_rhs_assemble, NULL, NULL, &op_rhs_assemble); 95 CeedOperatorSetField(op_rhs_assemble, "q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE); 96 CeedOperatorSetField(op_rhs_assemble, "Grad_q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE); 97 CeedOperatorSetField(op_rhs_assemble, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data); 98 CeedOperatorSetField(op_rhs_assemble, "x", ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord); 99 CeedOperatorSetField(op_rhs_assemble, "velocity gradient", elem_restr_grad_velo, basis_grad_velo, CEED_VECTOR_ACTIVE); 100 101 PetscCall(OperatorApplyContextCreate(user->dm, grad_velo_proj->dm, ceed, op_rhs_assemble, NULL, NULL, NULL, NULL, &grad_velo_proj->l2_rhs_ctx)); 102 103 // -- Build Mass operator 104 PetscCall(CreateMassQFunction(ceed, grad_velo_proj->num_comp, q_data_size, &qf_mass)); 105 CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass); 106 CeedOperatorSetField(op_mass, "u", elem_restr_grad_velo, basis_grad_velo, CEED_VECTOR_ACTIVE); 107 CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data); 108 CeedOperatorSetField(op_mass, "v", elem_restr_grad_velo, basis_grad_velo, CEED_VECTOR_ACTIVE); 109 110 { // -- Setup KSP for L^2 projection with lumped mass operator 111 Mat mat_mass; 112 MPI_Comm comm = PetscObjectComm((PetscObject)grad_velo_proj->dm); 113 114 PetscCall(OperatorApplyContextCreate(grad_velo_proj->dm, grad_velo_proj->dm, ceed, op_mass, NULL, NULL, NULL, NULL, &mass_matop_ctx)); 115 PetscCall(CreateMatShell_Ceed(mass_matop_ctx, &mat_mass)); 116 117 PetscCall(KSPCreate(comm, &grad_velo_proj->ksp)); 118 PetscCall(KSPSetOptionsPrefix(grad_velo_proj->ksp, "velocity_gradient_projection_")); 119 { 120 PC pc; 121 PetscCall(KSPGetPC(grad_velo_proj->ksp, &pc)); 122 PetscCall(PCSetType(pc, PCJACOBI)); 123 PetscCall(PCJacobiSetType(pc, PC_JACOBI_ROWSUM)); 124 PetscCall(KSPSetType(grad_velo_proj->ksp, KSPPREONLY)); 125 // TODO Not sure if the option below are necessary 126 PetscCall(KSPSetConvergenceTest(grad_velo_proj->ksp, KSPConvergedSkip, NULL, NULL)); 127 } 128 PetscCall(KSPSetOperators(grad_velo_proj->ksp, mat_mass, mat_mass)); 129 PetscCall(KSPSetFromOptions(grad_velo_proj->ksp)); 130 } 131 132 CeedBasisDestroy(&basis_grad_velo); 133 CeedElemRestrictionDestroy(&elem_restr_grad_velo); 134 CeedQFunctionDestroy(&qf_rhs_assemble); 135 CeedQFunctionDestroy(&qf_mass); 136 CeedOperatorDestroy(&op_rhs_assemble); 137 CeedOperatorDestroy(&op_mass); 138 PetscFunctionReturn(PETSC_SUCCESS); 139 } 140 141 PetscErrorCode VelocityGradientProjectionApply(User user, Vec Q_loc, Vec VelocityGradient) { 142 NodalProjectionData grad_velo_proj = user->grad_velo_proj; 143 OperatorApplyContext l2_rhs_ctx = grad_velo_proj->l2_rhs_ctx; 144 145 PetscFunctionBeginUser; 146 PetscCall(ApplyCeedOperatorLocalToGlobal(Q_loc, VelocityGradient, l2_rhs_ctx)); 147 148 PetscCall(KSPSolve(grad_velo_proj->ksp, VelocityGradient, VelocityGradient)); 149 150 PetscFunctionReturn(PETSC_SUCCESS); 151 } 152