c - 是否可以将多个值从外部文件返回到 Dymola?

标签 c modelica dymola

例如,我有一个返回结构的 C 外部函数。是否可以将结构返回给 Modelica?

struct point{
   double x;
   double y;
}

struct point return_Struct(double a, double b){
    struct point pt;
    pt.x = a;
    pt.y = b;
    return pt;
};

在 Modelica 中,

function structReturn
  input Real x;
  input Real y;
  output ??????;
external"C" ????? = return_Struct(x,y)
  annotation (Include="#include <cStructReturn.c>");
end structReturn;

最佳答案

使用记录并通过引用传递它。请参阅 Modelica 规范中的 12.9.1.3 Records 部分。请注意,该记录在 Modelica 工具中的名称可能与您期望的名称不同,因此通过 void* 传递它并显式转换它。使用库而不是包含的 C 文件来隐藏接口(interface),否则代码可能无法编译。

void return_Struct(double a, double b, void* result){
    struct point *pt = result;
    pt->x = a;
    pt->y = b;
};
record R
  Real x,y;
end R;

function structReturn
  input Real x;
  input Real y;
  output R r;
external"C" return_Struct(x,y,r)
  annotation (Library="cstructreturn");
end structReturn;

但我建议传递 2 个实数作为外部函数的输出,并在 Modelica 包装函数中构造记录。

function multipleReturn
  input Real x;
  input Real y;
  output Real ox;
  output Real oy;
external"C" return_notStruct(x,y,ox,oy)
  annotation (Library="cstructreturn");
end multipleReturn;

关于c - 是否可以将多个值从外部文件返回到 Dymola?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20974365/

相关文章:

java - C 到 Java 的内存分配

modelica - 如何避免 Modelica 大输出文件

Python-Dymola 接口(interface) : How is it possible to check in real time the progress of a Dymola model?

modelica - Dymola 如何将所有方程分成不同的组?

c++ - 为什么 Malloc() 关心边界对齐?

objective-c - 打印 double 类型结构成员时 LLDB 中的奇怪行为

inheritance - Modelica - 'extends' 带下拉菜单

modelica:方程部分与算法部分的不同结果

modelica - “Variability” 函数调用模型出错

c - 初始化基本 trie 节点时不兼容的指针类型