Skip to content

Commit

Permalink
Merge pull request #286 from fnalacceleratormodeling/egstern/285-miss…
Browse files Browse the repository at this point in the history
…ing-python-binding-for-lattice-copy-constructor

added binding and test
  • Loading branch information
egstern authored Jun 17, 2024
2 parents 73e2f5d + c6fbc4b commit ed2a450
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/synergia/lattice/lattice_pywrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ PYBIND11_MODULE(lattice, m)
"Construct an empty latttice.",
"name"_a )

.def( py::init<Lattice const&>(),
"Construct a copy of a lattice." )

.def( py::init<std::string const&, Reference_particle const&>(),
"Construct an empty latttice with given reference particle.",
"name"_a,
Expand Down
39 changes: 39 additions & 0 deletions src/synergia/lattice/tests/test_lattice.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
import pytest
import synergia
from synergia.lattice import Lattice, Lattice_element
from synergia.foundation import Reference_particle

Expand Down Expand Up @@ -44,3 +45,41 @@ def test_set_reference_particle_energy():
lattice.set_reference_particle(reference_particle)
lattice.get_reference_particle().set_total_energy(total_energy + 1)
assert lattice.get_reference_particle().get_total_energy() == total_energy + 1.0

def test_copy_constructor():
lattice_txt = """
q1: quadrupole, l=0.5, k1=0.2;
b1: sbend, angle=0.05, l=2.0;
machine: sequence, refer=entry, l=6.0;
q1, at=1.0;
b1, at=2.5;
endsequence;
beam, particle=proton, energy=1.5;
"""

reader = synergia.lattice.MadX_reader()
reader.parse(lattice_txt)
lattice1 = reader.get_lattice('machine')
assert lattice1.get_length() == 6.0

lattice2 = lattice1
assert lattice2.get_length() == 6.0

# Are lattice1 and lattice2 the same object?
assert id(lattice1) == id(lattice2)
# Change to an element in lattice2 is reflected in lattice1
elem = lattice2.get_elements()[1]
elem.set_double_attribute('xyzzy', 3.0)
assert lattice1.get_elements()[1].get_double_attribute('xyzzy') == 3.0

# lattice3 should be a seperate object with copies of the elements
lattice3 = synergia.lattice.Lattice(lattice1)
assert lattice3.get_length() == 6.0

assert id(lattice1) != id(lattice3)

# change parameters in an element of lattice3 to check that it doesn't
# affect lattice1
elem = lattice3.get_elements()[0]
elem.set_double_attribute('foo', 4)
assert not lattice1.get_elements()[0].has_double_attribute('foo')

0 comments on commit ed2a450

Please sign in to comment.