예제 #1
0
def test_custom_char_map_import():
    """Test importing a custom characteristc map."""

    # we need to write some data to the path first, using defaults
    data_path = resource_filename('tespy.data', 'char_maps.json')
    path = extend_basic_path('data')
    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['compressor']['char_map']
    with open(os.path.join(path, 'char_maps.json'), 'w') as outfile:
        json.dump(data, outfile)

    char_original = load_default_char('compressor', 'char_map', 'DEFAULT',
                                      compressor_map)
    char_custom = load_custom_char('DEFAULT', compressor_map)

    x_cond = np.array_equal(char_original.x, char_custom.x)
    y_cond = np.array_equal(char_original.y, char_custom.y)
    z1_cond = np.array_equal(char_original.z1, char_custom.z1)
    z2_cond = np.array_equal(char_original.z2, char_custom.z2)

    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)

    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) + ' '
           'as these have been duplicated before load.')
    assert x_cond is True, msg

    msg = ('The y values from the custom characteristic line ' +
           str(char_custom.y) + ' must be identical to the y values from '
           'the default characteristic line ' + str(char_original.y) + ' '
           'as these have been duplicated before load.')
    assert y_cond is True, msg

    msg = ('The z1 values from the custom characteristic line ' +
           str(char_custom.z1) + ' must be identical to the z1 values from '
           'the default characteristic line ' + str(char_original.z1) + ' '
           'as these have been duplicated before load.')
    assert z1_cond is True, msg

    msg = ('The z2 values from the custom characteristic line ' +
           str(char_custom.z2) + ' must be identical to the z2 values from '
           'the default characteristic line ' + str(char_original.z2) + ' '
           'as these have been duplicated before load.')
    assert z2_cond is True, msg
예제 #2
0
def test_custom_CharLine_import():
    """Test importing a custom characteristc lines."""

    # we need to write some data to the path first, using defaults
    data_path = resource_filename('tespy.data', 'char_lines.json')
    path = extend_basic_path('data')
    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', CharLine)
    char_custom = load_custom_char('EVAPORATING FLUID', CharLine)

    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) + ' '
           'as these have been duplicated before load.')
    assert x_cond, msg

    msg = ('The y values from the custom characteristic line ' +
           str(char_custom.y) + ' must be identical to the y values from '
           'the default characteristic line ' + str(char_original.y) + ' '
           'as these have been duplicated before load.')
    assert y_cond, msg
예제 #3
0
def test_missing_char_map_files():
    """Test missing files."""
    path = extend_basic_path('data')
    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)

        shutil.rmtree(path, ignore_errors=True)

    with raises(FileNotFoundError):
        load_custom_char('some other stuff', char_map)

    if os.path.exists(tmp_path):
        for f in os.listdir(tmp_path):
            shutil.copy(src=tmp_path + '/' + f, dst=path)

        shutil.rmtree(tmp_path, ignore_errors=True)
예제 #4
0
def load_custom_char(name, char_type):
    r"""
    Load a characteristic line of map.

    Parameters
    ----------
    name : str
        Name of the characteristics.

    char_type : class
        Class to be generate the object of.

    Returns
    -------
    obj : object
        The characteristics (char_line, char_map, compressor_map) object.
    """
    path = extend_basic_path('data')

    if char_type == char_line:
        path = os.path.join(path, 'char_lines.json')
    else:
        path = os.path.join(path, 'char_maps.json')

    if os.path.isfile(path):

        with open(path) as f:
            data = json.loads(f.read())

        if char_type == char_line:
            x = data[name]['x']
            y = data[name]['y']
            obj = char_type(x, y)

        else:
            x = data[name]['x']
            y = data[name]['y']
            z1 = data[name]['z1']
            z2 = data[name]['z2']
            obj = char_type(x, y, z1, z2)

        return obj

    else:
        msg = ('The file containing your custom charactersitics could not be '
               'found on your system. The path should be ' + path + '. Please '
               'make sure the .tespy/data path exists in your home directory.')
        logging.error(msg)
        raise FileNotFoundError(msg)
예제 #5
0
 def setup(self):
     # create data path and write json files into path
     self.path = extend_basic_path('data')