Re: For Loop and NDSolve
- To: mathgroup at smc.vnet.net
- Subject: [mg85259] Re: For Loop and NDSolve
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Mon, 4 Feb 2008 03:17:29 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <fnuh4s$9ni$1@smc.vnet.net> <fo19o9$fu8$1@smc.vnet.net> <fo3g6g$b24$1@smc.vnet.net>
PV wrote: > Hello Here is a stand alone version of my code, I have not got rid of > the for loop as suggested by Szabolcs here but Iam trying something > like that right now! I would be grateful to anybody who can give me a > hint or two abot which part of this code is eating up excessive > memory! <snip> Hi Prasanna, You will find below a slightly modify version of the code you posted, so we can track the memory consumption and some possible memory leaks, as well as part of the results I got for N0 = 500. (Note that the program took about 13 hours of CPU time to complete the hundred iterations. I started it on Saturday night -- before leaving for a party -- and it was completed when I return to my iMac on Sunday afternoon.) Glancing at the results, I suspect that the large memory consumption is due to the large size of the solution (called "soln" in the code) returned by NDSolve. NDSolve returns a solution in the form of an InterpolatingFunction object. The size of this object depends on the number of sampling points that are required to get a good fit. Roughly speaking, the size of the solution returned by NDSolve, an interpolating function, is proportional to the complexity of the system to be solved, say the number of non-trivial equations in your system. My understanding -- or, at this stage, I should say, "A wild guess" -- is that the sparce matrix you build at each iteration becomes less dense (that is, holds more non-zero entries) depending on the original size of the problem (N0) and the number of iterations (j). Therefore the system to be solved becomes more complex and the solution (soln) becomes larger. As one can see below, soln starts at about 600 MB and ends at about 2 GB. Hope this helps, -- Jean-Marc (* Beginning of the code *) $HistoryLength = 0; $Version (* "6.0 for Mac OS X x86 (64-bit) (June 19, 2007)" *) c3 = 0.035896; N0 = 500; k0 = 1/40; L = 80 \[Pi]; \[Eta] = 1.0485203*10^6; \[Gamma] = 1.85353*^6; \[Kappa] = 3.707*^6; delt = 1/1000; a = 0.26452763498582404; V0[a_] := 79.06*Abs[a]^2 deltanew[n_] := If[Mod[n, N0] == 0, N0*1/(Sqrt[L])* NIntegrate[ E^(-I*n*k0*z)*MathieuC[-0.81399423, -1.383053249, z], {z, 0, Pi}], 0] list1 = 1/(L)*Chop[Table[deltanew[n], {n, -4*N0, 4*N0}]]; zeropad = Table[0, {l, 1, 16*N0}]; wavefn = Join[list1, zeropad]; fieldlist3 = {}; couplinglist = {}; normlist = {}; Print["\n-----------------"]; Print["Memory consumption for N0 = ", N0]; Print["Entering Loop - Memory Used: ", MemoryInUse[], ", Max Memory: ", MaxMemoryUsed[]]; cputime = First@Timing[ For[j = 1, j <= 100, t = j*1/1000; Print["\nMemory Used: ", MemoryInUse[], ", Max Memory: ", MaxMemoryUsed[]]; solnmatrix = -I/c3* SparseArray[{{i_, i_} -> (-V0[a]/2 + ((i - (4*N0 + 1))*k0 - 2*t)^2), {m_, n_} /; Abs[m - n] == N0 -> -V0[a]/4}, {24*N0 + 1, 24*N0 + 1}]; Print["solnmatrix size: ", ByteCount@solnmatrix]; Print["Memory Used: ", MemoryInUse[], ", Max Memory: ", MaxMemoryUsed[]]; soln = NDSolve[{u'[p] == solnmatrix.u[p], u[0] == wavefn}, u, {p, 0, delt}]; Print["soln size: ", ByteCount@soln]; Print["Memory Used: ", MemoryInUse[], ", Max Memory: ", MaxMemoryUsed[]]; wavefn = First[u[delt] /. soln]; Print["wavefn size: ", ByteCount@wavefn]; Print["Memory Used: ", MemoryInUse[], ", Max Memory: ", MaxMemoryUsed[]]; C1 = (Tr[Abs[wavefn]^2/2] + 0.25*Sum[ Conjugate[wavefn[[n]]]*wavefn[[n + N0]], {n, 1, 23*N0 + 1}] + 0.25*Sum[ Conjugate[wavefn[[n]]]*wavefn[[n - N0]], {n, N0 + 1, 24*N0 + 1}])*L; Print["C1 size: ", ByteCount@C1]; Print["Memory Used: ", MemoryInUse[], ", Max Memory: ", MaxMemoryUsed[]]; b = -\[Eta]/(I*\[Gamma]* C1 - \[Kappa]) + ((\[Eta] + (I*\[Gamma]*C1 - \[Kappa])* a)/(I*\[Gamma]*C1 - \[Kappa]))* E^(delt*0.0011*(I*\[Gamma]*C1 - \[Kappa])); a = b; AppendTo[fieldlist3, a]; Print["fieldlist3 size: ", ByteCount@fieldlist3]; Print["Memory Used: ", MemoryInUse[], ", Max Memory: ", MaxMemoryUsed[]]; AppendTo[normlist, L*Tr[Abs[wavefn]^2]]; Print["normlist size: ", ByteCount@normlist]; Print["Memory Used: ", MemoryInUse[], ", Max Memory: ", MaxMemoryUsed[]]; AppendTo[couplinglist, C1]; Print["couplinglist size: ", ByteCount@couplinglist]; Print["Iter no: ", j, " , Memory Used: ", MemoryInUse[], ", Max Memory: ", MaxMemoryUsed[]]; j++;] ]; Print["CPU Time: ", cputime]; Print["Exiting Loop - Memory Used: ", MemoryInUse[], ", Max Memory: ", MaxMemoryUsed[]]; (* End of the code *) --------------------- Memory consumption for N0 = 500 Entering Loop - Memory Used: 14028176, Max Memory: 16809016 Memory Used: 14036208, Max Memory: 16809016 solnmatrix size: 2988828 Memory Used: 16224536, Max Memory: 24243360 soln size: 685656272 Memory Used: 702042440, Max Memory: 712914208 wavefn size: 192132 Memory Used: 702138664, Max Memory: 712914208 C1 size: 72 Memory Used: 702119728, Max Memory: 712914208 fieldlist3 size: 104 Memory Used: 702119760, Max Memory: 712914208 normlist size: 48 Memory Used: 702119792, Max Memory: 712914208 couplinglist size: 104 Iter no: 1 , Memory Used: 702119896, Max Memory: 712914208 Memory Used: 702119824, Max Memory: 712914208 solnmatrix size: 2988828 Memory Used: 702117024, Max Memory: 712914208 soln size: 947004752 Memory Used: 963530320, Max Memory: 1660783512 wavefn size: 192132 Memory Used: 963722616, Max Memory: 1660783512 C1 size: 72 Memory Used: 963530480, Max Memory: 1660783512 fieldlist3 size: 184 Memory Used: 963530528, Max Memory: 1660783512 normlist size: 72 Memory Used: 963530536, Max Memory: 1660783512 couplinglist size: 184 Iter no: 2 , Memory Used: 963530616, Max Memory: 1660783512 Memory Used: 963530544, Max Memory: 1660783512 solnmatrix size: 2988828 Memory Used: 963525616, Max Memory: 1660783512 soln size: 1209890576 Memory Used: 1226476640, Max Memory: 2185129408 wavefn size: 192132 Memory Used: 1226669128, Max Memory: 2185129408 C1 size: 72 Memory Used: 1226477184, Max Memory: 2185129408 fieldlist3 size: 264 Memory Used: 1226477424, Max Memory: 2185129408 normlist size: 96 Memory Used: 1226477624, Max Memory: 2185129408 couplinglist size: 264 Iter no: 3 , Memory Used: 1226477896, Max Memory: 2185129408 Memory Used: 1226478016, Max Memory: 2185129408 solnmatrix size: 2988828 Memory Used: 1226473248, Max Memory: 2185129408 soln size: 1413972992 Memory Used: 1430607432, Max Memory: 2652198880 wavefn size: 192132 Memory Used: 1430799888, Max Memory: 2652198880 C1 size: 72 Memory Used: 1430607944, Max Memory: 2652198880 fieldlist3 size: 344 Memory Used: 1430608184, Max Memory: 2652198880 normlist size: 120 Memory Used: 1430608384, Max Memory: 2652198880 couplinglist size: 344 Iter no: 4 , Memory Used: 1430608656, Max Memory: 2652198880 Memory Used: 1430608776, Max Memory: 2652198880 solnmatrix size: 2988828 Memory Used: 1430604008, Max Memory: 2652198880 soln size: 1551949616 Memory Used: 1568617136, Max Memory: 2994333440 wavefn size: 192132 Memory Used: 1568809592, Max Memory: 2994333440 C1 size: 72 Memory Used: 1568617648, Max Memory: 2994333440 fieldlist3 size: 424 Memory Used: 1568617888, Max Memory: 2994333440 normlist size: 144 Memory Used: 1568618088, Max Memory: 2994333440 couplinglist size: 424 Iter no: 5 , Memory Used: 1568618360, Max Memory: 2994333440