parfor loop
Closed this issue · 6 comments
(This is a test to see how to open an issue.)
Hey Dave,
let me explain the general concepts about the part following the vectorization, i.e. the parfor loop. Basically this part is the "heart" of the model.
The loop runs the individual simulations of the freezing process in parallel, which is very handy since the runtime scales well with the number of available cores. To allow for reproducibility, it is necessary to initialize the random number generator in each iteration and to this differently.
The loop starts with a couple of definitions; they are temporary variables required to pass values to the outside of the loop. This also includes the state variables (T_time and sigma_time), for which I also extract some values, which is needed to make the thermal evolution figures.
There are two nested loops within the parfor loop. The outer one is the time loop, the inner one goes through all vials. For each timestep I calculate the heat flows q and store some information on the vials states.
The inner loop is based on an if..else structure, comprising the two cases liquid and ice growth state, which we distinguish based on the value of sigma. In the ice growth state, we calculate the new state variables by following the freezing point depression curve in the phase diagram. Doing so is a bit ugly, so I think I will make a slide outlining the equations here. When a certain ice fraction is reached (i.e. 90% here), we define that the solidification time is reached. However, the vial stays in this state also afterwards.
The second state is the liquid one. Here we just follow the energy balance and calculate the new temperature in each step. If supercooled, we test for stochastic nucleation. There is also the possibility to do controlled nucleation (i.e. when setting cont = 1), where nucleation will be forced at iteration i == T_insert_end. When nucleation is induced, we calculate the initial sigma and T in the same way as in the ice growth state.
Thats's basically it. We also need to define some more variables to extract the data from the loop. Also, after the loop I restructure the output variables in a 4D tensor (i.e. based on the number of vials in x,y, and z direction), because I used this format before vectorizing and I still use it for figures.
Please let me know if anything is unclear.
yep, works
Note @ltdeck , I suggest we write the for loop without parallelization first, then confirm qualitatively similar behavior on a small-scale case (I am not sure whether we can have the same rng for both Matlab and python to have a perfect match). Once this is working we parallelize. The reason I want to split is because parallelization works a bit different in python in that there is no parfor function. We need to make some choices.
Hi @bizzinho I agree on this. Actually when I started programming the model, I did not know about parallelization. My first versions were with a for loop and they worked well. For small systems the model is fast enough without parallelization to get meaningful results for comparison with the matlab model.
Note to self: probably convert most of current snowfall class to class snowflake = one run!?
I like that thought. Many snowflakes = snowfall
Dashboard with snowfall results? Snowboard ;)