Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update grad.py #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 49 additions & 38 deletions python/nt_toolbox/grad.py
Original file line number Diff line number Diff line change
@@ -1,83 +1,94 @@
import numpy as np


def grad(M, bound="sym", order=1):
"""
grad - gradient, forward differences

[gx,gy] = grad(M, options);
or
g = grad(M, options);

options.bound = 'per' or 'sym'
options.order = 1 (backward differences)
= 2 (centered differences)

Works also for 3D array.
Assme that the function is evenly sampled with sampling step 1.

See also: div.

Copyright (c) Gabriel Peyre
"""
"""

ny = None
fy = None
fz = None

# retrieve number of dimensions
nbdims = np.ndim(M)


if bound == "sym":
if bound == "sym":
nx = np.shape(M)[0]

if order == 1:
fx = M[np.hstack((np.arange(1,nx),[nx-1])),:] - M
fx = M[np.hstack((np.arange(1, nx), [nx-1])), :] - M
else:
fx = (M[np.hstack((np.arange(1,nx),[nx-1])),:] - M[np.hstack(([0],np.arange(0,nx-1))),:])/2.
fx = (M[np.hstack((np.arange(1, nx), [nx-1])), :] -
M[np.hstack(([0], np.arange(0, nx-1))), :])/2.
# boundary
fx[0,:] = M[1,:]-M[0,:]
fx[nx-1,:] = M[nx-1,:]-M[nx-2,:]
fx[0, :] = M[1, :]-M[0, :]
fx[nx-1, :] = M[nx-1, :]-M[nx-2, :]

if nbdims >= 2:
ny = np.shape(M)[1]
if order == 1:
fy = M[:,np.hstack((np.arange(1,ny),[ny-1]))] - M
fy = M[:, np.hstack((np.arange(1, ny), [ny-1]))] - M
else:
fy = (M[:,np.hstack((np.arange(1,ny),[ny-1]))] - M[:,np.hstack(([0],np.arange(ny-1)))])/2.
fy = (M[:, np.hstack((np.arange(1, ny), [ny-1]))] -
M[:, np.hstack(([0], np.arange(ny-1)))])/2.
# boundary
fy[:,0] = M[:,1]-M[:,0]
fy[:,ny-1] = M[:,ny-1]-M[:,ny-2]
fy[:, 0] = M[:, 1]-M[:, 0]
fy[:, ny-1] = M[:, ny-1]-M[:, ny-2]

if nbdims >= 3:
nz = np.shape(M)[2]
if order == 1:
fz = M[:,:,np.hstack((np.arange(1,nz),[nz-1]))] - M
fz = M[:, :, np.hstack((np.arange(1, nz), [nz-1]))] - M
else:
fz = (M[:,:,np.hstack((np.arange(1,nz),[nz-1]))] - M[:,:,np.hstack(([0],np.arange(nz-1)))])/2.
fz = (M[:, :, np.hstack((np.arange(1, nz), [nz-1]))] -
M[:, :, np.hstack(([0], np.arange(nz-1)))])/2.
# boundary
fz[:,:,0] = M[:,:,1]-M[:,:,0]
fz[:,:,ny-1] = M[:,:,nz-1]-M[:,:,nz-2]
fz[:, :, 0] = M[:, :, 1]-M[:, :, 0]
fz[:, :, ny-1] = M[:, :, nz-1]-M[:, :, nz-2]
else:
nx = np.shape(M)[0]
if order == 1:
fx = M[np.hstack((np.arange(1,nx),[0])),:] - M
fx = M[np.hstack((np.arange(1, nx), [0])), :] - M
else:
fx = (M[np.hstack((np.arange(1,nx),[0])),:] - M[np.hstack(([nx-1],np.arange(nx-1))),:])/2.

fx = (M[np.hstack((np.arange(1, nx), [0])), :] -
M[np.hstack(([nx-1], np.arange(nx-1))), :])/2.

if nbdims >= 2:
ny = np.shape(M)[1]
if order == 1:
fy = M[:,np.hstack((np.arange(1,ny),[0]))] - M
fy = M[:, np.hstack((np.arange(1, ny), [0]))] - M
else:
fy = (M[:,np.hstack((np.arange(1,ny),[0]))] - M[:,np.hstack(([ny-1],np.arange(ny-1)))])/2.

fy = (M[:, np.hstack((np.arange(1, ny), [0]))] -
M[:, np.hstack(([ny-1], np.arange(ny-1)))])/2.

if nbdims >= 3:
nz = np.shape(M)[2]
if order == 1:
fz = M[:,:,np.hstack((np.arange(1,nz),[0]))] - M
fz = M[:, :, np.hstack((np.arange(1, nz), [0]))] - M
else:
fz = (M[:,:,np.hstack((np.arange(1,nz),[0]))] - M[:,:,np.hstack(([nz-1],np.arange(nz-1)))])/2.

if nbdims==2:
fx = np.concatenate((fx[:,:,np.newaxis],fy[:,:,np.newaxis]), axis=2)
elif nbdims==3:
fx = np.concatenate((fx[:,:,:,np.newaxis],fy[:,:,:,np.newaxis],fz[:,:,:,np.newaxis]),axis=3)

return fx
fz = (M[:, :, np.hstack((np.arange(1, nz), [0]))] -
M[:, :, np.hstack(([nz-1], np.arange(nz-1)))])/2.

if nbdims == 2:
fx = np.concatenate(
(fx[:, :, np.newaxis], fy[:, :, np.newaxis]), axis=2)
elif nbdims == 3:
fx = np.concatenate(
(fx[:, :, :, np.newaxis], fy[:, :, :, np.newaxis], fz[:, :, :, np.newaxis]), axis=3)

return fx