bmad-sim / bmad-ecosystem

Bmad simulation ecosystem for simulating high energy storage rings.
https://www.classe.cornell.edu/bmad/
12 stars 13 forks source link

Tao `python plot_lat_layout` and `floor_plan` lack information to generate a complete plot #1057

Closed ken-lauer closed 2 months ago

ken-lauer commented 2 months ago

With tao -init $ACC_ROOT_DIR/bmad-doc/tao_examples/cbeta_cell/tao.init, we have a layout that looks like this:

image

python plot_lat_layout provides information about 4 elements:

Tao> python plot_lat_layout
2;  0.00000000000000E+00;  5.00000000000000E-02;1;box;  1.00E-02;  1.00E-02;black;
6;  1.65302449549514E-01;  2.15302449549514E-01;1;box;  1.00E-02;  1.00E-02;black;
8;  2.15302449549514E-01;  3.22699081698724E-01;1;asym_var:box; -3.84E+00;  0.00E+00;blue;
11;  3.22699081698724E-01;  3.92699081698724E-01;1;box;  1.00E-02;  1.00E-02;black;

The lattice:

Tao> show lat
# Universe 1. Values shown are for the Downstream End of each Element:
# Index  name             key                          s          l    beta   phi_a    eta   orbit    beta   phi_b    eta   orbit  Track
#                                                                         a   [2pi]      x  x [mm]       b   [2pi]      y  y [mm]  State
      0  BEGINNING        Beginning_Ele         0.000000        ---    0.73   0.000  -0.02 -12.850    0.58   0.000   0.00   0.000  Alive
      1  FF.MAR.BEG       Marker                0.000000   0.000000    0.73   0.000  -0.02 -12.850    0.58   0.000   0.00   0.000  Alive
      2  FF.PIP01A        Pipe                  0.050000   0.050000    1.38   0.019  -0.02 -17.025    0.27   0.048   0.00   0.000  Alive
      3  FF.QUA01#1       Quadrupole            0.107651   0.057651    1.87   0.031  -0.01 -19.187    0.18   0.164   0.00   0.000  Alive
      4  FF.QUA01.MAR.MID Marker                0.107651   0.000000    1.87   0.031  -0.01 -19.187    0.18   0.164   0.00   0.000  Alive
      5  FF.QUA01#2       Quadrupole            0.165302   0.057651    1.20   0.045  -0.02 -14.861    0.52   0.243   0.00   0.000  Alive
      6  FF.PIP01         Pipe                  0.215302   0.050000    0.50   0.070  -0.03  -8.904    1.17   0.267   0.00   0.000  Alive
      7  FF.PATCH01A      Patch                 0.215302   0.000000    0.51   0.070  -0.03  -8.953    1.17   0.267   0.00   0.000  Alive
      8  FF.QUA02         Quadrupole            0.322699   0.107397    0.22   0.294  -0.03  -7.033    1.35   0.292   0.00   0.000  Alive
      9  FF.PATCH01B      Patch                 0.322699   0.000000    0.22   0.294  -0.03  -7.005    1.35   0.292   0.00   0.000  Alive
     10  FF.PATCH02       Patch                 0.322699   0.000000    0.22   0.294  -0.03  -7.005    1.35   0.292   0.00   0.000  Alive
     11  FF.PIP02         Pipe                  0.392699   0.070000    0.73   0.363  -0.02 -12.850    0.58   0.322   0.00   0.000  Alive
     12  FF.MAR.END       Marker                0.392699   0.000000    0.73   0.363  -0.02 -12.850    0.58   0.322   0.00   0.000  Alive
     13  END              Marker                0.392699   0.000000    0.73   0.363  -0.02 -12.850    0.58   0.322   0.00   0.000  Alive
Lord Elements:
     14  FF.QUA01         Quadrupole            0.165302   0.115302    1.20   0.045  -0.02     ---    0.52   0.243   0.00     ---  Not_Set
     15  O_FF_ANGLE_DEG   Overlay               0.322699        ---    0.22   0.294  -0.03     ---    1.35   0.292   0.00     ---  Not_Set
     16  O_APERTURE       Overlay               0.322699        ---    0.22   0.294  -0.03     ---    1.35   0.292   0.00     ---  Not_Set
     17  O_QUAD_LENGTH    Overlay               0.322699   0.222699    0.22   0.294  -0.03     ---    1.35   0.292   0.00     ---  Not_Set
# Index  name             key                          s          l    beta   phi_a    eta   orbit    beta   phi_b    eta   orbit  Track
#                                                                         a   [2pi]      x  x [mm]       b   [2pi]      y  y [mm]  State
# Universe 1. Values shown are for the Downstream End of each Element:

This appears to be related to the "lord" element skip: https://github.com/bmad-sim/bmad-ecosystem/blob/c8d13b3eccd290d2329cc0305d4092f63ce8f708/tao/code/tao_python_cmd.f90#L5623

Floor plan output skips a number of elements as well: https://github.com/bmad-sim/bmad-ecosystem/blob/c8d13b3eccd290d2329cc0305d4092f63ce8f708/tao/code/tao_python_cmd.f90#L4274-L4278

Tao> python floor_plan top.g
0;1;Marker; 0.0000000E+00; 0.0000000E+00; 0.0000000E+00; 0.0000000E+00; 0.0000000E+00; 0.0000000E+00;0;;  0.00E+00;  0.00E+00;;
0;2;Pipe; 0.0000000E+00; 0.0000000E+00; 0.0000000E+00; 5.0000000E-02; 0.0000000E+00; 0.0000000E+00;1;box;  1.00E-02;  1.00E-02;black;
0;4;Marker; 1.0765122E-01; 0.0000000E+00; 0.0000000E+00; 1.0765122E-01; 0.0000000E+00; 0.0000000E+00;0;;  0.00E+00;  0.00E+00;;
0;6;Pipe; 1.6530245E-01; 0.0000000E+00; 0.0000000E+00; 2.1530245E-01; 0.0000000E+00; 0.0000000E+00;1;box;  1.00E-02;  1.00E-02;black;
0;7;Patch; 2.1530245E-01; 0.0000000E+00;-3.9269908E-02; 2.1530245E-01; 0.0000000E+00;-3.9269908E-02;0;;  0.00E+00;  0.00E+00;;
0;8;Quadrupole; 2.1629737E-01; 2.5322462E-02;-3.9269908E-02; 3.2361121E-01; 2.1106090E-02;-3.9269908E-02;1;asym_var:box; -3.84E+00;  0.00E+00;blue;
0;9;Patch; 3.2261628E-01;-4.2163720E-03;-7.8539816E-02; 3.2261628E-01;-4.2163720E-03;-7.8539816E-02;0;;  0.00E+00;  0.00E+00;;
0;10;Patch; 3.2261628E-01;-4.2163720E-03;-7.8539816E-02; 3.2261628E-01;-4.2163720E-03;-7.8539816E-02;0;;  0.00E+00;  0.00E+00;;
0;11;Pipe; 3.2261628E-01;-4.2163720E-03;-7.8539816E-02; 3.9240050E-01;-9.7085087E-03;-7.8539816E-02;1;box;  1.00E-02;  1.00E-02;black;
0;12;Marker; 3.9240050E-01;-9.7085087E-03;-7.8539816E-02; 3.9240050E-01;-9.7085087E-03;-7.8539816E-02;0;;  0.00E+00;  0.00E+00;;
0;13;Marker; 3.9240050E-01;-9.7085087E-03;-7.8539816E-02; 3.9240050E-01;-9.7085087E-03;-7.8539816E-02;0;;  0.00E+00;  0.00E+00;;
0;14;Quadrupole; 5.0000000E-02; 0.0000000E+00; 0.0000000E+00; 1.6530245E-01; 0.0000000E+00; 0.0000000E+00;1;asym_var:box;  3.96E+00;  0.00E+00;blue;
DavidSagan commented 2 months ago

@ken-lauer Fixed. Notice that the output of plot_lat_layout has changed.

ken-lauer commented 2 months ago

Thank you for addressing this so quickly, @DavidSagan!

I'm running into an out-of-bounds error now with python floor_plan. Here's how to reproduce it:

tao -init $ACC_ROOT_DIR/regression_tests/python_test/tao.init_photon -external_plotting -noplot
Tao> python place_buffer
r13;floor_plan
r23;photon_intensity
r33;orbit
layout;lat_layout

Tao> place -no_buffer r13 floor_plan
Tao> python floor_plan r13.g
At line 4272 of file /Users/runner/miniforge3/conda-bld/bmad_1721028304028/work/tao/code/tao_python_cmd.f90
Fortran runtime error: Index '1' of dimension 1 of array 'g%floor_list' below lower bound of 2314885530818453536

Error termination. Backtrace:
...

This also happens in tao.init_wall if that helps.

DavidSagan commented 2 months ago

I could not reproduce but I put in what should be a fix.

ken-lauer commented 2 months ago

I'm still seeing it with the new release. It appears to be a difference of -noplot vs plotting mode.

Just -external_plotting:

$ tao -init $ACC_ROOT_DIR/regression_tests/python_test/tao.init_wall -external_plotting
Tao> place -no_buffer r12 floor_plan

Tao> python floor_plan r12.g
0;0;Beginning_Ele; 0.0000000E+00; 0.0000000E+00; 0.0000000E+00; 0.0000000E+00; 0.0000000E+00; 0.0000000E+00;0;;  0.00E+00;  0.00E+00;;
0;1;SBend; 0.0000000E+00; 0.0000000E+00; 0.0000000E+00; 1.0000000E+00; 0.0000000E+00; 0.0000000E+00;1;box;  0.00E+00;  0.00E+00;black;; 1.0000000E+00; 0.0000000E+00; 0.0000000E+00; 0.0000000E+00
0;2;Marker; 1.0000000E+00; 0.0000000E+00; 0.0000000E+00; 1.0000000E+00; 0.0000000E+00; 0.0000000E+00;0;;  0.00E+00;  0.00E+00;;

-noplot and -external_plotting:

$ tao -init $ACC_ROOT_DIR/regression_tests/python_test/tao.init_wall -external_plotting -noplot
Tao> place -no_buffer r12 floor_plan

Tao> python floor_plan r12.g
[ERROR | 2024-JUL-16 08:03:43] tao_python_cmd:
    "python floor_plan r12.g": Floor plan drawing not yet setup for this graph.
INVALID
At line 4276 of file /Users/runner/miniforge3/conda-bld/bmad_1721087783820/work/tao/code/tao_python_cmd.f90
Fortran runtime error: Index '1' of dimension 1 of array 'g%floor_list' below lower bound of 2314885530818453536

Error termination. Backtrace:
#0  0x1064b7cf7
(...clipped...)

Certainly it should not crash (looks like it's missing a return statement in the recent fix). -> However, "floor plan drawing" should have been initialized even in the -noplot case, right?

Separately, would you consider allowing a bmad-ecosystem contribution where pytao runs its test suite on your latest changes? While it may break at times when the python command output changes, but it might also help in the development/tweaking of all its sub-commands.

DavidSagan commented 2 months ago

Yes it was a missing return statement. The floor_plan drawing info is only initialized when the floor_plan is plotted and with the -noplot option the place command is not enough to cause the plotting calc.

And yes the pytao tests can be run. Currently, the pytao test does is not configured to be compatible with the regression test script. We can zoom to discuss.

DavidSagan commented 2 months ago

And the latest code in the repo does have the return statement.

ken-lauer commented 2 months ago

The floor_plan drawing info is only initialized when the floor_plan is plotted and with the -noplot option the place command is not enough to cause the plotting calc.

Is there a chance you'd consider addressing this on the Tao side? I'm working on a Python plotting backend that doesn't rely on the existence of X forwarding - specifically matplotlib/bokeh with Jupyter notebook integration. Requiring normal tao plotting to be enabled just to make the floor plan wouldn't be great.

DavidSagan commented 2 months ago

I should have said the plotting setup needs to be done. This is independent of X.

ken-lauer commented 2 months ago

I understand now - thanks. Would you like me to open a new issue as a reminder of the request, or is this one sufficient?

DavidSagan commented 2 months ago

Do you have time to zoom?