# This is a module for 8.08 - 8.S308 numerics recitation. module Rec #parameters of the model mutable struct Param L::Int64 # number of sites epsilon::Float64 # small hopping rate end #constructor function setParam(L, epsilon) param = Param(L, epsilon) return param end #state variables of the model mutable struct State t::Float64 # current time position::Int8 # occupancy field end #constructor function setState(param) position = 1 state = State(0.0, position) return state end end function tower_sampling(w, W, rng) ### Implement your tower sampling algorithm index = 1 return index end function run_simulation!(state, param, n_run, rng) t_now = state.t # time at the beginning of your simulation L, epsilon = param.L, param.epsilon # loading parameters move = 0 # type of your move w = zeros(Float64, 2) # transition rates W = 0.0 # sum of the transition rates tau = 0.0 # time sampled # move 1: move clockwise, 3->2->1->3 # move 2: move counter clockwise, 1->2->3->1 # position 1: move to 2 with a rate 1, move to 3 with a rate epsilon # position 2: move to 3 with a rate 1, move to 1 with a rate epsilon # position 3: move to 1 with a rtae epsilon, move to 2 with a rate epsilon ### write down transition rates if state.position==1 || state.position==2 w[1] = # rate for move 1 w[2] = # rate for move 2 else w[1] = # rate for move 1 w[2] = # rate for move 2 end W = sum(w) for i_run in 1:n_run ### get time tau = # gamble and get your move move = tower_sampling(w, W, rng) ### write down how your state would change if move==1 # move clockwise, 3->2->1->3 state.position = else # move counter clockwise, 1->2->3->1 state.position = end ### write down transition rates if state.position==1 || state.position==2 w[1] = # rate for move 1 w[2] = # rate for move 2 else w[1] = # rate for move 1 w[2] = # rate for move 2 end W = sum(w) state.t += tau end end function get_movie!(state, param, rng, n_gap, n_frame, in_fps) # arrays for recording time and the number of hopping time_array = zeros(Float64, 2*n_frame+1) hopping_array = zeros(Int64, 2*n_frame+1) # initialize time_array[1] = state.t hopping_array[1] = 0 current_position = state.position move = 0 anim = @animate for frame in 1:n_frame run_simulation!(state, param, n_gap, rng) # count the number of counter-clockwise move if mod(state.position - current_position,3)==1 move = 1 else move = -1 end current_position = state.position time_array[2frame] = state.t time_array[2frame+1] = state.t hopping_array[2frame] = hopping_array[2frame-1] hopping_array[2frame+1] = hopping_array[2frame] + move timetitle = "time = "*string( round(state.t, digits = 1) ) eps_label = "epsilon = "*string( param.epsilon ) plot( time_array[1:2frame+1], hopping_array[1:2frame+1], m=(2), legend=:bottomright, label=eps_label, title=timetitle) plot!(xlabel = "time", ylabel = "# of counter-clockwise motion") end name = "Rec11_3state_movie.gif" gif(anim, name, fps=in_fps) end