예제 #1
0
def construct_comps(c, *args):
    """
    creates TESPy component from class name provided in the .csv-file and
    specifies its parameter

    :param c: component information
    :type c: pandas.core.series.Series
    :returns: instance (*tespy.components.component*) - TESPy component object

    **additional arguments in args**

    - args[0]: char (*pandas.core.frame.DataFrame*) - DataFrame containing the
      x and y data of characteristic functions
    """

    target_class = getattr(cmp, c.cp)
    instance = target_class(c.label)
    kwargs = {}

    # basic properties
    for key in ['mode', 'design', 'offdesign']:
        kwargs[key] = c[key]

    for key, value in instance.attr().items():
        if key in c:
            # component parameters
            if isinstance(value, hlp.dc_cp):
                dc = hlp.dc_cp(val=c[key],
                               is_set=c[key + '_set'],
                               is_var=c[key + '_var'])
                kwargs[key] = dc
            # component characteristics
            elif isinstance(value, hlp.dc_cc):
                # finding x and y values of the characteristic function
                values = args[0]['id'] == c[key]
                x = args[0][values]['x'].values[0]
                y = args[0][values]['y'].values[0]
                dc = hlp.dc_cc(is_set=c[key + '_set'],
                               method=c[key + '_method'],
                               param=c[key + '_param'],
                               x=x,
                               y=y)
                kwargs[key] = dc
            else:
                continue

    instance.set_attr(**kwargs)
    return instance
예제 #2
0
    def test_set_attr_errors(self):
        #
        labels = [5, 'Label,', 'Labe;l', 'Label.']
        for l in labels:
            self.cmp_instanciation_ValueError(l)

        # ValueErrors
        self.set_attr_ValueError(self.comp, offdesign=['Q'])

        self.set_attr_ValueError(self.conn, offdesign=['f'])
        self.set_attr_ValueError(self.conn, state='f')

        self.set_attr_ValueError(self.nw, m_unit='kg')
        self.set_attr_ValueError(self.nw, h_unit='kg')
        self.set_attr_ValueError(self.nw, p_unit='kg')
        self.set_attr_ValueError(self.nw, T_unit='kg')
        self.set_attr_ValueError(self.nw, v_unit='kg')

        self.create_connection_ValueError('source')
        self.create_connection_ValueError('target')

        # TypeErrors
        self.set_attr_TypeError(self.comp, P=[5])
        self.set_attr_TypeError(self.comp, tiP_char=None)
        self.set_attr_TypeError(self.comp, design='f')
        self.set_attr_TypeError(self.comp, fuel=hlp.dc_cp(val='CH4'))
        self.set_attr_TypeError(self.comp, design_path=7)

        self.set_attr_TypeError(self.conn, design='h')
        self.set_attr_TypeError(self.conn, fluid_balance=1)
        self.set_attr_TypeError(self.conn, h0=[4])
        self.set_attr_TypeError(self.conn, fluid=5)
        self.set_attr_TypeError(self.conn, state=5)
        self.set_attr_TypeError(self.conn, design_path=5)

        self.set_attr_TypeError(self.nw, m_range=5)
        self.set_attr_TypeError(self.nw, p_range=5)
        self.set_attr_TypeError(self.nw, h_range=5)
        self.set_attr_TypeError(self.nw, T_range=5)

        self.bus_add_comps_TypeError({'c': self.conn})
        self.bus_add_comps_TypeError({'f': self.comp})
        self.bus_add_comps_TypeError({'c': self.comp, 'char': 'Hi'})
        self.bus_add_comps_TypeError({'c': self.comp, 'p': 5})
        self.bus_add_comps_TypeError({'c': self.comp, 'P_ref': 'what'})

        self.create_ref_TypeError([self.conn, 7, 'hi'])
        self.create_ref_TypeError([self.conn, 'hi', 0])
        self.create_ref_TypeError([self.comp, 1, 0])

        # KeyErrors
        self.set_attr_KeyError(self.comp, wow=5)
        self.set_attr_KeyError(self.conn, jey=5)
        self.set_attr_KeyError(self.sub, a=7)

        self.get_attr_KeyError(self.comp, 'wow')
        self.get_attr_KeyError(self.conn, 'key')
        self.get_attr_KeyError(self.bus, 'components')
        self.get_attr_KeyError(con.ref(self.conn, 1, 0), 'comp')
        self.get_attr_KeyError(self.nw, 'test')
        self.get_attr_KeyError(self.sub, 'test')
        self.get_attr_KeyError(cmp_char.characteristics(), 'test')
        self.get_attr_KeyError(hlp.data_container(), 'somekey')
예제 #3
0
def construct_comps(c, *args):
    r"""
    Creates TESPy component from class name provided in the .csv-file and specifies its parameters.

    Parameters
    ----------
    c : pandas.core.series.Series
        Component information from .csv-file.

    args[0] : pandas.core.frame.DataFrame
        DataFrame containing the x and y data of characteristic functions.

    args[1] : pandas.core.frame.DataFrame
        DataFrame containing the x, y, z1 and z2 data of characteristic maps.

    Returns
    -------
    instance : tespy.components.components.component
        TESPy component object.
    """
    if c.interface:
        instance = cmp.subsys_interface(c.label, num_inter=1)
    else:
        target_class = getattr(cmp, c.cp)
        instance = target_class(c.label)
    kwargs = {}

    # basic properties
    for key in ['mode', 'design', 'offdesign']:
        kwargs[key] = c[key]

    for key, value in instance.attr().items():
        if key in c:
            # component parameters
            if isinstance(value, hlp.dc_cp):
                dc = hlp.dc_cp(val=c[key],
                               is_set=c[key + '_set'], is_var=c[key + '_var'])
                kwargs[key] = dc
            # component parameters
            if isinstance(value, hlp.dc_simple):
                dc = hlp.dc_simple(val=c[key], val_set=c[key + '_set'])
                kwargs[key] = dc
            # component characteristics
            elif isinstance(value, hlp.dc_cc):
                # finding x and y values of the characteristic function
                values = args[0]['id'] == c[key]

                try:
                    x = args[0][values].x.values[0]
                    y = args[0][values].y.values[0]
                except IndexError:
                    # if characteristics are missing (for compressor map atm)
                    x = cmp_char.characteristics().x
                    y = cmp_char.characteristics().y
                    msg = 'Could not find x and y values for characteristic line, using defaults instead for function ' + key + ' at component ' + c.label + '.'
                    logging.warning(msg)

                char = cmp_char.characteristics(x=x, y=y, method=c[key + '_method'], comp=instance.component())

                dc = hlp.dc_cc(is_set=c[key + '_set'],
                               method=c[key + '_method'],
                               param=c[key + '_param'],
                               func=char,
                               x=x, y=y)
                kwargs[key] = dc
            # component characteristics
            elif isinstance(value, hlp.dc_cm):
                # finding x and y values of the characteristic function
                values = args[1]['id'] == c[key]

                try:
                    x = list(args[1][values].x.values[0])
                    y = list(args[1][values].y.values[0])
                    z1 = list(args[1][values].z1.values[0])
                    z2 = list(args[1][values].z2.values[0])
                except IndexError:
                    # if characteristics are missing (for compressor map atm)
                    x = cmp_char.char_map().x
                    y = cmp_char.char_map().y
                    z1 = cmp_char.char_map().z1
                    z2 = cmp_char.char_map().z2

                    msg = 'Could not find x, y, z1 and z2 values for characteristic map, using defaults instead.'
                    logging.warning(msg)

                char_map = cmp_char.char_map(x=x, y=y, z1=z1, z2=z2, method=c[key + '_method'], comp=instance.component())

                dc = hlp.dc_cm(is_set=c[key + '_set'],
                               method=c[key + '_method'],
                               param=c[key + '_param'],
                               func=char_map,
                               x=x, y=y, z1=z1, z2=z2)
                kwargs[key] = dc
            # grouped component parameters
            elif isinstance(value, hlp.dc_gcp):
                dc = hlp.dc_gcp(method=c[key])
                kwargs[key] = dc

    instance.set_attr(**kwargs)
    return instance