2009-12-16 14:21:31 +01:00
|
|
|
/*
|
2012-06-08 18:22:34 +02:00
|
|
|
* Copyright (C) 2007-2012 Dynare Team
|
2008-01-11 14:42:14 +01:00
|
|
|
*
|
|
|
|
* This file is part of Dynare.
|
|
|
|
*
|
|
|
|
* Dynare is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Dynare is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2010-02-05 12:05:21 +01:00
|
|
|
#include <cstring>
|
2009-12-16 14:21:31 +01:00
|
|
|
#include "Interpreter.hh"
|
2007-10-06 00:11:47 +02:00
|
|
|
|
2009-07-10 17:10:11 +02:00
|
|
|
#ifdef DEBUG_EX
|
2007-10-04 00:01:08 +02:00
|
|
|
|
2009-07-10 17:10:11 +02:00
|
|
|
using namespace std;
|
2009-12-16 18:18:38 +01:00
|
|
|
# include <sstream>
|
2010-02-05 12:05:21 +01:00
|
|
|
|
2009-09-11 19:06:54 +02:00
|
|
|
string
|
|
|
|
Get_Argument(const char *argv)
|
|
|
|
{
|
|
|
|
string f(argv);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-07-10 17:10:11 +02:00
|
|
|
#else
|
2009-09-11 19:06:54 +02:00
|
|
|
|
|
|
|
string
|
|
|
|
Get_Argument(const mxArray *prhs)
|
|
|
|
{
|
|
|
|
const mxArray *mxa = prhs;
|
2009-12-16 18:18:38 +01:00
|
|
|
int buflen = mxGetM(mxa) * mxGetN(mxa) + 1;
|
2009-09-11 19:06:54 +02:00
|
|
|
char *first_argument;
|
2009-12-16 18:18:38 +01:00
|
|
|
first_argument = (char *) mxCalloc(buflen, sizeof(char));
|
2009-09-11 19:06:54 +02:00
|
|
|
int status = mxGetString(mxa, first_argument, buflen);
|
|
|
|
if (status != 0)
|
|
|
|
mexWarnMsgTxt("Not enough space. The first argument is truncated.");
|
|
|
|
string f(first_argument);
|
|
|
|
mxFree(first_argument);
|
|
|
|
return f;
|
|
|
|
}
|
2010-10-11 19:21:32 +02:00
|
|
|
#endif
|
2009-12-16 14:21:31 +01:00
|
|
|
|
2009-12-16 18:18:38 +01:00
|
|
|
void
|
2010-10-11 19:21:32 +02:00
|
|
|
Get_Arguments_and_global_variables(int nrhs,
|
2010-08-18 13:51:57 +02:00
|
|
|
#ifndef DEBUG_EX
|
2010-10-11 19:21:32 +02:00
|
|
|
const mxArray *prhs[],
|
2010-08-18 13:51:57 +02:00
|
|
|
#else
|
2010-10-11 19:21:32 +02:00
|
|
|
const char *prhs[],
|
2010-08-18 13:51:57 +02:00
|
|
|
#endif
|
2010-10-11 19:21:32 +02:00
|
|
|
int &count_array_argument,
|
|
|
|
double *yd[], unsigned int &row_y, unsigned int &col_y,
|
|
|
|
double *xd[], unsigned int &row_x, unsigned int &col_x,
|
2011-03-14 17:46:00 +01:00
|
|
|
double *params[],
|
|
|
|
double *steady_yd[], unsigned int &steady_row_y, unsigned int &steady_col_y,
|
|
|
|
unsigned int &periods,
|
2010-10-11 19:21:32 +02:00
|
|
|
#ifndef DEBUG_EX
|
|
|
|
mxArray *block_structur[],
|
|
|
|
#endif
|
|
|
|
bool &steady_state, bool &evaluate, int &block,
|
2010-12-31 16:41:50 +01:00
|
|
|
mxArray *M_[], mxArray *oo_[], mxArray *options_[], bool &global_temporary_terms,
|
2011-01-14 19:22:29 +01:00
|
|
|
bool &print,
|
2011-06-17 16:37:36 +02:00
|
|
|
bool &print_error,
|
2011-02-04 16:53:12 +01:00
|
|
|
mxArray *GlobalTemporaryTerms[])
|
2010-10-11 19:21:32 +02:00
|
|
|
{
|
2010-08-18 13:51:57 +02:00
|
|
|
#ifdef DEBUG_EX
|
2010-10-11 19:21:32 +02:00
|
|
|
for (int i = 2; i < nrhs; i++)
|
2010-08-18 13:51:57 +02:00
|
|
|
#else
|
2011-02-04 16:53:12 +01:00
|
|
|
for (int i = 0; i < nrhs; i++)
|
2010-08-18 13:51:57 +02:00
|
|
|
#endif
|
2011-02-04 16:53:12 +01:00
|
|
|
{
|
2010-08-18 13:51:57 +02:00
|
|
|
#ifndef DEBUG_EX
|
2011-02-04 16:53:12 +01:00
|
|
|
if (!mxIsChar(prhs[i]))
|
|
|
|
{
|
|
|
|
switch (count_array_argument)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
*yd = mxGetPr(prhs[i]);
|
|
|
|
row_y = mxGetM(prhs[i]);
|
|
|
|
col_y = mxGetN(prhs[i]);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
*xd = mxGetPr(prhs[i]);
|
|
|
|
row_x = mxGetM(prhs[i]);
|
|
|
|
col_x = mxGetN(prhs[i]);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
*params = mxGetPr(prhs[i]);
|
|
|
|
break;
|
|
|
|
case 3:
|
2011-03-14 17:46:00 +01:00
|
|
|
*steady_yd = mxGetPr(prhs[i]);
|
|
|
|
steady_row_y = mxGetM(prhs[i]);
|
|
|
|
steady_col_y = mxGetN(prhs[i]);
|
2011-02-04 16:53:12 +01:00
|
|
|
break;
|
|
|
|
case 4:
|
2011-03-14 17:46:00 +01:00
|
|
|
periods = mxGetScalar(prhs[i]);
|
2011-02-04 16:53:12 +01:00
|
|
|
break;
|
|
|
|
case 5:
|
2011-03-14 17:46:00 +01:00
|
|
|
*block_structur = mxDuplicateArray(prhs[i]);
|
|
|
|
break;
|
|
|
|
case 6:
|
2011-02-04 16:53:12 +01:00
|
|
|
global_temporary_terms = true;
|
|
|
|
*GlobalTemporaryTerms = mxDuplicateArray(prhs[i]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
//mexPrintf("Unknown argument count_array_argument=%d\n",count_array_argument);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
count_array_argument++;
|
|
|
|
}
|
|
|
|
else
|
2010-08-18 13:51:57 +02:00
|
|
|
#endif
|
2011-02-04 16:53:12 +01:00
|
|
|
if (Get_Argument(prhs[i]) == "static")
|
|
|
|
steady_state = true;
|
|
|
|
else if (Get_Argument(prhs[i]) == "dynamic")
|
|
|
|
steady_state = false;
|
|
|
|
else if (Get_Argument(prhs[i]) == "evaluate")
|
|
|
|
evaluate = true;
|
|
|
|
else if (Get_Argument(prhs[i]) == "global_temporary_terms")
|
|
|
|
global_temporary_terms = true;
|
|
|
|
else if (Get_Argument(prhs[i]) == "print")
|
|
|
|
print = true;
|
2011-06-17 16:37:36 +02:00
|
|
|
else if (Get_Argument(prhs[i]) == "no_print_error")
|
|
|
|
print_error = false;
|
2010-10-11 19:21:32 +02:00
|
|
|
else
|
|
|
|
{
|
2011-02-04 16:53:12 +01:00
|
|
|
int pos = Get_Argument(prhs[i]).find("block");
|
|
|
|
if (pos != (int) string::npos)
|
|
|
|
{
|
|
|
|
int pos1 = Get_Argument(prhs[i]).find("=", pos+5);
|
|
|
|
if (pos1 != (int) string::npos)
|
|
|
|
pos = pos1 + 1;
|
|
|
|
else
|
|
|
|
pos += 5;
|
|
|
|
block = atoi(Get_Argument(prhs[i]).substr(pos, string::npos).c_str())-1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ostringstream tmp;
|
|
|
|
tmp << " in main, unknown argument : " << Get_Argument(prhs[i]) << "\n";
|
|
|
|
throw FatalExceptionHandling(tmp.str());
|
|
|
|
}
|
2010-10-11 19:21:32 +02:00
|
|
|
}
|
2011-02-04 16:53:12 +01:00
|
|
|
}
|
2011-03-14 17:46:00 +01:00
|
|
|
if (count_array_argument > 0 && count_array_argument < 5)
|
2010-07-23 11:20:24 +02:00
|
|
|
{
|
2010-10-11 19:21:32 +02:00
|
|
|
if (count_array_argument == 3 && steady_state)
|
|
|
|
periods = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ostringstream tmp;
|
2011-03-14 17:46:00 +01:00
|
|
|
tmp << " in main, missing arguments. All the following arguments have to be indicated y, x, params, it_, ys\n";
|
2010-10-11 19:21:32 +02:00
|
|
|
throw FatalExceptionHandling(tmp.str());
|
|
|
|
}
|
2010-07-23 11:20:24 +02:00
|
|
|
}
|
2010-10-11 19:21:32 +02:00
|
|
|
*M_ = mexGetVariable("global", "M_");
|
2009-12-16 18:18:38 +01:00
|
|
|
if (M_ == NULL)
|
2007-10-04 00:01:08 +02:00
|
|
|
{
|
2010-09-24 12:52:58 +02:00
|
|
|
ostringstream tmp;
|
|
|
|
tmp << " in main, global variable not found: M_\n";
|
|
|
|
throw FatalExceptionHandling(tmp.str());
|
2007-10-04 00:01:08 +02:00
|
|
|
}
|
|
|
|
/* Gets variables and parameters from global workspace of Matlab */
|
2010-10-11 19:21:32 +02:00
|
|
|
*oo_ = mexGetVariable("global", "oo_");
|
2009-12-16 18:18:38 +01:00
|
|
|
if (oo_ == NULL)
|
2007-10-04 00:01:08 +02:00
|
|
|
{
|
2010-09-24 12:52:58 +02:00
|
|
|
ostringstream tmp;
|
|
|
|
tmp << " in main, global variable not found: oo_\n";
|
|
|
|
throw FatalExceptionHandling(tmp.str());
|
2007-10-04 00:01:08 +02:00
|
|
|
}
|
2010-10-11 19:21:32 +02:00
|
|
|
*options_ = mexGetVariable("global", "options_");
|
2009-12-16 18:18:38 +01:00
|
|
|
if (options_ == NULL)
|
2007-10-04 00:01:08 +02:00
|
|
|
{
|
2010-09-24 12:52:58 +02:00
|
|
|
ostringstream tmp;
|
|
|
|
tmp << " in main, global variable not found: options_\n";
|
|
|
|
throw FatalExceptionHandling(tmp.str());
|
2007-10-04 00:01:08 +02:00
|
|
|
}
|
2010-10-11 19:21:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_EX
|
|
|
|
int
|
|
|
|
main(int nrhs, const char *prhs[])
|
|
|
|
#else
|
|
|
|
/* The gateway routine */
|
2011-02-04 16:53:12 +01:00
|
|
|
void
|
|
|
|
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
|
2010-10-11 19:21:32 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
mxArray *M_, *oo_, *options_;
|
2011-02-04 16:53:12 +01:00
|
|
|
mxArray *GlobalTemporaryTerms;
|
2010-10-11 19:21:32 +02:00
|
|
|
#ifndef DEBUG_EX
|
|
|
|
mxArray *block_structur = NULL;
|
|
|
|
#else
|
|
|
|
int nlhs = 0;
|
|
|
|
char *plhs[1];
|
2011-02-04 16:53:12 +01:00
|
|
|
load_global((char *) prhs[1]);
|
2010-10-11 19:21:32 +02:00
|
|
|
#endif
|
|
|
|
//ErrorHandlingException error_handling;
|
|
|
|
unsigned int i, row_y = 0, col_y = 0, row_x = 0, col_x = 0, nb_row_xd = 0;
|
2012-02-17 10:59:39 +01:00
|
|
|
unsigned int steady_row_y, steady_col_y;
|
2010-10-11 19:21:32 +02:00
|
|
|
int y_kmin = 0, y_kmax = 0, y_decal = 0;
|
|
|
|
unsigned int periods = 1;
|
|
|
|
double *direction;
|
|
|
|
bool steady_state = false;
|
|
|
|
bool evaluate = false;
|
|
|
|
int block = -1;
|
|
|
|
double *params = NULL;
|
|
|
|
double *yd = NULL, *xd = NULL;
|
|
|
|
int count_array_argument = 0;
|
2010-12-31 16:41:50 +01:00
|
|
|
bool global_temporary_terms = false;
|
2011-12-12 12:40:46 +01:00
|
|
|
bool print = false, print_error = true, print_it = false;
|
2011-03-14 17:46:00 +01:00
|
|
|
double *steady_yd = NULL, *steady_xd = NULL;
|
|
|
|
|
2010-10-11 19:21:32 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Get_Arguments_and_global_variables(nrhs, prhs, count_array_argument,
|
2011-02-04 16:53:12 +01:00
|
|
|
&yd, row_y, col_y,
|
|
|
|
&xd, row_x, col_x,
|
2011-03-14 17:46:00 +01:00
|
|
|
¶ms,
|
|
|
|
&steady_yd, steady_row_y, steady_col_y,
|
|
|
|
periods,
|
2010-10-11 19:21:32 +02:00
|
|
|
#ifndef DEBUG_EX
|
2011-02-04 16:53:12 +01:00
|
|
|
&block_structur,
|
2010-10-11 19:21:32 +02:00
|
|
|
#endif
|
2011-02-04 16:53:12 +01:00
|
|
|
steady_state, evaluate, block,
|
|
|
|
&M_, &oo_, &options_, global_temporary_terms,
|
2011-06-17 16:37:36 +02:00
|
|
|
print, print_error, &GlobalTemporaryTerms);
|
2010-10-11 19:21:32 +02:00
|
|
|
}
|
|
|
|
catch (GeneralExceptionHandling &feh)
|
|
|
|
{
|
|
|
|
DYN_MEX_FUNC_ERR_MSG_TXT(feh.GetErrorMsg().c_str());
|
|
|
|
}
|
|
|
|
|
2010-07-23 11:20:24 +02:00
|
|
|
if (!count_array_argument)
|
|
|
|
params = mxGetPr(mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_, "params")));
|
2010-09-24 12:52:58 +02:00
|
|
|
|
2011-03-14 17:46:00 +01:00
|
|
|
|
2009-12-16 18:18:38 +01:00
|
|
|
if (!steady_state)
|
2009-08-25 11:43:01 +02:00
|
|
|
{
|
2010-07-23 11:20:24 +02:00
|
|
|
if (!count_array_argument)
|
|
|
|
{
|
|
|
|
yd = mxGetPr(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_, "endo_simul")));
|
|
|
|
row_y = mxGetM(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_, "endo_simul")));
|
2010-08-18 13:51:57 +02:00
|
|
|
col_y = mxGetN(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_, "endo_simul")));
|
2010-07-23 11:20:24 +02:00
|
|
|
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")));
|
2011-03-18 01:09:20 +01:00
|
|
|
nb_row_xd = row_x;
|
2010-07-23 11:20:24 +02:00
|
|
|
}
|
2009-08-25 11:43:01 +02:00
|
|
|
|
2009-12-16 18:18:38 +01:00
|
|
|
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")))))));
|
2010-07-23 11:20:24 +02:00
|
|
|
if (!count_array_argument)
|
|
|
|
periods = int (floor(*(mxGetPr(mxGetFieldByNumber(options_, 0, mxGetFieldNumber(options_, "periods"))))));
|
2011-03-14 17:46:00 +01:00
|
|
|
if (!steady_yd )
|
|
|
|
{
|
|
|
|
steady_yd = mxGetPr(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_, "steady_state")));
|
|
|
|
steady_row_y = mxGetM(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_, "steady_state")));
|
2012-02-17 10:59:39 +01:00
|
|
|
steady_col_y = mxGetN(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_, "steady_state")));;
|
2011-03-14 17:46:00 +01:00
|
|
|
}
|
2009-12-16 18:18:38 +01:00
|
|
|
steady_xd = mxGetPr(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_, "exo_steady_state")));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-07-23 11:20:24 +02:00
|
|
|
if (!count_array_argument)
|
|
|
|
{
|
|
|
|
yd = mxGetPr(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_, "steady_state")));
|
|
|
|
row_y = mxGetM(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_, "steady_state")));
|
|
|
|
col_y = mxGetN(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_, "steady_state")));;
|
2010-08-18 13:51:57 +02:00
|
|
|
|
2010-07-23 11:20:24 +02:00
|
|
|
xd = mxGetPr(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_, "exo_steady_state")));
|
|
|
|
row_x = mxGetM(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_, "exo_steady_state")));
|
|
|
|
col_x = mxGetN(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_, "exo_steady_state")));
|
2011-03-18 01:09:20 +01:00
|
|
|
nb_row_xd = row_x;
|
2010-07-23 11:20:24 +02:00
|
|
|
}
|
2009-08-25 11:43:01 +02:00
|
|
|
}
|
2011-12-26 14:26:32 +01:00
|
|
|
int verbose= int(*mxGetPr((mxGetFieldByNumber(options_, 0, mxGetFieldNumber(options_, "verbosity")))));
|
2011-12-12 12:40:46 +01:00
|
|
|
if (verbose)
|
|
|
|
print_it = true;
|
2009-12-16 18:18:38 +01:00
|
|
|
int maxit_ = int (floor(*(mxGetPr(mxGetFieldByNumber(options_, 0, mxGetFieldNumber(options_, "maxit_"))))));
|
|
|
|
double slowc = double (*(mxGetPr(mxGetFieldByNumber(options_, 0, mxGetFieldNumber(options_, "slowc")))));
|
|
|
|
double markowitz_c = double (*(mxGetPr(mxGetFieldByNumber(options_, 0, mxGetFieldNumber(options_, "markowitz")))));
|
|
|
|
int minimal_solving_periods = int (*(mxGetPr(mxGetFieldByNumber(options_, 0, mxGetFieldNumber(options_, "minimal_solving_periods")))));
|
2010-07-23 11:20:24 +02:00
|
|
|
int stack_solve_algo = int (*(mxGetPr(mxGetFieldByNumber(options_, 0, mxGetFieldNumber(options_, "stack_solve_algo")))));
|
2010-08-18 13:51:57 +02:00
|
|
|
int solve_algo;
|
2009-08-25 11:43:01 +02:00
|
|
|
double solve_tolf;
|
2009-12-16 18:18:38 +01:00
|
|
|
if (steady_state)
|
2011-02-04 16:53:12 +01:00
|
|
|
{
|
|
|
|
solve_algo = int (*(mxGetPr(mxGetFieldByNumber(options_, 0, mxGetFieldNumber(options_, "solve_algo")))));
|
|
|
|
solve_tolf = *(mxGetPr(mxGetFieldByNumber(options_, 0, mxGetFieldNumber(options_, "solve_tolf"))));
|
|
|
|
}
|
2009-12-16 18:18:38 +01:00
|
|
|
else
|
2010-08-18 13:51:57 +02:00
|
|
|
{
|
|
|
|
solve_algo = stack_solve_algo;
|
2011-12-12 11:34:27 +01:00
|
|
|
mxArray *dynatol = mxGetFieldByNumber(options_, 0, mxGetFieldNumber(options_, "dynatol"));
|
|
|
|
solve_tolf= *mxGetPr((mxGetFieldByNumber(dynatol, 0, mxGetFieldNumber(dynatol, "f"))));
|
2010-08-18 13:51:57 +02:00
|
|
|
}
|
|
|
|
|
2009-12-16 18:18:38 +01:00
|
|
|
mxArray *mxa = mxGetFieldByNumber(M_, 0, mxGetFieldNumber(M_, "fname"));
|
|
|
|
int buflen = mxGetM(mxa) * mxGetN(mxa) + 1;
|
2007-10-04 00:01:08 +02:00
|
|
|
char *fname;
|
2010-08-18 13:51:57 +02:00
|
|
|
fname = (char *) mxCalloc(buflen+1, sizeof(char));
|
2007-10-04 00:01:08 +02:00
|
|
|
int status = mxGetString(mxa, fname, buflen);
|
2010-08-18 13:51:57 +02:00
|
|
|
fname[buflen] = ' ';
|
2007-10-04 00:01:08 +02:00
|
|
|
if (status != 0)
|
|
|
|
mexWarnMsgTxt("Not enough space. Filename is truncated.");
|
2010-08-18 13:51:57 +02:00
|
|
|
string file_name = fname;
|
2009-08-25 11:43:01 +02:00
|
|
|
|
2009-12-16 18:18:38 +01:00
|
|
|
int size_of_direction = col_y*row_y*sizeof(double);
|
|
|
|
double *y = (double *) mxMalloc(size_of_direction);
|
|
|
|
double *ya = (double *) mxMalloc(size_of_direction);
|
|
|
|
direction = (double *) mxMalloc(size_of_direction);
|
|
|
|
memset(direction, 0, size_of_direction);
|
2010-12-31 16:41:50 +01:00
|
|
|
|
2009-12-16 18:18:38 +01:00
|
|
|
double *x = (double *) mxMalloc(col_x*row_x*sizeof(double));
|
|
|
|
for (i = 0; i < row_x*col_x; i++)
|
|
|
|
x[i] = double (xd[i]);
|
|
|
|
for (i = 0; i < row_y*col_y; i++)
|
2009-09-11 19:06:54 +02:00
|
|
|
{
|
2009-12-16 18:18:38 +01:00
|
|
|
y[i] = double (yd[i]);
|
|
|
|
ya[i] = double (yd[i]);
|
2009-09-11 19:06:54 +02:00
|
|
|
}
|
2009-12-16 18:18:38 +01:00
|
|
|
int y_size = row_y;
|
|
|
|
int nb_row_x = row_x;
|
|
|
|
clock_t t0 = clock();
|
2010-10-11 19:21:32 +02:00
|
|
|
|
2011-06-17 16:37:36 +02:00
|
|
|
Interpreter interprete(params, y, ya, x, steady_yd, steady_xd, direction, y_size, nb_row_x, nb_row_xd, periods, y_kmin, y_kmax, maxit_, solve_tolf, size_of_direction, slowc, y_decal, markowitz_c, file_name, minimal_solving_periods, stack_solve_algo, solve_algo, global_temporary_terms, print, print_error, GlobalTemporaryTerms);
|
2010-10-11 19:21:32 +02:00
|
|
|
|
2007-10-04 00:01:08 +02:00
|
|
|
string f(fname);
|
2010-08-18 13:51:57 +02:00
|
|
|
mxFree(fname);
|
2010-07-23 11:20:24 +02:00
|
|
|
int nb_blocks = 0;
|
2010-09-24 12:52:58 +02:00
|
|
|
double *pind;
|
2012-02-17 10:59:39 +01:00
|
|
|
bool no_error = true;
|
2010-09-24 12:52:58 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2012-02-17 10:59:39 +01:00
|
|
|
interprete.compute_blocks(f, f, steady_state, evaluate, block, nb_blocks,print_it);
|
2010-09-24 12:52:58 +02:00
|
|
|
}
|
|
|
|
catch (GeneralExceptionHandling &feh)
|
|
|
|
{
|
|
|
|
DYN_MEX_FUNC_ERR_MSG_TXT(feh.GetErrorMsg().c_str());
|
|
|
|
}
|
|
|
|
|
2009-12-16 18:18:38 +01:00
|
|
|
clock_t t1 = clock();
|
2011-12-12 12:40:46 +01:00
|
|
|
if (!steady_state && !evaluate && no_error && print)
|
2009-12-16 18:18:38 +01:00
|
|
|
mexPrintf("Simulation Time=%f milliseconds\n", 1000.0*(double (t1)-double (t0))/double (CLOCKS_PER_SEC));
|
2010-08-18 13:51:57 +02:00
|
|
|
#ifndef DEBUG_EX
|
2010-10-11 19:21:32 +02:00
|
|
|
bool dont_store_a_structure = false;
|
2009-12-16 18:18:38 +01:00
|
|
|
if (nlhs > 0)
|
2007-10-04 00:01:08 +02:00
|
|
|
{
|
2010-09-24 12:52:58 +02:00
|
|
|
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
|
2007-10-04 00:01:08 +02:00
|
|
|
pind = mxGetPr(plhs[0]);
|
2010-09-24 12:52:58 +02:00
|
|
|
if (no_error)
|
|
|
|
pind[0] = 0;
|
2009-09-11 19:06:54 +02:00
|
|
|
else
|
2010-09-24 12:52:58 +02:00
|
|
|
pind[0] = 1;
|
2009-12-16 18:18:38 +01:00
|
|
|
if (nlhs > 1)
|
|
|
|
{
|
2010-10-11 19:21:32 +02:00
|
|
|
if (block >= 0)
|
|
|
|
{
|
|
|
|
if (evaluate)
|
|
|
|
{
|
|
|
|
vector<double> residual = interprete.get_residual();
|
|
|
|
plhs[1] = mxCreateDoubleMatrix(residual.size()/col_y, col_y, mxREAL);
|
|
|
|
pind = mxGetPr(plhs[1]);
|
|
|
|
for (i = 0; i < residual.size(); i++)
|
|
|
|
pind[i] = residual[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
plhs[1] = mxCreateDoubleMatrix(row_y, col_y, mxREAL);
|
|
|
|
pind = mxGetPr(plhs[1]);
|
|
|
|
for (i = 0; i < row_y*col_y; i++)
|
|
|
|
pind[i] = y[i];
|
|
|
|
}
|
|
|
|
}
|
2009-12-16 18:18:38 +01:00
|
|
|
else
|
2010-10-11 19:21:32 +02:00
|
|
|
{
|
|
|
|
plhs[1] = mxCreateDoubleMatrix(row_y, col_y, mxREAL);
|
|
|
|
pind = mxGetPr(plhs[1]);
|
|
|
|
if (evaluate)
|
2011-01-31 12:30:16 +01:00
|
|
|
{
|
|
|
|
vector<double> residual = interprete.get_residual();
|
|
|
|
for (i = 0; i < residual.size(); i++)
|
|
|
|
pind[i] = residual[i];
|
|
|
|
}
|
2010-10-11 19:21:32 +02:00
|
|
|
else
|
|
|
|
for (i = 0; i < row_y*col_y; i++)
|
2011-02-04 16:53:12 +01:00
|
|
|
pind[i] = y[i];
|
2010-10-11 19:21:32 +02:00
|
|
|
}
|
2010-07-23 11:20:24 +02:00
|
|
|
if (nlhs > 2)
|
|
|
|
{
|
2011-02-02 00:31:37 +01:00
|
|
|
if (evaluate)
|
2010-07-23 11:20:24 +02:00
|
|
|
{
|
2011-02-02 00:31:37 +01:00
|
|
|
int jacob_field_number = 0, jacob_exo_field_number = 0, jacob_exo_det_field_number = 0, jacob_other_endo_field_number = 0;
|
|
|
|
if (!block_structur)
|
|
|
|
{
|
2011-02-04 16:53:12 +01:00
|
|
|
const char *field_names[] = {"g1", "g1_x", "g1_xd", "g1_o"};
|
|
|
|
jacob_field_number = 0;
|
|
|
|
jacob_exo_field_number = 1;
|
|
|
|
jacob_exo_det_field_number = 2;
|
2011-06-17 16:37:36 +02:00
|
|
|
jacob_other_endo_field_number = 3;
|
2011-02-02 00:31:37 +01:00
|
|
|
mwSize dims[1] = {nb_blocks };
|
|
|
|
plhs[2] = mxCreateStructArray(1, dims, 4, field_names);
|
|
|
|
}
|
|
|
|
else if (!mxIsStruct(block_structur))
|
|
|
|
{
|
|
|
|
plhs[2] = interprete.get_jacob(0);
|
|
|
|
//mexCallMATLAB(0,NULL, 1, &plhs[2], "disp");
|
|
|
|
dont_store_a_structure = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
plhs[2] = block_structur;
|
|
|
|
jacob_field_number = mxAddField(plhs[2], "g1");
|
|
|
|
if (jacob_field_number == -1)
|
|
|
|
DYN_MEX_FUNC_ERR_MSG_TXT("Fatal error in bytecode: in main, cannot add extra field jacob to the structArray\n");
|
|
|
|
jacob_exo_field_number = mxAddField(plhs[2], "g1_x");
|
|
|
|
if (jacob_exo_field_number == -1)
|
|
|
|
DYN_MEX_FUNC_ERR_MSG_TXT("Fatal error in bytecode: in main, cannot add extra field jacob_exo to the structArray\n");
|
|
|
|
jacob_exo_det_field_number = mxAddField(plhs[2], "g1_xd");
|
|
|
|
if (jacob_exo_det_field_number == -1)
|
|
|
|
DYN_MEX_FUNC_ERR_MSG_TXT("Fatal error in bytecode: in main, cannot add extra field jacob_exo_det to the structArray\n");
|
|
|
|
jacob_other_endo_field_number = mxAddField(plhs[2], "g1_o");
|
|
|
|
if (jacob_other_endo_field_number == -1)
|
|
|
|
DYN_MEX_FUNC_ERR_MSG_TXT("Fatal error in bytecode: in main, cannot add extra field jacob_other_endo to the structArray\n");
|
|
|
|
}
|
|
|
|
if (!dont_store_a_structure)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < nb_blocks; i++)
|
2011-02-04 16:53:12 +01:00
|
|
|
{
|
|
|
|
mxSetFieldByNumber(plhs[2], i, jacob_field_number, interprete.get_jacob(i));
|
2012-06-06 16:32:01 +02:00
|
|
|
if (!steady_state)
|
|
|
|
{
|
|
|
|
mxSetFieldByNumber(plhs[2], i, jacob_exo_field_number, interprete.get_jacob_exo(i));
|
|
|
|
mxSetFieldByNumber(plhs[2], i, jacob_exo_det_field_number, interprete.get_jacob_exo_det(i));
|
|
|
|
mxSetFieldByNumber(plhs[2], i, jacob_other_endo_field_number, interprete.get_jacob_other_endo(i));
|
|
|
|
}
|
2011-02-02 00:31:37 +01:00
|
|
|
}
|
|
|
|
}
|
2010-07-23 11:20:24 +02:00
|
|
|
}
|
2011-02-04 16:53:12 +01:00
|
|
|
else
|
2010-10-11 19:21:32 +02:00
|
|
|
{
|
2011-02-02 00:31:37 +01:00
|
|
|
plhs[2] = mxCreateDoubleMatrix(1, 1, mxREAL);
|
|
|
|
pind = mxGetPr(plhs[0]);
|
|
|
|
pind[0] = NAN;
|
2010-10-11 19:21:32 +02:00
|
|
|
}
|
|
|
|
if (nlhs > 3)
|
2010-07-23 11:20:24 +02:00
|
|
|
{
|
2010-10-11 19:21:32 +02:00
|
|
|
plhs[3] = mxCreateDoubleMatrix(row_y, col_y, mxREAL);
|
|
|
|
pind = mxGetPr(plhs[3]);
|
|
|
|
for (i = 0; i < row_y*col_y; i++)
|
|
|
|
pind[i] = y[i];
|
2011-01-14 19:22:29 +01:00
|
|
|
if (nlhs > 4)
|
|
|
|
{
|
2011-02-04 16:53:12 +01:00
|
|
|
mxArray *GlobalTemporaryTerms = interprete.get_Temporary_Terms();
|
2011-01-14 19:22:29 +01:00
|
|
|
unsigned int nb_temp_terms = mxGetM(GlobalTemporaryTerms);
|
|
|
|
plhs[4] = mxCreateDoubleMatrix(nb_temp_terms, 1, mxREAL);
|
|
|
|
pind = mxGetPr(plhs[4]);
|
|
|
|
double *tt = mxGetPr(GlobalTemporaryTerms);
|
|
|
|
for (i = 0; i < nb_temp_terms; i++)
|
|
|
|
pind[i] = tt[i];
|
|
|
|
}
|
2010-07-23 11:20:24 +02:00
|
|
|
}
|
2011-01-14 19:22:29 +01:00
|
|
|
|
2010-07-23 11:20:24 +02:00
|
|
|
}
|
2009-12-16 18:18:38 +01:00
|
|
|
}
|
2007-10-04 00:01:08 +02:00
|
|
|
}
|
2010-08-18 13:51:57 +02:00
|
|
|
#else
|
|
|
|
Free_global();
|
|
|
|
#endif
|
2009-12-16 18:18:38 +01:00
|
|
|
if (x)
|
2007-11-21 00:24:01 +01:00
|
|
|
mxFree(x);
|
2009-12-16 18:18:38 +01:00
|
|
|
if (y)
|
2007-11-21 00:24:01 +01:00
|
|
|
mxFree(y);
|
2009-12-16 18:18:38 +01:00
|
|
|
if (ya)
|
2007-11-21 00:24:01 +01:00
|
|
|
mxFree(ya);
|
2009-12-16 18:18:38 +01:00
|
|
|
if (direction)
|
2007-11-21 00:24:01 +01:00
|
|
|
mxFree(direction);
|
2007-10-04 00:01:08 +02:00
|
|
|
}
|