changeset 869:d356f1a22d4f bcSetupExperiment

Start organizing the code
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 07 Sep 2018 09:19:03 +0200
parents 57760d7088ad
children fb91d12093f8
files +scheme/+bc/bcClosureSetup.m +scheme/+bc/bcForcingSetup.m +scheme/+bc/verifyFormat.m +scheme/bcSetup.m
diffstat 4 files changed, 111 insertions(+), 91 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+scheme/+bc/bcClosureSetup.m	Fri Sep 07 09:19:03 2018 +0200
@@ -0,0 +1,13 @@
+function [closure, penalties] = bcClosureSetup(diffOp, bcs)
+    assertType(bcs, 'cell');
+
+    % Setup storage arrays
+    closure = spzeros(size(diffOp));
+    penalties = cell(1, length(bcs));
+
+    % Collect closures and penalties
+    for i = 1:length(bcs)
+        [localClosure, penalties{i}] = diffOp.boundary_condition(bcs{i}.boundary, bcs{i}.type);
+        closure = closure + localClosure;
+    end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+scheme/+bc/bcForcingSetup.m	Fri Sep 07 09:19:03 2018 +0200
@@ -0,0 +1,70 @@
+function S = bcForcingSetup(diffOp, penalties, bcs, S_sign) %% SETUP in the name!!
+    default_arg('S_sign', 1);
+
+    assertType(bcs, 'cell');
+    assertIsMember(S_sign, [1, -1]);
+
+    verifyBcFormat(bcs, diffOp);
+
+    % % Setup storage arrays
+    % closure = spzeros(size(diffOp));
+    % gridData = {};
+    % symbolicData = {};
+
+    % Loop over bcs and collect data
+    for i = 1:length(bcs)
+        % [ok, isSym, data] = parseData(bcs{i}, penalties{i}, diffOp.grid)
+
+        % if ~ok
+        %     % There was no data
+        %     continue
+        % end
+
+        % if isSym
+        %     gridData{end+1} = data;
+        % else
+        %     symbolicData{end+1} = data;
+        % end
+    end
+
+
+    % Setup penalty function
+    O = spzeros(size(diffOp),1);
+    function v = S_fun(t)
+        v = O;
+        for i = 1:length(gridData)
+            v = v + gridData{i}.penalty*gridData{i}.func(t);
+        end
+
+        for i = 1:length(symbolicData)
+            v = v + symbolicData{i}.penalty*symbolicData{i}.func(t, symbolicData{i}.coords{:});
+        end
+
+        v = S_sign * v;
+    end
+    S = @S_fun;
+end
+
+function [ok, isSym, dataStruct] = parseData(bc, penalty, grid)
+    if ~isfield(bc,'data') || isempty(bc.data)
+        ok = false;
+        return
+    end
+    ok = true;
+
+    nArg = nargin(bc.data);
+
+    if nArg > 1
+        % Symbolic data
+        isSym = true;
+        coord = grid.getBoundary(bc.boundary);
+        dataStruct.penalty = penalty;
+        dataStruct.func = bc.data;
+        dataStruct.coords = num2cell(coord, 1);
+    else
+        % Grid data
+        isSym = false;
+        dataStruct.penalty = penalty;
+        dataStruct.func = bcs{i}.data;
+    end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+scheme/+bc/verifyFormat.m	Fri Sep 07 09:19:03 2018 +0200
@@ -0,0 +1,25 @@
+function verifyBcFormat(bcs, diffOp)
+    for i = 1:length(bcs)
+        assertType(bcs{i}, 'struct');
+        assertStructFields(bcs{i}, {'type', 'boundary'});
+
+        if ~isfield(bcs{i}, 'data') || isempty(bcs{i}.data)
+            continue
+        end
+
+        if ~isa(bcs{i}.data, 'function_handle')
+            error('bcs{%d}.data should be a function of time or a function of time and space',i);
+        end
+
+        b = diffOp.grid.getBoundary(bcs{i}.boundary);
+
+        dim = size(b,2);
+
+        if nargin(bcs{i}.data) == 1
+            % Grid data (only function of time)
+            assertSize(bcs{i}.data(0), 1, size(b));
+        elseif nargin(bcs{i}.data) ~= 1+dim
+           error('sbplib:scheme:bcSetup:DataWrongNumberOfArguments', 'bcs{%d}.data has the wrong number of input arguments. Must be either only time or time and space.', i);
+        end
+    end
+end
--- a/+scheme/bcSetup.m	Fri Jul 27 10:39:12 2018 -0700
+++ b/+scheme/bcSetup.m	Fri Sep 07 09:19:03 2018 +0200
@@ -16,49 +16,11 @@
     assertType(bcs, 'cell');
     assert(S_sign == 1 || S_sign == -1, 'S_sign must be either 1 or -1');
 
-    verifyBcFormat(bcs, diffOp);
-
-    % Setup storage arrays
-    closure = spzeros(size(diffOp));
-    gridData = {};
-    symbolicData = {};
-
-    % Collect closures, penalties and data
-    for i = 1:length(bcs)
-        [localClosure, penalty] = diffOp.boundary_condition(bcs{i}.boundary, bcs{i}.type);
-        closure = closure + localClosure;
-
-        [ok, isSym, data] = parseData(bcs{i}, penalty, diffOp.grid);
-
-        if ~ok
-            % There was no data
-            continue
-        end
-
-        if isSym
-            symbolicData{end+1} = data;
-        else
-            gridData{end+1} = data;
-        end
-    end
-
-    % Setup penalty function
-    O = spzeros(size(diffOp),1);
-    function v = S_fun(t)
-        v = O;
-        for i = 1:length(gridData)
-            v = v + gridData{i}.penalty*gridData{i}.func(t);
-        end
-
-        for i = 1:length(symbolicData)
-            v = v + symbolicData{i}.penalty*symbolicData{i}.func(t, symbolicData{i}.coords{:});
-        end
-
-        v = S_sign * v;
-    end
-    S = @S_fun;
+    [closure, penalties] = bcClosureSetup(diffOp, bcs);
+    S = bcForcingSetup(diffOp, penalties, bcs, S_sign);
 end
 
+%%% NOTES
 % Borde man använda eval on här??
 % Borde man dela upp bcSetup i bcSetupSymbolic(name?) och bcSetupGridData
 % och sen skriva en wrapper som sorterar och wrappar de två andra??
@@ -69,53 +31,3 @@
 % Erbjuda en separat function for att validera en bc specifikation?
 %  alltid kräva alla fields?
 %  literal struct improvement?
-
-function verifyBcFormat(bcs, diffOp)
-    for i = 1:length(bcs)
-        assertType(bcs{i}, 'struct');
-        assertStructFields(bcs{i}, {'type', 'boundary'});
-
-        if ~isfield(bcs{i}, 'data') || isempty(bcs{i}.data)
-            continue
-        end
-
-        if ~isa(bcs{i}.data, 'function_handle')
-            error('bcs{%d}.data should be a function of time or a function of time and space',i);
-        end
-
-        b = diffOp.grid.getBoundary(bcs{i}.boundary);
-
-        dim = size(b,2);
-
-        if nargin(bcs{i}.data) == 1
-            % Grid data (only function of time)
-            assertSize(bcs{i}.data(0), 1, size(b));
-        elseif nargin(bcs{i}.data) ~= 1+dim
-           error('sbplib:scheme:bcSetup:DataWrongNumberOfArguments', 'bcs{%d}.data has the wrong number of input arguments. Must be either only time or time and space.', i);
-        end
-    end
-end
-
-function [ok, isSym, dataStruct] = parseData(bc, penalty, grid)
-    if ~isfield(bc,'data') || isempty(bc.data)
-        ok = false;
-        return
-    end
-    ok = true;
-
-    nArg = nargin(bc.data);
-
-    if nArg > 1
-        % Symbolic data
-        isSym = true;
-        coord = grid.getBoundary(bc.boundary);
-        dataStruct.penalty = penalty;
-        dataStruct.func = bc.data;
-        dataStruct.coords = num2cell(coord, 1);
-    else
-        % Grid data
-        isSym = false;
-        dataStruct.penalty = penalty;
-        dataStruct.func = bcs{i}.data;
-    end
-end