xref: /libCEED/examples/fluids/src/grid_anisotropy_tensor.c (revision d0593705e733b5bdd5e4c173fe0008b11db2ed29)
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;
17cde30410SJames Wright   OperatorApplyContext 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;
210814d5a7SKenneth E. Jansen   CeedInt              q_data_size;
22052409adSJames Wright   MPI_Comm             comm = PetscObjectComm((PetscObject)user->dm);
23052409adSJames Wright   KSP                  ksp;
24bb85d312SJames Wright   DMLabel              domain_label = NULL;
25bb85d312SJames Wright   PetscInt             label_value = 0, height = 0, dm_field = 0;
26052409adSJames Wright 
27052409adSJames Wright   PetscFunctionBeginUser;
28052409adSJames Wright   PetscCall(PetscNew(&grid_aniso_proj));
29052409adSJames Wright 
30052409adSJames Wright   // -- Create DM for Anisotropic tensor L^2 projection
31052409adSJames Wright   grid_aniso_proj->num_comp = 7;
32052409adSJames Wright   PetscCall(DMClone(user->dm, &grid_aniso_proj->dm));
33052409adSJames Wright   PetscCall(PetscObjectSetName((PetscObject)grid_aniso_proj->dm, "Grid Anisotropy Tensor Projection"));
34052409adSJames Wright 
35052409adSJames Wright   {  // -- Setup DM
36052409adSJames Wright     PetscSection section;
37d68a66c4SJames Wright     PetscCall(DMSetupByOrder_FEM(PETSC_TRUE, PETSC_TRUE, user->app_ctx->degree, 1, user->app_ctx->q_extra, 1, &grid_aniso_proj->num_comp,
38d68a66c4SJames Wright                                  grid_aniso_proj->dm));
39052409adSJames Wright 
40052409adSJames Wright     PetscCall(DMGetLocalSection(grid_aniso_proj->dm, &section));
41052409adSJames Wright     PetscCall(PetscSectionSetFieldName(section, 0, ""));
42052409adSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 0, "KMGridAnisotropyTensorXX"));
43052409adSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 1, "KMGridAnisotropyTensorYY"));
44052409adSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 2, "KMGridAnisotropyTensorZZ"));
45052409adSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 3, "KMGridAnisotropyTensorYZ"));
46052409adSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 4, "KMGridAnisotropyTensorXZ"));
47052409adSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 5, "KMGridAnisotropyTensorXY"));
48052409adSJames Wright     PetscCall(PetscSectionSetComponentName(section, 0, 6, "GridAnisotropyTensorFrobNorm"));
49052409adSJames Wright   }
50052409adSJames Wright 
51052409adSJames Wright   // -- Get Pre-requisite things
52a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size));
53052409adSJames Wright 
54bb85d312SJames Wright   PetscCall(DMPlexCeedElemRestrictionCreate(ceed, grid_aniso_proj->dm, domain_label, label_value, height, dm_field, elem_restr_grid_aniso));
55bb85d312SJames Wright   PetscCall(CreateBasisFromPlex(ceed, grid_aniso_proj->dm, domain_label, label_value, height, dm_field, &basis_grid_aniso));
56052409adSJames Wright 
57052409adSJames Wright   // -- Build RHS operator
58a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, AnisotropyTensorProjection, AnisotropyTensorProjection_loc, &qf_rhs_assemble));
59a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_assemble, "qdata", q_data_size, CEED_EVAL_NONE));
60a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_rhs_assemble, "v", grid_aniso_proj->num_comp, CEED_EVAL_INTERP));
61052409adSJames Wright 
62a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_rhs_assemble, NULL, NULL, &op_rhs_assemble));
63356036faSJeremy L Thompson   PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_assemble, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_NONE, ceed_data->q_data));
64a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_assemble, "v", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE));
65052409adSJames Wright 
66e66242d1SJames Wright   PetscCall(OperatorApplyContextCreate(user->dm, grid_aniso_proj->dm, ceed, op_rhs_assemble, CEED_VECTOR_NONE, NULL, NULL, NULL, &l2_rhs_ctx));
67052409adSJames Wright 
68052409adSJames Wright   // -- Build Mass Operator
69052409adSJames Wright   PetscCall(CreateMassQFunction(ceed, grid_aniso_proj->num_comp, q_data_size, &qf_mass));
70a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass));
71a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "u", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE));
72356036faSJeremy L Thompson   PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_NONE, ceed_data->q_data));
73a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_mass, "v", *elem_restr_grid_aniso, basis_grid_aniso, CEED_VECTOR_ACTIVE));
74052409adSJames Wright 
75052409adSJames Wright   {  // -- Setup KSP for L^2 projection
767f2a9303SJames Wright     Mat mat_mass;
77cde30410SJames Wright 
787f2a9303SJames Wright     PetscCall(MatCeedCreate(grid_aniso_proj->dm, grid_aniso_proj->dm, op_mass, NULL, &mat_mass));
79052409adSJames Wright 
80052409adSJames Wright     PetscCall(KSPCreate(comm, &ksp));
81052409adSJames Wright     PetscCall(KSPSetOptionsPrefix(ksp, "grid_anisotropy_tensor_projection_"));
82052409adSJames Wright     {
83052409adSJames Wright       PC pc;
84052409adSJames Wright       PetscCall(KSPGetPC(ksp, &pc));
85052409adSJames Wright       PetscCall(PCSetType(pc, PCJACOBI));
86052409adSJames Wright       PetscCall(PCJacobiSetType(pc, PC_JACOBI_DIAGONAL));
87052409adSJames Wright       PetscCall(KSPSetType(ksp, KSPCG));
88052409adSJames Wright       PetscCall(KSPSetNormType(ksp, KSP_NORM_NATURAL));
89052409adSJames Wright       PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
90052409adSJames Wright     }
917f2a9303SJames Wright     PetscCall(KSPSetFromOptions_WithMatCeed(ksp, mat_mass));
92cde30410SJames Wright     PetscCall(MatDestroy(&mat_mass));
93052409adSJames Wright   }
94052409adSJames Wright 
95052409adSJames Wright   {  // -- Project anisotropy data and store in CeedVector
96052409adSJames Wright     Vec Grid_Anisotropy, grid_anisotropy_loc;
97052409adSJames Wright 
98052409adSJames Wright     // Get L^2 Projection RHS
99052409adSJames Wright     PetscCall(DMGetGlobalVector(grid_aniso_proj->dm, &Grid_Anisotropy));
100052409adSJames Wright 
101e66242d1SJames Wright     PetscCall(ApplyCeedOperatorLocalToGlobal(NULL, Grid_Anisotropy, l2_rhs_ctx));
102052409adSJames Wright 
103052409adSJames Wright     // Solve projection problem
104052409adSJames Wright     PetscCall(KSPSolve(ksp, Grid_Anisotropy, Grid_Anisotropy));
105052409adSJames Wright 
106052409adSJames Wright     // Copy anisotropy tensor data to CeedVector
107e66242d1SJames Wright     PetscCall(DMGetLocalVector(grid_aniso_proj->dm, &grid_anisotropy_loc));
108a424bcd0SJames Wright     PetscCallCeed(ceed, CeedElemRestrictionCreateVector(*elem_restr_grid_aniso, grid_aniso_vector, NULL));
109052409adSJames Wright     PetscCall(DMGlobalToLocal(grid_aniso_proj->dm, Grid_Anisotropy, INSERT_VALUES, grid_anisotropy_loc));
110*d0593705SJames Wright     PetscCall(VecCopyPetscToCeed(grid_anisotropy_loc, *grid_aniso_vector));
111052409adSJames Wright     PetscCall(DMRestoreLocalVector(grid_aniso_proj->dm, &grid_anisotropy_loc));
112052409adSJames Wright     PetscCall(DMRestoreGlobalVector(grid_aniso_proj->dm, &Grid_Anisotropy));
113052409adSJames Wright   }
114052409adSJames Wright 
115052409adSJames Wright   // -- Cleanup
116052409adSJames Wright   PetscCall(NodalProjectionDataDestroy(grid_aniso_proj));
117052409adSJames Wright   PetscCall(OperatorApplyContextDestroy(l2_rhs_ctx));
118a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs_assemble));
119a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass));
120a424bcd0SJames Wright   PetscCallCeed(ceed, CeedBasisDestroy(&basis_grid_aniso));
121a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs_assemble));
122a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_mass));
123052409adSJames Wright   PetscCall(KSPDestroy(&ksp));
124ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
125052409adSJames Wright }
126900aec75SJames Wright 
127900aec75SJames Wright PetscErrorCode GridAnisotropyTensorCalculateCollocatedVector(Ceed ceed, User user, CeedData ceed_data, CeedElemRestriction *elem_restr_grid_aniso,
128457e73b2SJames Wright                                                              CeedVector *aniso_colloc_ceed, PetscInt *num_comp_aniso) {
1290814d5a7SKenneth E. Jansen   CeedInt       q_data_size, num_nodes;
130900aec75SJames Wright   CeedQFunction qf_colloc;
131900aec75SJames Wright   CeedOperator  op_colloc;
132bb85d312SJames Wright   DMLabel       domain_label = NULL;
133bb85d312SJames Wright   PetscInt      label_value = 0, height = 0;
134900aec75SJames Wright 
135900aec75SJames Wright   PetscFunctionBeginUser;
136457e73b2SJames Wright   *num_comp_aniso = 7;
137a424bcd0SJames Wright   PetscCallCeed(ceed, CeedBasisGetNumNodes(ceed_data->basis_q, &num_nodes));
138a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(ceed_data->elem_restr_qd_i, &q_data_size));
139bb85d312SJames Wright   PetscCall(DMPlexCeedElemRestrictionQDataCreate(ceed, user->dm, domain_label, label_value, height, *num_comp_aniso, elem_restr_grid_aniso));
140900aec75SJames Wright 
141900aec75SJames Wright   // -- Build collocation operator
142a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, AnisotropyTensorCollocate, AnisotropyTensorCollocate_loc, &qf_colloc));
143a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_colloc, "qdata", q_data_size, CEED_EVAL_NONE));
144a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_colloc, "v", *num_comp_aniso, CEED_EVAL_NONE));
145900aec75SJames Wright 
146a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_colloc, NULL, NULL, &op_colloc));
147356036faSJeremy L Thompson   PetscCallCeed(ceed, CeedOperatorSetField(op_colloc, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_NONE, ceed_data->q_data));
148356036faSJeremy L Thompson   PetscCallCeed(ceed, CeedOperatorSetField(op_colloc, "v", *elem_restr_grid_aniso, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
149900aec75SJames Wright 
150a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionCreateVector(*elem_restr_grid_aniso, aniso_colloc_ceed, NULL));
151900aec75SJames Wright 
152a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorApply(op_colloc, CEED_VECTOR_NONE, *aniso_colloc_ceed, CEED_REQUEST_IMMEDIATE));
153900aec75SJames Wright 
154a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_colloc));
155a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_colloc));
156ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
157900aec75SJames Wright }
158