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

Find grid point indices corresponding to a spatial interval. More...

#include <mathfunctions.h>

Go to the source code of this file.

Functions

void FindInterval (double a, double b, double *x, int N, int *imin, int *imax)
 

Detailed Description

Find grid point indices corresponding to a spatial interval.

Author
Debojyoti Ghosh

Definition in file FindInterval.c.

Function Documentation

◆ FindInterval()

void FindInterval ( double  a,
double  b,
double *  x,
int  N,
int *  imin,
int *  imax 
)

Given an interval \(\left[a,b\right], a\leq b\), find grid indices imin and imax, such that

\begin{align} imin &= \min\ i\ {\rm satisfying}\ x_i \geq a\\ imax &= \max\ i\ {\rm satisfying}\ x_i \leq b \end{align}

where \(\left\{x_i; 0\leq i < N , x_i < x_{i+1} \forall i \right\}\) represents a 1-dimensional grid.

Note: This function handles 1-dimensional intervals and grids only.

Parameters
aLower bound of interval
bUpper bound of interval
xArray of spatial coordinates representing a grid
NNumber of grid points / size of x
iminLowest grid index within [a,b]
imaxHighest grid index within [a,b]

Definition at line 19 of file FindInterval.c.

27 {
28  int i;
29  *imax = -1;
30  *imin = N;
31 
32  double min_dx = x[1] - x[0];
33  for (i = 2; i < N; i++) {
34  double dx = x[i] - x[i-1];
35  if (dx < min_dx) min_dx = dx;
36  }
37  double tol = 1e-10 * min_dx;
38 
39  for (i = 0; i < N; i++) {
40  if (x[i] <= (b+tol)) *imax = i+1;
41  }
42  for (i = N-1; i > -1; i--) {
43  if (x[i] >= (a-tol)) *imin = i;
44  }
45 
46  return;
47 }