Question: Where is the problem in my loop which increases the memory usage?

In part of my program, I have a loop that the iterative calculations are performed there. During running the program when it comes to the loop the memory usage become full in a short period of time and I face the memory shortage. Since my program is so long and huge, I only can show the loop part of it here, you may look at where is the problem of high memory usage in loop and how to deal with it. Any comment will be appreciated.

I again note that, this segment can not be run independently since it is a part of big program. I put it just to see and mmake comment if any.

M := 3; Nm := 1

w0 := Matrix(M+Nm, 1)

wd0 := Matrix(M+Nm, 1)

taim := 1

F0 := convert([seq(Quadrature(Quadrature(simplify(add(add(W[i, j, 4, r]*LegendreP(i, 1)*LegendreP(j, 0), i = 0 .. II), j = 0 .. JJ)), `ζ__1` = -1 .. 1, method = romberg[3]), `η__1` = -1 .. 1, method = romberg[3]), r = 1 .. M), seq(0, n = 1 .. Nm)], Matrix)

T := 1/(2*Pi*`ω__km`[M][2])

dt := (1/10)*T

0.1123479896110e-4

(1)

for i to M do tau[i](t) := tau[i] end do

for i to Nm do p[i](t) := p[i] end do

`α__N` := .5; `β__N` := .25; a__0 := 1/(`β__N`*dt^2); a__1 := `α__N`/(`β__N`*dt); a__2 := 1/(`β__N`*dt); a__3 := 1/(2*`β__N`)-1; a__4 := `α__N`/`β__N`-1; a__5 := dt*(`α__N`/(2*`β__N`)-1); a__6 := dt*(1-`α__N`); a__7 := `α__N`*dt

q := Matrix(1 .. M+Nm, 1 .. floor(taim/dt)+1)

dq := Matrix(1 .. M+Nm, 1 .. floor(taim/dt)+1)

ddq := Matrix(1 .. M+Nm, 1 .. floor(taim/dt)+1)

q(1 .. M+Nm, 1) := w0

dq(1 .. M+Nm, 1) := wd0

for i to M do tau[i] := q(i, 1) end do

for i to Nm do p[i] := q(M+i, 1) end do

ddq(1 .. M+Nm, 1) := 1/ML.(diff((diff(sin(`ω__b`*t__0), t__0))*LinearAlgebra:-Transpose(F0), t__0)-CL.dq(1 .. M+Nm, 1 .. 1)-KL.q(1 .. M+Nm, 1 .. 1)-eqMNL)

t__0 := dt

V := LinearAlgebra:-Transpose(convert([seq(Tau[i], i = 1 .. M), seq(P[i], i = 1 .. Nm)], Matrix))

Qn__2 := LinearAlgebra:-Transpose(Matrix(1, M+Nm))

Qn__3 := LinearAlgebra:-Transpose(Matrix(1, M+Nm))

External force

 

`ω__b` := 25

``

F := diff((diff(sin(`ω__b`*t), t))*LinearAlgebra:-Transpose(convert(F0, Matrix)), t)

``

s := 1

k__d := 100000

with(Student[NumericalAnalysis]); dt

``

Loop

 

for t from t__0 by dt to taim do EQ := KL.V+a__0*ML.V+a__1*CL.V-ML.(a__0*q(1 .. M+Nm, s .. s)+a__2*dq(1 .. M+Nm, s .. s)+a__3*ddq(1 .. M+Nm, s .. s))-CL.(a__1*q(1 .. M+Nm, s .. s)+a__4*dq(1 .. M+Nm, s .. s)+a__5*ddq(1 .. M+Nm, s .. s))-F-Qn__2-Qn__3; G := Matrix(M+Nm, proc (i, j) options operator, arrow; (diff(EQ[i], V[j, 1]))[1] end proc); G1 := 1/G; if t = t__0 then X[1] := 1/(CL*a__1+ML*a__0+KL).(F+ML.(a__0*q(1 .. M+Nm, s .. s)+a__2*dq(1 .. M+Nm, s .. s)+a__3*ddq(1 .. M+Nm, s .. s))+CL.(a__1*q(1 .. M+Nm, s .. s)+a__4*dq(1 .. M+Nm, s .. s)+a__5*ddq(1 .. M+Nm, s .. s))) else X[1] := q(1 .. M+Nm, s .. s) end if; for k to 11 do X[k+1] := eval(V-G1.EQ, Equate(V, X[k])) end do; q(1 .. M+Nm, s+1 .. s+1) := X[k]; ddq(1 .. M+Nm, s+1 .. s+1) := a__0*(q(1 .. M+Nm, s+1 .. s+1)-q(1 .. M+Nm, s .. s))-a__2*dq(1 .. M+Nm, s .. s)-a__3*ddq(1 .. M+Nm, s .. s); dq(1 .. M+Nm, s+1 .. s+1) := dq(1 .. M+Nm, s .. s)+a__6*ddq(1 .. M+Nm, s .. s)+a__7*ddq(1 .. M+Nm, s+1 .. s+1); Wxy2 := add(add(add(h*W[i, j, 2, o]*LegendreP(i, `&zeta;__2`)*LegendreP(j, `&eta;__2`)*q[o, s+1], i = 0 .. II), j = 0 .. JJ), o = 1 .. M); Wxy3 := add(add(add(h*W[i, j, 3, o]*LegendreP(i, `&zeta;__2`)*LegendreP(j, `&eta;__2`)*q[o, s+1], i = 0 .. II), j = 0 .. JJ), o = 1 .. M); Plt := plots:-implicitplot(Wxy2-Wxy3, `&zeta;__2` = -1 .. 1, `&eta;__2` = -1 .. 1, color = red, thickness = 2, gridrefine = 3); j := 111111; Hvs2 := 1; for i while i <= j do Pnt[i] := op([1, i], Plt); if Wxy2-Wxy3 = 0 then j := 0 elif convert(Pnt[i], string)[1] = "C" then j := 0 else a__e[i] := (1/2)*abs(max(Column(Pnt[i], 1))-min(Column(Pnt[i], 1))); if a__e[i] = 0 then a__e[i] = 0.1e-5 end if; b__e[i] := (1/2)*abs(max(Column(Pnt[i], 2))-min(Column(Pnt[i], 2))); if b__e[i] = 0 then b__e[i] = 0.1e-5 end if; xc[i] := (max(Column(Pnt[i], 1))+min(Column(Pnt[i], 1)))*(1/2); yc[i] := (max(Column(Pnt[i], 2))+min(Column(Pnt[i], 2)))*(1/2); Hv2[i] := ((`&zeta;__2`-xc[i])/a__e[i])^2+((`&eta;__2`-yc[i])/b__e[i])^2-1; Hvs2 := Hv2[i]*Hvs2 end if end do; Qn__2 := LinearAlgebra:-Transpose(convert([(1/2)*k__d*Grid:-Seq(Quadrature(Quadrature(Heaviside(-Hvs2)*abs(Wxy2-Wxy3)*add(add(W[i, j, 2, r]*LegendreP(i, `&zeta;__2`)*LegendreP(j, `&eta;__2`), i = 0 .. II), j = 0 .. JJ), `&zeta;__2` = -1 .. 1, method = romberg[5]), `&eta;__2` = -1 .. 1, method = romberg[5]), r = 1 .. M), seq(0, n = 1 .. Nm)], Matrix)); Qn__3 := -Qn__2; s := s+1 end do

``

Download HighMemUseProb.mw

Please Wait...