-
Notifications
You must be signed in to change notification settings - Fork 13
[BUG/ISSUE] Matlab Fun() issues #56
Description
I'm trying to get a small mechanism running with Matlab/Octave but I'm running into a weird bug in the latest dev commit.
I have a new Matlab mechanism using small_strato, specified by m_small_strato.kpp:
#MODEL small_strato
#LANGUAGE matlab
#DOUBLE ON
#INTEGRATOR rosenbrock
#DRIVER general
#JACOBIAN SPARSE_LU_ROW
{This is the small_strato example from Chapter 2 of the KPP manual}
Running kpp this generates the code fine but it crashes on Fun() - looking at m_small_strato_Fun.m, the code is obviously wrong:
function [ Aout ] = m_small_strato_Fun ( V , F , RCT , Vdot )
% Local variables
% A - Rate for each equation
A=zeros(1,length(RCT));
Vdot=zeros(1,length(V));
% Computation of equation rates
A(1) = RCT(1)*F(2) ;
A(2) = RCT(2)*V(2)*F(2) ;
A(3) = RCT(3)*V(3) ;
A(4) = RCT(4)*V(2)*V(3) ;
A(5) = RCT(5)*V(3) ;
A(6) = RCT(6)*V(1)*F(1) ;
A(7) = RCT(7)*V(1)*V(3) ;
A(8) = RCT(8)*V(3)*V(4) ;
A(9) = RCT(9)*V(2)*V(5) ;
A(10) = RCT(10)*V(5) ;
% Aggregate function
Vdot(1) = A(5)-A(6)-A(7) ;
Vdot(2) = 2*A(1)-A(2)+A(3)-A(4)+A(6)-A(9)+A(10) ;
Vdot(3) = A(2)-A(3)-A(4)-A(5)-A(7)-A(8) ;
Vdot(4) = -A(8)+A(9)+A(10) ;
Vdot(5) = A(8)-A(9)-A(10) ;
return
% End of Fun functionFor some reason, Aout is set as the output argument. This is wrong. Looking at PACT-1D-HALOGENS' current code at https://github.com/PACT1D/PACT-1D-HALOGENS/blob/main/mechanism/mech_Fun.m
the declaration of Fun() should only have (V, F, RCT) and Vdot is the output argument:
function [ Vdot ] = mech_Fun ( V , F , RCT )
% Local variables
% A - Rate for each equation
A=zeros(1,length(RCT));
Vdot=zeros(1,length(V));
...I suspect that the function declaration at this line is not correct for MATLAB, but I'm not sure how to fix this. For some reason
if( z_useAggregate ) {
FunctionBegin( F_VAR, V, F, RCT, Vdot, Aout );
}
else {
FunctionBegin( FSPLIT_VAR, V, F, RCT, Vdot, P_VAR, D_VAR, Aout );
}Results in matlab generated code to have Aout as the output arg, not Vdot. I think Vdot is expected since Fun_Chem() uses it:
% This line calls the Matlab ODE function routine
P = m_small_strato_Fun( Y, FIX, RCONST );