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"
2
19
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"
4
24
5
25
namespace Kratos
6
26
{
27
+ namespace HDF5Utilities
28
+ {
7
29
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)
10
36
{
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 ;
20
60
}
21
- };
22
61
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)
25
71
{
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 ;
33
94
}
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
35
132
36
133
namespace HDF5
37
134
{
38
135
namespace Internals
39
136
{
40
137
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)
42
142
{
43
143
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
+
49
162
KRATOS_CATCH (" Path: \" " + rPrefix + " /DataValues\" ." );
50
163
}
51
164
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)
53
169
{
54
170
KRATOS_TRY;
171
+
55
172
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
+
66
189
KRATOS_CATCH (" Path: \" " + rPrefix + " /DataValues\" ." );
67
190
}
68
191
69
192
} // namespace Internals.
70
193
} // namespace HDF5.
71
- } // namespace Kratos.
194
+ } // namespace Kratos.
0 commit comments