Comment: J.E. Bunder and A. J. Roberts, December 2013. 
Derive the holistic model for the linear-diffusion equation with tk diffusivity values k(i). The diffusivities can be taken to be approximately equal, if desired, k(i)=>ka+eta*nu(i) for small eta.
Microscale field solutions in the jth patch uj are evaluated for one diffusivity configuration using difference equations for i=-(n-1),...0,...,(n-1) and coupling conditions for i=-n,...,(-n+2s) and i=n,...(n-2s), where n is the patch half-width and s is the core half-width. This single configuration is mapped to all configurations in the ensemble and, using the amplitude condition (the average over 2s+1 solutions in the core of the patch), the evolution g is evaluated. There is the option of only using one configuration in the ensemble. 
Solutions of g and uj are given in terms of macroscale fields uu(j) and microscale mean and difference operators mu and delta;

on div; off allfac; on revpri; on rat; factor b, gam;

kmax:=2; % maximum order, correct to delta^(2*kmax)  
imax:=9$ % maximum number of iterations
tk:=2; % period of diffusion
simplify:=0; % 1 for approx. equal diffusivities, otherwise independent
ens:=2; % zero: if only use one configuration- useful for tk|(n-s) cases
	% one: if only use two configurations- reflection symmetry
	% else: if use 2*tk configurations- reflection and translation
% ens=0,1 generally not ideal since give mu*del terms in solution
nmin:=7; % minimum patch half-width n
nmax:=7; % maximum patch half-width n
smin:=3; % minimum core half-width s, maximum is (n-1)
sstep:=nmin; % core steps
eord:=3$ % order at which to cutoff eta, the variation in diffusivities

operator u, uu, b, kk, c; 
operator bc1, bc2, bcl, bcr, sums, a1;

clear k;
clear eta;
operator k, ktemp;
if (simplify=1) then
<<	%make variations between diffusivities small
	operator nu;
	let {k(~~j)=>ka+eta*nu(j)};
	for p:=eord:eord do let {eta^p=>0};
	let {eta*c(~i,~j)=>0}>>;

% evaluate infinite domain evolution g00, up to order del^4
% del is microscale difference operator
kh:=tk/(for i:=1:tk sum 1/k(i))$
kg:=(for i:=1:tk product k(i))$
c2:=kg/2*(for i:=1:tk sum (for j:=1:(tk-1) sum (j*(tk-j)/(k(i)*k(mod(i+j-1,tk)+1))) ))$
g00:=kh*uu(j)/h^2*(del^2+((tk^2-1)/12-c2*kh^2/(tk^2*kg))*del^4)$
if simplify then %need taylor series of g00
	g00:=taylor(g00,eta,0,eord-1)$
g00:=(sub({eta=1},g00))$

% b=basis vectors over patch
let {df(b(~i,~w)*(~a),~y)=>b(i,~w)*df(a,y)};
let {b(~i,~w)^2=>b(i,~w), b(~i,~w)*b(~l,~w2)=>0};
% w is 1, 2... or tk for different configurations

% time derivative
let df(uu(~k),t)=>sub(j=k,g);

% coupling conditions to kmax order in terms of macroscale fields uu(j)
% and macroscale mean and difference operators muH, delH
bconr:=((uu(j)+for m:=1:kmax sum (eps*gam)^m*((for i:=0:(m-1) product (r^2-i^2))*(+muH*delH^(2*m-1)/r+delH^(2*m)/(2*m))*uu(j)/factorial(2*m-1) where {delH^2*uu(~j)=>uu(j+1)-2*uu(j)+uu(j-1), muH*delH*uu(~j)=>(uu(j+1)-uu(j-1))/2})))$
bconl:=((uu(j)+for m:=1:kmax sum (eps*gam)^m*((for i:=0:(m-1) product (r^2-i^2))*(-muH*delH^(2*m-1)/r+delH^(2*m)/(2*m))*uu(j)/factorial(2*m-1)  
where {delH^2*uu(~j)=>uu(j+1)-2*uu(j)+uu(j-1), muH*delH*uu(~j)=>(uu(j+1)-uu(j-1))/2})))$
% truncate asymptotics
for m:=(kmax+1):(kmax+1) do let eps^m=>0;

% shift operators for b(i,w)
operator sup, sdown;
linear sup, sdown;
let {sup(b(~i,~w),b)=>b(i-1,~w), sdown(b(~i,~w),b)=>b(i+1,~w)}; 

out gpatch;
if (ens=0) then ees:=1 else if (ens=1) then ees:=2 else ees:=2*tk$
write "Patch dynamics solution for evolution g ";
write "for different patch half-width n and core half-width s";
write "Order delta^(2*kmax), kmax = ",kmax;
write "Diffusion period tk = ",tk;
write "Number of configurations in ensemble: ", ees;
write "Infinite domain evolution to order del^4:";
write g00:=g00;

for n:=nmin:nmax do begin

for s:=smin step sstep until (n-1) do begin  
% if only want tk|(n-s) cases replace above line with:
%for s:=mod(n,tk) step tk until (n-1) do begin

core:=2*s+1; %size of core

% k=diffusion
clear kk;
operator kk;
linear kk;
let {kk(b(~i,1),b,~p)=>k(mod(i+n+p+tk,tk)+1)*b(i,1)};

% c=corrections to u
v:=(for i:=-n:n sum ( c(i,1)*b(i,1) ))$ 
cs:=(for i:=-n:n collect c(i,1) )$ 
% cs are polynomial coefficients

% initial solutions for evolution g and microscale field in jth patch uj
ones:=(for i:=-n:n sum ( b(i,1) ))$
uj:=uu(j)*ones$
g=g0;

out t;
% iterate to improve
for iter:=1:imax do begin
   	ujp:=sup(uj,b)-uj;
   	ujd:=sdown(uj,b)-uj;
   	de:=df(uj,t)*h^2-(kk(ujp,b,1)+kk(ujd,b,0));
   	de:=(de where b(~i,~w)=>0 when abs(i) geq n);
	bc1:=( for m:=-n:-(n-core+1) sum coeffn(uj,b(m,1),1))/core-bconl;
	bc2:=( for m:=(n-core+1):n sum coeffn(uj,b(m,1),1))/core-bconr;
	
        ujp:=sup(v,b)-v;
    	ujd:=sdown(v,b)-v;
	deq:=gd*ones*h^2-(kk(ujp,b,1)+kk(ujd,b,0));	
    	deq:=de+(deq where b(~i,~w)=>0 when abs(i) geq n);
	bcl:=bc1+(for m:=-n:-(n-core+1) sum c(m,1))/core;
	bcr:=bc2+(for m:=(n-core+1):n sum c(m,1))/core;	
	deq2:=(for i:=-n+1:n-1 collect coeffn(deq,b(i,1),1) );
	ans:=solve(append(deq2,{bcl,bcr}),cs);
	uj:=uj+sub(ans,v);
	if (ens=0) then
		amp:=(for m:=-s:s sum (sub(ans,c(m,1))))	
	else if (ens=1) then <<
		if (simplify=0) then <<
			(for l:=1:2 do a1(l):=((for m:=-s:s sum (sub(ans,c(m,1)) where {k(~j)=>ktemp(l,j)} ))));
			amp:=(for l:=1:2 sum a1(l) where {ktemp(1,~~j)=>k(mod(j-1,tk)+1), ktemp(2,~~j)=>k(mod(mod(2*n-1,tk)-j+1,tk)+1)}) 
		>>
		else <<
			(for l:=1:2 do a1(l):=((for m:=-s:s sum (sub(ans,c(m,1)) where {nu(~j)=>ktemp(l,j)} ))));
			amp:=(for l:=1:2 sum a1(l) where {ktemp(1,~~j)=>nu(mod(j-1,tk)+1), ktemp(2,~~j)=>nu(mod(mod(2*n-1,tk)-j+1,tk)+1)}) 
		>>	
	>>
	else <<
		if (simplify=0) then <<
			(for l:=1:2*tk do a1(l):=((for m:=-s:s sum (sub(ans,c(m,1)) where {k(~j)=>ktemp(l,j)} ))));
			amp:=(for l:=1:2*tk sum a1(l) where {ktemp(~~l,~~j)=>k(mod(l+j-2,tk)+1) when l<(tk+1), ktemp(~~l,~~j)=>k(mod(l-j,tk)+1) when l>tk}) 
		>>
		else <<
			(for l:=1:2*tk do a1(l):=((for m:=-s:s sum (sub(ans,c(m,1)) where {nu(~j)=>ktemp(l,j)} ))));
			amp:=(for l:=1:2*tk sum a1(l) where {ktemp(~~l,~~j)=>nu(mod(l+j-2,tk)+1) when l<(tk+1), ktemp(~~l,~~j)=>nu(mod(l-j,tk)+1) when l>tk})
		>>	
	>>; %if ens=0
	ans2:=solve(amp,gd);
	g:=g+sub(ans2,gd);
	uj:=sub(ans2,uj);
	if sub(ans2,sub(ans,de.{bc1,bc2}))={0,0,0} then 
		write iter:=1000000+iter;
showtime;
end;%for

write "iteration for patch, core = ",n,", ",s," complete";
out gpatch;
eps:=1$

g1:=(g where {
	muH^2=>1+delH^2/4, h=>2*r*hh/(2*n-core+1),
	uu(j+~p)=>(1+muH*delH+delH^2/2)^p*uu(j) when p>0,
	uu(j+~p)=>(1-muH*delH+delH^2/2)^(-p)*uu(j) when p<0});

% convert patch dynamics evolution from macroscale to microscale operators
let {sums(~sgn)=>(for j:=1:(20) sum ((for k:=0:(j-1) product ((n-s)/r-k))*(sgn*mu*del+del^2/2)^j/factorial(j)))};
let {mu^2=>1+del^2/4};
for j:=kmax:kmax do (let {del^(2*j+3)=>0});
write "n = ",n,", s = ",s; 
ggam:=((g1 where {gam^(kmax+1)=>0, del^(2*kmax+1)=>0, hh=>h*(2*n-core+1)/(2*r), delH^2=>(sums(1)+sums(-1))}) where {muH*delH=>(sums(1)-sums(-1))/2})$

factor del, mu;
%g:=(ggam where {eta=>1, gam=>1});
% compare patch dynamics evolution g to infinite domain evolution g00
for m:=(2*kmax+1):(2*kmax+1) do g:=(ggam where {eta=>1, gam=>1, del^m=>0});
if g=g00 then write "g=g00, up to given accuracy of g00" else write g:=g;

clear eps;
clear eta; 
end; %s
end; %n

shut gpatch;

end;%execution

