示例#1
0
 def __init__(self, parent, config, **kwargs):
     pdsim_panels.PDPanel.__init__(self, parent, **kwargs)
     
     # Now we are going to put everything into a scrolled window
     main_sizer = wx.BoxSizer(wx.VERTICAL)
     
     # The scrolled panel
     scrolled_panel = ScrolledPanel(self, size = (-1,-1), style = wx.TAB_TRAVERSAL, name="panel1")
     scrolled_panel.SetScrollbars(1, 1, 1, 1)
     
     annotated_GUI_objects = []
     self.config = config
     self.keys_for_config = []
     
     #----------------------------------------------------------------------
     # The sizer for all the heat transfer terms
     sizer_for_port_inputs = wx.FlexGridSizer(cols = 2, vgap = 4, hgap = 4)
     
     # Loop over the HT inputs
     annotated_values = self.get_annotated_values(['d_discharge','d_suction'])
         
     # Build the items and return the list of annotated GUI objects, add to existing list
     annotated_GUI_objects += self.construct_items(annotated_values,
                                                    sizer = sizer_for_port_inputs,
                                                    parent = scrolled_panel)
                                                    
    #----------------------------------------------------------------------
     # The sizer for all the valve terms
     sizer_for_valve_inputs = wx.FlexGridSizer(cols = 2, vgap = 4, hgap = 4)
 
     # Loop over the HT inputs
     annotated_values = self.get_annotated_values(['valve_E','valve_d','valve_h','valve_l','valve_a','valve_x_stopper','valve_rho','valve_C_D'])
         
     # Build the items and return the list of annotated GUI objects, add to existing list
     annotated_GUI_objects += self.construct_items(annotated_values,
                                                    sizer = sizer_for_valve_inputs,
                                                    parent = scrolled_panel)
                                                    
                                                    
     
     # Register terms in the GUI database
     self.main.register_GUI_objects(annotated_GUI_objects)
     
     sizer = wx.BoxSizer(wx.VERTICAL)
     sizer.Add(HeaderStaticText(scrolled_panel, "Geometric Inputs"), 0, wx.ALIGN_CENTER_HORIZONTAL)
     sizer.Add(sizer_for_port_inputs, 0, wx.ALIGN_CENTER_HORIZONTAL)
     
     sizer.Add(HeaderStaticText(scrolled_panel, "Valve Inputs"), 0, wx.ALIGN_CENTER_HORIZONTAL)
     sizer.Add(sizer_for_valve_inputs, 0, wx.ALIGN_CENTER_HORIZONTAL)
     
     scrolled_panel.SetSizer(sizer)
     main_sizer.Add(scrolled_panel, 1, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
     self.SetSizer(main_sizer)
     
     sizer.Layout()
示例#2
0
 def __init__(self, parent, config, **kwargs):
     pdsim_panels.PDPanel.__init__(self, parent, **kwargs)
     
     # Now we are going to put everything into a scrolled window
     main_sizer = wx.BoxSizer(wx.VERTICAL)
     
     # The scrolled panel
     scrolled_panel = ScrolledPanel(self, size = (-1,-1), style = wx.TAB_TRAVERSAL, name="panel1")
     scrolled_panel.SetScrollbars(1, 1, 1, 1)
     
     annotated_GUI_objects = []
     self.config = config
     self.keys_for_config = []
     
     #----------------------------------------------------------------------
     # The sizer for all the heat transfer terms
     sizer_for_inputs = wx.FlexGridSizer(cols = 2, vgap = 4, hgap = 4)
     
     # Loop over the HT inputs
     keys = ['piston_diameter','piston_length','crank_length',
             'connecting_rod_length','x_TDC','shell_volume',
             'inlet_tube_length','inlet_tube_ID','outlet_tube_length',
             'outlet_tube_ID']
     annotated_values = self.get_annotated_values(keys)
         
     # Build the items and return the list of annotated GUI objects, add to existing list
     annotated_GUI_objects += self.construct_items(annotated_values,
                                                    sizer = sizer_for_inputs,
                                                    parent = scrolled_panel)
     
     # Register terms in the GUI database
     self.main.register_GUI_objects(annotated_GUI_objects)
     
     #self.main.get_GUI_object('L_ratio_bearings').GUI_location.SetToolTipString('Ratio of z1/z2, where\n\nz1 : the length from the centerline of the upper bearing to the lower bearing\nz2 : the length from the centerline of the upper bearing to the orbiting scroll bearing')
     
     sizer = wx.BoxSizer(wx.VERTICAL)
     sizer.Add(HeaderStaticText(scrolled_panel, "Geometric Inputs"), 0, wx.ALIGN_CENTER_HORIZONTAL)
     sizer.Add(sizer_for_inputs, 0, wx.ALIGN_CENTER_HORIZONTAL)
     
     scrolled_panel.SetSizer(sizer)
     main_sizer.Add(scrolled_panel, 1, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
     self.SetSizer(main_sizer)
     
     sizer.Layout()
示例#3
0
class InjectionInputsPanel(pdsim_panels.PDPanel):
    """
    The container panel for all the injection ports and injection data 
    """
    def __init__(self, parent, **kwargs):
        pdsim_panels.PDPanel.__init__(self, parent, **kwargs)

        #Now we are going to put everything into a scrolled window
        main_sizer = wx.BoxSizer(wx.VERTICAL)

        self.scrolled_panel = ScrolledPanel(self,
                                            size=(-1, -1),
                                            style=wx.TAB_TRAVERSAL,
                                            name="panel1")
        self.scrolled_panel.SetScrollbars(1, 1, 1, 1)
        self.scrolled_panel.SetupScrolling()

        #Add the header row of buttons
        self.View = wx.Button(self.scrolled_panel, label='View')
        self.View.Bind(wx.EVT_BUTTON, self.OnView)
        self.AddInjection = wx.Button(self.scrolled_panel,
                                      label='Add Injection Line')
        self.AddInjection.Bind(wx.EVT_BUTTON, self.OnAddInjection)
        self.PlotExistence = wx.Button(self.scrolled_panel,
                                       label='Plot Existence')
        self.PlotExistence.Bind(wx.EVT_BUTTON, self.OnPlotExistence)
        buttons_sizer = wx.BoxSizer(wx.HORIZONTAL)
        buttons_sizer.Add(self.AddInjection)
        buttons_sizer.Add(self.View)
        buttons_sizer.Add(self.PlotExistence)

        sizer = wx.FlexGridSizer(cols=1)
        sizer.Add(buttons_sizer)
        sizer.AddSpacer(10)
        sizer.Layout()

        self.scrolled_panel.SetAutoLayout(1)

        #Do the layout of all the panels
        self.scrolled_panel.SetSizer(sizer)
        main_sizer.Add(self.scrolled_panel, 1, wx.EXPAND)
        self.SetSizer(main_sizer)
        main_sizer.Layout()

        #Set some local variables
        self.Nterms = 0
        self.Lines = []

    def OnAddInjection(self, event=None):
        """
        Add an injection line to the injection panel
        """
        IE = InjectionElementPanel(self.scrolled_panel, self.Nterms + 1)
        #Put the panel within the scrolled panel and refresh
        self.scrolled_panel.GetSizer().Add(IE, 0)
        self.scrolled_panel.FitInside()
        self.GetSizer().Layout()

        #Update the local variables
        self.Lines.append(IE)
        self.Nterms += 1

        self.Refresh()

    def remove_all(self):
        while self.Lines:
            self.RemoveInjection(self.Lines[0])

    def RemoveInjection(self, injection):
        """
        Remove the given injection term
        """
        self.Lines.remove(injection)
        injection.Destroy()
        self.Nterms -= 1
        #Renumber the injection panels that are contained in scrolled_panel
        I = 1
        for child in self.scrolled_panel.Children:
            if isinstance(child, InjectionElementPanel):
                child.SizerBox.SetLabel("Injection line #" + str(I))
                I += 1
        self.GetSizer().Layout()
        self.scrolled_panel.FitInside()
        self.Refresh()

    def OnView(self, event):

        geo = self.GetTopLevelParent().MTB.InputsTB.panels[0].Scroll.geo
        SAF = ScrollAnimForm(geo, start=False)

        #IEPs are children that are instances of InjectionElementPanel class
        IEPs = [
            child for child in self.scrolled_panel.Children
            if isinstance(child, InjectionElementPanel)
        ]
        for IEP in IEPs:
            for child in IEP.Children:
                if isinstance(child, InjectionPortPanel):
                    # Get the values from the panel
                    vals = child.get_values()
                    # Overlay the port on the scroll wrap plot
                    scroll_geo.overlay_injection_port(0,
                                                      geo,
                                                      vals['phi'],
                                                      SAF.ax,
                                                      vals['involute'],
                                                      rport=vals['D'] / 2,
                                                      offset=vals['offset'])
        SAF.start()
        SAF.Show()

    def OnPlotExistence(self, event=None):
        """
        Plot a 2D line plot showing which control volume is connected to 
        each injection port as a function of the crank angle
        """
        import pylab
        import numpy as np

        _Scroll = self.GetTopLevelParent().MTB.InputsTB.panels[0].Scroll

        Iport = 1
        #IEPs are children that are instances of InjectionElementPanel class
        IEPs = [
            child for child in self.scrolled_panel.Children
            if isinstance(child, InjectionElementPanel)
        ]
        for IEP in IEPs:
            for child in IEP.Children:
                if isinstance(child, InjectionPortPanel):
                    #Get the values from the port panel
                    v = child.get_values()

                    partner_list = []

                    theta = np.linspace(0, 2 * pi, 1000)
                    for th in theta:
                        partner_list.append(
                            _Scroll._get_injection_CVkey(
                                v['phi'], th, v['involute']))

                    #Find the break points in each segment
                    dividers = [
                        i for i in range(len(theta) - 1)
                        if not partner_list[i] == partner_list[i + 1]
                    ]
                    #Add end and beginning indices
                    dividers = [0] + dividers + [len(theta) - 1]

                    for i in range(len(dividers) - 1):
                        L = dividers[i]
                        R = dividers[i + 1]
                        M = int((L + R) / 2)
                        pylab.plot(np.r_[theta[L], theta[R]], np.r_[Iport,
                                                                    Iport])
                        pylab.plot(np.r_[theta[L], theta[L]],
                                   np.r_[Iport - 0.02, Iport + 0.02], 'k')
                        pylab.plot(np.r_[theta[R], theta[R]],
                                   np.r_[Iport - 0.02, Iport + 0.02], 'k')
                        pylab.text(theta[M],
                                   Iport + .02,
                                   partner_list[M],
                                   ha='center',
                                   va='bottom')

                    #Increase the counter
                    Iport += 1

        pylab.xticks([0, pi / 2, pi, 3 * pi / 2, 2 * pi],
                     [0, r'$\pi/2$', r'$\pi$', r'$3\pi/2$', r'$2\pi$'])
        pylab.xlim(0, 2 * pi)
        pylab.ylim(0.5, Iport - 1 + 0.5)
        pylab.yticks(range(1, Iport + 1))
        pylab.show()

    def build_from_configfile(self, config):
        """
        Get parameters from the configfile section for this plugin
        
        Parameters
        ----------
        config : yaml configuration section for the plugin
        
        """

        if config:
            self.remove_all()
            for line in config:
                # Add an injection line panel
                self.OnAddInjection()
                #Get a pointer to the last IEP (the one just added)
                IEP = self.Lines[-1]
                #Set the line length in the GUI [m]
                IEP.Lval.SetValue(str(line['Length']))
                #Set the line ID in the GUI
                IEP.IDval.SetValue(str(line['ID']))
                #Set the State in the GUI
                State = line['inletState']
                IEP.state.set_state(State['Fluid'],
                                    T=State['T'],
                                    D=State['rho'])
                if 'ports' in line and line['ports']:
                    for i, port in enumerate(line['ports']):
                        if i > 0: IEP.OnAddPort()
                        # Get a pointer to the port panel
                        portpanel = IEP.ports_list[-1]
                        # Set the values in the panel
                        portpanel.set_values(port)

    def get_additional_parametric_terms(self):

        #: the list of terms
        _T = []

        #IEPs are children of injection_panel that are instances of InjectionElementPanel class
        IEPs = [
            child for child in self.scrolled_panel.Children
            if isinstance(child, InjectionElementPanel)
        ]
        for i, IEP in enumerate(IEPs):
            I = str(i + 1)

            _T += [
                dict(attr='injection_state_pressure_' + I,
                     text='Injection pressure #' + I + ' [kPa]',
                     parent=self),
                dict(attr='injection_state_sat_temp_' + I,
                     text='Injection saturated temperature (dew) #' + I +
                     ' [K]',
                     parent=self),
                dict(attr='injection_state_temp_' + I,
                     text='Injection temperature #' + I + ' [K]',
                     parent=self),
                dict(attr='injection_state_superheat_' + I,
                     text='Injection superheat #' + I + ' [K]',
                     parent=self),
            ]

            Ports = [
                c for c in IEP.Children if isinstance(c, InjectionPortPanel)
            ]
            for j, child in enumerate(Ports):
                J = str(j + 1)
                _T += [
                    dict(attr='injection_phi_' + I + '_' + J,
                         text='Injection port angle #' + I + ':' + J +
                         ' [rad]',
                         parent=self)
                ]

        return _T

    def apply_additional_parametric_terms(self, attrs, vals, panel_items):
        """
        Set the terms in the injection panel based on the additional parametric
        terms provided by the get_additional_parametric_terms() function
        """
        def apply_line_terms(attrs, vals):
            def is_int(i):
                """ Returns True if it is an integer """
                try:
                    i = int(i)
                    return True
                except ValueError:
                    return False

            def is_line_term(attr):
                """
                Check if it is a line type term of the form injection_xxxxx_1'
                and is not a port term of the form injection_xxxxx_1_1
                """
                if not attr.startswith('injection'):
                    return False

                #If there are no underscores, return false
                if len(attr.rsplit('_', 1)) == 1:
                    return False

                #Try to split twice
                attr, i, j = attr.rsplit('_', 2)

                # If the far right one is an integer and the left part isn't you are
                # ok, its an injection line
                if not is_int(i) and is_int(j):
                    return True
                else:
                    return False

            # First check about the injection state; if two state related terms are
            # provided, use them to fix the injection state
            inj_state_params = [(par, val) for par, val in zip(attrs, vals)
                                if is_line_term(par)]
            num_inj_state_params = len(inj_state_params)

            for i in range(len(self.Lines)):

                #Find the injection state terms that apply for this line
                state_params = [
                    (par, val) for par, val in zip(attrs, vals)
                    if par.find('state') > -1 and par.endswith(str(i + 1))
                ]
                num_state_params = len(state_params)

                #Get a copy of the state from the StatePanel
                inletState = self.Lines[i].state.GetState()

                if num_state_params > 0:
                    #Unzip the parameters (List of tuples -> tuple of lists)
                    state_attrs, state_vals = zip(*state_params)

                if num_state_params == 2:
                    # Remove all the entries that correspond to the injection state -
                    # we need them and don't want to set them in the conventional way
                    for a in state_attrs:
                        vals.pop(attrs.index(a))
                        attrs.pop(attrs.index(a))

                    #: The string representation of the index (1-based)
                    I = str(i + 1)

                    #Temperature and pressure provided
                    if 'injection_state_temp_' + I in state_attrs and 'injection_state_pressure_' + I in state_attrs:
                        injection_temp = state_vals[state_attrs.index(
                            'injection_state_temp_' + I)]
                        injection_pressure = state_vals[state_attrs.index(
                            'injection_state_pressure_' + I)]
                        self.Lines[i].state.set_state(inletState.Fluid,
                                                      T=injection_temp,
                                                      P=injection_pressure)

                    #Dew temperature and superheat provided
                    elif 'injection_state_sat_temp_' + I in state_attrs and 'injection_state_superheat_' + I in state_attrs:
                        injection_sat_temp = state_vals[state_attrs.index(
                            'injection_state_sat_temp_' + I)]
                        injection_superheat = state_vals[state_attrs.index(
                            'injection_state_superheat_' + I)]
                        injection_temp = injection_sat_temp + injection_superheat
                        import CoolProp.CoolProp as CP
                        injection_pressure = CP.PropsSI(
                            'P', 'T', injection_sat_temp, 'Q', 1.0,
                            inletState.Fluid) / 1000.0
                        self.Lines[i].state.set_state(inletState.Fluid,
                                                      T=injection_temp,
                                                      P=injection_pressure)

                    else:
                        raise ValueError(
                            'Invalid combination of injection states: ' +
                            str(state_attrs))

                elif num_inj_state_params == 1:
                    import textwrap
                    string = textwrap.dedent("""
                             Sorry but you need to provide two variables for the injection
                             state in parametric table to fix the state.  
                             
                             If you want to just modify the saturated temperature, add the superheat as a
                             variable and give it one element in the parametric table
                             """)
                    dlg = wx.MessageDialog(None, string)
                    dlg.ShowModal()
                    dlg.Destroy()
                    raise ValueError(
                        'Must provide two state variables in the parametric table for injection line'
                    )

                elif num_inj_state_params > 2:
                    raise ValueError(
                        'Only two inlet state parameters can be provided in parametric table'
                    )

            return attrs, vals

        def apply_port_terms(attrs, vals):
            phi_params = [(par, val) for par, val in zip(attrs, vals)
                          if par.startswith('injection_phi')]
            num_phi_params = len(phi_params)

            if num_phi_params > 0:
                #Unzip the parameters (List of tuples -> tuple of lists)
                phi_attrs, phi_vals = zip(*phi_params)

                # Remove all the entries that correspond to the angles
                # we need them and don't want to set them in the conventional way
                for a in phi_attrs:
                    i = attrs.index(a)
                    vals.pop(i)
                    attrs.pop(i)

                for attr, val in zip(phi_attrs, phi_vals):

                    # Term might look like something like 'injection_phi_1_2'
                    # i would be 0, j would be 1
                    #indices are zero-based
                    j = int(attr.rsplit('_', 1)[1]) - 1
                    i = int(attr.rsplit('_', 2)[1]) - 1

                    self.Lines[i].ports_list[j].phi_inj_port.SetValue(str(val))

            return attrs, vals

        #Apply all the line terms and get back the lists
        attrs, vals = apply_line_terms(attrs, vals)
        #Apply all the line terms and get back the lists
        attrs, vals = apply_port_terms(attrs, vals)

        return attrs, vals