1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at 2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights 3 // reserved. See files LICENSE and NOTICE for details. 4 // 5 // This file is part of CEED, a collection of benchmarks, miniapps, software 6 // libraries and APIs for efficient high-order finite element and spectral 7 // element discretizations for exascale applications. For more information and 8 // source code availability see http://github.com/ceed. 9 // 10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 11 // a collaborative effort of two U.S. Department of Energy organizations (Office 12 // of Science and the National Nuclear Security Administration) responsible for 13 // the planning and preparation of a capable exascale ecosystem, including 14 // software, applications, hardware, advanced system engineering and early 15 // testbed platforms, in support of the nation's exascale computing imperative. 16 17 // libCEED + PETSc Example: Navier-Stokes 18 // 19 // This example demonstrates a simple usage of libCEED with PETSc to solve a 20 // Navier-Stokes problem. 21 // 22 // The code is intentionally "raw", using only low-level communication 23 // primitives. 24 // 25 // Build with: 26 // 27 // make [PETSC_DIR=</path/to/petsc>] [CEED_DIR=</path/to/libceed>] navierstokes 28 // 29 // Sample runs: 30 // 31 // ./navierstokes -ceed /cpu/self -problem density_current -degree 1 32 // ./navierstokes -ceed /gpu/cuda -problem advection -degree 1 33 // 34 //TESTARGS(name="dc_explicit") -ceed {ceed_resource} -test -degree 3 -dm_plex_box_faces 1,1,2 -units_kilogram 1e-9 -lx 125 -ly 125 -lz 250 -center 62.5,62.5,187.5 -rc 100. -thetaC -35. -ts_dt 1e-3 -compare_final_state_atol 1E-11 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-dc-explicit.bin 35 //TESTARGS(name="dc_implicit_stab_none") -ceed {ceed_resource} -test -degree 3 -dm_plex_box_faces 1,1,2 -units_kilogram 1e-9 -lx 125 -ly 125 -lz 250 -center 62.5,62.5,187.5 -rc 100. -thetaC -35. -ksp_atol 1e-4 -ksp_rtol 1e-3 -ksp_type bcgs -snes_atol 1e-3 -snes_lag_jacobian 100 -snes_lag_jacobian_persists -snes_mf_operator -ts_dt 1e-3 -implicit -ts_type alpha -compare_final_state_atol 5E-4 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-dc-implicit-stab-none.bin 36 //TESTARGS(name="adv_rotation_explicit_strong") -ceed {ceed_resource} -test -problem advection -strong_form 1 -degree 3 -dm_plex_box_faces 2,2,2 -units_kilogram 1e-9 -lx 125 -ly 125 -lz 250 -rc 100. -ts_dt 1e-3 -compare_final_state_atol 1E-11 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-adv-rotation-explicit-strong.bin 37 //TESTARGS(name="adv_rotation_implicit_sharp_cylinder") -ceed {ceed_resource} -test -problem advection -bubble_type cylinder -bubble_continuity back_sharp -degree 3 -dm_plex_box_faces 1,1,2 -units_kilogram 1e-9 -lx 125 -ly 125 -lz 250 -rc 100. -ksp_atol 1e-4 -ksp_rtol 1e-3 -ksp_type bcgs -snes_atol 1e-3 -snes_lag_jacobian 100 -snes_lag_jacobian_persists -snes_mf_operator -ts_dt 1e-3 -implicit -ts_type alpha -compare_final_state_atol 5E-4 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-adv-rotation-implicit-sharp-cylinder.bin 38 //TESTARGS(name="adv_rotation_implicit_stab_supg") -ceed {ceed_resource} -test -problem advection -CtauS .3 -stab supg -degree 3 -dm_plex_box_faces 1,1,2 -units_kilogram 1e-9 -lx 125 -ly 125 -lz 250 -rc 100. -ksp_atol 1e-4 -ksp_rtol 1e-3 -ksp_type bcgs -snes_atol 1e-3 -snes_lag_jacobian 100 -snes_lag_jacobian_persists -snes_mf_operator -ts_dt 1e-3 -implicit -ts_type alpha -compare_final_state_atol 5E-4 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-adv-rotation-implicit-stab-supg.bin 39 //TESTARGS(name="adv_translation_implicit_stab_su") -ceed {ceed_resource} -test -problem advection -CtauS .3 -stab su -degree 3 -dm_plex_box_faces 1,1,2 -units_kilogram 1e-9 -lx 125 -ly 125 -lz 250 -rc 100. -ksp_atol 1e-4 -ksp_rtol 1e-3 -ksp_type bcgs -snes_atol 1e-3 -snes_lag_jacobian 100 -snes_lag_jacobian_persists -snes_mf_operator -ts_dt 1e-3 -implicit -ts_type alpha -wind_type translation -wind_translation .53,-1.33,-2.65 -compare_final_state_atol 5E-4 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-adv-translation-implicit-stab-su.bin 40 //TESTARGS(name="adv2d_rotation_explicit_strong") -ceed {ceed_resource} -test -problem advection2d -strong_form 1 -degree 3 -dm_plex_box_faces 2,2 -units_kilogram 1e-9 -lx 125 -ly 125 -rc 100. -ts_dt 1e-3 -compare_final_state_atol 1E-11 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-adv2d-rotation-explicit-strong.bin 41 //TESTARGS(name="adv2d_rotation_implicit_stab_supg") -ceed {ceed_resource} -test -problem advection2d -CtauS .3 -stab supg -degree 3 -dm_plex_box_faces 1,1,2 -units_kilogram 1e-9 -lx 125 -ly 125 -lz 250 -rc 100. -ksp_atol 1e-4 -ksp_rtol 1e-3 -ksp_type bcgs -snes_atol 1e-3 -snes_lag_jacobian 100 -snes_lag_jacobian_persists -snes_mf_operator -ts_dt 1e-3 -implicit -ts_type alpha -compare_final_state_atol 5E-4 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-adv2d-rotation-implicit-stab-supg.bin 42 //TESTARGS(name="adv2d_translation_implicit_stab_su") -ceed {ceed_resource} -test -problem advection2d -CtauS .3 -stab su -degree 3 -dm_plex_box_faces 1,1,2 -units_kilogram 1e-9 -lx 125 -ly 125 -lz 250 -rc 100. -ksp_atol 1e-4 -ksp_rtol 1e-3 -ksp_type bcgs -snes_atol 1e-3 -snes_lag_jacobian 100 -snes_lag_jacobian_persists -snes_mf_operator -ts_dt 1e-3 -implicit -ts_type alpha -wind_type translation -wind_translation .53,-1.33,0 -compare_final_state_atol 5E-4 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-adv2d-translation-implicit-stab-su.bin 43 //TESTARGS(name="euler_implicit") -ceed {ceed_resource} -test -problem euler_vortex -degree 3 -dm_plex_box_faces 1,1,2 -units_meter 1e-4 -lx 125 -ly 125 -lz 1 -mean_velocity 1.4,-2.,0 -vortex_strength 2 -ksp_atol 1e-4 -ksp_rtol 1e-3 -ksp_type bcgs -snes_atol 1e-3 -snes_lag_jacobian 100 -snes_lag_jacobian_persists -snes_mf_operator -ts_dt 1e-3 -implicit -ts_type alpha -compare_final_state_atol 5E-4 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-euler-implicit.bin 44 //TESTARGS(name="euler_explicit") -ceed {ceed_resource} -test -problem euler_vortex -degree 3 -dm_plex_box_faces 2,2,1 -units_meter 1e-4 -lx 125 -ly 125 -lz 1 -mean_velocity 1.4,-2.,0 -vortex_strength 2 -ts_dt 1e-7 -ts_rk_type 5bs -ts_rtol 1e-10 -ts_atol 1e-10 -compare_final_state_atol 1E-7 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-euler-explicit.bin 45 46 /// @file 47 /// Navier-Stokes example using PETSc 48 49 const char help[] = "Solve Navier-Stokes using PETSc and libCEED\n"; 50 51 #include "navierstokes.h" 52 53 int main(int argc, char **argv) { 54 // --------------------------------------------------------------------------- 55 // Initialize PETSc 56 // --------------------------------------------------------------------------- 57 PetscInt ierr; 58 ierr = PetscInitialize(&argc, &argv, NULL, help); 59 if (ierr) return ierr; 60 61 // --------------------------------------------------------------------------- 62 // Create structs 63 // --------------------------------------------------------------------------- 64 AppCtx app_ctx; 65 ierr = PetscCalloc1(1, &app_ctx); CHKERRQ(ierr); 66 67 ProblemData *problem = NULL; 68 ierr = PetscCalloc1(1, &problem); CHKERRQ(ierr); 69 70 User user; 71 ierr = PetscCalloc1(1, &user); CHKERRQ(ierr); 72 73 CeedData ceed_data; 74 ierr = PetscCalloc1(1, &ceed_data); CHKERRQ(ierr); 75 76 SimpleBC bc; 77 ierr = PetscCalloc1(1, &bc); CHKERRQ(ierr); 78 79 SetupContext setup_ctx; 80 ierr = PetscCalloc1(1, &setup_ctx); CHKERRQ(ierr); 81 82 Physics phys_ctx; 83 ierr = PetscCalloc1(1, &phys_ctx); CHKERRQ(ierr); 84 85 Units units; 86 ierr = PetscCalloc1(1, &units); CHKERRQ(ierr); 87 88 user->app_ctx = app_ctx; 89 user->units = units; 90 user->phys = phys_ctx; 91 92 // --------------------------------------------------------------------------- 93 // Process command line options 94 // --------------------------------------------------------------------------- 95 // -- Register problems to be available on the command line 96 ierr = RegisterProblems_NS(app_ctx); CHKERRQ(ierr); 97 98 // -- Process general command line options 99 MPI_Comm comm = PETSC_COMM_WORLD; 100 user->comm = comm; 101 ierr = ProcessCommandLineOptions(comm, app_ctx); CHKERRQ(ierr); 102 103 // --------------------------------------------------------------------------- 104 // Choose the problem from the list of registered problems 105 // --------------------------------------------------------------------------- 106 { 107 PetscErrorCode (*p)(ProblemData *, void *, void *); 108 ierr = PetscFunctionListFind(app_ctx->problems, app_ctx->problem_name, &p); 109 CHKERRQ(ierr); 110 if (!p) SETERRQ1(PETSC_COMM_SELF, 1, "Problem '%s' not found", 111 app_ctx->problem_name); 112 ierr = (*p)(problem, &setup_ctx, &user); CHKERRQ(ierr); 113 } 114 115 // --------------------------------------------------------------------------- 116 // Initialize libCEED 117 // --------------------------------------------------------------------------- 118 // -- Initialize backend 119 Ceed ceed; 120 CeedInit(app_ctx->ceed_resource, &ceed); 121 user->ceed = ceed; 122 123 // -- Check preferred MemType 124 CeedMemType mem_type_backend; 125 CeedGetPreferredMemType(ceed, &mem_type_backend); 126 127 // --------------------------------------------------------------------------- 128 // Set up global mesh 129 // --------------------------------------------------------------------------- 130 // -- Create distribute DM 131 DM dm; 132 ierr = CreateDistributedDM(comm, problem, setup_ctx, &dm); CHKERRQ(ierr); 133 VecType vec_type = NULL; 134 switch (mem_type_backend) { 135 case CEED_MEM_HOST: vec_type = VECSTANDARD; break; 136 case CEED_MEM_DEVICE: { 137 const char *resolved; 138 CeedGetResource(ceed, &resolved); 139 if (strstr(resolved, "/gpu/cuda")) vec_type = VECCUDA; 140 else if (strstr(resolved, "/gpu/hip")) vec_type = VECHIP; 141 else vec_type = VECSTANDARD; 142 } 143 } 144 ierr = DMSetVecType(dm, vec_type); CHKERRQ(ierr); 145 ierr = DMSetFromOptions(dm); CHKERRQ(ierr); 146 user->dm = dm; 147 148 // -- Set up DM 149 ierr = SetUpDM(dm, problem, app_ctx->degree, bc, phys_ctx, setup_ctx); 150 CHKERRQ(ierr); 151 152 // -- Refine DM for high-order viz 153 if (app_ctx->viz_refine) { 154 ierr = VizRefineDM(dm, user, problem, bc, phys_ctx, setup_ctx); 155 CHKERRQ(ierr); 156 } 157 158 // --------------------------------------------------------------------------- 159 // Set up libCEED 160 // --------------------------------------------------------------------------- 161 // -- Set up libCEED objects 162 ierr = SetupLibceed(ceed, ceed_data, dm, user, app_ctx, problem, bc); 163 CHKERRQ(ierr); 164 165 // -- Set up context for QFunctions 166 ierr = problem->setup_ctx(ceed, ceed_data, app_ctx, setup_ctx, phys_ctx); 167 CHKERRQ(ierr); 168 169 // --------------------------------------------------------------------------- 170 // Set up ICs 171 // --------------------------------------------------------------------------- 172 // -- Set up global state vector Q 173 Vec Q; 174 ierr = DMCreateGlobalVector(dm, &Q); CHKERRQ(ierr); 175 ierr = VecZeroEntries(Q); CHKERRQ(ierr); 176 177 // -- Set up local state vector Q_loc 178 Vec Q_loc; 179 ierr = DMGetLocalVector(dm, &Q_loc); CHKERRQ(ierr); 180 181 // -- Fix multiplicity for ICs 182 ierr = ICs_FixMultiplicity(dm, ceed_data, Q_loc, Q, 0.0); CHKERRQ(ierr); 183 184 // --------------------------------------------------------------------------- 185 // Set up lumped mass matrix 186 // --------------------------------------------------------------------------- 187 // -- Set up global mass vector 188 ierr = VecDuplicate(Q, &user->M); CHKERRQ(ierr); 189 190 // -- Compute lumped mass matrix 191 ierr = ComputeLumpedMassMatrix(ceed, dm, ceed_data, user->M); CHKERRQ(ierr); 192 193 // --------------------------------------------------------------------------- 194 // Record boundary values from initial condition 195 // --------------------------------------------------------------------------- 196 // -- This overrides DMPlexInsertBoundaryValues(). 197 // We use this for the main simulation DM because the reference 198 // DMPlexInsertBoundaryValues() is very slow. If we disable this, we should 199 // still get the same results due to the problem->bc function, but with 200 // potentially much slower execution. 201 if (1) {ierr = SetBCsFromICs_NS(dm, Q, Q_loc); CHKERRQ(ierr);} 202 203 // --------------------------------------------------------------------------- 204 // Create output directory 205 // --------------------------------------------------------------------------- 206 PetscMPIInt rank; 207 MPI_Comm_rank(comm, &rank); 208 if (!rank) {ierr = PetscMkdir(app_ctx->output_dir); CHKERRQ(ierr);} 209 210 // --------------------------------------------------------------------------- 211 // Gather initial Q values in case of continuation of simulation 212 // --------------------------------------------------------------------------- 213 // -- Set up initial values from binary file 214 if (app_ctx->cont_steps) { 215 ierr = SetupICsFromBinary(comm, app_ctx, Q); CHKERRQ(ierr); 216 } 217 218 // --------------------------------------------------------------------------- 219 // Print problem summary 220 // --------------------------------------------------------------------------- 221 if (!app_ctx->test_mode) { 222 223 // Header and rank 224 char host_name[PETSC_MAX_PATH_LEN]; 225 int comm_size; 226 ierr = PetscGetHostName(host_name, sizeof host_name); CHKERRQ(ierr); 227 ierr = MPI_Comm_size(comm, &comm_size); CHKERRQ(ierr); 228 ierr = PetscPrintf(comm, 229 "\n-- Navier-Stokes solver - libCEED + PETSc --\n" 230 " MPI:\n" 231 " Host Name : %s\n" 232 " Total ranks : %d\n", 233 host_name, comm_size); CHKERRQ(ierr); 234 235 // Problem specific info 236 ierr = problem->print_info(phys_ctx, setup_ctx, app_ctx); CHKERRQ(ierr); 237 238 // libCEED 239 const char *used_resource; 240 CeedGetResource(ceed, &used_resource); 241 ierr = PetscPrintf(comm, 242 " libCEED:\n" 243 " libCEED Backend : %s\n" 244 " libCEED Backend MemType : %s\n", 245 used_resource, CeedMemTypes[mem_type_backend]); CHKERRQ(ierr); 246 // PETSc 247 char box_faces_str[PETSC_MAX_PATH_LEN] = "3,3,3"; 248 if (problem->dim == 2) box_faces_str[3] = '\0'; 249 ierr = PetscOptionsGetString(NULL, NULL, "-dm_plex_box_faces", box_faces_str, 250 sizeof(box_faces_str), NULL); CHKERRQ(ierr); 251 ierr = PetscPrintf(comm, 252 " PETSc:\n" 253 " Box Faces : %s\n" 254 " Time Stepping Scheme : %s\n", 255 box_faces_str, phys_ctx->implicit ? "implicit" : "explicit"); CHKERRQ(ierr); 256 // Mesh 257 const PetscInt num_comp_q = 5; 258 CeedInt glob_dofs, owned_dofs; 259 PetscInt glob_nodes, owned_nodes; 260 const CeedInt num_P = app_ctx->degree + 1, 261 num_Q = num_P + app_ctx->q_extra; 262 // -- Get global size 263 ierr = VecGetSize(Q, &glob_dofs); CHKERRQ(ierr); 264 ierr = VecGetLocalSize(Q, &owned_dofs); CHKERRQ(ierr); 265 glob_nodes = glob_dofs/num_comp_q; 266 // -- Get local size 267 ierr = VecGetSize(Q_loc, &owned_nodes); CHKERRQ(ierr); 268 owned_nodes /= num_comp_q; 269 ierr = PetscPrintf(comm, 270 " Mesh:\n" 271 " Number of 1D Basis Nodes (P) : %d\n" 272 " Number of 1D Quadrature Points (Q) : %d\n" 273 " Global DoFs : %D\n" 274 " Owned DoFs : %D\n" 275 " DoFs per node : %D\n" 276 " Global nodes : %D\n" 277 " Owned nodes : %D\n", 278 num_P, num_Q, glob_dofs, owned_dofs, num_comp_q, 279 glob_nodes, owned_nodes); CHKERRQ(ierr); 280 } 281 // -- Restore Q_loc 282 ierr = DMRestoreLocalVector(dm, &Q_loc); CHKERRQ(ierr); 283 284 // --------------------------------------------------------------------------- 285 // TS: Create, setup, and solve 286 // --------------------------------------------------------------------------- 287 TS ts; 288 PetscScalar final_time; 289 ierr = TSSolve_NS(dm, user, app_ctx, phys_ctx, &Q, &final_time, &ts); 290 CHKERRQ(ierr); 291 292 // --------------------------------------------------------------------------- 293 // Post-processing 294 // --------------------------------------------------------------------------- 295 ierr = PostProcess_NS(ts, ceed_data, dm, problem, app_ctx, Q, final_time); 296 CHKERRQ(ierr); 297 298 // --------------------------------------------------------------------------- 299 // Destroy libCEED objects 300 // --------------------------------------------------------------------------- 301 // -- Vectors 302 CeedVectorDestroy(&ceed_data->x_coord); 303 CeedVectorDestroy(&ceed_data->q_data); 304 CeedVectorDestroy(&user->q_ceed); 305 CeedVectorDestroy(&user->q_dot_ceed); 306 CeedVectorDestroy(&user->g_ceed); 307 308 // -- Contexts 309 CeedQFunctionContextDestroy(&ceed_data->setup_context); 310 CeedQFunctionContextDestroy(&ceed_data->dc_context); 311 CeedQFunctionContextDestroy(&ceed_data->advection_context); 312 CeedQFunctionContextDestroy(&ceed_data->euler_context); 313 314 // -- QFunctions 315 CeedQFunctionDestroy(&ceed_data->qf_setup_vol); 316 CeedQFunctionDestroy(&ceed_data->qf_ics); 317 CeedQFunctionDestroy(&ceed_data->qf_rhs_vol); 318 CeedQFunctionDestroy(&ceed_data->qf_ifunction_vol); 319 CeedQFunctionDestroy(&ceed_data->qf_setup_sur); 320 CeedQFunctionDestroy(&ceed_data->qf_apply_sur); 321 322 // -- Bases 323 CeedBasisDestroy(&ceed_data->basis_q); 324 CeedBasisDestroy(&ceed_data->basis_x); 325 CeedBasisDestroy(&ceed_data->basis_xc); 326 CeedBasisDestroy(&ceed_data->basis_q_sur); 327 CeedBasisDestroy(&ceed_data->basis_x_sur); 328 CeedBasisDestroy(&ceed_data->basis_xc_sur); 329 330 // -- Restrictions 331 CeedElemRestrictionDestroy(&ceed_data->elem_restr_q); 332 CeedElemRestrictionDestroy(&ceed_data->elem_restr_x); 333 CeedElemRestrictionDestroy(&ceed_data->elem_restr_qd_i); 334 335 // -- Operators 336 CeedOperatorDestroy(&ceed_data->op_setup_vol); 337 CeedOperatorDestroy(&ceed_data->op_ics); 338 CeedOperatorDestroy(&user->op_rhs_vol); 339 CeedOperatorDestroy(&user->op_ifunction_vol); 340 CeedOperatorDestroy(&user->op_rhs); 341 CeedOperatorDestroy(&user->op_ifunction); 342 343 // -- Ceed 344 CeedDestroy(&ceed); 345 346 // --------------------------------------------------------------------------- 347 // Clean up PETSc 348 // --------------------------------------------------------------------------- 349 // -- Vectors 350 ierr = VecDestroy(&Q); CHKERRQ(ierr); 351 ierr = VecDestroy(&user->M); CHKERRQ(ierr); 352 353 // -- Matrices 354 ierr = MatDestroy(&user->interp_viz); CHKERRQ(ierr); 355 356 // -- DM 357 ierr = DMDestroy(&dm); CHKERRQ(ierr); 358 ierr = DMDestroy(&user->dm_viz); CHKERRQ(ierr); 359 360 // -- TS 361 ierr = TSDestroy(&ts); CHKERRQ(ierr); 362 363 // -- Function list 364 ierr = PetscFunctionListDestroy(&app_ctx->problems); CHKERRQ(ierr); 365 366 // -- Structs 367 ierr = PetscFree(units); CHKERRQ(ierr); 368 ierr = PetscFree(user); CHKERRQ(ierr); 369 ierr = PetscFree(problem); CHKERRQ(ierr); 370 ierr = PetscFree(bc); CHKERRQ(ierr); 371 ierr = PetscFree(setup_ctx); CHKERRQ(ierr); 372 ierr = PetscFree(phys_ctx->dc_ctx); CHKERRQ(ierr); 373 ierr = PetscFree(phys_ctx->euler_ctx); CHKERRQ(ierr); 374 ierr = PetscFree(phys_ctx->advection_ctx); CHKERRQ(ierr); 375 ierr = PetscFree(phys_ctx); CHKERRQ(ierr); 376 ierr = PetscFree(app_ctx); CHKERRQ(ierr); 377 ierr = PetscFree(ceed_data); CHKERRQ(ierr); 378 379 return PetscFinalize(); 380 } 381