1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors. 2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause 3c38c977aSJames Wright 4c38c977aSJames Wright #include "../qfunctions/grid_anisotropy_tensor.h" 5c38c977aSJames Wright 6c38c977aSJames Wright #include <petscdmplex.h> 7c38c977aSJames Wright 8149fb536SJames Wright #include <navierstokes.h> 9c38c977aSJames Wright 10c38c977aSJames Wright PetscErrorCode GridAnisotropyTensorProjectionSetupApply(Ceed ceed, User user, CeedData ceed_data, CeedElemRestriction *elem_restr_grid_aniso, 11c38c977aSJames Wright CeedVector *grid_aniso_vector) { 12c38c977aSJames Wright NodalProjectionData grid_aniso_proj; 13b8462d76SJames Wright OperatorApplyContext l2_rhs_ctx; 14c38c977aSJames Wright CeedOperator op_rhs_assemble, op_mass; 15c38c977aSJames Wright CeedQFunction qf_rhs_assemble, qf_mass; 16c38c977aSJames Wright CeedBasis basis_grid_aniso; 17*be29160dSJames Wright CeedVector q_data; 18*be29160dSJames Wright CeedElemRestriction elem_restr_qd; 1967263decSKenneth E. Jansen CeedInt q_data_size; 20c38c977aSJames Wright MPI_Comm comm = PetscObjectComm((PetscObject)user->dm); 21c38c977aSJames Wright KSP ksp; 2215c18037SJames Wright DMLabel domain_label = NULL; 2315c18037SJames Wright PetscInt label_value = 0, height = 0, dm_field = 0; 24c38c977aSJames Wright 25c38c977aSJames Wright PetscFunctionBeginUser; 26c38c977aSJames Wright PetscCall(PetscNew(&grid_aniso_proj)); 27c38c977aSJames Wright 28c38c977aSJames Wright // -- Create DM for Anisotropic tensor L^2 projection 29c38c977aSJames Wright grid_aniso_proj->num_comp = 7; 30c38c977aSJames Wright PetscCall(DMClone(user->dm, &grid_aniso_proj->dm)); 310dee9b8eSJames Wright PetscCall(DMSetMatrixPreallocateSkip(grid_aniso_proj->dm, PETSC_TRUE)); 32c38c977aSJames Wright PetscCall(PetscObjectSetName((PetscObject)grid_aniso_proj->dm, "Grid Anisotropy Tensor Projection")); 33c38c977aSJames Wright 34c38c977aSJames Wright { // -- Setup DM 35c38c977aSJames Wright PetscSection section; 36da4ca0cfSJames Wright PetscCall(DMSetupByOrder_FEM(PETSC_TRUE, PETSC_TRUE, user->app_ctx->degree, 1, user->app_ctx->q_extra, 1, &grid_aniso_proj->num_comp, 37da4ca0cfSJames Wright grid_aniso_proj->dm)); 38c38c977aSJames Wright 39c38c977aSJames Wright PetscCall(DMGetLocalSection(grid_aniso_proj->dm, §ion)); 40c38c977aSJames Wright PetscCall(PetscSectionSetFieldName(section, 0, "")); 41c38c977aSJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 0, "KMGridAnisotropyTensorXX")); 42c38c977aSJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 1, "KMGridAnisotropyTensorYY")); 43c38c977aSJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 2, "KMGridAnisotropyTensorZZ")); 44c38c977aSJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 3, "KMGridAnisotropyTensorYZ")); 45c38c977aSJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 4, "KMGridAnisotropyTensorXZ")); 46c38c977aSJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 5, "KMGridAnisotropyTensorXY")); 47c38c977aSJames Wright PetscCall(PetscSectionSetComponentName(section, 0, 6, "GridAnisotropyTensorFrobNorm")); 48c38c977aSJames Wright } 49c38c977aSJames Wright 50c38c977aSJames Wright // -- Get Pre-requisite things 5115c18037SJames Wright PetscCall(DMPlexCeedElemRestrictionCreate(ceed, grid_aniso_proj->dm, domain_label, label_value, height, dm_field, elem_restr_grid_aniso)); 5215c18037SJames Wright PetscCall(CreateBasisFromPlex(ceed, grid_aniso_proj->dm, domain_label, label_value, height, dm_field, &basis_grid_aniso)); 53*be29160dSJames Wright PetscCall(QDataGet(ceed, grid_aniso_proj->dm, domain_label, label_value, ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord, 54*be29160dSJames Wright &elem_restr_qd, &q_data, &q_data_size)); 55c38c977aSJames Wright 56c38c977aSJames Wright // -- Build RHS operator 57*be29160dSJames Wright //TODO: Fix scoping of functions 58b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, AnisotropyTensorProjection, AnisotropyTensorProjection_loc, &qf_rhs_assemble)); 59b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_assemble, "qdata", q_data_size, CEED_EVAL_NONE)); 60b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_rhs_assemble, "v", grid_aniso_proj->num_comp, CEED_EVAL_INTERP)); 61c38c977aSJames Wright 62b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_rhs_assemble, NULL, NULL, &op_rhs_assemble)); 63*be29160dSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_assemble, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data)); 64b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_assemble, "v", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE)); 65c38c977aSJames Wright 6649f5db74SJames Wright PetscCall(OperatorApplyContextCreate(user->dm, grid_aniso_proj->dm, ceed, op_rhs_assemble, CEED_VECTOR_NONE, NULL, NULL, NULL, &l2_rhs_ctx)); 67c38c977aSJames Wright 68c38c977aSJames Wright // -- Build Mass Operator 69c38c977aSJames Wright PetscCall(CreateMassQFunction(ceed, grid_aniso_proj->num_comp, q_data_size, &qf_mass)); 70b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass)); 71b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "u", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE)); 72*be29160dSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data)); 73b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "v", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE)); 74c38c977aSJames Wright 75c38c977aSJames Wright { // -- Setup KSP for L^2 projection 763170c09fSJames Wright Mat mat_mass; 77b8462d76SJames Wright 78000d2032SJeremy L Thompson PetscCall(MatCreateCeed(grid_aniso_proj->dm, grid_aniso_proj->dm, op_mass, NULL, &mat_mass)); 79c38c977aSJames Wright 80c38c977aSJames Wright PetscCall(KSPCreate(comm, &ksp)); 81c38c977aSJames Wright PetscCall(KSPSetOptionsPrefix(ksp, "grid_anisotropy_tensor_projection_")); 82c38c977aSJames Wright { 83c38c977aSJames Wright PC pc; 84c38c977aSJames Wright PetscCall(KSPGetPC(ksp, &pc)); 85c38c977aSJames Wright PetscCall(PCSetType(pc, PCJACOBI)); 86c38c977aSJames Wright PetscCall(PCJacobiSetType(pc, PC_JACOBI_DIAGONAL)); 87c38c977aSJames Wright PetscCall(KSPSetType(ksp, KSPCG)); 88c38c977aSJames Wright PetscCall(KSPSetNormType(ksp, KSP_NORM_NATURAL)); 89c38c977aSJames Wright PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 90c38c977aSJames Wright } 913170c09fSJames Wright PetscCall(KSPSetFromOptions_WithMatCeed(ksp, mat_mass)); 92b8462d76SJames Wright PetscCall(MatDestroy(&mat_mass)); 93c38c977aSJames Wright } 94c38c977aSJames Wright 95c38c977aSJames Wright { // -- Project anisotropy data and store in CeedVector 96c38c977aSJames Wright Vec Grid_Anisotropy, grid_anisotropy_loc; 97c38c977aSJames Wright 98c38c977aSJames Wright // Get L^2 Projection RHS 99c38c977aSJames Wright PetscCall(DMGetGlobalVector(grid_aniso_proj->dm, &Grid_Anisotropy)); 100c38c977aSJames Wright 10149f5db74SJames Wright PetscCall(ApplyCeedOperatorLocalToGlobal(NULL, Grid_Anisotropy, l2_rhs_ctx)); 102c38c977aSJames Wright 103c38c977aSJames Wright // Solve projection problem 104c38c977aSJames Wright PetscCall(KSPSolve(ksp, Grid_Anisotropy, Grid_Anisotropy)); 105c38c977aSJames Wright 106c38c977aSJames Wright // Copy anisotropy tensor data to CeedVector 10749f5db74SJames Wright PetscCall(DMGetLocalVector(grid_aniso_proj->dm, &grid_anisotropy_loc)); 108b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionCreateVector(*elem_restr_grid_aniso, grid_aniso_vector, NULL)); 109c38c977aSJames Wright PetscCall(DMGlobalToLocal(grid_aniso_proj->dm, Grid_Anisotropy, INSERT_VALUES, grid_anisotropy_loc)); 110a7dac1d5SJames Wright PetscCall(VecCopyPetscToCeed(grid_anisotropy_loc, *grid_aniso_vector)); 111c38c977aSJames Wright PetscCall(DMRestoreLocalVector(grid_aniso_proj->dm, &grid_anisotropy_loc)); 112c38c977aSJames Wright PetscCall(DMRestoreGlobalVector(grid_aniso_proj->dm, &Grid_Anisotropy)); 113c38c977aSJames Wright } 114c38c977aSJames Wright 115c38c977aSJames Wright // -- Cleanup 116c38c977aSJames Wright PetscCall(NodalProjectionDataDestroy(grid_aniso_proj)); 117c38c977aSJames Wright PetscCall(OperatorApplyContextDestroy(l2_rhs_ctx)); 118b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs_assemble)); 119b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass)); 120b4c37c5cSJames Wright PetscCallCeed(ceed, CeedBasisDestroy(&basis_grid_aniso)); 121*be29160dSJames Wright PetscCallCeed(ceed, CeedVectorDestroy(&q_data)); 122*be29160dSJames Wright PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd)); 123b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs_assemble)); 124b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorDestroy(&op_mass)); 125c38c977aSJames Wright PetscCall(KSPDestroy(&ksp)); 126d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 127c38c977aSJames Wright } 1288dadcfbdSJames Wright 1298dadcfbdSJames Wright PetscErrorCode GridAnisotropyTensorCalculateCollocatedVector(Ceed ceed, User user, CeedData ceed_data, CeedElemRestriction *elem_restr_grid_aniso, 130defe8520SJames Wright CeedVector *aniso_colloc_ceed, PetscInt *num_comp_aniso) { 13167263decSKenneth E. Jansen CeedInt q_data_size, num_nodes; 1328dadcfbdSJames Wright CeedQFunction qf_colloc; 1338dadcfbdSJames Wright CeedOperator op_colloc; 134*be29160dSJames Wright CeedVector q_data; 135*be29160dSJames Wright CeedElemRestriction elem_restr_qd; 13615c18037SJames Wright DMLabel domain_label = NULL; 13715c18037SJames Wright PetscInt label_value = 0, height = 0; 1388dadcfbdSJames Wright 1398dadcfbdSJames Wright PetscFunctionBeginUser; 140defe8520SJames Wright *num_comp_aniso = 7; 141b4c37c5cSJames Wright PetscCallCeed(ceed, CeedBasisGetNumNodes(ceed_data->basis_q, &num_nodes)); 14215c18037SJames Wright PetscCall(DMPlexCeedElemRestrictionQDataCreate(ceed, user->dm, domain_label, label_value, height, *num_comp_aniso, elem_restr_grid_aniso)); 143*be29160dSJames Wright PetscCall(QDataGet(ceed, user->dm, domain_label, label_value, ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord, &elem_restr_qd, 144*be29160dSJames Wright &q_data, &q_data_size)); 1458dadcfbdSJames Wright 1468dadcfbdSJames Wright // -- Build collocation operator 147b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, AnisotropyTensorCollocate, AnisotropyTensorCollocate_loc, &qf_colloc)); 148b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddInput(qf_colloc, "qdata", q_data_size, CEED_EVAL_NONE)); 149b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_colloc, "v", *num_comp_aniso, CEED_EVAL_NONE)); 1508dadcfbdSJames Wright 151b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_colloc, NULL, NULL, &op_colloc)); 152*be29160dSJames Wright PetscCallCeed(ceed, CeedOperatorSetField(op_colloc, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data)); 15358e1cbfdSJeremy L Thompson PetscCallCeed(ceed, CeedOperatorSetField(op_colloc, "v", *elem_restr_grid_aniso, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE)); 1548dadcfbdSJames Wright 155b4c37c5cSJames Wright PetscCallCeed(ceed, CeedElemRestrictionCreateVector(*elem_restr_grid_aniso, aniso_colloc_ceed, NULL)); 1568dadcfbdSJames Wright 157b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorApply(op_colloc, CEED_VECTOR_NONE, *aniso_colloc_ceed, CEED_REQUEST_IMMEDIATE)); 1588dadcfbdSJames Wright 159*be29160dSJames Wright PetscCallCeed(ceed, CeedVectorDestroy(&q_data)); 160*be29160dSJames Wright PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd)); 161b4c37c5cSJames Wright PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_colloc)); 162b4c37c5cSJames Wright PetscCallCeed(ceed, CeedOperatorDestroy(&op_colloc)); 163d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 1648dadcfbdSJames Wright } 165