Function _ not found in scope _ - modelica

I am having trouble using a replaceable type parameter that defaults to the Modelica standard library 2D combi table. I am using OpenModelica 1.14.1. I am new to Modelica, so I am unsure whether what I am seeing is a bug or the result of misunderstanding.
The following code works fine (in that when I press 'Check Model' there are no translation errors):
model Error
replaceable class DefaultTable = Modelica.Blocks.Tables.CombiTable2D;
DefaultTable bob;
end Error;
However, this does not:
model Error
replaceable class DefaultTable = Modelica.Blocks.Tables.CombiTable2D;
DefaultTable bob;
DefaultTable geoff;
end Error;
The following error is obtained:
[Modelica.Blocks.Tables: 613:7-613:57]: Function Internal.getTable2DValueNoDer not found in scope DefaultTable.

That's a compiler bug: report it at https://trac.openmodelica.org/OpenModelica/newticket
It seems to work in the old frontend though (which is the default in command-line and can be turned on in OMEdit).

Related

Array Expandable Connector Issue

I'm trying to use an array expandable connector in the following way:
model MWE
expandable connector ControlBus
extends Modelica.Icons.SignalBus;
end ControlBus;
ControlBus controlBus[1];
Modelica.Blocks.Math.Gain gain(k=1);
equation
connect(gain.u, controlBus[1].a);
end MWE;
...however Dymola throws an error about not finding member 'a' in record extension.
Strangely, if i declare an integer parameter after the controlBus declaration (not before!) and use that to specify the size of the controlBus it works as expected with no errors:
model MWE
expandable connector ControlBus
extends Modelica.Icons.SignalBus;
end ControlBus;
ControlBus controlBus[k];
Modelica.Blocks.Math.Gain gain(k=1);
parameter Integer k=1;
equation
connect(gain.u, controlBus[1].a);
end MWE;
My main question is whether having an array expandable connector is allowed in Modelica? If so, any issues with my first approach or does Dymola have some bug (that apparently happens to be overcome by the workaround in the second approach)?
EDIT: adding another related example where the declaration order appears to make a difference with expandable connectors:
model MWE
expandable connector ControlBus
Real variable;
end ControlBus;
ControlBus controlBus;
Modelica.Blocks.Sources.RealExpression realExpression(y=controlBus.variable);
end MWE;
Here Dymola gives a warning on check but will compile. However, had I declared controlBus after realExpression it wouldn't have complained.
There is nothing making this example illegal in Modelica; a is an undeclared member of the expandable connector component controlBus[1].
https://specification.modelica.org/maint/3.5/connectors-and-connections.html#expandable-connectors
It seems that Dymola needs to be improved to handle this case, and assuming nothing odd happens that will be done in the next regular release.

Internal error in code generation for pre

I try to avoid the algebraic loop in Modelica by using pre operator, but when I use something like pre(x>0.5), there will be an error of Internal error in code generation for pre.
And if I use pre(cond), where cond is a boolean type variable, there won't be any error.
My question is: Is there some regulation of pre operator which requires that I could NOT use expressions within pre operator.
Here are the code and screenshot:
model WithAlgebraicLoop_Wrong2
"Demonstration of how to avoid generating algebraic loop,
but end up with internal error in code generation for pre"
Real x,y(start=1,fixed=true);
equation
when pre(x>0.5) then
y=1*time;
end when;
x=sin(y*10*time);
end WithAlgebraicLoop_Wrong2;
model WithAlgebraicLoop_Right "Demonstration of how to avoid generating algebraic loop"
Real x,y(start=1,fixed=true);
Boolean cond;
equation
cond=x>0.5;
when pre(cond) then
y=1*time;
end when;
x=sin(y*10*time);
end WithAlgebraicLoop_Right;
You can read in the Modelica Language Specification (section 3.7.3 on event related operators -> table) that the argument of pre needs to be a variable, but not an expression.

Correct syntax for redeclaration of replaceable parameters for array components

I have developed a model which uses replaceable records quite extensively. The parameters are passed down to arrays of components. I tried two approaches to do this, but neither of them work in all my test environments SimulationX (SimX), OpenModelica (OM), Dymola and Wolfram SystemModeler (WSM).
Let's consider a simplified package to illustrate my problem. Depending on the chosen sumbodel, the model calculates either the sum or the product of a replaceable record's parameters. I tried two approaches for changing the record within the sub-models.
approach 1: redeclare sub-model record by modification (MOD)
approach 2: redeclare a corresponding inner record to change the outer record within the sub-model (IO)
The package contains an example with 4 models with different record/sub-model combinations for MOD and IO each.
Both approaches work fine in SimX
Dymola correctly simulates with the MOD approach, but produces errors for IO
OM can simulate the model, but does not produce the correct results for all scenarios
WSM produces warning messages concerning the records I mentioned, but simulates the correct results.
I removed the example and annotations from the shown code for readability, but the full package can be downloaded here: https://hessenbox.tu-darmstadt.de/getlink/fiEgNjMEBZpSyJWHKafmoYYS/RedeclareTestPackage.rar
package RedeclareTestPackage "redeclarationTest"
partial record datasetPartial "NewRecord1"
parameter Real whatever;
parameter Integer idontcare;
end datasetPartial;
record datasetA "record A"
extends datasetPartial(
whatever=1.0,
idontcare=2);
end datasetA;
record datasetB "record B"
extends datasetPartial(
whatever=2.0,
idontcare=3);
end datasetB;
partial model partialSubModelType_Mod "NewModel1"
replaceable parameter datasetA subModelDataset constrainedby datasetPartial;
Real C;
end partialSubModelType_Mod;
model subModelTypeMultiply_Mod "sub-model type Multiply"
extends partialSubModelType_Mod;
equation
C=subModelDataset.whatever*subModelDataset.idontcare;
end subModelTypeMultiply_Mod;
model subModelTypeAdd_Mod "sub-model type Add"
extends partialSubModelType_Mod;
equation
C=subModelDataset.whatever+subModelDataset.idontcare;
end subModelTypeAdd_Mod;
partial model partialSubModelType_innerOuter "NewModel1"
outer replaceable parameter datasetA subModelDataset constrainedby datasetPartial;
Real C;
end partialSubModelType_innerOuter;
model subModelTypeMultiply_innerOuter "sub-model type Multiply"
extends partialSubModelType_innerOuter;
equation
C=subModelDataset.whatever*subModelDataset.idontcare;
end subModelTypeMultiply_innerOuter;
model subModelTypeAdd_innerOuter "sub-model type Add"
extends partialSubModelType_innerOuter;
equation
C=subModelDataset.whatever+subModelDataset.idontcare;
end subModelTypeAdd_innerOuter;
model modificationModel "NewModel1"
replaceable parameter datasetA modelData constrainedby datasetPartial;
replaceable subModelTypeAdd_Mod submodel[1,1] constrainedby
partialSubModelType_Mod(each subModelDataset=modelData);
Real test=submodel[1,1].C;
end modificationModel;
model innerOuterModel "NewModel1"
inner replaceable parameter datasetA subModelDataset constrainedby datasetPartial;
replaceable subModelTypeAdd_innerOuter submodel[1,1] constrainedby
partialSubModelType_innerOuter;
Real test=submodel[1,1].C;
end innerOuterModel;
end RedeclareTestPackage;
The error message in OM reads:
Translation Warning [RedeclareTestPackage: 39:3-39:84]: An inner
declaration for outer component subModelDataset could not be found and
was automatically generated.
Strangely, it is only the combination of redeclaring both the dataset and the sub-model with inner/outer which produces the message and fails to simulate correctly. Everything else works fine.
WSM produces error messages for both approaches. The MOD approach producing e.g.:
Warning: In variable or component
'MOD_ADD_1and2.submodel.subModelDataset': Duplicate modification of
element =(untyped) 1.0 and each = (typed)1.0 DAE.PROP(Real,
C_CONST), value: 1.0 on component whatever.
Warning: Duplicate
modification of element =(untyped) 1.0 and each = (typed)1.0
DAE.PROP(Real, C_CONST), value: 1.0 on component whatever.
A WSM example for the IO approach reads:
Warning: Ignoring the modification on outer element:
IO_ADD_1and2.submodel[1,1].subModelDataset .
All models simulate the correct results in WSM though. Dymola only works for the modification examples, the inner/outer redeclaration producing errors like:
Replaceable must be a subtype of constraining class. But different
inner/outer qualifiers for component subModelDataset. In declaration
of component subModelDataset: File:
C:/.../RedeclareTestPackage/RedeclareTestPackage.mo, line 39 Original
declaration of subModelDataset: File:
C:/.../RedeclareTestPackage/RedeclareTestPackage.mo, line 39 Context:
RedeclareTestPackage.partialSubModelType_innerOuter.subModelDataset
If someone could enlighten me, I would be very grateful. My actual model works just fine in SimX, but in my opinion one of the main reasons for Modelica is to develop vendor independent models, to be able to share them with a large community.
Best regards
Julian

Can a type be set globally using inner/outer and be replaceable?

Problem Description
I would like to use Non-SI-Units for time in economical modeling (e.g. System Dynamics). While of course I could go for seconds (s) and then use displayUnit there is to my knowledge no nice way to modify displayUnit for time in System Modeler, which I am mainly using.
So, writing a library I would like the user to make a choice of a global type called ModelTime which ideally would be declared as inner and replaceable at some top-level class. Then any component within a model could use the global type to consistently treat any time-related vars.
Minimal Example
The following example shows how I would like to implement this.
package Units declares two Non-SI Unit types( Time_year, Time_month)
package Interfaces contains a partial model class GenericSimulationModel which will be the top-level scope for any model written using the library. It is supposed to provide the type ModelTime as an inner and replaceable class
package Components defines a simple block class that uses ModelTime via an outer definition to define its output y that simple shows time in the globally chosen units of time
model Example ties all of this together to provide an example how any model using the library should work out
Here is the code:
model MinimalExample
package Units
type Time_year = Real(final quantity = "Time", final unit = "yr");
type Time_month = Real(final quantity = "Time", final unit = "mo");
end Units;
package Interfaces
partial model GenericSimulationModel "Top-level model scope providing global vars"
inner replaceable type ModelTime = Years "Set to : Months, Years";
protected
type Years = Units.Time_year;
type Months = Units.Time_month;
end GenericSimulationModel;
end Interfaces;
package Components
block ComponentUsingTime
outer type ModelTime = MinimalExample.Units.Time_year;
output ModelTime y;
equation
y = time;
end ComponentUsingTime;
end Components;
model Example
extends Interfaces.GenericSimulationModel(
redeclare replaceable type ModelTime = Months
);
Components.ComponentUsingTime c;
end Example;
equation
end MinimalExample;
While everything compiles without error in System Modeler and OpenModelica, it unfortunately does not work out: The redeclared type is not used within the component c in the Example model given above.
What can I do to achieve what I want to do?
I have received some feedback on Wolfram Community from someone at Wolfram MathCore (developers of the System Modeler):
The behavior you see for MinimalExample.example and MinimalLibrary.Example are bugs, and from what I can see they should work, I have forwarded them to a developer working on these things.

Connection Restrictions on Input and Output Connectors

I would like to enforce that the user cannot connect an input to an input. I expected the code below to give a compile-time error but it gives no error. How can I fix this?
Another issue is the package-global compile-time constant C. It is sort of a parameter, and it should be provided by the user of the package. How should this be implemented in Modelica?
package Pkg
constant Integer C=3;
connector Connector
Real x[C];
end Connector;
connector InConn = input Connector;
connector OutConn = output Connector;
class Base
InConn[:] inlet;
OutConn[:] outlet;
end Base;
class A
extends Base;
redeclare InConn[1] inlet;
redeclare OutConn[1] outlet;
end A;
end Pkg;
model Test
import Pkg.*;
A p;
A q;
equation
connect(p.inlet[1], q.inlet[1]);
end Test;
There are a couple of problems here. The main one is that your redeclarations in A are not correct. They should be modifications on the extends clause. But also note they are not even necessary since they don't actually change anything. Specifying sizes should be done through parameters.
Similarly, the constant really needs to be a parameter of your Connector definition. The Modelica compiler should throw an error if you connect two connectors with different sizes (specifically, it should generate an assertion on the values of any parameters in the connection set).
I don't have a Modelica compiler installed on this machine, but I suggest you try this and see if this works better for you:
package Pkg
connector Connector
parameter Integer C=3;
Real x[C];
end Connector;
connector InConn = input Connector;
connector OutConn = output Connector;
class Base
parameter Integer ni;
parameter Integer no;
InConn[ni] inlet;
OutConn[no] outlet;
end Base;
class A
extends Base(ni=1, no=1);
end A;
end Pkg;
model Test
import Pkg.*;
A p;
A q;
equation
connect(p.inlet[1], q.inlet[1]);
end Test;
Hopefully that will get things into a state where the compiler will generate the correct error. The semantics of Modelica are such that connection of two inputs should trigger an error (in fact, that is the fundamental restriction of input and output connectors).