#  immis
#  Copyright (C) 1995  Juergen Dollinger
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 1, or (at your option)
#  any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with CELLULAR; see the file COPYING.  If not, write to the
#  Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# Simulation of two immiscible fluids with different viscosity.
# 2 different timesteps
# 1.) particles travel
# 2.) collisions and recoloring
# If there is only one kind of particle symmetric 2-, 3-, and
# 4-particle collisions and asymmetric 3-particle collisions will be done.
# But collisions of particles of type 1 will happen with a lower 
# probabiliy to increase viscosity.
#
# The hexagonal neighborhood is made of alternating parts
# of the Moore Neighborhood
#               _ _
#             _|_|_|
# which =1 : |_|Z|_|
#              |_|_|
#               _ _
#              |_|_|_
# which =0 :   |_|Z|_|
#              |_|_|
#
#                              / \
#                           nw/   \ ne
#                            /     \
#                         w |       | e
#                           |       |
#                            \     /
#                          sw \   / se
#                              \ /

2 dimensions of
	# color of display
	color of 0..3
	# make a neighborhood
	which of 0..1
	# If the cell is a wall, it reflects particles!
	#
	wall of 0..1
	# The presence (non-zero) of a particle "traveling" in the given
	# direction. The non-zero value indicates the kind of particle.
	#
	particle[] for e, ne, nw, w, sw, se of 0..2
	random of 0..1
end

# Hexagonal neighborhood.
#
neighbor[] for e, ne, nw, w, sw, se
           := [1, 0], [1, 1], [0, 1], [-1, 0], [0, -1], [1, -1] when cell.which
           := [1, 0], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1] otherwise
if time%2=0 then
	forall i
		# Particles travel through undisturbed.
		cell.particle[i] := neighbor[i+%3].particle[i]
	end
else # time%2 != 0
	# Find out how many particles of each kind are in this cell.
	one := 0
	two := 0
	forall i
		one := one + 1 when cell.particle[i] = 1
		two := two + 1 when cell.particle[i] = 2
	end
	cell.color := 3 when cell.wall
	           := 0 when one=0 & two=0
	           := 1 when one > two
	           := 2 when two > one
	           := cell.color otherwise

	# collisions
	if cell.wall then
		#
		# reflect back to incoming direction.
		#
		forall i
			cell.particle[i] := cell.particle[i+%3]
		end
	elsif one = 0 | two = 0 then
		#
		# Only particles of one type
		#
		if one = 0 then 
			particle := 2
			randomnumber := 1 # always do a collision if possible.
		else 
			particle := 1
			randomnumber := [0,1].random + [1,0].random
			# Reduce probability of collision (if particle = 1) to 25%
		end
		if randomnumber = 1 then
			if one+two = 2 then #collision?
				stoss := 0 # initialize: no collision
				forall i
					stoss := stoss |
					         (cell.particle[i+%3] & cell.particle[i])
					# Only head on head collisions
				end
				forall i
					cell.particle[i] := cell.particle[i-%1] # counterclockwise
					                    when stoss & cell.random
					                 := cell.particle[i+%1] # clockwise
					                    when stoss & !cell.random
				end
			elsif one+two = 3 then
				stoss := 0 # initialize: no collision
				momencoll := 0 # initialize: no collision with nonzero momentum
				forall i
					momencoll := momencoll |                          #  __
					         (cell.particle[i+%3] & cell.particle[i]) #   \
					stoss := stoss |
								( cell.particle[i]     #
								& cell.particle[i+%2]  # \ /
								& cell.particle[i+%4]) #  |
				end
				if stoss then
					forall i
						cell.particle[i] := cell.particle[i+%1] #rotate 3 particles
					end
				elsif momencoll then
					forall i
						if cell.particle[i] & !cell.particle[i+%3] then
							if cell.particle[i+%1] then
								cell.particle[i+%1]:=0
								cell.particle[i+%2]:=particle  # \_
								cell.particle[i-%1]:=particle  #  \
								cell.particle[i-%2]:=0
							else
								cell.particle[i+%1]:=particle  #  i+%1
								cell.particle[i+%2]:=0         #  /_i
								cell.particle[i-%1]:=0         # /
								cell.particle[i-%2]:=particle  #i-%2
							end
						end
					end
				end
			elsif one+two = 4 then #4-particle collision?
				stoss := 0 # initialize: no collision
				forall i
					stoss := stoss +
					        ( cell.particle[i+%3] & cell.particle[i] )
				end
				forall i
					cell.particle[i] := cell.particle[i+%1] when stoss=4 & !cell.random
					                 := cell.particle[i-%1] when stoss=4 & cell.random
				end
			#else  Particles travel through undisturbed.
				#do nothing (already traveled)
			end
		end #if !randomnumber
	else
		#
		# two different types of particles
		#
		# Call
		# 2*Cos((i-k) * pi/3)
		# as cosik[i-%k]
		cosik[] for 6 := 2, 1, -1, -2, -1, 1

		if one + two = 2 then #2-particle collision?
			stoss := 0 # initialize: no collision
			forall i
				stoss := stoss |
				      ( cell.particle[i+%3] & cell.particle[i])
			end
		elsif one + two = 3 then #3-particle collision?
			stoss := 0 # initialize: no collision
			forall i
				stoss := stoss |
				         ( cell.particle[i]
				         &  cell.particle[i+%2]
				         &  cell.particle[i+%4])
				#      \ /           __
				# Only  |   but not   \
				#
			end
		elsif one + two = 4 then #4-particle collision?
			vstoss := 0 # initialize: no collision
			forall i
				vstoss := vstoss +
				          ( cell.particle[i+%3] & cell.particle[i] )
			end
			stoss := 1 when vstoss = 4
			      := 0 otherwise
		else # one + two >4 
			stoss := 0
		end #if one + two =

		if stoss then
			#
			# compute work := color flux * colorfield
			#
			minwork := -72 
			forall l:0..5 # rotate by l steps
				work := 0
				forall i:0..5 # all neighbours
					neigborcolor := 0
					forall k
						neigborcolor := neigborcolor+1 when neighbor[i].particle[k] = 1
						             := neigborcolor-1 when neighbor[i].particle[k] = 2
					end
					forall k:0..5
						localcolor := 1 when  cell.particle[l+%k] = 1
						           := -1 when  cell.particle[l+%k] = 2
						           := 0 otherwise
						work := work + cosik[i-%k] * neigborcolor * localcolor
					end # forall k
				end # forall i

				#Because of the missing minus minwork is maximum 
				if work > minwork | (work = minwork & neighbor[l].random) then
					minwork := work
					direction := l
				end
			end # forall l
			forall i
				cell.particle[i] := cell.particle[i] when direction = 0
				                 := cell.particle[i+%1] when direction = 1
				                 := cell.particle[i+%2] when direction = 2
				                 := cell.particle[i+%3] when direction = 3
				                 := cell.particle[i+%4] when direction = 4
				                 := cell.particle[i+%5] when direction = 5
			end
		else # recoloring: all particles stay at their positions
		     # but change their colour. The number of particles of
		     # each colour will stay constant.
			test[] for 6 := 0,0,0,0,0,0
			minwork := -72
			forall l:0..5 #begin with particles of type 1 at direction l
				work := 0
				if cell.particle[l] then
					tone := one
					forall k:0..5
						if cell.particle[l+%k] then
							if tone > 0 then
								test[l+%k] := 1
								tone := tone - 1
							else
								test[l+%k] := 2
							end
						end
					end # forall k
					forall i:0..5 # sum over all neigbors
						neigborcolor := 0
						forall k
							neigborcolor := neigborcolor+1 when neighbor[i].particle[k] = 1
							             := neigborcolor-1 when neighbor[i].particle[k] = 2
						end
						forall k
							localcolor := 1 when  test[k] = 1
							           := -1 when  test[k] = 2
							           := 0 otherwise
							work := work + cosik[i-%k] * neigborcolor * localcolor
						end #forall k
					end # forall i
					#Because of the missing minus minwork is maximum 
					if work > minwork | (work = minwork & neighbor[l].random) then
						minwork := work
						forall j
							cell.particle[j] := test[j]
						end
					end
				end # if cell.particle[l]
			end # forall l
		end # if stoss
	end # if cell.wall ... elsif one=0 | two=0 ...
	cell.random := (cell.random +[1,0].random + [-1,0].random 
	                +[0,1].random +[0,-1].random)%2
end # if time%2

