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

ReadFields.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 "ReadFields.H"
00028 #include "HashSet.H"
00029 #include "Pstream.H"
00030 #include "IOobjectList.H"
00031 
00032 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
00033 
00034 // Read all fields of type. Returns names of fields read. Guarantees all
00035 // processors to read fields in same order.
00036 template<class GeoField, class Mesh>
00037 Foam::wordList Foam::ReadFields
00038 (
00039     const Mesh& mesh,
00040     const IOobjectList& objects,
00041     PtrList<GeoField>& fields,
00042     const bool syncPar
00043 )
00044 {
00045     // Search list of objects for wanted type
00046     IOobjectList fieldObjects(objects.lookupClass(GeoField::typeName));
00047 
00048     wordList masterNames(fieldObjects.names());
00049 
00050     if (syncPar && Pstream::parRun())
00051     {
00052         // Check that I have the same fields as the master
00053         const wordList localNames(masterNames);
00054         Pstream::scatter(masterNames);
00055 
00056         HashSet<word> localNamesSet(localNames);
00057 
00058         forAll(masterNames, i)
00059         {
00060             const word& masterFld = masterNames[i];
00061 
00062             HashSet<word>::iterator iter = localNamesSet.find(masterFld);
00063 
00064             if (iter == localNamesSet.end())
00065             {
00066                 FatalErrorIn
00067                 (
00068                     "ReadFields<class GeoField, class Mesh>"
00069                     "(const Mesh&, const IOobjectList&, PtrList<GeoField>&"
00070                     ", const bool)"
00071                 )   << "Fields not synchronised across processors." << endl
00072                     << "Master has fields " << masterNames
00073                     << "  processor " << Pstream::myProcNo()
00074                     << " has fields " << localNames << exit(FatalError);
00075             }
00076             else
00077             {
00078                 localNamesSet.erase(iter);
00079             }
00080         }
00081 
00082         forAllConstIter(HashSet<word>, localNamesSet, iter)
00083         {
00084             FatalErrorIn
00085             (
00086                 "ReadFields<class GeoField, class Mesh>"
00087                 "(const Mesh&, const IOobjectList&, PtrList<GeoField>&"
00088                 ", const bool)"
00089             )   << "Fields not synchronised across processors." << endl
00090                 << "Master has fields " << masterNames
00091                 << "  processor " << Pstream::myProcNo()
00092                 << " has fields " << localNames << exit(FatalError);
00093         }
00094     }
00095 
00096 
00097     fields.setSize(masterNames.size());
00098 
00099     // Make sure to read in masterNames order.
00100 
00101     forAll(masterNames, i)
00102     {
00103         Info<< "Reading " << GeoField::typeName << ' ' << masterNames[i]
00104             << endl;
00105 
00106         const IOobject& io = *fieldObjects[masterNames[i]];
00107 
00108         fields.set
00109         (
00110             i,
00111             new GeoField
00112             (
00113                 IOobject
00114                 (
00115                     io.name(),
00116                     io.instance(),
00117                     io.local(),
00118                     io.db(),
00119                     IOobject::MUST_READ,
00120                     IOobject::AUTO_WRITE,
00121                     io.registerObject()
00122                 ),
00123                 mesh
00124             )
00125         );
00126     }
00127     return masterNames;    
00128 }
00129 
00130 
00131 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd