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

2D Shallow Water Equations More...

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

Go to the source code of this file.

Data Structures

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

Macros

#define _SHALLOW_WATER_2D_   "shallow-water-2d"
 
#define _MODEL_NDIMS_   2
 
#define _MODEL_NVARS_   3
 
#define _ROE_   "roe"
 
#define _LLF_   "llf-char"
 
#define _XDIR_   0
 
#define _YDIR_   1
 
#define _ShallowWater2DGetFlowVar_(u, h, uvel, vvel)
 
#define _ShallowWater2DSetFlux_(f, h, uvel, vvel, g, dir)
 
#define _ShallowWater2DRoeAverage_(uavg, uL, uR, p)
 
#define _ShallowWater2DEigenvalues_(u, D, p, dir)
 
#define _ShallowWater2DLeftEigenvectors_(u, L, p, dir)
 
#define _ShallowWater2DRightEigenvectors_(u, R, p, dir)
 

Functions

int ShallowWater2DInitialize (void *, void *)
 
int ShallowWater2DCleanup (void *)
 

Detailed Description

2D Shallow Water Equations

Author
Debojyoti Ghosh

2D Shallow Water Equations

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

where \(h\) is the height, \(\left(u,v\right)\) are the velocity components, \(b(x,y)\) is the bottom topography, and \(g\) is the gravitational constant.

The Coriolis parameter \(f\) is defined as

\begin{equation} f = \hat{f} + \beta\left( y - \frac{D}{2} \right) \end{equation}

based on:

For the treatment of the topography gradient 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 shallowwater2d.h.

Macro Definition Documentation

◆ _SHALLOW_WATER_2D_

#define _SHALLOW_WATER_2D_   "shallow-water-2d"

2D Shallow Water equations

Definition at line 43 of file shallowwater2d.h.

◆ _MODEL_NDIMS_

#define _MODEL_NDIMS_   2

Number of spatial dimensions

Definition at line 49 of file shallowwater2d.h.

◆ _MODEL_NVARS_

#define _MODEL_NVARS_   3

Number of variables per grid point

Definition at line 51 of file shallowwater2d.h.

◆ _ROE_

#define _ROE_   "roe"

Roe upwinding scheme

Definition at line 55 of file shallowwater2d.h.

◆ _LLF_

#define _LLF_   "llf-char"

Local Lax-Friedrich upwinding scheme

Definition at line 57 of file shallowwater2d.h.

◆ _XDIR_

#define _XDIR_   0

dimension corresponding to the x spatial dimension

Definition at line 63 of file shallowwater2d.h.

◆ _YDIR_

#define _YDIR_   1

Definition at line 64 of file shallowwater2d.h.

◆ _ShallowWater2DGetFlowVar_

#define _ShallowWater2DGetFlowVar_ (   u,
  h,
  uvel,
  vvel 
)
Value:
{ \
h = u[0]; \
uvel = u[1] / h; \
vvel = u[2] / h; \
}

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

\begin{equation} {\bf u} = \left[\begin{array}{c} h \\ hu \\ hv \end{array}\right] \end{equation}

Definition at line 73 of file shallowwater2d.h.

◆ _ShallowWater2DSetFlux_

#define _ShallowWater2DSetFlux_ (   f,
  h,
  uvel,
  vvel,
  g,
  dir 
)
Value:
{ \
if (dir == _XDIR_) { \
f[0] = (h) * (uvel); \
f[1] = (h) * (uvel) * (uvel) + 0.5 * (g) * (h) * (h); \
f[2] = (h) * (uvel) * (vvel); \
} else if (dir == _YDIR_) { \
f[0] = (h) * (vvel); \
f[1] = (h) * (uvel) * (vvel); \
f[2] = (h) * (vvel) * (vvel) + 0.5 * (g) * (h) * (h); \
} \
}
#define _YDIR_
#define _XDIR_

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

\begin{equation} {\bf F}\left({\bf u}\right) = \left\{\begin{array}{cc} \left[\begin{array}{c} hu \\ hu^2 + \frac{1}{2} gh^2 \\ huv \end{array}\right] & {\rm dir} = x \\ \left[\begin{array}{c} hv \\ huv \\ hv^2 + \frac{1}{2} gh^2 \end{array}\right] & {\rm dir} = y \\ \end{array}\right. \end{equation}

Definition at line 91 of file shallowwater2d.h.

◆ _ShallowWater2DRoeAverage_

#define _ShallowWater2DRoeAverage_ (   uavg,
  uL,
  uR,
 
)
Value:
{ \
double h , uvel , vvel ; \
double hL, uvelL, vvelL; \
double hR, uvelR, vvelR; \
_ShallowWater2DGetFlowVar_(uL,hL,uvelL,vvelL); \
_ShallowWater2DGetFlowVar_(uR,hR,uvelR,vvelR); \
h = 0.5 * (hL + hR ); \
uvel = (sqrt(hL)*uvelL + sqrt(hR)*uvelR) / (sqrt(hL) + sqrt(hR)); \
vvel = (sqrt(hL)*vvelL + sqrt(hR)*vvelR) / (sqrt(hL) + sqrt(hR)); \
uavg[0] = h; \
uavg[1] = h*uvel; \
uavg[2] = h*vvel; \
}

Compute the Roe average of two conserved solution vectors.

Definition at line 108 of file shallowwater2d.h.

◆ _ShallowWater2DEigenvalues_

#define _ShallowWater2DEigenvalues_ (   u,
  D,
  p,
  dir 
)
Value:
{ \
double h,uvel,vvel,c; \
_ShallowWater2DGetFlowVar_(u,h,uvel,vvel); \
c = sqrt(p->g*h); \
if (dir == _XDIR_) { \
D[0*_MODEL_NVARS_+0] = uvel-c; D[0*_MODEL_NVARS_+1] = 0; D[0*_MODEL_NVARS_+2] = 0; \
D[1*_MODEL_NVARS_+0] = 0; D[1*_MODEL_NVARS_+1] = uvel; D[1*_MODEL_NVARS_+2] = 0; \
D[2*_MODEL_NVARS_+0] = 0; D[2*_MODEL_NVARS_+1] = 0; D[2*_MODEL_NVARS_+2] = uvel+c; \
} else if (dir == _YDIR_) { \
D[0*_MODEL_NVARS_+0] = vvel-c; D[0*_MODEL_NVARS_+1] = 0; D[0*_MODEL_NVARS_+2] = 0; \
D[1*_MODEL_NVARS_+0] = 0; D[1*_MODEL_NVARS_+1] = vvel; D[1*_MODEL_NVARS_+2] = 0; \
D[2*_MODEL_NVARS_+0] = 0; D[2*_MODEL_NVARS_+1] = 0; D[2*_MODEL_NVARS_+2] = vvel+c; \
} \
}
#define _YDIR_
#define _XDIR_
#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 129 of file shallowwater2d.h.

◆ _ShallowWater2DLeftEigenvectors_

#define _ShallowWater2DLeftEigenvectors_ (   u,
  L,
  p,
  dir 
)

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

Definition at line 154 of file shallowwater2d.h.

◆ _ShallowWater2DRightEigenvectors_

#define _ShallowWater2DRightEigenvectors_ (   u,
  R,
  p,
  dir 
)

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

Definition at line 197 of file shallowwater2d.h.

Function Documentation

◆ ShallowWater2DInitialize()

int ShallowWater2DInitialize ( void *  s,
void *  m 
)

Function to initialize the 2D ShallowWater module

Function to initialize the 2D shallow water equations (ShallowWater2D) 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 ShallowWater2DInitialize.c.

41 {
42  HyPar *solver = (HyPar*) s;
43  MPIVariables *mpi = (MPIVariables*) m;
44  ShallowWater2D *physics = (ShallowWater2D*) solver->physics;
45  int ferr, d;
46 
47  static int count = 0;
48 
49  if (solver->nvars != _MODEL_NVARS_) {
50  fprintf(stderr,"Error in ShallowWater2DInitialize(): nvars has to be %d.\n",_MODEL_NVARS_);
51  return(1);
52  }
53  if (solver->ndims != _MODEL_NDIMS_) {
54  fprintf(stderr,"Error in ShallowWater2DInitialize(): 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  physics->fhat = 0.0;
62  physics->beta = 0.0;
63  physics->D = 0.0;
64  strcpy(physics->upw_choice,"roe");
65 
66  /* reading physical model specific inputs */
67  if (!mpi->rank) {
68  FILE *in;
69  if (!count) printf("Reading physical model inputs from file \"physics.inp\".\n");
70  in = fopen("physics.inp","r");
71  if (!in) printf("Warning: File \"physics.inp\" not found. Using default values.\n");
72  else {
73  char word[_MAX_STRING_SIZE_];
74  ferr = fscanf(in,"%s",word); if (ferr != 1) return(1);
75  if (!strcmp(word, "begin")){
76  while (strcmp(word, "end")){
77  ferr = fscanf(in,"%s",word); if (ferr != 1) return(1);
78  if (!strcmp(word, "gravity")) {
79  ferr = fscanf(in,"%lf",&physics->g);
80  if (ferr != 1) return(1);
81  } else if (!strcmp(word, "topography_type")) {
82  ferr = fscanf(in,"%d",&physics->bt_type);
83  if (ferr != 1) return(1);
84  } else if (!strcmp(word, "coriolis_fhat")) {
85  ferr = fscanf(in,"%lf",&physics->fhat);
86  if (ferr != 1) return(1);
87  } else if (!strcmp(word, "coriolis_beta")) {
88  ferr = fscanf(in,"%lf",&physics->beta);
89  if (ferr != 1) return(1);
90  } else if (!strcmp(word, "coriolis_D")) {
91  ferr = fscanf(in,"%lf",&physics->D);
92  if (ferr != 1) return(1);
93  } else if (!strcmp(word,"upwinding")) {
94  ferr = fscanf(in,"%s",physics->upw_choice);
95  if (ferr != 1) return(1);
96  } else if (strcmp(word,"end")) {
97  char useless[_MAX_STRING_SIZE_];
98  ferr = fscanf(in,"%s",useless); if (ferr != 1) return(ferr);
99  printf("Warning: keyword %s in file \"physics.inp\" with value %s not ",word,useless);
100  printf("recognized or extraneous. Ignoring.\n");
101  }
102  }
103  } else {
104  fprintf(stderr,"Error: Illegal format in file \"physics.inp\".\n");
105  return(1);
106  }
107  }
108  fclose(in);
109  }
110 
111 #ifndef serial
112  IERR MPIBroadcast_double (&physics->g ,1,0,&mpi->world); CHECKERR(ierr);
113  IERR MPIBroadcast_integer (&physics->bt_type ,1,0,&mpi->world); CHECKERR(ierr);
114  IERR MPIBroadcast_double (&physics->fhat ,1,0,&mpi->world); CHECKERR(ierr);
115  IERR MPIBroadcast_double (&physics->beta ,1,0,&mpi->world); CHECKERR(ierr);
116  IERR MPIBroadcast_double (&physics->D ,1,0,&mpi->world); CHECKERR(ierr);
118 #endif
119 
120  /* initializing physical model-specific functions */
122  solver->FFunction = ShallowWater2DFlux;
126  if (!strcmp(physics->upw_choice,_ROE_ )) solver->Upwind = ShallowWater2DUpwindRoe;
127  else if (!strcmp(physics->upw_choice,_LLF_ )) solver->Upwind = ShallowWater2DUpwindLLF;
128  else {
129  if (!mpi->rank) fprintf(stderr,"Error in ShallowWater2DInitialize(): %s is not a valid upwinding scheme.\n",
130  physics->upw_choice);
131  return(1);
132  }
137 
138  if (!strcmp(physics->upw_choice,_LLF_ )) physics->SourceUpwind = ShallowWater2DSourceUpwindLLF;
139  else if (!strcmp(physics->upw_choice,_ROE_ )) physics->SourceUpwind = ShallowWater2DSourceUpwindRoe;
140 
141  /* allocate array to hold the bottom topography field */
142  physics->b = (double*) calloc (solver->npoints_local_wghosts, sizeof(double));
144 
145  count++;
146  return(0);
147 }
int(* PhysicsOutput)(void *, void *, double)
Definition: hypar.h:347
int nvars
Definition: hypar.h:29
#define IERR
Definition: basic.h:16
int ShallowWater2DLeftEigenvectors(double *, double *, void *, int)
#define CHECKERR(ierr)
Definition: basic.h:18
char upw_choice[_MAX_STRING_SIZE_]
int(* SourceUpwind)(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
double ShallowWater2DComputeCFL(void *, void *, double, double)
int ShallowWater2DTopography(void *, 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 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 ShallowWater2DModifiedSolution(double *, double *, int, void *, void *, double)
int ShallowWater2DSource(double *, double *, void *, void *, double)
int MPIBroadcast_double(double *, int, int, void *)
Definition: MPIBroadcast.c:9
double(* ComputeCFL)(void *, void *, double, double)
Definition: hypar.h:269
Structure containing variables and parameters specific to the 2D Shallow Water equations. This structure contains the physical parameters, variables, and function pointers specific to the 2D ShallowWater equations.
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 ShallowWater2DFlux(double *, double *, int, void *, double)
int ShallowWater2DJacobian(double *, double *, void *, int, int, int)
#define _MODEL_NDIMS_
Definition: euler1d.h:56
int ShallowWater2DRightEigenvectors(double *, double *, void *, int)
MPI_Comm world
int ShallowWater2DUpwindLLF(double *, double *, double *, double *, double *, double *, int, void *, double)
int ShallowWater2DWriteTopography(void *, void *, double)
void * physics
Definition: hypar.h:266
int(* JFunction)(double *, double *, void *, int, int, int)
Definition: hypar.h:326
int ShallowWater2DSourceUpwindLLF(double *, double *, double *, double *, int, void *, double)
int ShallowWater2DSourceUpwindRoe(double *, double *, double *, double *, int, void *, double)
Structure of MPI-related variables.
#define _MODEL_NVARS_
Definition: euler1d.h:58
int npoints_local_wghosts
Definition: hypar.h:42
int(* GetRightEigenvectors)(double *, double *, void *, int)
Definition: hypar.h:359
int(* GetLeftEigenvectors)(double *, double *, void *, int)
Definition: hypar.h:357
int ShallowWater2DRoeAverage(double *, double *, double *, void *)
int(* UFunction)(double *, double *, int, void *, void *, double)
Definition: hypar.h:321
int ShallowWater2DUpwindRoe(double *, double *, double *, double *, double *, double *, int, void *, double)
#define _LLF_
Definition: euler1d.h:66

◆ ShallowWater2DCleanup()

int ShallowWater2DCleanup ( void *  s)

Function to clean up the 2D ShallowWater module

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

Parameters
sSolver object of type HyPar

Definition at line 10 of file ShallowWater2DCleanup.c.

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