deal2lkit: A ToolKit library for Deal.II
utilities.h
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 #ifndef _d2k_utilities_h
17 #define _d2k_utilities_h
18 
19 #include <deal2lkit/config.h>
20 #include <deal.II/base/utilities.h>
23 #include <deal.II/base/timer.h>
24 #include <deal.II/fe/fe.h>
26 
27 #include <typeinfo>
28 #include <cxxabi.h>
29 #include <sstream>
30 #include <sys/ioctl.h> // to know the number of cols and rows of a shell
31 #include <chrono> // for TimeUtilities std::chrono
32 #include <stdio.h>
33 
35 DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
36 #ifdef DEAL_II_WITH_TRILINOS
40 #include <Teuchos_TimeMonitor.hpp>
41 #endif
42 #ifdef DEAL_II_WITH_PETSC
46 #endif
47 DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
48 #include <deal.II/base/utilities.h>
49 
50 #ifdef D2K_WITH_SUNDIALS
51 #include <nvector/nvector_serial.h>
52 #ifdef DEAL_II_WITH_MPI
53 #include <nvector/nvector_parallel.h>
54 #endif
55 #endif
56 
57 
58 #include <deal.II/base/index_set.h>
59 using namespace dealii;
60 using std_cxx11::shared_ptr;
61 
62 
63 D2K_NAMESPACE_OPEN
64 
65 
67 std::string demangle(const char *name);
68 
76 {
77 public:
78 
80  :
81  status(true),
82  times()
83  {}
84 
88  void sleep(unsigned int t);
89 
93  void get_start_time();
94 
98  void get_end_time();
99 
103  int get_num_measures();
104 
108  double &operator[] (const unsigned int num)
109  {
110  AssertThrow( num < times.size(),
111  ExcMessage("Invalind number num. It is higher than the number of times.") );
112 
113  return times[num];
114  };
115 
116 private:
117  bool status; // This is used to check whether we are taking a start time or
118  // and end time.
119  std::chrono::high_resolution_clock::time_point t_start;
120  std::chrono::high_resolution_clock::time_point t_end;
121  std::vector<double> times;
122 };
123 
128 void append_to_file(const std::string &in_file, const std::string &out_file);
129 
136 unsigned int get_next_available_index_directory_name(const std::string &base, int n_digits=3,
137  unsigned int start=0, unsigned int index_max = 1000);
138 
145 std::string get_next_available_directory_name(const std::string &base, int n_digits=3,
146  unsigned int start=0, unsigned int index_max = 1000);
147 
151 bool dir_exists(const std::string &dir);
152 
156 bool file_exists(const std::string &file);
157 
161 bool create_directory(const std::string &name);
162 
167 bool copy_files(const std::string &files, const std::string &destination);
168 
172 bool copy_file(const std::string &files, const std::string &destination);
173 
177 bool rename_file(const std::string &file, const std::string &new_file);
178 
180 DeclException1(ExcCannottExecuteCommand, std::string,
181  << "Cannot execute command " << arg1 << "\n Please verify you have "
182  << "the needed permissions.");
183 
184 // Forward declaration for OverWriteStream:
185 template<typename Stream = std::ostream> class OverWriteStream;
196 template<typename Stream>
197 class OverWriteStream
198 {
199 public:
200  OverWriteStream( unsigned int n_lines = 1,
201  Stream &stream_out = std::cout,
202  unsigned int width = 60)
203  :
204  n_lines(n_lines),
205  width(width),
206  current_line(0),
207  clear_next(true),
208  stream_out(stream_out)
209  {
210  struct winsize w;
211  ioctl(0, TIOCGWINSZ, &w);
212  rows_shell = w.ws_row;
213  cols_shell = w.ws_col;
214  stream_out.width(width);
215  };
216 
222  {
223  for (; current_line<n_lines; ++current_line)
224  stream_out << std::endl;
225  };
226 
227  template<typename OBJ>
229  {
230  clear();
231  stream_out << o;
232  return *this;
233  }
234 
235  OverWriteStream<Stream> &operator<< (std::ostream& (*p) (std::ostream &))
236  {
237  class QueryStreambuf : public std::streambuf
238  {
239  // Implement a minimalistic stream buffer that only stores the fact
240  // whether overflow or sync was called
241  public:
242  QueryStreambuf()
243  : flushed_(false), newline_written_(false)
244  {
245  }
246  bool flushed()
247  {
248  return flushed_;
249  }
250  bool newline_written()
251  {
252  return newline_written_;
253  }
254  private:
255  int_type overflow(int_type ch)
256  {
257  newline_written_ = true;
258  return ch;
259  }
260  int sync()
261  {
262  flushed_ = true;
263  return 0;
264  }
265 
266  bool flushed_;
267  bool newline_written_;
268  } query_streambuf;
269 
270  {
271  // and initialize an ostream with this streambuf:
272  std::ostream inject (&query_streambuf);
273  inject << p;
274  }
275 
276  if (query_streambuf.newline_written())
277  current_line++;
278 
279  if (current_line == n_lines)
280  {
281  current_line = 0;
282  for (unsigned int i=0; i<n_lines; ++i)
283  stream_out << "\033[A" << "\r";
284  clear_next = true;
285  }
286 
287  stream_out << p;
288 
289  return *this;
290  };
291 
292  template <typename S, typename T> friend OverWriteStream<S>
293  &operator << (OverWriteStream<S> &, const T &);
294 
295  Stream &get_stream()
296  {
297  return stream_out;
298  };
299 
305  void end()
306  {
307  while (current_line <= n_lines-1)
308  {
309  stream_out << std::endl;
310  current_line++;
311  };
312  };
313 
320  void clear(bool force=false)
321  {
322  if (force)
323  {
324  while (current_line < n_lines-1)
325  {
326  stream_out << "\033[B";
327  current_line++;
328  };
329 
330  if (current_line>0)
331  {
332  for (unsigned int i=0; i<n_lines; ++i)
333  {
334  stream_out << "\r" << std::setfill(' ') << std::setw(width) << " " << "\r" << "\033[A";
335  current_line--;
336  if (current_line==0)
337  break;
338  }
339  stream_out << "\r" << std::setfill(' ') << std::setw(width) << " " << "\r";
340  }
341  else
342  {
343  stream_out << "\r" << std::setfill(' ') << std::setw(width) << " " << "\r";
344  }
345  clear_next = false;
346  }
347  else if (clear_next)
348  {
349  stream_out << "\r" << std::setfill(' ') << std::setw(width) << " " << "\r";
350  stream_out << "\033[A" << std::endl;
351  clear_next = false;
352  }
353  };
354 
358  unsigned int get_shell_rows()
359  {
360  return rows_shell;
361  }
362 
366  unsigned int get_shell_cols()
367  {
368  return cols_shell;
369  }
370 
375  {
376  return current_line;
377  }
378 
379 private:
380  unsigned int cols_shell;
381  unsigned int rows_shell;
382  // total number of lines:
383  const unsigned int n_lines;
384  const unsigned int width;
386  // the current line:
387  unsigned int current_line;
388  // stream where the output will be written
389  Stream &stream_out;
390 
391 };
392 
393 template <typename S, typename T>
394 inline
395 OverWriteStream<S> &operator<< (OverWriteStream<S> &output_stream, const T &t)
396 {
397  output_stream.clear();
398  output_stream.get_stream() << t;
399  return output_stream;
400 }
401 
402 // ======================================================================
403 // Explicit template functions. Only present in the include file.
404 // ======================================================================
405 
411 template<class T>
412 std::vector<T> unique(const std::vector<T> &myvector)
413 {
414  std::vector<T> ret;
415  std::unique_copy(myvector.begin(), myvector.end(), std::back_inserter(ret));
416  return ret;
417 }
418 
419 
424 template<class Type>
425 std::string print(const std::vector<Type> &list, const std::string sep=",")
426 {
427  std::stringstream ret;
428  if (list.size() > 0)
429  ret << list[0];
430 
431  for (unsigned int i=1; i<list.size(); ++i)
432  ret << sep << list[i];
433 
434  return ret.str();
435 }
436 
437 
442 template<int dim>
443 std::string print(const Point<dim> &point, const std::string sep=",")
444 {
445  std::stringstream ret;
446  ret << point[0];
447 
448  for (unsigned int i=1; i<dim; ++i)
449  ret << sep << point[i];
450 
451  return ret.str();
452 }
453 
454 
455 
456 
460 template <class T>
461 std::string type(const T &t)
462 {
463  return demangle(typeid(t).name());
464 }
465 
481 template <class T>
482 inline shared_ptr<T>
483 SP(T *t)
484 {
485  return shared_ptr<T>(t);
486 }
487 
503 template <class T>
504 inline shared_ptr<const T>
505 SP(const T *t)
506 {
507  return shared_ptr<const T>(t);
508 }
509 
515 template <typename VEC>
516 void vector_shift(VEC &in_vec, double a_scalar)
517 {
518  for (auto i : in_vec.locally_owned_elements())
519  in_vec[i] += a_scalar;
520 }
521 
522 #ifdef D2K_WITH_SUNDIALS
523 
524 #ifdef DEAL_II_WITH_MPI
525 
526 #ifdef DEAL_II_WITH_TRILINOS
527 void copy(TrilinosWrappers::MPI::Vector &dst, const N_Vector &src);
528 void copy(N_Vector &dst, const TrilinosWrappers::MPI::Vector &src);
529 void copy(TrilinosWrappers::MPI::BlockVector &dst, const N_Vector &src);
530 void copy(N_Vector &dst, const TrilinosWrappers::MPI::BlockVector &src);
531 #endif // DEAL_II_WITH_TRILINOS
532 
533 #ifdef DEAL_II_WITH_PETSC
534 void copy(PETScWrappers::MPI::Vector &dst, const N_Vector &src);
535 void copy(N_Vector &dst, const PETScWrappers::MPI::Vector &src);
536 void copy(PETScWrappers::MPI::BlockVector &dst, const N_Vector &src);
537 void copy(N_Vector &dst, const PETScWrappers::MPI::BlockVector &src);
538 #endif // DEAL_II_WITH_PETSC
539 
540 #endif
541 
542 void copy(BlockVector<double> &dst, const N_Vector &src);
543 void copy(N_Vector &dst, const BlockVector<double> &src);
544 
545 #endif
546 
547 #ifdef DEAL_II_WITH_TRILINOS
548 
555 class TimeMonitor
556 {
557 public:
561  TimeMonitor(const MPI_Comm &comm=MPI_COMM_WORLD,
562  std::ostream &stream=std::cout) :
563  outstream(stream),
564  out(outstream, Utilities::MPI::this_mpi_process(comm) == 0),
565  dealii_timer(comm, out, TimerOutput::never, TimerOutput::cpu_and_wall_times)
566  {};
567 
571  ~TimeMonitor()
572  {
573  dealii_timer.print_summary();
574  Teuchos::TimeMonitor::summarize(outstream);
575  }
576 
582  class Scope
583  {
584  public:
588  Scope(Teuchos::Time &trilinos_time,
589  TimerOutput &dealii_timer_output,
590  const std::string &section) :
591  trilinos_scoped_monitor(trilinos_time),
592  dealii_scoped_monitor(dealii_timer_output, section)
593  {};
594 
595  private:
599  Teuchos::TimeMonitor trilinos_scoped_monitor;
600 
604  TimerOutput::Scope dealii_scoped_monitor;
605  };
606 
612  Scope
613  scoped_timer(const std::string &section) const
614  {
615  if (timers.find(section) == timers.end())
616  timers[section] = Teuchos::TimeMonitor::getNewCounter(section);
617 
618  return Scope(*(timers[section]), dealii_timer, section);
619  }
620 
621 private:
625  std::ostream &outstream;
626 
630  ConditionalOStream out;
631 
635  mutable std::map<std::string, Teuchos::RCP<Teuchos::Time> > timers;
636 
640  mutable TimerOutput dealii_timer;
641 };
642 #endif
643 
644 D2K_NAMESPACE_CLOSE
645 
646 #endif
647 
OverWriteStream(unsigned int n_lines=1, Stream &stream_out=std::cout, unsigned int width=60)
Definition: utilities.h:200
void copy(BlockVector< double > &dst, const N_Vector &src)
Definition: utilities.cc:258
unsigned int current_line
Definition: utilities.h:387
unsigned int cols_shell
Definition: utilities.h:380
unsigned int rows_shell
Definition: utilities.h:381
unsigned int get_next_available_index_directory_name(const std::string &base, int n_digits=3, unsigned int start=0, unsigned int index_max=1000)
A function that return the index of the first non existing folder matching a pattern make by base and...
Definition: utilities.cc:89
int get_current_line()
It returns current line of the stream.
Definition: utilities.h:374
DeclException1(ExcCannottExecuteCommand, std::string,<< "Cannot execute command "<< arg1<< "\Please verify you have "<< "the needed permissions.")
Cannot execute std::system(command)
#define AssertThrow(cond, exc)
shared_ptr< T > SP(T *t)
Construct a shared pointer to a non const class T.
Definition: utilities.h:483
This function collects some time utilities.
Definition: utilities.h:75
unsigned int get_shell_rows()
It returns the number of rows of the current shell.
Definition: utilities.h:358
bool file_exists(const std::string &file)
A function to check the existence of file file.
Definition: utilities.cc:77
std::chrono::high_resolution_clock::time_point t_end
Definition: utilities.h:120
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
static::ExceptionBase & ExcMessage(std::string arg1)
bool copy_file(const std::string &files, const std::string &destination)
A function to make a copy of file with the name destination.
Definition: utilities.cc:139
OverWriteStream< S > & operator<<(OverWriteStream< S > &output_stream, const T &t)
Definition: utilities.h:395
std::string type(const T &t)
Return a human readable name of the type passed as argument.
Definition: utilities.h:461
std::vector< double > times
Definition: utilities.h:121
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
Stream & get_stream()
Definition: utilities.h:295
This class uses n_lines lines of stream_out to show the output.
Definition: utilities.h:185
const unsigned int width
Definition: utilities.h:384
~OverWriteStream()
Flush output at the end so that we don&#39;t make a mess with the console.
Definition: utilities.h:221
std::string print(const std::vector< Type > &list, const std::string sep=",")
Return a string containing the content of the vector, with elements separated by the @ sep parameter...
Definition: utilities.h:425
Stream & stream_out
Definition: utilities.h:389
std::chrono::high_resolution_clock::time_point t_start
Definition: utilities.h:119
void vector_shift(VEC &in_vec, double a_scalar)
A simple class to shift a vector by a scalar.
Definition: utilities.h:516
std::string demangle(const char *name)
Demangle c++ names.
Definition: utilities.cc:28
std::string get_next_available_directory_name(const std::string &base, int n_digits=3, unsigned int start=0, unsigned int index_max=1000)
A function that return the name of the first non existing folder matching a pattern make by base and ...
Definition: utilities.cc:102
bool dir_exists(const std::string &dir)
A function to check the existence of dir directory.
Definition: utilities.cc:83
std::vector< T > unique(const std::vector< T > &myvector)
A simple function that accepts a vector as an input and returns a second vector containing only the u...
Definition: utilities.h:412
unsigned int this_mpi_process(const MPI_Comm &mpi_communicator)
unsigned int get_shell_cols()
It returns the number of coloumns of the current shell.
Definition: utilities.h:366
void end()
Move the cursor at the end of the output.
Definition: utilities.h:305
bool create_directory(const std::string &name)
A function to create directory.
Definition: utilities.cc:108
void clear(bool force=false)
Clear the next n lines, return back to the original point, and reset the clear flag if force is set t...
Definition: utilities.h:320
const unsigned int n_lines
Definition: utilities.h:383
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