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

readNAS.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 Description
00026     Nastran surface reader.
00027 
00028     - Uses the Ansa "$ANSA_NAME" or the Hypermesh "$HMNAME COMP" extensions
00029       to obtain patch names.
00030     - Handles Nastran short and long formats, but not free format.
00031     - Properly handles the Nastran compact floating point notation: \n
00032     @verbatim
00033         GRID          28        10.20269-.030265-2.358-8
00034     @endverbatim
00035 
00036 
00037 \*---------------------------------------------------------------------------*/
00038 
00039 #include "triSurface.H"
00040 #include "IFstream.H"
00041 #include "IStringStream.H"
00042 
00043 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00044 
00045 namespace Foam
00046 {
00047 
00048 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00049 
00050 // Do weird things to extract number
00051 static scalar parseNASCoord(const string& s)
00052 {
00053     size_t expSign = s.find_last_of("+-");
00054 
00055     if (expSign != string::npos && expSign > 0 && !isspace(s[expSign-1]))
00056     {
00057         scalar mantissa = readScalar(IStringStream(s.substr(0, expSign))());
00058         scalar exponent = readScalar(IStringStream(s.substr(expSign+1))());
00059 
00060         if (s[expSign] == '-')
00061         {
00062             exponent = -exponent;
00063         }
00064         return mantissa*pow(10, exponent);
00065     }
00066     else
00067     {
00068         return readScalar(IStringStream(s)());
00069     }
00070 }
00071 
00072 
00073 bool triSurface::readNAS(const fileName& fName)
00074 {
00075     IFstream is(fName);
00076 
00077     if (!is.good())
00078     {
00079         FatalErrorIn("triSurface::readNAS(const fileName&)")
00080             << "Cannot read file " << fName
00081             << exit(FatalError);
00082     }
00083 
00084     // coordinates of point
00085     DynamicList<point> points;
00086     // Nastran index of point
00087     DynamicList<label> indices;
00088     // Faces in terms of Nastran point indices
00089     DynamicList<labelledTri> faces;
00090     // From face group to patch
00091     Map<label> groupToPatch;
00092     label nPatches = 0;
00093     // Name for face group
00094     Map<word> groupToName;
00095 
00096     // Ansa tags. Denoted by $ANSA_NAME. These will appear just before the
00097     // first use of a type. We read them and store the pshell types which
00098     // are used to name the patches.
00099     label ansaId = -1;
00100     word ansaType;
00101     string ansaName;
00102 
00103     // A single warning per unrecognized command
00104     HashSet<word> unhandledCmd;
00105 
00106     while (is.good())
00107     {
00108         string line;
00109         is.getLine(line);
00110 
00111         // Ansa extension
00112         if (line.substr(0, 10) == "$ANSA_NAME")
00113         {
00114             string::size_type sem0 = line.find (';', 0);
00115             string::size_type sem1 = line.find (';', sem0+1);
00116             string::size_type sem2 = line.find (';', sem1+1);
00117 
00118             if
00119             (
00120                 sem0 != string::npos
00121              && sem1 != string::npos
00122              && sem2 != string::npos
00123             )
00124             {
00125                 ansaId = readLabel
00126                 (
00127                     IStringStream(line.substr(sem0+1, sem1-sem0-1))()
00128                 );
00129                 ansaType = line.substr(sem1+1, sem2-sem1-1);
00130 
00131                 string nameString;
00132                 is.getLine(ansaName);
00133                 if (ansaName[ansaName.size()-1] == '\r')
00134                 {
00135                     ansaName = ansaName.substr(1, ansaName.size()-2);
00136                 }
00137                 else
00138                 {
00139                     ansaName = ansaName.substr(1, ansaName.size()-1);
00140                 }
00141 
00142                 // Info<< "ANSA tag for NastranID:" << ansaId
00143                 //     << " of type " << ansaType
00144                 //     << " name " << ansaName << endl;
00145             }
00146         }
00147 
00148 
00149         // Hypermesh extension
00150         // $HMNAME COMP                   1"partName"
00151         if
00152         (
00153             line.substr(0, 12) == "$HMNAME COMP"
00154          && line.find ('"') != string::npos
00155         )
00156         {
00157             label groupId = readLabel
00158             (
00159                 IStringStream(line.substr(16, 16))()
00160             );
00161 
00162             IStringStream lineStream(line.substr(32));
00163 
00164             string rawName;
00165             lineStream >> rawName;
00166 
00167             groupToName.insert(groupId, string::validate<word>(rawName));
00168             Info<< "group " << groupId << " => " << rawName << endl;
00169         }
00170 
00171 
00172         if (line.empty() || line[0] == '$')
00173         {
00174             // Skip empty or comment
00175             continue;
00176         }
00177 
00178         // Check if character 72 is continuation
00179         if (line.size() > 72 && line[72] == '+')
00180         {
00181             line = line.substr(0, 72);
00182 
00183             while (true)
00184             {
00185                 string buf;
00186                 is.getLine(buf);
00187 
00188                 if (buf.size() > 72 && buf[72]=='+')
00189                 {
00190                     line += buf.substr(8, 64);
00191                 }
00192                 else
00193                 {
00194                     line += buf.substr(8, buf.size()-8);
00195                     break;
00196                 }
00197             }
00198         }
00199 
00200         // Read first word
00201         IStringStream lineStream(line);
00202         word cmd;
00203         lineStream >> cmd;
00204 
00205         if (cmd == "CTRIA3")
00206         {
00207             label groupId = readLabel(IStringStream(line.substr(16,8))());
00208             label a = readLabel(IStringStream(line.substr(24,8))());
00209             label b = readLabel(IStringStream(line.substr(32,8))());
00210             label c = readLabel(IStringStream(line.substr(40,8))());
00211 
00212 
00213             // Convert group into patch
00214             Map<label>::const_iterator iter = groupToPatch.find(groupId);
00215 
00216             label patchI;
00217             if (iter == groupToPatch.end())
00218             {
00219                 patchI = nPatches++;
00220                 groupToPatch.insert(groupId, patchI);
00221                 Info<< "patch " << patchI << " => group " << groupId << endl;
00222             }
00223             else
00224             {
00225                 patchI = iter();
00226             }
00227 
00228             faces.append(labelledTri(a, b, c, patchI));
00229         }
00230         else if (cmd == "CQUAD4")
00231         {
00232             label groupId = readLabel(IStringStream(line.substr(16,8))());
00233             label a = readLabel(IStringStream(line.substr(24,8))());
00234             label b = readLabel(IStringStream(line.substr(32,8))());
00235             label c = readLabel(IStringStream(line.substr(40,8))());
00236             label d = readLabel(IStringStream(line.substr(48,8))());
00237 
00238             // Convert group into patch
00239             Map<label>::const_iterator iter = groupToPatch.find(groupId);
00240 
00241             label patchI;
00242             if (iter == groupToPatch.end())
00243             {
00244                 patchI = nPatches++;
00245                 groupToPatch.insert(groupId, patchI);
00246                 Info<< "patch " << patchI << " => group " << groupId << endl;
00247             }
00248             else
00249             {
00250                 patchI = iter();
00251             }
00252 
00253             faces.append(labelledTri(a, b, c, patchI));
00254             faces.append(labelledTri(c, d, a, patchI));
00255         }
00256         else if (cmd == "PSHELL")
00257         {
00258             // Read shell type since group gives patchnames
00259             label groupId = readLabel(IStringStream(line.substr(8,8))());
00260             if (groupId == ansaId && ansaType == "PSHELL")
00261             {
00262                 groupToName.insert(groupId, string::validate<word>(ansaName));
00263                 Info<< "group " << groupId << " => " << ansaName << endl;
00264             }
00265         }
00266         else if (cmd == "GRID")
00267         {
00268             label index = readLabel(IStringStream(line.substr(8,8))());
00269             scalar x = parseNASCoord(line.substr(24, 8));
00270             scalar y = parseNASCoord(line.substr(32, 8));
00271             scalar z = parseNASCoord(line.substr(40, 8));
00272 
00273             indices.append(index);
00274             points.append(point(x, y, z));
00275         }
00276         else if (cmd == "GRID*")
00277         {
00278             // Long format is on two lines with '*' continuation symbol
00279             // on start of second line.
00280             // Typical line (spaces compacted)
00281             // GRID*      126   0 -5.55999875E+02 -5.68730474E+02
00282             // *         2.14897901E+02
00283 
00284             label index = readLabel(IStringStream(line.substr(8,16))());
00285             scalar x = parseNASCoord(line.substr(40, 16));
00286             scalar y = parseNASCoord(line.substr(56, 16));
00287 
00288             is.getLine(line);
00289             if (line[0] != '*')
00290             {
00291                 FatalErrorIn("triSurface::readNAS(const fileName&)")
00292                     << "Expected continuation symbol '*' when reading GRID*"
00293                     << " (double precision coordinate) output" << nl
00294                     << "Read:" << line << nl
00295                     << "File:" << is.name()
00296                     << " line:" << is.lineNumber()
00297                     << exit(FatalError);
00298             }
00299             scalar z = parseNASCoord(line.substr(8, 16));
00300 
00301             indices.append(index);
00302             points.append(point(x, y, z));
00303         }
00304         else if (unhandledCmd.insert(cmd))
00305         {
00306             Info<< "Unhandled Nastran command " << line << nl
00307                 << "File:" << is.name() << " line:" << is.lineNumber() << endl;
00308         }
00309     }
00310 
00311     points.shrink();
00312     indices.shrink();
00313     faces.shrink();
00314 
00315 
00316     Info<< "Read triangles:" << faces.size() << " points:" << points.size()
00317         << endl;
00318 
00319     {
00320         // Build inverse mapping (index to point)
00321         Map<label> indexToPoint(2*indices.size());
00322         forAll(indices, i)
00323         {
00324             indexToPoint.insert(indices[i], i);
00325         }
00326 
00327         // Relabel faces
00328         forAll(faces, i)
00329         {
00330             labelledTri& f = faces[i];
00331 
00332             f[0] = indexToPoint[f[0]];
00333             f[1] = indexToPoint[f[1]];
00334             f[2] = indexToPoint[f[2]];
00335         }
00336     }
00337 
00338 
00339     // Convert groupToPatch to patchList.
00340     geometricSurfacePatchList patches(nPatches);
00341 
00342     forAllConstIter(Map<word>, groupToName, iter)
00343     {
00344         label patchI = groupToPatch[iter.key()];
00345 
00346         patches[patchI] = geometricSurfacePatch
00347         (
00348             "empty",
00349             iter(),
00350             patchI
00351         );
00352     }
00353 
00354     Info<< "patches:" << patches << endl;
00355 
00356     // Transfer DynamicLists to straight ones.
00357     pointField allPoints(points.xfer());
00358 
00359     // Create triSurface
00360     *this = triSurface(faces, patches, allPoints, true);
00361 
00362     return true;
00363 }
00364 
00365 
00366 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00367 
00368 } // End namespace Foam
00369 
00370 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd