User Guide
|
Sorts and maps statements to variables keeping track of what needs updating. More...
Sorts and maps statements to variables keeping track of what needs updating.
This is a multi-purpose object that:
Definition at line 313 of file sympy_replace_solutions_visitor.hpp.
Public Member Functions | |
StatementDispenser ()=default | |
Empty ctor. More... | |
StatementDispenser (const std::vector< std::string >::const_iterator &statements_str_beg, const std::vector< std::string >::const_iterator &statements_str_end, const int error_on_n_flushes) | |
Standard ctor. More... | |
void | build_maps () |
Construct the maps var2dependants, var2statement and dependency_map for easy access and classification of the statements. More... | |
bool | is_var_assigned_here (const std::string &var) const |
Check if one of the statements assigns this variable (i.e. More... | |
bool | try_emplace_back_tagged_statement (ast::StatementVector &new_statements, const std::string &var) |
Look for var in var2statement and emplace back that statement in new_statements . More... | |
size_t | emplace_back_next_tagged_statements (ast::StatementVector &new_statements, const size_t n_next_statements) |
Emplace back the next n_next_statements solutions in statements that is marked for updating in tags. More... | |
size_t | emplace_back_all_tagged_statements (ast::StatementVector &new_statements) |
Emplace back all the statements that are marked for updating in tags. More... | |
size_t | tag_dependant_statements (const std::string &var) |
Tag all the statements that depend on var for updating. More... | |
void | tag_all_statements () |
Mark that all the statements need updating (probably unused) More... | |
Public Attributes | |
std::unordered_map< std::string, std::unordered_set< std::string > > | dependency_map |
x (key) : f(a, b, c, ...) (values) More... | |
std::unordered_map< std::string, std::set< size_t > > | var2dependants |
a (key) : f(..., a, ...), g(..., a, ...), h(..., a, ...), ... More... | |
std::unordered_map< std::string, size_t > | var2statement |
a (key) : a = f(...) (value) More... | |
std::vector< std::shared_ptr< ast::Statement > > | statements |
Vector of statements. More... | |
std::set< size_t > | tags |
Keeps track of what statements need updating. More... | |
size_t | n_flushes = 0 |
Max number of times a statement was printed using an emplace_back_all_tagged_statements command. More... | |
int | error_on_n_flushes |
Emit error when n_flushes reaches this number. -1 disables the error entirely. More... | |
|
default |
Empty ctor.
nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser::StatementDispenser | ( | const std::vector< std::string >::const_iterator & | statements_str_beg, |
const std::vector< std::string >::const_iterator & | statements_str_end, | ||
const int | error_on_n_flushes | ||
) |
Standard ctor.
Definition at line 267 of file sympy_replace_solutions_visitor.cpp.
void nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser::build_maps | ( | ) |
Construct the maps var2dependants, var2statement and dependency_map for easy access and classification of the statements.
CHere we construct a map variable -> affected equations. In other words this map tells me what equations need to be updated when I change a particular variable. To do that we build a a graph of dependencies var -> vars and in the mean time we reduce it to the root variables. This is ensured by the fact that the tmp variables are sorted so that the next tmp variable may depend on the previous one. Since it is a relation of equivalence (if an equation depends on a variable, it needs to be updated if the variable changes), we build the two maps at the same time.
An example:
dependency_map should be (the order of the equation is unimportant since we are building a map):
and the var2statement map should be (the order of the following equations is unimportant since we are building a map. The number represents the index of the original equations):
Definition at line 311 of file sympy_replace_solutions_visitor.cpp.
size_t nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser::emplace_back_all_tagged_statements | ( | ast::StatementVector & | new_statements | ) |
Emplace back all the statements that are marked for updating in tags.
Definition at line 410 of file sympy_replace_solutions_visitor.cpp.
size_t nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser::emplace_back_next_tagged_statements | ( | ast::StatementVector & | new_statements, |
const size_t | n_next_statements | ||
) |
Emplace back the next n_next_statements
solutions in statements that is marked for updating in tags.
Definition at line 390 of file sympy_replace_solutions_visitor.cpp.
|
inline |
Check if one of the statements assigns this variable (i.e.
\( x' = f(x, y, x) \)) and is still tagged
Definition at line 328 of file sympy_replace_solutions_visitor.hpp.
void nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser::tag_all_statements | ( | ) |
Mark that all the statements need updating (probably unused)
Definition at line 455 of file sympy_replace_solutions_visitor.cpp.
size_t nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser::tag_dependant_statements | ( | const std::string & | var | ) |
Tag all the statements that depend on var
for updating.
This is necessary when an assignment has invalidated this variable
Definition at line 438 of file sympy_replace_solutions_visitor.cpp.
bool nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser::try_emplace_back_tagged_statement | ( | ast::StatementVector & | new_statements, |
const std::string & | var | ||
) |
Look for var
in var2statement and emplace back that statement in new_statements
.
If there is no var
key in var2statement, return false
Definition at line 363 of file sympy_replace_solutions_visitor.cpp.
std::unordered_map<std::string, std::unordered_set<std::string> > nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser::dependency_map |
x (key) : f(a, b, c, ...) (values)
Given a certain variable (map key) we get all the (root) variables on which this variable depends on (values)
For example, imagine we have these assignments:
dependency_map is:
Definition at line 380 of file sympy_replace_solutions_visitor.hpp.
int nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser::error_on_n_flushes |
Emit error when n_flushes reaches this number. -1 disables the error entirely.
Definition at line 458 of file sympy_replace_solutions_visitor.hpp.
size_t nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser::n_flushes = 0 |
Max number of times a statement was printed using an emplace_back_all_tagged_statements command.
This is useful to check if, during updates, a variable was assigned.
For example:
In this sequence of statements \( x \) was assigned within variable updates. This sequence of statements could lead to instability/wrong results for derivimplicit methods. Better to prevent this entirely. It can still be assigned at the end/beginning
Definition at line 455 of file sympy_replace_solutions_visitor.hpp.
std::vector<std::shared_ptr<ast::Statement> > nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser::statements |
Vector of statements.
Definition at line 426 of file sympy_replace_solutions_visitor.hpp.
std::set<size_t> nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser::tags |
Keeps track of what statements need updating.
The elements of this set are the indexes of the statements vector that need updating. It is a set because we need to be able to easily find them by value and we need them ordered to pick "the next one"
Definition at line 435 of file sympy_replace_solutions_visitor.hpp.
std::unordered_map<std::string, std::set<size_t> > nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser::var2dependants |
a (key) : f(..., a, ...), g(..., a, ...), h(..., a, ...), ...
(values)
This the "reverse" of dependency_map. Given a certain variable it provides the statements that depend on it. It is a set because we want to print them in order and we do not want duplicates. The value is the index in statements or tags
For example:
var2dependants is:
Definition at line 403 of file sympy_replace_solutions_visitor.hpp.
std::unordered_map<std::string, size_t> nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser::var2statement |
a (key) : a = f(...) (value)
Given a certain variable we get the statement where that variable is defined
For example:
var2dependants is:
Definition at line 423 of file sympy_replace_solutions_visitor.hpp.