thermohub / thermofun

A code for calculating the standard state thermodynamic properties at a given temperature and pressure.
https://thermohub.org/thermofun/thermofun/
GNU Lesser General Public License v2.1
25 stars 8 forks source link

Memoization leading to extreme memory demands in reactive transport simulations #72

Open j-engelmann opened 2 months ago

j-engelmann commented 2 months ago

I initially created this issue over on the Reaktoro github, but as it seems to be related to ThermoFun i will also raise it here. In developing reactive transport simulations using Reaktoro and ThermoFun as the thermodynamic backend, i discovered that the ThermoFun part (namely the PropertiesSolvent, ElectroPropertiesSolvent and ThermoPropertiesSubstance objects) grows in memory use over runtime, until it crashes the program.

From the previous discussion with @allanleal, this may be related to memoization in ThermoFun. I believe the memory issues arise because the thermodynamic properties are stored at all unique P/T points. While this may be efficient for smaller batch calculations, its too much information to handle in the kinds of simulations i run.

Is there a way to control or turn off this feature?

Any help would be greatly appreciated!

Cheers,

Jasper

j-engelmann commented 1 week ago

If anyone else runs into this issue, i have found a solution:

The memoization is set up in ThermoFun/ThermoEngine.cpp, using template functions defined in OptimizationUtils.h. By commenting lines 107, 113, 119 and 125, you can simply turn it off. For reference, they all look something like this, calling memoize():

107 thermo_properties_substance_fn = memoize(thermo_properties_substance_fn);

I also created a new template memoizeN(fun,max_cache_size) where the least recently used entry is removed if the max size of the cache is reached. Using that with a max size of 1e6, i can smoothly run my simulations with stable memory usage.