示例#1
0
    def solve_heatExchanger(self, device: HeatExchanger):
        """Constructs the heat balance equation over the flows entering and exiting the heat exchanger. If equation is solvable as is (i.e. has 1 unknown), calculates the missing property
        and sets its value in the relevant object."""

        # m1h11 + m2h21 + m3h31 = m1h12 + m2h22 + m3h32
        # m1(h1i - h1o) + m2(h2i - h2o) + m3(h3i - h3o) = 0
        # heatBalance = LinearEquation([ [ ( (state_in, 'flow.massFF'), state_in.h - state_out.h) for state_in, state_out in device.lines], 0 ])

        heatBalance_LHS = []
        for state_in, state_out in device.lines:
            heatBalance_LHS.append(
                ((state_in.flow, 'massFF'), (state_in, 'h')))
            heatBalance_LHS.append(
                ((-1), (state_out.flow, 'massFF'), (state_out, 'h')))
        heatBalance = LinearEquation(LHS=heatBalance_LHS, RHS=0)

        if heatBalance.isSolvable(
        ):  # if solvable by itself, there is only one unknown
            solution = heatBalance.solve()
            unknownAddress = list(solution.keys())[0]
            setattr_fromAddress(object=unknownAddress[0],
                                attributeName=unknownAddress[1],
                                value=solution[unknownAddress])
            self._updatedUnknowns.add(unknownAddress)
        else:
            self._equations.append(heatBalance)
            heatBalance.source = device
示例#2
0
    def _solve_combination_ofEquations(self, number_ofEquations: int):
        """Iterates through combinations of equations (from the _equations pool) with the specified number_ofEquations. For each combination, checks if the
        system is solvable. If so, solves it, assigns the unknowns the solution values and removes the solved equations from the _equations pool."""
        for equationCombination in combinations(self._equations,
                                                number_ofEquations):

            # If any of the equations got solved in a previous iteration and got removed from _equations, skip this combination
            # Combinations are generated beforehand at the beginning of the main for loop.
            if any(equation not in self._equations
                   for equation in equationCombination):
                continue

            if (system := System_ofLinearEquations(
                    list(equationCombination))).isSolvable():
                solution = system.solve()
                unknownAddresses = list(solution.keys())
                for unknownAddress in unknownAddresses:
                    setattr_fromAddress(object=unknownAddress[0],
                                        attributeName=unknownAddress[1],
                                        value=solution[unknownAddress])
                    self._updatedUnknowns.add(unknownAddress)

                # If system is solved, all equations in the combination is solved. Remove them from equations pool.
                for equation in equationCombination:
                    self._equations.remove(equation)
示例#3
0
    def _add_pressureRatioRelation(self, device: WorkDevice):
        """Adds a linear equation describing the relation between end state pressures and the pressure ratio parameter of the work device.
        Works only with work devices that have one state_in and one states_out."""
        state_out = device.states_out[0]

        if isinstance(device, (Compressor, Pump)):  # Compression
            pressureRatioRelation_LHS = [((device, 'pressureRatio'),
                                          (device.state_in, 'P')),
                                         (-1, (state_out, 'P'))]
        else:
            assert isinstance(device, Turbine)  # Expansion
            pressureRatioRelation_LHS = [((device, 'pressureRatio'),
                                          (state_out, 'P')),
                                         (-1, (device.state_in, 'P'))]

        pressureRatioRelation = LinearEquation(LHS=pressureRatioRelation_LHS,
                                               RHS=0)
        device._pressureRatioRelationSetup = True

        if pressureRatioRelation.isSolvable():
            solution = pressureRatioRelation.solve()
            unknownAddress = list(solution.keys())[0]
            setattr_fromAddress(object=unknownAddress[0],
                                attributeName=unknownAddress[1],
                                value=solution[unknownAddress])
            self._updatedUnknowns.add(unknownAddress)
        else:
            pressureRatioRelation.source = device
            self._equations.append(pressureRatioRelation)
示例#4
0
    def _solve_solvableEquations(self):
        solvedEquations = []
        for equation in self._equations:
            equation.update()
            if equation.isSolvable():
                solution = equation.solve()
                unknownAddress = list(solution.keys())[0]
                setattr_fromAddress(object=unknownAddress[0],
                                    attributeName=unknownAddress[1],
                                    value=solution[unknownAddress])
                self._updatedUnknowns.add(unknownAddress)
                solvedEquations.append(equation)

        for equation in solvedEquations:
            self._equations.remove(equation)
示例#5
0
    def solve_mixingChamber(self, device: MixingChamber):
        """Sets or verifies common mixing pressure on all end states. Does mass & heat balances on flows."""

        # Infer constant mixing pressure
        sampleState_withPressure = None
        for endState in device.endStates:
            if isNumeric(endState.P):
                sampleState_withPressure = endState
                break
        if sampleState_withPressure is not None:
            for endState in [
                    state for state in device.endStates
                    if state is not sampleState_withPressure
            ]:
                endState.set_or_verify({'P': sampleState_withPressure.P})

        # Construct the equations

        # m1 + m2 + m3 - m4 = 0
        massBalance_LHS = []
        for state_in in device.states_in:
            massBalance_LHS.append((1, (state_in.flow, 'massFF')))
        massBalance_LHS.append((-1, (device.state_out.flow, 'massFF')))
        massBalance = LinearEquation(LHS=massBalance_LHS, RHS=0)

        # m1h1 + m2h2 + m3h3 - m4h4 = 0
        heatBalance_LHS = []
        for state_in in device.states_in:
            heatBalance_LHS.append(
                ((state_in.flow, 'massFF'), (state_in, 'h')))
        heatBalance_LHS.append(
            (-1, (device.state_out.flow, 'massFF'), (device.state_out, 'h')))
        heatBalance = LinearEquation(LHS=heatBalance_LHS, RHS=0)

        for equation in [massBalance, heatBalance]:
            if equation.isSolvable():
                solution = equation.solve()
                unknownAddress = list(solution.keys())[0]
                setattr_fromAddress(object=unknownAddress[0],
                                    attributeName=unknownAddress[1],
                                    value=solution[unknownAddress])
                self._updatedUnknowns.add(unknownAddress)
            else:
                self._equations.append(equation)
                equation.source = device
示例#6
0
    def _add_turbineMassBalance(self, device: Turbine):
        """Creates a mass balance equation for flows entering/exiting a turbine."""
        massBalance_LHS = []
        massBalance_LHS.append((1, (device.state_in.flow, 'massFF')))
        for state_out in device.states_out:
            massBalance_LHS.append((-1, (state_out.flow, 'massFF')))
        massBalance = LinearEquation(LHS=massBalance_LHS, RHS=0)
        massBalance.source = device

        if massBalance.isSolvable():
            solution = massBalance.solve()
            unknownAddress = list(solution.keys())[0]
            setattr_fromAddress(object=unknownAddress[0],
                                attributeName=unknownAddress[1],
                                value=solution[unknownAddress])
            self._updatedUnknowns.add(unknownAddress)
        else:
            self._equations.append(massBalance)
示例#7
0
    def _solveDevice(self, device: Device):
        endStates = device.endStates

        if isinstance(device, WorkDevice):
            # Apply isentropic efficiency relations to determine outlet state
            self.solve_workDevice(device)

        # if not self._initialSolutionComplete:  # the below processes do not need to be done in each flow solution iteration, but only for the initial one

        if isinstance(device, HeatDevice):
            # Setting end state pressures to be the same
            if device._infer_constant_pressure:
                device.infer_constant_pressure()

            if isinstance(
                    device,
                    ReheatBoiler):  # reheat boilers can have multiple lines.
                # Setting up fixed exit temperature if inferring exit temperature from one exit state
                if device._infer_fixed_exitT:
                    device.infer_fixed_exitT()

            elif isinstance(device, Intercooler):
                if device.coolTo == 'ideal':  # Cool to the temperature of the compressor inlet state
                    assert isinstance(
                        (compressorBefore := self.get_itemRelative(
                            device, -2)), Compressor
                    )  # before intercooler, there should be compressor exit state, and then a compressor
                    device.state_out.set_or_verify(
                        {'T': compressorBefore.state_in.T})
                else:  # Cool to specified temperature
                    assert isNumeric(device.coolTo)
                    device.state_out.set_or_verify({'T': device.coolTo})

            elif isinstance(device, GasReheater):
                if device.heatTo == 'ideal':  # Heat to the temperature of the turbine inlet state
                    assert isinstance(
                        (turbineBefore := self.get_itemRelative(device, -2)),
                        Turbine)
                    device.state_out.set_or_verify(
                        {'T': turbineBefore.state_in.T})

                elif device.heatTo == 'heatSupplied':
                    if not self._initialSolutionComplete:
                        assert isNumeric(device.sHeatSupplied)
                        if not self.constant_c:
                            sHeatSuppliedRelation = LinearEquation(
                                LHS=[(1, (device.state_out, 'h')),
                                     (-1, (device.state_in, 'h'))],
                                RHS=device.sHeatSupplied)
                        else:
                            sHeatSuppliedRelation = LinearEquation(
                                LHS=[(1, self.workingFluid.cp,
                                      (device.state_out, 'T')),
                                     (-1, self.workingFluid.cp,
                                      (device.state_in, 'T'))],
                                RHS=device.sHeatSupplied)
                        self._equations.append(sHeatSuppliedRelation)

                        if sHeatSuppliedRelation.isSolvable():
                            solution = sHeatSuppliedRelation.solve()
                            unknownAddress = list(solution.keys())[0]
                            setattr_fromAddress(
                                object=unknownAddress[0],
                                attributeName=unknownAddress[1],
                                value=solution[unknownAddress])
                            self._updatedUnknowns.add(unknownAddress)
                        else:
                            sHeatSuppliedRelation.source = device
                            self._equations.append(sHeatSuppliedRelation)
                else:  # Heat to specified temperature
                    assert isNumeric(device.heatTo)
                    device.state_out.set_or_verify({'T': device.heatTo})

        elif isinstance(device, HeatExchanger):
            # Setting end state pressures along the same line if pressures is assumed constant along each line
            if device._infer_constant_linePressures:
                device.infer_constant_linePressures()

            # Setting temperature of exit states equal for all lines # TODO - not the ideal place - inter-flow operation should ideally be in cycle scope
            if device._infer_common_exitTemperatures:
                device.infer_common_exitTemperatures()

        elif isinstance(device, MixingChamber):
            # Setting pressures of all in / out flows to the same value
            if device._infer_common_mixingPressure:
                device.infer_common_mixingPressure()

        elif isinstance(device, Trap):
            if device._infer_constant_enthalpy:
                device.infer_constant_enthalpy()

        self._defineStates_ifDefinable(endStates)
示例#8
0
    def solve_regenerator(self, device: Regenerator):

        if all(
                isNumeric(line[0].T) for line in device.lines
        ):  # Need inlet temperatures of both lines to determine in which direction heat will flow

            warmLine, coldLine = device.lines
            if device.lines[1][0].T > device.lines[0][
                    0].T:  # state_in of device.lines[1]
                coldLine, warmLine = device.lines
            warm_in, warm_out = warmLine
            cold_in, cold_out = coldLine

            assert warm_in.flow.constant_c == cold_in.flow.constant_c, 'solve_regenerator: Flows of the warm and cold lines have different constant_c settings! Not allowed.'
            constant_c = warm_in.flow.constant_c

            if device.counterFlow_commonColdTemperature:
                warm_out.set_or_verify({'T': cold_in.T})

            heatBalance_LHS = []
            # warm_mFF*(warm_in.h - warm_out.h)*effectiveness = cold_mFF*(cold_out.h - cold_in.h)
            # warm_mFF*(warm_in.h - warm_out.h)*effectiveness + cold_mFF*(cold_in.h - cold_out.h) = 0

            if constant_c:
                assert isNumeric(warm_in.flow.workingFluid.cp)
                heatBalance_LHS.append(
                    ((device.effectiveness), (warm_in.flow, 'massFF'),
                     (warm_in.flow.workingFluid.cp), (warm_in, 'T')))
                heatBalance_LHS.append(
                    ((device.effectiveness), (-1), (warm_out.flow, 'massFF'),
                     (warm_out.flow.workingFluid.cp), (warm_out, 'T')))

                heatBalance_LHS.append(
                    ((cold_in.flow, 'massFF'), (cold_in.flow.workingFluid.cp),
                     (cold_in, 'T')))
                heatBalance_LHS.append(
                    ((-1), (cold_out.flow, 'massFF'),
                     (cold_out.flow.workingFluid.cp), (cold_out, 'T')))

            else:
                heatBalance_LHS.append(
                    ((device.effectiveness), (warm_in.flow, 'massFF'),
                     (warm_in, 'h')))
                heatBalance_LHS.append(
                    ((device.effectiveness), (-1), (warm_out.flow, 'massFF'),
                     (warm_out, 'h')))

                heatBalance_LHS.append(
                    ((cold_in.flow, 'massFF'), (cold_in, 'h')))
                heatBalance_LHS.append(
                    ((-1), (cold_out.flow, 'massFF'), (cold_out, 'h')))

            heatBalance = LinearEquation(LHS=heatBalance_LHS, RHS=0)

            if heatBalance.isSolvable(
            ):  # if solvable by itself, there is only one unknown
                solution = heatBalance.solve()
                unknownAddress = list(solution.keys())[0]
                setattr_fromAddress(object=unknownAddress[0],
                                    attributeName=unknownAddress[1],
                                    value=solution[unknownAddress])
                self._updatedUnknowns.add(unknownAddress)
            else:
                self._equations.append(heatBalance)
                heatBalance.source = device

            return True

        else:
            return False