Problem with saving table in a method
Posted 2025年4月25日 GMT-4 10:46 Version 6.3 5 Replies
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
Please login with a confirmed email address before reporting spam
- 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.
- 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
- 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.
- 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.
- 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
Attachments:
Please login with a confirmed email address before reporting spam
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")
Attachments:
Please login with a confirmed email address before reporting spam
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
Please login with a confirmed email address before reporting spam
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")
Dear Mr. Andersson, Thank you again for your reply. With your help, I was able to understand the tag name selection. Unfortunately, I’m facing another challenge where I need assistance.
I’m trying to apply a fillet to a geometry using a selection, as the points may change dynamically. However, the following code didn’t work:
// Disc-Selection of the Points model.component("Komponente2D").selection().create("disk1", "Disk"); model.component("Komponente2D").selection("disk1").set("entitydim", 0); model.component("Komponente2D").selection("disk1").set("r", "10"); //Will be dynamic model.component("Komponente2D").selection("disk1").set("rin", "9"); //Will be dynamic // Fillet model.component("Komponente2D").geom("Geometry2D").create("fil1", "Fillet"); model.component("Komponente2D").geom("Geometry2D").feature("fil1").selection("point").set("disk1");
Then I tried the following approach: *// Disc-Selection of the Points model.component("Komponente2D").selection().create("disk1", "Disk"); model.component("Komponente2D").selection("disk1").geom("Geometry2D", 0); model.component("Komponente2D").selection("disk1").set("r", "10"); //Will be dynamic model.component("Komponente2D").selection("disk1").set("rin", "9"); //Will be dynamic
// Point Identity ModelNode geom = model.component("Komponente2D").geom("Geometry2D"); Selection diskSelection = model.component("Komponente2D").selection("disk1"); int[] punktIDs = diskSelection.entities("point"); // Get the point identities
String[] punktNamen = new String[punktIDs.length]; for (int i = 0; i < punktIDs.length; i++) { punktNamen[i] = "pt"+punktIDs[i]; }
model.component("Komponente2D").geom("Geometry2D").feature("fil1").selection("point").set(punktNamen);*
Unfortunately, this approach also did not work. Is there a way to select the points for the fillet using a selection?
Best regards, Tobias Zeller
Reply
Please read the discussion forum rules before posting.
Discussion Forum Rules and Guidelines
The goal of COMSOL Access is to provide a forum for you to communicate effectively with COMSOL as well as your colleagues within the multiphysics simulation community. This involves providing you with access to technical support and downloads of the latest {:comsol} software releases, as well as the ability to share your comments and work with other users of the {:comsol} software through forums such as the blog, discussion forum, and Application Exchange. In order to make this an efficient and pleasant experience for you and other members of COMSOL Access, we ask that you follow a few rules and guidelines.
When registering for COMSOL Access, you agree to provide your complete and truthful information for all fields requested on your COMSOL Access account registration page. You also agree to maintain the accuracy of all information associated with you on your COMSOL Access account. You agree to maintain your COMSOL Access account for use solely by you, not to share your username and password with anyone else, and to take appropriate precautions to restrict access to your username and password from others. Furthermore, you agree not to submit any information relating to your employer through your COMSOL Access account without your employer’s authorization. Should you use a COMSOL Access account associated with an employer, you agree to immediately discontinue using that account upon termination of that employment.
The moderators of the forums will remove any generally objectionable material as quickly as possible. You acknowledge that all posts made to these forums express the views and opinions of the author and not the administrators, moderators, or webmaster (except for posts by these people). Hence, they will not be held liable.
You agree not to post or link to any material that is abusive, obscene, vulgar, slanderous, hateful, threatening, sexually oriented, or that infringes upon or violates any third-party rights or any other material that may violate any applicable laws. You agree that you will not otherwise use your COMSOL Access account to violate or to assist anyone in violating any law. Engaging in any activity in violation of these COMSOL Access rules and guidelines may lead to you being immediately and permanently banned from COMSOL Access. The IP address of all posts is recorded to aid in enforcing these conditions. You agree that the webmaster, administrator, and moderators of the forums have the right to remove, move, or close any topic at any time as they see fit. As a user of COMSOL Access, you agree to any information you have entered into any of the forums being stored in a database.
You agree that you will not use your COMSOL Access account in violation of any applicable export control laws. You represent and warrant that you are not subject to any comprehensive sanction or embargo by the U.S. or any other country, nor are you identified on any list maintained by the U.S. government that identifies persons for which the U.S. maintains restrictions. Further, you represent and warrant that you are not subject to any restriction on the receipt of technology or products under the export control laws of the U.S. or any other country.
Basic Rules
- Flaming: Do not post any messages that harass, insult, belittle, threaten, or flame another member or guest. Debates are fine, but argue with the point, not the person.
- Trolling: Do not post with the purpose of starting a dispute. Note that a person disagreeing with your opinion is not considered trolling; keep it civil, even if you are sure that the other person is wrong. Anything seen as trolling will result in you being banned.
- Spamming: Posts without content or containing nonsense waste space and everyone's time. Spam will be removed from the forum.
- Offensive Posts, Links or Images: Do not use profanity, racial, ethnic, religious, or other slurs or any other offensive material.
- Advertising: Posting of advertisements for products or services, links to auctions, affiliate links, links to promote websites, and so forth is not allowed.
- Disclosure of Personal Information: Do not disclose any other member's email, real name, address, phone number, IP address, or other personal information. This includes posting contents of emails and private messages without the sender's consent. Do not bring personal disputes into the forums. Bans and warnings issued to other users are considered personal information.
- Misleading Titles: The subject line of the post should be as informative as possible about the content of the post. This also reduces the amount of duplicate posts.
- Thread Hijacking: Taking a thread off-topic to pursue one's own agenda is not permitted.
- Piracy: Do not upload, post, email, transmit, or otherwise make available any content that infringes upon any patent, trademark, trade secret, copyright, or other proprietary rights ("Rights") of any party. This includes COMSOL model files created by a forged or illegitimate license.
- Honesty: Users must provide truthful information in creating their COMSOL Access account.
These rules are subject to change. The moderators reserve the right to remove, edit, or move posts at their discretion. The COMSOL Access administrators will reserve the right to permanently remove a user account without notice if any of the rules are not followed. Particular services accessible with your COMSOL Access account may be subject to additional rules. You agree to comply with all rules applicable to each service you access through your COMSOL Access account.
Posting Guidelines
When posting, understand that you are trying to communicate with other people. Although many COMSOL Access members are not fluent in English, the official language of this forum is English.
Here are some important guidelines of language:
- Write in English.
- If you are familiar with LaTeX, please use this to write mathematical equations.
- Always do a quick check for spelling/grammar mistakes.
- Format your post in a legible manner. Use the Preview button often.
- Be concise and articulate as much as possible.
- Use the Enter key to create paragraphs.
- Capitalize correctly. It is difficult to read posts that are written entirely in uppercase or lowercase.
- Use correct punctuation. Avoid run-on sentences.
- Try to avoid using “text speak”, “net speak”, or slang. The purpose of language is to be understood.
- Never invent acronyms and use as few acronyms as possible. For example, write "COMSOL Multiphysics" and not "CMP".
- Review your post before publishing it. Try to catch typos.
- Please check to see if a topic has already been posted. Do not post multiple threads on the same topic.
Disclaimer
By submitting content to the forums, you hereby grant COMSOL a nonexclusive, royalty-free, perpetual, worldwide, and unrestricted license to reproduce, publicly display, publicly distribute, and prepare derivative works of the content. COMSOL hereby grants you a license to copy and/or use content from the forums solely for your own internal purposes. COMSOL provides the forum service for the benefit of our users to share content with the community. All content is provided "as is" without warranty of any kind, express or implied, including without limitation, warranties of merchantability, noninfringement, design, operation, and fitness for a particular purpose, and the entire risk as to the quality and performance of the programs is with you.
Neither COMSOL, the authors, nor the copyright owners of submitted materials warrant that the programs will be error-free, uninterrupted, virus-free, secure, and suitable for your needs, produce specific results, or that errors or failures will be corrected. Comments on supplied content should be sent to the author or copyright owner through the tools provided in the forums.
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.