From f9ffffda467fde2dcf17912bded689f4cffb70aa Mon Sep 17 00:00:00 2001
From: Guillaume Demesy <guillaume.demesy@fresnel.fr>
Date: Sat, 31 Oct 2020 22:09:44 +0100
Subject: [PATCH] enable plotting over multiple periods in conical incidence

---
 DiffractionGratings/grating2D_conical.pro | 127 +++++++++++++---------
 DiffractionGratings/grating2D_scalar.pro  |   4 +-
 2 files changed, 76 insertions(+), 55 deletions(-)

diff --git a/DiffractionGratings/grating2D_conical.pro b/DiffractionGratings/grating2D_conical.pro
index b7251d3..c964296 100644
--- a/DiffractionGratings/grating2D_conical.pro
+++ b/DiffractionGratings/grating2D_conical.pro
@@ -439,58 +439,60 @@ Resolution {
 PostProcessing {
   { Name postpro_energy; NameOfFormulation helmoltz_conical;
     Quantity {
-    { Name lambda_step   ; Value { Local { [ lambda0/nm ]; In Omega ; Jacobian JVol; } } }
-    { Name E2d    ; Value { Local { [ {Et}+{El}      ]; In Omega; Jacobian JVol; } } }
-    { Name Etot   ; Value { Local { [ {Et}+{El}+E1[] ]; In Omega; Jacobian JVol; } } }
-    { Name H2d    ; Value { Local { [ (-I[]/(mur[]*mu0*om0))*({Curl Et}+{Curl El}) ]; In Omega; Jacobian JVol; } } }
-    { Name Htot   ; Value { Local { [ (-I[]/(mur[]*mu0*om0))*({Curl Et}+{Curl El})+H1[] ]; In Omega; Jacobian JVol; } } }
-    { Name E2d_z  ; Value { Local { [ CompZ[{Et}+{El}] ]; In Omega; Jacobian JVol; } } }
-    { Name E2d_t  ; Value { Local { [    {Et}          ]; In Omega; Jacobian JVol; } } }
-    { Name Etot_z ; Value { Local { [ CompZ[{Et}+{El}+E1[]] ]; In Omega; Jacobian JVol; } } }
-    { Name Htot_z ; Value { Local { [ CompZ[(-I[]/(mur[]*mu0*om0))*({Curl Et}+{Curl El})+H1[]] ]; In Omega; Jacobian JVol; } } }
-    { Name H2d_z  ; Value { Local { [ CompZ[(-I[]/(mur[]*mu0*om0))*({Curl Et}+{Curl El})] ]; In Omega; Jacobian JVol; } } }
-
-
-    { Name E1   ; Value { Local { [ E1[]  ]; In Omega; Jacobian JVol; } } }
-    { Name E1t  ; Value { Local { [ E1t[] ]; In Omega; Jacobian JVol; } } }
-    { Name E1l  ; Value { Local { [ E1l[] ]; In Omega; Jacobian JVol; } } }
-
-    { Name Q_subs      ; Value{ Integral{ [  0.5 * ep0*om0*Fabs[epsr2_im[]      ]        * ( SquNorm[{Et}+{El}+E1[]] ) / (Pinc[]*d) ] ; In sub         ; Integration Int_1 ; Jacobian JVol ; } } }
-    { Name Q_rod_out   ; Value{ Integral{ [  0.5 * ep0*om0*Fabs[epsr_rod_out_im[]]       * ( SquNorm[{Et}+{El}+E1[]] ) / (Pinc[]*d) ] ; In rod_out   ; Integration Int_1 ; Jacobian JVol ; } } }
-    { Name Q_layer_dep ; Value{ Integral{ [  0.5 * ep0*om0*Fabs[epsr_layer_dep_im[]]     * ( SquNorm[{Et}+{El}+E1[]] ) / (Pinc[]*d) ] ; In layer_dep   ; Integration Int_1 ; Jacobian JVol ; } } }
-    { Name Q_layer_cov ; Value{ Integral{ [  0.5 * ep0*om0*Fabs[epsr_layer_cov_im[]]     * ( SquNorm[{Et}+{El}+E1[]] ) / (Pinc[]*d) ] ; In layer_cov   ; Integration Int_1 ; Jacobian JVol ; } } }
-    { Name Q_tot       ; Value{ Integral{ [  0.5 * ep0*om0*Fabs[Im[CompZZ[epsilonr[]]]]  * ( SquNorm[{Et}+{El}+E1[]] ) / (Pinc[]*d) ] ; In Plot_domain ; Integration Int_1 ; Jacobian JVol ; } } }
-
-    For i In {0:2*nb_orders}
-      { Name int_x_t~{i} ; Value{ Integral{ [ CompX[{Et}+E1t[] ] * expialphax~{i}[]/d ] ; In SurfCutSubs1  ; Integration Int_1 ; Jacobian JSur ; } } }
-      { Name int_y_t~{i} ; Value{ Integral{ [ ({uy}+CompY[E1t[]])* expialphax~{i}[]/d ] ; In SurfCutSubs1  ; Integration Int_1 ; Jacobian JSur ; } } }
-      { Name int_z_t~{i} ; Value{ Integral{ [ CompZ[{El}+E1l[] ] * expialphax~{i}[]/d ] ; In SurfCutSubs1  ; Integration Int_1 ; Jacobian JSur ; } } }
-
-      { Name int_x_r~{i} ; Value{ Integral{ [ CompX[{Et}+E1dt[]] * expialphax~{i}[]/d ] ; In SurfCutSuper1 ; Integration Int_1 ; Jacobian JSur ; } } }
-      { Name int_y_r~{i} ; Value{ Integral{ [({uy}+CompY[E1dt[]])* expialphax~{i}[]/d ] ; In SurfCutSuper1 ; Integration Int_1 ; Jacobian JSur ; } } }
-      { Name int_z_r~{i} ; Value{ Integral{ [ CompZ[{El}+E1dl[]] * expialphax~{i}[]/d ] ; In SurfCutSuper1 ; Integration Int_1 ; Jacobian JSur ; } } }
-    EndFor
+      { Name lambda_step   ; Value { Local { [ lambda0/nm ]; In Omega ; Jacobian JVol; } } }
+      { Name E2d    ; Value { Local { [ {Et}+{El}      ]; In Omega; Jacobian JVol; } } }
+      { Name Etot   ; Value { Local { [ {Et}+{El}+E1[] ]; In Omega; Jacobian JVol; } } }
+      { Name boundary    ; Value { Local { [ bndCol[]    ]; In Plot_bnd ; Jacobian JVol ; } } }
+      For i In {-nb_plot_periods:nb_plot_periods}
+        { Name Etot~{i}     ; Value { Local { [ ({Et}+{El}+E1[])*Exp[I[]*i*CompX[k1[]]*d] ]; In Omega; Jacobian JVol; } } }
+      EndFor
+      { Name H2d    ; Value { Local { [ (-I[]/(mur[]*mu0*om0))*({Curl Et}+{Curl El}) ]; In Omega; Jacobian JVol; } } }
+      { Name Htot   ; Value { Local { [ (-I[]/(mur[]*mu0*om0))*({Curl Et}+{Curl El})+H1[] ]; In Omega; Jacobian JVol; } } }
+      { Name E2d_z  ; Value { Local { [ CompZ[{Et}+{El}] ]; In Omega; Jacobian JVol; } } }
+      { Name E2d_t  ; Value { Local { [    {Et}          ]; In Omega; Jacobian JVol; } } }
+      { Name Etot_z ; Value { Local { [ CompZ[{Et}+{El}+E1[]] ]; In Omega; Jacobian JVol; } } }
+      { Name Htot_z ; Value { Local { [ CompZ[(-I[]/(mur[]*mu0*om0))*({Curl Et}+{Curl El})+H1[]] ]; In Omega; Jacobian JVol; } } }
+      { Name H2d_z  ; Value { Local { [ CompZ[(-I[]/(mur[]*mu0*om0))*({Curl Et}+{Curl El})] ]; In Omega; Jacobian JVol; } } }
+
+      { Name E1   ; Value { Local { [ E1[]  ]; In Omega; Jacobian JVol; } } }
+      { Name E1t  ; Value { Local { [ E1t[] ]; In Omega; Jacobian JVol; } } }
+      { Name E1l  ; Value { Local { [ E1l[] ]; In Omega; Jacobian JVol; } } }
+
+      { Name Q_subs      ; Value{ Integral{ [  0.5 * ep0*om0*Fabs[epsr2_im[]      ]        * ( SquNorm[{Et}+{El}+E1[]] ) / (Pinc[]*d) ] ; In sub         ; Integration Int_1 ; Jacobian JVol ; } } }
+      { Name Q_rod_out   ; Value{ Integral{ [  0.5 * ep0*om0*Fabs[epsr_rod_out_im[]]       * ( SquNorm[{Et}+{El}+E1[]] ) / (Pinc[]*d) ] ; In rod_out   ; Integration Int_1 ; Jacobian JVol ; } } }
+      { Name Q_layer_dep ; Value{ Integral{ [  0.5 * ep0*om0*Fabs[epsr_layer_dep_im[]]     * ( SquNorm[{Et}+{El}+E1[]] ) / (Pinc[]*d) ] ; In layer_dep   ; Integration Int_1 ; Jacobian JVol ; } } }
+      { Name Q_layer_cov ; Value{ Integral{ [  0.5 * ep0*om0*Fabs[epsr_layer_cov_im[]]     * ( SquNorm[{Et}+{El}+E1[]] ) / (Pinc[]*d) ] ; In layer_cov   ; Integration Int_1 ; Jacobian JVol ; } } }
+      { Name Q_tot       ; Value{ Integral{ [  0.5 * ep0*om0*Fabs[Im[CompZZ[epsilonr[]]]]  * ( SquNorm[{Et}+{El}+E1[]] ) / (Pinc[]*d) ] ; In Plot_domain ; Integration Int_1 ; Jacobian JVol ; } } }
 
-    // // BUGGY
-    For i In {0:2*nb_orders}
-    //   { Name eff_t~{i}   ; Value{ Term{Type Global; [
-    //           1/(Ae^2*beta_subs~{i}[]*-beta1[]) * ((beta_subs~{i}[]^2+gamma[]^2    )*SquNorm[$int_z_t~{i}]+
-    //                                                (beta_subs~{i}[]^2+alpha~{i}[]^2)*SquNorm[$int_x_t~{i}]+
-    //                                               2*alpha~{i}[]*gamma[]*Re[$int_z_t~{i}*Conj[$int_x_t~{i}]] ) ] ; In SurfCutSubs1 ; } } }
-    //   { Name eff_r~{i}   ; Value { Term{Type Global; [
-    //           1/(Ae^2*beta_super~{i}[]*-beta1[]) * ((beta_super~{i}[]^2+gamma[]^2    )*SquNorm[$int_z_r~{i}]+
-    //                                                 (beta_super~{i}[]^2+alpha~{i}[]^2)*SquNorm[$int_x_r~{i}]+
-    //                                                 2*alpha~{i}[]*gamma[]*Re[$int_z_r~{i}*Conj[$int_x_r~{i}]]) ] ; In SurfCutSuper1 ; } } }
-
-      { Name eff_t~{i}   ; Value { Term{Type Global; [
-              1/(Ae^2*-beta1[]) * ( beta_subs~{i}[] * SquNorm[$int_x_t~{i}]+
-                                    beta_subs~{i}[] * SquNorm[$int_y_t~{i}]+
-                                    beta_subs~{i}[] * SquNorm[$int_z_t~{i}] ) ] ; In SurfCutSubs1 ; } } }
-      { Name eff_r~{i}   ; Value { Term{Type Global; [
-              1/(Ae^2*-beta1[]) * ( beta_super~{i}[] * SquNorm[$int_x_r~{i}]+
-                                    beta_super~{i}[] * SquNorm[$int_y_r~{i}]+
-                                    beta_super~{i}[] * SquNorm[$int_z_r~{i}] ) ] ; In SurfCutSuper1 ; } } }                                  
-    EndFor
+      For i In {0:2*nb_orders}
+        { Name int_x_t~{i} ; Value{ Integral{ [ CompX[{Et}+E1t[] ] * expialphax~{i}[]/d ] ; In SurfCutSubs1  ; Integration Int_1 ; Jacobian JSur ; } } }
+        { Name int_y_t~{i} ; Value{ Integral{ [ ({uy}+CompY[E1t[]])* expialphax~{i}[]/d ] ; In SurfCutSubs1  ; Integration Int_1 ; Jacobian JSur ; } } }
+        { Name int_z_t~{i} ; Value{ Integral{ [ CompZ[{El}+E1l[] ] * expialphax~{i}[]/d ] ; In SurfCutSubs1  ; Integration Int_1 ; Jacobian JSur ; } } }
+
+        { Name int_x_r~{i} ; Value{ Integral{ [ CompX[{Et}+E1dt[]] * expialphax~{i}[]/d ] ; In SurfCutSuper1 ; Integration Int_1 ; Jacobian JSur ; } } }
+        { Name int_y_r~{i} ; Value{ Integral{ [({uy}+CompY[E1dt[]])* expialphax~{i}[]/d ] ; In SurfCutSuper1 ; Integration Int_1 ; Jacobian JSur ; } } }
+        { Name int_z_r~{i} ; Value{ Integral{ [ CompZ[{El}+E1dl[]] * expialphax~{i}[]/d ] ; In SurfCutSuper1 ; Integration Int_1 ; Jacobian JSur ; } } }
+      EndFor
+
+      For i In {0:2*nb_orders}
+        { Name eff_t~{i}   ; Value { Term{Type Global; [
+                1/(Ae^2*-beta1[]) * ( beta_subs~{i}[] * SquNorm[$int_x_t~{i}]+
+                                      beta_subs~{i}[] * SquNorm[$int_y_t~{i}]+
+                                      beta_subs~{i}[] * SquNorm[$int_z_t~{i}] ) ] ; In SurfCutSubs1 ; } } }
+        { Name eff_r~{i}   ; Value { Term{Type Global; [
+                1/(Ae^2*-beta1[]) * ( beta_super~{i}[] * SquNorm[$int_x_r~{i}]+
+                                      beta_super~{i}[] * SquNorm[$int_y_r~{i}]+
+                                      beta_super~{i}[] * SquNorm[$int_z_r~{i}] ) ] ; In SurfCutSuper1 ; } } }                                  
+        // // BUGGY
+        // { Name eff_t~{i}   ; Value{ Term{Type Global; [
+        //         1/(Ae^2*beta_subs~{i}[]*-beta1[]) * ((beta_subs~{i}[]^2+gamma[]^2    )*SquNorm[$int_z_t~{i}]+
+        //                                              (beta_subs~{i}[]^2+alpha~{i}[]^2)*SquNorm[$int_x_t~{i}]+
+        //                                             2*alpha~{i}[]*gamma[]*Re[$int_z_t~{i}*Conj[$int_x_t~{i}]] ) ] ; In SurfCutSubs1 ; } } }
+        // { Name eff_r~{i}   ; Value { Term{Type Global; [
+        //         1/(Ae^2*beta_super~{i}[]*-beta1[]) * ((beta_super~{i}[]^2+gamma[]^2    )*SquNorm[$int_z_r~{i}]+
+        //                                               (beta_super~{i}[]^2+alpha~{i}[]^2)*SquNorm[$int_x_r~{i}]+
+        //                                               2*alpha~{i}[]*gamma[]*Re[$int_z_r~{i}*Conj[$int_x_r~{i}]]) ] ; In SurfCutSuper1 ; } } }
+      EndFor
     }
   }
 }
@@ -508,7 +510,6 @@ PostOperation {
         Print[ int_y_r~{i}[SurfCutSuper1], OnGlobal, StoreInVariable $int_y_r~{i}, File > StrCat[myDir, "int_y_r.txt"], Format Table];
         Print[ int_z_r~{i}[SurfCutSuper1], OnGlobal, StoreInVariable $int_z_r~{i}, File > StrCat[myDir, "int_z_r.txt"], Format Table];
       EndFor
-
       For i In {0:2*nb_orders}
         Print[ eff_t~{i}[SurfCutSubs1]  , OnRegion SurfCutSubs1 , File > StrCat[myDir, "eff_t.txt"], Format Table ];
         Print[ eff_r~{i}[SurfCutSuper1] , OnRegion SurfCutSuper1, File > StrCat[myDir, "eff_r.txt"], Format Table ];
@@ -518,6 +519,28 @@ PostOperation {
       Print[ Q_rod_out[rod_out]     , OnGlobal, Format FrequencyTable, File > StrCat[myDir, "absorption-Q_rod_out.txt"]];
       Print[ Q_layer_dep[layer_dep] , OnGlobal, Format FrequencyTable, File > StrCat[myDir, "absorption-Q_layer_dep.txt"]];
       Print[ Q_layer_cov[layer_cov] , OnGlobal, Format FrequencyTable, File > StrCat[myDir, "absorption-Q_layer_cov.txt"]];
+      If(multiplot)
+        Echo[ Str["For i In {PostProcessing.NbViews-1:0:-1}",
+                    "  If(!StrCmp(View[i].Name, 'boundary') || !StrCmp(View[i].Name, 'boundary_Combine'))",
+                    "    Delete View[i];",
+                    "  EndIf",
+                    "EndFor"], File StrCat[myDir,"tmp1.geo"]] ;
+        For i In {-nb_plot_periods:nb_plot_periods}
+          Print [ Etot~{i}, OnElementsOf Plot_domain, File StrCat[myDir, Sprintf("u_tot_lambda%.2fnm_%g.pos", lambda0/nm,i+nb_plot_periods)], ChangeOfCoordinates {$X+i*d,$Y,$Z}, Name Sprintf("Hz_tot_%.2fnm.pos", lambda0/nm) ] ;
+          Print[ boundary, OnElementsOf Plot_bnd, File StrCat[myDir,Sprintf("boundary_%g.pos",i+nb_plot_periods)], ChangeOfCoordinates {$X+i*d,$Y,$Z} ] ;
+        EndFor
+        Echo[ Str["Combine ElementsByViewName;",
+                  "Hide {",
+                  "Point{1,2,7,8,9,10,20,22};",
+                  "Line{1,7,8,9,10,30,32,34,2,3,4,5,6,12,16,20,24,28};",
+                  "Surface{36,48};}",
+                  "Geometry.Color.Lines = {0,0,0};",
+                  "l=PostProcessing.NbViews-1; View[l].ColorTable={Black}; ",
+                  "View[l-1].Visible=1; View[l-1].ShowScale=0;",
+                  "View[l].ShowScale=0; View[l].LineWidth=1.5; View[l].LineType=1;Geometry.LineWidth=0;"],
+                  File StrCat[myDir,"tmp3.geo" ]] ;
+      EndIf
+
     }
   }
 }
diff --git a/DiffractionGratings/grating2D_scalar.pro b/DiffractionGratings/grating2D_scalar.pro
index a726b20..8a8e62b 100644
--- a/DiffractionGratings/grating2D_scalar.pro
+++ b/DiffractionGratings/grating2D_scalar.pro
@@ -458,9 +458,7 @@ PostOperation {
                     "  EndIf",
                     "EndFor"], File StrCat[myDir,"tmp1.geo"]] ;
         For i In {-nb_plot_periods:nb_plot_periods}
-          If (i!=0)
-            Print [ u_tot~{i}, OnElementsOf Plot_domain, File StrCat[myDir, Sprintf("u_tot_lambda%.2fnm_%g.pos", lambda0/nm,i+nb_plot_periods)], ChangeOfCoordinates {$X+i*d,$Y,$Z}, Name Sprintf("Hz_tot_%.2fnm.pos", lambda0/nm) ] ;
-          EndIf
+          Print [ u_tot~{i}, OnElementsOf Plot_domain, File StrCat[myDir, Sprintf("u_tot_lambda%.2fnm_%g.pos", lambda0/nm,i+nb_plot_periods)], ChangeOfCoordinates {$X+i*d,$Y,$Z}, Name Sprintf("Hz_tot_%.2fnm.pos", lambda0/nm) ] ;
           Print[ boundary, OnElementsOf Plot_bnd, File StrCat[myDir,Sprintf("boundary_%g.pos",i+nb_plot_periods)], ChangeOfCoordinates {$X+i*d,$Y,$Z} ] ;
         EndFor
         Echo[ Str["Combine ElementsByViewName;",
-- 
GitLab