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 _SPARSEMATRIX_HH
|
|
|
|
|
#define _SPARSEMATRIX_HH
|
2007-11-21 00:27:30 +01:00
|
|
|
|
|
2021-02-09 15:55:36 +01:00
|
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
2007-11-21 00:27:30 +01:00
|
|
|
|
#include <map>
|
2021-02-03 18:10:01 +01:00
|
|
|
|
#include <tuple>
|
2021-02-09 15:55:36 +01:00
|
|
|
|
#include <stack>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2021-02-01 13:57:52 +01:00
|
|
|
|
#include "dynumfpack.h"
|
2021-02-09 15:55:36 +01:00
|
|
|
|
#include "dynmex.h"
|
2010-10-18 17:28:21 +02:00
|
|
|
|
|
2007-11-21 00:27:30 +01:00
|
|
|
|
#include "Mem_Mngr.hh"
|
2013-03-22 15:44:34 +01:00
|
|
|
|
#include "Evaluate.hh"
|
|
|
|
|
|
2007-11-21 00:27:30 +01:00
|
|
|
|
using namespace std;
|
2009-10-30 17:29:16 +01:00
|
|
|
|
|
2008-06-28 13:20:45 +02:00
|
|
|
|
struct t_save_op_s
|
2007-11-21 00:27:30 +01:00
|
|
|
|
{
|
|
|
|
|
short int lag, operat;
|
2009-08-25 11:43:01 +02:00
|
|
|
|
int first, second;
|
2007-11-21 00:27:30 +01:00
|
|
|
|
};
|
|
|
|
|
|
2022-07-29 12:58:10 +02:00
|
|
|
|
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>;
|
|
|
|
|
|
2021-02-03 18:10:01 +01:00
|
|
|
|
constexpr int IFLD = 0, IFDIV = 1, IFLESS = 2, IFSUB = 3, IFLDZ = 4, IFMUL = 5, IFSTP = 6, IFADD = 7;
|
|
|
|
|
constexpr double eps = 1e-15, very_big = 1e24;
|
|
|
|
|
constexpr int alt_symbolic_count_max = 1;
|
|
|
|
|
constexpr double mem_increasing_factor = 1.1;
|
2007-11-21 00:27:30 +01:00
|
|
|
|
|
2023-04-18 17:53:27 +02:00
|
|
|
|
class dynSparseMatrix
|
2009-12-16 18:18:38 +01:00
|
|
|
|
{
|
|
|
|
|
public:
|
2023-06-13 16:34:22 +02:00
|
|
|
|
dynSparseMatrix(Evaluate &evaluator_arg, int y_size_arg, int y_kmin_arg, int y_kmax_arg, bool steady_state_arg, bool block_decomposed_arg, int periods_arg, int minimal_solving_periods_arg, const BasicSymbolTable &symbol_table_arg, int verbosity_arg);
|
2022-07-28 16:56:40 +02:00
|
|
|
|
void Simulate_Newton_Two_Boundaries(int blck, int y_size, int y_kmin, int y_kmax, int Size, int periods, bool cvg, int minimal_solving_periods, int stack_solve_algo, const vector_table_conditional_local_type &vector_table_conditional_local);
|
2013-03-22 15:44:34 +01:00
|
|
|
|
void Simulate_Newton_One_Boundary(bool forward);
|
2009-12-16 18:18:38 +01:00
|
|
|
|
void fixe_u(double **u, int u_count_int, int max_lag_plus_max_lead_plus_1);
|
2021-02-03 18:10:01 +01:00
|
|
|
|
void Read_SparseMatrix(const string &file_name, int Size, int periods, int y_kmin, int y_kmax, bool two_boundaries, int stack_solve_algo, int solve_algo);
|
2015-09-22 12:45:27 +02:00
|
|
|
|
void Close_SaveCode();
|
2013-03-22 15:44:34 +01:00
|
|
|
|
void Singular_display(int block, int Size);
|
|
|
|
|
void End_Solver();
|
|
|
|
|
double g0, gp0, glambda2;
|
|
|
|
|
int try_at_iteration;
|
2021-02-03 18:10:01 +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);
|
2010-07-23 11:20:24 +02:00
|
|
|
|
|
2009-12-16 18:18:38 +01:00
|
|
|
|
private:
|
2021-02-03 18:10:01 +01:00
|
|
|
|
void Init_GE(int periods, int y_kmin, int y_kmax, int Size, const map<tuple<int, int, int>, int> &IM);
|
|
|
|
|
void Init_Matlab_Sparse(int periods, int y_kmin, int y_kmax, int Size, const map<tuple<int, int, int>, int> &IM, mxArray *A_m, mxArray *b_m, const mxArray *x0_m) const;
|
|
|
|
|
void Init_UMFPACK_Sparse(int periods, int y_kmin, int y_kmax, int Size, const map<tuple<int, int, int>, int> &IM, SuiteSparse_long **Ap, SuiteSparse_long **Ai, double **Ax, double **b, const mxArray *x0_m, const vector_table_conditional_local_type &vector_table_conditional_local, int block_num) const;
|
|
|
|
|
void Init_Matlab_Sparse_Simple(int Size, const map<tuple<int, int, int>, int> &IM, const mxArray *A_m, const mxArray *b_m, bool &zero_solution, const mxArray *x0_m) const;
|
|
|
|
|
void Init_UMFPACK_Sparse_Simple(int Size, const map<tuple<int, int, int>, int> &IM, SuiteSparse_long **Ap, SuiteSparse_long **Ai, double **Ax, double **b, bool &zero_solution, const mxArray *x0_m) const;
|
|
|
|
|
void Simple_Init(int Size, const map<tuple<int, int, int>, int> &IM, bool &zero_solution);
|
2022-07-05 12:12:08 +02:00
|
|
|
|
void End_GE();
|
2013-03-22 15:44:34 +01:00
|
|
|
|
bool mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc);
|
|
|
|
|
bool golden(double ax, double bx, double cx, double tol, double solve_tolf, double *xmin);
|
2010-09-24 12:52:58 +02:00
|
|
|
|
void Solve_ByteCode_Symbolic_Sparse_GaussianElimination(int Size, bool symbolic, int Block_number);
|
2013-03-22 15:44:34 +01:00
|
|
|
|
bool Solve_ByteCode_Sparse_GaussianElimination(int Size, int blck, int it_);
|
2022-07-05 12:12:08 +02:00
|
|
|
|
void Solve_Matlab_Relaxation(mxArray *A_m, mxArray *b_m, unsigned int Size, double slowc_l);
|
2021-02-03 18:10:01 +01:00
|
|
|
|
static void Print_UMFPack(const SuiteSparse_long *Ap, const SuiteSparse_long *Ai, const double *Ax, int n);
|
|
|
|
|
static void Printfull_UMFPack(const SuiteSparse_long *Ap, const SuiteSparse_long *Ai, const double *Ax, const double *b, int n);
|
|
|
|
|
static void PrintM(int n, const double *Ax, const mwIndex *Ap, const mwIndex *Ai);
|
|
|
|
|
void Solve_LU_UMFPack(SuiteSparse_long *Ap, SuiteSparse_long *Ai, double *Ax, double *b, int n, int Size, double slowc_l, bool is_two_boundaries, int it_, const vector_table_conditional_local_type &vector_table_conditional_local);
|
2019-12-20 14:50:19 +01:00
|
|
|
|
void Solve_LU_UMFPack(SuiteSparse_long *Ap, SuiteSparse_long *Ai, double *Ax, double *b, int n, int Size, double slowc_l, bool is_two_boundaries, int it_);
|
2015-09-22 12:45:27 +02:00
|
|
|
|
|
2013-03-22 15:44:34 +01:00
|
|
|
|
void End_Matlab_LU_UMFPack();
|
|
|
|
|
void Solve_Matlab_GMRES(mxArray *A_m, mxArray *b_m, int Size, double slowc, int block, bool is_two_boundaries, int it_, mxArray *x0_m);
|
|
|
|
|
void Solve_Matlab_BiCGStab(mxArray *A_m, mxArray *b_m, int Size, double slowc, int block, bool is_two_boundaries, int it_, mxArray *x0_m, int precond);
|
2022-07-05 12:12:08 +02:00
|
|
|
|
void Check_and_Correct_Previous_Iteration(int y_size, int size);
|
|
|
|
|
bool Simulate_One_Boundary(int blck, int y_size, int size);
|
|
|
|
|
bool solve_linear(int block_num, int y_size, int size, int iter);
|
|
|
|
|
void solve_non_linear(int block_num, int y_size, int size);
|
2013-06-08 17:16:20 +02:00
|
|
|
|
string preconditioner_print_out(string s, int preconditioner, bool ss);
|
2021-02-03 18:10:01 +01:00
|
|
|
|
bool compare(int *save_op, int *save_opa, int *save_opaa, int beg_t, int periods, long nop4, int Size);
|
|
|
|
|
void Insert(int r, int c, int u_index, int lag_index);
|
|
|
|
|
void Delete(int r, int c);
|
|
|
|
|
int At_Row(int r, NonZeroElem **first) const;
|
|
|
|
|
int At_Pos(int r, int c, NonZeroElem **first) const;
|
|
|
|
|
int At_Col(int c, NonZeroElem **first) const;
|
|
|
|
|
int At_Col(int c, int lag, NonZeroElem **first) const;
|
|
|
|
|
int NRow(int r) const;
|
|
|
|
|
int NCol(int c) const;
|
|
|
|
|
int Union_Row(int row1, int row2) const;
|
2009-12-16 18:18:38 +01:00
|
|
|
|
int Get_u();
|
|
|
|
|
void Delete_u(int pos);
|
|
|
|
|
void Clear_u();
|
2023-03-31 18:36:42 +02:00
|
|
|
|
void *Symbolic {nullptr}, *Numeric {nullptr};
|
2009-12-16 18:18:38 +01:00
|
|
|
|
int complete(int beg_t, int Size, int periods, int *b);
|
2021-02-03 18:10:01 +01:00
|
|
|
|
void bksub(int tbreak, int last_period, int Size, double slowc_l);
|
2012-02-17 10:59:39 +01:00
|
|
|
|
void simple_bksub(int it_, int Size, double slowc_l);
|
2022-02-28 12:24:37 +01:00
|
|
|
|
// Computes Aᵀ where A is are sparse. The result is sparse.
|
2021-02-03 18:10:01 +01:00
|
|
|
|
static mxArray *Sparse_transpose(const mxArray *A_m);
|
2022-02-28 12:24:37 +01:00
|
|
|
|
// Computes Aᵀ·B where A and B are sparse. The result is sparse.
|
2021-02-03 18:10:01 +01:00
|
|
|
|
static mxArray *Sparse_mult_SAT_SB(const mxArray *A_m, const mxArray *B_m);
|
2022-02-28 12:24:37 +01:00
|
|
|
|
// Computes Aᵀ·B where A is sparse and B is dense. The result is sparse.
|
2021-02-03 18:10:01 +01:00
|
|
|
|
static mxArray *Sparse_mult_SAT_B(const mxArray *A_m, const mxArray *B_m);
|
2022-02-28 12:24:37 +01:00
|
|
|
|
// Computes Aᵀ·B where A is sparse and B is dense. The result is dense.
|
2021-02-03 18:10:01 +01:00
|
|
|
|
static mxArray *mult_SAT_B(const mxArray *A_m, const mxArray *B_m);
|
2022-02-28 12:24:37 +01:00
|
|
|
|
// Computes A−B where A and B are sparse. The result is sparse.
|
2022-02-28 12:25:11 +01:00
|
|
|
|
static mxArray *Sparse_subtract_SA_SB(const mxArray *A_m, const mxArray *B_m);
|
2022-02-28 12:24:37 +01:00
|
|
|
|
// Computes A−B where A and B are dense. The result is dense.
|
2022-02-28 12:25:11 +01:00
|
|
|
|
static mxArray *subtract_A_B(const mxArray *A_m, const mxArray *B_m);
|
2013-03-22 15:44:34 +01:00
|
|
|
|
protected:
|
2023-04-18 15:59:03 +02:00
|
|
|
|
const BasicSymbolTable &symbol_table;
|
|
|
|
|
const bool steady_state; // Whether this is a static or dynamic model
|
|
|
|
|
|
2023-01-09 15:19:36 +01:00
|
|
|
|
// Whether to use the block-decomposed version of the bytecode file
|
|
|
|
|
bool block_decomposed;
|
|
|
|
|
|
2023-04-18 17:53:27 +02:00
|
|
|
|
Evaluate &evaluator;
|
|
|
|
|
|
2009-12-16 18:18:38 +01:00
|
|
|
|
stack<double> Stack;
|
|
|
|
|
int nb_prologue_table_u, nb_first_table_u, nb_middle_table_u, nb_last_table_u;
|
|
|
|
|
int nb_prologue_table_y, nb_first_table_y, nb_middle_table_y, nb_last_table_y;
|
|
|
|
|
int middle_count_loop;
|
|
|
|
|
fstream SaveCode;
|
|
|
|
|
string filename;
|
|
|
|
|
int max_u, min_u;
|
|
|
|
|
|
|
|
|
|
Mem_Mngr mem_mngr;
|
|
|
|
|
vector<int> u_liste;
|
|
|
|
|
map<pair<int, int>, NonZeroElem *> Mapped_Array;
|
|
|
|
|
int *NbNZRow, *NbNZCol;
|
|
|
|
|
NonZeroElem **FNZE_R, **FNZE_C;
|
2010-09-17 11:33:45 +02:00
|
|
|
|
int u_count_init;
|
2009-12-16 18:18:38 +01:00
|
|
|
|
|
|
|
|
|
int *pivot, *pivotk, *pivot_save;
|
|
|
|
|
double *pivotv, *pivotva;
|
|
|
|
|
int *b;
|
|
|
|
|
bool *line_done;
|
|
|
|
|
bool symbolic, alt_symbolic;
|
|
|
|
|
int alt_symbolic_count;
|
|
|
|
|
int *g_save_op;
|
|
|
|
|
int first_count_loop;
|
|
|
|
|
int g_nop_all;
|
|
|
|
|
double markowitz_c_s;
|
|
|
|
|
double res1a;
|
|
|
|
|
long int nop_all, nop1, nop2;
|
2021-02-03 18:10:01 +01:00
|
|
|
|
map<tuple<int, int, int>, int> IM_i;
|
2010-01-22 11:03:29 +01:00
|
|
|
|
int u_count_alloc, u_count_alloc_save;
|
2011-02-04 16:53:12 +01:00
|
|
|
|
vector<double *> jac;
|
2010-07-23 11:20:24 +02:00
|
|
|
|
double *jcb;
|
2022-03-31 17:39:43 +02:00
|
|
|
|
double slowc, slowc_save, prev_slowc_save, markowitz_c;
|
2010-11-20 15:45:15 +01:00
|
|
|
|
int y_decal;
|
2013-03-22 15:44:34 +01:00
|
|
|
|
int *index_equa;
|
2009-12-16 18:18:38 +01:00
|
|
|
|
int u_count, tbreak_g;
|
|
|
|
|
int iter;
|
|
|
|
|
int start_compare;
|
|
|
|
|
int restart;
|
2010-02-05 12:05:21 +01:00
|
|
|
|
double g_lambda1, g_lambda2, gp_0;
|
2010-07-23 11:20:24 +02:00
|
|
|
|
double lu_inc_tol;
|
2021-02-03 18:10:01 +01:00
|
|
|
|
|
2013-03-22 15:44:34 +01:00
|
|
|
|
SuiteSparse_long *Ap_save, *Ai_save;
|
|
|
|
|
double *Ax_save, *b_save;
|
|
|
|
|
mxArray *A_m_save, *b_m_save;
|
2023-02-17 19:46:41 +01:00
|
|
|
|
|
|
|
|
|
int stack_solve_algo, solve_algo;
|
|
|
|
|
int minimal_solving_periods;
|
2023-02-24 21:12:07 +01:00
|
|
|
|
int Per_u_, Per_y_;
|
|
|
|
|
int maxit_;
|
|
|
|
|
double *direction;
|
|
|
|
|
double solve_tolf;
|
2023-06-12 15:11:15 +02:00
|
|
|
|
// 1-norm error, square of 2-norm error, ∞-norm error
|
2023-04-06 18:44:22 +02:00
|
|
|
|
double res1, res2, max_res;
|
2023-02-24 21:12:07 +01:00
|
|
|
|
int max_res_idx;
|
|
|
|
|
int *index_vara;
|
|
|
|
|
|
2023-04-18 15:49:56 +02:00
|
|
|
|
double *y, *ya;
|
|
|
|
|
int y_size;
|
|
|
|
|
double *T;
|
|
|
|
|
int nb_row_x;
|
|
|
|
|
int y_kmin, y_kmax, periods;
|
|
|
|
|
double *x, *params;
|
|
|
|
|
double *u;
|
|
|
|
|
double *steady_y;
|
|
|
|
|
double *g1, *r, *res;
|
|
|
|
|
vector<mxArray *> jacobian_block, jacobian_exo_block, jacobian_det_exo_block;
|
|
|
|
|
mxArray *GlobalTemporaryTerms;
|
|
|
|
|
int it_;
|
|
|
|
|
map<int, double> TEF;
|
|
|
|
|
map<pair<int, int>, double> TEFD;
|
|
|
|
|
map<tuple<int, int, int>, double> TEFDD;
|
|
|
|
|
|
2023-04-18 16:55:12 +02:00
|
|
|
|
// Information about the current block
|
|
|
|
|
int block_num; // Index of the current block
|
|
|
|
|
int size; // Size of the current block
|
|
|
|
|
BlockSimulationType type;
|
|
|
|
|
bool is_linear;
|
|
|
|
|
int u_count_int;
|
|
|
|
|
vector<Block_contain_type> Block_Contain;
|
|
|
|
|
|
2023-06-13 16:34:22 +02:00
|
|
|
|
int verbosity; // Corresponds to options_.verbosity
|
|
|
|
|
|
2023-04-06 18:44:22 +02:00
|
|
|
|
void compute_block_time(int Per_u_, bool evaluate, bool no_derivatives);
|
2023-02-24 21:12:07 +01:00
|
|
|
|
bool compute_complete(bool no_derivatives, double &res1, double &res2, double &max_res, int &max_res_idx);
|
|
|
|
|
|
|
|
|
|
bool compute_complete(double lambda, double *crit);
|
2009-12-16 18:18:38 +01:00
|
|
|
|
};
|
2007-11-21 00:27:30 +01:00
|
|
|
|
|
2022-07-29 12:40:33 +02:00
|
|
|
|
#endif // _SPARSEMATRIX_HH
|