-
Notifications
You must be signed in to change notification settings - Fork 0
/
mls_heatFlux.f90
319 lines (243 loc) · 12.3 KB
/
mls_heatFlux.f90
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
319
subroutine mls_heatFlux
USE param
USE mls_param
USE mpi_param, only: kstart, kend
use mpih
implicit none
real,dimension(4) :: ptx
real,dimension(3) :: pos
integer, dimension(3) :: probe_inds
real, dimension(3) :: gradT
integer :: inp,nf
real :: s
! Evaluate heat flux at probe locations extrapolated from triangle centroids
qw_o(:,:) = 0.0d0
qw_i(:,:) = 0.0d0
qw_oVert(:,:) = 0.0d0
qw_iVert(:,:) = 0.0d0
gradT = 0.0d0
do inp=1,Nparticle
do nf = 1, maxnf
if (isGhostFace(nf,inp) .eqv. .false. ) then
if(pind(3,nf,inp).ge.kstart .and. pind(3,nf,inp).le.kend) then
s = norm2( tri_nor(1:3,nf,inp) )
!------------------ POSITIVE PROBE-------------------------
pos(1:3) = tri_bar(1:3,nf,inp) + h_eulerian * tri_nor(1:3,nf,inp) / s !Positive probe location
! initialise pre-factor matrix
ptx(1) = 1.0d0
ptx(2:4) = pos(1:3)
probe_inds(1:3) = pind(1:3,nf,inp) ! Positive probe cage-indices
call wght_gradT(pos,ptx,gradT,probe_inds)
qw_o(nf,inp) = tri_nor(1,nf,inp) * gradT(1) & ! nx dTdx
+ tri_nor(2,nf,inp) * gradT(2) & ! ny dTdy
+ tri_nor(3,nf,inp) * gradT(3) ! nz dTdz
qw_o(nf,inp) = qw_o(nf,inp) / pec
! Transfer the face A*qw to its vertices, to qw_oVert and area, Avert
call faceToVert_interp(vert_of_face(1:3,nf,inp),sur(nf,inp),Avert(:,inp),qw_o(nf,inp),qw_oVert(:,inp),maxnv)
!write(*,*) "centroid loc", nf, " is ", tri_bar(1:3,nf,1)
endif !end if pind(3...)
!------------------ NEGATIVE PROBE-------------------------
if(pind(6,nf,inp).ge.kstart .and. pind(6,nf,inp).le.kend) then
s = norm2( tri_nor(1:3,nf,inp) )
pos(1:3) = tri_bar(1:3,nf,inp) - h_eulerian * tri_nor(1:3,nf,inp) / s !Negative probe location
! initialise pre-factor matrix
ptx(1) = 1.0d0
ptx(2:4) = pos(1:3)
probe_inds(1:3) = pind(4:6,nf,inp) ! Negative probe cage-indices
call wght_gradT(pos,ptx,gradT,probe_inds)
qw_i(nf,inp) = tri_nor(1,nf,inp) * gradT(1) & ! nx dTdx
+ tri_nor(2,nf,inp) * gradT(2) & ! ny dTdy
+ tri_nor(3,nf,inp) * gradT(3) ! nz dTdz
qw_i(nf,inp) = qw_i(nf,inp) / pec
! Note the same normal-sign convetion
call faceToVert_interp(vert_of_face(1:3,nf,inp),sur(nf,inp),Avert(:,inp),qw_i(nf,inp),qw_iVert(:,inp),maxnv)
endif !end if pindv(6...)
endif
enddo
enddo
call MPI_ALLREDUCE(MPI_IN_PLACE,qw_oVert,maxnv*Nparticle,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
call MPI_ALLREDUCE(MPI_IN_PLACE,qw_iVert,maxnv*Nparticle,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
end subroutine mls_heatFlux
subroutine wght_gradT(pos,ptx,gradT,probe_inds)
USE param
USE geom
use local_arrays, only: temp
USE mls_param
USE mpi_param, only: kstart, kend
implicit none
real, dimension(3), intent(inout) :: gradT !dTdx, dTdy, dTdz at the probe
real,dimension(nel) :: ddx_PtxAB(nel), ddy_PtxAB(nel), ddz_PtxAB(nel) !Shape function derivatives
real,dimension(nel) :: Tnel ! Eulerian support domain values of temperature
real,dimension(4) :: pxk ! [1,x,y,z] Basis function evaluated at Eulerian cage coordinates
real,dimension(4), intent(in) :: ptx ! Basis function [1, x, y, z] where (x,y,z) evaluated at the probe location
real,dimension(3) :: pos,norp,Wt ! Probe location, normalised cage distances and weights
real,dimension(4,4) :: pinvA,invA, dAdx, dAdy, dAdz
real,dimension(4,nel) :: B, dBdx, dBdy, dBdz
real, dimension(4) :: gamvec(4), dgamdx(4), dgamdy(4), dgamdz(4) ! Auxilary vectors for computing shape function derivatives
real :: Wtx, Wt23
real :: dWx_dx, dWy_dy, dWz_dz
real :: dWdx, dWdy, dWdz
integer :: inp,inw,i,j,k,k1
integer :: ii,jj,kk,nk
integer, dimension(3) :: pind_i, pind_o, probe_inds, keul_inds, k_inds
! For LAPACK
integer :: LWORK = 4
real, dimension(4) :: WORK
integer :: IPIV(4), INFO
!-------------Shape function for cell centres (temp. or pressure cells) -------------------------
if(probe_inds(3).ge.kstart .and. probe_inds(3).le.kend) then
!-------------FORCING FUNCTION------------------------
! volume of a face with a specific marker - thickness taken as average of grid spacing
!WGHT1
!pind_i(1)=pindv(1,nv,inp)-1; pind_o(1)=pindv(1,nv,inp)+1
!pind_i(2)=pindv(2,nv,inp)-1; pind_o(2)=pindv(2,nv,inp)+1
!pind_i(3)=pind(3,ntr,inp)-1; pind_o(3)=pind(3,ntr,inp)+1
pind_i(1)=probe_inds(1)-1; pind_o(1)=probe_inds(1)+1
pind_i(2)=probe_inds(2)-1; pind_o(2)=probe_inds(2)+1
pind_i(3)=probe_inds(3)-1; pind_o(3)=probe_inds(3)+1
k1 = floor(pos(3)*dx3) + 1
pind_i(3)=k1-1
pind_o(3)=k1+1
! For the spatial grid
k_inds = [k1-1,k1,k1]
! For the Eulerian field
k1 = modulo(k1-1,n3m) + 1
keul_inds = [k1-1,k1,k1+1]
pinvA(1:4,1:4)=0.0d0 ! Is summed in the loop below
dAdx(1:4,1:4)=0.0d0
dAdy(1:4,1:4)=0.0d0
dAdz(1:4,1:4)=0.0d0
inw = 1
! Accumulate A(4,4) , B(4,27) linear system
! Likewise for derivatives of A, B
!do k=pind_i(3), pind_o(3)
do nk = 1,3 ! Different to apply periodicty in z
k = k_inds(nk)
norp(3)=abs( pos(3) - zm(k) ) / (wscl / dx3)
Wt(3) = mls_gaussian( norp(3) , wcon )
if ( norp(3) .lt. EPSILON(1.0d0) ) then
dWz_dz = 0.0
else
dWz_dz = mls_gauss_deriv(norp(3),wcon) ! dWz / dr_z
dWz_dz = dWz_dz * ( ( pos(3) - zm(k) ) / abs( pos(3) - zm(k) ) / (wscl/dx3) ) ! dWz/dz = ( dWz / dr_z ) * ( dr_z / dz )
endif
do j=pind_i(2), pind_o(2)
norp(2)=abs( pos(2) - ym(j) ) / (wscl / dx2)
Wt(2) = mls_gaussian( norp(2) , wcon )
Wt23 = Wt(2)*Wt(3)
if ( norp(2) .lt. EPSILON(1.0d0) ) then
dWy_dy = 0.0
else
dWy_dy = mls_gauss_deriv(norp(2),wcon) ! dWy / dr_y
dWy_dy = dWy_dy * ( ( pos(2) - ym(j) ) / abs( pos(2) - ym(j) ) / (wscl/dx2) ) ! dWy/dy = ( dWy / dr_y ) * ( dr_y / dy )
endif
do i=pind_i(1), pind_o(1)
norp(1)=abs(pos(1) - xm(i) ) / (wscl / dx1)
Wt(1) = mls_gaussian( norp(1) , wcon )
Wtx = Wt(1)*Wt23 !Eq. (3.165) Liu & Gu (2005)
if ( norp(1) .lt. EPSILON(1.0d0) ) then
dWx_dx = 0.0
else
dWx_dx = mls_gauss_deriv(norp(1),wcon) ! dWx / dr_x
dWx_dx = dWx_dx * ( ( pos(1) - xm(i) ) / abs( pos(1) - xm(i) ) / (wscl/dx1) ) ! dWx/dx = ( dWx / dr_x ) * ( dr_x / dx )
endif
pxk(1)=1.0d0
pxk(2)=xm(i)
pxk(3)=ym(j)
pxk(4)=zm(k)
! C = alpha * A * B + beta * C
! Note how the result of the computation is stored in C
!DGEMM(transA,transB,numRowsA,numColsB,numColsA,alpha, A ,numRowsA, B , numRowsB, beta , C/result, numRowsC)
call DGEMM('N','T',4,4,1,Wtx,pxk,4,pxk,4, 1.0d0,pinvA,4) ! Accumulate A matrix ( eventually inverted to pinvA )
B(1:4,inw)=Wtx*pxk(1:4)
! Eq. (3.165) and Program 3.5, 3.6 in Liu & Gu 2005
dWdx = dWx_dx * Wt(2) * Wt(3)
dWdy = Wt(1) * dWy_dy * Wt(3)
dWdz = Wt(1) * Wt(2) * dWz_dz
call DGEMM('N','T',4,4,1,dWdx,pxk,4,pxk,4, 1.0d0,dAdx,4)
call DGEMM('N','T',4,4,1,dWdy,pxk,4,pxk,4, 1.0d0,dAdy,4)
call DGEMM('N','T',4,4,1,dWdz,pxk,4,pxk,4, 1.0d0,dAdz,4)
dBdx(1:4,inw)=dWdx * pxk(1:4)
dBdy(1:4,inw)=dWdy * pxk(1:4)
dBdz(1:4,inw)=dWdz * pxk(1:4)
!Tnel(inw) = temp(i,j,k) ! Store Eulerian support domain values for summation later
ii = modulo(i-1,n1m) + 1
jj = modulo(j-1,n2m) + 1
!kk = modulo(k-1,n3m) + 1
kk = keul_inds(nk)
!Tnel(inw) = temp(ii,jj,kk) ! Store Eulerian support domain values for summation later
Tnel(inw) = temp(ii,jj,kk) ! Store Eulerian support domain values for summation later
inw = inw + 1
enddo !end i
enddo !end j
enddo !end k
! calling routine to compute inverse
! SPD matrix for uniform grids, we can use Cholesky decomp. instead: dpotrf
!call inverseLU(pinvA,invA)
call dgetrf(4, 4, pinvA, 4, IPIV, INFO) ! Compute the LU factorization of I_ij
if (info /= 0) then
print *, "Error in dgetrf"
stop
end if
call dgetri(4, pinvA, 4, IPIV, WORK, LWORK, INFO) ! Invert the LU factorization
invA = pinvA
!---------------------LIN-ALG ROUTINES----------------------------
! matrix multiplications for final interpolation
!DGEMM(transA,transB,numRowsA,numColsB,numColsA,alpha, A ,numRowsA, B , numRowsB, beta , C/result, numRowsC)
! C = alpha * A * B + beta * C
! Note how the result of the computation is stored in C
!
!
! Matrix-vector operations: y = alpha * Ax + beta * y ; result stored in y incx incy are strides (should be 1)
!DGEMV(TPOSE?, nRowA, nColA, alpha, A, nRowA, x, incx, beta, y, incy)
!
! NB: for the intel MKL implementations, must have B =/= C ! But this is not the case for LAPACK
!---------------SHAPE FUNCTION DERIVATIVE CALCULATIONS---------------
! For evaluation of shape function derivatives, ddx_ptxAB, ddy_ptxAB, ddz_ptxAB
! See equations (3.138--3.141 , 3.144) in Liu & Gu (2005)
! Our MLS shape function ptxAB := Phi in their notation
! Notably, only inversion of matrix A is required in this framework (stored in invA)
! Also no need for evaluating the shape function (i.e. ptxAB), as we only need its derivatives here
! Eq. (3.139 / 3.140) Compute gamma = invA * p , result gamvec = size(4) vector
call DGEMV('N', 4, 4, 1.0d0, invA, 4, ptx, 1, 0.0d0, gamvec, 1 )
! Now compute derivatives of gamma for use in (3.141)
!---------------------- x derivative ddx_ptxAB-------------------------------------------
pxk = [0.0d0,1.0d0,0.0d0,0.0d0] ! Store d/dx(ptx) initially, re-use pxk memory
call DGEMV('N', 4, 4, -1.0d0, dAdx, 4, gamvec, 1, 1.0d0, pxk, 1 ) ! Get -dAdx*gamma + dpdx first, store in pxk
!Now get dgamdx = Ainv * (-dAdx*gamma + dpdx) where (-dAdx*gamma + dpdx) was computed above and is stored in pxk
call DGEMV('N', 4, 4, 1.0d0, invA, 4, pxk, 1, 0.0d0, dgamdx, 1 )
! Compute MLS shape function derivative ddx_ptxAB = dgamdx^T*B + gamma^T*dBdx
call DGEMM('N', 'N', 1, nel, 4, 1.0d0, dgamdx, 1, B, 4, 0.0d0, ddx_ptxAB, 1) ! Term dgamdx^T*B
call DGEMM('N', 'N', 1, nel, 4, 1.0d0, gamvec, 1, dBdx, 4 , 1.0d0 ,ddx_ptxAB, 1) ! += gamma^T*dBdx
!---------------------- y derivative ddy_ptxAB-------------------------------------------
pxk = [0.0d0,0.0d0,1.0d0,0.0d0] ! Store d/dy(ptx) initially
call DGEMV('N', 4, 4, -1.0d0, dAdy, 4, gamvec, 1, 1.0d0, pxk, 1 )
call DGEMV('N', 4, 4, 1.0d0, invA, 4, pxk, 1, 0.0d0, dgamdy, 1 )
call DGEMM('N', 'N', 1, nel, 4, 1.0d0, dgamdy, 1, B, 4, 0.0d0, ddy_ptxAB, 1)
call DGEMM('N', 'N', 1, nel, 4, 1.0d0, gamvec, 1, dBdy, 4 , 1.0d0 ,ddy_ptxAB, 1)
!---------------------- z derivative ddz_ptxAB-------------------------------------------
pxk = [0.0d0,0.0d0,0.0d0,1.0d0] ! Store d/dz(ptx) initially
call DGEMV('N', 4, 4, -1.0d0, dAdz, 4, gamvec, 1, 1.0d0, pxk, 1 )
call DGEMV('N', 4, 4, 1.0d0, invA, 4, pxk, 1, 0.0d0, dgamdz, 1 )
call DGEMM('N', 'N', 1, nel, 4, 1.0d0, dgamdz, 1, B, 4, 0.0d0, ddz_ptxAB, 1)
call DGEMM('N', 'N', 1, nel, 4, 1.0d0, gamvec, 1, dBdz, 4 , 1.0d0 ,ddz_ptxAB, 1)
!-------------------- Now compute derivatives at the marker location---------------------
! e.g. Vanella & Balaras (2009) equation (27)
gradT(1) = sum( ddx_PtxAB * Tnel) !dT dx
gradT(2) = sum( ddy_PtxAB * Tnel) !dT dy
gradT(3) = sum( ddz_PtxAB * Tnel) !dT dz
endif
end subroutine wght_gradT
subroutine faceToVert_interp(vert_of_face,Atri,Avert,qw_F,qw_v,nv)
! For a SINGLE triangle/face, spreads the area-weighted heat flux and area to its 3 vertices
implicit none
integer :: i, nv
integer, dimension(3) :: vert_of_face ! 3 vertices of the specified triangle
real, intent(in) :: Atri, qw_F
real, dimension(nv), intent(out) :: qw_v ! Vertex heat flux
real, dimension(nv), intent(in) :: Avert ! Area associated with vertices
do i = 1,3 !vertices v1, v2, v3
qw_v( vert_of_face(i) ) = qw_v( vert_of_face(i) ) + qw_F * ( Atri / 3.0 / Avert( vert_of_face(i) ) )
!qw_v( vert_of_face(i) ) = qw_v( vert_of_face(i) ) + qw_F * ( Atri / Avert( vert_of_face(i) ) )
enddo
end subroutine faceToVert_interp