deal2lkit: A ToolKit library for Deal.II
utilities.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------
2 //
3 // Copyright (C) 2015 by the deal2lkit authors
4 //
5 // This file is part of the deal2lkit library.
6 //
7 // The deal2lkit library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal2lkit distribution.
13 //
14 //-----------------------------------------------------------
15 
16 #include <deal2lkit/utilities.h>
17 #include <vector>
18 #include <fstream>
19 #include <thread>
20 
21 #include <stdlib.h>
22 #include <sys/stat.h>
23 
24 
25 D2K_NAMESPACE_OPEN
26 
27 
28 std::string demangle(const char *name)
29 {
30  int status = -4; // some arbitrary value to eliminate the compiler warning
31  std::string result( abi::__cxa_demangle(name, NULL, NULL, &status) );
32  return (status==0) ? result.c_str() : name ;
33 }
34 
35 void TimeUtilities::sleep(unsigned int t)
36 {
37  std::this_thread::sleep_for(std::chrono::milliseconds(t));
38 }
39 
41 {
42  AssertThrow(status == true,
43  ExcMessage("Use get_end_time() before reuse get_start_time().") );
44 
45  t_start = std::chrono::high_resolution_clock::now();
46 
47  status = false;
48 }
49 
51 {
52  AssertThrow(status == false,
53  ExcMessage("Use get_start_time() before get_end_time().") );
54 
55  t_end = std::chrono::high_resolution_clock::now();
56  std::chrono::duration<double> time_span = std::chrono::duration_cast<std::chrono::duration<double>>(t_end - t_start);
57  times.push_back( time_span.count() );
58 
59  status = true;
60 }
61 
63 {
64  return times.size();
65 }
66 
67 void append_to_file(const std::string &in_file, const std::string &out_file)
68 {
69  std::ifstream ifile(in_file, std::ios::in);
70  std::ofstream ofile(out_file, std::ios::out | std::ios::app);
71  if (ifile.is_open())
72  ofile << ifile.rdbuf();
73 
74  return;
75 }
76 
77 bool file_exists(const std::string &file)
78 {
79  struct stat st;
80  return (stat (file.c_str(), &st) == 0);
81 }
82 
83 bool dir_exists(const std::string &dir)
84 {
85  struct stat st;
86  return (stat (dir.c_str(), &st) == 0);
87 }
88 
89 unsigned int get_next_available_index_directory_name(const std::string &base, int n_digits, unsigned int start, unsigned int index_max)
90 {
91  if (start<index_max)
92  {
93  if ( dir_exists( base + dealii::Utilities::int_to_string (start, n_digits) ) )
94  return get_next_available_index_directory_name(base, n_digits, ++start, index_max);
95  else
96  return start;
97  }
98  else
99  return index_max;
100 }
101 
102 std::string get_next_available_directory_name(const std::string &base, int n_digits, unsigned int start, unsigned int index_max)
103 {
104  unsigned int index = get_next_available_index_directory_name(base, n_digits, start, index_max);
105  return base + dealii::Utilities::int_to_string (index, n_digits);
106 }
107 
108 bool create_directory(const std::string &name)
109 {
110  Assert((std::find(name.begin(), name.end(), ' ') == name.end()),
111  ExcMessage("Invalid name of directory."));
112  std::string cmd = "mkdir -p " + name;
113  int status;
114  status = std::system(cmd.c_str());
115  AssertThrow(status == 0, ExcCannottExecuteCommand(cmd));
116  return dir_exists(name);
117 }
118 
119 bool copy_files(const std::string &files, const std::string &destination)
120 {
121  create_directory("./"+destination);
122  bool result = true;
123  int status;
124  std::vector<std::string> strs;
125  std::string new_file;
126  strs = dealii::Utilities::split_string_list(files, ' ');
127  for (size_t i = 0; i < strs.size(); i++)
128  {
129  Assert(file_exists(strs[i]), ExcMessage("Invalid name of file"));
130  new_file = destination+"/"+strs[i];
131  std::string cmd = "cp " + strs[i] + " " + new_file;
132  status = std::system( cmd.c_str() );
133  AssertThrow(status == 0, ExcCannottExecuteCommand(cmd));
134  result &= file_exists(new_file);
135  }
136  return result;
137 }
138 
139 bool copy_file(const std::string &file, const std::string &new_file)
140 {
141  Assert(file_exists(file),ExcMessage("No such file or directory"));
142  std::string cmd = "cp " + file + " " + new_file ;
143  int status;
144  status = std::system(cmd.c_str());
145  AssertThrow(status == 0, ExcCannottExecuteCommand(cmd));
146  return file_exists(new_file);
147 }
148 
149 bool rename_file(const std::string &file, const std::string &new_file)
150 {
151  Assert(file_exists(file),ExcMessage("No such file or directory"));
152  std::string cmd = "mv " + file + " " + new_file;
153  int status;
154  status = std::system(cmd.c_str());
155  AssertThrow(status == 0, ExcCannottExecuteCommand(cmd));
156  return file_exists(new_file);
157 }
158 
159 
160 #ifdef D2K_WITH_SUNDIALS
161 
162 #ifdef DEAL_II_WITH_MPI
163 
164 #ifdef DEAL_II_WITH_TRILINOS
165 
166 void copy(TrilinosWrappers::MPI::Vector &dst, const N_Vector &src)
167 {
168  IndexSet is = dst.locally_owned_elements();
169  AssertDimension(is.n_elements(), NV_LOCLENGTH_P(src));
170  for (unsigned int i=0; i<is.n_elements(); ++i)
171  {
172  dst[is.nth_index_in_set(i)] = NV_Ith_P(src, i);
173  }
175 }
176 
177 void copy(N_Vector &dst, const TrilinosWrappers::MPI::Vector &src)
178 {
179  IndexSet is = src.locally_owned_elements();
180  AssertDimension(is.n_elements(), NV_LOCLENGTH_P(dst));
181  for (unsigned int i=0; i<is.n_elements(); ++i)
182  {
183  NV_Ith_P(dst, i) = src[is.nth_index_in_set(i)];
184  }
185 }
186 
187 void copy(TrilinosWrappers::MPI::BlockVector &dst, const N_Vector &src)
188 {
189  IndexSet is = dst.locally_owned_elements();
190  AssertDimension(is.n_elements(), NV_LOCLENGTH_P(src));
191  for (unsigned int i=0; i<is.n_elements(); ++i)
192  {
193  dst[is.nth_index_in_set(i)] = NV_Ith_P(src, i);
194  }
196 }
197 
198 void copy(N_Vector &dst, const TrilinosWrappers::MPI::BlockVector &src)
199 {
200  IndexSet is = src.locally_owned_elements();
201  AssertDimension(is.n_elements(), NV_LOCLENGTH_P(dst));
202  for (unsigned int i=0; i<is.n_elements(); ++i)
203  {
204  NV_Ith_P(dst, i) = src[is.nth_index_in_set(i)];
205  }
206 }
207 
208 #endif //DEAL_II_WITH_TRILINOS
209 
210 #ifdef DEAL_II_WITH_PETSC
211 
212 void copy(PETScWrappers::MPI::Vector &dst, const N_Vector &src)
213 {
214  IndexSet is = dst.locally_owned_elements();
215  AssertDimension(is.n_elements(), NV_LOCLENGTH_P(src));
216  for (unsigned int i=0; i<is.n_elements(); ++i)
217  {
218  dst[is.nth_index_in_set(i)] = NV_Ith_P(src, i);
219  }
221 }
222 
223 void copy(N_Vector &dst, const PETScWrappers::MPI::Vector &src)
224 {
225  IndexSet is = src.locally_owned_elements();
226  AssertDimension(is.n_elements(), NV_LOCLENGTH_P(dst));
227  for (unsigned int i=0; i<is.n_elements(); ++i)
228  {
229  NV_Ith_P(dst, i) = src[is.nth_index_in_set(i)];
230  }
231 }
232 
233 void copy(PETScWrappers::MPI::BlockVector &dst, const N_Vector &src)
234 {
235  IndexSet is = dst.locally_owned_elements();
236  AssertDimension(is.n_elements(), NV_LOCLENGTH_P(src));
237  for (unsigned int i=0; i<is.n_elements(); ++i)
238  {
239  dst[is.nth_index_in_set(i)] = NV_Ith_P(src, i);
240  }
242 }
243 
244 void copy(N_Vector &dst, const PETScWrappers::MPI::BlockVector &src)
245 {
246  IndexSet is = src.locally_owned_elements();
247  AssertDimension(is.n_elements(), NV_LOCLENGTH_P(dst));
248  for (unsigned int i=0; i<is.n_elements(); ++i)
249  {
250  NV_Ith_P(dst, i) = src[is.nth_index_in_set(i)];
251  }
252 }
253 
254 #endif //DEAL_II_WITH_PETSC
255 
256 #endif //mpi
257 
258 void copy(BlockVector<double> &dst, const N_Vector &src)
259 {
260 #ifdef DEAL_II_WITH_MPI
261  AssertDimension((unsigned int)NV_LOCLENGTH_P(src), dst.size());
262 #else
263  AssertDimension((unsigned int)NV_LENGTH_S(src), dst.size());
264 #endif
265  for (unsigned int i=0; i<dst.size(); ++i)
266  {
267 #ifdef DEAL_II_WITH_MPI
268  dst[i] = NV_Ith_P(src, i);
269 #else
270  dst[i] = NV_Ith_S(src, i);
271 #endif
272  }
273 }
274 
275 void copy(N_Vector &dst, const BlockVector<double> &src)
276 {
277 #ifdef DEAL_II_WITH_MPI
278  AssertDimension((unsigned int)NV_LOCLENGTH_P(dst), src.size());
279 #else
280  AssertDimension((unsigned int)NV_LENGTH_S(dst), src.size());
281 #endif
282  for (unsigned int i=0; i<src.size(); ++i)
283  {
284 #ifdef DEAL_II_WITH_MPI
285  NV_Ith_P(dst, i) = src[i];
286 #else
287  NV_Ith_S(dst, i) = src[i];
288 #endif
289  }
290 }
291 
292 #endif // sundials
293 
294 
295 
296 D2K_NAMESPACE_CLOSE
297 
bool dir_exists(const std::string &dir)
A function to check the existence of dir directory.
Definition: utilities.cc:83
#define AssertDimension(dim1, dim2)
void compress(::VectorOperation::values operation)
IndexSet locally_owned_elements() const
void copy(BlockVector< double > &dst, const N_Vector &src)
Definition: utilities.cc:258
size_type n_elements() const
unsigned int get_next_available_index_directory_name(const std::string &base, int n_digits, unsigned int start, unsigned int index_max)
A function that return the index of the first non existing folder matching a pattern make by base and...
Definition: utilities.cc:89
void get_start_time()
It sets the start time for a measure.
Definition: utilities.cc:40
#define AssertThrow(cond, exc)
std::chrono::high_resolution_clock::time_point t_end
Definition: utilities.h:120
bool rename_file(const std::string &file, const std::string &new_file)
A function to rename a file with a new name new_file.
Definition: utilities.cc:149
static::ExceptionBase & ExcMessage(std::string arg1)
void compress(const VectorOperation::values operation)
std::string get_next_available_directory_name(const std::string &base, int n_digits, unsigned int start, unsigned int index_max)
A function that return the name of the first non existing folder matching a pattern make by base and ...
Definition: utilities.cc:102
#define Assert(cond, exc)
std::vector< double > times
Definition: utilities.h:121
bool file_exists(const std::string &file)
A function to check the existence of file file.
Definition: utilities.cc:77
bool copy_files(const std::string &files, const std::string &destination)
A function to copy a list of file ( "file1 file2 file3" ) in the destination folder (destination) ...
Definition: utilities.cc:119
int get_num_measures()
It returns the number of measures done.
Definition: utilities.cc:62
void sleep(unsigned int t)
It freezes the thread for t milliseconds.
Definition: utilities.cc:35
std::string demangle(const char *name)
Demangle c++ names.
Definition: utilities.cc:28
void compress(::VectorOperation::values operation)
bool create_directory(const std::string &name)
A function to create directory.
Definition: utilities.cc:108
std::chrono::high_resolution_clock::time_point t_start
Definition: utilities.h:119
void append_to_file(const std::string &in_file, const std::string &out_file)
This function copy the text contained in in_file to the file out_file .
Definition: utilities.cc:67
bool copy_file(const std::string &file, const std::string &new_file)
A function to make a copy of file with the name destination.
Definition: utilities.cc:139
IndexSet locally_owned_elements() const
void get_end_time()
It sets the end time for a measure.
Definition: utilities.cc:50
size_type nth_index_in_set(const unsigned int local_index) const
IndexSet locally_owned_elements() const