首页 文章

OpenModelica实时更新CombiTable输入

提问于
浏览
2

我有一个模型,我使用CombiTable1D从.txt文件中检索外部输入 . 该文件目前由Python脚本生成,但在项目的最后阶段,它将每秒更新一次 . 目前,由于.txt文件是静态的,因此模拟没有问题 . 只需读取文件并根据其中写入的数据进行模拟 .

我想要做的是模拟一个模型直到某个时间,比方说100秒,然后让它等到实时事件,通过该实时事件更新.txt文件以获得100-200之间的下一个外部输入值 . 通过在接下来的100秒内获取这些新值,模拟应该继续 .

由于我已经在使用OMPython,因此使用Python编辑.txt文件非常实用,让's say for each 10 seconds in real time. I can now simulate the model until the time instance that I define as the refreshing point of the external input. But I couldn'弄清楚如何保持模拟的状态并使其再次读取文件 .

2 回答

  • 0

    实际上,这对我来说听起来像是一个联合模拟场景 . 无论如何,你可以做的是从CombiTable1D扩展并有类似的东西

    block CombiTable1DWithUpdate
      extends Modelica.Blocks.Tables.CombiTable1D(final tableOnFile=true);
      algorithm
        when sample(0, 10) then
          readTableData(tableID, /* force update */ true, verboseRead);
        end when;
    end CombiTable1DWithUpdate;
    
  • 3

    除了我接受的答案,我想提出另一个效率不高的解决方案 . 对于带有电容器和电阻器的简单型号,我进行了成功的测试,但是对于更复杂的模型,它无法正常工作 . 在Modelica脚本中,realTimeSimulation.mos:

    outputFile := "stepResult.mat";
    simulation_step := 1;
    start_time := 0;
    stop_time := start_time+simulation_step;
    loadFile("WhereverTheFileIs.mo");
    buildModel(myTestModel);
    system("myTestModel-override=startTime="+String(start_time)+",stopTime="+String(stop_time)+" -r="+outputFile);
    

    将构建模型并模拟第一步,直到模拟时间t = 1s . 稍后,使用Python更新文本文件 . t = 1s和t = 2s之间时间的新数据被写入文本文件,我将获得模型的输入 . 然后对t = 1s和t = 2s之间的时间进行另一个模拟步骤 . 作为一个循环,它像永远一样继续:实现数据,为新的时间间隔进行新的模拟 . 诀窍是,使用以下脚本读取在每个步骤结束时创建的输出文件,并将所有变量值作为模拟的新初始条件:

    valueList := readSimulationResultVars(outputFile);
    start_time := start_time+simulation_step;
    stop_time := stop_time+simulation_step;
    value := val(OpenModelica.Scripting.stringVariableName(valueList[1]),start_time,outputFile);',
    variableString := valueList[1] + "=" + String(value);
    
    for i in 2:size(valueList,1) loop
    value := val(OpenModelica.Scripting.stringVariableName(valueList[i]),start_time,outputFile);
    variableString := variableString + "," + valueList[i] + "=" + String(value);
    end for;
    
    system("myTestModel-override startTime="+String(start_time)+",stopTime="+String(stop_time)+",variableString+" -r="+outputFile);
    

相关问题