/** @file main.cpp
 *
 *  @author Cory Alexander Balaton (coryab)
 *  @author Janita Ovidie Sandtrøen Willumsen (janitaws)
 *
 *  @version 1.0
 *
 *  @brief Implementation of the testing library
 *
 *  @bug No known bugs
 * */
#include "WaveSimulation.hpp"
#include "utils.hpp"
#include <fstream>

void probability_deviation()
{
    WaveSimulation sim_narrow(.005, 2.5e-5, .008, .25, .5, .05, .05, 200., 0.);

    WaveSimulation sim_wide(.005, 2.5e-5, .008, .25, .5, .05, .10, 200., 0.,
                            0.02, .5, .05, .05, 2);

    std::ofstream ofile;
    utils::mkpath("data");
    ofile.open("data/probability_deviation.txt");

    double sum_narrow, sum_wide;

    for (size_t i = 0; i < 320; i++) {
        sum_narrow = 0;
        sum_wide = 0;
        for (size_t j = 0; j < sim_narrow.U.n_elem; j++) {
            sum_narrow += (sim_narrow.U(j) * std::conj(sim_narrow.U(j))).real();
            sum_wide += (sim_wide.U(j) * std::conj(sim_wide.U(j))).real();
        }

        sim_narrow.step();
        sim_wide.step();
        ofile << i << ',' << utils::scientific_format(sum_narrow) << ','
              << utils::scientific_format(sum_wide) << '\n';
    }

    ofile.close();
}

void color_map()
{
    WaveSimulation sim(.005, 2.5e-5, .002, .25, .5, .05, .20, 200., 0., 0.02,
                       .5, .05, .05, 2);

    std::ofstream ofile;
    ofile.open("data/color_map.txt");
    ofile << sim.N << '\n';
    sim.write_U(ofile);
    for (size_t i = 0; i < 40; i++) {
        sim.step();
    }
    sim.write_U(ofile);
    for (size_t i = 0; i < 40; i++) {
        sim.step();
    }
    sim.write_U(ofile);
    ofile.close();
}

void detector_screen()
{
    WaveSimulation *sim = new WaveSimulation(
        .005, 2.5e-5, .002, .25, .5, .05, .20, 200., 0., 0.02, .5, .05, .05, 1);

    std::ofstream ofile;
    utils::mkpath("data/screen");
    ofile.open("data/screen/single_slit.txt");
    ofile << sim->N << '\n';
    for (size_t i = 0; i < 80; i++) {
        sim->step();
    }
    sim->write_U(ofile);
    ofile.close();

    delete sim;
    sim = new WaveSimulation(.005, 2.5e-5, .002, .25, .5, .05, .20, 200., 0.,
                             0.02, .5, .05, .05, 2);

    ofile.open("data/screen/double_slit.txt");
    ofile << sim->N << '\n';
    for (size_t i = 0; i < 80; i++) {
        sim->step();
    }
    sim->write_U(ofile);
    ofile.close();

    delete sim;
    sim = new WaveSimulation(.005, 2.5e-5, .002, .25, .5, .05, .20, 200., 0.,
                             0.02, .5, .05, .05, 3);

    ofile.open("data/screen/triple_slit.txt");
    ofile << sim->N << '\n';
    for (size_t i = 0; i < 80; i++) {
        sim->step();
    }
    sim->write_U(ofile);
    ofile.close();
}

int main()
{
    //probability_deviation();
    //color_map();
    detector_screen();
    
    //std::ofstream ofile;
    //ofile.open("test.txt");
    //WaveSimulation sim(.005, 2.5e-5, .008, .25, .5, .05, .10, 200., 0.,
                            //0.02, .5, .05, .05, 2);
    //sim.solve(ofile);


    return 0;
}
