diff --git a/Makefile b/Makefile index e58e339843ba024a03e322f823ec1d691fbaa7f7..9abcaca511a3e9bf5563fa4d5451e9664de969d8 100644 --- a/Makefile +++ b/Makefile @@ -48,7 +48,6 @@ MAIN_OBJS = \ ParsingDriver.o \ DataTree.o \ ModFile.o \ - MatlabFile.o \ Statement.o \ ExprNode.o \ ModelNormalization.o \ diff --git a/MatlabFile.cc b/MatlabFile.cc deleted file mode 100644 index 76c1fe766c8be3585c8a3f5d567558842db83ac4..0000000000000000000000000000000000000000 --- a/MatlabFile.cc +++ /dev/null @@ -1,1077 +0,0 @@ -/* - * Copyright (C) 2009 Dynare Team - * - * This file is part of Dynare. - * - * Dynare is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dynare is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dynare. If not, see <http://www.gnu.org/licenses/>. - */ - -/* - Useful documentation: Matlab 7 Mat-File Format - ----------------------------------------------- - revision: October 2008 PDF only Rereleased for Version 7.7 (Release 2008b) - available at: http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/matfile_format.pdf -*/ -#include "MatlabFile.hh" - - -MatlabFile::MatlabFile() -{ -} - -MatlabFile::~MatlabFile() -{ -} - -ArrayElem::ArrayElem() -{ -} - -ArrayElem::~ArrayElem() -{ -} - - -string -UTF8::ReadAlph(char* InBuff, int* pBuff, int Size) const -{ - char tmp_c[Size+1]; - string str; - memcpy(&tmp_c, InBuff+*pBuff, Size); - tmp_c[Size]=0; - str.assign(tmp_c); - if(Size<=4) - *pBuff += 4; - else if(Size % 8) - *pBuff += 8*ceil(double(Size) / 8); - else - *pBuff += Size; - return(str); -} - - -string -UTF16::ReadAlph(char* InBuff, int* pBuff, int Size) const -{ - string str(""); - for(int i=0;i<Size;i++) - str.append(&(InBuff[i*2+*pBuff])); - if(Size*2<=4) - *pBuff += 4; - else if((Size*2) % 8) - *pBuff += 8*ceil(double(Size*2) / 8); - else - *pBuff += Size*2; - return(str); -} - -string -UTF32::ReadAlph(char* InBuff, int* pBuff, int Size) const -{ - string str(""); - for(int i=0;i<Size;i++) - str.append(&(InBuff[i*4+*pBuff])); - if((Size*4) % 8) - *pBuff += 8*ceil(double(Size*4) / 8); - else - *pBuff += Size*4; - return(str); -} - -double -INT8::ReadNum(char* InBuff, int* pBuff) const -{ - char val; - val = InBuff[*pBuff]; - *pBuff += sizeof(val); - return(val); -} - - -double -INT16::ReadNum(char* InBuff, int* pBuff) const -{ - Int val; - memcpy(&val, InBuff+*pBuff, sizeof(val)); - *pBuff += sizeof(val); - return(val); -} - -double -INT32::ReadNum(char* InBuff, int* pBuff) const -{ - LongInt val; - memcpy(&val, InBuff+*pBuff, sizeof(val)); - *pBuff += sizeof(val); - return(val); -} - - -double -INT64::ReadNum(char* InBuff, int* pBuff) const -{ - LongLongInt val; - memcpy(&val, InBuff+*pBuff, sizeof(val)); - *pBuff += sizeof(val); - return(val); -} - -double -Single::ReadNum(char* InBuff, int* pBuff) const -{ - float val; - memcpy(&val, InBuff+*pBuff, sizeof(val)); - *pBuff += sizeof(val); - return(val); -} - -double -Double::ReadNum(char* InBuff, int* pBuff) const -{ - double val; - memcpy(&val, InBuff+*pBuff, sizeof(val)); - *pBuff += sizeof(val); - return(val); -} - - - -Data_Header_t -SimpleElem::ReadDataHeader(char* InBuff, int* pBuff) const -{ - Data_Header_t data_header; - memcpy(&data_header.DataType, InBuff+*pBuff, sizeof(data_header.DataType)); - *pBuff += 2; - memcpy(&data_header.S_Number_of_Bytes, InBuff+*pBuff, sizeof(data_header.S_Number_of_Bytes)); - *pBuff += 2; - if(data_header.S_Number_of_Bytes!=0) - data_header.Number_of_Bytes=data_header.S_Number_of_Bytes; - else - { - memcpy(&data_header.Number_of_Bytes, InBuff+*pBuff, sizeof(data_header.Number_of_Bytes)); - *pBuff += 4; - } - return(data_header); -} - - -SimpleElem::SimpleElem() -{ - verbose=true; - array_elem=NULL; - Type=-1; -} - -SimpleElem::~SimpleElem() -{ -} - -returned_ReadData_t -SimpleElem::Get_Data_Class(Data_Header_t data_header) const -{ - returned_ReadData_t ret; - switch(data_header.DataType) - { - case miINT8: //8 bit, signed - case miUINT8: //8 bit, unsigned - ret.Type = Numerical; - ret.Simple = new INT8; - return(ret); - break; - case miINT16: //16-bit, signed - case miUINT16: //16-bit, unsigned - ret.Type = Numerical; - ret.Simple = new INT16; - return(ret); - break; - case miINT32: //32-bit, signed - case miUINT32: //32-bit, unsigned - ret.Type = Numerical; - ret.Simple = new INT32; - return(ret); - break; - case miINT64: //64-bit, signed - case miUINT64: //64-bit, unsigned - ret.Type = Numerical; - ret.Simple = new INT64; - return(ret); - break; - case miSINGLE: //IEEE� 754 single format - ret.Type = Numerical; - ret.Simple = new Single; - return(ret); - break; - case miDOUBLE: //IEEE 754 double format - ret.Type = Numerical; - ret.Simple = new Double; - return(ret); - break; - case miMATRIX: //MATLAB array - ret.Type = Matrix; - ret.Simple = NULL; - return(ret); - break; - case miCOMPRESSED: //Compressed Data - ret.Type = Compressed; - ret.Simple = NULL; - return(ret); - break; - case miUTF8: //Unicode UTF-8 Encoded Character Data - ret.Type = AlphaNumeric; - ret.Simple = new UTF8; - return(ret); - break; - case miUTF16: //Unicode UTF-16 Encoded Character Data - ret.Type = AlphaNumeric; - ret.Simple = new UTF16; - return(ret); - break; - case miUTF32: //Unicode UTF-32 Encoded Character Data - ret.Type = AlphaNumeric; - ret.Simple = new UTF32; - return(ret); - break; - default: - ret.Type = Unknown; - ret.Simple = NULL; - return(ret); - } -} - -void -SimpleElem::DataProceed(Data_Header data_header, char* InBuff, int* pBuff, FlagStructure_t flag) -{ - ArrayElem matrix; - returned_ReadData ret; - ret = Get_Data_Class(data_header); - Type = ret.Type; - double tmpv; - switch(ret.Type) - { - case Numerical: - if(data_header.Number_of_Bytes/ret.Simple->size()) - for(unsigned int i=0;i<data_header.Number_of_Bytes/ret.Simple->size();i++) - { - tmpv=ret.Simple->ReadNum(InBuff, pBuff); - VNumeric.push_back(tmpv); - } - //to align pBuff on a 4 Bytes - if(*pBuff % 4) - *pBuff += 4-(*pBuff % 4); - delete ret.Simple; - break; - case AlphaNumeric: - for(unsigned int i=0;i<data_header.Number_of_Bytes/ret.Simple->size();i++) - Vstr.push_back(ret.Simple->ReadAlph(InBuff, pBuff, data_header.Number_of_Bytes)); - //to align pBuff on a 4 Bytes - if(*pBuff % 4) - *pBuff += 4-(*pBuff % 4); - delete ret.Simple; - break; - case Matrix: - array_elem = matrix.ReadArray_class(InBuff, pBuff); - array_elem->ReadArray(InBuff, pBuff, flag); - break; - case Compressed: - cerr << "Error: Compressed data in Mat-file not implemnted yet!\n set option -v6 when saving to Mat-file\n"; - exit(EXIT_FAILURE); - case Unknown: - cerr << "Error: Mat-file format use incomptible format with Matlab 7 specification!\n set option -v6 when saving to Mat-file\n"; - exit(EXIT_FAILURE); - } -} - -returned_ReadData_t -SimpleElem::ReadData(char* InBuff, int* pBuff) const -{ - Data_Header_t data_header; - data_header = ReadDataHeader(InBuff, pBuff); - return(Get_Data_Class(data_header)); -} - -void -SimpleElem::Collect(const string &name, bool found, CollectStruct &collect_struct) const -{ - if(VNumeric.size()) - { - if(found) - collect_struct.variable_double_name[collect_struct.tmp_name]=VNumeric; - } - else if(Vstr.size()) - { - if(found) - collect_struct.variable_string_name[collect_struct.tmp_name]=Vstr; - } - else - { - if(array_elem) - { - array_elem->Collect(name, found, collect_struct); - } - } -} - - -void -SimpleElem::Print() const -{ - if(VNumeric.size()) - { - for(vector<double>::const_iterator it=VNumeric.begin(); it!=VNumeric.end(); it++) - cout << " " << *it; - } - else if(Vstr.size()) - { - for(vector<string>::const_iterator it=Vstr.begin(); it!=Vstr.end(); it++) - cout << " " << *it; - } - else - { - if(array_elem) - array_elem->Print(); - } -} - -void -SimpleElem::Delete() const -{ - if(array_elem) - { - array_elem->Delete(); - delete array_elem; - } -} - -LongInt -ArrayElem::ReadINT32(char* InBuff, int* pBuff) const -{ - LongInt val; - memcpy(&val, InBuff+*pBuff, sizeof(val)); - *pBuff += sizeof(val); - return(val); -} - - -Array_Flag_t -ArrayElem::ReadArrayFlag(char* InBuff, int* pBuff) /*const*/ -{ - Array_Flag_t array_flag; - memcpy(&array_flag, InBuff+*pBuff, sizeof(array_flag)); - *pBuff += sizeof(array_flag); - array_complex = (bool)(array_flag.flag & 16); - array_global = array_flag.flag & 32; - array_logical = array_flag.flag & 64; - type = (Array_Type)array_flag.classe; - if(type==Sparse_array) - array_nzmax = array_flag.nzmax; - return(array_flag); -} - -void -ArrayElem::ReadArrayDimension(char* InBuff, int* pBuff) /*const*/ -{ - Data_Header_t data_header; - data_header = ReadDataHeader(InBuff, pBuff); - number_of_dimensions = data_header.Number_of_Bytes/4; - for(int i=0; i<number_of_dimensions; i++) - { - double tmp_d=ReadINT32(InBuff, pBuff); - dimension.push_back(tmp_d); - } - if(number_of_dimensions % 2) - *pBuff += sizeof(dimension[0]); -} - -void -ArrayElem::ReadStructureNames(char* InBuff, int* pBuff) -{ - Data_Header_t data_header, data_header_2; - LongInt Field_name_length; - data_header=ReadDataHeader(InBuff, pBuff); - Field_name_length=ReadINT32(InBuff, pBuff); - data_header_2=ReadDataHeader(InBuff, pBuff); - Structure_number = data_header_2.Number_of_Bytes/Field_name_length; - char tmp_c[Field_name_length]; - for(int i=0; i<Structure_number;i++) - { - memcpy(tmp_c, InBuff+*pBuff, Field_name_length); - *pBuff += Field_name_length; - string variable_name(tmp_c); - Structure_Elem_name.push_back(variable_name); - } -} - -void -ArrayElem::ReadArrayName(char* InBuff, int* pBuff) /*const*/ -{ - Data_Header_t data_header; - data_header = ReadDataHeader(InBuff, pBuff); - char tmp_c[data_header.Number_of_Bytes+1]; - memcpy(&tmp_c, InBuff+*pBuff, data_header.Number_of_Bytes); - tmp_c[data_header.Number_of_Bytes]=0; - variable_name.assign(tmp_c); - if(data_header.Number_of_Bytes<=4) - *pBuff += 4; - else if(data_header.Number_of_Bytes % 8) - *pBuff += 8*ceil(double(data_header.Number_of_Bytes) / 8); - else - *pBuff += data_header.Number_of_Bytes; -} - - -ArrayElem* -ArrayElem::ReadArray_class(char* InBuff, int* pBuff) const -{ - Array_Flag_t array_flag; - ArrayElem array_elem; - array_flag=array_elem.ReadArrayFlag(InBuff, pBuff); - switch(array_flag.classe) - { - case Cell_array: - return(new CellArray); - break; - case Structure_: - return(new Structure); - break; - case Object_: - return(new Object); - break; - case Character_array: - return(new CharacterArray); - break; - case Sparse_array: - return(new SparseArray); - break; - case Double_precision_array: - return(new DoublePrecisionArray); - break; - case Single_precision_array: - return(new SinglePrecisionArray); - break; - case Signed_integer_8_bit: - return(new Bit8SignedInteger); - break; - case Unsigned_integer_8_bit: - return(new Bit8UnsignedInteger); - break; - case Signed_integer_16_bit: - return(new Bit16SignedInteger); - break; - case Unsigned_integer_16_bit: - return(new Bit16UnsignedInteger); - break; - case Signed_integer_32_bit: - return(new Bit32SignedInteger); - break; - case Unsigned_integer_32_bit: - return(new Bit32UnsignedInteger); - break; - default: - return(NULL); - } - return(NULL); -} - -void -ArrayElem::Collect(const string &name, bool found, CollectStruct &collect_struct) const -{ -} - -void -ArrayElem::Print() const -{ -} - -void -ArrayElem::Delete() const -{ -} - -void -CellArray::Collect(const string &name, bool found, CollectStruct &collect_struct) const -{ -} - -void -CellArray::Print() const -{ - //cout << "CellArray: "<< variable_name << "\n"; -} - -void -CellArray::Delete() const -{ - //cout << "CellArray: "<< variable_name << "\n"; -} - -void -CellArray::ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag) -{ - SimpleElem* simple; - ReadArrayDimension(InBuff, pBuff); - Cell_number = 1; - for(unsigned int i=0;i<dimension.size();i++) - Cell_number *= dimension[i]; - ReadArrayName(InBuff, pBuff); - flag.character=true; - for(int i=0;i<Cell_number;i++) - { - simple=new SimpleElem; - Data_Header_t data_header=simple->ReadDataHeader(InBuff, pBuff); - VCell.push_back(simple); - simple->DataProceed(data_header, InBuff, pBuff, flag); - } -} - - -void -Structure::Collect(const string &name, bool found, CollectStruct &collect_struct) const -{ - if(name==variable_name || found) - { - found = true; - vector<string>::const_iterator it2=Structure_Elem_name.begin(); - for(vector<PSimpleElem>::const_iterator it=VCell.begin(); it!=VCell.end(); it++) - { - collect_struct.tmp_name = *it2; - it2++; - (*it)->Collect(name, found, collect_struct); - } - } -} - -void -Structure::Print() const -{ - cout << "Structure: " << variable_name << "\n"; - vector<string>::const_iterator it2=Structure_Elem_name.begin(); - int i=0; - for(vector<PSimpleElem>::const_iterator it=VCell.begin(); it!=VCell.end(); it++) - { - cout << ++i << " -> " << *it2 << " :"; - it2++; - (*it)->Print(); - cout << "\n"; - } -} - -void -Structure::Delete() const -{ - vector<string>::const_iterator it2=Structure_Elem_name.begin(); - for(vector<PSimpleElem>::const_iterator it=VCell.begin(); it!=VCell.end(); it++) - { - (*it)->Delete(); - delete *it; - } -} - -void -Structure::ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag) -{ - SimpleElem* simple; - int i; - ReadArrayDimension(InBuff, pBuff); - ReadArrayName(InBuff, pBuff); - ReadStructureNames(InBuff, pBuff); - flag.no_name=true; - for(i=0;i<Structure_number;i++) - { - simple=new SimpleElem; - Data_Header_t data_header=simple->ReadDataHeader(InBuff, pBuff); - simple->DataProceed(data_header, InBuff, pBuff, flag); - data_header=simple->ReadDataHeader(InBuff, pBuff); - VCell.push_back(simple); - simple->DataProceed(data_header, InBuff, pBuff, flag); - } -} - -void -Object::Collect(const string &name, bool found, CollectStruct &collect_struct) const -{ -} - - -void -Object::Print() const -{ -} - -void -Object::Delete() const -{ -} - - -void -Object::ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag) -{ - cerr << "Error: Object not implemented\n"; - exit(EXIT_FAILURE); -} - -void -CharacterArray::Collect(const string &name, bool found, CollectStruct &collect_struct) const -{ - if(name==variable_name || found) - { - found = true; - collect_struct.tmp_name=variable_name; - vector<PSimpleElem>::const_iterator it=VCell.begin(); - (*it)->Collect(name, found, collect_struct); - } -} - -void -CharacterArray::Print() const -{ - cout << "CharacterArray: " << variable_name << "\n"; - vector<PSimpleElem>::const_iterator it=VCell.begin(); - (*it)->Print(); - cout << "\n"; -} - -void -CharacterArray::Delete() const -{ - //cout << "CharacterArray: " << variable_name << "\n"; - vector<PSimpleElem>::const_iterator it=VCell.begin(); - (*it)->Delete(); - delete *it; - //cout << "\n"; -} - - -void -CharacterArray::ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag) -{ - Data_Header_t data_header; - SimpleElem* simple; - ReadArrayDimension(InBuff, pBuff); - Matrix_Elem_number = 1; - for(unsigned int i=0;i<dimension.size();i++) - { - Matrix_Elem_number *= dimension[i]; - } - simple=new SimpleElem; - VCell.push_back(simple); - data_header=simple->ReadDataHeader(InBuff, pBuff); - simple->DataProceed(data_header, InBuff, pBuff, flag); -} - -void -SparseArray::Collect(const string &name, bool found, CollectStruct &collect_struct) const -{ -} - -void -SparseArray::Print() const -{ - cout << "Sparse Array: " << variable_name << "\n"; -} - -void -SparseArray::Delete() const -{ - //cout << "Sparse Array: " << variable_name << "\n"; -} - - -void -SparseArray::ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag) -{ - cerr << "Error: Sparse Array not implemented\n"; - exit(EXIT_FAILURE); -} - -void -DoublePrecisionArray::Collect(const string &name, bool found, CollectStruct &collect_struct) const -{ - if(name==variable_name || found) - { - found = true; - collect_struct.tmp_name=variable_name; - vector<PSimpleElem>::const_iterator it=VCell.begin(); - (*it)->Collect(name, found, collect_struct); - } -} - - -void -DoublePrecisionArray::Print() const -{ - cout << "DoublePrecisionArray: " << variable_name << "\n"; - vector<PSimpleElem>::const_iterator it=VCell.begin(); - (*it)->Print(); - cout << "\n"; -} - -void -DoublePrecisionArray::Delete() const -{ - vector<PSimpleElem>::const_iterator it=VCell.begin(); - (*it)->Delete(); - delete *it; -} - -void -DoublePrecisionArray::ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag) -{ - Data_Header_t data_header; - SimpleElem* simple; - ReadArrayDimension(InBuff, pBuff); - Matrix_Elem_number = 1; - for(unsigned int i=0;i<dimension.size();i++) - { - Matrix_Elem_number *= dimension[i]; - } - if(!flag.no_name) - ReadArrayName(InBuff, pBuff); - simple=new SimpleElem; - VCell.push_back(simple); - data_header=simple->ReadDataHeader(InBuff, pBuff); - simple->DataProceed(data_header, InBuff, pBuff, flag); -} - -void -SinglePrecisionArray::Collect(const string &name, bool found, CollectStruct &collect_struct) const -{ - if(name==variable_name || found) - { - found = true; - collect_struct.tmp_name=variable_name; - vector<PSimpleElem>::const_iterator it=VCell.begin(); - (*it)->Collect(name, found, collect_struct); - } -} - - -void -SinglePrecisionArray::Print() const -{ - cout << "SinglePrecisionArray: " << variable_name << "\n"; - vector<PSimpleElem>::const_iterator it=VCell.begin(); - (*it)->Print(); - cout << "\n"; -} - -void -SinglePrecisionArray::Delete() const -{ - vector<PSimpleElem>::const_iterator it=VCell.begin(); - (*it)->Delete(); - delete *it; -} - -void -SinglePrecisionArray::ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag) -{ - Data_Header_t data_header; - SimpleElem* simple; - ReadArrayDimension(InBuff, pBuff); - Matrix_Elem_number = 1; - for(unsigned int i=0;i<dimension.size();i++) - { - Matrix_Elem_number *= dimension[i]; - } - if(!flag.no_name) - ReadArrayName(InBuff, pBuff); - simple=new SimpleElem; - VCell.push_back(simple); - data_header=simple->ReadDataHeader(InBuff, pBuff); - simple->DataProceed(data_header, InBuff, pBuff, flag); -} - -void -Bit8SignedInteger::Collect(const string &name, bool found, CollectStruct &collect_struct) const -{ -} - - - -void -Bit8SignedInteger::Print() const -{ - //cout << "Bit8SignedInteger: \n"; -} - -void -Bit8SignedInteger::Delete() const -{ - //cout << "Bit8SignedInteger: \n"; -} - -void -Bit8SignedInteger::ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag) -{ - Data_Header_t data_header; - SimpleElem* simple; - simple=new SimpleElem; - VCell.push_back(simple); - data_header=simple->ReadDataHeader(InBuff, pBuff); - simple->DataProceed(data_header, InBuff, pBuff, flag); -} - -void -Bit8UnsignedInteger::Collect(const string &name, bool found, CollectStruct &collect_struct) const -{ -} - - -void -Bit8UnsignedInteger::Print() const -{ - //cout << "Bit8UnsignedInteger: \n"; -} - -void -Bit8UnsignedInteger::Delete() const -{ - //cout << "Bit8UnsignedInteger: \n"; -} - -void -Bit8UnsignedInteger::ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag) -{ - Data_Header_t data_header; - SimpleElem* simple; - simple=new SimpleElem; - VCell.push_back(simple); - data_header=simple->ReadDataHeader(InBuff, pBuff); - simple->DataProceed(data_header, InBuff, pBuff, flag); -} - -void -Bit16SignedInteger::Collect(const string &name, bool found, CollectStruct &collect_struct) const -{ -} - -void -Bit16SignedInteger::Print() const -{ - //cout << "Bit16SignedInteger: \n"; -} - -void -Bit16SignedInteger::Delete() const -{ - //cout << "Bit16SignedInteger: \n"; -} - -void -Bit16SignedInteger::ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag) -{ - Data_Header_t data_header; - SimpleElem* simple; - simple=new SimpleElem; - VCell.push_back(simple); - data_header=simple->ReadDataHeader(InBuff, pBuff); - simple->DataProceed(data_header, InBuff, pBuff, flag); -} - -void -Bit16UnsignedInteger::Collect(const string &name, bool found, CollectStruct &collect_struct) const -{ -} - -void -Bit16UnsignedInteger::Print() const -{ - //cout << "Bit16UnsignedInteger: \n"; -} - -void -Bit16UnsignedInteger::Delete() const -{ - //cout << "Bit16UnsignedInteger: \n"; -} - -void -Bit16UnsignedInteger::ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag) -{ - Data_Header_t data_header; - SimpleElem* simple; - simple=new SimpleElem; - VCell.push_back(simple); - data_header=simple->ReadDataHeader(InBuff, pBuff); - simple->DataProceed(data_header, InBuff, pBuff, flag); -} - -void -Bit32SignedInteger::Collect(const string &name, bool found, CollectStruct &collect_struct) const -{ -} - -void -Bit32SignedInteger::Print() const -{ - //cout << "Bit32SignedInteger: \n"; -} - -void -Bit32SignedInteger::Delete() const -{ - //cout << "Bit32SignedInteger: \n"; -} - -void -Bit32SignedInteger::ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag) -{ - Data_Header_t data_header; - SimpleElem* simple; - simple=new SimpleElem; - VCell.push_back(simple); - data_header=simple->ReadDataHeader(InBuff, pBuff); - simple->DataProceed(data_header, InBuff, pBuff, flag); -} - -void -Bit32UnsignedInteger::Collect(const string &name, bool found, CollectStruct &collect_struct) const -{ -} - -void -Bit32UnsignedInteger::Print() const -{ - //cout << "Bit32UnsignedInteger: \n"; -} - -void -Bit32UnsignedInteger::Delete() const -{ - //cout << "Bit32UnsignedInteger: \n"; -} - -void -Bit32UnsignedInteger::ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag) -{ - Data_Header_t data_header; - SimpleElem* simple; - simple=new SimpleElem; - VCell.push_back(simple); - data_header=simple->ReadDataHeader(InBuff, pBuff); - simple->DataProceed(data_header, InBuff, pBuff, flag); -} - -Data_Header_t -MatlabFile::ReadDataHeader(ifstream &MatFile) -{ - Data_Header_t data_header; - MatFile.read(reinterpret_cast<char*>(&data_header.DataType),sizeof(data_header.DataType)); - MatFile.read(reinterpret_cast<char*>(&data_header.S_Number_of_Bytes),sizeof(data_header.S_Number_of_Bytes)); - if(data_header.S_Number_of_Bytes!=0) - data_header.Number_of_Bytes=data_header.S_Number_of_Bytes; - else - { - MatFile.read(reinterpret_cast<char*>(&data_header.Number_of_Bytes),sizeof(data_header.Number_of_Bytes)); - } - if(data_header.Number_of_Bytes<8) - data_header.Number_of_Bytes = 8; - return(data_header); -} - - - - - -void -MatlabFile::MatFileRead(string filename) -{ - ifstream MatFile; - Data_Header_t data_header; - SimpleElem *simpl; - int pBuff; - FlagStructure_t flag; - //ArrayElem elem; - MatFile.open(filename.c_str(),std::ios::in | std::ios::binary); - if (!MatFile.is_open()) - { - cerr << filename.c_str() << " Cannot be opened\n"; - exit(EXIT_FAILURE); - } - // Read the Header of the Mat-File - MatFile.read(reinterpret_cast<char*>(&header),sizeof(header)); - do - { - data_header=ReadDataHeader(MatFile); - char* InBuff; - InBuff = (char*)malloc(data_header.Number_of_Bytes+1); - MatFile.read(InBuff,data_header.Number_of_Bytes+1); - pBuff = 0; - simpl = new SimpleElem; - VSimpl.push_back(simpl); - flag.no_name=false; - flag.character=false; - simpl->DataProceed(data_header, InBuff, &pBuff, flag); - free(InBuff); - } - while(!MatFile.eof()); - MatFile.close(); -} - -bool -MatlabFile::Collect(const string &name, CollectStruct &collect_struct) const -{ - for(vector<PSimpleElem>::const_iterator it=VSimpl.begin(); it!=VSimpl.end(); it++) - (*it)->Collect(name, false, collect_struct); - return(!(collect_struct.variable_double_name.empty() and collect_struct.variable_string_name.empty())); -} - - -void -MatlabFile::MatFilePrint() -{ - for(vector<PSimpleElem>::iterator it=VSimpl.begin(); it!=VSimpl.end(); it++) - (*it)->Print(); -} - - -void -MatlabFile::Delete() -{ - for(vector<PSimpleElem>::iterator it=VSimpl.begin(); it!=VSimpl.end(); it++) - { - (*it)->Delete(); - delete *it; - } -} - -/* - int - main(int argc, char** argv) - { - CollectStruct collect_struct; - MatlabFile matlab_file; - matlab_file.MatFileRead("gimf_steady.mat"); - //matlab_file.MatFileRead("essai.mat"); - matlab_file.MatFilePrint(); - bool tmp_b=false; - //string tmp_s("stored_values"); - tmp_b=matlab_file.Collect("stored_values", collect_struct); - if(tmp_b) - { - int i=0; - for(map<string,vector<double> >::iterator it2=collect_struct.variable_double_name.begin();it2!=collect_struct.variable_double_name.end();it2++) - { - i++; - cout << i << " " << it2->first.c_str() << " : "; - for(vector<double>::iterator it=it2->second.begin();it!=it2->second.end();it++) - cout << *it; - cout << "\n"; - } - } - } - - -*/ diff --git a/MatlabFile.hh b/MatlabFile.hh deleted file mode 100644 index 5380f2daf13d273b093103ad6cb43c5b7c9dd134..0000000000000000000000000000000000000000 --- a/MatlabFile.hh +++ /dev/null @@ -1,426 +0,0 @@ -/* - * Copyright (C) 2009 Dynare Team - * - * This file is part of Dynare. - * - * Dynare is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dynare is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dynare. If not, see <http://www.gnu.org/licenses/>. - */ -/* - Usefull documentation: Matlab 7 Mat-File Format - ----------------------------------------------- - revision: October 2008 PDF only Rereleased for Version 7.7 (Release 2008b) - available at: http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/matfile_format.pdf -*/ - -#ifndef _MAT_FILE_HH -#define _MAT_FILE_HH - -#include <cstdio> -#include <cstdlib> -#include <string> -#include <fstream> -#include <cstring> -#include <iostream> -#include <map> -#include <vector> -#include <math.h> -//! zlib needed to uncompress the mat-file. It is available with GCC 4.3.2 but it needs a dll !! -//! => to avoid compress MatFile, save is used with option '-v6' in save_params_and_strady_state.m -//#include "zlib.h" -using namespace std; - - -enum Data_Type - { - miINT8 = 1, //8 bit, signed - miUINT8 = 2, //8 bit, unsigned - miINT16 = 3, //16-bit, signed - miUINT16 = 4, //16-bit, unsigned - miINT32 = 5, //32-bit, signed - miUINT32 = 6, //32-bit, unsigned - miSINGLE = 7, //IEEE� 754 single format - miDOUBLE = 9, //IEEE 754 double format - miINT64 = 12, //64-bit, signed - miUINT64 = 13, //64-bit, unsigned - miMATRIX = 14, //MATLAB array - miCOMPRESSED = 15, //Compressed Data - miUTF8 = 16, //Unicode UTF-8 Encoded Character Data - miUTF16 = 17, //Unicode UTF-16 Encoded Character Data - miUTF32 = 18 //Unicode UTF-32 Encoded Character Data - }; - -enum Array_Type - { - Cell_array = 1, - Structure_ = 2, - Object_ = 3, - Character_array = 4, - Sparse_array = 5, - Double_precision_array = 6, - Single_precision_array = 7, - Signed_integer_8_bit = 8, - Unsigned_integer_8_bit = 9, - Signed_integer_16_bit = 10, - Unsigned_integer_16_bit = 11, - Signed_integer_32_bit = 12, - Unsigned_integer_32_bit = 13 - }; - - -enum Function_Returned_Type - { - Numerical =1, - AlphaNumeric =2, - Matrix =3, - Compressed =4, - Unknown =5 - }; - -class ArrayElem; -class SimpleElem; - -typedef long long LongLongInt; -typedef long int LongInt; -typedef short int Int; -//typedef char ShortInt; -typedef unsigned long int uLongInt ; -typedef unsigned short int uShortInt ; -typedef short int ShortInt ; -typedef class SimpleElem *PSimpleElem; - -//!Header of MatFile -typedef struct Header -{ - char Theader[124]; - short int Version; - char Edian_Indicator[2]; -} - Header_t; - -typedef struct Data_Header -{ - ShortInt S_Number_of_Bytes; - ShortInt DataType; - uLongInt Number_of_Bytes; -} - Data_Header_t; - -typedef struct Array_Flag -{ - Data_Header_t tag; - unsigned char classe; - unsigned char flag; - char undef1[2]; - uLongInt nzmax; -} - Array_Flag_t; - - -typedef struct returned_ReadData -{ - SimpleElem* Simple; - Function_Returned_Type Type; -} returned_ReadData_t; - - -typedef struct FlagStructure -{ - bool no_name; - bool character; -} FlagStructure_t; - -typedef struct CollectStruct -{ - /*vector<string> variable_name; - vector< vector<double> > variable_double; - vector< vector<string> > variable_string;*/ - string tmp_name; - map<string,vector<string> > variable_string_name; - map<string,vector<double> > variable_double_name; -} - CollectStruct; - -typedef vector<int> Array_Dimensions_t; - - - - - -//! Base class for simple elements in Mat-File -class SimpleElem -{ -public: - bool verbose; - vector<double> VNumeric; - vector<string> Vstr; - ArrayElem *array_elem; - int Type; - SimpleElem(); - virtual ~SimpleElem(); - virtual double ReadNum(char* InBuff, int* pBuff) const{return(0);}; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const{return(NULL);}; - virtual Data_Header_t ReadDataHeader(char* InBuff, int* pBuff) const; - virtual int size() const{cout << "oups\n";return(0);}; - void Print() const; - void Delete() const; - void Collect(const string &name, bool found, CollectStruct &collect_struct) const; - returned_ReadData_t ReadData(char* InBuff, int* pBuff) const; - returned_ReadData_t Get_Data_Class(Data_Header_t data_header) const; - void DataProceed(Data_Header data_header, char* InBuff, int* pBuff, FlagStructure flag); -}; - - -class UTF8 : public SimpleElem -{ -public: - virtual int size() const {return(1);}; - virtual double ReadNum(char* InBuff, int* pBuff) const {return(0.0);}; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const; -}; - -class UTF16 : public SimpleElem -{ -public: - virtual int size() const {return(2);}; - virtual double ReadNum(char* InBuff, int* pBuff) const {return(0.0);}; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const; -}; - -class UTF32 : public SimpleElem -{ -public: - virtual int size() const {return(4);}; - virtual double ReadNum(char* InBuff, int* pBuff) const {return(0.0);}; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const; -}; - -class INT8 : public SimpleElem -{ -public: - virtual int size() const {return(1);}; - virtual double ReadNum(char* InBuff, int* pBuff) const; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(string(""));}; -}; - -class INT16 : public SimpleElem -{ -public: - virtual int size() const {return(2);}; - virtual double ReadNum(char* InBuff, int* pBuff) const; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(string(""));}; -}; - - -class INT32 : public SimpleElem -{ -public: - virtual int size() const {return(4);}; - virtual double ReadNum(char* InBuff, int* pBuff) const; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(string(""));}; -}; - - -class INT64 : public SimpleElem -{ -public: - virtual int size() const {return(8);}; - virtual double ReadNum(char* InBuff, int* pBuff) const; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(string(""));}; -}; - - -class Single : public SimpleElem -{ -public: - virtual int size() const {return(4);}; - virtual double ReadNum(char* InBuff, int* pBuff) const; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(string(""));}; -}; - - -class Double : public SimpleElem -{ -public: - virtual int size() const {return(8);}; - virtual double ReadNum(char* InBuff, int* pBuff) const; - virtual string ReadAlph(char* InBuff, int* pBuff, int Size) const {return(string(""));}; -}; - -//! Base class for Array Element in Mat-File -class ArrayElem : public SimpleElem -{ -private: - -protected: - -public: - int Cell_number, Structure_number, Matrix_Elem_number; - Array_Type Type; - vector<PSimpleElem> VCell; - vector<string> Structure_Elem_name; - bool array_complex, array_global, array_logical; - string variable_name; - int array_nzmax; - int number_of_dimensions; - vector<int> dimension; - vector<double> Double_value; - vector<string> String_value; - - Array_Type type; - ArrayElem(); - virtual LongInt ReadINT32(char* InBuff, int* pBuff) const; - virtual Array_Flag_t ReadArrayFlag(char* InBuff, int* pBuff) /*const*/; - virtual void ReadArrayDimension(char* InBuff, int* pBuff) /*const*/; - virtual void ReadArrayName(char* InBuff, int* pBuff) /*const*/; - virtual void ReadStructureNames(char* InBuff, int* pBuff); - virtual ArrayElem* ReadArray_class(char* InBuff, int* pBuff) const; - virtual void ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag) /*const*/ {cout << "oups..\n";}; - virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; - virtual void Print() const; - virtual void Delete() const; - virtual ~ArrayElem(); -}; - -class CellArray : public ArrayElem -{ -public: - virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; - virtual void Print() const; - virtual void Delete() const; - virtual void ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag); -}; - -class Structure : public ArrayElem -{ -public: - virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; - virtual void Print() const; - virtual void Delete() const; - virtual void ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag); -}; - -class Object : public ArrayElem -{ -public: - virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; - virtual void Print() const; - virtual void Delete() const; - virtual void ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag); -}; - -class CharacterArray : public ArrayElem -{ -public: - virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; - virtual void Print() const; - virtual void Delete() const; - virtual void ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag); -}; - -class SparseArray : public ArrayElem -{ -public: - virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; - virtual void Print() const; - virtual void Delete() const; - virtual void ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag); -}; - -class DoublePrecisionArray : public ArrayElem -{ -public: - virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; - virtual void Print() const; - virtual void Delete() const; - virtual void ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag); -}; - -class SinglePrecisionArray : public ArrayElem -{ -public: - virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; - virtual void Print() const; - virtual void Delete() const; - virtual void ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag); -}; - -class Bit8SignedInteger : public ArrayElem -{ -public: - virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; - virtual void Print() const; - virtual void Delete() const; - virtual void ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag); -}; - -class Bit8UnsignedInteger : public ArrayElem -{ -public: - virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; - virtual void Print() const; - virtual void Delete() const; - virtual void ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag); -}; - -class Bit16SignedInteger : public ArrayElem -{ -public: - virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; - virtual void Print() const; - virtual void Delete() const; - virtual void ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag); -}; - -class Bit16UnsignedInteger : public ArrayElem -{ -public: - virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; - virtual void Print() const; - virtual void Delete() const; - virtual void ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag); -}; - -class Bit32SignedInteger : public ArrayElem -{ -public: - virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; - virtual void Print() const; - virtual void Delete() const; - virtual void ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag); -}; - -class Bit32UnsignedInteger : public ArrayElem -{ -public: - virtual void Collect(const string &name, bool found, CollectStruct &collect_struct) const; - virtual void Print() const; - virtual void Delete() const; - virtual void ReadArray(char* InBuff, int* pBuff, FlagStructure_t flag); -}; - - -class MatlabFile -{ -public: - Header_t header; - vector<PSimpleElem> VSimpl; - MatlabFile(); - ~MatlabFile(); - void MatFileRead(string filename); - void MatFilePrint(); - void Delete(); - bool Collect(const string &name, CollectStruct &collect_struct) const; - Data_Header_t ReadDataHeader(ifstream &MatFile); -}; -#endif diff --git a/NumericalInitialization.cc b/NumericalInitialization.cc index 65ba044208171676a3e17f04664a232f9fcfbdd2..7b98b0c81b34f663e0c09a4ca0bca64b2c3900a7 100644 --- a/NumericalInitialization.cc +++ b/NumericalInitialization.cc @@ -17,9 +17,9 @@ * along with Dynare. If not, see <http://www.gnu.org/licenses/>. */ -#include "NumericalInitialization.hh" +#include <cstdlib> -#include "MatlabFile.hh" +#include "NumericalInitialization.hh" InitParamStatement::InitParamStatement(int symb_id_arg, const NodeID param_value_arg, @@ -247,49 +247,73 @@ SaveParamsAndSteadyStateStatement::writeOutput(ostream &output, const string &ba output << "save_params_and_steady_state('" << filename << "');" << endl; } -LoadParamsAndSteadyStateStatement::LoadParamsAndSteadyStateStatement(const string &filename_arg, +LoadParamsAndSteadyStateStatement::LoadParamsAndSteadyStateStatement(const string &filename, const SymbolTable &symbol_table_arg) : - filename(filename_arg), symbol_table(symbol_table_arg) -{ -} - -void -LoadParamsAndSteadyStateStatement::writeOutput(ostream &output, const string &basename) const -{ - output << "load_params_and_steady_state('" << filename << "');" << endl; -} - -void -LoadParamsAndSteadyStateStatement::fillEvalContext(eval_context_type &eval_context) const + symbol_table(symbol_table_arg) { cout << "Reading " << filename << " ..."; - MatlabFile matlab_file; - matlab_file.MatFileRead(filename); - - string sname = "stored_values"; - - CollectStruct collect_struct; - bool tmp_b = matlab_file.Collect(sname, collect_struct); - matlab_file.Delete(); - if (!tmp_b) - cout << "The structure " << sname << " was not found in " << filename << endl; - cout << "done\n"; + ifstream f; + f.open(filename.c_str(), ios::in); + if (f.bad()) + { + cerr << "ERROR: Can't open " << filename << endl; + exit(EXIT_FAILURE); + } - for(map<string, vector<double> >::iterator it = collect_struct.variable_double_name.begin(); - it != collect_struct.variable_double_name.end(); it++) + while(true) { - const string &symbol_name = it->first; - double val = it->second[0]; + string symb_name, value; + f >> symb_name >> value; + if (f.eof()) + break; try { - int symb_id = symbol_table.getID(symbol_name); - eval_context[symb_id] = val; + int symb_id = symbol_table.getID(symb_name); + content[symb_id] = value; } catch(SymbolTable::UnknownSymbolNameException &e) { - cerr << "Warning: unknown symbol " << symbol_name << " in " << filename << endl; + cerr << "WARNING: Unknown symbol " << symb_name << " in " << filename << endl; + } + } +} + +void +LoadParamsAndSteadyStateStatement::writeOutput(ostream &output, const string &basename) const +{ + for(map<int, string>::const_iterator it = content.begin(); + it != content.end(); it++) + { + switch(symbol_table.getType(it->first)) + { + case eParameter: + output << "M_.params"; + break; + case eEndogenous: + output << "oo_.steady_state"; + break; + case eExogenous: + output << "oo_.exo_steady_state"; + break; + case eExogenousDet: + output << "oo_.exo_det_steady_state"; + break; + default: + cerr << "ERROR: Unsupported variable type for " << symbol_table.getName(it->first) << " in load_params_and_steady_state" << endl; + exit(EXIT_FAILURE); } + + int tsid = symbol_table.getTypeSpecificID(it->first) + 1; + output << "(" << tsid << ") = " << it->second << ";" << endl; } } + +void +LoadParamsAndSteadyStateStatement::fillEvalContext(eval_context_type &eval_context) const +{ + for(map<int, string>::const_iterator it = content.begin(); + it != content.end(); it++) + eval_context[it->first] = atof(it->second.c_str()); +} diff --git a/NumericalInitialization.hh b/NumericalInitialization.hh index 77972ca529095a1490c94fda7c5dc4f118053874..c444fc62f22db16840401772ba05e1ee24f1183e 100644 --- a/NumericalInitialization.hh +++ b/NumericalInitialization.hh @@ -134,10 +134,12 @@ public: class LoadParamsAndSteadyStateStatement : public Statement { private: - const string filename; const SymbolTable &symbol_table; + //! Content of the file + /*! Maps symbol ID to numeric value (stored as string) */ + map<int, string> content; public: - LoadParamsAndSteadyStateStatement(const string &filename_arg, + LoadParamsAndSteadyStateStatement(const string &filename, const SymbolTable &symbol_table_arg); virtual void writeOutput(ostream &output, const string &basename) const; //! Fill eval context with parameters/variables values diff --git a/Statement.hh b/Statement.hh index deba172a86bfb66bf69083b407129665a051ee07..47a57dbadb3643a3a6b2778d19bbbf48a902dd67 100644 --- a/Statement.hh +++ b/Statement.hh @@ -63,7 +63,7 @@ public: //! Do some internal check, and fill the ModFileStructure class virtual void checkPass(ModFileStructure &mod_file_struct); virtual void computingPass(); - //! Write Matlab outout code + //! Write Matlab output code /*! \param output is the output stream of the main matlab file \param basename is the name of the modfile (without extension) which can be used to build auxiliary files