16 #ifndef _d2k_utilities_h 17 #define _d2k_utilities_h 19 #include <deal2lkit/config.h> 30 #include <sys/ioctl.h> 35 DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
36 #ifdef DEAL_II_WITH_TRILINOS 40 #include <Teuchos_TimeMonitor.hpp> 42 #ifdef DEAL_II_WITH_PETSC 47 DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
50 #ifdef D2K_WITH_SUNDIALS 51 #include <nvector/nvector_serial.h> 52 #ifdef DEAL_II_WITH_MPI 53 #include <nvector/nvector_parallel.h> 60 using std_cxx11::shared_ptr;
67 std::string
demangle(
const char *name);
88 void sleep(
unsigned int t);
93 void get_start_time();
103 int get_num_measures();
108 double &operator[] (
const unsigned int num)
111 ExcMessage(
"Invalind number num. It is higher than the number of times.") );
119 std::chrono::high_resolution_clock::time_point
t_start;
120 std::chrono::high_resolution_clock::time_point
t_end;
128 void append_to_file(
const std::string &in_file,
const std::string &out_file);
137 unsigned int start=0,
unsigned int index_max = 1000);
146 unsigned int start=0,
unsigned int index_max = 1000);
167 bool copy_files(
const std::string &files,
const std::string &destination);
172 bool copy_file(
const std::string &files,
const std::string &destination);
177 bool rename_file(
const std::string &file,
const std::string &new_file);
181 <<
"Cannot execute command " << arg1 <<
"\n Please verify you have " 182 <<
"the needed permissions.");
196 template<
typename Stream>
201 Stream &stream_out = std::cout,
202 unsigned int width = 60)
208 stream_out(stream_out)
211 ioctl(0, TIOCGWINSZ, &w);
212 rows_shell = w.ws_row;
213 cols_shell = w.ws_col;
214 stream_out.width(width);
223 for (; current_line<n_lines; ++current_line)
224 stream_out << std::endl;
227 template<
typename OBJ>
237 class QueryStreambuf :
public std::streambuf
243 : flushed_(
false), newline_written_(
false)
250 bool newline_written()
252 return newline_written_;
255 int_type overflow(int_type ch)
257 newline_written_ =
true;
267 bool newline_written_;
272 std::ostream inject (&query_streambuf);
276 if (query_streambuf.newline_written())
279 if (current_line == n_lines)
282 for (
unsigned int i=0; i<n_lines; ++i)
283 stream_out <<
"\033[A" <<
"\r";
293 &operator << (OverWriteStream<S> &,
const T &);
307 while (current_line <= n_lines-1)
309 stream_out << std::endl;
324 while (current_line < n_lines-1)
326 stream_out <<
"\033[B";
332 for (
unsigned int i=0; i<n_lines; ++i)
334 stream_out <<
"\r" << std::setfill(
' ') << std::setw(width) <<
" " <<
"\r" <<
"\033[A";
339 stream_out <<
"\r" << std::setfill(
' ') << std::setw(width) <<
" " <<
"\r";
343 stream_out <<
"\r" << std::setfill(
' ') << std::setw(width) <<
" " <<
"\r";
349 stream_out <<
"\r" << std::setfill(
' ') << std::setw(width) <<
" " <<
"\r";
350 stream_out <<
"\033[A" << std::endl;
393 template <
typename S,
typename T>
397 output_stream.
clear();
399 return output_stream;
412 std::vector<T>
unique(
const std::vector<T> &myvector)
415 std::unique_copy(myvector.begin(), myvector.end(), std::back_inserter(ret));
425 std::string
print(
const std::vector<Type> &list,
const std::string sep=
",")
427 std::stringstream ret;
431 for (
unsigned int i=1; i<list.size(); ++i)
432 ret << sep << list[i];
445 std::stringstream ret;
448 for (
unsigned int i=1; i<dim; ++i)
449 ret << sep << point[i];
485 return shared_ptr<T>(t);
504 inline shared_ptr<const T>
507 return shared_ptr<const T>(t);
515 template <
typename VEC>
518 for (
auto i : in_vec.locally_owned_elements())
519 in_vec[i] += a_scalar;
522 #ifdef D2K_WITH_SUNDIALS 524 #ifdef DEAL_II_WITH_MPI 526 #ifdef DEAL_II_WITH_TRILINOS 531 #endif // DEAL_II_WITH_TRILINOS 533 #ifdef DEAL_II_WITH_PETSC 538 #endif // DEAL_II_WITH_PETSC 547 #ifdef DEAL_II_WITH_TRILINOS 561 TimeMonitor(
const MPI_Comm &comm=MPI_COMM_WORLD,
562 std::ostream &stream=std::cout) :
573 dealii_timer.print_summary();
574 Teuchos::TimeMonitor::summarize(outstream);
588 Scope(Teuchos::Time &trilinos_time,
590 const std::string §ion) :
591 trilinos_scoped_monitor(trilinos_time),
592 dealii_scoped_monitor(dealii_timer_output, section)
599 Teuchos::TimeMonitor trilinos_scoped_monitor;
613 scoped_timer(
const std::string §ion)
const 615 if (timers.find(section) == timers.end())
616 timers[section] = Teuchos::TimeMonitor::getNewCounter(section);
618 return Scope(*(timers[section]), dealii_timer, section);
625 std::ostream &outstream;
635 mutable std::map<std::string, Teuchos::RCP<Teuchos::Time> > timers;
OverWriteStream(unsigned int n_lines=1, Stream &stream_out=std::cout, unsigned int width=60)
void copy(BlockVector< double > &dst, const N_Vector &src)
unsigned int current_line
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...
int get_current_line()
It returns current line of the stream.
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.
This function collects some time utilities.
unsigned int get_shell_rows()
It returns the number of rows of the current shell.
bool file_exists(const std::string &file)
A function to check the existence of file file.
std::chrono::high_resolution_clock::time_point t_end
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 .
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.
OverWriteStream< S > & operator<<(OverWriteStream< S > &output_stream, const T &t)
std::string type(const T &t)
Return a human readable name of the type passed as argument.
std::vector< double > times
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) ...
This class uses n_lines lines of stream_out to show the output.
~OverWriteStream()
Flush output at the end so that we don't make a mess with the console.
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...
std::chrono::high_resolution_clock::time_point t_start
void vector_shift(VEC &in_vec, double a_scalar)
A simple class to shift a vector by a scalar.
std::string demangle(const char *name)
Demangle c++ names.
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 ...
bool dir_exists(const std::string &dir)
A function to check the existence of dir directory.
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...
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.
void end()
Move the cursor at the end of the output.
bool create_directory(const std::string &name)
A function to create directory.
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...
const unsigned int n_lines
bool rename_file(const std::string &file, const std::string &new_file)
A function to rename a file with a new name new_file.