rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
burgers_examples.m
1 function res = burgers_examples(step)
2 %function res = burgers_examples(step)
3 %
4 % Burgers equation with 1D finite difference discretization
5 % demonstrating rarefaction waves and shocks moving
6 %
7 % The steps demonstrate the movement of a shock, the development of
8 % a rarefaction wave for different initial values and velocity directions,
9 % different ODE solvers, different inputs and initial conditions.
10 %
11 % As example solver I provide an implicit Euler solver and a visualization
12 % routine with the model. In step=6 I demonstrate that also matlab ODE
13 % solvers can be used (but are slower) than the implicit Euler scheme.
14 %
15 % The model is "tough" from model reduction perspective, as it has a
16 % moving discontinuity if a shock develops, which cannot very well be
17 % modelled by low-dimensional spaces.
18 %
19 % You can define the input signal u arbitrarily, also time-varying. In
20 % the examples I set it to constant-in-time values.
21 % However the implicit Euler solver has a time-step width set to be stable
22 % for u in [0,1]^2. Similar the inital value can be chosen arbitrarily in
23 % x0 \in [0,1]^(n_xi+1) guaranteeing a stable implicit Euler discretization.
24 % So you should prevent input or initial value going beyond this range.
25 
26 % B. Haasdonk 8.6.2016
27 
28 if nargin < 1
29  step = 1;
30 end;
31 
32 %%%% This parameter scales the spatial and time-domain resolution:
33 n_xi = 200; % choose between 10 and 1000 points
34 
35 %%%% do not change the following values, as these nicely result in
36 %%%% stable numerical scheme and give shock at boundary at end time:
37 nt = 2* n_xi;
38 T = 0.25;
39 
40 switch step
41  case 1 % rarefaction wave
42 
43  % model mainly makes sense with constant u corresponding to
44  % initial values
45 
46  disp('example of shock moving to the right');
47 
48  x_left = 1;
49  x_right = 0;
50  v = 1;
51  model = burgers_1d_model(n_xi, T, x_left, x_right, v);
52  u = @(t) [x_left, x_right]';
53  x0 = model.x0;
54  % nt = model.n_xi;
55  % nt = 200;
56  X = model.explicit_euler_time_integration(model,x0,u,nt);
57  model.plot_results(model,X);
58 
59  case 2 % shock moving
60 
61  disp('example of rarefaction wave to the right');
62 
63  %%%% change the following values between [-1,1]:
64  x_left = 0;
65  x_right = 1;
66  v = 1;
67  model = burgers_1d_model(n_xi, T, x_left, x_right, v);
68  u = @(t) [x_left, x_right]';
69  x0 = model.x0;
70  X = model.explicit_euler_time_integration(model,x0,u,nt);
71  model.plot_results(model,X);
72 
73  case 3 % same as 2 but reverted velocity
74 
75  disp('example of shock wave to the left');
76 
77  x_left = 0;
78  x_right = 1;
79  v = -1;
80  model = burgers_1d_model(n_xi, T, x_left, x_right, v);
81  u = @(t) [x_left, x_right]';
82  x0 = model.x0;
83  X = model.explicit_euler_time_integration(model,x0,u,nt);
84  model.plot_results(model,X);
85 
86  case 4
87 
88  disp('example of rarefaction wave to the left');
89 
90  x_left = 1;
91  x_right = 0;
92  v = -1;
93  model = burgers_1d_model(n_xi, T, x_left, x_right, v);
94  u = @(t) [x_left, x_right]';
95  x0 = model.x0;
96  X = model.explicit_euler_time_integration(model,x0,u,nt);
97  model.plot_results(model,X);
98 
99  case 5 % use arbitrary input u
100 
101  disp('arbitrary input (boundary values) 0.5:');
102  disp('result is shock and rarefaction wave');
103  x_left = 0;
104  x_right = 1;
105  v = 1;
106  model = burgers_1d_model(n_xi, T, x_left, x_right, v);
107  u = @(t) [0.5,0.5]';
108  x0 = model.x0;
109  tic
110  X = model.explicit_euler_time_integration(model,x0,u,nt);
111  t = toc;
112  disp(['computation took ',num2str(t),' sec.']);
113  model.plot_results(model,X);
114 
115  case 6 % use matlab integrator for case 5
116 
117  disp('same as step 5 (rarefaction wave + shock) but with ode45:')
118  x_left = 0;
119  x_right = 1;
120  v = 1;
121  model = burgers_1d_model(n_xi, T, x_left, x_right, v);
122  u = @(t) [0.5,0.5]';
123  x0 = model.x0;
124 
125  odefun = @(t,x) model.f(x,u(t));
126  tspan = [0,T/2,T];
127  tic
128  [tout, Xtransp] = ode45(odefun, tspan, x0);
129  t = toc;
130  disp(['computation took ',num2str(t),' sec.']);
131  model.plot_results(model,Xtransp');
132 
133  case 7 % use other initial value generating a shock
134 
135  disp('development of shock');
136  x_left = NaN; % not used
137  x_right = NaN; % not used if having external x0
138  v = 1;
139  model = burgers_1d_model(n_xi, T, x_left, x_right, v);
140  u = @(t) [0.5,0.5]';
141 % x0 = model.x0;
142  x0 = zeros(n_xi+1,1);
143  x0(20:40) = (0:20)/20;
144  x0(41:60) = (19:-1:0)/20;
145  tic
146  X = model.explicit_euler_time_integration(model,x0,u,nt);
147  t = toc;
148  disp(['computation took ',num2str(t),' sec.']);
149  model.plot_results(model,X);
150 
151 
152  otherwise
153  error('step unknown');
154 end;
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...