MATLAB: Is the computation time increasing every loop for transient heat transfer pde model

computation timePartial Differential Equation Toolboxpdesolvethermal

I have this line of code (below) within a for loop and every iteration, the computation time (solveTime) increases yet there is no change to the inputs other than a single initial condition of the T on a single edge of a 2D square mesh. I have pre-allocated memory for the output variable (although this would have no effect on solveTime as written in code) and experimented with the thermalmodel.Solveroptions but no change to this increasing computation time.
My initial iteration is approximately 0.03s then increases gradually until approximately 1.4s for my 5000th iteration. I have also tried beginning at iteration 1000 and again it starts at 0.03s then increases as before. tlist is a constant also of [0 0.5].
for i = 1:nn
tic
R = solve(thermalmodel,tlist); % Transient
solveTime = toc;
end
Any advice would be helpful. Thanks!! 🙂

Best Answer

The reason for increase in the time is due to the two thermalIC call that you make within the loop. Note that each call to thermalIC creates a IC object and appends to the thermalmodel's InitialConditions property as a new record of your action and does NOT overwrite the previous assignment. So, as you run in the loop the InitialConditions’ record length keeps increasing adding additional time to find the correct IC for the successive problems in the loop. As dpb already pointed out, you are solving a different problem each time you change IC, so when you invoke the solve it has to go several checks, each time.
I have modified the code to show the correct way of achieving what you intend to do here. Use the following section for Initial Conditions, you should be all set, time remains about the same for each iteration:
%%Initial Conditions
tend = 0.1;
tlist = 0:tend/1:tend;
%Ta = 1:0.01:40;
Ta = 1;
T1 = 1;
T_new = 1;
Tx = 1;
IC1 = thermalIC(thermalmodel, T1, 'Face', 1);
IC2 = thermalIC(thermalmodel, Ta, 'Edge', 4);
for i = 1:5000
IC1.InitialTemperature = T1;
tic
R = solve(thermalmodel,tlist); % Transient
solveTime = toc;
fprintf('Solve Time = %.4f\n', solveTime)
T1 = R.Temperature(1,1);
end