-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Read 1D or 2D arrays #34
Comments
Here's a minimal failing example for Dymola 2021. model JSONTest "JSON file sizing test"
parameter ExternData.JSONFile jsonfile(fileName=Modelica.Utilities.Files.loadResource("modelica://ExternData/Resources/Examples/test.json")) "JSON file"
annotation(Placement(transformation(extent={{-80,60},{-60,80}})));
parameter Integer m = jsonfile.getArrayRows2D("table1") "Number of rows in 2D array";
parameter Real table[:,2] = jsonfile.getRealArray2D("table1", m, 2);
annotation(uses(ExternData));
end JSONTest; I raised this topic in modelica/ModelicaSpecification#2425 when @GarronFish from @Claytex invented a workaround to hide the C implementation of the external object in another set of C functions which are then exposed using the Modelica external function interface. Thus, the main idea was to not use external objects but rather external functions. In their case they simply acted from necessity to read array (dimensions) from XML files. @HansOlsson It would be very appreciated to get your feedback either here or in modelica/ModelicaSpecification#2425 why it is not supported in Dymola (but works in other Modelica tools) and if it can be enhanced/clarified at all. Thank you. |
Well, it works fine with JModelica (Optimica Compiler Toolkit). |
Hi, if you want to load a dynamically sized table from a json file and set this at runtime, this can be done. I've recently implemented such a table in my own json library. The work around for me was to not assign the data from the json file directly to a matrix in Modelica, but rather pass it directly to the MSL table functions in C. To achieve this, I did the following:
Usage of Dynamic table: Standard table:
|
@Marvin-TMG Thank you for sharing this. I went ahead and implemented your suggestion adapting it for ExternData JSON functions. The model compiles fine. However, I found that the custom CombiTable1D block does not initialize unless I have an instance of Modelica CombiTable1D in the model. If you faced this issue, how did you overcome this ? More details and code to replicate this issue here: |
Hi vsskanth, Yes, you are correct. I ran into that issue as well and it was quite frustrating. It has been a while since I looked into this. I have just checked and I have two example models one with tables and one without anything, both are working properly, so I believe I solved it. If I remember correctly, you need to somehow force Dymola to load the libraries needed for the CombiTable1D blocks, even if you dont actually use these blocks in your model. This is my constructor for the extern data object, maybe the annotations or the IncludeDirectory did the trick, I am not sure:
Can you somehow verify that your json object is accessible and that the table is actually calling the external C function, which should initialize the table data? The modelica format functions can help you to get some feedback from the C code, while debugging the issue. EDIT: Sorry, my assumption was incorrect. I see that your problem is different and I have the same issue.
I am not sure how to solve this, sorry. I also dont have the time to investigate further at this point. |
FYI, I am working to solve the issue originally reported by @jfulem. But I have to admit I do not understand the use case and workaround posted by @Marvin-TMG . |
The usecase is to have a variable size table which is not predefined at compile time. The work around I posted will read the data from the JSON file, directly in C with variable size determined at runtime. This is then used to initialize a modelica table directly from the external C function (by calling ModelicaStandardTables_CombiTable1D_init2). |
Thank you @Marvin-TMG for taking the time to respond. To add some more context for @tbeu: As you probably already know, Modelica array parameter sizes have to be known at compile time. This means the size of the array is fixed post-compile, even if you somehow manage to get What I am trying to do here is to be able to read 2D arrays of unknown size from JSON, mainly for use in Modelica lookup tables. Modelica components such as What @Marvin-TMG suggested is to duplicate the implementation of Modelica ExternalCombiTable1D and wrap their constructor function in C to feed in a pointer to a double array created using JSON read functions, which would then return a table object pointer of type However, this approach only works if you have atleast one other instance of Code to replicate this: https://github.com/vsskanth/ExternData.CustomTable |
OK, I see. It is solely for the purpose of lookup-tables read from Json file. This is not the main use-case of ExternData, but rather should be implemented in https://github.com/tbeu/ModelicaTableAdditions. The table blocks of ModelicaTableAdditions are drop-in replacements of the Modelica Standard Library, but with support of additional file formats (currently CSV). What do you think? |
Thanks @tbeu I didnt know this existed. ModelicaTableAdditions is exactly what I want to be able to do. I will check it out. |
Well, it would not be a big deal to add Json read support there, too. Do you use MSL 3.2.x or MSL 4.0.0? |
I use MSL 4.0.0, Dymola 2021x, Windows 10. It would be great to have JSON support as we are currently making our custom table with a dummy |
OK. Does your Json array look much different than the one on https://github.com/modelica-3rdparty/ExternData/blob/master/ExternData/Resources/Examples/test.json? |
Its the same. The code I shared uses |
OK, I've just updated https://github.com/tbeu/ModelicaTableAdditions (main branch) to read tables from JSON files. You can give it a try, if you like. |
That worked. I also added an ExternData JSON object to the test experiment to try reading the array from the same file and it works without issues. Thank you! Much appreciated. |
Yes, that should work,too, since both external objects link with the (same) parson dependency. |
Feel free to check my sponsor page at https://github.com/sponsors/tbeu. Thank you. |
* To please Dymola, these functions allocate an separate external object and have the translate annotation set. Unfortunately, this comes with the disadvantage of redundant file I/O. * Add one more test example (for XML).
Resolved in f68d4ee by introduction of separate read functions especially for Dymola: readArraySize1D, readArraySize2D, readArrayRows2D and readArrayColumns2D where applicable. |
If I take the JSON example:
Is it possible to use
table=jsonfile.getRealArray2D("table1", m, 2)
instead of(table=jsonfile.getRealArray2D("table1", 3, 2)
?It looks Dymola fails to expand the variable. Is there any workaround, please?
Thank you.
The text was updated successfully, but these errors were encountered: