9.8 Exercise 8

A theoretical introduction for Monte-Carlo-simulation is given in section 8.1. Here we will discuss examples for diffusion problems which are numerically solved by implementing ”random walk”. Our final goal is to simulate the diffusion out of a membrane for which we already did the fitting to experimental data in exercise 9.5.
As a starting point we simulate the diffusion of a limited point source. Most of the program is related to drawing results and will not be discussed here in detail. The essential simulation part is remarkably short:

 function random_walk_point_source
     particles_start = 10000;
     particles = particles_start;
     steps = 2000;
     D = 1./3;
     thickness = 100;
     ...
     position = zeros(particles, 2);
     ...
     for i=2:steps
       if (mod(i,2)==0)
           i_old=1;
           i_new=2;
       else
           i_old=2;
           i_new=1;
       end
       randomsteps = randi([0 2], particles,1)-1;
       position(1:particles,i_new) = position(1:particles,i_old) + randomsteps;
       i2=1;
       while (i2{<}= particles)
        if (position(i2,i_new) == (thickness/2)+1)
               position(i2,i_new) = position(particles,i_new);
               particles= particles-1;
        elseif (position(i2,i_new) == -(thickness/2)-1)
               position(i2,i_new) = position(particles,i_new);
               particles= particles-1;
        else
               i2=i2+1;           
        end
       end
       part_inside(i) = particles;
       ...
     end
 end

The simulation will be performed for 10000 particles at start and for 2000 steps within a sample with thickness = 100 (reaching from -thickness./2 to thickness./2). The position(1:particles,i_old) of each particle is stored in an array and the new positions calculated in each loop step is stored in a second array position(1:particles,i_new). The indexes i_old and i_new are alternating their values in each loop. allowing for an continuous updating of the values without copying the values from one array to another.
The whole simulation is performed in basically three lines:

So first load down the complete example ”random_walk_point_source.m” from the running term page. Run the program and discuss several items:

Essentially the last job just needs a change in the initialization of the positions of the particles. They have to be homogeneously distributed within the limits of the sample:

     .....
     position = zeros(particles, 2);
     position(1:particles,1)=round((thickness+1).*(rand(particles,1)-0.5));
     ...

Your next jobs:

     ...
     part_inside_theo = particles_start.*(1-non_scaled_fu((0:(steps-1)),D./thickness.^2));
     ...

     function y = non_scaled_fu(t,Ddlq)
       h1 = 16.*Ddlq./pi;
       index_new = round(0.36/h1);
       y= (h1.*t).^0.5;
       y(index_new:end) = 1 - (8./(pi.^2)) .* exp (-pi.^2 .* Ddlq .* t(index_new:end));
     end

Your next jobs:

     ...
     barrier_probability= exp(-0.03./0.025);
     ...
       i2=1;
       while (i2{<}= particles)
        if (position(i2,i_new) == (thickness/2)+1)
           if (rand(1,1){<}barrier_probability)
                  position(i2,i_new) = position(particles,i_new);
                  particles= particles-1;
               else
                  position(i2,i_new) = (thickness/2);
                  i2=i2+1;                 
               end
        elseif (position(i2,i_new) == -(thickness/2)-1)
           if (rand(1,1){<}barrier_probability)
                  position(i2,i_new) = position(particles,i_new);
                  particles= particles-1;
               else
                  position(i2,i_new) = -(thickness/2); 
                  i2=i2+1;            
               end
        else
                  i2=i2+1;           
        end
       end
      end

As often in Monte-Carlo simulation the effort for implementing such new boundary conditions is minimal. For solving the differential equation analytically you would have to start from the scratch to implement this changes and the solutions would look completely differently.


With frame Back Forward as PDF

© J. Carstensen (Comp. Math.)