| 1 | within Modelica.Blocks; |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | package Tables |
|---|
| 5 | Â "Library of blocks to interpolate in one and two-dimensional tables" |
|---|
| 6 |  extends Modelica.Icons.Library; |
|---|
| 7 |  model CombiTable1D |
|---|
| 8 | Â Â "Table look-up in one dimension (matrix/file) with n inputs and n outputs " |
|---|
| 9 |   import Modelica.Blocks.Types; |
|---|
| 10 |   parameter Boolean tableOnFile=false |
|---|
| 11 | Â Â Â "true, if table is defined on file or in function usertab"Â |
|---|
| 12 | Â Â Â annotation(Dialog(group="table data definition")); |
|---|
| 13 |   parameter Real table[:, :]=fill(0.0,0,2) |
|---|
| 14 | Â Â Â "table matrix (grid = first column; e.g., table=[0,2])"Â |
|---|
| 15 |      annotation(Dialog(group="table data definition", enable = not tableOnFile)); |
|---|
| 16 |   parameter String tableName="NoName" |
|---|
| 17 | Â Â Â "table name on file or in function usertab (see docu)"Â |
|---|
| 18 |      annotation(Dialog(group="table data definition", enable = tableOnFile)); |
|---|
| 19 |   parameter String fileName="NoName" "file where matrix is stored" |
|---|
| 20 |      annotation(Dialog(group="table data definition", enable = tableOnFile, |
|---|
| 21 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â __Dymola_loadSelector(filter="Text files (*.txt);;Matlab files (*.mat)", |
|---|
| 22 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â caption="Open file in which table is present"))); |
|---|
| 23 |   parameter Integer columns[:]=2:size(table, 2) |
|---|
| 24 | Â Â Â "columns of table to be interpolated"Â |
|---|
| 25 | Â Â annotation(Dialog(group="table data interpretation")); |
|---|
| 26 |   parameter Modelica.Blocks.Types.Smoothness smoothness=Types.Smoothness.LinearSegments |
|---|
| 27 | Â Â Â "smoothness of table interpolation"Â |
|---|
| 28 | Â Â annotation(Dialog(group="table data interpretation")); |
|---|
| 29 |   extends Modelica.Blocks.Interfaces.MIMOs(final n=size(columns, 1)); |
|---|
| 30 |   annotation ( |
|---|
| 31 | Â Â Â Documentation(info="<html> |
|---|
| 32 | <p> |
|---|
| 33 | <b>Linear interpolation</b>Â in <b>one</b>Â dimension of a <b>table</b>. |
|---|
| 34 | Via parameter <b>columns</b>Â it can be defined how many columns of the |
|---|
| 35 | table are interpolated. If, e.g., columns={2,4}, it is assumed that 2 input |
|---|
| 36 | and 2 output signals are present and that the first output interpolates |
|---|
| 37 | the first input via column 2 and the second output interpolates the |
|---|
| 38 | second input via column 4 of the table matrix. |
|---|
| 39 | </p> |
|---|
| 40 | <p> |
|---|
| 41 | The grid points and function values are stored in a matrix \"table[i,j]\", |
|---|
| 42 | where the first column \"table[:,1]\" contains the grid points and the |
|---|
| 43 | other columns contain the data to be interpolated. Example: |
|---|
| 44 | </p> |
|---|
| 45 | <pre> |
|---|
| 46 |   table = [0, 0; |
|---|
| 47 |       1, 1; |
|---|
| 48 |       2, 4; |
|---|
| 49 | Â Â Â Â Â Â 4, 16] |
|---|
| 50 | Â Â If, e.g., the input u = 1.0, the output y =Â 1.0, |
|---|
| 51 | Â Â Â Â e.g., the input u = 1.5, the output y =Â 2.5, |
|---|
| 52 | Â Â Â Â e.g., the input u = 2.0, the output y =Â 4.0, |
|---|
| 53 | Â Â Â Â e.g., the input u =-1.0, the output y = -1.0 (i.e. extrapolation). |
|---|
| 54 | </pre> |
|---|
| 55 | <ul> |
|---|
| 56 | <li>Â The interpolation is <b>efficient</b>, because a search for a new interpolation |
|---|
| 57 | Â Â Â starts at the interval used in the last call.</li> |
|---|
| 58 | <li>Â If the table has only <b>one row</b>, the table value is returned, |
|---|
| 59 | Â Â Â independent of the value of the input signal.</li> |
|---|
| 60 | <li>Â If the input signal <b>u[i]</b>Â is <b>outside</b>Â of the defined <b>interval</b>, i.e., |
|---|
| 61 | Â Â Â u[i] >Â table[size(table,1),i+1] or u[i] <Â table[1,1], the corresponding |
|---|
| 62 | Â Â Â value is also determined by linear |
|---|
| 63 | Â Â Â interpolation through the last or first two points of the table.</li> |
|---|
| 64 | <li>Â The grid values (first column) have to be <b>strict</b> |
|---|
| 65 | Â Â Â monotonically increasing.</li> |
|---|
| 66 | </ul> |
|---|
| 67 | <p> |
|---|
| 68 | The table matrix can be defined in the following ways: |
|---|
| 69 | </p> |
|---|
| 70 | <ol> |
|---|
| 71 | <li>Â Explicitly supplied as <b>parameter matrix</b>Â \"table\", |
|---|
| 72 | Â Â Â and the other parameters have the following values: |
|---|
| 73 | <pre> |
|---|
| 74 | Â Â tableName is \"NoName\" or has only blanks, |
|---|
| 75 |   fileName is \"NoName\" or has only blanks. |
|---|
| 76 | </pre></li> |
|---|
| 77 | <li>Â <b>Read</b>Â from a <b>file</b>Â \"fileName\" where the matrix is stored as |
|---|
| 78 | Â Â Â \"tableName\". Both ASCII and binary file format is possible. |
|---|
| 79 | Â Â Â (the ASCII format is described below). |
|---|
| 80 | Â Â Â It is most convenient to generate the binary file from Matlab |
|---|
| 81 | Â Â Â (Matlab 4 storage format), e.g., by command |
|---|
| 82 | <pre> |
|---|
| 83 | Â Â save tables.mat tab1 tab2 tab3 -V4 |
|---|
| 84 | </pre> |
|---|
| 85 | Â Â Â when the three tables tab1, tab2, tab3 should be |
|---|
| 86 | Â Â Â used from the model.</li> |
|---|
| 87 | <li>Â Statically stored in function \"usertab\" in file \"usertab.c\". |
|---|
| 88 | Â Â Â The matrix is identified by \"tableName\". Parameter |
|---|
| 89 | Â Â Â fileName = \"NoName\" or has only blanks.</li> |
|---|
| 90 | </ol> |
|---|
| 91 | <p> |
|---|
| 92 | Table definition methods (1) and (3) do <b>not</b>Â allocate dynamic memory, |
|---|
| 93 | and do not access files, whereas method (2) does. Therefore (1) and (3) |
|---|
| 94 | are suited for hardware-in-the-loop simulation (e.g. with dSpace hardware). |
|---|
| 95 | When the constant \"NO_FILE\" is defined in \"usertab.c\", all parts of the |
|---|
| 96 | source code of method (2) are removed by the C-preprocessor, such that |
|---|
| 97 | no dynamic memory allocation and no access to files takes place. |
|---|
| 98 | </p> |
|---|
| 99 | <p> |
|---|
| 100 | If tables are read from an ASCII-file, the file need to have the |
|---|
| 101 | following structure (\"-----\" is not part of the file content): |
|---|
| 102 | </p> |
|---|
| 103 | <pre> |
|---|
| 104 | ----------------------------------------------------- |
|---|
| 105 | #1 |
|---|
| 106 | double tab1(5,2)Â Â # comment line |
|---|
| 107 | Â 0Â Â 0 |
|---|
| 108 | Â 1Â Â 1 |
|---|
| 109 | Â 2Â Â 4 |
|---|
| 110 | Â 3Â Â 9 |
|---|
| 111 | Â 4Â 16 |
|---|
| 112 | double tab2(5,2)Â Â # another comment line |
|---|
| 113 | Â 0Â Â 0 |
|---|
| 114 | Â 2Â Â 2 |
|---|
| 115 | Â 4Â Â 8 |
|---|
| 116 | Â 6Â 18 |
|---|
| 117 | Â 8Â 32 |
|---|
| 118 | ----------------------------------------------------- |
|---|
| 119 | </pre> |
|---|
| 120 | <p> |
|---|
| 121 | Note, that the first two characters in the file need to be |
|---|
| 122 | \"#1\". Afterwards, the corresponding matrix has to be declared |
|---|
| 123 | with type, name and actual dimensions. Finally, in successive |
|---|
| 124 | rows of the file, the elements of the matrix have to be given. |
|---|
| 125 | Several matrices may be defined one after another. |
|---|
| 126 | </p> |
|---|
| 127 | </HTML> |
|---|
| 128 | "),  Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,-100},{100, |
|---|
| 129 |        100}}), graphics={ |
|---|
| 130 | Â Â Â Â Â Line(points={{-60,40},{-60,-40},{60,-40},{60,40},{30,40},{30,-40},{-30, |
|---|
| 131 | Â Â Â Â Â Â Â Â -40},{-30,40},{-60,40},{-60,20},{60,20},{60,0},{-60,0},{-60,-20}, |
|---|
| 132 |         {60,-20},{60,-40},{-60,-40},{-60,40},{60,40},{60,-40}}, color={ |
|---|
| 133 | Â Â Â Â Â Â Â Â 0,0,0}), |
|---|
| 134 |      Line(points={{0,40},{0,-40}}, color={0,0,0}), |
|---|
| 135 | Â Â Â Â Â Rectangle( |
|---|
| 136 | Â Â Â Â Â Â extent={{-60,40},{-30,20}}, |
|---|
| 137 | Â Â Â Â Â Â lineColor={0,0,0}, |
|---|
| 138 | Â Â Â Â Â Â fillColor={255,255,0}, |
|---|
| 139 | Â Â Â Â Â Â fillPattern=FillPattern.Solid), |
|---|
| 140 | Â Â Â Â Â Rectangle( |
|---|
| 141 | Â Â Â Â Â Â extent={{-60,20},{-30,0}}, |
|---|
| 142 | Â Â Â Â Â Â lineColor={0,0,0}, |
|---|
| 143 | Â Â Â Â Â Â fillColor={255,255,0}, |
|---|
| 144 | Â Â Â Â Â Â fillPattern=FillPattern.Solid), |
|---|
| 145 | Â Â Â Â Â Rectangle( |
|---|
| 146 | Â Â Â Â Â Â extent={{-60,0},{-30,-20}}, |
|---|
| 147 | Â Â Â Â Â Â lineColor={0,0,0}, |
|---|
| 148 | Â Â Â Â Â Â fillColor={255,255,0}, |
|---|
| 149 | Â Â Â Â Â Â fillPattern=FillPattern.Solid), |
|---|
| 150 | Â Â Â Â Â Rectangle( |
|---|
| 151 | Â Â Â Â Â Â extent={{-60,-20},{-30,-40}}, |
|---|
| 152 | Â Â Â Â Â Â lineColor={0,0,0}, |
|---|
| 153 | Â Â Â Â Â Â fillColor={255,255,0}, |
|---|
| 154 | Â Â Â Â Â Â fillPattern=FillPattern.Solid)}), |
|---|
| 155 |    Diagram(coordinateSystem(preserveAspectRatio=true, extent={{-100,-100},{ |
|---|
| 156 |        100,100}}), graphics={ |
|---|
| 157 | Â Â Â Â Â Rectangle( |
|---|
| 158 | Â Â Â Â Â Â extent={{-60,60},{60,-60}}, |
|---|
| 159 | Â Â Â Â Â Â fillColor={235,235,235}, |
|---|
| 160 | Â Â Â Â Â Â fillPattern=FillPattern.Solid, |
|---|
| 161 | Â Â Â Â Â Â lineColor={0,0,255}), |
|---|
| 162 |      Line(points={{-100,0},{-58,0}}, color={0,0,255}), |
|---|
| 163 |      Line(points={{60,0},{100,0}}, color={0,0,255}), |
|---|
| 164 | Â Â Â Â Â Text( |
|---|
| 165 | Â Â Â Â Â Â extent={{-100,100},{100,64}}, |
|---|
| 166 | Â Â Â Â Â Â textString="1 dimensional linear table interpolation", |
|---|
| 167 | Â Â Â Â Â Â lineColor={0,0,255}), |
|---|
| 168 | Â Â Â Â Â Line(points={{-54,40},{-54,-40},{54,-40},{54,40},{28,40},{28,-40},{-28, |
|---|
| 169 | Â Â Â Â Â Â Â Â -40},{-28,40},{-54,40},{-54,20},{54,20},{54,0},{-54,0},{-54,-20}, |
|---|
| 170 |         {54,-20},{54,-40},{-54,-40},{-54,40},{54,40},{54,-40}}, color={ |
|---|
| 171 | Â Â Â Â Â Â Â Â 0,0,0}), |
|---|
| 172 |      Line(points={{0,40},{0,-40}}, color={0,0,0}), |
|---|
| 173 | Â Â Â Â Â Rectangle( |
|---|
| 174 | Â Â Â Â Â Â extent={{-54,40},{-28,20}}, |
|---|
| 175 | Â Â Â Â Â Â lineColor={0,0,0}, |
|---|
| 176 | Â Â Â Â Â Â fillColor={255,255,0}, |
|---|
| 177 | Â Â Â Â Â Â fillPattern=FillPattern.Solid), |
|---|
| 178 | Â Â Â Â Â Rectangle( |
|---|
| 179 | Â Â Â Â Â Â extent={{-54,20},{-28,0}}, |
|---|
| 180 | Â Â Â Â Â Â lineColor={0,0,0}, |
|---|
| 181 | Â Â Â Â Â Â fillColor={255,255,0}, |
|---|
| 182 | Â Â Â Â Â Â fillPattern=FillPattern.Solid), |
|---|
| 183 | Â Â Â Â Â Rectangle( |
|---|
| 184 | Â Â Â Â Â Â extent={{-54,0},{-28,-20}}, |
|---|
| 185 | Â Â Â Â Â Â lineColor={0,0,0}, |
|---|
| 186 | Â Â Â Â Â Â fillColor={255,255,0}, |
|---|
| 187 | Â Â Â Â Â Â fillPattern=FillPattern.Solid), |
|---|
| 188 | Â Â Â Â Â Rectangle( |
|---|
| 189 | Â Â Â Â Â Â extent={{-54,-20},{-28,-40}}, |
|---|
| 190 | Â Â Â Â Â Â lineColor={0,0,0}, |
|---|
| 191 | Â Â Â Â Â Â fillColor={255,255,0}, |
|---|
| 192 | Â Â Â Â Â Â fillPattern=FillPattern.Solid), |
|---|
| 193 | Â Â Â Â Â Text( |
|---|
| 194 | Â Â Â Â Â Â extent={{-50,54},{-32,42}}, |
|---|
| 195 | Â Â Â Â Â Â textString="u[1]/[2]", |
|---|
| 196 | Â Â Â Â Â Â lineColor={0,0,255}), |
|---|
| 197 | Â Â Â Â Â Text( |
|---|
| 198 | Â Â Â Â Â Â extent={{-24,54},{0,42}}, |
|---|
| 199 | Â Â Â Â Â Â textString="y[1]", |
|---|
| 200 | Â Â Â Â Â Â lineColor={0,0,255}), |
|---|
| 201 | Â Â Â Â Â Text( |
|---|
| 202 | Â Â Â Â Â Â extent={{-2,-40},{30,-54}}, |
|---|
| 203 | Â Â Â Â Â Â textString="columns", |
|---|
| 204 | Â Â Â Â Â Â lineColor={0,0,255}), |
|---|
| 205 | Â Â Â Â Â Text( |
|---|
| 206 | Â Â Â Â Â Â extent={{2,54},{26,42}}, |
|---|
| 207 | Â Â Â Â Â Â textString="y[2]", |
|---|
| 208 | Â Â Â Â Â Â lineColor={0,0,255})})); |
|---|
| 209 | |
|---|
| 210 | Â protected |
|---|
| 211 |   Integer tableID; |
|---|
| 212 | |
|---|
| 213 |   function tableInit |
|---|
| 214 |    input String tableName; |
|---|
| 215 |    input String fileName; |
|---|
| 216 |    input Real table[ :, :]; |
|---|
| 217 |    input Modelica.Blocks.Types.Smoothness smoothness; |
|---|
| 218 |    output Integer tableID; |
|---|
| 219 |   external "C" tableID = ModelicaTables_CombiTable1D_init( |
|---|
| 220 |           tableName, fileName, table, size(table, 1), size(table, 2), |
|---|
| 221 | Â Â Â Â Â Â Â Â Â Â smoothness); |
|---|
| 222 | Â Â Â annotation(Library="ModelicaExternalC"); |
|---|
| 223 |   end tableInit; |
|---|
| 224 | |
|---|
| 225 |   function tableIpo |
|---|
| 226 |    input Integer tableID; |
|---|
| 227 |    input Integer icol; |
|---|
| 228 |    input Real u; |
|---|
| 229 |    output Real value; |
|---|
| 230 |   external "C" value = |
|---|
| 231 |             ModelicaTables_CombiTable1D_interpolate(tableID, icol, u); |
|---|
| 232 | Â Â Â annotation(Library="ModelicaExternalC"); |
|---|
| 233 |   end tableIpo; |
|---|
| 234 | Â equation |
|---|
| 235 |   if tableOnFile then |
|---|
| 236 |    assert(tableName<>"NoName", "tableOnFile = true and no table name given"); |
|---|
| 237 |   end if; |
|---|
| 238 |   if not tableOnFile then |
|---|
| 239 |    assert(size(table,1) > 0 and size(table,2) > 0, "tableOnFile = false and parameter table is an empty matrix"); |
|---|
| 240 |   end if; |
|---|
| 241 | |
|---|
| 242 |   for i in 1:n loop |
|---|
| 243 |    y[i] = if not tableOnFile and size(table,1)==1 then |
|---|
| 244 |         table[1, columns[i]] else tableIpo(tableID, columns[i], u[i]); |
|---|
| 245 |   end for; |
|---|
| 246 |   when initial() then |
|---|
| 247 |    tableID=tableInit(if tableOnFile then tableName else "NoName", |
|---|
| 248 |             if tableOnFile then fileName else "NoName", table, smoothness); |
|---|
| 249 |   end when; |
|---|
| 250 |  end CombiTable1D; |
|---|
| 251 | |
|---|
| 252 |  model CombiTable1Ds |
|---|
| 253 | Â Â "Table look-up in one dimension (matrix/file) with one input and n outputs" |
|---|
| 254 | |
|---|
| 255 |   import Modelica.Blocks.Types; |
|---|
| 256 |   parameter Boolean tableOnFile=false |
|---|
| 257 | Â Â Â "true, if table is defined on file or in function usertab"Â |
|---|
| 258 | Â Â Â annotation(Dialog(group="table data definition")); |
|---|
| 259 |   parameter Real table[:, :]=fill(0.0,0,2) |
|---|
| 260 | Â Â Â "table matrix (grid = first column; e.g., table=[0,2])"Â |
|---|
| 261 |      annotation(Dialog(group="table data definition", enable = not tableOnFile)); |
|---|
| 262 |   parameter String tableName="NoName" |
|---|
| 263 | Â Â Â "table name on file or in function usertab (see docu)"Â |
|---|
| 264 |      annotation(Dialog(group="table data definition", enable = tableOnFile)); |
|---|
| 265 |   parameter String fileName="NoName" "file where matrix is stored" |
|---|
| 266 |      annotation(Dialog(group="table data definition", enable = tableOnFile, |
|---|
| 267 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â __Dymola_loadSelector(filter="Text files (*.txt);;Matlab files (*.mat)", |
|---|
| 268 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â caption="Open file in which table is present"))); |
|---|
| 269 |   parameter Integer columns[:]=2:size(table, 2) |
|---|
| 270 | Â Â Â "columns of table to be interpolated"Â |
|---|
| 271 | Â Â annotation(Dialog(group="table data interpretation")); |
|---|
| 272 |   parameter Modelica.Blocks.Types.Smoothness smoothness=Types.Smoothness.LinearSegments |
|---|
| 273 | Â Â Â "smoothness of table interpolation"Â |
|---|
| 274 | Â Â annotation(Dialog(group="table data interpretation")); |
|---|
| 275 |   extends Modelica.Blocks.Interfaces.SIMO(final nout=size(columns, 1)); |
|---|
| 276 |   annotation ( |
|---|
| 277 | Â Â Â Documentation(info="<html> |
|---|
| 278 | <p> |
|---|
| 279 | <b>Linear interpolation</b>Â in <b>one</b>Â dimension of a <b>table</b>. |
|---|
| 280 | Via parameter <b>columns</b>Â it can be defined how many columns of the |
|---|
| 281 | table are interpolated. If, e.g., icol={2,4}, it is assumed that one input |
|---|
| 282 | and 2 output signals are present and that the first output interpolates |
|---|
| 283 | via column 2 and the second output interpolates via column 4 of the |
|---|
| 284 | table matrix. |
|---|
| 285 | </p> |
|---|
| 286 | <p> |
|---|
| 287 | The grid points and function values are stored in a matrix \"table[i,j]\", |
|---|
| 288 | where the first column \"table[:,1]\" contains the grid points and the |
|---|
| 289 | other columns contain the data to be interpolated. Example: |
|---|
| 290 | </p> |
|---|
| 291 | <pre> |
|---|
| 292 |   table = [0, 0; |
|---|
| 293 |       1, 1; |
|---|
| 294 |       2, 4; |
|---|
| 295 | Â Â Â Â Â Â 4, 16] |
|---|
| 296 | Â Â If, e.g., the input u = 1.0, the output y =Â 1.0, |
|---|
| 297 | Â Â Â Â e.g., the input u = 1.5, the output y =Â 2.5, |
|---|
| 298 | Â Â Â Â e.g., the input u = 2.0, the output y =Â 4.0, |
|---|
| 299 | Â Â Â Â e.g., the input u =-1.0, the output y = -1.0 (i.e. extrapolation). |
|---|
| 300 | </pre> |
|---|
| 301 | <ul> |
|---|
| 302 | <li>Â The interpolation is <b>efficient</b>, because a search for a new interpolation |
|---|
| 303 | Â Â Â starts at the interval used in the last call.</li> |
|---|
| 304 | <li>Â If the table has only <b>one row</b>, the table value is returned, |
|---|
| 305 | Â Â Â independent of the value of the input signal.</li> |
|---|
| 306 | <li>Â If the input signal <b>u</b>Â is <b>outside</b>Â of the defined <b>interval</b>, i.e., |
|---|
| 307 | Â Â Â u >Â table[size(table,1),1] or u <Â table[1,1], the corresponding |
|---|
| 308 | Â Â Â value is also determined by linear |
|---|
| 309 | Â Â Â interpolation through the last or first two points of the table.</li> |
|---|
| 310 | <li>Â The grid values (first column) have to be <b>strict</b> |
|---|
| 311 | Â Â Â monotonically increasing.</li> |
|---|
| 312 | </ul> |
|---|
| 313 | <p> |
|---|
| 314 | The table matrix can be defined in the following ways: |
|---|
| 315 | </p> |
|---|
| 316 | <ol> |
|---|
| 317 | <li>Â Explicitly supplied as <b>parameter matrix</b>Â \"table\", |
|---|
| 318 | Â Â Â and the other parameters have the following values: |
|---|
| 319 | <pre> |
|---|
| 320 | Â Â tableName is \"NoName\" or has only blanks, |
|---|
| 321 |   fileName is \"NoName\" or has only blanks. |
|---|
| 322 | </pre></li> |
|---|
| 323 | <li>Â <b>Read</b>Â from a <b>file</b>Â \"fileName\" where the matrix is stored as |
|---|
| 324 | Â Â Â \"tableName\". Both ASCII and binary file format is possible. |
|---|
| 325 | Â Â Â (the ASCII format is described below). |
|---|
| 326 | Â Â Â It is most convenient to generate the binary file from Matlab |
|---|
| 327 | Â Â Â (Matlab 4 storage format), e.g., by command |
|---|
| 328 | <pre> |
|---|
| 329 | Â Â save tables.mat tab1 tab2 tab3 -V4 |
|---|
| 330 | </pre> |
|---|
| 331 | Â Â Â when the three tables tab1, tab2, tab3 should be |
|---|
| 332 | Â Â Â used from the model.</li> |
|---|
| 333 | <li>Â Statically stored in function \"usertab\" in file \"usertab.c\". |
|---|
| 334 | Â Â Â The matrix is identified by \"tableName\". Parameter |
|---|
| 335 | Â Â Â fileName = \"NoName\" or has only blanks.</li> |
|---|
| 336 | </ol> |
|---|
| 337 | <p> |
|---|
| 338 | Table definition methods (1) and (3) do <b>not</b>Â allocate dynamic memory, |
|---|
| 339 | and do not access files, whereas method (2) does. Therefore (1) and (3) |
|---|
| 340 | are suited for hardware-in-the-loop simulation (e.g. with dSpace hardware). |
|---|
| 341 | When the constant \"NO_FILE\" is defined, all parts of the |
|---|
| 342 | source code of method (2) are removed by the C-preprocessor, such that |
|---|
| 343 | no dynamic memory allocation and no access to files takes place. |
|---|
| 344 | </p> |
|---|
| 345 | <p> |
|---|
| 346 | If tables are read from an ASCII-file, the file need to have the |
|---|
| 347 | following structure (\"-----\" is not part of the file content): |
|---|
| 348 | </p> |
|---|
| 349 | <pre> |
|---|
| 350 | ----------------------------------------------------- |
|---|
| 351 | #1 |
|---|
| 352 | double tab1(5,2)Â Â # comment line |
|---|
| 353 | Â 0Â Â 0 |
|---|
| 354 | Â 1Â Â 1 |
|---|
| 355 | Â 2Â Â 4 |
|---|
| 356 | Â 3Â Â 9 |
|---|
| 357 | Â 4Â 16 |
|---|
| 358 | double tab2(5,2)Â Â # another comment line |
|---|
| 359 | Â 0Â Â 0 |
|---|
| 360 | Â 2Â Â 2 |
|---|
| 361 | Â 4Â Â 8 |
|---|
| 362 | Â 6Â 18 |
|---|
| 363 | Â 8Â 32 |
|---|
| 364 | ----------------------------------------------------- |
|---|
| 365 | </pre> |
|---|
| 366 | <p> |
|---|
| 367 | Note, that the first two characters in the file need to be |
|---|
| 368 | \"#1\". Afterwards, the corresponding matrix has to be declared |
|---|
| 369 | with type, name and actual dimensions. Finally, in successive |
|---|
| 370 | rows of the file, the elements of the matrix have to be given. |
|---|
| 371 | Several matrices may be defined one after another. |
|---|
| 372 | </p> |
|---|
| 373 | </HTML> |
|---|
| 374 | "),  Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,-100},{100, |
|---|
| 375 |        100}}), graphics={ |
|---|
| 376 | Â Â Â Â Â Line(points={{-60,40},{-60,-40},{60,-40},{60,40},{30,40},{30,-40},{-30, |
|---|
| 377 | Â Â Â Â Â Â Â Â -40},{-30,40},{-60,40},{-60,20},{60,20},{60,0},{-60,0},{-60,-20}, |
|---|
| 378 |         {60,-20},{60,-40},{-60,-40},{-60,40},{60,40},{60,-40}}, color={ |
|---|
| 379 | Â Â Â Â Â Â Â Â 0,0,0}), |
|---|
| 380 |      Line(points={{0,40},{0,-40}}, color={0,0,0}), |
|---|
| 381 | Â Â Â Â Â Rectangle( |
|---|
| 382 | Â Â Â Â Â Â extent={{-60,40},{-30,20}}, |
|---|
| 383 | Â Â Â Â Â Â lineColor={0,0,0}, |
|---|
| 384 | Â Â Â Â Â Â fillColor={255,255,0}, |
|---|
| 385 | Â Â Â Â Â Â fillPattern=FillPattern.Solid), |
|---|
| 386 | Â Â Â Â Â Rectangle( |
|---|
| 387 | Â Â Â Â Â Â extent={{-60,20},{-30,0}}, |
|---|
| 388 | Â Â Â Â Â Â lineColor={0,0,0}, |
|---|
| 389 | Â Â Â Â Â Â fillColor={255,255,0}, |
|---|
| 390 | Â Â Â Â Â Â fillPattern=FillPattern.Solid), |
|---|
| 391 | Â Â Â Â Â Rectangle( |
|---|
| 392 | Â Â Â Â Â Â extent={{-60,0},{-30,-20}}, |
|---|
| 393 | Â Â Â Â Â Â lineColor={0,0,0}, |
|---|
| 394 | Â Â Â Â Â Â fillColor={255,255,0}, |
|---|
| 395 | Â Â Â Â Â Â fillPattern=FillPattern.Solid), |
|---|
| 396 | Â Â Â Â Â Rectangle( |
|---|
| 397 | Â Â Â Â Â Â extent={{-60,-20},{-30,-40}}, |
|---|
| 398 | Â Â Â Â Â Â lineColor={0,0,0}, |
|---|
| 399 | Â Â Â Â Â Â fillColor={255,255,0}, |
|---|
| 400 | Â Â Â Â Â Â fillPattern=FillPattern.Solid)}), |
|---|
| 401 |    Diagram(coordinateSystem(preserveAspectRatio=true, extent={{-100,-100},{ |
|---|
| 402 |        100,100}}), graphics={ |
|---|
| 403 | Â Â Â Â Â Rectangle( |
|---|
| 404 | Â Â Â Â Â Â extent={{-60,60},{60,-60}}, |
|---|
| 405 | Â Â Â Â Â Â fillColor={235,235,235}, |
|---|
| 406 | Â Â Â Â Â Â fillPattern=FillPattern.Solid, |
|---|
| 407 | Â Â Â Â Â Â lineColor={0,0,255}), |
|---|
| 408 |      Line(points={{-100,0},{-58,0}}, color={0,0,255}), |
|---|
| 409 |      Line(points={{60,0},{100,0}}, color={0,0,255}), |
|---|
| 410 | Â Â Â Â Â Text( |
|---|
| 411 | Â Â Â Â Â Â extent={{-100,100},{100,64}}, |
|---|
| 412 | Â Â Â Â Â Â textString="1 dimensional linear table interpolation", |
|---|
| 413 | Â Â Â Â Â Â lineColor={0,0,255}), |
|---|
| 414 | Â Â Â Â Â Line(points={{-54,40},{-54,-40},{54,-40},{54,40},{28,40},{28,-40},{-28, |
|---|
| 415 | Â Â Â Â Â Â Â Â -40},{-28,40},{-54,40},{-54,20},{54,20},{54,0},{-54,0},{-54,-20}, |
|---|
| 416 |         {54,-20},{54,-40},{-54,-40},{-54,40},{54,40},{54,-40}}, color={ |
|---|
| 417 | Â Â Â Â Â Â Â Â 0,0,0}), |
|---|
| 418 |      Line(points={{0,40},{0,-40}}, color={0,0,0}), |
|---|
| 419 | Â Â Â Â Â Rectangle( |
|---|
| 420 | Â Â Â Â Â Â extent={{-54,40},{-28,20}}, |
|---|
| 421 | Â Â Â Â Â Â lineColor={0,0,0}, |
|---|
| 422 | Â Â Â Â Â Â fillColor={255,255,0}, |
|---|
| 423 | Â Â Â Â Â Â fillPattern=FillPattern.Solid), |
|---|
| 424 | Â Â Â Â Â Rectangle( |
|---|
| 425 | Â Â Â Â Â Â extent={{-54,20},{-28,0}}, |
|---|
| 426 | Â Â Â Â Â Â lineColor={0,0,0}, |
|---|
| 427 | Â Â Â Â Â Â fillColor={255,255,0}, |
|---|
| 428 | Â Â Â Â Â Â fillPattern=FillPattern.Solid), |
|---|
| 429 | Â Â Â Â Â Rectangle( |
|---|
| 430 | Â Â Â Â Â Â extent={{-54,0},{-28,-20}}, |
|---|
| 431 | Â Â Â Â Â Â lineColor={0,0,0}, |
|---|
| 432 | Â Â Â Â Â Â fillColor={255,255,0}, |
|---|
| 433 | Â Â Â Â Â Â fillPattern=FillPattern.Solid), |
|---|
| 434 | Â Â Â Â Â Rectangle( |
|---|
| 435 | Â Â Â Â Â Â extent={{-54,-20},{-28,-40}}, |
|---|
| 436 | Â Â Â Â Â Â lineColor={0,0,0}, |
|---|
| 437 | Â Â Â Â Â Â fillColor={255,255,0}, |
|---|
| 438 | Â Â Â Â Â Â fillPattern=FillPattern.Solid), |
|---|
| 439 | Â Â Â Â Â Text( |
|---|
| 440 | Â Â Â Â Â Â extent={{-52,56},{-34,44}}, |
|---|
| 441 | Â Â Â Â Â Â textString="u", |
|---|
| 442 | Â Â Â Â Â Â lineColor={0,0,255}), |
|---|
| 443 | Â Â Â Â Â Text( |
|---|
| 444 | Â Â Â Â Â Â extent={{-22,54},{2,42}}, |
|---|
| 445 | Â Â Â Â Â Â textString="y[1]", |
|---|
| 446 | Â Â Â Â Â Â lineColor={0,0,255}), |
|---|
| 447 | Â Â Â Â Â Text( |
|---|
| 448 | Â Â Â Â Â Â extent={{4,54},{28,42}}, |
|---|
| 449 | Â Â Â Â Â Â textString="y[2]", |
|---|
| 450 | Â Â Â Â Â Â lineColor={0,0,255}), |
|---|
| 451 | Â Â Â Â Â Text( |
|---|
| 452 | Â Â Â Â Â Â extent={{0,-40},{32,-54}}, |
|---|
| 453 | Â Â Â Â Â Â textString="columns", |
|---|
| 454 | Â Â Â Â Â Â lineColor={0,0,255})})); |
|---|
| 455 | |
|---|
| 456 | Â protected |
|---|
| 457 |   Integer tableID; |
|---|
| 458 | |
|---|
| 459 |   function tableInit |
|---|
| 460 |    input String tableName; |
|---|
| 461 |    input String fileName; |
|---|
| 462 |    input Real table[ :, :]; |
|---|
| 463 |    input Modelica.Blocks.Types.Smoothness smoothness; |
|---|
| 464 |    output Integer tableID; |
|---|
| 465 |   external "C" tableID = ModelicaTables_CombiTable1D_init( |
|---|
| 466 |           tableName, fileName, table, size(table, 1), size(table, 2), |
|---|
| 467 | Â Â Â Â Â Â Â Â Â Â smoothness); |
|---|
| 468 | Â Â Â annotation(Library="ModelicaExternalC"); |
|---|
| 469 |   end tableInit; |
|---|
| 470 | |
|---|
| 471 |   function tableIpo |
|---|
| 472 |    input Integer tableID; |
|---|
| 473 |    input Integer icol; |
|---|
| 474 |    input Real u; |
|---|
| 475 |    output Real value; |
|---|
| 476 |   external "C" value = |
|---|
| 477 |             ModelicaTables_CombiTable1D_interpolate(tableID, icol, u); |
|---|
| 478 | Â Â Â annotation(Library="ModelicaExternalC"); |
|---|
| 479 |   end tableIpo; |
|---|
| 480 | |
|---|
| 481 | Â equation |
|---|
| 482 |   if tableOnFile then |
|---|
| 483 |    assert(tableName<>"NoName", "tableOnFile = true and no table name given"); |
|---|
| 484 |   end if; |
|---|
| 485 |   if not tableOnFile then |
|---|
| 486 |    assert(size(table,1) > 0 and size(table,2) > 0, "tableOnFile = false and parameter table is an empty matrix"); |
|---|
| 487 |   end if; |
|---|
| 488 | |
|---|
| 489 |   for i in 1:nout loop |
|---|
| 490 |    y[i] = if not tableOnFile and size(table,1)==1 then |
|---|
| 491 |         table[1, columns[i]] else tableIpo(tableID, columns[i], u); |
|---|
| 492 |   end for; |
|---|
| 493 |   when initial() then |
|---|
| 494 |    tableID=tableInit(if tableOnFile then tableName else "NoName", |
|---|
| 495 |             if tableOnFile then fileName else "NoName", table, smoothness); |
|---|
| 496 |   end when; |
|---|
| 497 |  end CombiTable1Ds; |
|---|
| 498 | |
|---|
| 499 |  model CombiTable2D "Table look-up in two dimensions (matrix/file) " |
|---|
| 500 | |
|---|
| 501 |   import Modelica.Blocks.Types; |
|---|
| 502 |   extends Modelica.Blocks.Interfaces.SI2SO; |
|---|
| 503 | |
|---|
| 504 |   parameter Boolean tableOnFile=false |
|---|
| 505 | Â Â Â "true, if table is defined on file or in function usertab"Â |
|---|
| 506 | Â Â Â annotation(Dialog(group="table data definition")); |
|---|
| 507 |   parameter Real table[:, :]=fill(0.0,0,2) |
|---|
| 508 | Â Â Â "table matrix (grid u1 = first column, grid u2 = first row; e.g. table=[0,0;0,1])" |
|---|
| 509 |      annotation(Dialog(group="table data definition", enable = not tableOnFile)); |
|---|
| 510 |   parameter String tableName="NoName" |
|---|
| 511 | Â Â Â "table name on file or in function usertab (see docu)"Â |
|---|
| 512 |      annotation(Dialog(group="table data definition", enable = tableOnFile)); |
|---|
| 513 |   parameter String fileName="NoName" "file where matrix is stored" |
|---|
| 514 |      annotation(Dialog(group="table data definition", enable = tableOnFile, |
|---|
| 515 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â __Dymola_loadSelector(filter="Text files (*.txt);;Matlab files (*.mat)", |
|---|
| 516 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â caption="Open file in which table is present"))); |
|---|
| 517 |   parameter Modelica.Blocks.Types.Smoothness smoothness=Types.Smoothness.LinearSegments |
|---|
| 518 | Â Â Â "smoothness of table interpolation"Â |
|---|
| 519 | Â Â annotation(Dialog(group="table data interpretation")); |
|---|
| 520 |   annotation ( |
|---|
| 521 | Â Â Â Documentation(info="<html> |
|---|
| 522 | <p> |
|---|
| 523 | <b>Linear interpolation</b>Â in <b>two</b>Â dimensions of a <b>table</b>. |
|---|
| 524 | The grid points and function values are stored in a matrix \"table[i,j]\", |
|---|
| 525 | where: |
|---|
| 526 | </p> |
|---|
| 527 | <ul> |
|---|
| 528 | <li>Â the first column \"table[2:,1]\" contains the u[1] grid points,</li> |
|---|
| 529 | <li>Â the first row \"table[1,2:]\" contains the u[2] grid points,</li> |
|---|
| 530 | <li>Â the other rows and columns contain the data to be interpolated.</li> |
|---|
| 531 | </ul> |
|---|
| 532 | <p> |
|---|
| 533 | Example: |
|---|
| 534 | </p> |
|---|
| 535 | <pre> |
|---|
| 536 | Â Â Â Â Â Â |Â Â Â Â |Â Â Â Â |Â Â Â Â | |
|---|
| 537 | Â Â Â Â Â Â |Â 1.0Â |Â 2.0Â |Â 3.0Â |Â // u2 |
|---|
| 538 | Â Â Â Â ----*-------*-------*-------* |
|---|
| 539 | Â Â Â Â 1.0 |Â 1.0Â |Â 3.0Â |Â 5.0Â | |
|---|
| 540 | Â Â Â Â ----*-------*-------*-------* |
|---|
| 541 | Â Â Â Â 2.0 |Â 2.0Â |Â 4.0Â |Â 6.0Â | |
|---|
| 542 | Â Â Â Â ----*-------*-------*-------* |
|---|
| 543 | Â Â Â // u1 |
|---|
| 544 | Â Â is defined as |
|---|
| 545 |    table = [0.0,  1.0,  2.0,  3.0; |
|---|
| 546 |         1.0,  1.0,  3.0,  5.0; |
|---|
| 547 |         2.0,  2.0,  4.0,  6.0] |
|---|
| 548 | Â Â If, e.g. the input u is [1.0;1.0], the output y is 1.0, |
|---|
| 549 | Â Â Â Â e.g. the input u is [2.0;1.5], the output y is 3.0. |
|---|
| 550 | </pre> |
|---|
| 551 | <ul> |
|---|
| 552 | <li>Â The interpolation is <b>efficient</b>, because a search for a new interpolation |
|---|
| 553 | Â Â Â starts at the interval used in the last call.</li> |
|---|
| 554 | <li>Â If the table has only <b>one element</b>, the table value is returned, |
|---|
| 555 | Â Â Â independent of the value of the input signal.</li> |
|---|
| 556 | <li>Â If the input signal <b>u1</b>Â or <b>u2</b>Â is <b>outside</b>Â of the defined <b>interval</b>, |
|---|
| 557 | Â Â Â the corresponding value is also determined by linear |
|---|
| 558 | Â Â Â interpolation through the last or first two points of the table.</li> |
|---|
| 559 | <li>Â The grid values (first column and first row) have to be <b>strict</b> |
|---|
| 560 | Â Â Â monotonically increasing.</li> |
|---|
| 561 | </ul> |
|---|
| 562 | <p> |
|---|
| 563 | The table matrix can be defined in the following ways: |
|---|
| 564 | </p> |
|---|
| 565 | <ol> |
|---|
| 566 | <li>Â Explicitly supplied as <b>parameter matrix</b>Â \"table\", |
|---|
| 567 | Â Â Â and the other parameters have the following values: |
|---|
| 568 | <pre> |
|---|
| 569 | Â Â tableName is \"NoName\" or has only blanks, |
|---|
| 570 |   fileName is \"NoName\" or has only blanks. |
|---|
| 571 | </pre></li> |
|---|
| 572 | <li>Â <b>Read</b>Â from a <b>file</b>Â \"fileName\" where the matrix is stored as |
|---|
| 573 | Â Â Â \"tableName\". Both ASCII and binary file format is possible. |
|---|
| 574 | Â Â Â (the ASCII format is described below). |
|---|
| 575 | Â Â Â It is most convenient to generate the binary file from Matlab |
|---|
| 576 | Â Â Â (Matlab 4 storage format), e.g., by command |
|---|
| 577 | <pre> |
|---|
| 578 | Â Â save tables.mat tab1 tab2 tab3 -V4 |
|---|
| 579 | </pre> |
|---|
| 580 | Â Â Â when the three tables tab1, tab2, tab3 should be |
|---|
| 581 | Â Â Â used from the model.</li> |
|---|
| 582 | <li>Â Statically stored in function \"usertab\" in file \"usertab.c\". |
|---|
| 583 | Â Â Â The matrix is identified by \"tableName\". Parameter |
|---|
| 584 | Â Â Â fileName = \"NoName\" or has only blanks.</li> |
|---|
| 585 | </ol> |
|---|
| 586 | <p> |
|---|
| 587 | Table definition methods (1) and (3) do <b>not</b>Â allocate dynamic memory, |
|---|
| 588 | and do not access files, whereas method (2) does. Therefore (1) and (3) |
|---|
| 589 | are suited for hardware-in-the-loop simulation (e.g. with dSpace hardware). |
|---|
| 590 | When the constant \"NO_FILE\" is defined, all parts of the |
|---|
| 591 | source code of method (2) are removed by the C-preprocessor, such that |
|---|
| 592 | no dynamic memory allocation and no access to files takes place. |
|---|
| 593 | </p> |
|---|
| 594 | <p> |
|---|
| 595 | If tables are read from an ASCII-file, the file need to have the |
|---|
| 596 | following structure (\"-----\" is not part of the file content): |
|---|
| 597 | </p> |
|---|
| 598 | <pre> |
|---|
| 599 | ----------------------------------------------------- |
|---|
| 600 | #1 |
|---|
| 601 | double table2D_1(3,4)Â Â # comment line |
|---|
| 602 | 0.0Â 1.0Â 2.0Â 3.0Â # u[2] grid points |
|---|
| 603 | 1.0Â 1.0Â 3.0Â 5.0 |
|---|
| 604 | 2.0Â 2.0Â 4.0Â 6.0 |
|---|
| 605 | Â |
|---|
| 606 | double table2D_2(4,4)Â Â # comment line |
|---|
| 607 | 0.0Â 1.0Â 2.0Â 3.0Â # u[2] grid points |
|---|
| 608 | 1.0Â 1.0Â 3.0Â 5.0 |
|---|
| 609 | 2.0Â 2.0Â 4.0Â 6.0 |
|---|
| 610 | 3.0Â 3.0Â 5.0Â 7.0 |
|---|
| 611 | ----------------------------------------------------- |
|---|
| 612 | </pre> |
|---|
| 613 | <p> |
|---|
| 614 | Note, that the first two characters in the file need to be |
|---|
| 615 | \"#1\". Afterwards, the corresponding matrix has to be declared |
|---|
| 616 | with type, name and actual dimensions. Finally, in successive |
|---|
| 617 | rows of the file, the elements of the matrix have to be given. |
|---|
| 618 | Several matrices may be defined one after another. |
|---|
| 619 | The matrix elements are interpreted in exactly the same way |
|---|
| 620 | as if the matrix is given as a parameter. For example, the first |
|---|
| 621 | column \"table2D_1[2:,1]\" contains the u[1] grid points, |
|---|
| 622 | and the first row \"table2D_1[1,2:]\" contains the u[2] grid points. |
|---|
| 623 | </p> |
|---|
| 624 | Â |
|---|
| 625 | </html> |
|---|
| 626 | "),  Icon(coordinateSystem(preserveAspectRatio=true, extent={{-100,-100},{100, |
|---|
| 627 |        100}}), graphics={ |
|---|
| 628 | Â Â Â Â Â Line(points={{-60,40},{-60,-40},{60,-40},{60,40},{30,40},{30,-40},{-30, |
|---|
| 629 | Â Â Â Â Â Â Â Â -40},{-30,40},{-60,40},{-60,20},{60,20},{60,0},{-60,0},{-60,-20}, |
|---|
| 630 | Â Â Â Â Â |
|---|