2008-01-11 14:42:14 +01:00
|
|
|
|
/*
|
2023-01-09 15:19:36 +01:00
|
|
|
|
* Copyright © 2007-2023 Dynare Team
|
2008-01-11 14:42:14 +01: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 17:33:48 +02:00
|
|
|
|
* along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2008-01-11 14:42:14 +01:00
|
|
|
|
*/
|
|
|
|
|
|
2022-07-29 12:40:33 +02:00
|
|
|
|
#ifndef _INTERPRETER_HH
|
|
|
|
|
#define _INTERPRETER_HH
|
2007-11-21 00:27:30 +01:00
|
|
|
|
|
2021-02-09 15:55:36 +01:00
|
|
|
|
#include <cstddef>
|
2023-11-29 19:00:21 +01:00
|
|
|
|
#include <fstream>
|
2023-10-19 19:56:10 +02:00
|
|
|
|
#include <map>
|
|
|
|
|
#include <stack>
|
2023-11-29 19:00:21 +01:00
|
|
|
|
#include <string>
|
|
|
|
|
#include <tuple>
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
2021-02-09 15:55:36 +01:00
|
|
|
|
|
|
|
|
|
#include "dynmex.h"
|
2023-11-29 19:00:21 +01:00
|
|
|
|
#include "dynumfpack.h"
|
2021-02-09 15:55:36 +01:00
|
|
|
|
|
|
|
|
|
#include "ErrorHandling.hh"
|
2023-10-19 19:56:10 +02:00
|
|
|
|
#include "Evaluate.hh"
|
2023-11-29 19:00:21 +01:00
|
|
|
|
#include "Mem_Mngr.hh"
|
2007-11-21 00:27:30 +01:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
2023-10-19 19:56:10 +02:00
|
|
|
|
struct t_save_op_s
|
|
|
|
|
{
|
|
|
|
|
short int lag, operat;
|
|
|
|
|
int first, second;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct s_plan
|
|
|
|
|
{
|
|
|
|
|
string var, exo;
|
|
|
|
|
int var_num, exo_num;
|
|
|
|
|
vector<pair<int, double>> per_value;
|
|
|
|
|
vector<double> value;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct table_conditional_local_type
|
|
|
|
|
{
|
|
|
|
|
bool is_cond;
|
|
|
|
|
int var_exo, var_endo;
|
|
|
|
|
double constrained_value;
|
|
|
|
|
};
|
|
|
|
|
using vector_table_conditional_local_type = vector<table_conditional_local_type>;
|
|
|
|
|
using table_conditional_global_type = map<int, vector_table_conditional_local_type>;
|
|
|
|
|
|
2023-11-29 19:00:21 +01:00
|
|
|
|
constexpr int IFLD = 0, IFDIV = 1, IFLESS = 2, IFSUB = 3, IFLDZ = 4, IFMUL = 5, IFSTP = 6,
|
|
|
|
|
IFADD = 7;
|
2023-10-19 19:56:10 +02:00
|
|
|
|
constexpr double eps = 1e-15, very_big = 1e24;
|
|
|
|
|
constexpr int alt_symbolic_count_max = 1;
|
|
|
|
|
constexpr double mem_increasing_factor = 1.1;
|
|
|
|
|
|
2022-07-29 12:58:10 +02:00
|
|
|
|
constexpr int NO_ERROR_ON_EXIT {0}, ERROR_ON_EXIT {1};
|
|
|
|
|
|
2023-10-19 19:56:10 +02:00
|
|
|
|
class Interpreter
|
2007-11-21 00:27:30 +01:00
|
|
|
|
{
|
2010-01-22 17:42:08 +01:00
|
|
|
|
private:
|
2023-10-19 19:56:10 +02:00
|
|
|
|
double g0, gp0, glambda2;
|
|
|
|
|
int try_at_iteration;
|
|
|
|
|
|
|
|
|
|
void *Symbolic {nullptr}, *Numeric {nullptr};
|
|
|
|
|
|
2023-11-29 19:00:21 +01:00
|
|
|
|
const BasicSymbolTable& symbol_table;
|
2023-10-19 19:56:10 +02:00
|
|
|
|
const bool steady_state; // Whether this is a static or dynamic model
|
|
|
|
|
|
|
|
|
|
// Whether to use the block-decomposed version of the bytecode file
|
|
|
|
|
bool block_decomposed;
|
|
|
|
|
|
2023-11-29 19:00:21 +01:00
|
|
|
|
Evaluate& evaluator;
|
2023-10-19 19:56:10 +02:00
|
|
|
|
|
|
|
|
|
fstream SaveCode;
|
|
|
|
|
|
|
|
|
|
Mem_Mngr mem_mngr;
|
|
|
|
|
vector<int> u_liste;
|
|
|
|
|
int *NbNZRow, *NbNZCol;
|
|
|
|
|
NonZeroElem **FNZE_R, **FNZE_C;
|
|
|
|
|
int u_count_init;
|
|
|
|
|
|
|
|
|
|
int *pivot, *pivotk, *pivot_save;
|
|
|
|
|
double *pivotv, *pivotva;
|
2023-11-29 19:00:21 +01:00
|
|
|
|
int* b;
|
|
|
|
|
bool* line_done;
|
2023-10-19 19:56:10 +02:00
|
|
|
|
bool symbolic, alt_symbolic;
|
|
|
|
|
int alt_symbolic_count;
|
|
|
|
|
double markowitz_c_s;
|
|
|
|
|
double res1a;
|
|
|
|
|
long int nop1;
|
|
|
|
|
map<tuple<int, int, int>, int> IM_i;
|
|
|
|
|
int u_count_alloc, u_count_alloc_save;
|
|
|
|
|
double slowc, slowc_save, prev_slowc_save, markowitz_c;
|
2023-11-29 19:00:21 +01:00
|
|
|
|
int* index_equa; // Actually unused
|
2023-10-19 19:56:10 +02:00
|
|
|
|
int u_count, tbreak_g;
|
|
|
|
|
int iter;
|
|
|
|
|
int start_compare;
|
|
|
|
|
int restart;
|
|
|
|
|
double lu_inc_tol;
|
|
|
|
|
|
|
|
|
|
SuiteSparse_long *Ap_save, *Ai_save;
|
|
|
|
|
double *Ax_save, *b_save;
|
|
|
|
|
|
|
|
|
|
int stack_solve_algo, solve_algo;
|
|
|
|
|
int minimal_solving_periods;
|
|
|
|
|
int Per_u_, Per_y_;
|
|
|
|
|
int maxit_;
|
2023-11-29 19:00:21 +01:00
|
|
|
|
double* direction;
|
2023-10-19 19:56:10 +02:00
|
|
|
|
double solve_tolf;
|
|
|
|
|
// 1-norm error, square of 2-norm error, ∞-norm error
|
|
|
|
|
double res1, res2, max_res;
|
|
|
|
|
int max_res_idx;
|
2023-11-29 19:00:21 +01:00
|
|
|
|
int* index_vara;
|
2023-10-19 19:56:10 +02:00
|
|
|
|
|
|
|
|
|
double *y, *ya;
|
|
|
|
|
int y_size;
|
2023-11-29 19:00:21 +01:00
|
|
|
|
double* T;
|
2023-10-19 19:56:10 +02:00
|
|
|
|
int nb_row_x;
|
|
|
|
|
int y_kmin, y_kmax, periods;
|
|
|
|
|
double *x, *params;
|
2023-11-29 19:00:21 +01:00
|
|
|
|
double* u;
|
|
|
|
|
double* steady_y;
|
2023-10-19 19:56:10 +02:00
|
|
|
|
double *g1, *r, *res;
|
2023-11-29 19:00:21 +01:00
|
|
|
|
vector<mxArray*> jacobian_block, jacobian_exo_block, jacobian_det_exo_block;
|
|
|
|
|
mxArray* GlobalTemporaryTerms;
|
2023-10-19 19:56:10 +02:00
|
|
|
|
int it_;
|
|
|
|
|
map<int, double> TEF;
|
|
|
|
|
map<pair<int, int>, double> TEFD;
|
|
|
|
|
map<tuple<int, int, int>, double> TEFDD;
|
|
|
|
|
|
|
|
|
|
// Information about the current block
|
|
|
|
|
int block_num; // Index of the current block
|
2023-11-29 19:00:21 +01:00
|
|
|
|
int size; // Size of the current block
|
2023-10-19 19:56:10 +02:00
|
|
|
|
BlockSimulationType type;
|
|
|
|
|
bool is_linear;
|
|
|
|
|
int u_count_int;
|
|
|
|
|
vector<Block_contain_type> Block_Contain;
|
|
|
|
|
|
|
|
|
|
int verbosity; // Corresponds to options_.verbosity
|
|
|
|
|
|
2017-05-16 16:30:27 +02:00
|
|
|
|
vector<int> previous_block_exogenous;
|
2023-06-13 16:34:22 +02:00
|
|
|
|
bool print; // Whether the “print” command is requested
|
2023-02-17 19:46:41 +01:00
|
|
|
|
int col_x, col_y;
|
2023-06-12 19:12:12 +02:00
|
|
|
|
vector<double> residual;
|
2023-10-19 19:56:10 +02:00
|
|
|
|
|
2023-02-24 21:12:07 +01:00
|
|
|
|
void evaluate_over_periods(bool forward);
|
|
|
|
|
void solve_simple_one_periods();
|
|
|
|
|
void solve_simple_over_periods(bool forward);
|
2023-10-19 21:47:10 +02:00
|
|
|
|
void compute_complete_2b();
|
2023-11-29 19:00:21 +01:00
|
|
|
|
void evaluate_a_block(bool initialization, bool single_block, const string& bin_base_name);
|
|
|
|
|
int simulate_a_block(const vector_table_conditional_local_type& vector_table_conditional_local,
|
|
|
|
|
bool single_block, const string& bin_base_name);
|
2023-10-19 21:56:55 +02:00
|
|
|
|
static string elastic(string str, unsigned int len, bool left);
|
2023-11-29 19:00:21 +01:00
|
|
|
|
void check_for_controlled_exo_validity(const vector<s_plan>& sconstrained_extended_path);
|
|
|
|
|
pair<bool, vector<int>>
|
|
|
|
|
MainLoop(const string& bin_basename, bool evaluate, int block, bool constrained,
|
|
|
|
|
const vector<s_plan>& sconstrained_extended_path,
|
|
|
|
|
const vector_table_conditional_local_type& vector_table_conditional_local);
|
|
|
|
|
void Simulate_Newton_Two_Boundaries(
|
|
|
|
|
bool cvg, const vector_table_conditional_local_type& vector_table_conditional_local);
|
2023-10-19 19:56:10 +02:00
|
|
|
|
void Simulate_Newton_One_Boundary(bool forward);
|
2023-10-19 22:48:00 +02:00
|
|
|
|
void fixe_u();
|
2023-11-29 19:00:21 +01:00
|
|
|
|
void Read_SparseMatrix(const string& file_name, bool two_boundaries);
|
2023-10-19 22:55:51 +02:00
|
|
|
|
void Singular_display();
|
2023-10-19 19:56:10 +02:00
|
|
|
|
void End_Solver();
|
2023-11-29 19:00:21 +01:00
|
|
|
|
static int find_exo_num(const vector<s_plan>& sconstrained_extended_path, int value);
|
|
|
|
|
static int find_int_date(const vector<pair<int, double>>& per_value, int value);
|
2023-10-20 19:16:18 +02:00
|
|
|
|
void Init_Gaussian_Elimination();
|
2023-11-29 19:00:21 +01:00
|
|
|
|
void Init_Matlab_Sparse_Two_Boundaries(const mxArray* A_m, const mxArray* b_m,
|
|
|
|
|
const mxArray* x0_m) const;
|
|
|
|
|
tuple<SuiteSparse_long*, SuiteSparse_long*, double*, double*> Init_UMFPACK_Sparse_Two_Boundaries(
|
|
|
|
|
const mxArray* x0_m,
|
|
|
|
|
const vector_table_conditional_local_type& vector_table_conditional_local) const;
|
|
|
|
|
bool Init_Matlab_Sparse_One_Boundary(const mxArray* A_m, const mxArray* b_m,
|
|
|
|
|
const mxArray* x0_m) const;
|
|
|
|
|
tuple<bool, SuiteSparse_long*, SuiteSparse_long*, double*, double*>
|
|
|
|
|
Init_UMFPACK_Sparse_One_Boundary(const mxArray* x0_m) const;
|
2023-10-20 17:47:13 +02:00
|
|
|
|
bool Simple_Init();
|
2023-10-20 19:16:18 +02:00
|
|
|
|
void End_Gaussian_Elimination();
|
2023-11-29 19:00:21 +01:00
|
|
|
|
tuple<bool, double, double, double, double> mnbrak(double& ax, double& bx);
|
2023-10-20 16:17:08 +02:00
|
|
|
|
pair<bool, double> golden(double ax, double bx, double cx, double tol);
|
2023-10-20 16:20:45 +02:00
|
|
|
|
void Solve_ByteCode_Symbolic_Sparse_GaussianElimination(bool symbolic);
|
2023-10-20 16:25:06 +02:00
|
|
|
|
bool Solve_ByteCode_Sparse_GaussianElimination();
|
2023-11-29 19:00:21 +01:00
|
|
|
|
void Solve_LU_UMFPack_Two_Boundaries(
|
|
|
|
|
SuiteSparse_long* Ap, SuiteSparse_long* Ai, double* Ax, double* b,
|
|
|
|
|
const vector_table_conditional_local_type& vector_table_conditional_local);
|
|
|
|
|
void Solve_LU_UMFPack_One_Boundary(SuiteSparse_long* Ap, SuiteSparse_long* Ai, double* Ax,
|
|
|
|
|
double* b);
|
|
|
|
|
|
|
|
|
|
void Solve_Matlab_GMRES(mxArray* A_m, mxArray* b_m, bool is_two_boundaries, mxArray* x0_m);
|
|
|
|
|
void Solve_Matlab_BiCGStab(mxArray* A_m, mxArray* b_m, bool is_two_boundaries, mxArray* x0_m,
|
|
|
|
|
int precond);
|
2023-10-20 17:28:12 +02:00
|
|
|
|
void Check_and_Correct_Previous_Iteration();
|
|
|
|
|
bool Simulate_One_Boundary();
|
|
|
|
|
bool solve_linear(bool do_check_and_correct);
|
|
|
|
|
void solve_non_linear();
|
2023-10-19 19:56:10 +02:00
|
|
|
|
string preconditioner_print_out(string s, int preconditioner, bool ss);
|
2023-11-29 19:00:21 +01:00
|
|
|
|
bool compare(int* save_op, int* save_opa, int* save_opaa, int beg_t, long nop4);
|
2023-10-19 19:56:10 +02:00
|
|
|
|
void Insert(int r, int c, int u_index, int lag_index);
|
|
|
|
|
void Delete(int r, int c);
|
2023-11-29 19:00:21 +01:00
|
|
|
|
pair<int, NonZeroElem*> At_Row(int r) const;
|
|
|
|
|
NonZeroElem* At_Pos(int r, int c) const;
|
|
|
|
|
pair<int, NonZeroElem*> At_Col(int c) const;
|
|
|
|
|
pair<int, NonZeroElem*> At_Col(int c, int lag) const;
|
2023-10-19 19:56:10 +02:00
|
|
|
|
int NRow(int r) const;
|
|
|
|
|
int NCol(int c) const;
|
|
|
|
|
int Get_u();
|
|
|
|
|
void Delete_u(int pos);
|
|
|
|
|
void Clear_u();
|
|
|
|
|
|
2023-10-20 17:42:04 +02:00
|
|
|
|
int complete(int beg_t);
|
|
|
|
|
void bksub(int tbreak, int last_period);
|
2023-10-20 17:43:48 +02:00
|
|
|
|
void simple_bksub();
|
2023-10-19 19:56:10 +02:00
|
|
|
|
// Computes Aᵀ where A is are sparse. The result is sparse.
|
2023-11-29 19:00:21 +01:00
|
|
|
|
static mxArray* Sparse_transpose(const mxArray* A_m);
|
2023-10-19 19:56:10 +02:00
|
|
|
|
// Computes Aᵀ·B where A and B are sparse. The result is sparse.
|
2023-11-29 19:00:21 +01:00
|
|
|
|
static mxArray* Sparse_mult_SAT_SB(const mxArray* A_m, const mxArray* B_m);
|
2023-10-19 19:56:10 +02:00
|
|
|
|
// Computes Aᵀ·B where A is sparse and B is dense. The result is sparse.
|
2023-11-29 19:00:21 +01:00
|
|
|
|
static mxArray* Sparse_mult_SAT_B(const mxArray* A_m, const mxArray* B_m);
|
2023-10-19 19:56:10 +02:00
|
|
|
|
// Computes Aᵀ·B where A is sparse and B is dense. The result is dense.
|
2023-11-29 19:00:21 +01:00
|
|
|
|
static mxArray* mult_SAT_B(const mxArray* A_m, const mxArray* B_m);
|
2023-10-19 19:56:10 +02:00
|
|
|
|
// Computes A−B where A and B are sparse. The result is sparse.
|
2023-11-29 19:00:21 +01:00
|
|
|
|
static mxArray* Sparse_subtract_SA_SB(const mxArray* A_m, const mxArray* B_m);
|
2023-10-19 19:56:10 +02:00
|
|
|
|
// Computes A−B where A and B are dense. The result is dense.
|
2023-11-29 19:00:21 +01:00
|
|
|
|
static mxArray* subtract_A_B(const mxArray* A_m, const mxArray* B_m);
|
2023-10-19 19:56:10 +02:00
|
|
|
|
|
2023-10-20 17:49:04 +02:00
|
|
|
|
void compute_block_time(int my_Per_u_, bool evaluate, bool no_derivatives);
|
2023-10-19 22:31:50 +02:00
|
|
|
|
bool compute_complete(bool no_derivatives);
|
2023-10-19 19:56:10 +02:00
|
|
|
|
|
2023-10-20 15:38:03 +02:00
|
|
|
|
pair<bool, double> compute_complete(double lambda);
|
2023-10-19 19:56:10 +02:00
|
|
|
|
|
2009-12-16 18:18:38 +01:00
|
|
|
|
public:
|
2023-11-29 19:00:21 +01:00
|
|
|
|
Interpreter(Evaluate& evaluator_arg, double* params_arg, double* y_arg, double* ya_arg,
|
|
|
|
|
double* x_arg, double* steady_y_arg, double* direction_arg, int y_size_arg,
|
|
|
|
|
int nb_row_x_arg, int periods_arg, int y_kmin_arg, int y_kmax_arg, int maxit_arg_,
|
|
|
|
|
double solve_tolf_arg, double markowitz_c_arg, int minimal_solving_periods_arg,
|
|
|
|
|
int stack_solve_algo_arg, int solve_algo_arg, bool print_arg,
|
|
|
|
|
const mxArray* GlobalTemporaryTerms_arg, bool steady_state_arg,
|
|
|
|
|
bool block_decomposed_arg, int col_x_arg, int col_y_arg,
|
|
|
|
|
const BasicSymbolTable& symbol_table_arg, int verbosity_arg);
|
|
|
|
|
pair<bool, vector<int>>
|
|
|
|
|
extended_path(const string& file_name, bool evaluate, int block, int nb_periods,
|
|
|
|
|
const vector<s_plan>& sextended_path,
|
|
|
|
|
const vector<s_plan>& sconstrained_extended_path, const vector<string>& dates,
|
|
|
|
|
const table_conditional_global_type& table_conditional_global);
|
|
|
|
|
pair<bool, vector<int>> compute_blocks(const string& file_name, bool evaluate, int block);
|
2023-10-19 19:56:10 +02:00
|
|
|
|
void Close_SaveCode();
|
2013-03-22 15:44:34 +01:00
|
|
|
|
|
2023-11-29 19:00:21 +01:00
|
|
|
|
inline mxArray*
|
2021-02-03 18:10:01 +01:00
|
|
|
|
get_jacob(int block_num) const
|
2011-02-04 16:53:12 +01:00
|
|
|
|
{
|
|
|
|
|
return jacobian_block[block_num];
|
2021-02-03 18:10:01 +01:00
|
|
|
|
}
|
2023-11-29 19:00:21 +01:00
|
|
|
|
inline mxArray*
|
2021-02-03 18:10:01 +01:00
|
|
|
|
get_jacob_exo(int block_num) const
|
2011-02-04 16:53:12 +01:00
|
|
|
|
{
|
|
|
|
|
return jacobian_exo_block[block_num];
|
2021-02-03 18:10:01 +01:00
|
|
|
|
}
|
2023-11-29 19:00:21 +01:00
|
|
|
|
inline mxArray*
|
2021-02-03 18:10:01 +01:00
|
|
|
|
get_jacob_exo_det(int block_num) const
|
2011-02-04 16:53:12 +01:00
|
|
|
|
{
|
|
|
|
|
return jacobian_det_exo_block[block_num];
|
2021-02-03 18:10:01 +01:00
|
|
|
|
}
|
2011-02-04 16:53:12 +01:00
|
|
|
|
inline vector<double>
|
2021-02-03 18:10:01 +01:00
|
|
|
|
get_residual() const
|
2011-02-04 16:53:12 +01:00
|
|
|
|
{
|
|
|
|
|
return residual;
|
2021-02-03 18:10:01 +01:00
|
|
|
|
}
|
2023-11-29 19:00:21 +01:00
|
|
|
|
inline mxArray*
|
2021-02-03 18:10:01 +01:00
|
|
|
|
get_Temporary_Terms() const
|
2011-02-04 16:53:12 +01:00
|
|
|
|
{
|
|
|
|
|
return GlobalTemporaryTerms;
|
2021-02-03 18:10:01 +01:00
|
|
|
|
}
|
2007-11-21 00:27:30 +01:00
|
|
|
|
};
|
|
|
|
|
|
2022-07-29 12:40:33 +02:00
|
|
|
|
#endif // _INTERPRETER_HH
|