HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
FirstDerivativeSecondOrder.c File Reference

Second order finite-difference approximation to first derivative. More...

#include <stdio.h>
#include <stdlib.h>
#include <basic.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 FirstDerivativeSecondOrderCentral (double *Df, double *f, int dir, int bias, void *s, void *m)
 

Detailed Description

Second order finite-difference approximation to first derivative.

Author
Debojyoti Ghosh

Definition in file FirstDerivativeSecondOrder.c.

Typedef Documentation

◆ MPIContext

Definition at line 14 of file FirstDerivativeSecondOrder.c.

◆ SolverContext

Definition at line 15 of file FirstDerivativeSecondOrder.c.

Function Documentation

◆ FirstDerivativeSecondOrderCentral()

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

Computes the second-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} \frac{1}{2}\left(-3f_i+4f_{i+1}-f_{i+2}\right) & i = -g \\ \frac{1}{2}\left( f_{i+1} - f_{i-1} \right) & -g+1 \leq i \leq N+g-2 \\ \frac{1}{2}\left( f_{i-2} -4f_{i-1}+3f_i \right) & i = N+g-1 \end{array}\right. \end{equation}

where \(i\) is the grid index along the spatial dimension of the derivative, \(g\) is the number of ghost points, and \(N\) is the number of grid points (not including the ghost points) in 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 36 of file FirstDerivativeSecondOrder.c.

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