-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathTemperature.jl
222 lines (202 loc) · 8.89 KB
/
Temperature.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
213
214
215
216
217
218
219
220
221
222
# Define a function to form the energy equation left hand side and right hand side
function assemble_energy_equation_center(grid::CartesianGrid,rho_c::Matrix{Float64},Cp_c::Matrix{Float64},kThermal::Matrix{Float64},H::Matrix{Float64},Tlast::Matrix{Float64},dt::Float64,bctype,bcval)
# Assemble the left hand side for the energy equation
# Inputs:
# grid - this is the CartesianGrid
# rho_c - density at the cell centers
# Cp_c - heat capacity at the cell centers
# kThermal- thermal conductivity at the basic nodes
# H - volumetric rate of internal heating at the cell centers
# Tlast - the previous-timestep temperature.
# dt - the time step
# bctype - a vector containing a flag (-1=prescribed gradient, +1=prescribed value) for [left,right,top,bottom]
# bcval - a vector containing the temperature or normal gradient values at the [left, right, top, bottom]
# Returns:
# L,R, the left hand side and right hand side of the energy equation
bcleft = bctype[1] # -1 = insulating, 1 = constant temp
bcright = bctype[2] #
bctop = bctype[3]
bcbottom = bctype[4]
# bcval should contain temperature or dT/dx values for left,right,top,bottom
N = grid.nx*grid.ny
row = zeros(Int64,5*N);
col = zeros(Int64,5*N);
val = zeros(Float64, 5*N);
R = zeros(Float64,N,1);
k = 1;
# j-1 j j j+1 j+1
# i-1 |----kC----|----vy----|
# | | |
# i kA T kB T |
# | | |
# i |----kD--(i,j)--vy----|
# | | |
# i+1 vx c vx c vx
# | | |
# i+1 |----vy----|----vy----|
for j in 1:grid.nx
dxc = j>1 ? grid.x[j] - grid.x[j-1] : grid.x[j+1] - grid.x[j]
dxp = grid.xc[j+1] - grid.xc[j]
dxm = j>1 ? grid.xc[j]-grid.xc[j-1] : grid.xc[j+1] - grid.xc[j]
for i in 1:grid.ny
dyc = i>1 ? grid.y[i] - grid.y[i-1] : grid.y[i+1] - grid.y[i]
dyp = grid.yc[i+1] - grid.yc[i]
dym = i>1 ? grid.yc[i]-grid.yc[i-1] : grid.yc[i+1] - grid.yc[i]
this_row = node_index(i,j,grid.ny);
if i==1 # ghost nodes along top.
row[k] = this_row
col[k] = this_row
val[k] = 1.0/2.0
k+=1
row[k] = this_row
col[k] = node_index(i+1,j,grid.ny)
val[k] = bctop/2.0
k+=1
R[this_row] = bcval[3]
elseif j==1 # ghost nodes along left side.
row[k] = this_row
col[k] = this_row
val[k] = 1.0/2.0
k+=1
row[k] = this_row
col[k] = node_index(i,j+1,grid.ny)
val[k] = bcleft/2.0
k+=1
R[this_row] = bcval[1]
else
# kA, kB, kC, kD
kA = 0.5*(kThermal[i-1,j-1] + kThermal[i,j-1])
kB = 0.5*(kThermal[i,j] + kThermal[i-1,j])
kC = 0.5*(kThermal[i-1,j-1] + kThermal[i-1,j])
kD = 0.5*(kThermal[i,j-1] + kThermal[i,j])
#rho_c = 0.25*(rho[i-1,j-1] + rho[i,j-1] + rho[i-1,j] + rho[i,j])
#Cp_c = 0.25*(Cp[i-1,j-1] + Cp[i,j-1] + Cp[i-1,j] + Cp[i,j])
# diagonal entry
row[k] = this_row;
col[k] = this_row;
val[k] = (rho_c[i,j]*Cp_c[i,j])/dt + kB/dxp/dxc + kA/dxm/dxc + kD/dyp/dyc + kC/dyp/dyc;
k+=1
# right
row[k] = this_row;
col[k] = j==grid.nx ? node_index(i,j,grid.ny) : node_index(i,j+1,grid.ny);
val[k] = j==grid.nx ? bcright*kB/dxp/dxc : -kB/dxp/dxc;
k+=1
# left
row[k] = this_row;
col[k] = node_index(i,j-1,grid.ny);
val[k] = -kA/dxm/dxc;
k+=1
# down (+y)
row[k] = this_row;
col[k] = i==grid.ny ? node_index(i,j,grid.ny) : node_index(i+1,j,grid.ny);
val[k] = i==grid.ny ? bcbottom*kD/dyp/dyc : -kD/dyp/dyc;
k+=1
# up (-y)
row[k] = this_row;
col[k] = node_index(i-1,j,grid.ny);
val[k] = -kC/dyp/dyc;
k+=1
R[this_row] = Tlast[i,j]*rho_c[i,j]*Cp_c[i,j]/dt + H[i,j];
if j==grid.nx
R[this_row] += 2*bcval[2]*bcright*kB/dxp/dxc
end
if i==grid.ny
R[this_row] += 2*bcval[4]*bcbottom*kD/dyp/dyc
end
end
end
end
row = @views row[1:k-1]
col = @views col[1:k-1]
val = @views val[1:k-1]
L = sparse(row,col,val)
return L,R
end
function ghost_temperature_center(grid::CartesianGrid,T::Matrix{Float64},bctype,bcval)
# Define a new grid that is (ny+1)x(nx+1) and insert the ghost temperature values.
# bcval shoud be a vector containing the temperatures or temperature gradients
# along the left, right, top, and bottom (in that order)
bcleft = bctype[1] # -1 = insulating, 1 = constant temp
bcright = bctype[2]
bctop = bctype[3]
bcbottom = bctype[4]
#bcval = [0.0,0.0,1000.0,1000.0] # left,right,top,bottom
Tpad = Array{Float64,2}(undef,grid.ny+1,grid.nx+1)
Tpad[1:grid.ny,1:grid.nx] = T[1:grid.ny,1:grid.nx]
# enforce BCs along top and bottom.
if bctop == 1
Tpad[1,2:grid.nx] = 2.0*bcval[3] .- Tpad[2,2:grid.nx]
elseif bctop == -1
Tpad[1,2:grid.nx] = Tpad[2,2:grid.nx] .- ((grid.yc[2]-grid.yc[1]) * bcval[3])
end
# bottom
if bcbottom == 1
Tpad[grid.ny+1,2:grid.nx] = 2.0*bcval[4] .- Tpad[grid.ny,2:grid.nx]
elseif bcbottom == -1
Tpad[grid.ny+1,2:grid.nx] = Tpad[grid.ny,2:grid.nx] .+ ((grid.yc[grid.ny+1]-grid.yc[grid.ny]) * bcval[4])
end
# Left boundary
if bcleft == -1
Tpad[:,1] = Tpad[:,2] # insulating
elseif bcleft == 1
println("assigning left boundary temperature ",bcval[1])
Tpad[:,1] = 2.0*bcval[1] .- Tpad[:,2]
end
# Right boundary
if bcright == -1
Tpad[:,grid.nx+1] = Tpad[:,grid.nx] # insulating
elseif bcright == 1
Tpad[:,grid.nx+1] = 2.0*bcval[2] .- Tpad[:,grid.nx]
end
return Tpad
end
function temperature_to_basic_nodes(grid::CartesianGrid,Tc::Matrix{Float64})
# Interpolate temperature from the cell centers to the basic nodes.
# Tc should be cell centered values including ghost values outside domain.
if size(Tc,1) != grid.ny+1 || size(Tc,2) != grid.nx + 1
error("temperature array needs to contain ghost values")
end
Tn = zeros(grid.ny,grid.nx)
Threads.@threads for i in 1:grid.ny
for j in 1:grid.nx
Tn[i,j] = 0.25*(Tc[i,j] + Tc[i,j+1] + Tc[i+1,j] + Tc[i+1,j+1])
end
end
return Tn
end
function subgrid_temperature_relaxation_center!(markers::Markers,grid::CartesianGrid,Tlast::Matrix,dt::Float64;diffusivity::Float64=1.0)
# Perform the sub-grid scale temperature diffusion operation
# Inputs:
# markers - the markers
# grid - the grid
# Tlast - the previous timestep temperature solution at the cell centers
# Cp (scalar) heat capacity
# kthermal (scalar) thermal diffusivity
# dt - the time step
# Returns:
# a matrix whose values are the change in temperature at the cell centers
dsubgrid = diffusivity; # subgrid temperature diffusivity
dT_subgrid_m = Array{Float64,2}(undef,1,markers.nmark)
# compuate the nodal temperature on the markers.
cell_center_to_markers!(markers,grid,Tlast,dT_subgrid_m)
# compute the subgrid temperature changes on the markers
rho = markers.scalarFields["rho"]
Cp = markers.scalarFields["Cp"]
T = markers.scalarFields["T"]
kThermal = markers.scalarFields["kThermal"]
Threads.@threads for i in 1:markers.nmark
dx2 = (grid.x[markers.cell[1,i]+1] - grid.x[markers.cell[1,i]])^2
dy2 = (grid.y[markers.cell[2,i]+1] - grid.y[markers.cell[2,i]])^2
tdiff = markers.scalars[rho,i]*markers.scalars[Cp,i]/markers.scalars[kThermal,i] / (2/dx2 + 2/dy2)
dT_subgrid_m[i] = (dT_subgrid_m[i]-markers.scalars[T,i])*( 1.0 - exp(-dsubgrid*dt/tdiff) )
end
# interpolate subgrid temperature changes back onto basic nodes.
markers.scalars[T,1:markers.nmark] += dT_subgrid_m[1,:]
# zero out nodal values for any cells without markers (nan values)
# dTm, = marker_to_cell_center(markers,grid,dT_subgrid_m)
# use a special weighted version of marker to stag:
rhocp = markers.scalars[rho,:] .* markers.scalars[Cp,:]
dTm, = marker_to_stag(markers,grid,dT_subgrid_m,"center",extra_weight=rhocp)
dTm[isnan.(dTm)] .= 0.0
return dTm
end