<commit_before>/* 
 * File:   simulation.h
 * Author: <NAME>
 *
 * Created on August 3, 2011, 4:58 PM
 */

#ifndef SIMULATION_H
#define	SIMULATION_H

template <class EF, class SYS, class SAM> class Simulation;
template <class EF, class SYS, class SAM> std::ostream & operator<<(std::ostream & out, Simulation<EF, SYS, SAM> * simulation);

template <class EF, class SYS, class SAM> class Simulation {
private:
    EF * ENERGY_FUNCTION;
    struct timeval start_time, end_time, sweep_start, sweep_end;
    int lammpstrj_timestep, lammpstrj_snapshot_counter;
    std::ofstream LAMMPSTRJ_FILE;

    void default_initialize_sampling_parameters();
    void mc_sweep();

public:
    SYS SYSTEM;

    Simulation();
    ~Simulation();

    void equilibrate();
    void run_mc();

    std::vector <SAM *> SAMPLERS;
    int DATA_SAMPLING_RATE;
    int RELATIVE_LAMMPSTRJ_SNAPSHOT_RATE;

    void initialize_sampling();
    void sample_data();
    void finish_sampling();
    void print_individual_sampler_results();

    //void turn_on_lammpstrj_sampler();
    void add_rdf_sampler();
    void add_ion_pair_distance_sampler();

    std::string config_filename, lammpstrj_filename;
    void write_lammpstrj_snapshot();
    void write_config_snapshot();

    bool IS_LAMMPSTRJ_SAMPLING;

    friend std::ostream & operator<< <> (std::ostream & out, Simulation<EF, SYS, SAM> * simulation);
};

template <class EF, class SYS, class SAM> Simulation<EF, SYS, SAM>::Simulation() {
    ENERGY_FUNCTION = new EF(SYSTEM);
}

template <class EF, class SYS, class SAM> Simulation<EF, SYS, SAM>::~Simulation() {
    delete ENERGY_FUNCTION;
    SAMPLERS.clear();
    if (LAMMPSTRJ_FILE.is_open())
        LAMMPSTRJ_FILE.close();
}

template <class EF, class SYS, class SAM> void Simulation<EF, SYS, SAM>::default_initialize_sampling_parameters() {
    DATA_SAMPLING_RATE = 20;
    IS_LAMMPSTRJ_SAMPLING = false;
    RELATIVE_LAMMPSTRJ_SNAPSHOT_RATE = 10;
    lammpstrj_timestep = 0;
    lammpstrj_snapshot_counter = 0;
}

template <class EF, class SYS, class SAM> void Simulation<EF, SYS, SAM>::equilibrate() {
    ENERGY_FUNCTION->initialize_calculations();
    gettimeofday(&start_time, NULL);
    for (int h = 0; h < SYSTEM.NUM_EQUILIBRATION_SWEEPS; h++) {
        mc_sweep();
        std::cerr << "Equilibration MC sweep " << h + 1 << " of " << SYSTEM.NUM_EQUILIBRATION_SWEEPS << " complete." << std::endl;
    }
    gettimeofday(&end_time, NULL);
    std::cerr << "Equilibration completed in " << std::setprecision(10) << timeval_diff(&end_time, &start_time) / 1000000.0 / 3600.0 << " hours." << std::endl;
    return;
}

template <class EF, class SYS, class SAM> void Simulation<EF, SYS, SAM>::run_mc() {
    initialize_sampling();
    ENERGY_FUNCTION->initialize_calculations();
    gettimeofday(&start_time, NULL);
    for (int h = 0; h < SYSTEM.NUM_MC_SWEEPS; h++) {
        mc_sweep();
        if (h % DATA_SAMPLING_RATE == 0)
            sample_data();
        std::cerr << "MC sweep " << h + 1 << " of " << SYSTEM.NUM_MC_SWEEPS << " complete." << std::endl;
    }
    finish_sampling();
    gettimeofday(&end_time, NULL);
    std::cerr << "MC run completed in " << std::setprecision(10) << timeval_diff(&end_time, &start_time) / 1000000.0 / 3600.0 << " hours." << std::endl;<commit_msg>fixed sampling bug<commit_after>/* 
 * File:   simulation.h
 * Author: <NAME>
 *
 * Created on August 3, 2011, 4:58 PM
 */

#ifndef SIMULATION_H
#define	SIMULATION_H

template <class EF, class SYS, class SAM> class Simulation;
template <class EF, class SYS, class SAM> std::ostream & operator<<(std::ostream & out, Simulation<EF, SYS, SAM> * simulation);

template <class EF, class SYS, class SAM> class Simulation {
private:
    EF * ENERGY_FUNCTION;
    struct timeval start_time, end_time, sweep_start, sweep_end;
    int lammpstrj_timestep, lammpstrj_snapshot_counter;
    std::ofstream LAMMPSTRJ_FILE;

    void default_initialize_sampling_parameters();
    void mc_sweep();

public:
    SYS SYSTEM;

    Simulation();
    ~Simulation();

    void equilibrate();
    void run_mc();

    std::vector <SAM *> SAMPLERS;
    int DATA_SAMPLING_RATE;
    int RELATIVE_LAMMPSTRJ_SNAPSHOT_RATE;

    void initialize_sampling();
    void sample_data();
    void finish_sampling();
    void print_individual_sampler_results();

    //void turn_on_lammpstrj_sampler();
    void add_rdf_sampler();
    void add_ion_pair_distance_sampler();

    std::string config_filename, lammpstrj_filename;
    void write_lammpstrj_snapshot();
    void write_config_snapshot();

    bool IS_LAMMPSTRJ_SAMPLING;

    friend std::ostream & operator<< <> (std::ostream & out, Simulation<EF, SYS, SAM> * simulation);
};

template <class EF, class SYS, class SAM> Simulation<EF, SYS, SAM>::Simulation() {
    default_initialize_sampling_parameters();
    ENERGY_FUNCTION = new EF(SYSTEM);
}

template <class EF, class SYS, class SAM> Simulation<EF, SYS, SAM>::~Simulation() {
    delete ENERGY_FUNCTION;
    SAMPLERS.clear();
    if (LAMMPSTRJ_FILE.is_open())
        LAMMPSTRJ_FILE.close();
}

template <class EF, class SYS, class SAM> void Simulation<EF, SYS, SAM>::default_initialize_sampling_parameters() {
    DATA_SAMPLING_RATE = 20;
    IS_LAMMPSTRJ_SAMPLING = false;
    RELATIVE_LAMMPSTRJ_SNAPSHOT_RATE = 10;
    lammpstrj_timestep = 0;
    lammpstrj_snapshot_counter = 0;
}

template <class EF, class SYS, class SAM> void Simulation<EF, SYS, SAM>::equilibrate() {
    ENERGY_FUNCTION->initialize_calculations();
    gettimeofday(&start_time, NULL);
    for (int h = 0; h < SYSTEM.NUM_EQUILIBRATION_SWEEPS; h++) {
        mc_sweep();
        std::cerr << "Equilibration MC sweep " << h + 1 << " of " << SYSTEM.NUM_EQUILIBRATION_SWEEPS << " complete." << std::endl;
    }
    gettimeofday(&end_time, NULL);
    std::cerr << "Equilibration completed in " << std::setprecision(10) << timeval_diff(&end_time, &start_time) / 1000000.0 / 3600.0 << " hours." << std::endl;
    return;
}

template <class EF, class SYS, class SAM> void Simulation<EF, SYS, SAM>::run_mc() {
    ENERGY_FUNCTION->initialize_calculations();
    initialize_sampling();
    gettimeofday(&start_time, NULL);
    for (int h = 0; h < SYSTEM.NUM_MC_SWEEPS; h++) {
        mc_sweep();
        if (h % DATA_SAMPLING_RATE == 0)
            sample_data();
        std::cerr << "MC sweep " << h + 1 << " of " << SYSTEM.NUM_MC_SWEEPS << " complete." << std::endl;
    }
    finish_sampling();
    gettimeofday(&end_time, NULL);
    std::cerr << "MC run completed in " << std::setprecision(10) << timeval_diff(&end_time, &start_time) / 1000000.0 / 3600.0 << " hours." << std::endl;
{'fraction_non_alphanumeric': 0.1329392147946036, 'fraction_numerical': 0.014855237229043505, 'mean_word_length': 4.013677811550152, 'pattern_counts': {'":': 0, '<': 139, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4078814', 'n_tokens_mistral': 2514, 'n_tokens_neox': 2293, 'n_words': 526}