Magnitude gradient along flowlines


In [1]:
import math

In [2]:
import numpy as np

In [3]:
from pygsf.spatial.rasters.fields import *

Test 1


In [4]:
fx = np.array([
    [1,1,1],
    [1,1,1],
    [1,1,1]
])

In [5]:
fx


Out[5]:
array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]])

In [6]:
fy = np.array([
    [2,2,2],
    [2,2,2],
    [2,2,2]
])

In [7]:
magn = magnitude(fx, fy)

In [8]:
magn


Out[8]:
array([[2.23606798, 2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798]])

In [9]:
sqrt(1 + 2**2)


Out[9]:
2.23606797749979

In [10]:
oriens_d = orients_d(fx, fy)

In [11]:
oriens_d


Out[11]:
array([[26.56505118, 26.56505118, 26.56505118],
       [26.56505118, 26.56505118, 26.56505118],
       [26.56505118, 26.56505118, 26.56505118]])

In [12]:
degrees(math.atan(0.5))


Out[12]:
26.56505117707799

In [13]:
magn_grad_along_flowlines(fx, fy, 10, 10)


Out[13]:
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])

The previous result is correct.

Test 2


In [14]:
fx = np.array([
    [1,2,3],
    [1,2,3],
    [1,2,3]
])

In [15]:
fy = np.array([
    [0,0,0],
    [0,0,0],
    [0,0,0]
])

In [16]:
oriens_d = orients_d(fx, fy)

In [17]:
oriens_d


Out[17]:
array([[90., 90., 90.],
       [90., 90., 90.],
       [90., 90., 90.]])

In [18]:
magnitude(fx, fy)


Out[18]:
array([[1., 2., 3.],
       [1., 2., 3.],
       [1., 2., 3.]])

In [19]:
np.sqrt(fx**2 + fy**2)


Out[19]:
array([[1., 2., 3.],
       [1., 2., 3.],
       [1., 2., 3.]])

In [20]:
magn_grad_along_flowlines(
        fld_x=fx,
        fld_y=fy,
        cell_size_x=10,
        cell_size_y=10)


Out[20]:
array([[0.1, 0.1, 0.1],
       [0.1, 0.1, 0.1],
       [0.1, 0.1, 0.1]])

In [21]:
orien_rad = orients_r(fx, fy)

In [22]:
dm_dx, dm_dy = magn_grads(
    fld_x=fx,
    fld_y=fy,
    dir_cell_sizes=[1, 1])

In [23]:
dm_dx


Out[23]:
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

In [24]:
dm_dy


Out[24]:
array([[-0., -0., -0.],
       [-0., -0., -0.],
       [-0., -0., -0.]])

In [25]:
velocity_gradient = dm_dx * np.sin(orien_rad) + dm_dy * np.cos(orien_rad)

In [26]:
velocity_gradient


Out[26]:
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

Results correct.

Test 3


In [27]:
fx = np.array([
    [1,2,3],
    [2,3,4],
    [3,4,5]
])

In [28]:
fy = -fx

In [29]:
magn = magnitude(fx, fy)

In [30]:
magn


Out[30]:
array([[1.41421356, 2.82842712, 4.24264069],
       [2.82842712, 4.24264069, 5.65685425],
       [4.24264069, 5.65685425, 7.07106781]])

In [31]:
oriens_d = orients_d(fx, fy)

In [32]:
oriens_d


Out[32]:
array([[135., 135., 135.],
       [135., 135., 135.],
       [135., 135., 135.]])

In [33]:
magn_grad_along_flowlines(
        fld_x=fx,
        fld_y=fy,
        cell_size_x=1,
        cell_size_y=1)


Out[33]:
array([[2., 2., 2.],
       [2., 2., 2.],
       [2., 2., 2.]])

Cell (0,0) vs. cell (1,1):

sqrt(2) vs. 3 sqrt(2)

dist = sqrt(2)

gradient = (3sqrt(2) - sqrt(2))/sqrt(2) = 2

Result is correct.


In [34]:
magn_grad_along_flowlines(
        fld_x=fx,
        fld_y=fy,
        cell_size_x=10,
        cell_size_y=10)


Out[34]:
array([[0.2, 0.2, 0.2],
       [0.2, 0.2, 0.2],
       [0.2, 0.2, 0.2]])

Result is correct.