{ ============================================================================= }
{ procedures de calcul de diffraction de surface                                }
{ ============================================================================= }
{ 09.09.02:FO: première version                                                 }

unit CalculGID;

interface

uses SysUtils,Qdialogs,constants, dataReflec, ndspl, Math387, MathAux, MtxVec, GaussLegendre, CalculReflec;

procedure CalculSGuntherM(layerindex:integer;latticeparameter:double;GIDterms:integer;var Qzi,Qzf:double;var MyML:TMultilayer;MyEP:TExperimentalParameters;var Stupup,Stdodo,Stupdo,Stdoup:double);
procedure CalculSGuntherM2(layerindex:integer;latticeparameter:double;GIDterms:integer;var kziUP,ARiUP,ATiUP,kziDO,ARiDO,ATiDO:Tvec;
    var kzfUP,ARfUP,ATfUP,kzfDO,ARfDO,ATfDO:Tvec;var MyML:TMultilayer;MyEP:TExperimentalParameters;var Stupup,Stdodo,Stupdo,Stdoup:double);
{ semi desuet; calcul Dosch  }
procedure QzGIDPol(s:string;Qzi,Qzf:double;var MyML:TMultilayer;MyEP:TExperimentalParameters;var QzGID:Tcplx);


implementation

{procedure pour les multicouches 'M', cette procedure calcule UNIQUEMENT le signal diffracté que par une couche }
{Entrées : Qzi, Qzf, Tableau du modele (MyML)                                                                   }
{Sorties : les coefficients S(tild) calculés avec le formalisme de Gunther et Toperverg                         }
procedure CalculSGuntherM(layerindex:integer;latticeparameter:double;GIDterms:integer;var Qzi,Qzf:double;var MyML:TMultilayer;MyEP:TExperimentalParameters;var Stupup,Stdodo,Stupdo,Stdoup:double);
var i,j,idxi,idxj,nbrlayers:integer;
    lambda,QcUp,QcDo,ap:double;
    PhaseiUP,PhaseiDo,PhasefUP,PhasefDO,PhaseiapUP,PhaseiapDo,PhasefapUP,PhasefapDo:Tcplx;
    kziUP,ARiUP,ATiUP,kziDO,ARiDO,ATiDO:Tvec;
    kzfUP,ARfUP,ATfUP,kzfDO,ARfDO,ATfDO:Tvec;
    gtt,grr,gtr,grt:Tcplx;
    TfUPTiUPg,TfDOTiDOg,TfUPTiDOg,TfDOTiUPg:Tcplx;
    RfUPRiUPg,RfDORiDOg,RfUPRiDOg,RfDORiUPg:TCplx;
    RfUPTiUPg,RfDOTiDOg,RfUPTiDOg,RfDOTiUPg:Tcplx;
    TfUPRiUPg,TfDORiDOg,TfUPRiDOg,TfDORiUPg:Tcplx;
    aux:string;
begin
lambda:=MyEP.lambda;
nbrlayers:=MyML.nbrLayers;
createIt(kziUP,kzfUP,ARiUP,ATiUP);createIt(ARfUP,ATfUP);
kziUP.Size(nbrLayers,true);ARiUP.Size(nbrLayers,true);ATiUP.Size(nbrLayers,true);
kzfUP.Size(nbrLayers,true);ARfUP.Size(nbrLayers,true);ATfUP.Size(nbrLayers,true);
createIt(kziDO,kzfDO,ARiDO,ATiDO);createIt(ARfDO,ATfDO);
kziDO.Size(nbrLayers,true);ARiDO.Size(nbrLayers,true);ATiDO.Size(nbrLayers,true);
kzfDO.Size(nbrLayers,true);ARfDO.Size(nbrLayers,true);ATfDO.Size(nbrLayers,true);

ap:=latticeparameter;

{initialisation de termes avant de faire leur somme}
Stupup:=0;Stdodo:=0;Stupdo:=0;Stdoup:=0;

{on choisit la couche que l'on fait diffracter, le numero correspond numero de la couche dans le tableau du modele}


{on limite le calcul à une SEULE couche diffractante}
idxi:=layerindex;
idxj:=layerindex;
CalculReflecNPamplitudes(Qzi,MyML.z,MyML.Vup,MyML.Vi,MyML.sigma,kziUP,ARiUP,ATiUP);
CalculReflecNPamplitudes(Qzi,MyML.z,MyML.Vdo,MyML.Vi,MyML.sigma,kziDO,ARiDO,ATiDO);
CalculReflecNPamplitudes(Qzf,MyML.z,MyML.Vup,MyML.Vi,MyML.sigma,kzfUP,ARfUP,ATfUP);
CalculReflecNPamplitudes(Qzf,MyML.z,MyML.Vdo,MyML.Vi,MyML.sigma,kzfDO,ARfDO,ATfDO);
for i:=nbrlayers-2 downto 1 do
   for j:=nbrlayers-2 downto 1 do
        begin
        PhaseiUP:=Cmul(kziUP.Cvalues[i],Cplx(MyML.z[i],0));
        PhaseiDO:=Cmul(kziDO.Cvalues[i],Cplx(MyML.z[i],0));
        PhasefUP:=Cmul(kzfUP.Cvalues[j],Cplx(MyML.z[j],0));
        PhasefDO:=Cmul(kzfDO.Cvalues[j],Cplx(MyML.z[j],0));

        PhaseiapUP:=Cmul(kziUP.Cvalues[i],Cplx(ap,0));
        PhaseiapDO:=Cmul(kziDO.Cvalues[i],Cplx(ap,0));
        PhasefapUP:=Cmul(kzfUP.Cvalues[j],Cplx(ap,0));
        PhasefapDO:=Cmul(kzfDO.Cvalues[j],Cplx(ap,0));

        {Calcul des coefficients g avec les différentes combinaisons de spin}
        gtt:=Cdiv(Csub(exp(Cmul(C_I,Cadd(PhasefUP,PhaseiUP))),C_one),Cmul(C_I,Cadd(PhasefapUP,PhaseiapUP)));
        gtr:=Cdiv(Csub(exp(Cmul(C_I,Csub(PhasefUP,PhaseiUP))),C_one),Cmul(C_I,Csub(PhasefapUP,PhaseiapUP)));
        grr:=Cmul(gtt,exp(Cdiv(Cadd(PhasefUP,PhaseiUP),C_I)));
        grt:=Cmul(gtr,exp(Cdiv(Csub(PhasefUP,PhaseiUP),C_I)));

        TfUPTiUPg:=Cmul(ATfUP.Cvalues[idxj],Cmul(ATiUP.Cvalues[idxi],gtt));
        TfDOTiDOg:=Cmul(ATfDO.Cvalues[idxj],Cmul(ATiDO.Cvalues[idxi],gtt));
        TfUPTiDOg:=Cmul(ATfUP.Cvalues[idxj],Cmul(ATiDO.Cvalues[idxi],gtt));
        TfDOTiUPg:=Cmul(ATfDO.Cvalues[idxj],Cmul(ATiUP.Cvalues[idxi],gtt));

        RfUPRiUPg:=Cmul(Cmul(ARfUP.Cvalues[idxj],exp(Cmul(C_I,PhasefUP))),Cmul(Cmul(ARiUP.Cvalues[idxi],exp(Cmul(C_I,PhaseiUP))),grr));
        RfDORiDOg:=Cmul(Cmul(ARfDO.Cvalues[idxj],exp(Cmul(C_I,PhasefDO))),Cmul(Cmul(ARiDO.Cvalues[idxi],exp(Cmul(C_I,PhaseiDO))),grr));
        RfUPRiDOg:=Cmul(Cmul(ARfUP.Cvalues[idxj],exp(Cmul(C_I,PhasefUP))),Cmul(Cmul(ARiDO.Cvalues[idxi],exp(Cmul(C_I,PhaseiDO))),grr));
        RfDORiUPg:=Cmul(Cmul(ARfDO.Cvalues[idxj],exp(Cmul(C_I,PhasefDO))),Cmul(Cmul(ARiUP.Cvalues[idxi],exp(Cmul(C_I,PhaseiUP))),grr));

        RfUPTiUPg:=Cmul(Cmul(ARfUP.Cvalues[idxj],exp(Cmul(C_I,PhasefUP))),Cmul(ATiUP.Cvalues[idxi],grt));
        RfDOTiDOg:=Cmul(Cmul(ARfDO.Cvalues[idxj],exp(Cmul(C_I,PhasefDO))),Cmul(ATiDO.Cvalues[idxi],grt));
        RfUPTiDOg:=Cmul(Cmul(ARfUP.Cvalues[idxj],exp(Cmul(C_I,PhasefUP))),Cmul(ATiDO.Cvalues[idxi],grt));
        RfDOTiUPg:=Cmul(Cmul(ARfDO.Cvalues[idxj],exp(Cmul(C_I,PhasefDO))),Cmul(ATiUP.Cvalues[idxi],grt));

        TfUPRiUPg:=Cmul(ATfUP.Cvalues[idxj],Cmul(Cmul(ARiUP.Cvalues[idxi],exp(Cmul(C_I,PhaseiUP))),gtr));
        TfDORiDOg:=Cmul(ATfDO.Cvalues[idxj],Cmul(Cmul(ARiDO.Cvalues[idxi],exp(Cmul(C_I,PhaseiDO))),gtr));
        TfUPRiDOg:=Cmul(ATfUP.Cvalues[idxj],Cmul(Cmul(ARiDO.Cvalues[idxi],exp(Cmul(C_I,PhaseiDO))),gtr));
        TfDORiUPg:=Cmul(ATfDO.Cvalues[idxj],Cmul(Cmul(ARiUP.Cvalues[idxi],exp(Cmul(C_I,PhaseiUP))),gtr));


        {on normalise par 1E30 pour le produit des longueurs de diffusion}
        if GIDTerms=0 then {calculs avec seulement les transmissions}
        begin
        Stupup:=Stupup+1e30*sqr(Cabs(TfUPTiUPg));
        Stdodo:=Stdodo+1e30*sqr(Cabs(TfDOTiDOg));
        Stupdo:=Stupdo+1e30*sqr(Cabs(TfUPTiDOg));
        Stdoup:=Stdoup+1e30*sqr(Cabs(TfDOTiUPg));
        end;

        if GIDTerms=1 then {calculs avec les transmissions + les produits trnasmissions-reflexions}
        begin
        Stupup:=Stupup+1e30*sqr(Cabs(Cadd(TfUPTiUPg,Cadd(RfUPTiUPg,TfupRiUPg))));
        Stdodo:=Stdodo+1e30*sqr(Cabs(Cadd(TfDOTiDOg,Cadd(RfDOTiDOg,TfDORiDOg))));
        Stupdo:=Stupdo+1e30*sqr(Cabs(Cadd(TfUPTiDOg,Cadd(RfUPTiDOg,TfUPRiDOg))));
        Stdoup:=Stdoup+1e30*sqr(Cabs(Cadd(TfDOTiUPg,Cadd(RfDOTiUPg,TfDORiUPg))));
        end;

        if GIDTerms=2 then {calcul total}
        begin
        Stupup:=Stupup+1e30*sqr(Cabs(Cadd(Cadd(TfUPTiUPg,RfUpRiUPg),Cadd(RfUPTiUPg,TfupRiUPg))));
        Stdodo:=Stdodo+1e30*sqr(Cabs(Cadd(Cadd(TfDOTiDOg,RfDORiDOg),Cadd(RfDOTiDOg,TfDORiDOg))));
        Stupdo:=Stupdo+1e30*sqr(Cabs(Cadd(Cadd(TfUPTiDOg,RfUPRiDOg),Cadd(RfUPTiDOg,TfUPRiDOg))));
        Stdoup:=Stdoup+1e30*sqr(Cabs(Cadd(Cadd(TfDOTiUPg,RfDORiUPg),Cadd(RfDOTiUPg,TfDORiUPg))));
        end;

        end;

freeit(kziUP,kzfUP,kziDO,kzfDO);
freeit(ARiUP,ATiUP,ARfUP,ATfUP);
freeit(ARiDO,ATiDO,ARfDO,ATfDO);
end;

{procedure pour les multicouches 'M', cette procedure calcule UNIQUEMENT le signal diffracté que par une couche }
{Entrées : Qzi, Qzf, Tableau du modele (MyML)                                                                   }
{Sorties : les coefficients S(tild) calculés avec le formalisme de Gunther et Toperverg                         }
procedure CalculSGuntherM2(layerindex:integer;latticeparameter:double;GIDterms:integer;var kziUP,ARiUP,ATiUP,kziDO,ARiDO,ATiDO:Tvec;
    var kzfUP,ARfUP,ATfUP,kzfDO,ARfDO,ATfDO:Tvec;var MyML:TMultilayer;MyEP:TExperimentalParameters;var Stupup,Stdodo,Stupdo,Stdoup:double);
var i,j,idxi,idxj,nbrlayers:integer;
    lambda,QcUp,QcDo,ap:double;
    PhaseiUP,PhaseiDo,PhasefUP,PhasefDO,PhaseiapUP,PhaseiapDo,PhasefapUP,PhasefapDo:Tcplx;
    gttUPUP,gttDODO,gttUPDO,gttDOUP,gtrUPUP,gtrDODO,gtrUPDO,gtrDOUP,grtUPUP,grtDODO,grtUPDO,grtDOUP,grrUPUP,grrDODO,grrUPDO,grrDOUP:Tcplx;
    TfUPTiUPg,TfDOTiDOg,TfUPTiDOg,TfDOTiUPg:Tcplx;
    RfUPRiUPg,RfDORiDOg,RfUPRiDOg,RfDORiUPg:TCplx;
    RfUPTiUPg,RfDOTiDOg,RfUPTiDOg,RfDOTiUPg:Tcplx;
    TfUPRiUPg,TfDORiDOg,TfUPRiDOg,TfDORiUPg:Tcplx;

begin
lambda:=MyEP.lambda;
nbrlayers:=MyML.nbrLayers;

ap:=latticeparameter;

{initialisation de termes avant de faire leur somme}
Stupup:=0;Stdodo:=0;Stupdo:=0;Stdoup:=0;

{on choisit la couche que l'on fait diffracter, le numero correspond numero de la couche dans le tableau du modele}


{on limite le calcul à une SEULE couche diffractante}
idxi:=layerindex;
idxj:=layerindex;
for i:=nbrlayers-2 downto 1 do
   for j:=nbrlayers-2 downto 1 do
        begin
        PhaseiUP:=Cmul(kziUP.Cvalues[i],Cplx(MyML.z[i],0));
        PhaseiDO:=Cmul(kziDO.Cvalues[i],Cplx(MyML.z[i],0));
        PhasefUP:=Cmul(kzfUP.Cvalues[j],Cplx(MyML.z[j],0));
        PhasefDO:=Cmul(kzfDO.Cvalues[j],Cplx(MyML.z[j],0));

        PhaseiapUP:=Cmul(kziUP.Cvalues[i],Cplx(ap,0));
        PhaseiapDO:=Cmul(kziDO.Cvalues[i],Cplx(ap,0));
        PhasefapUP:=Cmul(kzfUP.Cvalues[j],Cplx(ap,0));
        PhasefapDO:=Cmul(kzfDO.Cvalues[j],Cplx(ap,0));

        {Calcul des coefficients g avec les différentes combinaisons de spin}
        gttUPUP:=Cdiv(Csub(exp(Cmul(C_I,Cadd(PhasefUP,PhaseiUP))),C_one),Cmul(C_I,Cadd(PhasefapUP,PhaseiapUP)));
        gtrUPUP:=Cdiv(Csub(exp(Cmul(C_I,Csub(PhasefUP,PhaseiUP))),C_one),Cmul(C_I,Csub(PhasefapUP,PhaseiapUP)));
        grrUPUP:=Cmul(gttUPUP,exp(Cdiv(Cadd(PhasefUP,PhaseiUP),C_I)));
        grtUPUP:=Cmul(gtrUPUP,exp(Cdiv(Csub(PhasefUP,PhaseiUP),C_I)));

        gttDODO:=Cdiv(Csub(exp(Cmul(C_I,Cadd(PhasefDO,PhaseiDO))),C_one),Cmul(C_I,Cadd(PhasefapDO,PhaseiapDO)));
        gtrDODO:=Cdiv(Csub(exp(Cmul(C_I,Csub(PhasefDO,PhaseiDO))),C_one),Cmul(C_I,Csub(PhasefapDO,PhaseiapDO)));
        grrDODO:=Cmul(gttDODO,exp(Cdiv(Cadd(PhasefDO,PhaseiDO),C_I)));
        grtDODO:=Cmul(gtrDODO,exp(Cdiv(Csub(PhasefDO,PhaseiDO),C_I)));

        gttUPDO:=Cdiv(Csub(exp(Cmul(C_I,Cadd(PhasefUP,PhaseiDO))),C_one),Cmul(C_I,Cadd(PhasefapUP,PhaseiapDO)));
        gtrUPDO:=Cdiv(Csub(exp(Cmul(C_I,Csub(PhasefUP,PhaseiDO))),C_one),Cmul(C_I,Csub(PhasefapUP,PhaseiapDO)));
        grrUPDO:=Cmul(gttUPDO,exp(Cdiv(Cadd(PhasefUP,PhaseiDO),C_I)));
        grtUPDO:=Cmul(gtrUPDO,exp(Cdiv(Csub(PhasefUP,PhaseiDO),C_I)));

        gttDOUP:=Cdiv(Csub(exp(Cmul(C_I,Cadd(PhasefDO,PhaseiUP))),C_one),Cmul(C_I,Cadd(PhasefapDO,PhaseiapUP)));
        gtrDOUP:=Cdiv(Csub(exp(Cmul(C_I,Csub(PhasefDO,PhaseiUP))),C_one),Cmul(C_I,Csub(PhasefapDO,PhaseiapUP)));
        grrDOUP:=Cmul(gttDOUP,exp(Cdiv(Cadd(PhasefDO,PhaseiUP),C_I)));
        grtDOUP:=Cmul(gtrDOUP,exp(Cdiv(Csub(PhasefDO,PhaseiUP),C_I)));

        TfUPTiUPg:=Cmul(ATfUP.Cvalues[idxj],Cmul(ATiUP.Cvalues[idxi],gttUPUP));
        TfDOTiDOg:=Cmul(ATfDO.Cvalues[idxj],Cmul(ATiDO.Cvalues[idxi],gttDODO));
        TfUPTiDOg:=Cmul(ATfUP.Cvalues[idxj],Cmul(ATiDO.Cvalues[idxi],gttUPDO));
        TfDOTiUPg:=Cmul(ATfDO.Cvalues[idxj],Cmul(ATiUP.Cvalues[idxi],gttDOUP));

        RfUPRiUPg:=Cmul(Cmul(ARfUP.Cvalues[idxj],exp(Cmul(C_I,PhasefUP))),Cmul(Cmul(ARiUP.Cvalues[idxi],exp(Cmul(C_I,PhaseiUP))),grrUPUP));
        RfDORiDOg:=Cmul(Cmul(ARfDO.Cvalues[idxj],exp(Cmul(C_I,PhasefDO))),Cmul(Cmul(ARiDO.Cvalues[idxi],exp(Cmul(C_I,PhaseiDO))),grrDODO));
        RfUPRiDOg:=Cmul(Cmul(ARfUP.Cvalues[idxj],exp(Cmul(C_I,PhasefUP))),Cmul(Cmul(ARiDO.Cvalues[idxi],exp(Cmul(C_I,PhaseiDO))),grrUPDO));
        RfDORiUPg:=Cmul(Cmul(ARfDO.Cvalues[idxj],exp(Cmul(C_I,PhasefDO))),Cmul(Cmul(ARiUP.Cvalues[idxi],exp(Cmul(C_I,PhaseiUP))),grrDOUP));

        RfUPTiUPg:=Cmul(Cmul(ARfUP.Cvalues[idxj],exp(Cmul(C_I,PhasefUP))),Cmul(ATiUP.Cvalues[idxi],grtUPUP));
        RfDOTiDOg:=Cmul(Cmul(ARfDO.Cvalues[idxj],exp(Cmul(C_I,PhasefDO))),Cmul(ATiDO.Cvalues[idxi],grtDODO));
        RfUPTiDOg:=Cmul(Cmul(ARfUP.Cvalues[idxj],exp(Cmul(C_I,PhasefUP))),Cmul(ATiDO.Cvalues[idxi],grtUPDO));
        RfDOTiUPg:=Cmul(Cmul(ARfDO.Cvalues[idxj],exp(Cmul(C_I,PhasefDO))),Cmul(ATiUP.Cvalues[idxi],grtDOUP));

        TfUPRiUPg:=Cmul(ATfUP.Cvalues[idxj],Cmul(Cmul(ARiUP.Cvalues[idxi],exp(Cmul(C_I,PhaseiUP))),gtrUPUP));
        TfDORiDOg:=Cmul(ATfDO.Cvalues[idxj],Cmul(Cmul(ARiDO.Cvalues[idxi],exp(Cmul(C_I,PhaseiDO))),gtrDODO));
        TfUPRiDOg:=Cmul(ATfUP.Cvalues[idxj],Cmul(Cmul(ARiDO.Cvalues[idxi],exp(Cmul(C_I,PhaseiDO))),gtrUPDO));
        TfDORiUPg:=Cmul(ATfDO.Cvalues[idxj],Cmul(Cmul(ARiUP.Cvalues[idxi],exp(Cmul(C_I,PhaseiUP))),gtrDOUP));

        {Calcul des coefficients g avec les différentes combinaisons de spin
        gtt:=Cdiv(Csub(exp(Cmul(C_I,Cadd(PhasefUP,PhaseiUP))),C_one),Cmul(C_I,Cadd(PhasefapUP,PhaseiapUP)));
        gtr:=Cdiv(Csub(exp(Cmul(C_I,Csub(PhasefUP,PhaseiUP))),C_one),Cmul(C_I,Csub(PhasefapUP,PhaseiapUP)));
        grr:=Cmul(gtt,exp(Cdiv(Cadd(PhasefUP,PhaseiUP),C_I)));
        grt:=Cmul(gtr,exp(Cdiv(Csub(PhasefUP,PhaseiUP),C_I)));

        TfUPTiUPg:=Cmul(ATfUP.Cvalues[idxj],Cmul(ATiUP.Cvalues[idxi],gtt));
        TfDOTiDOg:=Cmul(ATfDO.Cvalues[idxj],Cmul(ATiDO.Cvalues[idxi],gtt));
        TfUPTiDOg:=Cmul(ATfUP.Cvalues[idxj],Cmul(ATiDO.Cvalues[idxi],gtt));
        TfDOTiUPg:=Cmul(ATfDO.Cvalues[idxj],Cmul(ATiUP.Cvalues[idxi],gtt));

        RfUPRiUPg:=Cmul(Cmul(ARfUP.Cvalues[idxj],exp(Cmul(C_I,PhasefUP))),Cmul(Cmul(ARiUP.Cvalues[idxi],exp(Cmul(C_I,PhaseiUP))),grr));
        RfDORiDOg:=Cmul(Cmul(ARfDO.Cvalues[idxj],exp(Cmul(C_I,PhasefDO))),Cmul(Cmul(ARiDO.Cvalues[idxi],exp(Cmul(C_I,PhaseiDO))),grr));
        RfUPRiDOg:=Cmul(Cmul(ARfUP.Cvalues[idxj],exp(Cmul(C_I,PhasefUP))),Cmul(Cmul(ARiDO.Cvalues[idxi],exp(Cmul(C_I,PhaseiDO))),grr));
        RfDORiUPg:=Cmul(Cmul(ARfDO.Cvalues[idxj],exp(Cmul(C_I,PhasefDO))),Cmul(Cmul(ARiUP.Cvalues[idxi],exp(Cmul(C_I,PhaseiUP))),grr));

        RfUPTiUPg:=Cmul(Cmul(ARfUP.Cvalues[idxj],exp(Cmul(C_I,PhasefUP))),Cmul(ATiUP.Cvalues[idxi],grt));
        RfDOTiDOg:=Cmul(Cmul(ARfDO.Cvalues[idxj],exp(Cmul(C_I,PhasefDO))),Cmul(ATiDO.Cvalues[idxi],grt));
        RfUPTiDOg:=Cmul(Cmul(ARfUP.Cvalues[idxj],exp(Cmul(C_I,PhasefUP))),Cmul(ATiDO.Cvalues[idxi],grt));
        RfDOTiUPg:=Cmul(Cmul(ARfDO.Cvalues[idxj],exp(Cmul(C_I,PhasefDO))),Cmul(ATiUP.Cvalues[idxi],grt));

        TfUPRiUPg:=Cmul(ATfUP.Cvalues[idxj],Cmul(Cmul(ARiUP.Cvalues[idxi],exp(Cmul(C_I,PhaseiUP))),gtr));
        TfDORiDOg:=Cmul(ATfDO.Cvalues[idxj],Cmul(Cmul(ARiDO.Cvalues[idxi],exp(Cmul(C_I,PhaseiDO))),gtr));
        TfUPRiDOg:=Cmul(ATfUP.Cvalues[idxj],Cmul(Cmul(ARiDO.Cvalues[idxi],exp(Cmul(C_I,PhaseiDO))),gtr));
        TfDORiUPg:=Cmul(ATfDO.Cvalues[idxj],Cmul(Cmul(ARiUP.Cvalues[idxi],exp(Cmul(C_I,PhaseiUP))),gtr));}


        {on normalise par 1E30 pour le produit des longueurs de diffusion}
        if GIDTerms=0 then {calculs avec seulement les transmissions}
        begin
        Stupup:=Stupup+1e30*sqr(Cabs(TfUPTiUPg));
        Stdodo:=Stdodo+1e30*sqr(Cabs(TfDOTiDOg));
        Stupdo:=Stupdo+1e30*sqr(Cabs(TfUPTiDOg));
        Stdoup:=Stdoup+1e30*sqr(Cabs(TfDOTiUPg));
        end;

        if GIDTerms=1 then {calculs avec les transmissions + les produits trnasmissions-reflexions}
        begin
        Stupup:=Stupup+1e30*sqr(Cabs(Cadd(TfUPTiUPg,Cadd(RfUPTiUPg,TfupRiUPg))));
        Stdodo:=Stdodo+1e30*sqr(Cabs(Cadd(TfDOTiDOg,Cadd(RfDOTiDOg,TfDORiDOg))));
        Stupdo:=Stupdo+1e30*sqr(Cabs(Cadd(TfUPTiDOg,Cadd(RfUPTiDOg,TfUPRiDOg))));
        Stdoup:=Stdoup+1e30*sqr(Cabs(Cadd(TfDOTiUPg,Cadd(RfDOTiUPg,TfDORiUPg))));
        end;

        if GIDTerms=2 then {calcul total}
        begin
        Stupup:=Stupup+1e30*sqr(Cabs(Cadd(Cadd(TfUPTiUPg,RfUpRiUPg),Cadd(RfUPTiUPg,TfupRiUPg))));
        Stdodo:=Stdodo+1e30*sqr(Cabs(Cadd(Cadd(TfDOTiDOg,RfDORiDOg),Cadd(RfDOTiDOg,TfDORiDOg))));
        Stupdo:=Stupdo+1e30*sqr(Cabs(Cadd(Cadd(TfUPTiDOg,RfUPRiDOg),Cadd(RfUPTiDOg,TfUPRiDOg))));
        Stdoup:=Stdoup+1e30*sqr(Cabs(Cadd(Cadd(TfDOTiUPg,RfDORiUPg),Cadd(RfDOTiUPg,TfDORiUPg))));
        end;

        end;

end;


{ semi desuet }
procedure QzGIDPol(s:string;Qzi,Qzf:double;var MyML:TMultilayer;MyEP:TExperimentalParameters;var QzGID:Tcplx);
var
nbrlayers:integer;
kzi,ARi,ATi,kzf,ARf,ATf:Tvec;
lambda:double;
begin
{ mise a jour des paramètres experimentaux } { a reactiver }
{ UpdateVarExperimentalParameters(MyExperimentalParameters); }
{ mise a jour des paramètres de la multicouche a partir de la grille }
{ MyMultiLayer.UpdateFromMultiLayerGrid(MultiLayerGrid,MyExperimentalParameters);
MyML:=MyMultilayer; }

lambda:=MyEP.lambda;
nbrlayers:=MyML.nbrLayers;
createIt(kzi,ARi,ATi);createIt(kzf,ARf,ATf);
kzi.Size(nbrLayers,true);ARi.Size(nbrLayers,true);ATi.Size(nbrLayers,true);
kzf.Size(nbrLayers,true);ARf.Size(nbrLayers,true);ATf.Size(nbrLayers,true);

CalculReflecNPamplitudes(Qzi,MyML.z,MyML.V,MyML.Vi,MyML.sigma,kzi,ARi,ATi);

if s='np' then
begin
CalculReflecNPamplitudes(Qzf,MyML.z,MyML.V,MyML.Vi,MyML.sigma,kzf,ARf,ATf);
QzGID:=Csub(kzi.Cvalues[nbrLayers-2],neg(kzf.Cvalues[nbrLayers-2]));
end;

if s='up' then
begin
CalculReflecNPamplitudes(Qzf,MyML.z,MyML.Vup,MyML.Vi,MyML.sigma,kzf,ARf,ATf);
QzGID:=Csub(kzi.Cvalues[nbrLayers-2],neg(kzf.Cvalues[nbrLayers-2]));
end;

if s='do' then
begin
CalculReflecNPamplitudes(Qzf,MyML.z,MyML.Vdo,MyML.Vi,MyML.sigma,kzf,ARf,ATf);
QzGID:=Csub(kzi.Cvalues[nbrLayers-2],neg(kzf.Cvalues[nbrLayers-2]));
end;

freeit(kzi,ARi,ATi);
freeit(kzf,ARf,ATf);
end;

{ ================================ OBSOLETE =================================== }
{ procedure qui a ette utilisée pour le dev. de la version a une couche, elle est }
{ desormais prise en compte par la procedure CalculSGunther                     }
procedure CalculSGunther(var Qzi,Qzf:double;var MyML:TMultilayer;MyEP:TexperimentalParameters;var Stupup,Stdodo,Stupdo,Stdoup:double);
var nbrlayers:integer;
    lambda,QcUp,QcDo,ap:double;
    PhaseiUP,PhaseiDo,PhasefUP,PhasefDO,PhaseiapUP,PhaseiapDo,PhasefapUP,PhasefapDo:Tcplx;
    kziUP,ARiUP,ATiUP,kziDO,ARiDO,ATiDO:Tvec;
    kzfUP,ARfUP,ATfUP,kzfDO,ARfDO,ATfDO:Tvec;
    gtt,grr,gtr,grt:Tcplx;
    TfUPTiUPg,TfDOTiDOg,TfUPTiDOg,TfDOTiUPg:Tcplx;
    RfUPRiUPg,RfDORiDOg,RfUPRiDOg,RfDORiUPg:TCplx;
    RfUPTiUPg,RfDOTiDOg,RfUPTiDOg,RfDOTiUPg:Tcplx;
    TfUPRiUPg,TfDORiDOg,TfUPRiDOg,TfDORiUPg:Tcplx;
    indGID1,indGiD2,IndGID3,indGID4,nbrOutPts,nbrInPts,s,i,j:integer;
    Outmin,Outstep,OutMax,InMin,InStep,InMax:double;

begin
lambda:=MyEP.lambda;
nbrlayers:=MyML.nbrLayers;
createIt(kziUP,kzfUP,ARiUP,ATiUP);createIt(ARfUP,ATfUP);
kziUP.Size(nbrLayers,true);ARiUP.Size(nbrLayers,true);ATiUP.Size(nbrLayers,true);
kzfUP.Size(nbrLayers,true);ARfUP.Size(nbrLayers,true);ATfUP.Size(nbrLayers,true);
createIt(kziDO,kzfDO,ARiDO,ATiDO);createIt(ARfDO,ATfDO);
kziDO.Size(nbrLayers,true);ARiDO.Size(nbrLayers,true);ATiDO.Size(nbrLayers,true);
kzfDO.Size(nbrLayers,true);ARfDO.Size(nbrLayers,true);ATfDO.Size(nbrLayers,true);

CalculReflecNPamplitudes(Qzi,MyML.z,MyML.Vup,MyML.Vi,MyML.sigma,kziUP,ARiUP,ATiUP);
CalculReflecNPamplitudes(Qzi,MyML.z,MyML.Vdo,MyML.Vi,MyML.sigma,kziDO,ARiDO,ATiDO);

PhaseiUP:=Cmul(kziUP.Cvalues[nbrlayers-2],Cplx(MyML.z[nbrlayers-2],0));
PhaseiDO:=Cmul(kziDO.Cvalues[nbrlayers-2],Cplx(MyML.z[nbrlayers-2],0));

CalculReflecNPamplitudes(Qzf,MyML.z,MyML.Vup,MyML.Vi,MyML.sigma,kzfUP,ARfUP,ATfUP);
CalculReflecNPamplitudes(Qzf,MyML.z,MyML.Vdo,MyML.Vi,MyML.sigma,kzfDO,ARfDO,ATfDO);

PhasefUP:=Cmul(kzfUP.Cvalues[nbrlayers-2],Cplx(MyML.z[nbrlayers-2],0));
PhasefDO:=Cmul(kzfDO.Cvalues[nbrlayers-2],Cplx(MyML.z[nbrlayers-2],0));

{ ap:=latticeparameterbox.value*1e-9; }
PhaseiapUP:=Cmul(kziUP.Cvalues[nbrlayers-2],Cplx(ap,0));
PhaseiapDO:=Cmul(kziDO.Cvalues[nbrlayers-2],Cplx(ap,0));
PhasefapUP:=Cmul(kzfUP.Cvalues[nbrlayers-2],Cplx(ap,0));
PhasefapDO:=Cmul(kzfDO.Cvalues[nbrlayers-2],Cplx(ap,0));

{Calcul des coefficients g avec les différentes combinaisons de spin}
gtt:=Cdiv(Csub(exp(Cmul(C_I,Cadd(PhasefUP,PhaseiUP))),C_one),Cmul(C_I,Cadd(PhasefapUP,PhaseiapUP)));
gtr:=Cdiv(Csub(exp(Cmul(C_I,Csub(PhasefUP,PhaseiUP))),C_one),Cmul(C_I,Csub(PhasefapUP,PhaseiapUP)));
grr:=Cmul(gtt,exp(Cdiv(Cadd(PhasefUP,PhaseiUP),C_I)));
grt:=Cmul(gtr,exp(Cdiv(Csub(PhasefUP,PhaseiUP),C_I)));

TfUPTiUPg:=Cmul(ATfUP.Cvalues[nbrlayers-2],Cmul(ATiUP.Cvalues[nbrlayers-2],gtt));
TfDOTiDOg:=Cmul(ATfDO.Cvalues[nbrlayers-2],Cmul(ATiDO.Cvalues[nbrlayers-2],gtt));
TfUPTiDOg:=Cmul(ATfUP.Cvalues[nbrlayers-2],Cmul(ATiDO.Cvalues[nbrlayers-2],gtt));
TfDOTiUPg:=Cmul(ATfDO.Cvalues[nbrlayers-2],Cmul(ATiUP.Cvalues[nbrlayers-2],gtt));

RfUPRiUPg:=Cmul(Cmul(ARfUP.Cvalues[nbrlayers-2],exp(Cmul(C_I,PhasefUP))),Cmul(Cmul(ARiUP.Cvalues[nbrlayers-2],exp(Cmul(C_I,PhaseiUP))),grr));
RfDORiDOg:=Cmul(Cmul(ARfDO.Cvalues[nbrlayers-2],exp(Cmul(C_I,PhasefDO))),Cmul(Cmul(ARiDO.Cvalues[nbrlayers-2],exp(Cmul(C_I,PhaseiDO))),grr));
RfUPRiDOg:=Cmul(Cmul(ARfUP.Cvalues[nbrlayers-2],exp(Cmul(C_I,PhasefUP))),Cmul(Cmul(ARiDO.Cvalues[nbrlayers-2],exp(Cmul(C_I,PhaseiDO))),grr));
RfDORiUPg:=Cmul(Cmul(ARfDO.Cvalues[nbrlayers-2],exp(Cmul(C_I,PhasefDO))),Cmul(Cmul(ARiUP.Cvalues[nbrlayers-2],exp(Cmul(C_I,PhaseiUP))),grr));

RfUPTiUPg:=Cmul(Cmul(ARfUP.Cvalues[nbrlayers-2],exp(Cmul(C_I,PhasefUP))),Cmul(ATiUP.Cvalues[nbrlayers-2],grt));
RfDOTiDOg:=Cmul(Cmul(ARfDO.Cvalues[nbrlayers-2],exp(Cmul(C_I,PhasefDO))),Cmul(ATiDO.Cvalues[nbrlayers-2],grt));
RfUPTiDOg:=Cmul(Cmul(ARfUP.Cvalues[nbrlayers-2],exp(Cmul(C_I,PhasefUP))),Cmul(ATiDO.Cvalues[nbrlayers-2],grt));
RfDOTiUPg:=Cmul(Cmul(ARfDO.Cvalues[nbrlayers-2],exp(Cmul(C_I,PhasefDO))),Cmul(ATiUP.Cvalues[nbrlayers-2],grt));

TfUPRiUPg:=Cmul(ATfUP.Cvalues[nbrlayers-2],Cmul(Cmul(ARiUP.Cvalues[nbrlayers-2],exp(Cmul(C_I,PhaseiUP))),gtr));
TfDORiDOg:=Cmul(ATfDO.Cvalues[nbrlayers-2],Cmul(Cmul(ARiDO.Cvalues[nbrlayers-2],exp(Cmul(C_I,PhaseiDO))),gtr));
TfUPRiDOg:=Cmul(ATfUP.Cvalues[nbrlayers-2],Cmul(Cmul(ARiDO.Cvalues[nbrlayers-2],exp(Cmul(C_I,PhaseiDO))),gtr));
TfDORiUPg:=Cmul(ATfDO.Cvalues[nbrlayers-2],Cmul(Cmul(ARiUP.Cvalues[nbrlayers-2],exp(Cmul(C_I,PhaseiUP))),gtr));

{ if GIDTermsGroup.ItemIndex=0 then {calculs avec seulement les transmissions}
begin
Stupup:=1e30*sqr(Cabs(TfUPTiUPg));
Stdodo:=1e30*sqr(Cabs(TfDOTiDOg));
Stupdo:=1e30*sqr(Cabs(TfUPTiDOg));
Stdoup:=1e30*sqr(Cabs(TfDOTiUPg));
end;

{ if GIDTermsGroup.ItemIndex=1 then {calculs avec les transmissions + les produits trnasmissions-reflexions}
begin
Stupup:=1e30*sqr(Cabs(Cadd(TfUPTiUPg,Cadd(RfUPTiUPg,TfupRiUPg))));
Stdodo:=1e30*sqr(Cabs(Cadd(TfDOTiDOg,Cadd(RfDOTiDOg,TfDORiDOg))));
Stupdo:=1e30*sqr(Cabs(Cadd(TfUPTiDOg,Cadd(RfUPTiDOg,TfUPRiDOg))));
Stdoup:=1e30*sqr(Cabs(Cadd(TfDOTiUPg,Cadd(RfDOTiUPg,TfDORiUPg))));
end;

{ if GIDTermsGroup.ItemIndex=2 then {calcul total}
begin
Stupup:=1e30*sqr(Cabs(Cadd(Cadd(TfUPTiUPg,RfUpRiUPg),Cadd(RfUPTiUPg,TfupRiUPg))));
Stdodo:=1e30*sqr(Cabs(Cadd(Cadd(TfDOTiDOg,RfDORiDOg),Cadd(RfDOTiDOg,TfDORiDOg))));
Stupdo:=1e30*sqr(Cabs(Cadd(Cadd(TfUPTiDOg,RfUPRiDOg),Cadd(RfUPTiDOg,TfUPRiDOg))));
Stdoup:=1e30*sqr(Cabs(Cadd(Cadd(TfDOTiUPg,RfDORiUPg),Cadd(RfDOTiUPg,TfDORiUPg))));
end;

freeit(kziUP,kzfUP,kziDO,kzfDO);
freeit(ARiUP,ATiUP,ARfUP,ATfUP);
freeit(ARiDO,ATiDO,ARfDO,ATfDO);
end;



end.
