Skip to content

Commit

Permalink
Fix Issue #73
Browse files Browse the repository at this point in the history
  • Loading branch information
thelfer committed Oct 15, 2021
1 parent 372fd82 commit 3de56d3
Show file tree
Hide file tree
Showing 34 changed files with 1,199 additions and 14 deletions.
1 change: 1 addition & 0 deletions bindings/c/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ mgis_header(MGIS/Behaviour BehaviourDataView.h)
mgis_header(MGIS/Behaviour MaterialStateManager.h)
mgis_header(MGIS/Behaviour MaterialDataManager.h)
mgis_header(MGIS/Behaviour Integrate.h)
mgis_header(MGIS/Model Model.h)
8 changes: 8 additions & 0 deletions bindings/c/include/MGIS/Behaviour/State.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ MGIS_C_EXPORT mgis_status mgis_bv_state_set_internal_state_variable_by_name(
mgis_bv_State* const,
const char* const,
const mgis_real* const);
/*!
* \brief get a pointer to the state variables
* \param[out] v: pointer to internal state variables
* \param[in] s: state
*/
MGIS_C_EXPORT mgis_status mgis_bv_state_get_internal_state_variables(
mgis_real**,
mgis_bv_State* const);
/*!
* \brief get a internal state variable' value in a state
* \param[out] v: pointer to internal state variable' value(s)
Expand Down
60 changes: 60 additions & 0 deletions bindings/c/include/MGIS/Model/Model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*!
* \file bindings/c/include/MGIS/Model.h
* \brief
* \author Thomas Helfer
* \date 14/10/2021
* \copyright (C) Copyright Thomas Helfer 2018.
* Use, modification and distribution are subject
* to one of the following licences:
* - GNU Lesser General Public License (LGPL), Version 3.0. (See accompanying
* file LGPL-3.0.txt)
* - CECILL-C, Version 1.0 (See accompanying files
* CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt).
*/

#ifndef LIB_MGIS_MODEL_MODEL_H
#define LIB_MGIS_MODEL_MODEL_H

#include "MGIS/Config.h"
#include "MGIS/Status.h"
#include "MGIS/Behaviour/Behaviour.h"

#ifdef __cplusplus
#include "MGIS/Model/Model.hxx"
#endif /* __cplusplus */

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#ifdef __cplusplus
//! \brief a simple alias
using mgis_model_Model = mgis::model::Model;
#else
//! \brief a simple alias
typedef mgis_bv_Behaviour mgis_model_Model;
#endif

/*!
* \brief load a behaviour
*
* \param[out] ptr: behaviour
* \param[in] l: library name
* \param[in] m: model name
* \param[in] h: hypothesis
*/
MGIS_C_EXPORT mgis_status mgis_model_load(mgis_model_Model**,
const char* const,
const char* const,
const char* const);
/*!
* \brief free the memory associated with the given model.
* \param[in,out] m: model
*/
MGIS_C_EXPORT mgis_status mgis_model_free_model(mgis_model_Model**);

#ifdef __cplusplus
} // end of extern "C"
#endif /* __cplusplus */

#endif /* LIB_MGIS_MODEL_MODEL_H */
3 changes: 2 additions & 1 deletion bindings/c/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ mgis_library(MFrontGenericInterface-c SHARED
BehaviourDataView.cxx
MaterialStateManager.cxx
MaterialDataManager.cxx
Integrate.cxx)
Integrate.cxx
Model.cxx)
target_include_directories(MFrontGenericInterface-c
PUBLIC $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
PUBLIC $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/bindings/c/include>
Expand Down
43 changes: 43 additions & 0 deletions bindings/c/src/Model.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*!
* \file Model.cxx
* \brief
* \author Thomas Helfer
* \date 14/10/2021
* \copyright (C) Copyright Thomas Helfer 2018.
* Use, modification and distribution are subject
* to one of the following licences:
* - GNU Lesser General Public License (LGPL), Version 3.0. (See accompanying
* file LGPL-3.0.txt)
* - CECILL-C, Version 1.0 (See accompanying files
* CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt).
*/

#include "MGIS/Model/Model.h"

extern "C" {

mgis_status mgis_model_load(mgis_model_Model** ptr,
const char* const l,
const char* const m,
const char* const h) {
*ptr = nullptr;
try {
const auto model = mgis::model::load(l, m, mgis::behaviour::fromString(h));
*ptr = new mgis::model::Model(std::move(model));
if (*ptr == nullptr) {
return mgis_report_failure(
"mgis_model_load: "
"memory allocation failed");
}
} catch (...) {
return mgis_handle_cxx_exception();
}
return mgis_report_success();
} // end of mgis_model_load

mgis_status mgis_model_free_model(mgis_model_Model** m) {
return mgis_bv_free_behaviour(m);
} // end of mgis_model_free_model

} // end of extern "C"

16 changes: 16 additions & 0 deletions bindings/c/src/State.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,22 @@ mgis_status mgis_bv_state_set_internal_state_variable_by_name(
return mgis_report_success();
} // end of mgis_bv_state_set_internal_state_variable_by_name

mgis_status mgis_bv_state_get_internal_state_variables(
mgis_real** v, mgis_bv_State* const s) {
if (s == nullptr) {
return mgis_report_failure("invalid argument (null state)");
}
if (v == nullptr) {
return mgis_report_failure("invalid argument (null values)");
}
if(s->internal_state_variables.empty()){
*v = nullptr;
return mgis_report_failure("no internal state variables declared");
}
*v = s->internal_state_variables.data();
return mgis_report_success();
} // end of mgis_bv_state_get_internal_state_variable_by_name

mgis_status mgis_bv_state_get_internal_state_variable_by_name(
mgis_real** v, mgis_bv_State* const s, const char* const n) {
if (s == nullptr) {
Expand Down
34 changes: 34 additions & 0 deletions bindings/c/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ add_executable(IntegrateTest3-c
IntegrateTest3-c.c)
target_link_libraries(IntegrateTest3-c
PRIVATE MFrontGenericInterface-c MFrontGenericInterface)
add_executable(IntegrateTest4-c
EXCLUDE_FROM_ALL
IntegrateTest4-c.c)
target_link_libraries(IntegrateTest4-c
PRIVATE MFrontGenericInterface-c MFrontGenericInterface m)
add_executable(IntegrateTest5-c
EXCLUDE_FROM_ALL
IntegrateTest5-c.c)
target_link_libraries(IntegrateTest5-c
PRIVATE MFrontGenericInterface-c MFrontGenericInterface m)

add_test(NAME MFrontGenericBehaviourInterfaceTest-c
COMMAND MFrontGenericBehaviourInterfaceTest-c
Expand Down Expand Up @@ -207,4 +217,28 @@ else((CMAKE_HOST_WIN32) AND (NOT MSYS))
PROPERTY DEPENDS BehaviourTest)
endif((CMAKE_HOST_WIN32) AND (NOT MSYS))

add_test(NAME IntegrateTest4-c
COMMAND IntegrateTest4-c
"$<TARGET_FILE:ModelTest>")
add_dependencies(check IntegrateTest4-c)
if((CMAKE_HOST_WIN32) AND (NOT MSYS))
set_property(TEST IntegrateTest4-c
PROPERTY DEPENDS ModelTest
PROPERTY ENVIRONMENT "PATH=$<TARGET_FILE_DIR:MFrontGenericInterface>\;$<TARGET_FILE_DIR:MFrontGenericInterface-c>\;${MGIS_PATH_STRING}")
else((CMAKE_HOST_WIN32) AND (NOT MSYS))
set_property(TEST IntegrateTest4-c
PROPERTY DEPENDS ModelTest)
endif((CMAKE_HOST_WIN32) AND (NOT MSYS))

add_test(NAME IntegrateTest5-c
COMMAND IntegrateTest5-c
"$<TARGET_FILE:ModelTest>")
add_dependencies(check IntegrateTest5-c)
if((CMAKE_HOST_WIN32) AND (NOT MSYS))
set_property(TEST IntegrateTest5-c
PROPERTY DEPENDS ModelTest
PROPERTY ENVIRONMENT "PATH=$<TARGET_FILE_DIR:MFrontGenericInterface>\;$<TARGET_FILE_DIR:MFrontGenericInterface-c>\;${MGIS_PATH_STRING}")
else((CMAKE_HOST_WIN32) AND (NOT MSYS))
set_property(TEST IntegrateTest5-c
PROPERTY DEPENDS ModelTest)
endif((CMAKE_HOST_WIN32) AND (NOT MSYS))
131 changes: 131 additions & 0 deletions bindings/c/tests/IntegrateTest4-c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*!
* \file IntegrateTest4-c.c
* \brief
* \author Thomas Helfer
* \date 21/09/2018
* \copyright (C) Copyright Thomas Helfer 2018.
* Use, modification and distribution are subject
* to one of the following licences:
* - GNU Lesser General Public License (LGPL), Version 3.0. (See accompanying
* file LGPL-3.0.txt)
* - CECILL-C, Version 1.0 (See accompanying files
* CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt).
*/

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "MGIS/ThreadPool.h"
#include "MGIS/Model/Model.h"
#include "MGIS/Behaviour/MaterialDataManager.h"
#include "MGIS/Behaviour/Integrate.h"

int test_status = EXIT_SUCCESS;
mgis_model_Model* model = NULL;
mgis_ThreadPool* p = NULL;
mgis_bv_MaterialDataManager* m = NULL;

static void check_status(const mgis_status s) {
if (s.exit_status != MGIS_SUCCESS) {
fprintf(stderr, "invalid function call: %s\n", s.msg);
mgis_model_free_model(&model);
mgis_bv_free_material_data_manager(&m);
mgis_free_thread_pool(&p);
exit(EXIT_FAILURE);
}
} // end of check_status

static int check(const int b, const char* const e) {
if (b == 0) {
test_status = EXIT_FAILURE;
fprintf(stderr, "%s\n", e);
}
return b;
} // end of check

int main(const int argc, const char* const* argv) {
if (check(argc == 2, "expected two arguments") == 0) {
return EXIT_FAILURE;
}
const mgis_real eps = 1e-10;
const mgis_real dt =0.1;
mgis_size_type o;
mgis_real* isvs0; /* internal state variables at the beginning of the time step */
mgis_real* isvs1; /* internal state variables at the end of the time step */
mgis_size_type isvs_stride; /* internal state variables stride */
const mgis_size_type n = 100;
mgis_bv_MaterialStateManager*
s0; /* state at the beginning of the time step */
mgis_bv_MaterialStateManager* s1; /* state at the end of the time step */
mgis_real xi[11]; /* values of 'x' for the first integration point */
mgis_real xe[11]; /* values of 'x' for the last integration point */
mgis_size_type ni,ne;
mgis_size_type idx;
mgis_size_type i;
mgis_real A;
mgis_real t;
mgis_real x_ref;
int r;
check_status(mgis_create_thread_pool(&p, 2));
check_status(mgis_model_load(&model, argv[1], "ode_rk54", "Tridimensional"));
check_status(mgis_bv_behaviour_get_parameter_default_value(&A, model, "A"));
check_status(mgis_bv_create_material_data_manager(&m, model, 100));
check_status(
mgis_bv_behaviour_get_internal_state_variable_offset(&o, model, "x"));
check_status(mgis_bv_material_data_manager_get_state_0(&s0, m));
check_status(mgis_bv_material_data_manager_get_state_1(&s1, m));
/* initialize the internal state variable */
check_status(
mgis_bv_material_state_manager_get_internal_state_variables(&isvs0, s0));
check_status(
mgis_bv_material_state_manager_get_internal_state_variables(&isvs1, s1));
check_status(
mgis_bv_material_state_manager_get_internal_state_variables_stride(
&isvs_stride, s1));
for (idx = 0; idx != n; ++idx) {
isvs1[idx * isvs_stride + o] = 1;
}
/* initialize the external state variable */
check_status(
mgis_bv_material_state_manager_set_uniform_external_state_variable(
s1, "Temperature", 293.15));
/* copy s1 in s0 */
check_status(mgis_bv_update_material_data_manager(m));
// integration */
ni = o;
ne = (n - 1) * isvs_stride + o;
xi[0] = isvs0[ni];
xe[0] = isvs0[ne];
for (i = 0; i != 10; ++i) {
check_status(mgis_bv_integrate_material_data_manager(
&r, p, m, MGIS_BV_INTEGRATION_NO_TANGENT_OPERATOR, dt));
check_status(mgis_bv_update_material_data_manager(m));
xi[i + 1] = isvs1[ni];
xe[i + 1] = isvs1[ne];
}
check_status(mgis_model_free_model(&model));
check_status(mgis_bv_free_material_data_manager(&m));
check_status(mgis_free_thread_pool(&p));
t = 0;
for (i = 0; i != 11; ++i) {
x_ref = exp(-A * t);
if (fabs(xi[i] - x_ref) > eps) {
fprintf(stderr,
"IntegrateTest: invalid value for x "
"at the first integration point"
"(expected '%g', computed '%g')\n",
x_ref, xi[i]);
return EXIT_FAILURE;
}
if (fabs(xe[i] - x_ref) > eps) {
fprintf(stderr,
"IntegrateTest: invalid value for x "
"at the first integration point"
"(expected '%g', computed '%g')\n",
x_ref, xe[i]);
return EXIT_FAILURE;
}
t += dt;
}
return EXIT_SUCCESS;
}
Loading

0 comments on commit 3de56d3

Please sign in to comment.