Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_char_line_extrapolation():
"""Test the characteristc line with extrapolation."""
# create a characteristc line with values of y=(x-2)^2
line = char_line(x=[0, 1, 2, 3, 4], y=[4, 1, 0, 1, 4], extrapolate=True)
# test evaluation at x=-1 to check lower limits, result: y=7
x = -1
y = line.evaluate(x)
msg = ("The evaluation of x=" + str(x) + " must be 7, but is " +
str(y) + ".")
assert y == 7.0, msg
# test evaluation at x=5 to check upper limits, result: y=7
x = 5
y = line.evaluate(x)
msg = ("The evaluation of x=" + str(x) + " must be 7, but is " +
str(y) + ".")
assert y == 7.0, msg
def test_char_number_of_points():
with raises(ValueError):
char_line(x=[0, 1, 2], y=[1, 2, 3, 4])
def test_missing_char_line_files():
"""
This test might not work locally.
If you are using custom char_lines in your HOME/.tespy/data folder, this
test will throw a KeyError instead of a FileNotFoundError.
"""
with pytest.raises(FileNotFoundError):
load_custom_char('stuff', char_line)
y = np.array(
[0.01, 0.3148, 0.5346, 0.6843, 0.7835, 0.8477, 0.8885, 0.9145,
0.9318, 0.9443, 0.9546, 0.9638, 0.9724, 0.9806, 0.9878, 0.9938,
0.9982, 0.999, 0.9995, 0.9999, 1, 0.9977, 0.9947, 0.9909, 0.9853,
0.9644]) * 0.975
self.motor_bus_based = char_line(x=x, y=y)
self.motor_comp_based = char_line(x=x, y=1 / y)
# generator efficiency
x = np.array(
[0.100, 0.345, 0.359, 0.383, 0.410, 0.432, 0.451, 0.504, 0.541,
0.600, 0.684, 0.805, 1.000, 1.700, 10])
y = np.array(
[0.976, 0.989, 0.990, 0.991, 0.992, 0.993, 0.994, 0.995, 0.996,
0.997, 0.998, 0.999, 1.000, 0.999, 0.99]) * 0.975
self.generator = char_line(x=x, y=y)
power_bus_total = bus('total power output')
power_bus_total.add_comps(
{'comp': cp, 'char': self.motor_bus_based, 'base': 'bus'},
{'comp': gt, 'char': self.generator})
thermal_input = bus('thermal input')
thermal_input.add_comps({'comp': cc})
compressor_power_comp = bus('compressor power input')
compressor_power_comp.add_comps(
{'comp': cp, 'char': self.motor_comp_based})
compressor_power_bus = bus('compressor power input bus based')
compressor_power_bus.add_comps(
{'comp': cp, 'char': self.motor_bus_based, 'base': 'bus'})
def test_char_line_evaluation():
"""Test the characteristc line evaluation."""
# create a characteristc line with values of y=(x-2)^2
line = char_line(x=[0, 1, 2, 3, 4], y=[4, 1, 0, 1, 4])
# test evaluation at given x value (x=3, y=1)
x = 3
y = line.evaluate(x)
msg = ("The evaluation of x=" + str(x) + " must be 1.0, but is " +
str(y) + ".")
assert y == 1.0, msg
# test evaluation at x=0.5 to force interpolation, result: y=2.5
x = 0.5
y = line.evaluate(x)
msg = ("The evaluation of x=" + str(x) + " must be 2.5, but is " +
str(y) + ".")
assert y == 2.5, msg
# test evaluation at x=-1 to check lower limits, result: y=4
y = np.array(
[0.000, 0.164, 0.283, 0.389, 0.488, 0.581, 0.670, 0.756, 0.840,
0.921, 1.000, 1.078, 1.154, 1.228, 1.302, 1.374, 1.446, 1.516,
1.585, 1.654, 1.722, 1.789, 1.855, 1.921, 1.986, 2.051])
kA_char1 = dc_cc(func=char_line(x, y), param='m')
x = np.array(
[0.0100, 0.0400, 0.0700, 0.1100, 0.1500, 0.2000, 0.2500, 0.3000,
0.3500, 0.4000, 0.4500, 0.5000, 0.5500, 0.6000, 0.6500, 0.7000,
0.7500, 0.8000, 0.8500, 0.9000, 0.9500, 1.0000, 1.5000, 2.0000])
y = np.array(
[0.0185, 0.0751, 0.1336, 0.2147, 0.2997, 0.4118, 0.5310, 0.6582,
0.7942, 0.9400, 0.9883, 0.9913, 0.9936, 0.9953, 0.9966, 0.9975,
0.9983, 0.9988, 0.9992, 0.9996, 0.9998, 1.0000, 1.0008, 1.0014])
kA_char2 = dc_cc(func=char_line(x, y), param='m')
ev.set_attr(
pr1=1, pr2=.999, ttd_l=5, design=['ttd_l'], offdesign=['kA_char'],
kA_char1=kA_char1, kA_char2=kA_char2)
# no kA modification for hot side!
x = np.array([0, 1])
y = np.array([1, 1])
kA_char1 = dc_cc(func=char_line(x, y), param='m')
# characteristic line for superheater kA
x = np.array(
[0, 0.045, 0.136, 0.244, 0.43, 0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2])
y = np.array(
[0, 0.037, 0.112, 0.207, 0.5, 0.8, 0.85, 0.9, 0.95, 1, 1.04, 1.07])
kA_char2 = dc_cc(func=char_line(x, y), param='m')
su.set_attr(kA_char1=kA_char1, kA_char2=kA_char2,
tmp_path = extend_basic_path('tmp_dir_for_testing')
if os.path.exists(path):
for f in os.listdir(path):
shutil.copy(src=path + '/' + f, dst=tmp_path)
with open(data_path) as f:
raw_data = json.loads(f.read())
data = raw_data['heat exchanger']['kA_char2']
with open(os.path.join(path, 'char_lines.json'), 'w') as outfile:
json.dump(data, outfile)
char_original = load_default_char('heat exchanger', 'kA_char2',
'EVAPORATING FLUID', char_line)
char_custom = load_custom_char('EVAPORATING FLUID', char_line)
shutil.rmtree(path, ignore_errors=True)
if os.path.exists(tmp_path):
path = extend_basic_path('data')
for f in os.listdir(tmp_path):
shutil.copy(src=tmp_path + '/' + f, dst=path)
shutil.rmtree(tmp_path, ignore_errors=True)
x_cond = np.array_equal(char_original.x, char_custom.x)
y_cond = np.array_equal(char_original.y, char_custom.y)
msg = ('The x values from the custom characteristic line ' +
str(char_custom.x) + ' must be identical to the x values from '
'the default characteristic line ' + str(char_original.x) + ' '
nw.add_conns(cp1_he, he_cp2, ic_in_he, he_ic_out, cp2_close)
nw.add_conns(su_cp1)
# %% component parametrization
# condenser system
cd.set_attr(pr1=0.99, pr2=0.99, ttd_u=5, design=['pr2', 'ttd_u'],
offdesign=['zeta2', 'kA'])
rp.set_attr(eta_s=0.8, design=['eta_s'], offdesign=['eta_s_char'])
cons.set_attr(pr=0.99, design=['pr'], offdesign=['zeta'])
# evaporator system
kA_char1 = ldc('heat exchanger', 'kA_char1', 'DEFAULT', char_line)
kA_char2 = ldc('heat exchanger', 'kA_char2', 'EVAPORATING FLUID', char_line)
ev.set_attr(pr1=0.99, pr2=0.99, ttd_l=5,
kA_char1=kA_char1, kA_char2=kA_char2,
design=['pr1', 'ttd_l'], offdesign=['zeta1', 'kA'])
su.set_attr(pr1=0.99, pr2=0.99, ttd_u=2, design=['pr1', 'pr2', 'ttd_u'],
offdesign=['zeta1', 'zeta2', 'kA'])
pu.set_attr(eta_s=0.8, design=['eta_s'], offdesign=['eta_s_char'])
# compressor system
cp1.set_attr(eta_s=0.8, design=['eta_s'], offdesign=['eta_s_char'])
cp2.set_attr(eta_s=0.8, pr=5, design=['eta_s'], offdesign=['eta_s_char'])
he.set_attr(pr1=0.98, pr2=0.98, design=['pr1', 'pr2'],
offdesign=['zeta1', 'zeta2', 'kA'])
msg = (
'The dictionary keyword "p" will be removed '
'TESPy version 0.4.0. Please use "param" '
'instead.')
warnings.warn(msg, FutureWarning, stacklevel=2)
if isinstance(v, str) or v is None:
self.comps.loc[comp, 'param'] = v
else:
msg = (
'The bus parameter selection must be a '
'string (at bus ' + self.label + ').')
logging.error(msg)
raise TypeError(msg)
elif k == 'char':
if isinstance(v, char_line):
self.comps.loc[comp, 'char'] = v
elif (isinstance(v, float) or
isinstance(v, np.float64) or
isinstance(v, np.int64) or
isinstance(v, int)):
x = np.array([0, 3])
y = np.array([1, 1]) * v
self.comps.loc[comp, 'char'] = (
char_line(x=x, y=y))
else:
msg = (
'Char must be a number or a TESPy '
'characteristics char line.')
logging.error(msg)
raise TypeError(msg)