CArl
Code Arlequin / C++ implementation
common_functions.cpp
Go to the documentation of this file.
1 #include "common_functions.h"
2 
3 int kronecker_delta(unsigned int i,
4  unsigned int j)
5 {
6  return i == j ? 1. : 0.;
7 };
8 
10 {
11  switch (input)
12  {
13  case ClusterSchedulerType::LOCAL : return "LOCAL";
14  break;
15 
16  case ClusterSchedulerType::PBS : return "PBS";
17  break;
18 
19  case ClusterSchedulerType::SLURM : return "SLURM";
20  break;
21  }
22 
23  homemade_error_msg("Invalid enumerate argument!");
24  return "";
25 };
26 
28 {
29  switch (input)
30  {
31  case BaseCGPrecondType::NO_PRECONDITIONER : return "NONE";
32  break;
33 
34  case BaseCGPrecondType::COUPLING_OPERATOR : return "Coupling_operator";
35  break;
36 
37  case BaseCGPrecondType::COUPLING_JACOBI : return "Coupling_operator_jacobi";
38  break;
39  }
40 
41  homemade_error_msg("Invalid enumerate argument!");
42  return "";
43 };
44 
46 {
47  switch (input)
48  {
49  case ExtSolverType::LIBMESH_LINEAR : return "LIBMESH_LINEAR";
50  break;
51 
52  case ExtSolverType::DUMMY : return "DUMMY";
53  break;
54  }
55 
56  homemade_error_msg("Invalid enumerate argument!");
57  return "";
58 };
59 
60 std::string carl::exec_command(const std::string& cmd) {
61  std::array<char, 128> buffer;
62  std::string result;
63  std::shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
64  if (!pipe) throw std::runtime_error("popen() failed!");
65  while (!feof(pipe.get())) {
66  if (fgets(buffer.data(), 128, pipe.get()) != NULL)
67  result += buffer.data();
68  }
69  return result;
70 }
71 
73  const std::unordered_map<int,int>& input_map,
74  std::unordered_map<int,int>& output_map)
75 {
76  int map_length = input_map.size();
77  output_map.reserve(map_length);
78 
79  std::unordered_map<int,int>::const_iterator mapIt = input_map.begin();
80  std::unordered_map<int,int>::const_iterator end_mapIt = input_map.end();
81 
82  for( ; mapIt != end_mapIt; ++mapIt)
83  {
84  output_map[mapIt->second] = mapIt->first;
85  }
86 }
87 
88 void clear_line()
89 {
90  std::cout << '\r' << " " << "\r";
91 };
92 
93 int carl::voigt_index_converter(int aaa, int bbb)
94 {
95  if(aaa == bbb)
96  {
97  // 00 -> 0, 11 -> 1, 22 -> 2
98  return aaa;
99  }
100  else
101  {
102  // 12, 21 -> 3
103  // 02, 20 -> 4
104  // 01, 10 -> 5
105  int sum = aaa + bbb;
106 
107  if(sum == 3)
108  {
109  return 3;
110  }
111  else if(sum == 2)
112  {
113  return 4;
114  }
115  else if(sum == 1)
116  {
117  return 5;
118  }
119  }
120 
121  std::cerr << "Bad indexes! " << aaa << " " << bbb << std::endl;
122  homemade_error_msg(" You shouldn't be here! (voigt_index_converter)");
123  return -1;
124 };
125 
126 void carl::print_stats_to_file(std::vector<double>& vec_data, const std::string filename)
127 {
128  std::ofstream output_stream(filename,std::ofstream::app);
129  libMesh::StatisticsVector<double> statistics_vec(vec_data.size(),0);
130  for(unsigned int iii = 0; iii < vec_data.size(); ++iii)
131  {
132  statistics_vec[iii] = vec_data[iii];
133  }
134 
135  output_stream << statistics_vec.minimum() << " "
136  << statistics_vec.maximum() << " "
137  << statistics_vec.mean() << " "
138  << statistics_vec.median() << " "
139  << statistics_vec.stddev() << std::endl;
140 
141  output_stream.close();
142 };
int voigt_index_converter(int aaa, int bbb)
#define homemade_error_msg(msg)
Definition: common_header.h:73
void clear_line()
std::string exec_command(const std::string &cmd)
int kronecker_delta(unsigned int i, unsigned int j)
void print_stats_to_file(std::vector< double > &vec_data, const std::string filename)
ClusterSchedulerType
Definition: common_enums.h:14
void invert_index_unordered_map(const std::unordered_map< int, int > &input_map, std::unordered_map< int, int > &output_map)
std::string ClusterSchedulerType_to_string(ClusterSchedulerType input)
BaseCGPrecondType
Definition: common_enums.h:35
ExtSolverType
Definition: common_enums.h:22
std::string ExtSolverType_to_string(ExtSolverType input)
std::string BaseCGPrecondType_to_string(BaseCGPrecondType input)