Reputation: 103
I'm using OpenModelica 1.19.2 on Ubuntu 20.04, and I was wondering how I can create a custom periodic voltage source, with values read from an external file.
I noticed that there's the option of a TableVoltage, which takes a two-column table as input, and uses linear interpolation/extrapolation. I was hoping that this might have similar possibilities as CombiTable1D, in that you can specify a file name and extrapolation options, but that doesn't work.
Is there some way to achieve this behaviour? For example, would it be possible to generate a table using CombiTable1D and use this as an input for TableVoltage? And if so, how would I go about that? As a first guess, I tried
Modelica.Blocks.Tables.CombiTable1D myTable(fileName = "inputFile.txt", extrapolation = 3);
Modelica.Electrical.Analog.Sources.TableVoltage myVoltage(table = myTable);
but apparently that's not the right way:
Translation Error: Dimension 1 of ‘table‘ could not be deduced from the component's binding equation myTable[<myVoltage, myVoltage>]).
Upvotes: 2
Views: 165
Reputation: 7525
Using the CombiTable is a good idea as it provides a lot of functionality related to inter-/extrapolation. But you cannot pass the table object (myTable
) to TableVoltage
. Instead, usually connections are created between the objects. This sets the output myTable.y
to the desired value(*).
This is done in the following example, which you should be able to directly copy into your Modelica code editor:
model SignalSource
Modelica.Blocks.Sources.CombiTimeTable combiTimeTable(
tableOnFile=true,
tableName="voltage",
fileName="inputFile.txt",
extrapolation=Modelica.Blocks.Types.Extrapolation.Periodic)
annotation (Placement(transformation(extent={{-60,-10},{-40,10}})));
Modelica.Electrical.Analog.Sources.SignalVoltage signalVoltage
annotation (Placement(transformation(extent={{10,-10},{-10,10}}, rotation=90)));
Modelica.Electrical.Analog.Basic.Ground ground annotation (Placement(transformation(extent={{30,-40},{50,-20}})));
Modelica.Electrical.Analog.Basic.Resistor resistor(R=10)
annotation (Placement(transformation(
extent={{-10,-10},{10,10}},
rotation=270,
origin={80,0})));
equation
connect(resistor.p, signalVoltage.p) annotation (Line(points={{80,10},{80,20},{0,20},{0,10}}, color={0,0,255}));
connect(signalVoltage.n, ground.p) annotation (Line(points={{0,-10},{0,-20},{40,-20}}, color={0,0,255}));
connect(ground.p, resistor.n) annotation (Line(points={{40,-20},{80,-20},{80,-10}}, color={0,0,255}));
connect(combiTimeTable.y[1], signalVoltage.v)
annotation (Line(points={{-39,0},{-25.5,0},{-25.5,6.66134e-16},{-12,6.66134e-16}}, color={0,0,127}));
annotation (uses(Modelica(version="4.0.0")), experiment(StopTime=10));
end SignalSource;
If you then add a file inputFile.txt
with the following content:
#1
double voltage(6,2)
0 0
1 0
1 1
2 4
3 9
4 16
to the working directory(**), the model should provide the following result:
(*): An alternative to having connect statements, for this example would be adding the equation signalVoltage.v = combiTimeTable.y[1];
, but usually the graphical variant is preferred.
(**): As an alternative you can use any local path, but you need to specify it in the parameters of the combiTable.
Upvotes: 3