simulate.cc 8.98 KB
Newer Older
sebastien's avatar
sebastien committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Copyright (C) 2007-2008 Dynare Team
 *
 * 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/>.
 */

20
21
22
23
24
////////////////////////////////////////////////////////////////////////
//                           simulate.cc                              //
//              simulate file designed for GNU GCC C++ compiler       //
//                 use NO_COMPILER option in MODEL command            //
////////////////////////////////////////////////////////////////////////
sebastien's avatar
sebastien committed
25

26
27
#include <cstring>

28
#include "simulate.hh"
ferhat's avatar
ferhat committed
29
30
#include "Interpreter.hh"
#include "Mem_Mngr.hh"
31
32
33
34
35
36
37
38
39
40
41
42



int
max(int a, int b)
{
  if (a>b)
    return(a);
  else
    return(b);
}

43

44

ferhat's avatar
ferhat committed
45
46
#ifdef DEBUG_EX
/*The Matlab c++ interface*/
47

ferhat's avatar
ferhat committed
48
49
50
using namespace std;
#include <sstream>
#include "mex_interface.hh"
51

ferhat's avatar
ferhat committed
52
53
54
55
56
57
58
59
60
61
62
63
64
int
main( int argc, const char* argv[] )
{
  FILE *fid;
  printf("argc=%d\n",argc);
  float f_tmp;
  ostringstream tmp_out("");
  tmp_out << argv[1] << "_options.txt";
  cout << tmp_out.str().c_str() << "\n";
  int nb_params;
  int i, row_y, col_y, row_x, col_x;
  double *yd, *xd;
  double *direction;
65

ferhat's avatar
ferhat committed
66
67
  string file_name(argv[1]);
  //mexPrintf("file_name=%s\n",file_name.c_str());
68

ferhat's avatar
ferhat committed
69
70
71
72
73
74
75
76
77
78
79
  fid = fopen(tmp_out.str().c_str(),"r");
  fscanf(fid,"%d",&periods);
  fscanf(fid,"%d",&maxit_);
  fscanf(fid,"%f",&f_tmp);
  slowc = f_tmp;
  //mexPrintf("slowc_save=%f\n",slowc_save);
  fscanf(fid,"%f",&f_tmp);
  markowitz_c = f_tmp;
  fscanf(fid,"%f",&f_tmp);
  solve_tolf = f_tmp;
  fclose(fid);
80

ferhat's avatar
ferhat committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  tmp_out.str("");
  tmp_out << argv[1] << "_M.txt";
  //printf("%s\n",tmp_out.str().c_str());
  fid = fopen(tmp_out.str().c_str(),"r");
  fscanf(fid,"%d",&y_kmin);
  //printf("y_kmin=%d\n",y_kmin);
  fscanf(fid,"%d",&y_kmax);
  //printf("y_kmax=%d\n",y_kmax);
  fscanf(fid,"%d",&y_decal);
  //printf("y_decal=%d\n",y_decal);
  fscanf(fid,"%d",&nb_params);
  //printf("nb_params=%d\n",nb_params);
  fscanf(fid,"%d",&row_x);
  //printf("row_x=%d\n",row_x);
  fscanf(fid,"%d",&col_x);
  //printf("col_x=%d\n",col_x);
  fscanf(fid,"%d",&row_y);
  //printf("row_y=%d\n",row_y);
  fscanf(fid,"%d",&col_y);
  //printf("col_y=%d\n",col_y);
  fscanf(fid,"%d",&nb_row_xd);
  //printf("nb_row_xd=%d\n",nb_row_xd);
  params = (double*)malloc(nb_params*sizeof(params[0]));
  //printf("OK1\n");
  for(i=0; i < nb_params; i++)
    {
      fscanf(fid,"%f",&f_tmp);
      params[i] = f_tmp;
      //printf("param[%d]=%f\n",i,params[i]);
    }
  //printf("OK2\n");
  fclose(fid);
  yd = (double*)malloc(row_y*col_y*sizeof(yd[0]));
  xd = (double*)malloc(row_x*col_x*sizeof(xd[0]));
  tmp_out.str("");
  tmp_out << argv[1] << "_oo.txt";
  fid = fopen(tmp_out.str().c_str(),"r");
  for(i=0; i < col_y*row_y; i++)
    {
      fscanf(fid,"%f",&f_tmp);
      yd[i] = f_tmp;
    }
  for(i=0; i < col_x*row_x; i++)
    {
      fscanf(fid,"%f",&f_tmp);
      xd[i] = f_tmp;
    }
  fclose(fid);
129

ferhat's avatar
ferhat committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  size_of_direction=col_y*row_y*sizeof(double);
  y=(double*)mxMalloc(size_of_direction);
  ya=(double*)mxMalloc(size_of_direction);
  direction=(double*)mxMalloc(size_of_direction);
  memset(direction,0,size_of_direction);
  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++)
    y[i]=double(yd[i]);
  free(yd);
  free(xd);

  y_size=row_y;
  x_size=col_x/*row_x*/;
  nb_row_x=row_x;
  /*for(int i=0; i<y_size; i++)
    {
      for(int it_=0; it_<8;it_++)
        mexPrintf("y[t=%d, var=%d]=%f  ",it_+1, i+1, y[(it_)*y_size+i]);
      mexPrintf("\n");
    }
152

ferhat's avatar
ferhat committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  for(int i=0; i<col_x; i++)
    {
      for(int it_=0; it_<8;it_++)
        mexPrintf("x[t=%d, var=%d]=%f  ",it_, i+1, x[it_+i*nb_row_x]);
      mexPrintf("\n");
    }*/

  t0= clock();
  Interpreter interprete(params, y, ya, x, 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);
  string f(file_name);
  interprete.compute_blocks(f+"_dynamic", f);
  t1= clock();
  mexPrintf("Simulation Time=%f milliseconds\n",1000.0*(double(t1)-double(t0))/double(CLOCKS_PER_SEC));
  /*if (nlhs>0)
    {
      plhs[0] = mxCreateDoubleMatrix(row_y, col_y, mxREAL);
      pind = mxGetPr(plhs[0]);
      for (i=0;i<row_y*col_y;i++)
        pind[i]=y[i];
    }*/
  if(x)
    mxFree(x);
  if(y)
    mxFree(y);
  if(ya)
    mxFree(ya);
  if(direction)
    mxFree(direction);
  free(params);
}

#else
/* The gateway routine */
void
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
188
189
190
191
{
  mxArray *M_, *oo_, *options_;
  int i, row_y, col_y, row_x, col_x;
  double * pind ;
ferhat's avatar
ferhat committed
192
  double *direction;
193
194
195
196
  M_ = mexGetVariable("global","M_");
  if (M_ == NULL )
    {
      mexPrintf("Global variable not found : ");
ferhat's avatar
ferhat committed
197
      mexEvalString("st=fclose('all');clear all;");
198
199
200
201
202
203
204
      mexErrMsgTxt("M_ \n");
    }
  /* Gets variables and parameters from global workspace of Matlab */
  oo_ = mexGetVariable("global","oo_");
  if (oo_ == NULL )
    {
      mexPrintf("Global variable not found : ");
ferhat's avatar
ferhat committed
205
      mexEvalString("st=fclose('all');clear all;");
206
207
208
209
210
211
      mexErrMsgTxt("oo_ \n");
    }
  options_ = mexGetVariable("global","options_");
  if (options_ == NULL )
    {
      mexPrintf("Global variable not found : ");
ferhat's avatar
ferhat committed
212
      mexEvalString("st=fclose('all');clear all;");
213
214
      mexErrMsgTxt("options_ \n");
    }
ferhat's avatar
ferhat committed
215
  //mexPrintf("ok0\n");
216
217
218
219
220
221
222
223
224
225
226
227
228
  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")))));
ferhat's avatar
ferhat committed
229
  //slowc_save=slowc;
230
231
232
233
234
235
  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));
ferhat's avatar
ferhat committed
236
  string file_name=fname;
237
238
239
240
241
  int status = mxGetString(mxa, fname, buflen);
  if (status != 0)
    mexWarnMsgTxt("Not enough space. Filename is truncated.");
  col_y=mxGetN(mxGetFieldByNumber(oo_, 0, mxGetFieldNumber(oo_,"endo_simul")));;
  solve_tolf=*(mxGetPr(mxGetFieldByNumber(options_, 0, mxGetFieldNumber(options_,"dynatol"))));
ferhat's avatar
ferhat committed
242
243
244
245
  size_of_direction=col_y*row_y*sizeof(double);
  y=(double*)mxMalloc(size_of_direction);
  ya=(double*)mxMalloc(size_of_direction);
  direction=(double*)mxMalloc(size_of_direction);
246
  memset(direction,0,size_of_direction);
ferhat's avatar
ferhat committed
247
  x=(double*)mxMalloc(col_x*row_x*sizeof(double));
248
  for (i=0;i<row_x*col_x;i++)
249
     x[i]=double(xd[i]);
250
  for (i=0;i<row_y*col_y;i++)
ferhat's avatar
ferhat committed
251
    y[i]=double(yd[i]);
252
  y_size=row_y;
ferhat's avatar
ferhat committed
253
  x_size=col_x/*row_x*/;
254
255
  nb_row_x=row_x;

ferhat's avatar
ferhat committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
  /*for(int i=0; i<y_size; i++)
    {
      for(int it_=0; it_<8;it_++)
        mexPrintf("y[t=%d, var=%d]=%f  ",it_+1, i+1, y[(it_)*y_size+i]);
      mexPrintf("\n");
    }

  for(int i=0; i<col_x; i++)
    {
      for(int it_=0; it_<8;it_++)
        mexPrintf("x[t=%d, var=%d]=%f  ",it_, i+1, x[it_+i*nb_row_x]);
      mexPrintf("\n");
    }*/

270
  t0= clock();
ferhat's avatar
ferhat committed
271
  Interpreter interprete(params, y, ya, x, 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);
272
273
274
275
276
277
278
279
280
281
282
  string f(fname);
  interprete.compute_blocks(f+"_dynamic", f);
  t1= clock();
  mexPrintf("Simulation Time=%f milliseconds\n",1000.0*(double(t1)-double(t0))/double(CLOCKS_PER_SEC));
  if (nlhs>0)
    {
      plhs[0] = mxCreateDoubleMatrix(row_y, col_y, mxREAL);
      pind = mxGetPr(plhs[0]);
      for (i=0;i<row_y*col_y;i++)
        pind[i]=y[i];
    }
ferhat's avatar
ferhat committed
283
284
285
286
287
288
289
290
  if(x)
    mxFree(x);
  if(y)
    mxFree(y);
  if(ya)
    mxFree(ya);
  if(direction)
    mxFree(direction);
291
}
ferhat's avatar
ferhat committed
292
#endif