xref: /honee/src/grid_anisotropy_tensor.c (revision d102f3ec01414b532f8703c7898eae7b99d41b68)
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;
21defe8520SJames Wright   PetscInt             dim;
22defe8520SJames Wright   CeedInt              num_qpts_1d, num_nodes_1d, q_data_size;
23c38c977aSJames Wright   MPI_Comm             comm = PetscObjectComm((PetscObject)user->dm);
24c38c977aSJames Wright   KSP                  ksp;
25c38c977aSJames Wright 
26c38c977aSJames Wright   PetscFunctionBeginUser;
27c38c977aSJames Wright   PetscCall(PetscNew(&grid_aniso_proj));
28c38c977aSJames Wright 
29c38c977aSJames Wright   // -- Create DM for Anisotropic tensor L^2 projection
30c38c977aSJames Wright   grid_aniso_proj->num_comp = 7;
31c38c977aSJames Wright   PetscCall(DMClone(user->dm, &grid_aniso_proj->dm));
32c38c977aSJames Wright   PetscCall(DMGetDimension(grid_aniso_proj->dm, &dim));
33c38c977aSJames Wright   PetscCall(PetscObjectSetName((PetscObject)grid_aniso_proj->dm, "Grid Anisotropy Tensor Projection"));
34c38c977aSJames Wright 
35c38c977aSJames Wright   {  // -- Setup DM
36c38c977aSJames Wright     PetscFE      fe;
37c38c977aSJames Wright     PetscSection section;
38c38c977aSJames Wright     PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, grid_aniso_proj->num_comp, PETSC_FALSE, user->app_ctx->degree, PETSC_DECIDE, &fe));
39c38c977aSJames Wright     PetscCall(PetscObjectSetName((PetscObject)fe, "Grid Anisotropy Tensor Projection"));
40c38c977aSJames Wright     PetscCall(DMAddField(grid_aniso_proj->dm, NULL, (PetscObject)fe));
41c38c977aSJames Wright     PetscCall(DMCreateDS(grid_aniso_proj->dm));
42c38c977aSJames Wright     PetscCall(DMPlexSetClosurePermutationTensor(grid_aniso_proj->dm, PETSC_DETERMINE, NULL));
43c38c977aSJames Wright 
44c38c977aSJames Wright     PetscCall(DMGetLocalSection(grid_aniso_proj->dm, &section));
45c38c977aSJames Wright     PetscCall(PetscSectionSetFieldName(section, 0, ""));
46c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 0, "KMGridAnisotropyTensorXX"));
47c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 1, "KMGridAnisotropyTensorYY"));
48c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 2, "KMGridAnisotropyTensorZZ"));
49c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 3, "KMGridAnisotropyTensorYZ"));
50c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 4, "KMGridAnisotropyTensorXZ"));
51c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 5, "KMGridAnisotropyTensorXY"));
52c38c977aSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 6, "GridAnisotropyTensorFrobNorm"));
53c38c977aSJames Wright 
54c38c977aSJames Wright     PetscCall(PetscFEDestroy(&fe));
55c38c977aSJames Wright   }
56c38c977aSJames Wright 
57c38c977aSJames Wright   // -- Get Pre-requisite things
58c38c977aSJames Wright   CeedBasisGetNumQuadraturePoints1D(ceed_data->basis_q, &num_qpts_1d);
59c38c977aSJames Wright   CeedBasisGetNumNodes1D(ceed_data->basis_q, &num_nodes_1d);
60c38c977aSJames Wright   CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size);
61c38c977aSJames Wright 
6236c6cbc8SJames Wright   PetscCall(
6336c6cbc8SJames Wright       GetRestrictionForDomain(ceed, grid_aniso_proj->dm, 0, 0, 0, 0, num_qpts_1d, grid_aniso_proj->num_comp, elem_restr_grid_aniso, NULL, NULL));
64c38c977aSJames Wright   CeedBasisCreateTensorH1Lagrange(ceed, dim, grid_aniso_proj->num_comp, num_nodes_1d, num_qpts_1d, CEED_GAUSS, &basis_grid_aniso);
65c38c977aSJames Wright 
66c38c977aSJames Wright   // -- Build RHS operator
67c38c977aSJames Wright   CeedQFunctionCreateInterior(ceed, 1, AnisotropyTensorProjection, AnisotropyTensorProjection_loc, &qf_rhs_assemble);
68c38c977aSJames Wright   CeedQFunctionAddInput(qf_rhs_assemble, "qdata", q_data_size, CEED_EVAL_NONE);
69c38c977aSJames Wright   CeedQFunctionAddOutput(qf_rhs_assemble, "v", grid_aniso_proj->num_comp, CEED_EVAL_INTERP);
70c38c977aSJames Wright 
71c38c977aSJames Wright   CeedOperatorCreate(ceed, qf_rhs_assemble, NULL, NULL, &op_rhs_assemble);
72c38c977aSJames Wright   CeedOperatorSetField(op_rhs_assemble, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data);
73c38c977aSJames Wright   CeedOperatorSetField(op_rhs_assemble, "v", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE);
74c38c977aSJames Wright 
7549f5db74SJames Wright   PetscCall(OperatorApplyContextCreate(user->dm, grid_aniso_proj->dm, ceed, op_rhs_assemble, CEED_VECTOR_NONE, NULL, NULL, NULL, &l2_rhs_ctx));
76c38c977aSJames Wright 
77c38c977aSJames Wright   // -- Build Mass Operator
78c38c977aSJames Wright   PetscCall(CreateMassQFunction(ceed, grid_aniso_proj->num_comp, q_data_size, &qf_mass));
79c38c977aSJames Wright   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
80c38c977aSJames Wright   CeedOperatorSetField(op_mass, "u", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE);
81c38c977aSJames Wright   CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data);
82c38c977aSJames Wright   CeedOperatorSetField(op_mass, "v", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE);
83c38c977aSJames Wright 
84c38c977aSJames Wright   {  // -- Setup KSP for L^2 projection
85c38c977aSJames Wright     Mat mat_mass;
8649f5db74SJames Wright     PetscCall(OperatorApplyContextCreate(grid_aniso_proj->dm, grid_aniso_proj->dm, ceed, op_mass, NULL, NULL, NULL, NULL, &mass_matop_ctx));
8749f5db74SJames Wright     PetscCall(CreateMatShell_Ceed(mass_matop_ctx, &mat_mass));
88c38c977aSJames Wright 
89c38c977aSJames Wright     PetscCall(KSPCreate(comm, &ksp));
90c38c977aSJames Wright     PetscCall(KSPSetOptionsPrefix(ksp, "grid_anisotropy_tensor_projection_"));
91c38c977aSJames Wright     {
92c38c977aSJames Wright       PC pc;
93c38c977aSJames Wright       PetscCall(KSPGetPC(ksp, &pc));
94c38c977aSJames Wright       PetscCall(PCSetType(pc, PCJACOBI));
95c38c977aSJames Wright       PetscCall(PCJacobiSetType(pc, PC_JACOBI_DIAGONAL));
96c38c977aSJames Wright       PetscCall(KSPSetType(ksp, KSPCG));
97c38c977aSJames Wright       PetscCall(KSPSetNormType(ksp, KSP_NORM_NATURAL));
98c38c977aSJames Wright       PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
99c38c977aSJames Wright     }
100c38c977aSJames Wright     PetscCall(KSPSetOperators(ksp, mat_mass, mat_mass));
101c38c977aSJames Wright     PetscCall(KSPSetFromOptions(ksp));
102c38c977aSJames Wright   }
103c38c977aSJames Wright 
104c38c977aSJames Wright   {  // -- Project anisotropy data and store in CeedVector
105c38c977aSJames Wright     Vec Grid_Anisotropy, grid_anisotropy_loc;
106c38c977aSJames Wright 
107c38c977aSJames Wright     // Get L^2 Projection RHS
108c38c977aSJames Wright     PetscCall(DMGetGlobalVector(grid_aniso_proj->dm, &Grid_Anisotropy));
109c38c977aSJames Wright 
11049f5db74SJames Wright     PetscCall(ApplyCeedOperatorLocalToGlobal(NULL, Grid_Anisotropy, l2_rhs_ctx));
111c38c977aSJames Wright 
112c38c977aSJames Wright     // Solve projection problem
113c38c977aSJames Wright     PetscCall(KSPSolve(ksp, Grid_Anisotropy, Grid_Anisotropy));
114c38c977aSJames Wright 
115c38c977aSJames Wright     // Copy anisotropy tensor data to CeedVector
11649f5db74SJames Wright     PetscCall(DMGetLocalVector(grid_aniso_proj->dm, &grid_anisotropy_loc));
117c38c977aSJames Wright     CeedElemRestrictionCreateVector(*elem_restr_grid_aniso, grid_aniso_vector, NULL);
118c38c977aSJames Wright     PetscCall(DMGlobalToLocal(grid_aniso_proj->dm, Grid_Anisotropy, INSERT_VALUES, grid_anisotropy_loc));
119c38c977aSJames Wright     PetscCall(VecCopyP2C(grid_anisotropy_loc, *grid_aniso_vector));
120c38c977aSJames Wright     PetscCall(DMRestoreLocalVector(grid_aniso_proj->dm, &grid_anisotropy_loc));
121c38c977aSJames Wright     PetscCall(DMRestoreGlobalVector(grid_aniso_proj->dm, &Grid_Anisotropy));
122c38c977aSJames Wright   }
123c38c977aSJames Wright 
124c38c977aSJames Wright   // -- Cleanup
125c38c977aSJames Wright   PetscCall(NodalProjectionDataDestroy(grid_aniso_proj));
126c38c977aSJames Wright   PetscCall(OperatorApplyContextDestroy(l2_rhs_ctx));
127*d102f3ecSJames Wright   PetscCall(OperatorApplyContextDestroy(mass_matop_ctx));
128c38c977aSJames Wright   CeedQFunctionDestroy(&qf_rhs_assemble);
129c38c977aSJames Wright   CeedQFunctionDestroy(&qf_mass);
130c38c977aSJames Wright   CeedBasisDestroy(&basis_grid_aniso);
131c38c977aSJames Wright   CeedOperatorDestroy(&op_rhs_assemble);
132c38c977aSJames Wright   CeedOperatorDestroy(&op_mass);
133c38c977aSJames Wright   PetscCall(KSPDestroy(&ksp));
134d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
135c38c977aSJames Wright }
1368dadcfbdSJames Wright 
1378dadcfbdSJames Wright PetscErrorCode GridAnisotropyTensorCalculateCollocatedVector(Ceed ceed, User user, CeedData ceed_data, CeedElemRestriction *elem_restr_grid_aniso,
138defe8520SJames Wright                                                              CeedVector *aniso_colloc_ceed, PetscInt *num_comp_aniso) {
139*d102f3ecSJames Wright   CeedInt       q_data_size, num_qpts_1d, num_qpts;
1408dadcfbdSJames Wright   CeedQFunction qf_colloc;
1418dadcfbdSJames Wright   CeedOperator  op_colloc;
1428dadcfbdSJames Wright 
1438dadcfbdSJames Wright   PetscFunctionBeginUser;
1448dadcfbdSJames Wright   // -- Get Pre-requisite things
145defe8520SJames Wright   *num_comp_aniso = 7;
1468dadcfbdSJames Wright   CeedBasisGetNumQuadraturePoints1D(ceed_data->basis_q, &num_qpts_1d);
1478dadcfbdSJames Wright   CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size);
1488dadcfbdSJames Wright 
149defe8520SJames Wright   PetscCall(GetRestrictionForDomain(ceed, user->dm, 0, 0, 0, 0, num_qpts_1d, *num_comp_aniso, NULL, NULL, elem_restr_grid_aniso));
1508dadcfbdSJames Wright 
1518dadcfbdSJames Wright   // -- Build collocation operator
1528dadcfbdSJames Wright   CeedQFunctionCreateInterior(ceed, 1, AnisotropyTensorCollocate, AnisotropyTensorCollocate_loc, &qf_colloc);
1538dadcfbdSJames Wright   CeedQFunctionAddInput(qf_colloc, "qdata", q_data_size, CEED_EVAL_NONE);
154defe8520SJames Wright   CeedQFunctionAddOutput(qf_colloc, "v", *num_comp_aniso, CEED_EVAL_NONE);
1558dadcfbdSJames Wright 
1568dadcfbdSJames Wright   CeedOperatorCreate(ceed, qf_colloc, NULL, NULL, &op_colloc);
1578dadcfbdSJames Wright   CeedOperatorSetField(op_colloc, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data);
1588dadcfbdSJames Wright   CeedOperatorSetField(op_colloc, "v", *elem_restr_grid_aniso, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
159*d102f3ecSJames Wright   CeedBasisGetNumQuadraturePoints(ceed_data->basis_q, &num_qpts);
160*d102f3ecSJames Wright   CeedOperatorSetNumQuadraturePoints(op_colloc, num_qpts);
1618dadcfbdSJames Wright 
1628dadcfbdSJames Wright   CeedElemRestrictionCreateVector(*elem_restr_grid_aniso, aniso_colloc_ceed, NULL);
1638dadcfbdSJames Wright 
1648dadcfbdSJames Wright   CeedOperatorApply(op_colloc, CEED_VECTOR_NONE, *aniso_colloc_ceed, CEED_REQUEST_IMMEDIATE);
1658dadcfbdSJames Wright 
166*d102f3ecSJames Wright   CeedQFunctionDestroy(&qf_colloc);
167*d102f3ecSJames Wright   CeedOperatorDestroy(&op_colloc);
168d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
1698dadcfbdSJames Wright }
170