Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fbergero committed Aug 6, 2015
0 parents commit 3da2291
Show file tree
Hide file tree
Showing 186 changed files with 21,382 additions and 0 deletions.
674 changes: 674 additions & 0 deletions COPYING

Large diffs are not rendered by default.

1,772 changes: 1,772 additions & 0 deletions Doxyfile

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
CXXFLAGS = -I. -Wall -Wno-reorder -O3

OBJS_COMMON := ast/expression.o \
ast/class.o \
ast/equation.o \
ast/statement.o \
ast/modification.o \
ast/element.o \
ast/expression.o \
parser/parser.o

all:lib/libmodelica.a bin/antialias bin/flatter bin/mmo bin/causalize

include antialias/Makefile.include
include mmo/Makefile.include
include flatter/Makefile.include
include causalize/Makefile.include

lib/libmodelica.a: $(OBJS_COMMON)
$(AR) rcs lib/libmodelica.a $(OBJS_COMMON)

parser/parser.cpp: parser/parser_imp.h parser/actions.h parser/adapters.h

%.o: %.cpp %.h
$(CXX) -c $(CXXFLAGS) $*.cpp -o $*.o

clean:
$(RM) $(OBJS_COMMON) $(OBJS_ANTIALIAS) $(OBJS_FLATTER) $(OBJS_MMO) $(OBJS_CAUSALIZE) lib/libmodelica.a
28 changes: 28 additions & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
CXXFLAGS = -I. -Wall -Wno-reorder -O3

OBJS_COMMON := ast/expression.o \
ast/class.o \
ast/equation.o \
ast/statement.o \
ast/modification.o \
ast/element.o \
ast/expression.o \
parser/parser.o

all:lib/libmodelica.a bin/antialias bin/flatter bin/mmo bin/causalize

include antialias/Makefile.include
include mmo/Makefile.include
include flatter/Makefile.include
include causalize/Makefile.include

lib/libmodelica.a: $(OBJS_COMMON)
$(AR) rcs lib/libmodelica.a $(OBJS_COMMON)

parser/parser.cpp: parser/parser_imp.h parser/actions.h parser/adapters.h

%.o: %.cpp %.h
$(CXX) -c $(CXXFLAGS) $*.cpp -o $*.o

clean:
$(RM) $(OBJS_COMMON) $(OBJS_ANTIALIAS) $(OBJS_FLATTER) $(OBJS_MMO) $(OBJS_CAUSALIZE) lib/libmodelica.a
6 changes: 6 additions & 0 deletions antialias/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
all:
make -C ../




19 changes: 19 additions & 0 deletions antialias/Makefile.include
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
OBJS_ANTIALIAS := antialias/main.o \
mmo/mmo_class.o \
util/table.o \
util/type.o \
util/debug.o \
util/ast_visitors/evalexp.o \
util/ast_visitors/part_evalexp.o \
util/ast_visitors/replace.o \
util/ast_visitors/replace_eq.o \
util/ast_visitors/replace_st.o \
causalize/state_variables_finder.o \
antialias/remove_alias.o
LIBS=-L./lib -lginac -lmodelica
bin/antialias: $(OBJS_ANTIALIAS) lib/libmodelica.a
$(CXX) $(CXXFLAGS) $(OBJS_ANTIALIAS) -o bin/antialias $(LIBS)




44 changes: 44 additions & 0 deletions antialias/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*****************************************************************************
This file is part of Modelica C Compiler.
Modelica C Compiler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Modelica C Compiler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Modelica C Compiler. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include <iostream>
#include <ast/class.h>
#include <parser/parser.h>
#include <boost/variant/get.hpp>
#include <antialias/remove_alias.h>


int main(int argc, char ** argv)
{
using namespace std;
using namespace Modelica::AST;
using namespace Modelica;
bool ret;
StoredDef sd = parseFile("",ret);
if (!ret)
return -1;
Class ast_c = boost::get<Class>(sd.classes().front());
MMO_Class mmo(ast_c);
//std::cerr << "Number of equations is " << mmo.equations_ref().equations_ref().size() << "\n";
RemoveAlias ra(mmo);
ra.removeAliasEquations();
//std::cerr << "Number of equations is " << mmo.equations_ref().equations_ref().size() << "\n";
cout << mmo << "\n";
return 0;
}
214 changes: 214 additions & 0 deletions antialias/remove_alias.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/*****************************************************************************
This file is part of Modelica C Compiler.
Modelica C Compiler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Modelica C Compiler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Modelica C Compiler. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include <antialias/remove_alias.h>
#include <ast/equation.h>
#include <ast/queries.h>
#include <boost/variant/get.hpp>
#include <util/ast_visitors/part_evalexp.h>
#include <util/ast_visitors/replace_eq.h>
#include <util/ast_visitors/replace_st.h>
#include <causalize/state_variables_finder.h>
#include <algorithm>
#include <vector>


namespace Modelica {
RemoveAlias::RemoveAlias(MMO_Class &c): _c(c) {
StateVariablesFinder svf(c);
svf.findStateVariables();
}
void RemoveAlias::removeAliasEquations() {
EquationList &el = _c.equations_ref().equations_ref();
VarSymbolTable &syms = _c.syms_ref();
int aliased;
do {
aliased=0;
foreach_(Equation &e, el) {
if (is<Equality>(e)) {
Equality &eq = boost::get<Equality>(e);
PartEvalExp eval(syms, true);
Expression left= boost::apply_visitor(eval,eq.left_ref());
Expression right= boost::apply_visitor(eval,eq.right_ref());
eq.left_ref()=left;
eq.right_ref()=right;
if (is<UnaryOp>(left)) { // -a = ... ---> a = - ...
UnaryOp u = get<UnaryOp>(left);
if (u.op()==Minus) {
Expression exp = u.exp();
if (is<Reference>(exp)) {
left = exp;
right = UnaryOp(right,Minus);
right= boost::apply_visitor(eval,right);
}
}
}
if (is<Real>(left) || is<Integer>(left)) { // Always put contant values on the right
Expression t=right;
right=left;
left=t;
}
if (isZero(right) && is<BinOp>(left)) {
BinOp bop = get<BinOp>(left);
if (bop.op()==Add) {
right = UnaryOp(bop.right(),Minus);
right= boost::apply_visitor(eval,right);
left = bop.left();
}
if (bop.op()==Sub) {
right = bop.right();
left = bop.left();
}
}
if (is<UnaryOp>(left) && get<UnaryOp>(left).op()==Minus && is<Reference>(get<UnaryOp>(left).exp())) {
left = get<UnaryOp>(left).exp();
right = UnaryOp(right,Minus);
right= boost::apply_visitor(eval,right);
}
if (is<Reference>(left) && is<Reference>(right)) { // a = b case
Reference l = get<Reference>(left);
Reference r = get<Reference>(right);
if (l.ref().size()>1)
ERROR("antialias must be run on a flat model");
if (r.ref().size()>1)
ERROR("antialias must be run on a flat model");
if ((get<1>(l.ref().front()).size()==0) && (get<1>(r.ref().front()).size()==0)) {
if (isVariable(refName(l),syms) && isVariable(refName(r),syms)) {
if (!isState(refName(l),syms)) {
el.erase(std::find(el.begin(),el.end(),e));
alias(l,r);
aliased=1;
break;
} else if (!isState(refName(r),syms)) {
el.erase(std::find(el.begin(),el.end(),e));
alias(r,l);
aliased=1;
break;
}
}
}
} else if (is<Reference>(left) && is<UnaryOp>(right)) { // a= -b case
Reference l = get<Reference>(left);
UnaryOp u = get<UnaryOp>(right);
if (is<Reference>(u.exp())) {
Reference r = get<Reference>(u.exp());
if (l.ref().size()>1) ERROR("antialias must be run on a flat model");
if (r.ref().size()>1) ERROR("antialias must be run on a flat model");
if ((get<1>(l.ref().front()).size()==0) && (get<1>(r.ref().front()).size()==0)) {
if (isVariable(refName(l),syms) && isVariable(refName(r),syms)) {
if (!isState(refName(l),syms)) {
el.erase(std::find(el.begin(),el.end(),e));
alias(l,right);
aliased=1;
break;
}
}
}
}
} else if (is<Reference>(left) && (is<Real>(right) || is<Integer>(right))) { // a= K case
Reference l = get<Reference>(left);
if (l.ref().size()>1)
ERROR("antialias must be run on a flat model");
if (get<1>(l.ref().front()).size()>0) continue;
el.erase(std::find(el.begin(),el.end(),e));
alias(l,right);
aliased=1;
break;
}
} else if (is<ForEq>(e)) {
ForEq feq = get<ForEq>(e);
ERROR_UNLESS(feq.elements().size()==1, "Antialias not supported on multi-equation for");
ERROR_UNLESS(is<Equality>(feq.elements().front()), "Antialias not supported on non equality equation inside for");
Equality eq = get<Equality>(feq.elements().front());
Expression left= eq.left();
Expression right= eq.right();
if (isZero(left)) {
Expression t=right;
right=left;
left=t;
}
PartEvalExp eval(syms);
OptExp o_ind = feq.range().indexes().front().exp();
if (!o_ind) continue;
if (!is<Range>(o_ind.get())) continue;
Range range = get<Range>(o_ind.get());
Expression start_exp = boost::apply_visitor(eval,range.start_ref());
Expression end_exp = boost::apply_visitor(eval,range.end_ref());
if (!is<Integer>(start_exp) || !is<Integer>(end_exp)) continue;
int start = get<Integer>(start_exp);
int end = get<Integer>(end_exp);
if (start!=1) continue;
if (is<Reference>(left) && is<Reference>(right)) { // a = b case
Reference l = get<Reference>(left);
Reference r = get<Reference>(right);
if (l.ref().size()>1)
ERROR("antialias must be run on a flat model");
if (r.ref().size()>1)
ERROR("antialias must be run on a flat model");
if (isVariable(refName(l),syms) && isVariable(refName(r),syms)) {
if (isState(refName(r),syms) || isState(refName(l),syms))
continue;
if (!isArray1(refName(r),syms) || !isArray1(refName(l),syms))
continue;
Expression size_l = arraySize(refName(l),syms);
Expression size_r = arraySize(refName(r),syms);
size_l = boost::apply_visitor(eval,size_l);
size_r = boost::apply_visitor(eval,size_r);
if (!is<Integer>(size_l) || !is<Integer>(size_r)) continue;
int sz_l = get<Integer>(size_l);
int sz_r = get<Integer>(size_r);
if (end!=sz_l || end!=sz_r) continue;
el.erase(std::find(el.begin(),el.end(),e));
std::cerr << "Aliasing " << l << " with " << r << "\n";
alias(l,r);
aliased=1;
break;
}
}
}
}
} while (aliased);

}
void RemoveAlias::alias(Reference a, Expression b) { // Remove a from the model and replace every occurence with b
VarSymbolTable &syms = _c.syms_ref();
syms.remove(refName(a));

// Remove variable a
std::vector<Name> &vars = _c.variables_ref();
std::vector<Name>::iterator pos = std::find(vars.begin(),vars.end(), refName(a));
if (pos!=vars.end())
vars.erase(pos);
replace_eq req(a,b);
foreach_ (Equation &eq, _c.equations_ref().equations_ref()) {
//std::cerr << eq << " is now ";
eq=boost::apply_visitor(req,eq);
//std::cerr << eq << "\n";
}
foreach_ (Equation &eq, _c.initial_eqs_ref().equations_ref())
eq=boost::apply_visitor(req,eq);
replace_st rst(a,b);
foreach_ (Statement &st, _c.statements_ref().statements_ref())
st=boost::apply_visitor(rst,st);
foreach_ (Statement &st, _c.initial_sts_ref().statements_ref())
st=boost::apply_visitor(rst,st);

}
};

34 changes: 34 additions & 0 deletions antialias/remove_alias.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*****************************************************************************
This file is part of Modelica C Compiler.
Modelica C Compiler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Modelica C Compiler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Modelica C Compiler. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#ifndef REMOVE_ALIAS_H
#define REMOVE_ALIAS_H
#include <mmo/mmo_class.h>

namespace Modelica {
class RemoveAlias {
MMO_Class &_c;
void alias(Reference a, Expression b);
public:
RemoveAlias(MMO_Class &c);
void removeAliasEquations();
};
}

#endif
2 changes: 2 additions & 0 deletions ast/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
g++ main.cpp -g -I.. expression.cpp equation.cpp class.cpp element.cpp modification.cpp statement.cpp ../util/ast_visitors.cpp
Loading

0 comments on commit 3da2291

Please sign in to comment.