Elliott yz 2022-11-08 11:57 采纳率: 66.7%
浏览 76
已结题

全局变量、局部变量error

问题遇到的现象和发生背景

大家好,我在跑一个工程程序(基于水平集法优化悬臂梁)时,在iters=1之后会报错UnboundLocalError。我希望解决它,谢谢各位!

用代码块功能插入代码,请勿粘贴截图

报错的地方在子程序中的一个函数中:

@no_annotations
    def retract(self, input_phi, delta_x, scaling=1):
        """input_phi is not modified,
            output_phi refers to problem.phi
        Args:
            input_phi ([type]): [description]
            delta_x ([type]): [description]
            scaling (int, optional): [description]. Defaults to 1.

        Returns:
            [type]: [description]
        """
        self.current_max_distance_at_t0 = self.current_max_distance
        self.hj_solver.ts.setMaxTime(scaling)

        try:
            output_phi = self.hj_solver.solve(input_phi)
        except:
            reason = self.hj_solver.ts.getConvergedReason()
            print(f"Time stepping of Hamilton Jacobi failed. Reason: {reason}")
            if self.output_dir:
                print(f"Printing last solution to {self.output_dir}")
                fd.File(f"{self.output_dir}/failed_hj.pvd").write(input_phi)

        max_vel = calculate_max_vel(delta_x)
        self.last_distance = max_vel * scaling

        conv = self.hj_solver.ts.getConvergedReason()
        rtol, atol = (
            self.hj_solver.parameters["ts_rtol"],
            self.hj_solver.parameters["ts_atol"],
        )
        max_steps = self.hj_solver.parameters.get("ts_max_steps", 800)
        current_time = self.hj_solver.ts.getTime()
        total_time = self.hj_solver.ts.getMaxTime()
        if conv == 2:
            warning = (
                f"Maximum number of time steps {self.hj_solver.ts.getStepNumber()} reached."
                f"Current time is only: {current_time} for total time: {total_time}."
                "Consider making the optimization time step dt shorter."
                "Restarting this step with more time steps and tighter tolerances if applicable."
            )
            fd.warning(warning)

            # Tighten tolerances if we are really far
            if current_time / total_time < 0.2:
                current_rtol, current_atol = self.hj_solver.ts.getTolerances()
                new_rtol, new_atol = max(rtol / 200, current_rtol / 10), max(
                    atol / 200, current_atol / 10
                )
                self.hj_solver.ts.setTolerances(rtol=new_rtol, atol=new_atol)

            # Relax max time steps
            current_max_steps = self.hj_solver.ts.getMaxSteps()
            new_max_steps = min(current_max_steps * 1.5, max_steps * 3)
            self.hj_solver.ts.setMaxSteps(new_max_steps)

            output_phi.assign(input_phi)
        elif conv == 1:
            self.hj_solver.ts.setTolerances(rtol=rtol, atol=atol)

        return output_phi
运行结果及报错内容

运行结果为:
UnboundLocalError: local variable 'output_phi' referenced before assignment
在全局变量和局部变量处报错。

我的解答思路和尝试过的方法

按照网上的方法:
1.添加关键字,global output_phi。结果为:
NameError: name 'output_phi' is not defined

2.事先对output_phi赋值。结果为:
Warning, merit function did not decrease (merit=37.89031163251481, new_merit=779406.285122143)-> Trial 10
All trials have failed, passing to the next iteration.
这里虽然没有报错,但输出表示“试验了n次,数值未减小,算子未参与迭代步骤”。(总共试验了10次)
两个方法皆未能解决。

我想要达到的结果

希望各位师兄支招,解决报错!不胜感激!

  • 写回答

6条回答 默认 最新

  • 於黾 2022-11-08 14:19
    关注

    报错的原因是try里出错了,进入了except,导致output_phi的赋值语句没有执行
    你可以在try之前先把它赋值成0
    或者在try里面把它赋值成0也行

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月8日
  • 已采纳回答 11月8日
  • 创建了问题 11月8日

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行