rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
burgers_1d_model.m
Go to the documentation of this file.
1 function model = burgers_1d_model(n_xi,T,x_left, x_right, v)
2 % simple model for Burgers PDE
3 % $d/dt x + d/d_xi (1/2 * v * x^2) = 0$
4 % on the unit square xi in [0,1]
5 % with initial value
6 % x(.,0) = x0(.) = piecewise constant x_left and x_right on left/right half
7 % of domain
8 % and dirichlet boundary values x_left and x_right and
9 % discretization via Lax-friedrichs (central differences with
10 % numerical diffusion)
11 % The v is the velocity. the parameter vector is [x_left, x_right, v]
12 %
13 %
14 % The corresponding script showing several aspects: call
15 % burgers_examples(step),
16 % where step can vary from 1 to 7. Please run these steps to get an
17 % insight into the model.
18 %
19 %The model provides the function f(.,.) for the ode system
20 % d/dt x(t) = f(x(t),u(t)) with x(0) = x0
21 %which represents a nonlinear non-viscous Burgers equation discretized with
22 %upwind finite differences. This means, we have n_xi spatial intervals,
23 %hence n_xi+1 points on the grid and a solution variable in each gridpoint,
24 %hence the state x(t) is n_xi + 1 dimensional.
25 %The input u is assumed to be 2-dimensional: first entry left boundary
26 %value x_left, second entry right boundary value
27 %x_right, so that value will be "entering" the domain over time.
28 %x0 is arbitrary, but the model provides a "default" value in the sense of
29 %having half the domain filled with both boundary values, a so called
30 %"Riemann-problem" for the hyperbolic problem. Step 1-6 use the default
31 %initial value, step 7 uses an arbitrary initial value.
32 %
33 %It is a model, that is scalable, i.e. one can arbitrarily
34 %set the n_xi variable, that defines the number of spatial intervals
35 %of the discretized Burgers equation.
36 
37 % B .Haasdonk 8.6.2016
38 
39 model = [];
40 % number of x intervals
41 model.n_xi = n_xi;
42 model.delta_xi = 1/n_xi;
43 model.grid = 0:1/n_xi:1;
44 model.x_left = x_left;
45 model.x_right = x_right;
46 model.v = v;
47 model.T = T;
48 model.lambda = 0.5; % for Lax-Friedrichs-flux: numerical diffusion
49 model.x0 = my_init_values(model);
50 model.f = @(x,u) burgers_nonlinearity(model,x,u);
51 model.explicit_euler_time_integration = @(model,x0,u,nt) ...
52  explicit_euler_time_integration(model, x0, u, nt);
53 model.plot_results = @(model,X) plot_results(model,X);
54 
55 function x0 = my_init_values(model);
56 % piecewise constant initial value for Riemann problem
57 x0 = zeros(model.n_xi+1,1);
58 mid_ind = floor((model.n_xi+1)/2);
59 x0(1:mid_ind) = model.x_left;
60 x0(mid_ind+1:end) = model.x_right;
61 
62 function f = burgers_nonlinearity(model,x,u);
63 % For simplicity: Lax-Friedrichs flux
64 % g(x,w) = 1/2 (f(x) + f(w)) + 1/(2 lambda) (x-w)
65 % where (x,w) are neighbouring values of the state
66 % then
67 % 1/delta_t (x(t+delta_t)-x(t)) can be approximated by
68 % -1/delta_x * (g(x,xshift_plus1 - g(x_shift_min1,x))
69 
70 %% The following would be correct if boundary values instead of
71 %% input u were to be used:
72 %x_min1 = model.x_left; % left dirichlet value
73 %x_plus1 = model.x_right; % right dirichlet value
74 %xshift_plus1 = [x(2:end); x_plus1];
75 %xshift_min1 = [x_min1; x(1:end-1)];
76 
77 v = model.v; % velocity
78 xshift_plus1 = [x(2:end); u(2)];
79 xshift_min1 = [u(1); x(1:end-1)];
80 %%%% Lax Friedrichs Flux
81 %f = + 1/ (2*model.delta_xi) * ...
82 % ( v * ( xshift_plus1.^2 - xshift_min1.^2) ...
83 % - 1/model.lambda * (2*x -xshift_plus1 - xshift_min1));
84 %%%% Engquist Osher Flux (cmp: Kroener '97)
85 g_x_xshift_plus1 = fplus(model,x) + fminus(model,xshift_plus1);
86 g_xshift_min1_x = fplus(model,xshift_min1) + fminus(model,x);
87 f = - 1/ (model.delta_xi) * ...
88  ( g_x_xshift_plus1 - g_xshift_min1_x );
89 
90 function f = fplus(model,x)
91 f = max(2*model.v*x,0) .* x;
92 
93 function f = fminus(model,x)
94 f = min(2*model.v*x,0) .* x;
95 
96 function X = explicit_euler_time_integration(model, x0, u, nt)
97 % simple explicit Euler scheme
98 nx = length(x0);
99 delta_t = model.T/nt;
100 X = zeros(nx,nt+1);
101 x = x0(:);
102 X(:,1) = x;
103 for i=1:nt
104  ut = u((i-1)*delta_t);
105  xnew = x + delta_t* model.f(x,ut);
106 % keyboard;
107  X(:,i+1) = xnew;
108  x = xnew;
109 end;
110 
111 function p = plot_results(model,X)
112 ts = [0,floor((size(X,2)-1)/2), size(X,2)-1];
113 for i = 1:length(ts);
114  subplot(length(ts),1,i)
115  hold on;
116  plot(model.grid,X(:,ts(i)+1));
117  title(['state at t = ',num2str(ts(i)),' * \Delta t']);
118 end;
119 
120 
121 
122 
function model = burgers_1d_model(n_xi, T, x_left, x_right, v)
simple model for Burgers PDE $d/dt x + d/d_xi (1/2 * v * x^2) = 0$ on the unit square xi in [0...