2016-08-24 66 views
1

我有一个Modelica文件,在仿真期间通过外部库* .a文件引用c代码。在JModelica中引用外部文件

例如:

model CallAdd 
    input Real FirstInput(start=0); 
    input Real SecondInput(start=0); 
    output Real FMUOutput(start=0); 
    function CAdd 
     input Real x(start=0); 
     input Real y(start=0); 
     output Real z(start=0); 
     external "C" annotation(Library = "CAdd", LibraryDirectory = "modelica://CallAdd"); 
    end CAdd; 

equation 
    FMUOutput = CAdd(FirstInput,SecondInput); 
    annotation(uses(Modelica(version = "3.2.1"))); 
end CallAdd; 

当打开在OpenModelica的Modelica的模型所需的文件似乎被自动加载,因为它模拟,并给出相应的结果。

但是,当我尝试使用JModelica-SDK-1.12编译Modelica文件时,收到无法找到库* .a文件的错误。

所以我的问题是:在JModelica中使用compile_fmu时引用其他文件的正确方法是什么?

没有成功,我已经试过:

# Import the compiler function 
from pymodelica import compile_fmu 
model_name = "CallAdd" 
mo_file = "CallAdd.mo" 

# Compile the model and save the return argument, for use later if wanted 
my_fmu = compile_fmu(model_name, mo_file, target="cs",compiler_options = {'extra_lib_dirs':'C:/ToFolderContainingLib/'}) 

奇怪的是,当我使用JModelica-1.17(非SDK)文件编译罚款,但结果并没有什么意义。我建议尝试SDK版本,看看它是否修复了我之前发布的文章here中的错误。

回答

2

尝试将外部库定位到名为您当前所在平台的子文件夹中。因此,在您的示例中,我将库(libCAdd.a)放在名为linux64的子文件夹中,因为我在64位Linux机器上,然后运行代码。

+0

嗨克里斯蒂安安德森,似乎已经解决了这个问题。非常感谢你! –

2

如果是一小片的C代码,作为最后的选择,你可以尝试包括C文件直接在Modelica的代码:

external "C" annotation(Include=" 
// the entire C code here 
"); 

希望的JModelica人将尽快给你一个更好的答案。 您也可以尝试在其网站上询问此问题: http://www.jmodelica.org/forum

+0

谢谢你的快速回复。我会试一试。当我尝试将C代码直接放入Modelica文件中时,我收到了“未定义的函数参考”等错误。换句话说,当模型被实例化时,该功能就会丢失。作为参考:https://www.dropbox.com/s/lrz2sy0yics80w5/CallDirect.mo?dl=0 –

+0

我似乎已经明白了。我相信C函数名称需要匹配Modelica函数名称的名称,是否正确? 在C和Modelica之间工作时,我应该遵循一个语法。我发现有限的信息? Xogeny书(http://book.xogeny.com/behavior/functions/external/)和这个互操作性文章(https://www.openmodelica.org/doc/OpenModelicaUsersGuide/latest/interop_c_python.html)一直是最好的我迄今为止发现的资源。但调试通常需要更深入的信息。感谢您的时间。 –