extended-preprocessor: update test example1

issue#70
Michel Juillard 2013-11-03 18:05:38 +01:00
parent 416b7b849f
commit 3d11273a61
3 changed files with 34 additions and 136 deletions

View File

@ -2959,124 +2959,43 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
void
DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present) const
{
/* Writing initialisation for M_.lead_lag_incidence matrix
M_.lead_lag_incidence is a matrix with as many columns as there are
endogenous variables and as many rows as there are periods in the
models (nbr of rows = M_.max_lag+M_.max_lead+1)
The matrix elements are equal to zero if a variable isn't present in the
model at a given period.
*/
vector<int> state_var, state_equ;
output << endl
<< "vector<vector<int > > lead_lag_incidence;" << endl
<< "vector<int > variable_presence;" << endl;
int lag_presence[3];
// Loop on endogenous variables
int nstatic = 0,
nfwrd = 0,
npred = 0,
nboth = 0;
for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++)
{
output << "variable_presence.clear();" << endl;
int sstatic = 1,
sfwrd = 0,
spred = 0,
sboth = 0;
int varID;
// Loop on periods
for (int lag = -max_endo_lag; lag <= max_endo_lead; lag++)
{
// Print variableID if exists with current period, otherwise print 0
for (int lag = 0; lag <= 2; lag++)
{
lag_presence[lag] = 1;
try
{
int varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag);
output << "variable_presence.push_back(" << getDynJacobianCol(varID) << ");" << endl;
if (lag == -1)
{
sstatic = 0;
spred = 1;
}
else if (lag == 1)
if (spred == 1)
{
sboth = 1;
spred = 0;
}
else
{
sstatic = 0;
sfwrd = 1;
}
varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1);
}
catch (UnknownDerivIDException &e)
{
output << "variable_presence.push_back(0);" << endl;
lag_presence[lag] = 0;
}
}
nstatic += sstatic;
nfwrd += sfwrd;
npred += spred;
nboth += sboth;
output << "lead_lag_incidence.push_back(variable_presence);" << endl;
if (lag_presence[0] == 1)
if (lag_presence[2] == 1)
output << "zeta_mixed.push_back(" << endoID << ");" << endl;
else
output << "zeta_back.push_back(" << endoID << ");" << endl;
else if (lag_presence[2] == 1)
output << "zeta_fwrd.push_back(" << endoID << ");" << endl;
else
output << "zeta_static.push_back(" << endoID << ");" << endl;
}
output << "int nstatic = " << nstatic << ";" << endl
<< "int nfwrd = " << nfwrd << ";" << endl
<< "int npred = " << npred << ";" << endl
<< "int nboth = " << nboth << ";" << endl;
for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++)
// Loop on periods
for (int lag = -max_endo_lag; lag < 0; lag++)
// Print variableID if exists with current period, otherwise print 0
try
{
getDerivID(symbol_table.getID(eEndogenous, variable_reordered[endoID]), lag);
if (lag < 0 && find(state_var.begin(), state_var.end(), variable_reordered[endoID]+1) == state_var.end())
state_var.push_back(variable_reordered[endoID]);
}
catch (UnknownDerivIDException &e)
{
}
// Writing initialization for some other variables
output << endl
<< "int state_var[" << state_var.size() << "] = {";
for (size_t i = 0; i < state_var.size(); i++)
if (i+1 == state_var.size())
output << state_var[i];
else
output << state_var[i] << ", ";
output << "};" << endl;
output << endl
<< "int maximum_lag = " << max_lag << ";" << endl
<< "int maximum_lead = " << max_lead << ";" << endl;
if (symbol_table.endo_nbr())
output << endl
<< "int maximum_endo_lag = " << max_endo_lag << ";" << endl
<< "int maximum_endo_lead = " << max_endo_lead << ";" << endl
<< "double steady_state[" << symbol_table.endo_nbr() << "];" << endl;
if (symbol_table.exo_nbr())
output << endl
<< "int maximum_exo_lag = " << max_exo_lag << ";" << endl
<< "int maximum_exo_lead = " << max_exo_lead << ";" << endl
<< "double exo_steady_state[" << symbol_table.exo_nbr() << "];" << endl;
if (symbol_table.exo_det_nbr())
output << endl
<< "int maximum_exo_det_lag = " << max_exo_det_lag << ";" << endl
<< "int maximum_exo_det_lead = " << max_exo_det_lead << ";" << endl
<< "double exo_det_steady_state[" << symbol_table.exo_det_nbr() << "];" << endl;
output << endl
<< "map<int, double > params;" << endl;
output << "nstatic = zeta_static.size();" << endl
<< "nfwrd = zeta_fwrd.size();" << endl
<< "nback = zeta_back.size();" << endl
<< "nmixed = zeta_mixed.size();" << endl;
// Write number of non-zero derivatives
// Use -1 if the derivatives have not been computed
output << endl
<< "vector<double> NNZDerivatives;" << endl
<< "NNZDerivatives.push_back(" << NNZDerivatives[0] << ");" << endl;
if (order > 1)
{

View File

@ -826,40 +826,27 @@ ModFile::writeModelCC(const string &basename, bool cuda) const
<< endl
<< "#include \"dynare_cpp_driver.hh\"" << endl
<< endl
<< "DynareInfo *" << endl
<< "preprocessorOutput()" << endl
<< "DynareInfo::DynareInfo(void)" << endl
<< "{" << endl;
// Write basic info
symbol_table.writeCOutput(mDriverCFile);
mDriverCFile << "/*" << endl
<< " * Writing statements" << endl
<< " */" << endl
<< "/* prior args*/" << endl
<< "int index, index1;" << endl
<< "string shape;" << endl
<< "double mean, mode, stdev, variance;" << endl
<< "vector<double> domain;" << endl
<< "/* markov_switching args*/" << endl
<< "int chain, number_of_regimes, number_of_lags, number_of_lags_was_passed;" << endl
<< "vector<int> parameters;" << endl
<< "vector<double> duration;" << endl
<< "restriction_map_t restriction_map;" << endl
<< "/* options args*/" << endl
<< "double init;" << endl
<< "vector< vector<int> > lead_lag_incidence;" << endl
<< "vector<int> NNZDerivatives;" << endl
<< "vector<double> params(param_nbr);" << endl << endl;
mDriverCFile << endl << "params.resize(param_nbr);" << endl;
if (dynamic_model.equation_number() > 0)
{
dynamic_model.writeCOutput(mDriverCFile, basename, block, byte_code, use_dll, mod_file_struct.order_option, mod_file_struct.estimation_present);
// if (!no_static)
// static_model.writeCOutput(mOutputFile, block);
}
// Print statements
for (vector<Statement *>::const_iterator it = statements.begin();
it != statements.end(); it++)
(*it)->writeCOutput(mDriverCFile, basename);
mDriverCFile << "DynareInfo *model_info = new DynareInfo(exo_names, exo_det_names, endo_names, param_names, params, aux_vars, predetermined_variables, varobs, lead_lag_incidence, NNZDerivatives);" << endl
<< "return model_info;" << endl
<< "}" << endl;
mDriverCFile << "}" << endl;
mDriverCFile.close();
// Write informational m file

View File

@ -293,23 +293,19 @@ SymbolTable::writeCOutput(ostream &output) const throw (NotYetFrozenException)
throw NotYetFrozenException();
output << endl
<< "map<string, int > exo_names, exo_det_names, endo_names, param_names;" << endl;
output << endl
<< "int exo_nbr = " << exo_nbr() << ";" << endl;
<< "exo_nbr = " << exo_nbr() << ";" << endl;
if (exo_nbr() > 0)
for (int id = 0; id < exo_nbr(); id++)
output << "exo_names[\"" << getName(exo_ids[id]) << "\"] = " << id << ";" << endl;
output << endl
<< "int exo_det_nbr = " << exo_det_nbr() << ";" << endl;
<< "exo_det_nbr = " << exo_det_nbr() << ";" << endl;
if (exo_det_nbr() > 0)
for (int id = 0; id < exo_det_nbr(); id++)
output << "exo_det_names[\"" << getName(exo_det_ids[id]) << "\"] = " << id << " ;" << endl;
output << endl
<< "int endo_nbr = " << endo_nbr() << ";" << endl
<< "int orig_endo_nbr = " << orig_endo_nbr() << ";" << endl;
<< "endo_nbr = " << endo_nbr() << ";" << endl;
if (endo_nbr() > 0)
for (int id = 0; id < endo_nbr(); id++)
output << "endo_names[\"" << getName(endo_ids[id]) << "\"] = " << id << ";" << endl;
@ -321,8 +317,6 @@ SymbolTable::writeCOutput(ostream &output) const throw (NotYetFrozenException)
output << "param_names[\"" << getName(param_ids[id]) << "\"] = " << id << ";" << endl;
// Write the auxiliary variable table
output << endl
<< "vector <aux_vars_t> aux_vars;" << endl;
if (aux_vars.size() > 0)
for (int i = 0; i < (int) aux_vars.size(); i++)
{
@ -345,8 +339,6 @@ SymbolTable::writeCOutput(ostream &output) const throw (NotYetFrozenException)
output << "aux_vars.push_back(" << "av" << i << ");" << endl;
}
output << endl
<< "vector <int> predetermined_variables, varobs;" << endl;
if (predeterminedNbr() > 0)
for (set<int>::const_iterator it = predetermined_variables.begin();
it != predetermined_variables.end(); it++)