﻿#include "TTT_data.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include "misc/miscFunctions.hpp"
#include "cenos_exception.h"


TTT_data::TTT_data()
{
   /* austenizationTemperatureAc1 = 0;
    austenizationTemperatureAc3 = 0;

    dydx = 0;
    result = 0;
    temp_closest_high = 0;
    time_closest_high = 0;
    temp_closest_low = 0;
    time_closest_low = 0;

    austenizationTemperatureAc1 = 0;
    austenizationTemperatureAc3 = 0;
    martensiteStartTemperature = 0;*/
}

TTT_data::~TTT_data()
{
}

//read material properties from yaml file
TTT_data::TTT_data(std::string filename)
{
    ryml::Tree tree = misc::parseYaml(filename);

    if (tree["Ac1"] == nullptr)
    {
        throw cenos_exception("Material Ac1 temperature not found");
    }
    if (tree["Ac3"] == nullptr)
    {
        throw cenos_exception("Material Ac3 temperature not found");
    }
    if (tree["Ms"] == nullptr)
    {
        throw cenos_exception("Material Ms temperature not found");
    }
    if (tree["Start_time"] == nullptr)
    {
        throw cenos_exception("Material Start_time not found");
    }
    if (tree["Start_temperature"] == nullptr)
    {
        throw cenos_exception("Material Start_temperature not found");
    }
    if (tree["End_time"] == nullptr)
    {
        throw cenos_exception("Material End_time not found");
    }
    if (tree["End_temperature"] == nullptr)
    {
        throw cenos_exception("Material End_temperature not found");
    }
    tree["Name"] >> name_of_material;
    tree["Ac1"] >>  austenizationTemperatureAc1;
    tree["Ac3"] >> austenizationTemperatureAc3;
    tree["Ms"] >>  martensiteStartTemperature;
    tree["Start_time"] >> time1;
    tree["Start_temperature"] >> temperature1;
    tree["End_time"] >> time2;
    tree["End_temperature"] >> temperature2;
}



double TTT_data::interpolate(std::vector<double>& temperature_list, std::vector<double>& time_list, double temperature) 
{

    int size = temperature_list.size();

    for (int i = 0; i < size - 1; i++)
    {
        if ((temperature >= temperature_list[0]) && (temperature <= temperature_list[size-1]))  
        {

            if ((temperature > temperature_list[i]) && (temperature < temperature_list[i + 1]))

            {
                
                temp_closest_high = temperature_list[i], time_closest_high = time_list[i], temp_closest_low = temperature_list[i + 1], time_closest_low = time_list[i + 1];      // points on either side (unless beyond ends)

                dydx = (time_closest_high - time_closest_low) / (temp_closest_high - temp_closest_low);         // gradient

                result = time_closest_low + dydx * (temperature - temp_closest_low); // linear interpolation
                return result;
 
            }

            else
            {
                continue;
            }

        }
        else
        {
           
            return 0;
        }
        
    }
    
}


// get C-curve Start data
double TTT_data::getTStart(double current_temperature)
{

	return  interpolate(temperature1, time1, current_temperature);

	
}
// get C-curve End data
double TTT_data::getTEnd(double current_temperature)
{
	return interpolate(temperature2, time2, current_temperature);

	
}
void TTT_data::operator=(const TTT_data& that)
{
    time1 = that.time1;
    time2 = that.time2;
    temperature1 = that.temperature1;
    temperature2 = that.temperature2;

    name_of_material = that.name_of_material;
    source_name = that.source_name;

    austenizationTemperatureAc1 = that.austenizationTemperatureAc1;
    austenizationTemperatureAc3 = that.austenizationTemperatureAc3;
    martensiteStartTemperature = that.martensiteStartTemperature;

}