view +grid/evalOnScalar.m @ 1037:2d7ba44340d0 feature/burgers1d

Pass scheme specific parameters as cell array. This will enabale constructDiffOps to be more general. In addition, allow for schemes returning function handles as diffOps, which is currently how non-linear schemes such as Burgers1d are implemented.
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Fri, 18 Jan 2019 09:02:02 +0100
parents 3ea2ae2a3d15
children
line wrap: on
line source

% WHAT KIND OF A FUNCTION NAME IS THIS?!
%  This functions send matrixa arguments to func unlike grid.evalOn()
% Takes a funciton and evaluates it on a grid to return a grid function in the
% form of a (n*k)x1 vector, where n is the number of grid points and k is the
% number of components of the function.
%      g -- Grid to evaluate on.
%   func -- Function to evaluate. May be a function handle or a constant. If
%           it is a vector value it has to be provided as a column vector,
function gf = evalOn(g, func)
    if ~isa(func, 'function_handle')
        % We should have a constant.
        if size(func,2) ~= 1
            error('grid:evalOnScalar:VectorValuedWrongDim', 'A vector valued function must be given as a column vector')
        end

        gf = repmat(func,[g.N, 1]);
        return
    end
    % func should now be a function_handle

    % Get coordinates and convert to cell array for easier use as a parameter
    x = g.points();
    X = {};
    for i = 1:size(x, 2)
        X{i} = x(:,i);
    end

    % Find the number of components
    x0 = num2cell(x(1,:));
    f0 = func(x0{:});
    k = length(f0);

    if size(f0,2) ~= 1
        error('grid:evalOnScalar:VectorValuedWrongDim', 'A vector valued function must be given as a column vector')
    end

    gf = func(X{:});
    if  k > 1  % Reorder so that componets sits together.
        gf = reshape(reshape(gf, [g.N, k])', [g.N*k, 1]);
    end
end