xref: /libCEED/examples/fluids/src/setupdm.c (revision 1864f1c2b4e770a2a9adc26a02ef77fc3a284256)
177841947SLeila Ghaffari // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
277841947SLeila Ghaffari // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
377841947SLeila Ghaffari // reserved. See files LICENSE and NOTICE for details.
477841947SLeila Ghaffari //
577841947SLeila Ghaffari // This file is part of CEED, a collection of benchmarks, miniapps, software
677841947SLeila Ghaffari // libraries and APIs for efficient high-order finite element and spectral
777841947SLeila Ghaffari // element discretizations for exascale applications. For more information and
877841947SLeila Ghaffari // source code availability see http://github.com/ceed.
977841947SLeila Ghaffari //
1077841947SLeila Ghaffari // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
1177841947SLeila Ghaffari // a collaborative effort of two U.S. Department of Energy organizations (Office
1277841947SLeila Ghaffari // of Science and the National Nuclear Security Administration) responsible for
1377841947SLeila Ghaffari // the planning and preparation of a capable exascale ecosystem, including
1477841947SLeila Ghaffari // software, applications, hardware, advanced system engineering and early
1577841947SLeila Ghaffari // testbed platforms, in support of the nation's exascale computing imperative.
1677841947SLeila Ghaffari 
1777841947SLeila Ghaffari /// @file
1877841947SLeila Ghaffari /// Setup DM for Navier-Stokes example using PETSc
1977841947SLeila Ghaffari 
2077841947SLeila Ghaffari #include "../navierstokes.h"
2177841947SLeila Ghaffari 
22*1864f1c2SLeila Ghaffari // Create mesh
23*1864f1c2SLeila Ghaffari PetscErrorCode CreateDM(MPI_Comm comm, ProblemData *problem, DM *dm) {
2477841947SLeila Ghaffari   PetscErrorCode   ierr;
2577841947SLeila Ghaffari   PetscFunctionBeginUser;
26*1864f1c2SLeila Ghaffari   // Create DMPLEX
27*1864f1c2SLeila Ghaffari   ierr = DMCreate(comm, dm); CHKERRQ(ierr);
28*1864f1c2SLeila Ghaffari   ierr = DMSetType(*dm, DMPLEX); CHKERRQ(ierr);
29*1864f1c2SLeila Ghaffari   // Set Tensor elements
30*1864f1c2SLeila Ghaffari   ierr = PetscOptionsSetValue(NULL, "-dm_plex_simplex", "0"); CHKERRQ(ierr);
31*1864f1c2SLeila Ghaffari   // Set CL options
32*1864f1c2SLeila Ghaffari   ierr = DMSetFromOptions(*dm); CHKERRQ(ierr);
3377841947SLeila Ghaffari   ierr = DMViewFromOptions(*dm, NULL, "-dm_view"); CHKERRQ(ierr);
3477841947SLeila Ghaffari   PetscFunctionReturn(0);
3577841947SLeila Ghaffari }
3677841947SLeila Ghaffari 
3777841947SLeila Ghaffari // Setup DM
3877841947SLeila Ghaffari PetscErrorCode SetUpDM(DM dm, ProblemData *problem, PetscInt degree,
3977841947SLeila Ghaffari                        SimpleBC bc, Physics phys, void *setup_ctx) {
4077841947SLeila Ghaffari   PetscErrorCode ierr;
4177841947SLeila Ghaffari   PetscFunctionBeginUser;
4277841947SLeila Ghaffari   {
4377841947SLeila Ghaffari     // Configure the finite element space and boundary conditions
4477841947SLeila Ghaffari     PetscFE  fe;
4577841947SLeila Ghaffari     PetscInt num_comp_q = 5;
4677841947SLeila Ghaffari     ierr = PetscFECreateLagrange(PETSC_COMM_SELF, problem->dim, num_comp_q,
4777841947SLeila Ghaffari                                  PETSC_FALSE, degree, PETSC_DECIDE,
4877841947SLeila Ghaffari                                  &fe); CHKERRQ(ierr);
4977841947SLeila Ghaffari     ierr = PetscObjectSetName((PetscObject)fe, "Q"); CHKERRQ(ierr);
5077841947SLeila Ghaffari     ierr = DMAddField(dm, NULL,(PetscObject)fe); CHKERRQ(ierr);
5177841947SLeila Ghaffari     ierr = DMCreateDS(dm); CHKERRQ(ierr);
527ed3e4cdSJeremy L Thompson     {
537ed3e4cdSJeremy L Thompson       /* create FE field for coordinates */
547ed3e4cdSJeremy L Thompson       PetscFE fe_coords;
557ed3e4cdSJeremy L Thompson       PetscInt num_comp_coord;
567ed3e4cdSJeremy L Thompson       ierr = DMGetCoordinateDim(dm, &num_comp_coord); CHKERRQ(ierr);
577ed3e4cdSJeremy L Thompson       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, problem->dim, num_comp_coord,
587ed3e4cdSJeremy L Thompson                                    PETSC_FALSE, 1, 1, &fe_coords); CHKERRQ(ierr);
597ed3e4cdSJeremy L Thompson       ierr = DMProjectCoordinates(dm, fe_coords); CHKERRQ(ierr);
607ed3e4cdSJeremy L Thompson       ierr = PetscFEDestroy(&fe_coords); CHKERRQ(ierr);
617ed3e4cdSJeremy L Thompson     }
6277841947SLeila Ghaffari     ierr = problem->bc_func(dm, bc, phys, setup_ctx);
6377841947SLeila Ghaffari     ierr = DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL);
6477841947SLeila Ghaffari     CHKERRQ(ierr);
6577841947SLeila Ghaffari     ierr = PetscFEDestroy(&fe); CHKERRQ(ierr);
6677841947SLeila Ghaffari   }
6777841947SLeila Ghaffari   {
6877841947SLeila Ghaffari     // Empty name for conserved field (because there is only one field)
6977841947SLeila Ghaffari     PetscSection section;
7077841947SLeila Ghaffari     ierr = DMGetLocalSection(dm, &section); CHKERRQ(ierr);
7177841947SLeila Ghaffari     ierr = PetscSectionSetFieldName(section, 0, ""); CHKERRQ(ierr);
7277841947SLeila Ghaffari     ierr = PetscSectionSetComponentName(section, 0, 0, "Density");
7377841947SLeila Ghaffari     CHKERRQ(ierr);
7477841947SLeila Ghaffari     ierr = PetscSectionSetComponentName(section, 0, 1, "Momentum X");
7577841947SLeila Ghaffari     CHKERRQ(ierr);
7677841947SLeila Ghaffari     ierr = PetscSectionSetComponentName(section, 0, 2, "Momentum Y");
7777841947SLeila Ghaffari     CHKERRQ(ierr);
7877841947SLeila Ghaffari     ierr = PetscSectionSetComponentName(section, 0, 3, "Momentum Z");
7977841947SLeila Ghaffari     CHKERRQ(ierr);
8077841947SLeila Ghaffari     ierr = PetscSectionSetComponentName(section, 0, 4, "Energy Density");
8177841947SLeila Ghaffari     CHKERRQ(ierr);
8277841947SLeila Ghaffari   }
8377841947SLeila Ghaffari   PetscFunctionReturn(0);
8477841947SLeila Ghaffari }
8577841947SLeila Ghaffari 
8677841947SLeila Ghaffari // Refine DM for high-order viz
8777841947SLeila Ghaffari PetscErrorCode VizRefineDM(DM dm, User user, ProblemData *problem,
8877841947SLeila Ghaffari                            SimpleBC bc, Physics phys, void *setup_ctx) {
8977841947SLeila Ghaffari   PetscErrorCode ierr;
9077841947SLeila Ghaffari   DM             dm_hierarchy[user->app_ctx->viz_refine + 1];
9177841947SLeila Ghaffari   VecType        vec_type;
9277841947SLeila Ghaffari   PetscFunctionBeginUser;
9377841947SLeila Ghaffari 
9477841947SLeila Ghaffari   ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE); CHKERRQ(ierr);
9577841947SLeila Ghaffari 
9677841947SLeila Ghaffari   dm_hierarchy[0] = dm;
9777841947SLeila Ghaffari   for (PetscInt i = 0, d = user->app_ctx->degree;
9877841947SLeila Ghaffari        i < user->app_ctx->viz_refine; i++) {
9977841947SLeila Ghaffari     Mat interp_next;
10077841947SLeila Ghaffari     ierr = DMRefine(dm_hierarchy[i], MPI_COMM_NULL, &dm_hierarchy[i+1]);
10177841947SLeila Ghaffari     CHKERRQ(ierr);
10277841947SLeila Ghaffari     ierr = DMClearDS(dm_hierarchy[i+1]); CHKERRQ(ierr);
10377841947SLeila Ghaffari     ierr = DMClearFields(dm_hierarchy[i+1]); CHKERRQ(ierr);
10477841947SLeila Ghaffari     ierr = DMSetCoarseDM(dm_hierarchy[i+1], dm_hierarchy[i]); CHKERRQ(ierr);
10577841947SLeila Ghaffari     d = (d + 1) / 2;
10677841947SLeila Ghaffari     if (i + 1 == user->app_ctx->viz_refine) d = 1;
10777841947SLeila Ghaffari     ierr = DMGetVecType(dm, &vec_type); CHKERRQ(ierr);
10877841947SLeila Ghaffari     ierr = DMSetVecType(dm_hierarchy[i+1], vec_type); CHKERRQ(ierr);
10977841947SLeila Ghaffari     ierr = SetUpDM(dm_hierarchy[i+1], problem, d, bc, phys, setup_ctx);
11077841947SLeila Ghaffari     CHKERRQ(ierr);
11177841947SLeila Ghaffari     ierr = DMCreateInterpolation(dm_hierarchy[i], dm_hierarchy[i+1], &interp_next,
11277841947SLeila Ghaffari                                  NULL); CHKERRQ(ierr);
11377841947SLeila Ghaffari     if (!i) user->interp_viz = interp_next;
11477841947SLeila Ghaffari     else {
11577841947SLeila Ghaffari       Mat C;
11677841947SLeila Ghaffari       ierr = MatMatMult(interp_next, user->interp_viz, MAT_INITIAL_MATRIX,
11777841947SLeila Ghaffari                         PETSC_DECIDE, &C); CHKERRQ(ierr);
11877841947SLeila Ghaffari       ierr = MatDestroy(&interp_next); CHKERRQ(ierr);
11977841947SLeila Ghaffari       ierr = MatDestroy(&user->interp_viz); CHKERRQ(ierr);
12077841947SLeila Ghaffari       user->interp_viz = C;
12177841947SLeila Ghaffari     }
12277841947SLeila Ghaffari   }
12377841947SLeila Ghaffari   for (PetscInt i=1; i<user->app_ctx->viz_refine; i++) {
12477841947SLeila Ghaffari     ierr = DMDestroy(&dm_hierarchy[i]); CHKERRQ(ierr);
12577841947SLeila Ghaffari   }
12677841947SLeila Ghaffari   user->dm_viz = dm_hierarchy[user->app_ctx->viz_refine];
12777841947SLeila Ghaffari 
12877841947SLeila Ghaffari   PetscFunctionReturn(0);
12977841947SLeila Ghaffari }
130