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

JobInfo.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 "JobInfo.H"
00027 #include <OpenFOAM/OSspecific.H>
00028 #include <OpenFOAM/clock.H>
00029 #include <OpenFOAM/OFstream.H>
00030 #include <OpenFOAM/Pstream.H>
00031 
00032 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00033 
00034 bool Foam::JobInfo::writeJobInfo(Foam::debug::infoSwitch("writeJobInfo", 0));
00035 Foam::JobInfo Foam::jobInfo;
00036 
00037 
00038 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00039 
00040 // Null constructor
00041 Foam::JobInfo::JobInfo()
00042 :
00043     runningJobPath_(),
00044     finishedJobPath_(),
00045     cpuTime_()
00046 {
00047     name() = "JobInfo";
00048 
00049     if (writeJobInfo && Pstream::master())
00050     {
00051         string baseDir;
00052         if(env("FREEFOAM_JOB_DIR"))
00053         {
00054             baseDir = getEnv("FREEFOAM_JOB_DIR");
00055         }
00056         else if(env("FOAM_JOB_DIR"))
00057         {
00058             baseDir = getEnv("FOAM_JOB_DIR");
00059         }
00060         else if(debug::controlDict().found("foamJobDir"))
00061         {
00062             debug::controlDict().lookup("foamJobDir") >> baseDir;
00063         }
00064         else
00065         {
00066             baseDir = home()/".FreeFOAM"/"jobControl";
00067         }
00068 
00069         string jobFile = hostName() + '.' + Foam::name(pid());
00070 
00071         fileName runningDir(baseDir/"runningJobs");
00072         fileName finishedDir(baseDir/"finishedJobs");
00073 
00074         runningJobPath_  = runningDir/jobFile;
00075         finishedJobPath_ = finishedDir/jobFile;
00076 
00077         if (baseDir.empty())
00078         {
00079             FatalErrorIn("JobInfo::JobInfo()")
00080                 << "Cannot get JobInfo directory $FOAM_JOB_DIR"
00081                 << Foam::exit(FatalError);
00082         }
00083 
00084         if (!isDir(runningDir) && !mkDir(runningDir))
00085         {
00086             FatalErrorIn("JobInfo::JobInfo()")
00087                 << "Cannot make JobInfo directory " << runningDir
00088                 << Foam::exit(FatalError);
00089         }
00090 
00091         if (!isDir(finishedDir) && !mkDir(finishedDir))
00092         {
00093             FatalErrorIn("JobInfo::JobInfo()")
00094                 << "Cannot make JobInfo directory " << finishedDir
00095                 << Foam::exit(FatalError);
00096         }
00097     }
00098 
00099     constructed = true;
00100 }
00101 
00102 
00103 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00104 
00105 Foam::JobInfo::~JobInfo()
00106 {
00107     if (writeJobInfo && constructed && Pstream::master())
00108     {
00109         mv(runningJobPath_, finishedJobPath_);
00110     }
00111 
00112     constructed = false;
00113 }
00114 
00115 
00116 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00117 
00118 bool Foam::JobInfo::write(Ostream& os) const
00119 {
00120     if (writeJobInfo && Pstream::master())
00121     {
00122         if (os.good())
00123         {
00124             dictionary::write(os, false);
00125             return true;
00126         }
00127         else
00128         {
00129             return false;
00130         }
00131     }
00132     else
00133     {
00134         return true;
00135     }
00136 }
00137 
00138 
00139 void Foam::JobInfo::write() const
00140 {
00141     if (writeJobInfo && Pstream::master())
00142     {
00143         if (!write(OFstream(runningJobPath_)()))
00144         {
00145             FatalErrorIn("JobInfo::write() const")
00146                 << "Failed to write to JobInfo file "
00147                 << runningJobPath_
00148                 << Foam::exit(FatalError);
00149         }
00150     }
00151 }
00152 
00153 
00154 void Foam::JobInfo::end(const word& terminationType)
00155 {
00156     if (writeJobInfo && constructed && Pstream::master())
00157     {
00158         add("cpuTime", cpuTime_.elapsedCpuTime());
00159         add("endDate", clock::date());
00160         add("endTime", clock::clockTime());
00161 
00162         if (!found("termination"))
00163         {
00164             add("termination", terminationType);
00165         }
00166 
00167         rm(runningJobPath_);
00168         write(OFstream(finishedJobPath_)());
00169     }
00170 
00171     constructed = false;
00172 }
00173 
00174 
00175 void Foam::JobInfo::end()
00176 {
00177     end("normal");
00178 }
00179 
00180 
00181 void Foam::JobInfo::exit()
00182 {
00183     end("exit");
00184 }
00185 
00186 
00187 void Foam::JobInfo::abort()
00188 {
00189     end("abort");
00190 }
00191 
00192 
00193 void Foam::JobInfo::signalEnd() const
00194 {
00195     if (writeJobInfo && constructed && Pstream::master())
00196     {
00197         mv(runningJobPath_, finishedJobPath_);
00198     }
00199 
00200     constructed = false;
00201 }
00202 
00203 
00204 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines