FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

csvSetWriter.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-2011 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "csvSetWriter.H"
00027 #include <sampling/coordSet.H>
00028 #include <OpenFOAM/fileName.H>
00029 #include <OpenFOAM/OFstream.H>
00030 
00031 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00032 
00033 template<class Type>
00034 Foam::csvSetWriter<Type>::csvSetWriter()
00035 :
00036     writer<Type>()
00037 {}
00038 
00039 
00040 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00041 
00042 template<class Type>
00043 Foam::csvSetWriter<Type>::~csvSetWriter()
00044 {}
00045 
00046 
00047 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00048 
00049 template<class Type>
00050 Foam::fileName Foam::csvSetWriter<Type>::getFileName
00051 (
00052     const coordSet& points,
00053     const wordList& valueSetNames
00054 ) const
00055 {
00056     return this->getBaseName(points, valueSetNames) + ".csv";
00057 }
00058 
00059 
00060 template<class Type>
00061 void Foam::csvSetWriter<Type>::write
00062 (
00063     const coordSet& points,
00064     const wordList& valueSetNames,
00065     const List<const Field<Type>*>& valueSets,
00066     Ostream& os
00067 ) const
00068 {
00069     writeHeader(points,valueSetNames,os);
00070 
00071     // Collect sets into columns
00072     List<const List<Type>*> columns(valueSets.size());
00073 
00074     forAll(valueSets, i)
00075     {
00076         columns[i] = valueSets[i];
00077     }
00078 
00079     this->writeTable(points, columns, os);
00080 }
00081 
00082 
00083 template<class Type>
00084 void Foam::csvSetWriter<Type>::write
00085 (
00086     const bool writeTracks,
00087     const PtrList<coordSet>& points,
00088     const wordList& valueSetNames,
00089     const List<List<Field<Type> > >& valueSets,
00090     Ostream& os
00091 ) const
00092 {
00093     writeHeader(points[0],valueSetNames,os);
00094 
00095     if (valueSets.size() != valueSetNames.size())
00096     {
00097         FatalErrorIn("csvSetWriter<Type>::write(..)")
00098             << "Number of variables:" << valueSetNames.size() << endl
00099             << "Number of valueSets:" << valueSets.size()
00100             << exit(FatalError);
00101     }
00102 
00103     List<const List<Type>*> columns(valueSets.size());
00104 
00105     forAll(points, trackI)
00106     {
00107         // Collect sets into columns
00108         forAll(valueSets, i)
00109         {
00110             columns[i] = &valueSets[i][trackI];
00111         }
00112 
00113         this->writeTable(points[trackI], columns, os);
00114         os  << nl << nl;
00115     }
00116 }
00117 
00118 
00119 template<class Type>
00120 void Foam::csvSetWriter<Type>::writeSeparator(Ostream& os) const
00121 {
00122     os << token::COMMA;
00123 }
00124 
00125 
00126 namespace Foam
00127 {
00128     // otherwise compiler complains about specialization
00129     template<>
00130     void csvSetWriter<scalar>::writeHeader
00131     (
00132         const coordSet& points,
00133         const wordList& valueSetNames,
00134         Ostream& os
00135     ) const
00136     {
00137         writeCoordHeader(points, os);
00138 
00139         forAll(valueSetNames, i)
00140         {
00141             if (i > 0)
00142             {
00143                 writeSeparator(os);
00144             }
00145             os << valueSetNames[i];
00146         }
00147 
00148         os << nl;
00149     }
00150 } // end namespace
00151 
00152 
00153 template<class Type>
00154 void Foam::csvSetWriter<Type>::writeHeader
00155 (
00156     const coordSet& points,
00157     const wordList& valueSetNames,
00158     Ostream& os
00159 ) const
00160 {
00161     writeCoordHeader(points, os);
00162 
00163     forAll(valueSetNames, i)
00164     {
00165         for (label j=0; j<Type::nComponents; j++)
00166         {
00167             if (i>0 || j>0)
00168             {
00169                 writeSeparator(os);
00170             }
00171             os << valueSetNames[i] << "_" << j;
00172         }
00173     }
00174 
00175     os << nl;
00176 }
00177 
00178 
00179 template<class Type>
00180 void Foam::csvSetWriter<Type>::writeCoordHeader
00181 (
00182     const coordSet& points,
00183     Ostream& os
00184 ) const
00185 {
00186     if (points.hasVectorAxis())
00187     {
00188         forAll(points, i)
00189         {
00190             os << points.axis()[i];
00191             writeSeparator(os);
00192         }
00193     }
00194     else
00195     {
00196         os << points.axis();
00197         writeSeparator(os);
00198     }
00199 }
00200 
00201 
00202 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines