//////////////////////////////////////////////////////////////////////// // simulate.cc // // simulate file designed for GNU GCC C++ compiler // // use NO_COMPILER option in MODEL command // //////////////////////////////////////////////////////////////////////// #include "Simulate.hh" #include "Interpreter.hh" #include "Mem_Mngr.hh" #include "LinBCG.hh" int max(int a, int b) { if (a>b) return(a); else return(b); } /*class EvalException { };*/ //#define DEBUG /* The gateway routine */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { mxArray *M_, *oo_, *options_; int i, row_y, col_y, row_x, col_x; double * pind ; double *direction; //mexPrintf("mexFunction\n"); //mexEvalString("drawnow;"); /* Gets model parameters from global workspace of Matlab */ //mexPrintf("starting simulation\n"); M_ = mexGetVariable("global","M_"); if (M_ == NULL ) { mexPrintf("Global variable not found : "); mexEvalString("st=fclose('all');clear all;"); mexErrMsgTxt("M_ \n"); } /* Gets variables and parameters from global workspace of Matlab */ oo_ = mexGetVariable("global","oo_"); if (oo_ == NULL ) { mexPrintf("Global variable not found : "); mexEvalString("st=fclose('all');clear all;"); mexErrMsgTxt("oo_ \n"); } options_ = mexGetVariable("global","options_"); if (options_ == NULL ) { mexPrintf("Global variable not found : "); mexEvalString("st=fclose('all');clear all;"); mexErrMsgTxt("options_ \n"); } //mexPrintf("ok0\n"); params = mxGetPr(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_,"params"))); double *yd, *xd; yd= mxGetPr(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_,"endo_simul"))); row_y=mxGetM(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_,"endo_simul"))); xd= mxGetPr(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_,"exo_simul"))); row_x=mxGetM(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_,"exo_simul"))); col_x=mxGetN(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_,"exo_simul"))); y_kmin=int(floor(*(mxGetPr(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_,"maximum_lag")))))); y_kmax=int(floor(*(mxGetPr(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_,"maximum_lead")))))); y_decal=max(0,y_kmin-int(floor(*(mxGetPr(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_,"maximum_endo_lag"))))))); periods=int(floor(*(mxGetPr(mxGetFieldByNumber(options_, 0, mxGetFieldNumber(options_,"periods")))))); maxit_=int(floor(*(mxGetPr(mxGetFieldByNumber(options_, 0, mxGetFieldNumber(options_,"maxit_")))))); slowc=double(*(mxGetPr(mxGetFieldByNumber(options_, 0, mxGetFieldNumber(options_,"slowc"))))); slowc_save=slowc; markowitz_c=double(*(mxGetPr(mxGetFieldByNumber(options_, 0, mxGetFieldNumber(options_,"markowitz"))))); nb_row_xd=int(floor(*(mxGetPr(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_,"exo_det_nbr")))))); mxArray *mxa=mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_,"fname")); int buflen=mxGetM(mxa) * mxGetN(mxa) + 1; char *fname; fname=(char*)mxCalloc(buflen, sizeof(char)); string file_name=fname; int status = mxGetString(mxa, fname, buflen); if (status != 0) mexWarnMsgTxt("Not enough space. Filename is truncated."); //mexPrintf("fname=%s\n",fname); col_y=mxGetN(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_,"endo_simul")));; if (col_y0) { plhs[0] = mxCreateDoubleMatrix(row_y, col_y, mxREAL); pind = mxGetPr(plhs[0]); for (i=0;i