Why does NDSolve and ParametricNDSolveValue show different memory manage in same loop? - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-18T06:17:07Z https://mathematica.stackexchange.com/feeds/question/101088 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/101088 0 Why does NDSolve and ParametricNDSolveValue show different memory manage in same loop? sejabs https://mathematica.stackexchange.com/users/23198 2015-12-03T13:26:26Z 2015-12-03T13:55:31Z <p>This is a follow-up question about <a href="https://mathematica.stackexchange.com/questions/100677/how-to-delete-interpolationfunction-created-by-ndsolve-parametricndsolve">How to delete InterpolationFunction created by NDSolve/ParametricNDSolve?</a>. In the previous question, J. M.♦ and Albert Retey showed \$HistoryLength = 0 and Clear[] are uesful to release memory occupied by InterplatingFunction. But in the present question, I found a different memory manage between NDSolve and ParametricNDSolveValue. The code is follows:</p> <pre><code>(*NDSolve*) \$HistoryLength = 0; mpl = 1/Sqrt[6.70837*10^-39]; gsT = 106.75; Sup[ΛI_?NumericQ, ΓI_?NumericQ] := Module[{a, ρr, Trad, tf, s, t, result}, tf = 10/ΓI; s = NDSolve[{a'[t] == a[t]*Sqrt[(8 π)/(3 mpl^2) (ρr[ t] + ΛI^4/ a[t]^3 Exp[-ΓI t])], ρr'[t] + 4*Sqrt[(8 π)/(3 mpl^2) (ρr[ t] + ΛI^4/ a[t]^3 Exp[-ΓI t])] ρr[ t] == ΓI ΛI^4/ a[t]^3 Exp[-ΓI t], a == 1, ρr == 0}, {a, ρr}, {t, 0, tf}]; {a = a /. s[], ρr = ρr /. s[]}; result = ρr[tf]*a[tf]; Remove[tf]; Remove[Trad]; Remove[s]; Remove[a]; Remove[ρr]; Remove[t]; result] </code></pre> <p>I use more power clean function Remove[] to replace Clear[], and in the loop</p> <pre><code>Do[Sup[2.2, time], {time, 1, 10000, 0.1}] </code></pre> <p>showing a stable memory which is not increaing. But using ParametricNDSolveValue with the same equation and loop, it shows a increasing memory.</p> <pre><code>Do[(Module[{s, as}, s = ParametricNDSolveValue[{a'[t] == a[t]*Sqrt[(8 π)/(3 mpl^2) (ρr[ t] + ΛI^4/ a[t]^3 Exp[-ΓI t])], ρr'[t] + 4*Sqrt[(8 π)/(3 mpl^2) (ρr[ t] + ΛI^4/ a[t]^3 Exp[-ΓI t])] ρr[ t] == ΓI ΛI^4/ a[t]^3 Exp[-ΓI t], a == 1, ρr == 0}, {a, ρr}, {t, 0, tf}, {ΛI, ΓI, tf}]; as = s[2.2, time, time/10]; Remove[s]; Remove[as];]), {time, 1, 10000, 0.1}] </code></pre> <p>or </p> <pre><code>s = ParametricNDSolveValue[{a'[t] == a[t]*Sqrt[(8 π)/(3 mpl^2) (ρr[ t] + ΛI^4/ a[t]^3 Exp[-ΓI t])], ρr'[t] + 4*Sqrt[(8 π)/(3 mpl^2) (ρr[ t] + ΛI^4/ a[t]^3 Exp[-ΓI t])] ρr[ t] == ΓI ΛI^4/ a[t]^3 Exp[-ΓI t], a == 1, ρr == 0}, {a, ρr}, {t, 0, tf}, {ΛI, ΓI, tf}]; Do[(Module[{as}, as = s[2.2, time, time/10]; Remove[as];]), {time, 1, 10000, 0.1}] </code></pre> <p>So is there a explain?</p>