question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

boundary conditions for differentiate()

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
dcheriancommented, Apr 18, 2022

+1 for xgcm.

On the xarray side, I think we should just recommend composing pad with diff or differentiate. We’ll need to add a “extrapolate” option for the padded coordinate variables for this to work.

1reaction
jbuseckecommented, Apr 26, 2022

yes all of the grid methods (grid.diff etc) are now internally using grid_ufuncs. The axis methods are still going through the old code path, but will be deprecated soon! Please let us know how you get along with the new functionality, we are very curious for user feedback!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Differential Equations - Boundary Value Problems
With boundary value problems we will have a differential equation and we will specify the function and/or derivatives at different points, which ...
Read more >
Finite Difference Methods for Boundary Value Problems
Learn how to handle different boundary conditions. (). Finite Differences ... Compute error when solution is known. (). Finite Differences.
Read more >
Boundary value problem - Wikipedia
In mathematics, in the field of differential equations, a boundary value problem is a differential equation together with a set of additional constraints, ......
Read more >
Stable Boundary Conditions and Difference Schemes ... - jstor
STABLE BOUNDARY CONDITIONS AND DIFFERENCE SCHEMES. FOR NAVIER-STOKES EQUATIONS*. PRAVIR DUTTt. Abstract. The Navier-Stokes equations at high Reynolds ...
Read more >
Answered: Differentiate the three possible types… | bartleby
MathCalculusDifferentiate the three possible types of boundary conditions that can be used for second-order partial differential equations, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found