Skip to content

Commit bebdfcd

Browse files
authored
Now setting particle_start%t will work in Tao. (bmad-sim#1536)
* Now setting particle_start%t will work in Tao.
1 parent 8960657 commit bebdfcd

File tree

9 files changed

+178
-66
lines changed

9 files changed

+178
-66
lines changed

bmad/code/set_ele_real_attribute.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ subroutine set_ele_real_attribute (ele, attrib_name, value, err_flag, err_print_
5151
a_ptr%r = value
5252
elseif (associated(a_ptr%i)) then
5353
a_ptr%i = nint(value)
54+
elseif (associated(a_ptr%q)) then
55+
a_ptr%q = nint(value)
5456
else
5557
if (logic_option(.true., err_print_flag)) then
5658
call out_io (s_error$, r_name, 'BAD ATTRIBUTE: ' // a_name)

sim_utils/interfaces/sim_utils_interface.f90

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,15 @@ subroutine word_read (in_str, delim_list, word, ix_word, delim, delim_found, out
957957
logical, optional :: ignore_interior
958958
end subroutine
959959

960+
subroutine set_all_ptr (a_ptr, value, delta, value_set)
961+
import
962+
implicit none
963+
type (all_pointer_struct) a_ptr
964+
real(rp) value
965+
real(rp), optional :: value_set
966+
logical, optional :: delta
967+
end subroutine
968+
960969
subroutine str_substitute (string, str_match, str_replace, do_trim, ignore_escaped)
961970
implicit none
962971
character(*) string
@@ -994,6 +1003,13 @@ subroutine string_trim (in_string, out_string, word_len)
9941003
integer word_len
9951004
end subroutine string_trim
9961005

1006+
function value_of_all_ptr (a_ptr) result (value)
1007+
import
1008+
implicit none
1009+
type (all_pointer_struct) a_ptr
1010+
real(rp) value
1011+
end function value_of_all_ptr
1012+
9971013
function virtual_memory_usage() result (usage)
9981014
implicit none
9991015
integer usage

sim_utils/interfaces/sim_utils_struct.f90

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -219,57 +219,5 @@ function is_false (param) result (this_false)
219219

220220
end function is_false
221221

222-
!-------------------------------------------------------------------------------------------
223-
!-------------------------------------------------------------------------------------------
224-
!-------------------------------------------------------------------------------------------
225-
!+
226-
! Function value_of_all_ptr (a_ptr) result (value)
227-
!
228-
! Routine to return the value pointed to by an all_pointer_struct.
229-
!
230-
! Input:
231-
! a_ptr -- all_pointer_struct: Pointer to a variable
232-
!
233-
! Output:
234-
! value -- real(rp): Value pointed to by a_ptr. Set to true$ or false$ if a_ptr%l is associated.
235-
! Set to real_garbage$ if the number of pointer components of a_ptr that
236-
! are associated is not 1 (that is, value is not unique).
237-
!-
238-
239-
function value_of_all_ptr (a_ptr) result (value)
240-
241-
type (all_pointer_struct) a_ptr
242-
real(rp) value
243-
integer n
244-
245-
!
246-
247-
248-
n = 0
249-
250-
if (associated(a_ptr%r)) then
251-
n = n + 1
252-
value = a_ptr%r
253-
endif
254-
255-
if (associated(a_ptr%i)) then
256-
n = n + 1
257-
value = a_ptr%i
258-
endif
259-
260-
if (associated(a_ptr%l)) then
261-
n = n + 1
262-
if (a_ptr%l) then
263-
value = true$
264-
else
265-
value = false$
266-
endif
267-
endif
268-
269-
if (n /= 1) then
270-
value = real_garbage$
271-
endif
272-
273-
end function value_of_all_ptr
274-
275222
end module
223+

sim_utils/misc/set_all_ptr.f90

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
!+
2+
! Subroutine set_all_ptr (a_ptr, value, delta, value_set)
3+
!
4+
! Routine to set real or integer value pointed to by a_ptr.
5+
! If more than one thing is pointed to, an error is generated.
6+
!
7+
! Input:
8+
! a_ptr -- all_pointer_struct: Pointer to a variable
9+
! value -- real(rp): Value to load. An error is generated if the number of pointer components
10+
! of a_ptr that are associated is not 1.
11+
! delta -- real(rp): Default False. If True, load value as a change in value.
12+
!
13+
! Output:
14+
! a_ptr -- all_pointer_struct: Value set.
15+
! value_set -- real(rp): Value set. Useful when delta = True.
16+
!-
17+
18+
subroutine set_all_ptr (a_ptr, value, delta, value_set)
19+
20+
use sim_utils, dummy => set_all_ptr
21+
22+
implicit none
23+
24+
type (all_pointer_struct) a_ptr
25+
real(rp) value
26+
real(rp), optional :: value_set
27+
logical, optional :: delta
28+
integer n
29+
character(*), parameter :: r_name = 'set_all_ptr'
30+
31+
32+
!
33+
34+
n = 0
35+
36+
if (associated(a_ptr%r)) then
37+
n = n + 1
38+
if (logic_option(.false., delta)) then
39+
a_ptr%r = a_ptr%r + value
40+
else
41+
a_ptr%r = value
42+
endif
43+
if (present(value_set)) value_set = a_ptr%r
44+
endif
45+
46+
if (associated(a_ptr%q)) then
47+
n = n + 1
48+
if (logic_option(.false., delta)) then
49+
a_ptr%q = a_ptr%q + value
50+
else
51+
a_ptr%q = value
52+
endif
53+
if (present(value_set)) value_set = a_ptr%q
54+
endif
55+
56+
57+
if (associated(a_ptr%i)) then
58+
n = n + 1
59+
if (logic_option(.false., delta)) then
60+
a_ptr%i = a_ptr%i + nint(value)
61+
else
62+
a_ptr%i = nint(value)
63+
endif
64+
if (present(value_set)) value_set = a_ptr%i
65+
endif
66+
67+
if (n == 1) return
68+
69+
if (n == 0) then
70+
call out_io(s_error$, r_name, 'POINTER NOT ASSOCIATED! PLEASE REPORT THIS!')
71+
return
72+
endif
73+
74+
if (n > 1) then
75+
call out_io(s_error$, r_name, 'MULTIPLE ASSOCIATED POINTERS! PLEASE REPORT THIS!')
76+
return
77+
endif
78+
79+
end subroutine set_all_ptr
80+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
!+
2+
! Function value_of_all_ptr (a_ptr) result (value)
3+
!
4+
! Routine to return the value pointed to by an all_pointer_struct.
5+
!
6+
! Input:
7+
! a_ptr -- all_pointer_struct: Pointer to a variable
8+
!
9+
! Output:
10+
! value -- real(rp): Value pointed to by a_ptr. Set to true$ or false$ if a_ptr%l is associated.
11+
! Set to real_garbage$ if the number of pointer components of a_ptr that
12+
! are associated is not 1 (that is, value is not unique).
13+
!-
14+
15+
function value_of_all_ptr (a_ptr) result (value)
16+
17+
use sim_utils_interface, dummy => value_of_all_ptr
18+
19+
implicit none
20+
21+
type (all_pointer_struct) a_ptr
22+
real(rp) value
23+
integer n
24+
25+
!
26+
27+
28+
n = 0
29+
30+
if (associated(a_ptr%r)) then
31+
n = n + 1
32+
value = a_ptr%r
33+
endif
34+
35+
if (associated(a_ptr%i)) then
36+
n = n + 1
37+
value = a_ptr%i
38+
endif
39+
40+
if (associated(a_ptr%q)) then
41+
n = n + 1
42+
value = a_ptr%q
43+
endif
44+
45+
if (associated(a_ptr%l)) then
46+
n = n + 1
47+
if (a_ptr%l) then
48+
value = true$
49+
else
50+
value = false$
51+
endif
52+
endif
53+
54+
if (n /= 1) then
55+
value = real_garbage$
56+
endif
57+
58+
end function value_of_all_ptr
59+

tao/code/tao_change_mod.f90

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ subroutine tao_change_ele (ele_name, attrib_name, num_str, update, err_flag)
235235
type (ele_pointer_struct), allocatable :: eles(:)
236236

237237
real(rp), allocatable :: change_number(:), old_value(:)
238-
real(rp) new_merit, old_merit, new_value, delta, max_val
238+
real(rp) new_merit, old_merit, delta, max_val, new_value, design_value
239239

240240
integer i, ix, iu, nl, len_name, nd
241241
integer, parameter :: len_lines = 200
@@ -331,23 +331,24 @@ subroutine tao_change_ele (ele_name, attrib_name, num_str, update, err_flag)
331331
do i = 1, nd
332332
if (.not. free(i)) cycle
333333

334-
old_value(i) = m_ptr(i)%r
334+
old_value(i) = value_of_all_ptr(m_ptr(i))
335+
design_value = value_of_all_ptr(d_ptr(i))
335336

336337
if (abs_or_rel == '@') then
337-
m_ptr(i)%r = change_number(i)
338+
call set_all_ptr(m_ptr(i), change_number(i), value_set = new_value)
338339
elseif (abs_or_rel == 'd') then
339-
m_ptr(i)%r = d_ptr(i)%r + change_number(i)
340+
call set_all_ptr(m_ptr(i), design_value + change_number(i), value_set = new_value)
340341
elseif (abs_or_rel == '%') then
341-
m_ptr(i)%r = m_ptr(i)%r * (1 + 0.01 * change_number(i))
342+
call set_all_ptr(m_ptr(i), old_value(i) * (1 + 0.01 * change_number(i)), value_set = new_value)
342343
else
343-
m_ptr(i)%r = m_ptr(i)%r + change_number(i)
344+
call set_all_ptr(m_ptr(i), change_number(i), delta = .true., value_set = new_value)
344345
endif
345346

346-
delta = m_ptr(i)%r - old_value(i)
347+
delta = new_value - old_value(i)
347348

348349
call tao_set_flags_for_changed_attribute(u, e_name, eles(i)%ele, m_ptr(i), who = a_name)
349350

350-
max_val = max(abs(old_value(i)), abs(m_ptr(i)%r), abs(d_ptr(1)%r))
351+
max_val = max(abs(old_value(i)), abs(new_value), abs(design_value))
351352
str = real_num_fortran_format(max_val, 14, 2)
352353
fmt = '(5' // trim(str) // ', 4x, a)'
353354

@@ -356,9 +357,8 @@ subroutine tao_change_ele (ele_name, attrib_name, num_str, update, err_flag)
356357
if (nl < 11) then
357358
name = 'PARTICLE_START'
358359
if (associated(eles(i)%ele)) name = eles(i)%ele%name
359-
nl=nl+1; write (lines(nl), fmt) old_value(i), m_ptr(i)%r, &
360-
old_value(i)-d_ptr(i)%r, m_ptr(i)%r-d_ptr(i)%r, &
361-
m_ptr(i)%r-old_value(i), trim(name)
360+
nl=nl+1; write (lines(nl), fmt) old_value(i), new_value, old_value(i)-design_value, &
361+
new_value-design_value, delta, trim(name)
362362
else
363363
if (.not. etc_added) then
364364
nl=nl+1; lines(nl) = ' ... etc ...'

tao/code/tao_set_flags_for_changed_attribute.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ subroutine tao_set_flags_for_changed_attribute (u, ele_name, ele_ptr, val_ptr, w
4545
select case (who)
4646
case ('PZ'); lat%particle_start%p0c = 0
4747
case ('E_PHOTON'); lat%particle_start%vec(6) = 0
48+
case ('T'); lat%particle_start%vec(5) = real_garbage$ ! So time is used instead of z.
4849
end select
4950
return
5051
endif

tao/code/tao_set_mod.f90

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,13 @@ subroutine tao_set_particle_start_cmd (who, value_str)
13741374

13751375
! Set value
13761376

1377-
a_ptr(1)%r = set_val(1)
1377+
if (associated(a_ptr(1)%r)) then
1378+
a_ptr(1)%r = set_val(1)
1379+
elseif (associated(a_ptr(1)%q)) then ! Time is quad precision
1380+
a_ptr(1)%q = set_val(1)
1381+
else
1382+
call out_io(s_error$, r_name, 'Bad particle_start component: ' // who2)
1383+
endif
13781384
endif
13791385

13801386
call tao_set_flags_for_changed_attribute (u, 'PARTICLE_START', who = who2)

tao/version/tao_version_mod.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
!-
77

88
module tao_version_mod
9-
character(*), parameter :: tao_version_date = "2025/05/23 00:07:15"
9+
character(*), parameter :: tao_version_date = "2025/05/23 10:25:08"
1010
end module

0 commit comments

Comments
 (0)