fixing extended-preprocessor C++ API and test case
parent
b62e300f8a
commit
ad42818606
|
@ -0,0 +1,414 @@
|
|||
/*
|
||||
* Copyright (C) 2011-2012 Houtan Bastani, Daniel Waggoner, Tao Zha
|
||||
*
|
||||
* This 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.
|
||||
*
|
||||
* This code 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.
|
||||
*
|
||||
* More information is available at <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <assert.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "dynare_cpp_driver.hh"
|
||||
|
||||
DynareInfo::DynareInfo(map<string, int > exo_names_arg,
|
||||
map<string, int > exo_det_names_arg,
|
||||
map<string, int > endo_names_arg,
|
||||
map<string, int > param_names_arg,
|
||||
vector< double > params_arg,
|
||||
vector<aux_vars_t> aux_vars_arg,
|
||||
vector<int> predetermined_variables_arg,
|
||||
vector<int> varobs_arg,
|
||||
vector<int> NNZDerivatives_arg) :
|
||||
NNZDerivatives(NNZDerivatives_arg)
|
||||
{
|
||||
endo_nbr = endo_names.size();
|
||||
exo_nbr = exo_names.size();
|
||||
exo_det_nbr = exo_det_names.size();
|
||||
param_nbr = param_names.size();
|
||||
|
||||
exo_names = exo_names_arg;
|
||||
exo_det_names = exo_det_names_arg;
|
||||
endo_names = endo_names_arg;
|
||||
param_names = param_names_arg;
|
||||
params = params_arg;
|
||||
std::cout << params_arg[0] << std::endl;
|
||||
std::cout << params[0] << std::endl;
|
||||
aux_vars = aux_vars_arg;
|
||||
predetermined_variables = predetermined_variables_arg;
|
||||
}
|
||||
|
||||
DynareInfo::~DynareInfo()
|
||||
{
|
||||
for (vector<MarkovSwitching *>::iterator it = markov_switching_vector.begin();
|
||||
it < markov_switching_vector.end(); it++ )
|
||||
delete *it;
|
||||
|
||||
for (vector<ModFilePrior *>::iterator it = prior_vector.begin();
|
||||
it < prior_vector.end(); it++ )
|
||||
delete *it;
|
||||
|
||||
for (vector<ModFileStructuralInnovationPrior *>::iterator it = structural_innovation_prior_vector.begin();
|
||||
it < structural_innovation_prior_vector.end(); it++ )
|
||||
delete *it;
|
||||
|
||||
for (vector<ModFileMeasurementErrorPrior *>::iterator it = measurement_error_prior_vector.begin();
|
||||
it < measurement_error_prior_vector.end(); it++ )
|
||||
delete *it;
|
||||
|
||||
for (vector<ModFileStructuralInnovationCorrPrior *>::iterator it = structural_innovation_corr_prior_vector.begin();
|
||||
it < structural_innovation_corr_prior_vector.end(); it++ )
|
||||
delete *it;
|
||||
|
||||
for (vector<ModFileMeasurementErrorCorrPrior *>::iterator it = measurement_error_corr_prior_vector.begin();
|
||||
it < measurement_error_corr_prior_vector.end(); it++ )
|
||||
delete *it;
|
||||
|
||||
markov_switching_vector.clear();
|
||||
prior_vector.clear();
|
||||
structural_innovation_prior_vector.clear();
|
||||
measurement_error_prior_vector.clear();
|
||||
structural_innovation_corr_prior_vector.clear();
|
||||
measurement_error_corr_prior_vector.clear();
|
||||
exo_names.clear();
|
||||
exo_det_names.clear();
|
||||
endo_names.clear();
|
||||
param_names.clear();
|
||||
params.clear();
|
||||
aux_vars.clear();
|
||||
predetermined_variables.clear();
|
||||
varobs.clear();
|
||||
NNZDerivatives.clear();
|
||||
}
|
||||
|
||||
string
|
||||
DynareInfo::get_exo_name_by_index(int index) throw (ValueNotSetException)
|
||||
{
|
||||
for (map<string, int >::iterator it = exo_names.begin();
|
||||
it != exo_names.end(); it++)
|
||||
if (it->second == index)
|
||||
return it->first;
|
||||
throw ValueNotSetException("get_exo_name_by_index" + index);
|
||||
}
|
||||
|
||||
int
|
||||
DynareInfo::get_exo_index_by_name(string name) throw (ValueNotSetException)
|
||||
{
|
||||
map<string, int >::iterator it = exo_names.find(name);
|
||||
if (it != exo_names.end())
|
||||
return it->second;
|
||||
throw ValueNotSetException("get_exo_name_by_name" + name);
|
||||
}
|
||||
|
||||
string
|
||||
DynareInfo::get_exo_det_name_by_index(int index) throw (ValueNotSetException)
|
||||
{
|
||||
for (map<string, int >::iterator it = exo_det_names.begin();
|
||||
it != exo_det_names.end(); it++)
|
||||
if (it->second == index)
|
||||
return it->first;
|
||||
throw ValueNotSetException("get_exo_det_name_by_index" + index);
|
||||
}
|
||||
|
||||
int
|
||||
DynareInfo::get_exo_det_index_by_name(string name) throw (ValueNotSetException)
|
||||
{
|
||||
map<string, int >::iterator it = exo_det_names.find(name);
|
||||
if (it != exo_det_names.end())
|
||||
return it->second;
|
||||
throw ValueNotSetException("get_exo_det_name_by_name" + name);
|
||||
}
|
||||
|
||||
string
|
||||
DynareInfo::get_endo_name_by_index(int index) throw (ValueNotSetException)
|
||||
{
|
||||
for (map<string, int >::iterator it = endo_names.begin();
|
||||
it != endo_names.end(); it++)
|
||||
if (it->second == index)
|
||||
return it->first;
|
||||
throw ValueNotSetException("get_endo_name_by_index" + index);
|
||||
}
|
||||
|
||||
int
|
||||
DynareInfo::get_endo_index_by_name(string name) throw (ValueNotSetException)
|
||||
{
|
||||
map<string, int >::iterator it = endo_names.find(name);
|
||||
if (it != endo_names.end())
|
||||
return it->second;
|
||||
throw ValueNotSetException("get_endo_name_by_name" + name);
|
||||
}
|
||||
|
||||
string
|
||||
DynareInfo::get_param_name_by_index(int index) throw (ValueNotSetException)
|
||||
{
|
||||
for (map<string, int >::iterator it = param_names.begin();
|
||||
it != param_names.end(); it++)
|
||||
if (it->second == index)
|
||||
return it->first;
|
||||
throw ValueNotSetException("get_param_name_by_index" + index);
|
||||
}
|
||||
|
||||
int
|
||||
DynareInfo::get_param_index_by_name(string name) throw (ValueNotSetException)
|
||||
{
|
||||
map<string, int >::iterator it = param_names.find(name);
|
||||
if (it != param_names.end())
|
||||
return it->second;
|
||||
throw ValueNotSetException("get_param_name_by_name" + name);
|
||||
}
|
||||
|
||||
double
|
||||
DynareInfo::get_param_value_by_index(int index) throw (ValueNotSetException)
|
||||
{
|
||||
return params[index];
|
||||
// map<int, double >::iterator it = params.find(index);
|
||||
// if (it != params.end())
|
||||
// return it->second;
|
||||
// throw ValueNotSetException("get_param_value_by_index" + index);
|
||||
}
|
||||
|
||||
MarkovSwitching::MarkovSwitching(const int chain_arg,
|
||||
const int number_of_regimes_arg,
|
||||
const int number_of_lags_arg,
|
||||
const bool number_of_lags_was_passed_arg,
|
||||
const vector<int> parameters_arg,
|
||||
const vector<double> duration_arg,
|
||||
const restriction_map_t restriction_map_arg) :
|
||||
chain(chain_arg),
|
||||
number_of_regimes(number_of_regimes_arg),
|
||||
number_of_lags(number_of_lags_arg),
|
||||
number_of_lags_was_passed(number_of_lags_was_passed_arg),
|
||||
parameters(parameters_arg),
|
||||
duration(duration_arg),
|
||||
restriction_map(restriction_map_arg)
|
||||
{
|
||||
assert(chain >= 1);
|
||||
assert(number_of_regimes > 0);
|
||||
if (number_of_lags_was_passed)
|
||||
assert(number_of_lags > 0);
|
||||
assert(!parameters.empty());
|
||||
assert(!duration.empty());
|
||||
}
|
||||
|
||||
int
|
||||
MarkovSwitching::get_number_of_lags() throw (ValueNotSetException)
|
||||
{
|
||||
if (number_of_lags_has_val())
|
||||
return number_of_lags;
|
||||
throw ValueNotSetException("number_of_lags");
|
||||
}
|
||||
|
||||
restriction_map_t
|
||||
MarkovSwitching::get_restriction_map() throw (ValueNotSetException)
|
||||
{
|
||||
if (restriction_map_has_val())
|
||||
return restriction_map;
|
||||
throw ValueNotSetException("restriction_map");
|
||||
}
|
||||
|
||||
BasicModFilePrior::BasicModFilePrior(const int index_arg,
|
||||
const string shape_arg,
|
||||
const double mean_arg,
|
||||
const double mode_arg,
|
||||
const double stdev_arg,
|
||||
const double variance_arg,
|
||||
const vector <double> domain_arg) :
|
||||
index(index_arg),
|
||||
shape(shape_arg),
|
||||
mean(mean_arg),
|
||||
mode(mode_arg),
|
||||
stdev(stdev_arg),
|
||||
variance(variance_arg),
|
||||
domain(domain_arg)
|
||||
{
|
||||
assert(index >= 0);
|
||||
assert(!shape.empty());
|
||||
if (stdev_has_val())
|
||||
assert(stdev >= 0);
|
||||
if (variance_has_val())
|
||||
assert(variance >= 0);
|
||||
if (domain_has_val())
|
||||
assert(domain.size() == 2);
|
||||
}
|
||||
|
||||
double
|
||||
BasicModFilePrior::get_mean() throw (ValueNotSetException)
|
||||
{
|
||||
if (mean_has_val())
|
||||
return mean;
|
||||
throw ValueNotSetException("mean");
|
||||
};
|
||||
|
||||
double
|
||||
BasicModFilePrior::get_mode() throw (ValueNotSetException)
|
||||
{
|
||||
if (mode_has_val())
|
||||
return mode;
|
||||
throw ValueNotSetException("mode");
|
||||
};
|
||||
|
||||
double
|
||||
BasicModFilePrior::get_stdev() throw (ValueNotSetException)
|
||||
{
|
||||
if (stdev_has_val())
|
||||
return stdev;
|
||||
throw ValueNotSetException("stdev");
|
||||
};
|
||||
|
||||
double
|
||||
BasicModFilePrior::get_variance() throw (ValueNotSetException)
|
||||
{
|
||||
if (variance_has_val())
|
||||
return variance;
|
||||
throw ValueNotSetException("variance");
|
||||
};
|
||||
|
||||
vector<double>
|
||||
BasicModFilePrior::get_domain() throw (ValueNotSetException)
|
||||
{
|
||||
if (domain_has_val())
|
||||
return domain;
|
||||
throw ValueNotSetException("domain");
|
||||
};
|
||||
|
||||
ModFilePrior::ModFilePrior(const int index_arg,
|
||||
const string shape_arg,
|
||||
const double mean_arg,
|
||||
const double mode_arg,
|
||||
const double stdev_arg,
|
||||
const double variance_arg,
|
||||
const vector <double> domain_arg) :
|
||||
BasicModFilePrior(index_arg,
|
||||
shape_arg,
|
||||
mean_arg,
|
||||
mode_arg,
|
||||
stdev_arg,
|
||||
variance_arg,
|
||||
domain_arg)
|
||||
{
|
||||
}
|
||||
|
||||
ModFileStructuralInnovationPrior::ModFileStructuralInnovationPrior(const int index_arg,
|
||||
const string shape_arg,
|
||||
const double mean_arg,
|
||||
const double stdev_arg,
|
||||
const double variance_arg,
|
||||
const double mode_arg,
|
||||
const vector <double> domain_arg) :
|
||||
BasicModFilePrior(index_arg,
|
||||
shape_arg,
|
||||
mean_arg,
|
||||
mode_arg,
|
||||
stdev_arg,
|
||||
variance_arg,
|
||||
domain_arg)
|
||||
{
|
||||
}
|
||||
|
||||
ModFileMeasurementErrorPrior::ModFileMeasurementErrorPrior(const int index_arg,
|
||||
const string shape_arg,
|
||||
const double mean_arg,
|
||||
const double stdev_arg,
|
||||
const double variance_arg,
|
||||
const double mode_arg,
|
||||
const vector <double> domain_arg) :
|
||||
BasicModFilePrior(index_arg,
|
||||
shape_arg,
|
||||
mean_arg,
|
||||
mode_arg,
|
||||
stdev_arg,
|
||||
variance_arg,
|
||||
domain_arg)
|
||||
{
|
||||
}
|
||||
|
||||
ModFileStructuralInnovationCorrPrior::ModFileStructuralInnovationCorrPrior(const int index1_arg,
|
||||
const int index2_arg,
|
||||
const string shape_arg,
|
||||
const double mean_arg,
|
||||
const double stdev_arg,
|
||||
const double variance_arg,
|
||||
const double mode_arg,
|
||||
const vector <double> domain_arg) :
|
||||
BasicModFilePrior(index1_arg,
|
||||
shape_arg,
|
||||
mean_arg,
|
||||
mode_arg,
|
||||
stdev_arg,
|
||||
variance_arg,
|
||||
domain_arg),
|
||||
index2(index2_arg)
|
||||
{
|
||||
assert(index2 >= 0);
|
||||
}
|
||||
|
||||
ModFileMeasurementErrorCorrPrior::ModFileMeasurementErrorCorrPrior(const int index1_arg,
|
||||
const int index2_arg,
|
||||
const string shape_arg,
|
||||
const double mean_arg,
|
||||
const double stdev_arg,
|
||||
const double variance_arg,
|
||||
const double mode_arg,
|
||||
const vector <double> domain_arg) :
|
||||
BasicModFilePrior(index1_arg,
|
||||
shape_arg,
|
||||
mean_arg,
|
||||
mode_arg,
|
||||
stdev_arg,
|
||||
variance_arg,
|
||||
domain_arg),
|
||||
index2(index2_arg)
|
||||
{
|
||||
assert(index2 >= 0);
|
||||
}
|
||||
|
||||
BasicModFileOption::BasicModFileOption(const int index_arg,
|
||||
const double init_arg) :
|
||||
index(index_arg),
|
||||
init(init_arg)
|
||||
{
|
||||
assert(index >= 0);
|
||||
assert(!isnan(init));
|
||||
}
|
||||
|
||||
ModFileOption::ModFileOption(const int index_arg, const double init_arg) :
|
||||
BasicModFileOption(index_arg, init_arg)
|
||||
{
|
||||
}
|
||||
|
||||
ModFileStructuralInnovationOption::ModFileStructuralInnovationOption(const int index_arg, const double init_arg) :
|
||||
BasicModFileOption(index_arg, init_arg)
|
||||
{
|
||||
}
|
||||
|
||||
ModFileMeasurementErrorOption::ModFileMeasurementErrorOption(const int index_arg, const double init_arg) :
|
||||
BasicModFileOption(index_arg, init_arg)
|
||||
{
|
||||
}
|
||||
|
||||
ModFileStructuralInnovationCorrOption::ModFileStructuralInnovationCorrOption(const int index1_arg, const int index2_arg, const double init_arg) :
|
||||
BasicModFileOption(index1_arg, init_arg),
|
||||
index2(index2_arg)
|
||||
{
|
||||
assert(index2 >= 0);
|
||||
}
|
||||
|
||||
ModFileMeasurementErrorCorrOption::ModFileMeasurementErrorCorrOption(const int index1_arg, const int index2_arg, const double init_arg) :
|
||||
BasicModFileOption(index1_arg, init_arg),
|
||||
index2(index2_arg)
|
||||
{
|
||||
assert(index2 >= 0);
|
||||
}
|
|
@ -0,0 +1,333 @@
|
|||
/*
|
||||
* Copyright (C) 2011-2012 Houtan Bastani, Daniel Waggoner, Tao Zha
|
||||
*
|
||||
* This 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.
|
||||
*
|
||||
* This code 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.
|
||||
*
|
||||
* More information is available at <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _DYNARE_CPP_DRIVER_HH
|
||||
#define _DYNARE_CPP_DRIVER_HH
|
||||
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <limits>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct aux_vars_t {
|
||||
int endo_index, type, orig_index, orig_lead_lag;
|
||||
} ;
|
||||
|
||||
typedef map<pair<int, int >, double> restriction_map_t ;
|
||||
|
||||
class ValueNotSetException
|
||||
{
|
||||
public:
|
||||
string name;
|
||||
ValueNotSetException(const string &name_arg) : name(name_arg)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
class MarkovSwitching
|
||||
{
|
||||
private:
|
||||
const int chain, number_of_regimes, number_of_lags;
|
||||
const bool number_of_lags_was_passed;
|
||||
const vector<int> parameters;
|
||||
const vector<double> duration;
|
||||
const restriction_map_t restriction_map;
|
||||
public:
|
||||
MarkovSwitching(const int chain_arg,
|
||||
const int number_of_regimes_arg,
|
||||
const int number_of_lags_arg,
|
||||
const bool number_of_lags_was_passed_arg,
|
||||
const vector<int> parameters_arg,
|
||||
const vector<double> duration_arg,
|
||||
const restriction_map_t restriction_map_arg);
|
||||
|
||||
inline bool number_of_lags_has_val() const { return number_of_lags_was_passed; };
|
||||
inline bool restriction_map_has_val() const { return !restriction_map.empty(); };
|
||||
|
||||
inline int get_chain() const { return chain; };
|
||||
inline int get_number_of_regimes() const { return number_of_regimes; };
|
||||
int get_number_of_lags() throw (ValueNotSetException);
|
||||
inline vector<int> get_parameters() { return parameters; };
|
||||
inline vector<double> get_duration() { return duration; };
|
||||
restriction_map_t get_restriction_map() throw (ValueNotSetException);
|
||||
};
|
||||
|
||||
class BasicModFilePrior
|
||||
{
|
||||
private:
|
||||
inline bool isnan(double x) const { return (x!=x); };
|
||||
protected:
|
||||
const int index;
|
||||
const string shape;
|
||||
const double mean, mode, stdev, variance;
|
||||
const vector <double> domain;
|
||||
|
||||
BasicModFilePrior(const int index_arg,
|
||||
const string shape_arg,
|
||||
const double mean_arg,
|
||||
const double mode_arg,
|
||||
const double stdev_arg,
|
||||
const double variance_arg,
|
||||
const vector <double> domain_arg);
|
||||
public:
|
||||
inline bool mean_has_val() const { return !isnan(mean); };
|
||||
inline bool mode_has_val() const { return !isnan(mode); };
|
||||
inline bool stdev_has_val() const { return !isnan(stdev); };
|
||||
inline bool variance_has_val() const { return !isnan(variance); };
|
||||
inline bool domain_has_val() const { return (domain.size() == 2); };
|
||||
|
||||
inline int get_index() const { return index; };
|
||||
inline string get_shape() const { return shape; };
|
||||
double get_mean() throw (ValueNotSetException);
|
||||
double get_mode() throw (ValueNotSetException);
|
||||
double get_stdev() throw (ValueNotSetException);
|
||||
double get_variance() throw (ValueNotSetException);
|
||||
vector<double> get_domain() throw (ValueNotSetException);
|
||||
};
|
||||
|
||||
class ModFilePrior : public BasicModFilePrior
|
||||
{
|
||||
public:
|
||||
ModFilePrior(const int index_arg,
|
||||
const string shape_arg,
|
||||
const double mean_arg,
|
||||
const double mode_arg,
|
||||
const double stdev_arg,
|
||||
const double variance_arg,
|
||||
const vector <double> domain_arg);
|
||||
};
|
||||
|
||||
class ModFileStructuralInnovationPrior: public BasicModFilePrior
|
||||
{
|
||||
public:
|
||||
ModFileStructuralInnovationPrior(const int index_arg,
|
||||
const string shape_arg,
|
||||
const double mean_arg,
|
||||
const double mode_arg,
|
||||
const double stdev_arg,
|
||||
const double variance_arg,
|
||||
const vector <double> domain_arg);
|
||||
};
|
||||
|
||||
class ModFileMeasurementErrorPrior : public BasicModFilePrior
|
||||
{
|
||||
public:
|
||||
ModFileMeasurementErrorPrior(const int index_arg,
|
||||
const string shape_arg,
|
||||
const double mean_arg,
|
||||
const double stdev_arg,
|
||||
const double variance_arg,
|
||||
const double mode_arg,
|
||||
const vector <double> domain_arg);
|
||||
};
|
||||
|
||||
class ModFileStructuralInnovationCorrPrior : public BasicModFilePrior
|
||||
{
|
||||
private:
|
||||
const int index2;
|
||||
public:
|
||||
ModFileStructuralInnovationCorrPrior(const int index1_arg,
|
||||
const int index2_arg,
|
||||
const string shape_arg,
|
||||
const double mean_arg,
|
||||
const double stdev_arg,
|
||||
const double variance_arg,
|
||||
const double mode_arg,
|
||||
const vector <double> domain_arg);
|
||||
};
|
||||
|
||||
class ModFileMeasurementErrorCorrPrior : public BasicModFilePrior
|
||||
{
|
||||
private:
|
||||
const int index2;
|
||||
public:
|
||||
ModFileMeasurementErrorCorrPrior(const int index1_arg,
|
||||
const int index2_arg,
|
||||
const string shape_arg,
|
||||
const double mean_arg,
|
||||
const double stdev_arg,
|
||||
const double variance_arg,
|
||||
const double mode_arg,
|
||||
const vector <double> domain_arg);
|
||||
};
|
||||
|
||||
class BasicModFileOption
|
||||
{
|
||||
private:
|
||||
inline bool isnan(double x) const { return (x!=x); };
|
||||
protected:
|
||||
const int index;
|
||||
const double init;
|
||||
|
||||
BasicModFileOption(const int index_arg,
|
||||
const double init_arg);
|
||||
public:
|
||||
inline int get_index() const { return index; };
|
||||
inline double get_init() const { return init; };
|
||||
};
|
||||
|
||||
class ModFileOption : public BasicModFileOption
|
||||
{
|
||||
public:
|
||||
ModFileOption(const int index_arg,
|
||||
const double init_arg);
|
||||
};
|
||||
|
||||
class ModFileStructuralInnovationOption: public BasicModFileOption
|
||||
{
|
||||
public:
|
||||
ModFileStructuralInnovationOption(const int index_arg,
|
||||
const double init_arg);
|
||||
};
|
||||
|
||||
class ModFileMeasurementErrorOption : public BasicModFileOption
|
||||
{
|
||||
public:
|
||||
ModFileMeasurementErrorOption(const int index_arg,
|
||||
const double init_arg);
|
||||
};
|
||||
|
||||
class ModFileStructuralInnovationCorrOption : public BasicModFileOption
|
||||
{
|
||||
private:
|
||||
const int index2;
|
||||
public:
|
||||
ModFileStructuralInnovationCorrOption(const int index1_arg,
|
||||
const int index2_arg,
|
||||
const double init_arg);
|
||||
};
|
||||
|
||||
class ModFileMeasurementErrorCorrOption : public BasicModFileOption
|
||||
{
|
||||
private:
|
||||
const int index2;
|
||||
public:
|
||||
ModFileMeasurementErrorCorrOption(const int index1_arg,
|
||||
const int index2_arg,
|
||||
const double init_arg);
|
||||
};
|
||||
|
||||
class DynareInfo
|
||||
{
|
||||
private:
|
||||
vector<MarkovSwitching *> markov_switching_vector;
|
||||
vector<ModFilePrior *> prior_vector;
|
||||
vector<ModFileStructuralInnovationPrior *> structural_innovation_prior_vector;
|
||||
vector<ModFileMeasurementErrorPrior *> measurement_error_prior_vector;
|
||||
vector<ModFileStructuralInnovationCorrPrior *> structural_innovation_corr_prior_vector;
|
||||
vector<ModFileMeasurementErrorCorrPrior *> measurement_error_corr_prior_vector;
|
||||
vector<ModFileOption *> option_vector;
|
||||
vector<ModFileStructuralInnovationOption *> structural_innovation_option_vector;
|
||||
vector<ModFileMeasurementErrorOption *> measurement_error_option_vector;
|
||||
vector<ModFileStructuralInnovationCorrOption *> structural_innovation_corr_option_vector;
|
||||
vector<ModFileMeasurementErrorCorrOption *> measurement_error_corr_option_vector;
|
||||
map<string, int > exo_names, exo_det_names, endo_names, param_names;
|
||||
vector< double > params;
|
||||
vector<aux_vars_t> aux_vars;
|
||||
vector<int> predetermined_variables;
|
||||
vector<int> varobs;
|
||||
vector<size_t> zeta_fwrd, zeta_back, zeta_mixed, zeta_static;
|
||||
vector<int> NNZDerivatives;
|
||||
int endo_nbr, exo_nbr, exo_det_nbr, param_nbr, nstatic, nfwrd, nback, nmixed;
|
||||
public:
|
||||
DynareInfo(void); // this function is automatically written by the Dynare preprocessor
|
||||
DynareInfo(map<string, int > exo_names_arg,
|
||||
map<string, int > exo_det_names_arg,
|
||||
map<string, int > endo_names_arg,
|
||||
map<string, int > param_names_arg,
|
||||
vector< double > params_arg,
|
||||
vector<aux_vars_t> aux_vars_arg,
|
||||
vector<int> predetermined_variables_arg,
|
||||
vector<int> varobs_arg,
|
||||
vector<int> NNZDerivatives_arg);
|
||||
~DynareInfo();
|
||||
|
||||
inline void addMarkovSwitching(MarkovSwitching *ms) { markov_switching_vector.push_back(ms); };
|
||||
|
||||
inline void addPrior(ModFilePrior *p) { prior_vector.push_back(p); };
|
||||
inline void addStructuralInnovationPrior(ModFileStructuralInnovationPrior *sip) { structural_innovation_prior_vector.push_back(sip); };
|
||||
inline void addMeasurementErrorPrior(ModFileMeasurementErrorPrior *mep) { measurement_error_prior_vector.push_back(mep); };
|
||||
inline void addStructuralInnovationCorrPrior(ModFileStructuralInnovationCorrPrior *sicp) { structural_innovation_corr_prior_vector.push_back(sicp); };
|
||||
inline void addMeasurementErrorCorrPrior(ModFileMeasurementErrorCorrPrior *mecp) { measurement_error_corr_prior_vector.push_back(mecp); };
|
||||
|
||||
inline void addOption(ModFileOption *o) { option_vector.push_back(o); };
|
||||
inline void addStructuralInnovationOption(ModFileStructuralInnovationOption *sio) { structural_innovation_option_vector.push_back(sio); };
|
||||
inline void addMeasurementErrorOption(ModFileMeasurementErrorOption *meo) { measurement_error_option_vector.push_back(meo); };
|
||||
inline void addStructuralInnovationCorrOption(ModFileStructuralInnovationCorrOption *sico) { structural_innovation_corr_option_vector.push_back(sico); };
|
||||
inline void addMeasurementErrorCorrOption(ModFileMeasurementErrorCorrOption *meco) { measurement_error_corr_option_vector.push_back(meco); };
|
||||
|
||||
inline bool markov_switching_has_val() { return !markov_switching_vector.empty(); };
|
||||
inline bool prior_has_val() { return !prior_vector.empty(); };
|
||||
inline bool structural_innovation_prior_has_val() { return !structural_innovation_prior_vector.empty(); };
|
||||
inline bool measurement_error_prior_has_val() { return !measurement_error_prior_vector.empty(); };
|
||||
inline bool structural_innovation_corr_prior_has_val() { return !structural_innovation_corr_prior_vector.empty(); };
|
||||
inline bool measurement_error_corr_prior_has_val() { return !measurement_error_corr_prior_vector.empty(); };
|
||||
|
||||
inline bool option_has_val() { return !option_vector.empty(); };
|
||||
inline bool structural_innovation_option_has_val() { return !structural_innovation_option_vector.empty(); };
|
||||
inline bool measurement_error_option_has_val() { return !measurement_error_option_vector.empty(); };
|
||||
inline bool structural_innovation_corr_option_has_val() { return !structural_innovation_corr_option_vector.empty(); };
|
||||
inline bool measurement_error_corr_option_has_val() { return !measurement_error_corr_option_vector.empty(); };
|
||||
|
||||
inline vector<MarkovSwitching *>get_markov_switching() { return markov_switching_vector; };
|
||||
inline vector<ModFilePrior *> get_prior() { return prior_vector; };
|
||||
inline vector<ModFileStructuralInnovationPrior *> get_structural_innovation_prior() { return structural_innovation_prior_vector; };
|
||||
inline vector<ModFileMeasurementErrorPrior *> get_measurement_error_prior() { return measurement_error_prior_vector; };
|
||||
inline vector<ModFileStructuralInnovationCorrPrior *> get_structural_innovation_corr_prior() { return structural_innovation_corr_prior_vector; };
|
||||
inline vector<ModFileMeasurementErrorCorrPrior *> get_measurement_error_corr_prior() { return measurement_error_corr_prior_vector; };
|
||||
|
||||
inline vector<ModFileOption *> get_option() { return option_vector; };
|
||||
inline vector<ModFileStructuralInnovationOption *> get_structural_innovation_option() { return structural_innovation_option_vector; };
|
||||
inline vector<ModFileMeasurementErrorOption *> get_measurement_error_option() { return measurement_error_option_vector; };
|
||||
inline vector<ModFileStructuralInnovationCorrOption *> get_structural_innovation_corr_option() { return structural_innovation_corr_option_vector; };
|
||||
inline vector<ModFileMeasurementErrorCorrOption *> get_measurement_error_corr_option() { return measurement_error_corr_option_vector; };
|
||||
|
||||
inline map<string, int > get_exo_names() { return exo_names; };
|
||||
inline map<string, int > get_exo_det_names() { return exo_det_names; };
|
||||
inline map<string, int > get_endo_names() { return endo_names; };
|
||||
inline map<string, int > get_param_names() { return param_names; };
|
||||
inline vector<double> get_params() { return params; };
|
||||
inline double *get_params_data(void) { return params.data(); };
|
||||
inline vector <aux_vars_t> get_aux_vars() { return aux_vars; };
|
||||
inline vector <int> get_predetermined_variables() { return predetermined_variables; };
|
||||
inline vector <int> get_varobs() { return varobs; };
|
||||
inline vector<int> get_NNZDerivatives() { return NNZDerivatives; };
|
||||
|
||||
inline int get_endo_nbr(void) { return endo_nbr; };
|
||||
inline int get_exo_nbr(void) { return exo_nbr; };
|
||||
inline int get_exo_det_nbr(void) { return exo_det_nbr; };
|
||||
inline int get_param_nbr(void) { return param_nbr; };
|
||||
inline vector<size_t> get_zeta_back(void) { return zeta_back; };
|
||||
inline vector<size_t> get_zeta_fwrd(void) { return zeta_fwrd; };
|
||||
inline vector<size_t> get_zeta_mixed(void) { return zeta_mixed; };
|
||||
inline vector<size_t> get_zeta_static(void) { return zeta_static; };
|
||||
|
||||
string get_exo_name_by_index(int index) throw (ValueNotSetException);
|
||||
int get_exo_index_by_name(string name) throw (ValueNotSetException);
|
||||
string get_exo_det_name_by_index(int index) throw (ValueNotSetException);
|
||||
int get_exo_det_index_by_name(string name) throw (ValueNotSetException);
|
||||
string get_endo_name_by_index(int index) throw (ValueNotSetException);
|
||||
int get_endo_index_by_name(string name) throw (ValueNotSetException);
|
||||
string get_param_name_by_index(int index) throw (ValueNotSetException);
|
||||
int get_param_index_by_name(string name) throw (ValueNotSetException);
|
||||
double get_param_value_by_index(int index) throw (ValueNotSetException);
|
||||
|
||||
};
|
||||
|
||||
#endif // ! _DYNARE_CPP_DRIVER_HH
|
|
@ -0,0 +1,5 @@
|
|||
*.c
|
||||
*.cc
|
||||
*.m
|
||||
|
||||
!test1.cc
|
|
@ -24,13 +24,13 @@ test1.o : test1.cc ../dynare_cpp_driver.hh ../dynare_cpp_driver.cc
|
|||
dynare_cpp_driver.o: ../dynare_cpp_driver.cc ../dynare_cpp_driver.hh
|
||||
gcc -g -c ../dynare_cpp_driver.cc -I..
|
||||
example1.cc example1_steadystate.cc: example1.mod
|
||||
$(DYNARE) example1.mod output=first
|
||||
$(DYNARE) example1.mod output=first language=C++
|
||||
example1.o: example1.cc
|
||||
gcc -g -c example1.cc -I..
|
||||
example1_steadystate.o: example1_steadystate.cc
|
||||
gcc -g -c example1_steadystate.cc
|
||||
example1_first_derivatives.o: example1_first_derivatives.cc
|
||||
gcc -g -c example1_first_derivatives.cc
|
||||
example1_steadystate.o: example1_steadystate.c
|
||||
gcc -g -c example1_steadystate.c
|
||||
example1_first_derivatives.o: example1_first_derivatives.c
|
||||
gcc -g -c example1_first_derivatives.c
|
||||
|
||||
test1 : test1.o example1.o example1_steadystate.o example1_first_derivatives.o dynare_cpp_driver.o Matrix.o Vector.o QRDecomposition.o GeneralizedSchurDecomposition.o LUSolver.o DecisionRules.o
|
||||
gcc -g -o test1 test1.o example1.o example1_steadystate.o example1_first_derivatives.o dynare_cpp_driver.o Matrix.o Vector.o QRDecomposition.o GeneralizedSchurDecomposition.o LUSolver.o DecisionRules.o -llapack -lblas -lm -lstdc++
|
||||
|
|
|
@ -3,9 +3,10 @@
|
|||
#include "DecisionRules.hh"
|
||||
|
||||
DynareInfo *preprocessorOutput(void);
|
||||
void steadystate(const double *, const double *, double *, int *);
|
||||
extern "C"{
|
||||
void steadystate(double const*, double const*, double *, int *);
|
||||
void FirstDerivatives(const double *y, double *x, int nb_row_x, double *params, double *steady_state, int it_, double *residual, double *g1, double *v2, double *v3);
|
||||
|
||||
}
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
DynareInfo model_info;
|
||||
|
|
|
@ -2956,153 +2956,6 @@ 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
|
||||
{
|
||||
int lag_presence[3];
|
||||
// Loop on endogenous variables
|
||||
vector<int> zeta_back, zeta_mixed, zeta_fwrd, zeta_static;
|
||||
for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++)
|
||||
{
|
||||
int varID;
|
||||
// Loop on periods
|
||||
for (int lag = 0; lag <= 2; lag++)
|
||||
{
|
||||
lag_presence[lag] = 1;
|
||||
try
|
||||
{
|
||||
varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1);
|
||||
}
|
||||
catch (UnknownDerivIDException &e)
|
||||
{
|
||||
lag_presence[lag] = 0;
|
||||
}
|
||||
}
|
||||
if (lag_presence[0] == 1)
|
||||
if (lag_presence[2] == 1)
|
||||
zeta_mixed.push_back(endoID);
|
||||
else
|
||||
zeta_back.push_back(endoID);
|
||||
else if (lag_presence[2] == 1)
|
||||
zeta_fwrd.push_back(endoID);
|
||||
else
|
||||
zeta_static.push_back(endoID);
|
||||
|
||||
}
|
||||
output << "nstatic = " << zeta_static.size() << ";" << endl
|
||||
<< "nfwrd = " << zeta_fwrd.size() << ";" << endl
|
||||
<< "nback = " << zeta_back.size() << ";" << endl
|
||||
<< "nmixed = " << zeta_mixed.size() << ";" << endl;
|
||||
output << "zeta_static[" << zeta_static.size() << "] = {";
|
||||
for (vector<int>::iterator i = zeta_static.begin(); i != zeta_static.end(); ++i)
|
||||
{
|
||||
if ( i != zeta_static.begin() )
|
||||
output << ",";
|
||||
output << *i;
|
||||
}
|
||||
output << "};" << endl;
|
||||
|
||||
output << "zeta_back[" << zeta_back.size() << "] = {";
|
||||
for (vector<int>::iterator i = zeta_back.begin(); i != zeta_back.end(); ++i)
|
||||
{
|
||||
if ( i != zeta_back.begin() )
|
||||
output << ",";
|
||||
output << *i;
|
||||
}
|
||||
output << "};" << endl;
|
||||
|
||||
output << "zeta_fwrd[" << zeta_fwrd.size() << "] = {";
|
||||
for (vector<int>::iterator i = zeta_fwrd.begin(); i != zeta_fwrd.end(); ++i)
|
||||
{
|
||||
if ( i != zeta_fwrd.begin() )
|
||||
output << ",";
|
||||
output << *i;
|
||||
}
|
||||
output << "};" << endl;
|
||||
|
||||
output << "zeta_mixed[" << zeta_mixed.size() << "] = {";
|
||||
for (vector<int>::iterator i = zeta_mixed.begin(); i != zeta_mixed.end(); ++i)
|
||||
{
|
||||
if ( i != zeta_mixed.begin() )
|
||||
output << ",";
|
||||
output << *i;
|
||||
}
|
||||
output << "};" << endl;
|
||||
|
||||
// Write number of non-zero derivatives
|
||||
// Use -1 if the derivatives have not been computed
|
||||
output << "int *NNZDerivatives[3] = {";
|
||||
switch (order)
|
||||
{
|
||||
case 0:
|
||||
output << NNZDerivatives[0] << ",-1,-1};" << endl;
|
||||
break;
|
||||
case 1:
|
||||
output << NNZDerivatives[0] << "," << NNZDerivatives[1] << ",-1};" << endl;
|
||||
break;
|
||||
case 2:
|
||||
output << NNZDerivatives[0] << "," << NNZDerivatives[1] << "," << NNZDerivatives[2] << "};" << endl;
|
||||
break;
|
||||
default:
|
||||
cerr << "Order larger than 3 not implemented" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
DynamicModel::writeCCOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present) const
|
||||
{
|
||||
int lag_presence[3];
|
||||
// Loop on endogenous variables
|
||||
for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++)
|
||||
{
|
||||
int varID;
|
||||
// Loop on periods
|
||||
for (int lag = 0; lag <= 2; lag++)
|
||||
{
|
||||
lag_presence[lag] = 1;
|
||||
try
|
||||
{
|
||||
varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1);
|
||||
}
|
||||
catch (UnknownDerivIDException &e)
|
||||
{
|
||||
lag_presence[lag] = 0;
|
||||
}
|
||||
}
|
||||
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 << "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
|
||||
<< "NNZDerivatives.push_back(" << NNZDerivatives[0] << ");" << endl;
|
||||
if (order > 1)
|
||||
{
|
||||
output << "NNZDerivatives.push_back(" << NNZDerivatives[1] << ");" << endl;
|
||||
if (order > 2)
|
||||
output << "NNZDerivatives.push_back(" << NNZDerivatives[2] << ");" << endl;
|
||||
else
|
||||
output << "NNZDerivatives.push_back(-1);" << endl;
|
||||
}
|
||||
else
|
||||
output << "NNZDerivatives.push_back(-1);" << endl
|
||||
<< "NNZDerivatives.push_back(-1);" << endl;
|
||||
}
|
||||
|
||||
map<pair<int, pair<int, int > >, expr_t>
|
||||
DynamicModel::collect_first_order_derivatives_endogenous()
|
||||
{
|
||||
|
|
|
@ -0,0 +1,403 @@
|
|||
/*
|
||||
* Copyright (C) 2006-2013 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/>.
|
||||
*/
|
||||
#include "ModFile.hh"
|
||||
#include "DynamicModel.hh"
|
||||
#include "StaticModel.hh"
|
||||
#include "SteadyStateModel.hh"
|
||||
|
||||
void
|
||||
ModFile::writeExternalFiles(const string &basename, FileOutputType output, LanguageOutputType language) const
|
||||
{
|
||||
switch(language)
|
||||
{
|
||||
case c:
|
||||
writeExternalFilesC(basename, output);
|
||||
break;
|
||||
case cpp:
|
||||
writeExternalFilesCC(basename, output);
|
||||
break;
|
||||
default:
|
||||
cerr << "This case shouldn't happen. Contact the authors of Dynare" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
// C interface
|
||||
|
||||
void
|
||||
ModFile::writeExternalFilesC(const string &basename, FileOutputType output) const
|
||||
{
|
||||
writeModelC(basename);
|
||||
steady_state_model.writeSteadyStateFileC(basename, mod_file_struct.ramsey_model_present);
|
||||
|
||||
dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option);
|
||||
|
||||
if (!no_static)
|
||||
static_model.writeStaticFile(basename, false, false, true);
|
||||
|
||||
|
||||
// static_model.writeStaticCFile(basename, block, byte_code, use_dll);
|
||||
// static_model.writeParamsDerivativesFileC(basename, cuda);
|
||||
// static_model.writeAuxVarInitvalC(mOutputFile, oMatlabOutsideModel, cuda);
|
||||
|
||||
// dynamic_model.writeResidualsC(basename, cuda);
|
||||
// dynamic_model.writeParamsDerivativesFileC(basename, cuda);
|
||||
dynamic_model.writeFirstDerivativesC(basename, cuda);
|
||||
|
||||
if (output == second)
|
||||
dynamic_model.writeSecondDerivativesC_csr(basename, cuda);
|
||||
else if (output == third)
|
||||
{
|
||||
dynamic_model.writeSecondDerivativesC_csr(basename, cuda);
|
||||
dynamic_model.writeThirdDerivativesC_csr(basename, cuda);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ModFile::writeModelC(const string &basename) const
|
||||
{
|
||||
string filename = basename + ".c";
|
||||
|
||||
ofstream mDriverCFile;
|
||||
mDriverCFile.open(filename.c_str(), ios::out | ios::binary);
|
||||
if (!mDriverCFile.is_open())
|
||||
{
|
||||
cerr << "Error: Can't open file " << filename << " for writing" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
mDriverCFile << "/*" << endl
|
||||
<< " * " << filename << " : Driver file for Dynare C code" << endl
|
||||
<< " *" << endl
|
||||
<< " * Warning : this file is generated automatically by Dynare" << endl
|
||||
<< " * from model file (.mod)" << endl
|
||||
<< " */" << endl
|
||||
<< endl
|
||||
<< "#include \"dynare_driver.h\"" << endl
|
||||
<< endl
|
||||
<< "struct" << endl
|
||||
<< "{" << endl;
|
||||
|
||||
// Write basic info
|
||||
symbol_table.writeCOutput(mDriverCFile);
|
||||
|
||||
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;" << endl;
|
||||
mDriverCFile.close();
|
||||
|
||||
// Write informational m file
|
||||
ofstream mOutputFile;
|
||||
|
||||
if (basename.size())
|
||||
{
|
||||
string fname(basename);
|
||||
fname += ".m";
|
||||
mOutputFile.open(fname.c_str(), ios::out | ios::binary);
|
||||
if (!mOutputFile.is_open())
|
||||
{
|
||||
cerr << "ERROR: Can't open file " << fname
|
||||
<< " for writing" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cerr << "ERROR: Missing file name" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
mOutputFile << "%" << endl
|
||||
<< "% Status : informational m file" << endl
|
||||
<< "%" << endl
|
||||
<< "% Warning : this file is generated automatically by Dynare" << endl
|
||||
<< "% from model file (.mod)" << endl << endl
|
||||
<< "disp('The following C file was successfully created:');" << endl
|
||||
<< "ls preprocessorOutput.c" << endl << endl;
|
||||
mOutputFile.close();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present) const
|
||||
{
|
||||
int lag_presence[3];
|
||||
// Loop on endogenous variables
|
||||
vector<int> zeta_back, zeta_mixed, zeta_fwrd, zeta_static;
|
||||
for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++)
|
||||
{
|
||||
int varID;
|
||||
// Loop on periods
|
||||
for (int lag = 0; lag <= 2; lag++)
|
||||
{
|
||||
lag_presence[lag] = 1;
|
||||
try
|
||||
{
|
||||
varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1);
|
||||
}
|
||||
catch (UnknownDerivIDException &e)
|
||||
{
|
||||
lag_presence[lag] = 0;
|
||||
}
|
||||
}
|
||||
if (lag_presence[0] == 1)
|
||||
if (lag_presence[2] == 1)
|
||||
zeta_mixed.push_back(endoID);
|
||||
else
|
||||
zeta_back.push_back(endoID);
|
||||
else if (lag_presence[2] == 1)
|
||||
zeta_fwrd.push_back(endoID);
|
||||
else
|
||||
zeta_static.push_back(endoID);
|
||||
|
||||
}
|
||||
output << "size_t nstatic = " << zeta_static.size() << ";" << endl
|
||||
<< "size_t nfwrd = " << zeta_fwrd.size() << ";" << endl
|
||||
<< "size_t nback = " << zeta_back.size() << ";" << endl
|
||||
<< "size_t nmixed = " << zeta_mixed.size() << ";" << endl;
|
||||
output << "size_t zeta_static[" << zeta_static.size() << "] = {";
|
||||
for (vector<int>::iterator i = zeta_static.begin(); i != zeta_static.end(); ++i)
|
||||
{
|
||||
if ( i != zeta_static.begin() )
|
||||
output << ",";
|
||||
output << *i;
|
||||
}
|
||||
output << "};" << endl;
|
||||
|
||||
output << "size_t zeta_back[" << zeta_back.size() << "] = {";
|
||||
for (vector<int>::iterator i = zeta_back.begin(); i != zeta_back.end(); ++i)
|
||||
{
|
||||
if ( i != zeta_back.begin() )
|
||||
output << ",";
|
||||
output << *i;
|
||||
}
|
||||
output << "};" << endl;
|
||||
|
||||
output << "size_t zeta_fwrd[" << zeta_fwrd.size() << "] = {";
|
||||
for (vector<int>::iterator i = zeta_fwrd.begin(); i != zeta_fwrd.end(); ++i)
|
||||
{
|
||||
if ( i != zeta_fwrd.begin() )
|
||||
output << ",";
|
||||
output << *i;
|
||||
}
|
||||
output << "};" << endl;
|
||||
|
||||
output << "size_t zeta_mixed[" << zeta_mixed.size() << "] = {";
|
||||
for (vector<int>::iterator i = zeta_mixed.begin(); i != zeta_mixed.end(); ++i)
|
||||
{
|
||||
if ( i != zeta_mixed.begin() )
|
||||
output << ",";
|
||||
output << *i;
|
||||
}
|
||||
output << "};" << endl;
|
||||
|
||||
// Write number of non-zero derivatives
|
||||
// Use -1 if the derivatives have not been computed
|
||||
output << "int *NNZDerivatives[3] = {";
|
||||
switch (order)
|
||||
{
|
||||
case 0:
|
||||
output << NNZDerivatives[0] << ",-1,-1};" << endl;
|
||||
break;
|
||||
case 1:
|
||||
output << NNZDerivatives[0] << "," << NNZDerivatives[1] << ",-1};" << endl;
|
||||
break;
|
||||
case 2:
|
||||
output << NNZDerivatives[0] << "," << NNZDerivatives[1] << "," << NNZDerivatives[2] << "};" << endl;
|
||||
break;
|
||||
default:
|
||||
cerr << "Order larger than 3 not implemented" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
// C++ interface
|
||||
void
|
||||
ModFile::writeExternalFilesCC(const string &basename, FileOutputType output) const
|
||||
{
|
||||
writeModelCC(basename);
|
||||
steady_state_model.writeSteadyStateFileC(basename, mod_file_struct.ramsey_model_present);
|
||||
|
||||
dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option);
|
||||
|
||||
if (!no_static)
|
||||
static_model.writeStaticFile(basename, false, false, true);
|
||||
|
||||
|
||||
// static_model.writeStaticCFile(basename, block, byte_code, use_dll);
|
||||
// static_model.writeParamsDerivativesFileC(basename, cuda);
|
||||
// static_model.writeAuxVarInitvalC(mOutputFile, oMatlabOutsideModel, cuda);
|
||||
|
||||
// dynamic_model.writeResidualsC(basename, cuda);
|
||||
// dynamic_model.writeParamsDerivativesFileC(basename, cuda);
|
||||
dynamic_model.writeFirstDerivativesC(basename, cuda);
|
||||
|
||||
if (output == second)
|
||||
dynamic_model.writeSecondDerivativesC_csr(basename, cuda);
|
||||
else if (output == third)
|
||||
{
|
||||
dynamic_model.writeSecondDerivativesC_csr(basename, cuda);
|
||||
dynamic_model.writeThirdDerivativesC_csr(basename, cuda);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ModFile::writeModelCC(const string &basename) const
|
||||
{
|
||||
string filename = basename + ".cc";
|
||||
|
||||
ofstream mDriverCFile;
|
||||
mDriverCFile.open(filename.c_str(), ios::out | ios::binary);
|
||||
if (!mDriverCFile.is_open())
|
||||
{
|
||||
cerr << "Error: Can't open file " << filename << " for writing" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
mDriverCFile << "/*" << endl
|
||||
<< " * " << filename << " : Driver file for Dynare C++ code" << endl
|
||||
<< " *" << endl
|
||||
<< " * Warning : this file is generated automatically by Dynare" << endl
|
||||
<< " * from model file (.mod)" << endl
|
||||
<< " */" << endl
|
||||
<< endl
|
||||
<< "#include \"dynare_cpp_driver.hh\"" << endl
|
||||
<< endl
|
||||
<< "DynareInfo::DynareInfo(void)" << endl
|
||||
<< "{" << endl;
|
||||
|
||||
// Write basic info
|
||||
symbol_table.writeCCOutput(mDriverCFile);
|
||||
|
||||
mDriverCFile << endl << "params.resize(param_nbr);" << endl;
|
||||
|
||||
if (dynamic_model.equation_number() > 0)
|
||||
{
|
||||
dynamic_model.writeCCOutput(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 << "};" << endl;
|
||||
mDriverCFile.close();
|
||||
|
||||
// Write informational m file
|
||||
ofstream mOutputFile;
|
||||
|
||||
if (basename.size())
|
||||
{
|
||||
string fname(basename);
|
||||
fname += ".m";
|
||||
mOutputFile.open(fname.c_str(), ios::out | ios::binary);
|
||||
if (!mOutputFile.is_open())
|
||||
{
|
||||
cerr << "ERROR: Can't open file " << fname
|
||||
<< " for writing" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cerr << "ERROR: Missing file name" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
mOutputFile << "%" << endl
|
||||
<< "% Status : informational m file" << endl
|
||||
<< "%" << endl
|
||||
<< "% Warning : this file is generated automatically by Dynare" << endl
|
||||
<< "% from model file (.mod)" << endl << endl
|
||||
<< "disp('The following C++ file was successfully created:');" << endl
|
||||
<< "ls preprocessorOutput.cc" << endl << endl;
|
||||
mOutputFile.close();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
DynamicModel::writeCCOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present) const
|
||||
{
|
||||
int lag_presence[3];
|
||||
// Loop on endogenous variables
|
||||
for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++)
|
||||
{
|
||||
int varID;
|
||||
// Loop on periods
|
||||
for (int lag = 0; lag <= 2; lag++)
|
||||
{
|
||||
lag_presence[lag] = 1;
|
||||
try
|
||||
{
|
||||
varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1);
|
||||
}
|
||||
catch (UnknownDerivIDException &e)
|
||||
{
|
||||
lag_presence[lag] = 0;
|
||||
}
|
||||
}
|
||||
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 << "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
|
||||
<< "NNZDerivatives.push_back(" << NNZDerivatives[0] << ");" << endl;
|
||||
if (order > 1)
|
||||
{
|
||||
output << "NNZDerivatives.push_back(" << NNZDerivatives[1] << ");" << endl;
|
||||
if (order > 2)
|
||||
output << "NNZDerivatives.push_back(" << NNZDerivatives[2] << ");" << endl;
|
||||
else
|
||||
output << "NNZDerivatives.push_back(-1);" << endl;
|
||||
}
|
||||
else
|
||||
output << "NNZDerivatives.push_back(-1);" << endl
|
||||
<< "NNZDerivatives.push_back(-1);" << endl;
|
||||
}
|
||||
|
||||
|
|
@ -53,7 +53,8 @@ dynare_m_SOURCES = \
|
|||
SteadyStateModel.cc \
|
||||
WarningConsolidation.hh \
|
||||
WarningConsolidation.cc \
|
||||
ExtendedPreprocessorTypes.hh
|
||||
ExtendedPreprocessorTypes.hh \
|
||||
ExternalFiles.cc
|
||||
|
||||
# The -I. is for <FlexLexer.h>
|
||||
dynare_m_CPPFLAGS = $(BOOST_CPPFLAGS) -I.
|
||||
|
|
|
@ -820,183 +820,4 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b
|
|||
cout << "done" << endl;
|
||||
}
|
||||
|
||||
void
|
||||
ModFile::writeModelC(const string &basename, bool cuda) const
|
||||
{
|
||||
string filename = basename + ".c";
|
||||
|
||||
ofstream mDriverCFile;
|
||||
mDriverCFile.open(filename.c_str(), ios::out | ios::binary);
|
||||
if (!mDriverCFile.is_open())
|
||||
{
|
||||
cerr << "Error: Can't open file " << filename << " for writing" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
mDriverCFile << "/*" << endl
|
||||
<< " * " << filename << " : Driver file for Dynare C code" << endl
|
||||
<< " *" << endl
|
||||
<< " * Warning : this file is generated automatically by Dynare" << endl
|
||||
<< " * from model file (.mod)" << endl
|
||||
<< " */" << endl
|
||||
<< endl
|
||||
<< "#include \"dynare_driver.h\"" << endl
|
||||
<< endl
|
||||
<< "struct" << endl
|
||||
<< "{" << endl;
|
||||
|
||||
// Write basic info
|
||||
symbol_table.writeCOutput(mDriverCFile);
|
||||
|
||||
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;" << endl;
|
||||
mDriverCFile.close();
|
||||
|
||||
// Write informational m file
|
||||
ofstream mOutputFile;
|
||||
|
||||
if (basename.size())
|
||||
{
|
||||
string fname(basename);
|
||||
fname += ".m";
|
||||
mOutputFile.open(fname.c_str(), ios::out | ios::binary);
|
||||
if (!mOutputFile.is_open())
|
||||
{
|
||||
cerr << "ERROR: Can't open file " << fname
|
||||
<< " for writing" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cerr << "ERROR: Missing file name" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
mOutputFile << "%" << endl
|
||||
<< "% Status : informational m file" << endl
|
||||
<< "%" << endl
|
||||
<< "% Warning : this file is generated automatically by Dynare" << endl
|
||||
<< "% from model file (.mod)" << endl << endl
|
||||
<< "disp('The following C file was successfully created:');" << endl
|
||||
<< "ls preprocessorOutput.c" << endl << endl;
|
||||
mOutputFile.close();
|
||||
}
|
||||
|
||||
void
|
||||
ModFile::writeModelCC(const string &basename, bool cuda) const
|
||||
{
|
||||
string filename = basename + ".cc";
|
||||
|
||||
ofstream mDriverCFile;
|
||||
mDriverCFile.open(filename.c_str(), ios::out | ios::binary);
|
||||
if (!mDriverCFile.is_open())
|
||||
{
|
||||
cerr << "Error: Can't open file " << filename << " for writing" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
mDriverCFile << "/*" << endl
|
||||
<< " * " << filename << " : Driver file for Dynare C++ code" << endl
|
||||
<< " *" << endl
|
||||
<< " * Warning : this file is generated automatically by Dynare" << endl
|
||||
<< " * from model file (.mod)" << endl
|
||||
<< " */" << endl
|
||||
<< endl
|
||||
<< "#include \"dynare_cpp_driver.hh\"" << endl
|
||||
<< endl
|
||||
<< "DynareInfo::DynareInfo(void)" << endl
|
||||
<< "{" << endl;
|
||||
|
||||
// Write basic info
|
||||
symbol_table.writeCCOutput(mDriverCFile);
|
||||
|
||||
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 << "};" << endl;
|
||||
mDriverCFile.close();
|
||||
|
||||
// Write informational m file
|
||||
ofstream mOutputFile;
|
||||
|
||||
if (basename.size())
|
||||
{
|
||||
string fname(basename);
|
||||
fname += ".m";
|
||||
mOutputFile.open(fname.c_str(), ios::out | ios::binary);
|
||||
if (!mOutputFile.is_open())
|
||||
{
|
||||
cerr << "ERROR: Can't open file " << fname
|
||||
<< " for writing" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cerr << "ERROR: Missing file name" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
mOutputFile << "%" << endl
|
||||
<< "% Status : informational m file" << endl
|
||||
<< "%" << endl
|
||||
<< "% Warning : this file is generated automatically by Dynare" << endl
|
||||
<< "% from model file (.mod)" << endl << endl
|
||||
<< "disp('The following C++ file was successfully created:');" << endl
|
||||
<< "ls preprocessorOutput.cc" << endl << endl;
|
||||
mOutputFile.close();
|
||||
}
|
||||
|
||||
void
|
||||
ModFile::writeExternalFiles(const string &basename, FileOutputType output, bool cuda) const
|
||||
{
|
||||
writeModelC(basename, cuda);
|
||||
steady_state_model.writeSteadyStateFileC(basename, mod_file_struct.ramsey_model_present, cuda);
|
||||
|
||||
dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option);
|
||||
|
||||
if (!no_static)
|
||||
static_model.writeStaticFile(basename, false, false, true);
|
||||
|
||||
|
||||
// static_model.writeStaticCFile(basename, block, byte_code, use_dll);
|
||||
// static_model.writeParamsDerivativesFileC(basename, cuda);
|
||||
// static_model.writeAuxVarInitvalC(mOutputFile, oMatlabOutsideModel, cuda);
|
||||
|
||||
// dynamic_model.writeResidualsC(basename, cuda);
|
||||
// dynamic_model.writeParamsDerivativesFileC(basename, cuda);
|
||||
dynamic_model.writeFirstDerivativesC(basename, cuda);
|
||||
|
||||
if (output == second)
|
||||
dynamic_model.writeSecondDerivativesC_csr(basename, cuda);
|
||||
else if (output == third)
|
||||
{
|
||||
dynamic_model.writeSecondDerivativesC_csr(basename, cuda);
|
||||
dynamic_model.writeThirdDerivativesC_csr(basename, cuda);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -138,13 +138,16 @@ public:
|
|||
, bool cygwin, bool msvc
|
||||
#endif
|
||||
) const;
|
||||
// Functions located in ExternalFiles.cc
|
||||
void writeExternalFiles(const string &basename, FileOutputType output, LanguageOutputType language) const;
|
||||
void writeExternalFilesC(const string &basename, FileOutputType output) const;
|
||||
void writeExternalFilesCC(const string &basename, FileOutputType output) const;
|
||||
//! Writes C output files only => No further Matlab processing
|
||||
void writeCOutputFiles(const string &basename) const;
|
||||
void writeModelC(const string &basename, bool cuda) const;
|
||||
void writeModelC(const string &basename) const;
|
||||
//! Writes Cpp output files only => No further Matlab processing
|
||||
void writeCCOutputFiles(const string &basename) const;
|
||||
void writeModelCC(const string &basename, bool cuda) const;
|
||||
void writeExternalFiles(const string &basename, FileOutputType output, bool cuda) const;
|
||||
void writeModelCC(const string &basename) const;
|
||||
};
|
||||
|
||||
#endif // ! MOD_FILE_HH
|
||||
|
|
|
@ -153,7 +153,7 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model
|
|||
}
|
||||
|
||||
void
|
||||
SteadyStateModel::writeSteadyStateFileC(const string &basename, bool ramsey_model, bool cuda) const
|
||||
SteadyStateModel::writeSteadyStateFileC(const string &basename, bool ramsey_model) const
|
||||
{
|
||||
string filename = basename + "_steadystate.c";
|
||||
|
||||
|
@ -167,9 +167,6 @@ SteadyStateModel::writeSteadyStateFileC(const string &basename, bool ramsey_mode
|
|||
|
||||
output << "#include <math.h>" << endl;
|
||||
|
||||
if (cuda)
|
||||
output << "__global__ ";
|
||||
|
||||
output << "void steadystate("
|
||||
<< "const double *exo_, const double *params, double *ys_, int *info)" << endl
|
||||
<< "// Steady state file generated by Dynare preprocessor" << endl
|
||||
|
|
|
@ -49,7 +49,7 @@ public:
|
|||
\param[in] ramsey_model Is there a Ramsey model in the MOD file? If yes, then use the "ys" in argument of the steady state file as initial values
|
||||
*/
|
||||
void writeSteadyStateFile(const string &basename, bool ramsey_model) const;
|
||||
void writeSteadyStateFileC(const string &basename, bool ramsey_model, bool cuda) const;
|
||||
void writeSteadyStateFileC(const string &basename, bool ramsey_model) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue