diff +scheme/bcSetup.m @ 797:5cf9fdf4c98f feature/poroelastic

Merge with feature/grids and bugfix bcSetup
author Martin Almquist <malmquist@stanford.edu>
date Thu, 26 Jul 2018 10:53:05 -0700
parents 1f6b2fb69225 6afd3dd3ed96
children 006defd0247b 57760d7088ad
line wrap: on
line diff
--- a/+scheme/bcSetup.m	Wed Jul 25 18:53:07 2018 -0700
+++ b/+scheme/bcSetup.m	Thu Jul 26 10:53:05 2018 -0700
@@ -3,46 +3,108 @@
 % Each bc is a struct with the fields
 %  * type     -- Type of boundary condition
 %  * boundary -- Boundary identifier
-%  * data     -- A function_handle with time and space coordinates as a parameters, for example f(t,x,y) for a 2D problem
+%  * data     -- A function_handle for a function which provides boundary data.(see below)
 % Also takes S_sign which modifies the sign of S, [-1,1]
-% Returns a closure matrix and a forcing function S
-function [closure, S] = bcSetup(diffOp, bc, S_sign)
+% Returns a closure matrix and a forcing function S.
+%
+% The boundary data function can either be a function of time or a function of time and space coordinates.
+% In the case where it only depends on time it should return the data as grid function for the boundary.
+% In the case where it also takes space coordinates the number of space coordinates should match the number of dimensions of the problem domain.
+% For example in the 2D case: f(t,x,y).
+function [closure, S] = bcSetup(diffOp, bcs, S_sign)
     default_arg('S_sign', 1);
-    assertType(bc, 'cell');
+    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));
-    penalties = {};
-    dataFunctions = {};
-    dataParams = {};
+    gridData = {};
+    symbolicData = {};
 
-    for i = 1:length(bc)
-        assertType(bc{i}, 'struct');
-        [localClosure, penalty] = diffOp.boundary_condition(bc{i}.boundary, bc{i}.type);
+    % Collect closures, penalties and data
+    for i = 1:length(bcs)
+        [localClosure, penalty] = diffOp.boundary_condition(bcs{i}.boundary, bcs{i}.type);
         closure = closure + localClosure;
 
-        if isempty(bc{i}.data)
+        [ok, isSym, data] = parseData(bcs{i}, penalty, diffOp.grid);
+
+        if ~ok
+            % There was no data
             continue
         end
-        assertType(bc{i}.data, 'function_handle');
 
-        coord = diffOp.grid.getBoundary(bc{i}.boundary);
-        assertNumberOfArguments(bc{i}.data, 1+size(coord,2));
-
-        penalties{end+1} = penalty;
-        dataFunctions{end+1} = bc{i}.data;
-        dataParams{end+1} = num2cell(coord ,1);
+        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(dataFunctions)
-            v = v + penalties{i}*dataFunctions{i}(t, dataParams{i}{:});
+        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 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