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

NASsurfaceFormat.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 "NASsurfaceFormat.H"
00028 #include "IFstream.H"
00029 #include "IStringStream.H"
00030 
00031 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00032 
00033 template<class Face>
00034 Foam::fileFormats::NASsurfaceFormat<Face>::NASsurfaceFormat
00035 (
00036     const fileName& filename
00037 )
00038 {
00039     read(filename);
00040 }
00041 
00042 
00043 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00044 
00045 template<class Face>
00046 bool Foam::fileFormats::NASsurfaceFormat<Face>::read
00047 (
00048     const fileName& filename
00049 )
00050 {
00051     const bool mustTriangulate = this->isTri();
00052     this->clear();
00053 
00054     IFstream is(filename);
00055     if (!is.good())
00056     {
00057         FatalErrorIn
00058         (
00059             "fileFormats::NASsurfaceFormat::read(const fileName&)"
00060         )
00061             << "Cannot read file " << filename
00062             << exit(FatalError);
00063     }
00064 
00065     // Nastran index of points
00066     DynamicList<label>  pointId;
00067     DynamicList<point>  dynPoints;
00068     DynamicList<Face>   dynFaces;
00069     DynamicList<label>  dynZones;
00070     DynamicList<label>  dynSizes;
00071     Map<label>          lookup;
00072 
00073     // assume the types are not intermixed
00074     // leave faces that didn't have a group in 0
00075     bool sorted = true;
00076     label zoneI = 0;
00077 
00078     // Name for face group
00079     Map<word> nameLookup;
00080 
00081     // Ansa tags. Denoted by $ANSA_NAME.
00082     // These will appear just before the first use of a type.
00083     // We read them and store the PSHELL types which are used to name
00084     // the zones.
00085     label ansaId = -1;
00086     word  ansaType, ansaName;
00087 
00088     // A single warning per unrecognized command
00089     HashSet<word> unhandledCmd;
00090 
00091     while (is.good())
00092     {
00093         string line;
00094         is.getLine(line);
00095 
00096         // Ansa extension
00097         if (line.substr(0, 10) == "$ANSA_NAME")
00098         {
00099             string::size_type sem0 = line.find (';', 0);
00100             string::size_type sem1 = line.find (';', sem0+1);
00101             string::size_type sem2 = line.find (';', sem1+1);
00102 
00103             if
00104             (
00105                 sem0 != string::npos
00106              && sem1 != string::npos
00107              && sem2 != string::npos
00108             )
00109             {
00110                 ansaId = readLabel
00111                 (
00112                     IStringStream(line.substr(sem0+1, sem1-sem0-1))()
00113                 );
00114                 ansaType = line.substr(sem1+1, sem2-sem1-1);
00115 
00116                 string rawName;
00117                 is.getLine(rawName);
00118                 if (rawName[rawName.size()-1] == '\r')
00119                 {
00120                     rawName = rawName.substr(1, rawName.size()-2);
00121                 }
00122                 else
00123                 {
00124                     rawName = rawName.substr(1, rawName.size()-1);
00125                 }
00126 
00127                 string::stripInvalid<word>(rawName);
00128                 ansaName = rawName;
00129 
00130                 // Info<< "ANSA tag for NastranID:" << ansaId
00131                 //     << " of type " << ansaType
00132                 //     << " name " << ansaName << endl;
00133             }
00134         }
00135 
00136 
00137         // Hypermesh extension
00138         // $HMNAME COMP                   1"partName"
00139         if
00140         (
00141             line.substr(0, 12) == "$HMNAME COMP"
00142          && line.find ('"') != string::npos
00143         )
00144         {
00145             label groupId = readLabel
00146             (
00147                 IStringStream(line.substr(16, 16))()
00148             );
00149 
00150             IStringStream lineStream(line.substr(32));
00151 
00152             string rawName;
00153             lineStream >> rawName;
00154             string::stripInvalid<word>(rawName);
00155 
00156             word groupName(rawName);
00157             nameLookup.insert(groupId, groupName);
00158 
00159             // Info<< "group " << groupId << " => " << groupName << endl;
00160         }
00161 
00162 
00163         // Skip empty or comment
00164         if (line.empty() || line[0] == '$')
00165         {
00166             continue;
00167         }
00168 
00169         // Check if character 72 is continuation
00170         if (line.size() > 72 && line[72] == '+')
00171         {
00172             line = line.substr(0, 72);
00173 
00174             while (true)
00175             {
00176                 string buf;
00177                 is.getLine(buf);
00178 
00179                 if (buf.size() > 72 && buf[72] == '+')
00180                 {
00181                     line += buf.substr(8, 64);
00182                 }
00183                 else
00184                 {
00185                     line += buf.substr(8, buf.size()-8);
00186                     break;
00187                 }
00188             }
00189         }
00190 
00191 
00192         // Read first word
00193         IStringStream lineStream(line);
00194         word cmd;
00195         lineStream >> cmd;
00196 
00197         if (cmd == "CTRIA3")
00198         {
00199             triFace fTri;
00200 
00201             label groupId = readLabel(IStringStream(line.substr(16,8))());
00202             fTri[0] = readLabel(IStringStream(line.substr(24,8))());
00203             fTri[1] = readLabel(IStringStream(line.substr(32,8))());
00204             fTri[2] = readLabel(IStringStream(line.substr(40,8))());
00205 
00206             // Convert groupID into zoneId
00207             Map<label>::const_iterator fnd = lookup.find(groupId);
00208             if (fnd != lookup.end())
00209             {
00210                 if (zoneI != fnd())
00211                 {
00212                     // pshell types are intermixed
00213                     sorted = false;
00214                 }
00215                 zoneI = fnd();
00216             }
00217             else
00218             {
00219                 zoneI = dynSizes.size();
00220                 lookup.insert(groupId, zoneI);
00221                 dynSizes.append(0);
00222                 // Info<< "zone" << zoneI << " => group " << groupId <<endl;
00223             }
00224 
00225             dynFaces.append(fTri);
00226             dynZones.append(zoneI);
00227             dynSizes[zoneI]++;
00228         }
00229         else if (cmd == "CQUAD4")
00230         {
00231             face fQuad(4);
00232             UList<label>& f = static_cast<UList<label>&>(fQuad);
00233 
00234             label groupId = readLabel(IStringStream(line.substr(16,8))());
00235             fQuad[0] = readLabel(IStringStream(line.substr(24,8))());
00236             fQuad[1] = readLabel(IStringStream(line.substr(32,8))());
00237             fQuad[2] = readLabel(IStringStream(line.substr(40,8))());
00238             fQuad[3] = readLabel(IStringStream(line.substr(48,8))());
00239 
00240             // Convert groupID into zoneId
00241             Map<label>::const_iterator fnd = lookup.find(groupId);
00242             if (fnd != lookup.end())
00243             {
00244                 if (zoneI != fnd())
00245                 {
00246                     // pshell types are intermixed
00247                     sorted = false;
00248                 }
00249                 zoneI = fnd();
00250             }
00251             else
00252             {
00253                 zoneI = dynSizes.size();
00254                 lookup.insert(groupId, zoneI);
00255                 dynSizes.append(0);
00256                 // Info<< "zone" << zoneI << " => group " << groupId <<endl;
00257             }
00258 
00259 
00260             if (mustTriangulate)
00261             {
00262                 dynFaces.append(triFace(f[0], f[1], f[2]));
00263                 dynFaces.append(triFace(f[0], f[2], f[3]));
00264                 dynZones.append(zoneI);
00265                 dynZones.append(zoneI);
00266                 dynSizes[zoneI] += 2;
00267             }
00268             else
00269             {
00270                 dynFaces.append(Face(f));
00271                 dynZones.append(zoneI);
00272                 dynSizes[zoneI]++;
00273             }
00274         }
00275         else if (cmd == "GRID")
00276         {
00277             label index = readLabel(IStringStream(line.substr(8,8))());
00278             scalar x = parseNASCoord(line.substr(24, 8));
00279             scalar y = parseNASCoord(line.substr(32, 8));
00280             scalar z = parseNASCoord(line.substr(40, 8));
00281 
00282             pointId.append(index);
00283             dynPoints.append(point(x, y, z));
00284         }
00285         else if (cmd == "GRID*")
00286         {
00287             // Long format is on two lines with '*' continuation symbol
00288             // on start of second line.
00289             // Typical line (spaces compacted)
00290             // GRID*      126   0 -5.55999875E+02 -5.68730474E+02
00291             // *         2.14897901E+02
00292 
00293             label index = readLabel(IStringStream(line.substr(8,16))());
00294             scalar x = parseNASCoord(line.substr(40, 16));
00295             scalar y = parseNASCoord(line.substr(56, 16));
00296 
00297             is.getLine(line);
00298             if (line[0] != '*')
00299             {
00300                 FatalErrorIn
00301                 (
00302                     "fileFormats::NASsurfaceFormat::read(const fileName&)"
00303                 )
00304                     << "Expected continuation symbol '*' when reading GRID*"
00305                     << " (double precision coordinate) format" << nl
00306                     << "Read:" << line << nl
00307                     << "File:" << is.name() << " line:" << is.lineNumber()
00308                     << exit(FatalError);
00309             }
00310             scalar z = parseNASCoord(line.substr(8, 16));
00311 
00312             pointId.append(index);
00313             dynPoints.append(point(x, y, z));
00314         }
00315         else if (cmd == "PSHELL")
00316         {
00317             // pshell type for zone names with the Ansa extension
00318             label groupId = readLabel(IStringStream(line.substr(8,8))());
00319 
00320             if (groupId == ansaId && ansaType == "PSHELL")
00321             {
00322                 nameLookup.insert(ansaId, ansaName);
00323                 // Info<< "group " << groupId << " => " << ansaName << endl;
00324             }
00325         }
00326         else if (unhandledCmd.insert(cmd))
00327         {
00328             Info<< "Unhandled Nastran command " << line << nl
00329                 << "File:" << is.name() << " line:" << is.lineNumber()
00330                 << endl;
00331         }
00332     }
00333 
00334     //    Info<< "Read faces:" << dynFaces.size()
00335     //        << " points:" << dynPoints.size()
00336     //        << endl;
00337 
00338     // transfer to normal lists
00339     this->storedPoints().transfer(dynPoints);
00340 
00341     pointId.shrink();
00342     dynFaces.shrink();
00343 
00344     // Build inverse mapping (NASTRAN pointId -> index)
00345     Map<label> mapPointId(2*pointId.size());
00346     forAll(pointId, i)
00347     {
00348         mapPointId.insert(pointId[i], i);
00349     }
00350 
00351     // Relabel faces
00352     // ~~~~~~~~~~~~~
00353     forAll(dynFaces, i)
00354     {
00355         Face& f = dynFaces[i];
00356         forAll(f, fp)
00357         {
00358             f[fp] = mapPointId[f[fp]];
00359         }
00360     }
00361     pointId.clearStorage();
00362     mapPointId.clear();
00363 
00364 
00365     // create default zone names, or from ANSA/Hypermesh information
00366     List<word> names(dynSizes.size());
00367     forAllConstIter(Map<label>, lookup, iter)
00368     {
00369         const label zoneI  = iter();
00370         const label groupI = iter.key();
00371 
00372         Map<word>::const_iterator fnd = nameLookup.find(groupI);
00373         if (fnd != nameLookup.end())
00374         {
00375             names[zoneI] = fnd();
00376         }
00377         else
00378         {
00379             names[zoneI] = word("zone") + ::Foam::name(zoneI);
00380         }
00381     }
00382 
00383     sortFacesAndStore(dynFaces.xfer(), dynZones.xfer(), sorted);
00384 
00385     // add zones, culling empty ones
00386     this->addZones(dynSizes, names, true);
00387 
00388     return true;
00389 }
00390 
00391 
00392 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd