forked from festim-dev/FESTIM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheat_transfer_problem.py
More file actions
265 lines (222 loc) · 8.73 KB
/
heat_transfer_problem.py
File metadata and controls
265 lines (222 loc) · 8.73 KB
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
import basix
import ufl
from dolfinx import fem
from dolfinx.io import VTXWriter
from festim import boundary_conditions, exports, helpers, problem
from festim import source as _source
class HeatTransferProblem(problem.ProblemBase):
def __init__(
self,
mesh=None,
subdomains=None,
initial_condition=None,
boundary_conditions=None,
sources=None,
exports=None,
settings=None,
) -> None:
super().__init__(
mesh=mesh,
sources=sources,
exports=exports,
subdomains=subdomains,
boundary_conditions=boundary_conditions,
settings=settings,
)
self.initial_condition = initial_condition
self._vtxfile: VTXWriter | None = None
@property
def sources(self):
return self._sources
@sources.setter
def sources(self, value):
if not all(isinstance(source, _source.HeatSource) for source in value):
raise TypeError("sources must be a list of festim.HeatSource objects")
self._sources = value
@property
def boundary_conditions(self):
return self._boundary_conditions
@boundary_conditions.setter
def boundary_conditions(self, value):
if not all(
isinstance(
bc,
(
boundary_conditions.FixedTemperatureBC,
boundary_conditions.HeatFluxBC,
),
)
for bc in value
):
raise TypeError(
"boundary_conditions must be a list of festim.FixedTemperatureBC or festim.HeatFluxBC objects"
)
self._boundary_conditions = value
def initialise(self):
self.define_function_space()
self.define_meshtags_and_measures()
self.t = fem.Constant(self.mesh.mesh, 0.0)
if self.settings.transient:
# TODO should raise error if no stepsize is provided
# TODO Should this be an attribute of festim.Stepsize?
self.dt = helpers.as_fenics_constant(
self.settings.stepsize.initial_value, self.mesh.mesh
)
self.define_boundary_conditions()
self.create_source_values_fenics()
self.create_flux_values_fenics()
self.create_initial_conditions()
self.create_formulation()
self.create_solver()
self.initialise_exports()
def define_function_space(self):
"""Creates the function space of the model, creates a mixed element if
model is multispecies. Creates the main solution and previous solution
function u and u_n. Create global DG function spaces of degree 0 and 1
for the global diffusion coefficient"""
degree = 1
element_CG = basix.ufl.element(
basix.ElementFamily.P,
self.mesh.mesh.basix_cell(),
degree,
basix.LagrangeVariant.equispaced,
)
self.function_space = fem.functionspace(self.mesh.mesh, element_CG)
self.u = fem.Function(self.function_space)
self.u_n = fem.Function(self.function_space)
self.test_function = ufl.TestFunction(self.function_space)
def create_dirichletbc_form(self, bc):
"""Creates a dirichlet boundary condition form
Args:
bc (festim.FixedTemperatureBC): the boundary condition
Returns:
dolfinx.fem.bcs.DirichletBC: A representation of
the boundary condition for modifying linear systems.
"""
bc.create_value(
function_space=self.function_space,
t=self.t,
)
bc_dofs = bc.define_surface_subdomain_dofs(
facet_meshtags=self.facet_meshtags,
function_space=self.function_space,
)
if isinstance(bc.value_fenics, (fem.Function)):
form = fem.dirichletbc(value=bc.value_fenics, dofs=bc_dofs)
else:
form = fem.dirichletbc(
value=bc.value_fenics, dofs=bc_dofs, V=self.function_space
)
return form
def create_source_values_fenics(self):
"""For each source create the value_fenics"""
for source in self.sources:
# create value_fenics for all source objects
source.value.convert_input_value(
mesh=self.mesh.mesh,
t=self.t,
up_to_ufl_expr=True,
)
def create_flux_values_fenics(self):
"""For each heat flux create the value_fenics"""
for bc in self.boundary_conditions:
# create value_fenics for all F.HeatFluxBC objects
if isinstance(bc, boundary_conditions.HeatFluxBC):
bc.create_value_fenics(
mesh=self.mesh.mesh,
temperature=self.u,
t=self.t,
)
def create_initial_conditions(self):
"""For each initial condition, create the value_fenics and assign it to
the previous solution of the condition's species"""
if not self.initial_condition:
return
if not self.settings.transient:
raise ValueError(
"Initial conditions can only be defined for transient simulations"
)
# create value_fenics for condition
self.initial_condition.create_expr_fenics(
mesh=self.mesh.mesh,
function_space=self.function_space,
)
# assign to previous solution of species
self.u_n.interpolate(self.initial_condition.expr_fenics)
def create_formulation(self):
"""Creates the formulation of the model"""
self.formulation = 0
# add diffusion and time derivative for each species
for vol in self.volume_subdomains:
thermal_cond = vol.material.thermal_conductivity
if callable(thermal_cond):
thermal_cond = thermal_cond(self.u)
self.formulation += ufl.dot(
thermal_cond * ufl.grad(self.u), ufl.grad(self.test_function)
) * self.dx(vol.id)
if self.settings.transient:
density = vol.material.density
heat_capacity = vol.material.heat_capacity
if callable(density):
density = density(self.u)
if callable(heat_capacity):
heat_capacity = heat_capacity(self.u)
self.formulation += (
density
* heat_capacity
* ((self.u - self.u_n) / self.dt)
* self.test_function
* self.dx(vol.id)
)
# add sources
for source in self.sources:
self.formulation -= (
source.value.fenics_object
* self.test_function
* self.dx(source.volume.id)
)
# add fluxes
for bc in self.boundary_conditions:
if isinstance(bc, boundary_conditions.HeatFluxBC):
self.formulation -= (
bc.value_fenics * self.test_function * self.ds(bc.subdomain.id)
)
def initialise_exports(self):
"""Defines the export writers of the model, if field is given as
a string, find species object in self.species"""
for export in self.exports:
if isinstance(export, exports.XDMFExport):
raise NotImplementedError(
"XDMF export is not implemented yet for heat transfer problems"
)
if isinstance(export, exports.VTXTemperatureExport):
self._vtxfile = VTXWriter(
self.u.function_space.mesh.comm,
export.filename,
[self.u],
engine="BP5",
)
def post_processing(self):
"""Post processes the model"""
for export in self.exports:
# TODO if export type derived quantity
if isinstance(export, exports.SurfaceQuantity):
raise NotImplementedError(
"SurfaceQuantity export is not implemented yet for heat transfer problems"
)
export.compute(
self.mesh.n,
self.ds,
)
# update export data
export.t.append(float(self.t))
# if filename given write export data to file
if export.filename is not None:
export.write(t=float(self.t))
if isinstance(export, exports.XDMFExport):
export.write(float(self.t))
if self._vtxfile is not None:
self._vtxfile.write(float(self.t))
def __del__(self):
if self._vtxfile is not None:
self._vtxfile.close()