how to import a computed polynomial function in a Matlab script into a Comsol simulation?
Posted 31.01.2024, 11:03 GMT-5 12 Replies
Please login with a confirmed email address before reporting spam
Hello everyone,
I'd like to start by saying that I don't know much about this, in fact, almost nothing. I have an issue regarding the interaction between Matlab and Comsol, specifically in dynamically importing a polynomial function calculated in a Matlab script into a time-variable Comsol simulation.
To better explain the problem, I have uploaded three files:
A CSV file containing data to be interpolated in the Matlab script. Each row corresponds to different time instances, and the first row contains the points along x at which the quantity of interest was evaluated.
A Matlab script where, within a for loop, I analyze my CSV file. At each step of the loop, I obtain my polynomial function (string_polynomial), which I would like to import into Comsol.
The Comsol model containing my geometry. In particular, within the physics .mf with the Pointise Constraint function, I would like to impose the corresponding polynomial function for each timestep, calculated with the Matlab script.
It's precisely this step that is causing me problems. I'm not sure how to interface the two software tools and especially how to perform this "exchange" of polynomial functions for each timestep.
I assume that the number of timesteps in the transient study and the number of timesteps in the script (and thus in the CSV file) should be equal.
Thanks in advance to anyone who can help!
Attachments:
Please login with a confirmed email address before reporting spam
Hi Marianna,
I can't tell about the Matlab interface because I am not using it. You might consider to import the csv data directly into a 2D interpolation function in COMSOL and avoid the Matlab interfacing. This way you can call a function f(x, t) in the model. You have an interpolation in both dimensions, space and time.
Check the documentation about input formats of interpolation functions and about the way they interpolate to see if it is suitable for your project.
Cheers
Edgar
-------------------Edgar J. Kaiser
emPhys Physical Technology
www.emphys.com
Please login with a confirmed email address before reporting spam
Hi Edgar, many thanks for you suggest!
I have done some test but I didn't understand how I have to define my data. As I have read it's not possible used a grid with more 3 rows, is it correct? I have a file excel defined as in photo in which the yellow cells are the x coordinates and the rows from 2 to 6 are the number of timestep i.e. 1s, 2s, 3s...
How can I define a f(x,t) function in Comsol with my data?
Thank you in advance for your help! Marianna
Attachments:
Please login with a confirmed email address before reporting spam
Hi Marianna,
in the lines below the %Grid line you specify the nodes of your function, i.e the x and y values, or x and t in your case. So the first line would contain all the x values in the yellow line. The second line contains the t values, e.g. 1, 2, 3, etc. You don't need a third line because you only have two arguments in the function.
Below the %Data line you specify all the data values f(x, t) for the nodes in your function. Those are all the values in the white lines in your Excel sheet. They do not need to be structured in lines. This will be done according to the data under %Grid.
Cheers,
Edgar
-------------------Edgar J. Kaiser
emPhys Physical Technology
www.emphys.com
Please login with a confirmed email address before reporting spam
Hello Marianna,
Please review the section on interpolation functions in the COMSOL Multiphysics v6.2 Reference Manual, starting on page 362. In particular it contains, at the top of page 364, an example of what the data file should look like for a function of two arguments. All manuals are available in the GUI via the File > Help > Documentation menu.
Best,
Jeff
-------------------Jeff Hiller
Please login with a confirmed email address before reporting spam
I understood that Marianna is intending to import a grid format, while the example on top of page 364 is a table. The grid format is shown on page 365 and unfortunately there is no example with numbers. I remember that it took me a little trial and error before I successfully imported a 3D grid a few years ago.
-------------------Edgar J. Kaiser
emPhys Physical Technology
www.emphys.com
Please login with a confirmed email address before reporting spam
Hi all,
Point taken: An example with numbers will be added to the documentation for the next version of COMSOL Multiphysics. Some aspects that also could be clarified: The rows with coordinates for the x, y, and z grid coordinate values (for 3D), under %Grid, should only include the unique values for each coordinate direction and do not have to have the same length (say 10 x values and 6 y values for a 10-by-6 2D grid). The data values, under %Data, should then contain 6 rows with 10 values in each, representing the data values for grid points along the x direction for all 6 y values with one y value for each row. In 3D, you would have to repeat the data in blocks with the y values repeated for each z value to give you at total of (number of y values) x (number of z values) rows.
Best regards, Magnus
Please login with a confirmed email address before reporting spam
Hi everyone, first of all thank you for helping me!
I finally fixed this problem and got the graph shown in the photos which is exactly what I expected.
Now I have another problem related to simulation convergence. I imposed the function int1(x,t) in the magnetic field-point constraint section as mf. By-(int1(x,t)) and I've tried changing some solver settings but it doesn't work.
Do you have any suggestions?
Attachments:
Please login with a confirmed email address before reporting spam
Hi Marianna,
First: I think you have an issue with units and the location of the int1 function. The function is set up with mm and Comsol expects m in the constraint.
Second: The x-axis is ranging 0 ... 100 mm, while your line in the geometry is located -50 ... +50 mm. I changed the constraint expression to: mf.By-(int1(0.001*x+0.05,t)) It may be better to set up the input data in a way that fits the geometry.
Third: In the time dependent solver it is advisable to choose manual time stepping to make sure the solver properly samples the function in the temporal axis. I changed the time stepping to 'Generalized Alpha' with manual stepping and a time step of 1/(f*100). You can check if a larger time step still gives good results.
Fourth: I think the mesh on the constrained lines is not fine enough to properly sample the spatial dimension of the constraint. I changed it to equidistant distribution with 1000 elements. Maybe less works as well.
The model solves with those changes, and you will see if the results match your expectations.
Cheers Edgar
-------------------Edgar J. Kaiser
emPhys Physical Technology
www.emphys.com
Please login with a confirmed email address before reporting spam
Hi Edga, thanks for the suggestions. Some I was able to implement, others I didn't. By chance you would be able to share my model with these changes made.
Best, Marianna
Please login with a confirmed email address before reporting spam
Sorry, I didn't save the test model. Where are you having issues?
-------------------Edgar J. Kaiser
emPhys Physical Technology
www.emphys.com
Please login with a confirmed email address before reporting spam
Hi Edgar,
I have a preoblem with this part:
*I changed the time stepping to 'Generalized Alpha' with manual stepping and a time step of 1/(f100). *
I didn't find the setting Generalized Alpha.
Please login with a confirmed email address before reporting spam
Go down along the Solver Configurations to the Time Dependent Solver and expand the tab 'Time Stepping'. Change the Method from BDF to Generalized Alpha, the Steps taken by solver to Manual and the Time step to 1/(f*100).
This approach typically works well on periodic waveforms.
Good luck!
-------------------Edgar J. Kaiser
emPhys Physical Technology
www.emphys.com
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.
Suggested Content
- FORUM How to import the f(x,y) function computed in MATLAB into COMSOL
- FORUM Import of Experimental Mesh & Data
- KNOWLEDGE BASE COMSOL 6.1 macOS Apple Silicon Native Support
- BLOG Solutions as Starting Point Values with LiveLink™ for MATLAB®
- KNOWLEDGE BASE Solving time dependent models with inconsistent initial values