-
Notifications
You must be signed in to change notification settings - Fork 17
/
differentiation.py
60 lines (40 loc) · 1.36 KB
/
differentiation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import taichi as ti
@ti.func
def sample(field, i, j):
i = ti.max(0, ti.min(field.shape[0] - 1, i))
j = ti.max(0, ti.min(field.shape[1] - 1, j))
idx = ti.Vector([int(i), int(j)])
return field[idx]
@ti.func
def sign(x):
return -1.0 if x < 0.0 else 1.0
@ti.func
def fdiff_x(field, i, j, dx):
"""Forward Difference x"""
return (sample(field, i + 1, j) - sample(field, i, j)) / dx
@ti.func
def fdiff_y(field, i, j, dx):
"""Forward Difference y"""
return (sample(field, i, j + 1) - sample(field, i, j)) / dx
@ti.func
def bdiff_x(field, i, j, dx):
"""Backward Difference x"""
return (sample(field, i, j) - sample(field, i - 1, j)) / dx
@ti.func
def bdiff_y(field, i, j, dx):
"""Backward Difference y"""
return (sample(field, i, j) - sample(field, i, j - 1)) / dx
@ti.func
def diff_x(field, i, j, dx):
"""Central Difference x"""
return 0.5 * (sample(field, i + 1, j) - sample(field, i - 1, j)) / dx
@ti.func
def diff_y(field, i, j, dx):
"""Central Difference y"""
return 0.5 * (sample(field, i, j + 1) - sample(field, i, j - 1)) / dx
@ti.func
def diff2_x(field, i, j, dx):
return (sample(field, i + 1, j) - 2.0 * sample(field, i, j) + sample(field, i - 1, j)) / dx**2
@ti.func
def diff2_y(field, i, j, dx):
return (sample(field, i, j + 1) - 2.0 * sample(field, i, j) + sample(field, i, j - 1)) / dx**2