Skip to content
Merged
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
12 changes: 9 additions & 3 deletions pytac/load_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* uc_pchip_data.csv
"""
import collections
import copy
import csv
import os

Expand Down Expand Up @@ -76,7 +77,7 @@ def load_pchip_unitconv(filename):
for uc_id in data:
eng = [x[0] for x in sorted(data[uc_id])]
phy = [x[1] for x in sorted(data[uc_id])]
u = units.PchipUnitConv(eng, phy, uc_id)
u = units.PchipUnitConv(eng, phy, name=uc_id)
unitconvs[uc_id] = u
return unitconvs

Expand All @@ -100,9 +101,12 @@ def load_unitconv(directory, mode, lattice):
with open(os.path.join(directory, mode, UNITCONV_FILENAME)) as unitconv:
csv_reader = csv.DictReader(unitconv)
for item in csv_reader:
# Special case for element 0: the lattice itself.
if int(item['el_id']) == 0:
if item['uc_type'] != 'null':
uc = unitconvs[int(item['uc_id'])]
# Each element needs its own unitconv object as
# it may for example have different limit.
uc = copy.copy(unitconvs[int(item['uc_id'])])
uc.phys_units = item['phys_units']
uc.eng_units = item['eng_units']
if item['lower_lim'] != '':
Expand All @@ -120,7 +124,9 @@ def load_unitconv(directory, mode, lattice):
uc = units.NullUnitConv(item['eng_units'],
item['phys_units'])
else:
uc = unitconvs[int(item['uc_id'])]
# Each element needs its own unitconv object as
# it may for example have different limit.
uc = copy.copy(unitconvs[int(item['uc_id'])])
if element.families.intersection(('HSTR', 'VSTR', 'QUAD',
'SEXT', 'BEND')):
energy = lattice.get_value('energy', units=pytac.PHYS)
Expand Down
27 changes: 13 additions & 14 deletions pytac/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,22 +178,24 @@ def phys_to_eng(self, value):
adjusted_value = self._pre_phys_to_eng(value)
results = self._raw_phys_to_eng(adjusted_value)

valid_results = results[:]

if self.lower_limit is not None:
results = [r for r in results if r >= self.lower_limit]
valid_results = [r for r in valid_results if r >= self.lower_limit]
if self.upper_limit is not None:
results = [r for r in results if r <= self.upper_limit]
if len(results) == 1:
return results[0]
elif len(results) == 0:
raise UnitsException("{0}: no conversion result "
"within conversion limits ({1}, "
"{2}).".format(self,
valid_results = [r for r in valid_results if r <= self.upper_limit]
if len(valid_results) == 1:
return valid_results[0]
elif len(valid_results) == 0:
raise UnitsException("{0}: none of conversion results {1} "
"within conversion limits ({2}, "
"{3}).".format(self, results,
self.lower_limit,
self.upper_limit))
else:
raise UnitsException("{0}: There are multiple "
"corresponding engineering values ({1})."
.format(self, results))
.format(self, valid_results))

def convert(self, value, origin, target):
"""Convert between two different unit types and chek the validity of
Expand Down Expand Up @@ -412,7 +414,6 @@ class NullUnitConv(UnitConv):
**Attributes:**

Attributes:
name (str): An identifier for the unit conversion object.
eng_units (str): The unit type of the post conversion engineering
value.
phys_units (str): The unit type of the post conversion physics value.
Expand All @@ -423,18 +424,16 @@ class NullUnitConv(UnitConv):
_pre_phys_to_eng (function): Always unit_function as no conversion
is performed.
"""
def __init__(self, engineering_units='', physics_units='', name=None):
def __init__(self, engineering_units='', physics_units=''):
"""
Args:
engineering_units (str): The unit type of the post conversion
engineering value.
physics_units (str): The unit type of the post conversion physics
value.
name (str): An identifier for the unit conversion object.
"""
super(self.__class__, self).__init__(unit_function, unit_function,
engineering_units, physics_units,
name=None)
engineering_units, physics_units)

def _raw_eng_to_phys(self, eng_value):
"""Doesn't convert between engineering and physics units.
Expand Down
16 changes: 16 additions & 0 deletions test/test_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,19 @@ def test_quad_unitconv_known_failing_test():
uc._pre_phys_to_eng = pytac.utils.get_mult_rigidity(LAT_ENERGY)
numpy.testing.assert_allclose(uc.eng_to_phys(70), -0.69133465)
numpy.testing.assert_allclose(uc.phys_to_eng(-0.7), 70.8834284954)


@pytest.mark.parametrize('quad_index,phys_value', [
[747, -1.9457],
[1135, -1.9864]])
def test_quad_unitconv_with_different_limits(diad_ring, quad_index, phys_value):
"""Test elements with unit conversions that have different limits.

The limits on these quads are different to the rest of the family.
They caused problems until we implemented different limits per UnitConv
object.
"""
problem_quad = diad_ring[quad_index - 1]
uc = problem_quad.get_unitconv('b1')
# This is just outside the power supply limits 0 to 200A.
uc.phys_to_eng(phys_value)