nastranSurfaceWriterTemplates.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) 2012 OpenFOAM Foundation
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 "OFstream.H"
00027 #include "IOmanip.H"
00028 #include "OSspecific.H"
00029 
00030 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00031 
00032 template<class Type>
00033 void Foam::nastranSurfaceWriter::writeFaceValue
00034 (
00035     const word& nasFieldName,
00036     const Type& value,
00037     const label EID,
00038     OFstream& os
00039 ) const
00040 {
00041     // Fixed short/long formats:
00042     // 1 Nastran distributed load type, e.g. PLOAD4
00043     // 2 SID  : load set ID
00044     // 3 EID  : element ID
00045     // 4 onwards: load values
00046 
00047     label SID = 1;
00048 
00049     switch (writeFormat_)
00050     {
00051         case wfShort:
00052         {
00053             os.setf(ios_base::left);
00054             os  << setw(8) << nasFieldName;
00055             os.unsetf(ios_base::left);
00056             os.setf(ios_base::right);
00057             os  << setw(8) << SID
00058                 << setw(8) << EID;
00059 
00060             for (direction dirI = 0; dirI < pTraits<Type>::nComponents; dirI++)
00061             {
00062                 os  << setw(8) << component(value, dirI);
00063             }
00064 
00065             os.unsetf(ios_base::right);
00066 
00067             break;
00068         }
00069         case wfLong:
00070         {
00071             os.setf(ios_base::left);
00072             os  << setw(8) << word(nasFieldName + "*");
00073             os.unsetf(ios_base::left);
00074             os.setf(ios_base::right);
00075             os  << setw(16) << SID
00076                 << setw(16) << EID;
00077 
00078             for (direction dirI = 0; dirI < pTraits<Type>::nComponents; dirI++)
00079             {
00080                 os  << setw(16) << component(value, dirI);
00081             }
00082 
00083             os.unsetf(ios_base::right);
00084 
00085             os  << nl;
00086 
00087             os.setf(ios_base::left);
00088             os  << '*';
00089             os.unsetf(ios_base::left);
00090 
00091             break;
00092         }
00093         case wfFree:
00094         {
00095             os  << nasFieldName << ','
00096                 << SID << ','
00097                 << EID;
00098 
00099             for (direction dirI = 0; dirI < pTraits<Type>::nComponents; dirI++)
00100             {
00101                 os  << ',' << component(value, dirI);
00102             }
00103 
00104             break;
00105         }
00106         default:
00107         {
00108         }
00109     }
00110 
00111     os << nl;
00112 }
00113 
00114 
00115 template<class Type>
00116 void Foam::nastranSurfaceWriter::writeTemplate
00117 (
00118     const fileName& outputDir,
00119     const fileName& surfaceName,
00120     const pointField& points,
00121     const faceList& faces,
00122     const word& fieldName,
00123     const Field<Type>& values,
00124     const bool isNodeValues,
00125     const bool verbose
00126 ) const
00127 {
00128     if (!fieldMap_.found(fieldName))
00129     {
00130         WarningIn
00131         (
00132             "void Foam::nastranSurfaceWriter::writeTemplate"
00133             "("
00134                 "const fileName&, "
00135                 "const fileName&, "
00136                 "const pointField&, "
00137                 "const faceList&, "
00138                 "const word&, "
00139                 "const Field<Type>&, "
00140                 "const bool, "
00141                 "const bool"
00142             ") const"
00143         )
00144             << "No mapping found between field " << fieldName
00145             << " and corresponding Nastran field.  Available types are:"
00146             << fieldMap_
00147             << exit(FatalError);
00148 
00149         return;
00150     }
00151 
00152     const word& nasFieldName(fieldMap_[fieldName]);
00153 
00154     if (!isDir(outputDir/fieldName))
00155     {
00156         mkDir(outputDir/fieldName);
00157     }
00158 
00159     // const scalar timeValue = Foam::name(this->mesh().time().timeValue());
00160     const scalar timeValue = 0.0;
00161 
00162     OFstream os(outputDir/fieldName/surfaceName + ".dat");
00163     formatOS(os);
00164 
00165     if (verbose)
00166     {
00167         Info<< "Writing nastran file to " << os.name() << endl;
00168     }
00169 
00170     os  << "TITLE=OpenFOAM " << surfaceName.c_str() << " " << fieldName
00171         << " data" << nl
00172         << "$" << nl
00173         << "TIME " << timeValue << nl
00174         << "$" << nl
00175         << "BEGIN BULK" << nl;
00176 
00177     List<DynamicList<face> > decomposedFaces(faces.size());
00178 
00179     writeGeometry(points, faces, decomposedFaces, os);
00180 
00181 
00182     os  << "$" << nl
00183         << "$ Field data" << nl
00184         << "$" << nl;
00185 
00186     if (isNodeValues)
00187     {
00188         label n = 0;
00189 
00190         forAll(decomposedFaces, i)
00191         {
00192             const DynamicList<face>& dFaces = decomposedFaces[i];
00193             forAll(dFaces, faceI)
00194             {
00195                 Type v = pTraits<Type>::zero;
00196                 const face& f = dFaces[faceI];
00197 
00198                 forAll(f, fptI)
00199                 {
00200                     v += values[f[fptI]];
00201                 }
00202 
00203                 writeFaceValue(nasFieldName, v, ++n, os);
00204             }
00205         }
00206     }
00207     else
00208     {
00209         label n = 0;
00210 
00211         forAll(decomposedFaces, i)
00212         {
00213             const DynamicList<face>& dFaces = decomposedFaces[i];
00214 
00215             forAll(dFaces, faceI)
00216             {
00217                 writeFaceValue(nasFieldName, values[faceI], ++n, os);
00218             }
00219         }
00220     }
00221 
00222     os  << "ENDDATA" << endl;
00223 }
00224 
00225 
00226 // ************************************************************************* //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines