Skip to content

Commit 3de56d3

Browse files
committed
Fix Issue #73
1 parent 372fd82 commit 3de56d3

34 files changed

Lines changed: 1199 additions & 14 deletions

bindings/c/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ mgis_header(MGIS/Behaviour BehaviourDataView.h)
1111
mgis_header(MGIS/Behaviour MaterialStateManager.h)
1212
mgis_header(MGIS/Behaviour MaterialDataManager.h)
1313
mgis_header(MGIS/Behaviour Integrate.h)
14+
mgis_header(MGIS/Model Model.h)

bindings/c/include/MGIS/Behaviour/State.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ MGIS_C_EXPORT mgis_status mgis_bv_state_set_internal_state_variable_by_name(
159159
mgis_bv_State* const,
160160
const char* const,
161161
const mgis_real* const);
162+
/*!
163+
* \brief get a pointer to the state variables
164+
* \param[out] v: pointer to internal state variables
165+
* \param[in] s: state
166+
*/
167+
MGIS_C_EXPORT mgis_status mgis_bv_state_get_internal_state_variables(
168+
mgis_real**,
169+
mgis_bv_State* const);
162170
/*!
163171
* \brief get a internal state variable' value in a state
164172
* \param[out] v: pointer to internal state variable' value(s)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*!
2+
* \file bindings/c/include/MGIS/Model.h
3+
* \brief
4+
* \author Thomas Helfer
5+
* \date 14/10/2021
6+
* \copyright (C) Copyright Thomas Helfer 2018.
7+
* Use, modification and distribution are subject
8+
* to one of the following licences:
9+
* - GNU Lesser General Public License (LGPL), Version 3.0. (See accompanying
10+
* file LGPL-3.0.txt)
11+
* - CECILL-C, Version 1.0 (See accompanying files
12+
* CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt).
13+
*/
14+
15+
#ifndef LIB_MGIS_MODEL_MODEL_H
16+
#define LIB_MGIS_MODEL_MODEL_H
17+
18+
#include "MGIS/Config.h"
19+
#include "MGIS/Status.h"
20+
#include "MGIS/Behaviour/Behaviour.h"
21+
22+
#ifdef __cplusplus
23+
#include "MGIS/Model/Model.hxx"
24+
#endif /* __cplusplus */
25+
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif /* __cplusplus */
29+
30+
#ifdef __cplusplus
31+
//! \brief a simple alias
32+
using mgis_model_Model = mgis::model::Model;
33+
#else
34+
//! \brief a simple alias
35+
typedef mgis_bv_Behaviour mgis_model_Model;
36+
#endif
37+
38+
/*!
39+
* \brief load a behaviour
40+
*
41+
* \param[out] ptr: behaviour
42+
* \param[in] l: library name
43+
* \param[in] m: model name
44+
* \param[in] h: hypothesis
45+
*/
46+
MGIS_C_EXPORT mgis_status mgis_model_load(mgis_model_Model**,
47+
const char* const,
48+
const char* const,
49+
const char* const);
50+
/*!
51+
* \brief free the memory associated with the given model.
52+
* \param[in,out] m: model
53+
*/
54+
MGIS_C_EXPORT mgis_status mgis_model_free_model(mgis_model_Model**);
55+
56+
#ifdef __cplusplus
57+
} // end of extern "C"
58+
#endif /* __cplusplus */
59+
60+
#endif /* LIB_MGIS_MODEL_MODEL_H */

bindings/c/src/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ mgis_library(MFrontGenericInterface-c SHARED
99
BehaviourDataView.cxx
1010
MaterialStateManager.cxx
1111
MaterialDataManager.cxx
12-
Integrate.cxx)
12+
Integrate.cxx
13+
Model.cxx)
1314
target_include_directories(MFrontGenericInterface-c
1415
PUBLIC $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
1516
PUBLIC $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/bindings/c/include>

bindings/c/src/Model.cxx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*!
2+
* \file Model.cxx
3+
* \brief
4+
* \author Thomas Helfer
5+
* \date 14/10/2021
6+
* \copyright (C) Copyright Thomas Helfer 2018.
7+
* Use, modification and distribution are subject
8+
* to one of the following licences:
9+
* - GNU Lesser General Public License (LGPL), Version 3.0. (See accompanying
10+
* file LGPL-3.0.txt)
11+
* - CECILL-C, Version 1.0 (See accompanying files
12+
* CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt).
13+
*/
14+
15+
#include "MGIS/Model/Model.h"
16+
17+
extern "C" {
18+
19+
mgis_status mgis_model_load(mgis_model_Model** ptr,
20+
const char* const l,
21+
const char* const m,
22+
const char* const h) {
23+
*ptr = nullptr;
24+
try {
25+
const auto model = mgis::model::load(l, m, mgis::behaviour::fromString(h));
26+
*ptr = new mgis::model::Model(std::move(model));
27+
if (*ptr == nullptr) {
28+
return mgis_report_failure(
29+
"mgis_model_load: "
30+
"memory allocation failed");
31+
}
32+
} catch (...) {
33+
return mgis_handle_cxx_exception();
34+
}
35+
return mgis_report_success();
36+
} // end of mgis_model_load
37+
38+
mgis_status mgis_model_free_model(mgis_model_Model** m) {
39+
return mgis_bv_free_behaviour(m);
40+
} // end of mgis_model_free_model
41+
42+
} // end of extern "C"
43+

bindings/c/src/State.cxx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,22 @@ mgis_status mgis_bv_state_set_internal_state_variable_by_name(
231231
return mgis_report_success();
232232
} // end of mgis_bv_state_set_internal_state_variable_by_name
233233

234+
mgis_status mgis_bv_state_get_internal_state_variables(
235+
mgis_real** v, mgis_bv_State* const s) {
236+
if (s == nullptr) {
237+
return mgis_report_failure("invalid argument (null state)");
238+
}
239+
if (v == nullptr) {
240+
return mgis_report_failure("invalid argument (null values)");
241+
}
242+
if(s->internal_state_variables.empty()){
243+
*v = nullptr;
244+
return mgis_report_failure("no internal state variables declared");
245+
}
246+
*v = s->internal_state_variables.data();
247+
return mgis_report_success();
248+
} // end of mgis_bv_state_get_internal_state_variable_by_name
249+
234250
mgis_status mgis_bv_state_get_internal_state_variable_by_name(
235251
mgis_real** v, mgis_bv_State* const s, const char* const n) {
236252
if (s == nullptr) {

bindings/c/tests/CMakeLists.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ add_executable(IntegrateTest3-c
6363
IntegrateTest3-c.c)
6464
target_link_libraries(IntegrateTest3-c
6565
PRIVATE MFrontGenericInterface-c MFrontGenericInterface)
66+
add_executable(IntegrateTest4-c
67+
EXCLUDE_FROM_ALL
68+
IntegrateTest4-c.c)
69+
target_link_libraries(IntegrateTest4-c
70+
PRIVATE MFrontGenericInterface-c MFrontGenericInterface m)
71+
add_executable(IntegrateTest5-c
72+
EXCLUDE_FROM_ALL
73+
IntegrateTest5-c.c)
74+
target_link_libraries(IntegrateTest5-c
75+
PRIVATE MFrontGenericInterface-c MFrontGenericInterface m)
6676

6777
add_test(NAME MFrontGenericBehaviourInterfaceTest-c
6878
COMMAND MFrontGenericBehaviourInterfaceTest-c
@@ -207,4 +217,28 @@ else((CMAKE_HOST_WIN32) AND (NOT MSYS))
207217
PROPERTY DEPENDS BehaviourTest)
208218
endif((CMAKE_HOST_WIN32) AND (NOT MSYS))
209219

220+
add_test(NAME IntegrateTest4-c
221+
COMMAND IntegrateTest4-c
222+
"$<TARGET_FILE:ModelTest>")
223+
add_dependencies(check IntegrateTest4-c)
224+
if((CMAKE_HOST_WIN32) AND (NOT MSYS))
225+
set_property(TEST IntegrateTest4-c
226+
PROPERTY DEPENDS ModelTest
227+
PROPERTY ENVIRONMENT "PATH=$<TARGET_FILE_DIR:MFrontGenericInterface>\;$<TARGET_FILE_DIR:MFrontGenericInterface-c>\;${MGIS_PATH_STRING}")
228+
else((CMAKE_HOST_WIN32) AND (NOT MSYS))
229+
set_property(TEST IntegrateTest4-c
230+
PROPERTY DEPENDS ModelTest)
231+
endif((CMAKE_HOST_WIN32) AND (NOT MSYS))
210232

233+
add_test(NAME IntegrateTest5-c
234+
COMMAND IntegrateTest5-c
235+
"$<TARGET_FILE:ModelTest>")
236+
add_dependencies(check IntegrateTest5-c)
237+
if((CMAKE_HOST_WIN32) AND (NOT MSYS))
238+
set_property(TEST IntegrateTest5-c
239+
PROPERTY DEPENDS ModelTest
240+
PROPERTY ENVIRONMENT "PATH=$<TARGET_FILE_DIR:MFrontGenericInterface>\;$<TARGET_FILE_DIR:MFrontGenericInterface-c>\;${MGIS_PATH_STRING}")
241+
else((CMAKE_HOST_WIN32) AND (NOT MSYS))
242+
set_property(TEST IntegrateTest5-c
243+
PROPERTY DEPENDS ModelTest)
244+
endif((CMAKE_HOST_WIN32) AND (NOT MSYS))
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*!
2+
* \file IntegrateTest4-c.c
3+
* \brief
4+
* \author Thomas Helfer
5+
* \date 21/09/2018
6+
* \copyright (C) Copyright Thomas Helfer 2018.
7+
* Use, modification and distribution are subject
8+
* to one of the following licences:
9+
* - GNU Lesser General Public License (LGPL), Version 3.0. (See accompanying
10+
* file LGPL-3.0.txt)
11+
* - CECILL-C, Version 1.0 (See accompanying files
12+
* CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt).
13+
*/
14+
15+
#include <math.h>
16+
#include <stdio.h>
17+
#include <stdlib.h>
18+
#include "MGIS/ThreadPool.h"
19+
#include "MGIS/Model/Model.h"
20+
#include "MGIS/Behaviour/MaterialDataManager.h"
21+
#include "MGIS/Behaviour/Integrate.h"
22+
23+
int test_status = EXIT_SUCCESS;
24+
mgis_model_Model* model = NULL;
25+
mgis_ThreadPool* p = NULL;
26+
mgis_bv_MaterialDataManager* m = NULL;
27+
28+
static void check_status(const mgis_status s) {
29+
if (s.exit_status != MGIS_SUCCESS) {
30+
fprintf(stderr, "invalid function call: %s\n", s.msg);
31+
mgis_model_free_model(&model);
32+
mgis_bv_free_material_data_manager(&m);
33+
mgis_free_thread_pool(&p);
34+
exit(EXIT_FAILURE);
35+
}
36+
} // end of check_status
37+
38+
static int check(const int b, const char* const e) {
39+
if (b == 0) {
40+
test_status = EXIT_FAILURE;
41+
fprintf(stderr, "%s\n", e);
42+
}
43+
return b;
44+
} // end of check
45+
46+
int main(const int argc, const char* const* argv) {
47+
if (check(argc == 2, "expected two arguments") == 0) {
48+
return EXIT_FAILURE;
49+
}
50+
const mgis_real eps = 1e-10;
51+
const mgis_real dt =0.1;
52+
mgis_size_type o;
53+
mgis_real* isvs0; /* internal state variables at the beginning of the time step */
54+
mgis_real* isvs1; /* internal state variables at the end of the time step */
55+
mgis_size_type isvs_stride; /* internal state variables stride */
56+
const mgis_size_type n = 100;
57+
mgis_bv_MaterialStateManager*
58+
s0; /* state at the beginning of the time step */
59+
mgis_bv_MaterialStateManager* s1; /* state at the end of the time step */
60+
mgis_real xi[11]; /* values of 'x' for the first integration point */
61+
mgis_real xe[11]; /* values of 'x' for the last integration point */
62+
mgis_size_type ni,ne;
63+
mgis_size_type idx;
64+
mgis_size_type i;
65+
mgis_real A;
66+
mgis_real t;
67+
mgis_real x_ref;
68+
int r;
69+
check_status(mgis_create_thread_pool(&p, 2));
70+
check_status(mgis_model_load(&model, argv[1], "ode_rk54", "Tridimensional"));
71+
check_status(mgis_bv_behaviour_get_parameter_default_value(&A, model, "A"));
72+
check_status(mgis_bv_create_material_data_manager(&m, model, 100));
73+
check_status(
74+
mgis_bv_behaviour_get_internal_state_variable_offset(&o, model, "x"));
75+
check_status(mgis_bv_material_data_manager_get_state_0(&s0, m));
76+
check_status(mgis_bv_material_data_manager_get_state_1(&s1, m));
77+
/* initialize the internal state variable */
78+
check_status(
79+
mgis_bv_material_state_manager_get_internal_state_variables(&isvs0, s0));
80+
check_status(
81+
mgis_bv_material_state_manager_get_internal_state_variables(&isvs1, s1));
82+
check_status(
83+
mgis_bv_material_state_manager_get_internal_state_variables_stride(
84+
&isvs_stride, s1));
85+
for (idx = 0; idx != n; ++idx) {
86+
isvs1[idx * isvs_stride + o] = 1;
87+
}
88+
/* initialize the external state variable */
89+
check_status(
90+
mgis_bv_material_state_manager_set_uniform_external_state_variable(
91+
s1, "Temperature", 293.15));
92+
/* copy s1 in s0 */
93+
check_status(mgis_bv_update_material_data_manager(m));
94+
// integration */
95+
ni = o;
96+
ne = (n - 1) * isvs_stride + o;
97+
xi[0] = isvs0[ni];
98+
xe[0] = isvs0[ne];
99+
for (i = 0; i != 10; ++i) {
100+
check_status(mgis_bv_integrate_material_data_manager(
101+
&r, p, m, MGIS_BV_INTEGRATION_NO_TANGENT_OPERATOR, dt));
102+
check_status(mgis_bv_update_material_data_manager(m));
103+
xi[i + 1] = isvs1[ni];
104+
xe[i + 1] = isvs1[ne];
105+
}
106+
check_status(mgis_model_free_model(&model));
107+
check_status(mgis_bv_free_material_data_manager(&m));
108+
check_status(mgis_free_thread_pool(&p));
109+
t = 0;
110+
for (i = 0; i != 11; ++i) {
111+
x_ref = exp(-A * t);
112+
if (fabs(xi[i] - x_ref) > eps) {
113+
fprintf(stderr,
114+
"IntegrateTest: invalid value for x "
115+
"at the first integration point"
116+
"(expected '%g', computed '%g')\n",
117+
x_ref, xi[i]);
118+
return EXIT_FAILURE;
119+
}
120+
if (fabs(xe[i] - x_ref) > eps) {
121+
fprintf(stderr,
122+
"IntegrateTest: invalid value for x "
123+
"at the first integration point"
124+
"(expected '%g', computed '%g')\n",
125+
x_ref, xe[i]);
126+
return EXIT_FAILURE;
127+
}
128+
t += dt;
129+
}
130+
return EXIT_SUCCESS;
131+
}

0 commit comments

Comments
 (0)