Skip to content

Commit

Permalink
Merge pull request #122 from pbouda95/dev_generic
Browse files Browse the repository at this point in the history
MaterialDataManager: Enable to integrate with behaviour options through C and Fortran bindings
  • Loading branch information
thelfer committed Nov 28, 2023
2 parents 17b54cc + 438abcd commit 187db86
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 9 deletions.
66 changes: 66 additions & 0 deletions bindings/c/include/MGIS/Behaviour/Integrate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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);
Expand Down Expand Up @@ -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 */
Expand Down
79 changes: 78 additions & 1 deletion bindings/c/src/Integrate.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,64 @@ static mgis::behaviour::IntegrationType convertIntegrationType(
mgis::raise("convertIntegrationType: invalid integration type");
} // 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<bool>(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<bool>(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) {
Expand All @@ -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");
}
Expand All @@ -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,
Expand Down
132 changes: 128 additions & 4 deletions bindings/fortran/src/mgis_behaviour.f95
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2664,7 +2672,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
Expand Down Expand Up @@ -2693,7 +2701,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
Expand Down Expand Up @@ -2724,6 +2732,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
Expand Down Expand Up @@ -2844,7 +2939,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
Expand Down Expand Up @@ -2910,7 +3005,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
Expand Down Expand Up @@ -3549,6 +3644,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
Expand Down
4 changes: 2 additions & 2 deletions bindings/python/src/Integrate.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down
Loading

0 comments on commit 187db86

Please sign in to comment.