xref: /libCEED/examples/fluids/src/grid_anisotropy_tensor.c (revision 75f2cf911a7ee4ccd0a27da972e8a267d3cef135)
1052409adSJames Wright // Copyright (c) 2017-2023, Lawrence Livermore National Security, LLC and other CEED contributors.
2052409adSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3052409adSJames Wright //
4052409adSJames Wright // SPDX-License-Identifier: BSD-2-Clause
5052409adSJames Wright //
6052409adSJames Wright // This file is part of CEED:  http://github.com/ceed
7052409adSJames Wright 
8052409adSJames Wright #include "../qfunctions/grid_anisotropy_tensor.h"
9052409adSJames Wright 
10052409adSJames Wright #include <petscdmplex.h>
11052409adSJames Wright 
12052409adSJames Wright #include "../navierstokes.h"
13052409adSJames Wright 
14052409adSJames Wright PetscErrorCode GridAnisotropyTensorProjectionSetupApply(Ceed ceed, User user, CeedData ceed_data, CeedElemRestriction *elem_restr_grid_aniso,
15052409adSJames Wright                                                         CeedVector *grid_aniso_vector) {
16052409adSJames Wright   NodalProjectionData  grid_aniso_proj;
17052409adSJames Wright   OperatorApplyContext mass_matop_ctx, l2_rhs_ctx;
18052409adSJames Wright   CeedOperator         op_rhs_assemble, op_mass;
19052409adSJames Wright   CeedQFunction        qf_rhs_assemble, qf_mass;
20052409adSJames Wright   CeedBasis            basis_grid_aniso;
21457e73b2SJames Wright   PetscInt             dim;
22457e73b2SJames Wright   CeedInt              num_qpts_1d, num_nodes_1d, q_data_size;
23052409adSJames Wright   MPI_Comm             comm = PetscObjectComm((PetscObject)user->dm);
24052409adSJames Wright   KSP                  ksp;
25052409adSJames Wright 
26052409adSJames Wright   PetscFunctionBeginUser;
27052409adSJames Wright   PetscCall(PetscNew(&grid_aniso_proj));
28052409adSJames Wright 
29052409adSJames Wright   // -- Create DM for Anisotropic tensor L^2 projection
30052409adSJames Wright   grid_aniso_proj->num_comp = 7;
31052409adSJames Wright   PetscCall(DMClone(user->dm, &grid_aniso_proj->dm));
32052409adSJames Wright   PetscCall(DMGetDimension(grid_aniso_proj->dm, &dim));
33052409adSJames Wright   PetscCall(PetscObjectSetName((PetscObject)grid_aniso_proj->dm, "Grid Anisotropy Tensor Projection"));
34052409adSJames Wright 
35052409adSJames Wright   {  // -- Setup DM
36052409adSJames Wright     PetscFE      fe;
37052409adSJames Wright     PetscSection section;
38052409adSJames Wright     PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, grid_aniso_proj->num_comp, PETSC_FALSE, user->app_ctx->degree, PETSC_DECIDE, &fe));
39052409adSJames Wright     PetscCall(PetscObjectSetName((PetscObject)fe, "Grid Anisotropy Tensor Projection"));
40052409adSJames Wright     PetscCall(DMAddField(grid_aniso_proj->dm, NULL, (PetscObject)fe));
41052409adSJames Wright     PetscCall(DMCreateDS(grid_aniso_proj->dm));
42052409adSJames Wright     PetscCall(DMPlexSetClosurePermutationTensor(grid_aniso_proj->dm, PETSC_DETERMINE, NULL));
43052409adSJames Wright 
44052409adSJames Wright     PetscCall(DMGetLocalSection(grid_aniso_proj->dm, &section));
45052409adSJames Wright     PetscCall(PetscSectionSetFieldName(section, 0, ""));
46052409adSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 0, "KMGridAnisotropyTensorXX"));
47052409adSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 1, "KMGridAnisotropyTensorYY"));
48052409adSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 2, "KMGridAnisotropyTensorZZ"));
49052409adSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 3, "KMGridAnisotropyTensorYZ"));
50052409adSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 4, "KMGridAnisotropyTensorXZ"));
51052409adSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 5, "KMGridAnisotropyTensorXY"));
52052409adSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 6, "GridAnisotropyTensorFrobNorm"));
53052409adSJames Wright 
54052409adSJames Wright     PetscCall(PetscFEDestroy(&fe));
55052409adSJames Wright   }
56052409adSJames Wright 
57052409adSJames Wright   // -- Get Pre-requisite things
58052409adSJames Wright   CeedBasisGetNumQuadraturePoints1D(ceed_data->basis_q, &num_qpts_1d);
59052409adSJames Wright   CeedBasisGetNumNodes1D(ceed_data->basis_q, &num_nodes_1d);
60052409adSJames Wright   CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size);
61052409adSJames Wright 
62431cd09aSJames Wright   PetscCall(
63431cd09aSJames 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));
64052409adSJames Wright   CeedBasisCreateTensorH1Lagrange(ceed, dim, grid_aniso_proj->num_comp, num_nodes_1d, num_qpts_1d, CEED_GAUSS, &basis_grid_aniso);
65052409adSJames Wright 
66052409adSJames Wright   // -- Build RHS operator
67052409adSJames Wright   CeedQFunctionCreateInterior(ceed, 1, AnisotropyTensorProjection, AnisotropyTensorProjection_loc, &qf_rhs_assemble);
68052409adSJames Wright   CeedQFunctionAddInput(qf_rhs_assemble, "qdata", q_data_size, CEED_EVAL_NONE);
69052409adSJames Wright   CeedQFunctionAddOutput(qf_rhs_assemble, "v", grid_aniso_proj->num_comp, CEED_EVAL_INTERP);
70052409adSJames Wright 
71052409adSJames Wright   CeedOperatorCreate(ceed, qf_rhs_assemble, NULL, NULL, &op_rhs_assemble);
72052409adSJames Wright   CeedOperatorSetField(op_rhs_assemble, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data);
73052409adSJames Wright   CeedOperatorSetField(op_rhs_assemble, "v", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE);
74052409adSJames Wright 
75e66242d1SJames Wright   PetscCall(OperatorApplyContextCreate(user->dm, grid_aniso_proj->dm, ceed, op_rhs_assemble, CEED_VECTOR_NONE, NULL, NULL, NULL, &l2_rhs_ctx));
76052409adSJames Wright 
77052409adSJames Wright   // -- Build Mass Operator
78052409adSJames Wright   PetscCall(CreateMassQFunction(ceed, grid_aniso_proj->num_comp, q_data_size, &qf_mass));
79052409adSJames Wright   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
80052409adSJames Wright   CeedOperatorSetField(op_mass, "u", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE);
81052409adSJames Wright   CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data);
82052409adSJames Wright   CeedOperatorSetField(op_mass, "v", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE);
83052409adSJames Wright 
84052409adSJames Wright   {  // -- Setup KSP for L^2 projection
85052409adSJames Wright     Mat mat_mass;
86e66242d1SJames Wright     PetscCall(OperatorApplyContextCreate(grid_aniso_proj->dm, grid_aniso_proj->dm, ceed, op_mass, NULL, NULL, NULL, NULL, &mass_matop_ctx));
87e66242d1SJames Wright     PetscCall(CreateMatShell_Ceed(mass_matop_ctx, &mat_mass));
88052409adSJames Wright 
89052409adSJames Wright     PetscCall(KSPCreate(comm, &ksp));
90052409adSJames Wright     PetscCall(KSPSetOptionsPrefix(ksp, "grid_anisotropy_tensor_projection_"));
91052409adSJames Wright     {
92052409adSJames Wright       PC pc;
93052409adSJames Wright       PetscCall(KSPGetPC(ksp, &pc));
94052409adSJames Wright       PetscCall(PCSetType(pc, PCJACOBI));
95052409adSJames Wright       PetscCall(PCJacobiSetType(pc, PC_JACOBI_DIAGONAL));
96052409adSJames Wright       PetscCall(KSPSetType(ksp, KSPCG));
97052409adSJames Wright       PetscCall(KSPSetNormType(ksp, KSP_NORM_NATURAL));
98052409adSJames Wright       PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
99052409adSJames Wright     }
100052409adSJames Wright     PetscCall(KSPSetOperators(ksp, mat_mass, mat_mass));
101052409adSJames Wright     PetscCall(KSPSetFromOptions(ksp));
102052409adSJames Wright   }
103052409adSJames Wright 
104052409adSJames Wright   {  // -- Project anisotropy data and store in CeedVector
105052409adSJames Wright     Vec Grid_Anisotropy, grid_anisotropy_loc;
106052409adSJames Wright 
107052409adSJames Wright     // Get L^2 Projection RHS
108052409adSJames Wright     PetscCall(DMGetGlobalVector(grid_aniso_proj->dm, &Grid_Anisotropy));
109052409adSJames Wright 
110e66242d1SJames Wright     PetscCall(ApplyCeedOperatorLocalToGlobal(NULL, Grid_Anisotropy, l2_rhs_ctx));
111052409adSJames Wright 
112052409adSJames Wright     // Solve projection problem
113052409adSJames Wright     PetscCall(KSPSolve(ksp, Grid_Anisotropy, Grid_Anisotropy));
114052409adSJames Wright 
115052409adSJames Wright     // Copy anisotropy tensor data to CeedVector
116e66242d1SJames Wright     PetscCall(DMGetLocalVector(grid_aniso_proj->dm, &grid_anisotropy_loc));
117052409adSJames Wright     CeedElemRestrictionCreateVector(*elem_restr_grid_aniso, grid_aniso_vector, NULL);
118052409adSJames Wright     PetscCall(DMGlobalToLocal(grid_aniso_proj->dm, Grid_Anisotropy, INSERT_VALUES, grid_anisotropy_loc));
119052409adSJames Wright     PetscCall(VecCopyP2C(grid_anisotropy_loc, *grid_aniso_vector));
120052409adSJames Wright     PetscCall(DMRestoreLocalVector(grid_aniso_proj->dm, &grid_anisotropy_loc));
121052409adSJames Wright     PetscCall(DMRestoreGlobalVector(grid_aniso_proj->dm, &Grid_Anisotropy));
122052409adSJames Wright   }
123052409adSJames Wright 
124052409adSJames Wright   // -- Cleanup
125052409adSJames Wright   PetscCall(NodalProjectionDataDestroy(grid_aniso_proj));
126052409adSJames Wright   PetscCall(OperatorApplyContextDestroy(l2_rhs_ctx));
127*75f2cf91SJames Wright   PetscCall(OperatorApplyContextDestroy(mass_matop_ctx));
128052409adSJames Wright   CeedQFunctionDestroy(&qf_rhs_assemble);
129052409adSJames Wright   CeedQFunctionDestroy(&qf_mass);
130052409adSJames Wright   CeedBasisDestroy(&basis_grid_aniso);
131052409adSJames Wright   CeedOperatorDestroy(&op_rhs_assemble);
132052409adSJames Wright   CeedOperatorDestroy(&op_mass);
133052409adSJames Wright   PetscCall(KSPDestroy(&ksp));
134ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
135052409adSJames Wright }
136900aec75SJames Wright 
137900aec75SJames Wright PetscErrorCode GridAnisotropyTensorCalculateCollocatedVector(Ceed ceed, User user, CeedData ceed_data, CeedElemRestriction *elem_restr_grid_aniso,
138457e73b2SJames Wright                                                              CeedVector *aniso_colloc_ceed, PetscInt *num_comp_aniso) {
139*75f2cf91SJames Wright   CeedInt       q_data_size, num_qpts_1d, num_qpts;
140900aec75SJames Wright   CeedQFunction qf_colloc;
141900aec75SJames Wright   CeedOperator  op_colloc;
142900aec75SJames Wright 
143900aec75SJames Wright   PetscFunctionBeginUser;
144900aec75SJames Wright   // -- Get Pre-requisite things
145457e73b2SJames Wright   *num_comp_aniso = 7;
146900aec75SJames Wright   CeedBasisGetNumQuadraturePoints1D(ceed_data->basis_q, &num_qpts_1d);
147900aec75SJames Wright   CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size);
148900aec75SJames Wright 
149457e73b2SJames Wright   PetscCall(GetRestrictionForDomain(ceed, user->dm, 0, 0, 0, 0, num_qpts_1d, *num_comp_aniso, NULL, NULL, elem_restr_grid_aniso));
150900aec75SJames Wright 
151900aec75SJames Wright   // -- Build collocation operator
152900aec75SJames Wright   CeedQFunctionCreateInterior(ceed, 1, AnisotropyTensorCollocate, AnisotropyTensorCollocate_loc, &qf_colloc);
153900aec75SJames Wright   CeedQFunctionAddInput(qf_colloc, "qdata", q_data_size, CEED_EVAL_NONE);
154457e73b2SJames Wright   CeedQFunctionAddOutput(qf_colloc, "v", *num_comp_aniso, CEED_EVAL_NONE);
155900aec75SJames Wright 
156900aec75SJames Wright   CeedOperatorCreate(ceed, qf_colloc, NULL, NULL, &op_colloc);
157900aec75SJames Wright   CeedOperatorSetField(op_colloc, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data);
158900aec75SJames Wright   CeedOperatorSetField(op_colloc, "v", *elem_restr_grid_aniso, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
159*75f2cf91SJames Wright   CeedBasisGetNumQuadraturePoints(ceed_data->basis_q, &num_qpts);
160*75f2cf91SJames Wright   CeedOperatorSetNumQuadraturePoints(op_colloc, num_qpts);
161900aec75SJames Wright 
162900aec75SJames Wright   CeedElemRestrictionCreateVector(*elem_restr_grid_aniso, aniso_colloc_ceed, NULL);
163900aec75SJames Wright 
164900aec75SJames Wright   CeedOperatorApply(op_colloc, CEED_VECTOR_NONE, *aniso_colloc_ceed, CEED_REQUEST_IMMEDIATE);
165900aec75SJames Wright 
166*75f2cf91SJames Wright   CeedQFunctionDestroy(&qf_colloc);
167*75f2cf91SJames Wright   CeedOperatorDestroy(&op_colloc);
168ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
169900aec75SJames Wright }
170