HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
shallowwater1d.h File Reference

1D Shallow Water Equations More...

#include <basic.h>
#include <math.h>
#include <matops.h>

Go to the source code of this file.

Data Structures

struct  ShallowWater1D
 Structure containing variables and parameters specific to the 1D Shallow Water equations. This structure contains the physical parameters, variables, and function pointers specific to the 1D ShallowWater equations. More...
 

Macros

#define _SHALLOW_WATER_1D_   "shallow-water-1d"
 
#define _MODEL_NDIMS_   1
 
#define _MODEL_NVARS_   2
 
#define _ROE_   "roe"
 
#define _LLF_   "llf-char"
 
#define _XDIR_   0
 
#define _ShallowWater1DGetFlowVar_(u, h, v)
 
#define _ShallowWater1DSetFlux_(f, h, v, g)
 
#define _ShallowWater1DRoeAverage_(uavg, uL, uR, p)
 
#define _ShallowWater1DEigenvalues_(u, D, p, dir)
 
#define _ShallowWater1DLeftEigenvectors_(u, L, p, dir)
 
#define _ShallowWater1DRightEigenvectors_(u, R, p, dir)
 

Functions

int ShallowWater1DInitialize (void *, void *)
 
int ShallowWater1DCleanup (void *)
 

Detailed Description

1D Shallow Water Equations

Author
Debojyoti Ghosh

1D Shallow Water Equations

\begin{equation} \frac {\partial} {\partial t} \left[\begin{array}{c} h \\ hu \end{array} \right] + \frac {\partial} {\partial x} \left[\begin{array}{c} hu \\ hu^2 + \frac{1}{2}gh^2 \end{array} \right] = \left[\begin{array}{c} 0 \\ -ghb_x \end{array}\right] \end{equation}

where \(h\) is the height, \(u\) is the velocity, \(b(x)\) is the bottom topography, and \(g\) is the gravitational constant.

For the treatment of the source term (well-balanced formulation), refer to:

  • Xing, Y., Shu, C.-W., "High order finite difference WENO schemes with the exact conservation property for the shallow water equations", Journal of Computational Physics, 208, 2005, pp. 206-227. http://dx.doi.org/10.1016/j.jcp.2005.02.006

Definition in file shallowwater1d.h.

Macro Definition Documentation

◆ _SHALLOW_WATER_1D_

#define _SHALLOW_WATER_1D_   "shallow-water-1d"

1D Shallow Water equations

Definition at line 29 of file shallowwater1d.h.

◆ _MODEL_NDIMS_

#define _MODEL_NDIMS_   1

Number of spatial dimensions

Definition at line 35 of file shallowwater1d.h.

◆ _MODEL_NVARS_

#define _MODEL_NVARS_   2

Number of variables per grid point

Definition at line 37 of file shallowwater1d.h.

◆ _ROE_

#define _ROE_   "roe"

Roe upwinding scheme

Definition at line 41 of file shallowwater1d.h.

◆ _LLF_

#define _LLF_   "llf-char"

Local Lax-Friedrich upwinding scheme

Definition at line 43 of file shallowwater1d.h.

◆ _XDIR_

#define _XDIR_   0

dimension corresponding to the x spatial dimension

Definition at line 48 of file shallowwater1d.h.

◆ _ShallowWater1DGetFlowVar_

#define _ShallowWater1DGetFlowVar_ (   u,
  h,
 
)
Value:
{ \
h = u[0]; \
v = u[1] / h; \
}

Compute the flow variables (height, velocity) from the conserved solution vector.

Definition at line 54 of file shallowwater1d.h.

◆ _ShallowWater1DSetFlux_

#define _ShallowWater1DSetFlux_ (   f,
  h,
  v,
 
)
Value:
{ \
f[0] = (h) * (v); \
f[1] = (h) * (v) * (v) + 0.5 * (g) * (h) * (h); \
}

Set the flux vector given the flow variables (height, velocity).

Definition at line 64 of file shallowwater1d.h.

◆ _ShallowWater1DRoeAverage_

#define _ShallowWater1DRoeAverage_ (   uavg,
  uL,
  uR,
 
)
Value:
{ \
double h ,v; \
double hL,vL; \
double hR,vR; \
_ShallowWater1DGetFlowVar_(uL,hL,vL); \
_ShallowWater1DGetFlowVar_(uR,hR,vR); \
h = 0.5 * (hL + hR); \
v = (sqrt(hL)*vL + sqrt(hR)*vR) / (sqrt(hL) + sqrt(hR)); \
uavg[0] = h; \
uavg[1] = h*v; \
}

Compute the Roe average of two conserved solution vectors.

Definition at line 74 of file shallowwater1d.h.

◆ _ShallowWater1DEigenvalues_

#define _ShallowWater1DEigenvalues_ (   u,
  D,
  p,
  dir 
)
Value:
{ \
double h,v,c; \
_ShallowWater1DGetFlowVar_(u,h,v); \
c = sqrt(p->g*h); \
D[0*_MODEL_NVARS_+0] = (v+c); D[0*_MODEL_NVARS_+1] = 0; \
D[1*_MODEL_NVARS_+0] = 0; D[1*_MODEL_NVARS_+1] = (v-c); \
}
#define _MODEL_NVARS_

Compute the eigenvalues, given the conserved solution vector. The eigenvalues are returned as a 3x3 matrix stored in row-major format. It is a diagonal matrix with the eigenvalues as diagonal elements. Admittedly, it is a wasteful way of storing the eigenvalues.

Definition at line 93 of file shallowwater1d.h.

◆ _ShallowWater1DLeftEigenvectors_

#define _ShallowWater1DLeftEigenvectors_ (   u,
  L,
  p,
  dir 
)
Value:
{ \
_ShallowWater1DRightEigenvectors_(u,R,p,dir); \
_MatrixInvert_(R,L,_MODEL_NVARS_); \
}
#define _MODEL_NVARS_

Compute the matrix that has the left-eigenvectors as its rows. Stored in row-major format.

Definition at line 106 of file shallowwater1d.h.

◆ _ShallowWater1DRightEigenvectors_

#define _ShallowWater1DRightEigenvectors_ (   u,
  R,
  p,
  dir 
)
Value:
{ \
double h,v,c; \
_ShallowWater1DGetFlowVar_(u,h,v); \
c = sqrt(p->g*h); \
R[0*_MODEL_NVARS_+0] = 1.0; \
R[1*_MODEL_NVARS_+0] = v+c; \
R[0*_MODEL_NVARS_+1] = 1.0; \
R[1*_MODEL_NVARS_+1] = v-c; \
}
#define _MODEL_NVARS_

Compute the matrix that has the right-eigenvectors as its columns. Stored in row-major format.

Definition at line 117 of file shallowwater1d.h.

Function Documentation

◆ ShallowWater1DInitialize()

int ShallowWater1DInitialize ( void *  s,
void *  m 
)

Function to initialize the 1D ShallowWater module

Function to initialize the 1D shallow water equations (ShallowWater1D) module: Sets the default parameters, read in and set physics-related parameters, and set the physics-related function pointers in HyPar.

Parameters
sSolver object of type HyPar
mObject of type MPIVariables containing MPI-related info

Definition at line 37 of file ShallowWater1DInitialize.c.

41 {
42  HyPar *solver = (HyPar*) s;
43  MPIVariables *mpi = (MPIVariables*) m;
44  ShallowWater1D *physics = (ShallowWater1D*) solver->physics;
45  int ferr, d;
46 
47  static int count = 0;
48 
49  if (solver->nvars != _MODEL_NVARS_) {
50  fprintf(stderr,"Error in ShallowWater1DInitialize(): nvars has to be %d.\n",_MODEL_NVARS_);
51  return(1);
52  }
53  if (solver->ndims != _MODEL_NDIMS_) {
54  fprintf(stderr,"Error in ShallowWater1DInitialize(): ndims has to be %d.\n",_MODEL_NDIMS_);
55  return(1);
56  }
57 
58  /* default values */
59  physics->g = 1.0;
60  physics->bt_type = 0;
61  strcpy(physics->upw_choice,"roe");
62 
63  /* reading physical model specific inputs */
64  if (!mpi->rank) {
65  FILE *in;
66  if (!count) printf("Reading physical model inputs from file \"physics.inp\".\n");
67  in = fopen("physics.inp","r");
68  if (!in) printf("Warning: File \"physics.inp\" not found. Using default values.\n");
69  else {
70  char word[_MAX_STRING_SIZE_];
71  ferr = fscanf(in,"%s",word); if (ferr != 1) return(1);
72  if (!strcmp(word, "begin")){
73  while (strcmp(word, "end")){
74  ferr = fscanf(in,"%s",word); if (ferr != 1) return(1);
75  if (!strcmp(word, "gravity")) {
76  ferr = fscanf(in,"%lf",&physics->g);
77  if (ferr != 1) return(1);
78  } else if (!strcmp(word, "topography_type")) {
79  ferr = fscanf(in,"%d",&physics->bt_type);
80  if (ferr != 1) return(1);
81  } else if (!strcmp(word,"upwinding")) {
82  ferr = fscanf(in,"%s",physics->upw_choice);
83  if (ferr != 1) return(1);
84  } else if (strcmp(word,"end")) {
85  char useless[_MAX_STRING_SIZE_];
86  ferr = fscanf(in,"%s",useless); if (ferr != 1) return(ferr);
87  printf("Warning: keyword %s in file \"physics.inp\" with value %s not ",word,useless);
88  printf("recognized or extraneous. Ignoring.\n");
89  }
90  }
91  } else {
92  fprintf(stderr,"Error: Illegal format in file \"physics.inp\".\n");
93  return(1);
94  }
95  }
96  fclose(in);
97  }
98 
99 #ifndef serial
100  IERR MPIBroadcast_double (&physics->g ,1,0,&mpi->world); CHECKERR(ierr);
101  IERR MPIBroadcast_integer (&physics->bt_type ,1,0,&mpi->world); CHECKERR(ierr);
103 #endif
104 
105  /* initializing physical model-specific functions */
107  solver->FFunction = ShallowWater1DFlux;
111  if (!strcmp(physics->upw_choice,_ROE_ )) solver->Upwind = ShallowWater1DUpwindRoe;
112  else if (!strcmp(physics->upw_choice,_LLF_ )) solver->Upwind = ShallowWater1DUpwindLLF;
113  else {
114  if (!mpi->rank) fprintf(stderr,"Error in ShallowWater1DInitialize(): %s is not a valid upwinding scheme.\n",
115  physics->upw_choice);
116  return(1);
117  }
122 
123  if (!strcmp(physics->upw_choice,_LLF_ )) physics->SourceUpwind = ShallowWater1DSourceUpwindLLF;
124  else if (!strcmp(physics->upw_choice,_ROE_ )) physics->SourceUpwind = ShallowWater1DSourceUpwindRoe;
125 
126  /* allocate array to hold the bottom topography field */
127  physics->b = (double*) calloc (solver->npoints_local_wghosts, sizeof(double));
128  /* set function pointer to read this topography */
130 
131  count++;
132  return(0);
133 }
Structure containing variables and parameters specific to the 1D Shallow Water equations. This structure contains the physical parameters, variables, and function pointers specific to the 1D ShallowWater equations.
int(* PhysicsOutput)(void *, void *, double)
Definition: hypar.h:347
int ShallowWater1DSource(double *, double *, void *, void *, double)
int nvars
Definition: hypar.h:29
#define IERR
Definition: basic.h:16
#define CHECKERR(ierr)
Definition: basic.h:18
int ShallowWater1DRoeAverage(double *, double *, double *, void *)
int ShallowWater1DUpwindLLF(double *, double *, double *, double *, double *, double *, int, void *, double)
int MPIBroadcast_integer(int *, int, int, void *)
Definition: MPIBroadcast.c:23
int(* AveragingFunction)(double *, double *, double *, void *)
Definition: hypar.h:354
int(* SourceUpwind)(double *, double *, double *, double *, int, void *, double)
int ShallowWater1DJacobian(double *, double *, void *, int, int, int)
int(* SFunction)(double *, double *, void *, void *, double)
Definition: hypar.h:317
int(* PhysicsInput)(void *, void *, int, int, int *)
Definition: hypar.h:351
int(* Upwind)(double *, double *, double *, double *, double *, double *, int, void *, double)
Definition: hypar.h:295
int ShallowWater1DTopography(void *, void *, int, int, int *)
int ndims
Definition: hypar.h:26
int MPIBroadcast_character(char *, int, int, void *)
Definition: MPIBroadcast.c:37
int(* FFunction)(double *, double *, int, void *, double)
Definition: hypar.h:276
int MPIBroadcast_double(double *, int, int, void *)
Definition: MPIBroadcast.c:9
double(* ComputeCFL)(void *, void *, double, double)
Definition: hypar.h:269
Structure containing all solver-specific variables and functions.
Definition: hypar.h:23
#define _MAX_STRING_SIZE_
Definition: basic.h:14
#define _ROE_
Definition: euler1d.h:62
int ShallowWater1DModifiedSolution(double *, double *, int, void *, void *, double)
#define _MODEL_NDIMS_
Definition: euler1d.h:56
MPI_Comm world
int ShallowWater1DSourceUpwindRoe(double *, double *, double *, double *, int, void *, double)
int ShallowWater1DRightEigenvectors(double *, double *, void *, int)
void * physics
Definition: hypar.h:266
int ShallowWater1DFlux(double *, double *, int, void *, double)
int(* JFunction)(double *, double *, void *, int, int, int)
Definition: hypar.h:326
char upw_choice[_MAX_STRING_SIZE_]
double ShallowWater1DComputeCFL(void *, void *, double, double)
Structure of MPI-related variables.
#define _MODEL_NVARS_
Definition: euler1d.h:58
int npoints_local_wghosts
Definition: hypar.h:42
int ShallowWater1DUpwindRoe(double *, double *, double *, double *, double *, double *, int, void *, double)
int ShallowWater1DLeftEigenvectors(double *, double *, void *, int)
int(* GetRightEigenvectors)(double *, double *, void *, int)
Definition: hypar.h:359
int(* GetLeftEigenvectors)(double *, double *, void *, int)
Definition: hypar.h:357
int(* UFunction)(double *, double *, int, void *, void *, double)
Definition: hypar.h:321
int ShallowWater1DSourceUpwindLLF(double *, double *, double *, double *, int, void *, double)
int ShallowWater1DWriteTopography(void *, void *, double)
#define _LLF_
Definition: euler1d.h:66

◆ ShallowWater1DCleanup()

int ShallowWater1DCleanup ( void *  s)

Function to clean up the 1D ShallowWater module

Function to clean up all physics-related allocations for the 1D shallow water equations

Parameters
sSolver object of type HyPar

Definition at line 10 of file ShallowWater1DCleanup.c.

13 {
14  ShallowWater1D *param = (ShallowWater1D*) s;
15  free(param->b);
16  return(0);
17 }
Structure containing variables and parameters specific to the 1D Shallow Water equations. This structure contains the physical parameters, variables, and function pointers specific to the 1D ShallowWater equations.