-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfive_pde_plot.py
More file actions
292 lines (243 loc) · 10.7 KB
/
five_pde_plot.py
File metadata and controls
292 lines (243 loc) · 10.7 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 12 15:09:57 2025
@author: 44788
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
chemokine_present = True
flow_direction = 'NEG'
steady_state = False
# Define spatial step and domain.
x_0 = 0 # x_0 : Left boundary x
x_1 = 1 # x_1 : Right boundary x
dx = 0.01 # dx: Spatial step.
Dx = int((x_1 - x_0 + dx) / dx) # Dx: Number of spatial steps.
x = np.linspace(x_0, x_1, Dx) # x : Spatial mesh.
t_0 = 0 # t_0 : Initial time
if steady_state == True:
t_1 = 30 # t_1 : Final time
else:
t_1 = 1800/(1.44e4) # dt: Time step.
dt = t_1/100
Dt = int((t_1 - t_0 + dt) / dt) # Dt: Number of time steps.
t = np.linspace(t_0, t_1, Dt) # t : Time mesh.
plt.rcParams["font.size"] = 18
#%% Read data
c_matrix = np.loadtxt("c_matrix.txt", delimiter=",")
phi_matrix = np.loadtxt("phi_matrix.txt", delimiter=",")
phi_c_matrix = np.loadtxt("phi_c_matrix.txt", delimiter=",")
c_t_matrix = np.loadtxt("c_t_matrix.txt", delimiter=",")
phi_c_t_matrix = np.loadtxt("phi_c_t_matrix.txt", delimiter=",")
phi_combined_matrix = np.loadtxt("phi_combined_matrix.txt", delimiter=",")
flux_c_matrix = np.loadtxt("flux_c_matrix.txt", delimiter=",")
flux_phi_matrix = np.loadtxt("flux_phi_matrix.txt", delimiter=",")
flux_phi_c_matrix = np.loadtxt("flux_phi_c_matrix.txt", delimiter=",")
flux_c_t_matrix = np.loadtxt("flux_c_t_matrix.txt", delimiter=",")
flux_phi_c_t_matrix = np.loadtxt("flux_phi_c_t_matrix.txt", delimiter=",")
flux_phi_combined_matrix = np.loadtxt("flux_phi_combined_matrix.txt",
delimiter=",")
#%% Plot just results
for i, t_i in enumerate(t):
if i % 300 == 0:
# Plot concentrations
fig, ax = plt.subplots(figsize=(4, 4), dpi=300)
# Plot concentrations
ax.plot(x, c_matrix[i, :], label='c', color='darkmagenta')
ax.plot(x, c_t_matrix[i, :], label=r'$c_t$', color='darkmagenta',
linestyle='--')
ax.plot(x, phi_matrix[i, :], label=r'$\phi$', color='darkgreen')
ax.plot(x, phi_c_matrix[i, :], label=r'$\phi_c$', color='darkgreen',
linestyle='--')
ax.plot(x, phi_c_t_matrix[i, :], label=r'$\phi_c$', color='darkgreen',
linestyle='-.')
# Set axis labels, limits, and grid
ax.set_xlabel('x')
ax.set_xlim(x_0, x_1)
ax.set_xticks(np.linspace(x_0, x_1, 5))
ax.set_ylim(0, 1)
ax.grid()
# Add legend
#ax.legend(loc='upper right')
# Adjust layout
plt.tight_layout()
# Optionally save the figure
plt.savefig(f"5variable_density_t{t_i}.png", dpi=300,
bbox_inches='tight')
plt.show()
# %%# Plot results for density and flux
for i, t_i in enumerate(t):
if i % 300 == 0:
# Create figure and subplots for concentrations and fluxes
fig, axs = plt.subplots(1, 2, figsize=(7.5, 4), dpi=300)
# Plot concentrations
axs[0].plot(x, c_matrix[i, :], label='c', color='darkmagenta')
axs[0].plot(x, c_t_matrix[i, :], label=r'$c_t$',
color='mediumvioletred')
axs[0].plot(x, phi_matrix[i, :], label=r'$\phi$', color='darkgreen')
axs[0].plot(x, phi_c_matrix[i, :], label=r'$\phi_c$',
color='darkgreen', linestyle='--')
axs[0].plot(x, phi_c_t_matrix[i, :],
label=r'$\phi_{c_t}$', color='darkgreen', linestyle='-.')
axs[0].set_xlabel('x')
axs[0].set_xlim(x_0, x_1)
axs[0].set_ylim(0, 1)
axs[0].grid()
axs[0].set_title("Densities")
# Plot fluxes
# axs[1].plot(x, flux_c_matrix[i, :], color='darkmagenta')
axs[1].plot(x, flux_phi_matrix[i, :], color='darkgreen')
axs[1].plot(x, flux_phi_c_matrix[i, :],
color='darkgreen', linestyle='--')
# axs[1].plot(x, flux_c_t_matrix[i, :], color='mediumvioletred')
axs[1].plot(x, flux_phi_c_t_matrix[i, :],
color='darkgreen', linestyle='-.')
axs[1].set_xlabel('x')
axs[1].set_xlim(x_0, x_1)
axs[1].set_ylim(-0.1, 0.1)
axs[1].axhline(y=0, color='black')
axs[1].grid()
axs[1].set_title("Fluxes")
# Combine legends into one, positioned above the plots
handles, labels = axs[0].get_legend_handles_labels()
handles_flux, labels_flux = axs[1].get_legend_handles_labels()
handles_combined = handles + handles_flux
labels_combined = labels + labels_flux
#fig.legend(handles_combined, labels_combined,
#loc='upper center', ncol=3, frameon=False)
# Adjust spacing to accommodate the legend
plt.tight_layout()
# Optionally save the figure
plt.savefig(f"5variable_t{t_i:.1f}s.png", dpi=300,
bbox_inches='tight')
plt.show()
# %% Plot density matrices for phi, phi_c, and c in one loop
density_matrices = [c_matrix*6, phi_matrix*0.915, phi_c_matrix*0.915,
c_t_matrix*6, phi_c_t_matrix*0.915,
phi_combined_matrix*0.915]
labels = ['Density c', 'Density phi', 'Density phi_c', 'Density c_t',
'Density phi_c_t','Density phi_combined']
file_names = ['c_matrix', 'phi_matrix', 'phi_c_matrix', 'c_t_matrix',
'phi_c_t_matrix','phi_combined_matrix']
for density_matrix, label, file_name in zip(density_matrices,
labels, file_names):
if file_name == 'c_matrix':
vmin, vmax = 0, 6
elif file_name == 'phi_matrix':
vmin, vmax = 0, 0.6
elif file_name == 'phi_c_matrix':
vmin, vmax = 0, 0.6
elif file_name == 'c_t_matrix':
vmin, vmax = 0, 0.6
elif file_name == 'phi_c_t_matrix':
vmin, vmax = 0, 6e-3
else:
vmin, vmax = 0, 1
fig, (cbar_ax, ax) = plt.subplots(nrows=2, figsize=(6, 7),
gridspec_kw={"height_ratios": [1, 20]}, dpi=300)
# Create the image plot
im = ax.imshow(
density_matrix,
extent=[x_0, x_1, t_0, t_1],
aspect='auto',
origin='lower',
cmap='magma',
vmin=vmin,
vmax=vmax
)
# Set axis labels
ax.set_xlabel('Position $(\mu m)$')
if steady_state:
ax.set_ylabel('Time (hrs)')
else:
ax.set_ylabel('Time (s)')
ax.set_xticks(np.linspace(x_0, x_1, 7))
ax.set_yticks(np.linspace(t_0, t_1, 7))
if steady_state:
ax.set_yticklabels(np.linspace(1.44e4/3600*t_0,
1.44e4/3600*t_1, 7).astype(int))
else:
ax.set_yticklabels(np.linspace(1.44e4*t_0, 1.44e4*t_1, 7).astype(int))
ax.set_xticklabels(np.linspace(0, 1200, 7).astype(int))
# Create and configure the colorbar
cbar = fig.colorbar(im, cax=cbar_ax, orientation='horizontal',
location='top')
if file_name == 'c_matrix' or file_name == 'c_t_matrix':
cbar.set_label('Density $(CCL21/\mu m)$', fontsize=22)
else:
cbar.set_label('Density $(cells/\mu m)$', fontsize=22)
cbar.set_ticks(np.linspace(vmin, vmax, 5))
formatter = mticker.ScalarFormatter(useMathText=False)
formatter.set_scientific(True)
formatter.set_powerlimits((0, 0)) # Force scientific notation (e.g., 1e0)
cbar.ax.xaxis.set_major_formatter(formatter)
# Adjust layout
plt.tight_layout(pad=1.0)
plt.savefig(f"{file_name}.png", dpi=300, bbox_inches='tight')
plt.show()
# %% Plot flux matrices for phi, phi_c, and c in one loop
flux_matrices = [flux_c_matrix*8e4, flux_phi_matrix*7.9e-4,
flux_phi_c_matrix*7.9e-4, flux_c_t_matrix*8e5,
flux_phi_c_t_matrix*7.9e-4, flux_phi_combined_matrix*7.9e-4]
labels = ['Flux of c', 'Flux of phi', 'Flux of phi_c', 'Flux of c_t',
'Flux of phi_c_t', 'Flux of phi_combined']
file_names = ['flux_c_matrix', 'flux_phi_matrix', 'flux_phi_c_matrix',
'flux_c_t_matrix','flux_phi_c_t_matrix',
'flux_phi_combined_matrix']
for flux_matrix, label, file_name in zip(flux_matrices, labels, file_names):
if file_name == 'flux_c_matrix':
vlim = 1e5
elif file_name == 'flux_phi_matrix':
vlim = 1e-4
elif file_name == 'flux_phi_c_matrix':
vlim = 1e-4
elif file_name == 'flux_c_t_matrix':
vlim = 1e6
elif file_name == 'flux_phi_c_t_matrix':
vlim = 1e-6
else:
vlim = 1e-4
vmin, vmax = -vlim, vlim
fig, (cbar_ax, ax) = plt.subplots(nrows=2, figsize=(6, 7),
gridspec_kw={"height_ratios": [1, 20]}, dpi=300)
# Create the image plot
im = ax.imshow(
flux_matrix,
extent=[x_0, x_1, t_0, t_1],
aspect='auto',
origin='lower',
cmap='seismic',
vmin=vmin,
vmax=vmax
)
# Set axis labels
ax.set_xlabel('Position $(\mu m)$')
if steady_state:
ax.set_ylabel('Time (hrs)')
else:
ax.set_ylabel('Time (s)')
ax.set_xticks(np.linspace(x_0, x_1, 7))
ax.set_yticks(np.linspace(t_0, t_1, 7))
ax.set_xticklabels(np.linspace(1200 * x_0, 1200 * x_1, 7).astype(int))
if steady_state:
ax.set_yticklabels(np.linspace(1.44e4 / 3600 *\
t_0, 1.44e4 / 3600 * t_1, 7).astype(int))
else:
ax.set_yticklabels(np.linspace(1.44e4 *\
t_0, 1.44e4 * t_1, 7).astype(int))
# Create and configure the colorbar
cbar = fig.colorbar(im, cax=cbar_ax, orientation='horizontal',
location='top')
if file_name == 'flux_c_matrix' or file_name == 'flux_c_t_matrix':
cbar.set_label('Flux ($CCL21/s$)', fontsize=22)
else:
cbar.set_label('Flux ($cells/s$)', fontsize=22)
cbar.set_ticks(np.linspace(vmin, vmax, 3))
formatter = mticker.ScalarFormatter(useMathText=False)
formatter.set_scientific(True)
formatter.set_powerlimits((0, 0))
cbar.ax.xaxis.set_major_formatter(formatter)
plt.tight_layout(pad=1.0)
plt.savefig(f"{file_name}.png", dpi=300, bbox_inches='tight')
plt.show()