Skip to content
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

[MedApp] Trim Whitespace in Group Names #12147

Merged
merged 6 commits into from
Aug 26, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions applications/MedApplication/custom_io/med_model_part_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,26 @@ void CheckMEDErrorCode(const int ierr, const std::string& MEDCallName)
KRATOS_ERROR_IF(ierr < 0) << MEDCallName << " failed with error code " << ierr << "." << std::endl;
}

bool IsNotPaddingCharacter(std::string::value_type character) noexcept
{
return !(std::isspace(character) || character == '\0');
}

// The names in the MED-file often have trailing null-chars, which need to be removed
// this can otherwise make debugging very tricky
void RemoveTrailingNullChars(std::string& rInput)
void RemovePadding(std::string& rInput)
{
// Trime left
rInput.erase(rInput.begin(),
std::find_if(rInput.begin(),
rInput.end(),
IsNotPaddingCharacter));

// Trim right
rInput.erase(std::find_if(rInput.rbegin(),
rInput.rend(),
IsNotPaddingCharacter).base(),
rInput.end());
rInput.erase(std::find(rInput.begin(), rInput.end(), '\0'), rInput.end());
}
Copy link
Member

Choose a reason for hiding this comment

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

hmm this looks now like a pretty good addition to the StringUtilities

Could you please add it? I can review fast ;)

Not a blocker, we can also merge this one first.

Ideally this should have tests, but would not fit the current structure of the code, but once it is in the StringUtils

Copy link
Member

Choose a reason for hiding this comment

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

see #12225


Expand Down Expand Up @@ -330,7 +346,7 @@ auto GetGroupsByFamily(
// split the goup names
for (int i = 0; i < num_groups; i++) {
group_names[i] = c_group_names.substr(i * MED_LNAME_SIZE, MED_LNAME_SIZE);
RemoveTrailingNullChars(group_names[i]);
RemovePadding(group_names[i]);
}

groups_by_family[family_number] = std::move(group_names);
Expand Down Expand Up @@ -424,7 +440,7 @@ class MedModelPartIO::MedFileHandler
axis_unit.data());
CheckMEDErrorCode(err, "MEDmeshInfo");

RemoveTrailingNullChars(mMeshName);
RemovePadding(mMeshName);
mDimension = space_dim;
}

Expand Down Expand Up @@ -498,13 +514,14 @@ void MedModelPartIO::ReadModelPart(ModelPart& rThisModelPart)
const int dimension = mpFileHandler->GetDimension();

// read family info => Map from family number to group names aka SubModelPart names
const auto groups_by_fam = GetGroupsByFamily(
Copy link
Member

Choose a reason for hiding this comment

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

and this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it

auto groups_by_fam = GetGroupsByFamily(
mpFileHandler->GetFileHandle(),
mpFileHandler->GetMeshName());

// create SubModelPart hierarchy
for (const auto& r_map : groups_by_fam) {
for (const auto& r_smp_name : r_map.second) {
for (auto& r_map : groups_by_fam) {
for (auto& r_smp_name : r_map.second) {
Copy link
Member

Choose a reason for hiding this comment

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

why removing the const?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Leftovers ...
I fixed them now.

RemovePadding(r_smp_name);
Copy link
Member

Choose a reason for hiding this comment

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

I dont think this is still necessary, since the group names are already cleaned in L349 (see above)

if (!rThisModelPart.HasSubModelPart(r_smp_name)) {
rThisModelPart.CreateSubModelPart(r_smp_name);
}
Expand Down
Loading