Wednesday, March 6, 2019

The mysterious case of the shrinking river

In the last post, I noticed an annoying river:


This lake has a large river flowing out of its eastern side, heading roughly northeast. There is a smaller river flowing into that river from the west. But this smaller river has a gap in it. What's going on?

The problem lies somewhere in these three tiles:

  

In the first tile, the river flows from northwest to southeast. At the exit point of the tile (the marked cell), its flow is 176 m3/s in January and 168 m3/s in July. In the second tile, the river flows from northwest to east. At the entry point of the tile (the marked cell), its flow is 153/145. So for some reason, it's lost some of its volume - 23/23, to be precise! Halfway through the tile, a tributary joins the river. The tributary's size is 47/34. Our river accordingly ends the tile at 200/179. In the third tile, the river starts off at 212/188, so it has somehow grown in size by 12/9. (This last point is normal - my rivers typically do grow at the start of new tiles, although they're supposed to gain all their additional flow by the arrival of tributaries and springs. But it's not too disastrous - maybe there are little streams not shown on the map - so I'm not worrying about it for now.)

So the river is, for some reason, smaller in tile 2 than it should be. This causes it not to be drawn on the relief map, because it has fallen below the size threshold for depicting rivers on that map. In tile 2, the river grows again - enough to be drawn, but still it has not regained all the lost volume.

Why would this be happening? Well, the volume of any river at its entry point of a tile is simply taken from the volume of flow in the source cell of the upstream tile. The weirdly low flow in tile 2 is therefore caused by the volume of flow in the source cell of tile 1, which is 153/145. Why is the river in tile 1 bigger than that? Because the volume of flow in the source cell for the tile upstream of tile 1 is 176/168. The one before that is 152/146.

So the problem seems to lie not with the river generation at the regional level (thank God), but with the flow calculations at the global level. Somehow, the flow in these cells is being calculated in such a way that flow is lost from one cell to the next. That is something that should never happen.

The global river generation works by going to each (land) cell in turn, seeing how much precipitation there is there, working out how much Jan/Jul flow that would create, and then working downhill until it reaches the sea, adding that amount of flow to each cell as it goes. I wonder if maybe it is sometimes thinking that the additional flow is negative, and so it's sometimes removing flow as it goes. I don't really see how that could happen, but it would explain what we're seeing here. So I try adding a trap to catch that: the flow to be added cannot be lower than 0.

That didn't change anything. So that wasn't the cause.

An alternative approach: rather than try to work out what's causing this problem, I can apply a bodge to redress it. This is annoying (because I'd like to know why it happened) and inefficient (because the bodge will presumably slow down the world generation more than removing the glitch would), but allowing the whole project to grind to a halt while I tear my hair out trying to work this out is just making the best the enemy of the good. If I can reverse the effects of the glitch, that's almost as good as removing it completely.

So I can add a function to the global river generation that goes over the whole map looking for cells that have lower flow than the sum of all cells that flow into them. If it finds any, it adds the missing amount, and then goes downstream to the sea, checking each cell in that river as it goes.

And... that didn't change anything either. So either that bodge didn't work properly, or the problem is somehow coming about after the global flow map is created. But how could that happen? There isn't any point at which that map gets altered after it's been created. Except... for when the global lake map is created. Surely this can't be another lake-related problem?

I try turning off lakes and running it all again. This is what the region in question now looks like:



Despite the changes to this region caused by removing the large lake, it's still possible to recognise the river we're interested in, and it looks fine. There is no weird reduction of flow. In other words, this really is another lake-related problem, even though it didn't seem like it at all to start with; that river didn't appear to be close enough to the lake to have anything much to do with it. But something in the function that creates the lakes at the global level has screwed up that river, and no doubt other ones too that I haven't noticed.

Once again I'm severely tempted just to cut out all lakes and hope no-one notices. But no: there must be a solution to this.

The problem lies with the river diverting routine that's part of the lake creating phase. For some reason, as the program diverts rivers near lakes to go towards the lakes, it's removing that part of that river but not recreating it heading towards the lake. I can't work out why this is so. However, I still have my bodge routine that I put together before. It didn't make any difference when I first wrote it, because the program was calling it before the problem was created. It's a simple matter to change it so that the program calls it after doing the lakes on the global map. Now the region looks like this:


The lake's back, and the river is now the proper size. Success!

...Well, mostly. Here are two of the tiles of that river that we focused on before:

 

As before, our river enters tile 1 from the northwest and leaves it to the southeast; it enters tile 2 from the northwest and leaves it to the east. In each tile, you can see a tributary joining our river (at the highlighted cells in each tile). Now tributaries are supposed to deliver their flow into the river being joined, so it should grow by the appropriate amount at those cells. However, it doesn't. In tile 1, the main river simply ignores the tributary - it stays exactly the same size even after the tributary has flowed into it. In tile 2, the main river does grow when the tributary joins it, but not nearly enough.

Clearly this is an undesirable effect of the bodge routine. By artificially adding flow to those cells that somehow didn't have enough, it's screwed up the relationship between the flow in those cells and the flow in cells that is flowing into them. This is the result. However, I'm not going to deal with it any further for now. It's only affecting these tiles (and presumably a few others near lakes) - most tributaries are flowing into their major rivers just right. I think that this is a less serious issue than a river suddenly shrinking for 30km and then regrowing, and it's something I can live with, at least for now. I'm so professional.

1 comment:

  1. Great blog, very interesting to read all this! Good luck with this project!

    ReplyDelete