JKRT / OMFrontend.jl

Experimental implementation of NF. That is a Modelica frontend in 100% Julia
Other
2 stars 3 forks source link

Using external libraries other than MSL #68

Open PGimenez opened 2 years ago

PGimenez commented 2 years ago

I'd like to do some tests with the Dynawo model library. If I'm correct, I'd need to put the whole library in a single file and then load it like you do in loadMSL https://github.com/JKRT/OMFrontend.jl/blob/dc6238f0bca376b85ebbbd27fabdfd37e4c87425/src/OMFrontend.jl#L102-L114

Is there any way to add packages in my ~/.openmodelica/ folder? If not, how do I export the package into a single file? Can't find anything on this except the FMU/XML/etc export options in OMEdit.

Thanks

JKRT commented 2 years ago

Hi,

Thanks for the issue @PGimenez !

Yea you are right. Currently, it can handle libraries as a single file. I got some information from Adrian, on how to do it.

A mosscript doing roughly the following should work:

loadModel(lib);
writeFile("lib.mo", list())

Maybe this option would be a nice inclusion to OMEdit, but maybe that is an issue over at https://github.com/OpenModelica/OpenModelica

I will try to make a PR that enables flattening a model in a package this week (However, the limitation of having the library in a single file will remain). It would be good to have some more libraries to test on

Kind Regards, John

JKRT commented 2 years ago

@PGimenez

To make it a single file you can use the following mos script.

Navigate to the Dynawo Modelica folder.

Execute:

loadFile("package.mo");
loadModel(Dynawo);
writeFile("Dynawo.mo", list());

I attached the resulting file, it is quite large. Dynawo.txt

The file is quite large, I changed it to a text file since Github did not allow me to upload .mo files.

I have not personally tried any model there, but the procedure should be similar to what you describe in the issue. (See some of the tests involving the MSL)

PGimenez commented 2 years ago

Thanks for the instructions and the file, I was also able to generate my own.

I did some tests with Dynawo models and none worked. I also tried with another package of mine and it did not work either. Still, I was able to instantiate models from the MSL. This is the code I'm using:

import OM, OMFrontend, OMBackend
modelFile = "Dynawopere.mo"
modelName = "Dynawo.Examples.SMIB.SMIBStepPm" 
#modelName = "Dynawo.Electrical.Sources.Converter"
#modelName = "Modelica.Blocks.Sources.RealExpression"
mode = OMBackend.MTK_MODE
p = OMFrontend.parseFile(modelFile);
scodeProgram = OMFrontend.translateToSCode(p);
(FM, cache) = OMFrontend.instantiateSCodeToFM(modelName, scodeProgram);
OMFrontend.toString(FM)

This is the error I'm getting:

julia> (FM, cache) = OMFrontend.instantiateSCodeToFM(modelName, scodeProgram);
┌ Error: Lookup error for path: Dynawo.Examples.SMIB.SMIBStepPm in the scope <top>
│        with the following error: MethodError(OMFrontend.Main.var"#491#492"(), (OMFrontend.Main.LookupTree.IMPORT(1), OMFrontend.Main.LookupTree.CLASS(6), "Types"), 0x0000000000007c64)
└ @ OMFrontend.Main ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFLookup.jl:330
ERROR: MethodError: no method matching (::OMFrontend.Main.var"#491#492")(::OMFrontend.Main.LookupTree.IMPORT, ::OMFrontend.Main.LookupTree.CLASS, ::String)
Closest candidates are:
  (::OMFrontend.Main.var"#491#492")(::Any) at ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFClassTree.jl:2283
Stacktrace:
 [1] lookupNameWithError(name::QUALIFIED, scope::OMFrontend.Main.CLASS_NODE, info::SOURCEINFO, errorType::String, checkAccessViolations::Bool)
   @ OMFrontend.Main ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFLookup.jl:332
 [2] macro expansion
   @ ~/work/omjulia/OM.jl/MetaModelica.jl/src/utilityMacros.jl:18 [inlined]
 [3] lookupClassName(name::QUALIFIED, scope::OMFrontend.Main.CLASS_NODE, info::SOURCEINFO, checkAccessViolations::Bool)
   @ OMFrontend.Main ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFLookup.jl:12
 [4] instClassInProgramFM(classPath::QUALIFIED, program::Cons{SCode.CLASS})
   @ OMFrontend.Main ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFInst.jl:59
 [5] instantiateSCodeToFM(elementToInstantiate::String, inProgram::Cons{SCode.CLASS})
   @ OMFrontend ~/work/omjulia/OM.jl/OMFrontend.jl/src/OMFrontend.jl:95
 [6] top-level scope
   @ REPL[9]:1

caused by: MethodError: no method matching (::OMFrontend.Main.var"#491#492")(::OMFrontend.Main.LookupTree.IMPORT, ::OMFrontend.Main.LookupTree.CLASS, ::String)
Closest candidates are:
  (::OMFrontend.Main.var"#491#492")(::Any) at ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFClassTree.jl:2283
Stacktrace:
  [1] add(inTree::OMFrontend.Main.LookupTree.NODE, inKey::String, inValue::OMFrontend.Main.LookupTree.IMPORT, conflictFunc::OMFrontend.Main.var"#491#492")
    @ OMFrontend.Main.LookupTree ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/LookupTree.jl:329
  [2] add(inTree::OMFrontend.Main.LookupTree.NODE, inKey::String, inValue::OMFrontend.Main.LookupTree.IMPORT, conflictFunc::OMFrontend.Main.var"#491#492")
    @ OMFrontend.Main.LookupTree ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/LookupTree.jl:327
  [3] macro expansion
    @ ~/work/omjulia/OM.jl/MetaModelica.jl/src/utilityMacros.jl:18 [inlined]
  [4] addImport(imp::OMFrontend.Main.UNRESOLVED_IMPORT, index::Int64, tree::OMFrontend.Main.LookupTree.NODE, imports::Vector{OMFrontend.Main.NFImport})
    @ OMFrontend.Main ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFClassTree.jl:2279
  [5] macro expansion
    @ ~/work/omjulia/OM.jl/MetaModelica.jl/src/utilityMacros.jl:18 [inlined]
  [6] fromSCode(elements::Cons{SCode.Element}, isClassExtends::Bool, parent::OMFrontend.Main.CLASS_NODE)
    @ OMFrontend.Main ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFClassTree.jl:1401
  [7] macro expansion
    @ ~/work/omjulia/OM.jl/MetaModelica.jl/src/utilityMacros.jl:18 [inlined]
  [8] fromSCode(elements::Cons{SCode.Element}, isClassExtends::Bool, scope::OMFrontend.Main.CLASS_NODE, prefixes::OMFrontend.Main.PREFIXES)
    @ OMFrontend.Main ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFClass.jl:982
  [9] macro expansion
    @ ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFInst.jl:221 [inlined]
 [10] macro expansion
    @ ~/work/omjulia/OM.jl/MetaModelica.jl/src/utilityMacros.jl:18 [inlined]
 [11] partialInstClass2(definition::SCode.CLASS, scope::OMFrontend.Main.CLASS_NODE)
    @ OMFrontend.Main ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFInst.jl:218
 [12] partialInstClass(node::OMFrontend.Main.CLASS_NODE)
    @ OMFrontend.Main ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFInst.jl:197
 [13] macro expansion
    @ ~/work/omjulia/OM.jl/MetaModelica.jl/src/utilityMacros.jl:18 [inlined]
 [14] expand
    @ ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFInst.jl:144 [inlined]
 [15] macro expansion
    @ ~/work/omjulia/OM.jl/MetaModelica.jl/src/utilityMacros.jl:18 [inlined]
 [16] instantiateN1(node::OMFrontend.Main.CLASS_NODE, parentNode::OMFrontend.Main.EMPTY_NODE)
    @ OMFrontend.Main ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFInst.jl:137
 [17] macro expansion
    @ ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFInst.jl:870 [inlined]
 [18] macro expansion
    @ ~/work/omjulia/OM.jl/MetaModelica.jl/src/utilityMacros.jl:18 [inlined]
 [19] instPackage(node::OMFrontend.Main.CLASS_NODE)
    @ OMFrontend.Main ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFInst.jl:858
 [20] lookupLocalName(name::QUALIFIED, node::OMFrontend.Main.CLASS_NODE, state::OMFrontend.Main.LOOKUP_STATE_PACKAGE, checkAccessViolations::Bool, selfReference::Bool)
    @ OMFrontend.Main ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFLookup.jl:410
 [21] macro expansion
    @ ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFLookup.jl:347 [inlined]
 [22] macro expansion
    @ ~/work/omjulia/OM.jl/MetaModelica.jl/src/utilityMacros.jl:18 [inlined]
 [23] lookupName(name::QUALIFIED, scope::OMFrontend.Main.CLASS_NODE, checkAccessViolations::Bool)
    @ OMFrontend.Main ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFLookup.jl:340
 [24] lookupNameWithError(name::QUALIFIED, scope::OMFrontend.Main.CLASS_NODE, info::SOURCEINFO, errorType::String, checkAccessViolations::Bool)
    @ OMFrontend.Main ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFLookup.jl:327
 [25] macro expansion
    @ ~/work/omjulia/OM.jl/MetaModelica.jl/src/utilityMacros.jl:18 [inlined]
 [26] lookupClassName(name::QUALIFIED, scope::OMFrontend.Main.CLASS_NODE, info::SOURCEINFO, checkAccessViolations::Bool)
    @ OMFrontend.Main ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFLookup.jl:12
 [27] instClassInProgramFM(classPath::QUALIFIED, program::Cons{SCode.CLASS})
    @ OMFrontend.Main ~/work/omjulia/OM.jl/OMFrontend.jl/src/NewFrontend/NFInst.jl:59
 [28] instantiateSCodeToFM(elementToInstantiate::String, inProgram::Cons{SCode.CLASS})
    @ OMFrontend ~/work/omjulia/OM.jl/OMFrontend.jl/src/OMFrontend.jl:95
 [29] top-level scope
    @ REPL[9]:1
JKRT commented 2 years ago

@PGimenez Thanks for contributing, yes, currently the frontend is more capable than the backend. The main caveat currently is that I deprecated the support for if-equations (Readding it is one of my main current priorities, see https://github.com/SciML/ModelingToolkit.jl/issues/1523), quite a few components in the MSL make use of them and since the code generation in the backend does not support it fails there for some models (In some cases the resulting if-equation can be resolved statically and it will work). The latest error, however, is a frontend issue.

I fixed some models yesterday using over-constrained connectors v0.4.1 have you tried with that version?

Is it the Dynawo.Examples.SMIB.SMIBStepPm model from the latest version of Dynawo? (Master on github?)

Maybe we should open a separate issue for this specific model, I think this issue is more about the process on how to use other libraries than the MSL.

In that way, I could spend some time trying to get part of Dynawo working:)

PGimenez commented 2 years ago

@JKRT I see what you mean about if equations. Still, I'm not sure that this is a model-specific issue as I'm not able to load anything in Dynawo. I'm getting the exact same error, even for the infinite bus "Dynawo.Electrical.Buses.InfiniteBus" which is the simplest model I could find.

I was using an older release of Dynawo, but tried with a newer one and got the same result. I also updated OMFrontend to the latest from master

If you want I can open an issue about the infinitebus, as it should be easier to get it going.

JKRT commented 2 years ago

@PGimenez Feel free to do that, but I think we shall keep this issue open. This package really needs an efficient way of composing libraries consisting of several files + also we need to be able to switch between different versions of the MSL.

I checked your error out earlier this week. It is some sort of lookup error. Sadly the frontend is not very good at printing human-readable error messages. The issue might be that the script merging everything together into one file is wrong, some library incompatibility or a programming bug in the frontend.

Please make an issue about it and I can check it out. Also, if possible, attach the library file you are using to test it with