alimaye / sun-fan-delta-model

GNU General Public License v3.0
0 stars 0 forks source link

Optimizations #52

Closed amoodie closed 3 years ago

amoodie commented 3 years ago

optimization!

(suggest using the "hide whitespace changes" option to view the PR diff)

problem

I started a model run with a larger domain (1000x1000, matching the Sun base cases) and realized we needed some code optimizations. Most of the optimizations I have implemented fall under two simple problems (i) it is really expensive to index the grid.flowsTo cell array, and (ii) it is expensive to loop over every cell in the domain.

Most of the time for problem (i), we are interested in how many cells a cell flowsTo. We do this indexing over and over again throughout a timestep though, which just added up to a ton of time. Most of the time for problem (ii), we are immediately interested in knowing whether a cell is a channel or not, and then we do something with that information (skipping any other processing if a cell is not a channel...).

solution

The solution to problem (i) is to precompute the number of cells that each cell flowsTo after there are any changes to the channel network. This is achieved in the new countFlowsToInds function. We then use this field grid.flowsToCount(k) instead of numel(grid.flowsTo{k}) throughout the other functions.

The solution to problem (ii) is to precompute the cells indices that are channels (simply channelInds=find(grid.channelFlag)) and then loop over only the channel locations.

speedup

Note that this is a nonbreaking change to the model! I.e., the output is exactly the same before and after the optimizations.

These optimizations amount to a 75% increase in speed :tada:

before after
131 seconds 32 seconds
before after

timing runs used the debug figure, which was approx 9 seconds in each case (no optimizations done there)

example

Here's 100 years, with the parameters of Sun's base case (except for gravity parameters). Screenshot from 2021-09-17 15-04-43

amoodie commented 3 years ago

It's worth pointing out that these optimizations are targeted at early model development and there may be other, or more important, model optimizations for later timesteps when the channel network is longer or more complex...

amoodie commented 3 years ago

Well these optimizations are good regardless, but I'm confused now. Are Sun's model domains 100x100 or 1000x1000?

In the base case defined there, the length of the calculational domain Lb is assumed to be 10 km2, and cell size a is assumed to be 100 m

I think that is supposed to be "Lb is assumed to be 10 km", which makes it match with the info in table 1. Then the domain should be 100x100 with 100m cells, as we already have it? right?

alimaye commented 3 years ago

Well these optimizations are good regardless, but I'm confused now. Are Sun's model domains 100x100 or 1000x1000?

In the base case defined there, the length of the calculational domain Lb is assumed to be 10 km2, and cell size a is assumed to be 100 m

I think that is supposed to be "Lb is assumed to be 10 km", which makes it match with the info in table 1. Then the domain should be 100x100 with 100m cells, as we already have it? right?

Yes