From 21253101e21ca7b1bbae01f222b41695c3d71902 Mon Sep 17 00:00:00 2001 From: Pascal Bouda Date: Fri, 11 Aug 2023 11:52:08 +0200 Subject: [PATCH 1/4] fortran bindings: allow a more flexible way to allocate some external memory --- bindings/fortran/src/mgis_behaviour.f95 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bindings/fortran/src/mgis_behaviour.f95 b/bindings/fortran/src/mgis_behaviour.f95 index fb02e88f5..a834da64c 100644 --- a/bindings/fortran/src/mgis_behaviour.f95 +++ b/bindings/fortran/src/mgis_behaviour.f95 @@ -2560,7 +2560,7 @@ function material_data_manager_initializer_bind_tangent_operator_wrapper(d,K,s) end function material_data_manager_initializer_bind_tangent_operator_wrapper end interface type(MaterialDataManagerInitializer), intent(in) :: d - real(kind=8), dimension(:,:), target, intent(out) :: K + real(kind=8), dimension(*), target, intent(out) :: K integer, intent(in) :: n type(mgis_status) :: s type(c_ptr) K_ptr @@ -2589,7 +2589,7 @@ function material_data_manager_initializer_bind_speed_of_sound_wrapper(d,p,s) & end function material_data_manager_initializer_bind_speed_of_sound_wrapper end interface type(MaterialDataManagerInitializer), intent(in) :: d - real(kind=8), dimension(:,:), target, intent(out) :: p + real(kind=8), dimension(*), target, intent(out) :: p integer, intent(in) :: n type(mgis_status) :: s type(c_ptr) p_ptr @@ -2740,7 +2740,7 @@ function mdm_use_array_of_tangent_operator_blocks_wrapper(d, p, n) & end function mdm_use_array_of_tangent_operator_blocks_wrapper end interface type(MaterialDataManager), intent(in) :: d - real(kind=8), dimension(:,:), target, intent(out) :: p + real(kind=8), dimension(*), target, intent(out) :: p integer, intent(in) :: n type(mgis_status) :: r type(c_ptr) p_ptr From 08b0bd45e71c3cca8e5a9a321e9c78821960792c Mon Sep 17 00:00:00 2001 From: Pascal Bouda Date: Fri, 11 Aug 2023 18:37:58 +0200 Subject: [PATCH 2/4] MaterialDataManager integrate overloading to handle options object --- bindings/python/src/Integrate.cxx | 4 ++-- include/MGIS/Behaviour/Integrate.hxx | 28 ++++++++++++++++++++++++++-- src/Integrate.cxx | 8 ++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/bindings/python/src/Integrate.cxx b/bindings/python/src/Integrate.cxx index 5b4cc08d9..176f3daef 100644 --- a/bindings/python/src/Integrate.cxx +++ b/bindings/python/src/Integrate.cxx @@ -238,8 +238,8 @@ void declareIntegrate() { int (*integrate_ptr2)(MaterialDataManager&, const IntegrationType, const mgis::real, const mgis::size_type, const mgis::size_type) = integrate; - int (*integrate_ptr3)(mgis::ThreadPool&, MaterialDataManager&, - const IntegrationType, const mgis::real) = integrate; + int (*integrate_ptr3)(mgis::ThreadPool&, const IntegrationType, + MaterialDataManager&, const mgis::real) = integrate; BehaviourIntegrationResult (*integrate_ptr4)( MaterialDataManager&, const BehaviourIntegrationOptions&, const mgis::real, const mgis::size_type, const mgis::size_type) = diff --git a/include/MGIS/Behaviour/Integrate.hxx b/include/MGIS/Behaviour/Integrate.hxx index a69417a10..e89d9209c 100644 --- a/include/MGIS/Behaviour/Integrate.hxx +++ b/include/MGIS/Behaviour/Integrate.hxx @@ -49,15 +49,20 @@ namespace mgis::behaviour { INTEGRATION_CONSISTENT_TANGENT_OPERATOR = 4 }; // end of enum IntegrationType + enum struct SpeedOfSoundFlag { + INTEGRATION_WITHOUT_SPEED_OF_SOUND = false, + INTEGRATION_WITH_SPEED_OF_SOUND = true + }; // end of enum SpeedOfSoundFlag + /*! * \brief structure defining various option */ - struct BehaviourIntegrationOptions { + struct MGIS_EXPORT BehaviourIntegrationOptions { //! \brief type of integration to be performed IntegrationType integration_type = IntegrationType::INTEGRATION_CONSISTENT_TANGENT_OPERATOR; //! \brief if true, the speed of sound shall be computed - bool compute_speed_of_sound = false; + bool compute_speed_of_sound = static_cast(SpeedOfSoundFlag::INTEGRATION_WITHOUT_SPEED_OF_SOUND); }; // end of BehaviourIntegrationOptions /*! @@ -327,8 +332,27 @@ namespace mgis::behaviour { * is automatically allocated. */ MGIS_EXPORT int integrate(mgis::ThreadPool&, + const BehaviourIntegrationOptions& opts, MaterialDataManager&, + const real); + /*! + * \brief integrate the behaviour for a range of integration points. + * \return an exit status. The returned value has the following meaning: + * - -1: integration failed for at least one integration point + * - 0: all integrations succeeded but results are unreliable for at least + * one Gauss point + * - 1: integration succeeded and results are reliable. + * + * \param[in,out] p: thread pool + * \param[in,out] m: material data manager + * \param[in] dt: time step + * + * \note if required, the memory associated with the tangent operator blocks + * is automatically allocated. + */ + MGIS_EXPORT int integrate(mgis::ThreadPool&, const IntegrationType it, + MaterialDataManager&, const real); /*! * \brief integrate the behaviour for a range of integration points. diff --git a/src/Integrate.cxx b/src/Integrate.cxx index dbee1f953..9be06bd18 100644 --- a/src/Integrate.cxx +++ b/src/Integrate.cxx @@ -626,8 +626,16 @@ namespace mgis::behaviour { } // end of integrate int integrate(ThreadPool& p, + const BehaviourIntegrationOptions& opts, MaterialDataManager& m, + const real dt) { + const auto r = integrate(p, m, opts, dt); + return r.exit_status; + } // end of integrate + + int integrate(ThreadPool& p, const IntegrationType it, + MaterialDataManager& m, const real dt) { BehaviourIntegrationOptions opts; opts.integration_type = it; From 934c9f25df13bdbb95cdc332b94dadbc38c2ce51 Mon Sep 17 00:00:00 2001 From: Pascal Bouda Date: Fri, 11 Aug 2023 18:39:19 +0200 Subject: [PATCH 3/4] c bindings: handle MaterialDataManager integration with options object --- bindings/c/include/MGIS/Behaviour/Integrate.h | 66 ++++++++++++++++ bindings/c/src/Integrate.cxx | 79 ++++++++++++++++++- 2 files changed, 144 insertions(+), 1 deletion(-) diff --git a/bindings/c/include/MGIS/Behaviour/Integrate.h b/bindings/c/include/MGIS/Behaviour/Integrate.h index 386ef36dc..3d53f44e3 100644 --- a/bindings/c/include/MGIS/Behaviour/Integrate.h +++ b/bindings/c/include/MGIS/Behaviour/Integrate.h @@ -22,10 +22,26 @@ #include "MGIS/Behaviour/Behaviour.h" #include "MGIS/Behaviour/MaterialDataManager.h" +#ifdef __cplusplus +#include "MGIS/Behaviour/Integrate.hxx" +#endif /* __cplusplus */ + #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ +#ifdef __cplusplus +using mgis_bv_BehaviourIntegrationOptions = + mgis::behaviour::BehaviourIntegrationOptions; +#else +/*! + * \brief an opaque structure which can only be accessed through the MGIS' API. + */ +typedef struct mgis_bv_BehaviourIntegrationOptions + mgis_bv_BehaviourIntegrationOptions; +#endif + + //! kinematic of the behaviour treated typedef enum { MGIS_BV_PREDICTION_TANGENT_OPERATOR = -3, @@ -38,6 +54,48 @@ typedef enum { MGIS_BV_INTEGRATION_CONSISTENT_TANGENT_OPERATOR = 4 } mgis_bv_IntegrationType; +//! integration options speed of sound flag +typedef enum { + MGIS_BV_INTEGRATION_WITHOUT_SPEED_OF_SOUND = 0, + MGIS_BV_INTEGRATION_WITH_SPEED_OF_SOUND = 1 +} mgis_bv_SpeedOfSoundFlag; + + + +/*! + * \brief allocate a new + * `mgis_bv_BehaviourIntegrationOptions` object + * + * \param[in] o: behaviour integration options + */ +MGIS_C_EXPORT mgis_status mgis_bv_create_behaviour_integration_options( + mgis_bv_BehaviourIntegrationOptions**); +/*! + * \brief set integration type + * \param[in] o: behaviour integration options + * \param[in] s: type + */ +MGIS_C_EXPORT mgis_status +mgis_bv_behaviour_integration_options_set_integration_type( + mgis_bv_BehaviourIntegrationOptions* const, + const mgis_bv_IntegrationType); +/*! + * \brief set speed of sound flag + * \param[in] o: behaviour integration options + * \param[in] s: flag + */ +MGIS_C_EXPORT mgis_status +mgis_bv_behaviour_integration_options_set_speed_of_sound_flag( + mgis_bv_BehaviourIntegrationOptions* const, + const mgis_bv_SpeedOfSoundFlag); +/*! + * \brief free the ressources associated with a + * `mgis_bv_BehaviourIntegrationOptions` object + * + * \param[in] o: integration behaviour options + */ +MGIS_C_EXPORT mgis_status mgis_bv_free_behaviour_integration_options( + mgis_bv_BehaviourIntegrationOptions**); /*! * \brief integrate the behaviour. The returned value has the following * meaning: @@ -49,6 +107,7 @@ typedef enum { * \param[in,out] d: behaviour data * \param[in,out] b: behaviour */ + MGIS_C_EXPORT mgis_status mgis_bv_integrate(int* const, mgis_bv_BehaviourDataView* const, const mgis_bv_Behaviour* const); @@ -114,6 +173,13 @@ mgis_bv_integrate_material_data_manager(int* const, const mgis_bv_IntegrationType, const mgis_real); +MGIS_C_EXPORT mgis_status +mgis_bv_integrate_material_data_manager_with_options(int* const, + mgis_ThreadPool* const, + mgis_bv_MaterialDataManager* const, + mgis_bv_BehaviourIntegrationOptions* const, + const mgis_real); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/bindings/c/src/Integrate.cxx b/bindings/c/src/Integrate.cxx index bf4f247ef..72df1c387 100644 --- a/bindings/c/src/Integrate.cxx +++ b/bindings/c/src/Integrate.cxx @@ -45,6 +45,64 @@ static mgis::behaviour::IntegrationType convertIntegrationType( return mgis::behaviour::IntegrationType::INTEGRATION_NO_TANGENT_OPERATOR; } // end of convertIntegrationType +mgis_status mgis_bv_create_behaviour_integration_options( + mgis_bv_BehaviourIntegrationOptions** o) { + *o = nullptr; + try { + *o = new mgis::behaviour::BehaviourIntegrationOptions(); + if (*o == nullptr) { + return mgis_report_failure( + "mgis_bv_behaviour_integration_options: " + "memory allocation failed"); + } + } catch (...) { + return mgis_handle_cxx_exception(); + } + return mgis_report_success(); +} // end of mgis_bv_create_behaviour_integration_options + +mgis_status mgis_bv_behaviour_integration_options_set_integration_type( + mgis_bv_BehaviourIntegrationOptions* const o, + const mgis_bv_IntegrationType s) { + if (o == nullptr) { + return mgis_report_failure("invalid argument"); + } + try { + o->integration_type = convertIntegrationType(s); + } catch (...) { + return mgis_handle_cxx_exception(); + } + return mgis_report_success(); +} // end of mgis_bv_behaviour_integration_options_set_integration_type + +mgis_status mgis_bv_behaviour_integration_options_set_speed_of_sound_flag( + mgis_bv_BehaviourIntegrationOptions* const o, + const mgis_bv_SpeedOfSoundFlag s) { + if (o == nullptr) { + return mgis_report_failure("invalid argument"); + } + if (s == MGIS_BV_INTEGRATION_WITHOUT_SPEED_OF_SOUND) { + o->compute_speed_of_sound = static_cast(mgis::behaviour::SpeedOfSoundFlag::INTEGRATION_WITHOUT_SPEED_OF_SOUND); + } else if (s == MGIS_BV_INTEGRATION_WITH_SPEED_OF_SOUND) { + o->compute_speed_of_sound = static_cast(mgis::behaviour::SpeedOfSoundFlag::INTEGRATION_WITH_SPEED_OF_SOUND); + } else { + return mgis_report_failure("invalid speed of sound flag"); + } + return mgis_report_success(); +} // end of mgis_bv_behaviour_integration_options_set_speed_of_sound_flag + +mgis_status mgis_bv_free_behaviour_integration_options( + mgis_bv_BehaviourIntegrationOptions** o) { + try { + delete *o; + *o = nullptr; + } catch (...) { + *o = nullptr; + return mgis_handle_cxx_exception(); + } + return mgis_report_success(); +} // end of mgis_bv_free_behaviour_integration_options + mgis_status mgis_bv_integrate(int* const r, mgis_bv_BehaviourDataView* const d, const mgis_bv_Behaviour* const b) { @@ -71,7 +129,7 @@ mgis_status mgis_bv_integrate_material_data_manager( const mgis_real dt) { *r = -1; try { - *r = mgis::behaviour::integrate(*p, *m, convertIntegrationType(i), dt); + *r = mgis::behaviour::integrate(*p, convertIntegrationType(i), *m, dt); if ((*r != 1) && (*r != 0)) { return mgis_report_failure("behaviour integration failed"); } @@ -81,6 +139,25 @@ mgis_status mgis_bv_integrate_material_data_manager( return mgis_report_success(); } // end of mgis_bv_integrate_material_data_manager +mgis_status mgis_bv_integrate_material_data_manager_with_options( + int* const r, + mgis_ThreadPool* const p, + mgis_bv_MaterialDataManager* const m, + mgis_bv_BehaviourIntegrationOptions* const o, + const mgis_real dt) { + *r = -1; + try { + *r = mgis::behaviour::integrate(*p, *o, *m, dt); + if ((*r != 1) && (*r != 0)) { + return mgis_report_failure("behaviour integration failed"); + } + } catch (...) { + return mgis_handle_cxx_exception(); + } + return mgis_report_success(); +} // end of mgis_bv_integrate_material_data_manager_with_options + + mgis_status mgis_bv_integrate_material_data_manager_part( int* const r, mgis_bv_MaterialDataManager* const m, From 438abcd947c0d54db0beaa100c7c2284c6217f5a Mon Sep 17 00:00:00 2001 From: Pascal Bouda Date: Fri, 11 Aug 2023 18:39:41 +0200 Subject: [PATCH 4/4] fortran bindings: handle MaterialDataManager integration with options object --- bindings/fortran/src/mgis_behaviour.f95 | 126 +++++++++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) diff --git a/bindings/fortran/src/mgis_behaviour.f95 b/bindings/fortran/src/mgis_behaviour.f95 index a834da64c..a9938e718 100644 --- a/bindings/fortran/src/mgis_behaviour.f95 +++ b/bindings/fortran/src/mgis_behaviour.f95 @@ -48,6 +48,10 @@ module mgis_behaviour enumerator :: INTEGRATION_TANGENT_OPERATOR = 3 enumerator :: INTEGRATION_CONSISTENT_TANGENT_OPERATOR = 4 end enum + enum, bind(C) + enumerator :: INTEGRATION_WITHOUT_SPEED_OF_SOUND = 0 + enumerator :: INTEGRATION_WITH_SPEED_OF_SOUND = 1 + end enum enum, bind(C) enumerator :: CAUCHY = 0 enumerator :: PK2 = 1 @@ -82,6 +86,10 @@ module mgis_behaviour private type(c_ptr) :: ptr = c_null_ptr end type MaterialStateManager + type :: BehaviourIntegrationOptions + private + type(c_ptr) :: ptr = c_null_ptr + end type BehaviourIntegrationOptions type :: MaterialDataManagerInitializer private type(c_ptr) :: ptr = c_null_ptr @@ -2620,6 +2628,93 @@ end function free_material_data_manager_initializer_wrapper end if end function free_material_data_manager_initializer ! + function create_behaviour_integration_options(o) result(s) + use mgis_fortran_utilities + use mgis, only: mgis_status, report_failure + implicit none + interface + function create_behaviour_integration_options_wrapper(ptr) & + bind(c,name = 'mgis_bv_create_behaviour_integration_options') & + result(r) + use, intrinsic :: iso_c_binding, only: c_ptr + use mgis, only: mgis_status + implicit none + type(c_ptr), intent(out) :: ptr + type(mgis_status) :: r + end function create_behaviour_integration_options_wrapper + end interface + type(BehaviourIntegrationOptions), intent(out) :: o + type(mgis_status) :: s + s = create_behaviour_integration_options_wrapper(o%ptr) + end function create_behaviour_integration_options + ! + function behaviour_integration_options_set_speed_of_sound_flag(o, ss) result(s) + use, intrinsic :: iso_c_binding, only: c_int + use mgis_fortran_utilities + use mgis, only: mgis_status, report_failure + implicit none + interface + function bin_opts_set_speed_of_sound_flag_wrapper(o, ss) & + bind(c,name = 'mgis_bv_behaviour_integration_options_set_speed_of_sound_flag') & + result(r) + use, intrinsic :: iso_c_binding, only: c_ptr, c_int + use mgis, only: mgis_status + implicit none + type(c_ptr), intent(in), value :: o + integer(kind=c_int), intent(in),value :: ss + type(mgis_status) :: r + end function bin_opts_set_speed_of_sound_flag_wrapper + end interface + type(BehaviourIntegrationOptions), intent(in) :: o + integer(kind=c_int), intent(in) :: ss + type(mgis_status) :: s + s = bin_opts_set_speed_of_sound_flag_wrapper(o%ptr,ss) + end function behaviour_integration_options_set_speed_of_sound_flag + ! + function behaviour_integration_options_set_integration_type(o, ss) result(s) + use, intrinsic :: iso_c_binding, only: c_int + use mgis_fortran_utilities + use mgis, only: mgis_status, report_failure + implicit none + interface + function bin_opts_set_integration_type_wrapper(o, ss) & + bind(c,name = 'mgis_bv_behaviour_integration_options_set_integration_type') & + result(r) + use, intrinsic :: iso_c_binding, only: c_ptr, c_int + use mgis, only: mgis_status + implicit none + type(c_ptr), intent(in), value :: o + integer(kind=c_int), intent(in),value :: ss + type(mgis_status) :: r + end function bin_opts_set_integration_type_wrapper + end interface + type(BehaviourIntegrationOptions), intent(in) :: o + integer(kind=c_int), intent(in) :: ss + type(mgis_status) :: s + s = bin_opts_set_integration_type_wrapper(o%ptr,ss) + end function behaviour_integration_options_set_integration_type + ! + function free_behaviour_integration_options(ptr) result(r) + use, intrinsic :: iso_c_binding, only: c_associated + use mgis + implicit none + interface + function free_behaviour_integration_options_wrapper(ptr) & + bind(c, name='mgis_bv_free_behaviour_integration_options') result(r) + use, intrinsic :: iso_c_binding, only: c_ptr + use mgis + implicit none + type(c_ptr), intent(inout) :: ptr + type(mgis_status) :: r + end function free_behaviour_integration_options_wrapper + end interface + type(BehaviourIntegrationOptions), intent(inout) :: ptr + type(mgis_status) :: r + if (c_associated(ptr%ptr)) then + r = free_behaviour_integration_options_wrapper(ptr%ptr) + end if + end function free_behaviour_integration_options + ! function create_material_data_manager(d, b, n) result(s) use, intrinsic :: iso_c_binding, only: c_size_t use mgis_fortran_utilities @@ -2806,7 +2901,7 @@ function mdm_use_array_of_speed_of_sounds_wrapper(d, p, n) & end function mdm_use_array_of_speed_of_sounds_wrapper end interface type(MaterialDataManager), intent(in) :: d - real(kind=8), dimension(:,:), target, intent(out) :: p + real(kind=8), dimension(*), target, intent(out) :: p integer, intent(in) :: n type(mgis_status) :: r type(c_ptr) p_ptr @@ -3445,6 +3540,35 @@ end function integrate_material_data_manager_wrapper type(mgis_status) :: s s = integrate_material_data_manager_wrapper(r, p%ptr, m%ptr, i, dt) end function integrate_material_data_manager + ! + function integrate_material_data_manager_with_options(r, p, m, i, dt) result(s) + use, intrinsic :: iso_c_binding, only: c_size_t + use mgis_fortran_utilities, only: convert_to_c_index + use mgis, only: ThreadPool, mgis_status, report_failure + implicit none + interface + function integrate_material_data_manager_with_options_wrapper(r, p, m, i, dt) & + bind(c,name = 'mgis_bv_integrate_material_data_manager_with_options') & + result(s) + use, intrinsic :: iso_c_binding, only: c_ptr, c_int, c_double + use mgis, only: mgis_status + implicit none + integer(kind=c_int), intent(out) :: r + type(c_ptr), intent(in),value :: p + type(c_ptr), intent(in),value :: m + type(c_ptr), intent(in),value :: i + real(kind = c_double), intent(in),value :: dt + type(mgis_status) :: s + end function integrate_material_data_manager_with_options_wrapper + end interface + integer, intent(out) :: r + type(ThreadPool), intent(in) :: p + type(MaterialDataManager), intent(in) :: m + type(BehaviourIntegrationOptions), intent(in) :: i + real(kind = 8), intent(in) :: dt + type(mgis_status) :: s + s = integrate_material_data_manager_with_options_wrapper(r, p%ptr, m%ptr, i%ptr, dt) + end function integrate_material_data_manager_with_options ! function integrate_material_data_manager_part(r, m, i, dt, ni, ne) result(s) use, intrinsic :: iso_c_binding, only: c_size_t