xref: /honee/src/grid_anisotropy_tensor.c (revision da4ca0cf06af52345b7f9f59c7688c6a30b69b5e)
1c38c977aSJames Wright // Copyright (c) 2017-2023, Lawrence Livermore National Security, LLC and other CEED contributors.
2c38c977aSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3c38c977aSJames Wright //
4c38c977aSJames Wright // SPDX-License-Identifier: BSD-2-Clause
5c38c977aSJames Wright //
6c38c977aSJames Wright // This file is part of CEED:  http://github.com/ceed
7c38c977aSJames Wright 
8c38c977aSJames Wright #include "../qfunctions/grid_anisotropy_tensor.h"
9c38c977aSJames Wright 
10c38c977aSJames Wright #include <petscdmplex.h>
11c38c977aSJames Wright 
12c38c977aSJames Wright #include "../navierstokes.h"
13c38c977aSJames Wright 
14c38c977aSJames Wright PetscErrorCode GridAnisotropyTensorProjectionSetupApply(Ceed ceed, User user, CeedData ceed_data, CeedElemRestriction *elem_restr_grid_aniso,
15c38c977aSJames Wright                                                         CeedVector *grid_aniso_vector) {
16c38c977aSJames Wright   NodalProjectionData  grid_aniso_proj;
17c38c977aSJames Wright   OperatorApplyContext mass_matop_ctx, l2_rhs_ctx;
18c38c977aSJames Wright   CeedOperator         op_rhs_assemble, op_mass;
19c38c977aSJames Wright   CeedQFunction        qf_rhs_assemble, qf_mass;
20c38c977aSJames Wright   CeedBasis            basis_grid_aniso;
2167263decSKenneth E. Jansen   CeedInt              q_data_size;
22c38c977aSJames Wright   MPI_Comm             comm = PetscObjectComm((PetscObject)user->dm);
23c38c977aSJames Wright   KSP                  ksp;
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));
31c38c977aSJames Wright   PetscCall(PetscObjectSetName((PetscObject)grid_aniso_proj->dm, "Grid Anisotropy Tensor Projection"));
32c38c977aSJames Wright 
33c38c977aSJames Wright   {  // -- Setup DM
34c38c977aSJames Wright     PetscSection section;
35*da4ca0cfSJames Wright     PetscCall(DMSetupByOrder_FEM(PETSC_TRUE, PETSC_TRUE, user->app_ctx->degree, 1, user->app_ctx->q_extra, 1, &grid_aniso_proj->num_comp,
36*da4ca0cfSJames Wright                                  grid_aniso_proj->dm));
37c38c977aSJames Wright 
38c38c977aSJames Wright     PetscCall(DMGetLocalSection(grid_aniso_proj->dm, &section));
39c38c977aSJames Wright     PetscCall(PetscSectionSetFieldName(section, 0, ""));
40c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 0, "KMGridAnisotropyTensorXX"));
41c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 1, "KMGridAnisotropyTensorYY"));
42c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 2, "KMGridAnisotropyTensorZZ"));
43c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 3, "KMGridAnisotropyTensorYZ"));
44c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 4, "KMGridAnisotropyTensorXZ"));
45c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 5, "KMGridAnisotropyTensorXY"));
46c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 6, "GridAnisotropyTensorFrobNorm"));
47c38c977aSJames Wright   }
48c38c977aSJames Wright 
49c38c977aSJames Wright   // -- Get Pre-requisite things
50b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size));
51c38c977aSJames Wright 
5267263decSKenneth E. Jansen   PetscCall(GetRestrictionForDomain(ceed, grid_aniso_proj->dm, 0, 0, 0, 0, -1, grid_aniso_proj->num_comp, elem_restr_grid_aniso, NULL, NULL));
5367263decSKenneth E. Jansen   PetscCall(CreateBasisFromPlex(ceed, grid_aniso_proj->dm, 0, 0, 0, 0, &basis_grid_aniso));
54c38c977aSJames Wright 
55c38c977aSJames Wright   // -- Build RHS operator
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));
61b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_assemble, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data));
62b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_assemble, "v", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE));
63c38c977aSJames Wright 
6449f5db74SJames Wright   PetscCall(OperatorApplyContextCreate(user->dm, grid_aniso_proj->dm, ceed, op_rhs_assemble, CEED_VECTOR_NONE, NULL, NULL, NULL, &l2_rhs_ctx));
65c38c977aSJames Wright 
66c38c977aSJames Wright   // -- Build Mass Operator
67c38c977aSJames Wright   PetscCall(CreateMassQFunction(ceed, grid_aniso_proj->num_comp, q_data_size, &qf_mass));
68b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass));
69b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "u", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE));
70b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data));
71b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "v", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE));
72c38c977aSJames Wright 
73c38c977aSJames Wright   {  // -- Setup KSP for L^2 projection
74c38c977aSJames Wright     Mat mat_mass;
7549f5db74SJames Wright     PetscCall(OperatorApplyContextCreate(grid_aniso_proj->dm, grid_aniso_proj->dm, ceed, op_mass, NULL, NULL, NULL, NULL, &mass_matop_ctx));
7649f5db74SJames Wright     PetscCall(CreateMatShell_Ceed(mass_matop_ctx, &mat_mass));
77c38c977aSJames Wright 
78c38c977aSJames Wright     PetscCall(KSPCreate(comm, &ksp));
79c38c977aSJames Wright     PetscCall(KSPSetOptionsPrefix(ksp, "grid_anisotropy_tensor_projection_"));
80c38c977aSJames Wright     {
81c38c977aSJames Wright       PC pc;
82c38c977aSJames Wright       PetscCall(KSPGetPC(ksp, &pc));
83c38c977aSJames Wright       PetscCall(PCSetType(pc, PCJACOBI));
84c38c977aSJames Wright       PetscCall(PCJacobiSetType(pc, PC_JACOBI_DIAGONAL));
85c38c977aSJames Wright       PetscCall(KSPSetType(ksp, KSPCG));
86c38c977aSJames Wright       PetscCall(KSPSetNormType(ksp, KSP_NORM_NATURAL));
87c38c977aSJames Wright       PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
88c38c977aSJames Wright     }
89c38c977aSJames Wright     PetscCall(KSPSetOperators(ksp, mat_mass, mat_mass));
90c38c977aSJames Wright     PetscCall(KSPSetFromOptions(ksp));
91c38c977aSJames Wright   }
92c38c977aSJames Wright 
93c38c977aSJames Wright   {  // -- Project anisotropy data and store in CeedVector
94c38c977aSJames Wright     Vec Grid_Anisotropy, grid_anisotropy_loc;
95c38c977aSJames Wright 
96c38c977aSJames Wright     // Get L^2 Projection RHS
97c38c977aSJames Wright     PetscCall(DMGetGlobalVector(grid_aniso_proj->dm, &Grid_Anisotropy));
98c38c977aSJames Wright 
9949f5db74SJames Wright     PetscCall(ApplyCeedOperatorLocalToGlobal(NULL, Grid_Anisotropy, l2_rhs_ctx));
100c38c977aSJames Wright 
101c38c977aSJames Wright     // Solve projection problem
102c38c977aSJames Wright     PetscCall(KSPSolve(ksp, Grid_Anisotropy, Grid_Anisotropy));
103c38c977aSJames Wright 
104c38c977aSJames Wright     // Copy anisotropy tensor data to CeedVector
10549f5db74SJames Wright     PetscCall(DMGetLocalVector(grid_aniso_proj->dm, &grid_anisotropy_loc));
106b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedElemRestrictionCreateVector(*elem_restr_grid_aniso, grid_aniso_vector, NULL));
107c38c977aSJames Wright     PetscCall(DMGlobalToLocal(grid_aniso_proj->dm, Grid_Anisotropy, INSERT_VALUES, grid_anisotropy_loc));
108c38c977aSJames Wright     PetscCall(VecCopyP2C(grid_anisotropy_loc, *grid_aniso_vector));
109c38c977aSJames Wright     PetscCall(DMRestoreLocalVector(grid_aniso_proj->dm, &grid_anisotropy_loc));
110c38c977aSJames Wright     PetscCall(DMRestoreGlobalVector(grid_aniso_proj->dm, &Grid_Anisotropy));
111c38c977aSJames Wright   }
112c38c977aSJames Wright 
113c38c977aSJames Wright   // -- Cleanup
114c38c977aSJames Wright   PetscCall(NodalProjectionDataDestroy(grid_aniso_proj));
115c38c977aSJames Wright   PetscCall(OperatorApplyContextDestroy(l2_rhs_ctx));
116d102f3ecSJames Wright   PetscCall(OperatorApplyContextDestroy(mass_matop_ctx));
117b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs_assemble));
118b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass));
119b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedBasisDestroy(&basis_grid_aniso));
120b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs_assemble));
121b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_mass));
122c38c977aSJames Wright   PetscCall(KSPDestroy(&ksp));
123d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
124c38c977aSJames Wright }
1258dadcfbdSJames Wright 
1268dadcfbdSJames Wright PetscErrorCode GridAnisotropyTensorCalculateCollocatedVector(Ceed ceed, User user, CeedData ceed_data, CeedElemRestriction *elem_restr_grid_aniso,
127defe8520SJames Wright                                                              CeedVector *aniso_colloc_ceed, PetscInt *num_comp_aniso) {
12867263decSKenneth E. Jansen   CeedInt       q_data_size, num_nodes;
1298dadcfbdSJames Wright   CeedQFunction qf_colloc;
1308dadcfbdSJames Wright   CeedOperator  op_colloc;
1318dadcfbdSJames Wright 
1328dadcfbdSJames Wright   PetscFunctionBeginUser;
133defe8520SJames Wright   *num_comp_aniso = 7;
134b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedBasisGetNumNodes(ceed_data->basis_q, &num_nodes));
135b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size));
13667263decSKenneth E. Jansen   PetscCall(GetRestrictionForDomain(ceed, user->dm, 0, 0, 0, 0, num_nodes, *num_comp_aniso, NULL, NULL, elem_restr_grid_aniso));
1378dadcfbdSJames Wright 
1388dadcfbdSJames Wright   // -- Build collocation operator
139b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, AnisotropyTensorCollocate, AnisotropyTensorCollocate_loc, &qf_colloc));
140b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_colloc, "qdata", q_data_size, CEED_EVAL_NONE));
141b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_colloc, "v", *num_comp_aniso, CEED_EVAL_NONE));
1428dadcfbdSJames Wright 
143b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_colloc, NULL, NULL, &op_colloc));
144b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_colloc, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data));
145b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_colloc, "v", *elem_restr_grid_aniso, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE));
1468dadcfbdSJames Wright 
147b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionCreateVector(*elem_restr_grid_aniso, aniso_colloc_ceed, NULL));
1488dadcfbdSJames Wright 
149b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorApply(op_colloc, CEED_VECTOR_NONE, *aniso_colloc_ceed, CEED_REQUEST_IMMEDIATE));
1508dadcfbdSJames Wright 
151b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_colloc));
152b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_colloc));
153d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
1548dadcfbdSJames Wright }
155