Commit 9eaaff14 authored by Andrew Heather's avatar Andrew Heather
Browse files

included subtract method

parent 5235d416
......@@ -9,6 +9,6 @@ field/div/div.C
field/randomise/randomise.C
field/interpolate/interpolate.C
basic/add/add.C
basic/addSubtract/addSubtract.C
LIB = $(FOAM_LIBBIN)/libfoamCalcFunctions
......@@ -24,7 +24,7 @@ License
\*---------------------------------------------------------------------------*/
#include "add.H"
#include "addSubtract.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
......@@ -33,15 +33,15 @@ namespace Foam
{
namespace calcTypes
{
defineTypeNameAndDebug(add, 0);
addToRunTimeSelectionTable(calcType, add, dictionary);
defineTypeNameAndDebug(addSubtract, 0);
addToRunTimeSelectionTable(calcType, addSubtract, dictionary);
}
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::calcTypes::add::writeAddFields
void Foam::calcTypes::addSubtract::writeAddSubtractFields
(
const Time& runTime,
const fvMesh& mesh,
......@@ -50,48 +50,48 @@ void Foam::calcTypes::add::writeAddFields
{
bool processed = false;
IOobject addFieldHeader
IOobject addSubtractFieldHeader
(
addFieldName_,
addSubtractFieldName_,
runTime.timeName(),
mesh,
IOobject::MUST_READ
);
if (addFieldHeader.headerOk())
if (addSubtractFieldHeader.headerOk())
{
writeAddField<scalar>
writeAddSubtractField<scalar>
(
baseFieldHeader,
addFieldHeader,
addSubtractFieldHeader,
mesh,
processed
);
writeAddField<vector>
writeAddSubtractField<vector>
(
baseFieldHeader,
addFieldHeader,
addSubtractFieldHeader,
mesh,
processed
);
writeAddField<sphericalTensor>
writeAddSubtractField<sphericalTensor>
(
baseFieldHeader,
addFieldHeader,
addSubtractFieldHeader,
mesh,
processed
);
writeAddField<symmTensor>
writeAddSubtractField<symmTensor>
(
baseFieldHeader,
addFieldHeader,
addSubtractFieldHeader,
mesh,
processed
);
writeAddField<tensor>
writeAddSubtractField<tensor>
(
baseFieldHeader,
addFieldHeader,
addSubtractFieldHeader,
mesh,
processed
);
......@@ -100,23 +100,23 @@ void Foam::calcTypes::add::writeAddFields
{
FatalError
<< "Unable to process " << baseFieldName_
<< " + " << addFieldName_ << nl
<< "No call to add for fields of type "
<< " + " << addSubtractFieldName_ << nl
<< "No call to addSubtract for fields of type "
<< baseFieldHeader.headerClassName() << " + "
<< addFieldHeader.headerClassName() << nl << nl
<< addSubtractFieldHeader.headerClassName() << nl << nl
<< exit(FatalError);
}
}
else
{
FatalErrorIn("calcTypes::add::writeAddFields()")
<< "Unable to read add field: " << addFieldName_
FatalErrorIn("calcTypes::addSubtract::writeAddSubtractFields()")
<< "Unable to read addSubtract field: " << addSubtractFieldName_
<< nl << exit(FatalError);
}
}
void Foam::calcTypes::add::writeAddValues
void Foam::calcTypes::addSubtract::writeAddSubtractValues
(
const Time& runTime,
const fvMesh& mesh,
......@@ -125,48 +125,48 @@ void Foam::calcTypes::add::writeAddValues
{
bool processed = false;
writeAddValue<scalar>
writeAddSubtractValue<scalar>
(
baseFieldHeader,
addValueStr_,
addSubtractValueStr_,
mesh,
processed
);
writeAddValue<vector>
writeAddSubtractValue<vector>
(
baseFieldHeader,
addValueStr_,
addSubtractValueStr_,
mesh,
processed
);
writeAddValue<sphericalTensor>
writeAddSubtractValue<sphericalTensor>
(
baseFieldHeader,
addValueStr_,
addSubtractValueStr_,
mesh,
processed
);
writeAddValue<symmTensor>
writeAddSubtractValue<symmTensor>
(
baseFieldHeader,
addValueStr_,
addSubtractValueStr_,
mesh,
processed
);
writeAddValue<tensor>
writeAddSubtractValue<tensor>
(
baseFieldHeader,
addValueStr_,
addSubtractValueStr_,
mesh,
processed
);
if (!processed)
{
FatalErrorIn("calcTypes::add::writeAddValues()")
FatalErrorIn("calcTypes::addSubtract::writeAddSubtractValue()")
<< "Unable to process " << baseFieldName_
<< " + " << addValueStr_ << nl
<< "No call to add for fields of type "
<< " + " << addSubtractValueStr_ << nl
<< "No call to addSubtract for fields of type "
<< baseFieldHeader.headerClassName() << nl << nl
<< exit(FatalError);
}
......@@ -175,36 +175,38 @@ void Foam::calcTypes::add::writeAddValues
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::calcTypes::add::add()
Foam::calcTypes::addSubtract::addSubtract()
:
calcType(),
baseFieldName_(""),
calcType_(FIELD),
addFieldName_(""),
addValueStr_(""),
resultName_("")
addSubtractFieldName_(""),
addSubtractValueStr_(""),
resultName_(""),
calcMode_(ADD)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::calcTypes::add::~add()
Foam::calcTypes::addSubtract::~addSubtract()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::calcTypes::add::init()
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::add::preCalc
void Foam::calcTypes::addSubtract::preCalc
(
const argList& args,
const Time& runTime,
......@@ -212,21 +214,38 @@ void Foam::calcTypes::add::preCalc
)
{
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"))
{
addFieldName_ = args.options()["field"];
addSubtractFieldName_ = args.options()["field"];
calcType_ = FIELD;
}
else if (args.options().found("value"))
{
addValueStr_ = args.options()["value"];
addSubtractValueStr_ = args.options()["value"];
calcType_ = VALUE;
}
else
{
FatalErrorIn("calcTypes::add::preCalc")
<< "add requires either -field or -value option"
FatalErrorIn("calcTypes::addSubtract::preCalc")
<< "addSubtract requires either -field or -value option"
<< nl << exit(FatalError);
}
......@@ -237,7 +256,7 @@ void Foam::calcTypes::add::preCalc
}
void Foam::calcTypes::add::calc
void Foam::calcTypes::addSubtract::calc
(
const argList& args,
const Time& runTime,
......@@ -258,17 +277,17 @@ void Foam::calcTypes::add::calc
{
case FIELD:
{
writeAddFields(runTime, mesh, baseFieldHeader);
writeAddSubtractFields(runTime, mesh, baseFieldHeader);
break;
}
case VALUE:
{
writeAddValues(runTime, mesh, baseFieldHeader);
writeAddSubtractValues(runTime, mesh, baseFieldHeader);
break;
}
default:
{
FatalErrorIn("calcTypes::add::calc")
FatalErrorIn("calcTypes::addSubtract::calc")
<< "unknown calcType " << calcType_ << nl
<< abort(FatalError);
}
......@@ -276,7 +295,7 @@ void Foam::calcTypes::add::calc
}
else
{
FatalErrorIn("calcTypes::add::calc")
FatalErrorIn("calcTypes::addSubtract::calc")
<< "Unable to read base field: " << baseFieldName_
<< nl << exit(FatalError);
}
......
......@@ -23,28 +23,30 @@ License
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Class
Foam::calcTypes::add
Foam::calcTypes::addSubtract
Description
Adds and field or value to base field.
adds/subtracts a field or value to/from a base field.
New field name specified by -resultName option, or automatically as:
<baseFieldName>_plus_<addFieldName>
<baseFieldName>_plus_value
<baseFieldName>_add_<addSubtractFieldName>
<baseFieldName>_add_value
<baseFieldName>_subtract_<addSubtractFieldName>
<baseFieldName>_subtract_value
Example usage:
add p -value 100000 -resultName pAbs
add U -field U0
addSubtract p add -value 100000 -resultName pAbs
addSubtract U subtract -field U0
SourceFiles
add.C
writeAddField.C
writeAddValue.C
addSubtract.C
writeaddSubtractField.C
writeaddSubtractValue.C
\*---------------------------------------------------------------------------*/
#ifndef add_H
#define add_H
#ifndef addSubtract_H
#define addSubtract_H
#include "calcType.H"
......@@ -57,10 +59,10 @@ namespace calcTypes
{
/*---------------------------------------------------------------------------*\
Class add Declaration
Class addSubtract Declaration
\*---------------------------------------------------------------------------*/
class add
class addSubtract
:
public calcType
{
......@@ -72,41 +74,50 @@ public:
VALUE
};
enum calcModes
{
ADD,
SUBTRACT
};
private:
// Private data
//- Name of base field (to add to)
//- Name of base field (to addSubtract to)
word baseFieldName_;
//- Calc type as given by enumerations above
calcTypes calcType_;
//- Name of field to add
word addFieldName_;
//- Name of field to add/subtract
word addSubtractFieldName_;
//- String representation of value to add
string addValueStr_;
//- 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 additions
void writeAddFields
//- Calc and output field addSubtractitions
void writeAddSubtractFields
(
const Time& runTime,
const fvMesh& mesh,
const IOobject& baseFieldHeader
);
//- Calc and output field and value additions
void writeAddValues
//- Calc and output field and value addSubtractitions
void writeAddSubtractValues
(
const Time& runTime,
const fvMesh& mesh,
......@@ -115,10 +126,10 @@ private:
//- Disallow default bitwise copy construct
add(const add&);
addSubtract(const addSubtract&);
//- Disallow default bitwise assignment
void operator=(const add&);
void operator=(const addSubtract&);
protected:
......@@ -150,19 +161,19 @@ protected:
// I-O
//- Write add field
//- Write addSubtract field
template<class Type>
void writeAddField
void writeAddSubtractField
(
const IOobject& baseHeader,
const IOobject& addHeader,
const IOobject& addSubtractHeader,
const fvMesh& mesh,
bool& processed
);
//- Write add value
//- Write addSubtract value
template<class Type>
void writeAddValue
void writeAddSubtractValue
(
const IOobject& baseHeader,
const string& valueStr,
......@@ -174,18 +185,18 @@ protected:
public:
//- Runtime type information
TypeName("add");
TypeName("addSubtract");
// Constructors
//- Construct null
add();
addSubtract();
// Destructor
virtual ~add();
virtual ~addSubtract();
};
......@@ -197,8 +208,8 @@ public:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
# include "writeAddField.C"
# include "writeAddValue.C"
# include "writeAddSubtractField.C"
# include "writeAddSubtractValue.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -25,7 +25,7 @@ License
\*---------------------------------------------------------------------------*/
template<class Type>
void Foam::calcTypes::add::writeAddField
void Foam::calcTypes::addSubtract::writeAddSubtractField
(
const IOobject& baseHeader,
const IOobject& addHeader,
......@@ -33,11 +33,6 @@ void Foam::calcTypes::add::writeAddField
bool& processed
)
{
if (resultName_ == "")
{
resultName_ = baseHeader.name() + "_plus_" + addHeader.name();
}
typedef GeometricField<Type, fvPatchField, volMesh> fieldType;
if
......@@ -46,6 +41,19 @@ void Foam::calcTypes::add::writeAddField
&& 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);
......@@ -65,7 +73,7 @@ void Foam::calcTypes::add::writeAddField
mesh,
IOobject::NO_READ
),
baseField + addField
calcMode_ == ADD ? baseField + addField : baseField - addField
);
newField.write();
}
......
......@@ -25,7 +25,7 @@ License
\*---------------------------------------------------------------------------*/
template<class Type>
void Foam::calcTypes::add::writeAddValue
void Foam::calcTypes::addSubtract::writeAddSubtractValue
(
const IOobject& baseHeader,
const string& valueStr,
......@@ -39,14 +39,21 @@ void Foam::calcTypes::add::writeAddValue
{
if (resultName_ == "")
{
resultName_ = baseHeader.name() + "_plus_value";
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 baseField(baseHeader, mesh);
fieldType newField
(
......@@ -61,8 +68,17 @@ void Foam::calcTypes::add::writeAddValue
);
Info<< " Calculating " << resultName_ << endl;
newField == baseField
+ dimensioned<Type>("value", baseField.dimensions(), value);
if (calcMode_ == ADD)
{
newField == baseField
+ dimensioned<Type>("value", baseField.dimensions(), value);
}
else
{
newField == baseField
- dimensioned<Type>("value", baseField.dimensions(), value);
}
newField.write();
processed = true;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment