-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh.f95
318 lines (252 loc) · 9.05 KB
/
mesh.f95
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
module mesh_m
implicit none
type boundary_condition_t
character :: type ! N for Neumann, D for Dirichlet
real(kind=8) :: a, b, c ! Boundary condition formulated as a+b*x+c*x^2
end type boundary_condition_t
type mesh_t
integer :: n_rows, n_cols, N_cp, max_iter
integer,allocatable,dimension(:) :: W_ind, E_ind, S_ind, N_ind
real(kind=8) :: x_min, x_max, y_min, y_max
real(kind=8) :: dx, dy, gamma, omega
real(kind=8),allocatable,dimension(:) :: x_cp, y_cp, AP, AW, AS, AE, AN, Sp, Su, phi
type(boundary_condition_t) :: W_bc, S_bc, E_bc, N_bc
end type mesh_t
integer allocated
contains
subroutine mesh_allocate(t)
type(mesh_t) :: t
if(allocated.eq.1) call mesh_deallocate(t)
allocate(t%W_ind(t%N_cp))
allocate(t%E_ind(t%N_cp))
allocate(t%S_ind(t%N_cp))
allocate(t%N_ind(t%N_cp))
allocate(t%x_cp(t%n_cols))
allocate(t%y_cp(t%n_rows))
allocate(t%AP(t%N_cp))
allocate(t%AW(t%N_cp))
allocate(t%AS(t%N_cp))
allocate(t%AE(t%N_cp))
allocate(t%AN(t%N_cp))
allocate(t%Su(t%N_cp))
allocate(t%Sp(t%N_cp))
allocate(t%phi(t%N_cp))
allocated = 1
end subroutine mesh_allocate
subroutine mesh_deallocate(t)
type(mesh_t) :: t
deallocate(t%W_ind)
deallocate(t%E_ind)
deallocate(t%S_ind)
deallocate(t%N_ind)
deallocate(t%x_cp)
deallocate(t%y_cp)
deallocate(t%AP)
deallocate(t%AW)
deallocate(t%AS)
deallocate(t%AE)
deallocate(t%AN)
deallocate(t%Su)
deallocate(t%Sp)
deallocate(t%phi)
allocated = 0
end subroutine mesh_deallocate
subroutine mesh_load_input(t, inp)
type(mesh_t) :: t
character(100) :: inp
integer :: i,j
! Read input file
open(1,file=inp)
read(1,*)
read(1,*)
read(1,*) t%x_min, t%x_max, t%y_min, t%y_max
read(1,*)
read(1,*) t%n_rows, t%n_cols
read(1,*)
read(1,*) t%gamma
read(1,*)
read(1,*)
read(1,*) t%W_bc%type, t%W_bc%a, t%W_bc%b, t%W_bc%c
read(1,*)
read(1,*) t%S_bc%type, t%S_bc%a, t%S_bc%b, t%S_bc%c
read(1,*)
read(1,*) t%E_bc%type, t%E_bc%a, t%E_bc%b, t%E_bc%c
read(1,*)
read(1,*) t%N_bc%type, t%N_bc%a, t%N_bc%b, t%N_bc%c
read(1,*)
read(1,*)
read(1,*) t%omega, t%max_iter
close(1)
! Parse some derived quantities
t%dx = (t%x_max-t%x_min)/t%n_cols
t%dy = (t%y_max-t%y_min)/t%n_rows
t%N_cp = t%n_rows*t%n_cols
! Allocate
call mesh_allocate(t)
! Initialize control point locations
do i = 1, t%n_rows
t%y_cp(i) = t%y_min+(i-0.5)*t%dx
end do
do j = 1, t%n_cols
t%x_cp(j) = t%x_min+(j-0.5)*t%dx
end do
write(*,*) "Successfully initialized mesh."
end subroutine mesh_load_input
subroutine mesh_set_up_matrices(t)
! Sets up the matrices for actually solving the problem
implicit none
type(mesh_t) :: t
integer :: i, j, ind
real(kind=8) :: phi_bc, dphi_dn
! Loop through control points
ind = 0
do i = 1, t%n_rows
do j = 1, t%n_cols
! Calculate indices for this point
ind = ind + 1
t%W_ind(ind) = ind-1
t%E_ind(ind) = ind+1
t%S_ind(ind) = ind-t%n_cols
t%N_ind(ind) = ind+t%n_cols
! Initialize solution
t%phi(ind) = 0.0
! Calculate central coefficients
t%AW(ind) = t%gamma*t%dy/t%dx
t%AE(ind) = t%gamma*t%dy/t%dx
t%AS(ind) = t%gamma*t%dx/t%dy
t%AN(ind) = t%gamma*t%dx/t%dy
t%Su(ind) = 0
t%Sp(ind) = 0
! Handle west boundary
if (j .eq. 1) then
if (t%W_bc%type .eq. 'D') then
phi_bc = t%W_bc%a+t%W_bc%b*t%y_cp(i)+t%W_bc%c*t%y_cp(i)**2.0
t%Su(ind) = t%Su(ind) + 2.0*t%gamma*t%dy*phi_bc/t%dx
t%Sp(ind) = t%Sp(ind) - 2.0*t%gamma*t%dy/t%dx
else
dphi_dn = t%W_bc%a+t%W_bc%b*t%y_cp(i)+t%W_bc%c*t%y_cp(i)**2.0
t%Sp(ind) = t%Sp(ind) - t%dy*t%gamma*dphi_dn
end if
t%AW(ind) = 0.0
t%W_ind(ind) = 1 ! Not needed in this case
end if
! Handle east boundary
if (j .eq. t%n_cols) then
if (t%E_bc%type .eq. 'D') then
phi_bc = t%E_bc%a+t%E_bc%b*t%y_cp(i)+t%E_bc%c*t%y_cp(i)**2.0
t%Su(ind) = t%Su(ind) + 2.0*t%gamma*t%dy*phi_bc/t%dx
t%Sp(ind) = t%Sp(ind) - 2.0*t%gamma*t%dy/t%dx
else
dphi_dn = t%E_bc%a+t%E_bc%b*t%y_cp(i)+t%E_bc%c*t%y_cp(i)**2.0
t%Sp(ind) = t%Sp(ind) + t%dy*t%gamma*dphi_dn
end if
t%AE(ind) = 0.0
t%E_ind(ind) = 1 ! Not needed in this case
end if
! Handle south boundary
if (i .eq. 1) then
if (t%S_bc%type .eq. 'D') then
phi_bc = t%S_bc%a+t%S_bc%b*t%x_cp(j)+t%S_bc%c*t%x_cp(j)**2.0
t%Su(ind) = t%Su(ind) + 2.0*t%gamma*t%dx*phi_bc/t%dy
t%Sp(ind) = t%Sp(ind) - 2.0*t%gamma*t%dx/t%dy
else
dphi_dn = t%S_bc%a+t%S_bc%b*t%x_cp(j)+t%S_bc%c*t%x_cp(j)**2.0
t%Sp(ind) = t%Sp(ind) - t%dx*t%gamma*dphi_dn
end if
t%AS(ind) = 0.0
t%S_ind(ind) = 1 ! Not needed in this case
end if
! Handle north boundary
if (i .eq. t%n_rows) then
if (t%N_bc%type .eq. 'D') then
phi_bc = t%N_bc%a+t%N_bc%b*t%x_cp(j)+t%N_bc%c*t%x_cp(j)**2.0
t%Su(ind) = t%Su(ind) + 2.0*t%gamma*t%dx*phi_bc/t%dy
t%Sp(ind) = t%Sp(ind) - 2.0*t%gamma*t%dx/t%dy
else
dphi_dn = t%N_bc%a+t%N_bc%b*t%x_cp(j)+t%N_bc%c*t%x_cp(j)**2.0
t%Sp(ind) = t%Sp(ind) + t%dx*t%gamma*dphi_dn
end if
t%AN(ind) = 0.0
t%N_ind(ind) = 1 ! Not needed in this case
end if
! Calculate P coefficient
t%AP(ind) = t%AW(ind) + t%AE(ind) + t%AN(ind) + t%AS(ind) - t%Sp(ind)
end do
end do
end subroutine mesh_set_up_matrices
subroutine mesh_sor(t)
type(mesh_t) :: t
real(kind=8) :: max_corr, corr
integer :: i, iteration
write(*,*)
write(*,*) "Running SOR..."
! Iterative SOR
iteration = 0
max_corr = 1.0
do while (max_corr>1d-15 .and. iteration<t%max_iter)
max_corr = 0.0
! Loop through control points
iteration = iteration + 1
do i = 1, t%N_cp
! Calculate correction
corr = t%AW(i)*t%phi(t%W_ind(i)) + t%AE(i)*t%phi(t%E_ind(i))
corr = corr + t%AS(i)*t%phi(t%S_ind(i)) + t%AN(i)*t%phi(t%N_ind(i))
corr = corr + t%Su(i) - t%AP(i)*t%phi(i)
corr = corr*t%omega/t%AP(i)
! Check magnitude
if (abs(corr)>max_corr) then
max_corr = abs(corr)
end if
! Apply correction
t%phi(i) = t%phi(i) + corr
end do
! Output progress
write(*,*) iteration, max_corr
end do
write(*,*) "Complete! Final maximum correction: ", max_corr
end subroutine mesh_sor
subroutine mesh_write_results_to_csv(t, filename)
type(mesh_t) :: t
character(100) :: filename
integer :: i, j, ind
real(kind=8) :: x, y, phi
! Open file
open(1,file=filename)
! Header
write(1,*) 'x,y,z,phi'
! Control points
ind = 0
do i = 1,t%n_rows
do j =1,t%n_cols
ind = ind + 1
write(1,*) t%x_cp(j), ',', t%y_cp(i), ',', 0.0, ',', t%phi(ind)
end do
end do
! West and east boundaries
do i = 1,t%n_rows
y = t%y_cp(i)
phi = t%W_bc%a+t%W_bc%b*y+t%W_bc%c*y**2
write(1,*) t%x_min, ',', y, ',', 0.0, ',', phi
phi = t%E_bc%a+t%E_bc%b*y+t%E_bc%c*y**2
write(1,*) t%x_max, ',', y, ',', 0.0, ',', phi
end do
! North and south boundaries
do i = 1,t%n_cols
x = t%x_cp(i)
phi = t%N_bc%a+t%N_bc%b*x+t%N_bc%c*x**2
write(1,*) x, ',', t%y_max, ',', 0.0, ',', phi
phi = t%S_bc%a+t%S_bc%b*x+t%S_bc%c*x**2
write(1,*) x, ',', t%y_min, ',', 0.0, ',', phi
end do
! Corners
phi = t%W_bc%a+t%W_bc%b*t%y_min+t%W_bc%c*t%y_min**2 ! SW
write(1,*) t%x_min, ',', t%y_min, ',', 0.0, ',', phi
phi = t%W_bc%a+t%W_bc%b*t%y_max+t%W_bc%c*t%y_max**2 ! NW
write(1,*) t%x_min, ',', t%y_max, ',', 0.0, ',', phi
phi = t%E_bc%a+t%E_bc%b*t%y_min+t%E_bc%c*t%y_min**2 ! SE
write(1,*) t%x_max, ',', t%y_min, ',', 0.0, ',', phi
phi = t%E_bc%a+t%E_bc%b*t%y_max+t%E_bc%c*t%y_max**2 ! NE
write(1,*) t%x_max, ',', t%y_max, ',', 0.0, ',', phi
close(1)
end subroutine mesh_write_results_to_csv
end module mesh_m