HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
NavierStokes2DInitialize.c
Go to the documentation of this file.
1 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <basic.h>
9 #include <arrayfunctions.h>
10 #include <boundaryconditions.h>
12 #include <mpivars.h>
13 #include <hypar.h>
14 
15 double NavierStokes2DComputeCFL (void*,void*,double,double);
16 int NavierStokes2DFlux (double*,double*,int,void*,double);
17 int NavierStokes2DStiffFlux (double*,double*,int,void*,double);
18 int NavierStokes2DNonStiffFlux (double*,double*,int,void*,double);
19 int NavierStokes2DRoeAverage (double*,double*,double*,void*);
20 int NavierStokes2DParabolicFunction (double*,double*,void*,void*,double);
21 int NavierStokes2DSource (double*,double*,void*,void*,double);
22 
23 int NavierStokes2DJacobian (double*,double*,void*,int,int,int);
24 int NavierStokes2DStiffJacobian (double*,double*,void*,int,int,int);
25 
26 int NavierStokes2DLeftEigenvectors (double*,double*,void*,int);
27 int NavierStokes2DRightEigenvectors (double*,double*,void*,int);
28 
29 int NavierStokes2DUpwindRoe (double*,double*,double*,double*,double*,double*,int,void*,double);
30 int NavierStokes2DUpwindRF (double*,double*,double*,double*,double*,double*,int,void*,double);
31 int NavierStokes2DUpwindLLF (double*,double*,double*,double*,double*,double*,int,void*,double);
32 int NavierStokes2DUpwindSWFS (double*,double*,double*,double*,double*,double*,int,void*,double);
33 int NavierStokes2DUpwindRusanov (double*,double*,double*,double*,double*,double*,int,void*,double);
34 
35 int NavierStokes2DUpwinddFRoe (double*,double*,double*,double*,double*,double*,int,void*,double);
36 int NavierStokes2DUpwinddFRF (double*,double*,double*,double*,double*,double*,int,void*,double);
37 int NavierStokes2DUpwinddFLLF (double*,double*,double*,double*,double*,double*,int,void*,double);
38 int NavierStokes2DUpwinddFRusanov (double*,double*,double*,double*,double*,double*,int,void*,double);
39 
40 int NavierStokes2DUpwindFdFRoe (double*,double*,double*,double*,double*,double*,int,void*,double);
41 int NavierStokes2DUpwindFdFRF (double*,double*,double*,double*,double*,double*,int,void*,double);
42 int NavierStokes2DUpwindFdFLLF (double*,double*,double*,double*,double*,double*,int,void*,double);
43 int NavierStokes2DUpwindFdFRusanov (double*,double*,double*,double*,double*,double*,int,void*,double);
44 
45 int NavierStokes2DUpwindRusanovModified (double*,double*,double*,double*,double*,double*,int,void*,double);
46 int NavierStokes2DUpwinddFRusanovModified (double*,double*,double*,double*,double*,double*,int,void*,double);
47 int NavierStokes2DUpwindFdFRusanovModified(double*,double*,double*,double*,double*,double*,int,void*,double);
48 
49 int NavierStokes2DGravityField (void*,void*);
50 int NavierStokes2DModifiedSolution (double*,double*,int,void*,void*,double);
51 int NavierStokes2DPreStep (double*,void*,void*,double);
52 
53 #if defined(HAVE_CUDA) && defined(CUDA_VAR_ORDERDING_AOS)
54 int gpuNavierStokes2DInitialize (void*,void*);
55 int gpuNavierStokes2DFlux (double*,double*,int,void*,double);
56 int gpuNavierStokes2DSource (double*,double*,void*,void*,double);
57 int gpuNavierStokes2DUpwindRusanov (double*,double*,double*,double*,
58  double*,double*,int,void*,double);
59 int gpuNavierStokes2DModifiedSolution (double*,double*,int,void*,void*,double);
60 int gpuNavierStokes2DParabolicFunction (double*,double*,void*,void*,double);
61 #endif
62 
104  void *s,
105  void *m
106  )
107 {
108  HyPar *solver = (HyPar*) s;
109  MPIVariables *mpi = (MPIVariables*) m;
110  NavierStokes2D *physics = (NavierStokes2D*) solver->physics;
111  int ferr = 0;
112 
113  static int count = 0;
114 
115  if (solver->nvars != _MODEL_NVARS_) {
116  fprintf(stderr,"Error in NavierStokes2DInitialize(): nvars has to be %d.\n",_MODEL_NVARS_);
117  return(1);
118  }
119  if (solver->ndims != _MODEL_NDIMS_) {
120  fprintf(stderr,"Error in NavierStokes2DInitialize(): ndims has to be %d.\n",_MODEL_NDIMS_);
121  return(1);
122  }
123 
124  /* default values */
125  physics->gamma = 1.4;
126  physics->Pr = 0.72;
127  physics->Re = -1;
128  physics->Minf = 1.0;
129  physics->C1 = 1.458e-6;
130  physics->C2 = 110.4;
131  physics->grav_x = 0.0;
132  physics->grav_y = 0.0;
133  physics->rho0 = 1.0;
134  physics->p0 = 1.0;
135  physics->HB = 1;
136  physics->R = 1.0;
137  physics->N_bv = 0.0;
138  strcpy(physics->upw_choice,"roe");
139 
140  /* reading physical model specific inputs - all processes */
141  if (!mpi->rank) {
142  FILE *in;
143  if (!count) printf("Reading physical model inputs from file \"physics.inp\".\n");
144  in = fopen("physics.inp","r");
145  if (!in) printf("Warning: File \"physics.inp\" not found. Using default values.\n");
146  else {
147  char word[_MAX_STRING_SIZE_];
148  ferr = fscanf(in,"%s",word); if (ferr != 1) return(1);
149  if (!strcmp(word, "begin")){
150  while (strcmp(word, "end")){
151  ferr = fscanf(in,"%s",word); if (ferr != 1) return(1);
152  if (!strcmp(word, "gamma")) {
153  ferr = fscanf(in,"%lf",&physics->gamma); if (ferr != 1) return(1);
154  } else if (!strcmp(word,"upwinding")) {
155  ferr = fscanf(in,"%s",physics->upw_choice); if (ferr != 1) return(1);
156  } else if (!strcmp(word,"Pr")) {
157  ferr = fscanf(in,"%lf",&physics->Pr); if (ferr != 1) return(1);
158  } else if (!strcmp(word,"Re")) {
159  ferr = fscanf(in,"%lf",&physics->Re); if (ferr != 1) return(1);
160  } else if (!strcmp(word,"Minf")) {
161  ferr = fscanf(in,"%lf",&physics->Minf); if (ferr != 1) return(1);
162  } else if (!strcmp(word,"gravity")) {
163  ferr = fscanf(in,"%lf",&physics->grav_x); if (ferr != 1) return(1);
164  ferr = fscanf(in,"%lf",&physics->grav_y); if (ferr != 1) return(1);
165  } else if (!strcmp(word,"rho_ref")) {
166  ferr = fscanf(in,"%lf",&physics->rho0); if (ferr != 1) return(1);
167  } else if (!strcmp(word,"p_ref")) {
168  ferr = fscanf(in,"%lf",&physics->p0); if (ferr != 1) return(1);
169  } else if (!strcmp(word,"HB")) {
170  ferr = fscanf(in,"%d",&physics->HB); if (ferr != 1) return(1);
171  if (physics->HB==3) {
172  ferr = fscanf(in,"%lf",&physics->N_bv); if (ferr != 1) return(1);
173  }
174  } else if (!strcmp(word,"R")) {
175  ferr = fscanf(in,"%lf",&physics->R); if (ferr != 1) return(1);
176  } else if (strcmp(word,"end")) {
177  char useless[_MAX_STRING_SIZE_];
178  ferr = fscanf(in,"%s",useless); if (ferr != 1) return(ferr);
179  printf("Warning: keyword %s in file \"physics.inp\" with value %s not ",word,useless);
180  printf("recognized or extraneous. Ignoring.\n");
181  }
182  }
183  } else {
184  fprintf(stderr,"Error: Illegal format in file \"physics.inp\".\n");
185  return(1);
186  }
187  }
188  fclose(in);
189  }
190 
192  IERR MPIBroadcast_double (&physics->gamma ,1 ,0,&mpi->world); CHECKERR(ierr);
193  IERR MPIBroadcast_double (&physics->Pr ,1 ,0,&mpi->world); CHECKERR(ierr);
194  IERR MPIBroadcast_double (&physics->Re ,1 ,0,&mpi->world); CHECKERR(ierr);
195  IERR MPIBroadcast_double (&physics->Minf ,1 ,0,&mpi->world); CHECKERR(ierr);
196  IERR MPIBroadcast_double (&physics->grav_x ,1 ,0,&mpi->world); CHECKERR(ierr);
197  IERR MPIBroadcast_double (&physics->grav_y ,1 ,0,&mpi->world); CHECKERR(ierr);
198  IERR MPIBroadcast_double (&physics->rho0 ,1 ,0,&mpi->world); CHECKERR(ierr);
199  IERR MPIBroadcast_double (&physics->p0 ,1 ,0,&mpi->world); CHECKERR(ierr);
200  IERR MPIBroadcast_double (&physics->R ,1 ,0,&mpi->world); CHECKERR(ierr);
201  IERR MPIBroadcast_double (&physics->N_bv ,1 ,0,&mpi->world); CHECKERR(ierr);
202  IERR MPIBroadcast_integer (&physics->HB ,1 ,0,&mpi->world); CHECKERR(ierr);
203 
204  /* Scaling the Reynolds number with the M_inf */
205  physics->Re /= physics->Minf;
206 
207  /* check that a well-balanced upwinding scheme is being used for cases with gravity */
208  if ( ((physics->grav_x != 0.0) || (physics->grav_y != 0.0))
209  && (strcmp(physics->upw_choice,_LLF_ ))
210  && (strcmp(physics->upw_choice,_RUSANOV_))
211  && (strcmp(physics->upw_choice,_ROE_ )) ) {
212  if (!mpi->rank) {
213  fprintf(stderr,"Error in NavierStokes2DInitialize: %s, %s or %s upwinding is needed for flows ",_LLF_,_ROE_,_RUSANOV_);
214  fprintf(stderr,"with gravitational forces.\n");
215  }
216  return(1);
217  }
218  /* check that solver has the correct choice of diffusion formulation */
219  if (strcmp(solver->spatial_type_par,_NC_2STAGE_) && (physics->Re > 0)) {
220  if (!mpi->rank)
221  fprintf(stderr,"Error in NavierStokes2DInitialize(): Parabolic term spatial discretization must be \"%s\"\n",_NC_2STAGE_);
222  return(1);
223  }
224 
225  /* initializing physical model-specific functions */
226 #if defined(HAVE_CUDA) && defined(CUDA_VAR_ORDERDING_AOS)
227  if (solver->use_gpu) {
228  solver->FFunction = gpuNavierStokes2DFlux;
229  solver->SFunction = gpuNavierStokes2DSource;
230  solver->UFunction = gpuNavierStokes2DModifiedSolution;
231  } else {
232 #endif
233  solver->PreStep = NavierStokes2DPreStep;
235  solver->FFunction = NavierStokes2DFlux;
241 #if defined(HAVE_CUDA) && defined(CUDA_VAR_ORDERDING_AOS)
242  }
243 #endif
244 
245 #if defined(HAVE_CUDA) && defined(CUDA_VAR_ORDERDING_AOS)
246  if (solver->use_gpu) {
247  if (!strcmp(solver->SplitHyperbolicFlux,"yes")) {
248  if (!mpi->rank) {
249  fprintf(stderr,"Error in NavierStokes2DInitialize(): Not yet implemented on GPU!");
250  }
251  return 1;
252  } else {
254  if (!strcmp(physics->upw_choice,_RUSANOV_)) solver->Upwind = gpuNavierStokes2DUpwindRusanov;
255  else {
256  if (!mpi->rank) {
257  fprintf(stderr,"Error in NavierStokes2DInitialize(): %s is not implemented on GPU. ",
258  physics->upw_choice);
259  fprintf(stderr,"Only choice is %s.\n",_RUSANOV_);
260  }
261  return 1;
262  }
263  }
264  } else {
265 #endif
266  if (!strcmp(solver->SplitHyperbolicFlux,"yes")) {
270  if (!strcmp(physics->upw_choice,_ROE_)) {
274  } else if (!strcmp(physics->upw_choice,_RF_)) {
275  solver->Upwind = NavierStokes2DUpwindRF;
278  } else if (!strcmp(physics->upw_choice,_LLF_)) {
282  } else if (!strcmp(physics->upw_choice,_RUSANOV_)) {
286  } else {
287  if (!mpi->rank) {
288  fprintf(stderr,"Error in NavierStokes2DInitialize(): %s is not a valid upwinding scheme ",
289  physics->upw_choice);
290  fprintf(stderr,"for use with split hyperbolic flux form. Use %s, %s, %s, or %s.\n",
292  }
293  return(1);
294  }
295  } else {
297  if (!strcmp(physics->upw_choice,_ROE_ )) solver->Upwind = NavierStokes2DUpwindRoe;
298  else if (!strcmp(physics->upw_choice,_RF_ )) solver->Upwind = NavierStokes2DUpwindRF;
299  else if (!strcmp(physics->upw_choice,_LLF_ )) solver->Upwind = NavierStokes2DUpwindLLF;
300  else if (!strcmp(physics->upw_choice,_SWFS_ )) solver->Upwind = NavierStokes2DUpwindSWFS;
301  else if (!strcmp(physics->upw_choice,_RUSANOV_)) solver->Upwind = NavierStokes2DUpwindRusanov;
302  else {
303  if (!mpi->rank) {
304  fprintf(stderr,"Error in NavierStokes2DInitialize(): %s is not a valid upwinding scheme. ",
305  physics->upw_choice);
306  fprintf(stderr,"Choices are %s, %s, %s, %s, and %s.\n",_ROE_,_RF_,_LLF_,_SWFS_,_RUSANOV_);
307  }
308  return(1);
309  }
310  }
311 #if defined(HAVE_CUDA) && defined(CUDA_VAR_ORDERDING_AOS)
312  }
313 #endif
314 
315  /* set the value of gamma in all the boundary objects */
316  int n;
317  DomainBoundary *boundary = (DomainBoundary*) solver->boundary;
318  for (n = 0; n < solver->nBoundaryZones; n++) boundary[n].gamma = physics->gamma;
319 
320  /* hijack the main solver's dissipation function pointer
321  * to this model's own function, since it's difficult to express
322  * the dissipation terms in the general form */
323 #if defined(HAVE_CUDA) && defined(CUDA_VAR_ORDERDING_AOS)
324  if (solver->use_gpu) {
325  solver->ParabolicFunction = gpuNavierStokes2DParabolicFunction;
326  } else {
327 #endif
329 #if defined(HAVE_CUDA) && defined(CUDA_VAR_ORDERDING_AOS)
330  }
331 #endif
332 
333  /* allocate array to hold the gravity field */
334  int *dim = solver->dim_local;
335  int ghosts = solver->ghosts;
336  int d, size = 1; for (d=0; d<_MODEL_NDIMS_; d++) size *= (dim[d] + 2*ghosts);
337  physics->grav_field_f = (double*) calloc (size, sizeof(double));
338  physics->grav_field_g = (double*) calloc (size, sizeof(double));
339  /* allocate arrays to hold the fast Jacobian for split form of the hyperbolic flux */
340  physics->fast_jac = (double*) calloc (2*size*_MODEL_NVARS_*_MODEL_NVARS_,sizeof(double));
341  physics->solution = (double*) calloc (size*_MODEL_NVARS_,sizeof(double));
342  /* initialize the gravity fields */
343  IERR NavierStokes2DGravityField(solver,mpi); CHECKERR(ierr);
344 
345 #if defined(HAVE_CUDA) && defined(CUDA_VAR_ORDERDING_AOS)
346  if (solver->use_gpu) gpuNavierStokes2DInitialize(s,m);
347 #endif
348 
349  count++;
350  return(0);
351 }
int NavierStokes2DUpwinddFRoe(double *, double *, double *, double *, double *, double *, int, void *, double)
int nvars
Definition: hypar.h:29
Containts the structures and definitions for boundary condition implementation.
#define IERR
Definition: basic.h:16
MPI related function definitions.
#define CHECKERR(ierr)
Definition: basic.h:18
int(* ParabolicFunction)(double *, double *, void *, void *, double)
Definition: hypar.h:256
char SplitHyperbolicFlux[_MAX_STRING_SIZE_]
Definition: hypar.h:92
void * boundary
Definition: hypar.h:159
int NavierStokes2DUpwindFdFRusanov(double *, double *, double *, double *, double *, double *, int, void *, double)
int NavierStokes2DUpwinddFLLF(double *, double *, double *, double *, double *, double *, int, void *, double)
int MPIBroadcast_integer(int *, int, int, void *)
Definition: MPIBroadcast.c:23
int NavierStokes2DModifiedSolution(double *, double *, int, void *, void *, double)
int NavierStokes2DStiffJacobian(double *, double *, void *, int, int, int)
int(* AveragingFunction)(double *, double *, double *, void *)
Definition: hypar.h:354
Some basic definitions and macros.
int NavierStokes2DUpwinddFRF(double *, double *, double *, double *, double *, double *, int, void *, double)
int(* FdFFunction)(double *, double *, int, void *, double)
Definition: hypar.h:286
int(* SFunction)(double *, double *, void *, void *, double)
Definition: hypar.h:317
int NavierStokes2DRightEigenvectors(double *, double *, void *, int)
int(* Upwind)(double *, double *, double *, double *, double *, double *, int, void *, double)
Definition: hypar.h:295
Structure containing the variables and function pointers defining a boundary.
int NavierStokes2DJacobian(double *, double *, 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
double * grav_field_g
int NavierStokes2DStiffFlux(double *, double *, int, 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 all solver-specific variables and functions.
Definition: hypar.h:23
int NavierStokes2DSource(double *, double *, void *, void *, double)
#define _MAX_STRING_SIZE_
Definition: basic.h:14
#define _ROE_
Definition: euler1d.h:62
int NavierStokes2DUpwindFdFRF(double *, double *, double *, double *, double *, double *, int, void *, double)
int NavierStokes2DUpwindRoe(double *, double *, double *, double *, double *, double *, int, void *, double)
int NavierStokes2DGravityField(void *, void *)
int NavierStokes2DUpwindFdFLLF(double *, double *, double *, double *, double *, double *, int, void *, double)
int NavierStokes2DUpwindRusanov(double *, double *, double *, double *, double *, double *, int, void *, double)
#define _MODEL_NDIMS_
Definition: euler1d.h:56
Contains structure definition for hypar.
#define _RUSANOV_
Definition: euler1d.h:70
MPI_Comm world
2D Navier Stokes equations (compressible flows)
int NavierStokes2DPreStep(double *, void *, void *, double)
double NavierStokes2DComputeCFL(void *, void *, double, double)
int(* PreStep)(double *, void *, void *, double)
Definition: hypar.h:339
int NavierStokes2DLeftEigenvectors(double *, double *, void *, int)
char upw_choice[_MAX_STRING_SIZE_]
int(* UpwindFdF)(double *, double *, double *, double *, double *, double *, int, void *, double)
Definition: hypar.h:307
char spatial_type_par[_MAX_STRING_SIZE_]
Definition: hypar.h:96
Structure containing variables and parameters specific to the 2D Navier Stokes equations. This structure contains the physical parameters, variables, and function pointers specific to the 2D Navier-Stokes equations.
int NavierStokes2DUpwindSWFS(double *, double *, double *, double *, double *, double *, int, void *, double)
double * grav_field_f
int NavierStokes2DUpwindLLF(double *, double *, double *, double *, double *, double *, int, void *, double)
int NavierStokes2DUpwinddFRusanov(double *, double *, double *, double *, double *, double *, int, void *, double)
int NavierStokes2DUpwindFdFRoe(double *, double *, double *, double *, double *, double *, int, void *, double)
int * dim_local
Definition: hypar.h:37
int NavierStokes2DNonStiffFlux(double *, double *, int, void *, double)
int NavierStokes2DUpwindRusanovModified(double *, double *, double *, double *, double *, double *, int, void *, double)
int(* dFFunction)(double *, double *, int, void *, double)
Definition: hypar.h:280
void * physics
Definition: hypar.h:266
int(* JFunction)(double *, double *, void *, int, int, int)
Definition: hypar.h:326
int NavierStokes2DUpwindRF(double *, double *, double *, double *, double *, double *, int, void *, double)
int NavierStokes2DParabolicFunction(double *, double *, void *, void *, double)
int(* UpwinddF)(double *, double *, double *, double *, double *, double *, int, void *, double)
Definition: hypar.h:300
int ghosts
Definition: hypar.h:52
int NavierStokes2DFlux(double *, double *, int, void *, double)
#define _SWFS_
Definition: euler1d.h:68
Structure of MPI-related variables.
int use_gpu
Definition: hypar.h:449
#define _MODEL_NVARS_
Definition: euler1d.h:58
int gpuNavierStokes2DInitialize(void *s, void *m)
int nBoundaryZones
Definition: hypar.h:157
int NavierStokes2DUpwindFdFRusanovModified(double *, double *, double *, double *, double *, double *, int, void *, double)
int NavierStokes2DRoeAverage(double *, double *, double *, void *)
int NavierStokes2DUpwinddFRusanovModified(double *, double *, double *, double *, double *, double *, int, void *, double)
Contains macros and function definitions for common array operations.
int(* GetRightEigenvectors)(double *, double *, void *, int)
Definition: hypar.h:359
#define _RF_
Definition: euler1d.h:64
int(* GetLeftEigenvectors)(double *, double *, void *, int)
Definition: hypar.h:357
int(* UFunction)(double *, double *, int, void *, void *, double)
Definition: hypar.h:321
#define _NC_2STAGE_
Definition: hypar.h:477
#define _LLF_
Definition: euler1d.h:66
int NavierStokes2DInitialize(void *s, void *m)