(*SSEP in contact with reservoirs ; periodic boundary conditions ; jump rate to left or right is 1*)
(*SSEP = simple symmetric exclusion process*)

(*definitions for graphical representations*)

color[n_]=(1-n) White+n Black;

plotconf[conf_]:=Graphics@Table[{color[conf[[i]]],Disk[{i,0},9/10/2]},{i,1,Length@conf}]

tabconf[conf_]:=Table[{color[conf[[i]]],Disk[{i,0},9/10/2]},{i,1,Length@conf}]

colorhist[n_]=(1-n) White+n Purple;

tabconfhist[hist_]:=Table[
confj=hist[[j]];Table[{colorhist[confj[[i]]],Disk[{i,-j},9/10/2]},{i,1,Length@confj}],
{j,1,Length@hist}]

(*evolution of configurations*)

(*L is the number of sites*)
(*NN is the number of particles, which is conserved in time*)
L=50;NN=30;ninit=Table[1,{NN}]~Join~Table[0,{L-NN}];

evolve[m_]:=Module[{tabprob,tabevent,event,n=m},
(*event {i} is a change of state 10->01 or 01->10*)
tabprob=Table[(  n[[i]](1-n[[Mod[i+1,L,1]]]) + (1-n[[i]])n[[Mod[i+1,L,1]]]  )/2,{i,1,L}];
tabevent=Table[{i},{i,1,L}];
event=RandomChoice[tabprob->tabevent];
Do[n[[i]]=1-n[[i]];n[[Mod[i+1,L,1]]]=1-n[[Mod[i+1,L,1]]],{i,event}];
n
]

Framed[Show[Graphics[tabconf[ninit]],ImageSize->800]]

(*to stop the animation: suppress the result from this cell*)
DynamicModule[{nn=ninit},
Framed[Show[Graphics[Dynamic[tabconf[nn=evolve[nn];nn]]],
ImageSize->800
]]]

(*evolution,including past configuration (without their times)*)

L=22;NN=14;ninit=Table[1,{NN}]~Join~Table[0,{L-NN}];

(*duration is the number of previously visidet configurations that are remembered*)
IS=700;duration=L;
DynamicModule[{nn=ninit,hist=Table[ninit,{duration}]},
Framed[Dynamic[Column[{
Graphics[tabconf[nn=evolve[nn];nn],ImageSize->IS],
Graphics[
hist=(Prepend[hist,nn])[[;;duration]];
tabconfhist[hist]
,ImageSize->IS]
}]
]]]