|
30 | 30 | #include <sbg/expression.hpp> |
31 | 31 | #include <sbg/pw_map.hpp> |
32 | 32 |
|
| 33 | +#include <algorithm> |
33 | 34 | #include <utility> |
34 | 35 |
|
35 | 36 | namespace Modelica { |
@@ -116,6 +117,10 @@ namespace { |
116 | 117 | SBG::LIB::Expression getExpr(const SBG::LIB::Set& m_domain |
117 | 118 | , const SBG::LIB::PWMap& sort) |
118 | 119 | { |
| 120 | + if (m_domain.cardinal() == 1) { |
| 121 | + return (*sort.restrict(m_domain).begin()).law(); |
| 122 | + } |
| 123 | + |
119 | 124 | SBG::LIB::PWMap m_sort = sort.restrict(m_domain); |
120 | 125 | while (m_sort.image().intersection(m_domain).isEmpty()) { |
121 | 126 | m_sort = sort.composition(m_sort); |
@@ -250,30 +255,73 @@ CausalEquations VerticalSortingResult::causalizeLoop(LoopT loop) const |
250 | 255 | { |
251 | 256 | CausalEquations result; |
252 | 257 |
|
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); |
258 | 297 | } |
| 298 | + } |
| 299 | + std::reverse(new_loop.begin(), new_loop.end()); |
259 | 300 |
|
| 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"; |
260 | 305 | 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) { |
264 | 309 | EquationInfo eq_info = _modelica_bsbg.setVertex(se.eq_id()).info().value(); |
265 | 310 | std::vector<Name> counters; |
266 | 311 | for (const Index& index : eq_info.indices()) { |
267 | 312 | counters.push_back(index.name()); |
268 | 313 | } |
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 |
270 | 316 | , se.translation(), counters, expr); |
| 317 | + |
271 | 318 | for (const Indexes& indexes : indices) { |
272 | 319 | result.pushBack(eq_info.restrictEquation(indexes), se.access()); |
273 | 320 | } |
274 | 321 | } |
275 | 322 | } |
276 | 323 | } |
| 324 | + std::cout << "\n"; |
277 | 325 |
|
278 | 326 | return result; |
279 | 327 | } |
|
0 commit comments