Message Boards Message Boards

Specify sets of parameters in SystemModeler


Me and @Neil Singer had an interesting discussion regarding the best practices on how to collect different parameter values together in SystemModeler.

This can be useful in many different cases, for example: Your smart phone battery for example might have different performance characteristics than that of another model. So if you were to create a model of the battery discharge of your phone during the day, you could specify different parameter values from different battery manufacturers and compare different scenarios to each other.

So we have different parameters values that belong together and want to use theses in a model. We can of course go in and change each individual place were those parameters are used, but if you want to scale this up, that can become cumbersome.

Lets use a very simple example to illustrate this: a mass attached to a spring with a force that pushes on the mass. This model and everything I describe here is attached to this post so you can try it out for yourself.

diagram of model

Now, say we have different parameters sets containing values for the magnitude of the force, the mass of the mass and the spring constant. In Modelica, we can collect all these parameter values into a record.

I created a record called Set1.

modelica text of set1

Now this record can be added to the model by dragging and dropping it into the diagram view. Note that records will not show up in the diagram view by default, but you can drag it into the diagram view from the Component Browser (to the right in Model Center).

model with record

All the parameter values now be taken directly from the record (as indicated by the parameter labels in the diagram above). You can now easily switch between different records, either from the text view, or by right clicking the record component and choosing Change Type. Change the type to the record of your choice, containing the parameter set you want to use.

dialog of change type

Tip: You can drag and drop a record from the Class Browser into the text field of this dialog.

Another way to change the record is to declare it as a parameter. Either use the text view and preface the component declaration with the keyword parameter or right click the component (from the diagram view or the component browser) and choose Properties. Now you can set it to a parameter in the Attributes section:

attribute dialog

The record can now be specified in the General tab of Model Center. You need to use brackets at the end round brackets at the end of the declaration and the new record must be type compatible to the one which was initially declared.

Parameter dialog

What is nice about having it as a parameter is that you can set different choices that can be used. This information is put in the annotation for the component declaration, for example:

annotation(choices(choice = ParameterSets.Records.Set1() "Set 1", choice = ParameterSets.Records.Set2() "Set 2", choice = ParameterSets.Records.Set2Alt() "Set 2 alternative"))

Now these choices can be selected from a drop down menu:

drop down menu parameter dialog

With that, you can have a drop-down menu to choose different engine models for your car (now if only it was as easy in real life to get a Ferrari engine....)

This of course only changes the parameter values. If you want to change the whole structure of the model, you can do that to but it is outside of the scope of this discussion. If anyone in interested, I would recommend checking out the Golf and Soccer downloadable model where that functionality is used.

Tip: in SystemModeler 4.3, our web examples are available directly in product, the Golf and Soccer model can be found in the class browser, under EducationalExamples > Physics.

Examples library

POSTED BY: Patrik Ekenberg
1 year ago

enter image description here - you have earned "Featured Contributor" badge, congratulations !

This is a great post and it has been selected for the curated Staff Picks group. Your profile is now distinguished by a "Featured Contributor" badge and displayed on the "Featured Contributor" board.

POSTED BY: Moderation Team
1 year ago


I like the feature of the first approach in that you get to see the parameter of the set in the model view (In your figure the set block in the upper left corder has the values for m,c and f visible in the model view) . The second approach is more convenient to use, but the parameters do not appear anywhere on the model view. Is there a way to do the second approach and have the values used in the set appear in the model? I tried to experiment with this but could not get values to display.

Is there a way to use the replaceable components (that you show in the golf and soccer model) to redeclare the record so the visual changes to show some indication of the values in the record (for example: m=1 for one and m=2 for another, etc.)?


POSTED BY: Neil Singer
1 year ago

Yes, replaceable components could be used for that. If you put:

replaceable ParameterSets.Records.Set1 set

Then as before you can add choices for the user in the annotation. You can either add a choice like this:

choice(redeclare ParameterSets.Records.Set1 set(m=2,c=2,f=2) "Set 2")

Where you directly in the annotation hard code the different values.

For scale up, I would suggest using this instead:

choices(choice(redeclare ParameterSets.Records.Set1 set "Set 1"),choice(redeclare ParameterSets.Records.Set2 set "Set 2"),choice(redeclare ParameterSets.Records.Set2Alt set "Set 2 alternative"))

Now the actual values can be changed in the records instead and it is easier to add new ones.

You need to create a base class with this replaceable component. In an extended class, you can choose to redeclare the record:

diagram before redeclaration

redeclare record

diagram after redeclaration

I updated the model file and attached it below!

POSTED BY: Patrik Ekenberg
1 year ago


This is awesome!

My only suggested change is to make the replaceable record its own model because in many applications the parameter sets will be used in vastly different models. Having the set as part of a larger model is cumbersome and restricts changes. For example, in your spring mass example, lets say I wanted to add another mass but still use the same parameter sets. This would not be possible. I modified your example to make it possible to use the sets in various models:

Here is the base class replaceable record (NewReplaceableSetClass):

enter image description here

Patrick's model looks the same:

enter image description here

But now it is duplicatable and editable:

enter image description here

To do this you only need one line from Patrik's example in the newReplaceableSetClass:

  replaceable ParameterSetsRedeclare.Records.Set1 set annotation(choices(choice(redeclare ParameterSetsRedeclare.Records.Set1 set "Set 1"), choice(redeclare ParameterSetsRedeclare.Records.Set2 set "Set 2"), choice(redeclare ParameterSetsRedeclare.Records.Set2Alt set "Set 2 alternative")), Placement(visible = true, transformation(origin = {-124.703, 81.48}, extent = {{-13.52, -13.52}, {13.52, 13.52}}, rotation = 0)));

and you need to add only one line to the models in which you use the replaceable set: (Note I added the icon AFTER my parameter set so the model will replace the parameter icon with the example icon -- makes things look nicer)

  extends ParameterSetsRedeclare.BaseClasses.NewReplaceableSetClass(redeclare ParameterSetsRedeclare.Records.Set2 set);
  extends Modelica.Icons.Example;

I have attached Patrik's file with these small edits.

POSTED BY: Neil Singer
1 year ago

Group Abstract Group Abstract