Skip to content

Commit

Permalink
Merge branch 'qss-solver-dev' into mmoc/iss-217-adapt-reinit-statemen…
Browse files Browse the repository at this point in the history
…ts-to-equation-sections
  • Loading branch information
joaquinffernandez committed Apr 25, 2023
2 parents 77820f4 + 935c8fc commit b3c5251
Show file tree
Hide file tree
Showing 42 changed files with 428 additions and 351 deletions.
91 changes: 57 additions & 34 deletions src/mmoc/deps/builders/merge_graph_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ MergeGraphGenerator<S>::MergeGraphGenerator()
}

template <typename S>
MergeGraphGenerator<S>::~MergeGraphGenerator() {}
MergeGraphGenerator<S>::~MergeGraphGenerator()
{
}

template <typename S>
void MergeGraphGenerator<S>::setup(S config)
Expand All @@ -70,7 +72,8 @@ void MergeGraphGenerator<S>::setup(S config)

EventTable::iterator ev_it;
for (Event ev = events.begin(ev_it); !events.end(ev_it); ev = events.next(ev_it)) {
list<SB::Deps::SetVertex> ifr_nodes = createSetVertex(ev, _edge_dom_offset, max_dims, SB::Deps::VERTEX::Influencer, usage, STATEMENT::LHS);
list<SB::Deps::SetVertex> ifr_nodes =
createSetVertex(ev, _edge_dom_offset, max_dims, SB::Deps::VERTEX::Influencer, usage, STATEMENT::LHS);
for (SB::Deps::SetVertex ifr_node : ifr_nodes) {
addVertex(ifr_node, _graph);
}
Expand All @@ -83,31 +86,33 @@ void MergeGraphGenerator<S>::setup(S config)
addVertex(alg_node.get(), _graph);
}
}

}

template <typename S>
S MergeGraphGenerator<S>::config() { return _config; }
S MergeGraphGenerator<S>::config()
{
return _config;
}

template <typename S>
Intersections MergeGraphGenerator<S>::computeIntersections(SB::Set variables)
{
Intersections inters;
// This implementation relies on the fact that both discrete variables vertices
// This implementation relies on the fact that both discrete variables vertices
// are added in the same order, so the domain in both cases should match.
VertexIt dsc_vertex_it = SB::Deps::findSetVertex(_lhs_dsc_graph, variables);
SB::Deps::SetVertex dsc_vertex = _lhs_dsc_graph[*dsc_vertex_it];
SB::Set intersection = dsc_vertex.range().cap(variables);
list<SB::Deps::Edge> edges = SB::Deps::inputEdges(_lhs_dsc_graph, dsc_vertex.name());
list<SB::Deps::Edge> edges = SB::Deps::inputEdges(_lhs_dsc_graph, dsc_vertex.name());

// As a second step, look for all the input edges that arrives to the discrete variable.
for (SB::Deps::Edge edge : edges) {
SB::Deps::SetEdge edge_label = _lhs_dsc_graph[edge];
SB::Deps::Vertex E = boost::source(edge, _lhs_dsc_graph);
SB::Deps::SetVertex EV = _lhs_dsc_graph[E];
SB::Deps::SetVertex EV = _lhs_dsc_graph[E];
if (EV.desc().type() == SB::Deps::VERTEX::Influencer) {
SB::Set whole_dom = edge_label.mapU().wholeDom();
SB::Set input_edge_image = edge_label.mapU().image(whole_dom);
SB::Set input_edge_image = edge_label.mapU().image(whole_dom);
SB::Set used_dscs = intersection.cap(input_edge_image);
if (!used_dscs.empty()) {
IntersectInfo inter_info;
Expand All @@ -120,16 +125,23 @@ Intersections MergeGraphGenerator<S>::computeIntersections(SB::Set variables)
}
}
return inters;
}
}

template <typename S>
void MergeGraphGenerator<S>::init(SB::Deps::SetVertex vertex) { _nodes.push_back(vertex); }
void MergeGraphGenerator<S>::init(SB::Deps::SetVertex vertex)
{
_nodes.push_back(vertex);
}

template <typename S>
void MergeGraphGenerator<S>::end() {}
void MergeGraphGenerator<S>::end()
{
}

template <typename S>
void MergeGraphGenerator<S>::postProcess(SB::Deps::SetVertex vertex) {}
void MergeGraphGenerator<S>::postProcess(SB::Deps::SetVertex vertex)
{
}

template <typename S>
SB::PWLMap MergeGraphGenerator<S>::buildMap(SB::Set dom, int convert_offset, SB::Set dom_map, int graph_offset)
Expand Down Expand Up @@ -158,9 +170,8 @@ SB::PWLMap MergeGraphGenerator<S>::buildMap(SB::Set dom, int convert_offset, SB:
addDims(max_dims, edge_dom.minElem().size(), constant_pwl_map, slope_pwl_map, 1);
return buildPWLMap(constant_pwl_map, slope_pwl_map, edge_dom);
}

template <typename S>
SB::Deps::LMapExp MergeGraphGenerator<S>::buildLMapExp(Expression exp, SB::Deps::LMapExp use_map)
SB::Deps::LMapExp MergeGraphGenerator<S>::buildLMapFromExp(IR::Expression exp)
{
SB::Deps::Constants exp_constants;
SB::Deps::Slopes exp_slopes;
Expand All @@ -169,23 +180,29 @@ SB::Deps::LMapExp MergeGraphGenerator<S>::buildLMapExp(Expression exp, SB::Deps:
for (Expression idx : indexes) {
PWLMapValues pwl_map_values;
pwl_map_values.apply(idx.expression());
exp_constants.insert(pwl_map_values.constant());
exp_constants.insert(pwl_map_values.constant());
exp_slopes.insert(pwl_map_values.slope());
exp_init_values.insert(pwl_map_values.constant());
}
return SB::Deps::LMapExp(exp_constants, exp_slopes, exp_init_values);
}

template <typename S>
SB::Deps::LMapExp MergeGraphGenerator<S>::buildLMapExp(Expression exp, IR::Expression use_exp, SB::Deps::LMapExp use_map)
{
SB::Deps::LMapExp use_exp_map = buildLMapFromExp(use_exp);
SB::Deps::LMapExp exp_map = buildLMapFromExp(exp);

// We are dealing with expressions of different dimension, it could happen in merges.
if(use_map.slopes().size() > exp_slopes.size()){
int end = use_map.slopes().size();
int init = exp_slopes.size();
for (int add = init; add < end; add++){
exp_constants.addDim();
exp_slopes.addDim();
exp_init_values.addDim();
}
if (use_exp_map.slopes().size() > exp_map.slopes().size()) {
exp_map.padDims(use_exp_map.slopes().size());
}
SB::Deps::LMapExp exp_map = SB::Deps::LMapExp(exp_constants, exp_slopes, exp_init_values);
if (!exp_map.isEmpty() && !use_map.isEmpty()) {
exp_map = exp_map.compose(use_map.revert());
if (!exp_map.isEmpty() && !use_exp_map.isEmpty()) {
SB::Deps::LMapExp apply_map = use_exp_map;
if (!use_map.isEmpty()) {
apply_map = use_map.compose(use_exp_map);
}
exp_map = exp_map.compose(apply_map.revert());
}
return exp_map;
}
Expand All @@ -206,7 +223,7 @@ SB::Deps::SetVertex MergeGraphGenerator<S>::findAlgVertex(SB::Set variables)

template <typename S>
SB::EdgeMaps MergeGraphGenerator<S>::generatePWLMaps(IntersectInfo inter_info, SB::Deps::SetVertex orig_ife_vertex,
SB::Deps::SetVertex ife_vertex, SB::Deps::VariableDep var_dep)
SB::Deps::SetVertex ife_vertex, SB::Deps::VariableDep var_dep)
{
SB::Set edge_dom = inter_info.edge.mapU().preImage(inter_info.intersection);
SB::Set ifr_nodes = inter_info.edge.mapF().image(edge_dom);
Expand Down Expand Up @@ -242,7 +259,7 @@ SB::EdgeMaps MergeGraphGenerator<S>::generatePWLMaps(IntersectInfo inter_info, S
maps.F = buildMap(ifr_nodes, _graph[*inter_info.node].id() - inter_info.orig_node.id(), dom_nodes, _graph[*inter_info.node].id());
maps.U = buildMap(ife_nodes, ife_vertex.id() - orig_ife_vertex.id(), dom_nodes, ife_vertex.id());
Expression node = _config.exp(_config.getNode(orig_ife_vertex.index()));
maps.map_exp = buildLMapExp(node, var_dep.nMap());
maps.map_exp = buildLMapExp(node, inter_info.edge.desc().exp(), var_dep.nMap());
_edge_dom_offset += dom_nodes.size();
return maps;
}
Expand All @@ -255,7 +272,7 @@ void MergeGraphGenerator<S>::addEdges(SB::Deps::SetVertex vertex, SB::Deps::Vari
VertexIt ife_vertex_it = SB::Deps::findSetVertexByName(_graph, _config.nodeName(vertex.index()));
SB::Deps::SetVertex ife_vertex = _graph[*ife_vertex_it];

for(IntersectInfo inter_info : inters) {
for (IntersectInfo inter_info : inters) {
EdgeMaps maps = generatePWLMaps(inter_info, vertex, ife_vertex, var_dep);
string edge_name = "E_" + to_string(_edge_dom_offset);
LOG << "MapF: " << maps.F << endl;
Expand All @@ -281,10 +298,13 @@ void MergeGraphGenerator<S>::visitF(SB::Deps::SetVertex vertex, SB::Deps::Variab
}

template <typename S>
void MergeGraphGenerator<S>::visitF(SB::Deps::SetVertex vertex, SB::Deps::VariableDep var_dep, SB::Deps::SetVertex gen_vertex) {}
void MergeGraphGenerator<S>::visitF(SB::Deps::SetVertex vertex, SB::Deps::VariableDep var_dep, SB::Deps::SetVertex gen_vertex)
{
}

template <typename S>
void MergeGraphGenerator<S>::visitG(SB::Deps::SetVertex v_vertex, SB::Deps::SetVertex g_vertex, SB::Deps::VariableDep var_dep, int index_shift)
void MergeGraphGenerator<S>::visitG(SB::Deps::SetVertex v_vertex, SB::Deps::SetVertex g_vertex, SB::Deps::VariableDep var_dep,
int index_shift)
{
if (v_vertex.desc().type() != SB::Deps::VERTEX::Equation) {
addEdges(v_vertex, var_dep);
Expand All @@ -293,8 +313,8 @@ void MergeGraphGenerator<S>::visitG(SB::Deps::SetVertex v_vertex, SB::Deps::SetV

template <typename S>
void MergeGraphGenerator<S>::visitG(SB::Deps::SetVertex v_vertex, SB::Deps::SetVertex g_vertex, SB::PWLMap use_map,
SB::Deps::LMapExp use_map_exp, IR::Expression use_exp, SB::PWLMap def_map, SB::Deps::LMapExp def_map_exp,
SB::Set intersection)
SB::Deps::LMapExp use_map_exp, IR::Expression use_exp, SB::PWLMap def_map,
SB::Deps::LMapExp def_map_exp, SB::Set intersection)
{
}

Expand All @@ -304,7 +324,10 @@ void MergeGraphGenerator<S>::initG(SB::Deps::SetVertex vertex, SB::Deps::SetEdge
}

template <typename S>
SB::Deps::Graph MergeGraphGenerator<S>::def() { return _graph; }
SB::Deps::Graph MergeGraphGenerator<S>::def()
{
return _graph;
}

template class MergeGraphGenerator<Deps::EQSelector>;

Expand Down
4 changes: 2 additions & 2 deletions src/mmoc/deps/builders/merge_graph_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ struct IntersectInfo {

typedef list<IntersectInfo> Intersections;


template <typename S>
class MergeGraphGenerator {
public:
Expand All @@ -65,7 +64,8 @@ class MergeGraphGenerator {
protected:
Intersections computeIntersections(SB::Set variables);
SB::PWLMap buildMap(SB::Set dom, int convert_offset, SB::Set dom_map, int graph_offset);
SB::Deps::LMapExp buildLMapExp(IR::Expression dom, SB::Deps::LMapExp use_map);
SB::Deps::LMapExp buildLMapExp(IR::Expression dom, IR::Expression use_exp, SB::Deps::LMapExp use_map);
SB::Deps::LMapExp buildLMapFromExp(IR::Expression exp);
SB::EdgeMaps generatePWLMaps(IntersectInfo inter_info, SB::Deps::SetVertex orig_ife_vertex, SB::Deps::SetVertex ife_vertex,
SB::Deps::VariableDep var_dep);
void addEdges(SB::Deps::SetVertex vertex, SB::Deps::VariableDep var_dep);
Expand Down
47 changes: 28 additions & 19 deletions src/mmoc/deps/jacobian_matrices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ void JacMatrixGenerator::postProcess(SB::Deps::SetVertex vertex) {}

void JacMatrixGenerator::init(SB::Deps::SetVertex vertex)
{
stringstream code;
stringstream code, clean_vectors;
FunctionPrinter function_printer;
Equation eq = getEquation(vertex);
if (eq.isAlgebraic()) {
clean_vectors << "cleanVector(algebraics, 0, " << ModelConfig::instance().algebraicNbr() << ");" << endl;
} else {
clean_vectors << "cleanVector(states, 0, " << ModelConfig::instance().stateNbr() << ");" << endl;
}
code << "for(row = 1; row <= " << vertex.range().size() << "; row++) {" << endl;
code << TAB << "c_row = _c_index(row);" << endl;
code << function_printer.jacMacrosAccess(eq);
_matrix.alloc.append(code.str());
_matrix.init.append(clean_vectors.str());
_matrix.init.append(code.str());
_tabs++;
}
Expand All @@ -59,7 +65,6 @@ void JacMatrixGenerator::end()
_tabs--;
_matrix.alloc.append("}\n");
_matrix.init.append("}\n");
code << "cleanVector(states, 0, " << ModelConfig::instance().stateNbr() << ");" << endl;
_matrix.init.append(code.str());
}

Expand All @@ -78,7 +83,7 @@ string JacMatrixGenerator::guard(string exp, string id)
void JacMatrixGenerator::addDependency(Equation v_eq, Equation g_eq, SB::Deps::VariableDep var_dep, int id, string g_map_dom)
{
stringstream code;
SB::Deps::LMapExp map = var_dep.nMap();
SB::Deps::LMapExp map = var_dep.nMap();
Range range(var_dep.variables(), var_dep.varOffset());
vector<string> exps = map.apply(range.getDimensionVars());
Expression i_exp = Expression::generate(var_dep.var().name(), exps);
Expand All @@ -87,21 +92,24 @@ void JacMatrixGenerator::addDependency(Equation v_eq, Equation g_eq, SB::Deps::V
string tabs = Utils::instance().tabs(_tabs);
string inner_tabs = tabs + TAB;
stringstream matrix_eq_id;
string matrix_access_vector;
if (v_eq.type() == EQUATION::Algebraic) {
matrix_eq_id << "dg_dx";
matrix_access_vector = "algebraics";
} else {
matrix_eq_id << "df_dx";
matrix_access_vector = "states";
}
matrix_eq_id << "[" << id << "]";
string eq_id = matrix_eq_id.str();
if (var_dep.isRecursive() && g_eq.hasRange()) {
code << tabs << g_eq.range().get();
tabs = Utils::instance().tabs(_tabs+1);
vector<string> exps = g_eq.range()->getIndexes();
FunctionPrinter printer;
Expression a_exp = Expression::generate(g_eq.LHSVariable()->name(), exps);
Index a_ind(a_exp);
code << TAB << printer.jacMacrosAccess(g_eq, a_ind.print());
code << tabs << g_eq.range().get();
tabs = Utils::instance().tabs(_tabs + 1);
vector<string> exps = g_eq.range()->getIndexes();
FunctionPrinter printer;
Expression a_exp = Expression::generate(g_eq.LHSVariable()->name(), exps);
Index a_ind(a_exp);
code << TAB << printer.jacMacrosAccess(g_eq, a_ind.print());
}
if (!map.constantExp()) {
code << tabs << "if(" << range.in(exps);
Expand All @@ -123,7 +131,8 @@ void JacMatrixGenerator::addDependency(Equation v_eq, Equation g_eq, SB::Deps::V
code << include_guard;
code << TAB << inner_tabs << "modelData->jac_matrices->" << eq_id << "->size[c_row]--;" << endl;
code << inner_tabs << "} else {" << endl;
code << TAB << inner_tabs << "modelData->jac_matrices->" << eq_id << "->index[c_row][states[c_row]++] = x_ind;" << endl;
code << TAB << inner_tabs << "modelData->jac_matrices->" << eq_id << "->index[c_row][" << matrix_access_vector << "[c_row]++] = x_ind;"
<< endl;
code << inner_tabs << "}" << endl;
_matrix.init.append(code.str());
code.str("");
Expand All @@ -150,16 +159,16 @@ std::string JacMatrixGenerator::guard(SB::Set dom, int offset, SB::Deps::LMapExp
}

void JacMatrixGenerator::visitF(SB::Deps::SetVertex vertex, SB::Deps::VariableDep var_dep)
{
{
Equation eq = getEquation(vertex);
addDependency(eq, eq,var_dep, eq.arrayId());
addDependency(eq, eq, var_dep, eq.arrayId());
}

void JacMatrixGenerator::visitF(SB::Deps::SetVertex vertex, SB::Deps::VariableDep var_dep, SB::Deps::SetVertex gen_vertex)
{
{
Equation eq = getEquation(vertex);
Equation gen_eq = getEquation(gen_vertex);
addDependency(eq, eq,var_dep, gen_eq.arrayId());
Equation gen_eq = getEquation(gen_vertex);
addDependency(eq, eq, var_dep, gen_eq.arrayId());
}

void JacMatrixGenerator::visitG(SB::Deps::SetVertex v_vertex, SB::Deps::SetVertex g_vertex, SB::Deps::VariableDep var_dep, int index_shift)
Expand All @@ -169,10 +178,10 @@ void JacMatrixGenerator::visitG(SB::Deps::SetVertex v_vertex, SB::Deps::SetVerte
addDependency(v_eq, g_eq, var_dep, v_eq.arrayId(), guard(var_dep.equations(), var_dep.eqOffset(), var_dep.mMap(), var_dep.var().name()));
}

void JacMatrixGenerator::visitG(SB::Deps::SetVertex v_vertex, SB::Deps::SetVertex g_vertex, SB::PWLMap use_map, SB::Deps::LMapExp use_map_exp, Expression use_exp, SB::PWLMap def_map,
SB::Deps::LMapExp def_map_exp, SB::Set intersection)
void JacMatrixGenerator::visitG(SB::Deps::SetVertex v_vertex, SB::Deps::SetVertex g_vertex, SB::PWLMap use_map,
SB::Deps::LMapExp use_map_exp, Expression use_exp, SB::PWLMap def_map, SB::Deps::LMapExp def_map_exp,
SB::Set intersection)
{

}

void JacMatrixGenerator::initG(SB::Deps::SetVertex vertex, SB::Deps::SetEdge edge) {}
Expand Down
19 changes: 13 additions & 6 deletions src/mmoc/deps/sbg_graph/lmap_exp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Deps {

CoeffContainer::CoeffContainer(std::vector<int> coeffs) : _coeffs(coeffs), _valid_dims()
{
for(size_t i = 0; i < _coeffs.size(); i++) {
for (size_t i = 0; i < _coeffs.size(); i++) {
_valid_dims.push_back(true);
}
}
Expand All @@ -39,7 +39,7 @@ CoeffContainer::CoeffContainer(std::vector<int> coeffs, std::vector<bool> valid_

CoeffContainer::CoeffContainer() : _coeffs(), _valid_dims() {}

CoeffContainer& CoeffContainer::operator=(const CoeffContainer& other)
CoeffContainer& CoeffContainer::operator=(const CoeffContainer& other)
{
_coeffs = other._coeffs;
_valid_dims = other._valid_dims;
Expand All @@ -66,10 +66,7 @@ void CoeffContainer::addDim()
_coeffs.push_back(0);
}

bool CoeffContainer::isValid(int i) const
{
return _valid_dims[i];
}
bool CoeffContainer::isValid(int i) const { return _valid_dims[i]; }

bool CoeffContainer::isZeros()
{
Expand Down Expand Up @@ -284,6 +281,16 @@ LMapExp LMapExp::compose(const LMapExp& other)
return LMapExp(new_constants, new_slopes, initValues());
}

void LMapExp::padDims(int max)
{
int init = _slopes.size();
for (int add = init; add < max; add++) {
_constants.addDim();
_slopes.addDim();
_init_values.addDim();
}
}

LMapExp LMapExp::solve(const LMapExp& other)
{
if (other.isEmpty()) {
Expand Down
Loading

0 comments on commit b3c5251

Please sign in to comment.