Skip to content

Commit db44717

Browse files
authored
Merge pull request #11485 from KratosMultiphysics/hdf5/update/data_value_container_io
[HDF5] Update - 3 - Update data_value_container_io
2 parents 3578429 + 0bd11cd commit db44717

14 files changed

+477
-355
lines changed
Lines changed: 165 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,194 @@
1-
#include "custom_io/hdf5_data_value_container_io.h"
1+
// | / |
2+
// ' / __| _` | __| _ \ __|
3+
// . \ | ( | | ( |\__ `
4+
// _|\_\_| \__,_|\__|\___/ ____/
5+
// Multi-Physics
6+
//
7+
// License: BSD License
8+
// license: HDF5Application/license.txt
9+
//
10+
// Main author: Michael Andre, https://github.com/msandre
11+
// Suneth Warnakulasuriya
12+
//
13+
14+
// System includes
15+
16+
// Project includes
17+
#include "includes/constitutive_law.h"
18+
#include "utilities/data_type_traits.h"
219

3-
#include "custom_utilities/registered_component_lookup.h"
20+
// Application includes
21+
22+
// Include base h
23+
#include "custom_io/hdf5_data_value_container_io.h"
424

525
namespace Kratos
626
{
27+
namespace HDF5Utilities
28+
{
729

8-
template <typename TVariable>
9-
class ReadVariableFunctor
30+
template<class TDataType>
31+
bool ReadComponent(
32+
HDF5::File& rFile,
33+
const std::string& rVariableName,
34+
const std::string& rPrefix,
35+
DataValueContainer& rData)
1036
{
11-
public:
12-
void operator()(TVariable const& rVariable,
13-
HDF5::File& rFile,
14-
std::string const& rPrefix,
15-
DataValueContainer& rData)
16-
{
17-
typename TVariable::Type value;
18-
rFile.ReadAttribute(rPrefix + "/DataValues", rVariable.Name(), value);
19-
rData[rVariable] = value;
37+
KRATOS_TRY
38+
39+
if (KratosComponents<Variable<TDataType>>::Has(rVariableName)) {
40+
const auto& r_variable = KratosComponents<Variable<TDataType>>::Get(rVariableName);
41+
if constexpr(std::is_same_v<TDataType, ConstitutiveLaw::Pointer>) {
42+
// reading the constitutive law name
43+
std::string constitutive_law_name;
44+
rFile.ReadAttribute(rPrefix + "/DataValues", rVariableName, constitutive_law_name);
45+
KRATOS_ERROR_IF_NOT(KratosComponents<ConstitutiveLaw>::Has(constitutive_law_name)) << "Kratos components missing \"" << constitutive_law_name << "\"" << std::endl;
46+
47+
// TODO: The constitutive law cannot be constructed because, the constitutive law does not retain
48+
// the construction mechanism it used in creating it (especially in composites, where the composite
49+
// the structure of composite is given in json).
50+
// auto p_constitutive_law = KratosComponents<ConstitutiveLaw>::Get(constitutive_law_name).Create(cl_parameters, rProperty);
51+
// rData[r_variable] = p_constitutive_law;
52+
} else {
53+
TDataType value{};
54+
rFile.ReadAttribute(rPrefix + "/DataValues", rVariableName, value);
55+
rData[r_variable] = value;
56+
}
57+
return true;
58+
} else {
59+
return false;
2060
}
21-
};
2261

23-
template <typename TVariable>
24-
class WriteVariableFunctor
62+
KRATOS_CATCH("");
63+
}
64+
65+
template<class TDataType>
66+
bool WriteComponent(
67+
HDF5::File& rFile,
68+
const std::string& rVariableName,
69+
const std::string& rPrefix,
70+
const DataValueContainer& rData)
2571
{
26-
public:
27-
void operator()(TVariable const& rVariable,
28-
HDF5::File& rFile,
29-
std::string const& rPrefix,
30-
DataValueContainer const& rData)
31-
{
32-
rFile.WriteAttribute(rPrefix + "/DataValues", rVariable.Name(), rData[rVariable]);
72+
KRATOS_TRY
73+
74+
if (KratosComponents<Variable<TDataType>>::Has(rVariableName)) {
75+
const auto& r_variable = KratosComponents<Variable<TDataType>>::Get(rVariableName);
76+
const auto& r_value = rData[r_variable];
77+
if constexpr(std::is_same_v<TDataType, ConstitutiveLaw::Pointer>) {
78+
auto components_cl = KratosComponents<ConstitutiveLaw>::GetComponents();
79+
std::string cl_name = "";
80+
for (const auto& comp_cl : components_cl) {
81+
if (r_value->HasSameType(r_value.get(), comp_cl.second)) {
82+
cl_name = comp_cl.first;
83+
break;
84+
}
85+
}
86+
KRATOS_ERROR_IF(cl_name == "") << "Kratos components missing \"" << r_value << "\"" << std::endl;
87+
rFile.WriteAttribute(rPrefix + "/DataValues", rVariableName, cl_name);
88+
} else {
89+
rFile.WriteAttribute(rPrefix + "/DataValues", rVariableName, r_value);
90+
}
91+
return true;
92+
} else {
93+
return false;
3394
}
34-
};
95+
96+
KRATOS_CATCH("");
97+
}
98+
99+
template<class... TDataTypes>
100+
void Read(
101+
HDF5::File& rFile,
102+
const std::string& rVariableName,
103+
const std::string& rPrefix,
104+
DataValueContainer& rData)
105+
{
106+
KRATOS_TRY
107+
108+
const bool is_read = (... || ReadComponent<TDataTypes>(rFile, rVariableName, rPrefix, rData));
109+
110+
KRATOS_ERROR_IF_NOT(is_read) << "The variable \"" << rVariableName << "\" not found in registered variables list.";
111+
112+
KRATOS_CATCH("");
113+
}
114+
115+
template<class... TDataTypes>
116+
void Write(
117+
HDF5::File& rFile,
118+
const std::string& rVariableName,
119+
const std::string& rPrefix,
120+
const DataValueContainer& rData)
121+
{
122+
KRATOS_TRY
123+
124+
const bool is_written = (... || WriteComponent<TDataTypes>(rFile, rVariableName, rPrefix, rData));
125+
126+
KRATOS_ERROR_IF_NOT(is_written) << "The variable \"" << rVariableName << "\" not found in registered variables list.";
127+
128+
KRATOS_CATCH("");
129+
}
130+
131+
} // namespace HDF5Utilities
35132

36133
namespace HDF5
37134
{
38135
namespace Internals
39136
{
40137

41-
void ReadDataValueContainer(File& rFile, std::string const& rPrefix, DataValueContainer& rData)
138+
void ReadDataValueContainer(
139+
File& rFile,
140+
const std::string& rPrefix,
141+
DataValueContainer& rData)
42142
{
43143
KRATOS_TRY;
44-
std::vector<std::string> attr_names = rFile.GetAttributeNames(rPrefix + "/DataValues");
45-
for (const auto& r_name : attr_names)
46-
RegisteredComponentLookup<Variable<int>, Variable<double>,
47-
Variable<Vector<double>>, Variable<Matrix<double>>>(r_name)
48-
.Execute<ReadVariableFunctor>(rFile, rPrefix, rData);
144+
145+
const auto& attr_names = rFile.GetAttributeNames(rPrefix + "/DataValues");
146+
147+
for (const auto& r_name : attr_names) {
148+
HDF5Utilities ::Read<
149+
ConstitutiveLaw::Pointer,
150+
bool,
151+
int,
152+
double,
153+
std::string,
154+
array_1d<double, 3>,
155+
array_1d<double, 4>,
156+
array_1d<double, 6>,
157+
array_1d<double, 9>,
158+
Kratos::Vector,
159+
Kratos::Matrix>(rFile, r_name, rPrefix, rData);
160+
}
161+
49162
KRATOS_CATCH("Path: \"" + rPrefix + "/DataValues\".");
50163
}
51164

52-
void WriteDataValueContainer(File& rFile, std::string const& rPrefix, DataValueContainer const& rData)
165+
void WriteDataValueContainer(
166+
File& rFile,
167+
const std::string& rPrefix,
168+
const DataValueContainer& rData)
53169
{
54170
KRATOS_TRY;
171+
55172
rFile.AddPath(rPrefix + "/DataValues");
56-
for (auto it = rData.begin(); it != rData.end(); ++it)
57-
try
58-
{
59-
RegisteredComponentLookup<Variable<int>, Variable<double>, Variable<Vector<double>>,
60-
Variable<Matrix<double>>>(it->first->Name())
61-
.Execute<WriteVariableFunctor>(rFile, rPrefix, rData);
62-
}
63-
catch (Exception& e)
64-
{
65-
}
173+
174+
for (auto it = rData.begin(); it != rData.end(); ++it) {
175+
HDF5Utilities ::Write<
176+
ConstitutiveLaw::Pointer,
177+
bool,
178+
int,
179+
double,
180+
std::string,
181+
array_1d<double, 3>,
182+
array_1d<double, 4>,
183+
array_1d<double, 6>,
184+
array_1d<double, 9>,
185+
Kratos::Vector,
186+
Kratos::Matrix>(rFile, it->first->Name(), rPrefix, rData);
187+
}
188+
66189
KRATOS_CATCH("Path: \"" + rPrefix + "/DataValues\".");
67190
}
68191

69192
} // namespace Internals.
70193
} // namespace HDF5.
71-
} // namespace Kratos.
194+
} // namespace Kratos.

applications/HDF5Application/custom_io/hdf5_data_value_container_io.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// _|\_\_| \__,_|\__|\___/ ____/
55
// Multi-Physics
66
//
7-
// License: BSD License
8-
// license: HDF5Application/license.txt
7+
// License: BSD License
8+
// license: HDF5Application/license.txt
99
//
1010
// Main author: Michael Andre, https://github.com/msandre
1111
//
@@ -14,8 +14,7 @@
1414
* @brief Methods for storing and retrieving a data value container in an HDF5 file.
1515
*/
1616

17-
#if !defined(KRATOS_HDF5_DATA_VALUE_CONTAINER_IO_H_INCLUDED)
18-
#define KRATOS_HDF5_DATA_VALUE_CONTAINER_IO_H_INCLUDED
17+
#pragma once
1918

2019
// System includes
2120
#include <string>
@@ -37,13 +36,17 @@ namespace Internals
3736
///@addtogroup HDF5Application
3837
///@{
3938

40-
void KRATOS_API(HDF5_APPLICATION) ReadDataValueContainer(File& rFile, std::string const& rPrefix, DataValueContainer& rData);
39+
void KRATOS_API(HDF5_APPLICATION) ReadDataValueContainer(
40+
File& rFile,
41+
const std::string& rPrefix,
42+
DataValueContainer& rData);
4143

42-
void KRATOS_API(HDF5_APPLICATION) WriteDataValueContainer(File& rFile, std::string const& rPrefix, DataValueContainer const& rData);
44+
void KRATOS_API(HDF5_APPLICATION) WriteDataValueContainer(
45+
File& rFile,
46+
const std::string& rPrefix,
47+
const DataValueContainer& rData);
4348

44-
///@} addtogroup
49+
///@}
4550
} // namespace Internals.
4651
} // namespace HDF5.
47-
} // namespace Kratos.
48-
49-
#endif // KRATOS_HDF5_DATA_VALUE_CONTAINER_IO_H_INCLUDED defined
52+
} // namespace Kratos.

applications/HDF5Application/custom_io/hdf5_file.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,9 @@ bool File::HasDataType(const std::string& rPath) const
816816
KRATOS_HDF5_CALL(H5Tclose, dtype_id)
817817
KRATOS_HDF5_CALL(H5Dclose, dset_id)
818818

819-
if constexpr(std::is_same_v<TDataType, int>) {
819+
if constexpr(std::is_same_v<TDataType, bool>) {
820+
return type == H5T_INTEGER;
821+
} else if constexpr(std::is_same_v<TDataType, int>) {
820822
return type == H5T_INTEGER;
821823
} else if constexpr(std::is_same_v<TDataType, double>) {
822824
return type == H5T_FLOAT;
@@ -1165,6 +1167,7 @@ template KRATOS_API(HDF5_APPLICATION) bool File::HasDataType<double>(const std::
11651167

11661168
#endif
11671169

1170+
KRATOS_HDF5_FILE_ATTRIBUTE_METHOD_INSTANTIATION(bool);
11681171
KRATOS_HDF5_FILE_ATTRIBUTE_METHOD_INSTANTIATION(int);
11691172
KRATOS_HDF5_FILE_ATTRIBUTE_METHOD_INSTANTIATION(double);
11701173
KRATOS_HDF5_FILE_ATTRIBUTE_METHOD_INSTANTIATION(std::string);
@@ -1177,6 +1180,7 @@ KRATOS_HDF5_FILE_ATTRIBUTE_METHOD_INSTANTIATION(array_1d<double, 4>);
11771180
KRATOS_HDF5_FILE_ATTRIBUTE_METHOD_INSTANTIATION(array_1d<double, 6>);
11781181
KRATOS_HDF5_FILE_ATTRIBUTE_METHOD_INSTANTIATION(array_1d<double, 9>);
11791182

1183+
KRATOS_HDF5_FILE_DATA_SET_METHOD_INSTANTIATION(Vector<bool>);
11801184
KRATOS_HDF5_FILE_DATA_SET_METHOD_INSTANTIATION(Vector<char>);
11811185
KRATOS_HDF5_FILE_DATA_SET_METHOD_INSTANTIATION(Vector<int>);
11821186
KRATOS_HDF5_FILE_DATA_SET_METHOD_INSTANTIATION(Vector<double>);

applications/HDF5Application/custom_utilities/data_type_utilities.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ template <class TDataType> hid_t GetPrimitiveH5Type()
4141
{
4242
using primitive_type = typename DataTypeTraits<TDataType>::PrimitiveType;
4343

44-
if constexpr(std::is_same_v<primitive_type, char>) {
44+
if constexpr(std::is_same_v<primitive_type, bool>) {
45+
return H5T_NATIVE_HBOOL;
46+
} else if constexpr(std::is_same_v<primitive_type, char>) {
4547
return H5T_NATIVE_CHAR;
4648
} else if constexpr(std::is_same_v<primitive_type, int>) {
4749
return H5T_NATIVE_INT;

0 commit comments

Comments
 (0)