-
Notifications
You must be signed in to change notification settings - Fork 40
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
Clarify/Allow ExternalObject (member) functions to be used for (flexible) array parameters/dimensions #2425
Comments
@HansOlsson Any feedback please? Thanks. |
Just some quick feedback (will try to more completely review Modelica issues in a bit). To me it seems odd that an ExternalObject object in the model is used to evaluate a structural parameter, as that implies that the ExternalObject functions (that may have side-effects and/or modifying the ExternalObject) are called outside of the model evaluation. |
In the presented case the external object only is used as cache for the read/parsed file. The workaround would be to use external functions only and hide the caching in global variables together with thread-safe access. To me this looks similiarly as the old Dymola tables implementation (of MSL <= 3.2.1) compared to the existing implementation based on external objects with dedicated construction and destruction. Additionally in MSL, we cache the opened files for reading in ModelicaInternal implementation. |
The problem is that if you see:
you don't know whether calling SomeFun modifies the external object or if it depends on something truly external. But I guess you could include it and an assertion for checking if it had been modified. If that is hidden in a function you can be sure that any modification of the external object is intended to be lost.
|
Can't we use pure/impure to indicate these cases? |
Possibly. We might need to ensure that impure is sufficiently clear for functions taking external objects. |
Does Dymola call the destructor directly after calling foo? |
At the end of foo as I recall. |
As far I can see it is already stated that tools don't have to translate these.
|
Let's assume the external object construction of |
To clarify: My conclusion is that the current specification states neither of the variants need to be translated, as both involve external functions. So it is an enhancement; not something unclear. |
@HansOlsson I just noticed that the milestone was moved. Back to my original issue and my repeated tries to make it somehow working in Dymola. You mentioned example
But what the customer needs are dynamic array sizes read from file and used as parameters. However, if these dimensions are inputs to the function, I still do not get it running in Dymola 2021x. // Fails in Dymola 2021x
model SizingExampleXML3
constant String hString = "multiTank.tankHeights" "XML element name";
constant String fName = "C:\\temp\\multiTank1.xml" "XML file name";
final parameter Integer n = readArraySize1D(hString, fName) "Array size";
parameter Real heightVector[n] = readRealArray1D(hString, fName, n) "Array parameter";
function readArraySize1D "Read array size"
input String hString "XML element name";
input String fName "XML file name";
output Integer n "Array size";
protected
ExternData.Types.ExternXMLFile extObj = ExternData.Types.ExternXMLFile(fName, verboseRead=true) "External XML file object";
algorithm
n := ExternData.Functions.XML.getArraySize1D(hString, extObj);
end readArraySize1D;
function readRealArray1D "Read array"
input String hString "XML element name";
input String fName "XML file name";
input Integer n "Array size";
output Real arr[n] "Array";
protected
ExternData.Types.ExternXMLFile extObj = ExternData.Types.ExternXMLFile(fName, verboseRead=true) "External XML file object";
algorithm
arr := ExternData.Functions.XML.getRealArray1D(hString, n, extObj);
end readRealArray1D;
annotation(uses(ExternData(version="2.6.1")));
end SizingExampleXML3; Edit: See also #1811 and modelica/ModelicaStandardLibrary#1856 (comment). 2nd Edit: Applying |
Citing myself from ^^
It seems that Dymola 2025x users will benefit from a new tool-specific annotation |
Based on the technical blog post from @Claytex, it is currently either tool-specific or unspecified, if ExternalObject (member) functions can be used for flexible array sizes (as described in section 12.4.5 of the Modelica Language Specification v3.4).
The below example demonstrates what @GarronFish's intention is, i.e., to read a vector of unknown dimension from an XML file and use it as parameter.
with
This example fails in Dymola 2020 with
but runs in SimulationX 4.0.
@GarronFish's workaround was to wrap the C code of the external object in another C functions to be used as Modelica functions for flexible array sizes. (In his case, problems with thread-safety were introduced when using global variables for one-time construction and destruction of the external object. This of course could be improved, but I'd claim, that it would be even better, if no such workaround would be necessary at all.)
Please clarify, if ExternalObject (member) functions can be used for parameter expressions of flexible array size.
The text was updated successfully, but these errors were encountered: