xref: /libCEED/examples/fluids/src/setupdm.c (revision d4cc18453651bd0f94c1a2e078b2646a92dafdcc)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
377841947SLeila Ghaffari //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
577841947SLeila Ghaffari //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
777841947SLeila Ghaffari 
877841947SLeila Ghaffari /// @file
977841947SLeila Ghaffari /// Setup DM for Navier-Stokes example using PETSc
1077841947SLeila Ghaffari 
1149aac155SJeremy L Thompson #include <ceed.h>
1249aac155SJeremy L Thompson #include <petscdmplex.h>
130814d5a7SKenneth E. Jansen #include <petscds.h>
1449aac155SJeremy L Thompson 
1577841947SLeila Ghaffari #include "../navierstokes.h"
16363b60e1SJames Wright #include "../problems/stg_shur14.h"
1777841947SLeila Ghaffari 
181864f1c2SLeila Ghaffari // Create mesh
CreateDM(MPI_Comm comm,ProblemData problem,MatType mat_type,VecType vec_type,DM * dm)19731c13d7SJames Wright PetscErrorCode CreateDM(MPI_Comm comm, ProblemData problem, MatType mat_type, VecType vec_type, DM *dm) {
2077841947SLeila Ghaffari   PetscFunctionBeginUser;
211864f1c2SLeila Ghaffari   // Create DMPLEX
222b730f8bSJeremy L Thompson   PetscCall(DMCreate(comm, dm));
232b730f8bSJeremy L Thompson   PetscCall(DMSetType(*dm, DMPLEX));
24c32b0260SJed Brown   {
25c32b0260SJed Brown     PetscBool skip = PETSC_TRUE;
262b730f8bSJeremy L Thompson     PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_mat_preallocate_skip", &skip, NULL));
27c32b0260SJed Brown     PetscCall(DMSetMatrixPreallocateSkip(*dm, skip));
28c32b0260SJed Brown   }
292b730f8bSJeremy L Thompson   PetscCall(DMSetMatType(*dm, mat_type));
302b730f8bSJeremy L Thompson   PetscCall(DMSetVecType(*dm, vec_type));
314ea65e7bSJed Brown 
321864f1c2SLeila Ghaffari   // Set Tensor elements
332b730f8bSJeremy L Thompson   PetscCall(PetscOptionsSetValue(NULL, "-dm_plex_simplex", "0"));
342b730f8bSJeremy L Thompson   PetscCall(PetscOptionsSetValue(NULL, "-dm_sparse_localize", "0"));
352127b05cSJames Wright   PetscCall(PetscOptionsSetValue(NULL, "-dm_localize", "0"));  // Localization done in DMSetupByOrderEnd_FEM
3691c97f41SJames Wright   PetscCall(PetscOptionsSetValue(NULL, "-dm_blocking_type", "field_node"));
3791c97f41SJames Wright 
381864f1c2SLeila Ghaffari   // Set CL options
392b730f8bSJeremy L Thompson   PetscCall(DMSetFromOptions(*dm));
402b730f8bSJeremy L Thompson   PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view"));
41ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
4277841947SLeila Ghaffari }
4377841947SLeila Ghaffari 
4477841947SLeila Ghaffari // Setup DM
SetUpDM(DM dm,ProblemData problem,PetscInt degree,PetscInt q_extra,SimpleBC bc,Physics phys)45731c13d7SJames Wright PetscErrorCode SetUpDM(DM dm, ProblemData problem, PetscInt degree, PetscInt q_extra, SimpleBC bc, Physics phys) {
4677841947SLeila Ghaffari   PetscInt num_comp_q = 5;
47d68a66c4SJames Wright   PetscFunctionBeginUser;
48d68a66c4SJames Wright 
49a0b9cdb5SJames Wright   PetscCall(DMSetupByOrderBegin_FEM(PETSC_TRUE, PETSC_TRUE, degree, PETSC_DECIDE, q_extra, 1, &num_comp_q, dm));
50d68a66c4SJames Wright 
51d68a66c4SJames Wright   {  // Add strong boundary conditions to DM
52496f2382SJed Brown     DMLabel label;
532b730f8bSJeremy L Thompson     PetscCall(DMGetLabel(dm, "Face Sets", &label));
54ca69d878SAdeleke O. Bankole     PetscCall(DMPlexLabelComplete(dm, label));
55a8748852SJames Wright 
56a8748852SJames Wright     for (PetscInt i = 0; i < problem->num_bc_defs; i++) {
57a8748852SJames Wright       BCDefinition    bc_def = problem->bc_defs[i];
58a8748852SJames Wright       PetscInt        num_essential_comps, num_label_values;
59a8748852SJames Wright       const PetscInt *essential_comps, *label_values;
60a8748852SJames Wright       const char     *name;
61a8748852SJames Wright 
62a8748852SJames Wright       PetscCall(BCDefinitionGetEssential(bc_def, &num_essential_comps, &essential_comps));
63a8748852SJames Wright       if (essential_comps > 0) {
64a8748852SJames Wright         PetscCall(BCDefinitionGetInfo(bc_def, &name, &num_label_values, &label_values));
65a8748852SJames Wright         PetscCall(DMAddBoundary(dm, DM_BC_ESSENTIAL, name, label, num_label_values, label_values, 0, num_essential_comps, essential_comps, NULL, NULL,
66a8748852SJames Wright                                 NULL, NULL));
672fe7aee7SLeila Ghaffari       }
682fe7aee7SLeila Ghaffari     }
69363b60e1SJames Wright     {
70363b60e1SJames Wright       PetscBool use_strongstg = PETSC_FALSE;
712b730f8bSJeremy L Thompson       PetscCall(PetscOptionsGetBool(NULL, NULL, "-stg_strong", &use_strongstg, NULL));
72cbef7084SJames Wright       if (use_strongstg) PetscCall(SetupStrongStg(dm, bc, problem, phys));
73363b60e1SJames Wright     }
740814d5a7SKenneth E. Jansen   }
750814d5a7SKenneth E. Jansen 
76d68a66c4SJames Wright   PetscCall(DMSetupByOrderEnd_FEM(PETSC_TRUE, dm));
77dc805cc4SLeila Ghaffari 
7877841947SLeila Ghaffari   // Empty name for conserved field (because there is only one field)
7977841947SLeila Ghaffari   PetscSection section;
802b730f8bSJeremy L Thompson   PetscCall(DMGetLocalSection(dm, &section));
812b730f8bSJeremy L Thompson   PetscCall(PetscSectionSetFieldName(section, 0, ""));
8297baf651SJames Wright   switch (phys->state_var) {
8397baf651SJames Wright     case STATEVAR_CONSERVATIVE:
842b730f8bSJeremy L Thompson       PetscCall(PetscSectionSetComponentName(section, 0, 0, "Density"));
852b730f8bSJeremy L Thompson       PetscCall(PetscSectionSetComponentName(section, 0, 1, "MomentumX"));
862b730f8bSJeremy L Thompson       PetscCall(PetscSectionSetComponentName(section, 0, 2, "MomentumY"));
872b730f8bSJeremy L Thompson       PetscCall(PetscSectionSetComponentName(section, 0, 3, "MomentumZ"));
88116622c9SJames Wright       PetscCall(PetscSectionSetComponentName(section, 0, 4, "TotalEnergy"));
8997baf651SJames Wright       break;
9097baf651SJames Wright 
9197baf651SJames Wright     case STATEVAR_PRIMITIVE:
922b730f8bSJeremy L Thompson       PetscCall(PetscSectionSetComponentName(section, 0, 0, "Pressure"));
932b730f8bSJeremy L Thompson       PetscCall(PetscSectionSetComponentName(section, 0, 1, "VelocityX"));
942b730f8bSJeremy L Thompson       PetscCall(PetscSectionSetComponentName(section, 0, 2, "VelocityY"));
952b730f8bSJeremy L Thompson       PetscCall(PetscSectionSetComponentName(section, 0, 3, "VelocityZ"));
962b730f8bSJeremy L Thompson       PetscCall(PetscSectionSetComponentName(section, 0, 4, "Temperature"));
9797baf651SJames Wright       break;
98a2d72b6fSJames Wright 
99a2d72b6fSJames Wright     case STATEVAR_ENTROPY:
100a2d72b6fSJames Wright       PetscCall(PetscSectionSetComponentName(section, 0, 0, "EntropyDensity"));
101a2d72b6fSJames Wright       PetscCall(PetscSectionSetComponentName(section, 0, 1, "EntropyMomentumX"));
102a2d72b6fSJames Wright       PetscCall(PetscSectionSetComponentName(section, 0, 2, "EntropyMomentumY"));
103a2d72b6fSJames Wright       PetscCall(PetscSectionSetComponentName(section, 0, 3, "EntropyMomentumZ"));
104a2d72b6fSJames Wright       PetscCall(PetscSectionSetComponentName(section, 0, 4, "EntropyTotalEnergy"));
105a2d72b6fSJames Wright       break;
10677841947SLeila Ghaffari   }
107ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
10877841947SLeila Ghaffari }
10977841947SLeila Ghaffari 
11077841947SLeila Ghaffari // Refine DM for high-order viz
VizRefineDM(DM dm,User user,ProblemData problem,SimpleBC bc,Physics phys)111731c13d7SJames Wright PetscErrorCode VizRefineDM(DM dm, User user, ProblemData problem, SimpleBC bc, Physics phys) {
11277841947SLeila Ghaffari   DM      dm_hierarchy[user->app_ctx->viz_refine + 1];
11377841947SLeila Ghaffari   VecType vec_type;
11477841947SLeila Ghaffari 
115f17d818dSJames Wright   PetscFunctionBeginUser;
1162b730f8bSJeremy L Thompson   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
11777841947SLeila Ghaffari 
11877841947SLeila Ghaffari   dm_hierarchy[0] = dm;
1192b730f8bSJeremy L Thompson   for (PetscInt i = 0, d = user->app_ctx->degree; i < user->app_ctx->viz_refine; i++) {
12077841947SLeila Ghaffari     Mat interp_next;
1212b730f8bSJeremy L Thompson     PetscCall(DMRefine(dm_hierarchy[i], MPI_COMM_NULL, &dm_hierarchy[i + 1]));
1222b730f8bSJeremy L Thompson     PetscCall(DMClearDS(dm_hierarchy[i + 1]));
1232b730f8bSJeremy L Thompson     PetscCall(DMClearFields(dm_hierarchy[i + 1]));
1242b730f8bSJeremy L Thompson     PetscCall(DMSetCoarseDM(dm_hierarchy[i + 1], dm_hierarchy[i]));
12577841947SLeila Ghaffari     d                = (d + 1) / 2;
1260814d5a7SKenneth E. Jansen     PetscInt q_order = d + user->app_ctx->q_extra;
12777841947SLeila Ghaffari     if (i + 1 == user->app_ctx->viz_refine) d = 1;
1282b730f8bSJeremy L Thompson     PetscCall(DMGetVecType(dm, &vec_type));
1292b730f8bSJeremy L Thompson     PetscCall(DMSetVecType(dm_hierarchy[i + 1], vec_type));
1300814d5a7SKenneth E. Jansen     PetscCall(SetUpDM(dm_hierarchy[i + 1], problem, d, q_order, bc, phys));
1312b730f8bSJeremy L Thompson     PetscCall(DMCreateInterpolation(dm_hierarchy[i], dm_hierarchy[i + 1], &interp_next, NULL));
13277841947SLeila Ghaffari     if (!i) user->interp_viz = interp_next;
13377841947SLeila Ghaffari     else {
13477841947SLeila Ghaffari       Mat C;
1352b730f8bSJeremy L Thompson       PetscCall(MatMatMult(interp_next, user->interp_viz, MAT_INITIAL_MATRIX, PETSC_DECIDE, &C));
1362b730f8bSJeremy L Thompson       PetscCall(MatDestroy(&interp_next));
1372b730f8bSJeremy L Thompson       PetscCall(MatDestroy(&user->interp_viz));
13877841947SLeila Ghaffari       user->interp_viz = C;
13977841947SLeila Ghaffari     }
14077841947SLeila Ghaffari   }
14177841947SLeila Ghaffari   for (PetscInt i = 1; i < user->app_ctx->viz_refine; i++) {
1422b730f8bSJeremy L Thompson     PetscCall(DMDestroy(&dm_hierarchy[i]));
14377841947SLeila Ghaffari   }
14477841947SLeila Ghaffari   user->dm_viz = dm_hierarchy[user->app_ctx->viz_refine];
145ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
14677841947SLeila Ghaffari }
147