-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheat.py
executable file
·217 lines (198 loc) · 8.11 KB
/
heat.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
#!/usr/bin/python3
import multigrid as mg
import simulationData as sd
import numpy as np
import time
import optparse
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams["font.size"] = 20
def main():
# Set up options
parser = optparse.OptionParser()
parser.add_option("-x", "--xSpan", action="store", dest="xSpan",
default="0.0 1.0 11",
help="x-coordinate span and length. Default = 0 1 11")
parser.add_option("-y", "--ySpan", action="store", dest="ySpan",
default="0.0 1.0 11",
help="y-coordinate span and length. Default = 0 1 11")
parser.add_option("-v", "--nu", action="store", dest="nu",
default="1.0",
help="nu coefficient for heat equation. Default = 1.0")
parser.add_option("-n", "--timeSteps", action="store", dest="timeSteps",
default="1.0",
help="number of time steps. Default = 1")
parser.add_option("-t", "--threshold", action="store", dest="threshold",
default="1.0e-10",
help="residual threshold to use for timing. " \
+ "Default = 1e-10")
parser.add_option("-c", "--cornerTemperature", action="store",
dest="cornerTemp",
default="500",
help="corner temperature to use as BC. " \
+ "Default = 500")
options, remainder = parser.parse_args()
# ------------------------------------------------------------------
# baseline simulation - no multigrid
print("-----------------------------------------")
print("Baseline Simulation")
print("-----------------------------------------")
baselineData = sd.simulationData(options, 1, "V", "Baseline")
# construct grids
baseline = mg.mgSolution(baselineData)
# march solution in time
print("Iteration L2 Residual Linf Residual Time")
l2, linf = baseline.ResidNorm()
baselineData.LogResidual(0, l2, linf)
for nn in range(1, baselineData.timeSteps + 1):
baseline.MultigridCycle()
l2, linf = baseline.ResidNorm()
baselineData.LogResidual(nn, l2, linf)
print("\n\n")
# ------------------------------------------------------------------
# 2 level multigrid, V cycle
print("-----------------------------------------")
print("2 Level Multigrid V Cycle")
print("-----------------------------------------")
mg2vData = sd.simulationData(options, 2, "V", "2 Level V")
# construct grids
mg2v = mg.mgSolution(mg2vData)
# march solution in time
print("Iteration L2 Residual Linf Residual Time")
l2, linf = mg2v.ResidNorm()
mg2vData.LogResidual(0, l2, linf)
for nn in range(1, mg2vData.timeSteps + 1):
mg2v.MultigridCycle()
l2, linf = mg2v.ResidNorm()
mg2vData.LogResidual(nn, l2, linf)
print("\n\n")
# ------------------------------------------------------------------
# 4 level multigrid, V cycle
print("-----------------------------------------")
print("4 Level Multigrid V Cycle")
print("-----------------------------------------")
mg4vData = sd.simulationData(options, 4, "V", "4 Level V")
# construct grids
mg4v = mg.mgSolution(mg4vData)
# march solution in time
print("Iteration L2 Residual Linf Residual Time")
l2, linf = mg4v.ResidNorm()
mg4vData.LogResidual(0, l2, linf)
for nn in range(1, mg4vData.timeSteps + 1):
mg4v.MultigridCycle()
l2, linf = mg4v.ResidNorm()
mg4vData.LogResidual(nn, l2, linf)
print("\n\n")
# ------------------------------------------------------------------
# 4 level multigrid, W cycle
print("-----------------------------------------")
print("4 Level Multigrid W Cycle")
print("-----------------------------------------")
mg4wData = sd.simulationData(options, 4, "W", "4 Level W")
# construct grids
mg4w = mg.mgSolution(mg4wData)
# march solution in time
print("Iteration L2 Residual Linf Residual Time")
l2, linf = mg4w.ResidNorm()
mg4wData.LogResidual(0, l2, linf)
for nn in range(1, mg4wData.timeSteps + 1):
mg4w.MultigridCycle()
l2, linf = mg4w.ResidNorm()
mg4wData.LogResidual(nn, l2, linf)
print("\n\n")
# ------------------------------------------------------------------
# 4 level multigrid, W cycle
print("-----------------------------------------")
print("4 Level Multigrid F Cycle")
print("-----------------------------------------")
mg4fData = sd.simulationData(options, 4, "V", "4 Level F")
# construct grids
mg4f = mg.mgSolution(mg4fData)
# march solution in time
print("Iteration L2 Residual Linf Residual Time")
l2, linf = mg4f.ResidNorm()
mg4fData.LogResidual(0, l2, linf)
for nn in range(1, mg4fData.timeSteps + 1):
mg4f.MultigridFCycle()
l2, linf = mg4f.ResidNorm()
mg4fData.LogResidual(nn, l2, linf)
print("\n\n")
# ------------------------------------------------------------------
# 4 level full multigrid, V cycle
print("-----------------------------------------")
print("4 Level Full Multigrid V Cycle")
print("-----------------------------------------")
fmg4vData = sd.simulationData(options, 4, "V", "4 Level FMG V")
# construct grids
fmg4v = mg.mgSolution(fmg4vData)
# march solution in time
print("Iteration L2 Residual Linf Residual Time")
l2, linf = fmg4v.ResidNorm()
fmg4vData.LogResidual(0, l2, linf)
for nn in range(1, fmg4vData.timeSteps + 1):
fmg4v.FullMultigridCycle()
l2, linf = fmg4v.ResidNorm()
fmg4vData.LogResidual(nn, l2, linf)
print("\n\n")
# DEBUG
#fig1, ax1 = plt.subplots(figsize=(12, 8))
#plt.xlabel("X (m)")
#plt.ylabel("Y (m)")
#plt.title("Residual Contour")
#cf = ax1.contourf(fmg4v.levels[0].centers[1:-1, 1:-1, 0], \
# fmg4v.levels[0].centers[1:-1,1:-1, 1], \
# fmg4v.levels[0].CalcResidual())
#cbar = fig1.colorbar(cf)
#cbar.ax.set_ylabel("Residual")
#ax1.grid(True)
#plt.tight_layout()
#plt.show()
# ------------------------------------------------------------------
# plot solutions
_, ax = plt.subplots(2, 3, figsize=(24, 12))
baseline.PlotNode(ax[0, 0], baselineData.name)
mg2v.PlotNode(ax[0, 1], mg2vData.name)
mg4v.PlotNode(ax[0, 2], mg4vData.name)
mg4w.PlotNode(ax[1, 0], mg4wData.name)
mg4f.PlotNode(ax[1, 1], mg4fData.name)
fmg4v.PlotNode(ax[1, 2], fmg4vData.name)
plt.tight_layout()
plt.show()
_, ax = plt.subplots(1, 2, figsize=(24, 12))
# plot residual vs iteration
ax[0].set_xlabel("Iteration")
ax[0].set_ylabel("Normalized Residual")
ax[0].semilogy(baselineData.iteration, baselineData.NormResids(), "k", lw=3)
ax[0].semilogy(mg2vData.iteration, mg2vData.NormResids(), "b", lw=3)
ax[0].semilogy(mg4vData.iteration, mg4vData.NormResids(), "r", lw=3)
ax[0].semilogy(mg4wData.iteration, mg4wData.NormResids(), "g", lw=3)
ax[0].semilogy(mg4fData.iteration, mg4fData.NormResids(), "m", lw=3)
ax[0].semilogy(fmg4vData.iteration, fmg4vData.NormResids(), "c", lw=3)
ax[0].legend([baselineData.name, mg2vData.name, mg4vData.name,
mg4wData.name, mg4fData.name, fmg4vData.name])
ax[0].grid(True)
# plot residual vs wall clock time
ax[1].set_xlabel("Wall Clock Time (s)")
ax[1].set_ylabel("Normalized Residual")
ax[1].semilogy(baselineData.times, baselineData.NormResids(), "k", lw=3)
ax[1].semilogy(mg2vData.times, mg2vData.NormResids(), "b", lw=3)
ax[1].semilogy(mg4vData.times, mg4vData.NormResids(), "r", lw=3)
ax[1].semilogy(mg4wData.times, mg4wData.NormResids(), "g", lw=3)
ax[1].semilogy(mg4fData.times, mg4fData.NormResids(), "m", lw=3)
ax[1].semilogy(fmg4vData.times, fmg4vData.NormResids(), "c", lw=3)
ax[1].legend([baselineData.name, mg2vData.name, mg4vData.name,
mg4wData.name, mg4fData.name, fmg4vData.name])
ax[1].grid(True)
plt.tight_layout()
plt.show()
print("-----------------------------------------")
print("Summary")
print("-----------------------------------------")
baselineData.PrintTimeToThreshold()
mg2vData.PrintTimeToThreshold()
mg4vData.PrintTimeToThreshold()
mg4wData.PrintTimeToThreshold()
mg4fData.PrintTimeToThreshold()
fmg4vData.PrintTimeToThreshold()
if __name__ == "__main__":
main()