Skip to content

Commit

Permalink
Merge branch 'ConstitutiveRelationsIDsList' into 'master'
Browse files Browse the repository at this point in the history
Enable assigning id for constitutive relations as a list

See merge request ogs/ogs!5123
  • Loading branch information
endJunction committed Oct 14, 2024
2 parents d68c85b + b8f6141 commit afdb5bd
Show file tree
Hide file tree
Showing 65 changed files with 399 additions and 297 deletions.
64 changes: 5 additions & 59 deletions Applications/ApplicationsLib/ProjectData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "GeoLib/Raster.h"
#include "InfoLib/CMakeInfo.h"
#include "MaterialLib/MPL/CreateMedium.h"
#include "MaterialLib/Utils/MediaCreation.h"
#include "MathLib/Curve/CreatePiecewiseLinearCurve.h"
#if defined(USE_LIS)
#include "MathLib/LinAlg/EigenLis/LinearSolverOptionsParser.h"
Expand Down Expand Up @@ -356,61 +357,6 @@ std::vector<GeoLib::NamedRaster> readRasters(
// }
//}

std::vector<int> parseMaterialIdString(
std::string const& material_id_string,
MeshLib::PropertyVector<int> const* const material_ids)
{
if (material_id_string == "*")
{
if (material_ids == nullptr)
{
OGS_FATAL(
"MaterialIDs property is not defined in the mesh but it is "
"required to parse '*' definition.");
}

std::vector<int> material_ids_of_this_medium =
*material_ids |
ranges::views::adjacent_remove_if(std::equal_to<>()) |
ranges::to_vector;
BaseLib::makeVectorUnique(material_ids_of_this_medium);
DBUG("Catch all medium definition for material ids {}.",
fmt::join(material_ids_of_this_medium, ", "));
return material_ids_of_this_medium;
}

// Usual case of ids separated by comma.
return BaseLib::splitMaterialIdString(material_id_string);
}

template <typename T, typename CreateMedium>
requires std::convertible_to<decltype(std::declval<CreateMedium>()(
std::declval<int>())),
std::shared_ptr<T>>
void createMediumForId(int const id,
std::map<int, std::shared_ptr<T>>& _media,
std::vector<int> const& material_ids_of_this_medium,
CreateMedium&& create_medium)
{
if (_media.find(id) != end(_media))
{
OGS_FATAL(
"Multiple media were specified for the same material id "
"'{:d}'. Keep in mind, that if no material id is "
"specified, it is assumed to be 0 by default.",
id);
}

if (id == material_ids_of_this_medium[0])
{
_media[id] = create_medium(id);
}
else
{
_media[id] = _media[material_ids_of_this_medium[0]];
}
}

} // namespace

ProjectData::ProjectData() = default;
Expand Down Expand Up @@ -630,13 +576,13 @@ void ProjectData::parseMedia(
medium_config.getConfigAttribute<std::string>("id", "0");

std::vector<int> const material_ids_of_this_medium =
parseMaterialIdString(material_id_string,
materialIDs(*_mesh_vec[0]));
MaterialLib::parseMaterialIdString(material_id_string,
materialIDs(*_mesh_vec[0]));

for (auto const& id : material_ids_of_this_medium)
{
createMediumForId(id, _media, material_ids_of_this_medium,
create_medium);
MaterialLib::createMediumForId(
id, _media, material_ids_of_this_medium, create_medium);
}
}

Expand Down
52 changes: 0 additions & 52 deletions BaseLib/StringTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,56 +132,4 @@ std::string getUniqueName(std::vector<std::string> const& existing_names,
}
return result_name;
}

std::vector<int> splitMaterialIdString(std::string const& material_id_string)
{
auto const material_ids_strings =
BaseLib::splitString(material_id_string, ',');

std::vector<int> material_ids;
for (auto& mid_str : material_ids_strings)
{
std::size_t num_chars_processed = 0;
int material_id;
try
{
material_id = std::stoi(mid_str, &num_chars_processed);
}
catch (std::invalid_argument&)
{
OGS_FATAL(
"Could not parse material ID from '{}' to a valid "
"integer.",
mid_str);
}
catch (std::out_of_range&)
{
OGS_FATAL(
"Could not parse material ID from '{}'. The integer value "
"of the given string exceeds the permitted range.",
mid_str);
}

if (num_chars_processed != mid_str.size())
{
// Not the whole string has been parsed. Check the rest.
if (auto const it = std::find_if_not(
begin(mid_str) + num_chars_processed, end(mid_str),
[](unsigned char const c) { return std::isspace(c); });
it != end(mid_str))
{
OGS_FATAL(
"Could not parse material ID from '{}'. Please "
"separate multiple material IDs by comma only. "
"Invalid character: '{}' at position {}.",
mid_str, *it, distance(begin(mid_str), it));
}
}

material_ids.push_back(material_id);
};

return material_ids;
}

} // end namespace BaseLib
5 changes: 0 additions & 5 deletions BaseLib/StringTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,4 @@ std::string randomString(std::size_t length);
//! Append '-' and a number such that the name is unique.
std::string getUniqueName(std::vector<std::string> const& existing_names,
std::string const& input_name);

/// Parses a comma separated list of integers.
/// Such lists occur in the medium definition in the OGS prj file.
/// Error messages in this function refer to this specific purpose.
std::vector<int> splitMaterialIdString(std::string const& material_id_string);
} // end namespace BaseLib
3 changes: 2 additions & 1 deletion MaterialLib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_subdirectory(SolidModels)
add_subdirectory(FractureModels)
add_subdirectory(Utils)

# Source files
get_source_files(SOURCES)
Expand Down Expand Up @@ -42,7 +43,7 @@ ogs_add_library(

target_link_libraries(
MaterialLib PUBLIC BaseLib Eigen3::Eigen MaterialLib_SolidModels
MaterialLib_FractureModels
MaterialLib_FractureModels MaterialLib_Utils
PRIVATE MathLib MeshLib ParameterLib exprtk Boost::math
)

Expand Down
4 changes: 2 additions & 2 deletions MaterialLib/SolidModels/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ get_source_files(SOURCES)

ogs_add_library(MaterialLib_SolidModels ${SOURCES})
target_link_libraries(
MaterialLib_SolidModels PUBLIC MathLib Boost::math PRIVATE ParameterLib
NumLib
MaterialLib_SolidModels PUBLIC MathLib Boost::math
PRIVATE ParameterLib MaterialLib_Utils NumLib
)

if(OGS_USE_MFRONT)
Expand Down
10 changes: 7 additions & 3 deletions MaterialLib/SolidModels/CreateConstitutiveRelation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,33 +106,37 @@ createConstitutiveRelation(

template <int DisplacementDim>
std::map<int,
std::unique_ptr<MaterialLib::Solids::MechanicsBase<DisplacementDim>>>
std::shared_ptr<MaterialLib::Solids::MechanicsBase<DisplacementDim>>>
createConstitutiveRelations(
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
std::optional<ParameterLib::CoordinateSystem> const&
local_coordinate_system,
MeshLib::PropertyVector<int> const* const material_ids,
BaseLib::ConfigTree const& config)
{
return createConstitutiveRelationsGeneric<
MaterialLib::Solids::MechanicsBase<DisplacementDim>>(
parameters,
local_coordinate_system,
material_ids,
config,
createConstitutiveRelation<DisplacementDim>);
}

template std::map<int, std::unique_ptr<MaterialLib::Solids::MechanicsBase<2>>>
template std::map<int, std::shared_ptr<MaterialLib::Solids::MechanicsBase<2>>>
createConstitutiveRelations<2>(
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
std::optional<ParameterLib::CoordinateSystem> const&
local_coordinate_system,
MeshLib::PropertyVector<int> const* const material_ids,
BaseLib::ConfigTree const& config);

template std::map<int, std::unique_ptr<MaterialLib::Solids::MechanicsBase<3>>>
template std::map<int, std::shared_ptr<MaterialLib::Solids::MechanicsBase<3>>>
createConstitutiveRelations<3>(
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
std::optional<ParameterLib::CoordinateSystem> const&
local_coordinate_system,
MeshLib::PropertyVector<int> const* const material_ids,
BaseLib::ConfigTree const& config);
} // namespace Solids
} // namespace MaterialLib
14 changes: 11 additions & 3 deletions MaterialLib/SolidModels/CreateConstitutiveRelation.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ namespace BaseLib
{
class ConfigTree;
}
namespace MeshLib
{
template <typename T>
class PropertyVector;
}

namespace ParameterLib
{
Expand Down Expand Up @@ -58,27 +63,30 @@ createConstitutiveRelation(

template <int DisplacementDim>
std::map<int,
std::unique_ptr<MaterialLib::Solids::MechanicsBase<DisplacementDim>>>
std::shared_ptr<MaterialLib::Solids::MechanicsBase<DisplacementDim>>>
createConstitutiveRelations(
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
std::optional<ParameterLib::CoordinateSystem> const&
local_coordinate_system,
MeshLib::PropertyVector<int> const* const material_ids,
BaseLib::ConfigTree const& config);

extern template std::map<int,
std::unique_ptr<MaterialLib::Solids::MechanicsBase<2>>>
std::shared_ptr<MaterialLib::Solids::MechanicsBase<2>>>
createConstitutiveRelations<2>(
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
std::optional<ParameterLib::CoordinateSystem> const&
local_coordinate_system,
MeshLib::PropertyVector<int> const* const material_ids,
BaseLib::ConfigTree const& config);

extern template std::map<int,
std::unique_ptr<MaterialLib::Solids::MechanicsBase<3>>>
std::shared_ptr<MaterialLib::Solids::MechanicsBase<3>>>
createConstitutiveRelations<3>(
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
std::optional<ParameterLib::CoordinateSystem> const&
local_coordinate_system,
MeshLib::PropertyVector<int> const* const material_ids,
BaseLib::ConfigTree const& config);
} // namespace Solids
} // namespace MaterialLib
46 changes: 29 additions & 17 deletions MaterialLib/SolidModels/CreateConstitutiveRelationsGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
#pragma once

#include "BaseLib/ConfigTree.h"
#include "MaterialLib/Utils/MediaCreation.h"
#include "MeshLib/PropertyVector.h"
#include "ParameterLib/Parameter.h"

namespace MaterialLib::Solids
{
template <typename SolidConstitutiveRelation>
std::map<int, std::unique_ptr<SolidConstitutiveRelation>>
std::map<int, std::shared_ptr<SolidConstitutiveRelation>>
createConstitutiveRelationsGeneric(
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
std::optional<ParameterLib::CoordinateSystem> const&
local_coordinate_system,
MeshLib::PropertyVector<int> const* const material_ids,
BaseLib::ConfigTree const& config,
std::unique_ptr<SolidConstitutiveRelation> (*create_constitutive_relation)(
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const&,
Expand All @@ -31,31 +34,40 @@ createConstitutiveRelationsGeneric(
//! \ogs_file_param{material__solid__constitutive_relation}
config.getConfigSubtreeList("constitutive_relation");

std::map<int, std::unique_ptr<SolidConstitutiveRelation>>
std::map<int, std::shared_ptr<SolidConstitutiveRelation>>
constitutive_relations;

for (auto const& constitutive_relation_config :
constitutive_relation_configs)
{
int const material_id =
auto create = [&create_constitutive_relation,
&parameters,
&local_coordinate_system,
&constitutive_relation_config](int const /*id*/)
{
return create_constitutive_relation(parameters,
local_coordinate_system,
constitutive_relation_config);
};

auto const material_id_string =
//! \ogs_file_attr{material__solid__constitutive_relation__id}
constitutive_relation_config.getConfigAttribute<int>("id", 0);
constitutive_relation_config.getConfigAttribute<std::string>("id",
"0");

if (constitutive_relations.find(material_id) !=
constitutive_relations.end())
auto const material_ids_of_this_constitutive_relation =
MaterialLib::parseMaterialIdString(material_id_string,
material_ids);

for (auto const& material_id :
material_ids_of_this_constitutive_relation)
{
OGS_FATAL(
"Multiple constitutive relations were specified for the same "
"material id {:d}. Keep in mind, that if no material id is "
"specified, it is assumed to be 0 by default.",
material_id);
MaterialLib::createMediumForId(
material_id,
constitutive_relations,
material_ids_of_this_constitutive_relation,
create);
}

constitutive_relations.emplace(
material_id,
create_constitutive_relation(parameters,
local_coordinate_system,
constitutive_relation_config));
}

DBUG("Found {:d} constitutive relations.", constitutive_relations.size());
Expand Down
4 changes: 4 additions & 0 deletions MaterialLib/Utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
get_source_files(SOURCES)

ogs_add_library(MaterialLib_Utils ${SOURCES})
target_link_libraries(MaterialLib_Utils PRIVATE BaseLib MeshLib)
Loading

0 comments on commit afdb5bd

Please sign in to comment.