Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Example and Test for Homotopy Solver #528

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions examples/match/000_match_basic_homotopy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import xtrack as xt

# Load a line and build a tracker
line = xt.Line.from_json('../../test_data/hllhc15_thick/lhc_thick_with_knobs.json')
line.build_tracker()

# Match tunes and chromaticities to assigned values
opt = line.match(
solve=False,
method='4d', # <- passed to twiss
vary=[
xt.VaryList(['kqtf.b1', 'kqtd.b1'], step=1e-8, tag='quad'),
xt.VaryList(['ksf.b1', 'ksd.b1'], step=1e-4, limits=[-0.1, 0.1], tag='sext'),
],
targets = [
xt.TargetSet(qx=62.315, qy=60.325, tol=1e-6, tag='tune'),
xt.TargetSet(dqx=10.0, dqy=12.0, tol=0.01, tag='chrom'),
])

opt.solve_homotopy()

# Inspect optimization log
opt.log()

# prints:
# iteration penalty alpha tag tol_met target_active hit_limits vary_active vary_0 ...
# 0 12.8203 -1 nnnn yyyy nnnn yyyy 0
# 1 0.00207829 0 yyyy yyyy nnnn yyyy 4.27447e-06
# 2 0.00207829 -1 Homotopy it 0 yyyy yyyy nnnn yyyy 4.27447e-06
# 3 0.0033512 0 yyyy yyyy nnnn yyyy 8.54459e-06
# 4 0.0033512 -1 Homotopy it 1 yyyy yyyy nnnn yyyy 8.54459e-06
# 5 0.00676554 0 yyyy yyyy nnnn yyyy 1.28198e-05
# 6 0.00676554 -1 Homotopy it 2 yyyy yyyy nnnn yyyy 1.28198e-05
# 7 0.000607601 0 yyyy yyyy nnnn yyyy 1.70899e-05
# 8 0.000607601 -1 Homotopy it 3 yyyy yyyy nnnn yyyy 1.70899e-05
# 9 0.00254206 0 yyyy yyyy nnnn yyyy 2.13608e-05
# 10 0.00254206 -1 Homotopy it 4 yyyy yyyy nnnn yyyy 2.13608e-05
# 11 0.00127848 0 yyyy yyyy nnnn yyyy 2.56333e-05
# 12 0.00127848 -1 Homotopy it 5 yyyy yyyy nnnn yyyy 2.56333e-05
# 13 0.000285469 0 yyyy yyyy nnnn yyyy 2.99055e-05
# 14 0.000285469 -1 Homotopy it 6 yyyy yyyy nnnn yyyy 2.99055e-05
# 15 0.00366699 0 yyyy yyyy nnnn yyyy 3.41767e-05
# 16 0.00366699 -1 Homotopy it 7 yyyy yyyy nnnn yyyy 3.41767e-05
# 17 0.00158214 0 yyyy yyyy nnnn yyyy 3.84465e-05
# 18 0.00158214 -1 Homotopy it 8 yyyy yyyy nnnn yyyy 3.84465e-05
# 19 0.00353517 0 yyyy yyyy nnnn yyyy 4.2715e-05
# 20 0.00353517 -1 Homotopy it 9 yyyy yyyy nnnn yyyy 4.2715e-05

# Inspect optimization outcome
opt.target_status()
opt.vary_status()
# prints:
#
# Target status:
# id state tag tol_met residue current_val target_val description
# 0 ON tune True 8.53717e-11 62.315 62.315 'qx', val=62.315, tol=1e-06, weight=10)
# 1 ON tune True 1.49214e-13 60.325 60.325 'qy', val=60.325, tol=1e-06, weight=10)
# 2 ON chrom True 1.22005e-06 10 10 'dqx', val=10, tol=0.01, weight=1)
# 3 ON chrom True -1.87538e-09 12 12 'dqy', val=12, tol=0.01, weight=1)
# Vary status:
# id state tag name lower_limit current_val upper_limit val_at_iter_0 step weight
# 0 ON quad kqtf.b1 None 4.27163e-05 None 0 1e-08 1
# 1 ON quad kqtd.b1 None -4.27199e-05 None 0 1e-08 1
# 2 ON sext ksf.b1 -0.1 0.0118965 0.1 0 0.0001 1
# 3 ON sext ksd.b1 -0.1 -0.0232137 0.1 0 0.0001 1

# Get knob values after optimization
knobs_after_match = opt.get_knob_values()
# contains: {'kqtf.b1': 4.27163e-05, 'kqtd.b1': -4.27199e-05,
# 'ksf.b1': 0.0118965, 'ksd.b1': -0.0232137}

# Get knob values before optimization
knobs_before_match = opt.get_knob_values(iteration=0)
# contains: {'kqtf.b1': 0, 'kqtd.b1': 0, 'ksf.b1': 0, 'ksd.b1': 0}

60 changes: 60 additions & 0 deletions tests/test_match_tune_chroma_cminus.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,66 @@ def test_match_tune_chromaticity(test_context):
xo.assert_allclose(tw_final['dqx'], 6.0, atol=0.05)
xo.assert_allclose(tw_final['dqy'], 4.0, atol=0.05)

@for_all_test_contexts
def test_match_tune_chromaticity_homotopy(test_context):

with open(path_line) as f:
dct = json.load(f)

line = xt.Line.from_dict(dct['line'])
line.particle_ref = xp.Particles.from_dict(dct['particle'])

line.build_tracker(_context=test_context)

print('\nInitial twiss parameters')
tw_before = line.twiss()
print(f"Qx = {tw_before['qx']:.5f} Qy = {tw_before['qy']:.5f} "
f"Q'x = {tw_before['dqx']:.5f} Q'y = {tw_before['dqy']:.5f}")

print(f"kqtf.b1 = {line.vars['kqtf.b1']._value}")
print(f"kqtd.b1 = {line.vars['kqtd.b1']._value}")
print(f"ksf.b1 = {line.vars['ksf.b1']._value}")
print(f"ksd.b1 = {line.vars['ksd.b1']._value}")

t1 = time.time()
opt = line.match(
solve=False,
vary=[
xt.Vary('kqtf.b1', step=1e-8),
xt.Vary('kqtd.b1', step=1e-8),
xt.Vary('ksf.b1', step=1e-8),
xt.Vary('ksd.b1', step=1e-8),
],
targets = [
xt.Target('qx', 62.315, tol=1e-4),
xt.Target(lambda tw: tw['qx'] - tw['qy'], 1.99, tol=1e-4),
xt.Target('dqx', 10.0, tol=0.05),
xt.Target('dqy', 12.0, tol=0.05)])
opt.solve_homotopy()
t2 = time.time()
print('\nTime fsolve: ', t2-t1)

tw_final = line.twiss()
print('\nFinal twiss parameters')
print(f"Qx = {tw_final['qx']:.5f} Qy = {tw_final['qy']:.5f} "
f"Q'x = {tw_final['dqx']:.5f} Q'y = {tw_final['dqy']:.5f}")
print(f"kqtf.b1 = {line.vars['kqtf.b1']._value}")
print(f"kqtd.b1 = {line.vars['kqtd.b1']._value}")
print(f"ksf.b1 = {line.vars['ksf.b1']._value}")
print(f"ksd.b1 = {line.vars['ksd.b1']._value}")

xo.assert_allclose(tw_final['qx'], 62.315, atol=1e-7)
xo.assert_allclose(tw_final['qy'], 60.325, atol=1e-7)
xo.assert_allclose(tw_final['dqx'], 10.0, atol=0.05)
xo.assert_allclose(tw_final['dqy'], 12.0, atol=0.05)

# Assure reasonable number of steps in log
assert len(opt._log['tag']) < 25

# Look that Homotopy entries exist in the log
for i in range(10):
assert f'Homotopy it {i}' in opt._log['tag']


@for_all_test_contexts
def test_match_coupling(test_context):
Expand Down