changeset 3:5205251db8c3

Added some smartness to evolve with progress.`
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 18 Sep 2015 13:30:19 +0200
parents bce9e28c1e26
children 8e14b5a577a6
files +time/Timestepper.m
diffstat 1 files changed, 18 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/+time/Timestepper.m	Fri Sep 18 12:50:52 2015 +0200
+++ b/+time/Timestepper.m	Fri Sep 18 13:30:19 2015 +0200
@@ -46,18 +46,30 @@
         end
 
         function evolve_with_progress(obj, tend)
+            FRAME_RATE = 20;
+
             dt = tend-obj.t;
-            n = floor(dt/obj.k);
-            n1000 = floor(n/1000);
 
-            i = 0;
+            steps_to_update = 1;
+            steps_since_update = 0;
+            last_update = tic();
             s = util.replace_string('','   %d %%',0);
             while obj.t < tend - obj.k/100
                 obj.step();
 
-                i = i + 1;
-                if mod(i,n1000) == 0
-                    s = util.replace_string(s,'   %.2f %%',i/n*100);
+                steps_since_update = steps_since_update + 1;
+
+                if steps_since_update >= steps_to_update
+                    s = util.replace_string(s,'   %.2f %%',obj.t/tend*100);
+
+                    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
             end