Skip to content

Commit 6929379

Browse files
authored
fluids: Make simulation continuation more flexible (#1086)
* fluids: Add options to continue from specific file * fluids: Option to write out bins with step number * doc(fluids): Document output/continuation flags
1 parent 98e6643 commit 6929379

File tree

5 files changed

+67
-22
lines changed

5 files changed

+67
-22
lines changed

examples/fluids/README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,26 @@ The following options are common among all problem types:
7272
- Frequency of output, in number of steps. `0` has no output, `-1` outputs final state only
7373
- `10`
7474

75-
* - `-continue`
76-
- Continue from previous solution
77-
- `0`
78-
7975
* - `-output_dir`
8076
- Output directory
8177
- `.`
8278

79+
* - `-output_add_stepnum2bin`
80+
- Whether to add step numbers to output binary files
81+
- `false`
82+
83+
* - `-continue`
84+
- Continue from previous solution (input is step number of previous solution)
85+
- `0`
86+
87+
* - `-continue_filename`
88+
- Path to solution binary file from which to continue from
89+
- `[output_dir]/ns-solution.bin`
90+
91+
* - `-continue_time_filename`
92+
- Path to time stamp binary file from which to continue from
93+
- `[output_dir]/ns-time.bin`
94+
8395
* - `-bc_wall`
8496
- Use wall boundary conditions on this list of faces
8597
-

examples/fluids/navierstokes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ struct AppCtx_private {
118118
PetscInt output_freq;
119119
PetscInt viz_refine;
120120
PetscInt cont_steps;
121+
char cont_file[PETSC_MAX_PATH_LEN];
122+
char cont_time_file[PETSC_MAX_PATH_LEN];
121123
char output_dir[PETSC_MAX_PATH_LEN];
124+
PetscBool add_stepnum2bin;
122125
// Problem type arguments
123126
PetscFunctionList problems;
124127
char problem_name[PETSC_MAX_PATH_LEN];

examples/fluids/src/cloptions.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ PetscErrorCode RegisterProblems_NS(AppCtx app_ctx) {
4848
PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx,
4949
SimpleBC bc) {
5050

51-
PetscBool ceed_flag = PETSC_FALSE;
51+
PetscBool ceed_flag = PETSC_FALSE;
5252
PetscBool problem_flag = PETSC_FALSE;
53+
PetscBool option_set = PETSC_FALSE;
5354
PetscErrorCode ierr;
5455
PetscFunctionBeginUser;
5556

@@ -88,10 +89,37 @@ PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx,
8889
"Frequency of output, in number of steps",
8990
NULL, app_ctx->output_freq, &app_ctx->output_freq, NULL); CHKERRQ(ierr);
9091

92+
PetscCall(PetscOptionsBool("-output_add_stepnum2bin",
93+
"Add step number to the binary outputs",
94+
NULL, app_ctx->add_stepnum2bin, &app_ctx->add_stepnum2bin, NULL));
95+
96+
PetscCall(PetscStrncpy(app_ctx->output_dir, ".", 2));
97+
PetscCall(PetscOptionsString("-output_dir", "Output directory",
98+
NULL, app_ctx->output_dir, app_ctx->output_dir,
99+
sizeof(app_ctx->output_dir), NULL));
100+
91101
app_ctx->cont_steps = 0;
92102
ierr = PetscOptionsInt("-continue", "Continue from previous solution",
93103
NULL, app_ctx->cont_steps, &app_ctx->cont_steps, NULL); CHKERRQ(ierr);
94104

105+
PetscCall(PetscStrcpy(app_ctx->cont_file, "[output_dir]/ns-solution.bin"));
106+
PetscCall(PetscOptionsString("-continue_filename",
107+
"Filename to get initial condition from",
108+
NULL, app_ctx->cont_file, app_ctx->cont_file,
109+
sizeof(app_ctx->cont_file), &option_set));
110+
if(!option_set) PetscCall(PetscSNPrintf(app_ctx->cont_file,
111+
sizeof app_ctx->cont_file, "%s/ns-solution.bin",
112+
app_ctx->output_dir));
113+
114+
PetscCall(PetscStrcpy(app_ctx->cont_time_file, "[output_dir]/ns-time.bin"));
115+
PetscCall(PetscOptionsString("-continue_time_filename",
116+
"Filename to get initial condition time from",
117+
NULL, app_ctx->cont_time_file, app_ctx->cont_time_file,
118+
sizeof(app_ctx->cont_time_file), &option_set));
119+
if(!option_set) PetscCall(PetscSNPrintf(app_ctx->cont_time_file,
120+
sizeof app_ctx->cont_time_file, "%s/ns-time.bin",
121+
app_ctx->output_dir));
122+
95123
app_ctx->degree = 1;
96124
ierr = PetscOptionsInt("-degree", "Polynomial degree of finite elements",
97125
NULL, app_ctx->degree, &app_ctx->degree, NULL); CHKERRQ(ierr);
@@ -113,11 +141,6 @@ PetscErrorCode ProcessCommandLineOptions(MPI_Comm comm, AppCtx app_ctx,
113141
"Assemble only point-block diagonal for Pmat", NULL, app_ctx->pmat_pbdiagonal,
114142
&app_ctx->pmat_pbdiagonal, NULL));
115143

116-
ierr = PetscStrncpy(app_ctx->output_dir, ".", 2); CHKERRQ(ierr);
117-
ierr = PetscOptionsString("-output_dir", "Output directory",
118-
NULL, app_ctx->output_dir, app_ctx->output_dir,
119-
sizeof(app_ctx->output_dir), NULL); CHKERRQ(ierr);
120-
121144
// Provide default ceed resource if not specified
122145
if (!ceed_flag) {
123146
const char *ceed_resource = "/cpu/self";

examples/fluids/src/misc.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,11 @@ PetscErrorCode PostProcess_NS(TS ts, CeedData ceed_data, DM dm,
226226
PetscErrorCode SetupICsFromBinary(MPI_Comm comm, AppCtx app_ctx, Vec Q) {
227227

228228
PetscViewer viewer;
229-
char file_path[PETSC_MAX_PATH_LEN];
230229
PetscErrorCode ierr;
231230
PetscFunctionBegin;
232231

233232
// Read input
234-
ierr = PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin",
235-
app_ctx->output_dir); CHKERRQ(ierr);
236-
ierr = PetscViewerBinaryOpen(comm, file_path, FILE_MODE_READ, &viewer);
233+
ierr = PetscViewerBinaryOpen(comm, app_ctx->cont_file, FILE_MODE_READ, &viewer);
237234
CHKERRQ(ierr);
238235

239236
// Load Q from existent solution

examples/fluids/src/setupts.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,14 @@ PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no,
428428
PetscCall(DMRestoreLocalVector(user->dm, &Q_loc));
429429

430430
// Save data in a binary file for continuation of simulations
431-
PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin",
432-
user->app_ctx->output_dir));
431+
if (user->app_ctx->add_stepnum2bin) {
432+
PetscCall(PetscSNPrintf(file_path, sizeof file_path,
433+
"%s/ns-solution-%" PetscInt_FMT ".bin",
434+
user->app_ctx->output_dir, step_no + user->app_ctx->cont_steps));
435+
} else {
436+
PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-solution.bin",
437+
user->app_ctx->output_dir));
438+
}
433439
PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE,
434440
&viewer));
435441

@@ -439,8 +445,14 @@ PetscErrorCode WriteOutput(User user, Vec Q, PetscInt step_no,
439445
// Save time stamp
440446
// Dimensionalize time back
441447
time /= user->units->second;
442-
PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-time.bin",
443-
user->app_ctx->output_dir));
448+
if (user->app_ctx->add_stepnum2bin) {
449+
PetscCall(PetscSNPrintf(file_path, sizeof file_path,
450+
"%s/ns-time-%" PetscInt_FMT ".bin",
451+
user->app_ctx->output_dir, step_no + user->app_ctx->cont_steps));
452+
} else {
453+
PetscCall(PetscSNPrintf(file_path, sizeof file_path, "%s/ns-time.bin",
454+
user->app_ctx->output_dir));
455+
}
444456
PetscCall(PetscViewerBinaryOpen(user->comm, file_path, FILE_MODE_WRITE,
445457
&viewer));
446458

@@ -526,10 +538,8 @@ PetscErrorCode TSSolve_NS(DM dm, User user, AppCtx app_ctx, Physics phys,
526538
PetscReal time;
527539
PetscInt count;
528540
PetscViewer viewer;
529-
char file_path[PETSC_MAX_PATH_LEN];
530-
ierr = PetscSNPrintf(file_path, sizeof file_path, "%s/ns-time.bin",
531-
app_ctx->output_dir); CHKERRQ(ierr);
532-
ierr = PetscViewerBinaryOpen(comm, file_path, FILE_MODE_READ, &viewer);
541+
ierr = PetscViewerBinaryOpen(comm, app_ctx->cont_time_file, FILE_MODE_READ,
542+
&viewer);
533543
CHKERRQ(ierr);
534544
ierr = PetscViewerBinaryRead(viewer, &time, 1, &count, PETSC_REAL);
535545
CHKERRQ(ierr);

0 commit comments

Comments
 (0)