xref: /libCEED/examples/fluids/src/setupdm.c (revision cefa2673c9b7c6fb97d32b63fbbfb5d237e3c796)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 /// @file
9 /// Setup DM for Navier-Stokes example using PETSc
10 
11 #include "../navierstokes.h"
12 #include "../problems/stg_shur14.h"
13 
14 // Create mesh
15 PetscErrorCode CreateDM(MPI_Comm comm, ProblemData *problem,
16                         MatType mat_type, VecType vec_type,
17                         DM *dm) {
18   PetscErrorCode   ierr;
19   PetscFunctionBeginUser;
20   // Create DMPLEX
21   ierr = DMCreate(comm, dm); CHKERRQ(ierr);
22   ierr = DMSetType(*dm, DMPLEX); CHKERRQ(ierr);
23   ierr = DMSetMatType(*dm, mat_type); CHKERRQ(ierr);
24   ierr = DMSetVecType(*dm, vec_type); CHKERRQ(ierr);
25 
26   // Set Tensor elements
27   ierr = PetscOptionsSetValue(NULL, "-dm_plex_simplex", "0"); CHKERRQ(ierr);
28   // Set CL options
29   ierr = DMSetFromOptions(*dm); CHKERRQ(ierr);
30   ierr = DMViewFromOptions(*dm, NULL, "-dm_view"); CHKERRQ(ierr);
31   PetscFunctionReturn(0);
32 }
33 
34 // Setup DM
35 PetscErrorCode SetUpDM(DM dm, ProblemData *problem, PetscInt degree,
36                        SimpleBC bc, Physics phys) {
37   PetscErrorCode ierr;
38   PetscFunctionBeginUser;
39   {
40     // Configure the finite element space and boundary conditions
41     PetscFE  fe;
42     PetscInt num_comp_q = 5;
43     DMLabel label;
44     ierr = PetscFECreateLagrange(PETSC_COMM_SELF, problem->dim, num_comp_q,
45                                  PETSC_FALSE, degree, PETSC_DECIDE,
46                                  &fe); CHKERRQ(ierr);
47     ierr = PetscObjectSetName((PetscObject)fe, "Q"); CHKERRQ(ierr);
48     ierr = DMAddField(dm, NULL,(PetscObject)fe); CHKERRQ(ierr);
49     ierr = DMCreateDS(dm); CHKERRQ(ierr);
50     ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr);
51     // Set wall BCs
52     if (bc->num_wall > 0) {
53       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label,
54                            bc->num_wall, bc->walls, 0, bc->num_comps,
55                            bc->wall_comps, (void(*)(void))problem->bc,
56                            NULL, problem->bc_ctx, NULL);  CHKERRQ(ierr);
57     }
58     // Set slip BCs in the x direction
59     if (bc->num_slip[0] > 0) {
60       PetscInt comps[1] = {1};
61       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipx", label,
62                            bc->num_slip[0], bc->slips[0], 0, 1, comps,
63                            (void(*)(void))NULL, NULL, problem->bc_ctx, NULL); CHKERRQ(ierr);
64     }
65     // Set slip BCs in the y direction
66     if (bc->num_slip[1] > 0) {
67       PetscInt comps[1] = {2};
68       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipy", label,
69                            bc->num_slip[1], bc->slips[1], 0, 1, comps,
70                            (void(*)(void))NULL, NULL, problem->bc_ctx, NULL); CHKERRQ(ierr);
71     }
72     // Set slip BCs in the z direction
73     if (bc->num_slip[2] > 0) {
74       PetscInt comps[1] = {3};
75       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", label,
76                            bc->num_slip[2], bc->slips[2], 0, 1, comps,
77                            (void(*)(void))NULL, NULL, problem->bc_ctx, NULL); CHKERRQ(ierr);
78     }
79     {
80       PetscBool use_strongstg = PETSC_FALSE;
81       ierr = PetscOptionsGetBool(NULL, NULL, "-stg_strong", &use_strongstg, NULL);
82       CHKERRQ(ierr);
83       STGShur14Context stg_ctx;
84 
85       if (use_strongstg) {
86         CeedQFunctionContextGetData(problem->apply_inflow.qfunction_context,
87                                     CEED_MEM_HOST, &stg_ctx);
88         ierr = SetupStrongSTG(dm, bc, problem, stg_ctx); CHKERRQ(ierr);
89         CeedQFunctionContextRestoreData(problem->apply_inflow.qfunction_context,
90                                         &stg_ctx);
91       }
92     }
93 
94     ierr = DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL);
95     CHKERRQ(ierr);
96     ierr = PetscFEDestroy(&fe); CHKERRQ(ierr);
97   }
98   {
99     // Empty name for conserved field (because there is only one field)
100     PetscSection section;
101     ierr = DMGetLocalSection(dm, &section); CHKERRQ(ierr);
102     ierr = PetscSectionSetFieldName(section, 0, ""); CHKERRQ(ierr);
103     ierr = PetscSectionSetComponentName(section, 0, 0, "Density");
104     CHKERRQ(ierr);
105     ierr = PetscSectionSetComponentName(section, 0, 1, "Momentum X");
106     CHKERRQ(ierr);
107     ierr = PetscSectionSetComponentName(section, 0, 2, "Momentum Y");
108     CHKERRQ(ierr);
109     ierr = PetscSectionSetComponentName(section, 0, 3, "Momentum Z");
110     CHKERRQ(ierr);
111     ierr = PetscSectionSetComponentName(section, 0, 4, "Energy Density");
112     CHKERRQ(ierr);
113   }
114   PetscFunctionReturn(0);
115 }
116 
117 // Refine DM for high-order viz
118 PetscErrorCode VizRefineDM(DM dm, User user, ProblemData *problem,
119                            SimpleBC bc, Physics phys) {
120   PetscErrorCode ierr;
121   DM             dm_hierarchy[user->app_ctx->viz_refine + 1];
122   VecType        vec_type;
123   PetscFunctionBeginUser;
124 
125   ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE); CHKERRQ(ierr);
126 
127   dm_hierarchy[0] = dm;
128   for (PetscInt i = 0, d = user->app_ctx->degree;
129        i < user->app_ctx->viz_refine; i++) {
130     Mat interp_next;
131     ierr = DMRefine(dm_hierarchy[i], MPI_COMM_NULL, &dm_hierarchy[i+1]);
132     CHKERRQ(ierr);
133     ierr = DMClearDS(dm_hierarchy[i+1]); CHKERRQ(ierr);
134     ierr = DMClearFields(dm_hierarchy[i+1]); CHKERRQ(ierr);
135     ierr = DMSetCoarseDM(dm_hierarchy[i+1], dm_hierarchy[i]); CHKERRQ(ierr);
136     d = (d + 1) / 2;
137     if (i + 1 == user->app_ctx->viz_refine) d = 1;
138     ierr = DMGetVecType(dm, &vec_type); CHKERRQ(ierr);
139     ierr = DMSetVecType(dm_hierarchy[i+1], vec_type); CHKERRQ(ierr);
140     ierr = SetUpDM(dm_hierarchy[i+1], problem, d, bc, phys);
141     CHKERRQ(ierr);
142     ierr = DMCreateInterpolation(dm_hierarchy[i], dm_hierarchy[i+1], &interp_next,
143                                  NULL); CHKERRQ(ierr);
144     if (!i) user->interp_viz = interp_next;
145     else {
146       Mat C;
147       ierr = MatMatMult(interp_next, user->interp_viz, MAT_INITIAL_MATRIX,
148                         PETSC_DECIDE, &C); CHKERRQ(ierr);
149       ierr = MatDestroy(&interp_next); CHKERRQ(ierr);
150       ierr = MatDestroy(&user->interp_viz); CHKERRQ(ierr);
151       user->interp_viz = C;
152     }
153   }
154   for (PetscInt i=1; i<user->app_ctx->viz_refine; i++) {
155     ierr = DMDestroy(&dm_hierarchy[i]); CHKERRQ(ierr);
156   }
157   user->dm_viz = dm_hierarchy[user->app_ctx->viz_refine];
158 
159   PetscFunctionReturn(0);
160 }
161