Appendix A: Linking The Bzzmath Library To Matlab
Appendix A: Linking The Bzzmath Library To Matlab
Appendix A: Linking The Bzzmath Library To Matlab
A.1
Introduction
A.2
BzzSum Function
The goal is to create a function (BzzSum) written in C++ language that exploits
the BzzMath libraries in the Matlab environment. The function requires the
following information as input:
A numerical vector (the values for which we are searching for the sum)
The vector size
Thus, those inputs are 1 double and 1 int. The function has a double as
output: the sum.
A typical function consists of three parts: header, functions, and MEX
function.
Differential and Differential-Algebraic Systems for the Chemical Engineer: Solving Numerical Problems,
First Edition. Guido Buzzi-Ferraris and Flavio Manenti.
2014 Wiley-VCH Verlag GmbH & Co. KGaA. Published 2014 by Wiley-VCH Verlag GmbH & Co. KGaA.
270 Appendix A: Linking the BzzMath Library to Matlab
A.2.1
Header File
A.2.2
MEX Function
Note that while the double variables need to be used as pointers, the int
variables can be used as is. It will be claried later in the appendix.
It is also suitable to immediately check the consistency of user input/output
information:
if (nrhs != 2)
mexErrMsgTxt(Two input arguments required);
else if (nlhs > 1)
mexErrMsgTxt(Too many output arguments);
It is necessary to create an array for the output and assign the input array to
the function pointer:
somma_out = mxCreateDoubleMatrix (1,1,mxREAL);
somma = mxGetPr(somma_out);
numeri = mxGetPr(numeri_in);
n = mxGetScalar(n_in);
The output of the matrix must be created with a specic statement. The com-
mands to assign the pointer to different variables are as follows:
}
*Somma = A.GetSumElements();
return;
}
The program is intentionally trivial to allow the focus on the structure of the
function. A BzzVector is adopted in this case and a for loop is implemented
to assign the elements of the mxArray to the BzzVector.
The function GetSumElements of BzzMath library is used and passed to
the output pointer.
A.2.4
Compiling
First, take care that the appropriate version of BzzMath is present into the work
folder (see the ReadMe le). Next, the following steps have to be accomplished:
Type mexsetup
Choose the Visual Studio Compiler
Type mex BzzSum.cpp BzzMath.lib to compile
the le BzzSum.mexw32 is created
Call the function as a common Matlab function:
numeri = [1:4];
n=length(numeri);
BzzSum(numeri,n)
It results in 10.
A.3
Chemical Engineering Example
V, yt , yb
F, zt , zb P T
L, xt , xb
A.3.1
Denition of a New Class
eq[1] = F * z1 - V * y1 - L * x1;
eq[2] = F * z2 - V * y2 - L * x2;
eq[3] = y1 - k1 * x1;
eq[4] = y2 - k2 * x2;
eq[5] = y1 + y2 - x1 - x2;
eq[6] = F - V - L;
}
The virtual function GetResiduals will contain the model of the ash. Note
that in the model appears the vector data used in the denition of the new class
NLSO.
A.3.2
Main Program in C++
The vector Dati is passed to the object obj. The solution of the system is
evaluated and stored in the Soluzione vector and the residuals of equations
are stored in the Valori vector. The outputs are printed in appropriate les.
//C++ part MEX function
void mexFunction(
int nlhs,
mxArray *plhs[],
int nrhs,
const mxArray *prhs[]
)
{
double *incognite;
int n_incognite;
double *Soluzione;
double *dati;
int n_dati;
if (nrhs != 4)
mexErrMsgTxt(4 input arguments required);
else if (nlhs > 0)
mexErrMsgTxt(Too many output arguments);
dati = mxGetPr(dati_in); //for pointers
n_dati = mxGetScalar(n_dati_in); //for scalars
n_incognite = mxGetScalar(n_incognite_in);
incognite = mxGetPr(incognite_in);
main(dati,n_dati,incognite,n_incognite);
return;
}
Pointers to the data coming from Matlab are created. A check for the number
of inputs and outputs is performed. The pointers are associated with the data
variables and the main function is called.
A.3 Chemical Engineering Example 277
A.3.3
Main Program in Matlab