I am a newbie in Julia an I have written the following code:
using LinearAlgebra
using Plots
t = 0;
epsilon = 0.1;
########## initial data: Sod problem (rho,u,T) ##########
Prim_l = [1 0 1]; # 0 <= x <= 0.5
Prim_r = [0.125 0 0.1]; # 0.5 < x <= 1
########## domain in space ##########
x_start = 0; # start
x_end = 1; # end
k = 250; # cell discretization, grid-points
dx = (x_end-x_start)/k;
########## velocity space (v = molecular velocities at certain point x at time t) ##########
v_start = -4.5; # start
v_end = 4.5; # end
v_nodes = 100; # velocity discretization, velocity-points
v = collect(range(v_start,v_end,v_nodes)); # collect(range(a,b,n)) <-> LinRange(x): works!
w = 9/v_nodes*ones(size(v)); # width (for numerical integration)
########## calculation initial elements of U ##########
xx = LinRange(x_start,x_end,k+1);
xm = 0.5*(xx[1:end-1] + xx[2:end]);
U = zeros(k,3);
g_val = zeros(k+1,v_nodes);
U[:,1] = (xm.<=0.5)*Prim_l[1]+(xm.>0.5)*Prim_r[1]; # rho
U[:,2] = (xm.<=0.5)*Prim_l[2]+(xm.>0.5)*Prim_r[2]; # u bulk velocity of the gas
U[:,3] = (xm.<=0.5)*Prim_l[3]+(xm.>0.5)*Prim_r[3]; # temperatur
########## initial data: Flux, Source ##########
Flux = zeros(k+1,3);
Source = zeros(k,3);
########## parts of v ##########
vvp = kron(ones(k+1,1),v'.*(v'.>0)); # v^{+}
vvm = kron(ones(k+1,1),v'.*(v'.<0)); # v^{-}
vv = kron(ones(k+1,1),v'); # v
vv_s = kron(ones(k,1),v');
dt = 0.0005; # time-step
t_end = 0.14; # running time
while t < t_end
M = Maxwell(U, v);
vM = M[[1;1:k],:].*vvp + M[[1:k;k],:].*vvm; # term: <m*(v^{+}*M^{n}_{i} + v^{-}*M^{n}_{i+1})>
vg_dx = (g_val[1:k+1,:]-g_val[[1;1:k],:]).*vvp/dx + (g_val[[2:k+1;k+1],:]-g_val[1:k+1,:]).*vvm/dx; # term: [v^{+}*(g^{n}_{i+1/2}-g^{n}_{i-1/2})/deltax + v^{-}*(g^{n}_{i+3/2}-g^{n}_{i+1/2})/deltax]
vM_dx = vv.*(M[[1:k;k],:]-M[[1;1:k],:])/dx; # term: (v*(M^{n}_{i+1}-M^{n}_{i})/deltax)
A = id_min_proj(U, M, vg_dx, vM_dx, v, w); # return value id_min_proj: tensor([251,100],2)
vg_dx = A[1];
vM_dx = A[2];
g_val = (g_val - dt*vg_dx - dt/epsilon*vM_dx)/(1+dt/epsilon); # kinetic equation
Flux[:,1] = vM*w;
Flux[:,2] = (vv.*vM)*w;
Flux[:,3] = 0.5*(vv.^2 .*vM)*w;
g_dx = (g_val[2:k+1,:]-g_val[1:k,:])/dx;
Source[:,1] = g_dx*diagm(w)*v;
Source[:,2] = (vv_s.*g_dx)*diagm(w)*v;
Source[:,3] = 0.5*(vv_s.^2 .*g_dx)*diagm(w)*v;
U = U - dt/dx*(Flux[2:end,:]-Flux[1:end-1,:]) - dt*epsilon*Source; # macroscopic equation
t = t+dt;
end
rho = U[:,1];
u = U[:,2]./rho;
T = 2*U[:,3]./rho-u.^2;
I get the following output in REPL:
id_min_proj (generic function with 1 method)
Maxwell (generic function with 1 method)
┌ Warning: Assignment to `g_val` in soft scope is ambiguous because a global variable by the same name exists: `g_val` will be treated as a new local. Disambiguate by using `local g_val` to suppress this warning or `global g_val` to assign to the existing global variable.
└ @ d:ProgrammierungJulia.ProjekteTest.jl:71
┌ Warning: Assignment to `U` in soft scope is ambiguous because a global variable by the same name exists: `U` will be treated as a new local. Disambiguate by using `local U` to suppress this warning or `global U` to assign to the existing global variable.
└ @ d:ProgrammierungJulia.ProjekteTest.jl:82
┌ Warning: Assignment to `t` in soft scope is ambiguous because a global variable by the same name exists: `t` will be treated as a new local. Disambiguate by using `local t` to suppress this warning or `global t` to assign to the existing global variable.
└ @ d:ProgrammierungJulia.ProjekteTest.jl:84
ERROR: UndefVarError: `U` not defined in local scope
Suggestion: check for an assignment to a local variable that shadows a global of the same name.
Stacktrace:
[1] top-level scope
@ d:ProgrammierungJulia.ProjekteTest.jl:61
julia> M
ERROR: UndefVarError: `M` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Okay … the matrix M is not calculated, I think, due to the warnings.
I took some looks at books about Julia with code examples. I cannot find any hints with this scopes.
All in all … this code works in Matlab. I have re-write this code in Julia, with some changes (such as the matrix-notations).
Kind regars!
Julia documentation, books about Julia and code examples