MHD Problem 1,2,3
MHD Problem 1,2,3
Solve the one-dimensional MHD shock tube problem using the Lax-Friedrichs
method.
Steps: 1. Define the problem with initial and boundary conditions,
Problem Description:
The one-dimensional MHD shock tube problem involves a sudden transition from one state to
another across a shock wave.
Initial Conditions:
Left Side:
Right Side:
Wave Propagation
In the MHD shock tube problem, the initial discontinuity in density and pressure leads to the
propagation of various types of waves, each characterized by specific behaviors and interactions:
Regions where the fluid expands rapidly, resulting in a decrease in density and
pressure.
These waves travel faster than the speed of sound in the medium and are influenced
by both fluid and magnetic pressures.
Slow Shock Waves
Regions where the fluid compresses and slows down, resulting in an increase in
density and pressure.
These waves travel slower than the fast shocks and are influenced by the interaction
between the fluid dynamics and magnetic fields.
Contact Discontinuity
A surface separating two regions of fluid with different densities but the same
pressure and velocity.
It moves with the fluid velocity and marks the boundary between the left and right
states in the shock tube.
Alfven Waves
Numerical diffusion, or artificial viscosity, is introduced by the Lax-Friedrichs method to stabilize the
solution. It smooths out sharp gradients and discontinuities, preventing non-physical oscillations.
Steps to Implement the Lax-Friedrichs Method
Matlab code:
1.
% Define constants and parameters
nx = 200; % number of spatial points
nt = 400; % number of time steps
L = 1.0; % length of the tube
dx = L / nx; % spatial step size
dt = 0.001; % time step size
% Initialize variables
x = linspace(-L/2, L/2, nx);
rho = zeros(nx, 1);
u = zeros(nx, 1);
p = zeros(nx, 1);
B_x = 1.0 * ones(nx, 1);
B_y = zeros(nx, 1);
B_z = zeros(nx, 1);
E = zeros(nx, 1);
% Initial conditions
rho_L = 1.0;
u_L = 0.0;
p_L = 1.0;
rho_R = 0.125;
u_R = 0.0;
p_R = 0.1;
% Initial conditions
for i = 1:nx
if x(i) < 0
rho(i) = rho_L;
u(i) = u_L;
p(i) = p_L;
else
rho(i) = rho_R;
u(i) = u_R;
p(i) = p_R;
end
end
% Time-stepping loop
for n = 1:nt
% Compute fluxes
F = zeros(nx, 7); % Flux vector
for i = 1:nx
F(i, 1) = rho(i) * u(i);
F(i, 2) = rho(i) * u(i)^2 + p(i) + (B_x(i)^2 + B_y(i)^2 + B_z(i)^2) / 2 -
B_x(i)^2;
F(i, 3) = rho(i) * u(i) * u(i);
F(i, 4) = rho(i) * u(i) * u(i);
F(i, 5) = (E(i) + p(i) + (B_x(i)^2 + B_y(i)^2 + B_z(i)^2) / 2) * u(i);
F(i, 6) = u(i) * B_y(i);
F(i, 7) = u(i) * B_z(i);
end
% Update variables
rho = U_new(:, 1);
u = U_new(:, 2) ./ rho;
E = U_new(:, 5);
B_y = U_new(:, 6);
B_z = U_new(:, 7);
% Update pressure
for i = 1:nx
p(i) = (gamma - 1) * (E(i) - 0.5 * rho(i) * u(i)^2 - 0.5 * (B_x(i)^2 +
B_y(i)^2 + B_z(i)^2));
end
end
% Visualization
figure;
subplot(3, 1, 1);
plot(x, rho);
xlabel('x');
ylabel('\rho');
title('Density');
subplot(3, 1, 2);
plot(x, u);
xlabel('x');
ylabel('u');
title('Velocity');
subplot(3, 1, 3);
plot(x, p);
xlabel('x');
ylabel('p');
title('Pressure');
2.
% Parameters
nx = 200; % Number of spatial points
nt = 400; % Number of time steps
L = 1.0; % Length of the tube
dx = L / nx; % Spatial step size
dt = 0.001; % Time step size
gamma = 1.4; % Adiabatic index
% Initialize variables
x = linspace(-L/2, L/2, nx);
rho = zeros(nx, 1);
u = zeros(nx, 1);
v = zeros(nx, 1);
w = zeros(nx, 1);
p = zeros(nx, 1);
B_x = 1.0 * ones(nx, 1); % Constant magnetic field in x direction
B_y = zeros(nx, 1);
B_z = zeros(nx, 1);
E = zeros(nx, 1);
% Initial conditions
rho_L = 1.0; % Higher density on the left
u_L = 0.0; % Zero velocity on the left
p_L = 1.0; % Higher pressure on the left
B_x_L = 1.0; % Magnetic field aligned with the tube on the left
% Time-stepping loop
for n = 1:nt
% Compute fluxes
F = zeros(nx, 7); % Flux vector
for i = 1:nx
F(i, 1) = rho(i) * u(i);
F(i, 2) = rho(i) * u(i)^2 + p(i) + 0.5 * (B_x(i)^2 + B_y(i)^2 + B_z(i)^2)
- B_x(i)^2;
F(i, 3) = rho(i) * u(i) * v(i) - B_x(i) * B_y(i);
F(i, 4) = rho(i) * u(i) * w(i) - B_x(i) * B_z(i);
F(i, 5) = (E(i) + p(i) + 0.5 * (B_x(i)^2 + B_y(i)^2 + B_z(i)^2)) * u(i) -
B_x(i) * (u(i) * B_x(i) + v(i) * B_y(i) + w(i) * B_z(i));
F(i, 6) = u(i) * B_y(i) - v(i) * B_x(i);
F(i, 7) = u(i) * B_z(i) - w(i) * B_x(i);
end
% Update variables
rho = U_new(:, 1);
u = U_new(:, 2) ./ rho;
v = U_new(:, 3) ./ rho;
w = U_new(:, 4) ./ rho;
E = U_new(:, 5);
B_y = U_new(:, 6);
B_z = U_new(:, 7);
% Update pressure
for i = 1:nx
p(i) = (gamma - 1) * (E(i) - 0.5 * rho(i) * (u(i)^2 + v(i)^2 + w(i)^2) -
0.5 * (B_x(i)^2 + B_y(i)^2 + B_z(i)^2));
end
end
% Visualization
figure;
% Density
subplot(3, 2, 1);
plot(x, rho);
xlabel('x');
ylabel('\rho');
title('Density');
% Velocity
subplot(3, 2, 2);
plot(x, u);
xlabel('x');
ylabel('u');
title('Velocity');
% Pressure
subplot(3, 2, 3);
plot(x, p);
xlabel('x');
ylabel('p');
title('Pressure');
% Energy
subplot(3, 2, 6);
plot(x, E);
xlabel('x');
ylabel('E');
title('Energy');
Stability and Accuracy:
The Lax–Friedrichs method is first order accurate in time and first order accurate in
space.
It exhibits second-order dissipation and third-order dispersion.
2. Two-Dimensional MHD Flow in a Channel
Solve the two-dimensional MHD flow in a channel with conducting walls using
% Discretization
dx = Lx / (Nx - 1);
dy = Ly / (Ny - 1);
x = linspace(0, Lx, Nx);
y = linspace(0, Ly, Ny);
% Physical parameters
rho = 1; % Density
mu = 0.1; % Dynamic viscosity
sigma = 1; % Electrical conductivity
eta = 0.01; % Magnetic diffusivity
% Initial conditions
u = zeros(Nx, Ny);
v = zeros(Nx, Ny);
Bx = zeros(Nx, Ny);
By = zeros(Nx, Ny);
% Time loop
for t = 0:dt:T
% Copy current state
u_old = u;
v_old = v;
Bx_old = Bx;
By_old = By;
% Visualize
if mod(t, 0.1) == 0
subplot(2,1,1);
quiver(x, y, u', v');
title('Velocity Field');
xlabel('x');
ylabel('y');
subplot(2,1,2);
quiver(x, y, Bx', By');
title('Magnetic Field');
xlabel('x');
ylabel('y');
drawnow;
end
end
% Define domain parameters
L = 1.0; % Channel width (example value )
H = 1.0; % Channel height (example value )
Nx = 50; % Number of grid points in x-direction
Ny = 50; % Number of grid points in y-direction
dx = L / (Nx - 1); % Grid spacing in x
dy = H / (Ny - 1); % Grid spacing in y
% Time-stepping parameters
dt = 0.001; % Time step size (example value )
iterations = 100; % Number of iterations
% y-momentum equation
v(j, i) = v_old(j, i) + dt * ( ...
- u_old(j, i) * (v_old(j, i+1) - v_old(j, i-1)) / (2*dx) ...
- v_old(j, i) * (v_old(j+1, i) - v_old(j-1, i)) / (2*dy) ...
+ mu * ((v_old(j, i+1) - 2*v_old(j, i) + v_old(j, i-1)) / dx^2 ...
+ (v_old(j+1, i) - 2*v_old(j, i) + v_old(j-1, i)) / dy^2) / rho );
end
end
% Visualize results
figure
contourf(x, y, u);
title('Velocity Magnitude');
xlabel('x');
ylabel('y');
colorbar;
figure
contourf(x, y, B);
title('Magnetic Field');
xlabel('x');
ylabel('y');
colorbar;
3. Magnetic Diffusion in a One-Dimensional Conducting Medium
% Time-stepping loop
for j = 2:M
% Apply boundary conditions
B(1) = 0; % Left boundary condition
B(end) = 0; % Right boundary condition
% Parameters
L = 10; % Length of the domain
T = 1; % Total time
nx = 50; % Number of spatial grid points
nt = 1000; % Number of time steps
eta = 1; % Magnetic diffusivity
dx = L / (nx - 1);
dt = T / nt;
alpha = eta * dt / (2 * dx^2);
% Initial condition
B = sin(pi * x / L)'; % Example initial condition
% Boundary conditions
B1 = 0; % Boundary condition at x = 0
B2 = 0; % Boundary condition at x = L
% Time-stepping loop
for n = 1:nt
% Extract interior points and apply boundary conditions
B_interior = B(2:end-1); % Ensure B_interior is a column vector
% Visualization
figure;
plot(x, B, 'LineWidth', 2);
xlabel('x');
ylabel('B(x, T)');
title('Magnetic Field Distribution at Final Time T');
grid on;