MicroModelicaCCompiler  4.5.3
derivative.cpp
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 #include "derivative.hpp"
21 
22 #include <ginac/function.h>
23 #include <ginac/operators.h>
24 #include <ginac/relational.h>
25 #include <ginac/symbol.h>
26 #include <ginac/wildcard.h>
27 
28 #include <ast/ast_builder.hpp>
29 #include <ast/equation.hpp>
30 #include <ast/parser/parse.hpp>
31 #include "helpers.hpp"
32 #include <ir/expression.hpp>
33 #include <util/error.hpp>
34 #include <util/model_config.hpp>
35 #include <util/ginac_interface.hpp>
36 #include <util/util.hpp>
37 #include <util/symbol_table.hpp>
38 #include <util/visitors/is_constant_expression.hpp>
39 #include <util/visitors/replace_der.hpp>
40 
41 namespace MicroModelica {
42 using namespace Util;
43 using namespace Deps;
44 namespace IR {
45 
47 
48 AST_Equation_Equality EquationDerivator::derivate(AST_Equation_Equality eq)
49 {
50  VarSymbolTable symbols = ModelConfig::instance().symbols();
51  ConvertToGiNaC to_ginac = ConvertToGiNaC(Option<Expression>());
52  ConvertToExpression to_exp;
53  GiNaC::ex left = to_ginac.convert(eq->left());
54  GiNaC::ex right = to_ginac.convert(eq->right());
55  GiNaC::symbol time = to_ginac.getTime();
56  GiNaC::ex der_left = left.diff(time).subs(var(GiNaC::wild(), time) == GiNaC::wild());
57  GiNaC::ex der_right = right.diff(time).subs(var(GiNaC::wild(), time) == GiNaC::wild());
58  return (newAST_Equation_Equality(to_exp.convert(der_left), to_exp.convert(der_right), eq->comment())->getAsEquality());
59 }
60 
61 AST_Expression ExpressionDerivator::derivate(AST_Expression exp, Expression e)
62 {
64  ConvertToGiNaC to_ginac = ConvertToGiNaC(e);
66  GiNaC::ex dexp = to_ginac.convert(exp, false, true);
67  GiNaC::symbol time = to_ginac.getTime();
68  GiNaC::ex der_exp = dexp.diff(time).subs(var(GiNaC::wild(), time) == GiNaC::wild());
69  return to_exp.convert(der_exp);
70 }
71 
73 {
74  Variable var_usage = variable.variable();
75  assert(var_usage.isState() || var_usage.isAlgebraic());
76  AST_Expression rhs_exp = eq.rhs().expression();
77  string usage = variable.modelicaExp();
79  ConvertToExpression to_exp;
80  ReplaceDer replace_der;
81  GiNaC::ex dexp = to_ginac.convert(rhs_exp, false, true);
82  map<string, GiNaC::symbol> dir = to_ginac.directory();
83  GiNaC::symbol time = to_ginac.getTime();
84  GiNaC::symbol ginac_usage = dir[usage];
85  GiNaC::ex der_exp = dexp.subs(var(GiNaC::wild(), time) == GiNaC::wild()).diff(ginac_usage);
86  AST_Expression jac_exp = replace_der.apply(to_exp.convert(der_exp));
87  return Expression(jac_exp);
88 }
89 
90 } // namespace IR
91 } // namespace MicroModelica
equation.hpp
MicroModelica::IR::ExpressionDerivator::partialDerivative
static Expression partialDerivative(Equation eq, Index variable)
Definition: derivative.cpp:89
MicroModelica::Util::ConvertToGiNaC
Definition: ginac_interface.hpp:76
MicroModelica::Util::ConvertToExpression::convert
static AST_Expression convert(GiNaC::ex)
Definition: ginac_interface.cpp:305
ast_builder.hpp
usage
void usage()
Definition: main.cpp:48
MicroModelica::Util::VarSymbolTable
Definition: symbol_table.hpp:184
MicroModelica::Util::ModelConfig::symbols
VarSymbolTable & symbols()
Definition: model_config.hpp:122
MicroModelica::Util::Variable
Definition: symbol_table.hpp:75
ginac_interface.hpp
expression.hpp
MicroModelica::IR::Expression::expression
AST_Expression expression() const
Definition: expression.hpp:87
symbol_table.hpp
helpers.hpp
Option
Definition: util_types.hpp:32
newAST_Equation_Equality
AST_Equation newAST_Equation_Equality(AST_Expression left, AST_Expression right, AST_Comment comment)
Definition: ast_builder.cpp:153
MicroModelica::Util::ConvertToGiNaC::convert
GiNaC::ex convert(AST_Expression, bool replaceDer=true, bool generateIndexes=false)
Definition: ginac_interface.cpp:104
model_config.hpp
MicroModelica::IR::ExpressionDerivator::ExpressionDerivator
ExpressionDerivator()
Definition: derivative.cpp:63
MicroModelica::IR::EquationDerivator::derivate
static AST_Equation_Equality derivate(AST_Equation_Equality eq)
Definition: derivative.cpp:65
MicroModelica::IR::Expression
Definition: expression.hpp:64
MicroModelica::IR::Equation
Definition: equation.hpp:67
MicroModelica::Util::Variable::isAlgebraic
bool isAlgebraic() const
Definition: symbol_table.hpp:119
MicroModelica::Util::ModelConfig::instance
static ModelConfig & instance()
Definition: model_config.hpp:87
MicroModelica::Util::ConvertToGiNaC::directory
map< string, GiNaC::symbol > directory()
Definition: ginac_interface.cpp:122
MicroModelica::IR::Index
Definition: index.hpp:92
derivative.hpp
MicroModelica::Generator::MODEL_INSTANCE::Component::Deps
@ Deps
MicroModelica::Util::Variable::isState
bool isState() const
Definition: symbol_table.hpp:113
MicroModelica
Definition: files.cpp:45
MicroModelica::IR::Index::modelicaExp
std::string modelicaExp() const
Definition: index.cpp:190
MicroModelica::IR::ExpressionDerivator::derivate
static AST_Expression derivate(AST_Expression exp, Expression e)
Definition: derivative.cpp:78
MicroModelica::IR::Index::variable
Util::Variable variable() const
Definition: index.cpp:160
MicroModelica::IR::Equation::rhs
Expression rhs() const
Definition: equation.hpp:80
MicroModelica::Util::ConvertToGiNaC::getTime
GiNaC::symbol & getTime()
Definition: ginac_interface.cpp:193
util.hpp
MicroModelica::Util::ConvertToExpression
Definition: ginac_interface.hpp:101
error.hpp