chemistry in motion || appendix b: matlab code for the minotaur (example 4.1)

4
Appendix B: Matlab Code for the Minotaur (Example 4.1) clc; tic; X = []; X = imread (’maze2.bmp’, ’bmp’); %Read the initial Maze from %a bitmap file. %The outer edge of the %bitmap must consist %of only sources, sinks, or %walls. map = [1,1,1;0,0,0;0,1,0;1,0,0]; %Set the color map for i = 5:256 map(i,:) = [1,1-(i-5)/251,1-(i-5)/251]; %These values produce %shades of red end %for the diffusing aroma. C = zeros(size(X,1),size(X,2), 2); %Set the maze to be %initially empty tmax = 100000; alpha = 0.2; sink = 0; source = 1; %Define the concentrations %and a for i = 1:size(X,1) %Parse the input maze, %changing the for j = 1:size(X,2) %colors as necessary. Note %that these Chemistry in Motion: Reaction–Diffusion Systems for Micro- and Nanotechnology Bartosz A. Grzybowski © 2009 John Wiley & Sons Ltd. ISBN: 978-0-470-03043-1

Upload: bartosz-a

Post on 06-Jun-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Appendix B: Matlab Code for

the Minotaur (Example 4.1)

clc; tic; X = []; X = imread (’maze2.bmp’, ’bmp’);

%Read the initial Maze from

%a bitmap file.

%The outer edge of the

%bitmap must consist

%of only sources, sinks, or

%walls.

map = [1,1,1;0,0,0;0,1,0;1,0,0];

%Set the color map

for i = 5:256

map(i,:) = [1,1-(i-5)/251,1-(i-5)/251];

%These values produce

%shades of red

end %for the diffusing aroma.

C = zeros(size(X,1),size(X,2), 2);

%Set the maze to be

%initially empty

tmax = 100000; alpha = 0.2; sink = 0; source = 1;

%Define the concentrations

%and afor i = 1:size(X,1) %Parse the input maze,

%changing the

for j = 1:size(X,2) %colors as necessary. Note

%that these

Chemistry in Motion: Reaction–Diffusion Systems for Micro- and Nanotechnology Bartosz A. Grzybowski

© 2009 John Wiley & Sons Ltd. ISBN: 978-0-470-03043-1

if X(i,j) == 255 %Diffusion space

%correspond to a 256 color

%bitmap.

X(i,j) = 0;

elseif X(i,j) == 0 %Wall

%Also, reset the colors to

%the above color map.

X(i,j) = 1; C(i,j,:) = 2;

elseif X(i,j) == 250 %Source (Lair)

%Colors are from the standard

%pallet in

X(i,j) = 2; C(i,j,:) = source;

%Microsoft Paint, with the

%wallscolored

elseif X(i,j) == 249 %Sink (Exit)

%black, the diffusion areas

%white, the source

X(i,j) = 3; C(i,j,:)= sink;

%area(s) green, and the sink

%areas red.

end

end

end

imwrite(X,map,int2str(0),’bmp’)

%Save the initialstate as ’0’

for t = 1:tmax

if mod(t,100) == 0 %Output the concentrations

[toc, t/100] %every 100 iterations

for i = 1:size(X,1)

for j = 1:size(X,2)

if X(i,j) == 0 %Translate actual

%concentrations

O(i,j) = floor(C(i,j,2)*251+5);

%into the color map used

%above

else

O(i,j) = X(i,j)+1;

end

end

end

imwrite(O,map,int2str(t),’bmp’)

%Write the file, with the name

end %corresponding to the time

%step

272 APPENDIX B

C(:,:,1)=C(:,:,2); %Reset the concentration

%matrix

for i = 1:size(X,1)

for j = 1:size(X,2)

if X(i,j) == 0 %Perform diffusion only at

%nodes

switch (X(i+1,j)*10)+(X(i-1,j))

%that allow diffusion, also

%read:

case {0, 2, 3, 20, 22, 23,30, 32, 33}

%Treatment of individual

%nodes.

C(i,j,2) = C(i,j,1)+alpha*(C(i-1,j,1)+C(i+1,j,1)-

2*C(i,j,1));

case {1,21,31}

C(i,j,2) = C(i,j,1)+alpha*(C(i+1,j,1)+C(i+1,j,1)-

2*C(i,j,1));

case {10,12,13}

C(i,j,2) = C(i,j,1)+alpha*(C(i-1,j,1)+C(i-1,j,1)-

2*C(i,j,1));

case 11

C(i,j,2) = C(i,j,1);

end

switch X(i,j+1)*10+(X(i,j-1))

%Abbreviations defined below

case {0, 2, 3, 20, 22, 23, 30, 32, 33}

%DD

C(i,j,2) = C(i,j,2)+alpha*(C(i,j-1,1)+C(i,j+1,1)-

2*C(i,j,1));

case {1,21,31} %DW

C(i,j,2) = C(i,j,2)+alpha*(C(i,j+1,1)+C(i,j+1,1)-

2*C(i,j,1));

case {10,12,13} %WD

C(i,j,2) = C(i,j,2)+alpha*(C(i,j-1,1)+C(i,j-1,1)-

2*C(i,j,1));

case 11 %WW

C(i,j,2) = C(i,j,2);

end

end

end

end

end

%{

APPENDIX B 273

Treatment of individual nodes. In a square lattice with four types of nodes

(diffusion, source, sink or wall), each node has four independent nearest neighbors

(NNs). Each of the four NNs can have any one of those four values, resulting in a

maximumof 256 unique combinations of NNs in a given calculation.Writing code

to address each of these unique combinations is both tedious and error-prone due to

there being many copies of very similar code. However, the 256 combinations of

NNs can be divided into two sets of four mathematically unique cases by:

(i) separating the diffusion operation into two discrete steps and (ii) grouping

mathematically identical nodes. In (i), by separating the diffusion operation into

two discrete steps, one along the i axis and one along the j axis, the fourNNs of each

node are treated as two independent sets of two NNs. This reduces the 256 unique

combinations of four NNs to two sets of 16 unique pairs of NNs. In (ii), the forward

time centered space (FTCS) method for constant concentration NNs (either

sources or sinks) is mathematically identical to the method for NNs that allow

diffusion, leaving only two types of NN – diffusing (D) and wall (W). When only

twomathematically different NNs exist, the number of pairs of NNs on each axis is

reduced from 16 to 4. The four pairs of NNs correspond to: both NNs allow

diffusion (DD), the left or upper neighbor allows diffusion and the right or lower

neighbor is a wall (DW), the left or upper neighbor is a wall and the right or lower

neighbor allows diffusion (WD) and both NNs are walls (WW). The concentration

change at a particular node is calculated by encoding the type of NN on each axis,

then applying the appropriate concentration changes (DD, DW, WD or WW) for

each axis.

The code can be made significantly more efficient by writing the diffusion

algorithm twice – using an explicit step from C(:,:,1) to C(:,:,2), followed by an

explicit step from C(:,:,2) to C(:,:,1) – instead of copying the entire matrix during

each iteration.

274 APPENDIX B