Can COMSOL Integrate MATLAB Functions for Reaction-Diffusion Models?

In summary, Daniel said that COMSOL can't do the Jacobian assembly for m-function with dependent variable. He also said that other software can do this.
  • #1
TWNien
3
0
Hello,

Now I am working on the convection and diffusion problem with chemical reactions. The question is simple (but the answer is hard to me...) : I want to put a MATLAB function in the generation term, ex: the original one is " -k*c" , and I write a function r_c(x) which returns -k*x. In this case c is the dependent variable, and k is the reaction constant.

It seems to me that COMSOL can't do the Jacobian assembly for m-function with dependent variable (don't know how to compute the derivatives of m-function).

Does anyone have the same experience with the idea? Thanks for sharing.

Daniel
 
Physics news on Phys.org
  • #2
Yeah, if understood about right that's a problem haven't gotten around to myself (when using m - file - specified functions within the equation systems) directly but rather by specifying the derivatives manually (in the function definition). Often by specifying other m - file functions, where in complex cases compute the derivatives for example using numerical differentiation (in case analytical doesn't exist).
 
  • #3
PerennialII said:
Yeah, if understood about right that's a problem haven't gotten around to myself (when using m - file - specified functions within the equation systems) directly but rather by specifying the derivatives manually (in the function definition). Often by specifying other m - file functions, where in complex cases compute the derivatives for example using numerical differentiation (in case analytical doesn't exist).

Wow, thanks for your QUICK reply. I am so impressed.

Do you have any idea that other software can do this? I've tried specified other m-file for the derivatives, but not succeed yet even with the simplest problem. woe... There is still a long way to go if I want to incorporate more reaction rate function into the model.

Thank you PerennialII. It's so nice to meet you and this forum. I'll come more often and update my progress.


Daniel
 
  • #4
And forgot to welcome you to PF, belated one :biggrin: ! I'd say Comsol is build a bit different from other FE packages (pretty much all of them), and it doesn't "increment" or formulate the eq-systems like other software usually do (and I think that's one of the underlying issues affecting this as well, most software wouldn't complain about this, but then again they would lack many of the properties of Comsol). I've usually solved it simply by computing the derivatives like m-file(x+delta)-m-file(x-delta)/2*delta for models which are (for example) nonlinear and can't really get an analytical one (setting delta appropriately). And have gotten it to work with some pretty "weird" models.

Then again there are other tricks, developing things to series, adding stabilizing functions to aid differentiability etc., but those aren't really at the core of this problem.
 
  • #5
Hello, it's me again.
First, thanks for your hints. They are very helpful. So far I've made a big progress, and I want to make some remarks for this case. Actually, only one comment: the format of input and output. I was wrong that I thought COMSOL sends concentration of each points while calling m-function. In fact COMSOL sends c in the form of vector [c1 c2 c3 ... cn], that is I have to rewrite my m-function to deal with that.
After all things done, when I look back, it's kind of stupid stuck there for so long...

The next step will be using the better way to differentiate the m-function instead of using small delta.
 
  • #6
TWNien said:
Hello, it's me again.
First, thanks for your hints. They are very helpful. So far I've made a big progress, and I want to make some remarks for this case. Actually, only one comment: the format of input and output. I was wrong that I thought COMSOL sends concentration of each points while calling m-function. In fact COMSOL sends c in the form of vector [c1 c2 c3 ... cn], that is I have to rewrite my m-function to deal with that.
After all things done, when I look back, it's kind of stupid stuck there for so long...

The next step will be using the better way to differentiate the m-function instead of using small delta.

Yeah, the m-files should be made able to handle any form of input. Often get not-so-obvious error messages when fail to follow this "rule". Was doing some analyses of a highly nonlinear material last week and after a while of trying got it working best with a simple central - difference approach in an m-file combined with various smoothing functions to get it to accept a whole range of input. Although still favor developing pretty much everything complex in the m-files to a series (locally in a point of evaluation) and then working with them :biggrin: .
 

1. What is Comsol with matlab function?

Comsol with matlab function is a software tool that combines the capabilities of Comsol Multiphysics and Matlab to provide a powerful simulation and analysis platform for engineers and scientists. It allows for the creation of custom models using the Comsol interface and the use of Matlab functions for advanced analysis and post-processing.

2. How does Comsol with matlab function work?

The integration of Comsol and Matlab allows for the transfer of data and commands between the two programs. This means that users can create their models in Comsol, which generates a finite element mesh, and then use Matlab functions to manipulate and analyze the data from the simulation.

3. What are the benefits of using Comsol with matlab function?

Using Comsol with matlab function combines the strengths of both programs, allowing for more advanced and flexible simulation and analysis capabilities. It also streamlines the workflow, as users can create and analyze their models within one platform, saving time and effort.

4. Can Comsol with matlab function be used for any type of simulation?

Yes, Comsol with matlab function can be used for a wide range of simulations, including structural mechanics, heat transfer, fluid dynamics, and electromagnetics. It also supports multiphysics simulations, where multiple physical phenomena are coupled together.

5. How can I get started with using Comsol with matlab function?

To use Comsol with matlab function, you will need to have both Comsol Multiphysics and Matlab installed on your computer. You can then access the Comsol with matlab function interface within Comsol and begin creating and analyzing your models using Matlab functions. There are also tutorials and resources available to help you get started with the software.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top