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 221864f1c2SLeila Ghaffari // Create mesh 231864f1c2SLeila Ghaffari PetscErrorCode CreateDM(MPI_Comm comm, ProblemData *problem, DM *dm) { 2477841947SLeila Ghaffari PetscErrorCode ierr; 2577841947SLeila Ghaffari PetscFunctionBeginUser; 261864f1c2SLeila Ghaffari // Create DMPLEX 271864f1c2SLeila Ghaffari ierr = DMCreate(comm, dm); CHKERRQ(ierr); 281864f1c2SLeila Ghaffari ierr = DMSetType(*dm, DMPLEX); CHKERRQ(ierr); 291864f1c2SLeila Ghaffari // Set Tensor elements 301864f1c2SLeila Ghaffari ierr = PetscOptionsSetValue(NULL, "-dm_plex_simplex", "0"); CHKERRQ(ierr); 311864f1c2SLeila Ghaffari // Set CL options 321864f1c2SLeila 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 } 62*2fe7aee7SLeila Ghaffari // Set wall BCs 63*2fe7aee7SLeila Ghaffari if (bc->num_wall > 0) { 64*2fe7aee7SLeila Ghaffari DMLabel label; 65*2fe7aee7SLeila Ghaffari ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr); 66*2fe7aee7SLeila Ghaffari ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, 67*2fe7aee7SLeila Ghaffari bc->num_wall, bc->walls, 0, bc->num_comps, 68*2fe7aee7SLeila Ghaffari bc->wall_comps, (void(*)(void))problem->bc, 69*2fe7aee7SLeila Ghaffari NULL, setup_ctx, NULL); CHKERRQ(ierr); 70*2fe7aee7SLeila Ghaffari } 71*2fe7aee7SLeila Ghaffari // Set slip BCs in the x direction 72*2fe7aee7SLeila Ghaffari if (bc->num_slip[0] > 0) { 73*2fe7aee7SLeila Ghaffari DMLabel label; 74*2fe7aee7SLeila Ghaffari ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr); 75*2fe7aee7SLeila Ghaffari PetscInt comps[1] = {1}; 76*2fe7aee7SLeila Ghaffari ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipx", label, 77*2fe7aee7SLeila Ghaffari bc->num_slip[0], bc->slips[0], 0, 1, comps, 78*2fe7aee7SLeila Ghaffari (void(*)(void))NULL, NULL, setup_ctx, NULL); CHKERRQ(ierr); 79*2fe7aee7SLeila Ghaffari } 80*2fe7aee7SLeila Ghaffari // Set slip BCs in the y direction 81*2fe7aee7SLeila Ghaffari if (bc->num_slip[1] > 0) { 82*2fe7aee7SLeila Ghaffari DMLabel label; 83*2fe7aee7SLeila Ghaffari ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr); 84*2fe7aee7SLeila Ghaffari PetscInt comps[1] = {2}; 85*2fe7aee7SLeila Ghaffari ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipy", label, 86*2fe7aee7SLeila Ghaffari bc->num_slip[1], bc->slips[1], 0, 1, comps, 87*2fe7aee7SLeila Ghaffari (void(*)(void))NULL, NULL, setup_ctx, NULL); CHKERRQ(ierr); 88*2fe7aee7SLeila Ghaffari } 89*2fe7aee7SLeila Ghaffari // Set slip BCs in the z direction 90*2fe7aee7SLeila Ghaffari if (bc->num_slip[2] > 0) { 91*2fe7aee7SLeila Ghaffari DMLabel label; 92*2fe7aee7SLeila Ghaffari ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr); 93*2fe7aee7SLeila Ghaffari PetscInt comps[1] = {3}; 94*2fe7aee7SLeila Ghaffari ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", label, 95*2fe7aee7SLeila Ghaffari bc->num_slip[2], bc->slips[2], 0, 1, comps, 96*2fe7aee7SLeila Ghaffari (void(*)(void))NULL, NULL, setup_ctx, NULL); CHKERRQ(ierr); 97*2fe7aee7SLeila Ghaffari } 9877841947SLeila Ghaffari ierr = DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL); 9977841947SLeila Ghaffari CHKERRQ(ierr); 10077841947SLeila Ghaffari ierr = PetscFEDestroy(&fe); CHKERRQ(ierr); 10177841947SLeila Ghaffari } 10277841947SLeila Ghaffari { 10377841947SLeila Ghaffari // Empty name for conserved field (because there is only one field) 10477841947SLeila Ghaffari PetscSection section; 10577841947SLeila Ghaffari ierr = DMGetLocalSection(dm, §ion); CHKERRQ(ierr); 10677841947SLeila Ghaffari ierr = PetscSectionSetFieldName(section, 0, ""); CHKERRQ(ierr); 10777841947SLeila Ghaffari ierr = PetscSectionSetComponentName(section, 0, 0, "Density"); 10877841947SLeila Ghaffari CHKERRQ(ierr); 10977841947SLeila Ghaffari ierr = PetscSectionSetComponentName(section, 0, 1, "Momentum X"); 11077841947SLeila Ghaffari CHKERRQ(ierr); 11177841947SLeila Ghaffari ierr = PetscSectionSetComponentName(section, 0, 2, "Momentum Y"); 11277841947SLeila Ghaffari CHKERRQ(ierr); 11377841947SLeila Ghaffari ierr = PetscSectionSetComponentName(section, 0, 3, "Momentum Z"); 11477841947SLeila Ghaffari CHKERRQ(ierr); 11577841947SLeila Ghaffari ierr = PetscSectionSetComponentName(section, 0, 4, "Energy Density"); 11677841947SLeila Ghaffari CHKERRQ(ierr); 11777841947SLeila Ghaffari } 11877841947SLeila Ghaffari PetscFunctionReturn(0); 11977841947SLeila Ghaffari } 12077841947SLeila Ghaffari 12177841947SLeila Ghaffari // Refine DM for high-order viz 12277841947SLeila Ghaffari PetscErrorCode VizRefineDM(DM dm, User user, ProblemData *problem, 12377841947SLeila Ghaffari SimpleBC bc, Physics phys, void *setup_ctx) { 12477841947SLeila Ghaffari PetscErrorCode ierr; 12577841947SLeila Ghaffari DM dm_hierarchy[user->app_ctx->viz_refine + 1]; 12677841947SLeila Ghaffari VecType vec_type; 12777841947SLeila Ghaffari PetscFunctionBeginUser; 12877841947SLeila Ghaffari 12977841947SLeila Ghaffari ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE); CHKERRQ(ierr); 13077841947SLeila Ghaffari 13177841947SLeila Ghaffari dm_hierarchy[0] = dm; 13277841947SLeila Ghaffari for (PetscInt i = 0, d = user->app_ctx->degree; 13377841947SLeila Ghaffari i < user->app_ctx->viz_refine; i++) { 13477841947SLeila Ghaffari Mat interp_next; 13577841947SLeila Ghaffari ierr = DMRefine(dm_hierarchy[i], MPI_COMM_NULL, &dm_hierarchy[i+1]); 13677841947SLeila Ghaffari CHKERRQ(ierr); 13777841947SLeila Ghaffari ierr = DMClearDS(dm_hierarchy[i+1]); CHKERRQ(ierr); 13877841947SLeila Ghaffari ierr = DMClearFields(dm_hierarchy[i+1]); CHKERRQ(ierr); 13977841947SLeila Ghaffari ierr = DMSetCoarseDM(dm_hierarchy[i+1], dm_hierarchy[i]); CHKERRQ(ierr); 14077841947SLeila Ghaffari d = (d + 1) / 2; 14177841947SLeila Ghaffari if (i + 1 == user->app_ctx->viz_refine) d = 1; 14277841947SLeila Ghaffari ierr = DMGetVecType(dm, &vec_type); CHKERRQ(ierr); 14377841947SLeila Ghaffari ierr = DMSetVecType(dm_hierarchy[i+1], vec_type); CHKERRQ(ierr); 14477841947SLeila Ghaffari ierr = SetUpDM(dm_hierarchy[i+1], problem, d, bc, phys, setup_ctx); 14577841947SLeila Ghaffari CHKERRQ(ierr); 14677841947SLeila Ghaffari ierr = DMCreateInterpolation(dm_hierarchy[i], dm_hierarchy[i+1], &interp_next, 14777841947SLeila Ghaffari NULL); CHKERRQ(ierr); 14877841947SLeila Ghaffari if (!i) user->interp_viz = interp_next; 14977841947SLeila Ghaffari else { 15077841947SLeila Ghaffari Mat C; 15177841947SLeila Ghaffari ierr = MatMatMult(interp_next, user->interp_viz, MAT_INITIAL_MATRIX, 15277841947SLeila Ghaffari PETSC_DECIDE, &C); CHKERRQ(ierr); 15377841947SLeila Ghaffari ierr = MatDestroy(&interp_next); CHKERRQ(ierr); 15477841947SLeila Ghaffari ierr = MatDestroy(&user->interp_viz); CHKERRQ(ierr); 15577841947SLeila Ghaffari user->interp_viz = C; 15677841947SLeila Ghaffari } 15777841947SLeila Ghaffari } 15877841947SLeila Ghaffari for (PetscInt i=1; i<user->app_ctx->viz_refine; i++) { 15977841947SLeila Ghaffari ierr = DMDestroy(&dm_hierarchy[i]); CHKERRQ(ierr); 16077841947SLeila Ghaffari } 16177841947SLeila Ghaffari user->dm_viz = dm_hierarchy[user->app_ctx->viz_refine]; 16277841947SLeila Ghaffari 16377841947SLeila Ghaffari PetscFunctionReturn(0); 16477841947SLeila Ghaffari } 165