ug4
debug_writer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2015: G-CSC, Goethe University Frankfurt
3  * Author: Andreas Vogel
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__LIB_ALGEBRA__OPERATOR__DEBUG_WRITER__
34 #define __H__LIB_ALGEBRA__OPERATOR__DEBUG_WRITER__
35 
37 #include "common/math/ugmath.h"
39 #include "common/util/file_util.h"
40 
41 namespace ug{
42 
44 template <size_t dim>
46 {
47 public:
49 
51  virtual bool get_positions(std::vector<MathVector<dim> >&vec) = 0;
52  virtual ~IPositionProvider() {}
53 };
54 
57 {
58  public:
59 
62 
64  inline void set_base_dir(const char* const baseDir) {m_baseDir = std::string(baseDir);}
65  std::string get_base_dir() { return m_baseDir; }
66 
68  inline void enter_section(const char* secDir) {m_secDir.push_back(secDir);}
69 
71  inline void leave_section () {m_secDir.pop_back();}
72 
74  void print_message(const char * msg)
75  {
76  UG_LOG ("DBG > ");
77  for (size_t i = 0; i < m_secDir.size (); i++)
78  {
79  UG_LOG (":" << m_secDir[i]);
80  }
81  UG_LOG (" > " << msg);
82  }
83 
85  void compose_file_path(std::string& path)
86  {
87  path = get_base_dir ();
88  if (! FileExists (path))
89  {
90  UG_WARNING ("IVectorDebugWriter: Directory '" << path << "' does not exist. Using cwd instead.\n");
91  path = ".";
92  }
93  for (size_t i = 0; i < m_secDir.size (); i++)
94  {
95  path.append ("/"); //TODO: This can be OS-dependent.
96  path.append (m_secDir[i]);
97  if ((! FileExists (path)) && ! CreateDirectory (path))
98  UG_WARNING ("IVectorDebugWriter: Could not create directory '" << path << "'. Using cwd instead.\n");
99  }
100  path.append ("/"); //TODO: This can be OS-dependent.
101  }
102 
103  protected:
104 
106  std::string m_baseDir;
107 
109  std::vector<std::string> m_secDir;
110 };
111 
113 
116 template <typename TVector>
118 {
119  public:
121  typedef TVector vector_type;
122 
123  public:
127 
130 
132  virtual void write_vector(const vector_type& vec, const char* name) = 0;
133 
135  int current_dimension() const {return m_currentDim;}
136 
138  template <int dim>
139  const std::vector<MathVector<dim> >& get_positions() const
140  {
141  if(m_currentDim != dim) throw(UGError("Current dim is different."));
142  return get_pos(Int2Type<dim>());
143  }
144 
149  virtual void update_positions() {};
150 
152  template <int dim>
153  void set_positions(const std::vector<MathVector<dim> >& vPos)
154  {
155  get_pos(Int2Type<dim>()) = vPos; m_currentDim = dim;
156  }
157 
159  template <int dim>
161  {
162  provider.get_positions(get_pos(Int2Type<dim>()));
163  m_currentDim = dim;
164  }
165 
167  int get_dim() const
168  {
169  return m_currentDim;
170  }
171 
174 
177 
180 
182  virtual void print_message(const char * msg) {if (m_spContext.valid()) m_spContext->print_message(msg);}
183 
185  inline void set_base_dir(const char* const baseDir)
186  {
187  if (! m_spContext.valid())
189  m_spContext->set_base_dir (baseDir);
190  }
191 
192  std::string get_base_dir() {return (m_spContext.valid())? m_spContext->get_base_dir() : "";}
193 
195  inline void enter_section(const char* secDir) {if (m_spContext.valid()) m_spContext->enter_section (secDir);}
196 
198  inline void leave_section () {if (m_spContext.valid()) m_spContext->leave_section ();}
199 
200  protected:
201 
203  template <int dim>
204  std::vector<MathVector<dim> >& positions()
205  {
206  m_currentDim = dim; return get_pos(Int2Type<dim>());
207  }
208 
210  void compose_file_path(std::string& path)
211  {
212  if (m_spContext.valid()) m_spContext->compose_file_path (path);
213  else path = ".";
214  }
215 
216  protected:
217 
220 
223 
225  std::vector<MathVector<1> > m_vPos1d;
226  std::vector<MathVector<2> > m_vPos2d;
227  std::vector<MathVector<3> > m_vPos3d;
228 
230  std::vector<MathVector<1> >& get_pos(Int2Type<1>) {return m_vPos1d;}
231  std::vector<MathVector<2> >& get_pos(Int2Type<2>) {return m_vPos2d;}
232  std::vector<MathVector<3> >& get_pos(Int2Type<3>) {return m_vPos3d;}
233  const std::vector<MathVector<1> >& get_pos(Int2Type<1>) const {return m_vPos1d;}
234  const std::vector<MathVector<2> >& get_pos(Int2Type<2>) const {return m_vPos2d;}
235  const std::vector<MathVector<3> >& get_pos(Int2Type<3>) const {return m_vPos3d;}
236 };
237 
239 
242 template <typename TAlgebra>
243 class IDebugWriter : public IVectorDebugWriter<typename TAlgebra::vector_type>
244 {
245  public:
247  typedef TAlgebra algebra_type;
248 
250  typedef typename TAlgebra::vector_type vector_type;
251 
253  typedef typename TAlgebra::matrix_type matrix_type;
254 
255  public:
257  virtual void write_vector(const vector_type& vec, const char* name) = 0;
258 
260  virtual void write_matrix(const matrix_type& mat, const char* name) = 0;
261 };
262 
263 
264 template <typename TVector>
266 {
267  public:
269  typedef TVector vector_type;
270 
271  public:
274  : m_spVectorDebugWriter(spDebugWriter) {}
275 
278 
280  virtual void set_debug(SmartPtr<IVectorDebugWriter<vector_type> > spDebugWriter)
281  {
282  m_spVectorDebugWriter = spDebugWriter;
283  }
284 
288 
290  bool vector_debug_writer_valid() const {return m_spVectorDebugWriter.valid();}
291 
293  void write_debug(const vector_type& vec, const char* filename)
294  {
295  write_debug(vec, std::string (filename));
296  }
297 
298  protected:
300  virtual void write_debug(const vector_type& vec, std::string name)
301  {
302  PROFILE_FUNC_GROUP("algebra debug");
303  // if no debug writer set, we're done
304  if(m_spVectorDebugWriter.invalid()) return;
305 
306  // check ending
307  size_t iExtPos = name.find_last_of(".");
308  if(iExtPos != std::string::npos && name.substr(iExtPos).compare(".vec") != 0)
309  UG_THROW("Only '.vec' format supported for vectors, but"
310  " filename is '"<<name<<"'.");
311 
312  if(iExtPos == std::string::npos)
313  name.append(".vec");
314 
315  // write
316  m_spVectorDebugWriter->write_vector(vec, name.c_str());
317  }
318 
320  void print_debugger_message(std::string msg)
321  {
322  print_debugger_message(msg.c_str());
323  }
325  void print_debugger_message(const char * msg)
326  {
327  if (m_spVectorDebugWriter.valid()) m_spVectorDebugWriter->print_message(msg);
328  }
329 
331  void enter_vector_debug_writer_section(std::string secDir)
332  {
333  enter_vector_debug_writer_section(secDir.c_str());
334  }
336  void enter_vector_debug_writer_section(const char * secDir)
337  {
338  if (m_spVectorDebugWriter.valid()) m_spVectorDebugWriter->enter_section(secDir);
339  }
340 
343  {
344  if (m_spVectorDebugWriter.valid()) m_spVectorDebugWriter->leave_section();
345  }
346 
347  protected:
350 };
351 
352 template <typename TAlgebra>
353 class DebugWritingObject : public VectorDebugWritingObject<typename TAlgebra::vector_type>
354 {
355  public:
357  typedef TAlgebra algebra_type;
358 
360  typedef typename TAlgebra::vector_type vector_type;
361 
363  typedef typename TAlgebra::matrix_type matrix_type;
364 
365  protected:
368 
369  public:
372  : VectorDebugWritingObject<vector_type>(spDebugWriter),
373  m_spDebugWriter(spDebugWriter) {}
374 
379 
381  virtual ~DebugWritingObject() {}
382 
384  virtual void set_debug(SmartPtr<IDebugWriter<algebra_type> > spDebugWriter)
385  {
386  m_spDebugWriter = spDebugWriter;
388  }
389 
393 
395  bool debug_writer_valid() const {return m_spDebugWriter.valid();}
396 
397  protected:
399  void write_debug(const matrix_type& mat, const char* filename)
400  {
401  write_debug(mat, std::string(filename));
402  }
404  void write_debug(const matrix_type& mat, std::string name)
405  {
406  PROFILE_FUNC_GROUP("algebra debug");
407  // if no debug writer set, we're done
408  if(m_spDebugWriter.invalid()) return;
409 
410  // check ending
411  size_t iExtPos = name.find_last_of(".");
412  if(iExtPos != std::string::npos && name.substr(iExtPos).compare(".mat") != 0)
413  UG_THROW("Only '.mat' format supported for matrices, but"
414  " filename is '"<<name<<"'.");
415 
416  if(iExtPos == std::string::npos)
417  name.append(".mat");
418 
419  // write
420  m_spDebugWriter->write_matrix(mat, name.c_str());
421  }
422 
424  void enter_debug_writer_section(std::string secDir)
425  {
426  enter_debug_writer_section(secDir.c_str());
427  }
429  void enter_debug_writer_section(const char * secDir)
430  {
431  if (m_spDebugWriter.valid()) m_spDebugWriter->enter_section(secDir);
432  }
433 
436  {
437  if (m_spDebugWriter.valid()) m_spDebugWriter->leave_section();
438  }
439 
440  protected:
443 };
444 
445 } // end namespace ug
446 
447 #endif /* __H__LIB_ALGEBRA__OPERATOR__DEBUG_WRITER__ */
location name
Definition: checkpoint_util.lua:128
Definition: smart_pointer.h:296
Definition: smart_pointer.h:108
Context of a debugger writer: Keeps the debugging section etc.
Definition: debug_writer.h:57
std::vector< std::string > m_secDir
debuging section subdirectories
Definition: debug_writer.h:109
void compose_file_path(std::string &path)
composes the path for the files and creates the intermediate directories (up to the base one):
Definition: debug_writer.h:85
std::string get_base_dir()
Definition: debug_writer.h:65
std::string m_baseDir
base directory for the debugging output
Definition: debug_writer.h:106
void leave_section()
leave the current debugging section
Definition: debug_writer.h:71
void set_base_dir(const char *const baseDir)
set the base directory for output files (.vec and .mat)
Definition: debug_writer.h:64
void print_message(const char *msg)
prints a message
Definition: debug_writer.h:74
void enter_section(const char *secDir)
enter a new debugging section
Definition: debug_writer.h:68
DebugWriterContext()
constructor
Definition: debug_writer.h:61
Definition: debug_writer.h:354
void enter_debug_writer_section(std::string secDir)
enters a debugging section
Definition: debug_writer.h:424
virtual void set_debug(SmartPtr< IDebugWriter< algebra_type > > spDebugWriter)
set debug writer
Definition: debug_writer.h:384
void write_debug(const matrix_type &mat, const char *filename)
write debug output for a matrix (if debug writer set)
Definition: debug_writer.h:399
void write_debug(const matrix_type &mat, std::string name)
write debug output for a matrix (if debug writer set)
Definition: debug_writer.h:404
virtual ~DebugWritingObject()
virtual destructor
Definition: debug_writer.h:381
DebugWritingObject(SmartPtr< IDebugWriter< algebra_type > > spDebugWriter)
Definition: debug_writer.h:371
TAlgebra::vector_type vector_type
type of vector
Definition: debug_writer.h:360
ConstSmartPtr< IDebugWriter< algebra_type > > debug_writer() const
Definition: debug_writer.h:392
DebugWritingObject()
Definition: debug_writer.h:370
void enter_debug_writer_section(const char *secDir)
enters a debugging section
Definition: debug_writer.h:429
TAlgebra algebra_type
type of algebra
Definition: debug_writer.h:357
SmartPtr< IDebugWriter< algebra_type > > m_spDebugWriter
Debug Writer.
Definition: debug_writer.h:442
SmartPtr< IDebugWriter< algebra_type > > debug_writer()
returns the debug writer
Definition: debug_writer.h:391
TAlgebra::matrix_type matrix_type
type of matrix
Definition: debug_writer.h:363
DebugWritingObject(const DebugWritingObject< algebra_type > &parent)
clone constructor
Definition: debug_writer.h:376
bool debug_writer_valid() const
returns true if the debug writer is set
Definition: debug_writer.h:395
void leave_debug_writer_section()
leaves a debugging section
Definition: debug_writer.h:435
base class for all debug writer
Definition: debug_writer.h:244
virtual void write_vector(const vector_type &vec, const char *name)=0
write vector
TAlgebra algebra_type
type of algebra
Definition: debug_writer.h:247
TAlgebra::vector_type vector_type
type of vector
Definition: debug_writer.h:250
virtual void write_matrix(const matrix_type &mat, const char *name)=0
write matrix
TAlgebra::matrix_type matrix_type
type of matrix
Definition: debug_writer.h:253
Interface for providing vertex positions.
Definition: debug_writer.h:46
virtual bool get_positions(std::vector< MathVector< dim > > &vec)=0
Assigns position information to given vector.
virtual ~IPositionProvider()
Definition: debug_writer.h:52
base class for all vector debug writer
Definition: debug_writer.h:118
std::vector< MathVector< 2 > > m_vPos2d
Definition: debug_writer.h:226
std::string get_base_dir()
Definition: debug_writer.h:192
TVector vector_type
type of vector
Definition: debug_writer.h:121
const std::vector< MathVector< 1 > > & get_pos(Int2Type< 1 >) const
Definition: debug_writer.h:233
const std::vector< MathVector< dim > > & get_positions() const
returns the positions (only available for current dimension)
Definition: debug_writer.h:139
SmartPtr< DebugWriterContext > m_spContext
debugging writer context
Definition: debug_writer.h:219
void set_positions(IPositionProvider< dim > &provider)
employs a position provider to set the current positions
Definition: debug_writer.h:160
virtual ~IVectorDebugWriter()
virtual destructor
Definition: debug_writer.h:129
int m_currentDim
current dimension
Definition: debug_writer.h:222
void leave_section()
leave the current debugging section
Definition: debug_writer.h:198
virtual void print_message(const char *msg)
prints a message
Definition: debug_writer.h:182
std::vector< MathVector< dim > > & positions()
returns the positions and sets the current dim
Definition: debug_writer.h:204
std::vector< MathVector< 2 > > & get_pos(Int2Type< 2 >)
Definition: debug_writer.h:231
void set_positions(const std::vector< MathVector< dim > > &vPos)
sets the current positions
Definition: debug_writer.h:153
const std::vector< MathVector< 2 > > & get_pos(Int2Type< 2 >) const
Definition: debug_writer.h:234
std::vector< MathVector< 1 > > & get_pos(Int2Type< 1 >)
help function to get local ips
Definition: debug_writer.h:230
void set_base_dir(const char *const baseDir)
set the base directory for output files (.vec and .mat)
Definition: debug_writer.h:185
void set_context(SmartPtr< DebugWriterContext > context)
set the debugging writer context
Definition: debug_writer.h:173
SmartPtr< DebugWriterContext > get_context()
get the debugging writer context
Definition: debug_writer.h:176
std::vector< MathVector< 1 > > m_vPos1d
vectors of positions
Definition: debug_writer.h:225
std::vector< MathVector< 3 > > & get_pos(Int2Type< 3 >)
Definition: debug_writer.h:232
virtual void update_positions()
Definition: debug_writer.h:149
const std::vector< MathVector< 3 > > & get_pos(Int2Type< 3 >) const
Definition: debug_writer.h:235
int get_dim() const
get the dimensionality
Definition: debug_writer.h:167
ConstSmartPtr< DebugWriterContext > get_context() const
get the debugging writer context
Definition: debug_writer.h:179
void enter_section(const char *secDir)
enter a new debugging section
Definition: debug_writer.h:195
virtual void write_vector(const vector_type &vec, const char *name)=0
write vector
IVectorDebugWriter()
Constructor.
Definition: debug_writer.h:125
std::vector< MathVector< 3 > > m_vPos3d
Definition: debug_writer.h:227
void compose_file_path(std::string &path)
composes the path for the files and creates the intermediate directories (up to the base one):
Definition: debug_writer.h:210
int current_dimension() const
returns the current dimension
Definition: debug_writer.h:135
Instances of this class or of derived classes are thrown if errors arise.
Definition: error.h:104
Definition: debug_writer.h:266
bool vector_debug_writer_valid() const
returns true if the debug writer is set
Definition: debug_writer.h:290
virtual ~VectorDebugWritingObject()
virtual destructor
Definition: debug_writer.h:277
virtual void write_debug(const vector_type &vec, std::string name)
writing debug output for a vector (if debug writer set)
Definition: debug_writer.h:300
SmartPtr< IVectorDebugWriter< vector_type > > vector_debug_writer()
returns the debug writer
Definition: debug_writer.h:286
void enter_vector_debug_writer_section(std::string secDir)
enters a debugging section
Definition: debug_writer.h:331
VectorDebugWritingObject(SmartPtr< IVectorDebugWriter< vector_type > > spDebugWriter)
Definition: debug_writer.h:273
void leave_vector_debug_writer_section()
leaves a debugging section
Definition: debug_writer.h:342
VectorDebugWritingObject()
Definition: debug_writer.h:272
virtual void set_debug(SmartPtr< IVectorDebugWriter< vector_type > > spDebugWriter)
set debug writer
Definition: debug_writer.h:280
void print_debugger_message(const char *msg)
prints a debugger message (listing all the sections)
Definition: debug_writer.h:325
void enter_vector_debug_writer_section(const char *secDir)
enters a debugging section
Definition: debug_writer.h:336
SmartPtr< IVectorDebugWriter< vector_type > > m_spVectorDebugWriter
Debug Writer.
Definition: debug_writer.h:349
void write_debug(const vector_type &vec, const char *filename)
writing debug output for a vector (if debug writer set)
Definition: debug_writer.h:293
TVector vector_type
type of vector
Definition: debug_writer.h:269
ConstSmartPtr< IVectorDebugWriter< vector_type > > vector_debug_writer() const
Definition: debug_writer.h:287
void print_debugger_message(std::string msg)
prints a debugger message (listing all the sections)
Definition: debug_writer.h:320
File utility functions.
static const int dim
UG_API bool FileExists(const char *filename)
!!! Serial i/o version !!!
Definition: file_util.cpp:53
UG_API bool CreateDirectory(const char *directory)
Creates a directory.
Definition: file_util_posix.cpp:149
#define UG_THROW(msg)
Definition: error.h:57
#define UG_LOG(msg)
Definition: log.h:367
#define UG_WARNING(msg)
Definition: log.h:328
the ug namespace
#define PROFILE_FUNC_GROUP(groups)
Definition: profiler.h:258
Definition: metaprogramming_util.h:42