Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Dynare
preprocessor
Commits
ec215e2a
Commit
ec215e2a
authored
Mar 27, 2018
by
Houtan Bastani
Browse files
separate temporary terms: WIP
parent
7cbd7b6e
Changes
8
Expand all
Show whitespace changes
Inline
Side-by-side
src/DynamicModel.cc
View file @
ec215e2a
This diff is collapsed.
Click to expand it.
src/DynamicModel.hh
View file @
ec215e2a
...
...
@@ -109,6 +109,8 @@ private:
//! Writes the dynamic model equations and its derivatives
/*! \todo add third derivatives handling in C output */
void
writeDynamicModel
(
ostream
&
DynamicOutput
,
bool
use_dll
,
bool
julia
)
const
;
void
writeDynamicModel
(
const
string
&
dynamic_basename
,
bool
use_dll
,
bool
julia
)
const
;
void
writeDynamicModel
(
const
string
&
dynamic_basename
,
ostream
&
DynamicOutput
,
bool
use_dll
,
bool
julia
)
const
;
//! Writes the Block reordred structure of the model in M output
void
writeModelEquationsOrdered_M
(
const
string
&
dynamic_basename
)
const
;
//! Writes the code of the Block reordred structure of the model in virtual machine bytecode
...
...
@@ -235,6 +237,14 @@ private:
//!Maximum lead and lag for each block on endogenous of the block, endogenous of the previous blocks, exogenous and deterministic exogenous
vector
<
pair
<
int
,
int
>
>
endo_max_leadlag_block
,
other_endo_max_leadlag_block
,
exo_max_leadlag_block
,
exo_det_max_leadlag_block
,
max_leadlag_block
;
void
writeWrapperFunctions
(
const
string
&
name
,
const
string
&
ending
)
const
;
void
writeDynamicModelHelper
(
const
string
&
name
,
const
string
&
retvalname
,
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
;
public:
DynamicModel
(
SymbolTable
&
symbol_table_arg
,
NumericalConstants
&
num_constants_arg
,
ExternalFunctionsTable
&
external_functions_table_argx
);
//! Adds a variable node
...
...
src/ExprNode.cc
View file @
ec215e2a
This diff is collapsed.
Click to expand it.
src/ExprNode.hh
View file @
ec215e2a
This diff is collapsed.
Click to expand it.
src/ModelTree.cc
View file @
ec215e2a
/*
* Copyright (C) 2003-201
7
Dynare Team
* Copyright (C) 2003-201
8
Dynare Team
*
* This file is part of Dynare.
*
...
...
@@ -1120,10 +1120,25 @@ ModelTree::computeTemporaryTerms(bool is_matlab)
{
map
<
expr_t
,
pair
<
int
,
NodeTreeReference
>
>
reference_count
;
temporary_terms
.
clear
();
temporary_terms_mlv
.
clear
();
temporary_terms_res
.
clear
();
temporary_terms_g1
.
clear
();
temporary_terms_g2
.
clear
();
temporary_terms_g3
.
clear
();
// Collect all model local variables appearing in equations. See #101
// All used model local variables are automatically set as temporary variables
map
<
int
,
expr_t
>
used_local_vars
;
for
(
size_t
i
=
0
;
i
<
equations
.
size
();
i
++
)
equations
[
i
]
->
collectModelLocalVariables
(
used_local_vars
);
for
(
map
<
int
,
expr_t
>::
const_iterator
it
=
used_local_vars
.
begin
();
it
!=
used_local_vars
.
end
();
it
++
)
{
temporary_terms_mlv
[
it
->
second
]
=
local_variables_table
.
find
(
it
->
first
)
->
second
;
reference_count
[
it
->
second
]
=
make_pair
(
MIN_COST
(
is_matlab
)
+
1
,
eResiduals
);
}
map
<
NodeTreeReference
,
temporary_terms_t
>
temp_terms_map
;
temp_terms_map
[
eResiduals
]
=
temporary_terms_res
;
temp_terms_map
[
eFirstDeriv
]
=
temporary_terms_g1
;
...
...
@@ -1154,20 +1169,54 @@ ModelTree::computeTemporaryTerms(bool is_matlab)
temp_terms_map
,
is_matlab
,
eThirdDeriv
);
int
idx
=
0
;
for
(
map
<
NodeTreeReference
,
temporary_terms_t
>::
const_iterator
it
=
temp_terms_map
.
begin
();
it
!=
temp_terms_map
.
end
();
it
++
)
{
temporary_terms
.
insert
(
it
->
second
.
begin
(),
it
->
second
.
end
());
temporary_terms_idxs
.
push_back
(
idx
++
);
}
temporary_terms_res
=
temp_terms_map
[
eResiduals
];
temporary_terms_g1
=
temp_terms_map
[
eFirstDeriv
];
temporary_terms_g2
=
temp_terms_map
[
eSecondDeriv
];
temporary_terms_g3
=
temp_terms_map
[
eThirdDeriv
];
temporary_terms_mlv_idxs
.
clear
();
temporary_terms_res_idxs
.
clear
();
temporary_terms_g1_idxs
.
clear
();
temporary_terms_g2_idxs
.
clear
();
temporary_terms_g3_idxs
.
clear
();
idx
=
0
;
for
(
map
<
expr_t
,
expr_t
>::
const_iterator
it
=
temporary_terms_mlv
.
begin
();
it
!=
temporary_terms_mlv
.
end
();
it
++
)
temporary_terms_mlv_idxs
.
push_back
(
idx
++
);
for
(
temporary_terms_t
::
const_iterator
it
=
temporary_terms_res
.
begin
();
it
!=
temporary_terms_res
.
end
();
it
++
)
temporary_terms_res_idxs
.
push_back
(
idx
++
);
for
(
temporary_terms_t
::
const_iterator
it
=
temporary_terms_g1
.
begin
();
it
!=
temporary_terms_g1
.
end
();
it
++
)
temporary_terms_g1_idxs
.
push_back
(
idx
++
);
for
(
temporary_terms_t
::
const_iterator
it
=
temporary_terms_g2
.
begin
();
it
!=
temporary_terms_g2
.
end
();
it
++
)
temporary_terms_g2_idxs
.
push_back
(
idx
++
);
for
(
temporary_terms_t
::
const_iterator
it
=
temporary_terms_g3
.
begin
();
it
!=
temporary_terms_g3
.
end
();
it
++
)
temporary_terms_g3_idxs
.
push_back
(
idx
++
);
}
void
ModelTree
::
writeTemporaryTerms
(
const
temporary_terms_t
&
tt
,
const
temporary_terms_t
&
ttm1
,
ostream
&
output
,
ExprNodeOutputType
output_type
,
deriv_node_temp_terms_t
&
tef_terms
)
const
{
temporary_terms_idxs_t
tti
,
ttm1i
;
writeTemporaryTerms
(
tt
,
tti
,
ttm1
,
ttm1i
,
output
,
output_type
,
tef_terms
);
/*
// Local var used to keep track of temp nodes already written
temporary_terms_t tt2 = ttm1;
for (temporary_terms_t::const_iterator it = tt.begin();
...
...
@@ -1193,6 +1242,72 @@ ModelTree::writeTemporaryTerms(const temporary_terms_t &tt, const temporary_term
// Insert current node into tt2
tt2.insert(*it);
}
*/
}
void
ModelTree
::
writeModelLocalVariableTemporaryTerms
(
const
temporary_terms_t
&
tto
,
const
map
<
expr_t
,
expr_t
>
&
tt
,
const
temporary_terms_idxs_t
&
ttidxs
,
ostream
&
output
,
ExprNodeOutputType
output_type
,
deriv_node_temp_terms_t
&
tef_terms
)
const
{
temporary_terms_t
tt2
;
temporary_terms_idxs_t
tt2idxs
;
int
ttidx
=
0
;
for
(
map
<
expr_t
,
expr_t
>::
const_iterator
it
=
tt
.
begin
();
it
!=
tt
.
end
();
it
++
,
ttidx
++
)
{
if
(
IS_C
(
output_type
))
output
<<
"double "
;
else
if
(
IS_JULIA
(
output_type
))
output
<<
" @inbounds const "
;
it
->
first
->
writeOutput
(
output
,
output_type
,
tto
,
ttidxs
,
tef_terms
);
output
<<
" = "
;
it
->
second
->
writeOutput
(
output
,
output_type
,
tt2
,
tt2idxs
,
tef_terms
);
if
(
IS_C
(
output_type
)
||
IS_MATLAB
(
output_type
))
output
<<
";"
;
output
<<
endl
;
// Insert current node into tt2
tt2
.
insert
(
it
->
first
);
tt2idxs
.
push_back
(
ttidxs
[
ttidx
]);
}
}
void
ModelTree
::
writeTemporaryTerms
(
const
temporary_terms_t
&
tt
,
const
temporary_terms_idxs_t
&
ttidxs
,
const
temporary_terms_t
&
ttm1
,
const
temporary_terms_idxs_t
&
ttm1idxs
,
ostream
&
output
,
ExprNodeOutputType
output_type
,
deriv_node_temp_terms_t
&
tef_terms
)
const
{
// Local var used to keep track of temp nodes already written
temporary_terms_t
tt2
=
ttm1
;
temporary_terms_idxs_t
tt2idxs
=
ttm1idxs
;
int
ttidx
=
0
;
for
(
temporary_terms_t
::
const_iterator
it
=
tt
.
begin
();
it
!=
tt
.
end
();
it
++
,
ttidx
++
)
{
if
(
dynamic_cast
<
AbstractExternalFunctionNode
*>
(
*
it
)
!=
NULL
)
(
*
it
)
->
writeExternalFunctionOutput
(
output
,
output_type
,
tt2
,
tt2idxs
,
tef_terms
);
if
(
IS_C
(
output_type
))
output
<<
"double "
;
else
if
(
IS_JULIA
(
output_type
))
output
<<
" @inbounds "
;
(
*
it
)
->
writeOutput
(
output
,
output_type
,
tt
,
ttidxs
,
tef_terms
);
output
<<
" = "
;
(
*
it
)
->
writeOutput
(
output
,
output_type
,
tt2
,
tt2idxs
,
tef_terms
);
if
(
IS_C
(
output_type
)
||
IS_MATLAB
(
output_type
))
output
<<
";"
;
output
<<
endl
;
// Insert current node into tt2
tt2
.
insert
(
*
it
);
tt2idxs
.
push_back
(
ttidxs
[
ttidx
]);
}
}
void
...
...
@@ -1407,14 +1522,13 @@ ModelTree::compileTemporaryTerms(ostream &code_file, unsigned int &instruction_n
void
ModelTree
::
writeModelLocalVariables
(
ostream
&
output
,
ExprNodeOutputType
output_type
,
deriv_node_temp_terms_t
&
tef_terms
)
const
{
return
;
/* Collect all model local variables appearing in equations, and print only
them. Printing unused model local variables can lead to a crash (see
ticket #101). */
set
<
int
>
used_local_vars
;
// Use an empty set for the temporary terms
const
temporary_terms_t
tt
;
const
temporary_terms_idxs_t
tti
;
for
(
size_t
i
=
0
;
i
<
equations
.
size
();
i
++
)
equations
[
i
]
->
collectVariables
(
eModelLocalVariable
,
used_local_vars
);
...
...
@@ -1424,7 +1538,7 @@ ModelTree::writeModelLocalVariables(ostream &output, ExprNodeOutputType output_t
{
int
id
=
*
it
;
expr_t
value
=
local_variables_table
.
find
(
id
)
->
second
;
value
->
writeExternalFunctionOutput
(
output
,
output_type
,
tt
,
tef_terms
);
value
->
writeExternalFunctionOutput
(
output
,
output_type
,
tt
,
tti
,
tef_terms
);
if
(
IS_C
(
output_type
))
output
<<
"double "
;
...
...
@@ -1434,7 +1548,7 @@ ModelTree::writeModelLocalVariables(ostream &output, ExprNodeOutputType output_t
/* We append underscores to avoid name clashes with "g1" or "oo_" (see
also VariableNode::writeOutput) */
output
<<
symbol_table
.
getName
(
id
)
<<
"__ = "
;
value
->
writeOutput
(
output
,
output_type
,
tt
,
tef_terms
);
value
->
writeOutput
(
output
,
output_type
,
tt
,
tti
,
tef_terms
);
output
<<
";"
<<
endl
;
}
}
...
...
@@ -1491,12 +1605,16 @@ ModelTree::writeJsonModelLocalVariables(ostream &output, deriv_node_temp_terms_t
void
ModelTree
::
writeModelEquations
(
ostream
&
output
,
ExprNodeOutputType
output_type
)
const
{
temporary_terms_t
temp_terms
;
if
(
IS_JULIA
(
output_type
))
temp_terms
=
temporary_terms_res
;
else
temp_terms
=
temporary_terms
;
temporary_terms_t
tt
;
temporary_terms_idxs_t
ttidxs
;
writeModelEquations
(
output
,
output_type
,
tt
,
ttidxs
);
}
void
ModelTree
::
writeModelEquations
(
ostream
&
output
,
ExprNodeOutputType
output_type
,
const
temporary_terms_t
&
temporary_terms
,
const
temporary_terms_idxs_t
&
temporary_terms_idxs
)
const
{
for
(
int
eq
=
0
;
eq
<
(
int
)
equations
.
size
();
eq
++
)
{
BinaryOpNode
*
eq_node
=
equations
[
eq
];
...
...
@@ -1514,25 +1632,29 @@ ModelTree::writeModelEquations(ostream &output, ExprNodeOutputType output_type)
}
if
(
vrhs
!=
0
)
// The right hand side of the equation is not empty ==> residual=lhs-rhs;
{
if
(
IS_JULIA
(
output_type
))
output
<<
" @inbounds "
;
output
<<
"lhs ="
;
lhs
->
writeOutput
(
output
,
output_type
,
temp_terms
);
output
<<
";"
<<
endl
;
if
(
IS_JULIA
(
output_type
))
output
<<
" @inbounds "
;
output
<<
"rhs ="
;
rhs
->
writeOutput
(
output
,
output_type
,
temp_terms
);
output
<<
";"
<<
endl
;
if
(
IS_JULIA
(
output_type
))
output
<<
" @inbounds "
;
output
<<
"residual"
<<
LEFT_ARRAY_SUBSCRIPT
(
output_type
)
{
output
<<
" @inbounds residual"
<<
LEFT_ARRAY_SUBSCRIPT
(
output_type
)
<<
eq
+
ARRAY_SUBSCRIPT_OFFSET
(
output_type
)
<<
RIGHT_ARRAY_SUBSCRIPT
(
output_type
)
<<
" = ("
;
lhs
->
writeOutput
(
output
,
output_type
,
temporary_terms
,
temporary_terms_idxs
);
output
<<
") - ("
;
rhs
->
writeOutput
(
output
,
output_type
,
temporary_terms
,
temporary_terms_idxs
);
output
<<
")"
<<
endl
;
}
else
{
output
<<
"lhs = "
;
lhs
->
writeOutput
(
output
,
output_type
,
temporary_terms
,
temporary_terms_idxs
);
output
<<
";"
<<
endl
<<
"rhs = "
;
rhs
->
writeOutput
(
output
,
output_type
,
temporary_terms
,
temporary_terms_idxs
);
output
<<
";"
<<
endl
<<
"residual"
<<
LEFT_ARRAY_SUBSCRIPT
(
output_type
)
<<
eq
+
ARRAY_SUBSCRIPT_OFFSET
(
output_type
)
<<
RIGHT_ARRAY_SUBSCRIPT
(
output_type
)
<<
"= lhs
-
rhs;"
<<
endl
;
<<
"
= lhs
-
rhs;"
<<
endl
;
}
else
// The right hand side of the equation is empty ==> residual=lhs;
{
...
...
@@ -1542,7 +1664,7 @@ ModelTree::writeModelEquations(ostream &output, ExprNodeOutputType output_type)
<<
eq
+
ARRAY_SUBSCRIPT_OFFSET
(
output_type
)
<<
RIGHT_ARRAY_SUBSCRIPT
(
output_type
)
<<
" = "
;
lhs
->
writeOutput
(
output
,
output_type
,
temp
_term
s
);
lhs
->
writeOutput
(
output
,
output_type
,
temp
orary_terms
,
temporary_terms_idx
s
);
output
<<
";"
<<
endl
;
}
}
...
...
@@ -1787,9 +1909,8 @@ ModelTree::set_cutoff_to_zero()
void
ModelTree
::
jacobianHelper
(
ostream
&
output
,
int
eq_nb
,
int
col_nb
,
ExprNodeOutputType
output_type
)
const
{
output
<<
" "
;
if
(
IS_JULIA
(
output_type
))
output
<<
"@inbounds "
;
output
<<
"
@inbounds "
;
output
<<
"g1"
<<
LEFT_ARRAY_SUBSCRIPT
(
output_type
);
if
(
IS_MATLAB
(
output_type
)
||
IS_JULIA
(
output_type
))
output
<<
eq_nb
+
1
<<
","
<<
col_nb
+
1
;
...
...
@@ -1801,7 +1922,7 @@ ModelTree::jacobianHelper(ostream &output, int eq_nb, int col_nb, ExprNodeOutput
void
ModelTree
::
sparseHelper
(
int
order
,
ostream
&
output
,
int
row_nb
,
int
col_nb
,
ExprNodeOutputType
output_type
)
const
{
output
<<
"
v"
<<
order
<<
LEFT_ARRAY_SUBSCRIPT
(
output_type
);
output
<<
"v"
<<
order
<<
LEFT_ARRAY_SUBSCRIPT
(
output_type
);
if
(
IS_MATLAB
(
output_type
)
||
IS_JULIA
(
output_type
))
output
<<
row_nb
+
1
<<
","
<<
col_nb
+
1
;
else
...
...
src/ModelTree.hh
View file @
ec215e2a
/*
* Copyright (C) 2003-201
7
Dynare Team
* Copyright (C) 2003-201
8
Dynare Team
*
* This file is part of Dynare.
*
...
...
@@ -134,13 +134,21 @@ protected:
*/
third_derivatives_t
hessian_params_derivatives
;
//! Temporary terms for the static/dynamic file (those which will be noted T
xxxx
)
//! Temporary terms for the static/dynamic file (those which will be noted T
[x]
)
temporary_terms_t
temporary_terms
;
map
<
expr_t
,
expr_t
>
temporary_terms_mlv
;
temporary_terms_t
temporary_terms_res
;
temporary_terms_t
temporary_terms_g1
;
temporary_terms_t
temporary_terms_g2
;
temporary_terms_t
temporary_terms_g3
;
temporary_terms_idxs_t
temporary_terms_idxs
;
temporary_terms_idxs_t
temporary_terms_mlv_idxs
;
temporary_terms_idxs_t
temporary_terms_res_idxs
;
temporary_terms_idxs_t
temporary_terms_g1_idxs
;
temporary_terms_idxs_t
temporary_terms_g2_idxs
;
temporary_terms_idxs_t
temporary_terms_g3_idxs
;
//! Temporary terms for the file containing parameters derivatives
temporary_terms_t
params_derivs_temporary_terms
;
temporary_terms_t
params_derivs_temporary_terms_res
;
...
...
@@ -183,6 +191,9 @@ protected:
void
computeParamsDerivativesTemporaryTerms
();
//! Writes temporary terms
void
writeTemporaryTerms
(
const
temporary_terms_t
&
tt
,
const
temporary_terms_t
&
ttm1
,
ostream
&
output
,
ExprNodeOutputType
output_type
,
deriv_node_temp_terms_t
&
tef_terms
)
const
;
void
writeTemporaryTerms
(
const
temporary_terms_t
&
tt
,
const
temporary_terms_idxs_t
&
ttidxs
,
const
temporary_terms_t
&
ttm1
,
const
temporary_terms_idxs_t
&
ttm1idxs
,
ostream
&
output
,
ExprNodeOutputType
output_type
,
deriv_node_temp_terms_t
&
tef_terms
)
const
;
void
writeJsonTemporaryTerms
(
const
temporary_terms_t
&
tt
,
const
temporary_terms_t
&
ttm1
,
ostream
&
output
,
deriv_node_temp_terms_t
&
tef_terms
,
string
&
concat
)
const
;
//! Compiles temporary terms
void
compileTemporaryTerms
(
ostream
&
code_file
,
unsigned
int
&
instruction_number
,
const
temporary_terms_t
&
tt
,
map_idx_t
map_idx
,
bool
dynamic
,
bool
steady_dynamic
)
const
;
...
...
@@ -195,8 +206,15 @@ protected:
//! Writes model local variables
/*! No temporary term is used in the output, so that local parameters declarations can be safely put before temporary terms declaration in the output files */
void
writeModelLocalVariables
(
ostream
&
output
,
ExprNodeOutputType
output_type
,
deriv_node_temp_terms_t
&
tef_terms
)
const
;
void
writeModelLocalVariableTemporaryTerms
(
const
temporary_terms_t
&
tto
,
const
map
<
expr_t
,
expr_t
>
&
tt
,
const
temporary_terms_idxs_t
&
ttidxs
,
ostream
&
output
,
ExprNodeOutputType
output_type
,
deriv_node_temp_terms_t
&
tef_terms
)
const
;
//! Writes model equations
void
writeModelEquations
(
ostream
&
output
,
ExprNodeOutputType
output_type
)
const
;
void
writeModelEquations
(
ostream
&
output
,
ExprNodeOutputType
output_type
,
const
temporary_terms_t
&
temporary_terms
,
const
temporary_terms_idxs_t
&
temporary_terms_idxs
)
const
;
//! Writes JSON model equations
//! if residuals = true, we are writing the dynamic/static model.
//! Otherwise, just the model equations (with line numbers, no tmp terms)
...
...
src/StaticModel.cc
View file @
ec215e2a
This diff is collapsed.
Click to expand it.
src/StaticModel.hh
View file @
ec215e2a
/*
* Copyright (C) 2003-201
7
Dynare Team
* Copyright (C) 2003-201
8
Dynare Team
*
* This file is part of Dynare.
*
...
...
@@ -51,7 +51,7 @@ private:
void
writeStaticJuliaFile
(
const
string
&
basename
)
const
;
//! Writes the static model equations and its derivatives
void
writeStaticModel
(
ostream
&
StaticOutput
,
bool
use_dll
,
bool
julia
)
const
;
void
writeStaticModel
(
const
string
&
basename
,
ostream
&
StaticOutput
,
bool
use_dll
,
bool
julia
)
const
;
//! Writes the static function calling the block to solve (Matlab version)
void
writeStaticBlockMFSFile
(
const
string
&
basename
)
const
;
...
...
@@ -148,6 +148,15 @@ protected:
//!Maximum lead and lag for each block on endogenous of the block, endogenous of the previous blocks, exogenous and deterministic exogenous
vector
<
pair
<
int
,
int
>
>
endo_max_leadlag_block
,
other_endo_max_leadlag_block
,
exo_max_leadlag_block
,
exo_det_max_leadlag_block
,
max_leadlag_block
;
//! Helper functions for writeStaticModel
void
writeStaticModelHelper
(
const
string
&
name
,
const
string
&
retvalname
,
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
;
void
writeStaticModel
(
ostream
&
DynamicOutput
,
bool
use_dll
,
bool
julia
)
const
;
void
writeStaticModel
(
const
string
&
dynamic_basename
,
bool
use_dll
,
bool
julia
)
const
;
public:
StaticModel
(
SymbolTable
&
symbol_table_arg
,
NumericalConstants
&
num_constants
,
ExternalFunctionsTable
&
external_functions_table_arg
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment