George
George

Reputation: 5681

scipy - construction of lattice which traps molecules in 2d dimension

this is a continuation of a previous stackoverflow question here

I want to do it in 2 dimensions. I tried the following:

for i in range(pos):
    steps=0   #the steps that the particle does until it falls in  a trap
    in_pos = sc.random.randint(0, len(grid), 2)
    initial_trap=False
    while initial_trap==False:
        #the step of the particle can be one of this
        step=sc.array(random.choice([[0, 1], [1, 0], [0, -1], [-1, 0]]))          
        # Check position for edges and fix if required

        if in_pos + step > sc.size(grid) - 1:
            in_pos = 0
        elif in_pos + step < 0:
            in_pos = sc.size(grid) - 1
        else:
            in_pos += step


        # Check if it's a trap in order to stop the loop

        if grid[in_pos] == 0:
            initial_trap = True

        # If it isn't a trap, continue
        steps+=1
    steps_count.append(steps)
return steps_count

I also tried :

in_pos_x =int(sc.random.randint(0, len(grid), 1)) #initial position of particle in x axis
in_pos_y =int(sc.random.randint(0, len(grid), 1)) #initial position of particle in y axis

if  in_pos_x + step > len(grid)- 1 and in_pos_y + step > len(grid)-1:
    in_pos_x = 0
    in_pos_y = 0
elif in_pos_x + step < 0 and in_pos_y + step < 0:
    in_pos_x = len(grid)- 1
    in_pos_y = len(grid)- 1
else:    in_pos_x += step
    in_pos_y += step

if grid[in_pos_x][in_pos_y] == 0:
    initial_trap = True

And last,i tried to work with lists, not arrays.

in_pos = (scipy.random.randint(0,len(grid),2)).tolist()
step=random.choice([[0, 1], [1, 0], [0, -1], [-1, 0]])

but with no success either. I am lost here!

---------------Error messages-----------------------------

If run the program it gives me:

'The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()' at the line "if (in_pos + step) > sc.size(grid) - 1:"

If i use if sc.any(in_pos + step) > sc.size(grid) - 1: or if sc.any(grid[in_pos]) == 0: it runs but i used a print(grid[in_pos]) and I figured that it doesn't change values at all! It doesn't get the value '0', so the loop never ends.

Upvotes: 2

Views: 233

Answers (1)

Avaris
Avaris

Reputation: 36715

After the comments, I think I understand where you are going.

I guess you are using periodic boundaries, but the check you are doing as below is wrong:

# Check position for edges and fix if requireD

if in_pos + step > sc.size(grid) - 1:
    in_pos = 0
elif in_pos + step < 0:
    in_pos = sc.size(grid) - 1
else:
    in_pos += step

size returns the total number of elements in an array. You need to check individual lengths (in x and y directions) of array. Here, shape is your friend. And you need to correct the index that goes beyond the limits, not all of them. mod operator % can be of use. So the above code can be simply re-written as:

# Move by step
in_pos += step
# Correct according to periodic boundaries
in_pos = in_pos % grid.shape    # or simply in_pos %= grid.shape

Second problem is the indexing of grid. Suffice it to say, the way you are doing is not what you want (check the docs for details). For your simple case it can be re-written as:

if grid[in_pos[0], in_pos[1]] == 0:
    initial_trap = True

Upvotes: 3

Related Questions