can';t将结果从JModelica检索回Python
接下来,我尝试用JModelica编译和模拟Modelica模型。模型是:can';t将结果从JModelica检索回Python,python,modelica,jmodelica,pymodelica,pyfmi,Python,Modelica,Jmodelica,Pymodelica,Pyfmi,接下来,我尝试用JModelica编译和模拟Modelica模型。模型是: package friction1D function coulombFriction input Real relVel; input Real shearForce; input Real normalForce; input Real statfricco; input Real kinfricco; input Real inertia; input
package friction1D
function coulombFriction
input Real relVel;
input Real shearForce;
input Real normalForce;
input Real statfricco;
input Real kinfricco;
input Real inertia;
input Real relAcc;
output Real fricForce;
algorithm
if (relVel == 0) and (abs(shearForce - inertia * relAcc) < statfricco * normalForce) then
fricForce := shearForce - inertia * relAcc;
else
fricForce := kinfricco * normalForce * sign(relVel);
end if;
end coulombFriction;
model fricexample_1
//parameters
parameter Real kco = 0.3;
parameter Real sco = 0.4;
parameter Real nfo = 1.0;
parameter Real mass = 1;
Real sfo;
Real ffo;
Real x;
Real v;
initial equation
x = 0;
v = 0;
algorithm
sfo := 1 * sin(time);
equation
v = der(x);
mass * der(v) = sfo - ffo;
ffo = coulombFriction(relVel = v, shearForce = sfo, normalForce = nfo, statfricco = sco, kinfricco = kco, inertia = mass, relAcc = der(v));
annotation(
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-8, Interval = 0.02),
__OpenModelica_simulationFlags(lv = "LOG_STATS", outputFormat = "mat", s = "dassl"));end fricexample_1;
end friction1D;
当我试图传回结果时,time=res['time']
命令似乎工作正常,但对于所有其他变量,例如,vel=res['v']
,它返回以下错误:
---------------------------------------------------------------------------
VariableNotFoundError Traceback (most recent call last)
<ipython-input-19-a29270f8b1ab> in <module>()
----> 1 vel = res['v']
C:\Users\foobar\AppData\Roaming\JModelica.org-2.10\install\Python_64\pyfmi\common\algorithm_drivers.pyc in __getitem__(self, key)
176 Name of the variable/parameter/constant.
177 """
--> 178 val_x = self.result_data.get_variable_data(key).x
179
180 if self.result_data.is_variable(key):
C:\Users\foobar\AppData\Roaming\JModelica.org-2.10\install\Python_64\pyfmi\common\io.pyc in get_variable_data(self, name)
1160 varInd = 0;
1161 else:
-> 1162 varInd = self.get_variable_index(name)
1163
1164 dataInd = self.raw['dataInfo'][1][varInd]
C:\Users\foobar\AppData\Roaming\JModelica.org-2.10\install\Python_64\pyfmi\common\io.pyc in get_variable_index(self, name)
151 else:
152 raise VariableNotFoundError("Cannot find variable " +
--> 153 name + " in data file.")
154
155
VariableNotFoundError: Cannot find variable v in data file.
---------------------------------------------------------------------------
VariableNotFoundError回溯(上次最近的调用)
在()
---->1级=分辨率['v']
C:\Users\foobar\AppData\Roaming\JModelica.org-2.10\install\Python\u 64\pyfmi\common\algorithm\u drivers.pyc in uuu getitem_uuu(self,key)
176变量/参数/常量的名称。
177 """
-->178 val_x=self.result_data.get_variable_data(key).x
179
180如果self.result\u data.是变量(键):
获取变量数据(self,name)中的C:\Users\foobar\AppData\Roaming\JModelica.org-2.10\install\Python\u 64\pyfmi\common\io.pyc
1160变量=0;
1161其他:
->1162 varInd=self.get\u variable\u index(名称)
1163
1164 dataInd=self.raw['dataInfo'][1][varInd]
获取变量索引(self,name)中的C:\Users\foobar\AppData\Roaming\JModelica.org-2.10\install\Python\u 64\pyfmi\common\io.pyc
151其他:
152 raise VariableNotFoundError(“找不到变量”)+
-->153名称+“在数据文件中。”)
154
155
VariableNotFoundError:在数据文件中找不到变量v。
如果您能帮助我了解问题所在以及我如何解决问题,我将不胜感激
p.S.1.我也在网上发布了这个问题
p.S.2.我认为问题的出现是因为我正在解决一个包。如果模拟一个简单的模型,它可以检索变量
p.S.3.我想我解决了这个问题。行
model\u name='friction1D'
需要更改为model\u name='friction1D.fricecexample\u 1'
。基本上,它应该是
检查模型。获取模型变量(),以获取FMU中公开的变量列表(并查看是否有“v”)@ChristianWinther尝试了,但得到了错误AttributeError:“FMIResult”对象没有属性“get\u model\u variables”
!我尝试了sim.get\u model\u variables()
而不是res.get\u model\u variables()
,得到了一个神秘的变量列表,例如>),(“block\u jacobian\u check\u tol'),我尝试了res.keys()
,但它没有包含.mo
文件中的任何变量名!您好,它确实显示了模型变量,只是您没有编译包含任何变量的模型(如您在第3页中所述)