8000 给SimulationBase加一个logger和进度条submodule · Issue #1670 · weihuayi/fealpy · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

给SimulationBase加一个logger和进度条submodule #1670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
scaomath opened this issue Jun 3, 2025 · 4 comments
Open

给SimulationBase加一个logger和进度条submodule #1670

scaomath opened this issue Jun 3, 2025 · 4 comments
Labels
enhancement feature request Suggest an idea on this project

Comments

@scaomath
Copy link
Collaborator
scaomath commented Jun 3, 2025

学习了下几个的cfd例子的,发现学生的code都喜欢直接print,时间一长一屏幕的print。

我看有几个地方都import logging但是并没有调用。只有PoissonLFEMSolver里面用了。

这是我的建议:logging有一个很好用的功能就是自定义Handler,可以自动把stdout里面的信息抓下来

class TqdmLoggingHandler(logging.Handler):
    def __init__(self, level=logging.NOTSET):
        super().__init__(level)

    def emit(self, record):
        try:
            msg = self.format(record)
            tqdm.write(msg)
            self.flush()
        except Exception:
            self.handleError(record)

然后在SimulationBase里面设置一个进度条的toggle,比如self.progress_bar,然后

stream_handler = TqdmLoggingHandler() if self.progress_bar else logging.StreamHandler(sys.stdout)
logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s - %(message)s",
        datefmt="%d-%b-%Y %H:%M:%S",
        handlers=[
            logging.FileHandler(filename=filename),
            stream_handler,
        ],
    )
self.logger =  logging.getLogger()

这个写成一个class也可以。
这样在进度条上写入信息,就会被logger记录下:比如在模拟时间问题的时候

class simulation(SimulationBase):
    def __init__(self, method):
        super().__init__(method)
        self._initialize_variables()
        ...
        self.logger = CustomLogger()

    def run(self):
        ...
        with tqdm(total=nt) as pbar:
            for i in range(nt):
                t = t0 + (i + 1) * dt
                message = ""
                message += f"max_u: {bm.max(bm.abs(u0)))}"
                pbar.update()

最后save下来的log大概长这样,有时间stamp

31-Mar-2025 19:50:05 - Generating data for singularly perturbed Poisson with 1200 samples
31-Mar-2025 19:50:05 - Using device: cuda | save dtype: torch.float32 | compute dtype: torch.float64
31-Mar-2025 19:50:11 - Saving data to /home/scao/Documents/fno-cfd-dev/data/PoissonSingular_N1200_n512_alpha2_tau10_eps1e-2_dirichlet.pt
31-Mar-2025 19:50:12 - Data saved.
31-Mar-2025 19:50:13 - f: size torch.Size([1200, 512, 512]) 	 avg norm 1.00000e+00 	 device cuda:0
31-Mar-2025 19:50:13 - u: size torch.Size([1200, 513, 513]) 	 avg norm 3.86070e-01 	 device cuda:0
31-Mar-2025 19:50:13 - residual: size torch.Size([1200, 511, 511]) 	 avg norm 1.89579e-04 	 device cuda:0

大神看看如何?@weihuayi @AlbertZyy

@scaomath scaomath added enhancement feature request Suggest an idea on this project labels Jun 3, 2025
@weihuayi
Copy link
Owner
weihuayi commented Jun 4, 2025

@scaomath 非常好的建议!logging 这一块,我们前面都没有强制要求。加入清晰的 logging 代码,对于用户使用和理解程序有很大的帮助。我们最近正在优化这方面的程序,要把上面代码融合进来。

@weihuayi
Copy link
Owner
weihuayi commented Jun 4, 2025

FEALPy 中初步会引入两类面向用户的程序,一类是标准方程的标准离散求解方法,如 "PoissonLFEMModel"(称其为计算模型类),放在各自离散方法的目录下,PoissonLFEMModel 放在 fealpy.fem 下面。这些计算模型,有点像小型的 APP。

针对这种计算模型,我们设计了一种定义 API 及 API 不同实现变体的机制,用户能过字符串,就可以选择不同算法变体,但 API 是不变的。

这种计算模型,我们希望可以进一步组合为复杂的计算模型,也希望它能对智能体友好,对人类用户出友好。

在这种计算模型类中,就应该融入上面的 logging 技术。

@weihuayi
Copy link
Owner
weihuayi commented Jun 4, 2025

另外还有一些面向应用领域的计算模型,一般放在 fealpy.csm(计算固体力学), fealpy.cem(计算电磁学),这一块的命名还没有确定。

@weihuayi
Copy link
Owner
weihuayi commented Jun 4, 2025

面向应用领域的计算模型,将来要和 UI 交互,通过 UI 可设置各种控制参数,控制整个程序的运行。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement feature request Suggest an idea on this project
Projects
None yet
Development

No branches or pull requests

2 participants
0