xref: /libCEED/examples/fluids/src/setupdm.c (revision 65dd5cafde15489fff5d2ab607c335242f64f615)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, 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 
1177841947SLeila Ghaffari #include "../navierstokes.h"
12363b60e1SJames Wright #include "../problems/stg_shur14.h"
1377841947SLeila Ghaffari 
141864f1c2SLeila Ghaffari // Create mesh
154ea65e7bSJed Brown PetscErrorCode CreateDM(MPI_Comm comm, ProblemData *problem,
164ea65e7bSJed Brown                         MatType mat_type, VecType vec_type,
174ea65e7bSJed Brown                         DM *dm) {
1877841947SLeila Ghaffari   PetscErrorCode   ierr;
1977841947SLeila Ghaffari   PetscFunctionBeginUser;
201864f1c2SLeila Ghaffari   // Create DMPLEX
211864f1c2SLeila Ghaffari   ierr = DMCreate(comm, dm); CHKERRQ(ierr);
221864f1c2SLeila Ghaffari   ierr = DMSetType(*dm, DMPLEX); CHKERRQ(ierr);
234ea65e7bSJed Brown   ierr = DMSetMatType(*dm, mat_type); CHKERRQ(ierr);
244ea65e7bSJed Brown   ierr = DMSetVecType(*dm, vec_type); CHKERRQ(ierr);
254ea65e7bSJed Brown 
261864f1c2SLeila Ghaffari   // Set Tensor elements
271864f1c2SLeila Ghaffari   ierr = PetscOptionsSetValue(NULL, "-dm_plex_simplex", "0"); CHKERRQ(ierr);
281864f1c2SLeila Ghaffari   // Set CL options
291864f1c2SLeila Ghaffari   ierr = DMSetFromOptions(*dm); CHKERRQ(ierr);
3077841947SLeila Ghaffari   ierr = DMViewFromOptions(*dm, NULL, "-dm_view"); CHKERRQ(ierr);
3177841947SLeila Ghaffari   PetscFunctionReturn(0);
3277841947SLeila Ghaffari }
3377841947SLeila Ghaffari 
3477841947SLeila Ghaffari // Setup DM
3577841947SLeila Ghaffari PetscErrorCode SetUpDM(DM dm, ProblemData *problem, PetscInt degree,
36c95f9967SJed Brown                        SimpleBC bc, Physics phys) {
3777841947SLeila Ghaffari   PetscErrorCode ierr;
3877841947SLeila Ghaffari   PetscFunctionBeginUser;
3977841947SLeila Ghaffari   {
4077841947SLeila Ghaffari     // Configure the finite element space and boundary conditions
4177841947SLeila Ghaffari     PetscFE  fe;
4277841947SLeila Ghaffari     PetscInt num_comp_q = 5;
43496f2382SJed Brown     DMLabel label;
4477841947SLeila Ghaffari     ierr = PetscFECreateLagrange(PETSC_COMM_SELF, problem->dim, num_comp_q,
4577841947SLeila Ghaffari                                  PETSC_FALSE, degree, PETSC_DECIDE,
4677841947SLeila Ghaffari                                  &fe); CHKERRQ(ierr);
4777841947SLeila Ghaffari     ierr = PetscObjectSetName((PetscObject)fe, "Q"); CHKERRQ(ierr);
4877841947SLeila Ghaffari     ierr = DMAddField(dm, NULL,(PetscObject)fe); CHKERRQ(ierr);
4977841947SLeila Ghaffari     ierr = DMCreateDS(dm); CHKERRQ(ierr);
50496f2382SJed Brown     ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr);
512fe7aee7SLeila Ghaffari     // Set wall BCs
522fe7aee7SLeila Ghaffari     if (bc->num_wall > 0) {
532fe7aee7SLeila Ghaffari       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label,
542fe7aee7SLeila Ghaffari                            bc->num_wall, bc->walls, 0, bc->num_comps,
552fe7aee7SLeila Ghaffari                            bc->wall_comps, (void(*)(void))problem->bc,
56c95f9967SJed Brown                            NULL, problem->bc_ctx, NULL);  CHKERRQ(ierr);
572fe7aee7SLeila Ghaffari     }
582fe7aee7SLeila Ghaffari     // Set slip BCs in the x direction
592fe7aee7SLeila Ghaffari     if (bc->num_slip[0] > 0) {
602fe7aee7SLeila Ghaffari       PetscInt comps[1] = {1};
612fe7aee7SLeila Ghaffari       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipx", label,
622fe7aee7SLeila Ghaffari                            bc->num_slip[0], bc->slips[0], 0, 1, comps,
63c95f9967SJed Brown                            (void(*)(void))NULL, NULL, problem->bc_ctx, NULL); CHKERRQ(ierr);
642fe7aee7SLeila Ghaffari     }
652fe7aee7SLeila Ghaffari     // Set slip BCs in the y direction
662fe7aee7SLeila Ghaffari     if (bc->num_slip[1] > 0) {
672fe7aee7SLeila Ghaffari       PetscInt comps[1] = {2};
682fe7aee7SLeila Ghaffari       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipy", label,
692fe7aee7SLeila Ghaffari                            bc->num_slip[1], bc->slips[1], 0, 1, comps,
70c95f9967SJed Brown                            (void(*)(void))NULL, NULL, problem->bc_ctx, NULL); CHKERRQ(ierr);
712fe7aee7SLeila Ghaffari     }
722fe7aee7SLeila Ghaffari     // Set slip BCs in the z direction
732fe7aee7SLeila Ghaffari     if (bc->num_slip[2] > 0) {
742fe7aee7SLeila Ghaffari       PetscInt comps[1] = {3};
752fe7aee7SLeila Ghaffari       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", label,
762fe7aee7SLeila Ghaffari                            bc->num_slip[2], bc->slips[2], 0, 1, comps,
77c95f9967SJed Brown                            (void(*)(void))NULL, NULL, problem->bc_ctx, NULL); CHKERRQ(ierr);
782fe7aee7SLeila Ghaffari     }
79363b60e1SJames Wright     {
80363b60e1SJames Wright       PetscBool use_strongstg = PETSC_FALSE;
81363b60e1SJames Wright       ierr = PetscOptionsGetBool(NULL, NULL, "-stg_strong", &use_strongstg, NULL);
82363b60e1SJames Wright       CHKERRQ(ierr);
83363b60e1SJames Wright 
84363b60e1SJames Wright       if (use_strongstg) {
85*65dd5cafSJames Wright         ierr = SetupStrongSTG(dm, bc, problem); CHKERRQ(ierr);
86363b60e1SJames Wright       }
87363b60e1SJames Wright     }
88363b60e1SJames Wright 
8977841947SLeila Ghaffari     ierr = DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL);
9077841947SLeila Ghaffari     CHKERRQ(ierr);
9177841947SLeila Ghaffari     ierr = PetscFEDestroy(&fe); CHKERRQ(ierr);
9277841947SLeila Ghaffari   }
9377841947SLeila Ghaffari   {
9477841947SLeila Ghaffari     // Empty name for conserved field (because there is only one field)
9577841947SLeila Ghaffari     PetscSection section;
9677841947SLeila Ghaffari     ierr = DMGetLocalSection(dm, &section); CHKERRQ(ierr);
9777841947SLeila Ghaffari     ierr = PetscSectionSetFieldName(section, 0, ""); CHKERRQ(ierr);
9877841947SLeila Ghaffari     ierr = PetscSectionSetComponentName(section, 0, 0, "Density");
9977841947SLeila Ghaffari     CHKERRQ(ierr);
10077841947SLeila Ghaffari     ierr = PetscSectionSetComponentName(section, 0, 1, "Momentum X");
10177841947SLeila Ghaffari     CHKERRQ(ierr);
10277841947SLeila Ghaffari     ierr = PetscSectionSetComponentName(section, 0, 2, "Momentum Y");
10377841947SLeila Ghaffari     CHKERRQ(ierr);
10477841947SLeila Ghaffari     ierr = PetscSectionSetComponentName(section, 0, 3, "Momentum Z");
10577841947SLeila Ghaffari     CHKERRQ(ierr);
10677841947SLeila Ghaffari     ierr = PetscSectionSetComponentName(section, 0, 4, "Energy Density");
10777841947SLeila Ghaffari     CHKERRQ(ierr);
10877841947SLeila Ghaffari   }
10977841947SLeila Ghaffari   PetscFunctionReturn(0);
11077841947SLeila Ghaffari }
11177841947SLeila Ghaffari 
11277841947SLeila Ghaffari // Refine DM for high-order viz
11377841947SLeila Ghaffari PetscErrorCode VizRefineDM(DM dm, User user, ProblemData *problem,
114c95f9967SJed Brown                            SimpleBC bc, Physics phys) {
11577841947SLeila Ghaffari   PetscErrorCode ierr;
11677841947SLeila Ghaffari   DM             dm_hierarchy[user->app_ctx->viz_refine + 1];
11777841947SLeila Ghaffari   VecType        vec_type;
11877841947SLeila Ghaffari   PetscFunctionBeginUser;
11977841947SLeila Ghaffari 
12077841947SLeila Ghaffari   ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE); CHKERRQ(ierr);
12177841947SLeila Ghaffari 
12277841947SLeila Ghaffari   dm_hierarchy[0] = dm;
12377841947SLeila Ghaffari   for (PetscInt i = 0, d = user->app_ctx->degree;
12477841947SLeila Ghaffari        i < user->app_ctx->viz_refine; i++) {
12577841947SLeila Ghaffari     Mat interp_next;
12677841947SLeila Ghaffari     ierr = DMRefine(dm_hierarchy[i], MPI_COMM_NULL, &dm_hierarchy[i+1]);
12777841947SLeila Ghaffari     CHKERRQ(ierr);
12877841947SLeila Ghaffari     ierr = DMClearDS(dm_hierarchy[i+1]); CHKERRQ(ierr);
12977841947SLeila Ghaffari     ierr = DMClearFields(dm_hierarchy[i+1]); CHKERRQ(ierr);
13077841947SLeila Ghaffari     ierr = DMSetCoarseDM(dm_hierarchy[i+1], dm_hierarchy[i]); CHKERRQ(ierr);
13177841947SLeila Ghaffari     d = (d + 1) / 2;
13277841947SLeila Ghaffari     if (i + 1 == user->app_ctx->viz_refine) d = 1;
13377841947SLeila Ghaffari     ierr = DMGetVecType(dm, &vec_type); CHKERRQ(ierr);
13477841947SLeila Ghaffari     ierr = DMSetVecType(dm_hierarchy[i+1], vec_type); CHKERRQ(ierr);
135c95f9967SJed Brown     ierr = SetUpDM(dm_hierarchy[i+1], problem, d, bc, phys);
13677841947SLeila Ghaffari     CHKERRQ(ierr);
13777841947SLeila Ghaffari     ierr = DMCreateInterpolation(dm_hierarchy[i], dm_hierarchy[i+1], &interp_next,
13877841947SLeila Ghaffari                                  NULL); CHKERRQ(ierr);
13977841947SLeila Ghaffari     if (!i) user->interp_viz = interp_next;
14077841947SLeila Ghaffari     else {
14177841947SLeila Ghaffari       Mat C;
14277841947SLeila Ghaffari       ierr = MatMatMult(interp_next, user->interp_viz, MAT_INITIAL_MATRIX,
14377841947SLeila Ghaffari                         PETSC_DECIDE, &C); CHKERRQ(ierr);
14477841947SLeila Ghaffari       ierr = MatDestroy(&interp_next); CHKERRQ(ierr);
14577841947SLeila Ghaffari       ierr = MatDestroy(&user->interp_viz); CHKERRQ(ierr);
14677841947SLeila Ghaffari       user->interp_viz = C;
14777841947SLeila Ghaffari     }
14877841947SLeila Ghaffari   }
14977841947SLeila Ghaffari   for (PetscInt i=1; i<user->app_ctx->viz_refine; i++) {
15077841947SLeila Ghaffari     ierr = DMDestroy(&dm_hierarchy[i]); CHKERRQ(ierr);
15177841947SLeila Ghaffari   }
15277841947SLeila Ghaffari   user->dm_viz = dm_hierarchy[user->app_ctx->viz_refine];
15377841947SLeila Ghaffari 
15477841947SLeila Ghaffari   PetscFunctionReturn(0);
15577841947SLeila Ghaffari }
156