Repository navigation
Remove quadratic terms from printing, equivalence searches and the analyser #1479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
85220cb
367ec35
739ba83
005f2a4
e1a9477
6cfaedf
efa22eb
5baad16
5a167b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ limitations under the License. | |
| #include <numeric> | ||
| #include <set> | ||
| #include <sstream> | ||
| #include <unordered_set> | ||
| #include <vector> | ||
|
|
||
| #include "libcellml/analyserequation.h" | ||
|
|
@@ -1015,6 +1016,41 @@ std::string makeUniqueId(IdList &idList) | |
| return id; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Find the first variable of a component in a variable's equivalence set. | ||
| * | ||
| * Visits the equivalence set depth first, in the order of each variable's equivalent variables, and stops at the first | ||
| * variable owned by @p component. A visited set gives constant-time membership tests, so the search is O(n) in the size | ||
| * of the equivalence set (e.g., a time variable that is equivalent across every component of a large model). | ||
| */ | ||
| VariablePtr firstEquivalentVariableInComponent(const VariablePtr &variable, const ComponentPtr &component, | ||
| std::unordered_set<const Variable *> &visited) | ||
| { | ||
| for (size_t i = 0, n = variable->equivalentVariableCount(); i < n; ++i) { | ||
| auto equivalentVariable = variable->equivalentVariable(i); | ||
|
Comment on lines
+1029
to
+1030
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Measured with one variable mapped to N components: printing took 1.7 / 12 / 94 s at N = 1000 / 2000 / 4000. Rather than expose the private helper (we're keeping |
||
| if (visited.insert(equivalentVariable.get()).second) { | ||
| if (owningComponent(equivalentVariable) == component) { | ||
| return equivalentVariable; | ||
| } | ||
| auto found = firstEquivalentVariableInComponent(equivalentVariable, component, visited); | ||
| if (found != nullptr) { | ||
| return found; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nullptr; | ||
| } | ||
|
|
||
| VariablePtr firstEquivalentVariableInComponent(const VariablePtr &variable, const ComponentPtr &component) | ||
| { | ||
| if (owningComponent(variable) == component) { | ||
| return variable; | ||
| } | ||
| std::unordered_set<const Variable *> visited = {variable.get()}; | ||
| return firstEquivalentVariableInComponent(variable, component, visited); | ||
| } | ||
|
|
||
| ConnectionMap createConnectionMap(const VariablePtr &variable1, const VariablePtr &variable2) | ||
| { | ||
| ConnectionMap map; | ||
|
|
@@ -1024,39 +1060,17 @@ ConnectionMap createConnectionMap(const VariablePtr &variable1, const VariablePt | |
| if ((component1 != nullptr) && (component2 != nullptr)) { | ||
| for (size_t i = 0; i < component1->variableCount(); ++i) { | ||
| auto v = component1->variable(i); | ||
| for (const auto &vEquiv : equivalentVariables(v)) { | ||
| if (owningComponent(vEquiv) == component2) { | ||
| map.insert(std::make_pair(v, vEquiv)); | ||
| } | ||
| // The map keeps the first equivalent variable in component2 (v itself, then depth first). | ||
| VariablePtr vEquiv = firstEquivalentVariableInComponent(v, component2); | ||
| if (vEquiv != nullptr) { | ||
| map.insert(std::make_pair(v, vEquiv)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return map; | ||
| } | ||
|
|
||
| void recursiveEquivalentVariables(const VariablePtr &variable, std::vector<VariablePtr> &equivalentVariables) | ||
| { | ||
| for (size_t i = 0; i < variable->equivalentVariableCount(); ++i) { | ||
| VariablePtr equivalentVariable = variable->equivalentVariable(i); | ||
|
|
||
| if (std::find(equivalentVariables.begin(), equivalentVariables.end(), equivalentVariable) == equivalentVariables.end()) { | ||
| equivalentVariables.push_back(equivalentVariable); | ||
|
|
||
| recursiveEquivalentVariables(equivalentVariable, equivalentVariables); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| std::vector<VariablePtr> equivalentVariables(const VariablePtr &variable) | ||
| { | ||
| std::vector<VariablePtr> res = {variable}; | ||
|
|
||
| recursiveEquivalentVariables(variable, res); | ||
|
|
||
| return res; | ||
| } | ||
|
|
||
| bool linkComponentVariableUnits(const ComponentPtr &component, DescriptionList &descriptionList) | ||
| { | ||
| bool status = true; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Measured with one variable mapped to N components: printing took 1.7 / 12 / 94 s at N = 1000 / 2000 / 4000. Rather than expose the private helper (we're keeping
variable.hunchanged), 5a167b9 makesequivalentVariable(i)andequivalentVariableCount()constant time: a deleted variable marks each of its equivalent variables, and unmarked variables index their list directly. Printing now takes 0.08 / 0.30 / 1.2 s, and output is unchanged.