Reaction.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:Chemistry-ChemEng 作者: AndyWilliams682 项目源码 文件源码
def balance_reaction(self):

        """The balance_reaction method will generate a sympy matrix with rows that correspond to each element in the
        reaction and columns that correspond to each compound. Using the sympy linsolve function it will begin to solve
        the matrix for each coefficient in terms of of of the compounds. This compound can be assumed to equal 1 in the
        next method which will allow for scaling of the coefficients while keeping the reaction balanced."""

        # The rows of the reaction matrix is each element in the reaction
        # The columns are the total compounds present in the equation
        # The equation matrix is defined using the sympy zeros function, all slots are set to zero for easy manipulation
        reaction_matrix = sp.zeros(len(self.elements), len(self.species))

        # The symbols_list contains every compound in the reaction as a sympy symbol so that it can solve the matrix
        symbols_list = []

        # Every compound in the reaction is checked for elements
        for compound in self.species.keys():

            # The periodictable method atoms is called on the compound formula
            # This identifies how many of each element are present in the compound
            composition = ptable.formula(compound).atoms

            # The compound is made into a symbol and stored in the symbols_list
            symbols_list.append(sp.sympify(compound))

            # Every element in the reaction must be considered, even if they are not all present in the compound
            for element in self.elements:

                # if the element is not present in the compound, it will be stored as a zero in the composition
                if element not in composition:
                    composition[element] = 0

                # The reaction matrix for the compound and element is set to the compound amount of that element
                # self.elements.index(element) returns an integer number for the element position in the matrix
                # list(self.species.keys()).index(compound) returns an integer number for the compound position
                reaction_matrix[self.elements.index(element),
                                list(self.species.keys()).index(compound)] = composition[element]

        # Sympy converts the equation matrix to a sympy compatible matrix object
        # It also creates the solution vector of all 0
        reaction_matrix = sp.Matrix(reaction_matrix)
        solution_vector = sp.Matrix(sp.zeros(len(self.elements), 1))

        # A sympy set object containing values is output by the linsolve function from sympy,
        # which solves the matrix in terms of one of the compounds present (usually the last compound)
        solution_set = sp.linsolve((reaction_matrix, solution_vector), symbols_list)

        # This converts the solution_set to usable coefficients
        self.interpret_balance(symbols_list, solution_set)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号