-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functional_Derivative.py
66 lines (51 loc) · 1.21 KB
/
Functional_Derivative.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
from sympy import *
from sympy.abc import *
from IPython.display import display
init_printing(use_latex='mathjax')
u = Function('u')(x,y,z)
u0 = Function('u0')(x,y,z)
q = Function('q')(x,y,z)
q0 = Function('q0')(x,y,z)
f = Function('f')(x,y,z)
lamd = Function('lambda')(x,y,z)
reg_term = 0.1*0.5*(q-q0)*(q-q0)
def grad(f):
g = []
for i in f.args:
g.append(f.diff(i))
return g
def dot(v1, v2):
r = 0
for idx in range(len(v1)):
r += v1[idx]*v2[idx]
return r
def func_diff(F, f, df):
alpha = Symbol('alpha')
F = F.subs(f, f+alpha*df)
dF = F.diff(alpha)
return simplify(dF.subs(alpha, 0))
def func_grad(F, xs, dxs):
G = []
for i in range(len(xs)):
G.append(func_diff(F, xs[i], dxs[i]))
return G
L = 0.5 * (u-u0) * (u-u0) + reg_term + q*dot(grad(u), grad(lamd)) - f*lamd
print 'L='
display(L)
phi = Function('\phi')(x,y,z)
psi = Function('\psi')(x,y,z)
chi = Function('\chi')(x,y,z)
xs = (u, lamd, q)
dxs = (phi, psi, chi)
Lx = func_grad(L, xs, dxs)
print '(Lu, Llamd, Lq)='
display(Matrix(Lx))
du = Function('\delta u')(x,y,z)
dl = Function('\delta \lambda')(x,y,z)
dq = Function('\delta q')(x,y,z)
dxs2 = (du, dl, dq)
Lxx = []
for Lxi in Lx:
Lxx.append(func_grad(Lxi, xs, dxs2))
Lxx = Matrix(Lxx)
display(Lxx)