I'm creating an array of records dependent on the number of port connections made in a component. The actual component works fine when the number of connections is 1 or more, but does not work when no connections are made. Looking for input on how to resolve or workaround.
Here is the error:
Below is a MWE:
model RecordIssue
record Record
Real a=1;
end Record;
parameter Integer nPorts=0 annotation (Dialog(connectorSizing=true));
Modelica.Fluid.Interfaces.FluidPorts_a ports_a[nPorts];
// declare record array instance for each port connection
parameter Record[nPorts] recordInstance={Record() for i in 1:nPorts};
// this next line is an issue only when nPorts = 0 or no connections made
Real test[:]=recordInstance[:].a;
end RecordIssue;
The expected/desired behavior would be for the variable test
to not exist when nPorts=0
, which is the normal behavior for something like Real test[:] = {1.0 for i in 1:nPorts};
A workaround would be to ensure that the record variable is never empty and avoid access when nPorts
is zero.
model RecordIssue
record Record
Real a=1;
end Record;
record Dummy
extends Record(a=Modelica.Constants.inf);
end Dummy;
parameter Integer nPorts = 0 annotation (Dialog(connectorSizing=true));
Modelica.Fluid.Interfaces.FluidPorts_a ports_a[nPorts];
// declare record array and ensure that at least one exists
parameter Record[max(nPorts, 1)] recordInstance = if nPorts > 0 then {Record() for i in 1:nPorts} else {Dummy()};
// decide here if recordInstance shall be used
Real test[nPorts] = if nPorts > 0 then recordInstance[:].a else fill(0, 0);
end RecordIssue;