10
10
#include " setup.h"
11
11
#include " matmodel.h"
12
12
13
- py::array_t <double > merge_arrays (py::array_t <double > array1, py::array_t <double > array2) {
13
+ py::array_t <double > merge_arrays (py::array_t <double > array1, py::array_t <double > array2)
14
+ {
14
15
// Ensure arrays are contiguous for efficient merging
15
16
array1 = array1.attr (" copy" )();
16
17
array2 = array2.attr (" copy" )();
17
18
18
19
// Get numpy concatenate function
19
- py::object np = py::module::import (" numpy" );
20
+ py::object np = py::module::import (" numpy" );
20
21
py::object concatenate = np.attr (" concatenate" );
21
22
22
23
// Concatenate the two arrays
23
- py::tuple arrays = py::make_tuple (array1, array2);
24
+ py::tuple arrays = py::make_tuple (array1, array2);
24
25
py::array_t <double > result = concatenate (arrays, py::int_ (0 )).cast <py::array_t <double >>();
25
26
26
27
return result;
27
28
}
28
29
29
30
// Constructor
30
- MicroSimulation::MicroSimulation (int sim_id){
31
+ MicroSimulation::MicroSimulation (int sim_id)
32
+ {
31
33
// If used with the Micro Manager, MPI cannot be initialized again but
32
34
// if the python bindings are used standalone, MPI should be initialized
33
- // #ifdef USE_MPI
35
+ // #ifdef USE_MPI
34
36
MPI_Init (NULL , NULL );
35
37
int world_rank, world_size;
36
38
MPI_Comm_rank (MPI_COMM_WORLD, &world_rank);
37
39
MPI_Comm_size (MPI_COMM_WORLD, &world_size);
38
- // #endif
40
+ // #endif
39
41
40
42
// initialize fftw mpi
41
43
fftw_mpi_init ();
42
44
43
45
// Convert the input file path to char* and read the input file
44
- const char * input_files_path = " input_files/test_LinearElastic.json" ;
45
- int input_files_path_length = strlen (input_files_path) + 1 ;
46
- in_place_temp_path = new char [input_files_path_length];
46
+ const char * input_files_path = " input_files/test_LinearElastic.json" ;
47
+ int input_files_path_length = strlen (input_files_path) + 1 ;
48
+ in_place_temp_path = new char [input_files_path_length];
47
49
strcpy (in_place_temp_path, input_files_path);
48
50
reader.ReadInputFile (in_place_temp_path);
49
51
@@ -64,7 +66,7 @@ py::dict MicroSimulation::solve(py::dict macro_data, double dt)
64
66
py::array_t <double > strain2 = macro_data[" strains4to6" ].cast <py::array_t <double >>();
65
67
66
68
py::array_t <double > strain = merge_arrays (strain1, strain2);
67
- std::vector<double > _g0 = std::vector<double >(strain.data (), strain.data () + strain.size ()); // convert numpy array to std::vector.
69
+ std::vector<double > _g0 = std::vector<double >(strain.data (), strain.data () + strain.size ()); // convert numpy array to std::vector.
68
70
69
71
vector<double > g0_all = _g0;
70
72
@@ -75,10 +77,8 @@ py::dict MicroSimulation::solve(py::dict macro_data, double dt)
75
77
76
78
vector<double > g0 (matmodel->n_str );
77
79
78
- for (int i_load = 0 ; i_load < n_loads; i_load++)
79
- {
80
- for (int i = 0 ; i < matmodel->n_str ; ++i)
81
- {
80
+ for (int i_load = 0 ; i_load < n_loads; i_load++) {
81
+ for (int i = 0 ; i < matmodel->n_str ; ++i) {
82
82
g0[i] = g0_all[i_load * matmodel->n_str + i];
83
83
}
84
84
matmodel->setGradient (g0);
@@ -94,22 +94,21 @@ py::dict MicroSimulation::solve(py::dict macro_data, double dt)
94
94
Matrix<double , 3 , 3 > delta_strains;
95
95
Matrix<double , 6 , 6 > perturbed_strains;
96
96
97
- for (int i = 0 ; i < matmodel->n_str ; i++)
98
- {
97
+ for (int i = 0 ; i < matmodel->n_str ; i++) {
99
98
delta_strains.setZero ();
100
99
101
100
if (i == 0 ) { // 11
102
- delta_strains = (pert_param / 2.0 ) * (e1 * e1 .transpose () + e1 * e1 .transpose ());
101
+ delta_strains = (pert_param / 2.0 ) * (e1 * e1 .transpose () + e1 * e1 .transpose ());
103
102
} else if (i == 1 ) { // 22
104
- delta_strains = (pert_param / 2.0 ) * (e2 * e2 .transpose () + e2 * e2 .transpose ());
103
+ delta_strains = (pert_param / 2.0 ) * (e2 * e2 .transpose () + e2 * e2 .transpose ());
105
104
} else if (i == 2 ) { // 33
106
- delta_strains = (pert_param / 2.0 ) * (e3 * e3 .transpose () + e3 * e3 .transpose ());
105
+ delta_strains = (pert_param / 2.0 ) * (e3 * e3 .transpose () + e3 * e3 .transpose ());
107
106
} else if (i == 3 ) { // 12
108
- delta_strains = (pert_param / 2.0 ) * (e1 * e2 .transpose () + e2 * e1 .transpose ());
107
+ delta_strains = (pert_param / 2.0 ) * (e1 * e2 .transpose () + e2 * e1 .transpose ());
109
108
} else if (i == 4 ) { // 13
110
- delta_strains = (pert_param / 2.0 ) * (e1 * e3 .transpose () + e3 * e1 .transpose ());
109
+ delta_strains = (pert_param / 2.0 ) * (e1 * e3 .transpose () + e3 * e1 .transpose ());
111
110
} else if (i == 5 ) { // 23
112
- delta_strains = (pert_param / 2.0 ) * (e2 * e3 .transpose () + e3 * e2 .transpose ());
111
+ delta_strains = (pert_param / 2.0 ) * (e2 * e3 .transpose () + e3 * e2 .transpose ());
113
112
}
114
113
115
114
// Construct perturbed strain matrix according to Mandel notation
@@ -124,12 +123,10 @@ py::dict MicroSimulation::solve(py::dict macro_data, double dt)
124
123
std::cout << " Perturbed strains: " << perturbed_strains << std::endl;
125
124
126
125
// Calculate the homogenized stiffness matrix C using finite differences
127
- for (int i = 0 ; i < matmodel->n_str ; i++)
128
- {
126
+ for (int i = 0 ; i < matmodel->n_str ; i++) {
129
127
vector<double > pert_strain ({perturbed_strains.row (i).begin (), perturbed_strains.row (i).end ()});
130
128
131
- for (int j = 0 ; j < matmodel->n_str ; j++)
132
- {
129
+ for (int j = 0 ; j < matmodel->n_str ; j++) {
133
130
std::cout << " Perturbed strain[" << j << " ] = " << pert_strain[j] << std::endl;
134
131
}
135
132
@@ -138,8 +135,7 @@ py::dict MicroSimulation::solve(py::dict macro_data, double dt)
138
135
solver->postprocess (reader, " result.h5" , 0 , 0 );
139
136
unperturbed_stress = solver->get_homogenized_stress ();
140
137
141
- for (int j = 0 ; j < matmodel->n_str ; j++)
142
- {
138
+ for (int j = 0 ; j < matmodel->n_str ; j++) {
143
139
C (i, j) = (unperturbed_stress[j] - homogenized_stress.data ()[j]) / pert_param;
144
140
}
145
141
}
@@ -150,29 +146,28 @@ py::dict MicroSimulation::solve(py::dict macro_data, double dt)
150
146
py::dict micro_write_data;
151
147
152
148
// Add stress and stiffness matrix data to Python dict to be returned
153
- std::vector<double > stress13 = {homogenized_stress[0 ], homogenized_stress[1 ], homogenized_stress[2 ]};
149
+ std::vector<double > stress13 = {homogenized_stress[0 ], homogenized_stress[1 ], homogenized_stress[2 ]};
154
150
micro_write_data[" stresses1to3" ] = stress13;
155
- std::vector<double > stress46 = {homogenized_stress[3 ], homogenized_stress[4 ], homogenized_stress[5 ]};
151
+ std::vector<double > stress46 = {homogenized_stress[3 ], homogenized_stress[4 ], homogenized_stress[5 ]};
156
152
micro_write_data[" stresses4to6" ] = stress46;
157
- std::vector<double > C_1 = {C (0 , 0 ), C (0 , 1 ), C (0 , 2 )};
158
- micro_write_data[" cmat1" ] = C_1;
159
- std::vector<double > C_2 = {C (0 , 3 ), C (0 , 4 ), C (0 , 5 )};
160
- micro_write_data[" cmat2" ] = C_2;
161
- std::vector<double > C_3 = {C (1 , 1 ), C (1 , 2 ), C (1 , 3 )};
162
- micro_write_data[" cmat3" ] = C_3;
163
- std::vector<double > C_4 = {C (1 , 4 ), C (1 , 5 ), C (2 , 2 )};
164
- micro_write_data[" cmat4" ] = C_4;
165
- std::vector<double > C_5 = {C (2 , 3 ), C (2 , 4 ), C (2 , 5 )};
166
- micro_write_data[" cmat5" ] = C_5;
167
- std::vector<double > C_6 = {C (3 , 3 ), C (3 , 4 ), C (3 , 5 )};
168
- micro_write_data[" cmat6" ] = C_6;
169
- std::vector<double > C_7 = {C (4 , 4 ), C (4 , 5 ), C (5 , 5 )};
170
- micro_write_data[" cmat7" ] = C_7;
153
+ std::vector<double > C_1 = {C (0 , 0 ), C (0 , 1 ), C (0 , 2 )};
154
+ micro_write_data[" cmat1" ] = C_1;
155
+ std::vector<double > C_2 = {C (0 , 3 ), C (0 , 4 ), C (0 , 5 )};
156
+ micro_write_data[" cmat2" ] = C_2;
157
+ std::vector<double > C_3 = {C (1 , 1 ), C (1 , 2 ), C (1 , 3 )};
158
+ micro_write_data[" cmat3" ] = C_3;
159
+ std::vector<double > C_4 = {C (1 , 4 ), C (1 , 5 ), C (2 , 2 )};
160
+ micro_write_data[" cmat4" ] = C_4;
161
+ std::vector<double > C_5 = {C (2 , 3 ), C (2 , 4 ), C (2 , 5 )};
162
+ micro_write_data[" cmat5" ] = C_5;
163
+ std::vector<double > C_6 = {C (3 , 3 ), C (3 , 4 ), C (3 , 5 )};
164
+ micro_write_data[" cmat6" ] = C_6;
165
+ std::vector<double > C_7 = {C (4 , 4 ), C (4 , 5 ), C (5 , 5 )};
166
+ micro_write_data[" cmat7" ] = C_7;
171
167
172
168
return micro_write_data;
173
169
}
174
170
175
-
176
171
PYBIND11_MODULE (PyFANS, m)
177
172
{
178
173
// optional docstring
0 commit comments