User Guide
nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser Struct Reference

Sorts and maps statements to variables keeping track of what needs updating. More...

Detailed Description

Sorts and maps statements to variables keeping track of what needs updating.

This is a multi-purpose object that:

  • keeps track of what was already updated
  • decides what statements need updating in case there was a variable assignment (i.e. \( a = 3 \))
  • builds the statements from a vector of strings

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...
 

Constructor & Destructor Documentation

◆ StatementDispenser() [1/2]

nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser::StatementDispenser ( )
default

Empty ctor.

◆ StatementDispenser() [2/2]

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.

Member Function Documentation

◆ build_maps()

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:

tmp0 = x + a
tmp1 = tmp0 + b
tmp2 = y

dependency_map should be (the order of the equation is unimportant since we are building a map):

  • tmp0 : x, a
  • tmp1 : x, a, b
  • tmp2 : y

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):

  • x : 0, 1
  • y : 2
  • a : 0, 1
  • b : 1

Definition at line 311 of file sympy_replace_solutions_visitor.cpp.

◆ emplace_back_all_tagged_statements()

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.

◆ emplace_back_next_tagged_statements()

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.

◆ is_var_assigned_here()

bool nmodl::visitor::SympyReplaceSolutionsVisitor::StatementDispenser::is_var_assigned_here ( const std::string &  var) const
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.

◆ tag_all_statements()

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.

◆ tag_dependant_statements()

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.

◆ try_emplace_back_tagged_statement()

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.

Member Data Documentation

◆ dependency_map

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:

tmp = b
x = a + tmp + exp(a)

dependency_map is:

  • tmp : b
  • x : a, b

Definition at line 380 of file sympy_replace_solutions_visitor.hpp.

◆ error_on_n_flushes

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.

◆ n_flushes

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:

x' = a
x = a + 1
y' = b

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.

◆ statements

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.

◆ tags

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.

◆ var2dependants

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:

tmp = b // statement 0
x = a + tmp + exp(a) // statement 1

var2dependants is:

  • a : 1
  • b : 0, 1

Definition at line 403 of file sympy_replace_solutions_visitor.hpp.

◆ var2statement

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:

tmp = b // statement 0
x = a + tmp + exp(a) // statement 1

var2dependants is:

  • tmp : 0
  • x : 1

Definition at line 423 of file sympy_replace_solutions_visitor.hpp.


The documentation for this struct was generated from the following files: