Exemplo n.º 1
0
def run(tn=4000, space_order=4, kernel='OT4', nbpml=40, tolerance=0.01, parallel_compression=True, filename='', **kwargs):
    if kernel in ['OT2', 'OT4']:
        solver = overthrust_setup(filename=filename, tn=tn, nbpml=nbpml, space_order=space_order, kernel=kernel, **kwargs)
    elif kernel == 'TTI':
        solver = overthrust_setup_tti(filename=filename, tn=tn, nbpml=nbpml, space_order=space_order, kernel=kernel, **kwargs)
    else:
        raise ValueError()

    total_timesteps = solver.source.time_range.num
    u = None
    rec = None
    results = []
    print(total_timesteps)
    for t in range(1, total_timesteps-1):
        return_values = solver.forward(u=u, rec=rec, time_m=t, time_M=t, save=False)
        rec = return_values[0]
        last_time_step = rec.shape[0] - 1
        u = return_values[1]
        uncompressed = u.data[t+1]
        print(np.linalg.norm(uncompressed))
        with Timer(factor=1000) as time1:
            compressed = compress(uncompressed, tolerance=tolerance, parallel=parallel_compression)
        result = (t, len(uncompressed.tostring())/float(len(compressed)), time1.elapsed)
        results.append(result)
        print(result)

    ret = solver.forward(save=False)
    assert(ret[1].shape == u.shape)
    assert(np.all(np.isclose(ret[1].data, u.data)))
    
    with open('results.csv', 'w+') as csvfile:
        writer = csv.writer(csvfile)
        for row in results:
            writer.writerow(row)
Exemplo n.º 2
0
def run(original,
        lossy,
        filename='',
        space_order=4,
        kernel='OT2',
        nbpml=40,
        **kwargs):

    solver = overthrust_setup(filename=filename,
                              nbpml=nbpml,
                              space_order=space_order,
                              kernel=kernel,
                              **kwargs)
    # Load two timesteps of each
    # Uncompressed should be saved by simple.py
    # Compressed one should be generated on demand by makefile
    # (dedicated script to convert uncompressed previous timestep to compressed)
    with h5py.File(original) as original_file:
        correct_u = original_file['data'][()]
    with h5py.File(lossy) as lossy_file:
        lossy_u = lossy_file['data'][()]

    u = TimeFunction(name="u",
                     grid=solver.model.grid,
                     time_order=2,
                     space_order=solver.space_order)
    fig, im_correct_u, im_lossy_u, im_model = init_video(
        solver, correct_u, lossy_u)
    im_corrects = [[im_correct_u, im_model]]
    im_lossys = [[im_lossy_u, im_model]]
    errors = []
    print("steps, error_norm, max_error")
    for steps in range(1, 50, 10):
        u.data[:] = correct_u[:]
        ini_t = 2000
        rec, u_correct_p, summary = solver.forward(save=False,
                                                   u=u,
                                                   time_M=(ini_t + steps),
                                                   time_m=ini_t)
        u_correct_p = u_correct_p.data[(ini_t + steps) % 3].copy()
        u.data[:] = lossy_u[:]
        rec, u_lossy_p, summary = solver.forward(save=False,
                                                 u=u,
                                                 time_M=(ini_t + steps),
                                                 time_m=ini_t)
        u_lossy_p = u_lossy_p.data[(ini_t + steps) % 3].copy()
        error = u_correct_p - u_lossy_p
        it_errors = (steps, np.linalg.norm(error), np.max(error.data))
        print(", ".join([str(x) for x in it_errors]))
        errors.append(it_errors)
        im_corrects.append([plot_field(u_correct_p), im_model])
        im_lossys.append([plot_field(u_lossy_p), im_model])

    finalize_video(fig, im_corrects, original + ".mp4")
    finalize_video(fig, im_lossys, lossy + ".mp4")
def run_forward_error(filename, space_order=4, kernel='OT4', tolerance=0.001,
                      nbpml=10, dtype=np.float64, **kwargs):
    # Setup solver

    solver = overthrust_setup(filename=filename, tn=1000, nbpml=nbpml,
                              space_order=space_order, kernel=kernel,
                              dtype=dtype, **kwargs)

    # Run for nt/2 timesteps as a warm up
    nt = solver.geometry.time_axis.num
    nt_2 = int(floor(nt/2))

    print("first run")
    rec, u, profiler = solver.forward(time=nt_2)
    print("second run")
    _, u2, _ = solver.forward(time=nt_2)

    assert(np.allclose(u.data, u2.data))

    # Store last timestep

    u_comp = TimeFunction(name='u', grid=solver.model.grid, time_order=2,
                          space_order=solver.space_order)
    u_comp.data  # Force memory allocation
    # Compress-decompress with given tolerance

    compressed_u = compress(get_data(u), tolerance=tolerance, parallel=True)

    mem = get_data(u_comp)
    mem[:] = decompress(compressed_u, mem.shape, mem.dtype,
                        tolerance=tolerance)

    for i in range(nt_2):
        # Run for i steps (original last time step and compressed version)
        clear_cache()
        u_copy = TimeFunction(name='u', grid=solver.model.grid, time_order=2,
                              space_order=solver.space_order)
        u_copy.data[:] = u.data
        _, u_original, _ = solver.forward(time_m=nt_2, time_M=nt_2+i, u=u_copy)

        u_l_copy = TimeFunction(name='u', grid=solver.model.grid, time_order=2,
                                space_order=solver.space_order)
        u_l_copy.data[:] = u_comp.data
        _, u_lossy, _ = solver.forward(time_m=nt_2, time_M=nt_2+i, u=u_l_copy)

        # Compare and report error metrics

        data = get_all_errors(get_data(u_original), get_data(u_lossy))
        # error_field = u_original.data[nt_2+i] - u_lossy.data[nt_2+i]
        data['ntimesteps'] = i
        data['atol'] = tolerance
        write_results(data, "forward_prop_results.csv")