xref: /libCEED/examples/fluids/navierstokes.c (revision 50c301a53d2cec48a2aa861bf6f38393f4831c2f)
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 -units_second 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 -units_second 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     // Header and rank
223     char host_name[PETSC_MAX_PATH_LEN];
224     int  comm_size;
225     ierr = PetscGetHostName(host_name, sizeof host_name); CHKERRQ(ierr);
226     ierr = MPI_Comm_size(comm, &comm_size); CHKERRQ(ierr);
227     ierr = PetscPrintf(comm,
228                        "\n-- Navier-Stokes solver - libCEED + PETSc --\n"
229                        "  MPI:\n"
230                        "    Host Name                          : %s\n"
231                        "    Total ranks                        : %d\n",
232                        host_name, comm_size); CHKERRQ(ierr);
233 
234     // Problem specific info
235     ierr = problem->print_info(phys_ctx, setup_ctx, app_ctx); CHKERRQ(ierr);
236 
237     // libCEED
238     const char *used_resource;
239     CeedGetResource(ceed, &used_resource);
240     ierr = PetscPrintf(comm,
241                        "  libCEED:\n"
242                        "    libCEED Backend                    : %s\n"
243                        "    libCEED Backend MemType            : %s\n",
244                        used_resource, CeedMemTypes[mem_type_backend]); CHKERRQ(ierr);
245     // PETSc
246     char box_faces_str[PETSC_MAX_PATH_LEN] = "3,3,3";
247     if (problem->dim == 2) box_faces_str[3] = '\0';
248     ierr = PetscOptionsGetString(NULL, NULL, "-dm_plex_box_faces", box_faces_str,
249                                  sizeof(box_faces_str), NULL); CHKERRQ(ierr);
250     ierr = PetscPrintf(comm,
251                        "  PETSc:\n"
252                        "    Box Faces                          : %s\n"
253                        "    Time Stepping Scheme               : %s\n",
254                        box_faces_str, phys_ctx->implicit ? "implicit" : "explicit"); CHKERRQ(ierr);
255     // Mesh
256     const PetscInt num_comp_q = 5;
257     CeedInt        glob_dofs, owned_dofs;
258     PetscInt       glob_nodes, owned_nodes;
259     const CeedInt  num_P = app_ctx->degree + 1,
260                    num_Q = num_P + app_ctx->q_extra;
261     // -- Get global size
262     ierr = VecGetSize(Q, &glob_dofs); CHKERRQ(ierr);
263     ierr = VecGetLocalSize(Q, &owned_dofs); CHKERRQ(ierr);
264     glob_nodes = glob_dofs/num_comp_q;
265     // -- Get local size
266     ierr = VecGetSize(Q_loc, &owned_nodes); CHKERRQ(ierr);
267     owned_nodes /= num_comp_q;
268     ierr = PetscPrintf(comm,
269                        "  Mesh:\n"
270                        "    Number of 1D Basis Nodes (P)       : %d\n"
271                        "    Number of 1D Quadrature Points (Q) : %d\n"
272                        "    Global DoFs                        : %D\n"
273                        "    Owned DoFs                         : %D\n"
274                        "    DoFs per node                      : %D\n"
275                        "    Global nodes                       : %D\n"
276                        "    Owned nodes                        : %D\n",
277                        num_P, num_Q, glob_dofs, owned_dofs, num_comp_q,
278                        glob_nodes, owned_nodes); CHKERRQ(ierr);
279   }
280   // -- Restore Q_loc
281   ierr = DMRestoreLocalVector(dm, &Q_loc); CHKERRQ(ierr);
282 
283   // ---------------------------------------------------------------------------
284   // TS: Create, setup, and solve
285   // ---------------------------------------------------------------------------
286   TS ts;
287   PetscScalar final_time;
288   ierr = TSSolve_NS(dm, user, app_ctx, phys_ctx, &Q, &final_time, &ts);
289   CHKERRQ(ierr);
290 
291   // ---------------------------------------------------------------------------
292   // Post-processing
293   // ---------------------------------------------------------------------------
294   ierr = PostProcess_NS(ts, ceed_data, dm, problem, app_ctx, Q, final_time);
295   CHKERRQ(ierr);
296 
297   // ---------------------------------------------------------------------------
298   // Destroy libCEED objects
299   // ---------------------------------------------------------------------------
300   // -- Vectors
301   CeedVectorDestroy(&ceed_data->x_coord);
302   CeedVectorDestroy(&ceed_data->q_data);
303   CeedVectorDestroy(&user->q_ceed);
304   CeedVectorDestroy(&user->q_dot_ceed);
305   CeedVectorDestroy(&user->g_ceed);
306 
307   // -- Contexts
308   CeedQFunctionContextDestroy(&ceed_data->setup_context);
309   CeedQFunctionContextDestroy(&ceed_data->dc_context);
310   CeedQFunctionContextDestroy(&ceed_data->advection_context);
311   CeedQFunctionContextDestroy(&ceed_data->euler_context);
312 
313   // -- QFunctions
314   CeedQFunctionDestroy(&ceed_data->qf_setup_vol);
315   CeedQFunctionDestroy(&ceed_data->qf_ics);
316   CeedQFunctionDestroy(&ceed_data->qf_rhs_vol);
317   CeedQFunctionDestroy(&ceed_data->qf_ifunction_vol);
318   CeedQFunctionDestroy(&ceed_data->qf_setup_sur);
319   CeedQFunctionDestroy(&ceed_data->qf_apply_sur);
320 
321   // -- Bases
322   CeedBasisDestroy(&ceed_data->basis_q);
323   CeedBasisDestroy(&ceed_data->basis_x);
324   CeedBasisDestroy(&ceed_data->basis_xc);
325   CeedBasisDestroy(&ceed_data->basis_q_sur);
326   CeedBasisDestroy(&ceed_data->basis_x_sur);
327 
328   // -- Restrictions
329   CeedElemRestrictionDestroy(&ceed_data->elem_restr_q);
330   CeedElemRestrictionDestroy(&ceed_data->elem_restr_x);
331   CeedElemRestrictionDestroy(&ceed_data->elem_restr_qd_i);
332 
333   // -- Operators
334   CeedOperatorDestroy(&ceed_data->op_setup_vol);
335   CeedOperatorDestroy(&ceed_data->op_ics);
336   CeedOperatorDestroy(&user->op_rhs_vol);
337   CeedOperatorDestroy(&user->op_ifunction_vol);
338   CeedOperatorDestroy(&user->op_rhs);
339   CeedOperatorDestroy(&user->op_ifunction);
340 
341   // -- Ceed
342   CeedDestroy(&ceed);
343 
344   // ---------------------------------------------------------------------------
345   // Clean up PETSc
346   // ---------------------------------------------------------------------------
347   // -- Vectors
348   ierr = VecDestroy(&Q); CHKERRQ(ierr);
349   ierr = VecDestroy(&user->M); CHKERRQ(ierr);
350 
351   // -- Matrices
352   ierr = MatDestroy(&user->interp_viz); CHKERRQ(ierr);
353 
354   // -- DM
355   ierr = DMDestroy(&dm); CHKERRQ(ierr);
356   ierr = DMDestroy(&user->dm_viz); CHKERRQ(ierr);
357 
358   // -- TS
359   ierr = TSDestroy(&ts); CHKERRQ(ierr);
360 
361   // -- Function list
362   ierr = PetscFunctionListDestroy(&app_ctx->problems); CHKERRQ(ierr);
363 
364   // -- Structs
365   ierr = PetscFree(units); CHKERRQ(ierr);
366   ierr = PetscFree(user); CHKERRQ(ierr);
367   ierr = PetscFree(problem); CHKERRQ(ierr);
368   ierr = PetscFree(bc); CHKERRQ(ierr);
369   ierr = PetscFree(setup_ctx); CHKERRQ(ierr);
370   ierr = PetscFree(phys_ctx->dc_ctx); CHKERRQ(ierr);
371   ierr = PetscFree(phys_ctx->euler_ctx); CHKERRQ(ierr);
372   ierr = PetscFree(phys_ctx->advection_ctx); CHKERRQ(ierr);
373   ierr = PetscFree(phys_ctx); CHKERRQ(ierr);
374   ierr = PetscFree(app_ctx); CHKERRQ(ierr);
375   ierr = PetscFree(ceed_data); CHKERRQ(ierr);
376 
377   return PetscFinalize();
378 }
379