Skip to main content

numerics - Solving a steady-state viscous Burger's equation with NDSolve


A steady-state viscous Burger's equation is given by uu′=νu″,x∈(−1,1),

u(−1)=1+δ,u(1)=−1.
Here ν>0 is the viscosity, δ>0 is a small perturbation and u is the solution. This ODE problem has a unique solution: u(x)=−Atanh(A2ν(x−z)),
where A>0 and z>0 are constants determined by the boundary conditions: Atanh(A2ν(1+z))=1+δ,Atanh(A2ν(1−z))=1.
The exact solution can be plotted in Mathematica:


Azex[nu_, delta_] := 
Quiet[{a, zz} /. Flatten@NSolve[{a*Tanh[a*(1 + zz)/(2*nu)] == 1 + delta,
a*Tanh[a*(1 - zz)/(2*nu)] == 1, a > 0, zz > 0}, {a, zz}, Reals]]

nu = 0.05;

{A, zex} = Azex[nu, 0.01];


Plot[-A*Tanh[A*(x - zex)/(2*nu)], {x, -1, 1}, PlotStyle -> Black,
PlotRange -> All, AxesLabel -> {"x", "u(x)"}, BaseStyle -> {Bold, FontSize -> 12},
PlotLabel -> "Solution with \[Nu]=0.05 and \[Delta]=0.01"]

enter image description here


I am interested in solving the equation numerically with NDSolve. The standard routine would be


nu = 0.05; delta = 0.01;
NDSolve[{u''[x] - (1/nu)*u[x]*u'[x] == 0, u[-1] == 1 + delta, u[1] == -1}, u[x], {x, -1, 1}]

However, this code gives rise to a warning of the form step size is effectively zero; singularity or stiff system suspected. I have tried with different methods but obtained no solution.




  • Question 1: How can I solve the ODE {u''[x] - (1/nu)*u[x]*u'[x] == 0, u[-1] == 1 + delta, u[1] == -1}?


Even more complicated is to solve the following system of ODEs arising from a gPC-based stochastic Galerkin projection technique when δ∼Uniform(0,0.1):


p = 10; P = p + 1;

basis = Expand[Orthogonalize[Z^Range[0, p], Integrate[#1 #2 *10, {Z, 0, 1/10}] &]];

region = {Z \[Distributed] UniformDistribution[{0, 1/10}]};


mat = ConstantArray[0, {P, P, P}];
Do[mat[[l, j, k]] = Expectation[basis[[k]]*basis[[j]]*basis[[l]], region],
{k, 1, P}, {j, 1, k}, {l, 1, j}];
Do[mat[[l, j, k]] = mat[[##]] & @@ Sort[{l, j, k}], {k, 1, P}, {j, 1, P}, {l, 1, P}];

cond1 = Table[Expectation[(1 + Z)*basis[[j]], region], {j, 1, P}];
cond2 = ConstantArray[0, P]; cond2[[1]] = -1;

Clear[coeff, x]
coeff[x_] = Table[w[i, x], {i, 1, P}];

side1 = Table[coeff''[x][[j]] - (1/nu)*
Sum[coeff[x][[k]]*coeff'[x][[l]]*mat[[k, l, j]], {k, 1, P}, {l, 1, P}], {j, 1, P}];
side1 = Join[side1, coeff[-1], coeff[1]];
side2 = Join[ConstantArray[0, P], cond1, cond2];
solution = NDSolve[side1 == side2, coeff[x], {x, -1, 1}];

It is not necessary to enter into mathematical details. The idea is that coeff[x] are coefficients of a stochastic expansion of u(x) in terms of Legendre polynomials (which are orthogonal with respect to the density function of δ): u(x)≈∑pi=0wi(x)basisi(δ). The equation side1 == side2 is a system of ODEs with a certain similarity to the steady-state Burger's equation.



  • Question 2: How can I solve the ODE side1 == side2?



Remark: If someone is interested in the problem, it comes from the paper Supersensitivity due to uncertain boundary conditions (2004) by D. Xiu and G.E. Karniadakis, and the book Numerical Methods for Stochastic Computations: A Spectral Method Approach (2010) by D. Xiu (Chapter 1).



Answer




We need to adjust the option of NDSolve a bit. For the first problem, if you're in v12, then you can use nonlinear FiniteElement:


ref = Plot[-A Tanh[A (x - zex)/(2 nu)], {x, -1, 1}, PlotStyle -> Black, PlotRange -> All];

test = NDSolveValue[{u''[x] - (1/nu) u[x] u'[x] == 0, u[-1] == 1 + delta, u[1] == -1},
u, {x, -1, 1}, Method -> FiniteElement]

Plot[test[x], {x, -1, 1}, PlotRange -> All,

PlotStyle -> {Orange, Dashed, Thickness[.01]}]~Show~ref

enter image description here


If you're before v12, then we need to adjust initial guess of Shooting method and choose a higher WorkingPrecision:


shoot[ic_]:={"Shooting", "StartingInitialConditions"->ic};

nu = 5/100; delta = 1/100;
test2 = NDSolveValue[{u''[x] - (1/nu)*u[x]*u'[x] == 0, u[-1] == 1 + delta, u[1] == -1},
u, {x, -1, 1}, Method -> shoot@{u[-1] == 1 + delta, u'[-1] == 0},
WorkingPrecision -> 32]


ListPlot[test2, PlotStyle -> {PointSize@Medium, Orange}]~Show~ref

enter image description here


Here I've plotted InterpolatingFunction with ListPlot, this undocumented syntax is mentioned in this post.


Though the second problem is more challenging, it can be solved in similar manner. Shooting method returns a solution after an hour:


solutionlist = 
Head /@ NDSolveValue[side1 == side2, coeff[x], {x, -1, 1},
Method -> shoot@
Flatten@{side1[[-(p + P + 1);;-(P + 1)]]==side2[[-(p + P + 1);;-(P + 1)]] // Thread,

D[coeff[x], x] == 0 /. x -> -1 // Thread},
WorkingPrecision -> 32]; // AbsoluteTiming

(* {3614.74, Null} *)

ListLinePlot[#, PlotRange -> All] & /@ solutionlist

enter image description here



If speed is concerned for the second question, then turning to finite difference method (FDM) seems to be a good idea. Here I'll use pdetoae for the generation of difference equations.



First we slightly modify the definition of coeff to make it convenient for pdetoae:


coeff[x_] = Table[w[i][x], {i, 1, P}]; 
side1 = Table[
coeff''[x][[j]] -
Sum[coeff[x][[k]] coeff'[x][[l]] mat[[k, l, j]], {k, 1, P}, {l, 1, P}]/nu, {j, 1, P}];
side1lst = {side1, coeff[-1], coeff[1]};
side2lst = {ConstantArray[0, P], cond1, cond2};

Then we discretize the system:


domain = {-1, 1};

points = 100;
difforder = 2;
grid = Array[# &, points, domain];
(* Definition of pdetoae isn't included in this post,
please find it in the link above. *)
ptoafunc = pdetoae[coeff[x], grid, difforder];

del = #[[2 ;; -2]] &;

ae = del /@ ptoafunc[side1lst[[1]] == side2lst[[1]] // Thread];


aebc = Flatten@side1lst[[2 ;;]] == Flatten@side2lst[[2 ;;]] // Thread;

A trivial initial guess seems to be enough, you can choose a better one if you like:


initialguess[var_, x_] := 0

sollst = FindRoot[{ae, aebc},
Flatten[#, 1] &@
Table[{var[x], initialguess[var, x]}, {var, w /@ Range@P}, {x, grid}],
MaxIterations -> 500][[All, -1]]; // AbsoluteTiming

(* {9.655, Null} *)

ListLinePlot[#, PlotRange -> All, DataRange -> domain] & /@ Partition[sollst, points]

The result looks the same as the one given by NDSolve so I'd like to omit it.


Comments

Popular posts from this blog

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]