HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
FirstDerivativeFirstOrder.c File Reference

First order approximation to the first derivative. More...

#include <stdio.h>
#include <stdlib.h>
#include <basic.h>
#include <mathfunctions.h>
#include <arrayfunctions.h>
#include <firstderivative.h>
#include <mpivars.h>
#include <hypar.h>

Go to the source code of this file.

Typedefs

typedef MPIVariables MPIContext
 
typedef HyPar SolverContext
 

Functions

int FirstDerivativeFirstOrder (double *Df, double *f, int dir, int bias, void *s, void *m)
 

Detailed Description

First order approximation to the first derivative.

Author
Debojyoti Ghosh

Definition in file FirstDerivativeFirstOrder.c.

Typedef Documentation

Definition at line 15 of file FirstDerivativeFirstOrder.c.

Definition at line 16 of file FirstDerivativeFirstOrder.c.

Function Documentation

int FirstDerivativeFirstOrder ( double *  Df,
double *  f,
int  dir,
int  bias,
void *  s,
void *  m 
)

Computes the first-order finite-difference approximation to the first derivative (Note: not divided by the grid spacing):

\begin{equation} \left(\partial f\right)_i = \left\{ \begin{array}{ll} f_{i+1} - f_i & {\rm bias} = 1 \\ f_i - f_{i-1} & {\rm bias} = -1 \end{array}\right. \end{equation}

where \(i\) is the grid index along the spatial dimension of the derivative.

Notes:

  • The first derivative is computed at the grid points or the cell centers.
  • The first derivative is computed at the ghost points too. Thus, biased schemes are used at and near the boundaries.
  • Df and f are 1D arrays containing the function and its computed derivatives on a multi- dimensional grid. The derivative along the specified dimension dir is computed by looping through all grid lines along dir.
Parameters
DfArray to hold the computed first derivative (with ghost points)
fArray containing the grid point function values whose first derivative is to be computed (with ghost points)
dirThe spatial dimension along which the derivative is computed
biasForward or backward differencing for non-central finite-difference schemes (-1: backward, 1: forward)
sSolver object of type SolverContext
mMPI object of type MPIContext

Definition at line 37 of file FirstDerivativeFirstOrder.c.

47 {
48  SolverContext *solver = (SolverContext*) s;
49  int i, j, v;
50 
51  int ghosts = solver->ghosts;
52  int ndims = solver->ndims;
53  int nvars = solver->nvars;
54  int *dim = solver->dim_local;
55 
56 
57  if ((!Df) || (!f)) {
58  fprintf(stderr, "Error in FirstDerivativeSecondOrder(): input arrays not allocated.\n");
59  return(1);
60  }
61 
62  /* create index and bounds for the outer loop, i.e., to loop over all 1D lines along
63  dimension "dir" */
64  int indexC[ndims], index_outer[ndims], bounds_outer[ndims];
65  _ArrayCopy1D_(dim,bounds_outer,ndims); bounds_outer[dir] = 1;
66  int N_outer; _ArrayProduct1D_(bounds_outer,ndims,N_outer);
67 
68 #pragma omp parallel for schedule(auto) default(shared) private(i,j,v,index_outer,indexC)
69  for (j=0; j<N_outer; j++) {
70  _ArrayIndexnD_(ndims,j,bounds_outer,index_outer,0);
71  _ArrayCopy1D_(index_outer,indexC,ndims);
72  /* left boundary */
73  for (i = -ghosts; i < -ghosts+1; i++) {
74  int qC, qR;
75  indexC[dir] = i ; _ArrayIndex1D_(ndims,dim,indexC,ghosts,qC );
76  indexC[dir] = i+1; _ArrayIndex1D_(ndims,dim,indexC,ghosts,qR );
77  for (v=0; v<nvars; v++) Df[qC*nvars+v] = f[qR*nvars+v]-f[qC*nvars+v];
78  }
79  /* interior */
80  for (i = -ghosts+1; i < dim[dir]+ghosts-1; i++) {
81  int qC, qL, qR;
82  indexC[dir] = i ; _ArrayIndex1D_(ndims,dim,indexC,ghosts,qC );
83  indexC[dir] = i-1; _ArrayIndex1D_(ndims,dim,indexC,ghosts,qL);
84  indexC[dir] = i+1; _ArrayIndex1D_(ndims,dim,indexC,ghosts,qR);
85  for (v=0; v<nvars; v++) Df[qC*nvars+v] = max(bias,0)*f[qR*nvars+v]-bias*f[qC*nvars+v]+min(bias,0)*f[qL*nvars+v];
86  }
87  /* right boundary */
88  for (i = dim[dir]+ghosts-1; i < dim[dir]+ghosts; i++) {
89  int qL, qC;
90  indexC[dir] = i-1; _ArrayIndex1D_(ndims,dim,indexC,ghosts,qL );
91  indexC[dir] = i ; _ArrayIndex1D_(ndims,dim,indexC,ghosts,qC );
92  for (v=0; v<nvars; v++) Df[qC*nvars+v] = f[qC*nvars+v]-f[qL*nvars+v];
93  }
94  }
95 
96  return(0);
97 }
#define _ArrayIndexnD_(N, index, imax, i, ghost)
int * dim_local
Definition: hypar.h:37
int ghosts
Definition: hypar.h:52
#define _ArrayIndex1D_(N, imax, i, ghost, index)
#define _ArrayCopy1D_(x, y, size)
int nvars
Definition: hypar.h:29
int ndims
Definition: hypar.h:26
#define _ArrayProduct1D_(x, size, p)
#define max(a, b)
Definition: math_ops.h:18
Structure containing all solver-specific variables and functions.
Definition: hypar.h:23
#define min(a, b)
Definition: math_ops.h:14