view +noname/animate.m @ 577:e45c9b56d50d feature/grids

Add an Empty grid class The need turned up for the flexural code when we may or may not have a grid for the open water and want to plot that solution. In case there is no open water we need an empty grid to plot the empty gridfunction against to avoid errors.
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 07 Sep 2017 09:16:12 +0200
parents dfa4455033db
children 0da1c913a611
line wrap: on
line source

% hand = noname.animate(discretization, time_modifier, Tend, dirname, opt)
%
% Example:
%      noname.animate(discr,timemodifier,tend)
%      noname.animate(discr,1, [tstart tend],'my_mov', opt)

function hand = animate(discretization, time_modifier, Tend, dirname, opt)
    default_arg('time_modifier', 1);
    default_arg('Tend', Inf);
    default_arg('dirname', '');

    optDefault.plotType = 'animation';
    optDefault.time = [];

    default_struct('opt', optDefault);


    if time_modifier < 0
        do_pause = true;
        time_modifier = -time_modifier;
    else
        do_pause = false;
    end

    if isinf(time_modifier)
        do_step = true;
    else
        do_step = false;
    end

    makemovies = ~strcmp(dirname,'');
    if makemovies
        dirname = ['mov/' dirname];
    end

    fprintf('Animating: %s\n',discretization.name);
    fprintf('order    : %d\n',discretization.order);
    fprintf('m        : %d\n',size(discretization));


    ts = discretization.getTimestepper(opt.time);

    if numel(Tend) == 2
        Tstart = Tend(1);
        Tend = Tend(2);


        fprintf('Evolving to starting time: ');
        ts.evolve(Tstart,'true');
        fprintf(' - Done\n');
        start_solution = discretization.getTimeSnapshot(ts);
    else
        start_solution = discretization.getTimeSnapshot(0);
        Tstart = start_solution.t;
    end

    [update, figure_handle] = discretization.setupPlot(opt.plotType);
    if makemovies
        save_frame = anim.setup_fig_mov(figure_handle,dirname);
    end

    % Initialize loop
    str = '';
    % Loop function
    function next_t = G(next_t)
        ts.evolve(next_t);
        sol = discretization.getTimeSnapshot(ts);
        update(sol);
        drawnow
        % waitforbuttonpress
        if makemovies
            save_frame();
        end

        if do_pause
            pause
        end
    end
    update(start_solution);

    fprintf('Using time step k = %.6f\n',ts.k);
    fprintf('System size: %d\n',size(discretization));
    % waitforbuttonpress


    if ~do_step
        pause
        anim.animate(@G, Tstart, Tend, time_modifier);
    else
        while true
            ts.step();
            sol = discretization.getTimeSnapshot(ts);
            update(sol);
            drawnow

            if do_pause
                pause
            end
        end
    end

    % str = util.replace_string(str,'');

    % if makemovies
        % fprintf('Generating movies...\n')
        % system(sprintf('bash make_movie.sh %s',dirname));
    % end
end