def solve(parameter_file, dataset_file, num_points):
    if dataset_file is not None:
        data = load_dataset(dataset_file)
    else:
        data = generate_random_dataset(num_points)

    params = load_parameter_file(parameter_file)
    sim = Simulator(**params)
    solution = sim.evolve(data)

    plot_fitness(sim)
    plot_solution(data, solution)
def solve(parameter_file, dataset_file, num_points):
    if dataset_file is not None:
        data = load_dataset(dataset_file)
    else:
        data = generate_random_dataset(num_points)

    params = load_parameter_file(parameter_file)
    sim = Simulator(**params)
    solution = sim.evolve(data)

    plot_fitness(sim)
    plot_solution(data, solution)
Exemple #3
0
    phiNumerical = sch.WB(phi, c, nt)
elif scheme == 'TVD':
    phiNumerical = sch.TVD(phi, c, nt)

# Extra schemes (semi-langrangian)
elif scheme == 'SL1':
    phiNumerical = sch.SL1(phi, u, dt, dx, nt)
elif scheme == 'SL2':
    phiNumerical = sch.SL2(phi, u, dt, dx, nt)
elif scheme == 'SL3':
    phiNumerical = sch.SL3(phi, u, dt, dx, nt)
elif scheme == 'SL3QM':
    phiNumerical = sch.SL3(phi, u, dt, dx, nt, monotone=True)

else:
    raise Exception(
        "Option for scheme is not valid, check again. Make sure input is a string."
    )

#=========================================================================
# Error analysis
#=========================================================================
L2, phi_error = ea.L2ErrorNorm(phiNumerical, phiAnalytical)
# Print final total mass
ea.compute_mass(phiNumerical, 'Final')

#=========================================================================
# Plotting
#=========================================================================
plotting.plot_solution(x, phi, phiNumerical, phiAnalytical, scheme)
plotting.plot_error_norm(x, L2, phi_error, scheme)
Exemple #4
0
def main():
    parser = argparse.ArgumentParser(
        description='Run experiment and save the derivative results.')
    parser.add_argument(
        'yaml_config',
        help='Config YAML file specifying the parameters of experiment')

    args = parser.parse_args()
    with open(args.yaml_config, 'r') as f:
        config = yaml.load(f, Loader=yaml.FullLoader)

    if not validate_config(config):
        sys.exit(1)

    initial = None
    path_to_initial = config.get('path_to_initial')
    if path_to_initial is not None:
        initial = np.load(path_to_initial)

    e = experiment.Experiment(
        kernel_type=config.get('kernel_type'),
        simulation_type=config.get('simulation_type'),
        dt=config.get('dt'),
        lmbda=config.get('lambda'),
        alpha=config.get('alpha'),
        num_equations=config.get('num_equations'),
        initial=initial,
        final_lambda=config.get('final_lambda'),
        lambda_decay_type=config.get('lambda_decay_type'))

    name = config['experiment_name']

    print(f'Starting experiment {name}')
    e.run_experiment(name, config.get('num_iters'),
                     config.get('checkpoint_frequency'))

    print('Simulation finished. Loading results')
    iters, solutions = util.load_solutions(name)
    results_dir = os.path.join(experiment.DATA_DIRECTORY, name)

    lambda_history = util.load_param_history(name)
    ts = iters * config['dt']

    print('Computing moments')
    zeroth = util.compute_batch_moments(solutions, 0)
    first = util.compute_batch_moments(solutions, 1)
    second = util.compute_batch_moments(solutions, 2)

    print('Generating plots and movies')
    matplotlib.rcParams["axes.formatter.useoffset"] = False
    k = np.arange(solutions.shape[1] - 1) + 1

    fig, _ = plotting.plot_moments(ts, zeroth, first, second)
    fig.savefig(os.path.join(results_dir, 'moments.png'))

    cutoff = 10
    fig, _ = plotting.plot_moments(ts[cutoff:], zeroth[cutoff:],
                                   first[cutoff:], second[cutoff:])
    fig.savefig(os.path.join(results_dir, 'moments_cutoff.png'))

    fig, _ = plotting.plot_solution(k, solutions[-1, 1:])
    fig.savefig(os.path.join(results_dir, 'final_solution.png'))

    fig, _ = plotting.plot_parameter_history(ts, lambda_history, r'$\lambda$')
    fig.savefig(os.path.join(results_dir, 'lambda.png'))

    # Account for 1-indexing.
    plotting.create_solution_animation(
        ts, k, solutions[:, 1:], os.path.join(results_dir, 'solutions.mp4'))

    print(f'Experiment {name} done. Results saved to {results_dir}')
for i in range(n_iter):
    if is_outer(rank):
        comm.Recv(bc_rec_from_1, source=1)
        rhs = createMatrix.generate_outer_rhs(n, bc_rec_from_1)
    else:
        rhs = createMatrix.generate_inner_rhs(rhs_init.copy(), bc_rec_from_0, bc_rec_from_2)
    prev_sol = sol
    # sol = np.linalg.solve(A, rhs)
    sol = sl.lu_solve(lu_piv, rhs)
    sol = omega * sol + (1 - omega) * prev_sol
    if is_outer(rank):
        bc_send_to_1 = sol[::n].copy()  # copy is needed since mpi requires contigious data to send
        comm.Send(bc_send_to_1, dest=1)
    else:
        bc_send_to_0 = (sol[n * (n + 1) :: n] - sol[n * (n + 1) + 1 :: n]) / dx
        bc_send_to_2 = (sol[n - 1 : n * n : n] - sol[n - 2 : n * n : n]) / dx
        comm.Send(bc_send_to_0, dest=0)
        comm.Send(bc_send_to_2, dest=2)
        comm.Recv(bc_rec_from_0, source=0)
        comm.Recv(bc_rec_from_2, source=2)

if is_outer(rank):
    comm.Send(sol, dest=1)
else:
    sol1 = np.zeros((n * n, 1))
    sol2 = sol
    sol3 = np.zeros((n * n, 1))
    comm.Recv(sol1, source=0)
    comm.Recv(sol3, source=2)
    plotting.plot_solution(sol1, sol2, sol3, n)