classdef Utux < scheme.Scheme
   properties
        m % Number of points in each direction, possibly a vector
        h % Grid spacing
        grid % Grid
        order % Order accuracy for the approximation

        a % Wave speed
          % Can either be a constant or function handle.

        H % Discrete norm
        D

        D1
        Hi
        e_l
        e_r
    end


    methods
        function obj = Utux(g, order, a, fluxSplitting, opSet)
            default_arg('opSet',@sbp.D2Standard);
            default_arg('a',1);
            default_arg('fluxSplitting',[]);

            assertType(g, 'grid.Cartesian');
            if isa(a, 'function_handle')
                obj.a = spdiag(grid.evalOn(g, a));
            else
                obj.a = a;
            end

            m = g.size();
            xl = g.getBoundary('l');
            xr = g.getBoundary('r');
            xlim = {xl, xr};

            ops = opSet(m, xlim, order);

            if (isequal(opSet, @sbp.D1Upwind))
                obj.D1 = (ops.Dp + ops.Dm)/2;
                DissOp = (ops.Dm - ops.Dp)/2;
                obj.D = -(obj.a*obj.D1 + fluxSplitting*DissOp);
            else 
                obj.D1 = ops.D1;
                obj.D = -obj.a*obj.D1;
            end

            obj.grid = g;

            obj.H =  ops.H;
            obj.Hi = ops.HI;

            obj.e_l = ops.e_l;
            obj.e_r = ops.e_r;

            obj.m = m;
            obj.h = ops.h;
            obj.order = order;
        end
        % Closure functions return the opertors applied to the own doamin to close the boundary
        % Penalty functions return the opertors 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 if there are several.
        %       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)
            default_arg('type','dirichlet');
            s = obj.getBoundarySign(boundary);
            e = obj.getBoundaryOperator('e', boundary);
            switch boundary
                % Can only specify boundary condition where there is inflow
                % Extract the postivie resp. negative part of a, for the left
                % resp. right boundary, and set other values of a to zero.
                % Then the closure will effectively only contribute to inflow boundaries
                case {'l'}
                    a_inflow = obj.a;
                    a_inflow(a_inflow < 0) = 0;
                case {'r'}
                    a_inflow = obj.a;
                    a_inflow(a_inflow > 0) = 0;
            end
            tau = s*a_inflow*e;
            closure = obj.Hi*tau*e';
            penalty = -obj.Hi*tau;
         end

         function [closure, penalty] = interface(obj, boundary, neighbour_scheme, neighbour_boundary, type)
             switch boundary
                 % Upwind coupling
                 case {'l','left'}
                     tau = -1*obj.a*obj.e_l;
                     closure = obj.Hi*tau*obj.e_l';
                     penalty = -obj.Hi*tau*neighbour_scheme.e_r';
                 case {'r','right'}
                     tau = 0*obj.a*obj.e_r;
                     closure = obj.Hi*tau*obj.e_r';
                     penalty = -obj.Hi*tau*neighbour_scheme.e_l';
             end

         end

        % Returns the boundary sign. The right boundary is considered the positive boundary
        % boundary -- string
        function s = getBoundarySign(obj, boundary)
            assertIsMember(boundary, {'l', 'r'})

            switch boundary
                case {'r'}
                    s = 1;
                case {'l'}
                    s = -1;
            end
        end

        % Returns the boundary operator op for the boundary specified by the string boundary.
        % op        -- string
        % boundary  -- string
        function o = getBoundaryOperator(obj, op, boundary)
            assertIsMember(op, {'e'})
            assertIsMember(boundary, {'l', 'r'})

            o = obj.([op, '_', boundary]);
        end

        % Returns square boundary quadrature matrix, of dimension
        % corresponding to the number of boundary points
        %
        % boundary -- string
        % Note: for 1d diffOps, the boundary quadrature is the scalar 1.
        function H_b = getBoundaryQuadrature(obj, boundary)
            assertIsMember(boundary, {'l', 'r'})

            H_b = 1;
        end

        function N = size(obj)
            N = obj.m;
        end

    end
end
