mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
adding randomise foamCalc function
This commit is contained in:
@ -6,5 +6,6 @@ field/mag/mag.C
|
|||||||
field/magSqr/magSqr.C
|
field/magSqr/magSqr.C
|
||||||
field/magGrad/magGrad.C
|
field/magGrad/magGrad.C
|
||||||
field/div/div.C
|
field/div/div.C
|
||||||
|
field/randomise/randomise.C
|
||||||
|
|
||||||
LIB = $(FOAM_LIBBIN)/libfoamCalcFunctions
|
LIB = $(FOAM_LIBBIN)/libfoamCalcFunctions
|
||||||
|
|||||||
164
src/postProcessing/foamCalcFunctions/field/randomise/randomise.C
Normal file
164
src/postProcessing/foamCalcFunctions/field/randomise/randomise.C
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
/*---------------------------------------------------------------------------*\
|
||||||
|
========= |
|
||||||
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
|
\\ / O peration |
|
||||||
|
\\ / A nd | Copyright (C) 1991-2008 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 "randomise.H"
|
||||||
|
#include "addToRunTimeSelectionTable.H"
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
namespace Foam
|
||||||
|
{
|
||||||
|
namespace calcTypes
|
||||||
|
{
|
||||||
|
defineTypeNameAndDebug(randomise, 0);
|
||||||
|
addToRunTimeSelectionTable(calcType, randomise, dictionary);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
Foam::calcTypes::randomise::randomise()
|
||||||
|
:
|
||||||
|
calcType()
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
Foam::calcTypes::randomise::~randomise()
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
void Foam::calcTypes::randomise::init()
|
||||||
|
{
|
||||||
|
argList::validArgs.append("randomise");
|
||||||
|
argList::validArgs.append("perturbation");
|
||||||
|
argList::validArgs.append("fieldName1 .. fieldNameN");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Foam::calcTypes::randomise::preCalc
|
||||||
|
(
|
||||||
|
const argList& args,
|
||||||
|
const Time& runTime,
|
||||||
|
const fvMesh& mesh
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (args.additionalArgs().size() < 3)
|
||||||
|
{
|
||||||
|
Info<< nl
|
||||||
|
<< "must specify perturbation magnitude and one"
|
||||||
|
<< "or more fields"
|
||||||
|
<< nl;
|
||||||
|
args.printUsage();
|
||||||
|
FatalError.exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Foam::calcTypes::randomise::calc
|
||||||
|
(
|
||||||
|
const argList& args,
|
||||||
|
const Time& runTime,
|
||||||
|
const fvMesh& mesh
|
||||||
|
)
|
||||||
|
{
|
||||||
|
const stringList& params = args.additionalArgs();
|
||||||
|
const scalar pertMag = readScalar(IStringStream(params[1])());
|
||||||
|
|
||||||
|
Random rand(1234567);
|
||||||
|
|
||||||
|
for (label fieldi=2; fieldi<params.size(); fieldi++)
|
||||||
|
{
|
||||||
|
const word fieldName(params[fieldi]);
|
||||||
|
|
||||||
|
IOobject fieldHeader
|
||||||
|
(
|
||||||
|
fieldName,
|
||||||
|
runTime.timeName(),
|
||||||
|
mesh,
|
||||||
|
IOobject::MUST_READ
|
||||||
|
);
|
||||||
|
|
||||||
|
// Check field exists
|
||||||
|
if (fieldHeader.headerOk())
|
||||||
|
{
|
||||||
|
bool processed = false;
|
||||||
|
|
||||||
|
writeRandomField<vector>
|
||||||
|
(
|
||||||
|
fieldHeader,
|
||||||
|
pertMag,
|
||||||
|
rand,
|
||||||
|
mesh,
|
||||||
|
processed
|
||||||
|
);
|
||||||
|
writeRandomField<sphericalTensor>
|
||||||
|
(
|
||||||
|
fieldHeader,
|
||||||
|
pertMag,
|
||||||
|
rand,
|
||||||
|
mesh,
|
||||||
|
processed
|
||||||
|
);
|
||||||
|
writeRandomField<symmTensor>
|
||||||
|
(
|
||||||
|
fieldHeader,
|
||||||
|
pertMag,
|
||||||
|
rand,
|
||||||
|
mesh,
|
||||||
|
processed
|
||||||
|
);
|
||||||
|
writeRandomField<tensor>
|
||||||
|
(
|
||||||
|
fieldHeader,
|
||||||
|
pertMag,
|
||||||
|
rand,
|
||||||
|
mesh,
|
||||||
|
processed
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!processed)
|
||||||
|
{
|
||||||
|
FatalError
|
||||||
|
<< "Unable to process " << fieldName << nl
|
||||||
|
<< "No call to randomise for fields of type "
|
||||||
|
<< fieldHeader.headerClassName() << nl << nl
|
||||||
|
<< exit(FatalError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Info<< " No " << fieldName << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ************************************************************************* //
|
||||||
141
src/postProcessing/foamCalcFunctions/field/randomise/randomise.H
Normal file
141
src/postProcessing/foamCalcFunctions/field/randomise/randomise.H
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
/*---------------------------------------------------------------------------*\
|
||||||
|
========= |
|
||||||
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
|
\\ / O peration |
|
||||||
|
\\ / A nd | Copyright (C) 1991-2008 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::randomise
|
||||||
|
|
||||||
|
Description
|
||||||
|
Adds a random component to a field, with a given perturbation magnitude.
|
||||||
|
|
||||||
|
SourceFiles
|
||||||
|
randomise.C
|
||||||
|
|
||||||
|
\*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#ifndef randomise_H
|
||||||
|
#define randomise_H
|
||||||
|
|
||||||
|
#include "calcType.H"
|
||||||
|
#include "Random.H"
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
namespace Foam
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace calcTypes
|
||||||
|
{
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------*\
|
||||||
|
Class randomise Declaration
|
||||||
|
\*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
class randomise
|
||||||
|
:
|
||||||
|
public calcType
|
||||||
|
{
|
||||||
|
// Private Member Functions
|
||||||
|
|
||||||
|
//- Disallow default bitwise copy construct
|
||||||
|
randomise(const randomise&);
|
||||||
|
|
||||||
|
//- Disallow default bitwise assignment
|
||||||
|
void operator=(const randomise&);
|
||||||
|
|
||||||
|
|
||||||
|
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 component fields
|
||||||
|
template<class Type>
|
||||||
|
void writeRandomField
|
||||||
|
(
|
||||||
|
const IOobject& header,
|
||||||
|
const scalar pertMag,
|
||||||
|
Random& rand,
|
||||||
|
const fvMesh& mesh,
|
||||||
|
bool& processed
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
//- Runtime type information
|
||||||
|
TypeName("randomise");
|
||||||
|
|
||||||
|
|
||||||
|
// Constructors
|
||||||
|
|
||||||
|
//- Construct null
|
||||||
|
randomise();
|
||||||
|
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
|
||||||
|
virtual ~randomise();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
} // End namespace calcTypes
|
||||||
|
} // End namespace Foam
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
#ifdef NoRepository
|
||||||
|
# include "writeRandomField.C"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// ************************************************************************* //
|
||||||
@ -0,0 +1,73 @@
|
|||||||
|
/*---------------------------------------------------------------------------*\
|
||||||
|
========= |
|
||||||
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
|
\\ / O peration |
|
||||||
|
\\ / A nd | Copyright (C) 1991-2008 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::randomise::writeRandomField
|
||||||
|
(
|
||||||
|
const IOobject& header,
|
||||||
|
const scalar pertMag,
|
||||||
|
Random& rand,
|
||||||
|
const fvMesh& mesh,
|
||||||
|
bool& processed
|
||||||
|
)
|
||||||
|
{
|
||||||
|
typedef GeometricField<Type, fvPatchField, volMesh> fieldType;
|
||||||
|
|
||||||
|
if (header.headerClassName() == fieldType::typeName)
|
||||||
|
{
|
||||||
|
Info<< " Reading " << header.name() << endl;
|
||||||
|
fieldType field(header, mesh);
|
||||||
|
|
||||||
|
forAll(field, cellI)
|
||||||
|
{
|
||||||
|
Type rndPert;
|
||||||
|
rand.randomise(rndPert);
|
||||||
|
rndPert = 2.0*rndPert - pTraits<Type>::one;
|
||||||
|
rndPert /= mag(rndPert);
|
||||||
|
field[cellI] += pertMag*rndPert;
|
||||||
|
}
|
||||||
|
|
||||||
|
fieldType randomisedField
|
||||||
|
(
|
||||||
|
IOobject
|
||||||
|
(
|
||||||
|
header.name() + "Random",
|
||||||
|
mesh.time().timeName(),
|
||||||
|
mesh,
|
||||||
|
IOobject::NO_READ
|
||||||
|
),
|
||||||
|
field
|
||||||
|
);
|
||||||
|
|
||||||
|
Info<< " Writing " << header.name() << "Random" << endl;
|
||||||
|
randomisedField.write();
|
||||||
|
|
||||||
|
processed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
Reference in New Issue
Block a user