Skip to content

Commit

Permalink
Merge pull request #617 from bmad-sim/fix/mac-linker2
Browse files Browse the repository at this point in the history
Convert tao hook routines to use function pointers.
  • Loading branch information
DavidSagan authored Nov 3, 2023
2 parents c1b8c24 + e67dc55 commit d5f81ce
Show file tree
Hide file tree
Showing 24 changed files with 262 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ tao/doc/tao.pdf
bmad/doc/bmad.pdf
bsim/**/doc/*.pdf
util/searchf.pyc
util/__pycache__

**/doc/*.idx
**/doc/*.ilg
Expand Down
5 changes: 3 additions & 2 deletions bmad/custom/wall_hit_handler_custom.f90
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
!+
! Subroutine wall_hit_handler_custom (orb, ele, s)
!
! Dummy prototype routine used to customize the action when a particle hits
! Prototype routine for customizing the action when a particle hits
! a wall with Runge-Kutta integration in odeint_bmad.
! To
!
! To use, see the Bmad manual.
!
! Input:
! orb -- coord_struct: coordinates of particle.
Expand Down
13 changes: 10 additions & 3 deletions bmad/doc/misc-programming.tex
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ \section{Custom and Hook Routines}
\bmad calculations, like particle tracking through a lattice element, can be customized using what
are called ``\vn{custom}'' and ``\vn{hook}'' routines. The general idea is that a programmer can
implement custom code which is linked into a program and this custom code will be called at the
appropriate time by \bmad. Thus, for example, custom code can be created for Runge-Kutta tracking
appropriate time by \bmad. For example, custom code can be created for Runge-Kutta tracking
that calculates the electromagnetic field of some complicated electromagnet.

To enable \bmad to be able to call customized code, there are a number of ``\vn{entry} points''
defined in the \bmad code. At each entry point, a ``\vn{dummy}'' version of a custom and hook
To enable \bmad to be able to call customized code, there are a number of \vn{function pointers}
defined in the \bmad code. A function pointer is defined one for each custom or hook routine. By
default, these function pointers are not associated with any functions. At certain places in the
\bmad code, the appropriate function pointer will be checked. If the funtion pointer is associated
with a routine, that routine will be called.

\etcetc

At each entry point, a ``\vn{dummy}'' version of a custom and hook
routine is called. For \vn{hook} routines, this dummy version does nothing except to keep the linker
happy when customized hook routine is not implemented by a program. For \vn{custom} routines, the
dummy version will issue an error message since it should not have been called. That is, the
Expand Down
2 changes: 1 addition & 1 deletion regression_tests/patch_test/output.correct
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"Q2\1" ABS 0 1.806581E-03 1.626139E-03 0.000000E+00 0.000000E+00 -1.637773E-06 0.000000E+00
"P1\1" ABS 0 2.000000E-03 1.333336E-09 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00
"L" REL 1E-12 0.99999800000067
"Flexible" REL 1E-15 8.67E-19 0.00E+00 4.44E-16 2.20E-16 0.00E+00 0.00E+00
"Flexible" ABS 1E-18 8.673617E-19 0.000000E+00 4.440892E-16 2.203099E-16 0.000000E+00 0.000000E+00
"L1-ref" ABS 1E-15 0.001836433639099
"L2-ref" ABS 1E-15 0.000000000000000
"L3-ref" ABS 1E-15 0.001000000000000
2 changes: 1 addition & 1 deletion regression_tests/patch_test/patch_test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ program patch_test
enddo

ele => lat%ele(4)
write (1, '(a, 6es10.2)') '"Flexible" REL 1E-15 ', ele%floor%r, ele%floor%theta, ele%floor%phi, ele%floor%psi
write (1, '(a, 6es14.6)') '"Flexible" ABS 1E-18 ', ele%floor%r, ele%floor%theta, ele%floor%phi, ele%floor%psi

!

Expand Down
2 changes: 1 addition & 1 deletion tao/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ SET (PLOT_LINK_LIBS $ENV{PLOT_LINK_LIBS})

SET (SRC_DIRS
code
hook
## hook # Now using function pointers
version
)

Expand Down
2 changes: 1 addition & 1 deletion tao/code/tao_cmd_end_calc.f90
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ subroutine tao_cmd_end_calc
! update variable values to reflect lattice values

call tao_plot_setup() ! transfer data to the plotting structures
call tao_hook_plot_setup()
if (associated(tao_hook_plot_setup_ptr)) call tao_hook_plot_setup_ptr()

do i = 1, size(s%plot_page%region)
r => s%plot_page%region(i)
Expand Down
6 changes: 4 additions & 2 deletions tao/code/tao_data_and_eval_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,10 @@ recursive subroutine tao_evaluate_a_datum (datum, u, tao_lat, datum_value, valid

! See if there is a hook for this datum

call tao_hook_evaluate_a_datum (found, datum, u, tao_lat, datum_value, valid_value, why_invalid)
if (found) return
if (associated(tao_hook_evaluate_a_datum_ptr)) then
call tao_hook_evaluate_a_datum_ptr (found, datum, u, tao_lat, datum_value, valid_value, why_invalid)
if (found) return
endif

! Set ix_ele, ix_ref, and ix_start.
! Note: To check that a start element was set, need to look at datum%ele_start_name, not ix_start.
Expand Down
11 changes: 7 additions & 4 deletions tao/code/tao_graph_setup_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ subroutine tao_graph_setup (plot, graph)
call tao_remove_blank_characters(graph%curve(i)%component)
enddo

call tao_hook_graph_setup (plot, graph, found)
if (found) return
if (associated(tao_hook_graph_setup_ptr)) then
call tao_hook_graph_setup_ptr (plot, graph, found)
if (found) return
endif

!

Expand Down Expand Up @@ -116,7 +118,7 @@ subroutine tao_graph_setup (plot, graph)
enddo
endif

call tao_hook_graph_postsetup (plot, graph)
if (associated(tao_hook_graph_postsetup_ptr)) call tao_hook_graph_postsetup_ptr (plot, graph)

end subroutine tao_graph_setup

Expand Down Expand Up @@ -2288,7 +2290,8 @@ subroutine tao_calc_data_at_s_pts (tao_lat, curve, comp_sign, good)
endif

s_now = x1 + (ii-1) * (x2-x1) / (size(curve%x_line)-1)
s_now = tao_hook_curve_s_pt (s_now, ii, x1, x2, size(curve%x_line), tao_lat, curve)
if (associated(tao_hook_curve_s_pt_ptr))s_now = &
tao_hook_curve_s_pt_ptr (s_now, ii, x1, x2, size(curve%x_line), tao_lat, curve)

if (s_now > branch%ele(n_ele_track)%s) s_now = branch%ele(n_ele_track)%s
value = 0
Expand Down
4 changes: 2 additions & 2 deletions tao/code/tao_init.f90
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ subroutine tao_init (err_flag)
call tao_init_data (data_file)
call tao_init_building_wall (building_wall_file)

call tao_hook_init1 (init_tao_file)
if (associated(tao_hook_init1_ptr)) call tao_hook_init1_ptr (init_tao_file)

! Seed random number generator

Expand Down Expand Up @@ -392,7 +392,7 @@ subroutine tao_init (err_flag)
! Normally you will want to use tao_hook_init1. However, tao_hook_init2 can be used, for example,
! to set model variable values different from design variable values.

call tao_hook_init2 ()
if (associated(tao_hook_init2_ptr)) call tao_hook_init2_ptr ()

! Draw everything

Expand Down
4 changes: 2 additions & 2 deletions tao/code/tao_init_data_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ subroutine tao_init_data (data_file)
do i = lbound(s%u, 1), ubound(s%u, 1)
call tao_init_data_in_universe (s%u(i), 0)
enddo
call tao_hook_init_data()
if (associated(tao_hook_init_data_ptr)) call tao_hook_init_data_ptr()
call tao_init_data_end_stuff()
return
endif
Expand Down Expand Up @@ -275,7 +275,7 @@ subroutine tao_init_data (data_file)

! Custom data setup?

call tao_hook_init_data()
if (associated(tao_hook_init_data_ptr)) call tao_hook_init_data_ptr()

! Init ix_data array

Expand Down
4 changes: 2 additions & 2 deletions tao/code/tao_init_lattice.f90
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ subroutine tao_init_lattice (namelist_file, err_flag)

! Read lattice info

call tao_hook_init_read_lattice_info (namelist_file)
if (associated(tao_hook_init_read_lattice_info_ptr)) call tao_hook_init_read_lattice_info_ptr (namelist_file)

if (s%com%init_read_lat_info) then
! namelist_file == '' means there is no lattice file so just use the defaults.
Expand Down Expand Up @@ -270,7 +270,7 @@ subroutine tao_init_lattice (namelist_file, err_flag)

! Custom stuff

call tao_hook_init_lattice_post_parse (u)
if (associated(tao_hook_init_lattice_post_parse_ptr)) call tao_hook_init_lattice_post_parse_ptr (u)

! In case there is a match element with match_end = T, propagate the twiss parameters which
! makes the beginning Twiss of the match element match the end Twiss of the previous element
Expand Down
4 changes: 2 additions & 2 deletions tao/code/tao_init_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ subroutine tao_init_global (init_file)

global = s%global ! establish defaults

call tao_hook_init_global (init_file, global)
if (associated(tao_hook_init_global_ptr)) call tao_hook_init_global_ptr(init_file, global)

! read global structure from tao_params namelist
! init_file == '' means there is no lattice file so just use the defaults.
Expand Down Expand Up @@ -179,7 +179,7 @@ subroutine tao_init_beams (init_file)
! Init Beams
! Some init that will be needed with lat sigma tracking

call tao_hook_init_beam ()
if (associated(tao_hook_init_beam_ptr)) call tao_hook_init_beam_ptr()

do i = lbound(s%u, 1), ubound(s%u, 1)
u => s%u(i)
Expand Down
2 changes: 1 addition & 1 deletion tao/code/tao_init_plotting.f90
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ function old_style_title_syntax(iu) result (is_old_style)
! Hook

call number_template_plots()
call tao_hook_init_plotting()
if (associated(tao_hook_init_plotting_ptr)) call tao_hook_init_plotting_ptr()

! And finish

Expand Down
4 changes: 2 additions & 2 deletions tao/code/tao_init_variables_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ subroutine tao_init_variables (var_file)

if (var_file == '') then
call tao_setup_key_table ()
call tao_hook_init_var()
if (associated(tao_hook_init_var_ptr)) call tao_hook_init_var_ptr()
return
endif

Expand Down Expand Up @@ -304,7 +304,7 @@ subroutine tao_init_variables (var_file)

! Call the hook routine.

call tao_hook_init_var ()
if (associated(tao_hook_init_var_ptr)) call tao_hook_init_var_ptr()

! Record the longitudinal position

Expand Down
Loading

0 comments on commit d5f81ce

Please sign in to comment.