Message Boards Message Boards

0
|
10360 Views
|
6 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Resize arrays with WSMLink?

I'm trying to run my model with WSMLink, but I want to change the number of elements in my arrays before running. When I change the parameter corresponding to the length of the array, and then try to set the elements of this new array, I get an error saying that the new indices aren't parameter. Ok, I understand why, but how do I resize the arrays first?

e.g.

My array length was 4 now...

WSMSimulate["myPackage.myModel",WSMParameterValues->{arrayLength -> 5, param1[5]->2.0}]

And I get an error. Can I use WSMModelData or WSMCreateModel to change the length of the arrays before trying to set them?

POSTED BY: Eric Smith
6 Replies

Here is the "junk" model if it helps

Attachments:
POSTED BY: Neil Singer

Eric,

You can do what you want with WSMSetValues.

I created a model "junk" that has an array of masses and an array of forces. I have a parameter "n" that sets how many masses and forces are there.

To change the number of array elements and their values you do

WSMSetValues["junk", {DotName["n"] -> 5, 
  DotName["mass", "m"] -> {11, 12, 13, 23, 43}, 
  DotName["forceStep", "stepForce"] -> {1, 2, 3, 4, 5}}]

You use DotName to construct the WSM parameter names. You can view all the DotNames by doing this:

WSMModelData["junk", "ParameterNames"]

I do not believe that you can change one array value (for example mass[3].mass). I think its all or nothing -- you must send an array. You can query the values in Mathematica and send them back in with minor modifications but (as far as I know) you can't set one value. To query the values use

WSMModelData["junk", "ParameterValues"]

or you can grab just the mass values like this:

WSMModelData["junk", {"ParameterValues", "mass*.m"}]

to get

{mass[1].m->11,mass[2].m->12,mass[3].m->13,mass[4].m->23,mass[5].m->43}

Which you can parse and change and send back.

This should also solve your previous post about setting parameter arrays -- you need to enter arrays in the fields -- not values.

I hope this helps.

Regards

Neil

POSTED BY: Neil Singer

Thanks so much! This is exactly what I need. I'll test it when I get a chance and report back

POSTED BY: Eric Smith

This is all correct. One detail to note though is that DotName is a feature in the upcoming release of SystemModeler (of which Neil has a pre-release).

In the released 4.3, you would use period-separated strings instead:

WSMSetValues["junk", {"n" -> 5, 
  "mass.m" -> {11, 12, 13, 23, 43}, 
  "forceStep.stepForce" -> {1, 2, 3, 4, 5}}]
POSTED BY: Malte Lenz

By the way, what are the advantages of DotName? I looked at the documentation and can't find anything that stands out as being easier than just using "StringJoin" and "StringSplit". In fact, I found myself doing a StringRiffle[{##},"."]&@@DotName[package,model,var] operation before working on the variables.

Also, there seems to be a slight bug in DotName. If you try using DotName with more than 3 arguments it's highlighted Red, indicating too many arguments. It seemed to work, but is that being updated in a future Mathematica release?

POSTED BY: Eric Smith

DotName has a few benefits compared to pure strings:

  • It is guaranteed to handle all names correctly.

For a name like my.'name.with'.quotes, working with StringJoin and StringSplit on "." would be incorrect.

  • It makes pattern matching possible, without resorting to string matching/regular expressions

That said, you can do ToString on a DotName to get the .-separated string.

I can't reproduce the red highlight with more than three arguments. That might be a bug in a particular prerelease. If you still see it in the released version, please report it to support and we will have another look.

POSTED BY: Malte Lenz
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract