unit Direct_Task_Unit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Spin, ComCtrls, ShellCtrls, DBCtrls, FileCtrl,
  Math, HCArrays, HCConduct, HCMaterial, HCModel, UPohMath, UPohStrConv,
     BasicUnit, DirectUnit;

type
  TFDirect_Task = class(TForm)
    Material: TComboBox;
    MODEL: TLabel;
    DATA: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    BtnDirect: TButton;
    EData: TEdit;
    EResults: TEdit;
    BtnData: TButton;
    BtnResults: TButton;
    SampleL: TEdit;
    ETemp: TEdit;
    SelectFile: TOpenDialog;
    Label4: TLabel;
    Freq: TEdit;
    Label5: TLabel;
    EHloubka: TEdit;
    procedure BtnDirectClick(Sender: TObject);
    procedure BtnDataClick(Sender: TObject);
    procedure BtnResultsClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FDirect_Task: TFDirect_Task;
  DataFile, ResultFile:TextFile;
  path: string;

const
  mm=0.001;
  Cel_Kel=273.15;
  max_volL = 0.05*mm;

implementation

{$R *.dfm}

procedure TFDirect_Task.BtnDataClick(Sender: TObject);
begin
  SelectFile:= TOpenDialog.Create(Self);
  SelectFile.InitialDir:=GetCurrentDir;
  if SelectFile.Execute then begin
     EData.Text:=SelectFile.FileName;
     AssignFile(DataFile, SelectFile.FileName);
     end
     else EData.Text:='Soubor nebyl vybrán!';
  SelectFile.Free;
end;

procedure TFDirect_Task.BtnResultsClick(Sender: TObject);
begin
  SelectFile:=TOpenDialog.Create(Self);
  SelectFile.InitialDir:=GetCurrentDir;
  if SelectFile.Execute then begin
     EResults.Text:=SelectFile.FileName;
     AssignFile(ResultFile,SelectFile.FileName);
     end
     else EResults.Text:='Soubor nebyl vybrán!';
  path:=ExtractFilePath(SelectFile.FileName);
  SelectFile.Free;
end;


procedure TFDirect_Task.BtnDirectClick(Sender: TObject);
var
  MatDtb: tMatDB;
  model: tHCModel;
  MatID: integer;
  str: string;
  line: double;

  L,InitTemp,dt, hloubka: double;
  h: array [1..10000] of double;
  q: array of double;
  n, i: integer;

begin
  BtnDirect.Cursor:=crHourGlass;
//Nacteni dialogoveho okna
  str:=Material.Text;
  L := StrToFloat(SampleL.Text)*mm;        // mm
  InitTemp:=StrToFloat(ETemp.Text);        // °C
  dt:= 1/ StrToFloat(Freq.Text);           // s
  hloubka:=StrToFloat(EHloubka.Text)*mm;

// Nacteni datoveho souboru a souboru pro ukladani vysledku
  Reset(DataFile);
  Rewrite(ResultFile);
  //CreateDir(path+'Graph');

// Nacteni databaze a materialu
  MatID:=InitMat(MatDtb, str);
  if (MatID < 0) then raise Exception.Create('Material "' + str + '" was not found.');

// Model
  InitModel(model, L, InitTemp, MatID, max_volL);
  //fHC_SaveIntChart4D_HCModel(model, model.vMat, PChar(path + '\Graph\Material'), 0, 1/mm); // ulozeni vytvoreneho modelu jako 2D graf
  writeln(ResultFile,'Vzorek',#9,'Čas [s]',#9,'T [°C]');
  writeln(ResultFile,'0',#9,'0',#9,FloatToStr(fHC_GetInterpValHCModel(model.vT, model, hloubka, 0, 0)-Cel_Kel));

//Nacteni tep.toku ze souboru
  n:=0;
  while not Eof(DataFile) do begin
    n:=n+1;
    Readln(DataFile, line);
    h[n]:=line;
  end;
  SetLength(q,n);
  for i:=1 to (n-1) do q[i]:=-1*h[i];

// Prima uloha:
  SetBoundCond(model);
  fHC_ClearAccuracy(model);
  MainDirectTask(ResultFile, MatDtb, model, q, n, path, dt, hloubka);

// free RAM a ukonceni
	fHC_Free(model);
  fHC_Free(MatDtb);
  CloseFile(DataFile); CloseFile(ResultFile);
  
  ShowMessage('Výpočet byl úspěšně ukončen.');
  Application.Terminate;
end;

end.
