OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

MatrixIO.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "Matrix.H"
00028 #include "Istream.H"
00029 #include "Ostream.H"
00030 #include "token.H"
00031 #include "contiguous.H"
00032 
00033 // * * * * * * * * * * * * * * * Ostream Operator *  * * * * * * * * * * * * //
00034 
00035 template<class Form, class Type>
00036 Foam::Matrix<Form, Type>::Matrix(Istream& is)
00037 :
00038     n_(0),
00039     m_(0),
00040     v_(NULL)
00041 {
00042     operator>>(is, *this);
00043 }
00044 
00045 
00046 template<class Form, class Type>
00047 Foam::Istream& Foam::operator>>(Istream& is, Matrix<Form, Type>& M)
00048 {
00049     // Anull matrix
00050     M.clear();
00051 
00052     is.fatalCheck("operator>>(Istream&, Matrix<Form, Type>&)");
00053 
00054     token firstToken(is);
00055 
00056     is.fatalCheck("operator>>(Istream&, Matrix<Form, Type>&) : reading first token");
00057 
00058     if (firstToken.isLabel())
00059     {
00060         M.n_ = firstToken.labelToken();
00061         M.m_ = readLabel(is);
00062 
00063         label nm = M.n_*M.m_;
00064 
00065         // Read list contents depending on data format
00066         if (is.format() == IOstream::ASCII || !contiguous<Type>())
00067         {
00068             // Read beginning of contents
00069             char listDelimiter = is.readBeginList("Matrix");
00070 
00071             if (nm)
00072             {
00073                 M.allocate();
00074                 Type* v = M.v_[0];
00075 
00076                 if (listDelimiter == token::BEGIN_LIST)
00077                 {
00078                     label k = 0;
00079 
00080                     // loop over rows
00081                     for (register label i=0; i<M.n(); i++)
00082                     {
00083                         listDelimiter = is.readBeginList("MatrixRow");
00084 
00085                         for (register label j=0; j<M.m(); j++)
00086                         {
00087                             is >> v[k++];
00088 
00089                             is.fatalCheck
00090                             (
00091                                 "operator>>(Istream&, Matrix<Form, Type>&) : "
00092                                 "reading entry"
00093                             );
00094                         }
00095 
00096                         is.readEndList("MatrixRow");
00097                     }
00098                 }
00099                 else
00100                 {
00101                     Type element;
00102                     is >> element;
00103 
00104                     is.fatalCheck
00105                     (
00106                         "operator>>(Istream&, Matrix<Form, Type>&) : "
00107                         "reading the single entry"
00108                     );
00109 
00110                     for (register label i=0; i<nm; i++)
00111                     {
00112                         v[i] = element;
00113                     }
00114                 }
00115             }
00116 
00117             // Read end of contents
00118             is.readEndList("Matrix");
00119         }
00120         else
00121         {
00122             if (nm)
00123             {
00124                 M.allocate();
00125                 Type* v = M.v_[0];
00126 
00127                 is.read(reinterpret_cast<char*>(v), nm*sizeof(Type));
00128 
00129                 is.fatalCheck
00130                 (
00131                     "operator>>(Istream&, Matrix<Form, Type>&) : "
00132                     "reading the binary block"
00133                 );
00134             }
00135         }
00136     }
00137     else
00138     {
00139         FatalIOErrorIn("operator>>(Istream&, Matrix<Form, Type>&)", is)
00140             << "incorrect first token, expected <int>, found "
00141             << firstToken.info()
00142             << exit(FatalIOError);
00143     }
00144 
00145     return is;
00146 }
00147 
00148 
00149 template<class Form, class Type>
00150 Foam::Ostream& Foam::operator<<(Ostream& os, const Matrix<Form, Type>& M)
00151 {
00152     label nm = M.n_*M.m_;
00153 
00154     os  << M.n() << token::SPACE << M.m();
00155 
00156     // Write list contents depending on data format
00157     if (os.format() == IOstream::ASCII || !contiguous<Type>())
00158     {
00159         if (nm)
00160         {
00161             bool uniform = false;
00162 
00163             const Type* v = M.v_[0];
00164 
00165             if (nm > 1 && contiguous<Type>())
00166             {
00167                 uniform = true;
00168 
00169                 for (register label i=0; i< nm; i++)
00170                 {
00171                     if (v[i] != v[0])
00172                     {
00173                         uniform = false;
00174                         break;
00175                     }
00176                 }
00177             }
00178 
00179             if (uniform)
00180             {
00181                 // Write size of list and start contents delimiter
00182                 os  << token::BEGIN_BLOCK;
00183 
00184                 // Write list contents
00185                 os << v[0];
00186 
00187                 // Write end of contents delimiter
00188                 os << token::END_BLOCK;
00189             }
00190             else if (nm < 10 && contiguous<Type>())
00191             {
00192                 // Write size of list and start contents delimiter
00193                 os  << token::BEGIN_LIST;
00194 
00195                 label k = 0;
00196 
00197                 // loop over rows
00198                 for (register label i=0; i< M.n(); i++)
00199                 {
00200                     os  << token::BEGIN_LIST;
00201 
00202                     // Write row
00203                     for (register label j=0; j< M.m(); j++)
00204                     {
00205                         if (j > 0) os << token::SPACE;
00206                         os << v[k++];
00207                     }
00208 
00209                     os << token::END_LIST;
00210                 }
00211 
00212                 // Write end of contents delimiter
00213                 os << token::END_LIST;
00214             }
00215             else
00216             {
00217                 // Write size of list and start contents delimiter
00218                 os  << nl << token::BEGIN_LIST;
00219 
00220                 label k = 0;
00221 
00222                 // loop over rows
00223                 for (register label i=0; i< M.n(); i++)
00224                 {
00225                     os  << nl << token::BEGIN_LIST;
00226 
00227                     // Write row
00228                     for (register label j=0; j< M.m(); j++)
00229                     {
00230                         os << nl << v[k++];
00231                     }
00232 
00233                     os << nl << token::END_LIST;
00234                 }
00235 
00236                 // Write end of contents delimiter
00237                 os << nl << token::END_LIST << nl;
00238             }
00239         }
00240         else
00241         {
00242             os  << token::BEGIN_LIST << token::END_LIST << nl;
00243         }
00244     }
00245     else
00246     {
00247         if (nm)
00248         {
00249             os.write(reinterpret_cast<const char*>(M.v_[0]), nm*sizeof(Type));
00250         }
00251     }
00252 
00253     // Check state of IOstream
00254     os.check("Ostream& operator<<(Ostream&, const Matrix&)");
00255 
00256     return os;
00257 }
00258 
00259 
00260 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd