Skip to content

Commit

Permalink
[iss-209]
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit b05dfcb
Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com>
Date:   Thu Nov 3 09:47:09 2022 -0300

    Updated NeuralNetwork1 gt test data.

commit dabc29b
Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com>
Date:   Thu Nov 3 09:46:51 2022 -0300

    Print equation guards only for used variables.

commit dc62b9d
Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com>
Date:   Thu Nov 3 09:46:03 2022 -0300

    Added getusedVariables method to equations.

commit 20bfc1a
Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com>
Date:   Thu Nov 3 09:45:46 2022 -0300

    Added getUsedVariables method to expression.

commit 05b106c
Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com>
Date:   Thu Nov 3 09:45:22 2022 -0300

    Parse exp calls in get index variables visitor.
  • Loading branch information
joaquinffernandez committed Nov 3, 2022
1 parent 8410d84 commit e2aab0e
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 63 deletions.
20 changes: 9 additions & 11 deletions src/mmoc/ir/equation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,7 @@ Equation::Equation(AST_Expression lhs, AST_Expression rhs, Option<Range> range,
}

Equation::Equation(AST_Expression eq, Option<Range> range, EQUATION::Type type, int id, int offset)
: _eq(),
_lhs(),
_rhs(),
_range(range),
_autonomous(true),
_type(type),
_id(id),
_offset(offset),
_lhs_exp(),
_usage(),
_alg_code()
: _eq(), _lhs(), _rhs(), _range(range), _autonomous(true), _type(type), _id(id), _offset(offset), _lhs_exp(), _usage(), _alg_code()
{
initialize(eq);
}
Expand Down Expand Up @@ -242,5 +232,13 @@ void Equation::setAlgCode(std::string alg_code) { _alg_code = alg_code; }

std::string Equation::algCode() const { return _alg_code; }

std::multimap<std::string, int> Equation::usedVariables() const
{
std::multimap<std::string, int> ret = _lhs.usedVariables();
std::multimap<std::string, int> ret_rhs = _rhs.usedVariables();
ret.insert(ret_rhs.begin(), ret_rhs.end());
return ret;
}

} // namespace IR
} // namespace MicroModelica
20 changes: 12 additions & 8 deletions src/mmoc/ir/equation.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ class Equation {
inline void setRange(Option<Range> range) { _range = range; };
inline int id() const { return _id; };
inline EQUATION::Type type() const { return _type; }
inline bool isDerivative() const { return _type == EQUATION::QSSDerivative || _type == EQUATION::ClassicDerivative || _type == EQUATION::QSSBDFDerivative; }
inline bool isDerivative() const
{
return _type == EQUATION::QSSDerivative || _type == EQUATION::ClassicDerivative || _type == EQUATION::QSSBDFDerivative;
}
inline bool isZeroCrossing() const { return _type == EQUATION::ZeroCrossing; }
inline bool isOutput() const { return _type == EQUATION::Output; }
inline bool isAlgebraic() const { return _type == EQUATION::Algebraic; }
Expand All @@ -91,6 +94,7 @@ class Equation {
int arrayId() const;
void setAlgCode(std::string alg_code);
std::string algCode() const;
std::multimap<std::string, int> usedVariables() const;

protected:
void initialize(AST_Equation eq);
Expand All @@ -115,12 +119,12 @@ class Equation {

class EquationDefOrder {
public:
EquationDefOrder() : _var_name(), _var_init() {};
EquationDefOrder(std::string var_name, std::vector<int> var_init) : _var_name(var_name), _var_init(var_init) {};
EquationDefOrder() : _var_name(), _var_init(){};
EquationDefOrder(std::string var_name, std::vector<int> var_init) : _var_name(var_name), _var_init(var_init){};

bool operator<(const EquationDefOrder& other) const
bool operator<(const EquationDefOrder &other) const
{
int var_compare = _var_name.compare(other._var_name);
int var_compare = _var_name.compare(other._var_name);
if (var_compare == 0) {
assert(_var_init.size() == other._var_init.size());
size_t size = _var_init.size();
Expand All @@ -135,15 +139,15 @@ class EquationDefOrder {
}

std::string variable() const { return _var_name; }

protected:
std::string _var_name;
std::vector<int> _var_init;
};

typedef std::map<int, std::list<Equation>> OrderedEquations;
typedef std::map<int, std::list<Equation>> OrderedEquations;

typedef std::map<EquationDefOrder, Equation> EquationOrderMap;
typedef std::map<EquationDefOrder, Equation> EquationOrderMap;

typedef ModelTable<int, Equation> EquationTable;

Expand Down
9 changes: 6 additions & 3 deletions src/mmoc/ir/equation_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,13 @@ void EquationPrinter::setup(Equation eq)
}
const bool QSS_EQS = eq.type() == EQUATION::QSSDerivative || eq.type() == EQUATION::ZeroCrossing;
_return_stm = (QSS_EQS) ? FUNCTION_PRINTER::ReturnStatementType::Return : FUNCTION_PRINTER::ReturnStatementType::Continue;
_used_variables = eq.usedVariables();
}

FUNCTION_PRINTER::ReturnStatementType EquationPrinter::returnStm() const { return _return_stm; }

std::multimap<std::string, int> EquationPrinter::usedVariables() const { return _used_variables; }

string EquationPrinter::equationId() const
{
stringstream buffer;
Expand Down Expand Up @@ -228,7 +231,7 @@ string DerivativePrinter::print() const
tabs += TAB;
buffer << printer.beginExpression(identifier(), _range);
buffer << algCode();
buffer << printer.beginDimGuards(equationId(), arguments, _range);
buffer << printer.beginDimGuards(equationId(), arguments, _range, usedVariables());
buffer << tabs << prefix() << lhs() << " = " << _rhs << ";" << endl;
buffer << generateDerivatives(tabs) << endl;
buffer << endl << TAB << printer.endDimGuards(_range);
Expand Down Expand Up @@ -275,7 +278,7 @@ string OutputPrinter::print() const
}
block += TAB;
buffer << fp.beginExpression(identifier(), _range);
buffer << fp.beginDimGuards(equationId(), arguments, _range);
buffer << fp.beginDimGuards(equationId(), arguments, _range, usedVariables());
buffer << algCode();
buffer << block << prefix() << lhs() << " = " << _rhs << ";";
buffer << endl << TAB << fp.endDimGuards(_range);
Expand Down Expand Up @@ -368,7 +371,7 @@ string DependencyPrinter::print() const
}
}
tabs += TAB;
buffer << printer.beginDimGuards(equationId(), arguments, _range);
buffer << printer.beginDimGuards(equationId(), arguments, _range, usedVariables());
buffer << beginParallelMap(tabs);
buffer << tabs << prefix() << lhs(FIRST_ORDER) << " = " << _rhs << ";" << endl;
buffer << generateDerivatives(tabs, SECOND_ORDER) << endl;
Expand Down
4 changes: 3 additions & 1 deletion src/mmoc/ir/equation_printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class EquationPrinter {
std::string lhs(int order = 0) const;
virtual std::string equationId() const;
FUNCTION_PRINTER::ReturnStatementType returnStm() const;
std::multimap<std::string, int> usedVariables() const;

protected:
void setup(Equation eq);
Expand All @@ -56,6 +57,7 @@ class EquationPrinter {
Expression _lhs;
std::string _alg_code;
FUNCTION_PRINTER::ReturnStatementType _return_stm;
std::multimap<std::string, int> _used_variables;
};

EquationPrinter* getPrinter(Equation eq);
Expand Down Expand Up @@ -127,7 +129,7 @@ class DependencyPrinter : public DerivativePrinter {
std::string print() const override;

protected:
std::string beginParallelMap(std::string &tabs) const;
std::string beginParallelMap(std::string& tabs) const;
std::string endParallelMap() const;

private:
Expand Down
2 changes: 1 addition & 1 deletion src/mmoc/ir/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ string Event::handler(EVENT::Type type) const
}
block += TAB;
buffer << fp.beginExpression(_zero_crossing.identifier(), _range);
buffer << fp.beginDimGuards(_zero_crossing.identifier(), arguments, _range);
buffer << fp.beginDimGuards(_zero_crossing.identifier(), arguments, _range, _zero_crossing.usedVariables());
StatementTable::iterator it;
for (Statement stm = stms.begin(it); !stms.end(it); stm = stms.next(it)) {
buffer << block << stm << endl;
Expand Down
11 changes: 9 additions & 2 deletions src/mmoc/ir/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <util/model_config.h>
#include <util/util.h>
#include <util/visitors/expression_printer.h>
#include <util/visitors/get_index_variables.h>
#include <util/visitors/is_constant_index.h>
#include <util/visitors/partial_eval_exp.h>

Expand Down Expand Up @@ -161,14 +162,20 @@ std::ostream& operator<<(std::ostream& out, const Expression& s)
out << s.print();
return out;
}
bool Expression::operator<(const Expression &other) const
{
bool Expression::operator<(const Expression& other) const
{
std::stringstream cmp_this;
std::stringstream cmp_other;
cmp_this << *this;
cmp_other << other;
return cmp_this.str() < cmp_other.str();
}

std::multimap<std::string, int> Expression::usedVariables() const
{
GetIndexVariables used_variables;
return used_variables.apply(_exp);
}

} // namespace IR
} // namespace MicroModelica
4 changes: 3 additions & 1 deletion src/mmoc/ir/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ class Expression {
bool isScalar() const;
list<Expression> indexes() const;
static Expression generate(std::string var_name, std::vector<std::string> indices);
bool operator<(const Expression &other) const;
bool operator<(const Expression& other) const;

friend std::ostream& operator<<(std::ostream& out, const Expression& s);

std::multimap<std::string, int> usedVariables() const;

protected:
std::vector<Expression> usageExps() const;

Expand Down
46 changes: 23 additions & 23 deletions src/mmoc/ir/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@

#include <sstream>

#include <generator/macros.h>
#include "helpers.h"
#include "built_in_functions.h"
#include "expression.h"
#include "equation.h"
#include "../generator/macros.h"
#include "../util/error.h"
#include "../util/model_config.h"
#include "../util/util.h"
#include "../util/visitors/get_index_variables.h"
#include "../util/visitors/is_constant_expression.h"
#include <ir/built_in_functions.h>
#include <ir/equation.h>
#include <ir/expression.h>
#include <util/error.h>
#include <util/model_config.h>
#include <util/util.h>
#include <util/visitors/get_index_variables.h>
#include <util/visitors/is_constant_expression.h>

namespace MicroModelica {
using namespace Deps;
Expand All @@ -38,10 +38,7 @@ namespace IR {

/* ExternalFunction Class Implementation */

ExternalFunction::ExternalFunction(string lvalue, string name, AST_ExpressionList args)
: _lvalue(lvalue), _name(name), _args(args)
{
}
ExternalFunction::ExternalFunction(string lvalue, string name, AST_ExpressionList args) : _lvalue(lvalue), _name(name), _args(args) {}

std::ostream& operator<<(std::ostream& out, const ExternalFunction& e)
{
Expand Down Expand Up @@ -229,7 +226,7 @@ string FunctionPrinter::endExpression(Option<Range> range, FUNCTION_PRINTER::Ret
}
break;
}
case FUNCTION_PRINTER::Continue:
case FUNCTION_PRINTER::Continue:
buffer << "continue;" << endl;
break;
}
Expand All @@ -250,18 +247,21 @@ string FunctionPrinter::endDimGuards(Option<Range> range) const
return "";
}

string FunctionPrinter::beginDimGuards(string token, string args, Option<Range> range) const
string FunctionPrinter::beginDimGuards(string token, string args, Option<Range> range, std::multimap<std::string, int> used_variables) const
{
stringstream buffer;
if (range) {
buffer << TAB << "_apply_usage" << token << "(" << args << ");" << endl;
RangeDefinitionTable rdt = range->definition();
RangeDefinitionTable ranges = range->definition();
RangeDefinitionTable::iterator it;
int size = rdt.size(), i = 0, idx = 0;
int size = ranges.size(), idx = 1;
buffer << TAB << "if (";
for (RangeDefinition rd = rdt.begin(it); !rdt.end(it); rd = rdt.next(it), idx++) {
buffer << "(" << rdt.key(it) << " >= " << rd.begin() << " && " << rdt.key(it) << " <= " << rd.end() << ")";
buffer << (++i < size ? " && " : "");
for (RangeDefinition rd = ranges.begin(it); !ranges.end(it); rd = ranges.next(it), idx++) {
string variable = ranges.key(it);
if (used_variables.find(variable) != used_variables.end()) {
buffer << "(" << ranges.key(it) << " >= " << rd.begin() << " && " << ranges.key(it) << " <= " << rd.end() << ")";
buffer << (idx < size ? " && " : "");
}
}
buffer << ") {" << endl;
range->addLocalVariables();
Expand Down Expand Up @@ -348,7 +348,7 @@ string FunctionPrinter::printAlgebraicGuards(Equation alg, Index usage)
Index revert = usage.replace();
arguments = revert.usageExp();
}
buffer << printer.beginDimGuards(alg.applyId(), arguments, range);
buffer << printer.beginDimGuards(alg.applyId(), arguments, range, alg.usedVariables());
return buffer.str();
}

Expand Down Expand Up @@ -409,7 +409,7 @@ string FunctionPrinter::equationVariableMacros(Option<Range> range, Expression l
GetIndexVariables index_usage;
RangeDefinitionTable range_def = range->definition();
buffer << "#define _get" << id << "_var_idxs";
buffer << "(row, var)\\" << endl;
buffer << "(row, var)\\" << endl;
multimap<string, int> usage = index_usage.apply(lhs.expression());
map<string, string> parse_row = parseIndexes("row", range, 1);
map<int, string> ctes = parseConstants(lhs);
Expand All @@ -426,7 +426,7 @@ string FunctionPrinter::equationVariableMacros(Option<Range> range, Expression l
for (auto index : ctes) {
string local_range_var = range->getDimensionVar(index.first, USE_RANGE_DIM_VARS);
buffer << TAB << local_range_var << " = " << index.second << ";\\\n";
}
}
vector<string> exps;
exps.push_back(lhs.dimVariables(USE_RANGE_DIM_VARS));
Expression a_exp = Expression::generate(lhs.reference()->name(), exps);
Expand Down
21 changes: 11 additions & 10 deletions src/mmoc/ir/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
******************************************************************************/

#pragma once
#pragma once

#include <string>

#include "annotation.h"
#include "index.h"
#include "equation.h"
#include "expression.h"
#include "../util/symbol_table.h"
#include "../util/util.h"
#include <ir/annotation.h>
#include <ir/index.h>
#include <ir/equation.h>
#include <ir/expression.h>
#include <util/symbol_table.h>
#include <util/util.h>

namespace MicroModelica {
namespace IR {
Expand All @@ -37,7 +37,7 @@ namespace IR {
*/
class ExternalFunction {
public:
ExternalFunction() {};
ExternalFunction(){};
ExternalFunction(std::string lvalue, std::string name, AST_ExpressionList args);
~ExternalFunction() = default;
friend std::ostream& operator<<(std::ostream& out, const ExternalFunction& e);
Expand All @@ -53,7 +53,8 @@ typedef ModelTable<int, ExternalFunction> ExternalFunctionTable;
class CompiledFunction {
public:
CompiledFunction();
CompiledFunction(std::string name, std::string includeDir, std::string libraryDir, Util::SymbolTable& libraries, std::string prefix = "__");
CompiledFunction(std::string name, std::string includeDir, std::string libraryDir, Util::SymbolTable& libraries,
std::string prefix = "__");
~CompiledFunction() = default;
inline bool hasIncludeDirectory() const { return !_includeDirectory.empty(); };
inline bool hasLibraryDirectory() const { return !_libraryDirectory.empty(); };
Expand Down Expand Up @@ -123,7 +124,7 @@ class FunctionPrinter {
std::string getIndexes(string var, Option<Range> range, int offset, bool modelica_index) const;
std::map<std::string, std::string> parseIndexes(string var, Option<Range> range, int offset, bool modelica_index = true) const;
std::map<int, std::string> parseConstants(Expression ref) const;
std::string beginDimGuards(std::string token, string args, Option<Range> range) const;
std::string beginDimGuards(std::string token, string args, Option<Range> range, std::multimap<std::string, int> used_variables) const;
std::string endDimGuards(Option<Range> range) const;
std::string printAlgebraicGuards(Equation alg, Index usage);
/// TODO: Review modelica_index parameter usage.
Expand Down
6 changes: 3 additions & 3 deletions src/mmoc/tests/system/gt_data/NeuralNetwork1/NeuralNetwork1.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void MOD_definition(int idx, double *x, double *d, double *a, double t, double *
if (_is_var_V(idx)) {
_get_V_idxs(idx);
_apply_usage_alg_eq_1(_d1);
if ((_d2 >= 1 && _d2 <= 1) && (i >= 1 && i <= 100)) {
if ((i >= 1 && i <= 100)) {
_Is(i,0) = _Islast(i)*exp(-(_time-_tlast(i))/_taus);
_Is(i,1) = 0;
}
Expand Down Expand Up @@ -180,7 +180,7 @@ void MOD_dependencies(int idx, double *x, double *d, double *a, double t, double
if (_is_var_V(idx)) {
_get_V_idxs(idx);
_apply_usage_alg_eq_1(_d1);
if ((_d2 >= 1 && _d2 <= 1) && (i >= 1 && i <= 100)) {
if ((i >= 1 && i <= 100)) {
_Is(i,0) = _Islast(i)*exp(-(_time-_tlast(i))/_taus);
_Is(i,1) = 0;
}
Expand All @@ -204,7 +204,7 @@ void MOD_BDF_definition(double *x, double *d, double *a, double t, double *dx, i
if (_is_var_V(idx)) {
_get_V_idxs(idx);
_apply_usage_alg_eq_1(_d1);
if ((_d2 >= 1 && _d2 <= 1) && (i >= 1 && i <= 100)) {
if ((i >= 1 && i <= 100)) {
_Is(i,0) = _Islast(i)*exp(-(_time-_tlast(i))/_taus);

}
Expand Down
Loading

0 comments on commit e2aab0e

Please sign in to comment.