-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW3p4.jl
212 lines (165 loc) · 5.02 KB
/
HW3p4.jl
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
# solve 1.2.1 in the textbook
function driver()
xmin = 0
xmax = 1
tmax = 1.00
N = 161
r = 0.4
# delta_t = 0.02
nu = 1
ICFunc = ICcos
BCL = BCcos
BCR = BCsincos
ffunc = forcing
# for accurate performance benchmarking, JIT the function first
# u, tmax_ret = solve(xmin, xmax, tmax, N, r, nu, ICFunc, BCL, BCR, ffunc, write_files=false)
# now that the function is compiled, we wil get an accurate timing
u, tmax_ret = solve(xmin, xmax, tmax, N, r, nu, ICFunc, BCL, BCR, ffunc, write_files=true)
delta_x = (xmax - xmin)/(N-1)
delta_t = (r*delta_x^2)/nu # nu*delta_t
println("tmax = ", tmax, ", tmax_ret = ", tmax_ret)
vals = [xmin, xmax, tmax_ret, nu, delta_t]
writedlm("counts.dat", vals)
writedlm("u.dat", u[2:(end-1), end])
end
function solve(xmin, xmax, tmax, N, r, nu, ICFunc::Function, BCL::Function, BCR::Function, rhs::Function; write_files=true)
# xmin = minimum x coordinate
# xmax = maximum x coordinate
# tmax = maximum time value
# N = number of x points
# r = nu*delta_t/delta_x^2
# ICFunc = initial condition function with signature val = ICFunc(x)
# BCL = left boundary condition function with signature val = BCL(t)
# BCR = right boundary condition function
# write_files = whether or not to write to convergence.dat
delta_x = (xmax - xmin)/(N-1)
delta_t = (r*delta_x^2)/nu # nu*delta_t
#r = nu*delta_t/(delta_x^2)
nStep = convert(Int, div(tmax, delta_t)) + 1
println("delta_x = ", delta_x)
println("delta_t = ", delta_t)
println("r = ", r)
println("nStep = ", nStep)
# allocate storage
u = Array(Float64, N+2, nStep)
# apply IC
# Not applying BC at initial condition
# hopefully IC and BC are consistent
for i=2:(N+1)
u[i, 1] = ICFunc(xmin + (i-2)*delta_x)
end
flops = 0 # count number of FLOPs
time = @elapsed for tstep=2:nStep # loop over timesteps
# println("tstep = ", tstep)
tstep_1 = tstep-1
# apply BC
t_i = (tstep - 2)*delta_t
forcing_i = rhs(xmin, t_i)
# u[1, tstep] = BCL(t_i)
applyCompatBC(BCL, 2, t_i, tstep, delta_x, nu, forcing_i, u)
applyNeumannBC(BCR, N+1, t_i, tstep, delta_x, u; is_left=false)
# println("u at tstep $tstep_1 = ", u[:, tstep_1])
# calculate non-ghost points
for i=2:(N+1)
u_k = u[i, tstep - 1]
u_k_1 = u[i-1, tstep - 1 ]
u_k_p1 = u[i+1, tstep - 1]
x_i = xmin + (i-2)*delta_x
u[i, tstep] = u[i, tstep - 1] + r*(u_k_p1 - 2*u_k + u_k_1) + delta_t*rhs(x_i, t_i)
end
flops += 5*(N-2) # this doesn't count flops involving the rhs
end
println("time = ", time)
flop_rate = 1e-6*flops/time # MFlops/seconds
println("flop rate = ", flop_rate, " MFlops/sec")
# calculate maximum error
err_max = typemin(Float64)
for i=2:(N+1)
x_i = xmin + (i-2)*delta_x
ue = uExact(x_i, delta_t*(nStep - 1), nu)
err_i = abs(u[i, nStep] - ue)
println("err_$i = ", err_i)
if err_i > err_max
err_max = err_i
end
end
println("final error = ", err_max)
# write to file
if write_files
f = open("convergence.dat", "a")
@printf(f, "%d %16.15f %16.15f %d\n", N, err_max, time, nStep)
close(f)
end
return u, delta_t*(nStep - 1)
end
# for efficiency (to avoid dynamic dispatch), these functions should really be
# functors
function applyNeumannBC(f::Function, i::Integer, t, tstep, delta_x, u; is_left=true)
# apply Neumann BC to point i in the array u using ghost point
# there must be an element u[i+1] to use as a ghost point
# t is the time of tstep - 1
# forcing_term is the value of the forcing function at the boundary
# println("applying Neumann BC")
f_val = f(t)
if is_left
ghost_val = u[i+1, tstep - 1] - 2*delta_x*f_val
u[i-1, tstep - 1] = ghost_val
else
ghost_val = u[i-1, tstep - 1] + 2*delta_x*f_val
u[i+1, tstep - 1] = ghost_val
end
return nothing
end
function applyCompatBC(f::Function, i::Integer, t, tstep, delta_x, nu, forcing_term, u; is_left=true)
# apply a Direchlet compatable BC to the array u using a ghost point
# f is the Direchlet function
# forcing_term is the value of the forcing function at the boundary
# println("applying compatability BC")
# use complex step to get the df/dt
epsilon = 1e-20
val_pert = t + complex(0, epsilon)
dfdt = imag( f(val_pert) )/epsilon
if is_left
ghost_val = delta_x*delta_x*dfdt/nu - u[i+1, tstep - 1] + 2*u[i, tstep - 1] - delta_x*delta_x*forcing_term
u[i-1, tstep - 1] = ghost_val
else
ghost_val = delta_x*delta_x*dfdt/nu - u[i-1, tstep - 1] + 2*u[i, tstep - 1] - delta_x*delta_x*forcing_term
u[i+1, tstep - 1] = ghost_val
end
return nothing
end
function forcing(x, t)
# compute the value of the forcing function at a location x and time t
return 2*cos(x)*(cos(t) - sin(t))
end
function ICSin(x)
return sin(5*pi*x/2)
end
function ICcos(x)
return 2*cos(x)
end
function BCZero(x)
return 0.0
end
# left BC
function BCcos(t)
return 2*cos(t)
end
# right BC
function BCsincos(t)
return -2*sin(1)*cos(t)
end
function uExact(x, t, nu)
# calculate exact solution for an IC of sin(5*pi/2x)
return 2*cos(x)*cos(t)
end
function contains(A, val)
for i=1:length(A)
if A[i] == val
return true
end
end
return false
end
# run
driver()