Skip to content

External data into Modelica models🔗

Purpose🔗

Instruction for linking data from an external file (.xml or .xlsx or .mat or .json) to model parameters.

Background🔗

  • There are many types of data sources externally available for parameters that are required to be linked to the Modelica models.
  • The data access package supports both reading and writing data, fostering connections between simulations. It is compatible with XML, JSON, and structured MAT files, and it efficiently manages substantial data sets, even involving a high volume of calls.

Note

When changes like rename, and content modifications are made to the external file, the user should again upload the modified file to the ‘Resources’ folder & reinforce compilation (SHIFT+SIMULATE) to ensure that Modelon Impact re-reads the file.

Example Model Selection🔗

A simple model is selected from the Modelica Standard Library. The path of the model is:

Modelica.Mechanics.MultiBody.Examples.Elementary.SpringMassSystem

Right-click on the model as shown in the below image and extend it into the desired location of your workspace/package.

The selected model has two springs, and a mass is attached to each of them as shown in the image below. In the image, their values are shown and provided from the UI.

Let’s assume the body’s mass and spring’s stiffness parameters are available in an external data file of various formats like XML, XLSX, MAT, JSON.

XML FILE🔗

Let's assume the model needs to read the values of body masses and spring constants from the XML file.

Creating a .xml file🔗

Let’s create a .xml file with the contents shown below.

<?xml version="1.0"?>
<systemMasses>
  <system1>
    <body>
      <mass>1</mass>
    </body>
   <spring>
     <c>30</c>
   </spring>
  </system1>
  <system2>
    <body>
      <mass>1</mass>
    </body>
   <spring>
     <c>30</c>
   </spring>
  </system2>
</systemMasses>
  • Save the XML file and name it, say, “systemMasses.xml”.
  • In Impact, three icons show up when the cursor is taken over the ‘Resources’ folder of your package. Use the ‘Import file’ option (up arrow) to upload the XML file in the ‘Resources’ folder.

XML Data Access Package🔗

  • Drag the XML data access package from ‘Modelon.DataAccess.XmlFile’ into the canvas.
  • Double-click on the access package to open the details panel on the right side and drag and drop your .xml file from the ‘Resources’ folder in the ‘filename’ field.

The following path .Modelica.Utilities.Files.loadResource("modelica://MyPackage/Resources/springMass.xml") gets loaded in the filename.

The parameters should be linked to the values of the XML file.

  • Double-click on body1 to open its parametrization section. Use the below expression next to the field for m.
access.getReal("system1.body.mass")
  • Similarly, parametrize body2 using access.getReal("system2.body.mass") for m.
  • Spring constants of spring1 and spring2 are parametrized accordingly with the expressions access.getReal("system1.spring.c") and access.getReal("system2.spring.c") respectively.

Check Model Results:🔗

Simulate the model and check the results if they are in accordance with the .xml file values. In addition to this, the parameter values can be checked from the output as shown in the below image with stickies.

XLSX FILE🔗

Let's assume the model needs to read the values of body masses and spring constants from the XLSX file. Before carrying out this section of the tutorial complete the Example Model Selection.

Creating a .xlsx file🔗

Let’s create a .xlsx file, First create two sheets and name them system1 and system2 respectively. Enter the data according to the images shown below.

  • Save the XLSX file and name it, say “systemMasses.xlsx”.
  • Upload the file into the resource folder.

XLSX Data Access Package🔗

  • Drag the XLSX data access package from ‘Modelon.DataAccess.XlsxFile’ into the canvas.
  • Double-click on the access package to open the details panel on the right side and drag and drop your .xlsx file from the ‘Resources’ folder in the ‘filename’ field.

The following path .Modelica.Utilities.Files.loadResource("modelica://MyPackage/Resources/springMass.xlsx") gets loaded in the filename.

The parameters should be linked to the values of the XLSX file.

  • Double-click on body1 to open its parametrization section. Use the below expression next to the field for m.
  • Similarly, parametrize body2 using access.getReal("system2.body_mass") for m.
  • Spring constants of spring1 and spring2 are parametrized accordingly with the expressions access.getReal("system1.!\(B\)4") and access.getReal("system2.spring_c") respectively.

Check Model Results:🔗

Simulate the model and check the results if they are in accordance with the .xlsx file values. In addition to this, the parameter values can be checked from the output as shown in the image with stickies.

MAT File🔗

Let's assume the model needs to read the values of body masses and spring constants from the MAT file. Before carrying out this section of the tutorial complete the Example Model Selection.

Creating .mat file🔗

Let’s create a .mat file with the contents shown below.

springMass :
      system1 :
         body :
           mass: 1
         spring :
           c: 30
      system2 :
         body :
           mass: 1
         spring :
           c: 30

  • Save the MAT file and name it, say, “systemMasses.mat”.
  • Upload the file into the resource folder.

MAT Data Access Package🔗

  • Drag the MAT data access package from ‘Modelon.DataAccess.MatFile’ into the canvas.
  • Double-click on the access package to open the details panel on the right side and drag and drop your .mat file from the ‘Resources’ folder in the ‘filename’ field.

The following path .Modelica.Utilities.Files.loadResource("modelica://MyPackage/Resources/springMass.mat")* gets loaded in the filename.

The parameters should be linked to the values of the MAT file.

  • Double-click on body1 to open its parametrization section. Use the below expression next to the field for m.
access.getReal("system1.body.mass")
  • Similarly, parametrize body2 using access.getReal("system2.body.mass") for m.
  • Spring constants of spring1 and spring2 are parametrized accordingly with the expressions access.getReal("system1.spring.c") and access.getReal("system2.spring.c") respectively.

Check Model Results🔗

Simulate the model and check the results if they are in accordance with the .mat file values. In addition to this, the parameter values can be checked from the output as shown in the image with stickies.

JSON File🔗

Let's assume the model needs to read the values of body masses and spring constants from the JSON file. Before carrying out this section of the tutorial complete the Example Model Selection.

Creating .json file🔗

Let’s create a .json file with the contents shown below.

{
   "springMass": {
      "system1": {
         "body": {
              "mass": 1
         },
         "spring": {
            "c": 30
         }
      },
      "system2": {
         "body": {
            "mass": 1
         },
         "spring": {
            "c": 30
         }
      }
   }
}

  • Save the JSON file and name it, say, “systemMasses.json”.
  • Upload the file into the resource folder.

JSON Data Access Package🔗

  • Drag the JSON data access package from ‘Modelon.DataAccess.JsonFile’ into the canvas.
  • Double-click on the access package to open the details panel on the right side and drag and drop your .json file from the ‘Resources’ folder in the ‘filename’ field.

The following path .Modelica.Utilities.Files.loadResource("modelica://MyPackage/Resources/springMass.json") gets loaded in the filename.

The parameters should be linked to the values of the JSON file.

  • Double-click on body1 to open its parametrization section. Use the below expression next to the field for m.
access.getReal("system1.body.mass")
  • Similarly, parametrize body2 using access.getReal("system2.body.mass") for m.
  • Spring constants of spring1 and spring2 are parametrized accordingly with the expressions access.getReal("system1.spring.c") and access.getReal("system2.spring.c") respectively.

Check Model Results🔗

Simulate the model and check the results if they are in accordance with the .json file values. In addition to this, the parameter values can be checked from the output as shown in the image with stickies.