changeset 717:8e4274ee6dd8 feature/utux2D

Merge with feature/poroelastic
author Martin Almquist <malmquist@stanford.edu>
date Sat, 03 Mar 2018 14:58:21 -0800
parents 2d85f17a8aec (current diff) 60eb7f46d8d9 (diff)
children 71aa5828cbbf
files diffSymfun.m
diffstat 29 files changed, 1320 insertions(+), 76 deletions(-) [+]
line wrap: on
line diff
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +blockmatrix/toMatrix.m
--- a/+blockmatrix/toMatrix.m	Mon Oct 16 21:56:12 2017 -0700
+++ b/+blockmatrix/toMatrix.m	Sat Mar 03 14:58:21 2018 -0800
@@ -12,12 +12,9 @@
 
     A = sparse(N,M);
 
-    n_ind = [0 cumsum(n)];
-    m_ind = [0 cumsum(m)];
-
     for i = 1:size(bm,1)
         for j = 1:size(bm,2)
-            if(isempty(bm{i,j}))
+            if isempty(bm{i,j})
                 bm{i,j} = sparse(n(i),m(j));
             end
         end
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +grid/Cartesian.m
--- a/+grid/Cartesian.m	Mon Oct 16 21:56:12 2017 -0700
+++ b/+grid/Cartesian.m	Sat Mar 03 14:58:21 2018 -0800
@@ -5,6 +5,7 @@
         m % Number of points in each direction
         x % Cell array of vectors with node placement for each dimension.
         h % Spacing/Scaling
+        lim % Cell array of left and right boundaries for each dimension.
     end
 
     % General d dimensional grid with n points
@@ -27,6 +28,7 @@
             end
 
             obj.h = [];
+            obj.lim = [];
         end
         % n returns the number of points in the grid
         function o = N(obj)
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +grid/TODO.txt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+grid/TODO.txt	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,1 @@
+% TODO: Rename grid package. name conflicts with built in function
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +grid/evalOn.m
--- a/+grid/evalOn.m	Mon Oct 16 21:56:12 2017 -0700
+++ b/+grid/evalOn.m	Sat Mar 03 14:58:21 2018 -0800
@@ -7,68 +7,34 @@
 function gf = evalOn(g, func)
     if ~isa(func, 'function_handle')
         % We should have a constant.
-        if size(func,2) ~= 1
-            error('grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector')
-        end
+        assert(size(func,2) == 1,'grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector');
 
         gf = repmat(func,[g.N, 1]);
         return
     end
     % func should now be a function_handle
+    assert(g.D == nargin(func),'grid:evalOn:WrongNumberOfInputs', 'The number of inputs of the function must match the dimension of the domain.')
 
-    if g.D ~= nargin(func)
-        g.D
-        nargin(func)
-        error('grid:evalOn:WrongNumberOfInputs', 'The number of inputs of the function must match the dimension of the domain.')
-    end
+    x = num2cell(g.points(),1);
+    k = numberOfComponents(func);
 
+    gf = func(x{:});
+    gf = reorderComponents(gf, k);
+end
 
-    % Get coordinates
-    x = g.points();
+% Find the number of vector components of func
+function k = numberOfComponents(func)
+    x0 = num2cell(ones(1,nargin(func)));
+    f0 = func(x0{:});
+    assert(size(f0,2) == 1, 'grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector');
+    k = length(f0);
+end
 
-    % Find the number of components
-    if size(x,1) ~= 0
-        x0 = x(1,:);
-    else
-        x0 = num2cell(ones(1,size(x,2)));
+% Reorder the components of the function to sit together
+function gf = reorderComponents(a, k)
+    N = length(a)/k;
+    gf = zeros(N*k, 1);
+    for i = 1:k
+        gf(i:k:end) = a((i-1)*N + 1 : i*N);
     end
-    
-    dim = length(x0);
-    % Evaluate f0 = func(x0(1),x0(2),...,x0(dim));
-    if(dim == 1)
-        f0 = func(x0);
-    else
-        eval_str = 'f0 = func(x0(1)';
-        for i = 2:dim
-            eval_str = [eval_str, sprintf(',x0(%d)',i)];
-        end
-        eval_str = [eval_str, ');'];
-        eval(eval_str);
-    end
-
-    % k = number of components
-    k = length(f0);
-
-    if size(f0,2) ~= 1
-        error('grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector')
-    end
-
-    % Evaluate gf = func(x(:,1),x(:,2),...,x(:,dim));
-    if(dim == 1)
-        gf = func(x);
-    else
-        eval_str = 'gf = func(x(:,1)';
-        for i = 2:dim
-            eval_str = [eval_str, sprintf(',x(:,%d)',i)];
-        end
-        eval_str = [eval_str, ');'];
-        eval(eval_str);
-    end
-    
-    % Reorganize gf
-    gf_temp = gf;
-    gf = zeros(g.N*k, 1);
-    for i = 1:k
-        gf(i:k:end) = gf_temp((i-1)*g.N + 1 : i*g.N);
-    end
-end
\ No newline at end of file
+end
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +grid/evalOnTest.m
--- a/+grid/evalOnTest.m	Mon Oct 16 21:56:12 2017 -0700
+++ b/+grid/evalOnTest.m	Sat Mar 03 14:58:21 2018 -0800
@@ -31,7 +31,7 @@
     cases = {
         {getTestGrid('1d'), @(x,y)x-y},
         {getTestGrid('2d'), @(x)x    },
-    }
+    };
 
     for i = 1:length(cases)
         g = cases{i}{1};
@@ -111,9 +111,9 @@
 
 
 function testInputErrorVectorValued(testCase)
-     in  = {
+    in  = {
         [1,2,3],
-        @(x,y)[x,-y];
+        @(x,y)[x,-y],
     };
 
     g = getTestGrid('2d');
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +multiblock/DiffOp.m
--- a/+multiblock/DiffOp.m	Mon Oct 16 21:56:12 2017 -0700
+++ b/+multiblock/DiffOp.m	Sat Mar 03 14:58:21 2018 -0800
@@ -194,6 +194,7 @@
                 p{I} = blockPenalty;
                 penalty = blockmatrix.toMatrix(p);
             else
+                % TODO: used by beam equation, should be eliminated. SHould only set one BC per call
                 for i = 1:length(blockPenalty)
                     div{2} = size(blockPenalty{i}, 2); % Penalty is a column vector
                     p = blockmatrix.zero(div);
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +noname/Animation.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+noname/Animation.m	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,75 @@
+classdef Animation < handle
+    properties
+        timeStepper
+        representationMaker
+        updaters
+    end
+
+    % add input validation
+
+    methods
+        function obj = Animation(timeStepper, representationMaker, updaters);
+            obj.timeStepper = timeStepper;
+            obj.updaters = updaters;
+            obj.representationMaker = representationMaker;
+        end
+
+        function update(obj, r)
+            for i = 1:length(obj.updaters)
+                obj.updaters{i}(r);
+            end
+            drawnow
+        end
+
+        function run(obj, tEnd, timeModifier, do_pause)
+            default_arg('do_pause', false)
+
+            function next_t = G(next_t)
+                obj.timeStepper.evolve(next_t);
+                r = obj.representationMaker(obj.timeStepper);
+                obj.update(r);
+
+                if do_pause
+                    pause
+                end
+            end
+
+            anim.animate(@G, obj.timeStepper.t, tEnd, timeModifier);
+        end
+
+        function step(obj, tEnd, do_pause)
+            default_arg('do_pause', false)
+
+            while obj.timeStepper.t < tEnd
+                obj.timeStepper.step();
+
+                r = obj.representationMaker(obj.timeStepper);
+                obj.update(r);
+
+                % TODO: Make it never go faster than a certain fram rate
+
+                if do_pause
+                    pause
+                end
+            end
+        end
+
+        function saveMovie(obj, tEnd, timeModifier, figureHandle, dirname)
+            save_frame = anim.setup_fig_mov(figureHandle, dirname);
+
+            function next_t = G(next_t)
+                obj.timeStepper.evolve(next_t);
+                r = obj.representationMaker(obj.timeStepper);
+                obj.update(r);
+
+                save_frame();
+            end
+
+            fprintf('Generating and saving frames to: ..\n')
+            anim.animate(@G, obj.timeStepper.t, tEnd, timeModifier);
+            fprintf('Generating movies...\n')
+            cmd = sprintf('bash %s/+anim/make_movie.sh %s', sbplibLocation(),dirname);
+            system(cmd);
+        end
+    end
+end
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +noname/calculateErrors.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+noname/calculateErrors.m	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,40 @@
+% [discr, trueSolution] =  schemeFactory(m)
+%     where trueSolution should be a timeSnapshot of the true solution a time T
+% T is the end time
+% m are grid size parameters.
+% N are number of timesteps to use for each gird size
+% timeOpt are options for the timeStepper
+function e = calculateErrors(schemeFactory, T, m, N, errorFun, timeOpt)
+    assertType(schemeFactory, 'function_handle');
+    assertNumberOfArguments(schemeFactory, 1);
+    assertScalar(T);
+    assert(length(m) == length(N), 'Vectors m and N must have the same length');
+    assertType(errorFun, 'function_handle');
+    assertNumberOfArguments(errorFun, 2);
+    default_arg('timeOpt');
+
+    e = [];
+    for i = 1:length(m)
+        done = timeTask('m = %3d ', m(i));
+
+        [discr, trueSolution] = schemeFactory(m(i));
+
+        timeOpt.k = T/N(i);
+        ts = discr.getTimestepper(timeOpt);
+        ts.stepTo(N(i), true);
+        approxSolution = discr.getTimeSnapshot(ts);
+
+        e(i) = errorFun(trueSolution, approxSolution);
+
+        fprintf('e = %.4e', e(i))
+        done()
+    end
+    fprintf('\n')
+end
+
+
+%% Example error function
+% u_true = grid.evalOn(dr.grid, @(x,y)trueSolution(T,x,y));
+% err = u_true-u_false;
+% e(i) = norm(err)/norm(u_true);
+% % e(i) = sqrt(err'*d.H*d.J*err/(u_true'*d.H*d.J*u_true));
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +parametrization/Ti.m
--- a/+parametrization/Ti.m	Mon Oct 16 21:56:12 2017 -0700
+++ b/+parametrization/Ti.m	Sat Mar 03 14:58:21 2018 -0800
@@ -21,16 +21,29 @@
             D = g4(0);
 
             function o = S_fun(u,v)
+                if isrow(u) && isrow(v)
+                    flipped = false;
+                else
+                    flipped = true;
+                    u = u';
+                    v = v';
+                end
+
                 x1 = g1(u);
                 x2 = g2(v);
                 x3 = g3(1-u);
                 x4 = g4(1-v);
+
                 o1 = (1-v).*x1(1,:) + u.*x2(1,:) + v.*x3(1,:) + (1-u).*x4(1,:) ...
-                    -((1-u)*(1-v).*A(1,:) + u*(1-v).*B(1,:) + u*v.*C(1,:) + (1-u)*v.*D(1,:));
+                    -((1-u).*(1-v).*A(1,:) + u.*(1-v).*B(1,:) + u.*v.*C(1,:) + (1-u).*v.*D(1,:));
                 o2 = (1-v).*x1(2,:) + u.*x2(2,:) + v.*x3(2,:) + (1-u).*x4(2,:) ...
-                    -((1-u)*(1-v).*A(2,:) + u*(1-v).*B(2,:) + u*v.*C(2,:) + (1-u)*v.*D(2,:));
+                    -((1-u).*(1-v).*A(2,:) + u.*(1-v).*B(2,:) + u.*v.*C(2,:) + (1-u).*v.*D(2,:));
 
-                o = [o1;o2];
+                if ~flipped
+                    o = [o1;o2];
+                else
+                    o = [o1'; o2'];
+                end
             end
 
             obj.S = @S_fun;
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +parametrization/TiTest.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/TiTest.m	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,52 @@
+function tests = TiTest()
+    tests = functiontests(localfunctions);
+end
+
+function testScalarInput(testCase)
+    ti = getMinimumTi();
+
+    cases = {
+        % {u, v, out},
+        {0, 0, [1; 2]},
+        {0, 1, [1; 4]},
+        {1, 0, [3; 2]},
+        {1, 1, [3; 4]},
+        {0.5, 0.5, [2; 3]},
+    };
+
+    for i = 1:length(cases)
+        u = cases{i}{1};
+        v = cases{i}{2};
+        expected = cases{i}{3};
+
+        testCase.verifyEqual(ti.S(u,v), expected, sprintf('Case: %d',i));
+    end
+end
+
+function testRowVectorInput(testCase)
+    ti = getMinimumTi();
+
+    u = [0, 0.5, 1];
+    v = [0, 0, 0.5];
+    expected = [
+        1, 2, 3;
+        2, 2, 3;
+    ];
+
+    testCase.verifyEqual(ti.S(u,v), expected);
+end
+
+function testColumnvectorInput(testCase)
+   ti = getMinimumTi();
+
+    u = [0; 0.5; 1];
+    v = [0; 0; 0.5];
+    expected = [1; 2; 3; 2; 2; 3];
+
+    testCase.verifyEqual(ti.S(u,v), expected);
+end
+
+
+function ti = getMinimumTi()
+    ti = parametrization.Ti.rectangle([1; 2], [3; 4]);
+end
\ No newline at end of file
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +sbp/+implementations/d2_variable_periodic_2.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+sbp/+implementations/d2_variable_periodic_2.m	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,50 @@
+function [H, HI, D1, D2, e_l, e_r, d1_l, d1_r] = d2_variable_periodic_2(m,h)
+    % m = number of unique grid points, i.e. h = L/m;
+
+    if(m<3)
+        error(['Operator requires at least ' num2str(3) ' grid points']);
+    end
+
+    % Norm
+    Hv = ones(m,1);
+    Hv = h*Hv;
+    H = spdiag(Hv, 0);
+    HI = spdiag(1./Hv, 0);
+
+
+    % Dummy boundary operators
+    e_l = sparse(m,1);
+    e_r = rot90(e_l, 2);
+
+    d1_l = sparse(m,1);
+    d1_r = -rot90(d1_l, 2);
+
+    % D1 operator
+    diags   = -1:1;
+    stencil = [-1/2 0 1/2];
+    D1 = stripeMatrixPeriodic(stencil, diags, m);
+    D1 = D1/h;
+
+    scheme_width = 3;
+    scheme_radius = (scheme_width-1)/2;
+    
+    r = 1:m;
+    offset = scheme_width;
+    r = r + offset;
+
+    function D2 = D2_fun(c)
+        c = [c(end-scheme_width+1:end); c; c(1:scheme_width) ];
+
+        Mm1 = -c(r-1)/2 - c(r)/2;
+        M0  =  c(r-1)/2 + c(r)   + c(r+1)/2;
+        Mp1 =            -c(r)/2 - c(r+1)/2;
+
+        vals = [Mm1,M0,Mp1];
+        diags = -scheme_radius : scheme_radius;
+        M = spdiagsVariablePeriodic(vals,diags); 
+
+        M=M/h;
+        D2=HI*(-M );
+    end
+    D2 = @D2_fun;
+end
\ No newline at end of file
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +sbp/+implementations/d2_variable_periodic_4.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+sbp/+implementations/d2_variable_periodic_4.m	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,57 @@
+function [H, HI, D1, D2, e_l, e_r, d1_l, d1_r] = d2_variable_periodic_4(m,h)
+    % m = number of unique grid points, i.e. h = L/m;
+
+    if(m<5)
+        error(['Operator requires at least ' num2str(5) ' grid points']);
+    end
+
+    % Norm
+    Hv = ones(m,1);
+    Hv = h*Hv;
+    H = spdiag(Hv, 0);
+    HI = spdiag(1./Hv, 0);
+
+
+    % Dummy boundary operators
+    e_l = sparse(m,1);
+    e_r = rot90(e_l, 2);
+
+    d1_l = sparse(m,1);
+    d1_r = -rot90(d1_l, 2);
+
+    S = d1_l*d1_l' + d1_r*d1_r';
+
+    % D1 operator
+    stencil = [1/12 -2/3 0 2/3 -1/12];
+    diags = -2:2;
+    Q = stripeMatrixPeriodic(stencil, diags, m);
+    D1 = HI*(Q - 1/2*e_l*e_l' + 1/2*e_r*e_r');
+
+
+    scheme_width = 5;
+    scheme_radius = (scheme_width-1)/2;
+    
+    r = 1:m;
+    offset = scheme_width;
+    r = r + offset;
+
+    function D2 = D2_fun(c)
+        c = [c(end-scheme_width+1:end); c; c(1:scheme_width) ];
+
+        % Note: these coefficients are for -M.
+        Mm2 = -1/8*c(r-2) + 1/6*c(r-1) - 1/8*c(r);
+        Mm1 = 1/6 *c(r-2) + 1/2*c(r-1) + 1/2*c(r) + 1/6*c(r+1);
+        M0  = -1/24*c(r-2)- 5/6*c(r-1) - 3/4*c(r) - 5/6*c(r+1) - 1/24*c(r+2);
+        Mp1  = 0 * c(r-2) + 1/6*c(r-1) + 1/2*c(r) + 1/2*c(r+1) + 1/6 *c(r+2);
+        Mp2  = 0 * c(r-2) + 0 * c(r-1) - 1/8*c(r) + 1/6*c(r+1) - 1/8 *c(r+2);
+
+        vals = -[Mm2,Mm1,M0,Mp1,Mp2];
+        diags = -scheme_radius : scheme_radius;
+        M = spdiagsVariablePeriodic(vals,diags); 
+
+        M=M/h;
+        D2=HI*(-M );
+
+    end
+    D2 = @D2_fun;
+end
\ No newline at end of file
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +sbp/+implementations/d2_variable_periodic_6.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+sbp/+implementations/d2_variable_periodic_6.m	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,58 @@
+function [H, HI, D1, D2, e_l, e_r, d1_l, d1_r] = d2_variable_periodic_6(m,h)
+    % m = number of unique grid points, i.e. h = L/m;
+
+    if(m<7)
+        error(['Operator requires at least ' num2str(7) ' grid points']);
+    end
+
+    % Norm
+    Hv = ones(m,1);
+    Hv = h*Hv;
+    H = spdiag(Hv, 0);
+    HI = spdiag(1./Hv, 0);
+
+
+    % Dummy boundary operators
+    e_l = sparse(m,1);
+    e_r = rot90(e_l, 2);
+
+    d1_l = sparse(m,1);
+    d1_r = -rot90(d1_l, 2);
+
+
+    % D1 operator
+    diags   = -3:3;
+    stencil = [-1/60 9/60 -45/60 0 45/60 -9/60 1/60];
+    D1 = stripeMatrixPeriodic(stencil, diags, m);
+    D1 = D1/h;
+
+    % D2 operator
+    scheme_width = 7;
+    scheme_radius = (scheme_width-1)/2;
+
+    r = 1:m;
+    offset = scheme_width;
+    r = r + offset;
+
+    function D2 = D2_fun(c)
+        c = [c(end-scheme_width+1:end); c; c(1:scheme_width) ];
+
+        Mm3 =  c(r-2)/0.40e2 + c(r-1)/0.40e2 - 0.11e2/0.360e3 * c(r-3) - 0.11e2/0.360e3 * c(r);
+        Mm2 =  c(r-3)/0.20e2 - 0.3e1/0.10e2 * c(r-1) + c(r+1)/0.20e2 + 0.7e1/0.40e2 * c(r) + 0.7e1/0.40e2 * c(r-2);
+        Mm1 = -c(r-3)/0.40e2 - 0.3e1/0.10e2 * c(r-2) - 0.3e1/0.10e2 * c(r+1) - c(r+2)/0.40e2 - 0.17e2/0.40e2 * c(r) - 0.17e2/0.40e2 * c(r-1);
+        M0 =  c(r-3)/0.180e3 + c(r-2)/0.8e1 + 0.19e2/0.20e2 * c(r-1) + 0.19e2/0.20e2 * c(r+1) + c(r+2)/0.8e1 + c(r+3)/0.180e3 + 0.101e3/0.180e3 * c(r);
+        Mp1 = -c(r-2)/0.40e2 - 0.3e1/0.10e2 * c(r-1) - 0.3e1/0.10e2 * c(r+2) - c(r+3)/0.40e2 - 0.17e2/0.40e2 * c(r) - 0.17e2/0.40e2 * c(r+1);
+        Mp2 =  c(r-1)/0.20e2 - 0.3e1/0.10e2 * c(r+1) + c(r+3)/0.20e2 + 0.7e1/0.40e2 * c(r) + 0.7e1/0.40e2 * c(r+2);
+        Mp3 =  c(r+1)/0.40e2 + c(r+2)/0.40e2 - 0.11e2/0.360e3 * c(r) - 0.11e2/0.360e3 * c(r+3);
+
+        vals = [Mm3,Mm2,Mm1,M0,Mp1,Mp2,Mp3];
+        diags = -scheme_radius : scheme_radius;
+        M = spdiagsVariablePeriodic(vals,diags); 
+
+        M=M/h;
+        D2=HI*(-M );
+    end
+    D2 = @D2_fun;
+
+    
+end
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +sbp/D2Variable.m
--- a/+sbp/D2Variable.m	Mon Oct 16 21:56:12 2017 -0700
+++ b/+sbp/D2Variable.m	Sat Mar 03 14:58:21 2018 -0800
@@ -26,22 +26,39 @@
             obj.x = linspace(x_l,x_r,m)';
 
             switch order
+
+                case 6
+
+                    [obj.H, obj.HI, obj.D1, obj.D2, ...
+                    ~, obj.e_l, obj.e_r, ~, ~, ~, ~, ~,...
+                     obj.d1_l, obj.d1_r] = ...
+                        sbp.implementations.d4_variable_6(m, obj.h);
+                    obj.borrowing.M.d1 = 0.1878;
+                    obj.borrowing.R.delta_D = 0.3696;
+                    % Borrowing e^T*D1 - d1 from R
+
                 case 4
                     [obj.H, obj.HI, obj.D1, obj.D2, obj.e_l,...
                         obj.e_r, obj.d1_l, obj.d1_r] = ...
                         sbp.implementations.d2_variable_4(m,obj.h);
                     obj.borrowing.M.d1 = 0.2505765857;
+
+                    obj.borrowing.R.delta_D = 0.577587500088313;
+                    % Borrowing e^T*D1 - d1 from R
                 case 2
                     [obj.H, obj.HI, obj.D1, obj.D2, obj.e_l,...
                         obj.e_r, obj.d1_l, obj.d1_r] = ...
                         sbp.implementations.d2_variable_2(m,obj.h);
                     obj.borrowing.M.d1 = 0.3636363636; 
                     % Borrowing const taken from Virta 2014
+
+                    obj.borrowing.R.delta_D = 1.000000538455350;
+                    % Borrowing e^T*D1 - d1 from R
                     
                 otherwise
                     error('Invalid operator order %d.',order);
             end
-
+            obj.borrowing.H11 = obj.H(1,1)/obj.h; % First element in H/h,
             obj.m = m;
             obj.M = [];
         end
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +sbp/D2VariablePeriodic.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+sbp/D2VariablePeriodic.m	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,71 @@
+classdef D2VariablePeriodic < sbp.OpSet
+    properties
+        D1 % SBP operator approximating first derivative
+        H % Norm matrix
+        HI % H^-1
+        Q % Skew-symmetric matrix
+        e_l % Left boundary operator
+        e_r % Right boundary operator
+        D2 % SBP operator for second derivative
+        M % Norm matrix, second derivative
+        d1_l % Left boundary first derivative
+        d1_r % Right boundary first derivative
+        m % Number of grid points.
+        h % Step size
+        x % grid
+        borrowing % Struct with borrowing limits for different norm matrices
+    end
+
+    methods
+        function obj = D2VariablePeriodic(m,lim,order)
+
+            x_l = lim{1};
+            x_r = lim{2};
+            L = x_r-x_l;
+            obj.h = L/m;
+            x = linspace(x_l,x_r,m+1)';
+            obj.x = x(1:end-1);
+
+            switch order
+
+                case 6
+                    [obj.H, obj.HI, obj.D1, obj.D2, obj.e_l,...
+                        obj.e_r, obj.d1_l, obj.d1_r] = ...
+                        sbp.implementations.d2_variable_periodic_6(m,obj.h);
+                    obj.borrowing.M.d1 = 0.1878;
+                    obj.borrowing.R.delta_D = 0.3696;
+                    % Borrowing e^T*D1 - d1 from R
+
+                case 4
+                    [obj.H, obj.HI, obj.D1, obj.D2, obj.e_l,...
+                        obj.e_r, obj.d1_l, obj.d1_r] = ...
+                        sbp.implementations.d2_variable_periodic_4(m,obj.h);
+                    obj.borrowing.M.d1 = 0.2505765857;
+
+                    obj.borrowing.R.delta_D = 0.577587500088313;
+                    % Borrowing e^T*D1 - d1 from R
+                case 2
+                    [obj.H, obj.HI, obj.D1, obj.D2, obj.e_l,...
+                        obj.e_r, obj.d1_l, obj.d1_r] = ...
+                        sbp.implementations.d2_variable_periodic_2(m,obj.h);
+                    obj.borrowing.M.d1 = 0.3636363636; 
+                    % Borrowing const taken from Virta 2014
+
+                    obj.borrowing.R.delta_D = 1.000000538455350;
+                    % Borrowing e^T*D1 - d1 from R
+                    
+                otherwise
+                    error('Invalid operator order %d.',order);
+            end
+            obj.borrowing.H11 = obj.H(1,1)/obj.h; % First element in H/h,
+
+            obj.m = m;
+            obj.M = [];
+        end
+        function str = string(obj)
+            str = [class(obj) '_' num2str(obj.order)];
+        end
+    end
+
+
+end
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +scheme/Beam.m
--- a/+scheme/Beam.m	Mon Oct 16 21:56:12 2017 -0700
+++ b/+scheme/Beam.m	Sat Mar 03 14:58:21 2018 -0800
@@ -126,6 +126,44 @@
                     penalty{1} = -obj.Hi*tau;
                     penalty{1} = -obj.Hi*sig;
 
+                case 'e'
+                    alpha = obj.alpha;
+                    tuning = 1.1;
+
+                    tau1 = tuning * alpha/delt;
+                    tau4 = s*alpha;
+
+                    tau = tau1*e+tau4*d3;
+
+                    closure = obj.Hi*tau*e';
+                    penalty = -obj.Hi*tau;
+                case 'd1'
+                    alpha = obj.alpha;
+
+                    tuning = 1.1;
+
+                    sig2 = tuning * alpha/gamm;
+                    sig3 = -s*alpha;
+
+                    sig = sig2*d1+sig3*d2;
+
+                    closure = obj.Hi*sig*d1';
+                    penalty = -obj.Hi*sig;
+
+                case 'd2'
+                    a = obj.alpha;
+
+                    tau =  s*a*d1;
+
+                    closure = obj.Hi*tau*d2';
+                    penalty = -obj.Hi*tau;
+                case 'd3'
+                    a = obj.alpha;
+
+                    sig = -s*a*e;
+
+                    closure = obj.Hi*sig*d3';
+                    penalty = -obj.Hi*sig;
 
                 otherwise % Unknown, boundary condition
                     error('No such boundary condition: type = %s',type);
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +scheme/Elastic2dVariable.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+scheme/Elastic2dVariable.m	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,420 @@
+classdef Elastic2dVariable < scheme.Scheme
+
+% Discretizes the elastic wave equation:
+% rho u_{i,tt} = di lambda dj u_j + dj mu di u_j + dj mu dj u_i 
+% opSet should be cell array of opSets, one per dimension. This
+% is useful if we have periodic BC in one direction.
+
+    properties
+        m % Number of points in each direction, possibly a vector
+        h % Grid spacing
+
+        grid
+        dim
+
+        order % Order of accuracy for the approximation
+
+        % Diagonal matrices for varible coefficients
+        LAMBDA % Variable coefficient, related to dilation
+        MU     % Shear modulus, variable coefficient
+        RHO, RHOi % Density, variable
+
+        D % Total operator
+        D1 % First derivatives
+
+        % Second derivatives
+        D2_lambda
+        D2_mu
+
+        % Traction operators used for BC
+        T_l, T_r
+        tau_l, tau_r
+
+        H, Hi % Inner products
+        phi % Borrowing constant for (d1 - e^T*D1) from R
+        gamma % Borrowing constant for d1 from M
+        H11 % First element of H
+        e_l, e_r
+        d1_l, d1_r % Normal derivatives at the boundary
+        E % E{i}^T picks out component i
+        
+        H_boundary % Boundary inner products
+
+        % Kroneckered norms and coefficients
+        RHOi_kron
+        Hi_kron
+    end
+
+    methods
+
+        function obj = Elastic2dVariable(g ,order, lambda_fun, mu_fun, rho_fun, opSet)
+            default_arg('opSet',{@sbp.D2Variable, @sbp.D2Variable});
+            default_arg('lambda_fun', @(x,y) 0*x+1);
+            default_arg('mu_fun', @(x,y) 0*x+1);
+            default_arg('rho_fun', @(x,y) 0*x+1);
+            dim = 2;
+
+            assert(isa(g, 'grid.Cartesian'))
+
+            lambda = grid.evalOn(g, lambda_fun);
+            mu = grid.evalOn(g, mu_fun);
+            rho = grid.evalOn(g, rho_fun);
+            m = g.size();
+            m_tot = g.N();
+
+            h = g.scaling();
+            lim = g.lim;
+
+            % 1D operators
+            ops = cell(dim,1);
+            for i = 1:dim
+                ops{i} = opSet{i}(m(i), lim{i}, order);
+            end
+
+            % Borrowing constants
+            for i = 1:dim
+                beta = ops{i}.borrowing.R.delta_D;
+                obj.H11{i} = ops{i}.borrowing.H11;
+                obj.phi{i} = beta/obj.H11{i};
+                obj.gamma{i} = ops{i}.borrowing.M.d1;
+            end
+
+            I = cell(dim,1);
+            D1 = cell(dim,1);
+            D2 = cell(dim,1);
+            H = cell(dim,1);
+            Hi = cell(dim,1);
+            e_l = cell(dim,1);
+            e_r = cell(dim,1);
+            d1_l = cell(dim,1);
+            d1_r = cell(dim,1);
+
+            for i = 1:dim
+                I{i} = speye(m(i));
+                D1{i} = ops{i}.D1;
+                D2{i} = ops{i}.D2;
+                H{i} =  ops{i}.H;
+                Hi{i} = ops{i}.HI;
+                e_l{i} = ops{i}.e_l;
+                e_r{i} = ops{i}.e_r;
+                d1_l{i} = ops{i}.d1_l;
+                d1_r{i} = ops{i}.d1_r;
+            end
+
+            %====== Assemble full operators ========
+            LAMBDA = spdiag(lambda);
+            obj.LAMBDA = LAMBDA;
+            MU = spdiag(mu);
+            obj.MU = MU;
+            RHO = spdiag(rho);
+            obj.RHO = RHO;
+            obj.RHOi = inv(RHO);
+
+            obj.D1 = cell(dim,1);
+            obj.D2_lambda = cell(dim,1);
+            obj.D2_mu = cell(dim,1);
+            obj.e_l = cell(dim,1);
+            obj.e_r = cell(dim,1);
+            obj.d1_l = cell(dim,1);
+            obj.d1_r = cell(dim,1);
+
+            % D1
+            obj.D1{1} = kron(D1{1},I{2});
+            obj.D1{2} = kron(I{1},D1{2});
+
+            % Boundary operators
+            obj.e_l{1} = kron(e_l{1},I{2});
+            obj.e_l{2} = kron(I{1},e_l{2});
+            obj.e_r{1} = kron(e_r{1},I{2});
+            obj.e_r{2} = kron(I{1},e_r{2});
+
+            obj.d1_l{1} = kron(d1_l{1},I{2});
+            obj.d1_l{2} = kron(I{1},d1_l{2});
+            obj.d1_r{1} = kron(d1_r{1},I{2});
+            obj.d1_r{2} = kron(I{1},d1_r{2});
+
+            % D2
+            for i = 1:dim
+                obj.D2_lambda{i} = sparse(m_tot);
+                obj.D2_mu{i} = sparse(m_tot);
+            end
+            ind = grid.funcToMatrix(g, 1:m_tot);
+
+            for i = 1:m(2)
+                D_lambda = D2{1}(lambda(ind(:,i)));
+                D_mu = D2{1}(mu(ind(:,i)));
+
+                p = ind(:,i);
+                obj.D2_lambda{1}(p,p) = D_lambda;
+                obj.D2_mu{1}(p,p) = D_mu;
+            end
+
+            for i = 1:m(1)
+                D_lambda = D2{2}(lambda(ind(i,:)));
+                D_mu = D2{2}(mu(ind(i,:)));
+
+                p = ind(i,:);
+                obj.D2_lambda{2}(p,p) = D_lambda;
+                obj.D2_mu{2}(p,p) = D_mu;
+            end
+
+            % Quadratures
+            obj.H = kron(H{1},H{2});
+            obj.Hi = inv(obj.H);
+            obj.H_boundary = cell(dim,1);
+            obj.H_boundary{1} = H{2};
+            obj.H_boundary{2} = H{1};
+
+            % E{i}^T picks out component i.
+            E = cell(dim,1);
+            I = speye(m_tot,m_tot);
+            for i = 1:dim
+                e = sparse(dim,1);
+                e(i) = 1;
+                E{i} = kron(I,e);
+            end
+            obj.E = E;
+
+            % Differentiation matrix D (without SAT)
+            D2_lambda = obj.D2_lambda;
+            D2_mu = obj.D2_mu;
+            D1 = obj.D1;
+            D = sparse(dim*m_tot,dim*m_tot);
+            d = @kroneckerDelta;    % Kronecker delta
+            db = @(i,j) 1-d(i,j); % Logical not of Kronecker delta
+            for i = 1:dim
+                for j = 1:dim
+                    D = D + E{i}*inv(RHO)*( d(i,j)*D2_lambda{i}*E{j}' +...
+                                            db(i,j)*D1{i}*LAMBDA*D1{j}*E{j}' ...
+                                          );
+                    D = D + E{i}*inv(RHO)*( d(i,j)*D2_mu{i}*E{j}' +...
+                                            db(i,j)*D1{j}*MU*D1{i}*E{j}' + ...
+                                            D2_mu{j}*E{i}' ...
+                                          );
+                end
+            end
+            obj.D = D;
+            %=========================================%
+
+            % Numerical traction operators for BC.
+            % Because d1 =/= e0^T*D1, the numerical tractions are different
+            % at every boundary.
+            T_l = cell(dim,1);
+            T_r = cell(dim,1);
+            tau_l = cell(dim,1);
+            tau_r = cell(dim,1);
+            % tau^{j}_i = sum_k T^{j}_{ik} u_k
+
+            d1_l = obj.d1_l;
+            d1_r = obj.d1_r;
+            e_l = obj.e_l;
+            e_r = obj.e_r;
+            D1 = obj.D1;
+
+            % Loop over boundaries
+            for j = 1:dim
+                T_l{j} = cell(dim,dim);
+                T_r{j} = cell(dim,dim);
+                tau_l{j} = cell(dim,1);
+                tau_r{j} = cell(dim,1);
+
+                % Loop over components
+                for i = 1:dim
+                    tau_l{j}{i} = sparse(m_tot,dim*m_tot);
+                    tau_r{j}{i} = sparse(m_tot,dim*m_tot);
+                    for k = 1:dim
+                        T_l{j}{i,k} = ... 
+                        -d(i,j)*LAMBDA*(d(i,k)*e_l{k}*d1_l{k}' + db(i,k)*D1{k})...
+                        -d(j,k)*MU*(d(i,j)*e_l{i}*d1_l{i}' + db(i,j)*D1{i})... 
+                        -d(i,k)*MU*e_l{j}*d1_l{j}';
+
+                        T_r{j}{i,k} = ... 
+                        d(i,j)*LAMBDA*(d(i,k)*e_r{k}*d1_r{k}' + db(i,k)*D1{k})...
+                        +d(j,k)*MU*(d(i,j)*e_r{i}*d1_r{i}' + db(i,j)*D1{i})... 
+                        +d(i,k)*MU*e_r{j}*d1_r{j}';
+
+                        tau_l{j}{i} = tau_l{j}{i} + T_l{j}{i,k}*E{k}';
+                        tau_r{j}{i} = tau_r{j}{i} + T_r{j}{i,k}*E{k}';
+                    end
+
+                end
+            end
+            obj.T_l = T_l;
+            obj.T_r = T_r;
+            obj.tau_l = tau_l;
+            obj.tau_r = tau_r;
+
+            % Kroneckered norms and coefficients
+            I_dim = speye(dim);
+            obj.RHOi_kron = kron(obj.RHOi, I_dim);
+            obj.Hi_kron = kron(obj.Hi, I_dim);
+
+            % Misc.
+            obj.m = m;
+            obj.h = h;
+            obj.order = order;
+            obj.grid = g;
+            obj.dim = dim;
+
+        end
+
+
+        % Closure functions return the operators applied to the own domain to close the boundary
+        % Penalty functions return the operators to force the solution. In the case of an interface it returns the operator applied to the other doamin.
+        %       boundary            is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'.
+        %       type                is a cell array of strings specifying the type of boundary condition for each component.
+        %       data                is a function returning the data that should be applied at the boundary.
+        %       neighbour_scheme    is an instance of Scheme that should be interfaced to.
+        %       neighbour_boundary  is a string specifying which boundary to interface to.
+        function [closure, penalty] = boundary_condition(obj, boundary, type, parameter)
+            default_arg('type',{'free','free'});
+            default_arg('parameter', []);
+
+            % j is the coordinate direction of the boundary
+            % nj: outward unit normal component. 
+            % nj = -1 for west, south, bottom boundaries
+            % nj = 1  for east, north, top boundaries
+            [j, nj] = obj.get_boundary_number(boundary);
+            switch nj
+            case 1
+                e = obj.e_r;
+                d = obj.d1_r;
+                tau = obj.tau_r{j};
+                T = obj.T_r{j};
+            case -1
+                e = obj.e_l;
+                d = obj.d1_l;
+                tau = obj.tau_l{j};
+                T = obj.T_l{j};
+            end
+
+            E = obj.E;
+            Hi = obj.Hi;
+            H_gamma = obj.H_boundary{j};
+            LAMBDA = obj.LAMBDA;
+            MU = obj.MU;
+            RHOi = obj.RHOi;
+
+            dim = obj.dim;
+            m_tot = obj.grid.N();
+
+            RHOi_kron = obj.RHOi_kron;
+            Hi_kron = obj.Hi_kron;
+
+            % Preallocate
+            closure = sparse(dim*m_tot, dim*m_tot);
+            penalty = cell(dim,1);
+            for k = 1:dim
+                penalty{k} = sparse(dim*m_tot, m_tot/obj.m(j));
+            end
+
+            % Loop over components that we (potentially) have different BC on
+            for k = 1:dim
+                switch type{k}
+
+                % Dirichlet boundary condition
+                case {'D','d','dirichlet','Dirichlet'}
+
+                    tuning = 1.2;
+                    phi = obj.phi{j};
+                    h = obj.h(j);
+                    h11 = obj.H11{j}*h;
+                    gamma = obj.gamma{j};
+
+                    a_lambda = dim/h11 + 1/(h11*phi);
+                    a_mu_i = 2/(gamma*h);
+                    a_mu_ij = 2/h11 + 1/(h11*phi);
+
+                    d = @kroneckerDelta;  % Kronecker delta
+                    db = @(i,j) 1-d(i,j); % Logical not of Kronecker delta
+                    alpha = @(i,j) tuning*( d(i,j)* a_lambda*LAMBDA ...
+                                          + d(i,j)* a_mu_i*MU ...
+                                          + db(i,j)*a_mu_ij*MU ); 
+
+                    % Loop over components that Dirichlet penalties end up on
+                    for i = 1:dim
+                        C = T{k,i};
+                        A = -d(i,k)*alpha(i,j);
+                        B = A + C;
+                        closure = closure + E{i}*RHOi*Hi*B'*e{j}*H_gamma*(e{j}'*E{k}' ); 
+                        penalty{k} = penalty{k} - E{i}*RHOi*Hi*B'*e{j}*H_gamma;
+                    end 
+
+                % Free boundary condition
+                case {'F','f','Free','free','traction','Traction','t','T'}
+                        closure = closure - E{k}*RHOi*Hi*e{j}*H_gamma* (e{j}'*tau{k} ); 
+                        penalty{k} = penalty{k} + E{k}*RHOi*Hi*e{j}*H_gamma;
+
+                % Unknown boundary condition
+                otherwise
+                    error('No such boundary condition: type = %s',type);
+                end
+            end
+        end
+
+        function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary)
+            % u denotes the solution in the own domain
+            % v denotes the solution in the neighbour domain
+            tuning = 1.2;
+            % tuning = 20.2;
+            error('Interface not implemented');
+        end
+
+        % Returns the coordinate number and outward normal component for the boundary specified by the string boundary.
+        function [j, nj] = get_boundary_number(obj, boundary)
+
+            switch boundary
+                case {'w','W','west','West', 'e', 'E', 'east', 'East'}
+                    j = 1;
+                case {'s','S','south','South', 'n', 'N', 'north', 'North'}
+                    j = 2;
+                otherwise
+                    error('No such boundary: boundary = %s',boundary);
+            end
+
+            switch boundary
+                case {'w','W','west','West','s','S','south','South'}
+                    nj = -1;
+                case {'e', 'E', 'east', 'East','n', 'N', 'north', 'North'}
+                    nj = 1;
+            end
+        end
+
+        % Returns the coordinate number and outward normal component for the boundary specified by the string boundary.
+        function [return_op] = get_boundary_operator(obj, op, boundary)
+
+            switch boundary
+                case {'w','W','west','West', 'e', 'E', 'east', 'East'}
+                    j = 1;
+                case {'s','S','south','South', 'n', 'N', 'north', 'North'}
+                    j = 2;
+                otherwise
+                    error('No such boundary: boundary = %s',boundary);
+            end
+
+            switch op
+                case 'e'
+                    switch boundary
+                        case {'w','W','west','West','s','S','south','South'}
+                            return_op = obj.e_l{j};
+                        case {'e', 'E', 'east', 'East','n', 'N', 'north', 'North'}
+                            return_op = obj.e_r{j};
+                    end
+                case 'd'
+                    switch boundary
+                        case {'w','W','west','West','s','S','south','South'}
+                            return_op = obj.d1_l{j};
+                        case {'e', 'E', 'east', 'East','n', 'N', 'north', 'North'}
+                            return_op = obj.d1_r{j};
+                    end
+                otherwise
+                    error(['No such operator: operatr = ' op]);
+            end
+
+        end
+
+        function N = size(obj)
+            N = prod(obj.m);
+        end
+    end
+end
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +scheme/Heat2dVariable.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+scheme/Heat2dVariable.m	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,262 @@
+classdef Heat2dVariable < scheme.Scheme
+
+% Discretizes the Laplacian with variable coefficent,
+% In the Heat equation way (i.e., the discretization matrix is not necessarily 
+% symmetric)
+% u_t = div * (kappa * grad u ) 
+% opSet should be cell array of opSets, one per dimension. This
+% is useful if we have periodic BC in one direction.
+
+    properties
+        m % Number of points in each direction, possibly a vector
+        h % Grid spacing
+
+        grid
+        dim
+
+        order % Order of accuracy for the approximation
+
+        % Diagonal matrix for variable coefficients
+        KAPPA % Variable coefficient
+
+        D % Total operator
+        D1 % First derivatives
+
+        % Second derivatives
+        D2_kappa
+
+        H, Hi % Inner products
+        e_l, e_r
+        d1_l, d1_r % Normal derivatives at the boundary
+        
+        H_boundary % Boundary inner products
+
+    end
+
+    methods
+
+        function obj = Heat2dVariable(g ,order, kappa_fun, opSet)
+            default_arg('opSet',{@sbp.D2Variable, @sbp.D2Variable});
+            default_arg('kappa_fun', @(x,y) 0*x+1);
+            dim = 2;
+
+            assert(isa(g, 'grid.Cartesian'))
+
+            kappa = grid.evalOn(g, kappa_fun);
+            m = g.size();
+            m_tot = g.N();
+
+            h = g.scaling();
+            lim = g.lim;
+
+            % 1D operators
+            ops = cell(dim,1);
+            for i = 1:dim
+                ops{i} = opSet{i}(m(i), lim{i}, order);
+            end
+
+            I = cell(dim,1);
+            D1 = cell(dim,1);
+            D2 = cell(dim,1);
+            H = cell(dim,1);
+            Hi = cell(dim,1);
+            e_l = cell(dim,1);
+            e_r = cell(dim,1);
+            d1_l = cell(dim,1);
+            d1_r = cell(dim,1);
+
+            for i = 1:dim
+                I{i} = speye(m(i));
+                D1{i} = ops{i}.D1;
+                D2{i} = ops{i}.D2;
+                H{i} =  ops{i}.H;
+                Hi{i} = ops{i}.HI;
+                e_l{i} = ops{i}.e_l;
+                e_r{i} = ops{i}.e_r;
+                d1_l{i} = ops{i}.d1_l;
+                d1_r{i} = ops{i}.d1_r;
+            end
+
+            %====== Assemble full operators ========
+            KAPPA = spdiag(kappa);
+            obj.KAPPA = KAPPA;
+
+            obj.D1 = cell(dim,1);
+            obj.D2_kappa = cell(dim,1);
+            obj.e_l = cell(dim,1);
+            obj.e_r = cell(dim,1);
+            obj.d1_l = cell(dim,1);
+            obj.d1_r = cell(dim,1);
+
+            % D1
+            obj.D1{1} = kron(D1{1},I{2});
+            obj.D1{2} = kron(I{1},D1{2});
+
+            % Boundary operators
+            obj.e_l{1} = kron(e_l{1},I{2});
+            obj.e_l{2} = kron(I{1},e_l{2});
+            obj.e_r{1} = kron(e_r{1},I{2});
+            obj.e_r{2} = kron(I{1},e_r{2});
+
+            obj.d1_l{1} = kron(d1_l{1},I{2});
+            obj.d1_l{2} = kron(I{1},d1_l{2});
+            obj.d1_r{1} = kron(d1_r{1},I{2});
+            obj.d1_r{2} = kron(I{1},d1_r{2});
+
+            % D2
+            for i = 1:dim
+                obj.D2_kappa{i} = sparse(m_tot);
+            end
+            ind = grid.funcToMatrix(g, 1:m_tot);
+
+            for i = 1:m(2)
+                D_kappa = D2{1}(kappa(ind(:,i)));
+                p = ind(:,i);
+                obj.D2_kappa{1}(p,p) = D_kappa;
+            end
+
+            for i = 1:m(1)
+                D_kappa = D2{2}(kappa(ind(i,:)));
+                p = ind(i,:);
+                obj.D2_kappa{2}(p,p) = D_kappa;
+            end
+
+            % Quadratures
+            obj.H = kron(H{1},H{2});
+            obj.Hi = inv(obj.H);
+            obj.H_boundary = cell(dim,1);
+            obj.H_boundary{1} = H{2};
+            obj.H_boundary{2} = H{1};
+
+            % Differentiation matrix D (without SAT)
+            D2_kappa = obj.D2_kappa;
+            D1 = obj.D1;
+            D = sparse(m_tot,m_tot);
+            for i = 1:dim
+                D = D + D2_kappa{i};
+            end
+            obj.D = D;
+            %=========================================%
+
+            % Misc.
+            obj.m = m;
+            obj.h = h;
+            obj.order = order;
+            obj.grid = g;
+            obj.dim = dim;
+
+        end
+
+
+        % Closure functions return the operators applied to the own domain to close the boundary
+        % Penalty functions return the operators to force the solution. In the case of an interface it returns the operator applied to the other doamin.
+        %       boundary            is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'.
+        %       type                is a string specifying the type of boundary condition.
+        %       data                is a function returning the data that should be applied at the boundary.
+        %       neighbour_scheme    is an instance of Scheme that should be interfaced to.
+        %       neighbour_boundary  is a string specifying which boundary to interface to.
+        function [closure, penalty] = boundary_condition(obj, boundary, type, parameter)
+            default_arg('type','Neumann');
+            default_arg('parameter', []);
+
+            % j is the coordinate direction of the boundary
+            % nj: outward unit normal component. 
+            % nj = -1 for west, south, bottom boundaries
+            % nj = 1  for east, north, top boundaries
+            [j, nj] = obj.get_boundary_number(boundary);
+            switch nj
+            case 1
+                e = obj.e_r;
+                d = obj.d1_r;
+            case -1
+                e = obj.e_l;
+                d = obj.d1_l;
+            end
+
+            Hi = obj.Hi;
+            H_gamma = obj.H_boundary{j};
+            KAPPA = obj.KAPPA;
+            kappa_gamma = e{j}'*KAPPA*e{j}; 
+
+            switch type
+
+            % Dirichlet boundary condition
+            case {'D','d','dirichlet','Dirichlet'}
+                    closure = -nj*Hi*d{j}*kappa_gamma*H_gamma*(e{j}' ); 
+                    penalty =  nj*Hi*d{j}*kappa_gamma*H_gamma;
+
+            % Free boundary condition
+            case {'N','n','neumann','Neumann'}
+                    closure = -nj*Hi*e{j}*kappa_gamma*H_gamma*(d{j}' ); 
+                    penalty =  nj*Hi*e{j}*kappa_gamma*H_gamma; 
+
+            % Unknown boundary condition
+            otherwise
+                error('No such boundary condition: type = %s',type);
+            end
+        end
+
+        function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary)
+            % u denotes the solution in the own domain
+            % v denotes the solution in the neighbour domain
+            error('Interface not implemented');
+        end
+
+        % Returns the coordinate number and outward normal component for the boundary specified by the string boundary.
+        function [j, nj] = get_boundary_number(obj, boundary)
+
+            switch boundary
+                case {'w','W','west','West', 'e', 'E', 'east', 'East'}
+                    j = 1;
+                case {'s','S','south','South', 'n', 'N', 'north', 'North'}
+                    j = 2;
+                otherwise
+                    error('No such boundary: boundary = %s',boundary);
+            end
+
+            switch boundary
+                case {'w','W','west','West','s','S','south','South'}
+                    nj = -1;
+                case {'e', 'E', 'east', 'East','n', 'N', 'north', 'North'}
+                    nj = 1;
+            end
+        end
+
+        % Returns the coordinate number and outward normal component for the boundary specified by the string boundary.
+        function [return_op] = get_boundary_operator(obj, op, boundary)
+
+            switch boundary
+                case {'w','W','west','West', 'e', 'E', 'east', 'East'}
+                    j = 1;
+                case {'s','S','south','South', 'n', 'N', 'north', 'North'}
+                    j = 2;
+                otherwise
+                    error('No such boundary: boundary = %s',boundary);
+            end
+
+            switch op
+                case 'e'
+                    switch boundary
+                        case {'w','W','west','West','s','S','south','South'}
+                            return_op = obj.e_l{j};
+                        case {'e', 'E', 'east', 'East','n', 'N', 'north', 'North'}
+                            return_op = obj.e_r{j};
+                    end
+                case 'd'
+                    switch boundary
+                        case {'w','W','west','West','s','S','south','South'}
+                            return_op = obj.d1_l{j};
+                        case {'e', 'E', 'east', 'East','n', 'N', 'north', 'North'}
+                            return_op = obj.d1_r{j};
+                    end
+                otherwise
+                    error(['No such operator: operatr = ' op]);
+            end
+
+        end
+
+        function N = size(obj)
+            N = prod(obj.m);
+        end
+    end
+end
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +scheme/TODO.txt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+scheme/TODO.txt	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,1 @@
+% TODO: Rename package and abstract class to diffOp
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +scheme/bcSetup.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+scheme/bcSetup.m	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,48 @@
+% function [closure, S] = bcSetup(diffOp, bc)
+% Takes a diffOp and a cell array of boundary condition definitions.
+% 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
+% 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)
+    default_arg('S_sign', 1);
+    assertType(bc, 'cell');
+    assert(S_sign == 1 || S_sign == -1, 'S_sign must be either 1 or -1');
+
+
+    closure = spzeros(size(diffOp));
+    penalties = {};
+    dataFunctions = {};
+    dataParams = {};
+
+    for i = 1:length(bc)
+        assertType(bc{i}, 'struct');
+        [localClosure, penalty] = diffOp.boundary_condition(bc{i}.boundary, bc{i}.type);
+        closure = closure + localClosure;
+
+        if isempty(bc{i}.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);
+    end
+
+    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}{:});
+        end
+
+        v = S_sign * v;
+    end
+    S = @S_fun;
+end
diff -r 2d85f17a8aec -r 8e4274ee6dd8 +time/Timestepper.m
--- a/+time/Timestepper.m	Mon Oct 16 21:56:12 2017 -0700
+++ b/+time/Timestepper.m	Sat Mar 03 14:58:21 2018 -0800
@@ -62,6 +62,7 @@
 
 
         function [v, t] = stepTo(obj, n, progress_bar)
+            assertScalar(n);
             default_arg('progress_bar',false);
 
             [v, t] = obj.stepN(n-obj.n, progress_bar);
diff -r 2d85f17a8aec -r 8e4274ee6dd8 assertScalar.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/assertScalar.m	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,5 @@
+function assertScalar(obj)
+    if ~isscalar(obj)
+        error('sbplib:assertScalar:notScalar', '"%s" must be scalar, found size "%s"', inputname(1), toString(size(obj)));
+    end
+end
diff -r 2d85f17a8aec -r 8e4274ee6dd8 diffSymfun.m
--- a/diffSymfun.m	Mon Oct 16 21:56:12 2017 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,7 +0,0 @@
-% Differentiates a symbolic function like diff does, but keeps the function as a symfun
-function g = diffSymfun(f, varargin)
-    assertType(f, 'symfun');
-
-    args = argnames(f);
-    g = symfun(diff(f,varargin{:}), args);
-end
diff -r 2d85f17a8aec -r 8e4274ee6dd8 kroneckerDelta.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kroneckerDelta.m	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,6 @@
+function d = kroneckerDelta(i,j)
+
+d = 0;
+if i==j
+	d = 1;
+end
\ No newline at end of file
diff -r 2d85f17a8aec -r 8e4274ee6dd8 sbplibLocation.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sbplibLocation.m	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,4 @@
+function location = sbplibLocation()
+    scriptname  = mfilename('fullpath');
+    [location, ~, ~] = fileparts(scriptname);
+end
diff -r 2d85f17a8aec -r 8e4274ee6dd8 sbplibVersion.m
--- a/sbplibVersion.m	Mon Oct 16 21:56:12 2017 -0700
+++ b/sbplibVersion.m	Sat Mar 03 14:58:21 2018 -0800
@@ -1,11 +1,10 @@
 % Prints the version and location of the sbplib currently in use.
 function sbplibVersion()
-    scriptname  = mfilename('fullpath');
-    [folder,~,~] = fileparts(scriptname);
+    location = sbplibLocation();
 
     name = 'sbplib (feature/grids)';
     ver = '0.0.x';
 
     fprintf('%s %s\n', name, ver);
-    fprintf('Running in:\n%s\n',folder);
+    fprintf('Running in:\n%s\n', location);
 end
\ No newline at end of file
diff -r 2d85f17a8aec -r 8e4274ee6dd8 spdiagVariable.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spdiagVariable.m	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,17 @@
+function A = spdiagVariable(a,i)
+    default_arg('i',0);
+
+    if isrow(a)
+        a = a';
+    end
+
+    n = length(a)+abs(i);
+
+    if i > 0
+    	a = [sparse(i,1); a];
+    elseif i < 0
+    	a = [a; sparse(abs(i),1)];
+    end
+
+    A = spdiags(a,i,n,n);
+end
\ No newline at end of file
diff -r 2d85f17a8aec -r 8e4274ee6dd8 spdiagsVariablePeriodic.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spdiagsVariablePeriodic.m	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,42 @@
+function A = spdiagsVariablePeriodic(vals,diags)
+    % Creates an m x m periodic discretization matrix.
+    % vals - m x ndiags matrix of values
+    % diags - 1 x ndiags vector of the 'center diagonals' that vals end up on
+    % vals that are not on main diagonal are going to spill over to 
+    % off-diagonal corners.
+
+    default_arg('diags',0);
+
+    [m, ~] = size(vals); 
+
+    A = sparse(m,m);
+
+    for i = 1:length(diags)
+        
+        d = diags(i);
+        a = vals(:,i);
+
+        % Sub-diagonals
+        if d < 0
+            a_bulk = a(1+abs(d):end);
+            a_corner = a(1:1+abs(d)-1);
+            corner_diag = m-abs(d);
+            A = A + spdiagVariable(a_bulk, d); 
+            A = A + spdiagVariable(a_corner, corner_diag);
+
+        % Super-diagonals
+        elseif d > 0
+            a_bulk = a(1:end-d);
+            a_corner = a(end-d+1:end);
+            corner_diag = -m + d;
+            A = A + spdiagVariable(a_bulk, d); 
+            A = A + spdiagVariable(a_corner, corner_diag);
+
+        % Main diagonal
+        else
+             A = A + spdiagVariable(a, 0);
+        end
+
+    end
+
+end
\ No newline at end of file
diff -r 2d85f17a8aec -r 8e4274ee6dd8 stripeMatrixPeriodic.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stripeMatrixPeriodic.m	Sat Mar 03 14:58:21 2018 -0800
@@ -0,0 +1,8 @@
+% Creates a periodic discretization matrix of size n x n 
+%  with the values of val on the diagonals diag.
+%   A = stripeMatrix(val,diags,n)
+function A = stripeMatrixPeriodic(val,diags,n)
+
+    D = ones(n,1)*val;
+    A = spdiagsVariablePeriodic(D,diags);
+end
\ No newline at end of file