(* Defining the simulation parameters *)

A = 3; 
ϵ = 0.2;
α = 3; (* Changes height of cubic nullcline peak *)
γ = 0.05;   (* 1/γ is the gradient of the straight-line nullcline *)
v0 = 0.4; (* -1/γ v0 is the y intercept of the straight-line nullcline *)
w0 = 0.4;
T = 25;

n = 25; (* Size of lattice n x m *)
m = 25;

(* initialising parameters *)

clustersize=Table[i,{i,1,1}];

(* initialising step size for calculating the number of excited cells *)
stp = 0.1;
K = 1; (* Coupling strength *)


(* Calculating the starting values for t = 0 *)

initial     = Solve[wi == A*vi*(vi-α)*(1-vi)-w0 && wi == (vi-v0)/γ, {vi,wi}, Reals];
initialline = Solve[wj == A*vj*(vj-α)*(1-vj)-w0 && wj == wi/.initial[[1]],{vj,wj},Reals];
localmax    = FindMaxValue[A*vi*(vi-α)*(1-vi)-w0, {vi,vj/.initialline[[3]]}];

(* Defining the perturbed cell *)
kickleftN = Floor[(n-1)/2];
kickrightN = Floor[(n+3)/2];	
kickcellN = Floor[(n+1)/2];

kickleftM = Floor[(m-1)/2];
kickrightM = Floor[(m+3)/2];	
kickcellM = Floor[(m+1)/2];

(* Cycling through perturbation values, probability of connections existing, and number of iterations *)

Monitor[Monitor[Monitor[
For[kick=1,kick≤2.5,kick=kick+0.5,
For[prob=0, prob≤1, prob=prob+0.1,
For[JJ=1,JJ≤100,JJ++,


(* FHN equations and initial conditions *)

fitzeqns =Join[
{v[1,1]'[t]==((A*v[1,1][t]*(v[1,1][t]-α)*(1-v[1,1][t])-w[1,1][t]-w0)/ϵ)
+K*(v[1,2][t]-v[1,1][t])
+K*(v[2,1][t]-v[1,1][t])
+K*(v[1,m][t]-v[1,1][t])
+K*(v[n,1][t]-v[1,1][t])},

			Table[v[1,i]'[t]==((A*v[1,i][t]*(v[1,i][t]-α)*(1-v[1,i][t])-w[1,i][t]-w0)/ϵ)
+K*(v[1,i-1][t]-v[1,i][t])
+K*(v[2,i][t]-v[1,i][t])
+K*(v[1,i+1][t]-v[1,i][t])
+K*(v[n,i][t]-v[1,i][t]),{i,2,m-1}],
			
			{v[1,m]'[t]==((A*v[1,m][t]*(v[1,m][t]-α)*(1-v[1,m][t])-w[1,m][t]-w0)/ϵ)
+K*(v[1,m-1][t]-v[1,m][t])
+K*(v[2,m][t]-v[1,m][t])
+K*(v[1,1][t]-v[1,m][t])
+K*(v[n,m][t]-v[1,m][t])},

			Table[v[i,1]'[t]==((A*v[i,1][t]*(v[i,1][t]-α)*(1-v[i,1][t])-w[i,1][t]-w0)/ϵ)
+K*(v[i-1,1][t]-v[i,1][t])
+K*(v[i,2][t]-v[i,1][t])
+K*(v[i+1,1][t]-v[i,1][t])
+K*(v[i,m][t]-v[i,1][t]),{i,2,n-1}],

			Table[v[i,m]'[t]==((A*v[i,m][t]*(v[i,m][t]-α)*(1-v[i,m][t])-w[i,m][t]-w0)/ϵ)
+K*(v[i-1,m][t]-v[i,m][t])
+K*(v[i,m-1][t]-v[i,m][t])
+K*(v[i+1,m][t]-v[i,m][t])
+K*(v[i,1][t]-v[i,m][t]),{i,2,n-1}],

			{v[n,1]'[t]==((A*v[n,1][t]*(v[n,1][t]-α)*(1-v[n,1][t])-w[n,1][t]-w0)/ϵ)
+K*(v[n,2][t]-v[n,1][t])
+K*(v[n-1,1][t]-v[n,1][t])
+K*(v[1,1][t]-v[n,1][t])
+K*(v[n,m][t]-v[n,1][t])},

			Table[v[n,i]'[t]==((A*v[n,i][t]*(v[n,i][t]-α)*(1-v[n,i][t])-w[n,i][t]-w0)/ϵ)
+K*(v[n,i-1][t]-v[n,i][t])
+K*(v[n-1,i][t]-v[n,i][t])
+K*(v[n,i+1][t]-v[n,i][t])
+K*(v[1,i][t]-v[n,i][t]),{i,2,m-1}],

			{v[n,m]'[t]==((A*v[n,m][t]*(v[n,m][t]-α)*(1-v[n,m][t])-w[n,m][t]-w0)/ϵ)
+K*(v[n,m-1][t]-v[n,m][t])
+K*(v[n-1,m][t]-v[n,m][t])
+K*(v[n,1][t]-v[n,m][t])
+K*(v[1,m][t]-v[n,m][t])},

Flatten[Table[v[i,j]'[t]== ((A*v[i,j][t]*(v[i,j][t]-α)*(1-v[i,j][t])-w[i,j][t]-w0)/ϵ)
+K*(v[i-1,j][t]-v[i,j][t])
+K*(v[i,j-1][t]-v[i,j][t])
+K*(v[i+1,j][t]-v[i,j][t])
+K*(v[i,j+1][t]-v[i,j][t]),{i,2,n-1},{j,2,m-1}]],

			Flatten[Table[w[i,j]'[t]==v[i,j][t]-γ*w[i,j][t]-v0,{i,1,n},{j,1,m}]],

			Flatten[Table[v[i,j][0]==Evaluate[initial[[1,1,2]]],{i,1,kickleftN},{j,1,m}]],
			Flatten[Table[v[i,j][0]==Evaluate[initial[[1,1,2]]],{i,kickrightN,n},{j,1,m}]],
			Flatten[Table[v[kickcellN,j][0]==Evaluate[initial[[1,1,2]]],{j,1,kickleftM}]],
			Flatten[Table[v[kickcellN,j][0]==Evaluate[initial[[1,1,2]]],{j,kickrightM,m}]],
			{v[kickcellN,kickcellM][0]==kick+Evaluate[initial[[1,1,2]]]},
			Flatten[Table[w[i,j][0]==Evaluate[initial[[1,2,2]]],{i,1,n},{j,1,m}]]];

fitzsoln = NDSolve[fitzeqns,

			Join[Flatten[Table[v[i,j],{i,1,n},{j,1,m}]],Flatten[Table[w[i,j],{i,1,n},{j,1,m}]]],

			{t,0,T}, MaxSteps->100000];

(* Calculating the number of excited cells in the lattice, defined by the trajectory of the FHN solution *)

clustersize[[JJ]]=Sum[Boole[0<Max[Table[w[i,j][t]/.fitzsoln[[1]],{t,0,T,stp}]]-localmax],{i,n},{j,m}]];

cluster=Table[clustersize[[JJ]],{JJ,1,1,1}];

(* Export the number of connected cells from each iteration *)

Export["Fully_Connected_Pert_"<>ToString[kick]<>"_Prob_"<>ToString[prob]<>".xls",cluster]]],JJ],prob],kick]