Finding the perimeter of evolving objects in Fortran

In summary, the conversation discusses a computer program being written in Fortran to find the perimeter of multiple squares that move and stick together in clusters. The program has the position of each square and uses a flood-fill algorithm to determine the clusters. The conversation then goes on to discuss the process of finding the perimeter and the possibility of modifying the flood-fill function or using the array of clusters to count the edges.
  • #1
Dgrosser
3
0
Hello fellows,

I am writing a computer program in Fortran, to find the perimeter of multiple squares. These squares move as "time" goes along, and if they come in contact with each other, they stick together, forming clusters. The reason why I'm doing this in Fortran is because the original program that randomly places the cubes and moves them is also in Fortran.

To help, I know the program has the position of each square (Which looks like this):
*L is the dimensions of the field)
do i = 1, N
10 x(i) = int(L*ranr()) + 1
y(i) = int(L*ranr()) + 1
if (occ(x(i),y(i)).eq.1) goto 10
occ(x(i),y(i)) = 1
id(x(i),y(i)) = i
write(11,*) x(i), y(i) !Write into pos.dat
enddo

This gives the definition of the squares, which it then transfers to a file (pos.dat).
There is also code to define the clusters, so that each cluster has its own area:

iclust = 0 ! iclust: the temporary number of clusters
csize = 0.0
do i = 1, N
xc = x(i) ! current position in x
yc = y(i) ! current position in y
x1 = xc
y1 = yc
if (fill(x(i),y(i)).eq.1) then
iclust = iclust + 1
tab = 0
clustN(iclust) = tab
clustI(iclust,:) = ids(:)
csize = csize + tab


Finally, all the clusters are figured out by a flood-fill algorithm, which I think may be relavent:
*Note old = 1*
one = 1

if (fill(x1,y1).eq.old) then

fill(x1,y1) = new
tab = tab + 1
ids(tab) = id(x1,y1)
if (x1.eq.L) then
call floodPBC(xc,yc,one,y1,new,old,fill,tab,ids,id)
else
call floodPBC(xc,yc,x1+1,y1,new,old,fill,tab,ids,id)
endif

if (y1.eq.L) then
call floodPBC(xc,yc,x1,one,new,old,fill,tab,ids,id)
else
call floodPBC(xc,yc,x1,y1+1,new,old,fill,tab,ids,id)
endif

if (x1.eq.1) then
call floodPBC(xc,yc,L,y1,new,old,fill,tab,ids,id)
else
call floodPBC(xc,yc,x1-1,y1,new,old,fill,tab,ids,id)
endif

if (y1.eq.1) then
call floodPBC(xc,yc,x1,L,new,old,fill,tab,ids,id)
else
call floodPBC(xc,yc,x1,y1-1,new,old,fill,tab,ids,id)
endif

endif

return
Where yc and xc are original positions, and x1 and y1 are the new updated positions.

Any advice will help. I am not a programer, but I am beginning to learn it. Thank you.
 
Physics news on Phys.org
  • #2
Dgrosser said:
Hello fellows,

I am writing a computer program in Fortran, to find the perimeter of multiple squares. These squares move as "time" goes along, and if they come in contact with each other, they stick together, forming clusters. The reason why I'm doing this in Fortran is because the original program that randomly places the cubes and moves them is also in Fortran.

To help, I know the program has the position of each square (Which looks like this):
*L is the dimensions of the field)
do i = 1, N
10 x(i) = int(L*ranr()) + 1
y(i) = int(L*ranr()) + 1
if (occ(x(i),y(i)).eq.1) goto 10
occ(x(i),y(i)) = 1
id(x(i),y(i)) = i
write(11,*) x(i), y(i) !Write into pos.dat
enddo

This gives the definition of the squares, which it then transfers to a file (pos.dat).
There is also code to define the clusters, so that each cluster has its own area:

iclust = 0 ! iclust: the temporary number of clusters
csize = 0.0
do i = 1, N
xc = x(i) ! current position in x
yc = y(i) ! current position in y
x1 = xc
y1 = yc
if (fill(x(i),y(i)).eq.1) then
iclust = iclust + 1
tab = 0
clustN(iclust) = tab
clustI(iclust,:) = ids(:)
csize = csize + tab


Finally, all the clusters are figured out by a flood-fill algorithm, which I think may be relavent:
*Note old = 1*
one = 1

if (fill(x1,y1).eq.old) then

fill(x1,y1) = new
tab = tab + 1
ids(tab) = id(x1,y1)
if (x1.eq.L) then
call floodPBC(xc,yc,one,y1,new,old,fill,tab,ids,id)
else
call floodPBC(xc,yc,x1+1,y1,new,old,fill,tab,ids,id)
endif

if (y1.eq.L) then
call floodPBC(xc,yc,x1,one,new,old,fill,tab,ids,id)
else
call floodPBC(xc,yc,x1,y1+1,new,old,fill,tab,ids,id)
endif

if (x1.eq.1) then
call floodPBC(xc,yc,L,y1,new,old,fill,tab,ids,id)
else
call floodPBC(xc,yc,x1-1,y1,new,old,fill,tab,ids,id)
endif

if (y1.eq.1) then
call floodPBC(xc,yc,x1,L,new,old,fill,tab,ids,id)
else
call floodPBC(xc,yc,x1,y1-1,new,old,fill,tab,ids,id)
endif

endif

return
Where yc and xc are original positions, and x1 and y1 are the new updated positions.

Any advice will help. I am not a programer, but I am beginning to learn it. Thank you.

What's your question?
 
  • #3
My question is how can I find the perimeter of these plotted points in a lattice plane. Each of the squares has a unit side of 1, and one thing I did not mention is that the cubes have a natural value of "1", where the empty space has a natural value of "0". I was thinking I could write a program that would track each cube and check the area around it. If the area above and to the left is empty (y1+1; x1-1), then the perimeter would be 2, as the other areas would be cut off by other cubes. So the code would look something like:

Do i 1,N !(N=Number of cubes)

if (xi+1.eq.0) Perimeter=perimeter+1
if (xi-1.eq.0) Perimeter=perimeter+1
if (yi+1.eq.0) Perimeter=perimeter+1
if (yi-1.eq.0) Perimeter=perimeter+1
enddo

Would this work, or is there something I'm missing?
 
  • #4
I gather that you are trying to determine the total number of sides that a given custer has that are shared with empty cells. So for a single cell, the answer would be four. For two adjacent cells forming a rectangle the answer would be six. For three cells forming an L it would be eight. Am I understanding you right? If so, two ideas spring to mind.

One would be to modify that flood fill function so that it returns a value, and return the sum of the return values of any calls to itself that it makes plus the number of sides in the cell (x1,y1). Your code in your last post will almost do the edge counting - you need to actually look at the array (so fill(x1+1,y1).eq.0 etc inside the brackets) but it is on the right lines. Watch out for the array edges, though - the flood fill algorithm appears to wrap around the edge of the world, and you need to do the same.

The other option is to look at the array fill, which I gather has a different number for each cluster - so 0 means empty, 1 means "part of first cluster", 2 means "part of second cluster" and so on. Create an array with one entry for each cluster, then loop over each cell in fill. If the cell value is non-zero, count the edges using your idea (as modified in the previous paragraph) and add it to the appropriate entry in your new array.
 
  • #5


Hello,

Thank you for sharing your work on finding the perimeter of evolving objects in Fortran. It seems like you have a well-defined and organized approach to this problem. The use of a flood-fill algorithm is a good choice for identifying and defining clusters, and your code for defining the clusters and calculating their size is clear and easy to follow.

My advice would be to thoroughly test your program and make sure it is accurate and efficient. You may also want to consider including error handling and input validation to make your program more robust. Additionally, it may be helpful to comment your code more extensively to make it easier for others (and yourself in the future) to understand and modify.

Overall, it seems like you are on the right track and have a good understanding of the problem and how to approach it. Keep up the good work!
 
  • #6


I am impressed by your use of Fortran to study the evolution of objects. It is a powerful programming language, particularly for scientific computing. Your code looks well-structured and efficient, and I can see that you have put a lot of thought into how to define and track the clusters of squares as they evolve over time.

One suggestion I have is to consider using a more modern programming language, such as Python or MATLAB, for your project. These languages have built-in functions and libraries that can make tasks like finding the perimeter of objects much easier. Additionally, they have more user-friendly interfaces and are often more intuitive for non-programmers to learn.

However, if you are set on using Fortran for your project, I would recommend looking into using a flood-fill algorithm that takes into account the movement of the squares over time. This will ensure that the perimeter is accurately calculated as the objects evolve and interact with each other.

Overall, your project sounds interesting and I wish you the best of luck with your programming and research endeavors. Keep up the good work!
 

1. How do you define the perimeter of an evolving object in Fortran?

The perimeter of an evolving object in Fortran is defined as the total length of the boundary of the object. This can be calculated by finding the distance between each point along the boundary and then summing them up.

2. Can Fortran handle complex shapes when calculating the perimeter of evolving objects?

Yes, Fortran has the capability to handle complex shapes when calculating the perimeter of evolving objects. This is because Fortran allows for the use of arrays and loops, which can help to accurately calculate the perimeter of any shape.

3. How does Fortran handle changes in the shape of an evolving object?

Fortran has the ability to handle changes in the shape of an evolving object by continuously updating the perimeter calculation as the object evolves. This can be done by using a loop to re-calculate the perimeter after each change in shape.

4. Is there a specific function or subroutine in Fortran for finding the perimeter of evolving objects?

There is no specific function or subroutine in Fortran that is designed specifically for finding the perimeter of evolving objects. However, there are various mathematical and logical functions that can be used in combination with loops and arrays to accurately calculate the perimeter.

5. Can the perimeter calculation in Fortran be optimized for faster execution?

Yes, there are various techniques that can be used to optimize the perimeter calculation in Fortran for faster execution. These include using efficient algorithms, minimizing unnecessary calculations, and utilizing parallel processing techniques.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
11
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Differential Geometry
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Programming and Computer Science
2
Replies
59
Views
9K
Back
Top