pydata / pydata/xarray

boundary conditions for differentiate()

Open
#6,493 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

Is your feature request related to a problem?

I need to take centered finite difference of data of length N along the dimension 'X', with boundary conditions (BCs) specified in flexible ways. Before this, we need to pad data with BCs (length becoming N+2) so that the indicing will not be out-of-range.

Commonly used BCs are:

  1. fixed - fill with fixed values so derivatives at BCs are (BC - data[-1])/dx and (data[0] - BC)/dx;
  2. extend - fill BCs with second outer-most values so that derivatives at BCs are exactly zero;
  3. periodic - fill BCs cyclic so that the derivatives are also cyclic.
Describe the solution you'd like

The implementation of differentiate('X') would be like:

# padded with BCs into N+2
data_pad = pad_BCs(data, type='periodic')

# it is safe to take finite difference
for i in range(len(data))
    diff[i] = data_pad [i+1] - data_pad [i-1]

The pad_BCs function could be easily implemented with np.pad() function.

Then we can call:

data.differentiate('X', BCs='periodic')

We may also specify different kind of BCs at the two boundaries:

data.differentiate('X', BCs=['extend', 'fixed'], fill_values=0)
Describe alternatives you've considered

No response

Additional context

I am not clear how differentiate() is implemented and just want to know if this can be implemented in a straightforward way.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue identifies differentiate() as the entry point; start by tracing its current dimension-wise finite-difference implementation. Review the surrounding tests, then define coverage for fixed, extend, periodic, and asymmetric boundary conditions, including fill values. Done means the requested BC forms work without out-of-range indexing and their boundary derivatives match the stated behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.