xref: /honee/src/grid_anisotropy_tensor.c (revision 00359db47665a79ecb0241f6ccbf886b649022df)
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 
GridAnisotropyTensorProjectionSetupApply(Ceed ceed,Honee honee,CeedElemRestriction * elem_restr_grid_aniso,CeedVector * grid_aniso_vector)10e3663b90SJames Wright PetscErrorCode GridAnisotropyTensorProjectionSetupApply(Ceed ceed, Honee honee, CeedElemRestriction *elem_restr_grid_aniso,
11c38c977aSJames Wright                                                         CeedVector *grid_aniso_vector) {
12c38c977aSJames Wright   NodalProjectionData grid_aniso_proj;
13c38c977aSJames Wright   CeedBasis           basis_grid_aniso;
14be29160dSJames Wright   CeedVector          q_data;
15be29160dSJames Wright   CeedElemRestriction elem_restr_qd;
1667263decSKenneth E. Jansen   CeedInt             q_data_size;
170c373b74SJames Wright   MPI_Comm            comm   = PetscObjectComm((PetscObject)honee->dm);
18e3db12f8SJames Wright   PetscInt            height = 0, dm_field = 0;
19c38c977aSJames Wright 
20c38c977aSJames Wright   PetscFunctionBeginUser;
21c38c977aSJames Wright   PetscCall(PetscNew(&grid_aniso_proj));
22c38c977aSJames Wright 
2316c7b7c8SJames Wright   {  // -- Create DM for Anisotropic tensor L^2 projection
2416c7b7c8SJames Wright     PetscSection section;
2516c7b7c8SJames Wright 
260c373b74SJames Wright     PetscCall(DMClone(honee->dm, &grid_aniso_proj->dm));
270dee9b8eSJames Wright     PetscCall(DMSetMatrixPreallocateSkip(grid_aniso_proj->dm, PETSC_TRUE));
28c38c977aSJames Wright     PetscCall(PetscObjectSetName((PetscObject)grid_aniso_proj->dm, "Grid Anisotropy Tensor Projection"));
29c38c977aSJames Wright 
3016c7b7c8SJames Wright     // -- Setup DM
3116c7b7c8SJames Wright     grid_aniso_proj->num_comp = 7;
320c373b74SJames Wright     PetscCall(DMSetupByOrder_FEM(PETSC_TRUE, PETSC_TRUE, honee->app_ctx->degree, 1, honee->app_ctx->q_extra, 1, &grid_aniso_proj->num_comp,
33da4ca0cfSJames Wright                                  grid_aniso_proj->dm));
34c38c977aSJames Wright 
35c38c977aSJames Wright     PetscCall(DMGetLocalSection(grid_aniso_proj->dm, &section));
36c38c977aSJames Wright     PetscCall(PetscSectionSetFieldName(section, 0, ""));
37c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 0, "KMGridAnisotropyTensorXX"));
38c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 1, "KMGridAnisotropyTensorYY"));
39c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 2, "KMGridAnisotropyTensorZZ"));
40c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 3, "KMGridAnisotropyTensorYZ"));
41c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 4, "KMGridAnisotropyTensorXZ"));
42c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 5, "KMGridAnisotropyTensorXY"));
43c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 6, "GridAnisotropyTensorFrobNorm"));
44c38c977aSJames Wright   }
45c38c977aSJames Wright 
46c38c977aSJames Wright   // -- Get Pre-requisite things
47e3db12f8SJames Wright   PetscCall(DMPlexCeedElemRestrictionCreate(ceed, grid_aniso_proj->dm, DMLABEL_DEFAULT, DMLABEL_DEFAULT_VALUE, height, dm_field,
48e3db12f8SJames Wright                                             elem_restr_grid_aniso));
49e3db12f8SJames Wright   PetscCall(DMPlexCeedBasisCreate(ceed, grid_aniso_proj->dm, DMLABEL_DEFAULT, DMLABEL_DEFAULT_VALUE, height, dm_field, &basis_grid_aniso));
50*9018c49aSJames Wright   PetscCall(QDataGet(ceed, grid_aniso_proj->dm, DMLABEL_DEFAULT, DMLABEL_DEFAULT_VALUE, &elem_restr_qd, &q_data, &q_data_size));
51c38c977aSJames Wright 
5216c7b7c8SJames Wright   {  // -- Build RHS operator
5316c7b7c8SJames Wright     CeedOperator  op_rhs_assemble;
5416c7b7c8SJames Wright     CeedQFunction qf_rhs_assemble;
5516c7b7c8SJames Wright 
56b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, AnisotropyTensorProjection, AnisotropyTensorProjection_loc, &qf_rhs_assemble));
57b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_assemble, "qdata", q_data_size, CEED_EVAL_NONE));
58b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_rhs_assemble, "v", grid_aniso_proj->num_comp, CEED_EVAL_INTERP));
59c38c977aSJames Wright 
60b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_rhs_assemble, NULL, NULL, &op_rhs_assemble));
61be29160dSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_assemble, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data));
62b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_assemble, "v", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE));
63c38c977aSJames Wright 
640c373b74SJames Wright     PetscCall(OperatorApplyContextCreate(honee->dm, grid_aniso_proj->dm, ceed, op_rhs_assemble, CEED_VECTOR_NONE, NULL, NULL, NULL,
6516c7b7c8SJames Wright                                          &grid_aniso_proj->l2_rhs_ctx));
66c38c977aSJames Wright 
6716c7b7c8SJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs_assemble));
6816c7b7c8SJames Wright     PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs_assemble));
6916c7b7c8SJames Wright   }
7016c7b7c8SJames Wright 
7116c7b7c8SJames Wright   {  // Setup KSP for L^2 projection
7216c7b7c8SJames Wright     CeedOperator  op_mass;
7316c7b7c8SJames Wright     CeedQFunction qf_mass;
7416c7b7c8SJames Wright     Mat           mat_mass;
7516c7b7c8SJames Wright 
7664dd23feSJames Wright     PetscCall(HoneeMassQFunctionCreate(ceed, grid_aniso_proj->num_comp, q_data_size, &qf_mass));
77b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass));
78b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "u", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE));
79be29160dSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data));
80b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "v", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE));
81c38c977aSJames Wright 
82000d2032SJeremy L Thompson     PetscCall(MatCreateCeed(grid_aniso_proj->dm, grid_aniso_proj->dm, op_mass, NULL, &mat_mass));
83c38c977aSJames Wright 
8416c7b7c8SJames Wright     PetscCall(KSPCreate(comm, &grid_aniso_proj->ksp));
8516c7b7c8SJames Wright     PetscCall(KSPSetOptionsPrefix(grid_aniso_proj->ksp, "grid_anisotropy_tensor_projection_"));
86c38c977aSJames Wright     {
87c38c977aSJames Wright       PC pc;
8816c7b7c8SJames Wright       PetscCall(KSPGetPC(grid_aniso_proj->ksp, &pc));
89c38c977aSJames Wright       PetscCall(PCSetType(pc, PCJACOBI));
90c38c977aSJames Wright       PetscCall(PCJacobiSetType(pc, PC_JACOBI_DIAGONAL));
9116c7b7c8SJames Wright       PetscCall(KSPSetType(grid_aniso_proj->ksp, KSPCG));
9216c7b7c8SJames Wright       PetscCall(KSPSetNormType(grid_aniso_proj->ksp, KSP_NORM_NATURAL));
9316c7b7c8SJames Wright       PetscCall(KSPSetTolerances(grid_aniso_proj->ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
94c38c977aSJames Wright     }
9516c7b7c8SJames Wright     PetscCall(KSPSetFromOptions_WithMatCeed(grid_aniso_proj->ksp, mat_mass));
9616c7b7c8SJames Wright 
9716c7b7c8SJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass));
9816c7b7c8SJames Wright     PetscCallCeed(ceed, CeedOperatorDestroy(&op_mass));
99b8462d76SJames Wright     PetscCall(MatDestroy(&mat_mass));
100c38c977aSJames Wright   }
101c38c977aSJames Wright 
102c38c977aSJames Wright   {  // -- Project anisotropy data and store in CeedVector
103c38c977aSJames Wright     Vec Grid_Anisotropy, grid_anisotropy_loc;
104c38c977aSJames Wright 
105c38c977aSJames Wright     PetscCall(DMGetGlobalVector(grid_aniso_proj->dm, &Grid_Anisotropy));
10616c7b7c8SJames Wright     PetscCall(ApplyCeedOperatorLocalToGlobal(NULL, Grid_Anisotropy, grid_aniso_proj->l2_rhs_ctx));
10716c7b7c8SJames Wright     PetscCall(KSPSolve(grid_aniso_proj->ksp, Grid_Anisotropy, Grid_Anisotropy));
108c38c977aSJames Wright 
109c38c977aSJames Wright     // Copy anisotropy tensor data to CeedVector
11049f5db74SJames Wright     PetscCall(DMGetLocalVector(grid_aniso_proj->dm, &grid_anisotropy_loc));
111b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedElemRestrictionCreateVector(*elem_restr_grid_aniso, grid_aniso_vector, NULL));
112c38c977aSJames Wright     PetscCall(DMGlobalToLocal(grid_aniso_proj->dm, Grid_Anisotropy, INSERT_VALUES, grid_anisotropy_loc));
113a7dac1d5SJames Wright     PetscCall(VecCopyPetscToCeed(grid_anisotropy_loc, *grid_aniso_vector));
114c38c977aSJames Wright     PetscCall(DMRestoreLocalVector(grid_aniso_proj->dm, &grid_anisotropy_loc));
115c38c977aSJames Wright     PetscCall(DMRestoreGlobalVector(grid_aniso_proj->dm, &Grid_Anisotropy));
116c38c977aSJames Wright   }
117c38c977aSJames Wright 
1184ea616f4SJames Wright   PetscCall(NodalProjectionDataDestroy(&grid_aniso_proj));
119b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedBasisDestroy(&basis_grid_aniso));
120be29160dSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&q_data));
121be29160dSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd));
122d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
123c38c977aSJames Wright }
1248dadcfbdSJames Wright 
GridAnisotropyTensorCalculateCollocatedVector(Ceed ceed,Honee honee,CeedElemRestriction * elem_restr_grid_aniso,CeedVector * aniso_colloc_ceed,PetscInt * num_comp_aniso)125e3663b90SJames Wright PetscErrorCode GridAnisotropyTensorCalculateCollocatedVector(Ceed ceed, Honee honee, CeedElemRestriction *elem_restr_grid_aniso,
126defe8520SJames Wright                                                              CeedVector *aniso_colloc_ceed, PetscInt *num_comp_aniso) {
12767263decSKenneth E. Jansen   CeedInt             q_data_size, num_nodes;
1288dadcfbdSJames Wright   CeedQFunction       qf_colloc;
1298dadcfbdSJames Wright   CeedOperator        op_colloc;
130be29160dSJames Wright   CeedVector          q_data;
131be29160dSJames Wright   CeedElemRestriction elem_restr_qd;
132e3db12f8SJames Wright   PetscInt            height = 0;
1338dadcfbdSJames Wright 
1348dadcfbdSJames Wright   PetscFunctionBeginUser;
135defe8520SJames Wright   *num_comp_aniso = 7;
136cf8f12c8SJames Wright   {
137cf8f12c8SJames Wright     CeedBasis basis_q;
138cf8f12c8SJames Wright     PetscCall(DMPlexCeedBasisCreate(ceed, honee->dm, DMLABEL_DEFAULT, DMLABEL_DEFAULT_VALUE, 0, 0, &basis_q));
139cf8f12c8SJames Wright     PetscCallCeed(ceed, CeedBasisGetNumNodes(basis_q, &num_nodes));
140cf8f12c8SJames Wright     PetscCallCeed(ceed, CeedBasisDestroy(&basis_q));
141cf8f12c8SJames Wright   }
142e3db12f8SJames Wright   PetscCall(DMPlexCeedElemRestrictionQDataCreate(ceed, honee->dm, DMLABEL_DEFAULT, DMLABEL_DEFAULT_VALUE, height, *num_comp_aniso,
143e3db12f8SJames Wright                                                  elem_restr_grid_aniso));
144*9018c49aSJames Wright   PetscCall(QDataGet(ceed, honee->dm, DMLABEL_DEFAULT, DMLABEL_DEFAULT_VALUE, &elem_restr_qd, &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));
152be29160dSJames 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 
159be29160dSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&q_data));
160be29160dSJames 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