Wednesday 15 July 2020

Potts Model simulation code (Python)



import
numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import random
import copy

##################################################################################################
nx = 300
ny = 300
Temp=1

n_levels=3

fig = plt.figure()
data = np.zeros((nx, ny))
data=[[random.randint(0,n_levels-1) for i in range(nx)] for i in range(ny)]
data=np.array(data)
im = plt.imshow(data)
def init():
global data
im.set_data(data)



def animate(i):
global data
print(i)
data=next_state(data)
im.set_data(data)
return im

def next_state(data):
for i in range(5000):
n=random.randint(0, nx-1)
m=random.randint(0,ny-1)
next=random.randint(0,n_levels-1);
a=(data[m][(n-1+nx)%nx]==next)*2.0-1
b=(data[m][(n+1)%nx]==next)*2.0-1
c=(data[(m-1+ny)%ny][n]==next)*2.0-1
d=(data[(m+1)%ny][n]==next)*2.0-1

d1 = (data[(m + 1) % ny][(n+1)%nx] == next) * 2.0 - 1
d2 = (data[(m + 1) % ny][(n-1+nx)%nx] == next) * 2.0 - 1
d3 = (data[(m - 1+ny) % ny][(n+1)%nx] == next) * 2.0 - 1
d4 = (data[(m - 1+ny) % ny][(n-1+nx)%nx] == next) * 2.0 - 1

E=a+b+c+d+d1+d2+d3+d4

if E > 0:
data[m][n] = next;
else:
r=np.random.uniform();
if E <= 0 and r < np.exp(E) / Temp:
data[m][n] = random.randint(0,n_levels-1);

return data



anim = animation.FuncAnimation(fig, animate, init_func=init, frames=800,interval=1)

plt.show()



Thursday 26 March 2020

Ising Model simulation Code

MATLAB code for the Ising model simulation is given in this post. Try Yourself.

You can access ready-made simulation of this in your phone with the following Android app https://play.google.com/store/apps/details?id=com.gaurtavm.randomprocesssimulation


MATLAB CODE:

a=200;  %height
b=200;  %width
T=1;    %Temprature
steps=10000000;
img=2*(randi(2,a,b)-1)-1;  % Random 2D array containing +1 and -1
for i=1:steps
    x=randi(a,1);
    y=randi(b,1);
    tmp=(img(mod(x-1-1,a)+1,y)+img(mod(x+1-1,a)+1,y)+img(x,mod(y+1-1,b)+1)+img(x,mod(y-1-1,b)+1));
    E=-img(x,y)*tmp;   %Energy
    r=rand;
    if E>0
        img(x,y)=-img(x,y);
    elseif E<=0 && r<exp(E)/T
        img(x,y)=-img(x,y);
    end
   
    if mod(i,10000)==0   %update image after 10000 steps
        imshow(((img+1.0)/2))
        fprintf("%d\n",i)
    end
end


OUTPUT:


Monday 16 March 2020

My Android Apps on google play store

In this post, ill tell you about android apps I published and their use

Photo Noise Editor:  With help of this app you can add various kinds of noise to your photos. It is different from other apps in a way that you can pause the noise addition process at any time and save your photos. A small demo of app is given in the GIF image below.
App Link:  https://play.google.com/store/apps/details?id=com.gaurtavm.photonoiseadder




Virtual Line follower: This app lets you create a line follower inside your phone. This Line follower can have as many sensors as you want in a straight-line fashion.
The main purpose of this app is to make complex line followers easily. For example, it is a very hard task to make a PID line follower and tune Kp, Ki and Kd values but this application makes it easy. You can try those things in this app that are out of your budget for a practical line follower.
You can draw a path of your choice for the bot. That is a very time consuming and expensive task in practical line follower.
Blog page: https://crackeconcept.blogspot.com/2020/03/virtual-line-followerline-foll.html
 
The app is available on google play store. Link for the app is: https://play.google.com/store/apps/details?id=com.gaurtavm.virtuallinefollower








Probability Simulations: This app contains a number of coded simulations that are mostly related to probability theory. The animation helps to understand the process well. There are adjustable parameters for each simulation you can play with the values.  My motivation to make this app is to let the user visualize these processes easily through these simulations.
Currently Available simulations in the app are
- Discrete-time Markov Chain (DTMC)
- Ising Model 2D
- Random Walk 1D
- Random Walk 2D
- Diffusion-limited aggregation (DLA)
Link: https://play.google.com/store/apps/details?id=com.gaurtavm.randomprocesssimulation

Flexible Whiteboard: This app lets you draw on a small area on screen and view drawing on full screen. It is very useful for tablets where screen size is big. You don't have to take your hands everywhere to draw, just draw on a small area which is also adjustable.
Features:
- Zoom-In and Zoon-Out
- Instant sharing of drawing
- Adjustable drawing area
- ARGB brush control
- Brush width control
App Link: https://play.google.com/store/apps/details?id=com.gaurtavm.flexiblewhiteboard


Sunday 15 March 2020

Virtual PID Line Follower

When you are making a PID line follower there are a lot of problems. How many sensors to use? What should be coefficients? What should be the distance between sensors for your problem? One cannot afford to buy so many sensors and changing distance between sensors is a very time-consuming process.
For best performance, you have to tune Kp, Ki, and Kd. Tuning these parameters of a PID line follower is a very hard task. You have to keep changing value and burn the code again and again to the microcontroller. With the help of this app, you can learn easily how to tune these values.


PID Algorithm:

Initialize I=0,error=0,last_error=0
1. Read all sensors output s0,s1,s3...,sn which can be either 0 or 1. 


2. Calculate current Position using.
 if (s0+s1+s2+...+sn>0) then current_position=(s0*b0+s1*b1+s2*b2+..+sn*bn)/(s0+s1+s2+...+sn)

else current_position=s0*b0+ s1*b1+ s2*b2+...+ sn*bn
Where b0, b1, b2,..., bn are sensor coefficients. These sensor cofficients are decided by us. For example, for 5 sensors cofficients can be 
b0=10, b1=20,b2=30,b3=40,b4=50 ,For these cofficients our set point shoud be 30 which is the middle sensor cofficient value.

3. Find error using,  
error = set_position- current_position
set_position contains the value where the error should be zero.

4. Set I=I+error

5. pid_error = kp*error + ki*I + kd*(error-last_error)

6. Remember current error in last_error variable. last_error = error
7. Set motor speeds:  
if (pid_error<0) then left_motor=max_speed, right_motor=max_speed+pid_error

else left_motor=max_speed-pid_error, right_motor=max_speed

8. Limit output speed. if(left_motor>max_speed) then left_motor= max_speed
if(left_motor< -max_speed) then left_motor= -max_speed 
if(right_motor> max_speed) then right_motor= max_speed 
if(right_motor< -max_speed) then right_motor= -max_speed