HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
ShallowWater2DSource.c
Go to the documentation of this file.
1 
6 #include <stdlib.h>
7 #include <basic.h>
8 #include <arrayfunctions.h>
10 #include <mpivars.h>
11 #include <hypar.h>
12 
13 static int ShallowWater2DSourceFunction1 (double*,double*,double*,void*,void*,double,int);
14 static int ShallowWater2DSourceFunction2 (double*,double*,double*,void*,void*,double,int);
15 
34  double *source,
35  double *u,
36  void *s,
37  void *m,
38  double t
39  )
40 {
41  HyPar *solver = (HyPar* ) s;
42  MPIVariables *mpi = (MPIVariables*) m;
43  ShallowWater2D *param = (ShallowWater2D*) solver->physics;
44 
45  int v, done, p, p1, p2;
46  double *SourceI = solver->fluxI; /* interace source term */
47  double *SourceC = solver->fluxC; /* cell-centered source term */
48  double *SourceL = solver->fL;
49  double *SourceR = solver->fR;
50 
51  int ndims = solver->ndims;
52  int ghosts = solver->ghosts;
53  int *dim = solver->dim_local;
54  double *x = solver->x;
55  double *dxinv = solver->dxinv;
56  int index[ndims],index1[ndims],index2[ndims],dim_interface[ndims];
57 
58  /* Along X */
59 
60  /* set interface dimensions */
61  _ArrayCopy1D_(dim,dim_interface,ndims); dim_interface[_XDIR_]++;
62 
63  /* calculate the first source function */
64  IERR ShallowWater2DSourceFunction1(SourceC,u,x,solver,mpi,t,_XDIR_); CHECKERR(ierr);
65  /* calculate the left and right interface source terms */
66  IERR solver->InterpolateInterfacesHyp(SourceL,SourceC,u,x, 1,_XDIR_,solver,mpi,0); CHECKERR(ierr);
67  IERR solver->InterpolateInterfacesHyp(SourceR,SourceC,u,x,-1,_XDIR_,solver,mpi,0); CHECKERR(ierr);
68  /* calculate the final interface source term */
69  IERR param->SourceUpwind(SourceI,SourceL,SourceR,u,_XDIR_,solver,t);
70  /* calculate the final cell-centered source term */
71  done = 0; _ArraySetValue_(index,ndims,0);
72  while (!done) {
73  _ArrayCopy1D_(index,index1,ndims);
74  _ArrayCopy1D_(index,index2,ndims); index2[_XDIR_]++;
75  _ArrayIndex1D_(ndims,dim ,index ,ghosts,p );
76  _ArrayIndex1D_(ndims,dim_interface,index1,0 ,p1);
77  _ArrayIndex1D_(ndims,dim_interface,index2,0 ,p2);
78  double dx_inverse; _GetCoordinate_(_XDIR_,index[_XDIR_],dim,ghosts,dxinv,dx_inverse);
79  for (v=0; v<_MODEL_NVARS_; v++)
80  source[_MODEL_NVARS_*p+v] += (SourceI[_MODEL_NVARS_*p2+v]-SourceI[_MODEL_NVARS_*p1+v])*dx_inverse;
81  _ArrayIncrementIndex_(ndims,dim,index,done);
82  }
83  /* calculate the second source function */
84  IERR ShallowWater2DSourceFunction2(SourceC,u,x,solver,mpi,t,_XDIR_); CHECKERR(ierr);
85  /* calculate the left and right interface source terms */
86  IERR solver->InterpolateInterfacesHyp(SourceL,SourceC,u,x, 1,_XDIR_,solver,mpi,0); CHECKERR(ierr);
87  IERR solver->InterpolateInterfacesHyp(SourceR,SourceC,u,x,-1,_XDIR_,solver,mpi,0); CHECKERR(ierr);
88  /* calculate the final interface source term */
89  IERR param->SourceUpwind(SourceI,SourceL,SourceR,u,_XDIR_,solver,t);
90  /* calculate the final cell-centered source term */
91  done = 0; _ArraySetValue_(index,ndims,0);
92  while (!done) {
93  _ArrayCopy1D_(index,index1,ndims);
94  _ArrayCopy1D_(index,index2,ndims); index2[_XDIR_]++;
95  _ArrayIndex1D_(ndims,dim ,index ,ghosts,p );
96  _ArrayIndex1D_(ndims,dim_interface,index1,0 ,p1);
97  _ArrayIndex1D_(ndims,dim_interface,index2,0 ,p2);
98  double dx_inverse; _GetCoordinate_(_XDIR_,index[_XDIR_],dim,ghosts,dxinv,dx_inverse);
99  double h, uvel, vvel; _ShallowWater2DGetFlowVar_((u+_MODEL_NVARS_*p),h,uvel,vvel);
100  double term[_MODEL_NVARS_] = { 0.0, -param->g * (h + param->b[p]), 0.0 };
101  for (v=0; v<_MODEL_NVARS_; v++)
102  source[_MODEL_NVARS_*p+v] += term[v]*(SourceI[_MODEL_NVARS_*p2+v]-SourceI[_MODEL_NVARS_*p1+v])*dx_inverse;
103  uvel = vvel = h; /* useless statement to avoid compiler warning */
104  _ArrayIncrementIndex_(ndims,dim,index,done);
105  }
106 
107  /* Along Y */
108 
109  /* set interface dimensions */
110  _ArrayCopy1D_(dim,dim_interface,ndims); dim_interface[_YDIR_]++;
111 
112  /* calculate the first source function */
113  IERR ShallowWater2DSourceFunction1(SourceC,u,x,solver,mpi,t,_YDIR_); CHECKERR(ierr);
114  /* calculate the left and right interface source terms */
115  IERR solver->InterpolateInterfacesHyp(SourceL,SourceC,u,x, 1,_YDIR_,solver,mpi,0); CHECKERR(ierr);
116  IERR solver->InterpolateInterfacesHyp(SourceR,SourceC,u,x,-1,_YDIR_,solver,mpi,0); CHECKERR(ierr);
117  /* calculate the final interface source term */
118  IERR param->SourceUpwind(SourceI,SourceL,SourceR,u,_YDIR_,solver,t);
119  /* calculate the final cell-centered source term */
120  done = 0; _ArraySetValue_(index,ndims,0);
121  while (!done) {
122  _ArrayCopy1D_(index,index1,ndims);
123  _ArrayCopy1D_(index,index2,ndims); index2[_YDIR_]++;
124  _ArrayIndex1D_(ndims,dim ,index ,ghosts,p );
125  _ArrayIndex1D_(ndims,dim_interface,index1,0 ,p1);
126  _ArrayIndex1D_(ndims,dim_interface,index2,0 ,p2);
127  double dy_inverse; _GetCoordinate_(_YDIR_,index[_YDIR_],dim,ghosts,dxinv,dy_inverse);
128  for (v=0; v<_MODEL_NVARS_; v++)
129  source[_MODEL_NVARS_*p+v] += (SourceI[_MODEL_NVARS_*p2+v]-SourceI[_MODEL_NVARS_*p1+v])*dy_inverse;
130  _ArrayIncrementIndex_(ndims,dim,index,done);
131  }
132  /* calculate the second source function */
133  IERR ShallowWater2DSourceFunction2(SourceC,u,x,solver,mpi,t,_YDIR_); CHECKERR(ierr);
134  /* calculate the left and right interface source terms */
135  IERR solver->InterpolateInterfacesHyp(SourceL,SourceC,u,x, 1,_YDIR_,solver,mpi,0); CHECKERR(ierr);
136  IERR solver->InterpolateInterfacesHyp(SourceR,SourceC,u,x,-1,_YDIR_,solver,mpi,0); CHECKERR(ierr);
137  /* calculate the final interface source term */
138  IERR param->SourceUpwind(SourceI,SourceL,SourceR,u,_YDIR_,solver,t);
139  /* calculate the final cell-centered source term */
140  done = 0; _ArraySetValue_(index,ndims,0);
141  while (!done) {
142  _ArrayCopy1D_(index,index1,ndims);
143  _ArrayCopy1D_(index,index2,ndims); index2[_YDIR_]++;
144  _ArrayIndex1D_(ndims,dim ,index ,ghosts,p );
145  _ArrayIndex1D_(ndims,dim_interface,index1,0 ,p1);
146  _ArrayIndex1D_(ndims,dim_interface,index2,0 ,p2);
147  double dy_inverse; _GetCoordinate_(_YDIR_,index[_YDIR_],dim,ghosts,dxinv,dy_inverse);
148  double h, uvel, vvel; _ShallowWater2DGetFlowVar_((u+_MODEL_NVARS_*p),h,uvel,vvel);
149  double term[_MODEL_NVARS_] = { 0.0, 0.0, -param->g * (h + param->b[p]) };
150  for (v=0; v<_MODEL_NVARS_; v++)
151  source[_MODEL_NVARS_*p+v] += term[v]*(SourceI[_MODEL_NVARS_*p2+v]-SourceI[_MODEL_NVARS_*p1+v])*dy_inverse;
152  uvel = vvel = h; /* useless statement to avoid compiler warning */
153  _ArrayIncrementIndex_(ndims,dim,index,done);
154  }
155 
156  /* Naive addition of Coriolis force terms */
157  done = 0; _ArraySetValue_(index,ndims,0);
158  while (!done) {
159  _ArrayIndex1D_(ndims,dim,index ,ghosts,p);
160  double h, uvel, vvel;
161  _ShallowWater2DGetFlowVar_((u+_MODEL_NVARS_*p),h,uvel,vvel);
162  double ycoord; _GetCoordinate_(_YDIR_,index[_YDIR_],dim,ghosts,solver->x,ycoord);
163  double coeff = param->fhat + param->beta * (ycoord - 0.5*param->D);
164  double coriolis_x = coeff * vvel,
165  coriolis_y = -coeff * uvel;
166  source[_MODEL_NVARS_*p+1] += coriolis_x;
167  source[_MODEL_NVARS_*p+2] += coriolis_y;
168  _ArrayIncrementIndex_(ndims,dim,index,done);
169  }
170 
171  return(0);
172 }
173 
195  double *f,
196  double *u,
197  double *x,
198  void *s,
199  void *m,
200  double t,
201  int dir
202  )
203 {
204  HyPar *solver = (HyPar* ) s;
205  ShallowWater2D *param = (ShallowWater2D*) solver->physics;
206 
207  int ghosts = solver->ghosts;
208  int *dim = solver->dim_local;
209  int ndims = solver->ndims;
210  int index[ndims], bounds[ndims], offset[ndims];
211 
212  /* set bounds for array index to include ghost points */
213  _ArrayCopy1D_(dim,bounds,ndims);
214  int i; for (i=0; i<ndims; i++) bounds[i] += 2*ghosts;
215 
216  /* set offset such that index is compatible with ghost point arrangement */
217  _ArraySetValue_(offset,ndims,-ghosts);
218 
219  int done = 0; _ArraySetValue_(index,ndims,0);
220  while (!done) {
221  int p; _ArrayIndex1DWO_(ndims,dim,index,offset,ghosts,p);
222  (f+_MODEL_NVARS_*p)[0] = 0.0;
223  (f+_MODEL_NVARS_*p)[1] = (dir == _XDIR_ ? 0.5 * param->g * param->b[p] * param->b[p] : 0.0 );
224  (f+_MODEL_NVARS_*p)[2] = (dir == _YDIR_ ? 0.5 * param->g * param->b[p] * param->b[p] : 0.0 );
225  _ArrayIncrementIndex_(ndims,bounds,index,done);
226  }
227 
228  return(0);
229 }
230 
252  double *f,
253  double *u,
254  double *x,
255  void *s,
256  void *m,
257  double t,
258  int dir
259  )
260 {
261  HyPar *solver = (HyPar* ) s;
262  ShallowWater2D *param = (ShallowWater2D*) solver->physics;
263 
264  int ghosts = solver->ghosts;
265  int *dim = solver->dim_local;
266  int ndims = solver->ndims;
267  int index[ndims], bounds[ndims], offset[ndims];
268 
269  /* set bounds for array index to include ghost points */
270  _ArrayCopy1D_(dim,bounds,ndims);
271  int i; for (i=0; i<ndims; i++) bounds[i] += 2*ghosts;
272 
273  /* set offset such that index is compatible with ghost point arrangement */
274  _ArraySetValue_(offset,ndims,-ghosts);
275 
276  int done = 0; _ArraySetValue_(index,ndims,0);
277  while (!done) {
278  int p; _ArrayIndex1DWO_(ndims,dim,index,offset,ghosts,p);
279  (f+_MODEL_NVARS_*p)[0] = 0.0;
280  (f+_MODEL_NVARS_*p)[1] = (dir == _XDIR_ ? param->b[p] : 0.0 );
281  (f+_MODEL_NVARS_*p)[2] = (dir == _YDIR_ ? param->b[p] : 0.0 );
282  _ArrayIncrementIndex_(ndims,bounds,index,done);
283  }
284 
285  return(0);
286 }
#define IERR
Definition: basic.h:16
MPI related function definitions.
#define CHECKERR(ierr)
Definition: basic.h:18
int(* SourceUpwind)(double *, double *, double *, double *, int, void *, double)
int ShallowWater2DSource(double *source, double *u, void *s, void *m, double t)
Some basic definitions and macros.
static int ShallowWater2DSourceFunction2(double *, double *, double *, void *, void *, double, int)
double * x
Definition: hypar.h:107
2D Shallow Water Equations
double * fluxI
Definition: hypar.h:136
int ndims
Definition: hypar.h:26
static int ShallowWater2DSourceFunction1(double *, double *, double *, void *, void *, double, int)
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 _YDIR_
Definition: euler2d.h:41
Contains structure definition for hypar.
int(* InterpolateInterfacesHyp)(double *, double *, double *, double *, int, int, void *, void *, int)
Definition: hypar.h:224
#define _ArrayIndex1D_(N, imax, i, ghost, index)
#define _ArrayIndex1DWO_(N, imax, i, offset, ghost, index)
#define _ArraySetValue_(x, size, value)
int * dim_local
Definition: hypar.h:37
#define _ArrayIncrementIndex_(N, imax, i, done)
#define _GetCoordinate_(dir, i, dim, ghosts, x, coord)
Definition: basic.h:31
void * physics
Definition: hypar.h:266
double * fR
Definition: hypar.h:139
double * fL
Definition: hypar.h:139
#define _XDIR_
Definition: euler1d.h:75
int ghosts
Definition: hypar.h:52
Structure of MPI-related variables.
#define _MODEL_NVARS_
Definition: euler1d.h:58
#define _ArrayCopy1D_(x, y, size)
Contains macros and function definitions for common array operations.
double * fluxC
Definition: hypar.h:128
double * dxinv
Definition: hypar.h:110
#define _ShallowWater2DGetFlowVar_(u, h, uvel, vvel)