gpt4 book ai didi

modelica - 如何在 Dymola 中查找哪个组件正在使用外部组件?

转载 作者:行者123 更新时间:2023-12-04 08:13:03 25 4
gpt4 key购买 nike

在Modelica模型中,我们经常会用到外部组件,比如系统设置等常用变量,但是如果我正在读取一个新的模型,有没有什么简单的方法可以让我找到哪个组件使用了外部组件?
例如,以下屏幕截图是 Modelica.Fluid.Examples.HeatingSystem,我怎么知道哪个组件使用“系统”作为外部组件?
我可以逐行阅读代码,但是有更简单的方法吗?
enter image description here

最佳答案

Dymola 在 gui 中没有提供这样的功能。但随着 ModelManagement图书馆有可能获得此类信息。该库可通过标准 Dymola 许可证获得并已预安装。
您可以在下面找到函数 SO.printComponents()它使用 ModelManagement 库来获取符合给定条件的类中的所有组件。
对于您的模型,打印输出为:

Components in Modelica.Fluid.Examples.HeatingSystem using Modelica.Fluid.System as outer:
tank<Modelica.Fluid.Vessels.BaseClasses.PartialLumpedVessel>.heatTransfer<Modelica.Fluid.Interfaces.PartialHeatTransfer>.system
tank<Modelica.Fluid.Interfaces.PartialLumpedVolume>.system
pump<Modelica.Fluid.Machines.BaseClasses.PartialPump>.heatTransfer<Modelica.Fluid.Interfaces.PartialHeatTransfer>.system
...
valve<Modelica.Fluid.Interfaces.PartialTwoPort>.system
...
您可以在 Modelica.Fluid.Examples.HeatingSystem 中看到所有组件其中包含 Modelica.Fluid.System 的实例带有前缀外部。
如果组件通过 extends 从基类继承系统实例, 基类以 <base-class> 形式在尖括号之间给出.
例如,输出中的第一个组件意味着 tank组件正在扩展 PartialLumpedVessel类,其中包含一个组件 heatTransfer它扩展了 PartialHeatTransfer其中包含我们正在寻找的系统实例。
请注意,对于多级继承,只给出了最终的基类,而不是它的完整路径。这是例如与阀门相关,它从 PartialTwoPort 继承系统实例通过 PartialValvePartialTwoPortTransport .
背景资料
ModelManagement 库在 ModelManagement.Structure 包中具有一些有趣的功能。它允许获取有关 Modelica 类的信息。
功能分为两种不同的类型:
  • ModelManagement.Structure.AST:对于这些函数,只考虑所选类的 Modelica 代码。不考虑继承的所有内容。
  • ModelManagement.Structure.Instantiated:此包中的所有函数都在翻译后的模型上运行

  • 第一眼 ModelManagement.Structure.Instantiated.UsedModels似乎是一个很好的起点。但它的文档说:

    Optionally disabled components, base-classes, and called functions are not included.


    由于 Fluid 包大量使用继承,如果不考虑继承,我们很容易错过一个组件。
    幸运的是,Testing 库包含一个函数,该函数返回所有继承的基类 Testing.Utilities.Class.getExtendedClasses .另外 ModelManagement.Structure.AST.ComponentsInClassAttributes允许我们检索给定类的所有组件(但不是继承的)。
    使用这两个函数,我们可以构建一个新函数:
  • 检索类的所有组件,包括从基类继承的组件
  • 检查组件是否符合我们的搜索条件(在您的情况下,它应该是 Modelica.Fluid.System 的外部实例)
  • 递归检查所有组件的子组件,因为系统的使用可能发生在层次结构的更下方

  • 这是代码。此外,还包括一个小型打印功能,可将找到的组件很好地打印到终端。
    package SO

    function getComponentsUsingClass "Get all components in class c1 which use class c2 as local component"
    import ModelManagement.Structure.AST;
    import Testing.Utilities.Class.getExtendedClasses;
    import Testing.Utilities.Vectors.catStrings;

    input String c1 "Full path of class of which components are retrieved";
    input String c2 "Full path of class which must be used in the components";
    input Boolean isOuter=false "Return only components where c2 is used as an outer instance";
    output String cmp[:] "All components in c1 using c2";
    protected
    String sub[:] "Sub-components";
    Boolean baseClass "True if current class is a baseClass of c1 (c1 extends it)";
    String prefix "Used to give base classe";
    algorithm

    cmp :=fill("", 0);

    // loop over class c1 and all its base classes
    for c in cat(1,{c1}, getExtendedClasses(c1)) loop

    baseClass :=c <> c1;
    prefix :=if baseClass then "<" + c + ">." else ".";

    // loop over all components in the current class
    for cmpa in AST.ComponentsInClassAttributes(c) loop

    if cmpa.fullTypeName==c2 and cmpa.isOuter==isOuter then
    cmp :=cat(1, cmp, {prefix+cmpa.name});
    end if;

    // if the current component is a Modelica class, obtain all sub-components using c2
    // (Mon-Modelica classes would e.g. be the attributes min, max, stateSelect etc. of built in classes)
    if cmpa.fullTypeName<>"" then
    sub := getComponentsUsingClass(cmpa.fullTypeName, c2, isOuter);
    sub :=catStrings(fill(prefix+cmpa.name, size(sub, 1)), sub);
    cmp :=cat(1, cmp, sub);
    end if;
    end for;
    end for;
    end getComponentsUsingClass;

    function printComponents
    import Modelica.Utilities.Streams.print;
    import DymolaModels.Utilities.Strings.stripLeft;
    input String c1="Modelica.Fluid.Examples.HeatingSystem" "Full path of class of which components are retrieved";
    input String c2="Modelica.Fluid.System" "Full path of class which must be used in the components";
    input Boolean isOuter=true "Return only components where c2 is used as an outer instance";
    algorithm

    print("Components in " + c1 + " using " + c2 + (if isOuter then " as outer" else "") + ":");

    for c in getComponentsUsingClass(c1, c2,isOuter) loop
    print(" " + stripLeft(c, "."));
    end for;
    end printComponents;

    annotation (uses(
    Modelica(version="4.0.0"),
    Testing(version="1.3.1"),
    ModelManagement(version="1.2.0"),
    DymolaModels(version="1.2")));
    end SO;

    关于modelica - 如何在 Dymola 中查找哪个组件正在使用外部组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65857118/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com