changeset 16:f7975c054bc3

Improved progress indication for stepN
author Jonatan Werpers <jonatan@werpers.com>
date Tue, 22 Sep 2015 14:25:59 +0200
parents 16bad7c459da
children 30ae48efc7ae
files +time/Timestepper.m
diffstat 1 files changed, 50 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/+time/Timestepper.m	Tue Sep 22 13:36:45 2015 +0200
+++ b/+time/Timestepper.m	Tue Sep 22 14:25:59 2015 +0200
@@ -15,42 +15,71 @@
         function [v,t] = stepN(obj,n,progress_bar)
             default_arg('progress_bar',false);
 
-            if progress_bar && n > 1500
-                n1000 = floor(n/1000);
+            if ~progress_bar
+                obj.stepN_without_progress(n);
+            else
+                obj.stepN_with_progress(n);
+            end
+
+            v = obj.getV;
+            t = obj.t;
+        end
 
-                s = util.replace_string('','   %d %%',0);
-                for i=1:n
-                    obj.step();
-                    if mod(i,n1000) == 0
+        function stepN_without_progress(obj, n)
+            for i=1:n
+                obj.step();
+            end
+        end
+
+        function stepN_with_progress(obj, n)
+            FRAME_RATE = 20;
+
+            steps_to_update = 1;
+            steps_since_update = 0;
+            last_update = tic();
+            s = util.replace_string('','   %d %%',0);
+            for i=1:n
+                obj.step();
+
+                steps_since_update = steps_since_update + 1;
+
+                if steps_since_update >= steps_to_update
                     s = util.replace_string(s,'   %.2f %%',i/n*100);
-                    end
+
+                    time_since_update = toc(last_update);
+                    time_error = time_since_update - 1/FRAME_RATE;
+                    time_per_step = time_since_update/steps_since_update;
+
+                    steps_to_update = max(steps_to_update - 0.9*time_error/time_per_step ,1);
+
+                    steps_since_update = 0;
+                    last_update = tic();
                 end
-                s = util.replace_string(s,'');
+            end
+
+            s = util.replace_string(s,'');
+        end
+
+        function [v,t] = evolve(obj, tend, progress_bar)
+            default_arg('progress_bar',false)
+            if ~progress_bar
+                obj.evolve_without_progress(tend);
             else
-                for i=1:n
-                    obj.step();
-                end
+                obj.evolve_with_progress(tend);
             end
             v = obj.getV;
             t = obj.t;
         end
 
-        function [v,t] = evolve(obj, tend, progress_bar)
-            default_arg('progress_bar',false)
-            if progress_bar
-                obj.evolve_with_progress(tend);
-            else
-                obj.evolve_without_progress(tend);
+        function evolve_without_progress(obj, tend)
+            while obj.t < tend - obj.k/100
+                obj.step();
             end
-            v = obj.getV;
-            t = obj.t;
         end
 
         function evolve_with_progress(obj, tend)
             FRAME_RATE = 20;
 
-            dt = tend-obj.t;
-
             steps_to_update = 1;
             steps_since_update = 0;
             last_update = tic();
@@ -74,21 +103,9 @@
                 end
             end
 
-            % if t < tend
-            %     v = rk4.rungekutta_4(v, t, tend-t,F);
-            % end
-
             s = util.replace_string(s,'');
         end
 
-        function evolve_without_progress(obj, tend)
-            while obj.t < tend - obj.k/100
-                obj.step();
-            end
 
-            % if t < tend
-            %     v = rk4.rungekutta_4(v, t, tend-t,F);
-            % end
-        end
     end
 end
\ No newline at end of file