gavin@ridleygav.in:~/projects$ cat les.html
2.29 · numerical fluid mechanics · MITLES in MOOSE
So, you might have a knee-jerk reaction that this sounds pretty nasty to do in MOOSE. It's such a general finite element framework, and usually you would want any large eddy simulation code to be highly specialized for the problem at hand in order to reckon with the fact that LES simulations are really, really computationally expensive. That's why people have made stuff like Nek5000.
Well, it would be nice to have LES in MOOSE for a few reasons, even if it's slow. The biggest is that it would make coupling to realistic turbulent flows possible in MOOSE, which is currently simply not possible with any of the code that has been written to date in their framework. Despite the fact that a module for solving the Navier Stokes equations exists, it has never been expanded beyond solving extremely simple fluid flow problems.
In this project, I implemented both the Smagorinsky and WALE LES models into MOOSE, and attempted to simulate a turbulent pipe flow. I also solved a Kelvin-Helmholtz flow instability benchmark problem. I found the code conserving energy more-or-less in line with the expected benchmark results if using a second order implicit timestepper, but was never able to get the pipe flow case converged. Here's a video of the Kelvin-Helmholtz calculation I did:
That didn't use any LES model, though. It was merely a test of if the code was conserving energy. Writing the code for the LES models was the coolest part. Because MOOSE allows you to solve nonlinear PDEs with the multidimensional Newton's method, it is advantageous to write the Jacobian of the individual PDE terms yourself. For the WALE model, the Jacobian is absolutely mind-blowingly complicated. Luckily, Mathematica is really great for this type of work. The hard part is extracting the common terms that appear and saving them to intermediate variables. I've pasted the kernel I wrote for the WALE model below. Incredible!
One more fun thing about the project was that periodic boundary conditions did not work in MOOSE before I finished this project. Luckily, libMesh, which MOOSE is based on, already was supposed to support this. All I had to do was add some extra plumbing in this pull request. Boy was that fun to debug!
Source code for WALE LES model in MOOSE
#include "WALELESMaterial.h" registerMooseObject("NavierStokesApp", WALELESMaterial); InputParameters WALELESMaterial::validParams() { InputParameters params = Material::validParams(); params.addParam<Real>("rho", 1, "Value of fluid density"); params.addParam<Real>("c_w", 0.5, "Nicoud and Ducros's constant"); // TODO params.addRequiredParam<Real>("mu0", "Value of natural fluid dynamic viscosity"); params.addRequiredCoupledVar("u", "x-velocity"); params.addCoupledVar("v", "y-velocity"); // only required in 2D and 3D params.addCoupledVar("w", "z-velocity"); // only required in 3D return params; } WALELESMaterial::WALELESMaterial(const InputParameters & params) : Material(params), _grad_u_vel(coupledGradient("u")), _grad_v_vel(_mesh.dimension() >= 2 ? coupledGradient("v") : _grad_zero), _grad_w_vel(_mesh.dimension() == 3 ? coupledGradient("w") : _grad_zero), _density(getParam<Real>("rho")), _mu0(getParam<Real>("mu0")), _cw(getParam<Real>("c_w")), _mu(declareProperty<Real>("mu")), _rho(declareProperty<Real>("rho")), _d_mu_du(declareProperty<RealVectorValue>("d_mu_du")), _d_mu_dv(declareProperty<RealVectorValue>("d_mu_dv")), _d_mu_dw(declareProperty<RealVectorValue>("d_mu_dw")) { } void WALELESMaterial::computeQpProperties() { _rho[_qp] = _density; // natural dynamic viscosity plus LES kinematic viscosity times density constexpr Real one_third = 1.0 / 3.0; constexpr Real epsilon = std::numeric_limits<Real>::epsilon(); const Real delta = std::pow(_current_elem->volume(), one_third) / _current_elem->default_order(); const Real filter_term = Utility::pow<2>(_cw * delta) * _density; // Aliases make it easier to see what's going on (if possible) constexpr Real one_ninth = 1.0 / 9.0; const auto & ux = _grad_u_vel[_qp](0); const auto & uy = _grad_u_vel[_qp](1); const auto & uz = _grad_u_vel[_qp](2); const auto & vx = _grad_v_vel[_qp](0); const auto & vy = _grad_v_vel[_qp](1); const auto & vz = _grad_v_vel[_qp](2); const auto & wx = _grad_w_vel[_qp](0); const auto & wy = _grad_w_vel[_qp](1); const auto & wz = _grad_w_vel[_qp](2); const Real curlySS = 0.5 * Utility::pow<2>((uy + vx) * (ux + vy) + vz * wx + uz * wy) + one_ninth * Utility::pow<2>(2 * ux * ux + uy * vx - vy * vy + uz * wx - 2.0 * vz * wy - wz * wz) + one_ninth * Utility::pow<2>(-ux * ux + uy * vx + 2.0 * vy * vy - 2.0 * uz * wx + vz * wy - wz * wz) + one_ninth * Utility::pow<2>(-ux * ux - 2.0 * uy * vx - vy * vy + uz * wx + vz * wy + 2.0 * wz * wz) + 0.5 * Utility::pow<2>(uy * vz + vx * wy + (uz + wx) * (ux + wz)) + 0.5 * Utility::pow<2>(uz * vx + uy * wx + (vz + wy) * (vy + wz)); const Real SS = ux * ux + vy * vy + wz * wz + 0.5 * Utility::pow<2>(uy + vx) + 0.5 * Utility::pow<2>(uz + wx) + 0.5 * Utility::pow<2>(vz + wy); const Real c_one_half = std::sqrt(curlySS); const Real c_one_fourth = std::sqrt(c_one_half); const Real c_three_half = Utility::pow<3>(c_one_half); const Real c_five_fourth = curlySS * c_one_fourth; const Real n_three_half = std::sqrt(Utility::pow<3>(SS)); const Real n_five_half = n_three_half * SS; const Real denom = (n_five_half + c_five_fourth); const Real denom_sq = Utility::pow<2>(denom); _mu[_qp] = _mu0 + filter_term * c_three_half / (denom + epsilon); constexpr Real eight_third = 8.0 / 3.0; const Real dcdux = eight_third * ux * ux * ux + uy * uy * vy + uy * (2.0 * vx * vy + 2.0 * vz * wx + uz * (vz + wy)) + vx * (vx * vy + 2.0 * uz * wy + wx * (vz + wy)) + Utility::pow<2>(uz + wx) * wz + one_third * ux * ((3.0 * uy + vx) * (uy + 3.0 * vx) + (3.0 * uz + wx) * (uz + 3.0 * wx) - 4.0 * (vy * vy + 2.0 * vz * wy + wz * wz)); const Real dcduy = one_third * (4.0 * uy * vx * vx + ux * ux * (3.0 * uy + 5 * vx) + 5.0 * vx * vy * vy + uz * vx * wx + 6.0 * vy * vz * wx + 3.0 * uy * (vy * vy + vz * vz + wx * wx) + 3.0 * uz * vy * wy + vx * vz * wy + 3.0 * vy * wx * wy + 3.0 * ux * (2.0 * (uy + vx) * vy + 2.0 * vz * wx + uz * (vz + wy)) + 3.0 * (uz * vz + wx * (2.0 * vz + wy)) * wz - 4.0 * vx * wz * wz); const Real dcduz = one_third * (3.0 * vx * vy * vz + uy * vx * wx - 4.0 * vy * vy * wx + ux * ux * (3.0 * uz + 5.0 * wx) + 3.0 * uy * vy * wy + 6.0 * vx * vy * wy + vz * wx * wy + 3.0 * (uy + vx) * vz * wz + 6.0 * vx * wy * wz + 5.0 * wx * wz * wz + 3.0 * ux * (2.0 * vx * wy + uy * (vz + wy) + 2.0 * (uz + wx) * wz) + uz * (3.0 * vx * vx + 4.0 * wx * wx + 3.0 * (wy * wy + wz * wz))); const Real dcdvx = one_third * (4.0 * uy * uy * vx + ux * ux * (5.0 * uy + 3.0 * vx) + 3.0 * ux * (2.0 * (uy + vx) * vy + 2.0 * uz * wy + wx * (vz + wy)) + uy * (5.0 * vy * vy + uz * wx + vz * wy - 4.0 * wz * wz) + 3.0 * (uz * uz * vx + vx * vy * vy + vy * vz * wx + vx * wy * wy + wx * wy * wz + uz * (vz + 2.0 * wy) * (vy + wz))); const Real dcdvy = one_third * (3.0 * ux * (uy + vx) * (uy + vx) - 4.0 * ux * ux * vy + 3.0 * uy * uy * vy + 3.0 * vx * vx * vy + 8.0 * vy * vy * vy + 3.0 * uz * vx * vz + 3.0 * vy * vz * vz - 8.0 * uz * vy * wx + 3.0 * vx * vz * wx + 6.0 * uz * vx * wy + 10.0 * vy * vz * wy + 3.0 * vy * wy * wy + uy * (10.0 * vx * vy + 6.0 * vz * wx + 3.0 * (uz + wx) * wy) + 3.0 * (vz + wy) * (vz + wy) * wz - 4.0 * vy * wz * wz); const Real dcdvz = one_third * (3.0 * uy * uy * vz + 3.0 * vy * vy * vz + 6.0 * uy * vy * wx + 3.0 * vx * vy * wx + 3.0 * vz * wx * wx + 3.0 * ux * (vx * wx + uy * (uz + 2.0 * wx)) - 4.0 * ux * ux * wy + uy * vx * wy + 5.0 * vy * vy * wy + 4.0 * vz * wy * wy + 6.0 * (uy * wx + vy * (vz + wy)) * wz + (3.0 * vz + 5.0 * wy) * wz * wz + uz * (3.0 * vx * vy + wx * wy + 3.0 * (uy + vx) * wz)); const Real dcdwx = one_third * (uy * uz * vx - 4.0 * uz * vy * vy + 3.0 * vx * vy * vz + 3.0 * uy * uy * wx + 4.0 * uz * uz * wx + 3.0 * vz * vz * wx + ux * ux * (5.0 * uz + 3.0 * wx) + uz * vz * wy + 3.0 * vx * wy * wz + (5.0 * uz + 3.0 * wx) * wz * wz + 3.0 * uy * (2.0 * vz + wy) * (vy + wz) + 3.0 * ux * (2.0 * uy * vz + vx * (vz + wy) + 2.0 * (uz + wx) * wz)); const Real dcdwy = one_third * (6.0 * uz * vx * vy - 4 * ux * ux * vz + 5.0 * vy * vy * vz + uz * vz * wx + 3.0 * ux * (uy * uz + vx * (2.0 * uz + wx)) + 3.0 * uz * uz * wy + 3.0 * vx * vx * wy + 3.0 * vy * vy * wy + 4.0 * vz * vz * wy + 3.0 * vx * (2.0 * uz + wx) * wz + 6.0 * vy * (vz + wy) * wz + (5.0 * vz + 3.0 * wy) * wz * wz + uy * (vx * vz + 3.0 * vy * (uz + wx) + 3.0 * wx * wz)); const Real dcdwz = uy * uz * vz + uz * vx * vz + vy * vz * vz + 2.0 * uy * vz * wx + ux * (uz + wx) * (uz + wx) + 2.0 * uz * vx * wy + 2.0 * vy * vz * wy + uy * wx * wy + vx * wx * wy + vy * wy * wy - (4.0 * ux * ux * wz) * one_third + one_third * (-8.0 * uy * vx - 4 * vy * vy + (3.0 * uz + wx) * (uz + 3.0 * wx) + (3.0 * vz + wy) * (vz + 3.0 * wy)) * wz + eight_third * wz * wz * wz; _d_mu_du[_qp] = filter_term * RealVectorValue( (1.5 * c_one_half * dcdux * denom - c_three_half * (5.0 * n_three_half * ux + 1.25 * c_one_fourth * dcdux)) / (denom_sq + epsilon), (1.5 * c_one_half * dcduy * denom - c_three_half * (2.5 * n_three_half * (uy + vx) + 1.25 * c_one_fourth * dcduy)) / (denom_sq + epsilon), (1.5 * c_one_half * dcduz * denom - c_three_half * (2.5 * n_three_half * (uz + wx) + 1.25 * c_one_fourth * dcduz)) / (denom_sq + epsilon)); _d_mu_dv[_qp] = filter_term * RealVectorValue( (1.5 * c_one_half * dcdvx * denom - c_three_half * (2.5 * n_three_half * (uy + vx) + 1.25 * c_one_fourth * dcdvx)) / (denom_sq + epsilon), (1.5 * c_one_half * dcdvy * denom - c_three_half * (5.0 * n_three_half * vy + 1.25 * c_one_fourth * dcdvy)) / (denom_sq + epsilon), (1.5 * c_one_half * dcdvz * denom - c_three_half * (2.5 * n_three_half * (vz + wy) + 1.25 * c_one_fourth * dcdvz)) / (denom_sq + epsilon)); _d_mu_dw[_qp] = filter_term * RealVectorValue( (1.5 * c_one_half * dcdwx * denom - c_three_half * (2.5 * n_three_half * (ux + wx) + 1.25 * c_one_fourth * dcdwx)) / (denom_sq + epsilon), (1.5 * c_one_half * dcdwy * denom - c_three_half * (2.5 * n_three_half * (vz + wy) + 1.25 * c_one_fourth * dcdwy)) / (denom_sq + epsilon), (1.5 * c_one_half * dcdwz * denom - c_three_half * (5.0 * n_three_half * wz + 1.25 * c_one_fourth * dcdwz)) / (denom_sq + epsilon)); }