User Guide
diffeq_context.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #pragma once
9 
10 #include <string>
11 #include <utility>
12 
13 namespace nmodl {
14 namespace parser {
15 namespace diffeq {
16 
17 /**
18  * \class Term
19  * \brief Represent a term in differential equation and it's "current" solution
20  *
21  * When differential equation is parsed, each variable/term is represented
22  * by this class. As expressions are formed, like a+b, the solution gets
23  * updated
24  */
25 
26 struct Term {
27  /// expression being solved
28  std::string expr;
29 
30  /// derivative of the expression
31  std::string deriv = "0.0";
32 
33  /// \todo Need to check in neuron implementation?
34  std::string a = "0.0";
35  std::string b = "0.0";
36 
37  Term() = default;
38 
39  Term(const std::string& expr, const std::string& state);
40 
41  Term(std::string expr, std::string deriv, std::string a, std::string b)
42  : expr(std::move(expr))
43  , deriv(std::move(deriv))
44  , a(std::move(a))
45  , b(std::move(b)) {}
46 
47  /// helper routines used in parser
48 
49  bool deriv_nonzero() const {
50  return deriv != "0.0";
51  }
52 
53  bool a_nonzero() const {
54  return a != "0.0";
55  }
56 
57  bool b_nonzero() const {
58  return b != "0.0";
59  }
60 
61  void print() const;
62 };
63 
64 
65 /**
66  * \class DiffEqContext
67  * \brief Helper class used by driver and parser while solving diff equation
68  *
69  */
70 
72  /// name of the solve method
73  std::string method;
74 
75  /// name of the state variable
76  std::string state;
77 
78  /// rhs of equation
79  std::string rhs;
80 
81  /// return solution for cnexp method
82  std::string get_cnexp_solution() const;
83 
84  /// return only the derivate for euler method
85  std::string get_euler_derivate() const;
86 
87  /// return solution for non-cnexp method
88  std::string get_non_cnexp_solution() const;
89 
90  /// for non-cnexp methods : return the solution based on if equation is linear or not
91  std::string get_cvode_linear_diffeq() const;
92  std::string get_cvode_nonlinear_diffeq() const;
93 
94  /// \todo Methods inherited neuron implementation
95  std::string cvode_deriv() const;
96  std::string cvode_eqnrhs() const;
97 
98  public:
99  /// "final" solution of the equation
101 
102  /// if derivative of the equation is invalid
103  bool deriv_invalid = false;
104 
105  /// if equation itself is invalid
106  bool eqn_invalid = false;
107 
108  DiffEqContext() = default;
109 
110  DiffEqContext(std::string state, int /* order */, std::string rhs, std::string method)
111  : state(std::move(state))
112  , rhs(std::move(rhs))
113  , method(std::move(method)) {}
114 
115  /// return the state variable
116  const std::string& state_variable() const {
117  return state;
118  }
119 
120  /// return solution of the differential equation
121  std::string get_solution(bool& cnexp_possible);
122 
123  /// return expression with Dstate added
124  std::string get_expr_for_nonlinear() const;
125 
126  /// print the context (for debugging)
127  void print() const;
128 };
129 
130 } // namespace diffeq
131 } // namespace parser
132 } // namespace nmodl
nmodl::parser::diffeq::Term::deriv_nonzero
bool deriv_nonzero() const
helper routines used in parser
Definition: diffeq_context.hpp:49
nmodl::parser::diffeq::DiffEqContext::state_variable
const std::string & state_variable() const
return the state variable
Definition: diffeq_context.hpp:116
nmodl::parser::diffeq::DiffEqContext::get_euler_derivate
std::string get_euler_derivate() const
return only the derivate for euler method
Definition: diffeq_context.cpp:142
nmodl::parser::diffeq::Term::Term
Term(std::string expr, std::string deriv, std::string a, std::string b)
Definition: diffeq_context.hpp:41
nmodl::parser::diffeq::DiffEqContext::solution
Term solution
"final" solution of the equation
Definition: diffeq_context.hpp:100
nmodl::parser::diffeq::Term::expr
std::string expr
expression being solved
Definition: diffeq_context.hpp:28
nmodl::parser::diffeq::DiffEqContext::get_cnexp_solution
std::string get_cnexp_solution() const
return solution for cnexp method
Definition: diffeq_context.cpp:119
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::parser::diffeq::DiffEqContext::deriv_invalid
bool deriv_invalid
if derivative of the equation is invalid
Definition: diffeq_context.hpp:103
nmodl::parser::diffeq::DiffEqContext
Helper class used by driver and parser while solving diff equation.
Definition: diffeq_context.hpp:71
nmodl::parser::diffeq::Term::deriv
std::string deriv
derivative of the expression
Definition: diffeq_context.hpp:31
nmodl::parser::diffeq::DiffEqContext::get_cvode_linear_diffeq
std::string get_cvode_linear_diffeq() const
for non-cnexp methods : return the solution based on if equation is linear or not
Definition: diffeq_context.cpp:99
nmodl::parser::diffeq::DiffEqContext::rhs
std::string rhs
rhs of equation
Definition: diffeq_context.hpp:79
nmodl::parser::diffeq::DiffEqContext::get_expr_for_nonlinear
std::string get_expr_for_nonlinear() const
return expression with Dstate added
Definition: diffeq_context.cpp:70
nmodl::parser::diffeq::DiffEqContext::method
std::string method
name of the solve method
Definition: diffeq_context.hpp:73
nmodl::parser::diffeq::DiffEqContext::DiffEqContext
DiffEqContext()=default
nmodl::parser::diffeq::DiffEqContext::cvode_eqnrhs
std::string cvode_eqnrhs() const
Definition: diffeq_context.cpp:56
nmodl::parser::diffeq::Term
Represent a term in differential equation and it's "current" solution.
Definition: diffeq_context.hpp:26
nmodl::parser::diffeq::Term::b
std::string b
Definition: diffeq_context.hpp:35
nmodl::parser::diffeq::DiffEqContext::get_solution
std::string get_solution(bool &cnexp_possible)
return solution of the differential equation
Definition: diffeq_context.cpp:170
nmodl::parser::diffeq::Term::print
void print() const
Definition: diffeq_context.cpp:29
nmodl::parser::diffeq::DiffEqContext::cvode_deriv
std::string cvode_deriv() const
Definition: diffeq_context.cpp:47
nmodl::parser::diffeq::Term::a
std::string a
Definition: diffeq_context.hpp:34
nmodl::parser::diffeq::DiffEqContext::eqn_invalid
bool eqn_invalid
if equation itself is invalid
Definition: diffeq_context.hpp:106
nmodl::parser::diffeq::DiffEqContext::get_cvode_nonlinear_diffeq
std::string get_cvode_nonlinear_diffeq() const
Return solution for non-cnexp method and when equation is non-linear.
Definition: diffeq_context.cpp:108
nmodl::parser::diffeq::DiffEqContext::print
void print() const
print the context (for debugging)
Definition: diffeq_context.cpp:35
nmodl::parser::diffeq::DiffEqContext::state
std::string state
name of the state variable
Definition: diffeq_context.hpp:76
nmodl::parser::diffeq::DiffEqContext::DiffEqContext
DiffEqContext(std::string state, int, std::string rhs, std::string method)
Definition: diffeq_context.hpp:110
nmodl::parser::diffeq::Term::b_nonzero
bool b_nonzero() const
Definition: diffeq_context.hpp:57
nmodl::parser::diffeq::Term::a_nonzero
bool a_nonzero() const
Definition: diffeq_context.hpp:53
nmodl::parser::diffeq::Term::Term
Term()=default
nmodl::parser::diffeq::DiffEqContext::get_non_cnexp_solution
std::string get_non_cnexp_solution() const
return solution for non-cnexp method
Definition: diffeq_context.cpp:150