dynare/dynare++/src/dynare3.hh

306 lines
5.8 KiB
C++
Raw Normal View History

// $Id: dynare3.h 1764 2008-03-31 14:30:55Z kamenik $
// Copyright 2005, Ondra Kamenik
#ifndef DYNARE3_H
#define DYNARE3_H
#include "../tl/cc/t_container.hh"
#include "../tl/cc/sparse_tensor.hh"
#include "../kord/decision_rule.hh"
#include "../kord/dynamic_model.hh"
#include "dynare_model.hh"
#include "nlsolve.hh"
#include <vector>
#include <matio.h>
class Dynare;
2017-05-16 16:30:27 +02:00
class DynareNameList : public NameList
{
vector<const char *> names;
public:
2017-05-16 16:30:27 +02:00
DynareNameList(const Dynare &dynare);
int
getNum() const
{
return (int) names.size();
}
const char *
getName(int i) const
{
return names[i];
}
/** This for each string of the input vector calculates its index
* in the names. And returns the resulting vector of indices. If
* the name cannot be found, then an exception is raised. */
vector<int> selectIndices(const vector<const char *> &ns) const;
};
2017-05-16 16:30:27 +02:00
class DynareExogNameList : public NameList
{
vector<const char *> names;
public:
2017-05-16 16:30:27 +02:00
DynareExogNameList(const Dynare &dynare);
int
getNum() const
{
return (int) names.size();
}
const char *
getName(int i) const
{
return names[i];
}
};
2017-05-16 16:30:27 +02:00
class DynareStateNameList : public NameList
{
vector<const char *> names;
public:
2017-05-16 16:30:27 +02:00
DynareStateNameList(const Dynare &dynare, const DynareNameList &dnl,
const DynareExogNameList &denl);
int
getNum() const
{
return (int) names.size();
}
const char *
getName(int i) const
{
return names[i];
}
};
// The following only implements DynamicModel with help of ogdyn::DynareModel
class DynareJacobian;
2017-05-16 16:30:27 +02:00
class Dynare : public DynamicModel
{
friend class DynareNameList;
friend class DynareExogNameList;
friend class DynareStateNameList;
friend class DynareJacobian;
Journal &journal;
ogdyn::DynareModel *model;
Vector *ysteady;
TensorContainer<FSSparseTensor> md;
DynareNameList *dnl;
DynareExogNameList *denl;
DynareStateNameList *dsnl;
ogp::FormulaEvaluator *fe;
ogp::FormulaDerEvaluator *fde;
const double ss_tol;
public:
2017-05-16 16:30:27 +02:00
/** Parses the given model file and uses the given order to
* override order from the model file (if it is != -1). */
Dynare(const char *modname, int ord, double sstol, Journal &jr);
/** Parses the given equations with explicitly given names. */
Dynare(const char **endo, int num_endo,
const char **exo, int num_exo,
const char **par, int num_par,
const char *equations, int len, int ord,
double sstol, Journal &jr);
/** Makes a deep copy of the object. */
Dynare(const Dynare &dyn);
DynamicModel *
clone() const
{
return new Dynare(*this);
}
virtual
~Dynare();
int
nstat() const
{
return model->getAtoms().nstat();
}
int
nboth() const
{
return model->getAtoms().nboth();
}
int
npred() const
{
return model->getAtoms().npred();
}
int
nforw() const
{
return model->getAtoms().nforw();
}
int
nexog() const
{
return model->getAtoms().nexo();
}
int
nys() const
{
return model->getAtoms().nys();
}
int
nyss() const
{
return model->getAtoms().nyss();
}
int
ny() const
{
return model->getAtoms().ny();
}
int
order() const
{
return model->getOrder();
}
const NameList &
getAllEndoNames() const
{
return *dnl;
}
const NameList &
getStateNames() const
{
return *dsnl;
}
const NameList &
getExogNames() const
{
return *denl;
}
TwoDMatrix &
getVcov()
{
return model->getVcov();
}
const TwoDMatrix &
getVcov() const
{
return model->getVcov();
}
Vector &
getParams()
{
return model->getParams();
}
const Vector &
getParams() const
{
return model->getParams();
}
void
setInitOuter(const Vector &x)
{
model->setInitOuter(x);
}
const TensorContainer<FSSparseTensor> &
getModelDerivatives() const
{
return md;
}
const Vector &
getSteady() const
{
return *ysteady;
}
Vector &
getSteady()
{
return *ysteady;
}
const ogdyn::DynareModel &
getModel() const
{
return *model;
}
// here is true public interface
void solveDeterministicSteady(Vector &steady);
void
solveDeterministicSteady()
{
solveDeterministicSteady(*ysteady);
}
void evaluateSystem(Vector &out, const Vector &yy, const Vector &xx);
void evaluateSystem(Vector &out, const Vector &yym, const Vector &yy,
const Vector &yyp, const Vector &xx);
void calcDerivatives(const Vector &yy, const Vector &xx);
void calcDerivativesAtSteady();
void writeMat(mat_t *fd, const char *prefix) const;
void writeDump(const std::string &basename) const;
private:
2017-05-16 16:30:27 +02:00
void writeModelInfo(Journal &jr) const;
};
2017-05-16 16:30:27 +02:00
class DynareEvalLoader : public ogp::FormulaEvalLoader, public Vector
{
public:
2017-05-16 16:30:27 +02:00
DynareEvalLoader(const ogp::FineAtoms &a, Vector &out);
void
load(int i, double res)
{
operator[](i) = res;
}
};
2017-05-16 16:30:27 +02:00
class DynareDerEvalLoader : public ogp::FormulaDerEvalLoader
{
protected:
2017-05-16 16:30:27 +02:00
const ogp::FineAtoms &atoms;
TensorContainer<FSSparseTensor> &md;
public:
2017-05-16 16:30:27 +02:00
DynareDerEvalLoader(const ogp::FineAtoms &a, TensorContainer<FSSparseTensor> &mod_ders,
int order);
void load(int i, int iord, const int *vars, double res);
};
2017-05-16 16:30:27 +02:00
class DynareJacobian : public ogu::Jacobian, public ogp::FormulaDerEvalLoader
{
protected:
2017-05-16 16:30:27 +02:00
Dynare &d;
public:
2017-05-16 16:30:27 +02:00
DynareJacobian(Dynare &dyn);
virtual ~DynareJacobian()
= default;
2017-05-16 16:30:27 +02:00
void load(int i, int iord, const int *vars, double res);
void eval(const Vector &in);
};
2017-05-16 16:30:27 +02:00
class DynareVectorFunction : public ogu::VectorFunction
{
protected:
2017-05-16 16:30:27 +02:00
Dynare &d;
public:
2017-05-16 16:30:27 +02:00
DynareVectorFunction(Dynare &dyn)
: d(dyn)
{
}
virtual ~DynareVectorFunction()
= default;
2017-05-16 16:30:27 +02:00
int
inDim() const
{
return d.ny();
}
int
outDim() const
{
return d.ny();
}
void eval(const ConstVector &in, Vector &out);
};
#endif
// Local Variables:
// mode:C++
// End: