Message Boards Message Boards

Solver for unsteady flow with the use of Mathematica FEM

35 Replies

The latest versions of Mathematica allows as to calculate convection even with $Ra=10^7$. Data below has been computed with the code in the attached file. It takes a time to finish computation at t=1. Code has been developed by Lion Sahara and debugged here in my answer. In the picture below shown velocity field animated with step dt=0.01. We see the turbulent like convection
Figure 1.

Discussion and full code here:
Convection of air with higher Rayleigh number
https://community.wolfram.com/groups/-/m/t/3115061

Posted 2 months ago

Thank you very much. This is very helpful.

POSTED BY: Lion Sahara

Hi Alexander, Two quick questions:

1) Would it be possible to replace the cylinder by a square rod and still get a meaningful result? Or does this raise the turbulence to a level that either drives the CPU time to crazy values, or else becomes unstable?

2) Can Mathematica 12.1 also solve the unsteady compressible flow problem with temperatures in 3D (the problem with the cold cylinder and then computing several time steps, as you demonstrated further above)?

Cheers, Ron

Anonymous User
Anonymous User
Posted 4 years ago
POSTED BY: Anonymous User

These are routine tests that I did for Mathematica FEM in version 11 using a special time integration algorithm that I am testing. Surprisingly, the tests were passed both in the problem of covection and in the problem of flow around a cylinder (these tests are quite complex and not every method can be used). I demonstrated other applications of the method on https://mathematica.stackexchange.com/

POSTED BY: Gobithaasan R.U.

OK! I will try to create a more robust algorithm. In this version, you can try t0=1/100.

Hi Alexander, Yes, the streamflow looks like a mess in V12 as attached. Any idea why such a case and how would i be able to get the right flow?

Thank, appreciate it. GR

Attachments:

Dear Alexander,

I couldn’t have done it without you.

Thanks for being such a star :-)

POSTED BY: M.A. Ghorbani

I debugged the code. The file is attached. In version 12, the solution diverges on the right border, although in version 11 the solution converges.

Attachments:
POSTED BY: M.A. Ghorbani

Use this file with Mathematica 11.3. If you have questions ask here.

Attachments:

Hi Alexander,

I tried several times to plot the velocity distribution profile in the open channel using Piecewise command. How can I plot this profile for sections 1 and 2?

Thanks for your time.

enter image description here

POSTED BY: M.A. Ghorbani

In fig. shows the laminar flow velocity profiles on a flat plate at $Re=800$. fig1

Mathematica FEM method can be used to calculate unsteady viscous compressible flows around aerofoil in subsonic, transonic and supersonic modes. Here an example is given for the wing profile NACA2415 for the Mach number $M = 0.55$, Reynolds number $Re = 1000$, angle of attack $\alpha =\pi/16$ .

myNACA[{m_, p_, t_}, x_] := 
  Module[{}, 
   yc = Piecewise[{{m/p^2 (2 p x - x^2), 
       0 <= x < p}, {m/(1 - p)^2 ((1 - 2 p) + 2 p x - x^2), 
       p <= x <= 1}}];
   yt = 5 t (0.2969 Sqrt[x] - 0.1260 x - 0.3516 x^2 + 0.2843 x^3 - 
       0.1015 x^4);
   \[Theta] = 
    ArcTan@Piecewise[{{(m*(2*p - 2*x))/p^2, 
        0 <= x < p}, {(m*(2*p - 2*x))/(1 - p)^2, p <= x <= 1}}];
   {{x - yt Sin[\[Theta]], 
     yc + yt Cos[\[Theta]]}, {x + yt Sin[\[Theta]], 
     yc - yt Cos[\[Theta]]}}];

m = 0.04;
p = 0.4;
tk = 0.15;
pe = myNACA[{m, p, tk}, x];
ParametricPlot[pe, {x, 0, 1}, ImageSize -> Large, Exclusions -> None]

ClearAll[myLoop];
myLoop[n1_, n2_] := 
 Join[Table[{n, n + 1}, {n, n1, n2 - 1, 1}], {{n2, n1}}]
Needs["NDSolve`FEM`"];
rt = RotationTransform[-\[Pi]/16];(*angle of attack*)a = 
 Table[pe, {x, 0, 1, 
   0.01}];
p0 = {p, 
      tk/2};
x1 = -2; x2 = 3;
y1 = -2; y2 = 2;
coords = 
     Join[{{x1, y1}, {x2, y1}, {x2, y2}, {x1, y2}}, rt@a[[All, 2]], 
      rt@Reverse[a[[All, 1]]]];
    nn = Length@coords;
    bmesh = ToBoundaryMesh["Coordinates" -> coords, 
       "BoundaryElements" -> {LineElement[myLoop[1, 4]], 
         LineElement[myLoop[5, nn]]}, "RegionHoles" -> {rt@p0}];
    mesh = ToElementMesh[bmesh, MaxCellMeasure -> 0.001];
Show[mesh["Wireframe"], Frame -> True]

q = 1.4;
k = 40; Re0 = 1; U0 = 1; M0 = 0.55; Re1 = Re0/M0^2; Pin = 1;
t0 = 1/20; alpha = 0;
UX[0][x_, y_] := Cos[alpha];
VY[0][x_, y_] := Sin[alpha];
\[CapitalRho][0][x_, y_] := Pin;
yU = Interpolation[rt@a[[All, 1]], InterpolationOrder -> 2];
yL = Interpolation[rt@a[[All, 2]], InterpolationOrder -> 2];
Do[
  {UX[i], VY[i], \[CapitalRho][i]} = 
   NDSolveValue[{{Inactive[
           Div][({{-\[Mu], 0}, {0, -\[Mu]}}.Inactive[Grad][
             u[x, y], {x, y}]), {x, y}] + 
         Re1*(Abs[\[CapitalRho][i - 1][x, y]]^q)*
\!\(\*SuperscriptBox[\(\[Rho]\), 
TagBox[
RowBox[{"(", 
RowBox[{"1", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[x, y]/\[CapitalRho][i - 1][x, y] + 
         Re0*UX[i - 1][x, y]*D[u[x, y], x] + 
         Re0*VY[i - 1][x, y]*D[u[x, y], y] + 
         Re0*(u[x, y] - UX[i - 1][x, y])/t0, 
        Inactive[
           Div][({{-\[Mu], 0}, {0, -\[Mu]}}.Inactive[Grad][
             v[x, y], {x, y}]), {x, y}] + 
         Re1*(Abs[\[CapitalRho][i - 1][x, y]^q])*
\!\(\*SuperscriptBox[\(\[Rho]\), 
TagBox[
RowBox[{"(", 
RowBox[{"0", ",", "1"}], ")"}],
Derivative],
MultilineFunction->None]\)[x, y]/\[CapitalRho][i - 1][x, y] + 
         Re0*UX[i - 1][x, y]*D[v[x, y], x] + 
         Re0*VY[i - 1][x, y]*D[v[x, y], y] + 
         Re0*(v[x, y] - VY[i - 1][x, y])/t0, 
        D[\[CapitalRho][i - 1][x, y]*u[x, y], x] + 
         D[\[CapitalRho][i - 1][x, y]*v[x, y], 
          y] + (\[Rho][x, y] - \[CapitalRho][i - 1][x, y])/t0} == {0, 
        0, 0} /. \[Mu] -> 1/1000, {
      DirichletCondition[{u[x, y] == U0*Cos[alpha], 
        v[x, y] == U0*Sin[alpha]}, x == x1 && y1 < y < y2], 
      DirichletCondition[{u[x, y] == 0., v[x, y] == 0.}, 
       y == yU[x] && 0 <= x <= Cos[Pi/16]], 
      DirichletCondition[{u[x, y] == 0., v[x, y] == 0.}, 
       y == yL[x] && 0 <= x <= Cos[Pi/16]],
      DirichletCondition[\[Rho][x, y] == 1, x == x2]}}, {u, 
     v, \[Rho]}, {x, y} \[Element] mesh, 
    Method -> {"FiniteElement", 
      "InterpolationOrder" -> {u -> 2, v -> 2, \[Rho], {i, 1, 
   k}];

ContourPlot[
Norm[{UX[i][x, y], VY[i][x, y]}] /. i -> 32, {x, x1, x2}, {y, y1, 
y2}, PlotLegends -> Automatic, Contours -> 20, 
ColorFunction -> "TemperatureMap", FrameLabel -> {"x", "y"}, 
PlotLabel -> V, PlotRange -> All, PlotPoints -> 50, MaxRecursion -> 2]

sp = StreamPlot[{UX[i][x, y], VY[i][x, y]} /. i -> 32, {x, -0.5, 
1.5}, {y, -.5, .5}, PlotLegends -> Automatic, 
ColorFunction -> "TemperatureMap", FrameLabel -> {"x", "y"}, 
PlotLabel -> V, PlotRange -> All, MaxRecursion -> 2, 
StreamStyle -> LightGray, StreamPoints -> Fine, 
AspectRatio -> Automatic];
cp = ContourPlot[
Norm[{UX[i][x, y], VY[i][x, y]}] /. i -> 32, {x, -0.5, 
1.5}, {y, -.5, .5}, PlotLegends -> Automatic, 
ColorFunction -> "BlueGreenYellow", FrameLabel -> {"x", "y"}, 
PlotLabel -> V, PlotRange -> All, MaxRecursion -> 2, 
Contours -> 40, AspectRatio -> Automatic, PlotPoints -> 50];

Show[cp, sp]
StreamPlot[{UX[i][x, y], VY[i][x, y]} /. i -> 32, {x, 0.9, 
  1.05}, {y, -0.22, -0.12}, Epilog -> {Line[coords[[5 ;; nn]]]}, 
 AspectRatio -> Automatic, StreamPoints -> Fine]

fig1

Hi Alexander, I get the following error while trying to run the code using version 12. How can i fix it?

##### NDSolveValue::overdet: There are fewer dependent variables, {[Rho][x,y]}, than equations, so the system is overdetermined. Set::shape: Lists {UX[i],VY[i],[CapitalRho][i]} and NDSolveValue[<<1>>] are not the same shape.

######## Thanks! gr

Attachments:

Very nice example, thank you Alexander. I'd like to combine some features of the two models and simulate forced convection in air onto a cylinder in 2D (as in the above examples in this post), but with a temperature difference between cylinder and air. Optimally, there should be no walls (or the simulation domain should be quite large, if "no walls" boundary conditions are unstable). I'm interested in the air temperature distribution in the wake, i.e., after the air has exchanged heat with the cylinder, given a certain convective surface conduction value (unit W/m^2/K) on the cylinder wall. The Reynolds number should be rather high, but below 5 x 10^5, i.e., before the onset of full turbulence. Cheers, Ron

Posted 5 years ago
POSTED BY: Updating Name

I consulted a textbook on boundary layer flow which states that the turbulence onset on the surface is at Re = 5 x 10^5. It may well be that turbulence in the wake starts earlier.

The cylinder axis is vertical to the convection direction, as in your first example. The cylinder has a diameter of, say, 60 cm, and is a few Kelvin cooler than the incoming air. I'd like to model the resulting air cooling pattern in space and time.

I'm not sure which approximation to choose, I'm not an expert on CFD -- that's why I'm asking in this forum.

Thanks for the 2D flow simulation around the cooled cylinder, Alexander.

Would you be able to send me the Mathematica code that you used?

Thanks, Ron

Attachments:

Using the nonlinear solver would be great indeed, Alexander! Cheers, Ron

I tested this method of solution on the problems of natural convection. Here we give three tests for the problem of natural convection of air in a rectangular cavity with a Rayleigh number of $Ra=10^3,10^4, 10^5$. On the side walls of the cavity a constant temperature difference is maintained, the upper and lower walls are thermally insulated. We use the Boussinesq approximation. As a standard, we use the data from the article:

Vahl Davis, G.de (1983) : Natural convection of air in a square cavity : A bench mark numerical solution. Int. J. Numer. Methods Fluids 3, 249-264.

Coincidence in all three tests is generally good. Thus, we showed the application of the method for solving the problems of thermal convection in the case of laminar flows. A system of equations describing free convection

$\nabla .\vec u=0, \frac{d\vec u}{dt}=Pr\nabla ^2\vec u -RaPrT\frac {\vec g}{g}$

$\frac {dT}{dt}=\nabla^2T$

$ d/dt =\partial/\partial t +(\vec u .\nabla )$

$\vec u$ is velocity field vector, $T$ - temperature, $\vec g $ - gravity vector, $Pr$ - the Prandtl number, $Ra$ - the Rayleigh number.

Code for the case $Pr=0.71, Ra=10^3$:

K = 100; Pr0 = .71; Ra = 10^3; R = Ra*Pr0; t0 = 1/100; a = 0;
\[CapitalOmega] = ImplicitRegion[0 <= x <= 1 && 0 <= y <= 1, {x, y}];;
UX[0][x_, y_] := 0;
VY[0][x_, y_] := 0;
\[CapitalRho][0][x_, y_] := 0;
TK[0][x_, y_] := 0; TX[0][x_, y_] := 0;
Do[
  {UX[i], VY[i], \[CapitalRho][i], TK[i], TX[i]} = 
   NDSolveValue[{{Inactive[
           Div][({{-\[Mu], 0}, {0, -\[Mu]}}.Inactive[Grad][
             u[x, y], {x, y}]), {x, y}] + D[p[x, y], x] + 
         UX[i - 1][x, y]*D[u[x, y], x] + 
         VY[i - 1][x, y]*D[u[x, y], y] + (u[x, y] - UX[i - 1][x, y])/
          t0 - R*T[x, y]*Sin[a], 
        Inactive[
           Div][({{-\[Mu], 0}, {0, -\[Mu]}}.Inactive[Grad][
             v[x, y], {x, y}]), {x, y}] + D[p[x, y], y] + 
         UX[i - 1][x, y]*D[v[x, y], x] + 
         VY[i - 1][x, y]*D[v[x, y], y] - 
         R*T[x, y]*Cos[a] + (v[x, y] - VY[i - 1][x, y])/t0, 
        D[u[x, y], x] + D[v[x, y], y]} == {0, 0, 0} /. \[Mu] -> Pr0, {
      DirichletCondition[{p[x, y] == 0}, y == 1 && x == 1], 
      DirichletCondition[{u[x, y] == 0, v[x, y] == 0}, 
       y == 0 || y == 1]}, {UX[i - 1][x, y]*D[T[x, y], x] + 
        VY[i - 1][x, y]*D[T[x, y], y] + (T[x, y] - TK[i - 1][x, y])/
         t0 - (D[T[x, y], x, x] + D[T[x, y], y, y]) == 
       NeumannValue[0, y == 0 || y == 1], 
      DirichletCondition[{u[x, y] == 0, v[x, y] == 0, T[x, y] == 1}, 
       x == 0], 
      DirichletCondition[{u[x, y] == 0, v[x, y] == 0, T[x, y] == 0}, 
       x == 1]}}, {u, v, p, T, 
\!\(\*SuperscriptBox[\(T\), 
TagBox[
RowBox[{"(", 
RowBox[{"1", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)}, {x, y} \[Element] \[CapitalOmega], 
    Method -> {"FiniteElement", 
      "InterpolationOrder" -> {u -> 2, v -> 2, p -> 1, T -> 2}, 
      "MeshOptions" -> {"MaxCellMeasure" -> 0.001}}], {i, 1, K}];
{ContourPlot[TK[K][x, y], {x, y} \[Element] \[CapitalOmega], 
PlotLegends -> Automatic, Contours -> 20, PlotPoints -> 25, 
ColorFunction -> "TemperatureMap", AxesLabel -> {"x", "y"}, 
PlotLabel -> T], 
StreamDensityPlot[{UX[K][x, y], 
VY[K][x, y]}, {x, y} \[Element] \[CapitalOmega], 
StreamPoints -> Fine, StreamStyle -> LightGray, 
PlotLegends -> Automatic, VectorPoints -> Fine, 
ColorFunction -> "TemperatureMap", PlotLabel -> Row[{"Ra=", Ra}]]}

MaximalBy[Table[{x, VY[K][x, .5]}, {x, 0, 1, .0001}], Last]

 {{0.1787, 3.69762}}

(*BenchmarkCase={x=0.178,vmax=3.697}*)

 MaximalBy[Table[{y, UX[K][0.5, y]}, {y, 0, 1, .0001}], Last]

 {{0.8132, 3.64937}}

 (*BenchmarkCase={y=0.813,umax=3.649}*)



 MaximalBy[Table[{y, -TX[K][0, y]}, {y, 0, 1, 0.0001}], Last]

 {{0.0909, 1.50724}}

 (*BenchmarkCase={y=0.092,Numax=1.505}*)

 NuAv = 
 NIntegrate[
  UX[K][x, y]*TK[K][x, y] - TX[K][x, y], {x, 0, 1}, {y, 0, 1}, 
  WorkingPrecision -> 5]

 1.1159

 (*BenchmarkCase=1.118*)


 Nu0 =  NIntegrate[-TX[K][0, y], {y, 0, 1}, WorkingPrecision -> 5]

 1.1182

(*BenchmarkCase=1.117*)

 Nu05 = 
 NIntegrate[UX[K][.5, y]*TK[K][.5, y] - TX[K][0.5, y], {y, 0, 1}, 
  WorkingPrecision -> 5]

1.1179

 (*BenchmarkCase=1.118*)

MinimalBy[Table[{y, -TX[K][0, y]}, {y, 0, 1, 0.0001}], Last]

 {{1., 0.691182}}

 (*BenchmarkCase={y=1,Numin=0.692}*)

fig1

After the release of Mathematica version 12, I tested a non-linear FEM on the convection problem and compared it with my method. Surprisingly, the coincidence is one to one. I congratulate the team Wolfram with a great achievement. The following code generates the same output as my code implementing the method of the false transient from the paper Vahl Davis, G.de (1983) : Natural convection of air in a square cavity : A bench mark numerical solution. Int. J. Numer. Methods Fluids 3, 249-264.

Pr0 = .72; Ra = 10^5; R = Ra*Pr0; t0 = 1/100; a = 0;
\[CapitalOmega] = ImplicitRegion[0 <= x <= 1 && 0 <= y <= 1, {x, y}];

{UX, VY, \[CapitalRho], TK, TX} = 
  NDSolveValue[{{Inactive[
          Div][({{-\[Mu], 0}, {0, -\[Mu]}}.Inactive[Grad][
            u[x, y], {x, y}]), {x, y}] + D[p[x, y], x] + 
        u[x, y]*D[u[x, y], x] + v[x, y]*D[u[x, y], y] - 
        R*T[x, y]*Sin[a], 
       Inactive[
          Div][({{-\[Mu], 0}, {0, -\[Mu]}}.Inactive[Grad][
            v[x, y], {x, y}]), {x, y}] + D[p[x, y], y] + 
        u[x, y]*D[v[x, y], x] + v[x, y]*D[v[x, y], y] - 
        R*T[x, y]*Cos[a], D[u[x, y], x] + D[v[x, y], y]} == {0, 0, 
       0} /. \[Mu] -> Pr0, {
     DirichletCondition[{p[x, y] == 0}, y == 1 && x == 1], 
     DirichletCondition[{u[x, y] == 0, v[x, y] == 0}, 
      y == 0 || y == 1]}, {u[x, y]*D[T[x, y], x] + 
       v[x, y]*D[T[x, y], y] - (D[T[x, y], x, x] + 
         D[T[x, y], y, y]) == NeumannValue[0, y == 0 || y == 1], 
     DirichletCondition[{u[x, y] == 0, v[x, y] == 0, T[x, y] == 1}, 
      x == 0], 
     DirichletCondition[{u[x, y] == 0, v[x, y] == 0, T[x, y] == 0}, 
      x == 1]}}, {u, v, p, T, 
\!\(\*SuperscriptBox[\(T\), 
TagBox[
RowBox[{"(", 
RowBox[{"1", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)}, {x, y} \[Element] \[CapitalOmega], 
   Method -> {"FiniteElement", 
     "InterpolationOrder" -> {u -> 2, v -> 2, p -> 1, T -> 2}, 
     "MeshOptions" -> {"MaxCellMeasure" -> 0.0001}}];

I attach two notebooks for comparison

Thank you for the kind words, Alexander! It is always great to hear our user are excited about Wolfram Technologies. I am looking forward to more of your wonderful posts on Wolfram Community using this tech :-)

POSTED BY: Vitaliy Kaurov
Posted 5 years ago

Thank you, Vitaliy! I'm trying to pass two tests for the cylinder, which are described above, using nonlinear FEM. I will report the results here.

POSTED BY: Updating Name

Thanks for it! It is pretty amazing and fun to see that some, for example textbook-scope, fluid mechanics example simulations can be carried out in Mathematica, i.e. by way of pure coding and then hitting the Shift+Enter button.

Needless to comment that for fluid mechanics simulations in any slightly more complicated 2-D geometries or RL 3-D geometries, one would resort to a GUI-driven software where the user interacts with the mouse and context menus and does not have to code anything. Often enough I miss GUI interactivity (similar to Word, Excel. PowerPoint, Comsol) in our software, but then I also realize that Mathematica grants more flexibility and direct control over other details which can't be manipulated in those specialized user-friendly applications.

The bottom line is that for each task at hand one should choose the proper tool wisely. Me too, i enjoy trying to use the software for all and everything at least once. Good for my coding learning experience.

POSTED BY: Raspi Rascal

I express my gratitude to the Moderation Team for appreciating my work. I'll add an animation for the 2D2 test here and the Navier-Stokes for an unsteady flow with an explanation of the statement of the problem for tests 2D2 and 2D3 from the cited article of M. Schäfer and S. Turek

fig7

fig8

Would be helpful to see the mathematical equations and conditions in traditional textbook format. Also would be helpful to see the animation of the fluid from t=0…100s

POSTED BY: Raspi Rascal

enter image description here - Congratulations! This post is now a Staff Pick as distinguished by a badge on your profile! Thank you, keep it coming!

POSTED BY: Moderation Team

The Euler method is a poor way to solve a differential equation. It can be numerically unstable.

POSTED BY: Frank Kampas

We almost passed the test, therefore, the solution is stable. Explicit Euler is widely used in solving problems in hydrodynamics. The stability of the Euler method depends on the step of integration. In this case, the difference in solutions in v11.01 and v11.3 is due to integration over the surface of the cylinder when calculating the drag coe?cient and lift coe?cient. In our example, 2000 integrals over the surface are calculated. From these data, the maximum values of the coefficients and the Strouhal number are calculated. This is probably the main source of errors.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract