In [2]:
using Plots;
gadfly();

In [3]:
include("../fdtd/update.jl");
include("../fdtd/sources.jl");
include("../fdtd/boundaries.jl");
using update;
using sources;


WARNING: replacing module globals

In [23]:
#Global parameters
size = 400;
endTime = 5000;
num_snaps = 150;
snap_step = div(endTime, num_snaps);

# Incident
inc_pos = 100;

# Material
n1 = 1.5;
n2 = 2;
n2 = 2.076125;

eps1 = n1^2;
eps2 = n2^2;


wavelength = round(Integer, size / 5);
q_wavelength = round(Integer, wavelength * n1 / n2 / 2);
#Grid

# Magnetic
hy = zeros(size-1);
hy_0 = zeros(size-1);
mu = ones(size-1);

chyh = ones(size);
chye = ones(size);


# Electric
ez = zeros(size);
ez_0 = zeros(size);
eps = ones(size) * eps1;
eps_0 = ones(size) * eps1;

cezh = ones(size);
ceze = ones(size);


#for i in 110:170
#    eps[i] = eps1;
#end
for i in div(size, 2):div(size, 2)+q_wavelength
    eps[i] = eps2;
end

rightBound = boundaries.setup_first_order_abc(eps, mu, size, true)
leftBound = boundaries.setup_first_order_abc(eps, mu, 1, false)

rightBound_0 = boundaries.setup_first_order_abc(eps_0, mu, size, true)
leftBound_0 = boundaries.setup_first_order_abc(eps_0, mu, 1, false)

# output params
ez_snapshot = Array{Any}(num_snaps);
ez_0_snapshot = Array{Any}(num_snaps);

In [ ]:
# Time steps

for time in 1:endTime
    # Incident
    ez_inc = sin(2*pi/(wavelength) * (time-1))*exp(-1000/time)
    
    #
    # Magnetic
    #
        
    # Interior update
    update.update_magnetic_field!(ez, hy, mu, chyh, chye);    
    update.update_magnetic_field!(ez_0, hy_0, mu, chyh, chye);          
    #
    # Electric
    #
       
    # Interior update
    update.update_electric_field!(ez, hy, eps, cezh, ceze);
    update.update_electric_field!(ez_0, hy_0, eps_0, cezh, ceze);    
    
    # ABC
    boundaries.first_order_diff_abc!(ez, leftBound)
    boundaries.first_order_diff_abc!(ez, rightBound)

    boundaries.first_order_diff_abc!(ez_0, leftBound_0)
    boundaries.first_order_diff_abc!(ez_0, rightBound_0)

    
    # Incident
    ez[inc_pos] += ez_inc / sqrt( eps[inc_pos] * mu[inc_pos])
    ez_0[inc_pos] += ez_inc / sqrt( eps_0[inc_pos] * mu[inc_pos])
 
    # Snapshots for animation
    if mod(time, snap_step) == 0
        ez_snapshot[div(time,snap_step)] = (time, copy(ez))
        ez_0_snapshot[div(time,snap_step)] = (time, copy(ez_0))
    end
    
end

In [22]:
anim = Animation()

for i = 1:num_snaps
    p = plot(1:size, ez_snapshot[i][2], lab="Ez")
    plot!(1:size, ez_0_snapshot[i][2], lab="Ez_0")
    
    time = ez_snapshot[i][1]
    plot!(ann=[(0.8*size, 1.5, "time =$time")])
    plot!([size/2, size/2], [-2, 2])
    plot!([size/2+q_wavelength, size/2+q_wavelength-1], [-2, 2])
    
    plot!(xlims=(1, size), ylims=(-2, 2))
    frame(anim, p)
end
gif(anim, "./Task3/Half_Wavelength_Reflection.gif", fps=15)


INFO: Saved animation to /media/storage/Documents/Github/students-2016/1_fdtd/Tasks/Task3/Half_Wavelength_Reflection.gif
Out[22]:

In [19]:
plot( (ez-ez_0)[1:div(size, 2)])


0 50 100 150 200 y1 -0.2 -0.1 0.0 0.1 0.2
Out[19]:

In [ ]:


In [ ]: