xref: /honee/src/strong_boundary_conditions.c (revision ae2b091fac884a554e48acc4b4c187524c2a2818)
1*ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2*ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
32eb7bf1fSJames Wright 
42eb7bf1fSJames Wright #include "../qfunctions/strong_boundary_conditions.h"
52eb7bf1fSJames Wright 
62eb7bf1fSJames Wright #include <ceed.h>
72eb7bf1fSJames Wright #include <petscdmplex.h>
82eb7bf1fSJames Wright 
9149fb536SJames Wright #include <navierstokes.h>
102eb7bf1fSJames Wright #include "../problems/stg_shur14.h"
112eb7bf1fSJames Wright 
12991aef52SJames Wright PetscErrorCode SetupStrongSTG_Ceed(Ceed ceed, CeedData ceed_data, DM dm, ProblemData problem, SimpleBC bc, Physics phys, CeedOperator op_strong_bc) {
1321ba7ba4SJames Wright   CeedInt             num_comp_x = problem->dim, num_comp_q = 5, stg_data_size = 1, dXdx_size;
146f188493SJames Wright   CeedVector          multiplicity, x_stored, scale_stored, stg_data, dXdx;
1521ba7ba4SJames Wright   CeedBasis           basis_x_to_q_face;
1621ba7ba4SJames Wright   CeedElemRestriction elem_restr_x_face, elem_restr_q_face, elem_restr_x_stored, elem_restr_scale, elem_restr_stgdata, elem_restr_dXdx;
172eb7bf1fSJames Wright   CeedQFunction       qf_setup, qf_strongbc, qf_stgdata;
186f188493SJames Wright   CeedOperator        op_setup, op_strong_bc_sub, op_stgdata;
192eb7bf1fSJames Wright   DMLabel             domain_label;
2021ba7ba4SJames Wright   const PetscInt      dm_field = 0, height_face = 1, height_cell = 0;
2121ba7ba4SJames Wright   PetscInt            dim;
222eb7bf1fSJames Wright 
232eb7bf1fSJames Wright   PetscFunctionBeginUser;
242eb7bf1fSJames Wright   PetscCall(DMGetLabel(dm, "Face Sets", &domain_label));
2521ba7ba4SJames Wright   PetscCall(DMGetDimension(dm, &dim));
2621ba7ba4SJames Wright   dXdx_size = num_comp_x * (dim - height_cell);
272eb7bf1fSJames Wright 
28866f9b4aSJames Wright   {  // Basis
2921ba7ba4SJames Wright     CeedBasis basis_x_face, basis_q_face;
30866f9b4aSJames Wright     DM        dm_coord;
31866f9b4aSJames Wright 
32866f9b4aSJames Wright     PetscCall(DMGetCoordinateDM(dm, &dm_coord));
33866f9b4aSJames Wright     DMLabel  label       = NULL;
34866f9b4aSJames Wright     PetscInt label_value = 0;
3521ba7ba4SJames Wright     PetscCall(CreateBasisFromPlex(ceed, dm, label, label_value, height_face, dm_field, &basis_q_face));
3621ba7ba4SJames Wright     PetscCall(CreateBasisFromPlex(ceed, dm_coord, label, label_value, height_face, dm_field, &basis_x_face));
37866f9b4aSJames Wright 
3821ba7ba4SJames Wright     PetscCallCeed(ceed, CeedBasisCreateProjection(basis_x_face, basis_q_face, &basis_x_to_q_face));
39866f9b4aSJames Wright 
4021ba7ba4SJames Wright     PetscCallCeed(ceed, CeedBasisDestroy(&basis_q_face));
4121ba7ba4SJames Wright     PetscCallCeed(ceed, CeedBasisDestroy(&basis_x_face));
42866f9b4aSJames Wright   }
432eb7bf1fSJames Wright 
442eb7bf1fSJames Wright   // Setup QFunction
45b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, SetupStrongBC, SetupStrongBC_loc, &qf_setup));
46b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_setup, "x", num_comp_x, CEED_EVAL_INTERP));
4721ba7ba4SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_setup, "dxdX", dXdx_size, CEED_EVAL_GRAD));
48b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_setup, "multiplicity", num_comp_q, CEED_EVAL_NONE));
49b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_setup, "x stored", num_comp_x, CEED_EVAL_NONE));
50b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_setup, "scale", 1, CEED_EVAL_NONE));
516f188493SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_setup, "dXdx", dXdx_size, CEED_EVAL_NONE));
522eb7bf1fSJames Wright 
5351a85e6cSJames Wright   // Setup STG QFunctions
5442454adaSJames Wright   PetscCall(SetupStrongStg_PreProcessing(ceed, problem, num_comp_x, stg_data_size, dXdx_size, &qf_stgdata));
5551a85e6cSJames Wright   PetscCall(SetupStrongStg_QF(ceed, problem, num_comp_x, num_comp_q, stg_data_size, dXdx_size, &qf_strongbc));
562eb7bf1fSJames Wright 
572eb7bf1fSJames Wright   // Compute contribution on each boundary face
582eb7bf1fSJames Wright   for (CeedInt i = 0; i < bc->num_inflow; i++) {
5921ba7ba4SJames Wright     DMLabel  face_orientation_label;
605f65e1a7SJames Wright     PetscInt num_orientations_values, *orientation_values;
615f65e1a7SJames Wright 
6221ba7ba4SJames Wright     {
635f65e1a7SJames Wright       char *face_orientation_label_name;
6421ba7ba4SJames Wright 
655f65e1a7SJames Wright       PetscCall(DMPlexCreateFaceLabel(dm, bc->inflows[i], &face_orientation_label_name));
665f65e1a7SJames Wright       PetscCall(DMGetLabel(dm, face_orientation_label_name, &face_orientation_label));
675f65e1a7SJames Wright       PetscCall(PetscFree(face_orientation_label_name));
6821ba7ba4SJames Wright     }
695f65e1a7SJames Wright     PetscCall(DMLabelCreateGlobalValueArray(dm, face_orientation_label, &num_orientations_values, &orientation_values));
705f65e1a7SJames Wright     for (PetscInt o = 0; o < num_orientations_values; o++) {
7121ba7ba4SJames Wright       CeedBasis           basis_x_to_q_cell;
7221ba7ba4SJames Wright       CeedElemRestriction elem_restr_x_cell;
735f65e1a7SJames Wright       PetscInt            orientation = orientation_values[o];
7421ba7ba4SJames Wright 
7521ba7ba4SJames Wright       {
7621ba7ba4SJames Wright         CeedBasis basis_x_cell_to_face, basis_q_face;
7721ba7ba4SJames Wright 
7821ba7ba4SJames Wright         PetscCall(DMPlexCeedBasisCellToFaceCoordinateCreate(ceed, dm, face_orientation_label, orientation, orientation, &basis_x_cell_to_face));
7921ba7ba4SJames Wright         PetscCall(CreateBasisFromPlex(ceed, dm, face_orientation_label, orientation, height_face, dm_field, &basis_q_face));
8021ba7ba4SJames Wright         PetscCallCeed(ceed, CeedBasisCreateProjection(basis_x_cell_to_face, basis_q_face, &basis_x_to_q_cell));
8121ba7ba4SJames Wright         PetscCallCeed(ceed, CeedBasisDestroy(&basis_x_cell_to_face));
8221ba7ba4SJames Wright         PetscCallCeed(ceed, CeedBasisDestroy(&basis_q_face));
8321ba7ba4SJames Wright       }
8421ba7ba4SJames Wright 
8521ba7ba4SJames Wright       PetscCall(DMPlexCeedElemRestrictionCreate(ceed, dm, domain_label, bc->inflows[i], height_face, dm_field, &elem_restr_q_face));
8621ba7ba4SJames Wright       PetscCall(DMPlexCeedElemRestrictionCoordinateCreate(ceed, dm, domain_label, bc->inflows[i], height_face, &elem_restr_x_face));
8721ba7ba4SJames Wright       PetscCall(DMPlexCeedElemRestrictionCoordinateCreate(ceed, dm, face_orientation_label, orientation, height_cell, &elem_restr_x_cell));
8821ba7ba4SJames Wright       PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_q_face, &multiplicity, NULL));
8921ba7ba4SJames Wright       PetscCallCeed(ceed, CeedElemRestrictionGetMultiplicity(elem_restr_q_face, multiplicity));
9021ba7ba4SJames Wright 
9121ba7ba4SJames Wright       PetscCall(DMPlexCeedElemRestrictionCollocatedCreate(ceed, dm, domain_label, bc->inflows[i], height_face, num_comp_x, &elem_restr_x_stored));
9221ba7ba4SJames Wright       PetscCall(DMPlexCeedElemRestrictionCollocatedCreate(ceed, dm, domain_label, bc->inflows[i], height_face, 1, &elem_restr_scale));
9321ba7ba4SJames Wright       PetscCall(DMPlexCeedElemRestrictionCollocatedCreate(ceed, dm, domain_label, bc->inflows[i], height_face, stg_data_size, &elem_restr_stgdata));
9421ba7ba4SJames Wright       PetscCall(DMPlexCeedElemRestrictionCollocatedCreate(ceed, dm, domain_label, bc->inflows[i], height_face, dXdx_size, &elem_restr_dXdx));
95b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_x_stored, &x_stored, NULL));
96b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_scale, &scale_stored, NULL));
97b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_stgdata, &stg_data, NULL));
986f188493SJames Wright       PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_dXdx, &dXdx, NULL));
992eb7bf1fSJames Wright 
1002eb7bf1fSJames Wright       // -- Setup Operator
101b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_setup, NULL, NULL, &op_setup));
1026f188493SJames Wright       PetscCallCeed(ceed, CeedOperatorSetName(op_setup, "Precomputed data for strong boundary conditions"));
10321ba7ba4SJames Wright       PetscCallCeed(ceed, CeedOperatorSetField(op_setup, "x", elem_restr_x_face, basis_x_to_q_face, CEED_VECTOR_ACTIVE));
10421ba7ba4SJames Wright       PetscCallCeed(ceed, CeedOperatorSetField(op_setup, "dxdX", elem_restr_x_cell, basis_x_to_q_cell, CEED_VECTOR_ACTIVE));
10521ba7ba4SJames Wright       PetscCallCeed(ceed, CeedOperatorSetField(op_setup, "multiplicity", elem_restr_q_face, CEED_BASIS_NONE, multiplicity));
10658e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_setup, "x stored", elem_restr_x_stored, CEED_BASIS_NONE, x_stored));
10758e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_setup, "scale", elem_restr_scale, CEED_BASIS_NONE, scale_stored));
10858e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_setup, "dXdx", elem_restr_dXdx, CEED_BASIS_NONE, dXdx));
1092eb7bf1fSJames Wright 
1102eb7bf1fSJames Wright       // -- Compute geometric factors
111b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorApply(op_setup, ceed_data->x_coord, CEED_VECTOR_NONE, CEED_REQUEST_IMMEDIATE));
1122eb7bf1fSJames Wright 
1132eb7bf1fSJames Wright       // -- Compute STGData
114b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_stgdata, NULL, NULL, &op_stgdata));
11558e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_stgdata, "dXdx", elem_restr_dXdx, CEED_BASIS_NONE, dXdx));
11658e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_stgdata, "x", elem_restr_x_stored, CEED_BASIS_NONE, x_stored));
11758e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_stgdata, "stg data", elem_restr_stgdata, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
1182eb7bf1fSJames Wright 
119b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorApply(op_stgdata, CEED_VECTOR_NONE, stg_data, CEED_REQUEST_IMMEDIATE));
1202eb7bf1fSJames Wright 
12121ba7ba4SJames Wright       // -- Setup BC QFunctions
122b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_strongbc, NULL, NULL, &op_strong_bc_sub));
123b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorSetName(op_strong_bc_sub, "Strong STG"));
1242eb7bf1fSJames Wright 
12558e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_strong_bc_sub, "dXdx", elem_restr_dXdx, CEED_BASIS_NONE, dXdx));
12658e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_strong_bc_sub, "x", elem_restr_x_stored, CEED_BASIS_NONE, x_stored));
12758e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_strong_bc_sub, "scale", elem_restr_scale, CEED_BASIS_NONE, scale_stored));
12858e1cbfdSJeremy L Thompson       PetscCallCeed(ceed, CeedOperatorSetField(op_strong_bc_sub, "stg data", elem_restr_stgdata, CEED_BASIS_NONE, stg_data));
12921ba7ba4SJames Wright       PetscCallCeed(ceed, CeedOperatorSetField(op_strong_bc_sub, "q", elem_restr_q_face, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
1302eb7bf1fSJames Wright 
1312eb7bf1fSJames Wright       // -- Add to composite operator
132b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedCompositeOperatorAddSub(op_strong_bc, op_strong_bc_sub));
1332eb7bf1fSJames Wright 
134b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedVectorDestroy(&multiplicity));
135b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedVectorDestroy(&x_stored));
136b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedVectorDestroy(&scale_stored));
137b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedVectorDestroy(&stg_data));
1386f188493SJames Wright       PetscCallCeed(ceed, CeedVectorDestroy(&dXdx));
13921ba7ba4SJames Wright       PetscCallCeed(ceed, CeedBasisDestroy(&basis_x_to_q_cell));
14021ba7ba4SJames Wright       PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_x_face));
14121ba7ba4SJames Wright       PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_x_cell));
14221ba7ba4SJames Wright       PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_q_face));
143b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_x_stored));
144b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_scale));
145b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_stgdata));
1466f188493SJames Wright       PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_dXdx));
147b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorDestroy(&op_strong_bc_sub));
148b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorDestroy(&op_setup));
149b4c37c5cSJames Wright       PetscCallCeed(ceed, CeedOperatorDestroy(&op_stgdata));
1502eb7bf1fSJames Wright     }
1515f65e1a7SJames Wright     PetscCall(PetscFree(orientation_values));
15221ba7ba4SJames Wright   }
1532eb7bf1fSJames Wright 
154b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorGetContextFieldLabel(op_strong_bc, "solution time", &phys->stg_solution_time_label));
1552eb7bf1fSJames Wright 
15621ba7ba4SJames Wright   PetscCallCeed(ceed, CeedBasisDestroy(&basis_x_to_q_face));
15751a85e6cSJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_strongbc));
15851a85e6cSJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_stgdata));
159b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_setup));
160d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
1612eb7bf1fSJames Wright }
1622eb7bf1fSJames Wright 
1632eb7bf1fSJames Wright PetscErrorCode DMPlexInsertBoundaryValues_StrongBCCeed(DM dm, PetscBool insert_essential, Vec Q_loc, PetscReal time, Vec face_geom_FVM,
1642eb7bf1fSJames Wright                                                        Vec cell_geom_FVM, Vec grad_FVM) {
1652eb7bf1fSJames Wright   Vec  boundary_mask;
1662eb7bf1fSJames Wright   User user;
1672eb7bf1fSJames Wright 
1682eb7bf1fSJames Wright   PetscFunctionBeginUser;
1692eb7bf1fSJames Wright   PetscCall(DMGetApplicationContext(dm, &user));
1702eb7bf1fSJames Wright 
1712eb7bf1fSJames Wright   if (user->phys->stg_solution_time_label) {
172b4c37c5cSJames Wright     PetscCallCeed(user->ceed, CeedOperatorSetContextDouble(user->op_strong_bc_ctx->op, user->phys->stg_solution_time_label, &time));
1732eb7bf1fSJames Wright   }
1742eb7bf1fSJames Wright 
1752eb7bf1fSJames Wright   // Mask Strong BC entries
1762eb7bf1fSJames Wright   PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask));
1772eb7bf1fSJames Wright   PetscCall(VecPointwiseMult(Q_loc, Q_loc, boundary_mask));
1782eb7bf1fSJames Wright   PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask));
1792eb7bf1fSJames Wright 
1802eb7bf1fSJames Wright   PetscCall(ApplyAddCeedOperatorLocalToLocal(NULL, Q_loc, user->op_strong_bc_ctx));
181d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
1822eb7bf1fSJames Wright }
1832eb7bf1fSJames Wright 
184991aef52SJames Wright PetscErrorCode SetupStrongBC_Ceed(Ceed ceed, CeedData ceed_data, DM dm, User user, ProblemData problem, SimpleBC bc) {
1852eb7bf1fSJames Wright   CeedOperator op_strong_bc;
1862eb7bf1fSJames Wright 
1872eb7bf1fSJames Wright   PetscFunctionBeginUser;
1882eb7bf1fSJames Wright   {
1892eb7bf1fSJames Wright     Vec boundary_mask, global_vec;
1902eb7bf1fSJames Wright 
1912eb7bf1fSJames Wright     PetscCall(DMGetNamedLocalVector(dm, "boundary mask", &boundary_mask));
1922eb7bf1fSJames Wright     PetscCall(DMGetGlobalVector(dm, &global_vec));
1932eb7bf1fSJames Wright     PetscCall(VecZeroEntries(boundary_mask));
1942eb7bf1fSJames Wright     PetscCall(VecSet(global_vec, 1.0));
1952eb7bf1fSJames Wright     PetscCall(DMGlobalToLocal(dm, global_vec, INSERT_VALUES, boundary_mask));
1962eb7bf1fSJames Wright     PetscCall(DMRestoreNamedLocalVector(dm, "boundary mask", &boundary_mask));
1972eb7bf1fSJames Wright     PetscCall(DMRestoreGlobalVector(dm, &global_vec));
1982eb7bf1fSJames Wright   }
1992eb7bf1fSJames Wright 
200b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedCompositeOperatorCreate(ceed, &op_strong_bc));
2012eb7bf1fSJames Wright   {
2022eb7bf1fSJames Wright     PetscBool use_strongstg = PETSC_FALSE;
2032eb7bf1fSJames Wright     PetscCall(PetscOptionsGetBool(NULL, NULL, "-stg_strong", &use_strongstg, NULL));
2042eb7bf1fSJames Wright 
2052eb7bf1fSJames Wright     if (use_strongstg) {
2066f188493SJames Wright       PetscCall(SetupStrongSTG_Ceed(ceed, ceed_data, dm, problem, bc, user->phys, op_strong_bc));
2072eb7bf1fSJames Wright     }
2082eb7bf1fSJames Wright   }
2092eb7bf1fSJames Wright 
2102eb7bf1fSJames Wright   PetscCall(OperatorApplyContextCreate(NULL, NULL, ceed, op_strong_bc, CEED_VECTOR_NONE, NULL, NULL, NULL, &user->op_strong_bc_ctx));
2112eb7bf1fSJames Wright 
2122eb7bf1fSJames Wright   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_StrongBCCeed));
213d7b7c37aSJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_strong_bc));
214d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
2152eb7bf1fSJames Wright }
216