changeset 20:c7efff913935

Decoupled plotting and timesteppers. Added possibility of different plot_types. Added functions for printing and plotting solution file content.
author Jonatan Werpers <jonatan@werpers.com>
date Tue, 22 Sep 2015 17:35:58 +0200
parents 1644d000c304
children b1e04c1f2b45
files +noname/Discretization.m +noname/animate.m +noname/calculateSolution.m +noname/plotSolutions.m +noname/printSolutions.m
diffstat 5 files changed, 85 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/+noname/Discretization.m	Tue Sep 22 17:10:51 2015 +0200
+++ b/+noname/Discretization.m	Tue Sep 22 17:35:58 2015 +0200
@@ -28,6 +28,10 @@
         %             if skipped getTimestep should use a precomputed value.
         k = getTimestep(obj, method, cfl)
 
+        % getTimeSnapshot returns a struct which represents the solution in ts at current time.
+        % if ts is empty or 0 a representation of the initial conditions be returned.
+        repr = getTimeSnapshot(obj,ts)
+
 
         % Sets up movie recording to a given file.
         %     saveFrame is a function_handle with no inputs that records the current state
@@ -37,7 +41,8 @@
         % Sets up a plot of the discretisation
         %     update is a function_handle accepting a timestepper that updates the plot to the
         %            state of the timestepper
-        [update,hand] = setupPlot(obj)
+        %     type allows for different kinds of plots. Some special values are used by the lib. 'animate' and 'plot' for example
+        [update,hand] = setupPlot(obj, type)
 
     end
 
--- a/+noname/animate.m	Tue Sep 22 17:10:51 2015 +0200
+++ b/+noname/animate.m	Tue Sep 22 17:35:58 2015 +0200
@@ -25,7 +25,7 @@
     ts = discretization.getTimestepper(time_method);
     fprintf(' - done  %fs\n', toc())
 
-    [update, figure_handle] = discretization.setupPlot();
+    [update, figure_handle] = discretization.setupPlot('animation');
 
     if makemovies
         save_frame = anim.setup_fig_mov(figure_handle,dirname);
@@ -37,7 +37,8 @@
     % Loop function
     function next_t = G(next_t)
         ts.evolve(next_t);
-        update(ts);
+        sol = discretization.getTimeSnapshot(ts);
+        update(sol);
         % waitforbuttonpress
         if makemovies
             save_frame();
@@ -46,7 +47,8 @@
         str = util.replace_string(str,'t = %.2f',ts.t);
 
     end
-    update(ts);
+    sol = discretization.getTimeSnapshot(0);
+    update(sol);
 
     fprintf('Using time step k = %.6f\n',ts.k)
     fprintf('System size: %d\n',size(discretization))
--- a/+noname/calculateSolution.m	Tue Sep 22 17:10:51 2015 +0200
+++ b/+noname/calculateSolution.m	Tue Sep 22 17:35:58 2015 +0200
@@ -43,9 +43,8 @@
 
             % Do we want to to save the initial conditions?
             if T(1) == 0
-                v = discr.v0;
-                t = 0;
-                saveToFile(sf, method, order(i), m(j),T(1), v, t, NaN, NaN, discrHand);
+                snapshot = discr.getTimeSnapshot(0);
+                saveToFile(sf, method, order(i), m(j),T(1), snapshot, NaN, NaN, discrHand);
                 T(1) = [];
             end
 
@@ -61,9 +60,10 @@
                     end_step = N * time_multiples(l);
                     fprintf('[order = %-*d, m = %-*d, T = %-*d]: ',orderWidth,order(i),mWidth,m(j),TWidth,T(l));
                     clock_start = tic();
-                    [v,t] = ts.stepN(end_step-ts.n,true);
+                    ts.stepN(end_step-ts.n,true);
                     runtime = runtime + toc(clock_start);
-                    saveToFile(sf, method, order(i), m(j),T(l), v, t, runtime, k, discrHand);
+                    snapshot = discr.getTimeSnapshot(ts);
+                    saveToFile(sf, method, order(i), m(j),T(l), snapshot, runtime, k, discrHand);
                     fprintf('Done! (%.3fs)\n',runtime);
                 end
             else
@@ -75,7 +75,8 @@
                     clock_start = tic();
                     [v,t] = ts.stepN(N-ts.n,true);
                     runtime = toc(clock_start);
-                    saveToFile(sf, method, order(i), m(j),T(l), v, t, runtime, k, discrHand);
+                    snapshot = discr.getTimeSnapshot(ts);
+                    saveToFile(sf, method, order(i), m(j),T(l), snapshot, runtime, k, discrHand);
                     fprintf('Done! (%.3fs)\n',runtime);
                 end
 
@@ -86,14 +87,13 @@
 end
 
 
-function saveToFile(sf, method, order, m, T, v, t, runtime, k, discrHand)
+function saveToFile(sf, method, order, m, T, snapshot, runtime, k, discrHand)
     key.method = method;
     key.order  = order;
     key.m      = m;
     key.T      = T;
 
-    entry.v = v;
-    entry.t = t;
+    entry.repr = snapshot;
     entry.runtime = runtime;
     entry.k = k;
     entry.discrHand = discrHand;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+noname/plotSolutions.m	Tue Sep 22 17:35:58 2015 +0200
@@ -0,0 +1,27 @@
+function plotSolutions(filename, figename_prefix, plot_type)
+    default_arg('figename_prefix',[]);
+    default_arg('plot_type','plot')
+
+    save_figures = isempty(figename_prefix);
+
+    sf = SolutionFile(filename);
+
+    for i = 1:length(sf.keys)
+        key = sf.keys{i};
+        entry = sf.get(key);
+
+        method  = key.method;
+        order   = key.order;
+        m       = key.m;
+        T       = key.T;
+        repr       = entry.repr;
+        runtime = entry.runtime;
+        k       = entry.k;
+
+        discr = entry.discrHand(m,order);
+
+        [update, hand] = discr.setupPlot(plot_type);
+        update(repr);
+    end
+
+end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+noname/printSolutions.m	Tue Sep 22 17:35:58 2015 +0200
@@ -0,0 +1,38 @@
+function printSolutions(filename)
+    sf = SolutionFile(filename);
+
+    method  = {};
+    order   = [];
+    m       = [];
+    T       = [];
+    t       = [];
+    runtime = [];
+    k       = [];
+
+    for i = 1:length(sf.keys)
+        key = sf.keys{i};
+        entry = sf.get(key);
+
+
+        method  = [method  key.method];
+        order   = [order   key.order];
+        m       = [m       key.m];
+        T       = [T       key.T];
+        t       = [t       entry.repr.t];
+        runtime = [runtime entry.runtime];
+        k       = [k       entry.k];
+    end
+
+    methodW  = findFieldWidth('%s',method);
+    orderW   = findFieldWidth('%d',order);
+    mW       = findFieldWidth('%d',m);
+    TW       = findFieldWidth('%d',T);
+    tW       = findFieldWidth('%.3e',t);
+    runtimeW = findFieldWidth('%.3f',runtime);
+    kW       = findFieldWidth('%.4f',k);
+
+    for i = 1:length(sf.keys)
+        fprintf('[%*s: o=%-*d, m=%-*d, T=%-*d]: t=%-*.3e, runtime=%*.3f, k=%*.4f\n',methodW, method{i}, orderW,order(i),mW,m(i),TW,T(i), tW, t(i), runtimeW,runtime(i), kW, k(i));
+    end
+
+end
\ No newline at end of file