openfoam/src/postProcessing/functionObjects/field/fieldValues/cellSource/cellSource.C
2009-10-29 10:56:48 +00:00

258 lines
6.7 KiB
C

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2009-2009 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\*---------------------------------------------------------------------------*/
#include "cellSource.H"
#include "fvMesh.H"
#include "volFields.H"
#include "IOList.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace fieldValues
{
defineTypeNameAndDebug(cellSource, 0);
}
template<>
const char* NamedEnum<fieldValues::cellSource::sourceType, 1>::
names[] = {"cellZone"};
const NamedEnum<fieldValues::cellSource::sourceType, 1>
fieldValues::cellSource::sourceTypeNames_;
template<>
const char* NamedEnum<fieldValues::cellSource::operationType, 4>::
names[] = {"none", "sum", "volAverage", "volIntegrate"};
const NamedEnum<fieldValues::cellSource::operationType, 4>
fieldValues::cellSource::operationTypeNames_;
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::fieldValues::cellSource::setCellZoneCells()
{
label zoneId = mesh().cellZones().findZoneID(sourceName_);
if (zoneId < 0)
{
FatalErrorIn("cellSource::cellSource::setCellZoneCells()")
<< "Unknown cell zone name: " << sourceName_
<< ". Valid cell zones are: " << mesh().cellZones().names()
<< nl << exit(FatalError);
}
const cellZone& cZone = mesh().cellZones()[zoneId];
cellId_.setSize(cZone.size());
label count = 0;
forAll(cZone, i)
{
label cellI = cZone[i];
cellId_[count] = cellI;
count++;
}
cellId_.setSize(count);
if (debug)
{
Info<< "Original cell zone size = " << cZone.size()
<< ", new size = " << count << endl;
}
}
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
void Foam::fieldValues::cellSource::initialise()
{
switch (source_)
{
case stCellZone:
{
setCellZoneCells();
break;
}
default:
{
FatalErrorIn("cellSource::constructCellAddressing()")
<< "Unknown source type. Valid source types are:"
<< sourceTypeNames_ << nl << exit(FatalError);
}
}
Info<< type() << " " << name_ << ":" << nl
<< " total cells = " << cellId_.size() << nl
<< " total volume = " << sum(filterField(mesh().V()))
<< nl << endl;
}
void Foam::fieldValues::cellSource::makeFile()
{
// Create the forces file if not already created
if (outputFilePtr_.empty())
{
if (debug)
{
Info<< "Creating output file." << endl;
}
// File update
if (Pstream::master())
{
fileName outputDir;
if (Pstream::parRun())
{
// Put in undecomposed case (Note: gives problems for
// distributed data running)
outputDir =
obr_.time().path()/".."/name_/obr_.time().timeName();
}
else
{
outputDir = obr_.time().path()/name_/obr_.time().timeName();
}
// Create directory if does not exist
mkDir(outputDir);
// Open new file at start up
outputFilePtr_.reset(new OFstream(outputDir/(type() + ".dat")));
// Add headers to output data
writeFileHeader();
}
}
}
void Foam::fieldValues::cellSource::writeFileHeader()
{
if (outputFilePtr_.valid())
{
outputFilePtr_()
<< "# Source : " << sourceTypeNames_[source_] << " "
<< sourceName_ << nl << "# Cells : " << cellId_.size() << nl
<< "# Time" << tab << "sum(V)";
forAll(fields_, i)
{
outputFilePtr_()
<< tab << operationTypeNames_[operation_]
<< "(" << fields_[i] << ")";
}
outputFilePtr_() << endl;
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::fieldValues::cellSource::cellSource
(
const word& name,
const objectRegistry& obr,
const dictionary& dict,
const bool loadFromFiles
)
:
fieldValue(name, obr, dict, loadFromFiles),
source_(sourceTypeNames_.read(dict.lookup("source"))),
operation_(operationTypeNames_.read(dict.lookup("operation"))),
cellId_(),
outputFilePtr_(NULL)
{
initialise();
if (active_)
{
// Create the output file if not already created
makeFile();
}
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::fieldValues::cellSource::~cellSource()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::fieldValues::cellSource::read(const dictionary& dict)
{
if (active_)
{
fieldValue::read(dict);
initialise();
}
}
void Foam::fieldValues::cellSource::write()
{
if (active_)
{
if (log_)
{
Info<< type() << " " << name_ << " output:" << nl;
}
outputFilePtr_()
<< obr_.time().value() << tab
<< sum(filterField(mesh().V()));
forAll(fields_, i)
{
writeValues<scalar>(fields_[i]);
writeValues<vector>(fields_[i]);
writeValues<sphericalTensor>(fields_[i]);
writeValues<symmTensor>(fields_[i]);
writeValues<tensor>(fields_[i]);
}
outputFilePtr_()<< endl;
if (log_)
{
Info<< endl;
}
}
}
// ************************************************************************* //