-
Notifications
You must be signed in to change notification settings - Fork 241
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
Improve functions to retrieve fields of certain type from introspection #5643
Merged
bangerth
merged 2 commits into
geodynamics:main
from
gassmoeller:function_to_retrieve_fields_of_type
May 30, 2024
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,17 +57,22 @@ namespace aspect | |
*/ | ||
enum Type | ||
{ | ||
chemical_composition, | ||
stress, | ||
strain, | ||
grain_size, | ||
porosity, | ||
density, | ||
entropy, | ||
generic, | ||
unspecified | ||
chemical_composition = 0, | ||
stress = 1, | ||
strain = 2, | ||
grain_size = 3, | ||
porosity = 4, | ||
density = 5, | ||
entropy = 6, | ||
generic = 7, | ||
unspecified = 8 | ||
} type; | ||
|
||
/** | ||
* The number of different types defined in Type. | ||
*/ | ||
constexpr static unsigned int n_types = 9; | ||
|
||
/** | ||
* This function translates an input string into the | ||
* available enum options for the type of compositional field. | ||
|
@@ -480,20 +485,29 @@ namespace aspect | |
/** | ||
* A function that returns the names of | ||
* compositional fields that correspond to chemical compositions. | ||
* | ||
* This function is shorthand for | ||
* get_names_for_fields_of_type(CompositionalFieldDescription::chemical_composition). | ||
*/ | ||
const std::vector<std::string> & | ||
chemical_composition_field_names () const; | ||
|
||
/** | ||
* A function that returns the indices of | ||
* compositional fields that correspond to chemical compositions. | ||
* | ||
* This function is shorthand for | ||
* get_indices_for_fields_of_type(CompositionalFieldDescription::chemical_composition). | ||
*/ | ||
const std::vector<unsigned int> & | ||
chemical_composition_field_indices () const; | ||
|
||
/** | ||
* A function that returns the number of | ||
* compositional fields that correspond to chemical compositions. | ||
* | ||
* This function is shorthand for | ||
* get_number_of_fields_of_type(CompositionalFieldDescription::chemical_composition). | ||
*/ | ||
unsigned int | ||
n_chemical_composition_fields () const; | ||
|
@@ -509,7 +523,6 @@ namespace aspect | |
bool | ||
composition_type_exists (const CompositionalFieldDescription::Type &type) const; | ||
|
||
|
||
/** | ||
* A function that gets the type of a compositional field as an input | ||
* parameter and returns the index of the first compositional field of | ||
|
@@ -522,7 +535,6 @@ namespace aspect | |
unsigned int | ||
find_composition_type (const CompositionalFieldDescription::Type &type) const; | ||
|
||
|
||
/** | ||
* A function that gets the name of a compositional field as an input | ||
* parameter and returns if the compositional field is used in this | ||
|
@@ -538,14 +550,14 @@ namespace aspect | |
* Get the indices of the compositional fields which are of a | ||
* particular type (chemical composition, porosity, etc.). | ||
*/ | ||
const std::vector<unsigned int> | ||
const std::vector<unsigned int> & | ||
get_indices_for_fields_of_type (const CompositionalFieldDescription::Type &type) const; | ||
|
||
/** | ||
* Get the names of the compositional fields which are of a | ||
* particular type (chemical composition, porosity, etc.). | ||
*/ | ||
const std::vector<std::string> | ||
const std::vector<std::string> & | ||
Comment on lines
-541
to
+560
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. That was a bug to begin with. The fact that we marked these return types as |
||
get_names_for_fields_of_type (const CompositionalFieldDescription::Type &type) const; | ||
|
||
/** | ||
|
@@ -580,26 +592,18 @@ namespace aspect | |
std::vector<CompositionalFieldDescription> composition_descriptions; | ||
|
||
/** | ||
* A vector that stores the names of the compositional fields | ||
* corresponding to chemical compositions that will | ||
* be used in the simulation. | ||
*/ | ||
std::vector<std::string> chemical_composition_names; | ||
|
||
/** | ||
* A vector that stores the indices of the compositional fields | ||
* corresponding to chemical compositions that will | ||
* be used in the simulation. | ||
* A vector of vectors of composition names that stores the | ||
* names of the compositional fields corresponding to each field type | ||
* given in CompositionalFieldDescription. | ||
*/ | ||
std::vector<unsigned int> chemical_composition_indices; | ||
std::vector<std::vector<std::string>> composition_names_for_type; | ||
|
||
/** | ||
* The number of compositional fields | ||
* that correspond to chemical compositions that will | ||
* be used in the simulation. | ||
* A vector of vectors of composition indices that stores the | ||
* indices of the compositional fields corresponding to each field type | ||
* given in CompositionalFieldDescription. | ||
*/ | ||
const unsigned int n_chemical_compositions; | ||
|
||
std::vector<std::vector<unsigned int>> composition_indices_for_type; | ||
}; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do you actually make use of these numeric values? My preference is to treat
enum
s as symbolic names independent of any numerical representation.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.
Yes, I use these values to index into an std::vector below. That is why I explicitly specified them to make sure I can uniquely do the indexing. If I would just treat the
enum
s as symbolic names I would create a utility function with aswitch
statement to return anunsigned int
depending on type. However, I think according to the standard I am actually allowed to implicitly cast the enums to the unsigned ints.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.
I just dislike this from a conceptual perspective. It requires keeping track of things in different places.
What would you think of keeping the
enum
as just a list of symbolic names, and storing data instead of a fixed-size vector into astd::map<enum_type, value_type>
?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.
I am not happy about that solution because I do not know the performance implications. The cost of a lookup into a
std::map
already suprised us in the particle code before and the reason for this PR was to allow this lookup to be fast enough to be done multiple times per cell. What do you think about the alternative of keeping theenum
as just a list of names, and having a helper function in an anonymous namespace in introspection.cc that is essentially:This way no code outside of Introspection can see that we translate the enum values into indices of a
std::vector
. However, it requires to always change the function if we change the enum. The beaty of the current solution is that no other code is affected if we add another enum option, everything will just work like before.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.
But you still have to have two places that you keep in sync: The translation (whether in a function or by providing initializers) and the place where you store the number of
enum
elements.I can be ok with the current state. Let's not make it more difficult!