Skip to content

Commit

Permalink
dynawo#27 test
Browse files Browse the repository at this point in the history
Signed-off-by: FredericSabot <[email protected]>
  • Loading branch information
FredericSabot committed Aug 16, 2023
1 parent 04b51e2 commit 0a53b65
Show file tree
Hide file tree
Showing 21 changed files with 241 additions and 213 deletions.
2 changes: 1 addition & 1 deletion dynawo/sources/Common/DYNTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <chrono>
#include <boost/core/noncopyable.hpp>

// #define PRINT_TIMERS
// #define PRINT_TIMERS // Additional comment for easier access

namespace DYN {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ ConnectorCalculatedVariable::evalJt(const double /*t*/, const double /*cj*/, Spa
model_->evalJCalculatedVarI(indexCalculatedVariable_, JModel);

for (std::size_t i = 0, iEnd = varExtIndexes_.size(); i < iEnd; ++i) { // d(f)/dyModel = d(calculatedVariable)/d(yModel)
Jt.addTerm(model_->getOffsetY() + varExtIndexes_[i], JModel[i]);
if (std::isnan(JModel[i]) || std::isinf(JModel[i]))
Jt.addTerm(model_->getOffsetY() + varExtIndexes_[i], 0);
else
Jt.addTerm(model_->getOffsetY() + varExtIndexes_[i], JModel[i]);
}
}

Expand Down
31 changes: 27 additions & 4 deletions dynawo/sources/ModelicaCompiler/Scripts_OMC_1_13_2/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,12 +1269,18 @@ def build_relations(self):
tmps_to_add = []
index_additional_relation = 0
for eq in self.list_eq_syst:

no_event_nodes = []
if eq.get_evaluated_var() in self.reader.var_name_to_mixed_residual_vars_types:
no_event_nodes = self.reader.var_name_to_mixed_residual_vars_types[eq.get_evaluated_var()].get_no_event()

index_if = 0
for line in eq.get_body():
if (re.search(r'modelica_[a-z]+ tmp[0-9]+?;', line)):
tmps_definition.append(str(line).replace(" ", ""))
if (re.search(r'tmp[0-9]+ = [a-zA-Z]*.*?\;', line)):
tmps_assignment.append(str(line).replace(" ", ""))
if (("Greater" in line or "Less" in line) and "RELATIONHYSTERESIS" not in line):
if (("Greater" in line or "Less" in line) and "RELATIONHYSTERESIS" not in line and not no_event_nodes[index_if]):
tmps_relation = find_all_temporary_variable_in_line(line)
for tmp in tmps_relation:
tmps_to_add.extend(add_tmp_update_relations(tmp, tmps_assignment, tmps_to_add))
Expand All @@ -1287,6 +1293,8 @@ def build_relations(self):
relation_to_create.set_body_definition(" data->simulationInfo->relations[" + str(index_relation_to_create) + "] = " + line.split(" = ")[0].replace("tmp", "tmp_cr").replace(" ", "") + ";\n")
self.modes.add_created_relation(relation_to_create)
self.nb_created_relations = index_additional_relation
if "else" in line:
index_if +=1
self.add_tmps_for_modes(tmps_to_add, tmps_assignment, tmps_definition, True)

##
Expand Down Expand Up @@ -1591,15 +1599,23 @@ def dump_eq(self, eq):
standard_eq_body = []
standard_eq_body.append (self.ptrn_f_name %(eq.get_src_fct_name()))
eq_body = (eq.get_body_for_setf())

no_event_nodes = []
if var_name in self.reader.var_name_to_mixed_residual_vars_types:
no_event_nodes = self.reader.var_name_to_mixed_residual_vars_types[var_name].get_no_event()

index_if = 0
if self.create_additional_relations():
index = 0
index_relation = 0
for line in eq_body:
if (("Greater" in line or "Less" in line) and "RELATIONHYSTERESIS" not in line):
if (("Greater" in line or "Less" in line) and "RELATIONHYSTERESIS" not in line and not no_event_nodes[index_if]):
index_relations = self.modes.find_index_relation(eq.get_src_fct_name())
assert(len(index_relations) > 0 and index_relation < len(index_relations))
eq_body[index] = self.transform_in_relation(line, index_relations[index_relation])
index_relation += 1
if "else" in line:
index_if +=1
index += 1
standard_eq_body.extend(eq_body)

Expand Down Expand Up @@ -1743,7 +1759,7 @@ def prepare_for_setf(self):

def transform_in_relation(self, line, index_relation):
tmp_to_define = find_all_temporary_variable_in_line(line)[0]
parenthesis_split = re.findall(r'[a-z]*[A-Z].*?\*\/.*,', line)[0].split("(")
parenthesis_split = re.findall(r'Greater\(.*,|Less\(.*,|GreaterEq\(.*,|LessEq\(.*,|Greater<adept::adouble>\(.*,|Less<adept::adouble>\(.*,|GreaterEq<adept::adouble>\(.*,|LessEq<adept::adouble>\(.*,', line)[0].split("(")
comparator = parenthesis_split[0]
variable_1 = '('.join(parenthesis_split[1:]).split(",")[0]
variable_2 = re.findall(r',.*?;', line)[0].rsplit(")", 1)[0]
Expand Down Expand Up @@ -2571,17 +2587,24 @@ def prepare_for_evalfadept(self):
standard_body_with_standard_external_call = eq.get_body_for_evalf_adept()
standard_body = []
index_relation = 0

no_event_nodes = []
if var_name in self.reader.var_name_to_mixed_residual_vars_types:
no_event_nodes = self.reader.var_name_to_mixed_residual_vars_types[var_name].get_no_event()
index_if = 0
for line in standard_body_with_standard_external_call:
for func in list_omc_functions:
if (func.get_name() + "(" in line or func.get_name() + " (" in line):
if not is_adept_func(func, self.list_adept_structs) : continue
used_functions.append(func)
line = self.replace_adept_functions_in_line(line)
if self.create_additional_relations() and (("Greater" in line or "Less" in line) and "RELATIONHYSTERESIS" not in line):
if self.create_additional_relations() and (("Greater" in line or "Less" in line) and "RELATIONHYSTERESIS" not in line and not no_event_nodes[index_if]):
index_relations = self.modes.find_index_relation(eq.get_src_fct_name())
assert(len(index_relations) > 0 and index_relation < len(index_relations))
line = self.transform_in_relation(line, index_relations[index_relation])
index_relation+=1
if "else" in line:
index_if +=1
standard_body.append(line)

# Build the whole equation body as if clauses linked with reinit
Expand Down
47 changes: 33 additions & 14 deletions dynawo/sources/ModelicaCompiler/Scripts_OMC_1_13_2/readerOMC.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,14 @@ def __init__(self):
self.diff_var = []
self.children = []
self.eq = ""
self.no_event = False

def print_node(self, prefix):
print (prefix + "NODE : ")
print (prefix + "NODE TYPE: " + str(self.type))
print (prefix + "NODE DIFF VAR: " + str(self.diff_var))
print (prefix + "NODE EQ: " + self.eq)
print (prefix + "NODE noEvent: " + str(self.no_event))
for child in self.children:
child.print_node(prefix + " " )

Expand All @@ -477,6 +479,14 @@ def get_equations(self):
equations.append(self.eq)
return equations

def get_no_event(self):
no_event = []
for child in self.children:
no_event.extend(child.get_no_event())
if self.type != None:
no_event.append(self.no_event)
return no_event

def propagate_differential(self):
for child in self.children:
if self.type != None and self.type == DIFFERENTIAL:
Expand Down Expand Up @@ -511,7 +521,7 @@ def create_nodes_for_min_and_max(self, root):
for child in root.children:
self.create_nodes_for_min_and_max(child)

def analyse_eq_expr(self, eq, root):
def analyse_eq_expr(self, eq, root, no_event_found_parent):
der_var_ptrn = re.compile(r'der\s*\((?P<var>[\(\S\) ]+?)\)')
splitted_eq = eq.replace('[u\'', '').split(" ")
idx = 0
Expand All @@ -531,24 +541,27 @@ def analyse_eq_expr(self, eq, root):
nb_parenthesis-=splitted_eq[idx].count(")")
sub_eq+= " " + splitted_eq[idx]
idx+=1
self.analyse_eq_expr(sub_eq[:-1], root)
self.analyse_eq_expr(sub_eq[:-1], root, no_event_found_parent)
elif expr.startswith("if") or expr.startswith("-if") or expr.startswith("- if"):
idx = self.remove_condition(idx, splitted_eq)
(idx, no_event_found) = self.remove_condition(idx, splitted_eq)
node = self.Node()
node.eq = "IF"
node.no_event = no_event_found
root.children.append(node)
(final_expr, idx) = self.find_value(idx, splitted_eq)
self.analyse_eq_expr(final_expr, node)
(final_expr, idx, no_event_found_value) = self.find_value(idx, splitted_eq)
self.analyse_eq_expr(final_expr, node, no_event_found)
elif expr.startswith("else"):
node = self.Node()
root.children.append(node)
node.eq = "ELSE"
node.no_event = no_event_found_parent
idx += 1
(final_expr, idx) = self.find_value(idx, splitted_eq)
self.analyse_eq_expr(final_expr, node)
(final_expr, idx, no_event_found) = self.find_value(idx, splitted_eq)
self.analyse_eq_expr(final_expr, node, no_event_found)
else:
(final_expr, idx) = self.find_value(idx, splitted_eq)
(final_expr, idx, no_event_found) = self.find_value(idx, splitted_eq)
root.eq = final_expr
root.no_event = no_event_found_parent
if "der(" in final_expr or "der (" in final_expr:
root.type = DIFFERENTIAL
match2 = re.findall(der_var_ptrn, final_expr)
Expand All @@ -559,14 +572,18 @@ def analyse_eq_expr(self, eq, root):

def remove_condition(self, initial_idx, splitted_eq):
idx = initial_idx
no_event_found = False
while idx < len(splitted_eq) and splitted_eq[idx] != "then":
if "noEvent(" in splitted_eq[idx]:
no_event_found = True
idx+=1
return idx+1
return (idx+1, no_event_found)

def find_value(self, initial_idx, splitted_eq):
idx = initial_idx
no_event_found = False
if splitted_eq[idx].startswith("if") or splitted_eq[idx].startswith("-if"):
idx = self.remove_condition(idx, splitted_eq)
(idx, no_event_found) = self.remove_condition(idx, splitted_eq)
final_expr = ""

if splitted_eq[idx].startswith("(if") or splitted_eq[idx].startswith("-(if") or splitted_eq[idx].startswith("- (if"):
Expand All @@ -580,20 +597,22 @@ def find_value(self, initial_idx, splitted_eq):
nb_parenthesis-=splitted_eq[idx].count(")")
sub_eq+= " " + splitted_eq[idx]
idx+=1
return (sub_eq[:-1], idx)
return (sub_eq[:-1], idx, no_event_found)
else:
keywords = ["else", "if", "-if"]
operation_ptrn = re.compile(r'[+-]')
variable_ptrn = re.compile(r'[\S.]+')
while idx < len(splitted_eq) and splitted_eq[idx] not in keywords \
and (re.search(operation_ptrn,splitted_eq[idx]) != None or re.search(variable_ptrn,splitted_eq[idx]) != None):
if splitted_eq[idx].startswith("(if"):
(tmp, idx) = self.find_value(idx, splitted_eq)
(tmp, idx, no_event_found2) = self.find_value(idx, splitted_eq)
if no_event_found2:
no_event_found = True
final_expr += tmp
else:
final_expr += " " + splitted_eq[idx]
idx+=1
return (final_expr, idx)
return (final_expr, idx, no_event_found)
##
# Analyse the equation to retrieve the type (ALGEBRAIC, DIFFERENTIAL or MIXED) and the differential variables in a branch
# @param self : object pointer
Expand Down Expand Up @@ -621,7 +640,7 @@ def analyse_equations_and_get_types(self, eq, defined_var_eq):
self.var_name_to_differential_dependency_variables[defined_var_eq] = diff_var
else:
root = self.Node()
self.analyse_eq_expr(eq, root)
self.analyse_eq_expr(eq, root, False)
root.propagate_differential()
self.create_nodes_for_min_and_max(root)
types = root.get_types()
Expand Down
Loading

0 comments on commit 0a53b65

Please sign in to comment.