Skip to content

Perform topological sort to order components - #428

Open
cmacmackin wants to merge 31 commits into
masterfrom
cmacmackin/topological_sort_components
Open

cmacmackin wants to merge 31 commits into
masterfrom
cmacmackin/topological_sort_components

Conversation

@cmacmackin

@cmacmackin cmacmackin commented Nov 27, 2025 •

Copy link
Copy Markdown
Collaborator

Using the access control information introduced in #421, this PR makes it possible for Hermes-3 to work out the order of components at run-time. This will make things far simpler and more robust for users. It will also fail faster if there is an unsatisfiable or circular dependency.

Closes #384.

@cmacmackin

cmacmackin commented Nov 27, 2025 •

Copy link
Copy Markdown
Collaborator Author

This is nearly done. Remaining tasks:

  • Merge Control access to state variables in transform method #421 and rebase.
  • There is one integration test giving me strange h5py errors on my computer. It doesn't look to be related to any changes I've made. We'll see what happens in CI. I seem to recall we saw something similar elsewhere at one point.
  • Update documentation.

@cmacmackin cmacmackin changed the title WIP: Perform topologicalk sort to order components WIP: Perform topological sort to order components Nov 27, 2025
@codecov

codecov Bot commented Nov 27, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.53047% with 32 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.52%. Comparing base (c69f8d3) to head (889c9d0).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
include/component.hxx 63.63% 8 Missing and 4 partials ⚠️
src/component_scheduler.cxx 94.53% 0 Missing and 10 partials ⚠️
src/electromagnetic.cxx 16.66% 5 Missing ⚠️
src/permissions.cxx 91.30% 1 Missing and 1 partial ⚠️
include/set_temperature.hxx 0.00% 1 Missing ⚠️
src/component.cxx 95.45% 0 Missing and 1 partial ⚠️
src/izn_rec_reaction.cxx 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #428      +/-   ##
==========================================
+ Coverage   59.73%   60.52%   +0.78%     
==========================================
  Files          98       98              
  Lines       10270    10479     +209     
  Branches     1482     1520      +38     
==========================================
+ Hits         6135     6342     +207     
+ Misses       3488     3481       -7     
- Partials      647      656       +9     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cmacmackin
cmacmackin marked this pull request as draft November 27, 2025 18:45
@cmacmackin
cmacmackin force-pushed the cmacmackin/topological_sort_components branch from a75b2ce to 8d049e2 Compare December 2, 2025 15:57
@cmacmackin
cmacmackin marked this pull request as ready for review December 5, 2025 15:07
@cmacmackin cmacmackin changed the title WIP: Perform topological sort to order components Perform topological sort to order components Dec 5, 2025
cmacmackin added a commit that referenced this pull request Jan 2, 2026
The comments were in PR #445, but some of them actually relate to work
done in #428. Only the latter are dealt with in this PR.
@cmacmackin
cmacmackin force-pushed the cmacmackin/topological_sort_components branch from c31490e to e650c1c Compare January 2, 2026 14:28
cmacmackin added a commit that referenced this pull request Jan 5, 2026
The comments were in PR #445, but some of them actually relate to work
done in #428. Only the latter are dealt with in this PR.
@cmacmackin
cmacmackin force-pushed the cmacmackin/topological_sort_components branch from e650c1c to 9dbeed5 Compare January 5, 2026 17:42
cmacmackin added a commit that referenced this pull request Jan 6, 2026
The comments were in PR #445, but some of them actually relate to work
done in #428. Only the latter are dealt with in this PR.
@cmacmackin
cmacmackin force-pushed the cmacmackin/topological_sort_components branch from 9dbeed5 to 04a8977 Compare January 6, 2026 16:00
ZedThree pushed a commit that referenced this pull request Jan 7, 2026
The comments were in PR #445, but some of them actually relate to work
done in #428. Only the latter are dealt with in this PR.
@ZedThree
ZedThree force-pushed the cmacmackin/topological_sort_components branch from 04a8977 to 8fc17e0 Compare January 7, 2026 10:46
@mikekryjak

Copy link
Copy Markdown
Collaborator

I missed this in #421, but I see that you added a long-needed feature - a better way to determine species type which is based on the charge:

// FIXME: Would there be any spcies without AA? Is there any other
// reliable way to identify what is a species?
else if (component_options[name_trimmed].isSet("AA")) {
if (component_options[name_trimmed].isSet("charge")) {
const BoutReal charge = component_options[name_trimmed]["charge"];
if (charge > 1e-5) {
positive_ions.push_back(name_trimmed);
} else if (charge < -1e-5) {
negative_ions.push_back(name_trimmed);
} else {
neutrals.push_back(name_trimmed);
}
} else {
neutrals.push_back(name_trimmed);

There is already a function that does the same in hermes_utils and is used in a few places in the code:

/// Identify species name string as electron, ion or neutral
inline SpeciesType identifySpeciesType(const std::string& species) {
if (species == "e") {
return SpeciesType::electron;
} else if ((species == "i") or
species.find(std::string("+")) != std::string::npos) {
return SpeciesType::ion;
}
// Not electron or ion -> neutral
return SpeciesType::neutral;

Ideally there should be only one tool to do this, and your new one seems more robust. Is there any reason not to replace the one in hermes_utils with the new one?

@cmacmackin

Copy link
Copy Markdown
Collaborator Author

Ideally there should be only one tool to do this, and your new one seems more robust. Is there any reason not to replace the one in hermes_utils with the new one?

Probably not. I just wasn't sure if people would be happy about me changing that bit of code. Note that this will require changing the function signature of identifySpeciesType so that it takes the Options instead of just a string.

@mikekryjak

Copy link
Copy Markdown
Collaborator

I went through the sorting algorithm and I see that you have left a good amount of comments on the individual bits. However, I found it difficult to get my head around what's going on just because of the amount of steps involved. It would be very useful to have a paragraph describing how the algorithm works step-by-step, either in the docs or in the comments (or both)

@cmacmackin

Copy link
Copy Markdown
Collaborator Author

There are some places (e.g., when writing tests) where it was convenient to just be able to have a list of species names and use the old heuristics to categorise them. Probably not a good enough reason to keep that though.

@ZedThree ZedThree left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @cmacmackin!

There's some trivial bits that I'm happy to fix myself

Comment thread src/component_scheduler.cxx Outdated
Comment thread src/component_scheduler.cxx
Comment thread src/component_scheduler.cxx Outdated
Comment thread src/component_scheduler.cxx
Comment thread docs/sphinx/developer.rst Outdated
cmacmackin added a commit that referenced this pull request Jun 25, 2026
The comments were in PR #445, but some of them actually relate to work
done in #428. Only the latter are dealt with in this PR.
@cmacmackin
cmacmackin force-pushed the cmacmackin/topological_sort_components branch from 8fc17e0 to 73593b7 Compare June 25, 2026 14:44
cmacmackin added a commit that referenced this pull request Jun 29, 2026
The comments were in PR #445, but some of them actually relate to work
done in #428. Only the latter are dealt with in this PR.
@cmacmackin
cmacmackin force-pushed the cmacmackin/topological_sort_components branch from aacebfa to 4572264 Compare September 4, 2026 16:33
Comment thread src/component.cxx Outdated
Comment thread tests/unit/test_component.cxx
Comment thread docs/sphinx/developer.rst Outdated
Comment thread docs/sphinx/developer.rst Outdated
Comment thread docs/sphinx/developer.rst Outdated
Comment thread docs/sphinx/developer.rst Outdated
Comment thread include/permissions.hxx Outdated

@bendudson bendudson left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @cmacmackin !

@mikekryjak mikekryjak left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave another serious go at going through everything and left a few comments. My preference would be that we merge this with a flag to disable the sort if needed (enabled by default is fine) as well as a facility to print the as-written and as-sorted component list to console on initialisation.

We also definitely need to make a release before merging this in.

Comment thread include/permissions.hxx Outdated
/// permissions on the boundaries and read permissions for the
/// interior.
///
/// FIXME: Currently these permissiosn are not expressed properly, due

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this FIXME still active? what does it take to fix it and what are the possible risks of leaving it unfixed?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This limitation still exists and it would be nontrivial to implement the desired feature. The permission system doesn't currently allow conditional logic that depends on any other variables or regions. I played around a bit with a system for providing this over last Christmas break, but it would significantly delay getting this merged if we wanted something like that.

I think the risks of leaving this unfixed are relatively low. It could, in principle, result in spurious errors from circular dependencies in the sorting algorithm, but nothing like that has surfaced so far. I'll cherrypick the commits allowing us to turn off the sorting algorithm and incorporate that into this PR, so we'd have a fallback if we do encounter a situation like this.

Comment thread src/component_scheduler.cxx Outdated
Comment thread src/component_scheduler.cxx Outdated
Comment thread docs/sphinx/inputs.rst Outdated
Comment thread include/permissions.hxx Outdated
Comment thread src/component_scheduler.cxx Outdated
Comment thread src/component_scheduler.cxx Outdated
Comment thread src/component_scheduler.cxx Outdated
Comment thread src/component_scheduler.cxx Outdated
Comment thread tests/unit/test_component_scheduler.cxx Outdated
@cmacmackin

Copy link
Copy Markdown
Collaborator Author

I've addressed all comments left in review.

@cmacmackin
cmacmackin requested a review from mikekryjak October 1, 2026 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automatically order components

4 participants