37 #include <util/visitors/convert_condition.hpp>
38 #include <util/visitors/convert_cont_red.hpp>
39 #include <util/visitors/convert_equation.hpp>
40 #include <util/visitors/convert_output_range.hpp>
41 #include <util/visitors/convert_statement.hpp>
42 #include <util/visitors/eval_init_exp.hpp>
43 #include <util/visitors/partial_eval_exp.hpp>
44 #include <util/visitors/variable_lookup.hpp>
61 _external_function_id(0),
67 Function::Function(
string name)
75 _external_function_id(0),
81 VarSymbolTable Function::symbols()
const {
return _local_symbols; }
83 void Function::insert(
string n)
86 if (!Utils::instance().readPackage(n, _packages)) {
91 Utils::instance().addCompiledFunctions(cp->definitions());
95 void Function::insert(AST_Equation eq) {
return; }
97 void Function::insert(AST_Statement stm) { _statements.insert(++_statement_id,
Statement(stm)); }
99 void Function::insert(AST_Statement stm,
bool initial) { insert(stm); }
101 void Function::insert(AST_External_Function_Call efc)
104 VariableLookup var_lookup(symbols());
105 if (efc->hasComponentReference()) {
106 AST_Expression_ComponentReference cr = efc->componentReference();
107 if (!var_lookup.apply(cr)) {
114 AST_ExpressionListIterator eli;
115 if (efc->args() !=
nullptr) {
116 foreach (eli, efc->args()) {
123 _external_functions.insert(++_external_function_id, ExternalFunction(lvalue, efc->name(), efc->args()));
131 vi.setValue(eval.apply(vi.modification()->getAsEqual()->exp()));
133 _local_symbols.insert(n, vi);
138 _arguments.push_back(vi);
144 void Function::insert(AST_Argument_Modification x)
146 if (!_annotations.insert(x)) {
151 string Function::name()
const {
return _name; }
153 ImportTable Function::imports()
const {
return _imports; }
155 StatementTable Function::statements()
const {
return _statements; }
161 unsigned int Function::outputNbr()
const {
return _output_nbr; }
169 Package::Package(
string name) : _imports(), _name(name), _functions(), _packages() {}
209 _ordered_derivatives(),
215 _library_directories(),
217 _include_directories(),
231 _external_functions(false)
241 _ordered_derivatives(),
247 _library_directories(),
249 _include_directories(),
263 _external_functions(false)
276 vi.setValue(eval.apply(vi.exp()));
290 if (set_variable_count) {
291 offset += var.
size();
299 AST_Equation_Equality eqe = eq->getAsEquality();
301 AST_Expression_Derivative ed = eqe->left()->getAsDerivative();
304 }
else if (eqe->left()->expressionType() ==
EXPCOMPREF) {
306 }
else if (eqe->left()->expressionType() ==
EXPOUTPUT) {
307 AST_Expression_Output eout = eqe->left()->getAsOutput();
308 AST_ExpressionList el = eout->expressionList();
309 AST_ExpressionListIterator it;
322 AST_Equation teq = ConvertEquation(eq).get();
326 }
else if (teq->equationType() ==
EQFOR) {
327 AST_Equation_For eqf = teq->getAsFor();
328 AST_EquationList eqs = eqf->equationList();
329 AST_EquationListIterator it;
344 void Model::insert(AST_External_Function_Call efc) {
return; }
348 AST_Statement st = ConvertStatement(stm).get();
399 addFunction(ev.zeroCrossing().calledFunctions(), fs);
405 stms = ev.negativeHandler();
437 AST_Equation_Equality eqe = eq->getAsEquality();
443 }
else if (eqe->left()->expressionType() ==
EXPCOMPREF) {
446 }
else if (eqe->left()->expressionType() ==
EXPOUTPUT) {
447 if (eqe->right()->expressionType() !=
EXPCALL) {
450 AST_Expression_Output eout = eqe->left()->getAsOutput();
451 AST_ExpressionList el = eout->expressionList();
452 AST_ExpressionListIterator it;
464 if (eq->left()->expressionType() !=
EXPOUTPUT) {
465 ReductionFunctions<AST_Equation, ConvertContRed> reduction_functions(eq->right(),
Utils::instance().
variable(eq->left()));
466 AST_Expression new_exp = reduction_functions.apply();
467 eq->setRight(new_exp);
468 list<AST_Equation> code = reduction_functions.code();
469 new_eqs.insert(new_eqs.end(), code.begin(), code.end());
470 list<Variable> variables = reduction_functions.variables();
471 for (Variable v : variables) {
484 bdf_equations.
insert(bdf_der.
id(), bdf_der);
486 return bdf_equations;
493 string var_name = eq_var->name();
507 EquationOrderMap::iterator map_it;
508 for (map_it = equation_map.begin(); map_it != equation_map.end(); map_it++) {
511 list<Equation> current_eqs = orderded_derivatives[var->offset()];
512 current_eqs.push_back(map_it->second);
513 orderded_derivatives[var->offset()] = current_eqs;
515 OrderedEquations::iterator ord_eq_it;
517 for (ord_eq_it = orderded_derivatives.begin(); ord_eq_it != orderded_derivatives.end(); ord_eq_it++) {
518 list<Equation> ordered_eqs = ord_eq_it->second;
519 for (
Equation ord_eq : ordered_eqs) {
527 list<AST_Equation> new_eqs;
528 list<AST_Equation>::iterator it;
533 }
else if (eq->equationType() ==
EQFOR) {
534 AST_Equation_For eqf = eq->getAsFor();
535 AST_EquationList eqs = eqf->equationList();
536 AST_EquationListIterator for_eq_it;
537 foreach (for_eq_it, eqs) {
548 }
else if (eq->equationType() ==
EQFOR) {
549 vector<int> begin, end;
550 AST_Equation_For eqf = eq->getAsFor();
552 AST_EquationList eqs = eqf->equationList();
553 AST_EquationListIterator for_eq_it;
554 foreach (for_eq_it, eqs) {
568 for (RangeDefinition def = ranges.begin(it); !ranges.end(it); def = ranges.next(it)) {
569 s.push_back(def.size());
576 Variable var(
newType_Integer(), eq_type,
nullptr,
nullptr, s, !s.empty());
580 static bool DONT_INCREASE_OFFSET =
false;
581 setVariableOffset(variable.get(), offset, Variable::RealType::NotAssigned, DONT_INCREASE_OFFSET);
586 int offset_shift = 0;
592 offset_shift += i * range->rowSize(i) + rd.cBegin();
600 if (stm->statementType() ==
STWHEN) {
601 int new_event =
false;
603 AST_Statement_When sw = stm->getAsWhen();
608 AST_StatementList stl = sw->statements();
609 AST_StatementListIterator it;
613 if (sw->hasElsewhen()) {
614 AST_Statement_ElseList ewl = sw->else_when();
615 AST_Statement_ElseListIterator ewit;
616 foreach (ewit, ewl) {
618 Event else_event = event;
619 if (!else_event.
compare(se->condition())) {
626 AST_StatementList stel = se->statements();
627 AST_StatementListIterator steit;
628 foreach (steit, stel) {
647 assert(event->statementType() ==
STWHEN);
648 AST_StatementList stms =
event->statements();
649 AST_StatementListIterator stm_it;
650 foreach (stm_it, stms) {
653 if (event->hasElsewhen()) {
654 AST_Statement_ElseList else_when =
event->else_when();
655 AST_Statement_ElseListIterator else_when_it;
656 foreach (else_when_it, else_when) {
658 stms = else_stm->statements();
659 foreach (stm_it, stms) {
668 list<AST_Statement>::iterator it;
670 AST_Statement stm = *it;
671 if (stm->statementType() ==
STWHEN) {
673 }
else if (stm->statementType() ==
STFOR) {
674 AST_Statement_For for_stm = stm->getAsFor();
675 AST_StatementList stms = for_stm->statements();
676 AST_StatementListIterator stm_it;
677 foreach (stm_it, stms) {
683 AST_Statement stm = *it;
684 if (stm->statementType() ==
STWHEN) {
686 }
else if (stm->statementType() ==
STFOR) {
687 AST_Statement_For stf = stm->getAsFor();
689 AST_StatementList sts = stf->statements();
690 AST_StatementListIterator stit;
691 foreach (stit, sts) {
701 list<AST_Expression>::iterator it;
702 for (it = ast_outputs.begin(); it != ast_outputs.end(); it++) {
703 PartialEvalExp partial_eval;
704 AST_Expression reduced_out_exp = partial_eval.apply(*it);
705 ConvertOutputRange convert;
706 AST_Expression converted = convert.apply(reduced_out_exp);
728 if (!eq.autonomous()) {