{-----------------------------------------------------------------------------
 Unit Name: PosMatrix
 Author:    kaetzel
 Purpose:   Calculates the Position Matrix Files for Multisphere Scattering
            Program ScatAggr. These files are necessary to find the position of
            the Gaunt Coefficients in their files because the process of
            calculation is not recursive. When calculating new Gaunt Coefficient
            Files, this program also has to be run.
-----------------------------------------------------------------------------}

unit PosMatrix;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,Math, JvEdit,JvDSADialogs;

type
  TForm1 = class(TForm)
    Button1: TButton;
    JvEdit1: TJvEdit;
    Button2: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  EFileExistsError=class(Exception);

var
  Form1: TForm1;

implementation

{$R *.dfm}


{-----------------------------------------------------------------------------
  Procedure: TForm1.Button1Click
  Author:    kaetzel
  Purpose:   Calculates the Position Matrix Files for a given maximum number of
             the Wiscombe criterion
  Comments:
-----------------------------------------------------------------------------}

procedure TForm1.Button1Click(Sender: TObject);
        //Type for the creation of the files
type pos=packed record
    position_value:integer;
  end;
var
  f:file of pos;
  m,n,mu,nu,q,p,qmax,i,WiscombeNumber:integer;
  data:pos;
begin
  Button1.Enabled:=False;
  Button1.Caption:='Calculating';
  Application.ProcessMessages;
  try
        //Reading the maximum number of Wiscombe iterations introduced in the
        //calculated Gaunt coefficients
    WiscombeNumber:=StrToInt(JvEdit1.Text);
  except
    ShowMessage('This is not a valid value! Must be an integer!',
                dckActiveForm);
  end;
  for n:=1 to WiscombeNumber do
    begin
      if FileExists('PosMatrix_'+IntToStr(WiscombeNumber)+
                    '_'+IntToStr(n)+'.dat') then
      begin
        Button1.Enabled:=True;
        Raise EFileExistsError.Create('File PosMatrix_'+IntToStr(WiscombeNumber)+
                                      '_'+IntToStr(n)+'.dat already exists! '+
                                      'Process aborted!');
        exit;
      end;
                //Creation of the file
      AssignFile(f,'PosMatrix_'+IntToStr(WiscombeNumber)+'_'+
                 IntToStr(n)+'.dat');
      ReWrite(f);
      i:=0;
      for m:=-n to n do
        begin
          for nu:=1 to WiscombeNumber do
            begin
              for mu:=-nu to nu do
                begin
                  qmax:=min(n,min(nu,Round((n+nu-abs(m+mu))/2)));
                  for q:=0 to n do
                    begin
                      if q<=qmax then
                        begin
                                //For these values of m,n,mu,nu,q exist Gaunt
                                //coefficients, therefore the position number
                                //for the file is written to the file and
                                //increased afterwards
                          data.position_value:=i;
                          i:=i+1;
                        end
                      else
                        begin
                                //Here no Gaunt Coefficient exists, therefore
                                //the position value is set to -1 which produces
                                //an I/O Error in Scattaggr.exe when trying to
                                //read such a coefficient
                          data.position_value:=-1;
                        end;
                      Write(f,data);
                    end;
                end;
            end;
        end;
      CloseFile(f);
    end;
  Button1.Enabled:=True;
  Button1.Caption:='Finished';
end;


{-----------------------------------------------------------------------------
  Procedure: TForm1.Button2Click
  Author:    kaetzel
  Purpose:   Closes the Application
  Comments:
-----------------------------------------------------------------------------}

procedure TForm1.Button2Click(Sender: TObject);
begin
  Close;
end;

end.
