Location: hypar/Examples/2D/LinearDiffusion/SineWave (This directory contains all the input files needed to run this case. If there is a Run.m, run it in MATLAB to quickly set up, run, and visualize the example).
Governing equations: 2D Linear Diffusion Equation (linearadr.h)
Domain: \(0 \le x,y < 1\), "periodic" (_PERIODIC_) boundary conditions on all boundaries.
Initial solution: \(u\left(x,y,0\right) = u_0\left(x,y\right)= \sin\left(2\pi x\right)\sin\left(2\pi y\right)\)
Exact solution: \(u\left(x,y,t\right) = \exp\left[-\pi^2 \left(4\nu_x + 4\nu_y\right) t\right] u0\left(x,y\right)\).
Numerical Method:
Input files required:
solver.inp
begin
ndims 2
nvars 1
size 80 80
iproc 2 2
ghost 3
n_iter 200
time_scheme rk
time_scheme_type ssprk3
par_space_type conservative-1stage
par_space_scheme 2
dt 0.05
screen_op_iter 5
file_op_iter 20
op_file_format tecplot2d
op_overwrite no
model linear-advection-diffusion-reaction
end
boundary.inp
4
periodic 0 1 0 0 -3.0 3.0
periodic 0 -1 0 0 -3.0 3.0
periodic 1 1 -6.0 6.0 0 0
periodic 1 -1 -6.0 6.0 0 0
physics.inp (specifies \(\nu_x\) and \(\nu_y\))
begin
diffusion 0.001 0.001
end
To generate initial.inp (initial solution) and exact.inp (exact solution), compile and run the following code in the run directory.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
{
double pi = 4.0*atan(1.0);
int NI=100,NJ=100,ndims=1,niter=0;
FILE *in, *out;
double nu_x,nu_y,dt,final_time;
char ip_file_type[50];
strcpy(ip_file_type,"ascii");
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);
fscanf(in,"%d",&NJ);
} else if (!strcmp(word, "dt")) fscanf(in,"%lf",&dt);
else if (!strcmp(word, "n_iter")) fscanf(in,"%d",&niter);
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 != 2) {
printf("ndims is not 2 in solver.inp. this code is to generate 2D initial conditions\n");
return(0);
}
printf("Grid:\t\t\t%d X %d\n",NI,NJ);
final_time = (double)niter * dt;
printf("Final Time:\t\t\t%lf\n",final_time);
printf("Reading file \"physics.inp\"...\n");
in = fopen("physics.inp","r");
if (!in) {
printf("Error: Input file \"physics.inp\" not found. Default values will be used.\n");
nu_x = nu_y = 1.0;
} else {
char word[500];
fscanf(in,"%s",word);
if (!strcmp(word, "begin")){
while (strcmp(word, "end")){
fscanf(in,"%s",word);
if (!strcmp(word,"diffusion")) {
fscanf(in,"%lf",&nu_x);
fscanf(in,"%lf",&nu_y);
}
}
} else printf("Error: Illegal format in solver.inp. Crash and burn!\n");
}
fclose(in);
printf("Diffusion Coeff:\t\t\t%lf, %lf\n",nu_x,nu_y);
int i,j;
double dx = 1.0 / ((double)NI);
double dy = 1.0 / ((double)NJ);
double *x, *y, *u;
x = (double*) calloc (NI , sizeof(double));
y = (double*) calloc (NJ , sizeof(double));
u = (double*) calloc (NI*NJ, sizeof(double));
for (i = 0; i < NI; i++){
for (j = 0; j < NJ; j++){
x[i] = i*dx;
y[j] = j*dy;
}
}
for (i = 0; i < NI; i++){
for (j = 0; j < NJ; j++){
int p = NJ*i + j;
u[p] = sin(2*pi*x[i]) * sin(2*pi*y[j]);
}
}
if (!strcmp(ip_file_type,"ascii")) {
printf("Writing ASCII file initial.inp.\n");
out = fopen("initial.inp","w");
for (i = 0; i < NI; i++) fprintf(out,"%lf ",x[i]);
fprintf(out,"\n");
for (j = 0; j < NJ; j++) fprintf(out,"%lf ",y[j]);
fprintf(out,"\n");
for (j = 0; j < NJ; j++) {
for (i = 0; i < NI; i++) {
int p = NJ*i + j;
fprintf(out,"%lf ",u[p]);
}
}
fprintf(out,"\n");
fclose(out);
} else if ((!strcmp(ip_file_type,"binary")) || (!strcmp(ip_file_type,"bin"))) {
printf("Error: Writing binary initial solution file not implemented. ");
printf("Please choose ip_file_type in solver.inp as \"ascii\".\n");
}
for (i = 0; i < NI; i++){
for (j = 0; j < NJ; j++){
int p = NJ*i + j;
u[p] = exp(-pi*pi*(4*nu_x+4*nu_y)*final_time) * sin(2*pi*x[i]) * sin(2*pi*y[j]);
}
}
if (!strcmp(ip_file_type,"ascii")) {
printf("Writing ASCII file exact.inp.\n");
out = fopen("exact.inp","w");
for (i = 0; i < NI; i++) fprintf(out,"%lf ",x[i]);
fprintf(out,"\n");
for (j = 0; j < NJ; j++) fprintf(out,"%lf ",y[j]);
fprintf(out,"\n");
for (j = 0; j < NJ; j++) {
for (i = 0; i < NI; i++) {
int p = NJ*i + j;
fprintf(out,"%lf ",u[p]);
}
}
fprintf(out,"\n");
fclose(out);
} else if ((!strcmp(ip_file_type,"binary")) || (!strcmp(ip_file_type,"bin"))) {
printf("Error: Writing binary exact solution file not implemented. ");
printf("Please choose ip_file_type in solver.inp as \"ascii\".\n");
}
free(x);
free(y);
free(u);
return(0);
}
Output:
Note that iproc is set to
2 2
in solver.inp (i.e., 2 processors along x, and 2 processors along y). Thus, this example should be run with 4 MPI ranks (or change iproc).
After running the code, there should be 11 output files op_00000.dat, op_00001.dat, ... op_00010.dat; the first one is the solution at \(t=0\) and the final one is the solution at \(t=10\). Since HyPar::op_overwrite is set to no in solver.inp, separate files are written for solutions at each output time.
HyPar::op_file_format is set to tecplot2d in solver.inp, and thus, all the files are in a format that Tecplot (http://www.tecplot.com/) or other visualization software supporting the Tecplot format (e.g. VisIt - https://wci.llnl.gov/simulation/computer-codes/visit/) can read. In these files, the first two lines are the Tecplot headers, after which the data is written out as: the first two columns are grid indices, the next two columns are x and y coordinates, and the final column is the solution. HyPar::op_file_format can be set to text to get the solution files in plain text format (which can be read in and visualized in MATLAB for example).
The following animation was generated from the solution files:
Since the exact solution is available at the final time , the numerical errors are calculated and reported on screen (see below) as well as errors.dat:
80 80 2 2 5.0000000000000003E-02 6.2964224103140468E-02 6.2590490579189209E-02 6.1847450231008405E-02 6.1507500000000004E-01 6.1944299999999997E-01
The numbers are: number of grid points in each dimension (HyPar::dim_global), number of processors in each dimension (MPIVariables::iproc), time step size (HyPar::dt), L1, L2, and L-infinity errors (HyPar::error), solver wall time (seconds) (i.e., not accounting for initialization, and cleaning up), and total wall time.
Expected screen output:
HyPar - Parallel (MPI) version with 4 processes
Compiled with PETSc time integration.
Reading solver inputs from file "solver.inp".
No. of dimensions : 2
No. of variables : 1
Domain size : 80 80
Processes along each dimension : 2 2
No. of ghosts pts : 3
No. of iter. : 200
Restart iteration : 0
Time integration scheme : rk (ssprk3)
Spatial discretization scheme (hyperbolic) : 1
Split hyperbolic flux term? : no
Interpolation type for hyperbolic term : characteristic
Spatial discretization type (parabolic ) : conservative-1stage
Spatial discretization scheme (parabolic ) : 2
Time Step : 5.000000E-02
Check for conservation : no
Screen output iterations : 5
File output iterations : 20
Initial solution file type : ascii
Initial solution read mode : serial
Solution file write mode : serial
Solution file format : tecplot2d
Overwrite solution file : no
Physical model : linear-advection-diffusion-reaction
Partitioning domain.
Allocating data arrays.
Reading array from ASCII file initial.inp (Serial mode).
Volume integral of the initial solution:
0: 6.9388939039072284E-17
Reading boundary conditions from "boundary.inp".
Boundary periodic: Along dimension 0 and face +1
Boundary periodic: Along dimension 0 and face -1
Boundary periodic: Along dimension 1 and face +1
Boundary periodic: Along dimension 1 and face -1
4 boundary condition(s) read.
Initializing solvers.
Initializing physics. Model = "linear-advection-diffusion-reaction"
Reading physical model inputs from file "physics.inp".
Setting up time integration.
Solving in time (from 0 to 200 iterations)
Writing solution file op_00000.dat.
Iteration: 5 Time: 2.500E-01 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.9382E-03
Iteration: 10 Time: 5.000E-01 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.9003E-03
Iteration: 15 Time: 7.500E-01 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.8632E-03
Iteration: 20 Time: 1.000E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.8268E-03
Writing solution file op_00001.dat.
Iteration: 25 Time: 1.250E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.7911E-03
Iteration: 30 Time: 1.500E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.7561E-03
Iteration: 35 Time: 1.750E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.7218E-03
Iteration: 40 Time: 2.000E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.6882E-03
Writing solution file op_00002.dat.
Iteration: 45 Time: 2.250E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.6552E-03
Iteration: 50 Time: 2.500E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.6229E-03
Iteration: 55 Time: 2.750E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.5912E-03
Iteration: 60 Time: 3.000E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.5601E-03
Writing solution file op_00003.dat.
Iteration: 65 Time: 3.250E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.5296E-03
Iteration: 70 Time: 3.500E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.4997E-03
Iteration: 75 Time: 3.750E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.4704E-03
Iteration: 80 Time: 4.000E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.4417E-03
Writing solution file op_00004.dat.
Iteration: 85 Time: 4.250E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.4136E-03
Iteration: 90 Time: 4.500E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.3860E-03
Iteration: 95 Time: 4.750E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.3590E-03
Iteration: 100 Time: 5.000E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.3325E-03
Writing solution file op_00005.dat.
Iteration: 105 Time: 5.250E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.3067E-03
Iteration: 110 Time: 5.500E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.2817E-03
Iteration: 115 Time: 5.750E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.2577E-03
Iteration: 120 Time: 6.000E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.2353E-03
Writing solution file op_00006.dat.
Iteration: 125 Time: 6.250E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.2156E-03
Iteration: 130 Time: 6.500E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.2009E-03
Iteration: 135 Time: 6.750E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.1957E-03
Iteration: 140 Time: 7.000E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.2092E-03
Writing solution file op_00007.dat.
Iteration: 145 Time: 7.250E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.2578E-03
Iteration: 150 Time: 7.500E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.3699E-03
Iteration: 155 Time: 7.750E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.5878E-03
Iteration: 160 Time: 8.000E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.9669E-03
Writing solution file op_00008.dat.
Iteration: 165 Time: 8.250E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 2.5754E-03
Iteration: 170 Time: 8.500E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 3.4999E-03
Iteration: 175 Time: 8.750E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 4.8596E-03
Iteration: 180 Time: 9.000E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 6.8244E-03
Writing solution file op_00009.dat.
Iteration: 185 Time: 9.250E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 9.6382E-03
Iteration: 190 Time: 9.500E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.3650E-02
Iteration: 195 Time: 9.750E+00 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 1.9358E-02
Iteration: 200 Time: 1.000E+01 Max CFL: 0.000E+00 Max Diff. No.: 3.200E-01 Norm: 2.7471E-02
Writing solution file op_00010.dat.
Completed time integration (Final time: 10.000000).
Reading array from ASCII file exact.inp (Serial mode).
Computed errors:
L1 Error : 6.2964224103140468E-02
L2 Error : 6.2590490579189209E-02
Linfinity Error : 6.1847450231008405E-02
Conservation Errors:
0.0000000000000000E+00
Solver runtime (in seconds): 6.1507500000000004E-01
Total runtime (in seconds): 6.1944299999999997E-01
Deallocating arrays.
Finished.