Skip to content

Commit fff0581

Browse files
committed
Responded to some comments by @ZedThree
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.
1 parent 15f7e84 commit fff0581

File tree

3 files changed

+64
-54
lines changed

3 files changed

+64
-54
lines changed

include/component.hxx

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
#pragma once
2-
32
#ifndef HERMES_COMPONENT_H
43
#define HERMES_COMPONENT_H
54

6-
#include <bout/assert.hxx>
7-
#include <bout/bout_types.hxx>
8-
#include <bout/boutexception.hxx>
9-
#include <bout/field2d.hxx>
10-
#include <bout/field3d.hxx>
11-
#include <bout/generic_factory.hxx>
12-
#include <bout/options.hxx>
13-
#include <bout/unused.hxx>
14-
155
#include <cmath>
166
#include <initializer_list>
177
#include <map>
@@ -22,6 +12,15 @@
2212
#include <utility>
2313
#include <vector>
2414

15+
#include <bout/assert.hxx>
16+
#include <bout/bout_types.hxx>
17+
#include <bout/boutexception.hxx>
18+
#include <bout/field2d.hxx>
19+
#include <bout/field3d.hxx>
20+
#include <bout/generic_factory.hxx>
21+
#include <bout/options.hxx>
22+
#include <bout/unused.hxx>
23+
2524
#include "guarded_options.hxx"
2625
#include "permissions.hxx"
2726
#include "hermes_utils.hxx"
@@ -34,8 +33,10 @@ struct SpeciesInformation {
3433
SpeciesInformation(const std::vector<std::string>& electrons,
3534
const std::vector<std::string>& neutrals,
3635
const std::vector<std::string>& positive_ions,
37-
const std::vector<std::string> & negative_ions)
38-
: electrons(electrons), neutrals(neutrals), positive_ions(positive_ions), negative_ions(negative_ions), ions(positive_ions) {
36+
const std::vector<std::string>& negative_ions)
37+
: electrons(std::move(electrons)), neutrals(std::move(neutrals)),
38+
positive_ions(std::move(positive_ions)), negative_ions(std::move(negative_ions)),
39+
ions(std::move(positive_ions)) {
3940
finish_construction();
4041
}
4142

@@ -112,10 +113,9 @@ struct Component {
112113
/// @param name The species/name for this instance.
113114
/// @param options Component settings: options[name] are specific to this component
114115
/// @param solver Time-integration solver
115-
static std::unique_ptr<Component> create(const std::string &type, // The type to create
116-
const std::string &name, // The species/name for this instance
117-
Options &options, // Component settings: options[name] are specific to this component
118-
Solver *solver); // Time integration solver
116+
static std::unique_ptr<Component> create(const std::string& type,
117+
const std::string& name, Options& options,
118+
Solver* solver);
119119

120120
/// Tell the component the name of all species in the simulation, by type. It
121121
/// will use this information to substitute the following placeholders in
@@ -451,14 +451,15 @@ template<typename T>
451451
Options& add(Options& option, T value) {
452452
if (!option.isSet()) {
453453
return set(option, value);
454-
} else {
455-
try {
456-
return set(option, value + bout::utils::variantStaticCastOrThrow<Options::ValueType, T>(option.value));
457-
} catch (const std::bad_cast &e) {
458-
// Convert to a more useful error message
459-
throw BoutException("Could not convert {:s} to type {:s}",
460-
option.str(), typeid(T).name());
461-
}
454+
}
455+
try {
456+
return set(option, value
457+
+ bout::utils::variantStaticCastOrThrow<Options::ValueType, T>(
458+
option.value));
459+
} catch (const std::bad_cast& e) {
460+
// Convert to a more useful error message
461+
throw BoutException("Could not convert {:s} to type {:s}", option.str(),
462+
typeid(T).name());
462463
}
463464
}
464465

@@ -477,14 +478,15 @@ template<typename T>
477478
Options& subtract(Options& option, T value) {
478479
if (!option.isSet()) {
479480
return set(option, -value);
480-
} else {
481-
try {
482-
return set(option, bout::utils::variantStaticCastOrThrow<Options::ValueType, T>(option.value) - value);
483-
} catch (const std::bad_cast &e) {
484-
// Convert to a more useful error message
485-
throw BoutException("Could not convert {:s} to type {:s}",
486-
option.str(), typeid(T).name());
487-
}
481+
}
482+
try {
483+
return set(option,
484+
bout::utils::variantStaticCastOrThrow<Options::ValueType, T>(option.value)
485+
- value);
486+
} catch (const std::bad_cast& e) {
487+
// Convert to a more useful error message
488+
throw BoutException("Could not convert {:s} to type {:s}", option.str(),
489+
typeid(T).name());
488490
}
489491
}
490492

include/component_scheduler.hxx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#pragma once
2-
32
#ifndef COMPONENT_SCHEDULER_H
43
#define COMPONENT_SCHEDULER_H
54

6-
#include <vector>
75
#include <memory>
6+
#include <set>
7+
#include <string>
8+
#include <vector>
89

910
#include <bout/bout_types.hxx>
1011
#include <bout/options.hxx>

src/component_scheduler.cxx

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
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

1422
const 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.
4755
std::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

306313
ComponentScheduler::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

378385
std::unique_ptr<ComponentScheduler> ComponentScheduler::create(Options &scheduler_options,

0 commit comments

Comments
 (0)