Lars Gregersen
                                                                                                                                                    COMSOL Employee
                                                         
                            
                                                                                                                                                
                         
                                                
    
        Please login with a confirmed email address before reporting spam
     
    
 
                                                Posted:
                            
                                6 years ago                            
                            
                                17 juin 2019, 04:58 UTC−4                            
                        
                        Updated:
                            
                                6 years ago                            
                            
                                17 juin 2019, 04:57 UTC−4                            
                        
                        
                                                    Hi Ehsan
Yes, if you are using a MATLAB node in your Comsol model your function must be able to take arguments that are vectors of equal size and your function must return a vector of the same size as the inputs. You can test this using the Matlab command by passing vectors that you hace created yourself.
The reason is that each call to Matlab are rather slow and hence we need to batch the calls to speed things up.
You can insert a stop condition in your solver setup. The conditions used for stopping can depend on a Matlab function (and any other valid Comsol expression/function).
    -------------------
    Lars Gregersen
Comsol Denmark                                                
 
                                                
                            Hi Ehsan
Yes, if you are using a MATLAB node in your Comsol model your function must be able to take arguments that are vectors of equal size and your function must return a vector of the same size as the inputs. You can test this using the Matlab command by passing vectors that you hace created yourself.
The reason is that each call to Matlab are rather slow and hence we need to batch the calls to speed things up.
You can insert a stop condition in your solver setup. The conditions used for stopping can depend on a Matlab function (and any other valid Comsol expression/function).
                        
                                                
                                                                                                            
                                             
                        
                        
                                                
    
        Please login with a confirmed email address before reporting spam
     
    
 
                                                Posted:
                            
                                6 years ago                            
                            
                                20 juin 2019, 06:27 UTC−4                            
                        
                        Updated:
                            
                                6 years ago                            
                            
                                20 juin 2019, 06:29 UTC−4                            
                        
                        
                                                    
  Hi Ehsan
  
  Yes, if you are using a MATLAB node in your Comsol model your function must be able to take arguments that are vectors of equal size and your function must return a vector of the same size as the inputs. You can test this using the Matlab command by passing vectors that you hace created yourself.
  The reason is that each call to Matlab are rather slow and hence we need to batch the calls to speed things up.
  
  You can insert a stop condition in your solver setup. The conditions used for stopping can depend on a Matlab function (and any other valid Comsol expression/function).
Thank you for your attention!
I face with a problem. In fact, I wanna use this function as a stop condition.
this is my Matlab function:
function MI = MixingIndex(c)
sumc=0;
cavg=0;
for i=1:length(c)
    cavg= cavg+c(i);
end
cavg=(cavg/length(c));
for n=1: length(c)
a=(((c(n)-cavg/cavg)^2));
sumc=sumc+a;
end
MIpre=(1-sqrt((1/length(c))sumc))100;
X = zeros(1,length(c))
for i=1:length(c)
    X(i)=MIpre
end
MI=X;
=============================================
model.func.create('extm1', 'MATLAB');
model.func('extm1').setIndex('funcs', 'MixingIndex', 0, 0);
model.func('extm1').setIndex('funcs', 'ehs', 0, 1);
model.component('comp1').variable.create('var1');
model.component('comp1').variable('var1').selection.geom('geom1', 2);
model.component('comp1').variable('var1').selection.named('geom1_cad1_Face06Part8_bnd');
model.component('comp1').variable('var1').set('c_outlet', 'c');
model.component('comp1').probe.create('bnd1', 'Boundary');
model.component('comp1').probe('bnd1').set('intsurface', true);
model.component('comp1').probe('bnd1').selection.named('geom1_cad1_Face06Part8_bnd');
model.component('comp1').probe('bnd1').set('expr', 'MixingIndex(comp1.c_outlet[m^3/mol])');
model.component('comp1').probe.create('var1', 'GlobalVariable');
model.component('comp1').probe('var1').set('expr', '');
model.component('comp1').probe.remove('var1');
model.component('comp1').probe.create('var1', 'GlobalVariable');
model.component('comp1').probe('var1').set('expr', 'if(comp1.bnd1>1,(at(t,comp1.bnd1)-at(t-timestep,comp1.bnd1))/timestep,10[1/s])');
model.sol('sol1').feature('t1').create('st1', 'StopCondition');
model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondarr', '', 0);
model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondterminateon', 'true', 0);
model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondActive', true, 0);
model.sol('sol1').feature('t1').feature('st1').setIndex('stopconddesc', 'Stop expression 1', 0);
model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondarr', '', 0);
model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondterminateon', 'true', 0);
model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondActive', true, 0);
model.sol('sol1').feature('t1').feature('st1').setIndex('stopconddesc', 'Stop expression 1', 0);
model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondarr', 'comp1.var1<=0.2', 0);
%%%%%%% but the error is "could not evaluate stop condition %%%%%%%%
although all of the variables and probes evaluate during solution, It seems the stop condition could not access to the variable in previous time step!
what is your idea and suggestion?
                                                 
                                                
                            >Hi Ehsan
>
>Yes, if you are using a MATLAB node in your Comsol model your function must be able to take arguments that are vectors of equal size and your function must return a vector of the same size as the inputs. You can test this using the Matlab command by passing vectors that you hace created yourself.
>The reason is that each call to Matlab are rather slow and hence we need to batch the calls to speed things up.
>
>You can insert a stop condition in your solver setup. The conditions used for stopping can depend on a Matlab function (and any other valid Comsol expression/function).
Thank you for your attention!
I face with a problem. In fact, I wanna use this function as a stop condition.
this is my Matlab function:
function MI = MixingIndex(c)
sumc=0;
cavg=0;
for i=1:length(c)
    cavg= cavg+c(i);
end
cavg=(cavg/length(c));
for n=1: length(c)
a=(((c(n)-cavg/cavg)^2));
sumc=sumc+a;
end
MIpre=(1-sqrt((1/length(c))*sumc))*100;
X = zeros(1,length(c))
for i=1:length(c)
    X(i)=MIpre
end
MI=X;
=============================================
model.func.create('extm1', 'MATLAB');
model.func('extm1').setIndex('funcs', 'MixingIndex', 0, 0);
model.func('extm1').setIndex('funcs', 'ehs', 0, 1);
model.component('comp1').variable.create('var1');
model.component('comp1').variable('var1').selection.geom('geom1', 2);
model.component('comp1').variable('var1').selection.named('geom1_cad1_Face06Part8_bnd');
model.component('comp1').variable('var1').set('c_outlet', 'c');
model.component('comp1').probe.create('bnd1', 'Boundary');
model.component('comp1').probe('bnd1').set('intsurface', true);
model.component('comp1').probe('bnd1').selection.named('geom1_cad1_Face06Part8_bnd');
model.component('comp1').probe('bnd1').set('expr', 'MixingIndex(comp1.c_outlet[m^3/mol])');
model.component('comp1').probe.create('var1', 'GlobalVariable');
model.component('comp1').probe('var1').set('expr', '');
model.component('comp1').probe.remove('var1');
model.component('comp1').probe.create('var1', 'GlobalVariable');
model.component('comp1').probe('var1').set('expr', 'if(comp1.bnd1>1,(at(t,comp1.bnd1)-at(t-timestep,comp1.bnd1))/timestep,10[1/s])');
model.sol('sol1').feature('t1').create('st1', 'StopCondition');
model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondarr', '', 0);
model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondterminateon', 'true', 0);
model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondActive', true, 0);
model.sol('sol1').feature('t1').feature('st1').setIndex('stopconddesc', 'Stop expression 1', 0);
model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondarr', '', 0);
model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondterminateon', 'true', 0);
model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondActive', true, 0);
model.sol('sol1').feature('t1').feature('st1').setIndex('stopconddesc', 'Stop expression 1', 0);
model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondarr', 'comp1.var1