-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-inverse3D.py
177 lines (128 loc) · 4.12 KB
/
test-inverse3D.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from dolfin import *
from fenics_adjoint import *
from numpy.random import rand
class DiffCoeff(Expression):
def eval_cell(self, values, x, ufl_cell):
if self.mf[ufl_cell.index]==1:
values[0] = self.Dc
elif self.mf[ufl_cell.index]==2:
values[0] = self.Dg
else:
values[0] = self.Dw
class DerivDcDiffCoeff(Expression):
def eval_cell(self, values, x,ufl_cell):
if self.mf[ufl_cell.index]==1:
values[0] = 1.0
elif self.mf[ufl_cell.index]==2:
values[0] = 0.0
else:
values[0] = 0.0
class DerivDgDiffCoeff(Expression):
def eval_cell(self, values, x,ufl_cell):
if self.mf[ufl_cell.index]==1:
values[0] = 0.0
elif self.mf[ufl_cell.index]==2:
values[0] = 1.0
else:
values[0] = 0.0
class DerivDwDiffCoeff(Expression):
def eval_cell(self, values, x,ufl_cell):
if self.mf[ufl_cell.index]==1:
values[0] = 0.0
elif self.mf[ufl_cell.index]==2:
values[0] = 0.0
else:
values[0] = 1.0
mesh = Mesh("coarse_mesh.xml")
domains = MeshFunction('size_t',mesh,"coarse_sub_corrected.xml")
boundaries = FacetFunction("size_t", mesh )
boundaries.set_all(0)
D = mesh.topology().dim()
mesh.init(D-1,D) # Build connectivity between facets and cells
for f in facets(mesh):
if len(f.entities(D))==1 :
boundaries.array()[f.index()]= domains[int(f.entities(D))]
#plot(boundaries, interactive=True)
V = FunctionSpace(mesh, "Lagrange", 1)
u = TrialFunction(V)
v = TestFunction(V)
U = Function(V)
U_prev = Function(V)
U_noise = Function(V)
g = Function(V, name="Control")
T = 1.0
t = 0
dt_val = 0.1
dt = Constant(dt_val)
D = DiffCoeff(degree=1)
D.mf= domains
D.Dc = Constant(1000)
D.Dg = Constant(1)
D.Dw = Constant(2)
D.user_defined_derivatives = {D.Dc: DerivDcDiffCoeff(degree=1),
D.Dg: DerivDgDiffCoeff(degree=1),
D.Dw: DerivDwDiffCoeff(degree=1), }
ctrls = [Control(D.Dc), Control(D.Dg), Control(D.Dw)]
D_proj = project(D, V)
# plot(c_proj)
a = u * v * dx + dt * D * inner(grad(u), grad(v)) * dx
L = U_prev * v * dx
# A = assemble(a)
bc = DirichletBC(V,Constant(1.0),boundaries,1)
#bc = DirichletBC(V,g,"on_boundary")
write_observations = False
if write_observations:
observations = HDF5File(mpi_comm_world(), "U.xdmf", "w")
else:
observations = HDF5File(mpi_comm_world(), "U.xdmf", "r")
obs_func = Function(V)
J = 0
while t <= T:
solve(a == L, U, bc)
U_prev.assign(U)
# plot(U)
t += dt_val
print("Time ", t)
# Write observation
if write_observations:
U_noise .vector()[:] = U.vector()[:] #+ 0.05*rand(U.vector().size())
observations.write(U_noise, str(t))
else:
try :
observations.read(obs_func, str(t))
J += assemble((U - obs_func) ** 2 * dx)
except:
print "Error"
if write_observations:
exit()
print("Functional value:", J)
print type(J)
print("Computing gradient")
Jhat = ReducedFunctional(J, ctrls)
# exit()
m = [Constant(1000), Constant(2), Constant(2)]
U.vector()[:] = 0
U_prev.vector()[:] = 0
j = Jhat(m)
print("Functional value at the start: {}".format(j))
m = minimize(Jhat,'CG', options={"maxiter": 50})
j = Jhat(m)
print("Functional value after optimization: {}".format(j))
#for i in range(10):
# Evaluate forward model at new control values
# j = Jhat(m)
# print("Functional value at iteration {}: {}".format(i, j))
# Compute gradient
# dJdm = compute_gradient(J, ctrls)
# Update control values:
# alpha = 0.1
# m = [Constant(m[0] - alpha / dJdm[0]),
# Constant(m[1] - alpha / dJdm[1]),
# Constant(m[2] - alpha / dJdm[2])]
# print([float(mm) for mm in m])
#exit()
print("Running Taylor test")
from IPython import embed;
embed()
h = [Constant(10), Constant(1), Constant(1)]
taylor_test_multiple(Jhat, m, h)