1

I'm developing a Modelica library and need to produce a document with source code listings. I'd like to be able to include the source of the Modelica models without annotations.

I could manually edit them out, but I'm looking for a more automated strategy. I'm guessing the most convenient and straightforward approach is to use some tool to save .mo files with no annotations and include those in my document (I'm using \lstinputlisting in LaTeX).

Is it possible to do this? I have access to Dymola, OpenModelica and JModelica. Dymola is obviously capable of producing such a listing, as it's able to include it in the automatically generated documentation (File > Export > HTML...). I've been looking into scripting with Dymola and OpenModelica, but haven't found a way to do this either.

JModelica seems like it could be a good option, but I don't have experience working with Python. If this is possible and someone gives me some pointers, I'm willing to look into it myself. I found a mention to a prettyprint function that might do the job, but I'm not sure where to start. I can't even find reference to that function in the latest documentation.

It would also be more convenient for me to find a way of doing it with Dymola/OpenModelica (whether through the UI or by using a script). Have I missed something?

Raúl
  • 13
  • 5

1 Answers1

1

I think you could use saveTotalModel("total.mo", MyModelName) in OpenModelica. This will strip most annotations (not ones used for code generation if I remember correctly) and pretty-print the source code including all dependencies. Then you just copy-paste the models/packages that you want to include in the listing. Or if you prefer, you can do something like the following to only include code for a particular model:

loadModel(Modelica);
loadFile("MyModel.mo");
saveTotalModel("total.mo", MyModel.A.B);
clear();
loadFile(MyModel);
str := list(MyModel.A.B);
writeFile("MyModel.A.B.listing", str);
sjoelund.se
  • 3,468
  • 12
  • 15
  • Thanks for the quick reply, @sjoelund.se. I'm not sure how to implement your solution. I have [one file per model](https://github.com/raulrpearson/PVSystems). Ideally, I'd like to be able to save a complete version of the library without annotations. Were am I supposed to input these commands? I'm sorry, I'm know about scripts but I'm not familiar with them. I'm trying to input them in a Compiler CLI window I opened in OMEdit. I got this `saveTotalModel("total.mo",PVSystems.Electrical.PVArray) >> true` but I have no clue where that ended up! (`cd` returns nothing) – Raúl May 16 '17 at 17:14
  • You can use cd() as command to see where it is, or if you are on Windows it should be saved in %TEMP%\OpenModelica\OMEdit – Adrian Pop May 17 '17 at 22:13
  • Thanks a lot, @AdrianPop, indeed `cd()` worked and I found the file in the `%TEMP%` folder. I have been trying to create the results I want and found that I can create an annotation-less version of the model by using `saveTotalModel("ModelWithStuff.mo", PVSystems.Subpkg.Model)`. But that includes some extra stuff that I don't want. I then `clear()`, load _that_ version and extract the part I want with `writeFile("ModelClean.mo", list(PVSystems.Subpkg.Model))` – Raúl May 19 '17 at 15:57
  • I accepted this answer, thanks for taking the time to write it. I do think it can be improved by explaining where one can issue those commands (I used the Compiler CLI, but I'm looking into creating a script, not sure how to do that). I also struggled to understand the commands themselves, why load Modelica, where things were being saved, why the second `loadFile` has a different syntax than the first one, etc. Thanks! – Raúl May 19 '17 at 16:23
  • Here is the [resulting script](https://github.com/raulrpearson/PVSystems/blob/cc9fabdbcf81d0b37661bcf7de4f02f15f4fe478/PVSystems/Resources/Scripts/OpenModelica/PublishListings.mos) inspired by this answer. – Raúl May 19 '17 at 18:23
  • The first command is loadModel (basically loads a library from the directory pointed by the OPENMODELICALIBRARY env variable), the second is loadFile which loads an actual file (it could be a package.mo which will load a library). Put the command in a file with extension .mos and run it via command line using omc executable. Documentation of API commands is here: https://build.openmodelica.org/Documentation/OpenModelica.Scripting.html More information here (on invocation, etc): https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/ – Adrian Pop May 19 '17 at 19:41