-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotting.py
217 lines (187 loc) · 8.26 KB
/
plotting.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
216
217
import numpy as np
import matplotlib as mpl
from datetime import datetime
import sys
import os
import json
from scipy.interpolate import griddata
import matplotlib.gridspec as gridspec
from mpl_toolkits.axes_grid1 import make_axes_locatable
def saveResultDir(save_path, save_hp):
now = datetime.now()
scriptName = os.path.splitext(os.path.basename(sys.argv[0]))[0]
resDir = os.path.join(save_path, "results", f"{now.strftime('%Y%m%d-%H%M%S')}-{scriptName}")
os.mkdir(resDir)
print("Saving results to directory ", resDir)
savefig(os.path.join(resDir, "graph"))
with open(os.path.join(resDir, "hp.json"), "w") as f:
json.dump(save_hp, f)
# MIT License
#
# Copyright (c) 2018 maziarraissi
#
# https://github.com/maziarraissi/PINNs
def figsize(scale, nplots = 1):
fig_width_pt = 390.0 # Get this from LaTeX using \the\textwidth
inches_per_pt = 1.0/72.27 # Convert pt to inch
golden_mean = (np.sqrt(5.0)-1.0)/2.0 # Aesthetic ratio (you could change this)
fig_width = fig_width_pt*inches_per_pt*scale # width in inches
fig_height = nplots*fig_width*golden_mean # height in inches
fig_size = [fig_width,fig_height]
return fig_size
pgf_with_latex = { # setup matplotlib to use latex for output
"pgf.texsystem": "pdflatex", # change this if using xetex or lautex
"text.usetex": True, # use LaTeX to write all text
"font.family": "serif",
"font.serif": [], # blank entries should cause plots to inherit fonts from the document
"font.sans-serif": [],
"font.monospace": [],
"axes.labelsize": 10, # LaTeX default is 10pt font.
"font.size": 10,
"legend.fontsize": 8, # Make the legend/label fonts a little smaller
"xtick.labelsize": 8,
"ytick.labelsize": 8,
"figure.figsize": figsize(1.0), # default fig size of 0.9 textwidth
"pgf.preamble": [
r"\usepackage[utf8x]{inputenc}", # use utf8 fonts becasue your computer can handle it :)
r"\usepackage[T1]{fontenc}", # plots will be generated using this preamble
]
}
# mpl.rcParams.update(pgf_with_latex)
import matplotlib.pyplot as plt
# I make my own newfig and savefig functions
def newfig(width, nplots = 1):
fig = plt.figure(figsize=figsize(width, nplots))
ax = fig.add_subplot(111)
return fig, ax
def savefig(filename, crop = True):
if crop == True:
# plt.savefig('{}.pgf'.format(filename), bbox_inches='tight', pad_inches=0)
# plt.savefig('{}.pdf'.format(filename), bbox_inches='tight', pad_inches=0)
# plt.savefig('{}.eps'.format(filename), bbox_inches='tight', pad_inches=0)
plt.savefig('{}.png'.format(filename), bbox_inches='tight', pad_inches=0,dpi = 600)
else:
# plt.savefig('{}.pgf'.format(filename))
plt.savefig('{}.pdf'.format(filename))
# plt.savefig('{}.eps'.format(filename))
plt.savefig('{}.png'.format(filename))
def solutionplot(u_pred,usol,X_u_train,u_train,X,T,x,t, filename = 'burgers'):
t_dim = T.shape[0]
x_dim = T.shape[1]
u_pred = u_pred.reshape(x_dim,t_dim)
usol =usol.reshape(x_dim,t_dim)
fig, ax = plt.subplots()
ax.axis('off')
gs0 = gridspec.GridSpec(1, 2)
gs0.update(top=1-0.06, bottom=1-1/3, left=0.15, right=0.85, wspace=0)
ax = plt.subplot(gs0[:, :])
h = ax.imshow(u_pred, interpolation='nearest', cmap='rainbow',
extent=[T.min(), T.max(), X.min(), X.max()],
origin='lower', aspect='auto')
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
fig.colorbar(h, cax=cax)
ax.plot(X_u_train[:,1], X_u_train[:,0], 'kx', label = 'Data (%d points)' % (u_train.shape[0]), markersize = 4, clip_on = False)
line = np.linspace(x.min(), x.max(), 2)[:,None]
ax.plot(t[25]*np.ones((2,1)), line, 'w-', linewidth = 1)
ax.plot(t[50]*np.ones((2,1)), line, 'w-', linewidth = 1)
ax.plot(t[75]*np.ones((2,1)), line, 'w-', linewidth = 1)
ax.set_xlabel('$t$')
ax.set_ylabel('$x$')
ax.legend(frameon=False, loc = 'best')
ax.set_title('$u(x,t)$', fontsize = 10)
'''
Slices of the solution at points t = 0.25, t = 0.50 and t = 0.75
'''
####### Row 1: u(t,x) slices ##################
gs1 = gridspec.GridSpec(1, 3)
gs1.update(top=1-1/3, bottom=0, left=0.1, right=0.9, wspace=0.5)
ax = plt.subplot(gs1[0, 0])
ax.plot(x,usol.T[25,:], 'b-', linewidth = 2, label = 'Exact')
ax.plot(x,u_pred.T[25,:], 'r--', linewidth = 2, label = 'Prediction')
ax.set_xlabel('$x$')
ax.set_ylabel('$u(x,t)$')
ax.set_title('$t = 0.25s$', fontsize = 10)
ax.axis('square')
ax.set_xlim([-1.1,1.1])
ax.set_ylim([-1.1,1.1])
ax = plt.subplot(gs1[0, 1])
ax.plot(x,usol.T[50,:], 'b-', linewidth = 2, label = 'Exact')
ax.plot(x,u_pred.T[50,:], 'r--', linewidth = 2, label = 'Prediction')
ax.set_xlabel('$x$')
ax.set_ylabel('$u(x,t)$')
ax.axis('square')
ax.set_xlim([-1.1,1.1])
ax.set_ylim([-1.1,1.1])
ax.set_title('$t = 0.50s$', fontsize = 10)
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.35), ncol=5, frameon=False)
ax = plt.subplot(gs1[0, 2])
ax.plot(x,usol.T[75,:], 'b-', linewidth = 2, label = 'Exact')
ax.plot(x,u_pred.T[75,:], 'r--', linewidth = 2, label = 'Prediction')
ax.set_xlabel('$x$')
ax.set_ylabel('$u(x,t)$')
ax.axis('square')
ax.set_xlim([-1.1,1.1])
ax.set_ylim([-1.1,1.1])
ax.set_title('$t = 0.75s$', fontsize = 10)
plt.savefig(f'{filename}.png',dpi = 500)
def solutionplot_ch(u_pred,usol,X_u_train,u_train,X,T,x,t, filename = 'ch'):
t_dim = T.shape[0]
x_dim = T.shape[1]
u_pred = u_pred.reshape(x_dim,t_dim)
usol =usol.reshape(x_dim,t_dim)
fig, ax = plt.subplots()
ax.axis('off')
gs0 = gridspec.GridSpec(1, 2)
gs0.update(top=1-0.06, bottom=1-1/3, left=0.15, right=0.85, wspace=0)
ax = plt.subplot(gs0[:, :])
h = ax.imshow(u_pred, interpolation='nearest', cmap='rainbow',
extent=[T.min(), T.max(), X.min(), X.max()],
origin='lower', aspect='auto')
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
fig.colorbar(h, cax=cax)
ax.plot(X_u_train[:,1], X_u_train[:,0], 'kx', label = 'Data (%d points)' % (u_train.shape[0]), markersize = 4, clip_on = False)
line = np.linspace(x.min(), x.max(), 2)[:,None]
ax.plot(t[25]*np.ones((2,1)), line, 'w-', linewidth = 1)
ax.plot(t[50]*np.ones((2,1)), line, 'w-', linewidth = 1)
ax.plot(t[75]*np.ones((2,1)), line, 'w-', linewidth = 1)
ax.set_xlabel('$t$')
ax.set_ylabel('$x$')
ax.legend(frameon=False, loc = 'best')
ax.set_title('$u(x,t)$', fontsize = 10)
'''
Slices of the solution at points t = 0.25, t = 0.50 and t = 0.75
'''
####### Row 1: u(t,x) slices ##################
gs1 = gridspec.GridSpec(1, 3)
gs1.update(top=1-1/3, bottom=0, left=0.1, right=0.9, wspace=0.5)
ax = plt.subplot(gs1[0, 0])
ax.plot(x,usol.T[25,:], 'b-', linewidth = 2, label = 'Exact')
ax.plot(x,u_pred.T[25,:], 'r--', linewidth = 2, label = 'Prediction')
ax.set_xlabel('$x$')
ax.set_ylabel('$u(x,t)$')
ax.set_title('$t = 0.25s$', fontsize = 10)
ax.axis('square')
ax.set_xlim([-1.1,1.1])
ax.set_ylim([-1.1,1.1])
ax = plt.subplot(gs1[0, 1])
ax.plot(x,usol.T[50,:], 'b-', linewidth = 2, label = 'Exact')
ax.plot(x,u_pred.T[50,:], 'r--', linewidth = 2, label = 'Prediction')
ax.set_xlabel('$x$')
ax.set_ylabel('$u(x,t)$')
ax.axis('square')
ax.set_xlim([-1.1,1.1])
ax.set_ylim([-1.1,1.1])
ax.set_title('$t = 0.50s$', fontsize = 10)
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.35), ncol=5, frameon=False)
ax = plt.subplot(gs1[0, 2])
ax.plot(x,usol.T[75,:], 'b-', linewidth = 2, label = 'Exact')
ax.plot(x,u_pred.T[75,:], 'r--', linewidth = 2, label = 'Prediction')
ax.set_xlabel('$x$')
ax.set_ylabel('$u(x,t)$')
ax.axis('square')
ax.set_xlim([-1.1,1.1])
ax.set_ylim([-1.1,1.1])
ax.set_title('$t = 0.75s$', fontsize = 10)
plt.savefig(f'{filename}.png',dpi = 500)