changeset 32:ddfb98209aa2

Fixed a bunch of problems regarding convergence and saving solutions
author Jonatan Werpers <jonatan@werpers.com>
date Tue, 29 Sep 2015 09:22:22 +0200
parents d1f9dd55a2b0
children 6db2094976a6
files +noname/calculateSolution.m +noname/convergence.m +noname/plotSolutions.m SolutionFile.m convergenceTable.m
diffstat 5 files changed, 82 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/+noname/calculateSolution.m	Mon Sep 28 21:56:02 2015 +0200
+++ b/+noname/calculateSolution.m	Tue Sep 29 09:22:22 2015 +0200
@@ -5,9 +5,10 @@
 %    order     -- order of accuracy of the approximtion
 %    T         -- time to calculate solution for
 %    input paramters m, t, order may all be vectors.
-function [] = calculateSolution(filename, discrHand, method, m, T, order)
+function [] = calculateSolution(filename, discrHand, method, m, T, order, force_flag)
+    default_arg('force_flag',false);
 
-    if exist(filename,'file')
+    if exist(filename,'file') && ~force_flag
         fprintf('File ''%s'' already exist.',filename);
         do_append = yesnoQuestion('Do you want to append to it?');
         if ~do_append
@@ -81,7 +82,7 @@
                 end
 
             end
-
+            sf.stupidSave();
         end
     end
 end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+noname/convergence.m	Tue Sep 29 09:22:22 2015 +0200
@@ -0,0 +1,50 @@
+% Reference is either a key or a function handle
+function [q, e, h] = convergence(filename, errorFunc, reference, method, order, m, T)
+    default_arg('errorFunc', @scheme.error1d);
+
+    sf = SolutionFile(filename);
+
+    analytical_ref = isa(reference,'function_handle');
+    if ~analytical_ref
+        reference = sf.get(reference);
+    end
+
+
+    % Generate convergence, error, and efficiency plots for each search key with more than one entry.
+    for i = 1:length(m)
+        key.method = method;
+        key.order = order;
+        key.m = m(i);
+        key.T = T;
+
+        entry = sf.get(key);
+
+        [e(i),h(i)] = errorForEntry(key, entry, errorFunc, reference,T);
+
+    end
+    q = convergence(e,h);
+end
+
+function [e, h] = errorForEntry(key,entry, errorFunc, reference,T)
+    v_repr = entry.repr;
+    discr = entry.discr;
+
+    % Get the solution to be compared
+    v = v_repr.v;
+
+    % Get the reference solution vector
+    if isa(reference,'function_handle');
+        x = v_repr.x;
+        v_ref = reference(x,T);
+    else
+        % Downsample the reference solution
+        x = v_repr.x;
+        x_ref = reference.x;
+
+        [~,I] = ismember(x,x_ref,'rows');
+        v_ref = reference.v(I);
+    end
+
+    e = errorFunc(discr,v, v_ref);
+    h = discr.h;
+end
\ No newline at end of file
--- a/+noname/plotSolutions.m	Mon Sep 28 21:56:02 2015 +0200
+++ b/+noname/plotSolutions.m	Tue Sep 29 09:22:22 2015 +0200
@@ -17,8 +17,7 @@
         repr    = entry.repr;
         runtime = entry.runtime;
         k       = entry.k;
-
-        discr = entry.discrHand(m,order);
+        discr = entry.discr;
 
         [update, hand] = discr.setupPlot(plot_type);
         update(repr);
--- a/SolutionFile.m	Mon Sep 28 21:56:02 2015 +0200
+++ b/SolutionFile.m	Tue Sep 29 09:22:22 2015 +0200
@@ -2,7 +2,6 @@
     properties
         filename
         matfile
-        % keyHeaders
         keys % Cell array of keys. Each key is a structure
         % entries
     end
@@ -14,18 +13,35 @@
 
             is_new_file = ~exist(filename,'file');
 
-            obj.matfile = matfile(filename,'Writable',true);
+            % obj.matfile = matfile(filename,'Writable',true);
+            fprintf('MATLAB SUCKS!!!!\n')
 
             if is_new_file
                 obj.matfile.keys = {};
                 obj.matfile.entries = {};
+            else
+                matObj = matfile(filename,'Writable',true);
+                obj.matfile.keys = matObj.keys;
+                obj.matfile.entries = matObj.entries;
             end
 
-            % obj.keyHeaders = obj.matfile.keyHeaders;
             obj.keys = obj.matfile.keys;
 
         end
 
+        function stupidSave(obj)
+            matObj = matfile(obj.filename,'Writable',true);
+
+            keys = obj.matfile.keys;
+            entries = obj.matfile.entries;
+
+            delete(obj.filename);
+
+            matObj = matfile(obj.filename,'Writable',true);
+            matObj.keys = keys;
+            matObj.entries = entries;
+        end
+
         function list(obj, show_syntax)
             default_arg('show_syntax',false);
             for i = 1:length(obj.keys)
--- a/convergenceTable.m	Mon Sep 28 21:56:02 2015 +0200
+++ b/convergenceTable.m	Tue Sep 29 09:22:22 2015 +0200
@@ -10,7 +10,6 @@
 end
 
 function plainTextTable(methodName, T, orders, m, e, q)
-    description = sprintf('method: %s\nT: %d\n');
 
 
     eW = 0;
@@ -24,6 +23,7 @@
     mW = findFieldWidth('%d',m);
     orderHeaderWidth = eW + qW + 1;
 
+    fprintf('method: %s\nT: %d\n',methodName, T);
 
     % Print order headers
     fprintf(' %*s |',mW,'')
@@ -56,11 +56,17 @@
     for i = 1:length(m)
         fprintf(' %*d |',mW,m(i));
         for j = 1:length(orders)
-            fprintf(' %*.2f %*.2f |', eW, log_e{j}(i), qW, q{j}(i));
+            if i == 1
+                fprintf(' %*.2f %*s |', eW, log_e{j}(i), qW, '');
+            else
+                fprintf(' %*.2f %*.2f |', eW, log_e{j}(i), qW, q{j}(i-1));
+            end
         end
         fprintf('\n');
     end
 
+    fprintf('\n');
+
 end
 
 function latexTable(methodName, T, orders, m, e, q)