1
$\begingroup$

I'm trying to write a script that will dynamically update when I change the tensor that I am working with and the interval tensor elements.

Whenever I update the tensor elements that I am working with in the Dynamic function with the manipulate function, the values are updated, and then it is automatically set to zero again and the plot is reset, as shown in the video here.

Here is the following code

Dynamic[Manipulate[Module[{c, \[Epsilon], cx, cy, cz, kin, kout, Epin, Esin, Epout, Esout, Rotx, Roty, Rotz, Rot, chiprime, PxPin, 
     PyPin, PzPin, PxSin, PySin, PzSin, pinpout, pinsout, sinpout, sinsout, Ipinpout, Ipinsout, Isinpout, Isinsout}, 
     
    c = 3*10^8; 
    
    \[Epsilon] = 8.85/10^12; 
    
    {cx, cy, cz} := {0, 0, 1}; 
    
    kin[\[Theta]_] := {-Sin[\[Theta]], 0, -Cos[\[Theta]]}; 
     kout[\[Theta]_] := {Sin[\[Theta]], 0, Cos[\[Theta]]}; 
     Epin[\[Theta]_] := {Cos[\[Theta]], 0, Sin[\[Theta]]}; 
     Esin[\[Theta]_] := {0, 1, 0}; 
     Epout[\[Theta]_] := {Cos[\[Theta]], 0, -Sin[\[Theta]]}; 
     Esout[\[Theta]_] = {0, 1, 0}; 
     Rot[\[Phi]_] := {{Cos[\[Phi]] + cx^2*(1 - Cos[\[Phi]]), cx*cy*(1 - Cos[\[Phi]]) - cz*Sin[\[Phi]], cx*cz*(1 - Cos[\[Phi]]) - cy*Sin[\[Phi]]}, 
       {cx*cy*(1 - Cos[\[Phi]]) + cz*Sin[\[Phi]], Cos[\[Phi]] + cy^2*(1 - Cos[\[Phi]]), cy*cz*(1 - Cos[\[Phi]]) - cx*Sin[\[Phi]]}, 
       {cx*cz*(1 - Cos[\[Phi]]) - cy*Sin[\[Phi]], cy*cz*(1 - Cos[\[Phi]]) + cx*Sin[\[Phi]], Cos[\[Phi]] + cz^2*(1 - Cos[\[Phi]])}}; 
       
     chiC1 := {{{xxx, xxy, xxz}, {xxy, xyy, xyz}, {xxz, xyz, xzz}}, {{yxx, yxy, yxz}, {yxy, yyy, yyz}, 
         {yxz, yyz, yzz}}, {{zxx, zxy, zxz}, {zxy, zyy, zyz}, {zxz, zyz, zzz}}}; 
         
     chiprime[\[Phi]_, chi_] := FullSimplify[Transpose[Rot[\[Phi]] . Transpose[Rot[\[Phi]] . (chi . Transpose[Rot[\[Phi]]])]]];
      
     PxPin[\[Theta]_, \[Phi]_, chi_] := Sum[chiprime[\[Phi], chi][[1,i]][[j]]*Epin[\[Theta]][[i]]*Epin[\[Theta]][[j]], {i, 1, 3}, {j, 1, 3}]; 
     PyPin[\[Theta]_, \[Phi]_, chi_] := Sum[chiprime[\[Phi], chi][[2,i]][[j]]*Epin[\[Theta]][[i]]*Epin[\[Theta]][[j]], {i, 1, 3}, {j, 1, 3}]; 
     PzPin[\[Theta]_, \[Phi]_, chi_] := Sum[chiprime[\[Phi], chi][[3,i]][[j]]*Epin[\[Theta]][[i]]*Epin[\[Theta]][[j]], {i, 1, 3}, {j, 1, 3}]; 
     PxSin[\[Theta]_, \[Phi]_, chi_] := Sum[chiprime[\[Phi], chi][[1,i]][[j]]*Esin[\[Theta]][[i]]*Esin[\[Theta]][[j]], {i, 1, 3}, {j, 1, 3}]; 
     PySin[\[Theta]_, \[Phi]_, chi_] := Sum[chiprime[\[Phi], chi][[2,i]][[j]]*Esin[\[Theta]][[i]]*Esin[\[Theta]][[j]], {i, 1, 3}, {j, 1, 3}]; 
     PzSin[\[Theta]_, \[Phi]_, chi_] := Sum[chiprime[\[Phi], chi][[3,i]][[j]]*Esin[\[Theta]][[i]]*Esin[\[Theta]][[j]], {i, 1, 3}, {j, 1, 3}]; 
     
     pinpout[\[Theta]_, \[Phi]_, chi_] := FullSimplify[(PxPin[\[Theta], \[Phi], chi]*Cos[\[Theta]])^2 + (PzPin[\[Theta], \[Phi], chi]*Sin[\[Theta]])^2]; 
     
     Ipinpout[\[Theta]_, \[Phi]_, chi_] := (1/2)*c*\[Epsilon]*pinpout[\[Theta], \[Phi], chi]^2; PolarPlot[Ipinpout[\[Theta], \[Phi], chi], {\[Phi], 0, 2*Pi},
      
      PlotRange -> All, PlotLabel -> Subscript[p, in]*Subscript[p, out]]], 
      
      {chi, {chiC1}, ControlType -> PopupMenu}, 
      
   Delimiter,
   
    {{xxx, 0, "xxx"}, -1, 1, 0.1, Appearance -> "Labeled"}]]
$\endgroup$
6
  • $\begingroup$ I can't test it right now, but removing the Dynamic wrapped around the Manipulate should prevent the sliders from resetting $\endgroup$
    – Lukas Lang
    Commented Jul 31, 2022 at 22:35
  • $\begingroup$ @LukasLang that is what I advised the OP to do in first comment to his other similar question here but for some reason they insist in putting Dynamic around Manipulate. $\endgroup$
    – Nasser
    Commented Jul 31, 2022 at 23:39
  • $\begingroup$ When I remove Dynamic, the plot does not update when I use the sliders. Do you know of another way to actively change the sliders to update the plot without using Dynamic? $\endgroup$
    – Yeknic
    Commented Jul 31, 2022 at 23:56
  • $\begingroup$ Does the code work outside of Manipulate? Before getting to work inside, make sure it works OK outside. Makeup some values to test it outside first. You'll find coding errors much easier outside Manipulate. Once you fix all the errors, then move the code inside Manipulate. $\endgroup$
    – Nasser
    Commented Aug 1, 2022 at 0:07
  • $\begingroup$ The code that I am working with is a part longer string of code that I reduced to show the problem easier. The code does work outside of Manipulate but it involves a longer string of code. As such, I am only worried about the auto-resetting of the plot as my problem $\endgroup$
    – Yeknic
    Commented Aug 1, 2022 at 0:49

1 Answer 1

2
$\begingroup$

Here's a working version:

chiC1 = Hold@# -> 
     Map[ToString, #, {3}] &@{{{xxx, xxy, xxz}, {xxy, xyy, 
      xyz}, {xxz, xyz, xzz}}, {{yxx, yxy, yxz}, {yxy, yyy, yyz}, {yxz,
       yyz, yzz}}, {{zxx, zxy, zxz}, {zxy, zyy, zyz}, {zxz, zyz, 
      zzz}}};
chiC1h = Hold@# -> 
     Map[ToString, #, {3}] &@{{{xxx, 0, xxz}, {0, xyy, 0}, {xxz, 0, 
      xzz}}, {{0, yxy, 0}, {yxy, 0, yyz}, {0, yyz, 0}}, {{zxx, 0, 
      zxz}, {0, zyy, 0}, {zxz, 0, zzz}}};
Manipulate[
 Module[{c, ϵ, cx, cy, cz, kin, kout, Epin, Esin, Epout, 
   Esout, Rotx, Roty, Rotz, Rot, chiprime, PxPin, PyPin, PzPin, PxSin,
    PySin, PzSin, pinpout, pinsout, sinpout, sinsout, Ipinpout, 
   Ipinsout, Isinpout, Isinsout}, c = 3*10^8;
  ϵ = 8.85/10^12;
  {cx, cy, cz} := {0, 0, 1};
  kin[θ_] := {-Sin[θ], 0, -Cos[θ]};
  kout[θ_] := {Sin[θ], 0, Cos[θ]};
  Epin[θ_] := {Cos[θ], 0, Sin[θ]};
  Esin[θ_] := {0, 1, 0};
  Epout[θ_] := {Cos[θ], 0, -Sin[θ]};
  Esout[θ_] = {0, 1, 0};
  Rot[ϕ_] := {{Cos[ϕ] + cx^2*(1 - Cos[ϕ]), 
     cx*cy*(1 - Cos[ϕ]) - cz*Sin[ϕ], 
     cx*cz*(1 - Cos[ϕ]) - 
      cy*Sin[ϕ]}, {cx*cy*(1 - Cos[ϕ]) + cz*Sin[ϕ], 
     Cos[ϕ] + cy^2*(1 - Cos[ϕ]), 
     cy*cz*(1 - Cos[ϕ]) - 
      cx*Sin[ϕ]}, {cx*cz*(1 - Cos[ϕ]) - cy*Sin[ϕ], 
     cy*cz*(1 - Cos[ϕ]) + cx*Sin[ϕ], 
     Cos[ϕ] + cz^2*(1 - Cos[ϕ])}};
  chiprime[ϕ_, chi_] := 
   FullSimplify[
    Transpose[
     Rot[ϕ] . 
      Transpose[Rot[ϕ] . (chi . Transpose[Rot[ϕ]])]]];
  PxPin[θ_, ϕ_, chi_] := 
   Sum[chiprime[ϕ, chi][[1, i]][[j]]*Epin[θ][[i]]*
     Epin[θ][[j]], {i, 1, 3}, {j, 1, 3}];
  PyPin[θ_, ϕ_, chi_] := 
   Sum[chiprime[ϕ, chi][[2, i]][[j]]*Epin[θ][[i]]*
     Epin[θ][[j]], {i, 1, 3}, {j, 1, 3}];
  PzPin[θ_, ϕ_, chi_] := 
   Sum[chiprime[ϕ, chi][[3, i]][[j]]*Epin[θ][[i]]*
     Epin[θ][[j]], {i, 1, 3}, {j, 1, 3}];
  PxSin[θ_, ϕ_, chi_] := 
   Sum[chiprime[ϕ, chi][[1, i]][[j]]*Esin[θ][[i]]*
     Esin[θ][[j]], {i, 1, 3}, {j, 1, 3}];
  PySin[θ_, ϕ_, chi_] := 
   Sum[chiprime[ϕ, chi][[2, i]][[j]]*Esin[θ][[i]]*
     Esin[θ][[j]], {i, 1, 3}, {j, 1, 3}];
  PzSin[θ_, ϕ_, chi_] := 
   Sum[chiprime[ϕ, chi][[3, i]][[j]]*Esin[θ][[i]]*
     Esin[θ][[j]], {i, 1, 3}, {j, 1, 3}];
  pinpout[θ_, ϕ_, chi_] := 
   FullSimplify[(PxPin[θ, ϕ, chi]*
        Cos[θ])^2 + (PzPin[θ, ϕ, chi]*
        Sin[θ])^2];
  Ipinpout[θ_, ϕ_, chi_] := (1/2)*c*ϵ*
    pinpout[θ, ϕ, chi]^2;
  PolarPlot[
   Evaluate@Ipinpout[θ, ϕ, ReleaseHold@chi], {ϕ, 0, 
    2*Pi}, PlotRange -> All, 
   PlotLabel -> Subscript[p, in]*Subscript[p, out]]
  ],
 {chi, {chiC1,chiC1h}, ControlType -> PopupMenu}, {θ, {0, π/2}},
 Delimiter,
 Evaluate[
  Sequence @@ ({{Symbol@#, 0.1, #}, -1, 1, 0.1, 
       Appearance -> "Labeled"} & /@ 
     StringJoin @@@ Tuples[{"x", "y", "z"}, {3}])],
 TrackedSymbols :> True
 ]

Things I changed:

  • I added all the sliders back because otherwise the code doesn't work. For the xxx etc. sliders, I use Evaluate[Sequence @@ ({{Symbol@#, 0.1, #}, -1, 1, 0.1, Appearance -> "Labeled"} & /@ StringJoin @@@ Tuples[{"x", "y", "z"}, {3}])] to generate them for me
  • I have moved the definition of chiC1 out of the Manipulate body, because it's usually not a good idea to dynamically redefine the possible options for a Manipulate variable like this. I have also used a value -> label style specification, so that the thing displayed in the dropdown doesn't change when the variables change. Also, I wrapped the value in Hold, such that it is not prematurely evaluated when constructing the controls, but only once inside the body.
  • I added the TrackedSymbols:>True setting. This ensures that all slider variables cause the body of the Manipulate to update, not only those that a literally present (which the xxx are not).
  • I added Evaluate to the expression in PolarPlot to make the whole thing a lot more responsive. This forces the symbolic evaluation (including the FullSimplify to run only once, making everything a lot faster. I have also added ReleaseHold to remove the Hold wrapper around chi, now that we are inside the inner Dynamic.
$\endgroup$
4
  • $\begingroup$ Thank You, is there a way that I can add additional tensors that I can change that have, say with C1h ={{{xxx, 0, xxz}, {0, xyy, 0}, {xxz, 0, xzz}}, {{0, yxy, 0}, {yxy, 0, yyz}, {0, yyz, 0}}, {{zxx, 0, zxz}, {0, zyy, 0}, {zxz, 0, zzz}}} $\endgroup$
    – Yeknic
    Commented Aug 1, 2022 at 18:03
  • $\begingroup$ @Xenconic just add a copy of the first line defining chiC1 (with the appropriate changes for the new tensor, i.e. change the variable name & tensor entries) and add it to the list of allowed values for chi in the manipulate variable spec $\endgroup$
    – Lukas Lang
    Commented Aug 1, 2022 at 19:38
  • $\begingroup$ I added the new tensor chiC1h with the corresponding tensor above, and added it to the manipulate variable spec, but I encountered the error Argument 0 at position 1 is expected to be a symbol., could you give an example in your code? $\endgroup$
    – Yeknic
    Commented Aug 1, 2022 at 19:46
  • $\begingroup$ @Xenconic There was indeed a small mistake, sorry. I have updated the answer with a fixed version & added an example of how to add a second tensor. The issue was being caused by the SymbolName function that I used to convert the tensor elements to strings (so that they don't depend on the manipulate variable values). Of course that doesn't work for numbers such as 0. To fix it, I replaced SymbolName with ToString, which should work in all cases $\endgroup$
    – Lukas Lang
    Commented Aug 1, 2022 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.