FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
FANS/pyfans/micro.cpp at develop · DataAnalyticsEngineering/FANS · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
DataAnalyticsEngineering
/
FANS
Public
Notifications
You must be signed in to change notification settings
Fork
7
Star
23
Code
Issues
1
Pull requests
1
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
FANS
/
pyfans
/
micro.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
230 lines (196 loc) · 9.33 KB
Breadcrumbs
FANS
/
pyfans
/
micro.cpp
Copy path
File metadata and controls
230 lines (196 loc) · 9.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
Micro simulation for mechanical problems
//
In this file we solve a micro problem with FANS which is controlled by the Micro Manager
//
This file is compiled with nanobind to be available as a python module
//
#
include
"
micro.hpp
"
#
include
"
setup.h
"
#
include
"
matmodel.h
"
PyFANSConfig
load_config
(
const
std::string &file_path)
{
PyFANSConfig config{};
const
PyFANSConfig defaults = config;
try
{
ifstream
i
(file_path);
json j;
i >> j;
if
(j.
contains
(
"
no_mpi
"
) && j[
"
no_mpi
"
].
get
<
bool
>())
config.
disable_mpi
=
true
;
if
(j.
contains
(
"
log-level
"
))
config.
logging
.
level
=
spdlog::level::from_str
(j.
at
(
"
log-level
"
).
get
<std::string>());
}
catch
(
const
std::exception &e) {
fprintf
(stderr,
"
ERROR trying to read config file '%s' for pyFANS: %s
\n
"
, file_path.
c_str
(), e.
what
());
fprintf
(stderr,
"
Falling back to default values.
\n
"
);
return
defaults;
}
return
config;
}
MicroSimulation::MicroSimulation
(
int
sim_id,
bool
late_init,
const
std::string &input_file,
const
std::string &config_file)
: _sim_id(sim_id), _input_file(input_file)
{
//
initialize fftw mpi
fftw_mpi_init
();
const
auto
config =
load_config
(config_file);
//
Avoiding reader re-initialization due to unnecessary buffer copies
reader.
communicator
=
MPI_COMM_WORLD
;
if
(config.
disable_mpi
)
reader.
communicator
=
MPI_COMM_SELF
;
MPI_Comm_rank
(reader.
communicator
, &reader.
world_rank
);
MPI_Comm_size
(reader.
communicator
, &reader.
world_size
);
Log::init
(config.
logging
);
if
(
not
late_init || sim_id >=
0
) {
reader.
ReadInputFile
(input_file);
reader.
ReadMS
(
3
);
if
(reader.
strain_type
==
"
small
"
) {
matmanager = createMaterialManager<
3
,
6
>(reader);
solver = createSolver<
3
,
6
>(reader, std::get<MaterialManager<
3
,
6
> *>(matmanager));
}
else
{
matmanager = createMaterialManager<
3
,
9
>(reader);
solver = createSolver<
3
,
9
>(reader, std::get<MaterialManager<
3
,
9
> *>(matmanager));
}
}
}
std::vector<
double
>
merge_arrays
(
const
std::vector<
double
> &v1,
const
std::vector<
double
> &v2)
{
std::vector<
double
> res;
res.
resize
(v1.
size
() + v2.
size
());
std::copy
(v1.
begin
(), v1.
end
(), res.
begin
());
std::copy
(v2.
begin
(), v2.
end
(), res.
begin
() + v1.
size
());
return
res;
}
//
Strided access, so a non-contiguous array needs no copy.
using
Array1D = nb::ndarray<
const
double
, nb::ndim<
1
>>;
std::vector<
double
>
conv_to_vector
(
const
nb::object &obj,
const
int
size)
{
const
auto
arr = nb::cast<Array1D>(obj);
std::vector<
double
> res;
res.
resize
(size);
for
(
int
i =
0
; i < size; i++)
res[i] =
arr
(i);
return
res;
}
//
The capsule frees the buffer with the last Python reference to the array.
static
nb::ndarray<nb::numpy,
double
, nb::shape<
3
>>
make_np_array
(
double
d0,
double
d1,
double
d2)
{
auto
*data =
new
double
[
3
]{d0, d1, d2};
nb::capsule
owner
(data, [](
void
*p)
noexcept
{
delete[]
static_cast
<
double
*>(p); });
return
nb::ndarray<nb::numpy,
double
, nb::shape<
3
>>(data, {
3
}, owner);
}
nb::dict
MicroSimulation::solve
(
const
nb::dict ¯o_data,
double
dt)
{
const
bool
is_small_strain = std::holds_alternative<MaterialManager<
3
,
6
> *>(matmanager);
//
Time step value dt is not used currently, but is available for future use
std::vector<
double
> strain1 =
conv_to_vector
(macro_data[
"
Strains1to3
"
],
3
);
std::vector<
double
> strain2 =
conv_to_vector
(macro_data[
"
Strains4to6
"
],
3
);
std::vector<
double
> strain =
merge_arrays
(strain1, strain2);
if
(
not
is_small_strain) {
std::vector<
double
> strain3 =
conv_to_vector
(macro_data[
"
Strains7to9
"
],
3
);
strain =
merge_arrays
(strain, strain3);
}
VectorXd homogenized_stress;
std::visit
([&](
auto
&mm) { mm->
set_gradient
(strain); }, matmanager);
std::visit
([](
auto
&s) { s->
solve
(); }, solver);
homogenized_stress =
std::visit
([](
auto
&s) -> VectorXd {
return
s->
get_homogenized_stress
(); }, solver);
//
Convert data to a dict again to send it back to the Micro Manager
nb::dict micro_write_data;
micro_write_data[
"
Stresses1to3
"
] =
make_np_array
(homogenized_stress[
0
], homogenized_stress[
1
], homogenized_stress[
2
]);
micro_write_data[
"
Stresses4to6
"
] =
make_np_array
(homogenized_stress[
3
], homogenized_stress[
4
], homogenized_stress[
5
]);
if
(
not
is_small_strain)
micro_write_data[
"
Stresses7to9
"
] =
make_np_array
(homogenized_stress[
6
], homogenized_stress[
7
], homogenized_stress[
8
]);
bool
fresh =
true
;
if
(macro_data.
contains
(
"
ComputeTangent
"
))
fresh = nb::cast<
double
>(macro_data[
"
ComputeTangent
"
]) !=
0.0
;
if
(fresh || cached_tangent.
size
() ==
0
)
cached_tangent =
std::visit
([&](
auto
&s) -> MatrixXd {
return
s->
get_homogenized_tangent
(pert_param); }, solver);
const
MatrixXd &C = cached_tangent;
//
Add stiffness matrix data to Python dict to be returned
if
(is_small_strain) {
micro_write_data[
"
Cmat1
"
] =
make_np_array
(
C
(
0
,
0
),
C
(
0
,
1
),
C
(
0
,
2
));
micro_write_data[
"
Cmat2
"
] =
make_np_array
(
C
(
0
,
3
),
C
(
0
,
4
),
C
(
0
,
5
));
micro_write_data[
"
Cmat3
"
] =
make_np_array
(
C
(
1
,
1
),
C
(
1
,
2
),
C
(
1
,
3
));
micro_write_data[
"
Cmat4
"
] =
make_np_array
(
C
(
1
,
4
),
C
(
1
,
5
),
C
(
2
,
2
));
micro_write_data[
"
Cmat5
"
] =
make_np_array
(
C
(
2
,
3
),
C
(
2
,
4
),
C
(
2
,
5
));
micro_write_data[
"
Cmat6
"
] =
make_np_array
(
C
(
3
,
3
),
C
(
3
,
4
),
C
(
3
,
5
));
micro_write_data[
"
Cmat7
"
] =
make_np_array
(
C
(
4
,
4
),
C
(
4
,
5
),
C
(
5
,
5
));
}
else
{
micro_write_data[
"
Cmat1
"
] =
make_np_array
(
C
(
0
,
0
),
C
(
0
,
1
),
C
(
0
,
2
));
micro_write_data[
"
Cmat2
"
] =
make_np_array
(
C
(
0
,
3
),
C
(
0
,
4
),
C
(
0
,
5
));
micro_write_data[
"
Cmat3
"
] =
make_np_array
(
C
(
0
,
6
),
C
(
0
,
7
),
C
(
0
,
8
));
micro_write_data[
"
Cmat4
"
] =
make_np_array
(
C
(
1
,
0
),
C
(
1
,
1
),
C
(
1
,
2
));
micro_write_data[
"
Cmat5
"
] =
make_np_array
(
C
(
1
,
3
),
C
(
1
,
4
),
C
(
1
,
5
));
micro_write_data[
"
Cmat6
"
] =
make_np_array
(
C
(
1
,
6
),
C
(
1
,
7
),
C
(
1
,
8
));
micro_write_data[
"
Cmat7
"
] =
make_np_array
(
C
(
2
,
0
),
C
(
2
,
1
),
C
(
2
,
2
));
micro_write_data[
"
Cmat8
"
] =
make_np_array
(
C
(
2
,
3
),
C
(
2
,
4
),
C
(
2
,
5
));
micro_write_data[
"
Cmat9
"
] =
make_np_array
(
C
(
2
,
6
),
C
(
2
,
7
),
C
(
2
,
8
));
micro_write_data[
"
Cmat10
"
] =
make_np_array
(
C
(
3
,
0
),
C
(
3
,
1
),
C
(
3
,
2
));
micro_write_data[
"
Cmat11
"
] =
make_np_array
(
C
(
3
,
3
),
C
(
3
,
4
),
C
(
3
,
5
));
micro_write_data[
"
Cmat12
"
] =
make_np_array
(
C
(
3
,
6
),
C
(
3
,
7
),
C
(
3
,
8
));
micro_write_data[
"
Cmat13
"
] =
make_np_array
(
C
(
4
,
0
),
C
(
4
,
1
),
C
(
4
,
2
));
micro_write_data[
"
Cmat14
"
] =
make_np_array
(
C
(
4
,
3
),
C
(
4
,
4
),
C
(
4
,
5
));
micro_write_data[
"
Cmat15
"
] =
make_np_array
(
C
(
4
,
6
),
C
(
4
,
7
),
C
(
4
,
8
));
micro_write_data[
"
Cmat16
"
] =
make_np_array
(
C
(
5
,
0
),
C
(
5
,
1
),
C
(
5
,
2
));
micro_write_data[
"
Cmat17
"
] =
make_np_array
(
C
(
5
,
3
),
C
(
5
,
4
),
C
(
5
,
5
));
micro_write_data[
"
Cmat18
"
] =
make_np_array
(
C
(
5
,
6
),
C
(
5
,
7
),
C
(
5
,
8
));
micro_write_data[
"
Cmat19
"
] =
make_np_array
(
C
(
6
,
0
),
C
(
6
,
1
),
C
(
6
,
2
));
micro_write_data[
"
Cmat20
"
] =
make_np_array
(
C
(
6
,
3
),
C
(
6
,
4
),
C
(
6
,
5
));
micro_write_data[
"
Cmat21
"
] =
make_np_array
(
C
(
6
,
6
),
C
(
6
,
7
),
C
(
6
,
8
));
micro_write_data[
"
Cmat22
"
] =
make_np_array
(
C
(
7
,
0
),
C
(
7
,
1
),
C
(
7
,
2
));
micro_write_data[
"
Cmat23
"
] =
make_np_array
(
C
(
7
,
3
),
C
(
7
,
4
),
C
(
7
,
5
));
micro_write_data[
"
Cmat24
"
] =
make_np_array
(
C
(
7
,
6
),
C
(
7
,
7
),
C
(
7
,
8
));
micro_write_data[
"
Cmat25
"
] =
make_np_array
(
C
(
8
,
0
),
C
(
8
,
1
),
C
(
8
,
2
));
micro_write_data[
"
Cmat26
"
] =
make_np_array
(
C
(
8
,
3
),
C
(
8
,
4
),
C
(
8
,
5
));
micro_write_data[
"
Cmat27
"
] =
make_np_array
(
C
(
8
,
6
),
C
(
8
,
7
),
C
(
8
,
8
));
}
return
micro_write_data;
}
nb::dict
MicroSimulation::get_state
()
{
//
TODO populate state
nb::dict state;
return
state;
}
void
MicroSimulation::set_state
(
const
nb::dict &state)
{
//
TODO read from state, not file
reader.
FreeMS
();
reader.
ReadInputFile
(_input_file);
reader.
ReadMS
(
3
);
if
(reader.
strain_type
==
"
small
"
) {
delete
std::get<MaterialManager<
3
,
6
> *>(matmanager);
auto
*mat_ptr = createMaterialManager<
3
,
6
>(reader);
matmanager = mat_ptr;
delete
std::get<Solver<
3
,
6
> *>(solver);
auto
*sol_ptr = createSolver<
3
,
6
>(reader, mat_ptr);
solver = sol_ptr;
}
else
{
delete
std::get<MaterialManager<
3
,
9
> *>(matmanager);
auto
*mat_ptr = createMaterialManager<
3
,
9
>(reader);
matmanager = mat_ptr;
delete
std::get<Solver<
3
,
9
> *>(solver);
auto
*sol_ptr = createSolver<
3
,
9
>(reader, mat_ptr);
solver = sol_ptr;
}
}
int
MicroSimulation::get_id
()
{
return
_sim_id;
}
void
MicroSimulation::set_id
(
const
int
id)
{
_sim_id = id;
}
NB_MODULE
(PyFANS, m)
{
//
optional docstring
m.
doc
() =
"
Python bindings for FANS
"
;
nb::class_<MicroSimulation>(m,
"
MicroSimulation
"
)
.
def
(nb::init<
int
,
bool
,
const
std::string &,
const
std::string &>(),
nb::arg
(
"
sim_id
"
),
nb::arg
(
"
late_init
"
) =
false
,
nb::arg
(
"
input_file
"
) =
"
input.json
"
,
nb::arg
(
"
config_file
"
) =
"
pyfans-config.json
"
)
.
def
(
"
solve
"
, &MicroSimulation::solve)
.
def
(
"
set_state
"
, &MicroSimulation::set_state)
.
def
(
"
get_state
"
, &MicroSimulation::get_state)
.
def
(
"
get_global_id
"
, &MicroSimulation::get_id)
.
def
(
"
set_global_id
"
, &MicroSimulation::set_id);
}
Back
|
FazBrowse Home
|
New Git URL