Class documentation of Concepts

Loading...
Searching...
No Matches
matfileIO.hh
Go to the documentation of this file.
1
8#ifndef matfileInputOutput_hh
9#define matfileInputOutput_hh
10
11#include <string>
12#include <typeinfo>
13
14#include "matIO_1.5/matio.h" // <matio.h>
15#include "basics.hh"
16#include "toolbox.hh"
17#include "operator.hh"
19#include <basics/exceptions.hh>
20
21namespace concepts {
22
23 typedef std::set<const std::type_info*> set_info;
24
25 // forward declaration
26 class MatfileIO;
27
28 // **************************************************** MatfileIOError **
29
34 public:
38 MatfileIOError(const std::string& file, const unsigned int line,
39 const std::string& function,
40 const std::string& errorMessage) throw ();
41
42 virtual const char* what() const throw();
43
45 protected:
46 virtual std::ostream& info(std::ostream& os) const throw ();
47 private:
48 std::string errorMessage_;
49 mutable std::string outputMessage_ ;
50 };
51
52 // ********************************************************* MatfileIO **
53
54
55
56 //
57 // TODO: VALGRIND !
58 // @test test::MatfileTest
59 //
60
61
62
63 //############################################################ Introduction to update this class
64 //
65 // To add new dense/sparse objects (rank <= 2) to for add/get -routines. Do the following:
66 // -> Write new get_/add_ routine for the object (matfileIO_C /matfileIO_R for Cmplx/Real based ones)
67 // -> Write necessary assertion routines if needed,
68 //
69 // To generalise this class to rank > 2 objects (cubes, general multi-arrays) it could be a good idea to add a assertRank_
70 // routine in the get routines.
71 //
72 // This class works with dense/sparse/int formats.To write *.mat-files with structs/ cells data,
73 // check the documentation of matIO and add needed functions.
74 //
75 //############################################################
76
77
78
112 class MatfileIO : public OutputOperator{
113 public:
114
125 MatfileIO(const std::string filename);
126
127 /*
128 * The deconstructor closes the MatfileIO instance if it is still open.
129 */
130 virtual ~MatfileIO() {
131 close();
132 }
133
159 template<class T>
160 void add(const T& u, const std::string varName,
162 {
163
164
165 if (!isOpen()) {
166 throw conceptsException(MatfileIOError(__FILE__, __LINE__, __FUNCTION__, "Cannot add something. No *.mat-file opened."));
167 return;
168 }
169
170 //The var is currently being written in the open stream.
171 if (matvar_str_cur_.exist(varName)) {
172 throw conceptsException(MatfileIOError(__FILE__, __LINE__, __FUNCTION__, "Not able to add : a variable with the same name is being added currently. Close the MatfileIO and retry again with overwrite rights."));
173 return;
174 }
175 //The var already exists in the opened matfile and will be overwritten:
176 else if (matvar_str_.exist(varName)) {
177 if (overWrite_) {
179 //it gets a variable used in the current set;
180 matvar_str_cur_.insert(varName);
181
182#ifdef HAS_Z // if z lib is installed you can use the compression given
183 add_(u, varName, compress);
184#else // if not by default no compression is applicated
186#endif
187 return;
188 } else {
189 throw conceptsException(MatfileIOError(__FILE__, __LINE__, __FUNCTION__, " The variable '" + varName + "' has been declared not to be overwritten."));
190 }
191 } else if (!matvar_str_.exist(varName)) {
192 matvar_str_.insert(varName);
193 matvar_str_cur_.insert(varName);
194#ifdef HAS_Z // if z lib is installed you can use the compression given
195 add_(u, varName, compress);
196#else // if not by default no compression is applicated
198#endif
199 return;
200 }
201 // just to be sure
202 throw conceptsException(MatfileIOError(__FILE__, __LINE__, __FUNCTION__, "Some unhandled/ unknown error arisen in add-Routine in MatfileIO."));
203 return;
204 }
205
233 template<class T>
234 void get(T& u, const std::string varName)
235 {
236
237 //for sparse, int and uint use Mat_VarRead method, so structure = 1, (structure = 0, ie. for dense)
239
240
241 // isSparse checks also for existence of varName.
242 // for Sparse Format the Mat_VarReadinfo is not working with mat_sparse_t pointer
243 // so read with Mat_VarRead is used. Here it has no problems with reading compressed format.
244 // for integer format, needed to access data pointer.
245 matvar_ = structure ? Mat_VarRead(mat_, varName.c_str()): Mat_VarReadInfo(mat_, varName.c_str()) ;
246
247 //check if reading works
248 assertVar_(matvar_);
249
250 //Multidimensional Arrays, i.e. rank > 2 are not supported atm.
251 if (matvar_->rank > 2)
253 "At the moments just rank 2 objects are supported in MatfileIO");
254
255 //get_ routine checks if requested structure coincides with the one of u (dense/sparse)
256 get_(u, matvar_);
257 }
258
265 void reopen();
266
272 void close();
273
277 void clear();
278
283 inline bool isOpen() const {
284 return (mat_ != NULL);
285 }
286
290 inline bool isEmpty() const {
291 return (matvar_str_.isempty());
292 }
293
301 bool remove(const std::string varName);
302
306 inline std::string filename() const {
307 return fileName_;
308 }
309
316 inline void overWrite(bool toOverWrite) {
317 overWrite_ = toOverWrite;
318 }
319
320 //##################################################################
321 // public control routines
322 //##################################################################
323
328 bool exists(const std::string& varName) const;
329
333 bool isReal(const std::string& varName) const;
334
338 bool isCmplx(const std::string& varName) const;
339
343 bool isScalar(const std::string& varName) const;
344
348 bool isDense(const std::string& varName) const;
349
353 bool isSparse(const std::string& varName) const;
354
358 bool isInt(const std::string& varName) const;
359
363 bool isUint(const std::string& varName) const;
364
368 uint lengthVector(const std::string& varName) const;
369
370 // /*
371 // * Returns true if the variable /c varName is a cell, else 0.
372 // */
373 // bool isCell(const std::string& varName) const;
374
375
376 //##################################################################
377 // public assertion routines
378 //##################################################################
382 void assertCmplx(const std::string& varName) const;
386 void assertExistence(const std::string& varName) const;
387
391 void assertVector(const std::string& varName) const;
392
396 void assertQuadratic(const std::string& varName) const;
397
398 protected:
402 virtual std::ostream& info(std::ostream& os) const;
403
404 private:
405
406 //the current Matfile
407 mat_t* mat_;
408 //the current variable in the Matfile
409 matvar_t* matvar_;
410
411 //name of the current open *.mat-file
412 std::string fileName_;
413
414 //collection of all names of the variables in the current mat-file
415 concepts::Set<std::string> matvar_str_;
416
417 //collection of all names of variables that are going to be added in the current mat-file-stream
418 //this is used to avoid adding different objects with the same name to the Matfile, since
419 //this would be followed with an memory leak error, when the object MatfileIO wasn't closed before.
420 concepts::Set<std::string> matvar_str_cur_;
421
422 //flag that controls if one wants to overwrite variables or forbid to do so
423 //it can be controlled with the overWrite(bool) routine
424 bool overWrite_;
425
427 std::string matfileEnding_(const std::string& filename);
428
429 //##################################################################
430 // add section for sparse objects
431 //##################################################################
432
436 template<class T>
437 void add_(const SparseMatrix<T>& sparseM, const std::string& varName,
438 enum matio_compression compress);
439
443 template<class T>
444 void add_(const DiagonalMatrix<T>& diagM, const std::string& varName,
445 enum matio_compression compress);
446
447 //##################################################################
448 // add section for dense objects
449 //##################################################################
450
454 template<class T>
455 void add_(const ElementMatrix<T>& dense, const std::string& varName,
456 enum matio_compression compress);
457
461 template<uint dim>
462 void add_(const Array<Point<Real,dim> >& array, const std::string& varName,
463 enum matio_compression compress);
464
468 template<uint dim>
469 void add_(const Array<Point<Cmplx,dim> >& array, const std::string& varName,
470 enum matio_compression compress);
471
475 template<class T>
476 void add_(const Array<T>& array, const std::string& varName,
477 enum matio_compression compress);
478
482 template<class T>
483 void add_(const Vector<T>& vec, const std::string& varName,
484 enum matio_compression compress);
485
489 template<class T>
490 void add_(const std::vector<T> &vec, const std::string& varName,
491 enum matio_compression compress);
492
496 template<uint dim>
497 void add_(const Mapping<Real, dim>& map, const std::string& varName,
498 enum matio_compression compress);
499
503 template<uint dim>
504 void add_(const Mapping<Cmplx, dim>& map, const std::string& varName,
505 enum matio_compression compress);
506
510 template<uint dim>
511 void add_(const Point<Real, dim>& point, const std::string& varName,
512 enum matio_compression compress);
513
517 template<uint dim>
518 void add_(const Point<Cmplx, dim>& point, const std::string& varName,
519 enum matio_compression compress);
520
524 void add_(const Real& scalar, const std::string& varName,
525 enum matio_compression compress);
526
530 void add_(const Cmplx& scalar, const std::string& varName,
531 enum matio_compression compress);
532
536 void add_(const int scalar, const std::string& str,
537 enum matio_compression compress);
538
542 void add_(const uint scalar, const std::string& str,
543 enum matio_compression compress);
544
545 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
546 //TODO: add section for cell objects ?!
547 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
548 // possible further add classes
549 // void add_ vector<Vector>
550 // void add_ std::vector<ElementMatrix>
551 // void add_ std::vector<SparseMatrix>
552
553
554 //##################################################################
555 // getter section for sparse objects
556 //##################################################################
557
561 template<class T>
562 void get_(SparseMatrix<T>& sparse, matvar_t*& matvar);
563
571 template<class T>
573
574 //##################################################################
575 // getter section for dense objects
576 //##################################################################
577
578
582 template<class T>
583 void get_(ElementMatrix<T>& dense, matvar_t*& matvar);
584
588 template<class T>
589 void get_(DenseMatrix<T>& dense, matvar_t*& matvar);
590
594 template<class T>
595 void get_(Array<T>& arr, matvar_t*& matvar);
596
600 template<class T>
601 void get_(Vector<T>& vec, matvar_t*& matvar);
602
606 template<class T>
607 void get_(std::vector<T>& seq, matvar_t*& matvar);
608
612 template<class T>
613 void get_(std::vector<Vector<T> >& seq, matvar_t*& matvar);
614
618 template<uint dim>
619 void get_(Mapping<Real, dim>& map, matvar_t*& matvar);
620
624 template<uint dim>
625 void get_(Mapping<Cmplx, dim>& map, matvar_t*& matvar);
626
630 template<uint dim>
631 void get_(Point<Real, dim>& point, matvar_t*& matvar);
632
636 template<uint dim>
637 void get_(Point<Cmplx, dim>& point, matvar_t*& matvar);
638
642 void get_(Real& scalar, matvar_t*& matvar);
643
647 void get_(Cmplx& scalar, matvar_t*& matvar);
648
652 void get_(int& scalar, matvar_t*& matvar);
653
657 void get_(uint& scalar, matvar_t*& matvar);
658
659 //#################################################################
660 // matvar create section
661 //#################################################################
662
670 void createDenseReal_(uint m, uint n, const std::string& varName,
671 Real*& dataAccP);
672
681 void createDenseCmplx_(uint m, uint n, const std::string& varName,
682 Real*& realP, Real*& imgP);
683
684
685 //#################################################################
686 // private control section
687 //#################################################################
688
692 inline bool isReal_(matvar_t* var) const {
693 return (var->isComplex == 0);
694 }
698 inline bool isCmplx_(matvar_t* var) const {
699 return (var->isComplex != 0);
700 }
701
705 inline bool isSparse_(matvar_t* var) const {
706 return (var->class_type == MAT_C_SPARSE);
707 }
708
712 inline bool isDense_(matvar_t* var) const {
713 return (var->class_type == MAT_C_DOUBLE);
714 }
715
719 bool isInt_(matvar_t* var) const{
720 enum matio_classes vc = var->class_type;
721 return (vc == MAT_C_INT8 || vc == MAT_C_INT16 ||
722 vc == MAT_C_INT32|| vc == MAT_C_INT64);
723 }
724
728 bool isUint_(matvar_t* var) const{
729 enum matio_classes vc = var->class_type;
730 return (vc == MAT_C_UINT8 || vc == MAT_C_UINT16 ||
732 }
733
738 bool isVector_(matvar_t* var) const;
739
743 bool hasDiagStructur_(matvar_t* var) const;
744
748 inline bool isQuadratic_(matvar_t* var) const {
749 return (var->dims[0] == var->dims[1]) && (var->rank == 2);
750 }
751
756 bool isScalar_(matvar_t* var) const;
757
761 uint lengthVector_(matvar_t* var) const;
762
763 //#################################################################
764 // private assert section
765 //#################################################################
766 // These methods are used to work with current pointers and don't reread the file, like in
767 // possible public analog methods having a string as input, if assert, they free the matvar_t.
768
769
770 void wrongField_(matvar_t*& var) const;
771
776 void wrongStructure_(matvar_t*& var) const;
777
781 void assertDimension_(uint dim, uint vardim) const;
782
787 void assertQuadratic_(matvar_t*& var) const;
788
793 void assertVector_(matvar_t*& var) const;
794
800 void assertDiagStructur_(matvar_t*& var) const;
801
806 void assertScalar_(matvar_t*& var) const;
807
812 void assertVar_(matvar_t* var) const;
813
818 void assertVar_(mat_sparse_t* var) const;
819
823 void assertOpenFile_(mat_t* file) const;
824
825 };
826
827}
828#endif
#define conceptsException(exc)
bool exist(F val) const
Returns true, if a value is in the set.
Definition set.hh:296
bool isempty() const
Returns true, if set is empty.
Definition set.hh:110
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
MatfileIOError(const std::string &file, const unsigned int line, const std::string &function, const std::string &errorMessage)
void assertQuadratic(const std::string &varName) const
bool exists(const std::string &varName) const
bool isCmplx(const std::string &varName) const
bool isDense(const std::string &varName) const
void get(T &u, const std::string varName)
Definition matfileIO.hh:234
void add(const T &u, const std::string varName, enum matio_compression compress=MAT_COMPRESSION_NONE)
Definition matfileIO.hh:160
std::string filename() const
Definition matfileIO.hh:306
bool isOpen() const
Definition matfileIO.hh:283
void assertVector(const std::string &varName) const
bool isReal(const std::string &varName) const
bool isUint(const std::string &varName) const
MatfileIO(const std::string filename)
bool isSparse(const std::string &varName) const
void assertCmplx(const std::string &varName) const
bool remove(const std::string varName)
void assertExistence(const std::string &varName) const
bool isScalar(const std::string &varName) const
bool isInt(const std::string &varName) const
bool isEmpty() const
Definition matfileIO.hh:290
virtual std::ostream & info(std::ostream &os) const
void overWrite(bool toOverWrite)
Definition matfileIO.hh:316
uint lengthVector(const std::string &varName) const
double Real
Definition typedefs.hh:39
Set< F > makeSet(uint n, const F &first,...)
Definition set.hh:320
std::complex< Real > Cmplx
Type for a complex number. It also depends on the setting of Real.
Definition typedefs.hh:42