HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
1D Linear Advection - Sine Wave

See 1D Linear Advection - Sine Wave to familiarize yourself with this case. This example uses a DMD object that has already been trained (see 1D Linear Advection - Sine Wave).

Location: hypar/Examples/1D/LinearAdvection/SineWave_libROM_DMD_Predict (This directory contains all the input files needed to run this case.)

Governing equations: 1D Linear Advection Equation (linearadr.h)

Reduced Order Modeling: This example predicts the solution from a trained DMD object. The code does not solve the PDE by discretizing in space and integrating in time.

References:

  • Ghosh, D., Baeder, J. D., "Compact Reconstruction Schemes with Weighted ENO Limiting for Hyperbolic Conservation Laws", SIAM Journal on Scientific Computing, 34 (3), 2012, A1678–A1706

Domain: \(0 \le x < 1\), "periodic" (_PERIODIC_) boundary conditions

Initial solution: \(u\left(x,0\right) = \sin\left(2\pi x\right)\)

Reduced Order Modeling:

Note:

In this mode, HyPar will run just like an usual PDE simulation, except that it will swap out the numerical spatial discretization and time integration with the ROM-based prediction. The input files and output files will be the same as a regular simulation with the following comments:

  • Numerical method inputs are ignored (eg. those that specify spatial discretization and time integration methods).
  • In solver.inp, the values for dt, n_iter, and file_op_iter is used only to compute the simulation times at which to compute and write the solution. The time step size, dt, need not respect any CFL criterion.
  • HyPar::ConservationCheck is set to "no" since it is not possible to compute conservation loss for a general domain (because boundary fluxes are not being computed).

Input files required:

librom.inp

begin
mode predict
dmd_dirname DMD
end

DMD Object(s) :
The trained DMD object(s) must be located in the directory specified in librom.inp as dmd_dirname (DMDROMObject::m_dirname). For this example, they were generated using 1D Linear Advection - Sine Wave.

solver.inp

begin
ndims 1
nvars 1
size 80
iproc 4
ghost 3
n_iter 30
dt 0.1
file_op_iter 1
ip_file_type ascii
op_file_format binary
op_overwrite no
model linear-advection-diffusion-reaction
end

boundary.inp

2
periodic 0 1 0 0
periodic 0 -1 0 0

physics.inp

begin
advection 1.0
end

To generate initial.inp, compile and run the following code in the run directory. Note: if the final time is an integer multiple of the time period, the file initial.inp can also be used as the exact solution exact.inp (i.e. create a sym link called exact.inp pointing to initial.inp, or just copy initial.inp to exact.inp).

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main(){
double pi = 4.0*atan(1.0);
int NI,ndims;
char ip_file_type[50];
strcpy(ip_file_type,"ascii");
FILE *in;
printf("Reading file \"solver.inp\"...\n");
in = fopen("solver.inp","r");
if (!in) printf("Error: Input file \"solver.inp\" not found. Default values will be used.\n");
else {
char word[500];
fscanf(in,"%s",word);
if (!strcmp(word, "begin")){
while (strcmp(word, "end")){
fscanf(in,"%s",word);
if (!strcmp(word, "ndims")) fscanf(in,"%d",&ndims);
else if (!strcmp(word, "size")) fscanf(in,"%d",&NI);
else if (!strcmp(word, "ip_file_type")) fscanf(in,"%s",ip_file_type);
}
} else printf("Error: Illegal format in solver.inp. Crash and burn!\n");
}
fclose(in);
if (ndims != 1) {
printf("ndims is not 1 in solver.inp. this code is to generate 1D initial conditions\n");
return(0);
}
printf("Grid:\t\t\t%d\n",NI);
int i;
double dx = 1.0 / ((double)NI);
double *x, *u;
x = (double*) calloc (NI, sizeof(double));
u = (double*) calloc (NI, sizeof(double));
for (i = 0; i < NI; i++){
x[i] = i*dx;
u[i] = sin(2*pi*x[i]);
}
FILE *out;
if (!strcmp(ip_file_type,"ascii")) {
printf("Writing ASCII initial solution file initial.inp\n");
out = fopen("initial.inp","w");
for (i = 0; i < NI; i++) fprintf(out,"%lf ",x[i]);
fprintf(out,"\n");
for (i = 0; i < NI; i++) fprintf(out,"%lf ",u[i]);
fprintf(out,"\n");
fclose(out);
} else if ((!strcmp(ip_file_type,"binary")) || (!strcmp(ip_file_type,"bin"))) {
printf("Writing binary initial solution file initial.inp\n");
out = fopen("initial.inp","wb");
fwrite(x,sizeof(double),NI,out);
fwrite(u,sizeof(double),NI,out);
fclose(out);
}
free(x);
free(u);
return(0);
}

Output:

After running the code, there should be the following output files:

  • 31 output files op_00000.bin, op_00001.bin, ... op_00030.bin; these are the solutions as predicted by the ROM .

The first of each of these file sets is the solution at \(t=0\) and the final one is the solution at \(t=3\). Since HyPar::op_overwrite is set to no in solver.inp, a separate file is written for solutions at each output time. All the files are binary (HyPar::op_file_format is set to binary in solver.inp).

The provided Python script (Examples/Python/plotSolution_1DBinary.py) can be used to generate plots from these binary files. Alternatively, HyPar::op_file_format can be set to text, and GNUPlot or something similar can be used to plot the resulting text files.

The animation shows the evolution of the solution.

Solution_1DLinearAdvSinelibROM_DMD_Predict.gif

Expected screen output:

HyPar - Parallel (MPI) version with 4 processes
Compiled with PETSc time integration.
Allocated simulation object(s).
Reading solver inputs from file "solver.inp".
No. of dimensions : 1
No. of variables : 1
Domain size : 80
Processes along each dimension : 4
Exact solution domain size : 80
No. of ghosts pts : 3
No. of iter. : 30
Restart iteration : 0
Time integration scheme : euler
Spatial discretization scheme (hyperbolic) : 1
Split hyperbolic flux term? : no
Interpolation type for hyperbolic term : characteristic
Spatial discretization type (parabolic ) : nonconservative-1stage
Spatial discretization scheme (parabolic ) : 2
Time Step : 1.000000E-01
Check for conservation : no
Screen output iterations : 1
File output iterations : 1
Initial solution file type : ascii
Initial solution read mode : serial
Solution file write mode : serial
Solution file format : binary
Overwrite solution file : no
Physical model : linear-advection-diffusion-reaction
Partitioning domain and allocating data arrays.
Reading array from ASCII file initial.inp (Serial mode).
Volume integral of the initial solution:
0: -5.5511151231257827E-17
Reading boundary conditions from boundary.inp.
Boundary periodic: Along dimension 0 and face +1
Boundary periodic: Along dimension 0 and face -1
2 boundary condition(s) read.
Initializing solvers.
Initializing physics. Model = "linear-advection-diffusion-reaction"
Reading physical model inputs from file "physics.inp".
Setting up libROM interface.
libROM inputs and parameters:
reduced model dimensionality: -1429413552
sampling frequency: 10922
mode: predict
type: DMD
save to file: true
libROM DMD inputs:
number of samples per window: 2147483647
directory name for DMD onjects: DMD
libROMInterface::loadROM() - loading ROM objects.
Loading DMD object (DMD/dmdobj_0000), time window=[0.00e+00,-1.00e+00].
libROM: Predicted solution at time 0.0000e+00 using ROM, wallclock time: 0.000088.
Writing solution file op_00000.bin.
libROM: Predicted solution at time 1.0000e-01 using ROM, wallclock time: 0.067550.
Writing solution file op_00001.bin.
libROM: Predicted solution at time 2.0000e-01 using ROM, wallclock time: 0.074819.
Writing solution file op_00002.bin.
libROM: Predicted solution at time 3.0000e-01 using ROM, wallclock time: 0.228214.
Writing solution file op_00003.bin.
libROM: Predicted solution at time 4.0000e-01 using ROM, wallclock time: 0.117337.
Writing solution file op_00004.bin.
libROM: Predicted solution at time 5.0000e-01 using ROM, wallclock time: 0.139047.
Writing solution file op_00005.bin.
libROM: Predicted solution at time 6.0000e-01 using ROM, wallclock time: 0.102845.
Writing solution file op_00006.bin.
libROM: Predicted solution at time 7.0000e-01 using ROM, wallclock time: 0.071836.
Writing solution file op_00007.bin.
libROM: Predicted solution at time 8.0000e-01 using ROM, wallclock time: 0.112971.
Writing solution file op_00008.bin.
libROM: Predicted solution at time 9.0000e-01 using ROM, wallclock time: 0.043692.
Writing solution file op_00009.bin.
libROM: Predicted solution at time 1.0000e+00 using ROM, wallclock time: 0.093272.
Writing solution file op_00010.bin.
libROM: Predicted solution at time 1.1000e+00 using ROM, wallclock time: 0.105779.
Writing solution file op_00011.bin.
libROM: Predicted solution at time 1.2000e+00 using ROM, wallclock time: 0.073120.
Writing solution file op_00012.bin.
libROM: Predicted solution at time 1.3000e+00 using ROM, wallclock time: 0.055043.
Writing solution file op_00013.bin.
libROM: Predicted solution at time 1.4000e+00 using ROM, wallclock time: 0.096621.
Writing solution file op_00014.bin.
libROM: Predicted solution at time 1.5000e+00 using ROM, wallclock time: 0.087529.
Writing solution file op_00015.bin.
libROM: Predicted solution at time 1.6000e+00 using ROM, wallclock time: 0.085602.
Writing solution file op_00016.bin.
libROM: Predicted solution at time 1.7000e+00 using ROM, wallclock time: 0.084388.
Writing solution file op_00017.bin.
libROM: Predicted solution at time 1.8000e+00 using ROM, wallclock time: 0.066612.
Writing solution file op_00018.bin.
libROM: Predicted solution at time 1.9000e+00 using ROM, wallclock time: 0.117045.
Writing solution file op_00019.bin.
libROM: Predicted solution at time 2.0000e+00 using ROM, wallclock time: 0.053710.
Writing solution file op_00020.bin.
libROM: Predicted solution at time 2.1000e+00 using ROM, wallclock time: 0.075127.
Writing solution file op_00021.bin.
libROM: Predicted solution at time 2.2000e+00 using ROM, wallclock time: 0.074802.
Writing solution file op_00022.bin.
libROM: Predicted solution at time 2.3000e+00 using ROM, wallclock time: 0.070621.
Writing solution file op_00023.bin.
libROM: Predicted solution at time 2.4000e+00 using ROM, wallclock time: 0.081049.
Writing solution file op_00024.bin.
libROM: Predicted solution at time 2.5000e+00 using ROM, wallclock time: 0.100844.
Writing solution file op_00025.bin.
libROM: Predicted solution at time 2.6000e+00 using ROM, wallclock time: 0.099568.
Writing solution file op_00026.bin.
libROM: Predicted solution at time 2.7000e+00 using ROM, wallclock time: 0.052327.
Writing solution file op_00027.bin.
libROM: Predicted solution at time 2.8000e+00 using ROM, wallclock time: 0.154874.
Writing solution file op_00028.bin.
libROM: Predicted solution at time 2.9000e+00 using ROM, wallclock time: 0.138962.
Writing solution file op_00029.bin.
libROM: Predicted solution at time 3.0000e+00 using ROM, wallclock time: 0.141040.
Writing solution file op_00030.bin.
Reading array from ASCII file exact.inp (Serial mode).
libROM: total prediction/query wallclock time: 2.866334 (seconds).
Computed errors for domain 0:
L1 Error : 2.4380601432333813E-05
L2 Error : 2.4456827071613315E-05
Linfinity Error : 2.4416947642880160E-05
Solver runtime (in seconds): 3.7908719999999998E+00
Total runtime (in seconds): 3.9607500000000000E+00
Deallocating arrays.
Finished.