2009-04-14 16:39:53 +02:00
|
|
|
/*
|
2020-03-20 18:00:56 +01:00
|
|
|
* Copyright © 2003-2020 Dynare Team
|
2009-04-14 16:39:53 +02:00
|
|
|
*
|
|
|
|
* 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
|
2021-06-09 16:52:20 +02:00
|
|
|
* along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2009-04-14 16:39:53 +02:00
|
|
|
*/
|
|
|
|
|
2009-12-16 14:21:31 +01:00
|
|
|
#ifndef _STATIC_MODEL_HH
|
|
|
|
#define _STATIC_MODEL_HH
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <fstream>
|
2019-04-04 17:01:37 +02:00
|
|
|
#include <filesystem>
|
2018-10-26 11:44:26 +02:00
|
|
|
|
2009-04-14 16:39:53 +02:00
|
|
|
#include "ModelTree.hh"
|
|
|
|
|
2018-10-10 17:08:54 +02:00
|
|
|
class DynamicModel;
|
|
|
|
|
2010-04-23 18:39:07 +02:00
|
|
|
//! Stores a static model, as derived from the "model" block when leads and lags have been removed
|
2009-04-14 16:39:53 +02:00
|
|
|
class StaticModel : public ModelTree
|
|
|
|
{
|
|
|
|
private:
|
2009-07-07 17:58:17 +02:00
|
|
|
//! Writes static model file (standard Matlab version)
|
2018-06-27 15:01:31 +02:00
|
|
|
void writeStaticMFile(const string &basename) const;
|
2009-12-16 14:21:31 +01:00
|
|
|
|
2011-08-18 12:44:11 +02:00
|
|
|
//! Writes static model file (C version)
|
2018-06-27 15:01:31 +02:00
|
|
|
void writeStaticCFile(const string &basename) const;
|
2011-08-18 12:44:11 +02:00
|
|
|
|
2015-07-27 15:33:38 +02:00
|
|
|
//! Writes static model file (Julia version)
|
2015-07-27 15:59:13 +02:00
|
|
|
void writeStaticJuliaFile(const string &basename) const;
|
2015-07-27 15:33:38 +02:00
|
|
|
|
2011-08-18 12:44:11 +02:00
|
|
|
//! Writes the static model equations and its derivatives
|
2018-03-27 17:14:30 +02:00
|
|
|
void writeStaticModel(const string &basename, ostream &StaticOutput, bool use_dll, bool julia) const;
|
2011-08-18 12:44:11 +02:00
|
|
|
|
2020-05-19 16:56:53 +02:00
|
|
|
//! Writes the main static function of block decomposed model (MATLAB version)
|
|
|
|
void writeStaticBlockMFile(const string &basename) const;
|
2009-04-14 16:39:53 +02:00
|
|
|
|
2020-06-23 15:13:04 +02:00
|
|
|
//! Writes the main static function of block decomposed model (C version)
|
|
|
|
void writeStaticBlockCFile(const string &basename) const;
|
|
|
|
|
2020-06-22 12:46:33 +02:00
|
|
|
//! Helper for writing a per-block static file of block decomposed model
|
|
|
|
void writeStaticPerBlockHelper(int blk, ostream &output, ExprNodeOutputType output_type, temporary_terms_t &temporary_terms) const;
|
|
|
|
|
2020-05-19 16:56:53 +02:00
|
|
|
//! Writes the per-block static files of block decomposed model (MATLAB version)
|
|
|
|
void writeStaticPerBlockMFiles(const string &basename) const;
|
2009-06-30 17:07:09 +02:00
|
|
|
|
2020-06-23 15:13:04 +02:00
|
|
|
//! Writes the per-block static files of block decomposed model (C version)
|
|
|
|
void writeStaticPerBlockCFiles(const string &basename) const;
|
|
|
|
|
2020-05-19 17:43:35 +02:00
|
|
|
//! Writes the code of the block-decomposed model in virtual machine bytecode
|
|
|
|
void writeStaticBlockBytecode(const string &basename) const;
|
2010-01-22 11:03:29 +01:00
|
|
|
|
|
|
|
//! Writes the code of the model in virtual machine bytecode
|
2020-05-19 17:43:35 +02:00
|
|
|
void writeStaticBytecode(const string &basename) const;
|
|
|
|
|
|
|
|
//! Adds per-block information for bytecode simulation in a separate .bin file
|
|
|
|
void writeBlockBytecodeBinFile(const string &basename, int num,
|
|
|
|
int &u_count_int, bool &file_open) const;
|
2010-01-22 11:03:29 +01:00
|
|
|
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Computes jacobian and prepares for equation normalization
|
|
|
|
/*! Using values from initval/endval blocks and parameter initializations:
|
|
|
|
- computes the jacobian for the model w.r. to contemporaneous variables
|
|
|
|
- removes edges of the incidence matrix when derivative w.r. to the corresponding variable is too close to zero (below the cutoff)
|
|
|
|
*/
|
2010-09-16 19:00:48 +02:00
|
|
|
void evaluateJacobian(const eval_context_t &eval_context, jacob_map_t *j_m, bool dynamic);
|
2009-07-10 16:22:40 +02:00
|
|
|
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Write derivative code of an equation w.r. to a variable
|
2020-05-13 12:53:47 +02:00
|
|
|
void compileDerivative(ofstream &code_file, unsigned int &instruction_number, int eq, int symb_id, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs) const;
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Write chain rule derivative code of an equation w.r. to a variable
|
2020-05-13 12:53:47 +02:00
|
|
|
void compileChainRuleDerivative(ofstream &code_file, unsigned int &instruction_number, int blk, int eq, int var, int lag, const temporary_terms_t &temporary_terms, const temporary_terms_idxs_t &temporary_terms_idxs) const;
|
2009-04-27 19:15:14 +02:00
|
|
|
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Get the type corresponding to a derivation ID
|
2018-06-04 12:53:26 +02:00
|
|
|
SymbolType getTypeByDerivID(int deriv_id) const noexcept(false) override;
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Get the lag corresponding to a derivation ID
|
2018-06-04 12:53:26 +02:00
|
|
|
int getLagByDerivID(int deriv_id) const noexcept(false) override;
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Get the symbol ID corresponding to a derivation ID
|
2018-06-04 12:53:26 +02:00
|
|
|
int getSymbIDByDerivID(int deriv_id) const noexcept(false) override;
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Compute the column indices of the static Jacobian
|
|
|
|
void computeStatJacobianCols();
|
|
|
|
//! Computes chain rule derivatives of the Jacobian w.r. to endogenous variables
|
2020-03-19 17:46:10 +01:00
|
|
|
void computeChainRuleJacobian();
|
2009-12-16 14:21:31 +01:00
|
|
|
|
2018-03-27 17:14:30 +02:00
|
|
|
//! Helper functions for writeStaticModel
|
2018-06-27 15:01:31 +02:00
|
|
|
void writeStaticModelHelper(const string &basename,
|
|
|
|
const string &name, const string &retvalname,
|
2018-03-27 17:14:30 +02:00
|
|
|
const string &name_tt, size_t ttlen,
|
|
|
|
const string &previous_tt_name,
|
|
|
|
const ostringstream &init_s, const ostringstream &end_s,
|
|
|
|
const ostringstream &s, const ostringstream &s_tt) const;
|
|
|
|
void writeWrapperFunctions(const string &basename, const string &ending) const;
|
2018-05-23 16:10:26 +02:00
|
|
|
|
|
|
|
//! Create a legacy *_static.m file for Matlab/Octave not yet using the temporary terms array interface
|
|
|
|
void writeStaticMatlabCompatLayer(const string &name) const;
|
|
|
|
|
2018-03-27 17:14:30 +02:00
|
|
|
void writeStaticModel(ostream &DynamicOutput, bool use_dll, bool julia) const;
|
|
|
|
void writeStaticModel(const string &dynamic_basename, bool use_dll, bool julia) const;
|
2018-10-09 18:27:19 +02:00
|
|
|
|
|
|
|
//! Internal helper for the copy constructor and assignment operator
|
|
|
|
/*! Copies all the structures that contain ExprNode*, by the converting the
|
|
|
|
pointers into their equivalent in the new tree */
|
|
|
|
void copyHelper(const StaticModel &m);
|
|
|
|
|
2009-04-14 16:39:53 +02:00
|
|
|
public:
|
2018-08-14 14:23:21 +02:00
|
|
|
StaticModel(SymbolTable &symbol_table_arg,
|
|
|
|
NumericalConstants &num_constants,
|
2018-09-14 17:04:06 +02:00
|
|
|
ExternalFunctionsTable &external_functions_table_arg);
|
2009-07-07 16:20:48 +02:00
|
|
|
|
2018-10-09 18:27:19 +02:00
|
|
|
StaticModel(const StaticModel &m);
|
|
|
|
StaticModel(StaticModel &&) = delete;
|
2019-12-20 16:59:30 +01:00
|
|
|
StaticModel &operator=(const StaticModel &m);
|
2018-10-10 17:08:54 +02:00
|
|
|
/* The move assignment operator is not explicitly deleted, otherwise the
|
2019-12-20 16:59:30 +01:00
|
|
|
static_cast from DynamicModel does not work. However it looks like this
|
|
|
|
operator will not be used in the end. See
|
|
|
|
https://en.cppreference.com/w/cpp/language/copy_initialization
|
|
|
|
With C++17, it should be possible to explicitly delete it */
|
2018-10-10 17:08:54 +02:00
|
|
|
//StaticModel & operator=(StaticModel &&) = delete;
|
|
|
|
|
|
|
|
//! Creates the static version of a dynamic model
|
|
|
|
explicit StaticModel(const DynamicModel &m);
|
2018-10-09 18:27:19 +02:00
|
|
|
|
2020-05-20 11:44:40 +02:00
|
|
|
//! Writes information about the static model to the driver file
|
|
|
|
void writeDriverOutput(ostream &output, bool block) const;
|
2009-07-07 16:20:48 +02:00
|
|
|
|
2009-12-16 14:21:31 +01:00
|
|
|
//! Execute computations (variable sorting + derivation)
|
|
|
|
/*!
|
|
|
|
\param eval_context evaluation context for normalization
|
|
|
|
\param no_tmp_terms if true, no temporary terms will be computed in the static files
|
2018-11-22 14:32:40 +01:00
|
|
|
\param derivsOrder order of derivation with respect to endogenous
|
|
|
|
\param paramsDerivsOrder order of derivatives w.r. to a pair (endogenous, parameter) to be computed
|
2009-12-16 14:21:31 +01:00
|
|
|
*/
|
2018-12-20 17:04:28 +01:00
|
|
|
void computingPass(int derivsOrder, int paramsDerivsOrder, const eval_context_t &eval_context, bool no_tmp_terms, bool block, bool bytecode);
|
2009-12-16 14:21:31 +01:00
|
|
|
|
2009-04-14 16:39:53 +02:00
|
|
|
//! Writes static model file
|
2019-04-04 17:01:37 +02:00
|
|
|
void writeStaticFile(const string &basename, bool block, bool bytecode, bool use_dll, const string &mexext, const filesystem::path &matlabroot, const filesystem::path &dynareroot, bool julia) const;
|
2009-04-17 18:26:23 +02:00
|
|
|
|
2017-02-27 15:40:34 +01:00
|
|
|
//! Write JSON Output (used by PlannerObjectiveStatement)
|
|
|
|
void writeJsonOutput(ostream &output) const;
|
|
|
|
|
2017-02-20 12:18:11 +01:00
|
|
|
//! Write JSON representation of static model
|
2017-03-02 18:34:18 +01:00
|
|
|
void writeJsonComputingPassOutput(ostream &output, bool writeDetails) const;
|
2017-02-20 12:18:11 +01:00
|
|
|
|
|
|
|
//! Writes file containing static parameters derivatives
|
2017-03-02 18:34:18 +01:00
|
|
|
void writeJsonParamsDerivativesFile(ostream &output, bool writeDetails) const;
|
2017-02-20 12:18:11 +01:00
|
|
|
|
2012-11-29 18:07:48 +01:00
|
|
|
//! Writes file containing static parameters derivatives
|
2015-07-28 12:29:42 +02:00
|
|
|
void writeParamsDerivativesFile(const string &basename, bool julia) const;
|
2012-11-29 18:07:48 +01:00
|
|
|
|
2009-04-30 15:14:33 +02:00
|
|
|
//! Writes LaTeX file with the equations of the static model
|
2019-12-16 19:42:59 +01:00
|
|
|
void writeLatexFile(const string &basename, bool write_equation_tags) const;
|
2009-04-30 15:14:33 +02:00
|
|
|
|
2010-04-27 17:04:52 +02:00
|
|
|
//! Writes initializations in oo_.steady_state or steady state file for the auxiliary variables
|
|
|
|
void writeAuxVarInitval(ostream &output, ExprNodeOutputType output_type) const;
|
2009-09-30 17:10:31 +02:00
|
|
|
|
2015-08-17 15:36:18 +02:00
|
|
|
//! Writes definition of the auxiliary variables in a .m or .jl file
|
2019-12-16 19:42:59 +01:00
|
|
|
void writeSetAuxiliaryVariables(const string &basename, bool julia) const;
|
2016-04-04 17:11:03 +02:00
|
|
|
void writeAuxVarRecursiveDefinitions(ostream &output, ExprNodeOutputType output_type) const;
|
2017-08-30 11:32:01 +02:00
|
|
|
void writeLatexAuxVarRecursiveDefinitions(ostream &output) const;
|
2017-10-16 17:24:55 +02:00
|
|
|
void writeJsonAuxVarRecursiveDefinitions(ostream &output) const;
|
2011-07-24 20:52:03 +02:00
|
|
|
|
2016-08-12 11:50:57 +02:00
|
|
|
//! To ensure that no exogenous is present in the planner objective
|
|
|
|
//! See #1264
|
|
|
|
bool exoPresentInEqs() const;
|
|
|
|
|
2018-06-04 12:53:26 +02:00
|
|
|
int getDerivID(int symb_id, int lag) const noexcept(false) override;
|
|
|
|
void addAllParamDerivId(set<int> &deriv_id_set) override;
|
2009-04-14 16:39:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|