Skip to content

Commit

Permalink
Faster watermap (#245)
Browse files Browse the repository at this point in the history
* created simulation_test.py

* test for _watermap rng stability

* improved comments on test_watermap_rng_stability

* random_land can return a list

* typo in changelog

* test that random_land actually returns land

* missing whitespace

* decided that a recatangle is a better test case than a square w. r. t. indexing correctly.

* more missing whitespaces

* fixed comment to match code

* use faster random land

* random_land returns an array of length num_samples*2

* moved check for no land present outside of loop

* added test for calling watermap with no land

* comments and cleanup
  • Loading branch information
MM1nd authored and psi29a committed Oct 1, 2017
1 parent 88f0235 commit b5db0c6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Version 0.20 (UNRELEASED)

* Calculation of sea depth is now faster.
* Hydrology simulation is now faster.

Version 0.19

Expand Down
23 changes: 15 additions & 8 deletions tests/simulation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
from worldengine.model.world import World, Size, GenerationParameters

class TestSimulation(unittest.TestCase):


# The hydrology simulation indirectly calls the global rng.
# We want different implementations of _watermap
# and internally called functions (especially random_land)
# to show the same rng behaviour and not contamine the state of the global rng
# should anyone else happen to rely on it.
# This does only test that watermap leaves the rng in the state that the
# original implementation would have left it in and that a very small sample
# of results is the same as the original results.
Expand All @@ -26,7 +20,6 @@ class TestSimulation(unittest.TestCase):
# have a finer grained picture.

def test_watermap_rng_stabilty(self):

seed=12345
numpy.random.seed(seed)

Expand Down Expand Up @@ -56,8 +49,22 @@ def test_watermap_rng_stabilty(self):
d = numpy.random.randint(0,100)
self.assertEqual(d, 59)


def test_watermap_does_not_break_with_no_land(self):
seed=12345
numpy.random.seed(seed)

size = Size(16,8)

ocean = numpy.full((size.height, size.width), True, bool)

w = World("watermap", size, seed, GenerationParameters(0, 1.0, 0))
w.ocean = ocean

data, t = WatermapSimulation._watermap(w, 200)


def test_random_land_returns_only_land(self):

size = Size(100,90)

ocean = numpy.fromfunction(lambda y, x: y>=x, (size.height, size.width))
Expand Down
1 change: 0 additions & 1 deletion worldengine/model/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ def contains(self, pos):
#

def random_land(self, num_samples=1):

if self.layers['ocean'].data.all():
return None, None # return invalid indices if there is no land at all

Expand Down
19 changes: 15 additions & 4 deletions worldengine/simulations/hydrology.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,21 @@ def droplet(world, pos, q, _watermap):
_watermap[y, x] += q

_watermap_data = numpy.zeros((world.height, world.width), dtype=float)
for i in range(n):
x, y = world.random_land() # will return None for x and y if no land exists
if x is not None and world.precipitations_at((x, y)) > 0:
droplet(world, (x, y), world.precipitations_at((x, y)), _watermap_data)

# This indirectly calls the global rng.
# We want different implementations of _watermap
# and internally called functions (especially random_land)
# to show the same rng behaviour and not contamine the state of the global rng
# should anyone else happen to rely on it.

land_sample = world.random_land(n)

if land_sample[0] is not None:
for i in range(n):
x, y = land_sample[2*i], land_sample[2*i+1]
if world.precipitations_at((x, y)) > 0:
droplet(world, (x, y), world.precipitations_at((x, y)), _watermap_data)

ocean = world.layers['ocean'].data
thresholds = dict()
thresholds['creek'] = find_threshold_f(_watermap_data, 0.05, ocean=ocean)
Expand Down

0 comments on commit b5db0c6

Please sign in to comment.