NEURON
matexp_visitor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2025 David McDougall
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #pragma once
9 
10 /**
11  * \file
12  * \brief \copybrief nmodl::visitor::MatexpVisitor
13  */
14 
15 #include "symtab/symbol.hpp"
16 #include "visitors/ast_visitor.hpp"
17 
18 namespace nmodl {
19 namespace visitor {
20 
21 /**
22  * \addtogroup visitor_classes
23  * \{
24  */
25 
26 /**
27  * \class MatexpVisitor
28  * \brief Visitor used for generating the necessary AST nodes for matexp solver
29  */
30 class MatexpVisitor: public AstVisitor {
31  private:
32  /// blocks to be solved
33  std::vector<ast::SolveBlock*> steadystate_blocks;
34 
35  /// blocks to be solved
36  std::vector<ast::SolveBlock*> solve_blocks;
37 
38  /// blocks to be solved by a different solver method
39  std::vector<std::string> keep_blocks;
40 
41  /// all kinetic blocks in the program
42  std::vector<ast::KineticBlock*> kinetic_blocks;
43 
44  /// search the "kinetic_blocks" vector for the given block
45  ast::KineticBlock* find_kinetic_block(const std::string& block_name);
46 
47  /// convert a KineticBlock into a MatexpBlock
48  std::shared_ptr<ast::MatexpBlock> solve_kinetic_block(const ast::KineticBlock& node,
49  bool steadystate);
50 
51  /// replace the given solve-block statement with a MatexpBlock
52  void replace_solve_block(const ast::SolveBlock& node, bool steadystate);
53 
54  /// return the MatexpBlock solution for the given solve-block statement
55  std::shared_ptr<ast::MatexpBlock> get_solve_block(const ast::SolveBlock& node,
56  bool steadystate);
57 
58  /// ordered list of state variables
59  std::vector<std::shared_ptr<symtab::Symbol>> states;
60 
61  /// returns an index into the "states" vector
62  int get_state_index(const std::string& state_name);
63 
64  /// currently visiting kinetic block that is being solved
65  bool in_jacobian_block = false;
66 
67  /// convert a decay reaction statement "->" into equivalent assignments to the Jacobian matrix
68  std::shared_ptr<ast::Statement> transform_decay_statement(std::shared_ptr<ast::Expression> lhs,
69  std::shared_ptr<ast::Expression> kf);
70 
71  /// convert a reaction statement "<->" into equivalent assignments to the Jacobian matrix
72  std::vector<std::shared_ptr<ast::Statement>> transform_reaction_statement(
73  std::shared_ptr<ast::Expression> lhs,
74  std::shared_ptr<ast::Expression> rhs,
75  std::shared_ptr<ast::Expression> kf,
76  std::shared_ptr<ast::Expression> kb);
77 
78  public:
79  MatexpVisitor() = default;
80 
81  void visit_program(ast::Program& node) override;
82  void visit_solve_block(ast::SolveBlock& node) override;
86 };
87 
88 /** \} */ // end of visitor_classes
89 
90 } // namespace visitor
91 } // namespace nmodl
Concrete visitor for all AST classes.
Represents top level AST node for whole NMODL input.
Definition: program.hpp:39
Represents block encapsulating list of statements.
Concrete visitor for all AST classes.
Definition: ast_visitor.hpp:37
Visitor used for generating the necessary AST nodes for matexp solver.
void visit_program(ast::Program &node) override
visit node of type ast::Program
std::vector< ast::SolveBlock * > solve_blocks
blocks to be solved
std::shared_ptr< ast::MatexpBlock > get_solve_block(const ast::SolveBlock &node, bool steadystate)
return the MatexpBlock solution for the given solve-block statement
std::vector< std::shared_ptr< ast::Statement > > transform_reaction_statement(std::shared_ptr< ast::Expression > lhs, std::shared_ptr< ast::Expression > rhs, std::shared_ptr< ast::Expression > kf, std::shared_ptr< ast::Expression > kb)
convert a reaction statement "<->" into equivalent assignments to the Jacobian matrix
std::vector< ast::SolveBlock * > steadystate_blocks
blocks to be solved
void visit_statement_block(ast::StatementBlock &node) override
visit node of type ast::StatementBlock
void visit_reaction_statement(ast::ReactionStatement &node) override
visit node of type ast::ReactionStatement
std::vector< std::shared_ptr< symtab::Symbol > > states
ordered list of state variables
std::shared_ptr< ast::Statement > transform_decay_statement(std::shared_ptr< ast::Expression > lhs, std::shared_ptr< ast::Expression > kf)
convert a decay reaction statement "->" into equivalent assignments to the Jacobian matrix
std::vector< ast::KineticBlock * > kinetic_blocks
all kinetic blocks in the program
std::vector< std::string > keep_blocks
blocks to be solved by a different solver method
std::shared_ptr< ast::MatexpBlock > solve_kinetic_block(const ast::KineticBlock &node, bool steadystate)
convert a KineticBlock into a MatexpBlock
ast::KineticBlock * find_kinetic_block(const std::string &block_name)
search the "kinetic_blocks" vector for the given block
void replace_solve_block(const ast::SolveBlock &node, bool steadystate)
replace the given solve-block statement with a MatexpBlock
int get_state_index(const std::string &state_name)
returns an index into the "states" vector
bool in_jacobian_block
currently visiting kinetic block that is being solved
void visit_solve_block(ast::SolveBlock &node) override
Populate the lists of solve-block statements in the program.
void visit_kinetic_block(ast::KineticBlock &node) override
Populate the list of kinetic blocks in the program.
#define rhs
Definition: lineq.h:6
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
static Node * node(Object *)
Definition: netcvode.cpp:291
Implement class to represent a symbol in Symbol Table.