{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Stationnary non-linear heat transfer: 3D problem and performance comparisons\n",
"\n",
"## Description of the non-linear constitutive heat transfer law\n",
"\n",
"This example is a direct continuation of the [previous 2D example on non-linear heat transfer](./mgis_fenics_nonlinear_heat_transfer.ipynb). The present computations will use the same behaviour `StationaryHeatTransfer.mfront` which will be loaded with a `\"3d\"` hypothesis (default case).\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"## `FEniCS` implementation\n",
"\n",
"We now consider a portion of nuclear fuel rod (Uranium Dioxide $\\text{UO}_2$) subject to an external imposed temperature $T_{ext}=1000\\text{ K}$ and uniform volumetric heat source $r=300 \\text{ MW/m}^3$. From the steady state heat balance equation $\\operatorname{div}\\mathbf{j} = r$, the variational formulation is now:\n",
"\n",
"\\begin{equation}\n",
"F(\\widehat{T}) = \\int_\\Omega \\mathbf{j}(T,\\nabla T)\\cdot\\nabla \\widehat{T}\\,\\text{dx} + \\int_\\Omega r \\widehat{T} \\,\\text{dx}=0 \\quad \\forall \\widehat{T}\n",
"\\end{equation}\n",
"\n",
"which fits the general default format of a `MFrontNonlinearProblem`:\n",
"\\begin{equation}\n",
"F(\\widehat{u}) = \\sum_i \\int_\\Omega \\boldsymbol{\\sigma}_i(u)\\cdot \\mathbf{g}_i(\\widehat{u})\\,\\text{dx} -L(\\widehat{u}) =0 \\quad \\forall \\widehat{u}\n",
"\\end{equation}\n",
"\n",
"where $(\\boldsymbol{\\sigma}_i,\\mathbf{g}_i)$ are pairs of dual flux/gradient and here the external loading form $L$ is given by $-\\int_\\Omega r \\widehat{T} \\,\\text{dx}$. Compared to the previous example, we just add this source term using the `set_loading` method. Here we use a quadratic interpolation for the temperature field and external temperature is imposed on the surface numbered 12. Finally, we also rely on automatic registration of the gradient and external state variables as explained in the previous demo."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from dolfin import *\n",
"import mgis.fenics as mf\n",
"from time import time\n",
"\n",
"mesh = Mesh()\n",
"with XDMFFile(\"meshes/fuel_rod_mesh.xdmf\") as infile:\n",
" infile.read(mesh)\n",
"mvc = MeshValueCollection(\"size_t\", mesh, 2)\n",
"with XDMFFile(\"meshes/fuel_rod_mf.xdmf\") as infile:\n",
" infile.read(mvc, \"facets\")\n",
"facets = cpp.mesh.MeshFunctionSizet(mesh, mvc)\n",
"\n",
"V = FunctionSpace(mesh, \"CG\", 2)\n",
"T = Function(V, name=\"Temperature\")\n",
"T_ = TestFunction(V)\n",
"dT = TrialFunction(V)\n",
"T0 = Constant(300.)\n",
"T.interpolate(T0)\n",
"\n",
"Text = Constant(1e3)\n",
"bc = DirichletBC(V, Text, facets, 12)\n",
"\n",
"r = Constant(3e8)\n",
"\n",
"quad_deg = 2\n",
"material = mf.MFrontNonlinearMaterial(\"./src/libBehaviour.so\",\n",
" \"StationaryHeatTransfer\")\n",
"problem = mf.MFrontNonlinearProblem(T, material, quadrature_degree=quad_deg, bcs=bc)\n",
"problem.set_loading(-r*T*dx)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `solve` method computing time is monitored:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Automatic registration of 'TemperatureGradient' as grad(Temperature).\n",
"\n",
"Automatic registration of 'Temperature' as an external state variable.\n",
"\n",
"MFront/FEniCS solve time: 53.746278047561646\n"
]
}
],
"source": [
"tic = time()\n",
"problem.solve(T.vector())\n",
"print(\"MFront/FEniCS solve time:\", time()-tic)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The temperature field along a radial direction along the top surface has been compared with computations using [`Cast3M` finite-element solver](http://www-cast3m.cea.fr/). Both solutions agree perfectly:\n",
"\n",
"\n",
"\n",
"\n",
"## Performance comparison\n",
"\n",
"For the purpose of performance comparison, we also implement a direct non-linear variational problem with pure UFL expressions. This is possible in the present case since the non-linear heat constitutive law is very simple. Note that we enfore the use of the same quadrature rule degree. The temperature field is also reinterpolated to its previous initial value for a fair comparison between both solution strategies."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pure FEniCS solve time: 49.15058135986328\n"
]
}
],
"source": [
"A = Constant(material.get_parameter(\"A\"))\n",
"B = Constant(material.get_parameter(\"B\"))\n",
"j = -1/(A + B*T)*grad(T)\n",
"F = (dot(grad(T_), j) + r*T_)*dx(metadata={'quadrature_degree': quad_deg})\n",
"J = derivative(F, T, dT)\n",
"\n",
"T.interpolate(T0)\n",
"tic = time()\n",
"solve(F == 0, T, bc, J=J)\n",
"print(\"Pure FEniCS solve time:\", time()-tic)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can observe that both methods, relying on the same default Newton solver, yield the same total iteration counts and residual values. As regards computing time, the pure `FEniCS` implementation is slightly faster as expected. In the following table, comparison has been made for a coarse (approx 4 200 cells) and a refined (approx 34 000 cells) mesh with quadrature degrees equal either to 2 or 5.\n",
"\n",
"|Mesh type | Quadrature degree | `FEniCS`/`MFront` | Pure `FEniCS` |\n",
"|:--------:|:-----------------:|:-----------------:|:-------------:|\n",
"| coarse | 2 | 1.2 s | 0.8 s |\n",
"| coarse | 5 | 2.2 s | 1.0 s |\n",
"| fine | 2 | 62.8 s | 58.4 s |\n",
"| fine | 5 | 77.0 s | 66.3 s | \n",
"\n",
"The difference is slightly larger for large quadrature degrees, however, the difference is moderate when compared to the total computing time for large scale problems.\n",
"\n",
"## On the use of the correct tangent operator\n",
"\n",
"Most FE software do not take into account the contribution of $\\dfrac{\\partial \\mathbf{j}}{\\partial T}$ to the tangent operator. One can easily test this variant by assigning `dj_ddT` in the `MFront` behaviour or change the expression of the jacobian in the pure `FEniCS` implementation by:\n",
"```\n",
"J = dot(grad(T_), -grad(dT)/(A+B*T))*dx(metadata={'quadrature_degree': quad_deg})\n",
"```\n",
"In the present case, using this partial tangent operator yields a convergence in 4 iterations instead of 3, giving a computational cost increase by roughly 25%."
]
}
],
"metadata": {
"celltoolbar": "Pièces jointes",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autoclose": false,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
},
"title": "3D problem"
},
"nbformat": 4,
"nbformat_minor": 4
}