![]() |
User Guide
|
Replace statements in node
with pre_solve_statements, tmp_statements, and solutions.
More...
Replace statements in node
with pre_solve_statements, tmp_statements, and solutions.
The goal is to replace statements with solutions in place. In this way we can allow (to some extent) the use of control flow blocks and assignments. pre_solve_statements are added in front of the replaced statements in case their variable needs updating. StatementDispenser keeps track of what needs updating. Let's start with some nomenclature:
We employ a multi-step approach:
Let's finish with an example (that are usually better than blabbling around).
Imagine we have this derivative block in the ast (before SympyReplaceSolutionsVisitor passes):
where SympySolverVisitor already added variables in the LOCAL declaration.
Sympy solver visitor also provides:
SympySolveVisitor works in this way:
Last notes:
For linEquations or NonLinEquations association of the solution with a particular statement could be impossible. For example \( ~ x + y = 0 \) does not have a simple variable in the lhs. Thus, an association with a particular solution statement is not possible. Thus we do 2 runs where we first match everything we can by value and then we associate everything we can in a greedy way.
For large system of equations the code sets up the J matrix and F vector to be sent to eigen for the Newton method which will solve a bunch of J x = F for each time step). In this case it is always safe to replace greedy because sympy does not sort the equations in the matrix/vector. In addition, cse is disabled by default. Thus, there is a 1:1 correspondence of an equation of the original mod file and a row of the matrix and an element of F. So if we have:
We get the vector F and matrix J
Where the numbers indicate their column-wise index. The solution replacement becomes:
Definition at line 212 of file sympy_replace_solutions_visitor.hpp.
#include <sympy_replace_solutions_visitor.hpp>
Classes | |
struct | InterleavesCounter |
Count interleaves of assignment statement inside the system of equations. More... | |
struct | StatementDispenser |
Sorts and maps statements to variables keeping track of what needs updating. More... | |
Public Types | |
enum | ReplacePolicy { ReplacePolicy::VALUE = 0, ReplacePolicy::GREEDY = 1 } |
Private Member Functions | |
void | try_replace_tagged_statement (const ast::Node &node, std::shared_ptr< ast::Expression > get_lhs(const ast::Node &node)) |
Try to replace a statement. More... | |
Private Attributes | |
StatementDispenser | pre_solve_statements |
Update state variable statements (i.e. \(old_x = x \)) More... | |
StatementDispenser | tmp_statements |
tmp statements that appear with –cse (i.e. \(tmp0 = a \)) More... | |
StatementDispenser | solution_statements |
solutions that we want to replace More... | |
std::unordered_map< std::shared_ptr< ast::Statement >, ast::StatementVector > | replacements |
Replacements found by the visitor. More... | |
bool | is_top_level_statement_block = true |
Used to notify to visit_statement_block was called by the user (or another visitor) or re-called in a nested block. More... | |
ReplacePolicy | policy |
Replacement policy used by the various visitors. More... | |
size_t | n_next_equations |
Number of solutions that match each old_statement with the greedy policy. More... | |
const std::unordered_set< ast::Statement * > * | to_be_removed |
group of old statements that need replacing More... | |
InterleavesCounter | interleaves_counter |
counts how many times the solution statements are interleaved with assignment expressions More... | |
std::pair< int, int > | replaced_statements_range = {-1, -1} |
{begin index, end index} of the added statements. -1 means that it is invalid More... | |
Enumerator | |
---|---|
VALUE | Replace statements matching by lhs varName. |
GREEDY | Replace statements greedily. |
Definition at line 214 of file sympy_replace_solutions_visitor.hpp.
|
delete |
Empty ctor.
nmodl::visitor::SympyReplaceSolutionsVisitor::SympyReplaceSolutionsVisitor | ( | const std::vector< std::string > & | pre_solve_statements, |
const std::vector< std::string > & | solutions, | ||
const std::unordered_set< ast::Statement * > & | to_be_removed, | ||
const ReplacePolicy | policy, | ||
size_t | n_next_equations, | ||
const std::string & | tmp_unique_prefix | ||
) |
Default constructor.
Definition at line 34 of file sympy_replace_solutions_visitor.cpp.
|
inline |
idx (in the new statementVector) of the first statement that was added.
-1 if nothing was added
Definition at line 231 of file sympy_replace_solutions_visitor.hpp.
|
inline |
idx (in the new statementVector) of the last statement that was added.
-1 if nothing was added
Definition at line 236 of file sympy_replace_solutions_visitor.hpp.
|
private |
Try to replace a statement.
node | it can be Diff_Eq_Expression/LinEquation/NonLinEquation |
get_lhs | method with witch we may get the lhs (in case we need it) |
Definition at line 162 of file sympy_replace_solutions_visitor.cpp.
|
overridevirtual |
visit node of type ast::BinaryExpression
Implements nmodl::visitor::Visitor.
Definition at line 240 of file sympy_replace_solutions_visitor.cpp.
|
overridevirtual |
visit node of type ast::DiffEqExpression
Implements nmodl::visitor::Visitor.
Definition at line 211 of file sympy_replace_solutions_visitor.cpp.
|
overridevirtual |
visit node of type ast::LinEquation
Implements nmodl::visitor::Visitor.
Definition at line 220 of file sympy_replace_solutions_visitor.cpp.
|
overridevirtual |
visit node of type ast::NonLinEquation
Implements nmodl::visitor::Visitor.
Definition at line 230 of file sympy_replace_solutions_visitor.cpp.
|
overridevirtual |
visit node of type ast::StatementBlock
Implements nmodl::visitor::Visitor.
Definition at line 64 of file sympy_replace_solutions_visitor.cpp.
|
private |
counts how many times the solution statements are interleaved with assignment expressions
Definition at line 491 of file sympy_replace_solutions_visitor.hpp.
|
private |
Used to notify to visit_statement_block was called by the user (or another visitor) or re-called in a nested block.
Definition at line 479 of file sympy_replace_solutions_visitor.hpp.
|
private |
Number of solutions that match each old_statement with the greedy policy.
Definition at line 485 of file sympy_replace_solutions_visitor.hpp.
|
private |
Replacement policy used by the various visitors.
Definition at line 482 of file sympy_replace_solutions_visitor.hpp.
|
private |
Update state variable statements (i.e. \(old_x = x \))
Definition at line 460 of file sympy_replace_solutions_visitor.hpp.
|
private |
{begin index, end index} of the added statements. -1 means that it is invalid
Definition at line 494 of file sympy_replace_solutions_visitor.hpp.
|
private |
Replacements found by the visitor.
The keys are the old_statements that need replacing with the new ones (the value). Since there are pre_solve_statements and tmp_statements; it is in general a replacement of 1 : n statements
Definition at line 475 of file sympy_replace_solutions_visitor.hpp.
|
private |
solutions that we want to replace
Definition at line 466 of file sympy_replace_solutions_visitor.hpp.
|
private |
tmp statements that appear with –cse (i.e. \(tmp0 = a \))
Definition at line 463 of file sympy_replace_solutions_visitor.hpp.
|
private |
group of old statements that need replacing
Definition at line 488 of file sympy_replace_solutions_visitor.hpp.