13 #if !defined(KRATOS_SPATIAL_VARIANCE_METHOD_H_INCLUDED)
14 #define KRATOS_SPATIAL_VARIANCE_METHOD_H_INCLUDED
42 namespace SpatialMethods
44 template <
class TContainerType,
class TContainerItemType,
template <
class T>
class TDataRetrievalFunctor>
51 const TContainerType& r_container =
52 MethodUtilities::GetLocalDataContainer<TContainerType>(rModelPart);
55 #pragma omp parallel for reduction(+ : sum)
56 for (
int i = 0; i < static_cast<int>(r_container.size()); ++
i)
58 const TContainerItemType& r_item = *(r_container.begin() +
i);
59 sum += r_item.Is(rVariable);
66 template <
class TDataType>
71 const TContainerType& r_container =
72 MethodUtilities::GetLocalDataContainer<TContainerType>(rModelPart);
74 TDataType global_sum = rVariable.
Zero();
76 if (r_container.size() > 0)
78 const TDataType& r_initial_value =
79 TDataRetrievalFunctor<TContainerItemType>()(*r_container.begin(), rVariable);
85 TDataType
sum = rVariable.
Zero();
89 for (
int i = 0; i < static_cast<int>(r_container.size()); ++
i)
91 const TContainerItemType& r_item = *(r_container.begin() +
i);
92 const TDataType& current_value =
93 TDataRetrievalFunctor<TContainerItemType>()(r_item, rVariable);
111 template <
class TDataType>
115 const std::string& rNormType,
120 const TContainerType& r_container =
121 MethodUtilities::GetLocalDataContainer<TContainerType>(rModelPart);
123 double global_sum = 0.0;
124 const auto& norm_method =
125 MethodUtilities::GetNormMethod<TDataType>(rVariable, rNormType);
131 for (
int i = 0; i < static_cast<int>(r_container.size()); ++
i)
133 const TContainerItemType& r_item = *(r_container.begin() +
i);
134 const TDataType& current_value =
135 TDataRetrievalFunctor<TContainerItemType>()(r_item, rVariable);
136 sum += norm_method(current_value);
148 template <
class TDataType>
153 const TContainerType& r_container =
154 MethodUtilities::GetLocalDataContainer<TContainerType>(rModelPart);
156 TDataType global_sum = rVariable.
Zero();
158 if (r_container.size() > 0)
160 const TDataType& r_initial_value =
161 TDataRetrievalFunctor<TContainerItemType>()(*r_container.begin(), rVariable);
167 TDataType
sum = rVariable.
Zero();
171 for (
int i = 0; i < static_cast<int>(r_container.size()); ++
i)
173 const TContainerItemType& r_item = *(r_container.begin() +
i);
174 const TDataType& current_value =
175 TDataRetrievalFunctor<TContainerItemType>()(r_item, rVariable);
177 sum += MethodUtilities::RaiseToPower<TDataType>(current_value, 2);
187 const double number_of_items =
189 static_cast<double>(r_container.size()));
190 global_sum = MethodUtilities::RaiseToPower<TDataType>(
191 global_sum * (1.0 /
std::max(number_of_items, 1.0)), 0.5);
198 template <
class TDataType>
202 const std::string& rNormType,
207 const TContainerType& r_container =
208 MethodUtilities::GetLocalDataContainer<TContainerType>(rModelPart);
210 double global_sum = 0.0;
211 const auto& norm_method =
212 MethodUtilities::GetNormMethod<TDataType>(rVariable, rNormType);
218 for (
int i = 0; i < static_cast<int>(r_container.size()); ++
i)
220 const TContainerItemType& r_item = *(r_container.begin() +
i);
221 const TDataType& current_value =
222 TDataRetrievalFunctor<TContainerItemType>()(r_item, rVariable);
223 sum += std::pow(norm_method(current_value), 2);
230 const double number_of_items =
232 static_cast<double>(r_container.size()));
233 return std::sqrt(global_sum /
std::max(number_of_items, 1.0));
238 template <
class TDataType>
241 const TDataType&
sum = CalculateSum<TDataType>(rModelPart, rVariable);
242 const TContainerType& r_container =
243 MethodUtilities::GetLocalDataContainer<TContainerType>(rModelPart);
245 const double number_of_items =
247 static_cast<double>(r_container.size()));
249 return sum * (1.0 /
std::max(number_of_items, 1.0));
252 template <
class TDataType>
256 const std::string& rNormType,
260 CalculateNormSum<TDataType>(rModelPart, rVariable, rNormType, Params);
261 const TContainerType& r_container =
262 MethodUtilities::GetLocalDataContainer<TContainerType>(rModelPart);
264 const double number_of_items =
266 static_cast<double>(r_container.size()));
268 if (number_of_items > 0)
270 return sum * (1.0 / number_of_items);
276 template <
class TDataType>
280 TDataType mean = CalculateMean<TDataType>(rModelPart, rVariable);
281 TDataType global_variance = rVariable.
Zero();
283 const TContainerType& r_container =
284 MethodUtilities::GetLocalDataContainer<TContainerType>(rModelPart);
286 if (r_container.size() > 0)
288 const TDataType& r_initial_value =
289 TDataRetrievalFunctor<TContainerItemType>()(*r_container.begin(), rVariable);
295 TDataType variance = rVariable.
Zero();
299 for (
int i = 0; i < static_cast<int>(r_container.size()); ++
i)
301 const TContainerItemType& r_item = *(r_container.begin() +
i);
302 const TDataType& current_value =
303 TDataRetrievalFunctor<TContainerItemType>()(r_item, rVariable);
310 global_variance += variance;
317 const double number_of_items =
319 static_cast<double>(r_container.size()));
321 if (number_of_items > 0)
323 global_variance *= (1.0 / number_of_items);
327 return std::make_tuple<TDataType, TDataType>(
328 std::forward<TDataType>(mean), std::forward<TDataType>(global_variance));
331 template <
class TDataType>
335 const std::string& rNormType,
338 double mean = CalculateNormMean<TDataType>(rModelPart, rVariable, rNormType, Params);
340 const TContainerType& r_container =
341 MethodUtilities::GetLocalDataContainer<TContainerType>(rModelPart);
343 double global_variance = 0.0;
344 const auto& norm_method =
345 MethodUtilities::GetNormMethod<TDataType>(rVariable, rNormType);
349 double variance = 0.0;
352 for (
int i = 0; i < static_cast<int>(r_container.size()); ++
i)
354 const TContainerItemType& r_item = *(r_container.begin() +
i);
355 const TDataType& current_value =
356 TDataRetrievalFunctor<TContainerItemType>()(r_item, rVariable);
358 variance += std::pow(norm_method(current_value), 2);
361 global_variance += variance;
365 const double number_of_items =
367 static_cast<double>(r_container.size()));
369 if (number_of_items > 0)
371 global_variance *= (1.0 / number_of_items);
375 return std::make_tuple<double, double>(
376 std::forward<double>(mean), std::forward<double>(global_variance));
379 template <
class TDataType>
383 const std::string& rNormType,
388 const TContainerType& r_container =
389 MethodUtilities::GetLocalDataContainer<TContainerType>(rModelPart);
391 double global_max = std::numeric_limits<double>::lowest();
392 unsigned int global_id = 0;
393 const auto& norm_method =
394 MethodUtilities::GetNormMethod<TDataType>(rVariable, rNormType);
398 double current_max = std::numeric_limits<double>::lowest();
401 for (
int i = 0; i < static_cast<int>(r_container.size()); ++
i)
403 const TContainerItemType& r_item = *(r_container.begin() +
i);
404 const TDataType& current_value =
405 TDataRetrievalFunctor<TContainerItemType>()(r_item, rVariable);
406 const double value_norm = norm_method(current_value);
407 if (value_norm > current_max)
409 current_max = value_norm;
415 if (current_max > global_max)
417 global_max = current_max;
425 const auto& global_max_value_array =
426 r_data_communicator.AllGather(std::vector<double>{global_max});
427 const auto& global_max_id_array =
428 r_data_communicator.AllGather(std::vector<unsigned int>{global_id});
430 for (std::size_t
i = 0;
i < global_max_value_array.size(); ++
i)
432 if (global_max_value_array[
i] > global_max)
434 global_max = global_max_value_array[
i];
435 global_id = global_max_id_array[
i];
439 return std::make_tuple<double, unsigned int>(
440 std::forward<double>(global_max), std::forward<unsigned int>(global_id));
445 template <
class TDataType>
449 const std::string& rNormType,
454 const TContainerType& r_container =
455 MethodUtilities::GetLocalDataContainer<TContainerType>(rModelPart);
458 unsigned int global_id = 0;
459 const auto& norm_method =
460 MethodUtilities::GetNormMethod<TDataType>(rVariable, rNormType);
467 for (
int i = 0; i < static_cast<int>(r_container.size()); ++
i)
469 const TContainerItemType& r_item = *(r_container.begin() +
i);
470 const TDataType& current_value =
471 TDataRetrievalFunctor<TContainerItemType>()(r_item, rVariable);
472 const double value_norm = norm_method(current_value);
473 if (value_norm < current_min)
475 current_min = value_norm;
481 if (current_min < global_min)
483 global_min = current_min;
491 const auto& global_min_value_array =
492 r_data_communicator.AllGather(std::vector<double>{global_min});
493 const auto& global_min_id_array =
494 r_data_communicator.AllGather(std::vector<unsigned int>{global_id});
496 for (std::size_t
i = 0;
i < global_min_value_array.size(); ++
i)
498 if (global_min_value_array[
i] < global_min)
500 global_min = global_min_value_array[
i];
501 global_id = global_min_id_array[
i];
505 return std::make_tuple<double, unsigned int>(
506 std::forward<double>(global_min), std::forward<unsigned int>(global_id));
511 template <
class TDataType>
515 const std::string& rNormType,
520 const TContainerType& r_container =
521 MethodUtilities::GetLocalDataContainer<TContainerType>(rModelPart);
523 const auto& norm_method =
524 MethodUtilities::GetNormMethod<TDataType>(rVariable, rNormType);
526 std::vector<double> local_values;
527 local_values.resize(r_container.size());
528 local_values.shrink_to_fit();
530 #pragma omp parallel for
531 for (
int i = 0; i < static_cast<int>(r_container.size()); ++
i)
533 const TContainerItemType& r_item = *(r_container.begin() +
i);
534 const TDataType& current_value =
535 TDataRetrievalFunctor<TContainerItemType>()(r_item, rVariable);
536 local_values[
i] = norm_method(current_value);
539 std::sort(local_values.begin(), local_values.end());
543 const std::vector<std::vector<double>>& global_values =
544 r_data_communicator.Gatherv(local_values, 0);
547 if (r_data_communicator.
Rank() == 0)
549 const std::vector<double>& sorted_values_list =
551 const int number_of_values = sorted_values_list.size();
553 if (number_of_values > 0)
555 if (number_of_values % 2 != 0)
557 median = sorted_values_list[number_of_values / 2];
561 median = (sorted_values_list[(number_of_values - 1) / 2] +
562 sorted_values_list[number_of_values / 2]) *
568 r_data_communicator.
Broadcast(median, 0);
574 template <
class TDataType>
575 std::tuple<double, double, std::vector<double>, std::vector<int>, std::vector<double>, std::vector<double>, std::vector<double>>
static GetNormDistribution(
579 const std::string& rNormType,
586 "number_of_value_groups" : 10,
591 if (Params.
Has(
"min_value") && Params[
"min_value"].
IsDouble())
593 default_parameters[
"min_value"].SetDouble(0.0);
595 if (Params.
Has(
"max_value") && Params[
"max_value"].
IsDouble())
597 default_parameters[
"max_value"].SetDouble(0.0);
601 double min_value{0.0};
602 if (Params[
"min_value"].IsDouble())
604 min_value = Params[
"min_value"].
GetDouble();
607 Params[
"min_value"].IsString() &&
608 Params[
"min_value"].GetString() ==
"min")
610 const auto& min_data =
611 GetNormMin<TDataType>(rModelPart, rVariable, rNormType, Params);
612 min_value = std::get<0>(min_data);
616 KRATOS_ERROR <<
"Unknown min_value. Allowed only double or \"min\" "
617 "string as a value. [ min_value = "
618 << Params[
"min_value"] <<
" ]\n.";
621 double max_value{0.0};
622 if (Params[
"max_value"].IsDouble())
624 max_value = Params[
"max_value"].
GetDouble();
627 Params[
"max_value"].IsString() &&
628 Params[
"max_value"].GetString() ==
"max")
630 const auto& max_data =
631 GetNormMax<TDataType>(rModelPart, rVariable, rNormType, Params);
632 max_value = std::get<0>(max_data);
636 KRATOS_ERROR <<
"Unknown max_value. Allowed only double or \"max\" "
637 "string as a value. [ max_value = "
638 << Params[
"max_value"] <<
" ]\n.";
641 const int number_of_groups = Params[
"number_of_value_groups"].
GetInt();
643 const TContainerType& r_container =
644 MethodUtilities::GetLocalDataContainer<TContainerType>(rModelPart);
646 const auto& norm_method =
647 MethodUtilities::GetNormMethod<TDataType>(rVariable, rNormType);
649 std::vector<double> group_limits;
650 for (
int i = 0;
i < number_of_groups + 1; ++
i)
652 group_limits.push_back(
653 min_value + (max_value - min_value) *
static_cast<double>(
i) /
654 static_cast<double>(number_of_groups));
660 group_limits[group_limits.size() - 1] += 1
e-16;
663 group_limits.shrink_to_fit();
664 const int number_of_limits = group_limits.size();
666 std::vector<int> distribution;
667 std::vector<double> group_means, group_variances;
668 for (
int i = 0;
i < number_of_limits; ++
i)
670 distribution.push_back(0);
671 group_means.push_back(0.0);
672 group_variances.push_back(0.0);
674 distribution.shrink_to_fit();
675 group_means.shrink_to_fit();
676 group_variances.shrink_to_fit();
680 std::vector<int> local_distribution;
681 std::vector<double> local_means, local_variances;
682 for (
int i = 0;
i < number_of_limits; ++
i)
684 local_distribution.push_back(0);
685 local_means.push_back(0.0);
686 local_variances.push_back(0.0);
688 local_distribution.shrink_to_fit();
689 local_means.shrink_to_fit();
690 local_variances.shrink_to_fit();
693 for (
int i = 0; i < static_cast<int>(r_container.size()); ++
i)
695 const TContainerItemType& r_item = *(r_container.begin() +
i);
696 const TDataType& current_value =
697 TDataRetrievalFunctor<TContainerItemType>()(r_item, rVariable);
698 const double value_norm = norm_method(current_value);
699 for (
int i = 0;
i < number_of_limits; ++
i)
701 if (value_norm < group_limits[
i])
703 ++local_distribution[
i];
704 local_means[
i] += value_norm;
705 local_variances[
i] += std::pow(value_norm, 2);
712 for (
int i = 0;
i < number_of_limits; ++
i)
714 distribution[
i] += local_distribution[
i];
715 group_means[
i] += local_means[
i];
716 group_variances[
i] += local_variances[
i];
721 std::vector<int> global_distribution =
723 std::vector<double> global_mean_distribution =
725 std::vector<double> global_variance_distribution =
728 const double number_of_items =
static_cast<double>(
std::max(
729 std::accumulate(global_distribution.begin(), global_distribution.end(), 0), 1));
730 std::vector<double> global_percentage_distributions;
731 for (
int i = 0;
i < number_of_limits; ++
i)
733 const double number_of_values_in_group =
734 static_cast<double>(global_distribution[
i]);
735 global_percentage_distributions.push_back(number_of_values_in_group / number_of_items);
736 if (number_of_values_in_group > 0.0)
738 global_mean_distribution[
i] /= number_of_values_in_group;
739 global_variance_distribution[
i] /= number_of_values_in_group;
740 global_variance_distribution[
i] -=
741 std::pow(global_mean_distribution[
i], 2);
746 group_limits[group_limits.size() - 2] -= 1
e-16;
747 group_limits[group_limits.size() - 1] = max_value;
749 return std::make_tuple<
751 std::vector<double>, std::vector<double>, std::vector<double>>(
752 std::forward<double>(min_value), std::forward<double>(max_value),
753 std::forward<std::vector<double>>(group_limits),
754 std::forward<std::vector<int>>(global_distribution),
755 std::forward<std::vector<double>>(global_percentage_distributions),
756 std::forward<std::vector<double>>(global_mean_distribution),
757 std::forward<std::vector<double>>(global_variance_distribution));
virtual const DataCommunicator & GetDataCommunicator() const
Definition: communicator.cpp:340
Base class for all Conditions.
Definition: condition.h:59
Serial (do-nothing) version of a wrapper class for MPI communication.
Definition: data_communicator.h:318
virtual int Rank() const
Get the parallel rank for this DataCommunicator.
Definition: data_communicator.h:587
void Broadcast(TObject &rBroadcastObject, const int SourceRank) const
Synchronize a buffer to the value held by the broadcasting rank.
Definition: data_communicator.h:473
Base class for all Elements.
Definition: element.h:60
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
MeshType::ConditionsContainerType ConditionsContainerType
Condintions container. A vector set of Conditions with their Id's as key.
Definition: model_part.h:183
MeshType::ElementsContainerType ElementsContainerType
Element container. A vector set of Elements with their Id's as key.
Definition: model_part.h:168
Element ElementType
Definition: model_part.h:120
Communicator & GetCommunicator()
Definition: model_part.h:1821
Node NodeType
Definition: model_part.h:117
MeshType::NodesContainerType NodesContainerType
Nodes container. Which is a vector set of nodes with their Id's as key.
Definition: model_part.h:128
Condition ConditionType
Definition: model_part.h:121
This class defines the node.
Definition: node.h:65
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
double GetDouble() const
This method returns the double contained in the current Parameter.
Definition: kratos_parameters.cpp:657
int GetInt() const
This method returns the integer contained in the current Parameter.
Definition: kratos_parameters.cpp:666
bool IsDouble() const
This method checks if the parameter is a double.
Definition: kratos_parameters.cpp:544
void RecursivelyValidateAndAssignDefaults(const Parameters &rDefaultParameters)
This function is designed to verify that the parameters under testing match the form prescribed by th...
Definition: kratos_parameters.cpp:1389
bool Has(const std::string &rEntry) const
This method checks if the Parameter contains a certain entry.
Definition: kratos_parameters.cpp:520
Definition: spatial_methods.h:783
Definition: spatial_methods.h:46
static double CalculateNormRootMeanSquare(const ModelPart &rModelPart, const Variable< TDataType > &rVariable, const std::string &rNormType, Parameters Params)
Definition: spatial_methods.h:199
static TDataType CalculateRootMeanSquare(const ModelPart &rModelPart, const Variable< TDataType > &rVariable)
Definition: spatial_methods.h:149
static int CalculateSum(const ModelPart &rModelPart, const Flags &rVariable)
Definition: spatial_methods.h:49
static double CalculateNormMean(const ModelPart &rModelPart, const Variable< TDataType > &rVariable, const std::string &rNormType, Parameters Params)
Definition: spatial_methods.h:253
static std::tuple< double, std::size_t > GetNormMax(const ModelPart &rModelPart, const Variable< TDataType > &rVariable, const std::string &rNormType, Parameters Params)
Definition: spatial_methods.h:380
static double CalculateNormSum(const ModelPart &rModelPart, const Variable< TDataType > &rVariable, const std::string &rNormType, Parameters Params)
Definition: spatial_methods.h:112
static std::tuple< double, double, std::vector< double >, std::vector< int >, std::vector< double >, std::vector< double >, std::vector< double > > GetNormDistribution(const ModelPart &rModelPart, const Variable< TDataType > &rVariable, const std::string &rNormType, Parameters Params)
Definition: spatial_methods.h:575
static std::tuple< double, double > CalculateNormVariance(const ModelPart &rModelPart, const Variable< TDataType > &rVariable, const std::string &rNormType, Parameters Params)
Definition: spatial_methods.h:332
static TDataType CalculateSum(const ModelPart &rModelPart, const Variable< TDataType > &rVariable)
Definition: spatial_methods.h:67
static std::tuple< double, std::size_t > GetNormMin(const ModelPart &rModelPart, const Variable< TDataType > &rVariable, const std::string &rNormType, Parameters Params)
Definition: spatial_methods.h:446
static std::tuple< TDataType, TDataType > CalculateVariance(const ModelPart &rModelPart, const Variable< TDataType > &rVariable)
Definition: spatial_methods.h:277
static double GetNormMedian(const ModelPart &rModelPart, const Variable< TDataType > &rVariable, const std::string &rNormType, Parameters Params)
Definition: spatial_methods.h:512
static TDataType CalculateMean(const ModelPart &rModelPart, const Variable< TDataType > &rVariable)
Definition: spatial_methods.h:239
Definition: spatial_methods.h:788
Definition: spatial_methods.h:773
Definition: spatial_methods.h:778
Variable class contains all information needed to store and retrive data from a data container.
Definition: variable.h:63
const TDataType & Zero() const
This method returns the zero value of the variable type.
Definition: variable.h:346
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR
Definition: exception.h:161
static double max(double a, double b)
Definition: GeometryFunctions.h:79
std::vector< double > SortSortedValuesList(const std::vector< std::vector< double >> &rValues)
Definition: method_utilities.cpp:725
void DataTypeSizeChecker(const TDataType &rData, const TDataType &rReferenceData)
Definition: method_utilities.cpp:131
TDataType RaiseToPower(const TDataType &rData, const double Power)
Definition: method_utilities.cpp:34
void DataTypeSizeInitializer(TDataType &rData, const TDataType &rReferenceData)
Definition: method_utilities.cpp:82
ModelPart::ElementsContainerType ElementsContainerType
Definition: spatial_methods.h:768
ModelPart::ConditionsContainerType ConditionsContainerType
Definition: spatial_methods.h:769
ModelPart::NodesContainerType NodesContainerType
Definition: spatial_methods.h:767
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
TExpressionType::data_type sum(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression)
Definition: amatrix_interface.h:675
TABLE_NUMBER_ANGULAR_VELOCITY TABLE_NUMBER_MOMENT I33 BEAM_INERTIA_ROT_UNIT_LENGHT_Y KRATOS_DEFINE_APPLICATION_VARIABLE(DEM_APPLICATION, double, BEAM_INERTIA_ROT_UNIT_LENGHT_Z) typedef std double
Definition: DEM_application_variables.h:182
int current_id
Output settings end ####.
Definition: script.py:194
integer i
Definition: TensorModule.f:17
e
Definition: run_cpp_mpi_tests.py:31