//  PERC_RES.C
//  Percolation results
//  © 2021 Peter J. Meyer

#include "iss.h"

#include <process.h>

#define W 13

extern double min_p, max_p, precision2;     //  PERC.C
extern int total_num_configs;
extern char anomalies[][64];

/*---------------------------------------------------*/
void write_percolation_results(int num_iterations_done)
{
int i, iter, ch, p_num;
double p;
FILE *of;

while  ( !open_file(output_filepath,"wt",&of) )
    {
    printf("\nCannot open output file %s.",output_filepath);
    printf("\nPerhaps this file is already open?  Retry or quit? (R|Q) ");
    ch = getch();
    if ( ! ( ch == 'R' || ch == 'r' ) )
        exit(1);
    }

fprintf(of,"\nDetermination of %s percolation threshold.",
    ( goal_is_site_percolation_threshold ? "site" : "bond") );

fprintf(of,"\nRun begun at %s %s",run_start_date,run_start_time);

if ( escape_key_termination )
    fprintf(of,"\nEscape key was pressed.\n");

fprintf(of,"\nlattice dimensionality/type/size:  %dd  %s (%d)  %d",
    dimensionality,lattice_types[l_type].name,
    lattice_types[l_type].coord_num,size);
for ( i=2; i<=dimensionality; i++ )
    fprintf(of,"x%d",size);
if ( direction_table_is_parity_dependent && MAJOR_AXIS_SPECIFICATION_PERMITTED )
    fprintf(of,"\nmajor axis:          %d",major_axis);

#if FREE_BOUNDARY_CONDITIONS_POSSIBLE
fprintf(of,"\nboundary conditions:               %s",
    (free_boundary_conditions ? "free" : "periodic"));
#endif

if ( goal_is_site_percolation_threshold )
    fprintf(of,"\nbond concentration:                %.3f",bond_concentration);
else 
    fprintf(of,"\nsite concentration:                %.3f",site_concentration);

fprintf(of,"\nprecision:                         +/- %.*f",num_decimal_places,precision);

fprintf(of,"\n");

for ( iter=0; iter<num_iterations_done; iter++ )
    {
    if ( iter > 0 )
        {
        if ( iterations[iter-1].repeat )
            fprintf(of,"\nRepeating because %s.\n",anomalies[iterations[iter-1].anom_code]);
        else if ( iterations[iter-1].anom_code == 4 )
            fprintf(of,"\nAdjusting range because %s.\n",anomalies[4]);
        }

    fprintf(of,"\nIteration #%d, level #%d, epsilon = %.*f",
        iter+1,iterations[iter].level,num_decimal_places+1,iterations[iter].epsilon);

    fprintf(of,"\nRange %.*f - %.*f, %.*f in steps of %.*f, %s configurations.\n",
        num_decimal_places+1,iterations[iter].min_p,
        num_decimal_places+1,iterations[iter].max_p,
        num_decimal_places+1,iterations[iter].range,
        num_decimal_places+1,iterations[iter].range/(NUM_CONCENTRATIONS-1),
        ultoa_commas(iterations[iter].num_configurations,temp));

    for ( p_num=0; p_num<NUM_CONCENTRATIONS; p_num++ )
        {
        p = iterations[iter].min_p 
            + p_num*(iterations[iter].range/(NUM_CONCENTRATIONS-1));
        fprintf(of,"%*.*f",W,num_decimal_places+1,p);
        }

    fprintf(of,"\n");

    for ( p_num=0; p_num<NUM_CONCENTRATIONS; p_num++ )
        {
        if ( iterations[iter].perc_data[p_num][1] == 200 )
            break;
        if ( iterations[iter].perc_data[p_num][1] == 0 )
            fprintf(of,"%*d",W,0);
        else if ( iterations[iter].perc_data[p_num][1] == 100 )
            fprintf(of,"%*d",W,100);
        else
            fprintf(of,"%*.2f",W,iterations[iter].perc_data[p_num][1]);
        }

    fprintf(of,"\n");
    }

if ( iterations[--iter].anom_code == 5 )
    fprintf(of,"\nQuit because %s.\n",anomalies[5]);

if ( precision < 0.5 )
    {
    fprintf(of,"\nThe %s percolation threshold for this lattice"
           "\n(with %s concentration %.3f)",
            ( goal_is_site_percolation_threshold ? "site" : "bond" ), 
            ( goal_is_site_percolation_threshold ? "bond" : "site" ), 
            ( goal_is_site_percolation_threshold ? 
                bond_concentration : site_concentration ));
    fprintf(of,"\nis %.*f +/- %.*f (%.*f - %.*f)\n",
            num_decimal_places+1, (max_p+min_p)/2,
            num_decimal_places+1, precision2,
            num_decimal_places+1, (max_p+min_p)/2-precision2,
            num_decimal_places+1, (max_p+min_p)/2+precision2);
    }


fprintf(of,"\n%s configurations generated.\n",ultoa_commas(total_num_configs,temp));

write_stack_data(of);       //  STACK.C

fprintf(of,"\nRun ended at %s %s",run_end_date,run_end_time);

fprintf(of,"\nRun time = %s",seconds_to_time_str((unsigned long)run_time,temp));

fclose(of);

spawnlp(P_NOWAIT,"notepad.exe","notepad.exe",output_filepath,NULL);
}