classdef Schrodinger1dCurve < scheme.Scheme
    properties
        m % Number of points in each direction, possibly a vector
        h % Grid spacing
        xi % Grid
        order % Order accuracy for the approximation
        grid
        
        D % non-stabalized scheme operator
        H % Discrete norm
        M % Derivative norm
        alpha
        x_r
        x_l
        ddt_x_r
        ddt_x_l
        a
        a_xi
        Ji
        J
        t_up
        x
        
        V_mat
        D1
        D2
        Hi
        e_l
        e_r
        d1_l
        d1_r
        gamm
    end
    
    methods
        % Solving SE in the form u_t = i*u_xx +i*V on deforming 1D domain;
        function obj = Schrodinger1dCurve(g,order,boundaries,V,constJi)
            default_arg('V',0);
            default_arg('constJi',false)
            xilim={0 1};
            m = N(g);
            if constJi
                ops = sbp.D2Standard(m,xilim,order);
            else
                ops = sbp.D4Variable(m,xilim,order);
            end
            
            obj.x_l = boundaries{1};
            obj.x_r = boundaries{2};
            obj.ddt_x_l = boundaries{3};
            obj.ddt_x_r = boundaries{4};
            
            obj.xi=ops.x;
            obj.h=ops.h;
            obj.D2 = ops.D2;
            obj.D1 = ops.D1;
            obj.H =  ops.H;
            obj.Hi = ops.HI;
            obj.M =  ops.M;
            obj.e_l = ops.e_l;
            obj.e_r = ops.e_r;
            obj.d1_l = ops.d1_l;
            obj.d1_r = ops.d1_r;
            obj.grid = g;
            
            if isa(V,'function_handle')
                V_vec = V(obj.x);
            else
                V_vec = obj.xi*0 + V;
            end
            
            obj.V_mat = spdiags(V_vec,0,m,m);
            obj.D = @(t) obj.d_fun(t);
            obj.m = m;
            obj.order = order;
        end
        
        
        % Closure functions return the opertors appliedo 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 [D] = d_fun(obj,t)
            obj.variable_update(t); % In driscretization?
            D = sqrt(obj.Ji)*(-0.5*(obj.D1*obj.a*sqrt(obj.Ji) + obj.a*obj.D1*sqrt(obj.Ji)) + 1i*obj.D2(diag(obj.Ji))*obj.Ji + 1i*obj.V_mat);
          %   D= obj.Ji*(-sqrt(obj.a)*obj.D1*sqrt(obj.a) + 0.5*obj.a_xi + 1i*obj.D2(diag(obj.Ji)) + 1i*obj.V_mat);
        end
        
        
        function [] = variable_update(obj,t)
            if (t == obj.t_up)
                return
            else
                x_r = obj.x_r(t);
                x_l = obj.x_l(t);
                ddt_x_r = obj. ddt_x_r(t);
                ddt_x_l = obj.ddt_x_l(t);
                obj.x = obj.xi*(x_r -x_l) + x_l;
                obj.a = sparse(diag((-ddt_x_l*( x_r - x_l) - (obj.x-x_l)*(ddt_x_r-ddt_x_l))/(x_r-x_l)));            
                
                obj.Ji = sparse(diag(1./(x_r - x_l + 0*obj.x)));
                obj.J = sparse(x_r -x_l);
                obj.a_xi = sparse(diag(-1*(ddt_x_r - ddt_x_l + 0*obj.x)));
                obj.t_up = t;
            end
        end
        
        function [closure, penalty] = boundary_condition(obj,boundary,type,data)
            default_arg('type','dirichlet');
            default_arg('data',0);
            
            [e,d,s,p] = obj.get_boundary_ops(boundary);
            
            switch type
                % Dirichlet boundary condition
                case {'D','d','dirichlet'}
                    tau1 = @(t) s * 1i*obj.Ji(p,p)*d;
                    tau2 = @(t) (1*s*obj.a(p,p))/2*e;
                    closure = @(t)(obj.Ji*obj.Hi*tau1(t)*e' + obj.Hi*tau2(obj.a)*e');
                    
                    switch class(data)
                        case 'double'
                            penalty = @(t) -(obj.Hi*tau1*data+obj.Hi*tau2(obj.a)*data);
                            %                      case 'function_handle'
                            %                           penalty = @(t)-obj.Hi*tau*data(t);
                        otherwise
                            error('Weird data argument!')
                    end
                    
                    % 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
            [e_u,d_u,s_u,p_u] = obj.get_boundary_ops(boundary);
            [e_v,d_v,s_v,p_v] = neighbour_scheme.get_boundary_ops(neighbour_boundary);
            
            a1 =   s_u* 1/2 * 1i ;
            b1 =   -s_u* 1/2 * 1i;
            gamma = @(a) -obj.Ji*s_u*a(p_u,p_u)/2*e_u;
            
            tau = @(t) a1*obj.Ji(p_u,p_u)^2*d_u;
            sig = b1*e_u;
            
            closure = @(t) obj.Hi * (tau(t)*e_u' + sig*obj.Ji(p_u,p_u)^2*d_u' + gamma(obj.a)*e_u');
            penalty = @(t) obj.Hi * (-tau(t)*e_v' - sig*obj.Ji(p_u,p_u)^2*d_v' - gamma(obj.a)*e_v');
        end
        
        % Ruturns the boundary ops and sign for the boundary specified by the string boundary.
        % The right boundary is considered the positive boundary
        function [e,d,s,p] = get_boundary_ops(obj,boundary)
            switch boundary
                case 'l'
                    e = obj.e_l;
                    d = obj.d1_l;
                    s = -1;
                    p=1;
                case 'r'
                    e = obj.e_r;
                    d = obj.d1_r;
                    s = 1;
                    p=obj.m;
                otherwise
                    error('No such boundary: boundary = %s',boundary);
            end
        end
        
        function N = size(obj)
            N = obj.m;
        end
        
    end
    
    methods(Static)
        % Calculates the matrcis need for the inteface coupling between boundary bound_u of scheme schm_u
        % and bound_v of scheme schm_v.
        %   [uu, uv, vv, vu] = inteface_couplong(A,'r',B,'l')
        function [uu, uv, vv, vu] = interface_coupling(schm_u,bound_u,schm_v,bound_v)
            [uu,uv] = schm_u.interface(bound_u,schm_v,bound_v);
            [vv,vu] = schm_v.interface(bound_v,schm_u,bound_u);
        end
    end
end