Skip to content

Commit d650353

Browse files
authored
Merge pull request #214 from thelfer/209-add-basic-support-for-saverestore-operations-in-materialdatamanager
209 add basic support for saverestore operations in materialdatamanager
2 parents ab2192d + c34c736 commit d650353

24 files changed

+1678
-75
lines changed

CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ if(enable-julia-bindings)
111111
set(CMAKE_CXX_STANDARD "${_CMAKE_CXX_STANDARD}")
112112
endif(enable-julia-bindings)
113113

114+
# HDF5 support (optional)
115+
116+
option(enable-hdf5-support "enable HDF5 support" OFF)
117+
option(enable-hdf5-automatic-support "if set, try to support HDF5, even if not explicitly requested by enable-hdf5-support" ON)
118+
if(enable-hdf5-support)
119+
find_package(HDF5 REQUIRED COMPONENTS CXX)
120+
else(enable-hdf5-support)
121+
if(enable-hdf5-automatic-support)
122+
find_package(HDF5 COMPONENTS CXX)
123+
endif(enable-hdf5-automatic-support)
124+
endif(enable-hdf5-support)
125+
if(HDF5_FOUND)
126+
set(MGIS_HDF5_SUPPORT ON)
127+
MESSAGE(STATUS "hdf5 headers : " ${HDF5_INCLUDE_DIRS})
128+
MESSAGE(STATUS "hdf5 librairies : " ${HDF5_LIBRARIES})
129+
MESSAGE(STATUS "hdf5 definitions : " ${HDF5_DEFINITIONS})
130+
else(HDF5_FOUND)
131+
set(MGIS_HDF5_SUPPORT OFF)
132+
endif(HDF5_FOUND)
133+
114134
# summary
115135

116136
if(enable-c-bindings)
@@ -129,6 +149,10 @@ if(enable-fenics-bindings)
129149
message(STATUS "FEniCS bindings support enabled")
130150
endif(enable-fenics-bindings)
131151

152+
if(MGIS_HDF5_SUPPORT)
153+
message(STATUS "HDF5 support enabled")
154+
endif(MGIS_HDF5_SUPPORT)
155+
132156
find_package(Threads)
133157
# if(MINGW)
134158
# file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/thread-mingw.cxx"

INSTALL-cmake.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ Options
6363
- `enable-exceptions`: use exceptions to report contract violation and error reporting.
6464
By default, contract violation leads to abort the program.
6565
- `enable-parallel-stl-algorithms`: by default, STL algorithms are used if available
66+
- `enable-hdf5-support`: enable HDF5 support for save/restore
67+
operations. Note that if this support is not explicitely requested,
68+
`MGIS` still tries by default to support `HDF5` but configuration will
69+
not fail if the `HDF5` library is not found. See
70+
`enable-hdf5-automatic-support` for details.
71+
- `enable-hdf5-automatic-support`: if set, `MGIS` tries by default to
72+
support `HDF5` even if enable-hdf5-support` is not set.
6673

6774
`cmake` usefull variables
6875
=======================

bindings/python/src/Variable.cxx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ void declareVariable(pybind11::module_& m) {
104104
// free functions
105105
m.def("getVariable", getVariableByString,
106106
pybind11::return_value_policy::reference);
107-
m.def("getVariableSize", &mgis::behaviour::getVariableSize);
107+
m.def("getVariableSize",
108+
pybind11::overload_cast<const Variable&,
109+
const mgis::behaviour::Hypothesis>(
110+
&mgis::behaviour::getVariableSize));
108111
m.def("getVariableSize", getVariableSizeByString);
109112
m.def("getArraySize", &mgis::behaviour::getArraySize);
110113
m.def("getVariableOffset", getVariableOffsetByString);

docs/web/release-notes-3.2.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,28 @@ This version is meant to be used with `TFEL` Version 5.2.
2222

2323
# Documentation
2424

25+
## New `cmake` options
26+
27+
### HDF5 support
28+
29+
The following options control the support of the `HDF5` library:
30+
31+
- `enable-hdf5-support`: enable HDF5 support for save/restore
32+
operations. Note that if this support is not explicitely requested,
33+
`MGIS` still tries by default to support `HDF5` but configuration will
34+
not fail if the `HDF5` library is not found. See
35+
`enable-hdf5-automatic-support` for details.
36+
- `enable-hdf5-automatic-support`: if set, `MGIS` tries by default to
37+
support `HDF5` even if enable-hdf5-support` is not set.
38+
2539
## `Doxygen` documentation
2640

2741
The doxygen documentation is now online:
2842
<https://thelfer.github.io/mgis/doxygen/index.html>.
2943

3044
# New features
3145

32-
## Scripts to define environment variables for `mGIS` to work properly
46+
## Scripts to define environment variables for `MGIS` to work properly
3347

3448
Depending on the system and compilation options, some of following
3549
variables shall be set for `MGIS` to work properly: `MGISHOME`, `PATH`,
@@ -72,6 +86,25 @@ directory (refered to `<install_prefix>` in the following):
7286
7387
# New features of the `MGIS/Behaviour` library
7488

89+
## Save/restore operations in `MaterialStateManager` and `MaterialDataManager`
90+
91+
If `HDF5` support is enabled, two functions `save` and `restore` are
92+
available:
93+
94+
- The `save` function allows saving the values stored in a
95+
`MaterialStateManager` or in a `MaterialDataManager` to an `HDF5`
96+
file.
97+
- The `restore` function allows retrieving the values stored in a
98+
`MaterialStateManager` or in a `MaterialDataManager` from an `HDF5`
99+
file.
100+
101+
Those functions have options allowing to precisely select what is saved
102+
or restored.
103+
104+
By default, one expects to restore the maximum amount of information.
105+
The `getGreedyMaterialStateManagerRestoreOptions` function creates
106+
option that can restore everything that has been saved.
107+
75108
## Control on variables updated/reverted in `MaterialStateManager`
76109

77110
By default, material properties, mass density and external state
@@ -152,6 +185,10 @@ const auto e2 = f.get<0, tfel::math::stensor<2, real>>(1);
152185

153186
For more details, see <https://github.com/thelfer/MFrontGenericInterfaceSupport/issues/210>
154187

188+
## Issue 209: Add basic support for `save`/`restore` operations in `MaterialDataManager`
189+
190+
For more details, see <https://github.com/thelfer/MFrontGenericInterfaceSupport/issues/209>
191+
155192
## Issue 201: Add the ability to update only the state variables of a `MaterialStateManager`
156193

157194
For more details, see <https://github.com/thelfer/MFrontGenericInterfaceSupport/issues/201>

include/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ mgis_header(MGIS/Behaviour FiniteStrainSupport.hxx)
4646
mgis_header(MGIS/Model Model.hxx)
4747

4848
if(MGIS_HAVE_TFEL)
49-
mgis_header(MGIS Database.hxx)
49+
mgis_header(MGIS Database.hxx)
5050
endif(MGIS_HAVE_TFEL)
5151

52+
if(MGIS_HDF5_SUPPORT)
53+
mgis_header(MGIS/Utilities HDF5Forward.hxx)
54+
mgis_header(MGIS/Utilities HDF5Support.hxx)
55+
endif(MGIS_HDF5_SUPPORT)
56+
5257
if(enable-mgis-function)
5358
mgis_header(MGIS/Function CompileTimeSize.hxx)
5459
mgis_header(MGIS/Function SpaceConcept.hxx)

include/MGIS/Behaviour/MaterialDataManager.hxx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include <thread>
2020
#include <memory>
2121
#include <vector>
22+
#ifdef MGIS_HAVE_HDF5
23+
#include "MGIS/Utilities/HDF5Forward.hxx"
24+
#endif /* MGIS_HAVE_HDF5 */
25+
2226
#include "MGIS/Config.hxx"
2327
#include "MGIS/Behaviour/MaterialStateManager.hxx"
2428

@@ -280,6 +284,46 @@ namespace mgis::behaviour {
280284
MGIS_EXPORT std::vector<mgis::real> allocatePostProcessingVariables(
281285
const MaterialDataManager&, const std::string_view);
282286

287+
#ifdef MGIS_HAVE_HDF5
288+
289+
/*!
290+
* \brief structure used to customize the saving of a `MaterialDataManager`
291+
*/
292+
struct MaterialDataManagerSavingOptions
293+
: public MaterialStateManagerSavingOptions {};
294+
295+
/*!
296+
* \brief save a `MaterialDataManager` to an HDF5 group
297+
* \param[in] ctx: execution context
298+
* \param[in] g: group
299+
* \param[in] m: material data manager
300+
* \param[in] opts: options
301+
*/
302+
MGIS_EXPORT [[nodiscard]] bool save(
303+
Context&,
304+
H5::Group&,
305+
const MaterialDataManager&,
306+
const MaterialDataManagerSavingOptions& = {}) noexcept;
307+
/*!
308+
* \brief structure used to customize the saving of a `MaterialDataManager`
309+
*/
310+
struct MaterialDataManagerRestoreOptions
311+
: public MaterialStateManagerRestoreOptions {};
312+
/*!
313+
* \brief restore a `MaterialDataManager` from an HDF5 group
314+
* \param[in] ctx: execution context
315+
* \param[in] g: group
316+
* \param[in] m: material data manager
317+
* \param[in] opts: options
318+
*/
319+
MGIS_EXPORT [[nodiscard]] bool restore(
320+
Context&,
321+
MaterialDataManager&,
322+
const H5::Group&,
323+
const MaterialDataManagerRestoreOptions& = {}) noexcept;
324+
325+
#endif /* MGIS_HAVE_HDF5 */
326+
283327
} // end of namespace mgis::behaviour
284328

285329
#endif /* LIB_MGIS_BEHAVIOUR_MATERIALDATAMANAGER_HXX */

include/MGIS/Behaviour/MaterialStateManager.hxx

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include <variant>
2323
#include <optional>
2424
#include <string_view>
25+
#ifdef MGIS_HAVE_HDF5
26+
#include "MGIS/Utilities/HDF5Forward.hxx"
27+
#endif /* MGIS_HAVE_HDF5 */
28+
2529
#include "MGIS/Config.hxx"
2630
#include "MGIS/StorageMode.hxx"
2731

@@ -240,6 +244,37 @@ namespace mgis::behaviour {
240244
const MaterialStateManager::StorageMode =
241245
MaterialStateManager::LOCAL_STORAGE,
242246
const MaterialStateManager::UpdatePolicy = MaterialStateManager::UPDATE);
247+
/*!
248+
* \brief set the given material property
249+
* \param[out] m: material data manager
250+
* \param[in] n: name
251+
* \param[in] v: value
252+
* \param[in] p: update policy
253+
*/
254+
MGIS_EXPORT [[nodiscard]] bool setMaterialProperty(
255+
Context&,
256+
MaterialStateManager&,
257+
const std::string_view&,
258+
const real,
259+
const MaterialStateManager::UpdatePolicy =
260+
MaterialStateManager::UPDATE) noexcept;
261+
/*!
262+
* \brief set the given material property
263+
* \param[out] m: material data manager
264+
* \param[in] n: name
265+
* \param[in] v: values
266+
* \param[in] s: storage mode
267+
* \param[in] p: update policy
268+
*/
269+
MGIS_EXPORT [[nodiscard]] bool setMaterialProperty(
270+
Context&,
271+
MaterialStateManager&,
272+
const std::string_view&,
273+
const std::span<mgis::real>&,
274+
const MaterialStateManager::StorageMode =
275+
MaterialStateManager::LOCAL_STORAGE,
276+
const MaterialStateManager::UpdatePolicy =
277+
MaterialStateManager::UPDATE) noexcept;
243278
/*!
244279
* \return true if the given external state variable is defined.
245280
* \param[out] m: material data manager
@@ -354,6 +389,93 @@ namespace mgis::behaviour {
354389
const mgis::behaviour::MaterialStateManager&,
355390
const std::string_view);
356391

392+
#ifdef MGIS_HAVE_HDF5
393+
394+
/*!
395+
* \brief structure used to customize the saving of a `MaterialStateManager`
396+
*/
397+
struct MaterialStateManagerSavingOptions {
398+
const bool allow_overwrite = true;
399+
const bool save_gradients = true;
400+
const bool save_thermodynamic_forces = true;
401+
const bool save_stored_energies = true;
402+
const bool save_dissipated_energies = true;
403+
const bool save_mass_densities = true;
404+
const bool save_material_properties = true;
405+
const bool save_external_state_variables = true;
406+
};
407+
408+
/*!
409+
* \brief save a `MaterialStateManager` to an HDF5 group
410+
* \param[in] ctx: execution context
411+
* \param[in] g: group
412+
* \param[in] s: material state manager
413+
* \param[in] opts: options
414+
*/
415+
MGIS_EXPORT [[nodiscard]] bool save(
416+
Context&,
417+
H5::Group&,
418+
const MaterialStateManager&,
419+
const MaterialStateManagerSavingOptions& = {}) noexcept;
420+
421+
/*!
422+
* \brief structure used to customize how to restore a `MaterialStateManager`
423+
*/
424+
struct MaterialStateManagerRestoreOptions {
425+
const bool restore_gradients = true;
426+
const bool restore_thermodynamic_forces = true;
427+
/*!
428+
* \brief flag stating if the stored energies shall be read
429+
*
430+
* \note this flag is ignored if the behaviour does not compute the
431+
* stored energy
432+
*/
433+
const bool restore_stored_energies = true;
434+
/*!
435+
* \brief flag stating if the dissipated energies shall be read
436+
*
437+
* \note this flag is ignored if the behaviour does not compute the
438+
* dissipated energy
439+
*/
440+
const bool restore_dissipated_energies = true;
441+
const bool restore_internal_state_variables = true;
442+
const bool restore_mass_densities = true;
443+
const bool restore_material_properties = true;
444+
//! \brief list of material properties that shall not be restored
445+
const std::vector<std::string> ignored_material_properties = {};
446+
const bool restore_external_state_variables = true;
447+
//! \brief list of external state variables that shall not be restored
448+
const std::vector<std::string> ignored_external_state_variables = {};
449+
}; // end of MaterialStateManagerRestoreOptions
450+
/*!
451+
* \brief return restore options selecting all that can be read in the given
452+
* group.
453+
*
454+
* \param[in] ctx: execution context
455+
* \param[in] g: group
456+
*/
457+
MGIS_EXPORT [[nodiscard]] std::optional<MaterialStateManagerRestoreOptions>
458+
getGreedyMaterialStateManagerRestoreOptions(Context&,
459+
const H5::Group&) noexcept;
460+
/*!
461+
* \brief restore a `MaterialStateManager` from a HDF5 group
462+
*
463+
* \param[in] ctx: execution context
464+
* \param[in] g: group
465+
* \param[in] s: material state manager
466+
* \param[in] opts: options
467+
*
468+
* \note update policies are set to their values for material properties and
469+
* external state variables created during the restoration
470+
*/
471+
MGIS_EXPORT [[nodiscard]] bool restore(
472+
Context&,
473+
MaterialStateManager&,
474+
const H5::Group&,
475+
const MaterialStateManagerRestoreOptions&) noexcept;
476+
477+
#endif /* MGIS_HAVE_HDF5 */
478+
357479
} // end of namespace mgis::behaviour
358480

359481
#endif /* LIB_MGIS_BEHAVIOUR_MATERIALSTATEMANAGER_HXX */

0 commit comments

Comments
 (0)