11#include < algorithm>
2+ #include < cstddef>
3+ #include < iterator>
4+ #include < map>
25#include < memory>
6+ #include < set>
37#include < string>
8+ #include < utility>
49#include < vector>
510
611#include < bout/bout_types.hxx>
12+ #include < bout/boutexception.hxx>
713#include < bout/options.hxx>
814#include < bout/utils.hxx> // for trim, strsplit
15+ #include < fmt/format.h>
916#include < fmt/ranges.h>
1017
1118#include " ../include/component.hxx"
1219#include " ../include/component_scheduler.hxx"
20+ #include " ../include/permissions.hxx"
1321
1422const std::set<std::string> ComponentScheduler::predeclared_variables = {
1523 " time" , " linear" , " units:inv_meters_cubed" , " units:eV" , " units:Tesla" ,
@@ -46,7 +54,8 @@ void topological_sort(const std::vector<std::set<size_t>>& dependencies, size_t
4654// / separated by colons in the path.
4755std::set<std::string> getParents (const std::string& name) {
4856 std::set<std::string> result;
49- size_t start = 0 , position = name.find (" :" , start);
57+ size_t start = 0 ;
58+ size_t position = name.find (" :" , start);
5059 while (position != std::string::npos) {
5160 result.insert (name.substr (0 , position));
5261 start = position + 1 ;
@@ -77,7 +86,8 @@ getVariableHierarchy(const std::vector<std::unique_ptr<Component>>& components)
7786 // Build up a set of all section/variable names which are definitely
7887 // read/written by components, and the sections which they imply
7988 // exist
80- std::set<std::string> unconditional_names, unconditional_sections;
89+ std::set<std::string> unconditional_names;
90+ std::set<std::string> unconditional_sections;
8191 for (const auto & component : components) {
8292 const Permissions& permissions = component->getPermissions ();
8393 for (const auto & [varname, _] :
@@ -191,8 +201,9 @@ setReadDependencies(const std::vector<std::unique_ptr<Component>>& components,
191201 // them
192202 for (const auto & [name, regions] :
193203 permissions.getVariablesWithPermission (permission)) {
194- if (ComponentScheduler::predeclared_variables.count (name) > 0 )
204+ if (ComponentScheduler::predeclared_variables.count (name) > 0 ) {
195205 continue ;
206+ }
196207 for (const auto & sub_name : expandVariableName (hierarchy, permissions, name)) {
197208 for (const auto & [region, _] : Permissions::fundamental_regions) {
198209 if ((regions & region) == region) {
@@ -211,25 +222,21 @@ setReadDependencies(const std::vector<std::unique_ptr<Component>>& components,
211222 return missing;
212223}
213224
214- // / Consumes a list of components and returns a new one that has been
215- // / topolgically sorted to ensure variables are written and read in
216- // / the right order.
217- std::vector<std::unique_ptr<Component>>
218- sortComponents (std::vector<std::unique_ptr<Component>>&& components) {
225+ // / Topologically sorts the list of components to ensure variables are
226+ // / written and read in the right order.
227+ void sortComponents (std::vector<std::unique_ptr<Component>>& components) {
219228 // Map between variable/section names specified by component
220229 // permissions and the variables they contain. In the case of
221230 // sections this is all variables within the section and any
222231 // sub-sections. Non-section viarables map to themselves.
223- std::map<std::string, std::set<std::string>> variable_hierarchy =
232+ const std::map<std::string, std::set<std::string>> variable_hierarchy =
224233 getVariableHierarchy (components);
225234
226235 // Get information on which components write each variable
227- std::map<Var, std::set<size_t >> nonfinal_writes = getPermissionComponentMap (
228- components, variable_hierarchy,
229- PermissionTypes::Write),
230- final_writes = getPermissionComponentMap (
231- components, variable_hierarchy,
232- PermissionTypes::Final);
236+ std::map<Var, std::set<size_t >> nonfinal_writes =
237+ getPermissionComponentMap (components, variable_hierarchy, PermissionTypes::Write);
238+ std::map<Var, std::set<size_t >> final_writes =
239+ getPermissionComponentMap (components, variable_hierarchy, PermissionTypes::Final);
233240
234241 // Object mapping between components (reprsented by the index of
235242 // that component in the `components` argument) and the components
@@ -244,7 +251,7 @@ sortComponents(std::vector<std::unique_ptr<Component>>&& components) {
244251 throw BoutException (
245252 " Multiple components have permission to make final write to variable {}" , var);
246253 }
247- for (size_t i : comp_indices) {
254+ for (const size_t i : comp_indices) {
248255 const auto item = nonfinal_writes.find (var);
249256 if (item != nonfinal_writes.end ()) {
250257 // Note that calling merge actually removes the items from the
@@ -283,8 +290,8 @@ sortComponents(std::vector<std::unique_ptr<Component>>&& components) {
283290 PermissionTypes::ReadIfSet, component_dependencies);
284291
285292 // Create ancillary variables for sorting process
286- std::vector<bool > processing (components.size (), false ),
287- processed (components.size (), false );
293+ std::vector<bool > processing (components.size (), false );
294+ std::vector< bool > processed (components.size (), false );
288295 std::vector<std::size_t > order;
289296
290297 // Perform the sort
@@ -300,7 +307,7 @@ sortComponents(std::vector<std::unique_ptr<Component>>&& components) {
300307 std::swap (result[i], components[order[i]]);
301308 }
302309
303- return result;
310+ components = std::move ( result) ;
304311}
305312
306313ComponentScheduler::ComponentScheduler (Options &scheduler_options,
@@ -372,7 +379,7 @@ ComponentScheduler::ComponentScheduler(Options &scheduler_options,
372379 component->declareAllSpecies (species);
373380 }
374381
375- components = sortComponents (std::move ( components) );
382+ sortComponents (components);
376383}
377384
378385std::unique_ptr<ComponentScheduler> ComponentScheduler::create (Options &scheduler_options,
0 commit comments