Skip to content

Commit

Permalink
Changed dumping of linear torn systems
Browse files Browse the repository at this point in the history
  - The internal equations are now dumped as fields of the main equation.
    They used to be dumped before the actual equation which made parsing them contrived.

  - Diabled old parsing and wrote a new one since the dump format has changed.
    In order to avoid surprises, we stick to parsing only what we know we can handle for now.

  - Disabled source dumping for now. It is not really needed anyway.
  - Normalized debug and log dumping a bit.
  • Loading branch information
mahge committed May 20, 2020
1 parent 7d0a626 commit 54ce151
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 65 deletions.
38 changes: 21 additions & 17 deletions OMCompiler/Compiler/SimCode/SerializeModelInfo.mo
Expand Up @@ -277,6 +277,8 @@ protected
Integer i;
list<DAE.SymbolicOperation> operations;
algorithm
File.write(file,"{}");
return;
DAE.SOURCE(typeLst=typeLst,info=info,instance=instance,partOfLst=partOfLst,operations=operations) := source;
File.write(file,"{\"info\":");
serializeInfo(file,info);
Expand Down Expand Up @@ -592,29 +594,15 @@ algorithm
case SimCode.SES_LINEAR(lSystem = lSystem as SimCode.LINEARSYSTEM(), alternativeTearing = NONE()) equation
i = listLength(lSystem.beqs);
j = listLength(lSystem.simJac);

eqs = SimCodeUtil.sortEqSystems(lSystem.residual);
if not listEmpty(eqs) then
serializeEquation(file,listHead(eqs),section,withOperations,parent=lSystem.index,first=true,assign_type=if lSystem.tornSystem then 1 else 0);
min(serializeEquation(file,e,section,withOperations,parent=lSystem.index,assign_type=if lSystem.tornSystem then 1 else 0) for e in List.rest(eqs));
end if;

jeqs = match lSystem.jacobianMatrix
case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs,constantEqns=constantEqns)})) then SimCodeUtil.sortEqSystems(listAppend(jeqs,constantEqns));
else {};
end match;
if not listEmpty(jeqs) then
File.write(file, ",");
serializeEquation(file,listHead(jeqs),section,withOperations,parent=lSystem.index,first=true,assign_type=2);
min(serializeEquation(file,e,section,withOperations,parent=lSystem.index,assign_type=2) for e in List.rest(jeqs));
end if;

if listEmpty(eqs) and listEmpty(jeqs) then
File.write(file, "\n{\"eqIndex\":");
else
File.write(file, ",\n{\"eqIndex\":");
end if;
File.write(file, "\n{\"eqIndex\":");
File.writeInt(file, lSystem.index);

if parent <> 0 then
File.write(file, ",\"parent\":");
File.writeInt(file, parent);
Expand Down Expand Up @@ -642,7 +630,23 @@ algorithm
serializeList1(file,lSystem.simJac,withOperations,serializeLinearCell);
File.write(file,"],\"b\":[");
serializeList(file,lSystem.beqs,serializeExp);
File.write(file,"]}]}");
File.write(file,"]}]");

File.write(file, ",\n\"internal-equations\":[");

if not listEmpty(eqs) then
serializeEquation(file,listHead(eqs),section,withOperations,parent=lSystem.index,first=true,assign_type=if lSystem.tornSystem then 1 else 0);
min(serializeEquation(file,e,section,withOperations,parent=lSystem.index,assign_type=if lSystem.tornSystem then 1 else 0) for e in List.rest(eqs));
end if;
File.write(file, "\n]");

File.write(file, ",\n\"jacobian-equations\":[");
if not listEmpty(jeqs) then
serializeEquation(file,listHead(jeqs),section,withOperations,parent=lSystem.index,first=true,assign_type=2);
min(serializeEquation(file,e,section,withOperations,parent=lSystem.index,assign_type=2) for e in List.rest(jeqs));
end if;

File.write(file, "\n]}");
then true;

// dynamic tearing
Expand Down
189 changes: 142 additions & 47 deletions OMCompiler/SimulationRuntime/ParModelica/auto/om_pm_model.cpp
Expand Up @@ -305,6 +305,8 @@ inline void check_tag(int index, const std::string& tag) {
if (tag == "dummy"
or tag == "assign"
or tag == "residual"
or tag == "tornsystem"
or tag == "jacobian" // TODO: Skip me. These cariables are visible only inside the SCC
or tag == "algorithm"
or tag == "container")
return;
Expand Down Expand Up @@ -346,11 +348,22 @@ void OMModel::load_from_json(TaskSystemT& task_system, const std::string& eq_to_
continue;
}

// So that we know what we can handle so far.
check_tag(index, eq["tag"]);
if(eq["section"] != "regular") {
utility::eq_index_fatal(index, "Unkown section!" + eq["section"].get<std::string>());
}

const std::string& tag = eq["tag"];
check_tag(index, tag);


if(tag == "assign") {

#ifdef NDEBUG
if (eq["defines"].size() != 1) {
utility::eq_index_error(index, "Assign with more than one define!");
}
#endif

/*an equation with no parent and is not a container(system). create a new node for it.*/
if(eq["parent"] == nullptr && eq["tag"] != "container") {
Equation current_node;
current_node.index = index;

Expand All @@ -360,39 +373,17 @@ void OMModel::load_from_json(TaskSystemT& task_system, const std::string& eq_to_
current_node.threadData = this->threadData;
current_node.function_system = function_system;

for(auto def : eq["defines"]) {
current_node.lhs.insert(def.get<std::string>());
}
current_node.lhs.insert(eq["defines"].front().get<std::string>());

for(auto use : eq["uses"])
current_node.rhs.insert(use.get<std::string>());

++node_count;
task_system.add_node(current_node);

}
/*an equation with parent and is not a complex system. collect references from it to pass
to its parent.*/
else if(eq["parent"] != nullptr && eq["tag"] != "container") {
if(current_parent == -1)
current_parent = eq["parent"];
else if (eq["parent"] != current_parent) {
std::cerr << "current parent " << current_parent <<" and equation parent " << eq["parent"] << " don't add up. something is fishy" << std::endl;
exit(1);
}

// std::cout << "Collecting from : "<< index << " for : " << eq["parent"] << std::endl;

for(auto def : eq["defines"]) {
complex_eq_lhs.insert(def.get<std::string>());
}
for(auto use : eq["uses"])
complex_eq_rhs.insert(use.get<std::string>());
}
/*an equation with no parent and is a complex system. create a new node for it
using the collected rhs and lsh references from its children.*/
else if(eq["parent"] == nullptr && eq["tag"] == "container") {

check_container_dispaly(index,eq["display"]);
else if(tag == "tornsystem") {

Equation current_node;
current_node.index = index;
Expand All @@ -403,40 +394,144 @@ void OMModel::load_from_json(TaskSystemT& task_system, const std::string& eq_to_
current_node.threadData = this->threadData;
current_node.function_system = function_system;

current_node.lhs = complex_eq_lhs;
complex_eq_lhs.clear();
for(auto def : eq["defines"]) {
current_node.lhs.insert(def.get<std::string>());
utility::indexed_dlog(index, ": added own defines: " + def.get<std::string>());
}

// std::cout << "Equation: " << index << " defines : "<< std::endl;
// for(auto def : current_node.lhs)
// std::cout << def << ", ";
// std::cout << std::endl;
auto sys_size = eq["unknowns"].get<int>();

for(auto int_eq : eq["internal-equations"]) {
for(auto def : int_eq["defines"]) {
current_node.lhs.insert(def.get<std::string>());
utility::indexed_dlog(index, ": added defines: " + def.get<std::string>() + " : due to " + std::to_string(int_eq["eqIndex"].get<int>()));
}

for(auto use : int_eq["uses"]) {
auto var_s = use.get<std::string>();
auto local_defined = current_node.lhs.find(var_s) != current_node.lhs.end();

// Disable me and see if graphs look different.
if (!local_defined) {
current_node.rhs.insert(use.get<std::string>());
utility::indexed_dlog(index, ": added uses: " + use.get<std::string>() + " : due to " + std::to_string(int_eq["eqIndex"].get<int>()));
}
else{
utility::indexed_dlog(index, ": skiped uses: " + use.get<std::string>() + " : due to local define");
}
}

current_node.rhs = complex_eq_rhs;
complex_eq_rhs.clear();
for(auto use : eq["uses"]) {
current_node.rhs.insert(use.get<std::string>());
}

// std::cout << "Equation: " << index << " uses : "<< std::endl;
// for(auto use : current_node.rhs)
// std::cout << use << ", ";
// std::cout << std::endl;
utility::indexed_dlog(index, "Total number of defines: " + std::to_string(current_node.lhs.size()));
utility::indexed_dlog(index, "Total number of uses: " + std::to_string(current_node.rhs.size()));

current_parent = -1;

++node_count;
task_system.add_node(current_node);

}

else {
std::cerr << "Equation type not yet handled : " << index << std::endl;
std::cerr << eq << std::endl;
exit(1);
utility::eq_index_fatal(index, "Equation type not yet handled: " + eq["tag"].get<std::string>());
}






// // So that we know what we can handle so far.
// check_tag(index, eq["tag"]);

// /*an equation with no parent and is not a container(system). create a new node for it.*/
// if(eq["parent"] == nullptr && eq["tag"] != "container") {
// Equation current_node;
// current_node.index = index;

// // Copy the pointers to the needed info from the Model
// // to each equation node.
// current_node.data = this->data;
// current_node.threadData = this->threadData;
// current_node.function_system = function_system;

// for(auto def : eq["defines"]) {
// current_node.lhs.insert(def.get<std::string>());
// }
// for(auto use : eq["uses"])
// current_node.rhs.insert(use.get<std::string>());

// ++node_count;
// task_system.add_node(current_node);

// }
// /*an equation with parent and is not a complex system. collect references from it to pass
// to its parent.*/
// else if(eq["parent"] != nullptr && eq["tag"] != "container") {
// if(current_parent == -1)
// current_parent = eq["parent"];
// else if (eq["parent"] != current_parent) {
// std::cerr << "current parent " << current_parent <<" and equation parent " << eq["parent"] << " don't add up. something is fishy" << std::endl;
// exit(1);
// }

// // std::cout << "Collecting from : "<< index << " for : " << eq["parent"] << std::endl;

// for(auto def : eq["defines"]) {
// complex_eq_lhs.insert(def.get<std::string>());
// }
// for(auto use : eq["uses"])
// complex_eq_rhs.insert(use.get<std::string>());
// }
// /*an equation with no parent and is a complex system. create a new node for it
// using the collected rhs and lsh references from its children.*/
// else if(eq["parent"] == nullptr && eq["tag"] == "container") {

// check_container_dispaly(index,eq["display"]);

// Equation current_node;
// current_node.index = index;

// // Copy the pointers to the needed info from the Model
// // to each equation node.
// current_node.data = this->data;
// current_node.threadData = this->threadData;
// current_node.function_system = function_system;

// current_node.lhs = complex_eq_lhs;
// complex_eq_lhs.clear();
// for(auto def : eq["defines"]) {
// current_node.lhs.insert(def.get<std::string>());
// }

// // std::cout << "Equation: " << index << " defines : "<< std::endl;
// // for(auto def : current_node.lhs)
// // std::cout << def << ", ";
// // std::cout << std::endl;


// current_node.rhs = complex_eq_rhs;
// complex_eq_rhs.clear();
// for(auto use : eq["uses"]) {
// current_node.rhs.insert(use.get<std::string>());
// }

// // std::cout << "Equation: " << index << " uses : "<< std::endl;
// // for(auto use : current_node.rhs)
// // std::cout << use << ", ";
// // std::cout << std::endl;

// current_parent = -1;

// ++node_count;
// task_system.add_node(current_node);

// }
// else {
// std::cerr << "Equation type not yet handled : " << index << std::endl;
// std::cerr << eq << std::endl;
// exit(1);
// }
}

std::cout << "Number of tasks = " << node_count << newl;
Expand Down
16 changes: 15 additions & 1 deletion OMCompiler/SimulationRuntime/ParModelica/auto/pm_utility.cpp
Expand Up @@ -52,7 +52,13 @@ std::ostream& log(const char* pref = "") {
}

std::ostream& log() {
return log_stream;
return std::cout;
}

void indexed_dlog(int index, const std::string& message) {
#ifdef OM_PM_LOG_VERBOSE
std::cerr << "INFO: "<< std::to_string(index) << " : " << message << std::endl;
#endif
}

std::ostream& warning(const char* pref = "") {
Expand All @@ -75,6 +81,14 @@ std::ostream& error() {
return std::cerr;
}

void eq_index_error(int index, const std::string& message) {
std::cerr << std::to_string(index) << " : " << message << std::endl;
}

void eq_index_fatal(int index, const std::string& message) {
utility::eq_index_error(index, message);
exit(1);
}

} // utility
} // parmodelica
Expand Down
5 changes: 5 additions & 0 deletions OMCompiler/SimulationRuntime/ParModelica/auto/pm_utility.hpp
Expand Up @@ -72,6 +72,11 @@ extern std::ostringstream error_stream;
std::ostream& error(const char* pref);
std::ostream& error();

// debug only log
void indexed_dlog(int index, const std::string& message);
void eq_index_error(int index, const std::string& message);
void eq_index_fatal(int index, const std::string& message);



template<typename InputIterator1, typename InputIterator2>
Expand Down

0 comments on commit 54ce151

Please sign in to comment.