如何从控制台存储最后显示的值?

2024-04-18 07:31:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我的python脚本将不断变化的输入传递给一个名为“Dymola”的程序,该程序依次执行模拟以生成输出。这些输出存储为numpy数组“out1.npy”。你知道吗

for i in range(0,100):
    #code to initiate simulation
    print(startValues, 'ParameterSet:', ParameterSet,'time:', stoptime)
    np.save('out1.npy', output_data)

不幸的是,Dymola经常崩溃,这使得有必要在崩溃时从控制台中显示的时间(例如:50)重新运行循环,并将输出文件的数量增加1。否则第一组的数据将被覆盖。你知道吗

for i in range(50,100):
    #code to initiate simulation
    print(startValues, 'ParameterSet:', ParameterSet,'time:', stoptime)
    np.save('out2.npy', output_data)

Dymola崩溃后,有没有办法从控制台读出“stoptime”值(例如50)?你知道吗


Tags: toin程序forcoderangesimulationprint
2条回答

我假设dymola是第三方实体,你不能改变。你知道吗

一种可能性是使用subprocess模块启动dymola并从您的程序中读取它的输出,或者在它运行时逐行读取,或者在创建的进程退出后全部读取。您还可以访问dymola的退出状态。你知道吗

如果它是一个Windows-y的东西,它不做流输出,但操纵一个windowgui样式,如果它不生成有用的退出状态代码,那么最好的办法可能是查看它在退出时或之后创建了哪些文件。sorted( glob.glob("somepath/*.out"))可能有用吗?你知道吗

我假设您正在使用dymola接口来模拟您的模型。如果是这样,为什么不使用动态模拟()函数并检查错误。 例如:

crash_counter = 1
from dymola.dymola_interface import DymolaInterface
dymola = DymolaInterface()
for i in range(0,100):
    res = dymola.simulate("myModel")
    if not res:
        crash_counter += 1
    print(startValues, 'ParameterSet:', ParameterSet,'time:', stoptime)
    np.save('out%d.npy'%crash_counter, output_data)

由于有时很难在您的机器上安装DymolaInterface,这里有一个有用的link。 从那里取材:

The Dymola Python Interface comes in the form of a few modules at \Dymola 2018\Modelica\Library\python_interface. The modules are bundled within the dymola.egg file.

要安装:

The recommended way to use the package is to append the \Dymola 2018\Modelica\Library\python_interface\dymola.egg file to your PYTHONPATH environment variable. You can do so from the Windows command line via set PYTHONPATH=%PYTHONPATH%;D:\Program Files (x86)\Dymola 2018\Modelica\Library\python_interface\dymola.egg.

如果不起作用,请在实例化接口之前附加以下代码:

import os
import sys
sys.path.insert(0, os.path.join('PATHTODYMOLA',
                                'Modelica',
                                'Library',
                                'python_interface',
                                'dymola.egg'))

相关问题 更多 >