Skip to content

Commit 45c8c42

Browse files
authored
Merge pull request #3 from JPTS2/0.1.2
0.1.2
2 parents e7be4fb + 9056b01 commit 45c8c42

30 files changed

+847
-286
lines changed

.github/workflows/007_sol_test.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Test 007-Solenoid
2+
3+
on:
4+
workflow_run:
5+
workflows: [ "SAD Docker Build" ]
6+
types: [ completed ]
7+
schedule:
8+
- cron: '0 2 * * *' # daily at 02:00 UTC
9+
workflow_dispatch: {}
10+
11+
permissions:
12+
contents: read
13+
packages: read
14+
15+
concurrency:
16+
group: test-007-sol-${{ github.ref }}
17+
cancel-in-progress: true
18+
19+
jobs:
20+
drift:
21+
# if triggered by workflow_run, only run when rebuild succeeded
22+
if: ${{ github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' }}
23+
uses: ./.github/workflows/_test_template.yml
24+
with:
25+
pull_tag: latest
26+
test_files: |
27+
# List the relevant tests here (exact paths)
28+
tests/test_007a_sol_off_geo_in_in.py
29+
tests/test_007b_sol_off_geo_in_out.py
30+
tests/test_007c_sol_off_geo_out_in.py
31+
tests/test_007d_sol_off_geo_out_out.py
32+
tests/test_007e_sol_on.py
33+
secrets: inherit

.github/workflows/004_reversal_test.yml renamed to .github/workflows/008_cavi_test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Test 004-Reversal
1+
name: Test 008-Cavity
22

33
on:
44
workflow_run:
@@ -13,7 +13,7 @@ permissions:
1313
packages: read
1414

1515
concurrency:
16-
group: test-004-reversal-${{ github.ref }}
16+
group: test-008-cavi-${{ github.ref }}
1717
cancel-in-progress: true
1818

1919
jobs:
@@ -25,5 +25,5 @@ jobs:
2525
pull_tag: latest
2626
test_files: |
2727
# List the relevant tests here (exact paths)
28-
tests/test_010_line_reversal.py
28+
tests/test_008_cavi.py
2929
secrets: inherit

.github/workflows/002_coord_test.yml renamed to .github/workflows/010_coord_test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Test 002-Coordinate Transform
1+
name: Test 010-Coordinate Transform
22

33
on:
44
workflow_run:
@@ -13,7 +13,7 @@ permissions:
1313
packages: read
1414

1515
concurrency:
16-
group: test-002-coord-${{ github.ref }}
16+
group: test-010-coord-${{ github.ref }}
1717
cancel-in-progress: true
1818

1919
jobs:
@@ -25,5 +25,5 @@ jobs:
2525
pull_tag: latest
2626
test_files: |
2727
# List the relevant tests here (exact paths)
28-
tests/test_002_coord.py
28+
tests/test_010_coord.py
2929
secrets: inherit

.github/workflows/003_sol_test.yml renamed to .github/workflows/012_line_reversal_test.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Test 003-Solenoid
1+
name: Test 012-Reversal
22

33
on:
44
workflow_run:
@@ -13,7 +13,7 @@ permissions:
1313
packages: read
1414

1515
concurrency:
16-
group: test-003-sol-${{ github.ref }}
16+
group: test-012-reversal-${{ github.ref }}
1717
cancel-in-progress: true
1818

1919
jobs:
@@ -25,9 +25,5 @@ jobs:
2525
pull_tag: latest
2626
test_files: |
2727
# List the relevant tests here (exact paths)
28-
tests/test_003_sol_off_geo_in_in.py
29-
tests/test_004_sol_off_geo_in_out.py
30-
tests/test_005_sol_off_geo_out_in.py
31-
tests/test_006_sol_off_geo_out_out.py
32-
tests/test_007_sol_on.py
28+
tests/test_012_line_reversal.py
3329
secrets: inherit

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,8 @@ build_instructions
6767
# Ignore output lattices
6868
# ==============================================================================
6969
/lattice_tests/out
70+
71+
# ==============================================================================
72+
# Ignore local development
73+
# ==============================================================================
74+
/dev

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ A series of ongoing tests are performed to test the equivalence of SAD elements
9191

9292
### Element Tests
9393
[![Drift](https://github.com/JPTS2/sad2xs/actions/workflows/001_drift_test.yml/badge.svg?branch=main)](https://github.com/JPTS2/sad2xs/actions/workflows/001_drift_test.yml)
94-
[![Coordinate Transform](https://github.com/JPTS2/sad2xs/actions/workflows/002_coord_test.yml/badge.svg?branch=main)](https://github.com/JPTS2/sad2xs/actions/workflows/002_coord_test.yml)
95-
[![Solenoid](https://github.com/JPTS2/sad2xs/actions/workflows/003_sol_test.yml/badge.svg?branch=main)](https://github.com/JPTS2/sad2xs/actions/workflows/003_sol_test.yml)
96-
[![Reversal](https://github.com/JPTS2/sad2xs/actions/workflows/004_reversal_test.yml/badge.svg?branch=main)](https://github.com/JPTS2/sad2xs/actions/workflows/004_reversal_test.yml)
94+
<!-- [![Bend](https://github.com/JPTS2/sad2xs/actions/workflows/002_bend_test.yml/badge.svg?branch=main)](https://github.com/JPTS2/sad2xs/actions/workflows/002_bend_test.yml) -->
95+
<!-- [![Quadrupole](https://github.com/JPTS2/sad2xs/actions/workflows/003_quad_test.yml/badge.svg?branch=main)](https://github.com/JPTS2/sad2xs/actions/workflows/003_quad_test.yml) -->
96+
<!-- [![Sextupole](https://github.com/JPTS2/sad2xs/actions/workflows/004_sext_test.yml/badge.svg?branch=main)](https://github.com/JPTS2/sad2xs/actions/workflows/004_sext_test.yml) -->
97+
<!-- [![Octupole](https://github.com/JPTS2/sad2xs/actions/workflows/005_oct_test.yml/badge.svg?branch=main)](https://github.com/JPTS2/sad2xs/actions/workflows/005_oct_test.yml) -->
98+
<!-- [![Multipole](https://github.com/JPTS2/sad2xs/actions/workflows/006_mult_test.yml/badge.svg?branch=main)](https://github.com/JPTS2/sad2xs/actions/workflows/006_mult_test.yml) -->
99+
[![Solenoid](https://github.com/JPTS2/sad2xs/actions/workflows/007_sol_test.yml/badge.svg?branch=main)](https://github.com/JPTS2/sad2xs/actions/workflows/007_sol_test.yml)
100+
[![Cavity](https://github.com/JPTS2/sad2xs/actions/workflows/008_cavi_test.yml/badge.svg?branch=main)](https://github.com/JPTS2/sad2xs/actions/workflows/008_cavi_test.yml)
101+
<!-- [![Aperture](https://github.com/JPTS2/sad2xs/actions/workflows/009_apert_test.yml/badge.svg?branch=main)](https://github.com/JPTS2/sad2xs/actions/workflows/009_apert_test.yml) -->
102+
[![Coordinate Transform](https://github.com/JPTS2/sad2xs/actions/workflows/010_coord_test.yml/badge.svg?branch=main)](https://github.com/JPTS2/sad2xs/actions/workflows/010_coord_test.yml)
103+
<!-- [![Marker](https://github.com/JPTS2/sad2xs/actions/workflows/011_mark_test.yml/badge.svg?branch=main)](https://github.com/JPTS2/sad2xs/actions/workflows/011_mark_test.yml) -->
104+
[![Reversal](https://github.com/JPTS2/sad2xs/actions/workflows/012_reversal_test.yml/badge.svg?branch=main)](https://github.com/JPTS2/sad2xs/actions/workflows/012_reversal_test.yml)

lattice_tests/003_fccee_sol.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
LINE["F1", "ESCR*"] = 0;""",
3838
output_filename = REBUILT_SAD_LATTICE_PATH)
3939

40-
print("Lattice Rebuilt")
41-
4240
tw_sad = twiss_sad(
4341
lattice_filename = REBUILT_SAD_LATTICE_PATH,
4442
line_name = LINE_NAME,
@@ -48,8 +46,6 @@
4846
reverse_bend_direction = False,
4947
additional_commands = "")
5048

51-
print("Twiss acquired")
52-
5349
################################################################################
5450
# Convert Lattice
5551
################################################################################

lattice_tests/_sad_helpers.py

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
################################################################################
88
import os
99
import subprocess
10+
import ast
1011
import numpy as np
1112
import tfs
1213
import xtrack as xt
@@ -203,6 +204,7 @@ def twiss_sad(
203204
radiation: bool = False,
204205
rad_compensation: bool = False,
205206
rad_taper: bool = False,
207+
delta0: float = 0.0,
206208
additional_commands: str = ''):
207209
"""
208210
Generate a SAD command to compute the twiss parameters of a lattice.
@@ -257,7 +259,7 @@ def twiss_sad(
257259
USE {line_name};
258260
259261
{additional_commands};
260-
262+
DP0 = {delta0};
261263
{RF_FLAG}
262264
{RAD_FLAG}
263265
{RADCOMP_FLAG}
@@ -418,6 +420,89 @@ def twiss_sad(
418420
########################################
419421
return tw_sad
420422

423+
################################################################################
424+
# Calculate Transfer Matrix
425+
################################################################################
426+
def sad_transfer_matrix(
427+
lattice_filename: str,
428+
line_name: str,
429+
start_element: str | None = None,
430+
end_element: str | None = None):
431+
432+
########################################
433+
# Ensure both or neither of start_element and end_element are provided
434+
########################################
435+
if start_element is not None and end_element is None:
436+
raise ValueError("If start_element is provided, end_element must also be provided")
437+
if start_element is None and end_element is not None:
438+
raise ValueError("If end_element is provided, start_element must also be provided")
439+
440+
########################################
441+
# Generate the Transfer Matrix command
442+
########################################
443+
if start_element is not None and end_element is not None:
444+
SAD_COMMAND = f"""
445+
FFS;
446+
447+
GetMAIN["./{lattice_filename}"];
448+
USE {line_name};
449+
450+
CALC4D;
451+
CALC;
452+
TM = TransferMatrix["{start_element}", "{end_element}"];
453+
Print[TM];
454+
abort;
455+
"""
456+
else:
457+
SAD_COMMAND = f"""
458+
FFS;
459+
460+
GetMAIN["./{lattice_filename}"];
461+
USE {line_name};
462+
463+
CALC4D;
464+
CALC;
465+
TM = TransferMatrix[1, -1];
466+
Print[TM];
467+
abort;
468+
"""
469+
470+
########################################
471+
# Write and execute the SAD command
472+
########################################
473+
with open("temporary_sad_transfer_matrix.sad", "w") as f:
474+
f.write(SAD_COMMAND)
475+
476+
try:
477+
result = subprocess.run(
478+
["sad", "temporary_sad_transfer_matrix.sad"],
479+
capture_output = True,
480+
text = True,
481+
timeout = 30)
482+
os.remove("temporary_sad_transfer_matrix.sad")
483+
except subprocess.TimeoutExpired:
484+
print("Timed out at 30s")
485+
486+
os.remove("temporary_sad_transfer_matrix.sad")
487+
raise subprocess.TimeoutExpired(cmd = ["sad", "temporary_sad_transfer_matrix.sad"], timeout = 30)
488+
489+
# Take the raw text output
490+
raw = result.stdout
491+
492+
# Isolate the matrix
493+
start = raw.find("{{")
494+
end = raw.rfind("}}")
495+
if start == -1 or end == -1:
496+
raise ValueError("Matrix not found in subprocess output")
497+
matrix_str = raw[start:end + 2]
498+
499+
# Convert to numpy array
500+
cleaned = matrix_str.replace("}", "]").replace("{", "[")
501+
matrix_list = ast.literal_eval(cleaned)
502+
R_matrix = np.array(matrix_list, dtype = float)
503+
504+
return R_matrix
505+
421506
################################################################################
422507
# Rebuild SAD lattice (post GEO for solenoid)
423508
################################################################################

sad2xs/output_writer/_014_offset_markers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ def create_offset_marker_lattice_file_information(offset_marker_locations):
9292
except AssertionError as err:
9393
print("Couldn't insert all the markers. Usually this is because of negative drifts")
9494
print(err)
95-
pass"""
95+
pass
96+
97+
########################################
98+
# Replace repeated elements (again, to account for new markers)
99+
########################################
100+
line.replace_all_repeated_elements()
101+
"""
96102

97103
########################################
98104
# Return

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
=============================================
44
Author(s): John P T Salvesen
55
6-
Date: 01-10-2025
6+
Date: 07-10-2025
77
"""
88

99
################################################################################
@@ -22,8 +22,8 @@
2222
################################################################################
2323
setup(
2424
name = "sad2xs",
25-
version = "0.1.1",
26-
date = "01-10-2025",
25+
version = "0.1.2",
26+
date = "07-10-2025",
2727
description = "Conversion of SAD lattices to Xtrack format",
2828
long_description = description,
2929
long_description_content_type = "text/markdown",

0 commit comments

Comments
 (0)