ControlVolume.py 文件源码

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

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

        """The subs() method allows for unknowns found from other ControlVolumes to be substituted into the equations
        for this system, reducing the total number of unknowns and the total degrees of freedom (hopefully until the
        degrees of freedom for the system reach zero and it becomes solvable). It requires a dictionary of
        variable:solution format from which it will substitute values into the ControlVolume equations_dict."""

        # This list is created to store equations that are no longer useful and remove them
        # This occurs when an equation (generally an info equation) is used in another ControlVolume which implies that
        # all variables in that equation have been solved and it cannot provide any new relationships to the system
        remove_equation_list = []

        # For each solved variable in substitution_dict
        for substitution, solution in substitution_dict.items():

            # The substitution must meet 3 characteristics: it must exist in the current ControlVolume as a variable,
            # the variable in the ControlVolume must be unknown (a sympy Symbol, not a value), and the substitution must
            # be solved (it itself has zero unknowns in the form of sympy Symbols)
            if substitution in self.dict_of_variables and type(self.dict_of_variables[substitution]) == sp.Symbol and \
                            len(solution.atoms(sp.Symbol)) < 1:

                # If this is true, then the ControlVolume can remove the variable from it's dict_of_variables as it has
                # already been solved and doesn't need to be solved again. The total unknowns decreases by one
                self.dict_of_variables.pop(substitution)
                self.unknowns -= 1

                # Each equation needs to substitute the unknown for it's solved solution using the sympy subs() method
                for key, equation in self.equations_dict.items():
                    self.equations_dict[key] = equation.subs(substitution, solution)

                    # This if statement checks if the equation has become irrelevant (nothing to solve, just 0)
                    # If the equation lacks unknowns, it will be removed from the equations_dict for the ControlVolume
                    if len(self.equations_dict[key].atoms(sp.Symbol)) == 0:
                        remove_equation_list.append(key)

                # This loop removes every equation that is no longer useful
                for key in remove_equation_list:
                    self.equations_dict.pop(key)

        # After a substitution is done, the degrees of freedom have likely changed, so the ControlVolume will update it
        self.degrees_of_freedom_update()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号