MicroModelicaCCompiler  4.5.3
jacobian.hpp
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  This file is part of QSS Solver.
4 
5  QSS Solver is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  QSS Solver is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QSS Solver. If not, see <http://www.gnu.org/licenses/>.
17 
18  ******************************************************************************/
19 
20 #pragma once
21 
22 #include <string>
23 #include <map>
24 
25 #include <ir/equation.hpp>
26 #include <deps/sbg_graph/deps_graph.hpp>
27 #include <util/symbol_table.hpp>
28 #include <util/table.hpp>
29 
30 namespace MicroModelica {
31 namespace IR {
32 
33 struct JacDef {
34  std::string code;
35 };
36 
37 class JacGenerator {
38  public:
39  JacGenerator();
40  ~JacGenerator() = default;
41 
42  void init(SB::Deps::SetVertex vertex);
43  void end();
44  void postProcess(SB::Deps::SetVertex vertex);
45  void visitF(SB::Deps::SetVertex vertex, SB::Deps::VariableDep var_dep);
46  void visitF(SB::Deps::SetVertex vertex, SB::Deps::VariableDep var_dep, SB::Deps::SetVertex gen_vertex);
47  void visitG(SB::Deps::SetVertex v_vertex, SB::Deps::SetVertex g_vertex, SB::Deps::VariableDep var_dep, int index_shift);
48  void visitG(SB::Deps::SetVertex v_vertex, SB::Deps::SetVertex g_vertex, SB::PWLMap use_map, SB::Deps::LMapExp use_map_exp,
49  Expression use_exp, SB::PWLMap def_map, SB::Deps::LMapExp def_map_exp, SB::Set intersection);
50  void initG(SB::Deps::SetVertex vertex, SB::Deps::SetEdge edge);
51  JacDef def();
52  void setup(EquationTable algebraics) { _algebraics = algebraics; };
53  EquationTable config() { return _algebraics; }
54 
55  protected:
56  IR::Expression generateExp(string var_name, vector<string> indices);
57  void dependencyPrologue(Equation eq, SB::Deps::VariableDep var_dep, std::string guard = "");
58  void dependencyEpilogue(Equation eq, SB::Deps::VariableDep var_dep);
59  void updateMatrix(std::map<std::string, std::set<std::string>>& matrix);
60  void generatePos(int id, EQUATION::Type type, std::string row = "c_row", std::string col = "col");
61  void generateEquation(int id, EQUATION::Type type);
62  void generateEquation(int v_id, int g_id, EQUATION::Type type);
63  std::string getVariableIndexes(Equation eq);
64  std::string guard(SB::Set dom, int offset, std::string var_name, SB::Deps::LMapExp map, Equation v_eq);
65  void Fvisitor(SB::Deps::SetVertex vertex, SB::Deps::VariableDep var_dep, int eq_id);
66  JacDef _jac_def;
67  int _tabs;
69 };
70 
71 class Jacobian {
72  public:
73  Jacobian();
74  ~Jacobian() = default;
75 
76  void build();
77 
78  std::string code();
79 
80  protected:
82 };
83 
84 } // namespace IR
85 } // namespace MicroModelica
ModelTable< int, Equation >
MicroModelica::IR::Jacobian
Definition: jacobian.hpp:105
MicroModelica::IR::JacGenerator::generateEquation
void generateEquation(int id, EQUATION::Type type)
Definition: jacobian.cpp:173
MicroModelica::IR::JacGenerator::init
void init(SB::Deps::SetVertex vertex)
Definition: jacobian.cpp:76
MicroModelica::IR::JacGenerator::JacGenerator
JacGenerator()
Definition: jacobian.cpp:58
MicroModelica::IR::EquationTable
ModelTable< int, Equation > EquationTable
Definition: equation.hpp:169
symbol_table.hpp
MicroModelica::IR::JacGenerator::end
void end()
Definition: jacobian.cpp:88
MicroModelica::IR::Jacobian::build
void build()
Definition: jacobian.cpp:296
MicroModelica::IR::Jacobian::_jac_def
JacDef _jac_def
Definition: jacobian.hpp:115
MicroModelica::IR::EQUATION::Type
Type
Definition: equation.hpp:50
MicroModelica::IR::JacDef::code
std::string code
Definition: jacobian.hpp:85
MicroModelica::IR::JacGenerator::guard
std::string guard(SB::Set dom, int offset, std::string var_name, SB::Deps::LMapExp map, Equation v_eq)
Definition: jacobian.cpp:94
MicroModelica::IR::JacGenerator::Fvisitor
void Fvisitor(SB::Deps::SetVertex vertex, SB::Deps::VariableDep var_dep, int eq_id)
Definition: jacobian.cpp:216
MicroModelica::IR::JacGenerator::generateExp
IR::Expression generateExp(string var_name, vector< string > indices)
MicroModelica::IR::JacGenerator::dependencyPrologue
void dependencyPrologue(Equation eq, SB::Deps::VariableDep var_dep, std::string guard="")
Definition: jacobian.cpp:110
MicroModelica::IR::Jacobian::~Jacobian
~Jacobian()=default
MicroModelica::IR::Jacobian::Jacobian
Jacobian()
Definition: jacobian.cpp:294
MicroModelica::IR::JacGenerator::visitF
void visitF(SB::Deps::SetVertex vertex, SB::Deps::VariableDep var_dep)
Definition: jacobian.cpp:204
MicroModelica::IR::JacGenerator::_jac_def
JacDef _jac_def
Definition: jacobian.hpp:100
equation.hpp
MicroModelica::IR::JacGenerator::~JacGenerator
~JacGenerator()=default
MicroModelica::IR::Jacobian::code
std::string code()
Definition: jacobian.cpp:310
MicroModelica::IR::JacGenerator::dependencyEpilogue
void dependencyEpilogue(Equation eq, SB::Deps::VariableDep var_dep)
Definition: jacobian.cpp:146
MicroModelica
Definition: files.cpp:45
MicroModelica::IR::JacGenerator::getVariableIndexes
std::string getVariableIndexes(Equation eq)
Definition: jacobian.cpp:192
MicroModelica::IR::JacGenerator::visitG
void visitG(SB::Deps::SetVertex v_vertex, SB::Deps::SetVertex g_vertex, SB::Deps::VariableDep var_dep, int index_shift)
Definition: jacobian.cpp:231
MicroModelica::IR::JacDef
Definition: jacobian.hpp:67
MicroModelica::IR::JacGenerator::config
EquationTable config()
Definition: jacobian.hpp:87
MicroModelica::IR::JacGenerator::generatePos
void generatePos(int id, EQUATION::Type type, std::string row="c_row", std::string col="col")
Definition: jacobian.cpp:159
table.hpp
MicroModelica::IR::JacGenerator::def
JacDef def()
Definition: jacobian.cpp:292
MicroModelica::IR::JacGenerator::initG
void initG(SB::Deps::SetVertex vertex, SB::Deps::SetEdge edge)
Definition: jacobian.cpp:282
MicroModelica::IR::JacGenerator::setup
void setup(EquationTable algebraics)
Definition: jacobian.hpp:86
MicroModelica::IR::JacGenerator::_algebraics
EquationTable _algebraics
Definition: jacobian.hpp:102
MicroModelica::IR::JacGenerator::_tabs
int _tabs
Definition: jacobian.hpp:101
MicroModelica::IR::JacGenerator::postProcess
void postProcess(SB::Deps::SetVertex vertex)
Definition: jacobian.cpp:60
MicroModelica::IR::JacGenerator::updateMatrix
void updateMatrix(std::map< std::string, std::set< std::string >> &matrix)