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; 46*496f2382SJed Brown DMLabel label; 4777841947SLeila Ghaffari ierr = PetscFECreateLagrange(PETSC_COMM_SELF, problem->dim, num_comp_q, 4877841947SLeila Ghaffari PETSC_FALSE, degree, PETSC_DECIDE, 4977841947SLeila Ghaffari &fe); CHKERRQ(ierr); 5077841947SLeila Ghaffari ierr = PetscObjectSetName((PetscObject)fe, "Q"); CHKERRQ(ierr); 5177841947SLeila Ghaffari ierr = DMAddField(dm, NULL,(PetscObject)fe); CHKERRQ(ierr); 5277841947SLeila Ghaffari ierr = DMCreateDS(dm); CHKERRQ(ierr); 537ed3e4cdSJeremy L Thompson { 547ed3e4cdSJeremy L Thompson /* create FE field for coordinates */ 557ed3e4cdSJeremy L Thompson PetscFE fe_coords; 567ed3e4cdSJeremy L Thompson PetscInt num_comp_coord; 577ed3e4cdSJeremy L Thompson ierr = DMGetCoordinateDim(dm, &num_comp_coord); CHKERRQ(ierr); 587ed3e4cdSJeremy L Thompson ierr = PetscFECreateLagrange(PETSC_COMM_SELF, problem->dim, num_comp_coord, 597ed3e4cdSJeremy L Thompson PETSC_FALSE, 1, 1, &fe_coords); CHKERRQ(ierr); 607ed3e4cdSJeremy L Thompson ierr = DMProjectCoordinates(dm, fe_coords); CHKERRQ(ierr); 617ed3e4cdSJeremy L Thompson ierr = PetscFEDestroy(&fe_coords); CHKERRQ(ierr); 627ed3e4cdSJeremy L Thompson } 63*496f2382SJed Brown ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr); 642fe7aee7SLeila Ghaffari // Set wall BCs 652fe7aee7SLeila Ghaffari if (bc->num_wall > 0) { 662fe7aee7SLeila Ghaffari ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, 672fe7aee7SLeila Ghaffari bc->num_wall, bc->walls, 0, bc->num_comps, 682fe7aee7SLeila Ghaffari bc->wall_comps, (void(*)(void))problem->bc, 692fe7aee7SLeila Ghaffari NULL, setup_ctx, NULL); CHKERRQ(ierr); 702fe7aee7SLeila Ghaffari } 712fe7aee7SLeila Ghaffari // Set slip BCs in the x direction 722fe7aee7SLeila Ghaffari if (bc->num_slip[0] > 0) { 732fe7aee7SLeila Ghaffari PetscInt comps[1] = {1}; 742fe7aee7SLeila Ghaffari ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipx", label, 752fe7aee7SLeila Ghaffari bc->num_slip[0], bc->slips[0], 0, 1, comps, 762fe7aee7SLeila Ghaffari (void(*)(void))NULL, NULL, setup_ctx, NULL); CHKERRQ(ierr); 772fe7aee7SLeila Ghaffari } 782fe7aee7SLeila Ghaffari // Set slip BCs in the y direction 792fe7aee7SLeila Ghaffari if (bc->num_slip[1] > 0) { 802fe7aee7SLeila Ghaffari PetscInt comps[1] = {2}; 812fe7aee7SLeila Ghaffari ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipy", label, 822fe7aee7SLeila Ghaffari bc->num_slip[1], bc->slips[1], 0, 1, comps, 832fe7aee7SLeila Ghaffari (void(*)(void))NULL, NULL, setup_ctx, NULL); CHKERRQ(ierr); 842fe7aee7SLeila Ghaffari } 852fe7aee7SLeila Ghaffari // Set slip BCs in the z direction 862fe7aee7SLeila Ghaffari if (bc->num_slip[2] > 0) { 872fe7aee7SLeila Ghaffari PetscInt comps[1] = {3}; 882fe7aee7SLeila Ghaffari ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", label, 892fe7aee7SLeila Ghaffari bc->num_slip[2], bc->slips[2], 0, 1, comps, 902fe7aee7SLeila Ghaffari (void(*)(void))NULL, NULL, setup_ctx, NULL); CHKERRQ(ierr); 912fe7aee7SLeila Ghaffari } 9277841947SLeila Ghaffari ierr = DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL); 9377841947SLeila Ghaffari CHKERRQ(ierr); 9477841947SLeila Ghaffari ierr = PetscFEDestroy(&fe); CHKERRQ(ierr); 9577841947SLeila Ghaffari } 9677841947SLeila Ghaffari { 9777841947SLeila Ghaffari // Empty name for conserved field (because there is only one field) 9877841947SLeila Ghaffari PetscSection section; 9977841947SLeila Ghaffari ierr = DMGetLocalSection(dm, §ion); CHKERRQ(ierr); 10077841947SLeila Ghaffari ierr = PetscSectionSetFieldName(section, 0, ""); CHKERRQ(ierr); 10177841947SLeila Ghaffari ierr = PetscSectionSetComponentName(section, 0, 0, "Density"); 10277841947SLeila Ghaffari CHKERRQ(ierr); 10377841947SLeila Ghaffari ierr = PetscSectionSetComponentName(section, 0, 1, "Momentum X"); 10477841947SLeila Ghaffari CHKERRQ(ierr); 10577841947SLeila Ghaffari ierr = PetscSectionSetComponentName(section, 0, 2, "Momentum Y"); 10677841947SLeila Ghaffari CHKERRQ(ierr); 10777841947SLeila Ghaffari ierr = PetscSectionSetComponentName(section, 0, 3, "Momentum Z"); 10877841947SLeila Ghaffari CHKERRQ(ierr); 10977841947SLeila Ghaffari ierr = PetscSectionSetComponentName(section, 0, 4, "Energy Density"); 11077841947SLeila Ghaffari CHKERRQ(ierr); 11177841947SLeila Ghaffari } 11277841947SLeila Ghaffari PetscFunctionReturn(0); 11377841947SLeila Ghaffari } 11477841947SLeila Ghaffari 11577841947SLeila Ghaffari // Refine DM for high-order viz 11677841947SLeila Ghaffari PetscErrorCode VizRefineDM(DM dm, User user, ProblemData *problem, 11777841947SLeila Ghaffari SimpleBC bc, Physics phys, void *setup_ctx) { 11877841947SLeila Ghaffari PetscErrorCode ierr; 11977841947SLeila Ghaffari DM dm_hierarchy[user->app_ctx->viz_refine + 1]; 12077841947SLeila Ghaffari VecType vec_type; 12177841947SLeila Ghaffari PetscFunctionBeginUser; 12277841947SLeila Ghaffari 12377841947SLeila Ghaffari ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE); CHKERRQ(ierr); 12477841947SLeila Ghaffari 12577841947SLeila Ghaffari dm_hierarchy[0] = dm; 12677841947SLeila Ghaffari for (PetscInt i = 0, d = user->app_ctx->degree; 12777841947SLeila Ghaffari i < user->app_ctx->viz_refine; i++) { 12877841947SLeila Ghaffari Mat interp_next; 12977841947SLeila Ghaffari ierr = DMRefine(dm_hierarchy[i], MPI_COMM_NULL, &dm_hierarchy[i+1]); 13077841947SLeila Ghaffari CHKERRQ(ierr); 13177841947SLeila Ghaffari ierr = DMClearDS(dm_hierarchy[i+1]); CHKERRQ(ierr); 13277841947SLeila Ghaffari ierr = DMClearFields(dm_hierarchy[i+1]); CHKERRQ(ierr); 13377841947SLeila Ghaffari ierr = DMSetCoarseDM(dm_hierarchy[i+1], dm_hierarchy[i]); CHKERRQ(ierr); 13477841947SLeila Ghaffari d = (d + 1) / 2; 13577841947SLeila Ghaffari if (i + 1 == user->app_ctx->viz_refine) d = 1; 13677841947SLeila Ghaffari ierr = DMGetVecType(dm, &vec_type); CHKERRQ(ierr); 13777841947SLeila Ghaffari ierr = DMSetVecType(dm_hierarchy[i+1], vec_type); CHKERRQ(ierr); 13877841947SLeila Ghaffari ierr = SetUpDM(dm_hierarchy[i+1], problem, d, bc, phys, setup_ctx); 13977841947SLeila Ghaffari CHKERRQ(ierr); 14077841947SLeila Ghaffari ierr = DMCreateInterpolation(dm_hierarchy[i], dm_hierarchy[i+1], &interp_next, 14177841947SLeila Ghaffari NULL); CHKERRQ(ierr); 14277841947SLeila Ghaffari if (!i) user->interp_viz = interp_next; 14377841947SLeila Ghaffari else { 14477841947SLeila Ghaffari Mat C; 14577841947SLeila Ghaffari ierr = MatMatMult(interp_next, user->interp_viz, MAT_INITIAL_MATRIX, 14677841947SLeila Ghaffari PETSC_DECIDE, &C); CHKERRQ(ierr); 14777841947SLeila Ghaffari ierr = MatDestroy(&interp_next); CHKERRQ(ierr); 14877841947SLeila Ghaffari ierr = MatDestroy(&user->interp_viz); CHKERRQ(ierr); 14977841947SLeila Ghaffari user->interp_viz = C; 15077841947SLeila Ghaffari } 15177841947SLeila Ghaffari } 15277841947SLeila Ghaffari for (PetscInt i=1; i<user->app_ctx->viz_refine; i++) { 15377841947SLeila Ghaffari ierr = DMDestroy(&dm_hierarchy[i]); CHKERRQ(ierr); 15477841947SLeila Ghaffari } 15577841947SLeila Ghaffari user->dm_viz = dm_hierarchy[user->app_ctx->viz_refine]; 15677841947SLeila Ghaffari 15777841947SLeila Ghaffari PetscFunctionReturn(0); 15877841947SLeila Ghaffari } 159