예제 #1
0
def joint_plots(list_of_result_paths):

    # get result data from JUBE tables
    results = []
    for result_path in list_of_result_paths:
        results.append(np.genfromtxt(result_path, names=True, skip_header=1, delimiter='|', dtype=float, comments='--'))

    # fill arrays with data
    ncores = np.concatenate(results)['ntasks'] * np.concatenate(results)['nnodes']
    timings_space = results[0]['timing_pat']
    timings_spacetime = results[1]['timing_pat']
    ideal = [timings_space[0] / (c / ncores[0]) for c in np.unique(ncores)]

    # setup and fill plots
    plt_helper.setup_mpl()
    plt_helper.newfig(textwidth=238.96, scale=1.0)

    plt_helper.plt.loglog(np.unique(ncores), ideal, 'k--', label='ideal')
    plt_helper.plt.loglog(ncores[0:len(results[0])], timings_space, lw=1, ls='-', color='b', marker='o',
                          markersize=4, markeredgecolor='k', label='parallel-in-space')
    plt_helper.plt.loglog(ncores[-len(results[1]):], timings_spacetime, lw=1, ls='-', color='r', marker='d',
                          markersize=4, markeredgecolor='k', label='parallel-in-space-time')

    plt_helper.plt.grid()
    plt_helper.plt.legend(loc=3, ncol=1)
    plt_helper.plt.xlabel('Number of cores')
    plt_helper.plt.ylabel('Time [s]')

    # save plot, beautify
    fname = 'data/scaling'
    plt_helper.savefig(fname)
예제 #2
0
def plot_graphs(cwd=''):
    """
    Helper function to plot graphs of initial and final values

    Args:
        cwd (str): current working directory
    """
    plt_helper.mpl.style.use('classic')

    file = open(cwd + 'data/error_reduction_data.pkl', 'rb')
    results = pickle.load(file)

    sweeper_list = results['sweeper_list']
    dt_list = results['dt_list']

    color_list = ['red', 'blue', 'green']
    marker_list = ['o', 's', 'd']
    label_list = []
    for sweeper in sweeper_list:
        if sweeper == 'generic_implicit':
            label_list.append('SDC')
        elif sweeper == 'linearized_implicit_fixed_parallel':
            label_list.append('Simplified Newton')
        elif sweeper == 'linearized_implicit_fixed_parallel_prec':
            label_list.append('Inexact Newton')

    setups = zip(sweeper_list, color_list, marker_list, label_list)

    plt_helper.setup_mpl()

    plt_helper.newfig(textwidth=238.96, scale=0.89)

    for sweeper, color, marker, label in setups:
        plt_helper.plt.loglog(dt_list, results[sweeper], lw=1, ls='-', color=color, marker=marker,
                              markeredgecolor='k', label=label)

    plt_helper.plt.loglog(dt_list, [dt * 2 for dt in dt_list], lw=0.5, ls='--', color='k', label='linear')
    plt_helper.plt.loglog(dt_list, [dt * dt / dt_list[0] * 2 for dt in dt_list], lw=0.5, ls='-.', color='k',
                          label='quadratic')

    plt_helper.plt.xlabel('dt')
    plt_helper.plt.ylabel('error reduction')
    plt_helper.plt.grid()

    # ax.set_xticks(dt_list, dt_list)
    plt_helper.plt.xticks(dt_list, dt_list)

    plt_helper.plt.legend(loc=1, ncol=1)

    plt_helper.plt.gca().invert_xaxis()
    plt_helper.plt.xlim([dt_list[0] * 1.1, dt_list[-1] / 1.1])
    plt_helper.plt.ylim([4E-03, 1E0])

    # save plot, beautify
    fname = 'data/parallelSDC_fisher_newton'
    plt_helper.savefig(fname)

    assert os.path.isfile(fname + '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(fname + '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(fname + '.png'), 'ERROR: plotting did not create PNG file'
예제 #3
0
    def __init__(self):
        """
        Initialization of Allen-Cahn monitoring
        """
        super(output, self).__init__()

        plt_helper.setup_mpl()

        self.counter = 0
        self.fig = None
        self.ax = None
        self.output_ratio = 1
예제 #4
0
def show_results(prob=None, cwd=''):
    """
    Helper function to plot the error of the Hamiltonian

    Args:
        prob (str): name of the problem
        cwd (str): current working directory
    """

    # read in the dill data
    f = open(cwd + 'data/' + prob + '.dat', 'rb')
    stats = dill.load(f)
    f.close()

    # extract error in hamiltonian and prepare for plotting
    extract_stats = filter_stats(stats, type='err_hamiltonian')
    result = defaultdict(list)
    for k, v in extract_stats.items():
        result[k.iter].append((k.time, v))
    for k, v in result.items():
        result[k] = sorted(result[k], key=lambda x: x[0])

    plt_helper.mpl.style.use('classic')
    plt_helper.setup_mpl()
    plt_helper.newfig(textwidth=238.96, scale=0.89)

    # Rearrange data for easy plotting
    err_ham = 1
    for k, v in result.items():
        time = [item[0] for item in v]
        ham = [item[1] for item in v]
        err_ham = ham[-1]
        plt_helper.plt.semilogy(time, ham, '-', lw=1, label='Iter ' + str(k))

    assert err_ham < 3.7E-08, 'Error in the Hamiltonian is too large for %s, got %s' % (
        prob, err_ham)

    plt_helper.plt.xlabel('Time')
    plt_helper.plt.ylabel('Error in Hamiltonian')
    plt_helper.plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))

    fname = 'data/' + prob + '_hamiltonian'
    plt_helper.savefig(fname)

    assert os.path.isfile(fname +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(fname +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(fname +
                          '.png'), 'ERROR: plotting did not create PNG file'
def visualize_matrix(result=None):
    """
    Visualizes runtimes in a matrix (cores in space vs. cores in time)

    Args:
        result: dictionary containing the runtimes
    """
    process_list = [1, 2, 4, 6, 12, 24]
    dim = len(process_list)
    mat = np.zeros((dim, dim))
    tmin = 1E03
    tmax = 0
    for key, item in result.items():
        mat[process_list.index(key[0]), process_list.index(key[1])] = item
        tmin = min(tmin, item)
        tmax = max(tmax, item)

    plt_helper.setup_mpl()
    plt_helper.newfig(textwidth=120, scale=1.5)
    cmap = plt_helper.plt.get_cmap('RdYlGn_r')
    new_cmap = truncate_colormap(cmap, 0.1, 0.9)
    plt_helper.plt.imshow(mat.T,
                          origin='lower',
                          norm=colors.LogNorm(vmin=tmin, vmax=tmax),
                          cmap=new_cmap,
                          aspect='auto')

    for key, item in result.items():
        timing = "{:3.1f}".format(item)
        plt_helper.plt.annotate(timing,
                                xy=(process_list.index(key[0]),
                                    process_list.index(key[1])),
                                size='x-small',
                                ha='center',
                                va='center')

    plt_helper.plt.xticks(range(dim), process_list)
    plt_helper.plt.yticks(range(dim), process_list)
    plt_helper.plt.xlabel('Cores in space')
    plt_helper.plt.ylabel('Cores in time')

    fname = 'data/runtimes_matrix_heat'
    plt_helper.savefig(fname)

    assert os.path.isfile(fname +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(fname +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(fname +
                          '.png'), 'ERROR: plotting did not create PNG file'
def visualize_speedup(result=None):
    """
    Visualizes runtimes of two different runs (MLSDC vs. PFASST)

    Args:
        result: dictionary containing the runtimes
    """
    process_list_MLSDC = [1, 2, 4, 6, 12, 24]
    process_list_PFASST = [24, 48, 96, 144, 288, 576]

    timing_MLSDC = np.zeros(len(process_list_MLSDC))
    timing_PFASST = np.zeros((len(process_list_PFASST)))
    for key, item in result.items():
        if key[0] * key[1] in process_list_MLSDC:
            timing_MLSDC[process_list_MLSDC.index(key[0] * key[1])] = item
        if key[0] * key[1] in process_list_PFASST:
            timing_PFASST[process_list_PFASST.index(key[0] * key[1])] = item

    plt_helper.setup_mpl()
    plt_helper.newfig(textwidth=120, scale=1.5)

    process_list_all = process_list_MLSDC + process_list_PFASST
    ideal = [timing_MLSDC[0] / nproc for nproc in process_list_all]
    plt_helper.plt.loglog(process_list_all, ideal, 'k--', label='ideal')
    plt_helper.plt.loglog(process_list_MLSDC,
                          timing_MLSDC,
                          'bo-',
                          label='MLSDC')
    plt_helper.plt.loglog(process_list_PFASST,
                          timing_PFASST,
                          'rs-',
                          label='PFASST')

    plt_helper.plt.xlim(process_list_all[0] / 2, process_list_all[-1] * 2)
    plt_helper.plt.ylim(ideal[-1] / 2, ideal[0] * 2)
    plt_helper.plt.xlabel('Number of cores')
    plt_helper.plt.ylabel('Runtime (sec.)')

    plt_helper.plt.legend()
    plt_helper.plt.grid()

    fname = 'data/speedup_heat'
    plt_helper.savefig(fname)
    assert os.path.isfile(fname +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(fname +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(fname +
                          '.png'), 'ERROR: plotting did not create PNG file'
예제 #7
0
def plot_graphs():
    """
    Helper function to plot graphs of initial and final values
    """

    file = open('data/parallelSDC_results_graphs.pkl', 'rb')
    results = pickle.load(file)

    interval = results['interval']
    xvalues = results['xvalues']
    uinit = results['uinit']
    uend = results['uend']
    uex = results['uex']

    plt_helper.setup_mpl()

    # set up figure
    plt_helper.newfig(textwidth=338.0, scale=1.0)

    plt_helper.plt.xlabel('x')
    plt_helper.plt.ylabel('f(x)')
    plt_helper.plt.xlim((interval[0] - 0.01, interval[1] + 0.01))
    plt_helper.plt.ylim((-0.1, 1.1))
    plt_helper.plt.grid()

    # plot
    plt_helper.plt.plot(xvalues, uinit, 'r--', lw=1, label='initial')
    plt_helper.plt.plot(xvalues,
                        uend,
                        'bs',
                        lw=1,
                        markeredgecolor='k',
                        label='computed')
    plt_helper.plt.plot(xvalues, uex, 'g-', lw=1, label='exact')

    plt_helper.plt.legend(loc=2, ncol=1)

    # save plot as PDF, beautify
    fname = 'data/parallelSDC_fisher'
    plt_helper.savefig(fname)

    assert os.path.isfile(fname +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(fname +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(fname +
                          '.png'), 'ERROR: plotting did not create PNG file'
예제 #8
0
def show_results(fname):
    """
    Plotting routine

    Args:
        fname: file name to read in and name plots
    """

    file = open(fname + '.pkl', 'rb')
    results = pickle.load(file)
    file.close()

    plt_helper.mpl.style.use('classic')
    plt_helper.setup_mpl()

    plt_helper.newfig(textwidth=238.96, scale=1.0)

    xcoords = [i for i in range(len(results))]
    sorted_data = sorted([(key, results[key][0]) for key in results],
                         reverse=True,
                         key=lambda tup: tup[1])
    heights = [item[1] for item in sorted_data]
    keys = [(item[0][1] + ' ' + item[0][0]).replace('-', '\n')
            for item in sorted_data]

    plt_helper.plt.bar(xcoords, heights, align='center')

    plt_helper.plt.xticks(xcoords, keys, rotation=90)
    plt_helper.plt.ylabel('time (sec)')

    # save plot, beautify
    plt_helper.savefig(fname)

    assert os.path.isfile(fname +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(fname +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(fname +
                          '.png'), 'ERROR: plotting did not create PNG file'

    return None
예제 #9
0
def plot_data(name=''):
    """
    Visualization using numpy arrays (written via MPI I/O) and json description

    Produces one png file per time-step, combine as movie via e.g.
      > ffmpeg -i data/name_%08d.png name.mp4

    Args:
        name (str): name of the simulation (expects data to be in data path)
    """

    # get data and json files
    json_files = sorted(glob.glob(f'./data/{name}_*.json'))
    data_files = sorted(glob.glob(f'./data/{name}_*.dat'))

    # setup plotting
    plt_helper.setup_mpl()

    for json_file, data_file in zip(json_files, data_files):
        with open(json_file, 'r') as fp:
            obj = json.load(fp)

        index = json_file.split('_')[1].split('.')[0]
        print(f'Working on step {index}...')

        # get data and format
        array = np.fromfile(data_file, dtype=obj['datatype'])
        array = array.reshape(obj['shape'], order='C')

        # plot
        plt_helper.newfig(textwidth=238.96, scale=1.0)
        plt_helper.plt.imshow(array, vmin=0, vmax=1, extent=[-2, 2, -2, 2], origin='lower')

        plt_helper.plt.yticks(range(-2, 3))
        cbar = plt_helper.plt.colorbar()
        cbar.set_label('concentration')
        # plt_helper.plt.title(f"Time: {obj['time']:6.4f}")

        # save plot, beautify
        fname = f'data/{name}_{index}'
        plt_helper.savefig(fname, save_pgf=False, save_png=False)
예제 #10
0
def show_results(cwd=''):
    """
    Helper function to plot the error of the Hamiltonian

    Args:
        cwd (str): current working directory
    """

    plt_helper.mpl.style.use('classic')
    plt_helper.setup_mpl()
    plt_helper.newfig(textwidth=238.96, scale=0.89)

    # read in the dill data
    f = open(cwd + 'data/harmonic_k.dat', 'rb')
    results = dill.load(f)
    f.close()

    ks = results['ks']

    for qd in results:

        if qd != 'ks':

            plt_helper.plt.plot(ks, results[qd], label=qd)

    plt_helper.plt.xlabel('k')
    plt_helper.plt.ylabel('Number of iterations')
    plt_helper.plt.legend(loc='upper left', )
    plt_helper.plt.ylim([0, 15])

    fname = 'data/harmonic_qd_iterations'
    plt_helper.savefig(fname)

    assert os.path.isfile(fname +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(fname +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(fname +
                          '.png'), 'ERROR: plotting did not create PNG file'
def show_results(fname, cwd=''):
    """
    Plotting routine

    Args:
        fname (str): file name to read in and name plots
        cwd (str): current working directory
    """

    file = open(cwd + fname + '.pkl', 'rb')
    results = dill.load(file)
    file.close()

    # plt_helper.mpl.style.use('classic')
    plt_helper.setup_mpl()

    # set up plot for timings
    fig, ax1 = plt_helper.newfig(textwidth=238.96, scale=1.5, ratio=0.4)

    timings = {}
    niters = {}
    for key, item in results.items():
        timings[key] = sort_stats(filter_stats(item, type='timing_run'),
                                  sortby='time')[0][1]
        iter_counts = sort_stats(filter_stats(item, type='niter'),
                                 sortby='time')
        niters[key] = np.mean(np.array([item[1] for item in iter_counts]))

    xcoords = [i for i in range(len(timings))]
    sorted_timings = sorted([(key, timings[key]) for key in timings],
                            reverse=True,
                            key=lambda tup: tup[1])
    sorted_niters = [(k, niters[k])
                     for k in [key[0] for key in sorted_timings]]
    heights_timings = [item[1] for item in sorted_timings]
    heights_niters = [item[1] for item in sorted_niters]
    keys = [
        (item[0][1] + ' ' + item[0][0]).replace('-',
                                                '\n').replace('_v2', ' mod.')
        for item in sorted_timings
    ]

    ax1.bar(xcoords,
            heights_timings,
            align='edge',
            width=-0.3,
            label='timings (left axis)')
    ax1.set_ylabel('time (sec)')

    ax2 = ax1.twinx()
    ax2.bar(xcoords,
            heights_niters,
            color='r',
            align='edge',
            width=0.3,
            label='iterations (right axis)')
    ax2.set_ylabel('mean number of iterations')

    ax1.set_xticks(xcoords)
    ax1.set_xticklabels(keys, rotation=90, ha='center')

    # ask matplotlib for the plotted objects and their labels
    lines, labels = ax1.get_legend_handles_labels()
    lines2, labels2 = ax2.get_legend_handles_labels()
    ax2.legend(lines + lines2, labels + labels2, loc=0)

    # save plot, beautify
    f = fname + '_timings'
    plt_helper.savefig(f)

    assert os.path.isfile(f +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(f +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(f +
                          '.png'), 'ERROR: plotting did not create PNG file'

    # set up plot for radii
    fig, ax = plt_helper.newfig(textwidth=238.96, scale=1.0)

    exact_radii = []
    for key, item in results.items():
        computed_radii = sort_stats(filter_stats(item, type='computed_radius'),
                                    sortby='time')

        xcoords = [item0[0] for item0 in computed_radii]
        radii = [item0[1] for item0 in computed_radii]
        if key[0] + ' ' + key[1] == 'fully-implicit exact':
            ax.plot(xcoords,
                    radii,
                    label=(key[0] + ' ' + key[1]).replace('_v2', ' mod.'))

        exact_radii = sort_stats(filter_stats(item, type='exact_radius'),
                                 sortby='time')

        diff = np.array([
            abs(item0[1] - item1[1])
            for item0, item1 in zip(exact_radii, computed_radii)
        ])
        max_pos = int(np.argmax(diff))
        assert max(
            diff
        ) < 0.07, 'ERROR: computed radius is too far away from exact radius, got %s' % max(
            diff)
        assert 0.028 < computed_radii[max_pos][0] < 0.03, \
            'ERROR: largest difference is at wrong time, got %s' % computed_radii[max_pos][0]

    xcoords = [item[0] for item in exact_radii]
    radii = [item[1] for item in exact_radii]
    ax.plot(xcoords,
            radii,
            color='k',
            linestyle='--',
            linewidth=1,
            label='exact')

    ax.yaxis.set_major_formatter(ticker.FormatStrFormatter('%1.2f'))
    ax.set_ylabel('radius')
    ax.set_xlabel('time')
    ax.grid()
    ax.legend(loc=3)

    # save plot, beautify
    f = fname + '_radii'
    plt_helper.savefig(f)

    assert os.path.isfile(f +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(f +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(f +
                          '.png'), 'ERROR: plotting did not create PNG file'

    # set up plot for interface width
    fig, ax = plt_helper.newfig(textwidth=238.96, scale=1.0)

    interface_width = []
    for key, item in results.items():
        interface_width = sort_stats(filter_stats(item,
                                                  type='interface_width'),
                                     sortby='time')
        xcoords = [item[0] for item in interface_width]
        width = [item[1] for item in interface_width]
        if key[0] + ' ' + key[1] == 'fully-implicit exact':
            ax.plot(xcoords, width, label=key[0] + ' ' + key[1])

    xcoords = [item[0] for item in interface_width]
    init_width = [interface_width[0][1]] * len(xcoords)
    ax.plot(xcoords,
            init_width,
            color='k',
            linestyle='--',
            linewidth=1,
            label='exact')

    ax.yaxis.set_major_formatter(ticker.FormatStrFormatter('%1.2f'))
    ax.set_ylabel(r'interface width ($\epsilon$)')
    ax.set_xlabel('time')
    ax.grid()
    ax.legend(loc=3)

    # save plot, beautify
    f = fname + '_interface'
    plt_helper.savefig(f)

    assert os.path.isfile(f +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(f +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(f +
                          '.png'), 'ERROR: plotting did not create PNG file'

    return None
예제 #12
0
M.mult(w.vector(), Mw.vector())

# Do FFT to get the frequencies
fw = np.fft.fft(w.vector()[:])
fMw = np.fft.fft(Mw.vector()[:])
# Shift to have zero frequency in the middle of the plot
fw2 = np.fft.fftshift(fw)
fMw2 = np.fft.fftshift(fMw)

fw2 /= np.amax(abs(fw2))
fMw2 /= np.amax(abs(fMw2))

ndofs = fw.shape[0]

# Plot
plt_helper.setup_mpl()

plt_helper.newfig(240, 1, ratio=0.8)
plt_helper.plt.plot(abs(fw2),
                    lw=2,
                    label=f'N = {N} \n degree = {d} \n wave number = {k}')
plt_helper.plt.xticks(
    [0, ndofs / 4 - 1, ndofs / 2 - 1, 3 * ndofs / 4 - 1, ndofs - 1],
    (r'-$\pi$', r'-$\pi/2$', r'$0$', r'+$\pi$/2', r'+$\pi$'))
plt_helper.plt.xlabel('spectrum')
plt_helper.plt.ylabel('normed amplitude')
# plt_helper.plt.legend()
plt_helper.plt.grid()
plt_helper.savefig('spectrum_noM_CG')
# plt_helper.savefig('spectrum_noM_DG')
# plt_helper.savefig('spectrum_noM_DG_8')
예제 #13
0
파일: fput.py 프로젝트: daveb-dev/pySDC
def show_results(cwd=''):
    """
    Helper function to plot the error of the Hamiltonian

    Args:
        cwd (str): current working directory
    """

    # read in the dill data
    f = open(cwd + 'data/fput.dat', 'rb')
    stats = dill.load(f)
    f.close()

    plt_helper.mpl.style.use('classic')
    plt_helper.setup_mpl()

    # HAMILTONIAN PLOTTING #
    # extract error in hamiltonian and prepare for plotting
    extract_stats = filter_stats(stats, type='err_hamiltonian')
    result = defaultdict(list)
    for k, v in extract_stats.items():
        result[k.iter].append((k.time, v))
    for k, v in result.items():
        result[k] = sorted(result[k], key=lambda x: x[0])

    plt_helper.newfig(textwidth=238.96, scale=0.89)

    # Rearrange data for easy plotting
    err_ham = 1
    for k, v in result.items():
        time = [item[0] for item in v]
        ham = [item[1] for item in v]
        err_ham = ham[-1]
        plt_helper.plt.semilogy(time, ham, '-', lw=1, label='Iter ' + str(k))
    print(err_ham)
    assert err_ham < 6E-10, 'Error in the Hamiltonian is too large, got %s' % err_ham

    plt_helper.plt.xlabel('Time')
    plt_helper.plt.ylabel('Error in Hamiltonian')
    plt_helper.plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))

    fname = 'data/fput_hamiltonian'
    plt_helper.savefig(fname)

    assert os.path.isfile(fname +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(fname +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(fname +
                          '.png'), 'ERROR: plotting did not create PNG file'

    # ENERGY PLOTTING #
    # extract error in hamiltonian and prepare for plotting
    extract_stats = filter_stats(stats, type='energy_step')
    result = sort_stats(extract_stats, sortby='time')

    plt_helper.newfig(textwidth=238.96, scale=0.89)

    # Rearrange data for easy plotting
    for mode in result[0][1].keys():
        time = [item[0] for item in result]
        energy = [item[1][mode] for item in result]
        plt_helper.plt.plot(time, energy, label=str(mode) + 'th mode')

    plt_helper.plt.xlabel('Time')
    plt_helper.plt.ylabel('Energy')
    plt_helper.plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))

    fname = 'data/fput_energy'
    plt_helper.savefig(fname)

    assert os.path.isfile(fname +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(fname +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(fname +
                          '.png'), 'ERROR: plotting did not create PNG file'

    # POSITION PLOTTING #
    # extract positions and prepare for plotting
    extract_stats = filter_stats(stats, type='position')
    result = sort_stats(extract_stats, sortby='time')

    plt_helper.newfig(textwidth=238.96, scale=0.89)

    # Rearrange data for easy plotting
    nparts = len(result[0][1])
    nsteps = len(result)
    pos = np.zeros((nparts, nsteps))
    time = np.zeros(nsteps)
    for idx, item in enumerate(result):
        time[idx] = item[0]
        for n in range(nparts):
            pos[n, idx] = item[1][n]

    for n in range(nparts):
        plt_helper.plt.plot(time, pos[n, :])

    fname = 'data/fput_positions'
    plt_helper.savefig(fname)

    assert os.path.isfile(fname +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(fname +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(fname +
                          '.png'), 'ERROR: plotting did not create PNG file'
예제 #14
0
def show_results(prob=None, cwd=''):
    """
    Helper function to plot the error of the Hamiltonian

    Args:
        prob (str): name of the problem
        cwd (str): current working directory
    """

    # read in the dill data
    f = open(cwd + 'data/' + prob + '.dat', 'rb')
    stats = dill.load(f)
    f.close()

    plt_helper.mpl.style.use('classic')
    plt_helper.setup_mpl()

    # extract error in hamiltonian and prepare for plotting
    extract_stats = filter_stats(stats, type='err_hamiltonian')
    result = defaultdict(list)
    for k, v in extract_stats.items():
        result[k.iter].append((k.time, v))
    for k, v in result.items():
        result[k] = sorted(result[k], key=lambda x: x[0])

    plt_helper.newfig(textwidth=238.96, scale=0.89)

    # Rearrange data for easy plotting
    err_ham = 1
    for k, v in result.items():
        time = [item[0] for item in v]
        ham = [item[1] for item in v]
        err_ham = ham[-1]
        plt_helper.plt.semilogy(time, ham, '-', lw=1, label='Iter ' + str(k))
    assert err_ham < 2.4E-14, 'Error in the Hamiltonian is too large for %s, got %s' % (
        prob, err_ham)

    plt_helper.plt.xlabel('Time')
    plt_helper.plt.ylabel('Error in Hamiltonian')
    plt_helper.plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))

    fname = 'data/' + prob + '_hamiltonian'
    plt_helper.savefig(fname)

    assert os.path.isfile(fname +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(fname +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(fname +
                          '.png'), 'ERROR: plotting did not create PNG file'

    # extract positions and prepare for plotting
    extract_stats = filter_stats(stats, type='position')
    result = sort_stats(extract_stats, sortby='time')

    fig = plt_helper.plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    # Rearrange data for easy plotting
    nparts = len(result[1][1][0])
    ndim = len(result[1][1])
    nsteps = len(result)
    pos = np.zeros((nparts, ndim, nsteps))

    for idx, item in enumerate(result):
        for n in range(nparts):
            for m in range(ndim):
                pos[n, m, idx] = item[1][m][n]

    for n in range(nparts):
        if ndim == 2:
            ax.plot(pos[n, 0, :], pos[n, 1, :])
        elif ndim == 3:
            ax.plot(pos[n, 0, :], pos[n, 1, :], pos[n, 2, :])
        else:
            raise NotImplemented(
                'Wrong number of dimensions for plotting, got %s' % ndim)

    fname = 'data/' + prob + '_positions'
    plt_helper.savefig(fname)

    assert os.path.isfile(fname +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(fname +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(fname +
                          '.png'), 'ERROR: plotting did not create PNG file'
예제 #15
0
def plot_iterations():
    """
    Helper routine to plot iteration counts
    """

    file = open('data/parallelSDC_iterations_precond.pkl', 'rb')
    results = pickle.load(file)

    # find the lists/header required for plotting
    qd_type_list = []
    setup_list = []
    for key in results.keys():
        if isinstance(key, ID):
            if key.qd_type not in qd_type_list:
                qd_type_list.append(key.qd_type)
        elif isinstance(key, str):
            setup_list.append(key)
    print('Found these type of preconditioners:', qd_type_list)
    print('Found these setups:', setup_list)

    assert len(qd_type_list) == 5, 'ERROR did not find five preconditioners, got %s' % qd_type_list
    assert len(setup_list) == 4, 'ERROR: did not find three setup, got %s' % setup_list

    qd_type_list = ['LU', 'IE', 'IEpar', 'Qpar', 'MIN']
    marker_list = [None, None, 's', 'o', '^']
    color_list = ['k', 'k', 'r', 'g', 'b']

    plt_helper.setup_mpl()

    # loop over setups and Q-delta types: one figure per setup, all Qds in one plot
    for setup in setup_list:

        plt_helper.newfig(textwidth=238.96, scale=0.89)

        for qd_type, marker, color in zip(qd_type_list, marker_list, color_list):
            niter = np.zeros(len(results[setup][1]))
            for key in results.keys():
                if isinstance(key, ID):
                    if key.setup == setup and key.qd_type == qd_type:
                        xvalue = results[setup][1].index(key.param)
                        niter[xvalue] = results[key]
            if qd_type == 'LU':
                ls = '--'
                lw = 0.5
            elif qd_type == 'IE':
                ls = '-.'
                lw = 0.5
            else:
                ls = '-'
                lw = 1
            plt_helper.plt.semilogx(results[setup][1], niter, label=qd_type, lw=lw, linestyle=ls, color=color,
                                    marker=marker, markeredgecolor='k')

        if setup == 'heat':
            xlabel = r'$\nu$'
        elif setup == 'advection':
            xlabel = r'$c$'
        elif setup == 'fisher':
            xlabel = r'$\lambda_0$'
        elif setup == 'vanderpol':
            xlabel = r'$\mu$'
        else:
            print('Setup not implemented..', setup)
            exit()

        plt_helper.plt.ylim([0, 60])
        plt_helper.plt.legend(loc=2, ncol=1)
        plt_helper.plt.ylabel('number of iterations')
        plt_helper.plt.xlabel(xlabel)
        plt_helper.plt.grid()

        # save plot as PDF and PGF
        fname = 'data/parallelSDC_preconditioner_' + setup
        plt_helper.savefig(fname)

        assert os.path.isfile(fname + '.pdf'), 'ERROR: plotting did not create PDF file'
        assert os.path.isfile(fname + '.pgf'), 'ERROR: plotting did not create PGF file'
        assert os.path.isfile(fname + '.png'), 'ERROR: plotting did not create PNG file'
def show_results(fname, cwd=''):
    """
    Plotting routine

    Args:
        fname (str): file name to read in and name plots
        cwd (str): current working directory
    """

    file = open(cwd + fname + '.pkl', 'rb')
    results = dill.load(file)
    file.close()

    # plt_helper.mpl.style.use('classic')
    plt_helper.setup_mpl()

    # set up plot for timings
    plt_helper.newfig(textwidth=238.96, scale=1.0)

    timings = {}
    for key, item in results.items():
        timings[key] = sort_stats(filter_stats(item, type='timing_run'),
                                  sortby='time')[0][1]

    xcoords = [i for i in range(len(timings))]
    sorted_data = sorted([(key, timings[key]) for key in timings],
                         reverse=True,
                         key=lambda tup: tup[1])
    heights = [item[1] for item in sorted_data]
    keys = [(item[0][1] + ' ' + item[0][0]).replace('-', '\n')
            for item in sorted_data]

    plt_helper.plt.bar(xcoords, heights, align='center')

    plt_helper.plt.xticks(xcoords, keys, rotation=90)
    plt_helper.plt.ylabel('time (sec)')

    # # save plot, beautify
    f = fname + '_timings'
    plt_helper.savefig(f)

    assert os.path.isfile(f +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(f +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(f +
                          '.png'), 'ERROR: plotting did not create PNG file'

    # set up plot for radii
    plt_helper.newfig(textwidth=238.96, scale=1.0)

    exact_radii = []
    for key, item in results.items():
        computed_radii = sort_stats(filter_stats(item, type='computed_radius'),
                                    sortby='time')

        xcoords = [item0[0] for item0 in computed_radii]
        radii = [item0[1] for item0 in computed_radii]
        plt_helper.plt.plot(xcoords, radii, label=key[0] + ' ' + key[1])

        exact_radii = sort_stats(filter_stats(item, type='exact_radius'),
                                 sortby='time')

        diff = np.array([
            abs(item0[1] - item1[1])
            for item0, item1 in zip(exact_radii, computed_radii)
        ])
        max_pos = int(np.argmax(diff))
        assert max(
            diff
        ) < 0.07, 'ERROR: computed radius is too far away from exact radius, got %s' % max(
            diff)
        assert 0.028 < computed_radii[max_pos][0] < 0.03, \
            'ERROR: largest difference is at wrong time, got %s' % computed_radii[max_pos][0]

    xcoords = [item[0] for item in exact_radii]
    radii = [item[1] for item in exact_radii]
    plt_helper.plt.plot(xcoords,
                        radii,
                        color='k',
                        linestyle='--',
                        linewidth=1,
                        label='exact')

    plt_helper.plt.ylabel('radius')
    plt_helper.plt.xlabel('time')
    plt_helper.plt.grid()
    plt_helper.plt.legend()

    # save plot, beautify
    f = fname + '_radii'
    plt_helper.savefig(f)

    assert os.path.isfile(f +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(f +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(f +
                          '.png'), 'ERROR: plotting did not create PNG file'

    # set up plot for interface width
    plt_helper.newfig(textwidth=238.96, scale=1.0)

    interface_width = []
    for key, item in results.items():
        interface_width = sort_stats(filter_stats(item,
                                                  type='interface_width'),
                                     sortby='time')
        xcoords = [item[0] for item in interface_width]
        width = [item[1] for item in interface_width]
        plt_helper.plt.plot(xcoords, width, label=key[0] + ' ' + key[1])

    xcoords = [item[0] for item in interface_width]
    init_width = [interface_width[0][1]] * len(xcoords)
    plt_helper.plt.plot(xcoords,
                        init_width,
                        color='k',
                        linestyle='--',
                        linewidth=1,
                        label='exact')

    plt_helper.plt.ylabel('interface width')
    plt_helper.plt.xlabel('time')
    plt_helper.plt.grid()
    plt_helper.plt.legend()

    # save plot, beautify
    f = fname + '_interface'
    plt_helper.savefig(f)

    assert os.path.isfile(f +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(f +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(f +
                          '.png'), 'ERROR: plotting did not create PNG file'

    return None
예제 #17
0
def create_plots(cwd=''):
    """
    Function to visualize the results of the Gray-Scott show case

    Args:
        cwd: current working directory
    """

    ref = 'PFASST_GRAYSCOTT_stats_hf_NOFAULT_P32.npz'

    list = [('PFASST_GRAYSCOTT_stats_hf_SPREAD_P32.npz', 'SPREAD', '1-sided',
             'red', 's'),
            ('PFASST_GRAYSCOTT_stats_hf_INTERP_P32.npz', 'INTERP', '2-sided',
             'orange', 'o'),
            ('PFASST_GRAYSCOTT_stats_hf_SPREAD_PREDICT_P32.npz',
             'SPREAD_PREDICT', '1-sided+corr', 'blue', '^'),
            ('PFASST_GRAYSCOTT_stats_hf_INTERP_PREDICT_P32.npz',
             'INTERP_PREDICT', '2-sided+corr', 'green', 'd'),
            ('PFASST_GRAYSCOTT_stats_hf_NOFAULT_P32.npz', 'NOFAULT',
             'no fault', 'black', 'v')]
    # list = [('PFASST_GRAYSCOTT_stats_hf_INTERP_P32_cN512.npz', 'INTERP', '2-sided', 'orange', 'o'),
    #        ('PFASST_GRAYSCOTT_stats_hf_INTERP_PREDICT_P32_cN512.npz', 'INTERP_PREDICT', '2-sided+corr', 'green', 'd'),
    #        ('PFASST_GRAYSCOTT_stats_hf_NOFAULT_P32.npz', 'NOFAULT', 'no fault', 'black', 'v')]

    nprocs = 32

    xtick_dist = 16

    minstep = 288
    maxstep = 384
    # minstep = 0
    # maxstep = 640

    nblocks = int(640 / nprocs)

    # maxiter = 14
    nsteps = 0
    maxiter = 0
    vmax = -99
    vmin = 99
    for file, strategy, label, color, marker in list:
        data = np.load(cwd + 'data/' + file)

        iter_count = data['iter_count'][minstep:maxstep]
        residual = data['residual'][:, minstep:maxstep]

        residual[residual <= 0] = 1E-99
        residual = np.log10(residual)
        vmin = -9
        vmax = max(vmax, int(np.amax(residual)))

        maxiter = max(maxiter, int(max(iter_count)))
        nsteps = max(nsteps, len(iter_count))

    data = np.load(cwd + 'data/' + ref)
    ref_iter_count = data['iter_count'][nprocs - 1::nprocs]

    plt_helper.setup_mpl()

    fig, ax = plt_helper.newfig(textwidth=238.96, scale=2.0, ratio=0.3)

    ax.plot(range(nblocks), [0] * nblocks, 'k-', linewidth=2)

    ymin = 99
    ymax = 0
    for file, strategy, label, color, marker in list:

        if file is not ref:
            data = np.load(cwd + 'data/' + file)
            iter_count = data['iter_count'][nprocs - 1::nprocs]

            ymin = min(ymin, min(iter_count - ref_iter_count))
            ymax = max(ymax, max(iter_count - ref_iter_count))

            ax.plot(range(nblocks),
                    iter_count - ref_iter_count,
                    color=color,
                    label=label,
                    marker=marker,
                    linestyle='',
                    linewidth=lw,
                    markersize=ms)

    ax.set_xlabel('block')
    ax.set_ylabel(r'$K_\mathrm{add}$')
    # ax.set_title('ALL')
    ax.set_xlim(-1, nblocks)
    ax.set_ylim(-1 + ymin, ymax + 1)
    ax.legend(loc=2, numpoints=1, fontsize=fs)
    ax.tick_params(axis='both', which='major', labelsize=fs)

    # save plot, beautify
    fname = 'data/GRAYSCOTT_Kadd_vs_NOFAULT_hf'
    plt_helper.savefig(fname)

    assert os.path.isfile(fname +
                          '.pdf'), 'ERROR: plotting did not create PDF file'
    assert os.path.isfile(fname +
                          '.pgf'), 'ERROR: plotting did not create PGF file'
    assert os.path.isfile(fname +
                          '.png'), 'ERROR: plotting did not create PNG file'

    for file, strategy, label, color, marker in list:

        data = np.load(cwd + 'data/' + file)

        residual = data['residual'][:, minstep:maxstep]
        stats = data['hard_stats']

        residual[residual <= 0] = 1E-99
        residual = np.log10(residual)

        fig, ax = plt_helper.newfig(textwidth=238.96, scale=2.0, ratio=0.3)

        cmap = plt_helper.plt.get_cmap('Reds', vmax - vmin + 1)
        pcol = ax.pcolor(residual, cmap=cmap, vmin=vmin, vmax=vmax)
        pcol.set_edgecolor('face')

        if file is not ref:
            for item in stats:
                if item[0] in range(minstep, maxstep):
                    ax.text(item[0] + 0.5 - (maxstep - nsteps),
                            item[1] - 1 + 0.5,
                            'X',
                            horizontalalignment='center',
                            verticalalignment='center')

        ax.axis([0, nsteps, 0, maxiter - 1])

        ticks = np.arange(vmin, vmax + 1, 2)
        tickpos = np.linspace(ticks[0] + 0.5, ticks[-1] - 0.5, len(ticks))
        cax = plt_helper.plt.colorbar(pcol, ticks=tickpos, pad=0.02)
        cax.set_ticklabels(ticks)
        cax.ax.tick_params(labelsize=fs)

        cax.set_label('log10(residual)')
        ax.tick_params(axis='both', which='major', labelsize=fs)

        ax.set_xlabel('step')
        ax.set_ylabel('iteration')

        ax.set_yticks(np.arange(1, maxiter, 2) + 0.5, minor=False)
        ax.set_xticks(np.arange(0, nsteps, xtick_dist) + 0.5, minor=False)
        ax.set_yticklabels(np.arange(1, maxiter, 2) + 1, minor=False)
        ax.set_xticklabels(np.arange(minstep, maxstep, xtick_dist),
                           minor=False)

        # plt.title(strategy.replace('_', '-'))
        # plt.tight_layout()

        fname = 'data/GRAYSCOTT_steps_vs_iteration_hf_' + strategy
        plt_helper.savefig(fname)

        assert os.path.isfile(
            fname + '.pdf'), 'ERROR: plotting did not create PDF file'
        assert os.path.isfile(
            fname + '.pgf'), 'ERROR: plotting did not create PGF file'
        assert os.path.isfile(
            fname + '.png'), 'ERROR: plotting did not create PNG file'
예제 #18
0
def visualize():

    errors_sdc_M = np.load('errors_sdc_M.npy')
    errors_sdc_noM = np.load('errors_sdc_noM.npy')
    errors_mlsdc_M = np.load('errors_mlsdc_M.npy')
    errors_mlsdc_noM = np.load('errors_mlsdc_noM.npy')

    plt_helper.setup_mpl()

    plt_helper.newfig(240, 1, ratio=0.8)

    plt_helper.plt.semilogy([err[0] for err in errors_sdc_noM], [err[1] for err in errors_sdc_noM], lw=2,
                            marker='s',
                            markersize=6,
                            color='darkblue',
                            label='SDC without M')

    plt_helper.plt.xlim([0, 11])
    plt_helper.plt.ylim([6E-09, 2E-03])
    plt_helper.plt.xlabel('iteration')
    plt_helper.plt.ylabel('error')
    plt_helper.plt.legend()
    plt_helper.plt.grid()

    plt_helper.savefig('error_SDC_noM_CG_4')

    plt_helper.newfig(240, 1, ratio=0.8)

    plt_helper.plt.semilogy([err[0] for err in errors_sdc_noM], [err[1] for err in errors_sdc_noM], lw=2,
                            color='darkblue',
                            marker='s',
                            markersize=6,
                            label='SDC without M')
    plt_helper.plt.semilogy([err[0] for err in errors_sdc_M], [err[1] for err in errors_sdc_M], lw=2,
                            marker='o',
                            markersize=6,
                            color='red',
                            label='SDC with M')

    plt_helper.plt.xlim([0, 11])
    plt_helper.plt.ylim([6E-09, 2E-03])
    plt_helper.plt.xlabel('iteration')
    plt_helper.plt.ylabel('error')
    plt_helper.plt.legend()
    plt_helper.plt.grid()

    plt_helper.savefig('error_SDC_M_CG_4')

    plt_helper.newfig(240, 1, ratio=0.8)

    plt_helper.plt.semilogy([err[0] for err in errors_mlsdc_noM], [err[1] for err in errors_mlsdc_noM], lw=2,
                            marker='s',
                            markersize=6,
                            color='darkblue',
                            label='MLSDC without M')

    plt_helper.plt.xlim([0, 11])
    plt_helper.plt.ylim([6E-09, 2E-03])
    plt_helper.plt.xlabel('iteration')
    plt_helper.plt.ylabel('error')
    plt_helper.plt.legend()
    plt_helper.plt.grid()

    plt_helper.savefig('error_MLSDC_noM_CG_4')

    plt_helper.newfig(240, 1, ratio=0.8)

    plt_helper.plt.semilogy([err[0] for err in errors_mlsdc_noM], [err[1] for err in errors_mlsdc_noM], lw=2,
                            color='darkblue',
                            marker='s',
                            markersize=6,
                            label='MLSDC without M')
    plt_helper.plt.semilogy([err[0] for err in errors_mlsdc_M], [err[1] for err in errors_mlsdc_M], lw=2,
                            marker='o',
                            markersize=6,
                            color='red',
                            label='MLSDC with M')

    plt_helper.plt.xlim([0, 11])
    plt_helper.plt.ylim([6E-09, 2E-03])
    plt_helper.plt.xlabel('iteration')
    plt_helper.plt.ylabel('error')
    plt_helper.plt.legend()
    plt_helper.plt.grid()

    plt_helper.savefig('error_MLSDC_M_CG_4')