-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathex17.py
99 lines (72 loc) · 2.62 KB
/
ex17.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
r"""Insulated wire.
This example solves the steady heat conduction
with generation in an insulated wire. In radial
coordinates, the governing equations read: find :math:`T`
satisfying
.. math::
\nabla \cdot (k_0 \nabla T) + A = 0, \quad 0<r<a,
and
.. math::
\nabla \cdot (k_1 \nabla T) = 0, \quad a<r<b,
with the boundary condition
.. math::
k_1 \frac{\partial T}{\partial r} + h T = 0, \quad \text{on $r=b$}.
The parameter values are :math:`k_0 = 101`, :math:`k_1 = 11`, :math:`A = 5`,
:math:`h = 7`, and the geometry is defined as :math:`a=2` and :math:`b=3`.
For comparison purposes, the exact solution at the origin is
.. math::
T(r=0) = \frac{A b^2}{4 k_0} \left( \frac{2k_0}{bh} + \frac{2 k_0}{k_1} \log \frac{b}{a} + 1\right).
"""
from pathlib import Path
from typing import Optional
import numpy as np
from skfem import *
from skfem.helpers import dot, grad
from skfem.models.poisson import mass, unit_load
from skfem.io.json import from_file
joule_heating = 5.
heat_transfer_coefficient = 7.
thermal_conductivity = {'core': 101., 'annulus': 11.}
mesh = from_file(Path(__file__).parent / 'meshes' / 'disk.json')
radii = sorted([np.linalg.norm(mesh.p[:, mesh.t[:, s]], axis=0).max() for s in mesh.subdomains.values()])
@BilinearForm
def conduction(u, v, w):
return dot(w['conductivity'] * grad(u), grad(v))
convection = mass
element = ElementQuad1()
basis = Basis(mesh, element)
conductivity = basis.zero_w()
for subdomain, elements in mesh.subdomains.items():
conductivity[elements] = thermal_conductivity[subdomain]
L = asm(conduction, basis, conductivity=conductivity)
facet_basis = FacetBasis(mesh, element, facets=mesh.boundaries['perimeter'])
H = heat_transfer_coefficient * asm(convection, facet_basis)
core_basis = Basis(mesh, basis.elem, elements=mesh.subdomains['core'])
f = joule_heating * asm(unit_load, core_basis)
temperature = solve(L + H, f)
T0 = {
"skfem": (basis.probes(np.zeros((2, 1))) @ temperature)[0],
"exact": (
joule_heating
* radii[0] ** 2
/ 4
/ thermal_conductivity["core"]
* (
2 * thermal_conductivity["core"] / radii[1] / heat_transfer_coefficient
+ (
2
* thermal_conductivity["core"]
/ thermal_conductivity["annulus"]
* np.log(radii[1] / radii[0])
)
+ 1
)
),
}
if __name__ == '__main__':
from os.path import splitext
from sys import argv
from skfem.visuals.matplotlib import draw, plot, savefig
ax = draw(mesh)
plot(basis, temperature, ax=ax, colorbar=True)
savefig(splitext(argv[0])[0] + '_solution.png')