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

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