-
Notifications
You must be signed in to change notification settings - Fork 0
/
ach_model.py
187 lines (153 loc) · 6.28 KB
/
ach_model.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
"""
This is the code for Dendritic Growth benchmark.
The domain is a square with no-flux bcs.
Follow the link for the description of the benchmark:
https://pages.nist.gov/pfhub/benchmarks/benchmark3.ipynb/
"""
from collections import namedtuple
import firedrake as fd
import ufl
class ProblemModel:
"""Base class for problem formulation.
Attributes:
residual (Form): Residual formulation for the problem.
jacobian (Form): Jacobian of the problem.
solution_function (Function): Current time step solution.
solutiondot_function (Function): Current time derivative solution.
initial_function (Function): Initial.
"""
def __init__(self):
self._residual = None
self._jacobian = None
self._solution_function = None
self._solutiondot_function = None
self._initial_function = None
@property
def residual_form(self):
"""Return the residual.
Returns:
Form: UFL form representing the specified residual formulation.
Raises:
NotImplementedError: If the residual is not set.
"""
return self._residual or NotImplementedError
@property
def jacobian_form(self):
"""Return the Jacobian.
Returns:
Form: UFL form representing the Jacobian.
Raises:
NotImplementedError: If the Jacobian is not set.
"""
return self._jacobian or NotImplementedError
@property
def solution_function(self):
"""Return the current time step solution.
Returns:
Function: Current time step solution.
Raises:
NotImplementedError: If the solution_function is not set.
"""
return self._solution_function or NotImplementedError
@property
def solutiondot_function(self):
"""Return the current time derivative.
Returns:
Function: Current time derivative.
Raises:
NotImplementedError: If the solution_function is not set.
"""
return self._solutiondot_function or NotImplementedError
class AllenCahnHeatModel(ProblemModel):
def __init__(self, function_space, model_parameters):
super(AllenCahnHeatModel, self).__init__()
self._solution_function = fd.Function(function_space)
self._solutiondot_function = fd.Function(function_space)
test_function = fd.TestFunction(function_space)
# Split mixed FE function into separate functions and put them into namedtuple
FiniteElementFunction = namedtuple("FiniteElementFunction", ["phi", "T"])
split_solution_function = FiniteElementFunction(
*ufl.split(self._solution_function)
)
split_solutiondot_function = FiniteElementFunction(
*ufl.split(self._solutiondot_function)
)
split_test_function = FiniteElementFunction(*ufl.split(test_function))
finite_element_functions = (
split_solution_function,
split_solutiondot_function,
split_test_function,
)
F_AC = get_allen_cahn_weak_form(*finite_element_functions, model_parameters)
F_heat = get_heat_weak_form(*finite_element_functions, model_parameters)
F = F_AC + F_heat
self._residual = F
def get_allen_cahn_weak_form(
solution_functions, solutiondot_functions, test_functions, model_parameters
):
"""Return a weak form for Allen-Cahn equation.
Allen-Cahn (AC) is solved to obtain the phase field.
Args:
solution_functions (tuple(Function)): Current time step solution.
solutiondot_functions (tuple(Function)): Current time derivative.
test_functions (tuple(TestFunction)): An appropriate test function.
model_parameters (Dictionary): Model parameters.
Returns:
Form: UFL form representing the Allen-Cahn in residual formulation.
"""
try:
# Free energy density function may depend on temperature
T = solution_functions.T
dTdt = solutiondot_functions.T
except AttributeError:
raise AttributeError(
"Current implementation Allen-Cahn model expects \
Temperature function to couple with \
(LocalFreeEnergyDensityFunction is assumed to \
depend both on phase field and temperature functions)"
)
T = None
dTdt = None
phi = solution_functions.phi
dphi_dt = solutiondot_functions.phi
phi_ = test_functions.phi
f_chem = model_parameters["LocalFreeEnergyDensityFunction"]
tau = model_parameters["KineticCoefficient"]
kappa = model_parameters["GradientEnergyCoefficient"]
inner, grad, dx = ufl.inner, ufl.grad, ufl.dx
F_total = 0.5 * kappa(phi) * inner(grad(phi), grad(phi)) + f_chem(phi, T)
# Functional derivative of F_total w.r.t phi in the test function direction phi_
dFdphi = ufl.derivative(F_total, phi, phi_)
# firedrake fails without the following call
dFdphi = ufl.algorithms.expand_derivatives(dFdphi)
F_AC = tau(phi) * inner(dphi_dt, phi_) * dx + dFdphi * dx
return F_AC
def get_heat_weak_form(
solution_functions, solutiondot_functions, test_functions, model_parameters
):
"""Return a weak form for convective Heat equation.
The heat equation is solved to get temperature field. The equation is in the convective form.
Solid-Liquid phase transition is modeled using nonlinear latent heat source.
Args:
solution_functions (tuple(Function)): Current time step solution.
solutiondot_functions (tuple(Function)): Current time derivative.
test_functions (tuple(TestFunction)): An appropriate test function.
model_parameters (Dictionary): Model parameters.
Returns:
Form: UFL form representing the heat equation in residual formulation.
"""
try:
# Heat equation may depend on phase field
phi = solution_functions.phi
dphi_dt = solutiondot_functions.phi
except AttributeError:
phi = None
T = solution_functions.T
dT_dt = solutiondot_functions.T
T_ = test_functions.T
kappa = model_parameters["ThermalDiffusion"]
inner, grad, dx = ufl.inner, ufl.grad, ufl.dx
F_heat = inner(dT_dt, T_) * dx + inner(kappa(T) * grad(T), grad(T_)) * dx
if phi is not None:
F_heat += -0.5 * dphi_dt * T_ * dx
return F_heat