-
Notifications
You must be signed in to change notification settings - Fork 25
/
surface_flux.py
292 lines (247 loc) · 9.46 KB
/
surface_flux.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
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
from festim import SurfaceQuantity, k_B
import fenics as f
import numpy as np
class SurfaceFlux(SurfaceQuantity):
"""
Computes the surface flux of a field at a given surface in cartesian coordinates
Args:
field (str, int): the field ("solute", 0, 1, "T", "retention")
surface (int): the surface id
Attributes:
field (str, int): the field ("solute", 0, 1, "T", "retention")
surface (int): the surface id
export_unit (str): the unit of the derived quantity in the export file
title (str): the title of the derived quantity
show_units (bool): show the units in the title in the derived quantities
file
function (dolfin.function.function.Function): the solution function of
the field
.. note::
Object to compute the flux J of a field u through a surface
J = integral(+prop * grad(u) . n ds)
where prop is the property of the field (D, thermal conductivity, etc)
u is the field
n is the normal vector of the surface
ds is the surface measure.
units are in H/m2/s in 1D, H/m/s in 2D and H/s in 3D domains for hydrogen
concentration and W/m2 in 1D, W/m in 2D and W in 3D domains for temperature
"""
def __init__(self, field, surface) -> None:
super().__init__(field=field, surface=surface)
self.soret = None
@property
def allowed_meshes(self):
return ["cartesian"]
@property
def export_unit(self):
# obtain domain dimension
dim = self.function.function_space().mesh().topology().dim()
# return unit depending on field and dimension of domain
if self.field == "T":
return f"W m{dim-3}".replace(" m0", "")
else:
return f"H m{dim-3} s-1".replace(" m0", "")
@property
def title(self):
quantity_title = f"Flux surface {self.surface}: {self.field}"
if self.show_units:
if self.field == "T":
quantity_title = f"Heat flux surface {self.surface}"
else:
quantity_title = f"{self.field} flux surface {self.surface}"
return quantity_title + f" ({self.export_unit})"
else:
return quantity_title
@property
def prop(self):
field_to_prop = {
"0": self.D,
"solute": self.D,
0: self.D,
"T": self.thermal_cond,
}
return field_to_prop[self.field]
def compute(self):
flux = f.assemble(
self.prop * f.dot(f.grad(self.function), self.n) * self.ds(self.surface)
)
if self.soret and self.field in [0, "0", "solute"]:
flux += f.assemble(
self.prop
* self.function
* self.Q
/ (k_B * self.T**2)
* f.dot(f.grad(self.T), self.n)
* self.ds(self.surface)
)
return flux
class SurfaceFluxCylindrical(SurfaceFlux):
"""
Object to compute the flux J of a field u through a surface
J = integral(-prop * grad(u) . n ds)
where prop is the property of the field (D, thermal conductivity, etc)
u is the field
n is the normal vector of the surface
ds is the surface measure in cylindrical coordinates.
ds = r dr dtheta or ds = r dz dtheta
.. note::
For particle fluxes J is given in H/s, for heat fluxes J is given in W
Args:
field (str, int): the field ("solute", 0, 1, "T", "retention")
surface (int): the surface id
azimuth_range (tuple, optional): Range of the azimuthal angle
(theta) needs to be between 0 and 2 pi. Defaults to (0, 2 * np.pi).
"""
def __init__(self, field, surface, azimuth_range=(0, 2 * np.pi)) -> None:
super().__init__(field=field, surface=surface)
self.r = None
self.azimuth_range = azimuth_range
@property
def export_unit(self):
# obtain domain dimension
dim = self.function.function_space().mesh().topology().dim()
# return unit depending on field and dimension of domain
if self.field == "T":
return f"W m{dim-2}".replace(" m0", "")
else:
return f"H m{dim-2} s-1".replace(" m0", "")
@property
def allowed_meshes(self):
return ["cylindrical"]
@property
def title(self):
if self.field == "T":
quantity_title = f"Heat flux surface {self.surface}"
else:
quantity_title = f"{self.field} flux surface {self.surface}"
if self.show_units:
return quantity_title + f" ({self.export_unit})"
else:
return quantity_title
@property
def azimuth_range(self):
return self._azimuth_range
@azimuth_range.setter
def azimuth_range(self, value):
if value[0] < 0 or value[1] > 2 * np.pi:
raise ValueError("Azimuthal range must be between 0 and pi")
self._azimuth_range = value
def compute(self):
if self.r is None:
mesh = (
self.function.function_space().mesh()
) # get the mesh from the function
rthetaz = f.SpatialCoordinate(mesh) # get the coordinates from the mesh
self.r = rthetaz[0] # only care about r here
# dS_z = r dr dtheta , assuming axisymmetry dS_z = theta r dr
# dS_r = r dz dtheta , assuming axisymmetry dS_r = theta r dz
# in both cases the expression with self.ds is the same
flux = f.assemble(
self.prop
* self.r
* f.dot(f.grad(self.function), self.n)
* self.ds(self.surface)
)
if self.soret and self.field in [0, "0", "solute"]:
flux += f.assemble(
self.prop
* self.r
* self.function
* self.Q
/ (k_B * self.T**2)
* f.dot(f.grad(self.T), self.n)
* self.ds(self.surface)
)
flux *= self.azimuth_range[1] - self.azimuth_range[0]
return flux
class SurfaceFluxSpherical(SurfaceFlux):
"""
Object to compute the flux J of a field u through a surface
J = integral(-prop * grad(u) . n ds)
where prop is the property of the field (D, thermal conductivity, etc)
u is the field
n is the normal vector of the surface
ds is the surface measure in spherical coordinates.
ds = r^2 sin(theta) dtheta dphi
.. note::
For particle fluxes J is given in H/s, for heat fluxes J is given in W
Args:
field (str, int): the field ("solute", 0, 1, "T", "retention")
surface (int): the surface id
azimuth_range (tuple, optional): Range of the azimuthal angle
(phi) needs to be between 0 and pi. Defaults to (0, np.pi).
polar_range (tuple, optional): Range of the polar angle
(theta) needs to be between - pi and pi. Defaults to (-np.pi, np.pi).
"""
def __init__(
self, field, surface, azimuth_range=(0, np.pi), polar_range=(-np.pi, np.pi)
) -> None:
super().__init__(field=field, surface=surface)
self.r = None
self.polar_range = polar_range
self.azimuth_range = azimuth_range
@property
def export_unit(self):
if self.field == "T":
return f"W"
else:
return f"H s-1"
@property
def allowed_meshes(self):
return ["spherical"]
@property
def title(self):
if self.field == "T":
quantity_title = f"Heat flux surface {self.surface}"
else:
quantity_title = f"{self.field} flux surface {self.surface}"
if self.show_units:
return quantity_title + f" ({self.export_unit})"
else:
return quantity_title
@property
def polar_range(self):
return self._polar_range
@polar_range.setter
def polar_range(self, value):
if value[0] < -np.pi or value[1] > np.pi:
raise ValueError("Polar range must be between - pi and pi")
self._polar_range = value
@property
def azimuth_range(self):
return self._azimuth_range
@azimuth_range.setter
def azimuth_range(self, value):
if value[0] < 0 or value[1] > np.pi:
raise ValueError("Azimuthal range must be between 0 and pi")
self._azimuth_range = value
def compute(self):
if self.r is None:
mesh = (
self.function.function_space().mesh()
) # get the mesh from the function
rthetaphi = f.SpatialCoordinate(mesh) # get the coordinates from the mesh
self.r = rthetaphi[0] # only care about r here
# dS_r = r^2 sin(theta) dtheta dphi
# integral(f dS_r) = integral(f r^2 sin(theta) dtheta dphi)
# = (phi2 - phi1) * (-cos(theta2) + cos(theta1)) * f r^2
flux = f.assemble(
self.prop
* self.r**2
* f.dot(f.grad(self.function), self.n)
* self.ds(self.surface)
)
if self.soret and self.field in [0, "0", "solute"]:
flux += f.assemble(
self.prop
* self.r**2
* self.function
* self.Q
/ (k_B * self.T**2)
* f.dot(f.grad(self.T), self.n)
* self.ds(self.surface)
)
flux *= (self.polar_range[1] - self.polar_range[0]) * (
-np.cos(self.azimuth_range[1]) + np.cos(self.azimuth_range[0])
)
return flux