dynare/dynare++/src/dynare3.hh

319 lines
7.0 KiB
C++
Raw Normal View History

/*
* Copyright © 2005 Ondra Kamenik
* Copyright © 2019 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 <https://www.gnu.org/licenses/>.
*/
#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>
2019-04-23 18:57:52 +02:00
#include <memory>
#include <matio.h>
class Dynare;
2017-05-16 16:30:27 +02:00
class DynareNameList : public NameList
{
std::vector<std::string> names;
public:
2017-05-16 16:30:27 +02:00
DynareNameList(const Dynare &dynare);
int
getNum() const override
2017-05-16 16:30:27 +02:00
{
return static_cast<int>(names.size());
2017-05-16 16:30:27 +02:00
}
const std::string &
getName(int i) const override
2017-05-16 16:30:27 +02:00
{
return names[i];
}
2019-06-19 17:33:01 +02:00
/* 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. */
std::vector<int> selectIndices(const std::vector<std::string> &ns) const;
};
2017-05-16 16:30:27 +02:00
class DynareExogNameList : public NameList
{
std::vector<std::string> names;
public:
2017-05-16 16:30:27 +02:00
DynareExogNameList(const Dynare &dynare);
int
getNum() const override
2017-05-16 16:30:27 +02:00
{
return static_cast<int>(names.size());
2017-05-16 16:30:27 +02:00
}
const std::string &
getName(int i) const override
2017-05-16 16:30:27 +02:00
{
return names[i];
}
};
2017-05-16 16:30:27 +02:00
class DynareStateNameList : public NameList
{
std::vector<std::string> names;
public:
2017-05-16 16:30:27 +02:00
DynareStateNameList(const Dynare &dynare, const DynareNameList &dnl,
const DynareExogNameList &denl);
int
getNum() const override
2017-05-16 16:30:27 +02:00
{
return static_cast<int>(names.size());
2017-05-16 16:30:27 +02:00
}
const std::string &
getName(int i) const override
2017-05-16 16:30:27 +02:00
{
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;
2019-04-23 18:57:52 +02:00
std::unique_ptr<ogdyn::DynareModel> model;
std::unique_ptr<Vector> ysteady;
2017-05-16 16:30:27 +02:00
TensorContainer<FSSparseTensor> md;
2019-04-23 18:57:52 +02:00
std::unique_ptr<DynareNameList> dnl;
std::unique_ptr<DynareExogNameList> denl;
std::unique_ptr<DynareStateNameList> dsnl;
std::unique_ptr<ogp::FormulaEvaluator> fe;
std::unique_ptr<ogp::FormulaDerEvaluator> fde;
2017-05-16 16:30:27 +02:00
const double ss_tol;
public:
2019-06-19 17:33:01 +02:00
/* Parses the given model file and uses the given order to
override order from the model file (if it is 1). */
2019-04-23 18:57:52 +02:00
Dynare(const std::string &modname, int ord, double sstol, Journal &jr);
2017-05-16 16:30:27 +02:00
/** Parses the given equations with explicitly given names. */
2019-04-23 18:57:52 +02:00
Dynare(const std::vector<std::string> &endo,
const std::vector<std::string> &exo,
const std::vector<std::string> &par,
2019-04-24 14:52:30 +02:00
const std::string &equations, int ord,
2017-05-16 16:30:27 +02:00
double sstol, Journal &jr);
2019-06-19 17:33:01 +02:00
/* Makes a deep copy of the object. */
2017-05-16 16:30:27 +02:00
Dynare(const Dynare &dyn);
2019-04-23 18:57:52 +02:00
Dynare(Dynare &&) = default;
std::unique_ptr<DynamicModel>
clone() const override
2017-05-16 16:30:27 +02:00
{
return std::make_unique<Dynare>(*this);
2017-05-16 16:30:27 +02:00
}
2019-12-20 14:36:20 +01:00
2019-04-23 18:57:52 +02:00
~Dynare() override = default;
2017-05-16 16:30:27 +02:00
int
nstat() const override
2017-05-16 16:30:27 +02:00
{
return model->getAtoms().nstat();
}
int
nboth() const override
2017-05-16 16:30:27 +02:00
{
return model->getAtoms().nboth();
}
int
npred() const override
2017-05-16 16:30:27 +02:00
{
return model->getAtoms().npred();
}
int
nforw() const override
2017-05-16 16:30:27 +02:00
{
return model->getAtoms().nforw();
}
int
nexog() const override
2017-05-16 16:30:27 +02:00
{
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 override
2017-05-16 16:30:27 +02:00
{
return model->getOrder();
}
const NameList &
getAllEndoNames() const override
2017-05-16 16:30:27 +02:00
{
return *dnl;
}
const NameList &
getStateNames() const override
2017-05-16 16:30:27 +02:00
{
return *dsnl;
}
const NameList &
getExogNames() const override
2017-05-16 16:30:27 +02:00
{
return *denl;
}
TwoDMatrix &
getVcov()
{
return model->getVcov();
}
const TwoDMatrix &
getVcov() const override
2017-05-16 16:30:27 +02:00
{
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 override
2017-05-16 16:30:27 +02:00
{
return md;
}
const Vector &
getSteady() const override
2017-05-16 16:30:27 +02:00
{
return *ysteady;
}
Vector &
getSteady() override
2017-05-16 16:30:27 +02:00
{
return *ysteady;
}
const ogdyn::DynareModel &
getModel() const
{
return *model;
}
// here is true public interface
void solveDeterministicSteady(Vector &steady);
void
solveDeterministicSteady() override
2017-05-16 16:30:27 +02:00
{
solveDeterministicSteady(*ysteady);
}
void evaluateSystem(Vector &out, const ConstVector &yy, const Vector &xx) override;
void evaluateSystem(Vector &out, const ConstVector &yym, const ConstVector &yy,
const ConstVector &yyp, const Vector &xx) override;
2017-05-16 16:30:27 +02:00
void calcDerivatives(const Vector &yy, const Vector &xx);
void calcDerivativesAtSteady() override;
2017-05-16 16:30:27 +02:00
2019-04-23 18:57:52 +02:00
void writeMat(mat_t *fd, const std::string &prefix) const;
2017-05-16 16:30:27 +02:00
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) override
2017-05-16 16:30:27 +02:00
{
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) override;
};
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);
2019-04-23 18:57:52 +02:00
~DynareJacobian() override = default;
void load(int i, int iord, const int *vars, double res) override;
void eval(const Vector &in) override;
};
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)
{
}
2019-04-23 18:57:52 +02:00
~DynareVectorFunction() override = default;
2017-05-16 16:30:27 +02:00
int
inDim() const override
2017-05-16 16:30:27 +02:00
{
return d.ny();
}
int
outDim() const override
2017-05-16 16:30:27 +02:00
{
return d.ny();
}
void eval(const ConstVector &in, Vector &out) override;
};
#endif