-
Notifications
You must be signed in to change notification settings - Fork 1
/
chemotaxis_generate_pattern_FCT.py
215 lines (167 loc) · 7.52 KB
/
chemotaxis_generate_pattern_FCT.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from pathlib import Path
from dolfin import *
import numpy as np
import matplotlib.pyplot as plt
from scipy.sparse import diags, block_diag, vstack, hstack, csr_matrix, lil_matrix, spdiags, triu, tril
from scipy.sparse.linalg import spsolve, gmres, minres, LinearOperator
from timeit import default_timer as timer
from datetime import timedelta
from scipy.integrate import simps
from helpers import *
import mimura_data_helpers
# ---------------------------------------------------------------------------
### PDE-constrained optimisation problem for the chemotaxis system
### with Flux-corrected transport method, norms over L^2
# min_{m,f,c} ( ||m(T)-\hat{m}||^2 + ||f(T)-\hat{f}||^2 + beta*||c||^2) / 2
# subject to:
# dm/dt - Dm*grad^2(m) + div(chi*m*grad(f)) = m(4-m) in Ωx[0,T]
# df/dt - Df*grad^2(f)) + delta*f = c*m in Ωx[0,T]
# zero Neumann BC for m,f on ∂Ωx[0,T]
# given initial conditions for m,f in Ω
# Dm, Df, chi are parameters
# ---------------------------------------------------------------------------
## Define the parameters
a1 = 0
a2 = 10
deltax = 0.05*2
intervals_line = round((a2 - a1) / deltax)
delta = 1
Dm = 0.05
Df = 0.05
chi = 5 #0.17
gamma = 0.5#2
t0 = 0
dt = 0.1
T = 50 #5*dt #50
num_steps = round((T-t0)/dt)
# Initialize a square mesh
mesh = RectangleMesh(Point(a1, a1), Point(a2, a2), intervals_line, intervals_line)
V = FunctionSpace(mesh, 'CG', 1)
nodes = V.dim()
sqnodes = round(np.sqrt(nodes))
u = TrialFunction(V)
v = TestFunction(V)
vertextodof = vertex_to_dof_map(V)
boundary_nodes, boundary_nodes_dof = generate_boundary_nodes(nodes, vertextodof)
mesh.init(0, 1)
dof_neighbors = find_node_neighbours(mesh, nodes, vertextodof)
show_plots = True
out_folder_name = f"chtx_chi{chi}_simplfeathers_dx{deltax}_cos"
if not Path(out_folder_name).exists():
Path(out_folder_name).mkdir(parents=True)
###############################################################################
################### Define the stationary matrices ###########################
###############################################################################
# Mass matrix
M = assemble_sparse_lil(u * v * dx)
# Row-lumped mass matrix
M_Lump = row_lump(M, nodes)
# Stiffness matrix
Ad = assemble_sparse(dot(grad(u), grad(v)) * dx)
# System matrix: equation for f
Mat_f = M + dt * (Df * Ad + delta * M)
zeros = np.zeros(nodes)
###############################################################################
######################## Initial conditions for m,f ###########################
###############################################################################
m0_orig = mimura_data_helpers.m_initial_condition(a1, a2, deltax).reshape(nodes)
f0_orig = m0_orig #/ delta #m0_orig #1/32 * np.ones(nodes)
m0 = reorder_vector_to_dof_time(m0_orig, 1, nodes, vertextodof)
f0 = reorder_vector_to_dof_time(f0_orig, 1, nodes, vertextodof)
###############################################################################
########################### Initial guesses for GD ############################
###############################################################################
vec_length = (num_steps + 1) * nodes # include zero and final time
zeros_nt = np.zeros(vec_length)
mk = np.zeros(vec_length)
fk = np.zeros(vec_length)
mk[:nodes] = m0
fk[:nodes] = f0
t = 0
for i in range(1, num_steps + 1): # solve for fk(t_{n+1}), mk(t_{n+1})
start = i * nodes
end = (i + 1) * nodes
t += dt
if i % 50 == 0:
print('t = ', round(t, 4))
m_n = mk[start - nodes : start] # mk(t_n)
m_n_fun = vec_to_function(m_n,V)
f_n_fun = vec_to_function(fk[start - nodes : start],V)
f_rhs = np.asarray(assemble(f_n_fun * v * dx + dt * gamma * m_n_fun * v * dx))
fk[start : end] = spsolve(Mat_f, f_rhs)
f_np1_fun = vec_to_function(fk[start : end], V)
A_m = mimura_data_helpers.mat_chtx_m(f_np1_fun, m_n_fun, Dm, chi, u, v)
m_rhs = np.zeros(nodes)
mk[start : end] = FCT_alg(A_m, m_rhs, m_n, dt, nodes, M, M_Lump, dof_neighbors)
# mk[start : end] = spsolve(M - dt*A_m, M@m_n + dt*m_rhs)
m_re = reorder_vector_from_dof_time(mk[start : end], 1, nodes, vertextodof)
f_re = reorder_vector_from_dof_time(fk[start : end], 1, nodes, vertextodof)
if show_plots is True and i % 5 == 0:
fig2 = plt.figure(figsize = (10, 5))
fig2.tight_layout(pad = 3.0)
ax2 = plt.subplot(1,2,1)
im1 = plt.imshow(m_re.reshape((sqnodes, sqnodes)))#, vmin = min_m, vmax = max_m)
fig2.colorbar(im1)
plt.title(f'Computed state $m$ at t = {round(t,5)}')
ax2 = plt.subplot(1,2,2)
im2 = plt.imshow(f_re.reshape((sqnodes, sqnodes)))#, vmin = min_f, vmax = max_f)
fig2.colorbar(im2)
plt.title(f'Computed state $f$ at t = {round(t,5)}')
plt.show()
mk.tofile(out_folder_name + f'/chtx_m.csv', sep = ',')
fk.tofile(out_folder_name + f'/chtx_f.csv', sep = ',')
###############################################################################
# # Mapping to order the solution vectors based on vertex indices
# mk_re = reorder_vector_from_dof_time(mk, num_steps + 1, nodes, vertextodof)
# fk_re = reorder_vector_from_dof_time(fk, num_steps + 1, nodes, vertextodof)
# min_m = np.amin(mk)
# min_f = np.amin(fk)
# max_m = np.amax(mk)
# max_f = np.amax(fk)
# for i in range(num_steps):
# start_st = (i+1) * nodes
# end_st = (i+2) * nodes
# t_st = (i+1) * dt
# m_re = mk_re[start_st : end_st]
# f_re = fk_re[start_st : end_st]
# m_re = m_re.reshape((sqnodes, sqnodes))
# f_re = f_re.reshape((sqnodes, sqnodes))
# # # save target states:
# # if t_st == 14 or t_st == 30:
# # print('here')
# # plt.imshow(m_re, cmap='gray_r')
# # plt.axis('off')
# # filename = f'data/mimura_tsujikawa_t' + str(t_st) + '_m.png'
# # plt.savefig(filename)
# # plt.close()
# # plt.imshow(f_re, cmap='gray_r')
# # plt.axis('off')
# # filename = f'data/mimura_tsujikawa_t' + str(t_st) + '_f.png'
# # plt.savefig(filename)
# # plt.close()
# # m_dof = mk[start_st : end_st]
# # f_dof = fk[start_st : end_st]
# # # m_dof.tofile('data/mimura_tsujikawa_t' + str(t_st) + '_m.csv', sep = ',')
# # # f_dof.tofile('data/mimura_tsujikawa_t' + str(t_st) + '_f.csv', sep = ',')
# # print('At t=', t_st)
# # print(f'm, from {np.amin(m_re)} to {np.amax(m_re)}')
# # print(f'f, from {np.amin(f_re)} to {np.amax(f_re)}')
# if show_plots is True and i % 20 == 0:
# fig2 = plt.figure(figsize = (10, 5))
# fig2.tight_layout(pad = 3.0)
# ax2 = plt.subplot(1,2,1)
# im1 = plt.imshow(m_re, vmin = min_m, vmax = max_m, cmap='gray_r')
# fig2.colorbar(im1)
# plt.title(f'Computed state $m$ at t = {round(t_st,5)}')
# ax2 = plt.subplot(1,2,2)
# im2 = plt.imshow(f_re, vmin = min_f, vmax = max_f, cmap='gray_r')
# fig2.colorbar(im2)
# plt.title(f'Computed state $f$ at t = {round(t_st,5)}')
# plt.show()
# # filename = f'mimura_FCT_state/plot_{i:03}.png' # e.g., plot_001.png, plot_002.png, etc.
# # plt.savefig(filename)
# # plt.close()
# print('------------------------------------------------------')
print(f'{T=}, {dt=}, {deltax=}, {chi=}, {Dm=}, {Df=}')
mk.tofile(out_folder_name + f'/chtx_m.csv', sep = ',')
fk.tofile(out_folder_name + f'/chtx_f.csv', sep = ',')