Skip to content

Commit 15be8d7

Browse files
committed
<feature>[SBG vertical sorting]: added simple merge for border elements
1 parent 1795f9d commit 15be8d7

1 file changed

Lines changed: 57 additions & 9 deletions

File tree

causalize/sbg_implementation/vertical_sorting.cpp

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <sbg/expression.hpp>
3131
#include <sbg/pw_map.hpp>
3232

33+
#include <algorithm>
3334
#include <utility>
3435

3536
namespace Modelica {
@@ -116,6 +117,10 @@ namespace {
116117
SBG::LIB::Expression getExpr(const SBG::LIB::Set& m_domain
117118
, const SBG::LIB::PWMap& sort)
118119
{
120+
if (m_domain.cardinal() == 1) {
121+
return (*sort.restrict(m_domain).begin()).law();
122+
}
123+
119124
SBG::LIB::PWMap m_sort = sort.restrict(m_domain);
120125
while (m_sort.image().intersection(m_domain).isEmpty()) {
121126
m_sort = sort.composition(m_sort);
@@ -250,30 +255,73 @@ CausalEquations VerticalSortingResult::causalizeLoop(LoopT loop) const
250255
{
251256
CausalEquations result;
252257

253-
// Convert to ModelicaCC representation
254-
for (const SBG::LIB::Set& s : loop) {
255-
SBG::LIB::Expression expr{s.arity(), 1, 0};
256-
if (s.cardinal() > 1) {
257-
expr = getExpr(s, _sort);
258+
// Merge border elements if possible
259+
LoopT new_loop;
260+
SBG::LIB::Set sorted;
261+
for (const SBG::LIB::Set& s : LoopT{loop.rbegin(), loop.rend()}) {
262+
SBG::LIB::Set new_s = s.difference(sorted);
263+
if (!new_s.isEmpty()) {
264+
SBG::LIB::PWMap m_sort = _sort.restrict(new_s);
265+
unsigned int j = 0;
266+
if (new_s.cardinal() == 1) {
267+
m_sort = _sort.restrict(new_s);
268+
} else {
269+
while (m_sort.image().intersection(new_s).isEmpty()) {
270+
m_sort = _sort.composition(m_sort);
271+
}
272+
SBG::LIB::Set aux = new_s.intersection(m_sort.preImage(new_s));
273+
aux = aux.difference(m_sort.fixedPoints());
274+
m_sort = m_sort.restrict(aux);
275+
for (const auto& _ : m_sort) {
276+
++j;
277+
}
278+
ERROR_UNLESS(1 >= j, "getExpr: case not yet supported");
279+
}
280+
281+
if (j > 0) {
282+
SBG::LIB::Expression expr = (*(m_sort.begin())).law();
283+
for (const SBG::LIB::Set& other_s : LoopT{loop.rbegin(), loop.rend()}) {
284+
if (s != other_s) {
285+
SBG::LIB::Set image_in_other = SBG::LIB::Map{new_s, expr}.image().intersection(other_s);
286+
if (!image_in_other.isEmpty()) {
287+
new_s = std::move(new_s).disjointCup(other_s).difference(sorted);
288+
new_s.compact();
289+
break;
290+
}
291+
}
292+
}
293+
}
294+
295+
new_loop.push_back(new_s);
296+
sorted = std::move(sorted).cup(new_s);
258297
}
298+
}
299+
std::reverse(new_loop.begin(), new_loop.end());
259300

301+
// Convert to ModelicaCC representation
302+
SBG::LIB::Expression expr{sorted.arity(), 1, 0};
303+
for (const SBG::LIB::Set& s : new_loop) {
304+
std::cout << s << "\n";
260305
for (const SetEdge& se : _modelica_bsbg.set_edges()) {
261-
CompactSet se_and_m_domain = se.domain();
262-
se_and_m_domain.intersection(CompactSet{s});
263-
if (se_and_m_domain.cardinal() > 0) {
306+
CompactSet se_and_s = se.domain();
307+
se_and_s.intersection(CompactSet{s});
308+
if (se_and_s.cardinal() > 0) {
264309
EquationInfo eq_info = _modelica_bsbg.setVertex(se.eq_id()).info().value();
265310
std::vector<Name> counters;
266311
for (const Index& index : eq_info.indices()) {
267312
counters.push_back(index.name());
268313
}
269-
std::vector<Indexes> indices = toModelicaIndices(se_and_m_domain
314+
expr = getExpr(se_and_s.set(), _sort);
315+
std::vector<Indexes> indices = toModelicaIndices(se_and_s
270316
, se.translation(), counters, expr);
317+
271318
for (const Indexes& indexes : indices) {
272319
result.pushBack(eq_info.restrictEquation(indexes), se.access());
273320
}
274321
}
275322
}
276323
}
324+
std::cout << "\n";
277325

278326
return result;
279327
}

0 commit comments

Comments
 (0)