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

PstreamImpl.C

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------*\
00002                 ______                _     ____          __  __
00003                |  ____|             _| |_  / __ \   /\   |  \/  |
00004                | |__ _ __ ___  ___ /     \| |  | | /  \  | \  / |
00005                |  __| '__/ _ \/ _ ( (| |) ) |  | |/ /\ \ | |\/| |
00006                | |  | | |  __/  __/\_   _/| |__| / ____ \| |  | |
00007                |_|  |_|  \___|\___|  |_|   \____/_/    \_\_|  |_|
00008 
00009                     FreeFOAM: The Cross-Platform CFD Toolkit
00010 
00011   Copyright (C) 2008-2012 Michael Wild <themiwi@users.sf.net>
00012                           Gerber van der Graaf <gerber_graaf@users.sf.net>
00013 --------------------------------------------------------------------------------
00014 License
00015     This file is part of FreeFOAM.
00016 
00017     FreeFOAM is free software: you can redistribute it and/or modify it
00018     under the terms of the GNU General Public License as published by the
00019     Free Software Foundation, either version 3 of the License, or (at your
00020     option) any later version.
00021 
00022     FreeFOAM is distributed in the hope that it will be useful, but WITHOUT
00023     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00024     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00025     for more details.
00026 
00027     You should have received a copy of the GNU General Public License
00028     along with FreeFOAM.  If not, see <http://www.gnu.org/licenses/>.
00029 
00030 \*----------------------------------------------------------------------------*/
00031 
00032 #include <OpenFOAM/PstreamImpl.H>
00033 #include <OpenFOAM/Pstream.H>
00034 #include <OpenFOAM/debug.H>
00035 #include <OpenFOAM/OSspecific.H>
00036 #include <OpenFOAM/dictionary.H>
00037 #include <OpenFOAM/dlLibraryTable.H>
00038 
00039 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00040 
00041 defineTypeNameAndDebug(Foam::PstreamImpl, 0);
00042 defineRunTimeSelectionTable(Foam::PstreamImpl, dictionary);
00043 
00044 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00045 
00046 Foam::autoPtr<Foam::PstreamImpl> Foam::PstreamImpl::instance_;
00047 
00048 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00049 
00050 void Foam::PstreamImpl::setParRun(bool& isParallel)
00051 {
00052     isParallel = true;
00053 
00054     Pout.prefix() = '[' +  name(Pstream::myProcNo()) + "] ";
00055     Perr.prefix() = '[' +  name(Pstream::myProcNo()) + "] ";
00056 }
00057 
00058 
00059 void Foam::PstreamImpl::initCommunicationSchedule()
00060 {
00061     Pstream::initCommunicationSchedule();
00062 }
00063 
00064 // * * * * * * * * * * * * * Local Helper Functions  * * * * * * * * * * * * //
00065 
00066 // helper function to find the config section name
00067 Foam::word Foam::PstreamConfigSectionName()
00068 {
00069     // check FREEFOAM_PSTREAM_CONFIG
00070     if(env("FREEFOAM_PSTREAM_CONFIG"))
00071     {
00072         return getEnv("FREEFOAM_PSTREAM_CONFIG");
00073     }
00074     else if(debug::controlDict().found("PstreamImplementation"))
00075     {
00076         // check the global controlDict::PstreamImplementation::configName
00077         word configName;
00078         debug::controlDict().subDict("PstreamImplementation").lookup("configName") >> configName;
00079         return configName;
00080     }
00081     else
00082     {
00083         // otherwise return empty
00084         return word();
00085     }
00086 }
00087 
00088 // * * * * * * * * * * * * * Public Member Functions * * * * * * * * * * * * //
00089 
00090 Foam::autoPtr<Foam::PstreamImpl> Foam::PstreamImpl::New()
00091 {
00092     if(!instance_.valid())
00093     {
00094         // load the Pstream library the user wants to use
00095         loadPstreamLibrary();
00096         // find the Pstream type the user wants to use
00097         instance_ =  loadPstreamInstance<PstreamImpl>
00098         (
00099             "Pstream",
00100             "FREEFOAM_PSTREAM_CLASS",
00101             dictionaryConstructorTablePtr_
00102         );
00103     }
00104     return instance_;
00105 }
00106 
00107 void Foam::PstreamImpl::loadPstreamLibrary()
00108 {
00109     static bool didLoad = false;
00110     if(!didLoad)
00111     {
00112         fileName PstreamLibName;
00113         // check whether FREEFOAM_PSTREAM_LIBRARY is set
00114         if(env("FREEFOAM_PSTREAM_LIBRARY"))
00115         {
00116             PstreamLibName = getEnv("FREEFOAM_PSTREAM_LIBRARY");
00117         }
00118         else
00119         {
00120             // otherwise check the global controlDict
00121             word configName = PstreamConfigSectionName();
00122             if
00123             (
00124                  !configName.empty() &&
00125                  debug::controlDict().subDict("PstreamImplementation")
00126                      .subDict(configName).found("library")
00127             )
00128             {
00129                 debug::controlDict().subDict("PstreamImplementation")
00130                     .subDict(configName).lookup("library") >> PstreamLibName;
00131             }
00132         }
00133 
00134         // if we found some Pstream library, load it, otherwise assume it is
00135         // not necessary
00136         if(!PstreamLibName.empty())
00137         {
00138             if(PstreamImpl::debug)
00139             {
00140                 Info<< "Trying to load Pstream library '" << PstreamLibName << "'" << endl;
00141             }
00142 
00143             if(!dlLibraryTable::open(PstreamLibName))
00144             {
00145                 FatalErrorIn
00146                 (
00147                      "PstreamImpl::loadPstreamLibrary()"
00148                 ) << "Failed to load the library '" << PstreamLibName << "'" << endl
00149                   << Foam::exit(FatalError);
00150             }
00151         }
00152         else
00153         {
00154             if(PstreamImpl::debug)
00155             {
00156                 WarningIn
00157                 (
00158                      "PstreamImpl::loadPstreamLibrary()"
00159                 ) << "No Pstream library specified to load" << endl;
00160             }
00161         }
00162         didLoad = true;
00163     }
00164 }
00165 
00166 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines