MATLAB Answers

Pre-allocating memory for a while loop

12 views (last 30 days)
Bas Haar
Bas Haar on 9 Dec 2019
Commented: Bas Haar on 10 Dec 2019
Hello everyone,
I'm running a code that is based on a while loop to similate a chemical process. It simulates an added amount of species each second, and the mass transport, reaction, pressure, energy production etcetera that is coupled with it. When the material limit is reached, the addition stops and the system runs a little longer until nearly everything has reacted away. The problem is, there is a complicated equation involved using vpasolve, and simulating every second is starting to take very long times (over 1500 seconds for 15000 iterations). A larger timestep is not really an option due to required accuracy.
I would like to pre-allocate memory by defining some of the results in arrays already, since I've read that this can reduce computational speed. However, beforehand, I don't know for how many iterations my system will run, therefore I don't know how big my arrays should be. Would it already help to make "zeros" arrays that are plenty large for sure? And how could I deal with this when plotting my results?
Thanks in advance!

  0 Comments

Sign in to comment.

Answers (2)

Thomas Satterly
Thomas Satterly on 9 Dec 2019
If you don't know how large your arrays need to be for preallocating the entire array, what you can do is allocate "chunks" of the array at a time. While it won't be as fast as having the entire array preallocated, appending on large chunks to an array when needed instead of just appending single values every iteration will likely be faster because it reduces the amount of times Matlab has to resize an array. One disadvantage is that you'll have to keep track of the true end of each array, as the size of the array might be larger than the data you actually saved to it. For example, let's say you have a while loop that runs between 1,000 and 10,000 times. If you preallocate an array with 1,000 elements, every time you exceed the array size, append another 1,000 blank elements to the end. Let's say the iteration stops after 5,268 loops. At this point, you will have resized the array 5 times for a total array size of 6,000 elements, and your data would end at element 5,268. At the every end, you can do one last resizing to bring the array size down to just the elements you care about.

  0 Comments

Sign in to comment.


Steven Lord
Steven Lord on 9 Dec 2019
I'm running a code that is based on a while loop to similate a chemical process. It simulates an added amount of species each second, and the mass transport, reaction, pressure, energy production etcetera that is coupled with it.
Is a while loop the right tool here? Based on your text description, it sounds like you've build your own very simple ODE solver. If you do have the differential equations for your chemical process, consider using the Symbolic Math Toolbox tools for solving ODEs or the tools to convert the symbolic ODE into a form the numeric ODE solvers can handle.

  1 Comment

Bas Haar
Bas Haar on 10 Dec 2019
I think this is the case, but I believe this is necessary, as a lot of the properties of the material change based on the amount of species that has reacted, which uses the complicated solver I mentioned (if you'd like to know the details, it calculates the molar fraction x in the liquid based on Raoult's law and the Wilson equation). Therefore I have to iterate this every time, at least I believe.

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by