Skip to content

Commit cbdcccd

Browse files
committed
Formatting
1 parent 57d22bb commit cbdcccd

File tree

2 files changed

+55
-61
lines changed

2 files changed

+55
-61
lines changed

python/micro.cpp

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,42 @@
1010
#include "setup.h"
1111
#include "matmodel.h"
1212

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+
{
1415
// Ensure arrays are contiguous for efficient merging
1516
array1 = array1.attr("copy")();
1617
array2 = array2.attr("copy")();
1718

1819
// Get numpy concatenate function
19-
py::object np = py::module::import("numpy");
20+
py::object np = py::module::import("numpy");
2021
py::object concatenate = np.attr("concatenate");
2122

2223
// Concatenate the two arrays
23-
py::tuple arrays = py::make_tuple(array1, array2);
24+
py::tuple arrays = py::make_tuple(array1, array2);
2425
py::array_t<double> result = concatenate(arrays, py::int_(0)).cast<py::array_t<double>>();
2526

2627
return result;
2728
}
2829

2930
// Constructor
30-
MicroSimulation::MicroSimulation(int sim_id){
31+
MicroSimulation::MicroSimulation(int sim_id)
32+
{
3133
// If used with the Micro Manager, MPI cannot be initialized again but
3234
// if the python bindings are used standalone, MPI should be initialized
33-
//#ifdef USE_MPI
35+
// #ifdef USE_MPI
3436
MPI_Init(NULL, NULL);
3537
int world_rank, world_size;
3638
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
3739
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
38-
//#endif
40+
// #endif
3941

4042
// initialize fftw mpi
4143
fftw_mpi_init();
4244

4345
// 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];
4749
strcpy(in_place_temp_path, input_files_path);
4850
reader.ReadInputFile(in_place_temp_path);
4951

@@ -64,7 +66,7 @@ py::dict MicroSimulation::solve(py::dict macro_data, double dt)
6466
py::array_t<double> strain2 = macro_data["strains4to6"].cast<py::array_t<double>>();
6567

6668
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.
6870

6971
vector<double> g0_all = _g0;
7072

@@ -75,10 +77,8 @@ py::dict MicroSimulation::solve(py::dict macro_data, double dt)
7577

7678
vector<double> g0(matmodel->n_str);
7779

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) {
8282
g0[i] = g0_all[i_load * matmodel->n_str + i];
8383
}
8484
matmodel->setGradient(g0);
@@ -94,22 +94,21 @@ py::dict MicroSimulation::solve(py::dict macro_data, double dt)
9494
Matrix<double, 3, 3> delta_strains;
9595
Matrix<double, 6, 6> perturbed_strains;
9696

97-
for (int i = 0; i < matmodel->n_str; i++)
98-
{
97+
for (int i = 0; i < matmodel->n_str; i++) {
9998
delta_strains.setZero();
10099

101100
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());
103102
} 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());
105104
} 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());
107106
} 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());
109108
} 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());
111110
} 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());
113112
}
114113

115114
// Construct perturbed strain matrix according to Mandel notation
@@ -124,12 +123,10 @@ py::dict MicroSimulation::solve(py::dict macro_data, double dt)
124123
std::cout << "Perturbed strains: " << perturbed_strains << std::endl;
125124

126125
// 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++) {
129127
vector<double> pert_strain({perturbed_strains.row(i).begin(), perturbed_strains.row(i).end()});
130128

131-
for (int j = 0; j < matmodel->n_str; j++)
132-
{
129+
for (int j = 0; j < matmodel->n_str; j++) {
133130
std::cout << "Perturbed strain[" << j << "] = " << pert_strain[j] << std::endl;
134131
}
135132

@@ -138,8 +135,7 @@ py::dict MicroSimulation::solve(py::dict macro_data, double dt)
138135
solver->postprocess(reader, "result.h5", 0, 0);
139136
unperturbed_stress = solver->get_homogenized_stress();
140137

141-
for (int j = 0; j < matmodel->n_str; j++)
142-
{
138+
for (int j = 0; j < matmodel->n_str; j++) {
143139
C(i, j) = (unperturbed_stress[j] - homogenized_stress.data()[j]) / pert_param;
144140
}
145141
}
@@ -150,29 +146,28 @@ py::dict MicroSimulation::solve(py::dict macro_data, double dt)
150146
py::dict micro_write_data;
151147

152148
// 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]};
154150
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]};
156152
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;
171167

172168
return micro_write_data;
173169
}
174170

175-
176171
PYBIND11_MODULE(PyFANS, m)
177172
{
178173
// optional docstring

python/micro.hpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,24 @@
1818

1919
namespace py = pybind11;
2020

21-
class MicroSimulation
22-
{
23-
public:
21+
class MicroSimulation {
22+
public:
2423
MicroSimulation(int sim_id);
25-
py::dict solve(py::dict macro_write_data, double dt); // API according to the Micro Manager
24+
py::dict solve(py::dict macro_write_data, double dt); // API according to the Micro Manager
2625

27-
private:
28-
int _sim_id;
26+
private:
27+
int _sim_id;
2928
std::vector<double> g0;
30-
double _state;
31-
char* input_temp_path;
32-
char* in_place_temp_path;
33-
char* out_temp_path;
34-
Reader reader;
35-
Matmodel<3>* matmodel;
36-
Solver<3>* solver;
29+
double _state;
30+
char *input_temp_path;
31+
char *in_place_temp_path;
32+
char *out_temp_path;
33+
Reader reader;
34+
Matmodel<3> *matmodel;
35+
Solver<3> *solver;
3736

38-
double pert_param = 1e-3; // scalar strain perturbation parameter
39-
std::vector<double> homogenized_stress;
40-
std::vector<double> unperturbed_stress;
37+
double pert_param = 1e-3; // scalar strain perturbation parameter
38+
std::vector<double> homogenized_stress;
39+
std::vector<double> unperturbed_stress;
4140
Matrix<double, 6, 6> C; // Homogenized stiffness matrix C
4241
};

0 commit comments

Comments
 (0)