-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathmask_utils.cpp
More file actions
463 lines (371 loc) · 22.9 KB
/
Copy pathmask_utils.cpp
File metadata and controls
463 lines (371 loc) · 22.9 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// | / |
// ' / __| _` | __| _ \ __|
// . \ | ( | | ( |\__ `
// _|\_\_| \__,_|\__|\___/ ____/
// Multi-Physics
//
// License: BSD License
// Kratos default license: SystemIdentificationApplication/license.txt
//
// Main authors: Suneth Warnakulasuriya
//
// System includes
#include <numeric>
#include <tuple>
#include <algorithm>
// External includes
// Project includes
#include "utilities/parallel_utilities.h"
#include "utilities/reduction_utilities.h"
#include "expression/literal_expression.h"
#include "expression/literal_flat_expression.h"
// Application includes
// Include base h
#include "mask_utils.h"
namespace Kratos {
template<class TContainerType>
void MaskUtils::CheckCompatibility(
const ContainerExpression<TContainerType>& rMask1,
const ContainerExpression<TContainerType>& rMask2)
{
KRATOS_TRY
KRATOS_ERROR_IF_NOT(rMask1.GetItemComponentCount() == 1)
<< "rMask1 should be a scalar expression. [ shape of the given expression = "
<< rMask1.GetItemShape() << " ].\n";
KRATOS_ERROR_IF_NOT(rMask2.GetItemComponentCount() == 1)
<< "rMask2 should be a scalar expression. [ shape of the given expression = "
<< rMask2.GetItemShape() << " ].\n";
KRATOS_ERROR_IF_NOT(rMask1.GetContainer().size() == rMask2.GetContainer().size())
<< "rMask1 and rMask2 entities size mismatch [ rMask1.size() = " << rMask1.GetContainer().size()
<< ", rMask2.size() = " << rMask2.GetContainer().size() << " ].\n";
KRATOS_CATCH("");
}
template<class TContainerType>
std::size_t MaskUtils::GetMaskSize(
const ContainerExpression<TContainerType>& rMask,
const IndexType RequiredMinimumRedundancy)
{
KRATOS_TRY
KRATOS_ERROR_IF_NOT(rMask.GetItemComponentCount() == 1)
<< "Mask should be a scalar expression. [ shape of the given expression = "
<< rMask.GetItemShape() << " ].\n";
const auto& r_expression = rMask.GetExpression();
return rMask.GetModelPart().GetCommunicator().GetDataCommunicator().SumAll(IndexPartition<IndexType>(r_expression.NumberOfEntities()).for_each<SumReduction<int>>([&r_expression, RequiredMinimumRedundancy](const auto Index) {
return r_expression.Evaluate(Index, Index, 0) >= RequiredMinimumRedundancy;
}));
KRATOS_CATCH("");
}
template<class TContainerType>
ContainerExpression<TContainerType> MaskUtils::GetMask(
const ContainerExpression<TContainerType>& rScalarExpression)
{
KRATOS_TRY
const auto& r_input_expression = rScalarExpression.GetExpression();
const auto number_of_entities = r_input_expression.NumberOfEntities();
KRATOS_ERROR_IF_NOT(r_input_expression.GetItemComponentCount() == 1)
<< "rScalarExpression should be a scalar expression. [ shape of the given expression = "
<< r_input_expression.GetItemShape() << " ].\n";
struct Data
{
IndexType mIndex;
double mValue;
bool operator<(const Data& rRight) const { return mValue < rRight.mValue; }
};
std::vector<Data> index_value_pairs_vector;
index_value_pairs_vector.resize(number_of_entities);
IndexPartition<IndexType>(number_of_entities).for_each([&index_value_pairs_vector, &r_input_expression](const auto Index) {
index_value_pairs_vector[Index].mIndex = Index;
index_value_pairs_vector[Index].mValue = r_input_expression.Evaluate(Index, Index, 0);
});
// now sort expression values
std::sort(index_value_pairs_vector.begin(), index_value_pairs_vector.end(), [](const auto& rV1, const auto& rV2){
return rV1.mValue > rV2.mValue;
});
// now find the coverage
const auto& r_data = IndexPartition<IndexType>(number_of_entities).for_each<MaxReduction<Data>>([&index_value_pairs_vector](const auto Index){
const double current_sum = std::accumulate(index_value_pairs_vector.begin(), index_value_pairs_vector.begin() + Index + 1, 0.0, [](const auto& rValue, const auto& rIndexValuePair) {
return rValue + rIndexValuePair.mValue;
});
return Data{Index, current_sum / std::sqrt(Index + 1)};
});
auto p_expression = LiteralFlatExpression<int>::Create(number_of_entities, {});
IndexPartition<IndexType>(number_of_entities).for_each([&p_expression](const auto Index) {
*(p_expression->begin() + Index) = 0;
});
IndexPartition<IndexType>(r_data.mIndex + 1).for_each([&p_expression, &index_value_pairs_vector](const auto Index){
*(p_expression->begin() + index_value_pairs_vector[Index].mIndex) = 1;
});
auto result = rScalarExpression;
result.SetExpression(p_expression);
return result;
KRATOS_CATCH("");
}
template<class TContainerType>
double MaskUtils::GetMaskThreshold(const ContainerExpression<TContainerType>& rScalarExpression)
{
KRATOS_TRY
const auto& r_input_expression = rScalarExpression.GetExpression();
const auto number_of_entities = r_input_expression.NumberOfEntities();
KRATOS_ERROR_IF_NOT(r_input_expression.GetItemComponentCount() == 1)
<< "rScalarExpression should be a scalar expression. [ shape of the given expression = "
<< r_input_expression.GetItemShape() << " ].\n";
struct Data
{
IndexType mIndex;
double mValue;
bool operator<(const Data& rRight) const { return mValue < rRight.mValue; }
};
std::vector<Data> index_value_pairs_vector;
index_value_pairs_vector.resize(number_of_entities);
IndexPartition<IndexType>(number_of_entities).for_each([&index_value_pairs_vector, &r_input_expression](const auto Index) {
index_value_pairs_vector[Index].mIndex = Index;
index_value_pairs_vector[Index].mValue = r_input_expression.Evaluate(Index, Index, 0);
});
// now sort expression values
std::sort(index_value_pairs_vector.begin(), index_value_pairs_vector.end(), [](const auto& rV1, const auto& rV2){
return rV1.mValue > rV2.mValue;
});
// now find the coverage
const auto& r_data = IndexPartition<IndexType>(number_of_entities).for_each<MaxReduction<Data>>([&index_value_pairs_vector](const auto Index){
const double current_sum = std::accumulate(index_value_pairs_vector.begin(), index_value_pairs_vector.begin() + Index + 1, 0.0, [](const auto& rValue, const auto& rIndexValuePair) {
return rValue + rIndexValuePair.mValue;
});
return Data{Index, current_sum / std::sqrt(Index + 1)};
});
if (r_data.mIndex < number_of_entities - 1) {
const auto threshold_value_index_1 = index_value_pairs_vector[r_data.mIndex].mIndex;
const auto threshold_value_index_2 = index_value_pairs_vector[r_data.mIndex + 1].mIndex;
return 0.5 * (r_input_expression.Evaluate(threshold_value_index_1, threshold_value_index_1, 0) + r_input_expression.Evaluate(threshold_value_index_2, threshold_value_index_2, 0));
} else {
const auto threshold_value_index = index_value_pairs_vector[r_data.mIndex].mIndex;
return r_input_expression.Evaluate(threshold_value_index, threshold_value_index, 0);
}
KRATOS_CATCH("");
}
template<class TContainerType>
ContainerExpression<TContainerType> MaskUtils::GetMask(
const ContainerExpression<TContainerType>& rScalarExpression,
const double Threshold)
{
KRATOS_TRY
const auto& r_input_expression = rScalarExpression.GetExpression();
const auto number_of_entities = r_input_expression.NumberOfEntities();
KRATOS_ERROR_IF_NOT(r_input_expression.GetItemComponentCount() == 1)
<< "rScalarExpression should be a scalar expression. [ shape of the given expression = "
<< r_input_expression.GetItemShape() << " ].\n";
auto p_expression = LiteralFlatExpression<int>::Create(number_of_entities, {});
IndexPartition<IndexType>(number_of_entities).for_each([&p_expression, &r_input_expression, Threshold](const auto Index) {
*(p_expression->begin() + Index) = r_input_expression.Evaluate(Index, Index, 0) > Threshold;
});
auto result = rScalarExpression;
result.SetExpression(p_expression);
return result;
KRATOS_CATCH("");
}
template<class TContainerType>
ContainerExpression<TContainerType> MaskUtils::Union(
const ContainerExpression<TContainerType>& rMask1,
const ContainerExpression<TContainerType>& rMask2,
const IndexType RequiredMinimumRedundancy)
{
KRATOS_TRY
CheckCompatibility(rMask1, rMask2);
const auto& r_mask_1_exp = rMask1.GetExpression();
const auto& r_mask_2_exp = rMask2.GetExpression();
const auto number_of_entities = r_mask_1_exp.NumberOfEntities();
auto p_expression = LiteralFlatExpression<int>::Create(number_of_entities, {});
IndexPartition<IndexType>(number_of_entities).for_each([&p_expression, &r_mask_1_exp, &r_mask_2_exp, RequiredMinimumRedundancy](const auto Index) {
*(p_expression->begin() + Index) = (r_mask_1_exp.Evaluate(Index, Index, 0) >= RequiredMinimumRedundancy ||
r_mask_2_exp.Evaluate(Index, Index, 0) >= RequiredMinimumRedundancy)
? RequiredMinimumRedundancy
: 0;
});
auto result = rMask1;
result.SetExpression(p_expression);
return result;
KRATOS_CATCH("");
}
template<class TContainerType>
ContainerExpression<TContainerType> MaskUtils::Intersect(
const ContainerExpression<TContainerType>& rMask1,
const ContainerExpression<TContainerType>& rMask2,
const IndexType RequiredMinimumRedundancy)
{
KRATOS_TRY
CheckCompatibility(rMask1, rMask2);
const auto& r_mask_1_exp = rMask1.GetExpression();
const auto& r_mask_2_exp = rMask2.GetExpression();
const auto number_of_entities = r_mask_1_exp.NumberOfEntities();
auto p_expression = LiteralFlatExpression<int>::Create(number_of_entities, {});
IndexPartition<IndexType>(number_of_entities).for_each([&p_expression, &r_mask_1_exp, &r_mask_2_exp, RequiredMinimumRedundancy](const auto Index) {
*(p_expression->begin() + Index) = (r_mask_1_exp.Evaluate(Index, Index, 0) >= RequiredMinimumRedundancy &&
r_mask_2_exp.Evaluate(Index, Index, 0) >= RequiredMinimumRedundancy)
? RequiredMinimumRedundancy
: 0;
});
auto result = rMask1;
result.SetExpression(p_expression);
return result;
KRATOS_CATCH("");
}
template<class TContainerType>
ContainerExpression<TContainerType> MaskUtils::Subtract(
const ContainerExpression<TContainerType>& rMask1,
const ContainerExpression<TContainerType>& rMask2,
const IndexType RequiredMinimumRedundancy)
{
KRATOS_TRY
CheckCompatibility(rMask1, rMask2);
const auto& r_mask_1_exp = rMask1.GetExpression();
const auto& r_mask_2_exp = rMask2.GetExpression();
const auto number_of_entities = r_mask_1_exp.NumberOfEntities();
auto p_expression = LiteralFlatExpression<int>::Create(number_of_entities, {});
IndexPartition<IndexType>(number_of_entities).for_each([&p_expression, &r_mask_1_exp, &r_mask_2_exp, RequiredMinimumRedundancy](const auto Index) {
*(p_expression->begin() + Index) = (r_mask_1_exp.Evaluate(Index, Index, 0) >= RequiredMinimumRedundancy &&
r_mask_2_exp.Evaluate(Index, Index, 0) < RequiredMinimumRedundancy)
? RequiredMinimumRedundancy
: 0;
});
auto result = rMask1;
result.SetExpression(p_expression);
return result;
KRATOS_CATCH("");
}
template<class TContainerType>
ContainerExpression<TContainerType> MaskUtils::Scale(
const ContainerExpression<TContainerType>& rScalarExpression,
const ContainerExpression<TContainerType>& rMask,
const IndexType RequiredMinimumRedundancy)
{
KRATOS_TRY
CheckCompatibility(rScalarExpression, rMask);
const auto& r_scalar_expression = rScalarExpression.GetExpression();
const auto& r_mask = rMask.GetExpression();
const auto number_of_entities = r_scalar_expression.NumberOfEntities();
auto p_expression = LiteralFlatExpression<double>::Create(number_of_entities, {});
IndexPartition<IndexType>(number_of_entities).for_each([&p_expression, &r_scalar_expression, &r_mask, RequiredMinimumRedundancy](const auto Index) {
*(p_expression->begin() + Index) = (r_mask.Evaluate(Index, Index, 0) >= RequiredMinimumRedundancy)
? r_scalar_expression.Evaluate(Index, Index, 0)
: 0;
});
auto result = rScalarExpression;
result.SetExpression(p_expression);
return result;
KRATOS_CATCH("");
}
template<class TContainerType>
std::vector<std::tuple<std::vector<IndexType>, typename ContainerExpression<TContainerType>::Pointer>> MaskUtils::ClusterMasks(
const std::vector<ContainerExpression<TContainerType>>& rMasksList,
const IndexType RequiredMinimumRedundancy)
{
KRATOS_TRY
using DataType = std::tuple<std::vector<IndexType>, typename ContainerExpression<TContainerType>::Pointer>;
std::vector<DataType> cluster_data;
if (rMasksList.size() == 0) {
return cluster_data;
}
const auto& r_front_cexp = rMasksList.front();
const IndexType number_of_entities = r_front_cexp.GetExpression().NumberOfEntities();
// Check if all the masks are compatible
for (const auto& r_mask : rMasksList) {
KRATOS_ERROR_IF_NOT(number_of_entities == r_mask.GetExpression().NumberOfEntities())
<< "Mismatch in mask size [required mask size = " << number_of_entities << ", "
<< " found one mask with size = " << r_mask.GetExpression().NumberOfEntities() << " ].\n";
KRATOS_ERROR_IF_NOT(r_mask.GetExpression().GetItemComponentCount() == 1)
<< "Found a mask with a non scalar dimensionality which is not allowed [ requried mask shape = {}, found shape = "
<< r_mask.GetExpression().GetItemShape() << " ].\n";
}
std::vector<std::vector<IndexType>> domain_mask_indices(number_of_entities);
IndexPartition<IndexType>(number_of_entities).for_each([&domain_mask_indices, &rMasksList, RequiredMinimumRedundancy](const auto Index) {
auto& r_indices_list = domain_mask_indices[Index];
for (IndexType i = 0; i < rMasksList.size(); ++i) {
if (rMasksList[i].GetExpression().Evaluate(Index, Index, 0) >= RequiredMinimumRedundancy) {
r_indices_list.push_back(i);
}
}
});
// now find unique list of cluster indices
std::vector<LiteralFlatExpression<int>::Pointer> cluster_mask_exps;
for (IndexType i = 0; i < number_of_entities; ++i) {
auto& mask_indices = domain_mask_indices[i];
std::sort(mask_indices.begin(), mask_indices.end());
/// check against existing mask indices
auto p_itr = std::find_if(cluster_data.begin(), cluster_data.end(), [&mask_indices](const DataType& rData){ return std::get<0>(rData) == mask_indices; });
if (p_itr == cluster_data.end()) {
auto p_expression = LiteralFlatExpression<int>::Create(number_of_entities, {});
IndexPartition<IndexType>(number_of_entities).for_each([&p_expression](const auto Index){
*(p_expression->begin() + Index) = 0;
});
auto p_container_exp = Kratos::make_shared<ContainerExpression<TContainerType>>(*r_front_cexp.pGetModelPart());
p_container_exp->SetExpression(p_expression);
cluster_data.push_back(std::make_tuple(mask_indices, p_container_exp));
cluster_mask_exps.push_back(p_expression);
}
}
// now fill in the cluster masks
IndexPartition<IndexType>(number_of_entities).for_each([&cluster_data, &domain_mask_indices, &cluster_mask_exps](const IndexType Index) {
const auto& mask_indices = domain_mask_indices[Index];
auto p_itr = std::find_if(cluster_data.begin(), cluster_data.end(), [&mask_indices](const DataType& rData){ return std::get<0>(rData) == mask_indices; });
const auto cluster_index = std::distance(cluster_data.begin(), p_itr);
*(cluster_mask_exps[cluster_index]->begin() + Index) = 1;
});
return cluster_data;
KRATOS_CATCH("");
}
template<class TContainerType>
std::vector<IndexType> MaskUtils::GetMasksDividingReferenceMask(
const ContainerExpression<TContainerType>& rReferenceMask,
const std::vector<typename ContainerExpression<TContainerType>::Pointer>& rMasksList,
const IndexType RequiredMinimumRedundancy)
{
KRATOS_TRY
const auto reference_mask_coverage = GetMaskSize(rReferenceMask, RequiredMinimumRedundancy);
std::vector<IndexType> indices;
for (IndexType i = 0; i < rMasksList.size(); ++i) {
const auto& r_intersected_exp = Intersect(rReferenceMask, *rMasksList[i], RequiredMinimumRedundancy);
const auto intesection_coverage = GetMaskSize(r_intersected_exp, RequiredMinimumRedundancy);
if (intesection_coverage > 0 && intesection_coverage < reference_mask_coverage) {
indices.push_back(i);
}
}
return indices;
KRATOS_CATCH("");
}
// template instantiations
#ifndef KRATOS_SI_APP_MASK_UTILS_INSTANTIATION
#define KRATOS_SI_APP_MASK_UTILS_INSTANTIATION(CONTAINER_TYPE) \
template KRATOS_API(SYSTEM_IDENTIFICATION_APPLICATION) void MaskUtils::CheckCompatibility( \
const ContainerExpression<CONTAINER_TYPE> &, \
const ContainerExpression<CONTAINER_TYPE> &); \
template KRATOS_API(SYSTEM_IDENTIFICATION_APPLICATION) std::size_t MaskUtils::GetMaskSize( \
const ContainerExpression<CONTAINER_TYPE> &, const IndexType); \
template KRATOS_API(SYSTEM_IDENTIFICATION_APPLICATION) ContainerExpression<CONTAINER_TYPE> MaskUtils::GetMask( \
const ContainerExpression<CONTAINER_TYPE> &); \
template KRATOS_API(SYSTEM_IDENTIFICATION_APPLICATION) double MaskUtils::GetMaskThreshold( \
const ContainerExpression<CONTAINER_TYPE> &); \
template KRATOS_API(SYSTEM_IDENTIFICATION_APPLICATION) ContainerExpression<CONTAINER_TYPE> MaskUtils::GetMask( \
const ContainerExpression<CONTAINER_TYPE> &, const double); \
template KRATOS_API(SYSTEM_IDENTIFICATION_APPLICATION) ContainerExpression<CONTAINER_TYPE> MaskUtils::Union( \
const ContainerExpression<CONTAINER_TYPE> &, \
const ContainerExpression<CONTAINER_TYPE> &, const IndexType); \
template KRATOS_API(SYSTEM_IDENTIFICATION_APPLICATION) ContainerExpression<CONTAINER_TYPE> MaskUtils::Intersect( \
const ContainerExpression<CONTAINER_TYPE> &, \
const ContainerExpression<CONTAINER_TYPE> &, const IndexType); \
template KRATOS_API(SYSTEM_IDENTIFICATION_APPLICATION) ContainerExpression<CONTAINER_TYPE> MaskUtils::Subtract( \
const ContainerExpression<CONTAINER_TYPE> &, \
const ContainerExpression<CONTAINER_TYPE> &, const IndexType); \
template KRATOS_API(SYSTEM_IDENTIFICATION_APPLICATION) ContainerExpression<CONTAINER_TYPE> MaskUtils::Scale( \
const ContainerExpression<CONTAINER_TYPE> &, \
const ContainerExpression<CONTAINER_TYPE> &, const IndexType); \
template KRATOS_API(SYSTEM_IDENTIFICATION_APPLICATION) std::vector<std::tuple<std::vector<IndexType>, typename ContainerExpression<CONTAINER_TYPE>::Pointer>> MaskUtils::ClusterMasks( \
const std::vector<ContainerExpression<CONTAINER_TYPE>> &, const IndexType); \
template KRATOS_API(SYSTEM_IDENTIFICATION_APPLICATION) std::vector<IndexType> MaskUtils::GetMasksDividingReferenceMask( \
const ContainerExpression<CONTAINER_TYPE> &, \
const std::vector<typename ContainerExpression<CONTAINER_TYPE>::Pointer> &, \
const IndexType);
#endif
KRATOS_SI_APP_MASK_UTILS_INSTANTIATION(ModelPart::NodesContainerType)
KRATOS_SI_APP_MASK_UTILS_INSTANTIATION(ModelPart::ConditionsContainerType)
KRATOS_SI_APP_MASK_UTILS_INSTANTIATION(ModelPart::ElementsContainerType)
#undef KRATOS_SI_APP_MASK_UTILS_INSTANTIATION
} // namespace Kratos