Problem with saving table in a method

Please login with a confirmed email address before reporting spam

Hi everyone, I'm currently working on creating small scripts using methods in the Application Builder to simplify the export of data in the Model Builder. I call the equivalent method in the Model Builder to get it running.

Right now, I'm trying to save a table using the following script and ran into two issues:

model.result().evaluationGroup().create("eg10", "EvaluationGroup"); model.result().evaluationGroup("eg10").set("data", "dset1"); model.result().evaluationGroup("eg10").label("Iron Losses"); model.result().evaluationGroup("eg10").create("int1", "IntSurface"); model.result().evaluationGroup("eg10").feature("int1").label("Surface Iron Losses"); model.result().evaluationGroup("eg10").feature("int1").set("intvolume", true); model.result().evaluationGroup("eg10").feature("int1").selection().named("sel15"); model.result().evaluationGroup("eg10").feature("int1").set("expr", new String[]{"rmm.Qh"}); model.result().evaluationGroup("eg10").feature("int1").set("descr", new String[]{"Volumetric loss density, electromagnetic"}); model.result().evaluationGroup("eg10").feature("int1").set("unit", new String[]{"W/m"}); model.result().evaluationGroup("eg10").feature("int1").setIndex("expr", "rmm.Qh*L", 0); model.result().evaluationGroup("eg10").feature("int1").setIndex("descr", "P_iron_MOD", 0); model.result().evaluationGroup("eg10").run();
//model.result().evaluationGroup("eg10").save("Z:\Simulationsergebnisse\Iron_losses.txt");
//model.result().evaluationGroup("eg10").save("C:\Users\kzt\Documents\Iron_losses.txt");
//model.result().evaluationGroup("eg10").save("C:\Temp\Iron_losses.txt");

1.) Everything works fine up to the last commented-out line. Unfortunately, I can’t save to any of the specified directories and I always get the following error:

"Cannot open file for writing. - Filename: Z:\Simulationsergebnisse\Iron_losses.txt (with all the other paths as well) - Table: Iron Losses"

However, I can manually save to the paths via mouse click.

2.) Is there an alternative to the following line?

"model.result().evaluationGroup("eg10").feature("int1").selection().named("sel15");"

I’d like to use the name of the selection instead of the index number 15. When I add or remove selections, the index changes, so "15" may no longer refer to the correct selection.

Best regards and many thanks in advance for your help!

Tobias Zeller


4 Replies Last Post 30 avr. 2025, 10:22 UTC−4
Gunnar Andersson COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 3 days ago 29 avr. 2025, 02:39 UTC−4
  1. For security reasons, methods by default have limited access to the file system. You can change this in Preferences > Methods and Java Libraries > File system access.
  2. The argument to selection().named() is the tag of a selection. This is the tag that you used when creating the selection (or was it created for you at some point)? In the former case, you can store the selection a variable, let's call it SEL, and pass SEL.tag() as argument to selection().named().
1. For security reasons, methods by default have limited access to the file system. You can change this in Preferences > Methods and Java Libraries > File system access. 2. The argument to selection().named() is the tag of a selection. This is the tag that you used when creating the selection (or was it created for you at some point)? In the former case, you can store the selection a variable, let's call it SEL, and pass SEL.tag() as argument to selection().named().

Please login with a confirmed email address before reporting spam

Posted: 3 days ago 29 avr. 2025, 05:07 UTC−4
  1. For security reasons, methods by default have limited access to the file system. You can change this in Preferences > Methods and Java Libraries > File system access.
  2. The argument to selection().named() is the tag of a selection. This is the tag that you used when creating the selection (or was it created for you at some point)? In the former case, you can store the selection a variable, let's call it SEL, and pass SEL.tag() as argument to selection().named().

Dear Mr. Andersson, First of all, thank you very much for your reply. Unfortunately, I still have one remaining question.

  1. I’m not entirely sure how to interpret your answer. Is the tag the same as the label?

In my case, I have nearly 20 different selections (see attachment), and “sel15,” which is the 15th selection, is the one I need. If the label is not the tag, is there a way to change or view the tag of a selection? Additionally, when I create a new explicit selection, I don’t see an option for assigning a "tag."

If I determine my tag name—for example, if the tag name is “test”—what would my code look like? Would it be something like this? "model.result().evaluationGroup("eg10").feature("int1").selection().test.tag();"

Best regards and thanks a lot Tobias Zeller

>1. For security reasons, methods by default have limited access to the file system. You can change this in Preferences > Methods and Java Libraries > File system access. >2. The argument to selection().named() is the tag of a selection. This is the tag that you used when creating the selection (or was it created for you at some point)? In the former case, you can store the selection a variable, let's call it SEL, and pass SEL.tag() as argument to selection().named(). Dear Mr. Andersson, First of all, thank you very much for your reply. Unfortunately, I still have one remaining question. 2. I’m not entirely sure how to interpret your answer. Is the tag the same as the label? In my case, I have nearly 20 different selections (see attachment), and “sel15,” which is the 15th selection, is the one I need. If the label is not the tag, is there a way to change or view the tag of a selection? Additionally, when I create a new explicit selection, I don’t see an option for assigning a "tag." If I determine my tag name—for example, if the tag name is “test”—what would my code look like? Would it be something like this? "model.result().evaluationGroup("eg10").feature("int1").selection().test.tag();" Best regards and thanks a lot Tobias Zeller


Gunnar Andersson COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 3 days ago 29 avr. 2025, 11:58 UTC−4

Every feature in the model has a tag and a label. By default only the label is shown in the GUI, and it's only the label that you can change - you can think of the tag as a more permanent identification (which is why it's used in the API). It's possible to enable showing the labels in the Model Builder, see attached screenshot.

If you want to find the tag for a given label, then you can write method code that loops over the selections in the model, retrieve the label for each selection using the label() method, compare against the label that you want, and then retrieve its tag using the tag() method.

If the tag is "test", then you would use it as follows:

model.result().evaluationGroup("eg10").feature("int1").selection().named("test")

Every feature in the model has a tag and a label. By default only the label is shown in the GUI, and it's only the label that you can change - you can think of the tag as a more permanent identification (which is why it's used in the API). It's possible to enable showing the labels in the Model Builder, see attached screenshot. If you want to find the tag for a given label, then you can write method code that loops over the selections in the model, retrieve the label for each selection using the label() method, compare against the label that you want, and then retrieve its tag using the tag() method. If the tag is "test", then you would use it as follows: model.result().evaluationGroup("eg10").feature("int1").selection().named("test")


Please login with a confirmed email address before reporting spam

Posted: 2 days ago 30 avr. 2025, 10:22 UTC−4

Dear Mr. Andersson,

Thank you very much for your reply. With your explanation, I was able to get everything working just as I had hoped.

I have one final question: If the tag is assigned automatically by COMSOL and only the label can be changed by the user, does that mean it’s not possible to automate multiple simulations—since the tag name might vary between simulations, even if the label remains the same?

For example, in Simulation 1 I selected the Rotor (sel1, label = "Rotor") first, followed by the Stator (sel2, label = "Stator"). In Simulation 2, I reversed the order and ended up with Stator (sel1, label = "Stator") and Rotor (sel2, label = "Rotor"). Although the labels and selected domains are consistent across both simulations, I can't reliably identify them because the tags may differ.

This is somewhat unfortunate, since a minor mistake could force me to delete and recreate all selections in the exact intended order. Another example would be a simulation in which the Rotor isn’t present at all—but I’d still want to automate the remaining selections.

I hope my explanation was clear. Do you happen to know of any workaround for this?

Kind regards, Tobias Zeller

Dear Mr. Andersson, Thank you very much for your reply. With your explanation, I was able to get everything working just as I had hoped. I have one final question: If the tag is assigned automatically by COMSOL and only the label can be changed by the user, does that mean it’s not possible to automate multiple simulations—since the tag name might vary between simulations, even if the label remains the same? For example, in Simulation 1 I selected the Rotor (sel1, label = "Rotor") first, followed by the Stator (sel2, label = "Stator"). In Simulation 2, I reversed the order and ended up with Stator (sel1, label = "Stator") and Rotor (sel2, label = "Rotor"). Although the labels and selected domains are consistent across both simulations, I can't reliably identify them because the tags may differ. This is somewhat unfortunate, since a minor mistake could force me to delete and recreate all selections in the exact intended order. Another example would be a simulation in which the Rotor isn’t present at all—but I’d still want to automate the remaining selections. I hope my explanation was clear. Do you happen to know of any workaround for this? Kind regards, Tobias Zeller

Reply

Please read the discussion forum rules before posting.

Please log in to post a reply.

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.