included subtract method

This commit is contained in:
andy
2009-02-10 18:38:01 +00:00
parent 5235d41622
commit 9eaaff14c4
5 changed files with 153 additions and 99 deletions

View File

@ -0,0 +1,306 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-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 "addSubtract.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace calcTypes
{
defineTypeNameAndDebug(addSubtract, 0);
addToRunTimeSelectionTable(calcType, addSubtract, dictionary);
}
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::calcTypes::addSubtract::writeAddSubtractFields
(
const Time& runTime,
const fvMesh& mesh,
const IOobject& baseFieldHeader
)
{
bool processed = false;
IOobject addSubtractFieldHeader
(
addSubtractFieldName_,
runTime.timeName(),
mesh,
IOobject::MUST_READ
);
if (addSubtractFieldHeader.headerOk())
{
writeAddSubtractField<scalar>
(
baseFieldHeader,
addSubtractFieldHeader,
mesh,
processed
);
writeAddSubtractField<vector>
(
baseFieldHeader,
addSubtractFieldHeader,
mesh,
processed
);
writeAddSubtractField<sphericalTensor>
(
baseFieldHeader,
addSubtractFieldHeader,
mesh,
processed
);
writeAddSubtractField<symmTensor>
(
baseFieldHeader,
addSubtractFieldHeader,
mesh,
processed
);
writeAddSubtractField<tensor>
(
baseFieldHeader,
addSubtractFieldHeader,
mesh,
processed
);
if (!processed)
{
FatalError
<< "Unable to process " << baseFieldName_
<< " + " << addSubtractFieldName_ << nl
<< "No call to addSubtract for fields of type "
<< baseFieldHeader.headerClassName() << " + "
<< addSubtractFieldHeader.headerClassName() << nl << nl
<< exit(FatalError);
}
}
else
{
FatalErrorIn("calcTypes::addSubtract::writeAddSubtractFields()")
<< "Unable to read addSubtract field: " << addSubtractFieldName_
<< nl << exit(FatalError);
}
}
void Foam::calcTypes::addSubtract::writeAddSubtractValues
(
const Time& runTime,
const fvMesh& mesh,
const IOobject& baseFieldHeader
)
{
bool processed = false;
writeAddSubtractValue<scalar>
(
baseFieldHeader,
addSubtractValueStr_,
mesh,
processed
);
writeAddSubtractValue<vector>
(
baseFieldHeader,
addSubtractValueStr_,
mesh,
processed
);
writeAddSubtractValue<sphericalTensor>
(
baseFieldHeader,
addSubtractValueStr_,
mesh,
processed
);
writeAddSubtractValue<symmTensor>
(
baseFieldHeader,
addSubtractValueStr_,
mesh,
processed
);
writeAddSubtractValue<tensor>
(
baseFieldHeader,
addSubtractValueStr_,
mesh,
processed
);
if (!processed)
{
FatalErrorIn("calcTypes::addSubtract::writeAddSubtractValue()")
<< "Unable to process " << baseFieldName_
<< " + " << addSubtractValueStr_ << nl
<< "No call to addSubtract for fields of type "
<< baseFieldHeader.headerClassName() << nl << nl
<< exit(FatalError);
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::calcTypes::addSubtract::addSubtract()
:
calcType(),
baseFieldName_(""),
calcType_(FIELD),
addSubtractFieldName_(""),
addSubtractValueStr_(""),
resultName_(""),
calcMode_(ADD)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::calcTypes::addSubtract::~addSubtract()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::calcTypes::addSubtract::init()
{
argList::validArgs.append("add");
argList::validArgs.append("baseField");
argList::validArgs.append("calcMode");
argList::validOptions.insert("field", "fieldName");
argList::validOptions.insert("value", "valueString");
argList::validOptions.insert("resultName", "fieldName");
}
void Foam::calcTypes::addSubtract::preCalc
(
const argList& args,
const Time& runTime,
const fvMesh& mesh
)
{
baseFieldName_ = args.additionalArgs()[1];
word calcModeName = args.additionalArgs()[2];
if (calcModeName == "add")
{
calcMode_ = ADD;
}
else if (calcModeName == "subtract")
{
calcMode_ = SUBTRACT;
}
else
{
FatalErrorIn("calcTypes::addSubtract::preCalc")
<< "Invalid calcMode: " << calcModeName << nl
<< " Valid calcModes are add and subtract" << nl
<< exit(FatalError);
}
if (args.options().found("field"))
{
addSubtractFieldName_ = args.options()["field"];
calcType_ = FIELD;
}
else if (args.options().found("value"))
{
addSubtractValueStr_ = args.options()["value"];
calcType_ = VALUE;
}
else
{
FatalErrorIn("calcTypes::addSubtract::preCalc")
<< "addSubtract requires either -field or -value option"
<< nl << exit(FatalError);
}
if (args.options().found("resultName"))
{
resultName_ = args.options()["resultName"];
}
}
void Foam::calcTypes::addSubtract::calc
(
const argList& args,
const Time& runTime,
const fvMesh& mesh
)
{
IOobject baseFieldHeader
(
baseFieldName_,
runTime.timeName(),
mesh,
IOobject::MUST_READ
);
if (baseFieldHeader.headerOk())
{
switch (calcType_)
{
case FIELD:
{
writeAddSubtractFields(runTime, mesh, baseFieldHeader);
break;
}
case VALUE:
{
writeAddSubtractValues(runTime, mesh, baseFieldHeader);
break;
}
default:
{
FatalErrorIn("calcTypes::addSubtract::calc")
<< "unknown calcType " << calcType_ << nl
<< abort(FatalError);
}
}
}
else
{
FatalErrorIn("calcTypes::addSubtract::calc")
<< "Unable to read base field: " << baseFieldName_
<< nl << exit(FatalError);
}
}
// ************************************************************************* //

View File

@ -0,0 +1,219 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-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
Class
Foam::calcTypes::addSubtract
Description
adds/subtracts a field or value to/from a base field.
New field name specified by -resultName option, or automatically as:
<baseFieldName>_add_<addSubtractFieldName>
<baseFieldName>_add_value
<baseFieldName>_subtract_<addSubtractFieldName>
<baseFieldName>_subtract_value
Example usage:
addSubtract p add -value 100000 -resultName pAbs
addSubtract U subtract -field U0
SourceFiles
addSubtract.C
writeaddSubtractField.C
writeaddSubtractValue.C
\*---------------------------------------------------------------------------*/
#ifndef addSubtract_H
#define addSubtract_H
#include "calcType.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace calcTypes
{
/*---------------------------------------------------------------------------*\
Class addSubtract Declaration
\*---------------------------------------------------------------------------*/
class addSubtract
:
public calcType
{
public:
enum calcTypes
{
FIELD,
VALUE
};
enum calcModes
{
ADD,
SUBTRACT
};
private:
// Private data
//- Name of base field (to addSubtract to)
word baseFieldName_;
//- Calc type as given by enumerations above
calcTypes calcType_;
//- Name of field to add/subtract
word addSubtractFieldName_;
//- String representation of value to add/subtract
string addSubtractValueStr_;
//- Name of result field
word resultName_;
//- Mode - addSubtract/subtract
calcModes calcMode_;
// Private Member Functions
// Output
//- Calc and output field addSubtractitions
void writeAddSubtractFields
(
const Time& runTime,
const fvMesh& mesh,
const IOobject& baseFieldHeader
);
//- Calc and output field and value addSubtractitions
void writeAddSubtractValues
(
const Time& runTime,
const fvMesh& mesh,
const IOobject& baseFieldHeader
);
//- Disallow default bitwise copy construct
addSubtract(const addSubtract&);
//- Disallow default bitwise assignment
void operator=(const addSubtract&);
protected:
// Member Functions
// Calculation routines
//- Initialise - typically setting static variables,
// e.g. command line arguments
virtual void init();
//- Pre-time loop calculations
virtual void preCalc
(
const argList& args,
const Time& runTime,
const fvMesh& mesh
);
//- Time loop calculations
virtual void calc
(
const argList& args,
const Time& runTime,
const fvMesh& mesh
);
// I-O
//- Write addSubtract field
template<class Type>
void writeAddSubtractField
(
const IOobject& baseHeader,
const IOobject& addSubtractHeader,
const fvMesh& mesh,
bool& processed
);
//- Write addSubtract value
template<class Type>
void writeAddSubtractValue
(
const IOobject& baseHeader,
const string& valueStr,
const fvMesh& mesh,
bool& processed
);
public:
//- Runtime type information
TypeName("addSubtract");
// Constructors
//- Construct null
addSubtract();
// Destructor
virtual ~addSubtract();
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace calcTypes
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
# include "writeAddSubtractField.C"
# include "writeAddSubtractValue.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,93 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-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
\*---------------------------------------------------------------------------*/
template<class Type>
void Foam::calcTypes::addSubtract::writeAddSubtractField
(
const IOobject& baseHeader,
const IOobject& addHeader,
const fvMesh& mesh,
bool& processed
)
{
typedef GeometricField<Type, fvPatchField, volMesh> fieldType;
if
(
baseHeader.headerClassName() == fieldType::typeName
&& baseHeader.headerClassName() == addHeader.headerClassName()
)
{
if (resultName_ == "")
{
if (calcMode_ == ADD)
{
resultName_ = baseHeader.name() + "_add_" + addHeader.name();
}
else
{
resultName_ = baseHeader.name() + "_subtract_"
+ addHeader.name();
}
}
Info<< " Reading " << baseHeader.name() << endl;
fieldType baseField(baseHeader, mesh);
Info<< " Reading " << addHeader.name() << endl;
fieldType addField(addHeader, mesh);
if (baseField.dimensions() == addField.dimensions())
{
Info<< " Calculating " << resultName_ << endl;
fieldType newField
(
IOobject
(
resultName_,
mesh.time().timeName(),
mesh,
IOobject::NO_READ
),
calcMode_ == ADD ? baseField + addField : baseField - addField
);
newField.write();
}
else
{
Info<< " Cannot calculate " << resultName_ << nl
<< " - inconsistent dimensions: "
<< baseField.dimensions() << " - " << addField.dimensions()
<< endl;
}
processed = true;
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -0,0 +1,90 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-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
\*---------------------------------------------------------------------------*/
template<class Type>
void Foam::calcTypes::addSubtract::writeAddSubtractValue
(
const IOobject& baseHeader,
const string& valueStr,
const fvMesh& mesh,
bool& processed
)
{
typedef GeometricField<Type, fvPatchField, volMesh> fieldType;
if (baseHeader.headerClassName() == fieldType::typeName)
{
if (resultName_ == "")
{
if (calcMode_ == ADD)
{
resultName_ = baseHeader.name() + "_add_value";
}
else
{
resultName_ = baseHeader.name() + "_subtract_value";
}
}
Type value;
IStringStream(valueStr)() >> value;
Info<< " Reading " << baseHeader.name() << endl;
fieldType baseField(baseHeader, mesh);
fieldType newField
(
IOobject
(
resultName_,
mesh.time().timeName(),
mesh,
IOobject::NO_READ
),
baseField
);
Info<< " Calculating " << resultName_ << endl;
if (calcMode_ == ADD)
{
newField == baseField
+ dimensioned<Type>("value", baseField.dimensions(), value);
}
else
{
newField == baseField
- dimensioned<Type>("value", baseField.dimensions(), value);
}
newField.write();
processed = true;
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //