(* onestep.nb  *)

Needs["Graphics`ComplexMap`"]

Clear[V,a,b,c,d,e,p,t,frc,frd,frtex,
      nec,nef,neq,tcb,tcf,mult,tex1]
Off[General::spell1]
 
                (* Input data *)
            
V = 1 (* Potential on upper plate in Volt,
         potential on lower plate = 0 *) ; 
a = 3 (* Distance of plates at left side in mm :
         for 2 10^-5 < a < 10^4 *); 
b = 2 (* Distance of plates at the right side in mm :
         for a/10 < b < a/1.0001 *);
         
nef = 11 (* Number of field lines between plates;
            variable needed in the pictures: 
            eflandeqpl7, efl7, absefateflu7, 
            absefateflv7, efuatefl7, efvatefl7 *);
            
neq = 11 (* Number of equipotential lines between plates;
            variable needed in the pictures: 
            eflandeqpl7, eqpll7, absefateqplu7, 
            absefataqplv7, efuateqpl7, efvateqpl7 *);
            
nec = 30 (* Number of lines used in 3D-image of 
            el. field`s absolute value: absef3D7 *);

tcf = .005	(* Relative thickness of field or equipotential  
               lines in color diagrams *);
               
tcb = .011	(* Relative thickness of lines representing 
		   		boundaries, electrodes, pole shoes, etc. *);

                 (* Global variables *)
                  
mult = V 1000/a; tex1 = "E[V/m]"; p = a^2/b^2; 

                     (* Functions *) 

cm7[t_] = 2/Pi (b Log[a/b Sqrt[t + 1] + Sqrt[t + p]] -
                 a Log[Sqrt[t + 1] + Sqrt[t + p]] -
                 (b - a) Log[Sqrt[p - 1]]) - b/Pi Log[t] ;
                 (* Conformal map *)
                 
af70[t_] =  Sqrt[t + p]/Sqrt[t + 1] ; 
ef7[t_] = - I mult Conjugate[af70[t]] ;
             (* Function of el. field *) 
                      
t[z_] = Exp[z] ;            

            (* Variables needed in all pictures *)

e = Re[cm7[t[c]]] ; 
frc = FindRoot[e + 1.5 a, {c,0,8}, 
      AccuracyGoal -> 5 - Floor[N[Log[10,a]]]]; c = c/.frc ;
frd = FindRoot[Re[cm7[t[d]]] + e, {d,-10,-5}]; d = d/.frd ;

Print[
"Distance between plates:" ColumnForm[{SequenceForm["right side a = ", a ," mm"],SequenceForm["left side b = ", b ," mm."]}]]

(* Section with black & white graphics *)

Print["Electric field on upper plate "]
ppup7 = ParametricPlot[
        {Re[cm7[t[x]]],-mult af70[t[x]]}, {x,d,c},
        PlotRegion -> {{.01,.99},{.01,.99}},
        PlotRange -> {{e,-e},{0,-1.05 mult a/b}},
        AxesLabel -> {"u[mm]",tex1}];
      
Print["Electric field lines & equipotentials "]
eflandeqpl7 = CartesianMap[cm7[t[#]] &,
              {d,c,(c - d)/(nef-1)},
              {0,Pi-.001,(Pi-.001)/(neq-1)},
              PlotRange -> All, 
              AxesLabel -> {"u[mm]","v[mm]"},
	          Epilog -> {Thickness[tcb], 
	                    {Line[{{e,0},{-e,0}}],
                        Line[{{e,-a},{0,-a},{0,-b},{-e,-b}}]}}];

Print["Electric field on left side of lower plate "]
ppdownleft7 = ParametricPlot[
              {Re[cm7[-t[-x]]],-mult Re[af70[-t[-x]]]},
              {x,-c,-Log[p] - 10^-7}, 
              PlotRegion -> {{.01,.99},{.01,.99}},
              PlotRange -> {{e,0},{0,-mult}},
              AxesOrigin -> {0,0}, 
              AxesLabel -> {"u[mm]",tex1}] ;
      
Print["Electric field at step of lower plate "]
ppdownstep7 = ParametricPlot[
              {Im[cm7[-t[-x]]],-mult Im[af70[-t[-x]]]},
              {x,-Log[p] + 10^-7,-10^-7}, 
              PlotRegion -> {{.01,.99},{.01,.99}},
              AxesLabel -> {"v[mm]",tex1}, 
              AxesOrigin -> {-b,0}, 
              PlotRange -> {{-a,-b},{0,6 mult}}] ;
  
Print["Electric field on right side of lower plate "]
ppdownright7 = ParametricPlot[
              {Re[cm7[-t[-x]]],-mult af70[-t[-x]]}, 
              {x,10^-7,-d},
              PlotRegion -> {{.01,.99},{.01,.99}},
              PlotRange -> {{0,-e},{0,-1.5 a/b mult}}, 
              AxesOrigin -> {0,0}, 
              AxesLabel -> {"u[mm]",tex1}] ;

Print["Electric field of upper plate without units"]
Show[ppup7, Ticks -> None, AxesLabel -> {"u","E"}] ; 

Print["Electric fieldlines & equipotentials without units"]
Show[eflandeqpl7, Axes -> None] ; 
     
Print["Electric field of lower plate without units"] 
Show[GraphicsArray[{ppdownleft7,ppdownstep7,ppdownright7}, GraphicsSpacing -> 0]/.
     (Ticks -> Automatic) -> (Ticks -> None) /.
     (AxesLabel -> {"u[mm]",tex1}) -> (AxesLabel -> {None,None}) /.
     (AxesLabel -> {"v[mm]",tex1}) -> (AxesLabel -> {None,None}) /.
     (PlotRange -> {{e,0},{0,-mult}}) -> 
       (PlotRange -> {{e,0},{-1.5 a/b mult,1.5 a/b mult}}) /.
     (PlotRange -> {{-a,-b},{0,6 mult}}) -> 
       (PlotRange -> {{-a,-b},{-1.5 a/b mult,1.5 a/b mult}}) /.
     (PlotRange -> {{0,-e},{0,-1.5 a/b mult}}) -> 
       (PlotRange -> {{0,-e},{-1.5 a/b mult,1.5 a/b mult}}) /.
     (PlotRegion -> {{.01,.99},{.01,.99}}) -> 
       (PlotRegion -> {{0,1},{0,1}}),
     Epilog -> { Text["u ->                v ->              u ->",
                      {1.5,.5}] }] ;

(* Section with colour graphics *)

Print[ColumnForm[{"Absolute value of electric field",
                  "in 3D seen from left lower plate"}]]
absef3D7 = ParametricPlot3D[Evaluate[
        {Re[cm7[t[x + I y]]], Im[cm7[t[x + I y]]], 
        Abs[ef7[t[x + I y]] ]}], 
		{x,d/2,c/2},{y,0,Pi-.001}, 
		PlotRange -> {0, 4 mult}, 
  		AxesLabel -> {"u[mm]","v[mm]","|E| [V/m]"}, 
  		PlotPoints -> nec,
  		BoxRatios -> {2,1,1},
  		ViewPoint -> {-1.737, -1.657, 1.5}];

hli =	Table[Hue[N[1 - (k-1)/(neq - 2)]],{k,neq-2}] ;
hlc =  	Join[{GrayLevel[0]}, Reverse[hli], {GrayLevel[0]}] ;
hlt = 	Table[Thickness[tcf],{k,neq}] ;
styleq = Transpose[{hlc,hlt}] ;

Print[ColumnForm[{"Absolute value of electric field in 3D",
                  "seen from below left lower plate"}]]
eqpl7 = ParametricPlot[ Evaluate[Table[
		{Re[cm7[t[(k-1)/(neq-1) Pi I + x]]], 
		 Im[cm7[t[(k-1)/(neq-1) Pi I + x]]]},
		{k,neq}]],{x,d,c}, PlotRange -> {0,-1.01a}, 
		PlotRegion -> {{.01,.99},{.01,.99}},
		AxesLabel -> {"u [mm]","v [mm]"},
		PlotStyle -> styleq,
		Epilog -> { Thickness[.011], { Line[{{e,0},{-e,0}}],
                  Line[{{e,-a},{0,-a},{0,-b},{-e,-b}}] }} ] ;

Print[ColumnForm[
	  {"Absolute electric fields on field lines as",
	  "functions of u (colours match the colours above)"}]]
absefateqplu7 = ParametricPlot[ Evaluate[Table[
	 {Re[cm7[t[(k-1)/(neq-1) Pi I + x]]], 
	 Abs[ef7[t[(k-1)/(neq-1) Pi I + x]]]},
		{k,neq}]],{x,d,c}, 
		PlotRange -> {All,{0,1.5 mult a/b}},
		PlotRegion -> {{.01,.99},{.01,.99}},	
		AxesLabel -> {"u [mm]", "|E(u)| [V/m]"},
		PlotStyle -> styleq ];

Print[ColumnForm[
	  {"Absolute electric fields on field lines as", 
	  "functions of v (colours match the colours above)"}]]
absefateqplv7 = ParametricPlot[ Evaluate[Table[
	 {Im[cm7[t[(k-1)/(neq-1) Pi I + x]]], 
	 Abs[ef7[t[(k-1)/(neq-1) Pi I + x]]]},
		{k,neq}]],{x,d,c}, 
		PlotRange -> {All,{0,1.5 mult a/b}},
		PlotRegion -> {{.01,.99},{.01,.99}},	
		AxesLabel -> {"v [mm]", "|E(v)| [V/m]"},
		PlotStyle -> styleq ];

Print[ColumnForm[
	  {"Electric fields (v-component) on field lines as", 
	  "functions of u (colours match the colours above)"}]]
efvateqpl7 = ParametricPlot[ Evaluate[Table[
	 {Re[cm7[t[(k-1)/(neq-1) Pi I + x]]], 
	  Im[ef7[t[(k-1)/(neq-1) Pi I + x]]]},
		{k,neq}]],{x,d,c}, 
		PlotRegion -> {{.01,.99},{.01,.99}},	
		AxesLabel -> {"u [mm]", "\!\(E\_v\) [V/m]"},
		PlotStyle -> styleq ];

Print[ColumnForm[
	  {"Electric fields (u-component) on field lines as", 
	  "functions of v (colours match the colours above)"}]]
efuateqpl7 = ParametricPlot[ Evaluate[Table[
	 {Im[cm7[t[(k-1)/(neq-1) Pi I + x]]], 
	  Re[ef7[t[(k-1)/(neq-1) Pi I + x]]]},
		{k,neq}]],{x,d,c}, 
		PlotRange -> {All,{0,1.5 mult a/b}},
		PlotRegion -> {{.01,.99},{.01,.99}},	
		AxesLabel -> {"v [mm]", "\!\(E\_u\) [V/m]"},
		PlotStyle -> styleq ];

hli =  	Table[Hue[N[1 - (k-1)/(nef - 2)]],{k,nef-2}] ;
hlc = 	Join[{GrayLevel[0]}, hli, {GrayLevel[0]}] ;
hlt = 	Table[Thickness[tcf],{k,nef}] ;
stylfe = Transpose[{hlc,hlt}] ;
dx  = 	c - d;

Print[nef," Electric field lines"]
efl7 = ParametricPlot[ Evaluate[Table[
       {Re[cm7[t[d + (k-1)/(nef-1) dx  + I y]]], 
        Im[cm7[t[d + (k-1)/(nef-1) dx  + I y]]]},
		{k,nef}]],{y,0,Pi-.001}, 
		PlotRange -> All,
		PlotRegion -> {{.01,.99},{.01,.99}},
		AxesLabel -> {"u [mm]","v [mm]"},
		PlotStyle -> stylfe,
     	Epilog -> { Thickness[tcb],{ Line[{{e,0},{-e,0}}],
                    Line[{{e,-a},{0,-a},{0,-b},{-e,-b}}] }} ] ;

Print[ColumnForm[
	  {"Absolute electric fields on field lines as",
	  "functions of u (colours match the colours above)"}]]
absefateflu7 = ParametricPlot[ Evaluate[Table[
       {Re[cm7[t[d + (k-1)/(nef-1) dx  + I y]]], 
	    Abs[ef7[t[d + (k-1)/(nef-1) dx  + I y]]]},
		{k,nef}]],{y,0,Pi-.001},
		PlotRange -> {All,{0,1.5 mult a/b}},
		PlotRegion -> {{.01,.99},{.01,.99}},	
		AxesLabel -> {"u [mm]", "|E(u)| [V/m]"},
		PlotStyle -> stylfe ];

Print[ColumnForm[
	  {"Absolute electric fields on field lines as", 
	  "functions of v (colours match the colours above)"}]]
absefateflv7 = ParametricPlot[ Evaluate[Table[
       {Im[cm7[t[d + (k-1)/(nef-1) dx  + I y]]], 
	    Abs[ef7[t[d + (k-1)/(nef-1) dx  + I y]]]},
		{k,nef}]],{y,0,Pi-.001}, 
		PlotRange -> {All,{0,1.5 mult a/b}},
		PlotRegion -> {{.01,.99},{.01,.99}},	
		AxesLabel -> {"v [mm]", "|E(v)| [V/m]"},
		PlotStyle -> stylfe ];

Print[ColumnForm[
	  {"Electric fields (v-component) on field lines as", 
	  "functions of u (colours match the colours above)"}]]
efvatefl7 = ParametricPlot[ Evaluate[Table[
	  {Re[cm7[t[d + (k-1)/(nef-1) dx  + I y]]], 
	   Im[ef7[t[d + (k-1)/(nef-1) dx  + I y]]]},
	   {k,nef}]],{y,0,Pi-.001}, 
	   PlotRange -> {All,{0,-1.5 mult a/b}},
	   PlotRegion -> {{.01,.99},{.01,.99}},	
	   AxesLabel -> {"u [mm]", "\!\(E\_v\) [V/m]"},
	   PlotStyle -> stylfe ];

Print[ColumnForm[
	  {"Electric fields (u-component) on field lines as", 
	  "functions of v (colours match the colours above)"}]]
efuatefl7 = ParametricPlot[ Evaluate[Table[
	   {Im[cm7[t[d + (k-1)/(nef-1) dx  + I y]]], 
	    Re[ef7[t[d + (k-1)/(nef-1) dx  + I y]]]},
		{k,nef}]],{y,0,Pi-.001}, 
		PlotRange -> {All,{0,.5 mult a/b}},
		PlotRegion -> {{.01,.99},{.01,.99}},	
		AxesLabel -> {"v [mm]", "\!\(E\_u\) [V/m]"},
		PlotStyle -> stylfe ];

Clear[up,vp,frefp,efp,error,imefp,s]

up = -2; vp = -2.8;
(* above you may insert values of the Cartesian coordinates 
u and v (in mm) of a point of your choice within the area 
shown in Fig. eflandeqpl3 *) 
				
Which[vp > 0,     Print[vp " = vp > 0  is unphysical.",⚠],
      -a < vp < -b && up > 0 || vp < -a, 
      Print[ColumnForm[{SequenceForm[-a " < vp = ",vp," < ",-b ," and ", up ," = up > 0"], 
            SequenceForm["or ",vp ," = vp < ",-a , " is unphysical.",⚠]}]],
True,

frefp = FindRoot[cm7[t[efp]] - (up + I vp), 
                 {efp,-Sign[up] Sign[b-a] (d-c)/2 + Pi I}];
efp = efp /. frefp; imefp = N[Im[efp]];
error = Abs[N[cm7[t[efp]] - (up + I vp)]];

Which[0 > imefp, 	 Print["The calculated point doesn`t fulfill the requirements!",\[FreakedSmiley]], 
     imefp > N[Pi], Print["The calculated point doesn`t fulfill the requirements!",\[FreakedSmiley]],
     error > 10^-6, Print["The error for the calculated point is too big !",☹], 
     True, Print["The calculated point fulfills the requirements.",☺];
     
valef = ef7[t[efp]]//N;
Print[
SequenceForm["At point (up,vp) = (",up ,", ",vp ,") mm : ",
"Potential = ",N[V (1-Im[efp]/Pi)], " V;"]];
Print[SequenceForm["Field (\!\(E\_u\),\!\(E\_v\)) = (", 
Re[valef],", ", Im[valef],") V/m "]]; 
caleqpl7 = ParametricPlot[Evaluate[
	  {Re[cm7[t[I Im[efp] + s]]], Im[cm7[t[I Im[efp] + s]]]}], 
	  {s,d,c}, 
	  PlotStyle -> Dashing[{.01}], 
	  DisplayFunction -> Identity];
calefl7 = ParametricPlot[Evaluate[
	  {Re[cm7[t[Re[efp] + I s]]], Im[cm7[t[Re[efp] + I s]]]}],
	  {s,0,Pi-.00001}, 
	  DisplayFunction -> Identity];
	  
Show[caleqpl7,calefl7, 
      DisplayFunction -> $DisplayFunction,
      PlotRange -> All,
	  AxesLabel -> {"u","v"},
 	  Epilog -> { Thickness[tcb],
                {Line[{{e,0},{-e,0}}],
                 Line[{{e,-a},{0,-a},{0,-b},{-e,-b}}]}}]; ]]
                 
On[General::spell1]