xref: /honee/src/strong_boundary_conditions.c (revision 5f65e1a774b14ec487a302217987100fc16f77ea)
1dc936754SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
22eb7bf1fSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
32eb7bf1fSJames Wright //
42eb7bf1fSJames Wright // SPDX-License-Identifier: BSD-2-Clause
52eb7bf1fSJames Wright //
62eb7bf1fSJames Wright // This file is part of CEED:  http://github.com/ceed
72eb7bf1fSJames Wright 
82eb7bf1fSJames Wright #include "../qfunctions/strong_boundary_conditions.h"
92eb7bf1fSJames Wright 
102eb7bf1fSJames Wright #include <ceed.h>
112eb7bf1fSJames Wright #include <petscdmplex.h>
122eb7bf1fSJames Wright 
13149fb536SJames Wright #include <navierstokes.h>
142eb7bf1fSJames Wright #include "../problems/stg_shur14.h"
152eb7bf1fSJames Wright 
16991aef52SJames Wright PetscErrorCode SetupStrongSTG_Ceed(Ceed ceed, CeedData ceed_data, DM dm, ProblemData problem, SimpleBC bc, Physics phys, CeedOperator op_strong_bc) {
1721ba7ba4SJames Wright   CeedInt             num_comp_x = problem->dim, num_comp_q = 5, stg_data_size = 1, dXdx_size;
186f188493SJames Wright   CeedVector          multiplicity, x_stored, scale_stored, stg_data, dXdx;
1921ba7ba4SJames Wright   CeedBasis           basis_x_to_q_face;
2021ba7ba4SJames Wright   CeedElemRestriction elem_restr_x_face, elem_restr_q_face, elem_restr_x_stored, elem_restr_scale, elem_restr_stgdata, elem_restr_dXdx;
212eb7bf1fSJames Wright   CeedQFunction       qf_setup, qf_strongbc, qf_stgdata;
226f188493SJames Wright   CeedOperator        op_setup, op_strong_bc_sub, op_stgdata;
232eb7bf1fSJames Wright   DMLabel             domain_label;
2421ba7ba4SJames Wright   const PetscInt      dm_field = 0, height_face = 1, height_cell = 0;
2521ba7ba4SJames Wright   PetscInt            dim;
262eb7bf1fSJames Wright 
272eb7bf1fSJames Wright   PetscFunctionBeginUser;
282eb7bf1fSJames Wright   PetscCall(DMGetLabel(dm, "Face Sets", &domain_label));
2921ba7ba4SJames Wright   PetscCall(DMGetDimension(dm, &dim));
3021ba7ba4SJames Wright   dXdx_size = num_comp_x * (dim - height_cell);
312eb7bf1fSJames Wright 
32866f9b4aSJames Wright   {  // Basis
3321ba7ba4SJames Wright     CeedBasis basis_x_face, basis_q_face;
34866f9b4aSJames Wright     DM        dm_coord;
35866f9b4aSJames Wright 
36866f9b4aSJames Wright     PetscCall(DMGetCoordinateDM(dm, &dm_coord));
37866f9b4aSJames Wright     DMLabel  label       = NULL;
38866f9b4aSJames Wright     PetscInt label_value = 0;
3921ba7ba4SJames Wright     PetscCall(CreateBasisFromPlex(ceed, dm, label, label_value, height_face, dm_field, &basis_q_face));
4021ba7ba4SJames Wright     PetscCall(CreateBasisFromPlex(ceed, dm_coord, label, label_value, height_face, dm_field, &basis_x_face));
41866f9b4aSJames Wright 
4221ba7ba4SJames Wright     PetscCallCeed(ceed, CeedBasisCreateProjection(basis_x_face, basis_q_face, &basis_x_to_q_face));
43866f9b4aSJames Wright 
4421ba7ba4SJames Wright     PetscCallCeed(ceed, CeedBasisDestroy(&basis_q_face));
4521ba7ba4SJames Wright     PetscCallCeed(ceed, CeedBasisDestroy(&basis_x_face));
46866f9b4aSJames Wright   }
472eb7bf1fSJames Wright 
482eb7bf1fSJames Wright   // Setup QFunction
49b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, SetupStrongBC, SetupStrongBC_loc, &qf_setup));
50b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_setup, "x", num_comp_x, CEED_EVAL_INTERP));
5121ba7ba4SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_setup, "dxdX", dXdx_size, CEED_EVAL_GRAD));
52b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_setup, "multiplicity", num_comp_q, CEED_EVAL_NONE));
53b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_setup, "x stored", num_comp_x, CEED_EVAL_NONE));
54b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_setup, "scale", 1, CEED_EVAL_NONE));
556f188493SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_setup, "dXdx", dXdx_size, CEED_EVAL_NONE));
562eb7bf1fSJames Wright 
5751a85e6cSJames Wright   // Setup STG QFunctions
5842454adaSJames Wright   PetscCall(SetupStrongStg_PreProcessing(ceed, problem, num_comp_x, stg_data_size, dXdx_size, &qf_stgdata));
5951a85e6cSJames Wright   PetscCall(SetupStrongStg_QF(ceed, problem, num_comp_x, num_comp_q, stg_data_size, dXdx_size, &qf_strongbc));
602eb7bf1fSJames Wright 
612eb7bf1fSJames Wright   // Compute contribution on each boundary face
622eb7bf1fSJames Wright   for (CeedInt i = 0; i < bc->num_inflow; i++) {
6321ba7ba4SJames Wright     DMLabel  face_orientation_label;
64*5f65e1a7SJames Wright     PetscInt num_orientations_values, *orientation_values;
65*5f65e1a7SJames Wright 
6621ba7ba4SJames Wright     {
67*5f65e1a7SJames Wright       char *face_orientation_label_name;
6821ba7ba4SJames Wright 
69*5f65e1a7SJames Wright       PetscCall(DMPlexCreateFaceLabel(dm, bc->inflows[i], &face_orientation_label_name));
70*5f65e1a7SJames Wright       PetscCall(DMGetLabel(dm, face_orientation_label_name, &face_orientation_label));
71*5f65e1a7SJames Wright       PetscCall(PetscFree(face_orientation_label_name));
7221ba7ba4SJames Wright     }
73*5f65e1a7SJames Wright     PetscCall(DMLabelCreateGlobalValueArray(dm, face_orientation_label, &num_orientations_values, &orientation_values));
74*5f65e1a7SJames Wright     for (PetscInt o = 0; o < num_orientations_values; o++) {
7521ba7ba4SJames Wright       CeedBasis           basis_x_to_q_cell;
7621ba7ba4SJames Wright       CeedElemRestriction elem_restr_x_cell;
77*5f65e1a7SJames Wright       PetscInt            orientation = orientation_values[o];
7821ba7ba4SJames Wright 
7921ba7ba4SJames Wright       {
8021ba7ba4SJames Wright         CeedBasis basis_x_cell_to_face, basis_q_face;
8121ba7ba4SJames Wright 
8221ba7ba4SJames Wright         PetscCall(DMPlexCeedBasisCellToFaceCoordinateCreate(ceed, dm, face_orientation_label, orientation, orientation, &basis_x_cell_to_face));
8321ba7ba4SJames Wright         PetscCall(CreateBasisFromPlex(ceed, dm, face_orientation_label, orientation, height_face, dm_field, &basis_q_face));
8421ba7ba4SJames Wright         PetscCallCeed(ceed, CeedBasisCreateProjection(basis_x_cell_to_face, basis_q_face, &basis_x_to_q_cell));
8521ba7ba4SJames Wright         PetscCallCeed(ceed, CeedBasisDestroy(&basis_x_cell_to_face));
8621ba7ba4SJames Wright         PetscCallCeed(ceed, CeedBasisDestroy(&basis_q_face));
8721ba7ba4SJames Wright       }
8821ba7ba4SJames Wright 
8921ba7ba4SJames Wright       PetscCall(DMPlexCeedElemRestrictionCreate(ceed, dm, domain_label, bc->inflows[i], height_face, dm_field, &elem_restr_q_face));
9021ba7ba4SJames Wright       PetscCall(DMPlexCeedElemRestrictionCoordinateCreate(ceed, dm, domain_label, bc->inflows[i], height_face, &elem_restr_x_face));
9121ba7ba4SJames Wright       PetscCall(DMPlexCeedElemRestrictionCoordinateCreate(ceed, dm, face_orientation_label, orientation, height_cell, &elem_restr_x_cell));
9221ba7ba4SJames Wright       PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_q_face, &multiplicity, NULL));
9321ba7ba4SJames Wright       PetscCallCeed(ceed, CeedElemRestrictionGetMultiplicity(elem_restr_q_face, multiplicity));
9421ba7ba4SJames Wright 
9521ba7ba4SJames Wright       PetscCall(DMPlexCeedElemRestrictionCollocatedCreate(ceed, dm, domain_label, bc->inflows[i], height_face, num_comp_x, &elem_restr_x_stored));
9621ba7ba4SJames Wright       PetscCall(DMPlexCeedElemRestrictionCollocatedCreate(ceed, dm, domain_label, bc->inflows[i], height_face, 1, &elem_restr_scale));
9721ba7ba4SJames Wright       PetscCall(DMPlexCeedElemRestrictionCollocatedCreate(ceed, dm, domain_label, bc->inflows[i], height_face, stg_data_size, &elem_restr_stgdata));
9821ba7ba4SJames Wright       PetscCall(DMPlexCeedElemRestrictionCollocatedCreate(ceed, dm, domain_label, bc->inflows[i], height_face, dXdx_size, &elem_restr_dXdx));
99b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_x_stored, &x_stored, NULL));
100b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_scale, &scale_stored, NULL));
101b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_stgdata, &stg_data, NULL));
1026f188493SJames Wright       PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_dXdx, &dXdx, NULL));
1032eb7bf1fSJames Wright 
1042eb7bf1fSJames Wright       // -- Setup Operator
105b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_setup, NULL, NULL, &op_setup));
1066f188493SJames Wright       PetscCallCeed(ceed, CeedOperatorSetName(op_setup, "Precomputed data for strong boundary conditions"));
10721ba7ba4SJames Wright       PetscCallCeed(ceed, CeedOperatorSetField(op_setup, "x", elem_restr_x_face, basis_x_to_q_face, CEED_VECTOR_ACTIVE));
10821ba7ba4SJames Wright       PetscCallCeed(ceed, CeedOperatorSetField(op_setup, "dxdX", elem_restr_x_cell, basis_x_to_q_cell, CEED_VECTOR_ACTIVE));
10921ba7ba4SJames Wright       PetscCallCeed(ceed, CeedOperatorSetField(op_setup, "multiplicity", elem_restr_q_face, CEED_BASIS_NONE, multiplicity));
11058e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_setup, "x stored", elem_restr_x_stored, CEED_BASIS_NONE, x_stored));
11158e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_setup, "scale", elem_restr_scale, CEED_BASIS_NONE, scale_stored));
11258e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_setup, "dXdx", elem_restr_dXdx, CEED_BASIS_NONE, dXdx));
1132eb7bf1fSJames Wright 
1142eb7bf1fSJames Wright       // -- Compute geometric factors
115b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorApply(op_setup, ceed_data->x_coord, CEED_VECTOR_NONE, CEED_REQUEST_IMMEDIATE));
1162eb7bf1fSJames Wright 
1172eb7bf1fSJames Wright       // -- Compute STGData
118b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_stgdata, NULL, NULL, &op_stgdata));
11958e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_stgdata, "dXdx", elem_restr_dXdx, CEED_BASIS_NONE, dXdx));
12058e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_stgdata, "x", elem_restr_x_stored, CEED_BASIS_NONE, x_stored));
12158e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_stgdata, "stg data", elem_restr_stgdata, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
1222eb7bf1fSJames Wright 
123b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorApply(op_stgdata, CEED_VECTOR_NONE, stg_data, CEED_REQUEST_IMMEDIATE));
1242eb7bf1fSJames Wright 
12521ba7ba4SJames Wright       // -- Setup BC QFunctions
126b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_strongbc, NULL, NULL, &op_strong_bc_sub));
127b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorSetName(op_strong_bc_sub, "Strong STG"));
1282eb7bf1fSJames Wright 
12958e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_strong_bc_sub, "dXdx", elem_restr_dXdx, CEED_BASIS_NONE, dXdx));
13058e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_strong_bc_sub, "x", elem_restr_x_stored, CEED_BASIS_NONE, x_stored));
13158e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_strong_bc_sub, "scale", elem_restr_scale, CEED_BASIS_NONE, scale_stored));
13258e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_strong_bc_sub, "stg data", elem_restr_stgdata, CEED_BASIS_NONE, stg_data));
13321ba7ba4SJames Wright       PetscCallCeed(ceed, CeedOperatorSetField(op_strong_bc_sub, "q", elem_restr_q_face, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
1342eb7bf1fSJames Wright 
1352eb7bf1fSJames Wright       // -- Add to composite operator
136b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedCompositeOperatorAddSub(op_strong_bc, op_strong_bc_sub));
1372eb7bf1fSJames Wright 
138b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedVectorDestroy(&multiplicity));
139b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedVectorDestroy(&x_stored));
140b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedVectorDestroy(&scale_stored));
141b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedVectorDestroy(&stg_data));
1426f188493SJames Wright       PetscCallCeed(ceed, CeedVectorDestroy(&dXdx));
14321ba7ba4SJames Wright       PetscCallCeed(ceed, CeedBasisDestroy(&basis_x_to_q_cell));
14421ba7ba4SJames Wright       PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_x_face));
14521ba7ba4SJames Wright       PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_x_cell));
14621ba7ba4SJames Wright       PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_q_face));
147b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_x_stored));
148b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_scale));
149b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_stgdata));
1506f188493SJames Wright       PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_dXdx));
151b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorDestroy(&op_strong_bc_sub));
152b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorDestroy(&op_setup));
153b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorDestroy(&op_stgdata));
1542eb7bf1fSJames Wright     }
155*5f65e1a7SJames Wright     PetscCall(PetscFree(orientation_values));
15621ba7ba4SJames Wright   }
1572eb7bf1fSJames Wright 
158b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorGetContextFieldLabel(op_strong_bc, "solution time", &phys->stg_solution_time_label));
1592eb7bf1fSJames Wright 
16021ba7ba4SJames Wright   PetscCallCeed(ceed, CeedBasisDestroy(&basis_x_to_q_face));
16151a85e6cSJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_strongbc));
16251a85e6cSJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_stgdata));
163b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_setup));
164d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
1652eb7bf1fSJames Wright }
1662eb7bf1fSJames Wright 
1672eb7bf1fSJames Wright PetscErrorCode DMPlexInsertBoundaryValues_StrongBCCeed(DM dm, PetscBool insert_essential, Vec Q_loc, PetscReal time, Vec face_geom_FVM,
1682eb7bf1fSJames Wright                                                        Vec cell_geom_FVM, Vec grad_FVM) {
1692eb7bf1fSJames Wright   Vec  boundary_mask;
1702eb7bf1fSJames Wright   User user;
1712eb7bf1fSJames Wright 
1722eb7bf1fSJames Wright   PetscFunctionBeginUser;
1732eb7bf1fSJames Wright   PetscCall(DMGetApplicationContext(dm, &user));
1742eb7bf1fSJames Wright 
1752eb7bf1fSJames Wright   if (user->phys->stg_solution_time_label) {
176b4c37c5cSJames Wright     PetscCallCeed(user->ceed, CeedOperatorSetContextDouble(user->op_strong_bc_ctx->op, user->phys->stg_solution_time_label, &time));
1772eb7bf1fSJames Wright   }
1782eb7bf1fSJames Wright 
1792eb7bf1fSJames Wright   // Mask Strong BC entries
1802eb7bf1fSJames Wright   PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask));
1812eb7bf1fSJames Wright   PetscCall(VecPointwiseMult(Q_loc, Q_loc, boundary_mask));
1822eb7bf1fSJames Wright   PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask));
1832eb7bf1fSJames Wright 
1842eb7bf1fSJames Wright   PetscCall(ApplyAddCeedOperatorLocalToLocal(NULL, Q_loc, user->op_strong_bc_ctx));
185d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
1862eb7bf1fSJames Wright }
1872eb7bf1fSJames Wright 
188991aef52SJames Wright PetscErrorCode SetupStrongBC_Ceed(Ceed ceed, CeedData ceed_data, DM dm, User user, ProblemData problem, SimpleBC bc) {
1892eb7bf1fSJames Wright   CeedOperator op_strong_bc;
1902eb7bf1fSJames Wright 
1912eb7bf1fSJames Wright   PetscFunctionBeginUser;
1922eb7bf1fSJames Wright   {
1932eb7bf1fSJames Wright     Vec boundary_mask, global_vec;
1942eb7bf1fSJames Wright 
1952eb7bf1fSJames Wright     PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask));
1962eb7bf1fSJames Wright     PetscCall(DMGetGlobalVector(dm, &global_vec));
1972eb7bf1fSJames Wright     PetscCall(VecZeroEntries(boundary_mask));
1982eb7bf1fSJames Wright     PetscCall(VecSet(global_vec, 1.0));
1992eb7bf1fSJames Wright     PetscCall(DMGlobalToLocal(dm, global_vec, INSERT_VALUES, boundary_mask));
2002eb7bf1fSJames Wright     PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask));
2012eb7bf1fSJames Wright     PetscCall(DMRestoreGlobalVector(dm, &global_vec));
2022eb7bf1fSJames Wright   }
2032eb7bf1fSJames Wright 
204b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedCompositeOperatorCreate(ceed, &op_strong_bc));
2052eb7bf1fSJames Wright   {
2062eb7bf1fSJames Wright     PetscBool use_strongstg = PETSC_FALSE;
2072eb7bf1fSJames Wright     PetscCall(PetscOptionsGetBool(NULL, NULL, "-stg_strong", &use_strongstg, NULL));
2082eb7bf1fSJames Wright 
2092eb7bf1fSJames Wright     if (use_strongstg) {
2106f188493SJames Wright       PetscCall(SetupStrongSTG_Ceed(ceed, ceed_data, dm, problem, bc, user->phys, op_strong_bc));
2112eb7bf1fSJames Wright     }
2122eb7bf1fSJames Wright   }
2132eb7bf1fSJames Wright 
2142eb7bf1fSJames Wright   PetscCall(OperatorApplyContextCreate(NULL, NULL, ceed, op_strong_bc, CEED_VECTOR_NONE, NULL, NULL, NULL, &user->op_strong_bc_ctx));
2152eb7bf1fSJames Wright 
2162eb7bf1fSJames Wright   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_StrongBCCeed));
217d7b7c37aSJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_strong_bc));
218d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
2192eb7bf1fSJames Wright }
220