-
Notifications
You must be signed in to change notification settings - Fork 0
/
derivatives.py
83 lines (60 loc) · 2.44 KB
/
derivatives.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from vector_field import *
about.hermitianize.off()
def Nabla_discrete(k_space,s_space=None,kfield=None):
"""
This function returns the difference operator represented in Fourier
space.
"""
if(about.hermitianize.status):
print 'It is not recommended to calculate derivatives in'
print 'Fourier space while hermitianize is on.'
print 'Please type \'about.hermitianize.off()\' to switch off hermitianization. '
if(kfield is None):
kfield = position_field(k_space)
if(s_space is None):
s_space = k_space.get_codomain()
Ndim = k_space.naxes()
basis_vectors = [np.zeros(Ndim) for ii in range(Ndim) ]
for ii in range(Ndim):
basis_vectors[ii][ii] = s_space.vol[ii]
Nabla = [exp((0.+1.j)*2*pi*kfield.vec_dot(basis_vectors[ii]))-1. for ii in range(Ndim)]
val = np.array([Nabla[ii].val/s_space.vol[ii] for ii in range(len(s_space.vol))])
Nabla = vector_field(k_space,Ndim=k_space.naxes(),target=s_space,val=val)
return Nabla
def Nabla_continuous(k_space,s_space=None,kfield=None):
"""
This function returns the differential operator represented in Fourier
space.
i 2 \pi \vec{k}
"""
if(about.hermitianize.status):
print 'It is not recommended to calculate derivatives in'
print 'Fourier space while hermitianize is on.'
print 'Please type \'about.hermitianize.off()\' to switch off hermitianization. '
if(kfield is None):
kfield = position_field(k_space)
if(s_space is None):
s_space = k_space.get_codomain()
Nabla = (0.+1.j)*2*pi*kfield
return Nabla
def curl(x,Nabla):
"""
This function returns the curl of a field x.
x needs to be a vector field.
k_Nabla needs to be the Nabla operator in Fourier representation.
"""
return (Nabla.cross(x.transform())).transform()
def div(x,Nabla):
"""
This function returns the divergence of a field x.
x needs to be a vector field.
k_Nabla needs to be the Nabla operator in Fourier representation.
"""
return (Nabla.vec_dot(x.transform())).transform()
def grad(x,Nabla):
"""
This function returns the gradient of a field x.
x needs to be a scalar field.
k_Nabla needs to be the Nabla operator in Fourier representation.
"""
return (Nabla*(x.transform())).transform()