[QUIZ] SimFrost (#117)

R

Ruby Quiz

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion. Please reply to the original quiz message,
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

This was one of the Perl Quiz of the Week problems a couple of years ago. It's
also my favorite computer simulation.

The goal is to create a simulation of frost. The end result should look
something like this (13MB Quicktime Movie):

http://www.rubyquiz.com/SimFrost.mov

Of course, you don't have to create a movie. Using the terminal is just fine.

Here are the rules of the simulation.

First, create a two-dimensional grid to hold our simulation. The width and
height must be even numbers. The top of this grid wraps (or connects) to the
bottom and the left and right sides also touch, so technically we are
representing a torus here.

Each cell in the grid can hold one of three things: vacuum, vapor, or ice. To
begin with, place a single ice particle in the center of the grid and fill the
remaining space with some random mix of vacuum and vapor. You will probably
want to leave yourself a way to easily adjust the starting vapor percentage as
you begin to play with the simulation.

Display the starting grid for the user, then begin a cycle of "ticks" that
change the current state of the grid. Redraw the grid after each tick, so the
user can see the changes. The simulation continues until there are no more
vapor particles. At that time your program should exit.

Each tick changes the grid as follows. First, divide the board into
"neighborhoods." A neighborhood is always a square of four cells. Given that,
on the first tick we would divide a six by four grid as:

+--+--+--+
| | | |
| | | |
+--+--+--+
| | | |
| | | |
+--+--+--+

However, even numbered ticks divide the board with an offset of one. In other
words, the neighborhoods will be as follows in the second round:

| | |
-+--+--+-
| | |
| | |
-+--+--+-
| | |

Remember that the grid wraps in all directions, so there are still just six
neighborhoods here. Later ticks just alternate these two styles of dividing the
grid.

In each neighborhood, apply one of the following two changes:

1. If any cell in the neighborhood contains ice, all vapor particles in the
neighborhood turn to ice.
2. Otherwise, rotate the contents of the neighborhood 90% clockwise or
counter-clockwise (50% random chance for either).

For example (using " " for vacuum, "." for vapor, and "*" for ice), the first
rule changes:

+--+
| .|
|* |
+--+

into:

+--+
| *|
|* |
+--+

The second rule changes:

+--+
|..|
| |
+--+

into:

+--+ +--+
|. | or | .| 50% chance
|. | | .|
+--+ +--+

Time is discrete in these ticks, so given some grid state at tick T all
neighborhood changes appear simultaneously in tick T + 1.

Again, use whatever output you are comfortable with, from ASCII art in the
terminal to pretty graphics.
 
J

James Edward Gray II

Quick clarification, I would assume you mean "turn 90 degrees", not
90%, as
that doesn't make any sense.

Ick, yes. Good catch. I'll fix it on the web site...

James Edward Gray II
 
R

Robert Dober

Ick, yes. Good catch. I'll fix it on the web site...

James Edward Gray II
Maybe you would like to clarify what the center is too (I'll just use
the left hand upper corner of the center but maybe).

Cheers
Robert
 
J

James Edward Gray II

Maybe you would like to clarify what the center is too (I'll just use
the left hand upper corner of the center but maybe).

That's what my diagrams are for, I hope. ;)

To eliminate the idea of rotation, we could just say this: given the
neighborhood:

+--+
|12|
|43|
+--+

Change to:

+--+
|23|
|14|
+--+

or:

+--+
|41|
|32|
+--+

There's a 50% chance to go either way.

James Edward Gray II
 
J

John Joyce

To eliminate the idea of rotation, we could just say this: given
the neighborhood:

+--+
|12|
|43|
+--+

Change to:

+--+
|23|
|14|
+--+

or:

+--+
|41|
|32|
+--+

There's a 50% chance to go either way.

James Edward Gray II
these examples are rotation. Anything else would be permutations.
Could be done numerous ways. Array sorts, Rot13 (with only 4 digits.)
etc..
 
R

Robert Dober

That's what my diagrams are for, I hope. ;)

To eliminate the idea of rotation, we could just say this: given the
neighborhood:

+--+
|12|
|43|
+--+

Change to:

+--+
|23|
|14|
+--+

or:

+--+
|41|
|32|
+--+

There's a 50% chance to go either way.

James Edward Gray II
Sorry if I was not clear, I meant the center where to put the initial freezer :)
strictly speaking it does not exist in an even order grid, well I did
not think that was a problem but as you were about to update the
site...

The Quiz description is perfectly clear though apart this little formalism.
Good work as always.
 
J

James Edward Gray II

Sorry if I was not clear, I meant the center where to put the
initial freezer :)

Oh, THAT center. ;)

It doesn't matter. I just used Ruby's integer division.

James Edward Gray II
 
J

Josef 'Jupp' Schugt

--------------enig4F9F016B9085C6CF4B6B80A5
Content-Type: text/plain; charset=ISO-8859-15
Content-Transfer-Encoding: quoted-printable

* Ruby Quiz, 09.03.2007 13:58:
The three rules of Ruby Quiz:
=20
1. Please do not post any solutions or spoiler discussion for this qui= z until
48 hours have passed from the time on this message.
=20
2. Support Ruby Quiz by submitting ideas as often as you can:
=20
http://www.rubyquiz.com/
=20
3. Enjoy!

What about using using nonstandard ruby packages? To in particular I
am talking about NArray:
=09
NArray is an n-dimensional numerical array class. Data types:
integer/float/complexe/Ruby object. Methods: array manipulation
including multi-dimensional slicing, fast arithmetic/matrix
operations, etc.

http://rubyforge.org/projects/narray/


Josef 'Jupp' Schugt
--=20
Blog available at http://www.mynetcologne.de/~nc-schugtjo/blog/
PGP key with id 6CC6574F available at http://wwwkeys.de.pgp.net/


--------------enig4F9F016B9085C6CF4B6B80A5
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFF8blOrhv7B2zGV08RApl8AKCgQgADZcocUqvWNHPvXIL6ypdHiACgwUy4
FllplQWhABmwZsc4o5nS5x0=
=Ds7j
-----END PGP SIGNATURE-----

--------------enig4F9F016B9085C6CF4B6B80A5--
 
M

Matthew Moss

Sorry if I was not clear, I meant the center where to put the initial freezer :)
strictly speaking it does not exist in an even order grid, well I did
not think that was a problem but as you were about to update the
site...

Strictly speaking, as this is a torus, you could put it anywhere and
be at "the center".
 
J

Josef 'Jupp' Schugt

--------------enig314A13FB2163DB00EE431FD2
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

* Matthew Moss, 09.03.2007 21:38:
Strictly speaking, as this is a torus, you could put it anywhere
and be at "the center".

Actually it only looks as if we are acting on a torus. The reason is
as follows:

We start with the physical problem that you have a center at which
freezing starts and an area around this center that can be dealt with
as if it is infinite in extension (meaning that we need not take into
account the surroundings).

For the simulation being feasible it is unavoidable to restrict the
simulation not only to a certain granularity but also to a certain
size of the simulated area[1] which means introducing a boundary into
the simulation that is not present in the simulated situation.

Now that we have an artificial boundary we need to find some clever
boundary conditions that limit artefacts to an unavoidable minimum.
Unless freezing takes place next to the boundary we can assume that
the leftmost region of the simulated area is not too different from
what is immediately to the right of the rightmost region of the
simulated area so that we can use it as an approximation of the true
state for this outside region (similar holding for the other sides).
This approximation obviously is quite good as long as the freezing
has not reached the boundary. But what if that happens? In the case
of the simulation we are dealing with it still is not too bad. The
reason for this is that the growth of the crystal starts at the
center of the simulated are so that the asymmetry present is a
reasult of the growth but not of the system itself being asymmetric.

In other words: While it at first sight may seems that the periodic
boundary conditions allow the center to be anywhere the truth is that
(quite the other way round) the fact that the center is in the middle
of the simulated area allows to use periodic boundary conditions (as
they are called) even after the frozen area has reached the
boundaries of the simulated area which otherwise would mark the end
of the validity of the simulation.

[1] Many of you may have heard that this is the second major issues
of the simulation of weather and climate simulation - the other
being the impossibility to simulate all the complex phenomena
involved in these processes (not even the most imporant ones).

Josef 'Jupp' Schugt
--=20
Blog available at http://www.mynetcologne.de/~nc-schugtjo/blog/
PGP key with id 6CC6574F available at http://wwwkeys.de.pgp.net/


--------------enig314A13FB2163DB00EE431FD2
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFF8dxErhv7B2zGV08RAviDAKCFj2jp0of9Net/iWmadFUX7SHOyACgpIFv
7Bjxqs4ghtc+3R02Bs4MBQ8=
=1CNT
-----END PGP SIGNATURE-----

--------------enig314A13FB2163DB00EE431FD2--
 
D

doug meyer

Again, use whatever output you are comfortable with, from ASCII art in
the
terminal to pretty graphics.

Could somebody point me in the right direction so I can find out how
to do pretty graphics?
Thanks,
 
J

James Edward Gray II

Could somebody point me in the right direction so I can find out how
to do pretty graphics?

Well, I cheated. I wrote output frames in the PPM image format
because it's dang easy and I'm dang lazy. Then I just joined them
into a movie with Graphic Converter.

If you're familiar with an of the GUI toolkits though you could just
draw on some canvas object as you simulate.

What output options are the rest of you using?

James Edward Gray II
 
M

Matthew Moss

* Matthew Moss, 09.03.2007 21:38:

Actually it only looks as if we are acting on a torus. The reason is
as follows:

My fault for getting technical... someone else was bound to get even
more technical. =)
 
M

Morton Goldberg

My fault for getting technical... someone else was bound to get even
more technical. =)

No, I think your observation is fundamentally sound. Since the
simulation really does take place on a torus, statistically speaking,
the outcome of the simulation should be independent of where the seed
for freezing is placed.

That this topology does distorts the actual physical situation being
simulated may reduce the simulation's usefulness for making
inferences about nature, but it does not change the fact that, in the
simulation, any notion of center is an artifact of the coordinate
lattice introduced to facilitate its implementation.

Regards, Morton
 
R

Raj Sahae

Any standard for the pixel width/height we should use for the
simulation? Have it as a command line option or some default value?

Raj
 
R

Robert Dober

I failed to see one impact of where to put the initial freezer so far.
Now that I have eventually looked at James' movie - great job James,
really nice - I imagine that it might indeed be a nice thing to start
the freezing process somewhere else than in the center of the
*viewscreen* that might nicely show how frost growth extends to the
other extremity of the viewscreen.

OTOH if someone can make a 3D movie of frost growth on a real torus :-D

Just an idea.

Cheers
Robert
 
J

James Edward Gray II

Any standard for the pixel width/height we should use for the
simulation? Have it as a command line option or some default value?

I say use whatever looks the best.

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top